From 115c431e20e7dd623bd6b13c98f1dae753072c31 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 18 May 2024 15:11:54 +0800 Subject: [PATCH] feat: python ffi support --- .gitmodules | 3 ++ CMakeLists.txt | 13 +++++--- src/core/CMakeLists.txt | 1 + src/core_ffi/CMakeLists.txt | 17 +++++++--- src/core_ffi/{ => c_ffi}/all_cases.cc | 0 src/core_ffi/{ => c_ffi}/include/klotski.h | 0 src/core_ffi/py_ffi/demo.cc | 39 ++++++++++++++++++++++ src/core_test/CMakeLists.txt | 8 ++--- third_party/ThirdParty.cmake | 23 ++++++++----- third_party/pybind11 | 1 + 10 files changed, 83 insertions(+), 22 deletions(-) rename src/core_ffi/{ => c_ffi}/all_cases.cc (100%) rename src/core_ffi/{ => c_ffi}/include/klotski.h (100%) create mode 100644 src/core_ffi/py_ffi/demo.cc create mode 160000 third_party/pybind11 diff --git a/.gitmodules b/.gitmodules index 50eb257..faf5645 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,6 @@ [submodule "third_party/xxHash"] path = third_party/xxHash url = https://github.com/Cyan4973/xxHash.git +[submodule "third_party/pybind11"] + path = third_party/pybind11 + url = https://github.com/pybind/pybind11.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 681402d..8de77eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,10 +3,13 @@ project(klotski) # ------------------------------------------------------------------------------------ # -option(KLOTSKI_ENABLE_LTO "Enable LTO optimisation of the project." OFF) -option(KLOTSKI_SHARED_LIB "Build klotski core as a dynamic library." OFF) -option(KLOTSKI_ENABLE_TESTING "Enable testing of the klotski project." ON) -option(KLOTSKI_ENABLE_BENCHMARK "Enable benchmark of the klotski project." ON) +option(KLSK_ENABLE_LTO "Enable LTO optimisation of the project." OFF) +option(KLSK_SHARED_LIB "Build klotski core as a dynamic library." OFF) +option(KLSK_ENABLE_TESTING "Enable testing of the klotski project." ON) +option(KLSK_ENABLE_BENCHMARK "Enable benchmark of the klotski project." ON) + +option(KLSK_C_FFI "" ON) +option(KLSK_PYTHON_FFI "" ON) # ------------------------------------------------------------------------------------ # @@ -17,7 +20,7 @@ endif() add_compile_options(-Wall -Wextra) add_compile_options(-flto=auto) # TODO: enabled by LTO option -get_filename_component(KLOTSKI_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR} ABSOLUTE) +get_filename_component(KLSK_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR} ABSOLUTE) include(third_party/ThirdParty.cmake) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 2196ef9..689c246 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -28,6 +28,7 @@ set(KLOTSKI_CORE_SRC add_library(klotski_core STATIC ${KLOTSKI_CORE_SRC}) target_compile_options(klotski_core PRIVATE -fno-rtti -fno-exceptions) target_include_directories(klotski_core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +add_library(klotski::core ALIAS klotski_core) # TODO: just for dev testing add_executable(klotski_core_bin main.cc) diff --git a/src/core_ffi/CMakeLists.txt b/src/core_ffi/CMakeLists.txt index 75fb362..c735a97 100644 --- a/src/core_ffi/CMakeLists.txt +++ b/src/core_ffi/CMakeLists.txt @@ -1,8 +1,17 @@ cmake_minimum_required(VERSION 3.12) -project(core_ffi LANGUAGES CXX) +project(core_ffi) set(CMAKE_CXX_STANDARD 23) -add_library(klotski all_cases.cc) -target_include_directories(klotski PUBLIC include) -target_link_libraries(klotski PRIVATE klotski_core) +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) +endif() + +if (KLSK_PYTHON_FFI) + pybind11_add_module(klotski_py py_ffi/demo.cc) + target_link_libraries(klotski_py PRIVATE klotski::core) + set_target_properties(klotski_py PROPERTIES OUTPUT_NAME klotski) +endif() diff --git a/src/core_ffi/all_cases.cc b/src/core_ffi/c_ffi/all_cases.cc similarity index 100% rename from src/core_ffi/all_cases.cc rename to src/core_ffi/c_ffi/all_cases.cc diff --git a/src/core_ffi/include/klotski.h b/src/core_ffi/c_ffi/include/klotski.h similarity index 100% rename from src/core_ffi/include/klotski.h rename to src/core_ffi/c_ffi/include/klotski.h diff --git a/src/core_ffi/py_ffi/demo.cc b/src/core_ffi/py_ffi/demo.cc new file mode 100644 index 0000000..2b4793a --- /dev/null +++ b/src/core_ffi/py_ffi/demo.cc @@ -0,0 +1,39 @@ +#include +#include + +#include + +namespace py = pybind11; + +// class CommonCode { +// public: +// explicit CommonCode(uint64_t val) : value_(val) {} +// +// uint64_t Value() const { +// return value_; +// } +// +// static std::optional Create(uint64_t val) { +// if (val == 343) { +// return CommonCode {val}; +// } +// return std::nullopt; +// } +// +// private: +// uint64_t value_ {343}; +// }; + +using klotski::codec::CommonCode; + +PYBIND11_MODULE(klotski, m) { + + py::class_(m, "CommonCode") + // .def(py::init()) + .def("to_string", &CommonCode::to_string, py::arg("shorten") = false) + .def_property_readonly("value", &CommonCode::unwrap) + // .def_static("from_val", &CommonCode::Create); + .def_static("create", &CommonCode::create); + + m.attr("__version__") = "version field"; +} diff --git a/src/core_test/CMakeLists.txt b/src/core_test/CMakeLists.txt index e5f782e..c8482e5 100644 --- a/src/core_test/CMakeLists.txt +++ b/src/core_test/CMakeLists.txt @@ -3,7 +3,7 @@ project(core-test LANGUAGES CXX) set(CMAKE_CXX_STANDARD 23) -set(KLOTSKI_TEST_DEPS klotski klotski_core +set(KLSK_TEST_DEPS klotski_c klotski_core GTest::gtest_main bs::thread_pool md5sum::md5 xxHash::xxh3) # ------------------------------------------------------------------------------------ # @@ -25,7 +25,7 @@ set(KLOTSKI_TEST_CASES_SRC ) add_executable(test_klotski_cases ${KLOTSKI_TEST_CASES_SRC}) -target_link_libraries(test_klotski_cases PRIVATE ${KLOTSKI_TEST_DEPS}) +target_link_libraries(test_klotski_cases PRIVATE ${KLSK_TEST_DEPS}) add_test(NAME klotski_cases COMMAND test_klotski_cases) # ------------------------------------------------------------------------------------ # @@ -35,7 +35,7 @@ set(KLOTSKI_TEST_FFI_SRC ) add_executable(test_klotski_ffi ${KLOTSKI_TEST_FFI_SRC}) -target_link_libraries(test_klotski_ffi PRIVATE ${KLOTSKI_TEST_DEPS}) +target_link_libraries(test_klotski_ffi PRIVATE ${KLSK_TEST_DEPS}) add_test(NAME klotski_ffi COMMAND test_klotski_ffi) # ------------------------------------------------------------------------------------ # @@ -48,7 +48,7 @@ set(KLOTSKI_TEST_CODEC_SRC ) add_executable(test_klotski_codec ${KLOTSKI_TEST_CODEC_SRC}) -target_link_libraries(test_klotski_codec PRIVATE ${KLOTSKI_TEST_DEPS}) +target_link_libraries(test_klotski_codec PRIVATE ${KLSK_TEST_DEPS}) add_test(NAME klotski_codec COMMAND test_klotski_codec) # ------------------------------------------------------------------------------------ # diff --git a/third_party/ThirdParty.cmake b/third_party/ThirdParty.cmake index 5571909..25c877a 100644 --- a/third_party/ThirdParty.cmake +++ b/third_party/ThirdParty.cmake @@ -1,32 +1,37 @@ -set(KLOTSKI_THIRD_PARTY ${KLOTSKI_ROOT_DIR}/third_party) +set(KLSK_THIRD_PARTY ${KLSK_ROOT_DIR}/third_party) # abseil library set(ABSL_PROPAGATE_CXX_STD ON) -add_subdirectory(${KLOTSKI_THIRD_PARTY}/abseil-cpp EXCLUDE_FROM_ALL) +add_subdirectory(${KLSK_THIRD_PARTY}/abseil-cpp EXCLUDE_FROM_ALL) -if (KLOTSKI_ENABLE_BENCHMARK) +if (KLSK_ENABLE_BENCHMARK) # google benchmark framework set(BENCHMARK_ENABLE_TESTING OFF) set(BENCHMARK_ENABLE_EXCEPTIONS OFF) - add_subdirectory(${KLOTSKI_THIRD_PARTY}/benchmark EXCLUDE_FROM_ALL) + add_subdirectory(${KLSK_THIRD_PARTY}/benchmark EXCLUDE_FROM_ALL) endif() -if (KLOTSKI_ENABLE_TESTING) +if (KLSK_ENABLE_TESTING) # BS thread pool add_library(thread_pool INTERFACE) target_include_directories(thread_pool INTERFACE - ${KLOTSKI_THIRD_PARTY}/thread-pool/include) + ${KLSK_THIRD_PARTY}/thread-pool/include) add_library(bs::thread_pool ALIAS thread_pool) # md5sum implementation set(MD5_ENABLE_LTO OFF) - add_subdirectory(${KLOTSKI_THIRD_PARTY}/md5sum EXCLUDE_FROM_ALL) + add_subdirectory(${KLSK_THIRD_PARTY}/md5sum EXCLUDE_FROM_ALL) # google test framework - add_subdirectory(${KLOTSKI_THIRD_PARTY}/googletest EXCLUDE_FROM_ALL) + add_subdirectory(${KLSK_THIRD_PARTY}/googletest EXCLUDE_FROM_ALL) # xxHash implementation add_library(xxhash INTERFACE) - target_include_directories(xxhash INTERFACE ${KLOTSKI_THIRD_PARTY}/xxHash) + target_include_directories(xxhash INTERFACE ${KLSK_THIRD_PARTY}/xxHash) add_library(xxHash::xxh3 ALIAS xxhash) endif() + +if (KLSK_PYTHON_FFI) + # python ffi exposure + add_subdirectory(${KLSK_THIRD_PARTY}/pybind11 EXCLUDE_FROM_ALL) +endif() diff --git a/third_party/pybind11 b/third_party/pybind11 new file mode 160000 index 0000000..3e9dfa2 --- /dev/null +++ b/third_party/pybind11 @@ -0,0 +1 @@ +Subproject commit 3e9dfa2866941655c56877882565e7577de6fc7b