Browse Source

feat: python ffi support

master
Dnomd343 6 months ago
parent
commit
115c431e20
  1. 3
      .gitmodules
  2. 13
      CMakeLists.txt
  3. 1
      src/core/CMakeLists.txt
  4. 17
      src/core_ffi/CMakeLists.txt
  5. 0
      src/core_ffi/c_ffi/all_cases.cc
  6. 0
      src/core_ffi/c_ffi/include/klotski.h
  7. 39
      src/core_ffi/py_ffi/demo.cc
  8. 8
      src/core_test/CMakeLists.txt
  9. 23
      third_party/ThirdParty.cmake
  10. 1
      third_party/pybind11

3
.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

13
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)

1
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)

17
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()

0
src/core_ffi/all_cases.cc → src/core_ffi/c_ffi/all_cases.cc

0
src/core_ffi/include/klotski.h → src/core_ffi/c_ffi/include/klotski.h

39
src/core_ffi/py_ffi/demo.cc

@ -0,0 +1,39 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <common_code/common_code.h>
namespace py = pybind11;
// class CommonCode {
// public:
// explicit CommonCode(uint64_t val) : value_(val) {}
//
// uint64_t Value() const {
// return value_;
// }
//
// static std::optional<CommonCode> 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_<CommonCode>(m, "CommonCode")
// .def(py::init<uint64_t>())
.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";
}

8
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)
# ------------------------------------------------------------------------------------ #

23
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()

1
third_party/pybind11

@ -0,0 +1 @@
Subproject commit 3e9dfa2866941655c56877882565e7577de6fc7b
Loading…
Cancel
Save