Browse Source

feat: support rust crate building

legacy
Dnomd343 1 month ago
parent
commit
5d200cb351
  1. 6
      src/core/CMakeLists.txt
  2. 12
      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) add_library(klotski::core ALIAS klotski_core)
# TODO: just for dev testing # TODO: just for dev testing
add_executable(klotski_core_bin main.cc) if (KLSK_DEV_MODE)
target_link_libraries(klotski_core_bin PRIVATE klotski_core) add_executable(klotski_core_bin main.cc)
target_link_libraries(klotski_core_bin PRIVATE klotski_core)
endif()
if (KLSK_ENABLE_BENCHMARK) if (KLSK_ENABLE_BENCHMARK)
set(KLSK_BENCHMARK_OPTS -fno-rtti -fno-exceptions -fno-access-control) set(KLSK_BENCHMARK_OPTS -fno-rtti -fno-exceptions -fno-access-control)

12
src/core_ffi/CMakeLists.txt

@ -4,10 +4,10 @@ project(core_ffi)
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)
if (KLSK_C_FFI) if (KLSK_C_FFI)
add_library(klotski_c c_ffi/all_cases.cc) add_library(klotski_c c_ffi/all_cases.cc)
target_include_directories(klotski_c PUBLIC c_ffi/include) target_include_directories(klotski_c PUBLIC c_ffi/include)
target_link_libraries(klotski_c PRIVATE klotski::core) target_link_libraries(klotski_c PRIVATE klotski::core)
set_target_properties(klotski_c PROPERTIES OUTPUT_NAME klotski) set_target_properties(klotski_c PROPERTIES OUTPUT_NAME klotski)
endif() endif()
if (KLSK_PYTHON_FFI) if (KLSK_PYTHON_FFI)
@ -15,6 +15,9 @@ if (KLSK_PYTHON_FFI)
endif() endif()
if (KLSK_RUST_FFI) if (KLSK_RUST_FFI)
if (CARGO_BUILD)
install(TARGETS klotski_core DESTINATION .)
else()
# just for IDE highlight # just for IDE highlight
add_library(klotski_rust add_library(klotski_rust
rust_ffi/adapter/common_code.cc 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})
target_include_directories(klotski_rust PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/rust_ffi/target/cxxbridge) target_include_directories(klotski_rust PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/rust_ffi/target/cxxbridge)
target_link_libraries(klotski_rust PRIVATE klotski_core) target_link_libraries(klotski_rust PRIVATE klotski_core)
endif()
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" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "907d8581360765417f8f2e0e7d602733bbed60156b4465b7617243689ef9b83d" checksum = "907d8581360765417f8f2e0e7d602733bbed60156b4465b7617243689ef9b83d"
[[package]]
name = "cmake"
version = "0.1.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb1e43aa7fd152b1f968787f7dbcdeb306d1867ff373c69955211876c053f91a"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "codespan-reporting" name = "codespan-reporting"
version = "0.11.1" version = "0.11.1"
@ -66,6 +75,7 @@ dependencies = [
name = "klotski" name = "klotski"
version = "0.0.1" version = "0.0.1"
dependencies = [ dependencies = [
"cmake",
"cxx", "cxx",
"cxx-build", "cxx-build",
] ]

1
src/core_ffi/rust_ffi/Cargo.toml

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

26
src/core_ffi/rust_ffi/build.rs

@ -1,3 +1,5 @@
extern crate cmake;
use cxx_build::CFG; use cxx_build::CFG;
// NOTE: add `CC=clang-20 CXX=clang++-20 CXXFLAGS="-stdlib=libc++"` for cargo command // 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: 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() { 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"); CFG.include_prefix = "rust_ffi";
// std::env::set_var("CXX", "clang++-20");
cxx_build::bridge("src/common_code.rs") cxx_build::bridge("src/common_code.rs")
.file("adapter/common_code.cc") .file("adapter/common_code.cc")
.flag("-std=c++23") .flag("-std=c++23")
.flag("-fno-rtti") .flag("-fno-rtti")
.flag("-fno-exceptions") .flag("-fno-exceptions")
// .flag("-stdlib=libc++") .include("klotski/src/core")
.include("../../core")
// .cpp_set_stdlib("c++")
// .cpp_link_stdlib("c++")
.compile("klotski"); .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-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=adapter/common_code.cc");
println!("cargo:rerun-if-changed=src/common_code.rs"); 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