Skip to content

Commit dcf5951

Browse files
committed
Configure godot-bindings with new templating
Changes: - Cargo features now include all patch versions. - #[cfg(before/since_patch_api = "4.3.2")] added. - #[cfg(gdextension_exact_api)] removed. - #[cfg(gdextension_minor_api)] removed.
1 parent df702e5 commit dcf5951

File tree

3 files changed

+172
-77
lines changed

3 files changed

+172
-77
lines changed

godot-bindings/Cargo.toml

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,48 @@ categories = ["game-engines", "graphics"]
1212
# requiring no-default-features), so we unfortunately still need to depend on prebuilt and just ignore it.
1313
# The artifact generator explicitly excludes that though (to avoid a quasi-circular dependency back to its repo).
1414
[features]
15+
# [version-sync] [[
16+
# [repeat] past
17+
# [fmt] api-$kebabVersion = ["dep:prebuilt-$kebabVersion"]
1518
api-4-0 = ["dep:prebuilt-4-0"]
19+
api-4-0-1 = ["dep:prebuilt-4-0-1"]
20+
api-4-0-2 = ["dep:prebuilt-4-0-2"]
21+
api-4-0-3 = ["dep:prebuilt-4-0-3"]
22+
api-4-0-4 = ["dep:prebuilt-4-0-4"]
1623
api-4-1 = ["dep:prebuilt-4-1"]
24+
api-4-1-1 = ["dep:prebuilt-4-1-1"]
25+
api-4-1-2 = ["dep:prebuilt-4-1-2"]
26+
api-4-1-3 = ["dep:prebuilt-4-1-3"]
27+
api-4-1-4 = ["dep:prebuilt-4-1-4"]
28+
# ]]
29+
30+
# [version-sync] [[
31+
# [repeat] current.minor
32+
# [fmt] default = ["dep:prebuilt-$kebabVersion"]
1733
default = ["dep:prebuilt-4-2"]
34+
# ]]
35+
1836
api-custom = ["dep:bindgen", "dep:regex", "dep:which"]
1937
api-custom-extheader = []
2038

21-
2239
[dependencies]
23-
prebuilt-4-0 = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "4.0.4" }
24-
prebuilt-4-1 = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "4.1.4" }
25-
prebuilt-4-2 = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "4.2.2" }
40+
# [version-sync] [[
41+
# [repeat] past+current
42+
# [fmt] prebuilt-$kebabVersion = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "$dotVersion" }
43+
prebuilt-4-0 = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "4.0" }
44+
prebuilt-4-0-1 = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "4.0.1" }
45+
prebuilt-4-0-2 = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "4.0.2" }
46+
prebuilt-4-0-3 = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "4.0.3" }
47+
prebuilt-4-0-4 = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "4.0.4" }
48+
prebuilt-4-1 = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "4.1" }
49+
prebuilt-4-1-1 = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "4.1.1" }
50+
prebuilt-4-1-2 = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "4.1.2" }
51+
prebuilt-4-1-3 = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "4.1.3" }
52+
prebuilt-4-1-4 = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "4.1.4" }
53+
prebuilt-4-2 = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "4.2" }
54+
prebuilt-4-2-1 = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "4.2.1" }
55+
prebuilt-4-2-2 = { optional = true, package = "godot4-prebuilt", git = "https://github.com/godot-rust/godot4-prebuilt", branch = "4.2.2" }
56+
# ]]
2657

2758
# Do not use bindgen 0.69, it contains regression that forces recompilation of code.
2859
bindgen = { optional = true, version = "0.68", default-features = false, features = ["runtime"] }

godot-bindings/src/import.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (c) godot-rust; Bromeon and contributors.
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
6+
*/
7+
8+
//! Versions to be updated whenever Godot releases a new patch version we support.
9+
//!
10+
//! This file contains several templating comments, who are substituted by the machinery itest/repo-tweak.
11+
//! When modifying those, make sure to rerun.
12+
13+
/// All stable Godot releases _and_ upcoming next minor release.
14+
pub const ALL_VERSIONS: &[(u8, u8, u8)] = &[
15+
// [version-sync] [[
16+
// [repeat] past+current+future
17+
// [fmt] \t$triple,
18+
(4, 0, 0),
19+
(4, 0, 1),
20+
(4, 0, 2),
21+
(4, 0, 3),
22+
(4, 0, 4),
23+
(4, 1, 0),
24+
(4, 1, 1),
25+
(4, 1, 2),
26+
(4, 1, 3),
27+
(4, 1, 4),
28+
(4, 2, 0),
29+
(4, 2, 1),
30+
(4, 2, 2),
31+
(4, 3, 0),
32+
// ]]
33+
];
34+
35+
// [version-sync] [[
36+
// [repeat] past+current
37+
// [fmt] #[cfg(feature = "api-$kebabVersion")]\npub use prebuilt_$snakeVersion as godot4_prebuilt;\n
38+
#[cfg(feature = "api-4-0")]
39+
pub use prebuilt_4_0 as godot4_prebuilt;
40+
41+
#[cfg(feature = "api-4-0-1")]
42+
pub use prebuilt_4_0_1 as godot4_prebuilt;
43+
44+
#[cfg(feature = "api-4-0-2")]
45+
pub use prebuilt_4_0_2 as godot4_prebuilt;
46+
47+
#[cfg(feature = "api-4-0-3")]
48+
pub use prebuilt_4_0_3 as godot4_prebuilt;
49+
50+
#[cfg(feature = "api-4-0-4")]
51+
pub use prebuilt_4_0_4 as godot4_prebuilt;
52+
53+
#[cfg(feature = "api-4-1")]
54+
pub use prebuilt_4_1 as godot4_prebuilt;
55+
56+
#[cfg(feature = "api-4-1-1")]
57+
pub use prebuilt_4_1_1 as godot4_prebuilt;
58+
59+
#[cfg(feature = "api-4-1-2")]
60+
pub use prebuilt_4_1_2 as godot4_prebuilt;
61+
62+
#[cfg(feature = "api-4-1-3")]
63+
pub use prebuilt_4_1_3 as godot4_prebuilt;
64+
65+
#[cfg(feature = "api-4-1-4")]
66+
pub use prebuilt_4_1_4 as godot4_prebuilt;
67+
68+
#[cfg(feature = "api-4-2")]
69+
pub use prebuilt_4_2 as godot4_prebuilt;
70+
71+
#[cfg(feature = "api-4-2-1")]
72+
pub use prebuilt_4_2_1 as godot4_prebuilt;
73+
74+
#[cfg(feature = "api-4-2-2")]
75+
pub use prebuilt_4_2_2 as godot4_prebuilt;
76+
77+
// ]]
78+
79+
// If none of the api-* features are provided, use default prebuilt version (typically latest Godot stable release).
80+
81+
// [version-sync] [[
82+
// [repeat] past+current+future
83+
// [fmt] \tfeature = "api-$kebabVersion",
84+
// [pre] #[cfg(not(any(
85+
// [post] \tfeature = "api-custom",\n)))]
86+
#[cfg(not(any(
87+
feature = "api-4-0",
88+
feature = "api-4-0-1",
89+
feature = "api-4-0-2",
90+
feature = "api-4-0-3",
91+
feature = "api-4-0-4",
92+
feature = "api-4-1",
93+
feature = "api-4-1-1",
94+
feature = "api-4-1-2",
95+
feature = "api-4-1-3",
96+
feature = "api-4-1-4",
97+
feature = "api-4-2",
98+
feature = "api-4-2-1",
99+
feature = "api-4-2-2",
100+
feature = "api-4-3",
101+
feature = "api-custom",
102+
)))]
103+
// ]]
104+
105+
// [version-sync] [[
106+
// [repeat] current.minor
107+
// [fmt] pub use prebuilt_$snakeVersion as godot4_prebuilt;
108+
pub use prebuilt_4_2 as godot4_prebuilt;
109+
// ]]

godot-bindings/src/lib.rs

Lines changed: 28 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,7 @@ use std::path::Path;
1111

1212
pub use watch::StopWatch;
1313

14-
#[cfg(feature = "api-4-0")]
15-
use prebuilt_4_0 as godot4_prebuilt;
16-
#[cfg(feature = "api-4-1")]
17-
use prebuilt_4_1 as godot4_prebuilt;
18-
19-
// If none of the api-* features are provided, use default prebuilt version (typically latest Godot stable release).
20-
#[cfg(not(any(
21-
feature = "api-4-0", //
22-
feature = "api-4-1", //
23-
feature = "api-custom", //
24-
)))]
25-
use prebuilt_4_2 as godot4_prebuilt;
14+
mod import;
2615

2716
// This is outside of `godot_version` to allow us to use it even when we don't have the `api-custom`
2817
// feature enabled.
@@ -45,7 +34,7 @@ pub struct GodotVersion {
4534
}
4635

4736
// ----------------------------------------------------------------------------------------------------------------------------------------------
48-
// Regenerate all files
37+
// Custom mode: Regenerate all files
4938

5039
// This file is explicitly included in unit tests. Needs regex dependency.
5140
#[cfg(test)]
@@ -82,12 +71,13 @@ mod custom {
8271
pub use custom::*;
8372

8473
// ----------------------------------------------------------------------------------------------------------------------------------------------
85-
// Reuse existing files
74+
// Prebuilt mode: Reuse existing files
8675

8776
#[cfg(not(feature = "api-custom"))]
8877
#[path = ""]
8978
mod prebuilt {
9079
use super::*;
80+
use crate::import::godot4_prebuilt;
9181

9282
pub fn load_gdextension_json(_watch: &mut StopWatch) -> &'static str {
9383
godot4_prebuilt::load_gdextension_json()
@@ -130,14 +120,6 @@ pub use prebuilt::*;
130120
// ----------------------------------------------------------------------------------------------------------------------------------------------
131121
// Common
132122

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)];
140-
141123
pub fn clear_dir(dir: &Path, watch: &mut StopWatch) {
142124
if dir.exists() {
143125
remove_dir_all_reliable(dir);
@@ -146,54 +128,43 @@ pub fn clear_dir(dir: &Path, watch: &mut StopWatch) {
146128
std::fs::create_dir_all(dir).unwrap_or_else(|e| panic!("failed to create dir: {e}"));
147129
}
148130

131+
/// Emit the `cfg` flags for the current Godot version. Allows rustc to know about valid `cfg` values.
149132
pub fn emit_godot_version_cfg() {
133+
// This could also be done as `KNOWN_API_VERSIONS.len() - 1`, but this is more explicit.
134+
let all_versions = import::ALL_VERSIONS;
135+
136+
// Emit `rustc-check-cfg` for all minor versions (patch .0), so Cargo doesn't complain when we use the #[cfg]s.
137+
for (_, minor, patch) in all_versions.iter().copied() {
138+
if minor > 0 && patch == 0 {
139+
println!(r#"cargo:rustc-check-cfg=cfg(since_api, values("4.{minor}"))"#);
140+
println!(r#"cargo:rustc-check-cfg=cfg(before_api, values("4.{minor}"))"#);
141+
}
142+
}
143+
150144
let GodotVersion {
151-
major,
145+
major: _,
152146
minor,
153147
patch,
154148
..
155149
} = get_godot_version();
156150

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-
151+
// Emit `rustc-cfg` dependent on current API version.
160152
// Start at 1; checking for "since/before 4.0" makes no sense
153+
let upcoming_minor = all_versions.last().unwrap().1;
161154
for m in 1..=minor {
162-
println!(r#"cargo:rustc-cfg=since_api="{major}.{m}""#);
155+
println!(r#"cargo:rustc-cfg=since_api="4.{m}""#);
163156
}
164-
for m in minor + 1..=max {
165-
println!(r#"cargo:rustc-cfg=before_api="{major}.{m}""#);
157+
for m in minor + 1..=upcoming_minor {
158+
println!(r#"cargo:rustc-cfg=before_api="4.{m}""#);
166159
}
167160

168161
// The below configuration keys are very rarely needed and should generally not be used.
169-
println!(r#"cargo:rustc-cfg=gdextension_minor_api="{major}.{minor}""#);
170-
171-
// Godot drops the patch version if it is 0.
172-
if patch != 0 {
173-
println!(r#"cargo:rustc-cfg=gdextension_exact_api="{major}.{minor}.{patch}""#);
174-
} else {
175-
println!(r#"cargo:rustc-cfg=gdextension_exact_api="{major}.{minor}""#);
176-
}
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-
}
162+
// Emit #[cfg]s since/before for patch level.
163+
for (_, m, p) in all_versions.iter().copied() {
164+
if (m, p) >= (minor, patch) {
165+
println!(r#"cargo:rustc-cfg=since_patch_api="4.{m}.{p}""#);
166+
} else {
167+
println!(r#"cargo:rustc-cfg=before_patch_api="4.{m}.{p}""#);
197168
}
198169
}
199170
}
@@ -241,19 +212,3 @@ pub fn before_api(major_minor: &str) -> bool {
241212
pub fn since_api(major_minor: &str) -> bool {
242213
!before_api(major_minor)
243214
}
244-
245-
//
246-
// pub fn write_module_file(path: &Path) {
247-
// let code = quote! {
248-
// pub mod table_builtins;
249-
// pub mod table_builtins_lifecycle;
250-
// pub mod table_servers_classes;
251-
// pub mod table_scene_classes;
252-
// pub mod table_editor_classes;
253-
// pub mod table_utilities;
254-
//
255-
// pub mod central;
256-
// pub mod gdextension_interface;
257-
// pub mod interface;
258-
// };
259-
// }

0 commit comments

Comments
 (0)