Browse Source

feat: support rust crate building

master
Dnomd343 1 month ago
parent
commit
3b15d0f170
  1. 6
      src/core/CMakeLists.txt
  2. 4
      src/core_ffi/CMakeLists.txt
  3. 10
      src/core_ffi/rust_ffi/Cargo.lock
  4. 1
      src/core_ffi/rust_ffi/Cargo.toml
  5. 26
      src/core_ffi/rust_ffi/build.rs
  6. 1
      src/core_ffi/rust_ffi/klotski/CMakeLists.txt
  7. 1
      src/core_ffi/rust_ffi/klotski/src/CMakeLists.txt
  8. 1
      src/core_ffi/rust_ffi/klotski/src/core
  9. 1
      src/core_ffi/rust_ffi/klotski/src/core_ffi/CMakeLists.txt
  10. 1
      src/core_ffi/rust_ffi/klotski/third_party/ThirdParty.cmake
  11. 1
      src/core_ffi/rust_ffi/klotski/third_party/parallel-hashmap

6
src/core/CMakeLists.txt

@ -38,8 +38,10 @@ target_link_libraries(klotski_core PUBLIC phmap::phmap)
add_library(klotski::core ALIAS klotski_core)
# TODO: just for dev testing
add_executable(klotski_core_bin main.cc)
target_link_libraries(klotski_core_bin PRIVATE klotski_core)
if (KLSK_DEV_MODE)
add_executable(klotski_core_bin main.cc)
target_link_libraries(klotski_core_bin PRIVATE klotski_core)
endif()
if (KLSK_ENABLE_BENCHMARK)
set(KLSK_BENCHMARK_OPTS -fno-rtti -fno-exceptions -fno-access-control)

4
src/core_ffi/CMakeLists.txt

@ -15,6 +15,9 @@ if (KLSK_PYTHON_FFI)
endif()
if (KLSK_RUST_FFI)
if (CARGO_BUILD)
install(TARGETS klotski_core DESTINATION .)
else()
# just for IDE highlight
add_library(klotski_rust
rust_ffi/adapter/common_code.cc
@ -23,4 +26,5 @@ if (KLSK_RUST_FFI)
target_include_directories(klotski_rust PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(klotski_rust PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/rust_ffi/target/cxxbridge)
target_link_libraries(klotski_rust PRIVATE klotski_core)
endif()
endif()

10
src/core_ffi/rust_ffi/Cargo.lock

@ -8,6 +8,15 @@ version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "907d8581360765417f8f2e0e7d602733bbed60156b4465b7617243689ef9b83d"
[[package]]
name = "cmake"
version = "0.1.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb1e43aa7fd152b1f968787f7dbcdeb306d1867ff373c69955211876c053f91a"
dependencies = [
"cc",
]
[[package]]
name = "codespan-reporting"
version = "0.11.1"
@ -66,6 +75,7 @@ dependencies = [
name = "klotski"
version = "0.0.1"
dependencies = [
"cmake",
"cxx",
"cxx-build",
]

1
src/core_ffi/rust_ffi/Cargo.toml

@ -9,4 +9,5 @@ edition = "2021"
cxx = "1.0.124"
[build-dependencies]
cmake = "0.1"
cxx-build = "1.0.124"

26
src/core_ffi/rust_ffi/build.rs

@ -1,3 +1,5 @@
extern crate cmake;
use cxx_build::CFG;
// NOTE: add `CC=clang-20 CXX=clang++-20 CXXFLAGS="-stdlib=libc++"` for cargo command
@ -5,29 +7,31 @@ use cxx_build::CFG;
// NOTE: also, `RUSTFLAGS="-C linker=clang-20"` should be add to cargo env for using lld
// NOTE: add `CC=clang-20 CXX=clang++-20 CXXFLAGS="-stdlib=libc++" RUSTFLAGS="-C linker=clang-20 -C link-arg=-fuse-ld=lld-20 -C link-arg=-stdlib=libc++"` for using llvm toolchain
fn main() {
CFG.include_prefix = "rust_ffi";
let dst = cmake::Config::new("klotski")
// .build_target("klotski_core")
.define("CARGO_BUILD:BOOL", "ON")
.define("KLSK_ENABLE_TESTING:BOOL", "OFF")
.define("KLSK_ENABLE_BENCHMARK:BOOL", "OFF")
.define("KLSK_C_FFI:BOOL", "OFF")
.define("KLSK_PYTHON_FFI:BOOL", "OFF")
.build();
// std::env::set_var("CC", "clang-20");
// std::env::set_var("CXX", "clang++-20");
CFG.include_prefix = "rust_ffi";
cxx_build::bridge("src/common_code.rs")
.file("adapter/common_code.cc")
.flag("-std=c++23")
.flag("-fno-rtti")
.flag("-fno-exceptions")
// .flag("-stdlib=libc++")
.include("../../core")
// .cpp_set_stdlib("c++")
// .cpp_link_stdlib("c++")
.include("klotski/src/core")
.compile("klotski");
println!("cargo:rustc-link-search=native=../../../cmake-build-release/src/core");
println!("cargo:rustc-link-search=native={}", dst.display());
println!("cargo:rustc-link-lib=static=klotski_core");
println!("cargo:rustc-link-arg=-fuse-ld=lld-20");
println!("cargo:rustc-link-arg=-stdlib=libc++");
println!("cargo:rerun-if-changed=adapter/common_code.cc");
println!("cargo:rerun-if-changed=src/common_code.rs");
}

1
src/core_ffi/rust_ffi/klotski/CMakeLists.txt

@ -0,0 +1 @@
../../../../CMakeLists.txt

1
src/core_ffi/rust_ffi/klotski/src/CMakeLists.txt

@ -0,0 +1 @@
../../../../CMakeLists.txt

1
src/core_ffi/rust_ffi/klotski/src/core

@ -0,0 +1 @@
../../../../core

1
src/core_ffi/rust_ffi/klotski/src/core_ffi/CMakeLists.txt

@ -0,0 +1 @@
../../../../CMakeLists.txt

1
src/core_ffi/rust_ffi/klotski/third_party/ThirdParty.cmake

@ -0,0 +1 @@
../../../../../third_party/ThirdParty.cmake

1
src/core_ffi/rust_ffi/klotski/third_party/parallel-hashmap

@ -0,0 +1 @@
../../../../../third_party/parallel-hashmap
Loading…
Cancel
Save