diff --git a/platform/rust-cxx/build.rs b/platform/rust-cxx/build.rs deleted file mode 100644 index 5de758f7020..00000000000 --- a/platform/rust-cxx/build.rs +++ /dev/null @@ -1,18 +0,0 @@ -fn main() { - // use src/main.rs to generate a header - // use cmake crate with flags to compile that header + rust-ffi.cc + core -> staticlib - - // println what to link with - println!("cargo:rustc-link-lib=static=rust-ffi"); - - - // - // cxx_build::bridge("src/main.rs") - // .file("src/blobstore.cc") - // .std("c++20") - // .compile("cxxbridge-demo"); - // - // println!("cargo:rerun-if-changed=src/main.rs"); - // println!("cargo:rerun-if-changed=src/blobstore.cc"); - // println!("cargo:rerun-if-changed=include/blobstore.h"); -} diff --git a/platform/rust-cxx/src/main.rs b/platform/rust-cxx/src/main.rs deleted file mode 100644 index 42c9f14c1f4..00000000000 --- a/platform/rust-cxx/src/main.rs +++ /dev/null @@ -1,12 +0,0 @@ -// cxx wrapper for maplibre-native -#[cxx::bridge] -mod ffi { - unsafe extern "C++" { - include!("maplibre-native/include/maplibre-native.h"); - - type MaplibreNative; - - fn new_maplibre_native() -> UniquePtr; - fn render(&self, style: &str, width: u32, height: u32) -> Vec; - } -} diff --git a/platform/rust-cxx/src/rust-ffi.cc b/platform/rust-cxx/src/rust-ffi.cc deleted file mode 100644 index a083d389d3e..00000000000 --- a/platform/rust-cxx/src/rust-ffi.cc +++ /dev/null @@ -1 +0,0 @@ -// use ParsedColor diff --git a/platform/rust-cxx/src/rust-ffi.h b/platform/rust-cxx/src/rust-ffi.h deleted file mode 100644 index a083d389d3e..00000000000 --- a/platform/rust-cxx/src/rust-ffi.h +++ /dev/null @@ -1 +0,0 @@ -// use ParsedColor diff --git a/platform/rust-cxx/.gitignore b/platform/rust/.gitignore similarity index 100% rename from platform/rust-cxx/.gitignore rename to platform/rust/.gitignore diff --git a/platform/rust-cxx/Cargo.toml b/platform/rust/Cargo.toml similarity index 91% rename from platform/rust-cxx/Cargo.toml rename to platform/rust/Cargo.toml index e6530b9f4c7..19741b52319 100644 --- a/platform/rust-cxx/Cargo.toml +++ b/platform/rust/Cargo.toml @@ -8,3 +8,4 @@ cxx = "1.0" [build-dependencies] cxx-build = "1.0" +cmake = "0.1" diff --git a/platform/rust/build.rs b/platform/rust/build.rs new file mode 100644 index 00000000000..17f84b90e2f --- /dev/null +++ b/platform/rust/build.rs @@ -0,0 +1,33 @@ +use std::env; +use std::path::PathBuf; + +fn main() { + let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + let project_root = PathBuf::from(manifest_dir) + .parent().unwrap() // "platform/" + .parent().unwrap() // "maplibre-native/" + .to_path_buf(); + + let mut cmake_cfg = cmake::Config::new(&project_root); + cmake_cfg + .define("MLN_WITH_CORE_ONLY", "ON") + .generator("Ninja") + .build_target("mbgl-core"); + + let build_output = cmake_cfg.build(); + + // The library is in build_output/build/libmbgl-core.a + let lib_dir = build_output.join("build"); + println!("cargo:rustc-link-search=native={}", lib_dir.display()); + println!("cargo:rustc-link-lib=static=mbgl-core"); + + // If on macOS/Clang: + println!("cargo:rustc-link-lib=c++"); + // If on Linux/GCC: + // println!("cargo:rustc-link-lib=stdc++"); + + let mut cxx = cxx_build::bridge("src/lib.rs"); + cxx.include(project_root.join("include")) + .flag_if_supported("-std=c++20") + .compile("maplibre_rust_bindings"); +} diff --git a/platform/rust-cxx/justfile b/platform/rust/justfile similarity index 100% rename from platform/rust-cxx/justfile rename to platform/rust/justfile diff --git a/platform/rust/src/lib.rs b/platform/rust/src/lib.rs new file mode 100644 index 00000000000..c4e24b088f4 --- /dev/null +++ b/platform/rust/src/lib.rs @@ -0,0 +1,38 @@ +// platform/rust/src/lib.rs + +#[cxx::bridge(namespace = "mbgl::util")] +mod ffi { + // cxx allows including C++ headers directly: + unsafe extern "C++" { + include!("mbgl/math/log2.hpp"); + + // We specify the C++ namespace and the free function name exactly. + // cxx can bind free functions directly if they have a compatible signature. + // The signature must match what's in log2.hpp: + // "uint32_t ceil_log2(uint64_t x);" + // + // We'll express that to Rust as (u64 -> u32). + pub fn ceil_log2(x: u64) -> u32; + } +} + +/// A safe Rust wrapper so you can call `ceil_log2` from your code: +pub fn ceil_log2(x: u64) -> u32 { + ffi::ceil_log2(x) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_log2() { + let result = ceil_log2(1); + assert_eq!(result, 0, "log2(1) = 0 bits needed"); + + let result = ceil_log2(2); + assert_eq!(result, 1, "log2(2) = 1 bit needed"); + + let result = ceil_log2(3); + assert_eq!(result, 2, "log2(3) -> 2 bits needed"); + } +}