gdext_gen/gdext/
libs.rs

1//! Module for the generation of the libraries section of the `.gdextension` file.
2
3#[allow(unused_imports)]
4use std::path::{Path, PathBuf};
5
6use super::GDExtension;
7use crate::{
8    args::BaseDirectory,
9    features::{
10        arch::Architecture,
11        mode::Mode,
12        sys::{System, WindowsABI},
13        target::Target,
14    },
15};
16
17impl GDExtension {
18    /// Generates the libraries section of the [`GDExtension`].
19    ///
20    /// # Parameters
21    ///
22    /// * `base_dir` - The base directory to use for the paths of the libraries in the `.gdextension` file.
23    /// * `lib_name` - Name of the library crate that is being compiled. It can be retrieved with the environmental variable: "`CARGO_PKG_NAME"`, but it must be turned into snake_case.
24    /// * `windows_abi` - Env ABI used to build for `Windows`.
25    /// * `target_dir` - Path to the build folder (specified inside the variable `[build] target-dir` of `.cargo/config.toml`) **relative** to the *`base_dir`*. For example, if the `base_dir` is [`ProjectFolder`](crate::args::BaseDirectory::ProjectFolder), the path for `Godot` would be `"res://path/to/dep"` and the path provided must be `"path/to/build"`. If the path contains non valid Unicode, it will be stored calling [`to_string_lossy`](Path::to_string_lossy).
26    ///
27    /// # Returns
28    ///
29    /// The same [`GDExtension`] mutable reference it was passed to it.
30    pub fn generate_libs(
31        &mut self,
32        base_dir: BaseDirectory,
33        lib_name: &str,
34        windows_abi: WindowsABI,
35        target_dir: PathBuf,
36    ) -> &mut Self {
37        for system in System::get_systems(windows_abi) {
38            for architecture in system.get_architectures() {
39                for mode in Mode::get_modes() {
40                    let target = Target(system, mode, architecture);
41                    self.libraries.insert(
42                        target.get_godot_target(),
43                        // If the Architecture is Generic, it takes the path it would be if no target was specified.
44                        if target.2 == Architecture::Generic {
45                            format!(
46                                "{}{}",
47                                base_dir.as_str(),
48                                target_dir
49                                    .join(target.1.get_rust_name())
50                                    .join(target.0.get_lib_export_name(lib_name))
51                                    .to_string_lossy()
52                                    .replace('\\', "/")
53                            )
54                        } else {
55                            format!(
56                                "{}{}",
57                                base_dir.as_str(),
58                                target_dir
59                                    .join(target.get_rust_target_triple())
60                                    .join(target.1.get_rust_name())
61                                    .join(target.0.get_lib_export_name(lib_name))
62                                    .to_string_lossy()
63                                    .replace('\\', "/")
64                            )
65                        }
66                        .into(),
67                    );
68                }
69            }
70        }
71
72        self
73    }
74}