From 624dffc35ac3b1625a7818d6189d83a13a6610dc Mon Sep 17 00:00:00 2001 From: PJB3005 Date: Sun, 9 Nov 2025 00:10:43 +0100 Subject: [PATCH] MacOS & ARM64 support This is for OpenDream. macOS specifically just needs to be supported in the library loader code, by making it use the same dlopen() path as Linux. byondapi.h does not support ARM64 properly due to missing #if statements for it. This leads to DM_64BIT not being set and u4c being miscompiled as a u64. I decided to just work around this by telling bindgen to add that define if the target is aarch64, instead of modifying the header files themselves. --- crates/byondapi-rs/src/static_global.rs | 2 +- crates/byondapi-sys/build.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/byondapi-rs/src/static_global.rs b/crates/byondapi-rs/src/static_global.rs index cc23355..17b4707 100644 --- a/crates/byondapi-rs/src/static_global.rs +++ b/crates/byondapi-rs/src/static_global.rs @@ -23,7 +23,7 @@ fn init_lib() -> byondapi_sys::ByondApi { .expect("Failed to initialize library.") } -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "macos"))] fn init_lib() -> byondapi_sys::ByondApi { for func in inventory::iter:: { func.0(); diff --git a/crates/byondapi-sys/build.rs b/crates/byondapi-sys/build.rs index 27819e7..eddf006 100644 --- a/crates/byondapi-sys/build.rs +++ b/crates/byondapi-sys/build.rs @@ -44,6 +44,9 @@ fn copy_wrapper(lib_dir: &Path) -> PathBuf { fn generate_all() { let out_dir = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR not defined")); + let target_triple = std::env::var("TARGET").unwrap_or("".into()); + let is_64_bit = target_triple.starts_with("aarch64"); + get_headers() .into_iter() .for_each(|(path, (major, minor))| { @@ -51,7 +54,7 @@ fn generate_all() { std::fs::copy(path, target).expect("Failed to copy to out_dir"); let wrapper = copy_wrapper(&out_dir); - let builder = bindgen::Builder::default() + let mut builder = bindgen::Builder::default() .header(wrapper.to_string_lossy()) .dynamic_library_name("ByondApi") .dynamic_link_require_all(true) @@ -60,6 +63,10 @@ fn generate_all() { .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) .parse_callbacks(Box::new(DoxygenCallbacks)); + if is_64_bit { + builder = builder.clang_arg("-DDM_64BIT") + } + builder .generate() .expect("Unable to generate bindings")