From 19072d6987bca2e75566f1b5467872508dea5e32 Mon Sep 17 00:00:00 2001 From: andriyDev Date: Fri, 29 Sep 2023 16:42:52 -0700 Subject: [PATCH] Add a check for 32bit Windows to target nightly Rust. Previously, compiling with non-nightly Rust meant 32bit Windows would fail to generate bindings - bindgen will not use the "thiscall" ABI unless using nightly Rust. The bindings would therefore have (almost) nothing in them, so any calls would fail. Now at least there will be a warning, and if using nightly Rust, this will work! --- build.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index 524e817..ab0ed49 100644 --- a/build.rs +++ b/build.rs @@ -55,6 +55,18 @@ fn is_debug() -> bool { } } +fn is_32_bit() -> bool { + env::var("TARGET") + .expect("TARGET should be provided by Cargo.") + .contains("i686") +} + +fn is_nightly() -> bool { + env::var("RUSTUP_TOOLCHAIN") + .expect("RUSTUP_TOOLCHAIN should be provided by Cargo.") + .starts_with("nightly") +} + fn lib_names() -> Vec { let mut root_names = Vec::new(); #[cfg(feature = "recast")] @@ -185,7 +197,7 @@ fn generate_recast_bindings( add_to_builder: impl Fn(bindgen::Builder) -> bindgen::Builder, out_file: PathBuf, ) { - let builder = bindgen::Builder::default() + let mut builder = bindgen::Builder::default() .parse_callbacks(Box::new(bindgen::CargoCallbacks)) .clang_args(["-x", "c++"].iter()) .clang_args( @@ -200,8 +212,18 @@ fn generate_recast_bindings( .blocklist_file(".*stddef\\.h") .blocklist_type("max_align_t"); + if is_windows() && is_32_bit() { + if is_nightly() { + builder = builder.rust_target(bindgen::RustTarget::Nightly); + } else { + println!("cargo:warning=Windows 32 bit uses the \"thiscall\" ABI. This feature is not enabled, so compilation may fail! Consider using nightly Rust, which enables this feature."); + } + } + #[cfg(feature = "detour_large_nav_meshes")] - let builder = builder.clang_args(["-DDT_POLYREF64"]); + { + builder = builder.clang_args(["-DDT_POLYREF64"]); + } let bindings = add_to_builder(builder).generate().expect("Unable to generate bindings.");