-
-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathlib.rs
163 lines (130 loc) · 5.27 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Copyright (c) godot-rust; Bromeon and contributors.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
pub(crate) mod watch;
use std::path::Path;
pub use watch::StopWatch;
// Note: we cannot prevent both `custom-godot` and `prebuilt-godot` from being specified; see Cargo.toml for more information.
#[cfg(not(any(feature = "custom-godot", feature = "prebuilt-godot")))]
compile_error!(
"At least one of `custom-godot` or `prebuilt-godot` must be specified (none given)."
);
// This is outside of `godot_version` to allow us to use it even when we don't have the `custom-godot`
// feature enabled.
#[derive(Eq, PartialEq, Debug)]
pub struct GodotVersion {
/// the original string (trimmed, stripped of text around)
pub full_string: String,
pub major: u8,
pub minor: u8,
/// 0 if none
pub patch: u8,
/// alpha|beta|dev|stable
pub status: String,
/// Git revision 'custom_build.{rev}' or '{official}.rev', if available
pub custom_rev: Option<String>,
}
// ----------------------------------------------------------------------------------------------------------------------------------------------
// Regenerate all files
// This file is explicitly included in unit tests. Needs regex dependency.
#[cfg(test)]
mod godot_version;
#[cfg(feature = "custom-godot")]
#[path = ""]
mod custom {
use super::*;
pub(crate) mod godot_exe;
pub(crate) mod godot_version;
pub(crate) mod header_gen;
pub fn load_gdextension_json(watch: &mut StopWatch) -> String {
godot_exe::load_gdextension_json(watch)
}
pub fn write_gdextension_headers(h_path: &Path, rs_path: &Path, watch: &mut StopWatch) {
godot_exe::write_gdextension_headers(h_path, rs_path, false, watch);
}
#[cfg(feature = "custom-godot-extheader")]
pub fn write_gdextension_headers_from_c(h_path: &Path, rs_path: &Path, watch: &mut StopWatch) {
godot_exe::write_gdextension_headers(h_path, rs_path, true, watch);
}
pub(crate) fn get_godot_version() -> GodotVersion {
godot_exe::read_godot_version(&godot_exe::locate_godot_binary())
}
}
#[cfg(feature = "custom-godot")]
pub use custom::*;
// ----------------------------------------------------------------------------------------------------------------------------------------------
// Reuse existing files
#[cfg(not(feature = "custom-godot"))]
#[path = ""]
mod prebuilt {
use super::*;
pub fn load_gdextension_json(_watch: &mut StopWatch) -> &'static str {
godot4_prebuilt::load_gdextension_json()
}
pub fn write_gdextension_headers(h_path: &Path, rs_path: &Path, watch: &mut StopWatch) {
// Note: prebuilt artifacts just return a static str.
let h_contents = godot4_prebuilt::load_gdextension_header_h();
std::fs::write(h_path, h_contents)
.unwrap_or_else(|e| panic!("failed to write gdextension_interface.h: {e}"));
watch.record("write_header_h");
let rs_contents = godot4_prebuilt::load_gdextension_header_rs();
std::fs::write(rs_path, rs_contents)
.unwrap_or_else(|e| panic!("failed to write gdextension_interface.rs: {e}"));
watch.record("write_header_rs");
}
pub(crate) fn get_godot_version() -> GodotVersion {
let version: Vec<&str> = godot4_prebuilt::GODOT_VERSION
.split('.')
.collect::<Vec<_>>();
GodotVersion {
full_string: godot4_prebuilt::GODOT_VERSION.into(),
major: version[0].parse().unwrap(),
minor: version[1].parse().unwrap(),
patch: version
.get(2)
.and_then(|patch| patch.parse().ok())
.unwrap_or(0),
status: "stable".into(),
custom_rev: None,
}
}
}
#[cfg(not(feature = "custom-godot"))]
pub use prebuilt::*;
// ----------------------------------------------------------------------------------------------------------------------------------------------
// Common
const NEXT_MINOR_VERSION: u8 = 3;
pub fn clear_dir(dir: &Path, watch: &mut StopWatch) {
if dir.exists() {
std::fs::remove_dir_all(dir).unwrap_or_else(|e| panic!("failed to delete dir: {e}"));
watch.record("delete_gen_dir");
}
std::fs::create_dir_all(dir).unwrap_or_else(|e| panic!("failed to create dir: {e}"));
}
pub fn emit_godot_version_cfg() {
let GodotVersion {
major,
minor,
patch,
..
} = get_godot_version();
// Start at 1; checking for "since/before 4.0" makes no sense
let max = NEXT_MINOR_VERSION;
for m in 1..=minor {
println!(r#"cargo:rustc-cfg=since_api="{major}.{m}""#);
}
for m in minor + 1..=max {
println!(r#"cargo:rustc-cfg=before_api="{major}.{m}""#);
}
// The below configuration keys are very rarely needed and should generally not be used.
println!(r#"cargo:rustc-cfg=gdextension_minor_api="{major}.{minor}""#);
// Godot drops the patch version if it is 0.
if patch != 0 {
println!(r#"cargo:rustc-cfg=gdextension_exact_api="{major}.{minor}.{patch}""#);
} else {
println!(r#"cargo:rustc-cfg=gdextension_exact_api="{major}.{minor}""#);
}
}