@@ -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
135141pub 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! {
0 commit comments