Skip to content

Commit d03e73d

Browse files
authored
Merge pull request #730 from lilizoey/fix/rustc-check-cfg
Emit `rustc-check-cfg`
2 parents 47fb204 + f37f4b0 commit d03e73d

File tree

13 files changed

+86
-6
lines changed

13 files changed

+86
-6
lines changed

.github/workflows/full-ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ jobs:
311311
os: ubuntu-20.04
312312
artifact-name: linux-nightly
313313
godot-binary: godot.linuxbsd.editor.dev.x86_64
314-
rust-extra-args: --features godot/api-custom,godot/experimental-threads,godot/serde,codegen-full-experimental
314+
rust-extra-args: --features itest/experimental-threads,itest/codegen-full-experimental,godot/api-custom,godot/serde
315315

316316
- name: linux-release
317317
os: ubuntu-20.04

.github/workflows/minimal-ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ jobs:
154154
os: ubuntu-20.04
155155
artifact-name: linux-nightly
156156
godot-binary: godot.linuxbsd.editor.dev.x86_64
157-
rust-extra-args: --features godot/api-custom,godot/experimental-threads,godot/serde,codegen-full-experimental
157+
rust-extra-args: --features itest/experimental-threads,itest/codegen-full-experimental,godot/api-custom,godot/serde
158158

159159
# Linux compat
160160

godot-bindings/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ regex = { version = "1.5.5", default-features = false, features = ["std", "unico
3939
[package.metadata.docs.rs]
4040
features = ["experimental-godot-api"]
4141
rustdoc-args = ["--cfg", "published_docs"]
42+
43+
[lints.rust]
44+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(published_docs)'] }

godot-bindings/src/lib.rs

+55-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,13 @@ pub use prebuilt::*;
130130
// ----------------------------------------------------------------------------------------------------------------------------------------------
131131
// Common
132132

133-
const NEXT_MINOR_VERSION: u8 = 3;
133+
// List of minor versions with the highest known patch number for each.
134+
//
135+
// We could have this just be a list of patch numbers, letting the index be the minor version. However it's more readable to include the
136+
// minor versions as well.
137+
//
138+
// Note that when the patch version is `0`, then the patch number is not included in Godot versioning, i.e. `4.1.0` is displayed as `4.1`.
139+
const HIGHEST_PATCH_VERSIONS: &[(u8, u8)] = &[(0, 4), (1, 4), (2, 2), (3, 0)];
134140

135141
pub fn clear_dir(dir: &Path, watch: &mut StopWatch) {
136142
if dir.exists() {
@@ -148,8 +154,10 @@ pub fn emit_godot_version_cfg() {
148154
..
149155
} = get_godot_version();
150156

157+
// This could also be done as `KNOWN_API_VERSIONS.len() - 1`, but this is more explicit.
158+
let max = HIGHEST_PATCH_VERSIONS.last().unwrap().0;
159+
151160
// Start at 1; checking for "since/before 4.0" makes no sense
152-
let max = NEXT_MINOR_VERSION;
153161
for m in 1..=minor {
154162
println!(r#"cargo:rustc-cfg=since_api="{major}.{m}""#);
155163
}
@@ -166,6 +174,28 @@ pub fn emit_godot_version_cfg() {
166174
} else {
167175
println!(r#"cargo:rustc-cfg=gdextension_exact_api="{major}.{minor}""#);
168176
}
177+
178+
// Emit `rustc-check-cfg` so cargo doesn't complain when we use the cfgs.
179+
for (minor, highest_patch) in HIGHEST_PATCH_VERSIONS {
180+
if *minor > 0 {
181+
println!(r#"cargo::rustc-check-cfg=cfg(since_api, values("4.{minor}"))"#);
182+
println!(r#"cargo::rustc-check-cfg=cfg(before_api, values("4.{minor}"))"#);
183+
}
184+
185+
println!(r#"cargo::rustc-check-cfg=cfg(gdextension_minor_api, values("4.{minor}"))"#);
186+
187+
for patch in 0..=*highest_patch {
188+
if patch == 0 {
189+
println!(
190+
r#"cargo::rustc-check-cfg=cfg(gdextension_exact_api, values("4.{minor}"))"#
191+
);
192+
} else {
193+
println!(
194+
r#"cargo::rustc-check-cfg=cfg(gdextension_exact_api, values("4.{minor}.{patch}"))"#
195+
);
196+
}
197+
}
198+
}
169199
}
170200

171201
// Function for safely removal of build directory. Workaround for errors happening during CI builds:
@@ -189,6 +219,29 @@ pub fn remove_dir_all_reliable(path: &Path) {
189219
}
190220
}
191221
}
222+
223+
// Duplicates code from `make_gdext_build_struct` in `godot-codegen/generator/gdext_build_struct.rs`.
224+
pub fn before_api(major_minor: &str) -> bool {
225+
let mut parts = major_minor.split('.');
226+
let queried_major = parts
227+
.next()
228+
.unwrap()
229+
.parse::<u8>()
230+
.expect("invalid major version");
231+
let queried_minor = parts
232+
.next()
233+
.unwrap()
234+
.parse::<u8>()
235+
.expect("invalid minor version");
236+
assert_eq!(queried_major, 4, "major version must be 4");
237+
let godot_version = get_godot_version();
238+
godot_version.minor < queried_minor
239+
}
240+
241+
pub fn since_api(major_minor: &str) -> bool {
242+
!before_api(major_minor)
243+
}
244+
192245
//
193246
// pub fn write_module_file(path: &Path) {
194247
// let code = quote! {

godot-cell/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ proptest = { version = "1.4.0", optional = true }
1717
[package.metadata.docs.rs]
1818
features = ["experimental-godot-api"]
1919
rustdoc-args = ["--cfg", "published_docs"]
20+
21+
[lints.rust]
22+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(published_docs)'] }

godot-codegen/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ godot-bindings = { path = "../godot-bindings" } # emit_godot_version_cfg
3838
features = ["experimental-godot-api"]
3939
rustdoc-args = ["--cfg", "published_docs"]
4040

41+
[lints.rust]
42+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(published_docs)'] }
4143

4244
# To check formatter: Disabled below, since it pulls in too many dependencies during `cargo test` but is not really used.
4345
# Dev-dependencies cannot be optional and feature-gated. Enable manually when needed.
@@ -48,3 +50,4 @@ rustdoc-args = ["--cfg", "published_docs"]
4850
#
4951
#[dev-dependencies]
5052
#criterion = "0.5"
53+

godot-codegen/src/generator/gdext_build_struct.rs

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ pub fn make_gdext_build_struct(header: &GodotApiVersion) -> TokenStream {
5353
(version.major as u8, version.minor as u8, version.patch as u8)
5454
}
5555

56+
// Duplicates code from `before_api` in `godot-bindings/lib.rs`.
57+
5658
/// For a string `"4.x"`, returns `true` if the current Godot version is strictly less than 4.x.
5759
///
5860
/// Runtime equivalent of `#[cfg(before_api = "4.x")]`.

godot-core/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ godot-codegen = { path = "../godot-codegen" }
4545
[package.metadata.docs.rs]
4646
features = ["experimental-godot-api"]
4747
rustdoc-args = ["--cfg", "published_docs"]
48+
49+
[lints.rust]
50+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(published_docs)'] }

godot-ffi/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ godot-codegen = { path = "../godot-codegen" }
3737
[package.metadata.docs.rs]
3838
features = ["experimental-godot-api"]
3939
rustdoc-args = ["--cfg", "published_docs"]
40+
41+
[lints.rust]
42+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(published_docs)'] }

godot-macros/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ godot-bindings = { path = "../godot-bindings" } # emit_godot_version_cfg
3131
[package.metadata.docs.rs]
3232
features = ["experimental-godot-api"]
3333
rustdoc-args = ["--cfg", "published_docs"]
34+
35+
[lints.rust]
36+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(published_docs)'] }

godot/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ godot-macros = { path = "../godot-macros" }
3434
[package.metadata.docs.rs]
3535
features = ["experimental-godot-api"]
3636
rustdoc-args = ["--cfg", "published_docs"]
37+
38+
[lints.rust]
39+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(published_docs)'] }

itest/rust/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ crate-type = ["cdylib"]
1212
[features]
1313
default = []
1414
codegen-full-experimental = ["godot/codegen-full", "godot/experimental-godot-api"]
15+
experimental-threads = ["godot/experimental-threads"]
1516
serde = ["dep:serde", "dep:serde_json", "godot/serde"]
1617

1718
# Do not add features here that are 1:1 forwarded to the `godot` crate, unless they are needed by itest itself.

itest/rust/build.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,11 @@ fn collect_inputs() -> Vec<Input> {
148148
push!(inputs; PackedStringArray, PackedStringArray, PackedStringArray(), PackedStringArray::new());
149149
push!(inputs; PackedVector2Array, PackedVector2Array, PackedVector2Array(), PackedVector2Array::new());
150150
push!(inputs; PackedVector3Array, PackedVector3Array, PackedVector3Array(), PackedVector3Array::new());
151-
#[cfg(since_api = "4.3")]
152-
push!(inputs; PackedVector4Array, PackedVector4Array, PackedVector4Array(), PackedVector4Array::new());
151+
// This is being run in a build script at the same time as other build-scripts, so the `rustc-cfg` directives haven't been run for this
152+
// build-script. This means that `#[cfg(since_api = "4.3")]` wouldn't do anything.
153+
if godot_bindings::since_api("4.3") {
154+
push!(inputs; PackedVector4Array, PackedVector4Array, PackedVector4Array(), PackedVector4Array::new());
155+
}
153156
push!(inputs; PackedColorArray, PackedColorArray, PackedColorArray(), PackedColorArray::new());
154157

155158
push_newtype!(inputs; int, NewI64(i64), -922337203685477580);

0 commit comments

Comments
 (0)