gdext_gen/features/sys.rs
1//! Module for the [`System`] a `Godot` game using `Rust GDExtension` can be compiled for.
2
3use super::arch::Architecture;
4
5/// System to compile the `Godot` game and the `Rust GDExtension` for.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum System {
8 /// Android system.
9 Android,
10 /// iOS system.
11 IOS,
12 /// Linux system.
13 Linux,
14 /// MacOS system.
15 MacOS,
16 /// Web browser.
17 Web,
18 /// Windows system.
19 Windows(WindowsABI),
20}
21
22impl System {
23 /// Gets all [`System`]s available.
24 ///
25 /// # Parameters
26 ///
27 /// * `windows_abi` - Env and ABI used to build for `Windows`.
28 ///
29 /// # Returns
30 ///
31 /// An array with all available [`System`]s.
32 pub fn get_systems(windows_abi: WindowsABI) -> [Self; 6] {
33 [
34 Self::Android,
35 Self::IOS,
36 Self::Linux,
37 Self::MacOS,
38 Self::Web,
39 Self::Windows(windows_abi),
40 ]
41 }
42
43 /// Gets all [`Architecture`]s available for a [`System`].
44 ///
45 /// # Returns
46 ///
47 /// A [`Vec`] with all available [`Architecture`] for the [`System`].
48 pub fn get_architectures(&self) -> Vec<Architecture> {
49 match self {
50 Self::Android => vec![
51 Architecture::Generic,
52 Architecture::Armv7,
53 Architecture::Arm64,
54 Architecture::X86_32,
55 Architecture::X86_64,
56 ],
57 Self::IOS => vec![Architecture::Generic, Architecture::Arm64],
58 Self::Linux => vec![
59 Architecture::Generic,
60 Architecture::Arm64,
61 Architecture::Rv64,
62 Architecture::X86_64,
63 ],
64 Self::MacOS => vec![
65 Architecture::Generic,
66 Architecture::Arm64,
67 Architecture::X86_64,
68 ],
69 Self::Web => vec![Architecture::Generic, Architecture::Wasm32],
70 Self::Windows(_) => vec![
71 Architecture::Generic,
72 Architecture::Arm64,
73 Architecture::X86_32,
74 Architecture::X86_64,
75 ],
76 }
77 }
78
79 /// Gets the name of the [`System`] in lowercase.
80 ///
81 /// # Returns
82 ///
83 /// The name of the [`System`] in lowercase.
84 pub fn get_name(&self) -> &'static str {
85 match self {
86 Self::Android => "android",
87 Self::IOS => "ios",
88 Self::Linux => "linux",
89 Self::MacOS => "macos",
90 Self::Web => "web",
91 Self::Windows(_) => "windows",
92 }
93 }
94
95 /// Gets the name of the compiled library for the given system.
96 ///
97 /// # Parameters
98 ///
99 /// * `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.
100 ///
101 /// # Returns
102 ///
103 /// The name of the file that's going to be compiled.
104 pub fn get_lib_export_name(&self, lib_name: &str) -> String {
105 format!(
106 "{}{}.{}",
107 match self {
108 // The `godot-rust` book has android libraries without the lib in front, but it may be an error.
109 Self::IOS | Self::Linux | Self::MacOS => "lib",
110 Self::Android | Self::Windows(_) | Self::Web => "",
111 },
112 lib_name,
113 match self {
114 Self::Android | Self::Linux => "so",
115 Self::IOS => "ios.framework",
116 Self::MacOS => "dylib",
117 Self::Web => "wasm",
118 Self::Windows(_) => "dll",
119 }
120 )
121 }
122}
123
124/// Env and ABI used to build the `Rust GDExtension` for `Windows`.
125#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
126pub enum WindowsABI {
127 /// Microsoft Visual C++ compiler.
128 #[default]
129 MSVC,
130 /// The `MinGW` compiler (`MSYS2` port of `GCC`).
131 MinGW,
132 /// Similar to `MinGW` but using `UCRT` as the runtime and various `LLVM` tools/libraries instead of `GCC/Binutils`. More information: <https://doc.rust-lang.org/rustc/platform-support/pc-windows-gnullvm.html>
133 LLVM,
134}
135
136impl WindowsABI {
137 /// Gets the name of the [`WindowsABI`] used in `Rust` target triples.
138 ///
139 /// # Returns
140 ///
141 /// The name of the [`WindowsABI`] for the `Rust` target triple.
142 pub fn get_rust_name(&self) -> &'static str {
143 match self {
144 Self::MSVC => "msvc",
145 Self::MinGW => "gnu",
146 Self::LLVM => "gnullvm",
147 }
148 }
149}