diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index a1c4c20..f0e1184 100644 --- a/src/core/CMakeLists.txt +++ b/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) diff --git a/src/core_ffi/CMakeLists.txt b/src/core_ffi/CMakeLists.txt index dd88c5d..d28d493 100644 --- a/src/core_ffi/CMakeLists.txt +++ b/src/core_ffi/CMakeLists.txt @@ -4,10 +4,10 @@ project(core_ffi) set(CMAKE_CXX_STANDARD 23) if (KLSK_C_FFI) - add_library(klotski_c c_ffi/all_cases.cc) - target_include_directories(klotski_c PUBLIC c_ffi/include) - target_link_libraries(klotski_c PRIVATE klotski::core) - set_target_properties(klotski_c PROPERTIES OUTPUT_NAME klotski) + add_library(klotski_c c_ffi/all_cases.cc) + target_include_directories(klotski_c PUBLIC c_ffi/include) + target_link_libraries(klotski_c PRIVATE klotski::core) + set_target_properties(klotski_c PROPERTIES OUTPUT_NAME klotski) endif() if (KLSK_PYTHON_FFI) @@ -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() diff --git a/src/core_ffi/rust_ffi/Cargo.lock b/src/core_ffi/rust_ffi/Cargo.lock index 615b97d..5478db0 100644 --- a/src/core_ffi/rust_ffi/Cargo.lock +++ b/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", ] diff --git a/src/core_ffi/rust_ffi/Cargo.toml b/src/core_ffi/rust_ffi/Cargo.toml index 7549bf7..4f647d6 100644 --- a/src/core_ffi/rust_ffi/Cargo.toml +++ b/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" diff --git a/src/core_ffi/rust_ffi/build.rs b/src/core_ffi/rust_ffi/build.rs index 384a3f9..0741961 100644 --- a/src/core_ffi/rust_ffi/build.rs +++ b/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"); } diff --git a/src/core_ffi/rust_ffi/klotski/CMakeLists.txt b/src/core_ffi/rust_ffi/klotski/CMakeLists.txt new file mode 120000 index 0000000..75f9ac9 --- /dev/null +++ b/src/core_ffi/rust_ffi/klotski/CMakeLists.txt @@ -0,0 +1 @@ +../../../../CMakeLists.txt \ No newline at end of file diff --git a/src/core_ffi/rust_ffi/klotski/src/CMakeLists.txt b/src/core_ffi/rust_ffi/klotski/src/CMakeLists.txt new file mode 120000 index 0000000..75f9ac9 --- /dev/null +++ b/src/core_ffi/rust_ffi/klotski/src/CMakeLists.txt @@ -0,0 +1 @@ +../../../../CMakeLists.txt \ No newline at end of file diff --git a/src/core_ffi/rust_ffi/klotski/src/core b/src/core_ffi/rust_ffi/klotski/src/core new file mode 120000 index 0000000..58ffb12 --- /dev/null +++ b/src/core_ffi/rust_ffi/klotski/src/core @@ -0,0 +1 @@ +../../../../core \ No newline at end of file diff --git a/src/core_ffi/rust_ffi/klotski/src/core_ffi/CMakeLists.txt b/src/core_ffi/rust_ffi/klotski/src/core_ffi/CMakeLists.txt new file mode 120000 index 0000000..75f9ac9 --- /dev/null +++ b/src/core_ffi/rust_ffi/klotski/src/core_ffi/CMakeLists.txt @@ -0,0 +1 @@ +../../../../CMakeLists.txt \ No newline at end of file diff --git a/src/core_ffi/rust_ffi/klotski/third_party/ThirdParty.cmake b/src/core_ffi/rust_ffi/klotski/third_party/ThirdParty.cmake new file mode 120000 index 0000000..833cf51 --- /dev/null +++ b/src/core_ffi/rust_ffi/klotski/third_party/ThirdParty.cmake @@ -0,0 +1 @@ +../../../../../third_party/ThirdParty.cmake \ No newline at end of file diff --git a/src/core_ffi/rust_ffi/klotski/third_party/parallel-hashmap b/src/core_ffi/rust_ffi/klotski/third_party/parallel-hashmap new file mode 120000 index 0000000..4d44a12 --- /dev/null +++ b/src/core_ffi/rust_ffi/klotski/third_party/parallel-hashmap @@ -0,0 +1 @@ +../../../../../third_party/parallel-hashmap \ No newline at end of file