From 31b7ee3d8b1b9e182b76b51550a9342fb03f3a78 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 16 Nov 2024 21:17:03 +0800 Subject: [PATCH] feat: support python package building --- CMakeLists.txt | 2 +- src/CMakeLists.txt | 11 ++-- src/core/CMakeLists.txt | 54 ++++++++++--------- src/core_ffi/py_ffi/CMakeLists.txt | 2 +- src/core_ffi/py_ffi/klotski.cc | 2 +- src/core_ffi/py_ffi/packing/CMakeLists.txt | 7 +++ src/core_ffi/py_ffi/packing/MANIFEST.in | 8 +++ src/core_ffi/py_ffi/packing/README.md | 1 + src/core_ffi/py_ffi/packing/pyproject.toml | 8 +++ src/core_ffi/py_ffi/packing/setup.py | 13 +++++ .../py_ffi/packing/src/klotski/__init__.py | 3 ++ src/core_ffi/py_ffi/packing/src/project | 1 + 12 files changed, 76 insertions(+), 36 deletions(-) create mode 100644 src/core_ffi/py_ffi/packing/CMakeLists.txt create mode 100644 src/core_ffi/py_ffi/packing/MANIFEST.in create mode 100644 src/core_ffi/py_ffi/packing/README.md create mode 100644 src/core_ffi/py_ffi/packing/pyproject.toml create mode 100644 src/core_ffi/py_ffi/packing/setup.py create mode 100644 src/core_ffi/py_ffi/packing/src/klotski/__init__.py create mode 120000 src/core_ffi/py_ffi/packing/src/project diff --git a/CMakeLists.txt b/CMakeLists.txt index 1528471..b2fed0d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ add_compile_options(-Wall -Wextra) if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") add_compile_options(-flto=full) else () - add_compile_options(-flto) # TODO: only for g++ + #add_compile_options(-flto) # TODO: only for g++ endif () # TODO: for python-ffi diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3ea04a6..23e463f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,13 +2,10 @@ cmake_minimum_required(VERSION 3.12) add_subdirectory(core) add_subdirectory(core_ffi) -add_subdirectory(core_test) + +if (KLSK_ENABLE_TESTING) + add_subdirectory(core_test) +endif() #project(klotski-cli LANGUAGES C) #set(CMAKE_C_STANDARD 90) - -#add_executable(klotski_cli main.c) -#target_link_libraries(klotski_cli PRIVATE klotski-core) -#target_link_libraries(klotski_cli PRIVATE klotski absl::flat_hash_map) - -# -labsl_hash -labsl_city -labsl_low_level_hash -labsl_raw_hash_set diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 0808758..a1c4c20 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -41,29 +41,31 @@ add_library(klotski::core ALIAS klotski_core) add_executable(klotski_core_bin main.cc) target_link_libraries(klotski_core_bin PRIVATE klotski_core) -set(KLSK_BENCHMARK_OPTS -fno-rtti -fno-exceptions -fno-access-control) -set(KLSK_BENCHMARK_LIBS klotski::core benchmark::benchmark_main bs::thread_pool) - -add_executable(bm_klsk_codec benchmark/codec.cc) -target_compile_options(bm_klsk_codec PRIVATE ${KLSK_BENCHMARK_OPTS}) -target_link_libraries(bm_klsk_codec PRIVATE ${KLSK_BENCHMARK_LIBS}) - -add_executable(bm_klsk_group benchmark/group.cc) -target_compile_options(bm_klsk_group PRIVATE ${KLSK_BENCHMARK_OPTS}) -target_link_libraries(bm_klsk_group PRIVATE ${KLSK_BENCHMARK_LIBS}) - -add_executable(bm_klsk_all_cases benchmark/all_cases.cc) -target_compile_options(bm_klsk_all_cases PRIVATE ${KLSK_BENCHMARK_OPTS}) -target_link_libraries(bm_klsk_all_cases PRIVATE ${KLSK_BENCHMARK_LIBS}) - -add_executable(bm_klsk_ranges benchmark/ranges.cc) -target_compile_options(bm_klsk_ranges PRIVATE ${KLSK_BENCHMARK_OPTS}) -target_link_libraries(bm_klsk_ranges PRIVATE ${KLSK_BENCHMARK_LIBS}) - -add_executable(bm_klsk_utility benchmark/utility.cc) -target_compile_options(bm_klsk_utility PRIVATE ${KLSK_BENCHMARK_OPTS}) -target_link_libraries(bm_klsk_utility PRIVATE ${KLSK_BENCHMARK_LIBS}) - -add_executable(bm_klsk_fast_cal benchmark/fast_cal.cc) -target_compile_options(bm_klsk_fast_cal PRIVATE ${KLSK_BENCHMARK_OPTS}) -target_link_libraries(bm_klsk_fast_cal PRIVATE ${KLSK_BENCHMARK_LIBS}) +if (KLSK_ENABLE_BENCHMARK) + set(KLSK_BENCHMARK_OPTS -fno-rtti -fno-exceptions -fno-access-control) + set(KLSK_BENCHMARK_LIBS klotski::core benchmark::benchmark_main bs::thread_pool) + + add_executable(bm_klsk_codec benchmark/codec.cc) + target_compile_options(bm_klsk_codec PRIVATE ${KLSK_BENCHMARK_OPTS}) + target_link_libraries(bm_klsk_codec PRIVATE ${KLSK_BENCHMARK_LIBS}) + + add_executable(bm_klsk_group benchmark/group.cc) + target_compile_options(bm_klsk_group PRIVATE ${KLSK_BENCHMARK_OPTS}) + target_link_libraries(bm_klsk_group PRIVATE ${KLSK_BENCHMARK_LIBS}) + + add_executable(bm_klsk_all_cases benchmark/all_cases.cc) + target_compile_options(bm_klsk_all_cases PRIVATE ${KLSK_BENCHMARK_OPTS}) + target_link_libraries(bm_klsk_all_cases PRIVATE ${KLSK_BENCHMARK_LIBS}) + + add_executable(bm_klsk_ranges benchmark/ranges.cc) + target_compile_options(bm_klsk_ranges PRIVATE ${KLSK_BENCHMARK_OPTS}) + target_link_libraries(bm_klsk_ranges PRIVATE ${KLSK_BENCHMARK_LIBS}) + + add_executable(bm_klsk_utility benchmark/utility.cc) + target_compile_options(bm_klsk_utility PRIVATE ${KLSK_BENCHMARK_OPTS}) + target_link_libraries(bm_klsk_utility PRIVATE ${KLSK_BENCHMARK_LIBS}) + + add_executable(bm_klsk_fast_cal benchmark/fast_cal.cc) + target_compile_options(bm_klsk_fast_cal PRIVATE ${KLSK_BENCHMARK_OPTS}) + target_link_libraries(bm_klsk_fast_cal PRIVATE ${KLSK_BENCHMARK_LIBS}) +endif() diff --git a/src/core_ffi/py_ffi/CMakeLists.txt b/src/core_ffi/py_ffi/CMakeLists.txt index c351ec8..ed2e2d2 100644 --- a/src/core_ffi/py_ffi/CMakeLists.txt +++ b/src/core_ffi/py_ffi/CMakeLists.txt @@ -22,4 +22,4 @@ pybind11_add_module(klotski_py ) target_include_directories(klotski_py PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) target_link_libraries(klotski_py PRIVATE klotski::core) -set_target_properties(klotski_py PROPERTIES OUTPUT_NAME klotski) +set_target_properties(klotski_py PROPERTIES OUTPUT_NAME _klotski) diff --git a/src/core_ffi/py_ffi/klotski.cc b/src/core_ffi/py_ffi/klotski.cc index 6787c5d..117fb3f 100644 --- a/src/core_ffi/py_ffi/klotski.cc +++ b/src/core_ffi/py_ffi/klotski.cc @@ -28,7 +28,7 @@ using klotski::ffi::PyExc_GroupError; // return PyCases::from_ref(klotski::cases::AllCases::instance().fetch()); // } -PYBIND11_MODULE(klotski, m) { +PYBIND11_MODULE(_klotski, m) { py::register_exception(m, "GroupError", PyExc_ValueError); py::register_exception(m, "CodecError", PyExc_ValueError); diff --git a/src/core_ffi/py_ffi/packing/CMakeLists.txt b/src/core_ffi/py_ffi/packing/CMakeLists.txt new file mode 100644 index 0000000..f48e1d0 --- /dev/null +++ b/src/core_ffi/py_ffi/packing/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.12) + +project(klotski_py_demo VERSION "0.0.1") + +add_subdirectory(src/project) + +install(TARGETS klotski_py DESTINATION .) diff --git a/src/core_ffi/py_ffi/packing/MANIFEST.in b/src/core_ffi/py_ffi/packing/MANIFEST.in new file mode 100644 index 0000000..d36c238 --- /dev/null +++ b/src/core_ffi/py_ffi/packing/MANIFEST.in @@ -0,0 +1,8 @@ +include CMakeLists.txt +include README.md +include pyproject.toml +include setup.py +include src/klotski/__init__.py +recursive-include src/project * +recursive-exclude src/project/src/core_ffi/py_ffi/packing * +exclude MANIFEST.in diff --git a/src/core_ffi/py_ffi/packing/README.md b/src/core_ffi/py_ffi/packing/README.md new file mode 100644 index 0000000..6adf09d --- /dev/null +++ b/src/core_ffi/py_ffi/packing/README.md @@ -0,0 +1 @@ +# klotski python ffi diff --git a/src/core_ffi/py_ffi/packing/pyproject.toml b/src/core_ffi/py_ffi/packing/pyproject.toml new file mode 100644 index 0000000..159d250 --- /dev/null +++ b/src/core_ffi/py_ffi/packing/pyproject.toml @@ -0,0 +1,8 @@ +[build-system] +requires = [ + "setuptools", + "scikit-build>=0.13", + "cmake", + "ninja", +] +build-backend = "setuptools.build_meta" diff --git a/src/core_ffi/py_ffi/packing/setup.py b/src/core_ffi/py_ffi/packing/setup.py new file mode 100644 index 0000000..d6a760f --- /dev/null +++ b/src/core_ffi/py_ffi/packing/setup.py @@ -0,0 +1,13 @@ +from skbuild import setup + +setup( + name="py-klotski", + version="0.0.1", + description="klotski engine", + author="Dnomd343", + license="MIT", + packages=["klotski"], + package_dir={"": "src"}, + cmake_install_dir="src/klotski", + python_requires=">=3.9", +) diff --git a/src/core_ffi/py_ffi/packing/src/klotski/__init__.py b/src/core_ffi/py_ffi/packing/src/klotski/__init__.py new file mode 100644 index 0000000..e7c8909 --- /dev/null +++ b/src/core_ffi/py_ffi/packing/src/klotski/__init__.py @@ -0,0 +1,3 @@ +from ._klotski import Cases, CommonCode, FastCal, Group, GroupUnion, ShortCode + +__all__ = ["Cases", "CommonCode", "FastCal", "Group", "GroupUnion", "ShortCode"] diff --git a/src/core_ffi/py_ffi/packing/src/project b/src/core_ffi/py_ffi/packing/src/project new file mode 120000 index 0000000..9bc160e --- /dev/null +++ b/src/core_ffi/py_ffi/packing/src/project @@ -0,0 +1 @@ +../../../../../../klotski/ \ No newline at end of file