forked from maplibre/maplibre-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
72 additions
and
32 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ cxx = "1.0" | |
|
||
[build-dependencies] | ||
cxx-build = "1.0" | ||
cmake = "0.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |