From 3ed08b5a2b7592d8bd871bbfab1cb569d1a23de0 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 28 Jan 2023 01:04:03 +0800 Subject: [PATCH] feat: rust-ffi framework --- .gitignore | 1 + src/CMakeLists.txt | 2 +- src/klotski/CMakeLists.txt | 39 ++-- src/klotski/all_cases/CMakeLists.txt | 4 +- src/klotski/analyse/CMakeLists.txt | 4 +- src/klotski/benchmark/CMakeLists.txt | 4 +- src/klotski/common_code/CMakeLists.txt | 4 +- src/klotski/core/CMakeLists.txt | 2 +- src/klotski/fast_cal/CMakeLists.txt | 4 +- src/klotski/ffi/tmain.cc | 14 +- src/klotski/raw_code/CMakeLists.txt | 4 +- src/klotski/short_code/CMakeLists.txt | 4 +- src/klotski/utils/CMakeLists.txt | 2 +- src/main.c | 3 +- src/rust_ffi/Cargo.lock | 242 +++++++++++++++++++++++++ src/rust_ffi/Cargo.toml | 11 ++ src/rust_ffi/build.rs | 26 +++ src/rust_ffi/src/main.rs | 18 ++ src/rust_ffi/wrapper.h | 3 + test/CMakeLists.txt | 4 +- 20 files changed, 352 insertions(+), 43 deletions(-) create mode 100644 src/rust_ffi/Cargo.lock create mode 100644 src/rust_ffi/Cargo.toml create mode 100644 src/rust_ffi/build.rs create mode 100644 src/rust_ffi/src/main.rs create mode 100644 src/rust_ffi/wrapper.h diff --git a/.gitignore b/.gitignore index 197f167..9b5a342 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /.idea/ /cmake-build-debug/ /cmake-build-release/ +/src/rust_ffi/target/ /src/klotski/utils/version.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1155cad..1803e53 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,4 +7,4 @@ include_directories(klotski/ffi) add_subdirectory(klotski) add_executable(cli main.c) -target_link_libraries(cli PUBLIC klotski) +target_link_libraries(cli PRIVATE klotski) diff --git a/src/klotski/CMakeLists.txt b/src/klotski/CMakeLists.txt index 9b3ba76..41456c6 100644 --- a/src/klotski/CMakeLists.txt +++ b/src/klotski/CMakeLists.txt @@ -2,7 +2,10 @@ cmake_minimum_required(VERSION 3.0) project(klotski-core VERSION 0.0.1 LANGUAGES CXX) -configure_file(utils/version.h.in ${PROJECT_SOURCE_DIR}/utils/version.h) +configure_file( + utils/version.h.in + ${PROJECT_SOURCE_DIR}/utils/version.h +) set(CMAKE_CXX_STANDARD 14) @@ -42,26 +45,28 @@ set(FFI_SRC codec.cc tmain.cc version.cc) list(TRANSFORM FFI_SRC PREPEND "ffi/") add_library(klotski-ffi OBJECT ${FFI_SRC}) -option(SHARED_LIB "build static library" ON) -if (SHARED_LIB) - add_library(klotski SHARED $) -else() - add_library(klotski STATIC $) -endif() +set(OBJS $) -################################################################ +list(APPEND OBJS $) +list(APPEND OBJS $) -target_link_libraries(klotski PRIVATE utils) -target_link_libraries(klotski PRIVATE all_cases) +list(APPEND OBJS $) +list(APPEND OBJS $) +list(APPEND OBJS $) -target_link_libraries(klotski PRIVATE raw_code) -target_link_libraries(klotski PRIVATE short_code) -target_link_libraries(klotski PRIVATE common_code) +list(APPEND OBJS $) +list(APPEND OBJS $) +list(APPEND OBJS $) -target_link_libraries(klotski PRIVATE core) -target_link_libraries(klotski PRIVATE analyse) -target_link_libraries(klotski PRIVATE fast_cal) +list(APPEND OBJS $) -target_link_libraries(klotski PRIVATE benchmark) +################################################################ + +option(STATIC_LIB "build static library" ON) +if (STATIC_LIB) + add_library(klotski STATIC ${OBJS}) +else() + add_library(klotski SHARED ${OBJS}) +endif() ################################################################ diff --git a/src/klotski/all_cases/CMakeLists.txt b/src/klotski/all_cases/CMakeLists.txt index 3fcf4da..275a4e0 100644 --- a/src/klotski/all_cases/CMakeLists.txt +++ b/src/klotski/all_cases/CMakeLists.txt @@ -1,4 +1,4 @@ cmake_minimum_required(VERSION 3.0) -add_library(all_cases STATIC all_cases.cc basic_ranges.cc) -target_link_libraries(all_cases PUBLIC utils) +add_library(all_cases OBJECT all_cases.cc basic_ranges.cc) +#target_link_libraries(all_cases PUBLIC utils) diff --git a/src/klotski/analyse/CMakeLists.txt b/src/klotski/analyse/CMakeLists.txt index 839307f..6fd693c 100644 --- a/src/klotski/analyse/CMakeLists.txt +++ b/src/klotski/analyse/CMakeLists.txt @@ -1,4 +1,4 @@ cmake_minimum_required(VERSION 3.0) -add_library(analyse STATIC analyse.cc backtrack.cc) -target_link_libraries(analyse PUBLIC core) +add_library(analyse OBJECT analyse.cc backtrack.cc) +#target_link_libraries(analyse PUBLIC core) diff --git a/src/klotski/benchmark/CMakeLists.txt b/src/klotski/benchmark/CMakeLists.txt index b7e7f2a..77b5045 100644 --- a/src/klotski/benchmark/CMakeLists.txt +++ b/src/klotski/benchmark/CMakeLists.txt @@ -1,4 +1,4 @@ cmake_minimum_required(VERSION 3.0) -add_library(benchmark STATIC chore.cc benchmark.cc) -target_link_libraries(benchmark PUBLIC all_cases) +add_library(benchmark OBJECT chore.cc benchmark.cc) +#target_link_libraries(benchmark PUBLIC all_cases) diff --git a/src/klotski/common_code/CMakeLists.txt b/src/klotski/common_code/CMakeLists.txt index dd52c5f..8fd9876 100644 --- a/src/klotski/common_code/CMakeLists.txt +++ b/src/klotski/common_code/CMakeLists.txt @@ -1,4 +1,4 @@ cmake_minimum_required(VERSION 3.0) -add_library(common_code STATIC convert.cc serialize.cc common_code.cc) -target_link_libraries(common_code PUBLIC utils) +add_library(common_code OBJECT convert.cc serialize.cc common_code.cc) +#target_link_libraries(common_code PUBLIC utils) diff --git a/src/klotski/core/CMakeLists.txt b/src/klotski/core/CMakeLists.txt index 71314f0..9d6bf25 100644 --- a/src/klotski/core/CMakeLists.txt +++ b/src/klotski/core/CMakeLists.txt @@ -1,3 +1,3 @@ cmake_minimum_required(VERSION 3.0) -add_library(core STATIC core.cc) +add_library(core OBJECT core.cc) diff --git a/src/klotski/fast_cal/CMakeLists.txt b/src/klotski/fast_cal/CMakeLists.txt index a2da0e3..5f154ef 100644 --- a/src/klotski/fast_cal/CMakeLists.txt +++ b/src/klotski/fast_cal/CMakeLists.txt @@ -1,4 +1,4 @@ cmake_minimum_required(VERSION 3.0) -add_library(fast_cal STATIC cal_core.cc fast_cal.cc) -target_link_libraries(fast_cal PUBLIC core) +add_library(fast_cal OBJECT cal_core.cc fast_cal.cc) +#target_link_libraries(fast_cal PUBLIC core) diff --git a/src/klotski/ffi/tmain.cc b/src/klotski/ffi/tmain.cc index ea2c7fb..a6848b7 100644 --- a/src/klotski/ffi/tmain.cc +++ b/src/klotski/ffi/tmain.cc @@ -1,12 +1,11 @@ #include -#include -#include +//#include #include "klotski.h" //#include "core.h" -#include "common.h" +//#include "common.h" #include "benchmark.h" #include "all_cases.h" @@ -16,13 +15,16 @@ using namespace klotski; void tmain() { printf("tmain start\n"); - std::cout << ALL_CASES_SIZE_SUM << std::endl; + printf("%d\n", ALL_CASES_SIZE_SUM); +// std::cout << ALL_CASES_SIZE_SUM << std::endl; // uint64_t common_code = 0x1A9BC0C00; // klotski::Common::range_reverse(common_code); - std::cout << Benchmark::basic_ranges(Benchmark::MS) << std::endl; - std::cout << Benchmark::all_cases(Benchmark::MS) << std::endl; + printf("%f\n", Benchmark::basic_ranges(Benchmark::MS)); + printf("%f\n", Benchmark::all_cases(Benchmark::MS)); +// std::cout << Benchmark::basic_ranges(Benchmark::MS) << std::endl; +// std::cout << Benchmark::all_cases(Benchmark::MS) << std::endl; printf("tmain exit\n"); } diff --git a/src/klotski/raw_code/CMakeLists.txt b/src/klotski/raw_code/CMakeLists.txt index c850338..fac385f 100644 --- a/src/klotski/raw_code/CMakeLists.txt +++ b/src/klotski/raw_code/CMakeLists.txt @@ -1,4 +1,4 @@ cmake_minimum_required(VERSION 3.0) -add_library(raw_code STATIC convert.cc raw_code.cc) -target_link_libraries(raw_code PUBLIC utils) +add_library(raw_code OBJECT convert.cc raw_code.cc) +#target_link_libraries(raw_code PUBLIC utils) diff --git a/src/klotski/short_code/CMakeLists.txt b/src/klotski/short_code/CMakeLists.txt index ad24a2d..42eb33f 100644 --- a/src/klotski/short_code/CMakeLists.txt +++ b/src/klotski/short_code/CMakeLists.txt @@ -2,5 +2,5 @@ cmake_minimum_required(VERSION 3.0) include_directories(offset) -add_library(short_code STATIC convert.cc serialize.cc short_code.cc) -target_link_libraries(short_code PUBLIC utils all_cases) +add_library(short_code OBJECT convert.cc serialize.cc short_code.cc) +#target_link_libraries(short_code PUBLIC utils all_cases) diff --git a/src/klotski/utils/CMakeLists.txt b/src/klotski/utils/CMakeLists.txt index da9333e..af19f11 100644 --- a/src/klotski/utils/CMakeLists.txt +++ b/src/klotski/utils/CMakeLists.txt @@ -1,3 +1,3 @@ cmake_minimum_required(VERSION 3.0) -add_library(utils STATIC common.cc) +add_library(utils OBJECT common.cc) diff --git a/src/main.c b/src/main.c index 5cb3987..8b3f9be 100644 --- a/src/main.c +++ b/src/main.c @@ -4,7 +4,8 @@ int main() { // printf("cli boot\n"); -// tmain(); + tmain(); + return 0; // bool ret = common_code_check(0x1A9BF0C00); // printf("result -> %d\n", ret); diff --git a/src/rust_ffi/Cargo.lock b/src/rust_ffi/Cargo.lock new file mode 100644 index 0000000..2c36b2c --- /dev/null +++ b/src/rust_ffi/Cargo.lock @@ -0,0 +1,242 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bindgen" +version = "0.63.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36d860121800b2a9a94f9b5604b332d5cffb234ce17609ea479d723dbc9d3885" +dependencies = [ + "bitflags", + "cexpr", + "clang-sys", + "lazy_static", + "lazycell", + "log", + "peeking_take_while", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "syn", + "which", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cexpr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clang-sys" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa2e27ae6ab525c3d369ded447057bca5438d86dc3a68f6faafb8269ba82ebf3" +dependencies = [ + "glob", + "libc", + "libloading", +] + +[[package]] +name = "either" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" + +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + +[[package]] +name = "klotski_ffi" +version = "0.1.0" +dependencies = [ + "bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "lazycell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" + +[[package]] +name = "libc" +version = "0.2.139" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" + +[[package]] +name = "libloading" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" +dependencies = [ + "cfg-if", + "winapi", +] + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "once_cell" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" + +[[package]] +name = "peeking_take_while" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" + +[[package]] +name = "proc-macro2" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" +dependencies = [ + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" + +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + +[[package]] +name = "shlex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" + +[[package]] +name = "syn" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" + +[[package]] +name = "which" +version = "4.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2441c784c52b289a054b7201fc93253e288f094e2f4be9058343127c4226a269" +dependencies = [ + "either", + "libc", + "once_cell", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/src/rust_ffi/Cargo.toml b/src/rust_ffi/Cargo.toml new file mode 100644 index 0000000..c9d32c4 --- /dev/null +++ b/src/rust_ffi/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "klotski_ffi" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +[build-dependencies] +bindgen = "0.63.0" diff --git a/src/rust_ffi/build.rs b/src/rust_ffi/build.rs new file mode 100644 index 0000000..21f45ab --- /dev/null +++ b/src/rust_ffi/build.rs @@ -0,0 +1,26 @@ +extern crate bindgen; + +use std::env; +use std::path::PathBuf; + +const RELEASE_DIR: &str = "../../cmake-build-release/src/klotski"; + +fn main() { + // basic compile options + println!("cargo:rustc-link-search={}", RELEASE_DIR); + println!("cargo:rustc-link-lib=static=klotski"); + println!("cargo:rerun-if-changed=wrapper.h"); + println!("cargo:rustc-link-lib=stdc++"); + + // build binding info from c-header file + let bindings = bindgen::Builder::default() + .header("wrapper.h") + .parse_callbacks(Box::new(bindgen::CargoCallbacks)) + .generate() + .expect("Unable to generate bindings"); + + // release binding rust source code + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + bindings.write_to_file(out_path.join("bindings.rs")) + .expect("Couldn't write bindings"); +} diff --git a/src/rust_ffi/src/main.rs b/src/rust_ffi/src/main.rs new file mode 100644 index 0000000..96ade13 --- /dev/null +++ b/src/rust_ffi/src/main.rs @@ -0,0 +1,18 @@ +#![allow(non_snake_case)] +#![allow(non_camel_case_types)] +#![allow(non_upper_case_globals)] +include!(concat!(env!("OUT_DIR"), "/bindings.rs")); + +use std::env; + +fn klotski_tmain() { + unsafe { + tmain(); + } +} + +fn main() { + println!("rust start"); + klotski_tmain(); + println!("rust exit"); +} diff --git a/src/rust_ffi/wrapper.h b/src/rust_ffi/wrapper.h new file mode 100644 index 0000000..74ac8f7 --- /dev/null +++ b/src/rust_ffi/wrapper.h @@ -0,0 +1,3 @@ +#pragma once + +#include "../klotski/ffi/klotski.h" diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a73f3a1..0e3e265 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,7 +14,7 @@ add_library(md5 STATIC ../third_party/md5/md5.cpp) include_directories(../src/klotski/utils) add_executable(test_utils utils.cc) target_link_libraries(test_utils PUBLIC gtest gtest_main) -target_link_libraries(test_utils PUBLIC utils) +target_link_libraries(test_utils PUBLIC klotski) add_test(NAME utils COMMAND test_utils) ################################################################ @@ -22,7 +22,7 @@ add_test(NAME utils COMMAND test_utils) include_directories(../src/klotski/all_cases) add_executable(test_all_cases all_cases.cc) target_link_libraries(test_all_cases PUBLIC gtest gtest_main) -target_link_libraries(test_all_cases PUBLIC md5 all_cases) +target_link_libraries(test_all_cases PUBLIC klotski md5) add_test(NAME all_cases COMMAND test_all_cases) ################################################################