Skip to content

Commit

Permalink
Add ChatGPT generated build.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
louwers committed Jan 28, 2025
1 parent d2ef4df commit 1c1773e
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 32 deletions.
18 changes: 0 additions & 18 deletions platform/rust-cxx/build.rs

This file was deleted.

12 changes: 0 additions & 12 deletions platform/rust-cxx/src/main.rs

This file was deleted.

1 change: 0 additions & 1 deletion platform/rust-cxx/src/rust-ffi.cc

This file was deleted.

1 change: 0 additions & 1 deletion platform/rust-cxx/src/rust-ffi.h

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions platform/rust-cxx/Cargo.toml → platform/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ cxx = "1.0"

[build-dependencies]
cxx-build = "1.0"
cmake = "0.1"
33 changes: 33 additions & 0 deletions platform/rust/build.rs
Original file line number Diff line number Diff line change
@@ -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");
}
File renamed without changes.
38 changes: 38 additions & 0 deletions platform/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 1c1773e

Please sign in to comment.