From e78f905d18ddaab26eacbad40ffc0a7f2a09c9ec Mon Sep 17 00:00:00 2001 From: innerNULL Date: Mon, 21 Feb 2022 10:40:39 +0000 Subject: [PATCH] Reorganization dev: Refactor building process. dev: Reorganize python binding. dev: Reorganize cpp example. --- CMakeLists.txt | 129 +++++++++--------- README.md | 4 +- bindings/python/CMakeLists.txt | 27 ++++ .../python}/FeaHash_pybind.h | 0 .../python}/FeaSlot_pybind.h | 0 .../python}/LibsvmExtractor_pybind.h | 0 .../python/src}/FeaHash_pybind.cpp | 2 +- .../python/src}/FeaSlot_pybind.cpp | 2 +- .../python/src}/LibsvmExtractor_pybind.cpp | 2 +- .../python/src}/pymodule.cpp | 6 +- examples/cpp/CMakeLists.txt | 22 +++ example.cpp => examples/cpp/example.cpp | 0 setup.py | 5 +- test/cpp/CMakeLists.txt | 58 ++++++-- vcpkg.json | 23 ++++ 15 files changed, 200 insertions(+), 80 deletions(-) create mode 100644 bindings/python/CMakeLists.txt rename {include/feather/pybind => bindings/python}/FeaHash_pybind.h (100%) rename {include/feather/pybind => bindings/python}/FeaSlot_pybind.h (100%) rename {include/feather/pybind => bindings/python}/LibsvmExtractor_pybind.h (100%) rename {src/pybind => bindings/python/src}/FeaHash_pybind.cpp (97%) rename {src/pybind => bindings/python/src}/FeaSlot_pybind.cpp (92%) rename {src/pybind => bindings/python/src}/LibsvmExtractor_pybind.cpp (94%) rename {src/pybind => bindings/python/src}/pymodule.cpp (56%) create mode 100644 examples/cpp/CMakeLists.txt rename example.cpp => examples/cpp/example.cpp (100%) create mode 100644 vcpkg.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 4cbe807..84470b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.14) project(example) +include(FetchContent) +include(ExternalProject) + set(CMAKE_BUILD_TYPE "Debug") #set(CMAKE_CXX_FLAGS " -pthread -std=c++14 -funroll-loops -O3 -march=native -fPIC") @@ -12,83 +15,87 @@ set(CMAKE_CXX_FLAGS " -pthread -std=c++14 -funroll-loops -O3 -march=native") set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb") set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall") -option(BUILD_TESTS "Build tests" OFF) -option(BUILD_EXAMPLES "Build examples" ON) +option(FEATHER_BUILD_PY_BINDER "Build python binder" ON) +option(FEATHER_BUILD_TESTS "Build tests" ON) +option(FEATHER_BUILD_EXAMPLES "Build examples" ON) + -execute_process(COMMAND bash -c "which python3" OUTPUT_VARIABLE PYTHON3_ENV) +execute_process( + COMMAND bash -c "which python3" + OUTPUT_VARIABLE PYTHON3_ENV) if(NOT "${PYTHON3_ENV}" STREQUAL "") string(REPLACE "\n" "" PYTHON3_ENV ""${PYTHON3_ENV}) endif() -set(PYTHON_EXECUTABLE ${PYTHON3_ENV} CACHE STRING "PYTHON_EXECUTABLE") -message(STATUS "PYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}") - -execute_process(COMMAND bash -c "pwd" OUTPUT_VARIABLE CURR_DIR) -string(REPLACE "\n" "" CURR_DIR ${CURR_DIR}) -message(STATUS "CURR_DIR: ${CURR_DIR}") - -# Download cmakeup as pkg management. -if(EXISTS "${CURR_DIR}/main.zip") - message(STATUS "cmakeup has been downloaded.") -else() - message(STATUS "downloading cmakeup first.") - execute_process( - COMMAND bash -c "wget https://ghproxy.com/https://github.com/innerNULL/cmakeup/archive/refs/heads/main.zip" - WORKING_DIRECTORY ${CURR_DIR}) - execute_process( - COMMAND unzip main.zip WORKING_DIRECTORY ${CURR_DIR}) -endif() -set(CMKAEUP_ROOT_PATH "${CURR_DIR}/cmakeup-main/cmakeup") -set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMKAEUP_ROOT_PATH}/module") -include(cmakeup) -cmakeup_init("${CURR_DIR}/_cmakeup_hub" "https://ghproxy.com/https://github.com") -cmakeup_root_path_register(${CMKAEUP_ROOT_PATH}) -include(vcpkg_helpers) - -integrate_vcpkg( - "https://ghproxy.com/https://github.com/microsoft/vcpkg/archive/refs/heads/master.zip" - "${CURR_DIR}" -) -message(STATUS "All vcpkg pkg are installed under ${CMAKEUP_VCPKG_ROOT}/packages") +set(PYTHON_EXECUTABLE + ${PYTHON3_ENV} CACHE STRING "PYTHON_EXECUTABLE") +message(STATUS "PYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}") -cmakeup_pkg_cmake_importer("abseil" "abseil-cpp") -cmakeup_integrate_abseil_abseil_cpp("master" "null" "global") - -cmakeup_pkg_cmake_importer("gabime" "spdlog") -cmakeup_integrate_gabime_spdlog("v1.x" "null" "global") -cmakeup_pkg_cmake_importer("nlohmann" "json") -cmakeup_integrate_nlohmann_json("master" "null" "global") +set(FETCHCONTENT_QUIET FALSE) +set(GIT_HOST "ghproxy.com/https://github.com") +set(VCPKG_FEATURE_FLAGS "versions") +set(VCPKG_ROOT ${FETCHCONTENT_BASE_DIR}/vcpkg-src) +set(_VCPKG_BIN ${VCPKG_ROOT}/vcpkg) +set(_VCPKG_CMAKE + ${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake) +set(CMAKE_TOOLCHAIN_FILE ${_VCPKG_CMAKE}) -message(STATUS "Default python environment is ${PYTHON3_ENV}") -message(STATUS "Target python environment is ${PYTHON_EXECUTABLE}") -execute_process(COMMAND bash -c "${CMAKEUP_VCPKG_BIN} install pybind11") -find_package(pybind11 CONFIG REQUIRED) -include_directories(${pybind11_INCLUDE_DIRS}) +if (EXISTS ${_VCPKG_BIN}) + message( + STATUS + "vcpkg already located at ${_VCPKG_BIN}") + set(_VCPKG_EXISTS TRUE) +else() + message(STATUS "downloading vcpkg first") + FetchContent_Declare( + vcpkg + GIT_REPOSITORY https://${GIT_HOST}/microsoft/vcpkg.git + GIT_TAG 2022.02.02 + GIT_PROGRESS TRUE + DOWNLOAD_NO_PROGRESS FALSE + PATCH_COMMAND + cd && + bash /bootstrap-vcpkg.sh) + FetchContent_MakeAvailable(vcpkg) +endif() -execute_process(COMMAND bash -c "${CMAKEUP_VCPKG_BIN} install xxhash") +include(${_VCPKG_CMAKE}) +execute_process( + COMMAND + ${_VCPKG_BIN} integrate install + --x-manifest-root=${CMAKE_SOURCE_DIR}) +find_package(absl CONFIG REQUIRED) +find_package(nlohmann_json CONFIG REQUIRED) +find_package(spdlog CONFIG REQUIRED) find_package(xxHash CONFIG REQUIRED) + include_directories(${PROJECT_SOURCE_DIR}/include) aux_source_directory(${PROJECT_SOURCE_DIR}/src LIB_SOURCE) -aux_source_directory(${PROJECT_SOURCE_DIR}/src/pybind PYBINDER) -add_library(feather STATIC ${LIB_SOURCE}) -if (BUILD_EXAMPLES) - add_executable(example "./example.cpp") - target_link_libraries(example PRIVATE feather) - #target_link_libraries(example PRIVATE pybind11::module pybind11::lto pybind11::windows_extras pybind11::embed) - #target_link_libraries(example PRIVATE pybind11::embed) - target_link_libraries(example PRIVATE absl::strings) - target_link_libraries(example PRIVATE xxHash::xxhash) +add_library(feather STATIC ${LIB_SOURCE}) +target_link_libraries(feather + PRIVATE absl::strings) +target_link_libraries(feather + PRIVATE nlohmann_json nlohmann_json::nlohmann_json) +target_link_libraries(feather + PRIVATE spdlog::spdlog spdlog::spdlog_header_only) +target_link_libraries(feather + PRIVATE xxHash::xxhash) + + +if (${FEATHER_BUILD_EXAMPLES}) + add_subdirectory( + ${CMAKE_CURRENT_SOURCE_DIR}/examples/cpp) endif() -pybind11_add_module(pyfeather ${LIB_SOURCE} ${PYBINDER}) -target_link_libraries(pyfeather PRIVATE absl::strings) -target_link_libraries(pyfeather PRIVATE xxHash::xxhash) +if (${FEATHER_BUILD_PY_BINDER}) + add_subdirectory( + ${CMAKE_CURRENT_SOURCE_DIR}/bindings/python) +endif() -if (BUILD_TESTS) - message(STATUS "Building with unit-tests.") - add_subdirectory(./test/cpp) +if (${FEATHER_BUILD_TESTS}) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/test/cpp) endif() diff --git a/README.md b/README.md index 4192fc6..2a33113 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,9 @@ FEATure HashER ```bash cd PATH/TO/FEATHER mkdir build && cd build -cmake ../ -DBUILD_TESTS=ON # build with test +cmake ../ -DFEATHER_BUILD_TESTS=ON -DFEATHER_BUILD_PY_BINDER=OFF # build with test #or -cmake ../ +cmake ../ -DFEATHER_BUILD_TESTS=OFF -DFEATHER_BUILD_PY_BINDER=OFF make -j12 ``` diff --git a/bindings/python/CMakeLists.txt b/bindings/python/CMakeLists.txt new file mode 100644 index 0000000..00f0979 --- /dev/null +++ b/bindings/python/CMakeLists.txt @@ -0,0 +1,27 @@ +# file: CMakeLists.txt +# date: 2022-02-21 + + +FetchContent_Declare( + pybind_pybind11 + GIT_REPOSITORY https://${GIT_HOST}/pybind/pybind11.git + GIT_TAG v2.9.0 + GIT_PROGRESS TRUE) +FetchContent_MakeAvailable(pybind_pybind11) + +aux_source_directory( + ${PROJECT_SOURCE_DIR}/src/pybind PYBINDER) + +pybind11_add_module(pyfeather ${LIB_SOURCE} ${PYBINDER}) +target_link_libraries( + pyfeather + PRIVATE absl::strings) +target_link_libraries( + pyfeather + PRIVATE nlohmann_json nlohmann_json::nlohmann_json) +target_link_libraries( + pyfeather + PRIVATE spdlog::spdlog spdlog::spdlog_header_only) +target_link_libraries( + pyfeather + PRIVATE xxHash::xxhash) diff --git a/include/feather/pybind/FeaHash_pybind.h b/bindings/python/FeaHash_pybind.h similarity index 100% rename from include/feather/pybind/FeaHash_pybind.h rename to bindings/python/FeaHash_pybind.h diff --git a/include/feather/pybind/FeaSlot_pybind.h b/bindings/python/FeaSlot_pybind.h similarity index 100% rename from include/feather/pybind/FeaSlot_pybind.h rename to bindings/python/FeaSlot_pybind.h diff --git a/include/feather/pybind/LibsvmExtractor_pybind.h b/bindings/python/LibsvmExtractor_pybind.h similarity index 100% rename from include/feather/pybind/LibsvmExtractor_pybind.h rename to bindings/python/LibsvmExtractor_pybind.h diff --git a/src/pybind/FeaHash_pybind.cpp b/bindings/python/src/FeaHash_pybind.cpp similarity index 97% rename from src/pybind/FeaHash_pybind.cpp rename to bindings/python/src/FeaHash_pybind.cpp index 8506d04..9dd1180 100644 --- a/src/pybind/FeaHash_pybind.cpp +++ b/bindings/python/src/FeaHash_pybind.cpp @@ -5,7 +5,7 @@ #include #include "feather/FeaHash.h" -#include "feather/pybind/FeaHash_pybind.h" +#include "bindings/python/FeaHash_pybind.h" namespace py = pybind11; diff --git a/src/pybind/FeaSlot_pybind.cpp b/bindings/python/src/FeaSlot_pybind.cpp similarity index 92% rename from src/pybind/FeaSlot_pybind.cpp rename to bindings/python/src/FeaSlot_pybind.cpp index b2ff8f7..2fac07e 100644 --- a/src/pybind/FeaSlot_pybind.cpp +++ b/bindings/python/src/FeaSlot_pybind.cpp @@ -5,7 +5,7 @@ #include #include "feather/FeaSlot.h" -#include "feather/pybind/FeaSlot_pybind.h" +#include "bindings/python/FeaSlot_pybind.h" namespace py = pybind11; diff --git a/src/pybind/LibsvmExtractor_pybind.cpp b/bindings/python/src/LibsvmExtractor_pybind.cpp similarity index 94% rename from src/pybind/LibsvmExtractor_pybind.cpp rename to bindings/python/src/LibsvmExtractor_pybind.cpp index b30b92e..c27d6d9 100644 --- a/src/pybind/LibsvmExtractor_pybind.cpp +++ b/bindings/python/src/LibsvmExtractor_pybind.cpp @@ -5,7 +5,7 @@ #include #include "feather/LibsvmExtractor.h" -#include "feather/pybind/LibsvmExtractor_pybind.h" +#include "bindings/python/LibsvmExtractor_pybind.h" namespace py = pybind11; diff --git a/src/pybind/pymodule.cpp b/bindings/python/src/pymodule.cpp similarity index 56% rename from src/pybind/pymodule.cpp rename to bindings/python/src/pymodule.cpp index e9757eb..082ae55 100644 --- a/src/pybind/pymodule.cpp +++ b/bindings/python/src/pymodule.cpp @@ -1,9 +1,9 @@ /// file: pymodule.cpp -#include "feather/pybind/FeaHash_pybind.h" -#include "feather/pybind/FeaSlot_pybind.h" -#include "feather/pybind/LibsvmExtractor_pybind.h" +#include "bindings/python/FeaHash_pybind.h" +#include "bindings/python/FeaSlot_pybind.h" +#include "bindings/python/LibsvmExtractor_pybind.h" namespace feather { diff --git a/examples/cpp/CMakeLists.txt b/examples/cpp/CMakeLists.txt new file mode 100644 index 0000000..510db10 --- /dev/null +++ b/examples/cpp/CMakeLists.txt @@ -0,0 +1,22 @@ +# file: CMakeLists.txt +# date: 2022-02-22 + + +add_executable( + example + ${CMAKE_CURRENT_SOURCE_DIR}/example.cpp) +target_link_libraries( + example + PRIVATE feather) +target_link_libraries( + example + PRIVATE absl::strings) +target_link_libraries( + example + PRIVATE nlohmann_json nlohmann_json::nlohmann_json) +target_link_libraries( + example + PRIVATE spdlog::spdlog spdlog::spdlog_header_only) +target_link_libraries( + example + PRIVATE xxHash::xxhash) diff --git a/example.cpp b/examples/cpp/example.cpp similarity index 100% rename from example.cpp rename to examples/cpp/example.cpp diff --git a/setup.py b/setup.py index 6c965ce..cae7779 100644 --- a/setup.py +++ b/setup.py @@ -56,8 +56,9 @@ def build_extension(self, ext): "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={}".format(extdir), "-DPYTHON_EXECUTABLE={}".format(sys.executable), "-DCMAKE_BUILD_TYPE={}".format(cfg), # not used on MSVC, but no harm - "-DPYTHON_EXECUTABLE={}".format(PYTHON_ENV), - "-DBUILD_EXAMPLES=OFF", + #"-DPYTHON_EXECUTABLE={}".format(PYTHON_ENV), + "-DFEATHER_BUILD_EXAMPLES=OFF", + "-DFEATHER_BUILD_TESTS=OFF" ] build_args = [] # Adding CMake arguments set as environment variable diff --git a/test/cpp/CMakeLists.txt b/test/cpp/CMakeLists.txt index 48ca5a9..d4b87d7 100644 --- a/test/cpp/CMakeLists.txt +++ b/test/cpp/CMakeLists.txt @@ -2,22 +2,62 @@ # date: 2021-02-01 +enable_testing() -execute_process(COMMAND bash -c "${CMAKEUP_VCPKG_BIN} install gtest") -find_package(GTest CONFIG REQUIRED) -enable_testing() +ExternalProject_Add( + googletest + PREFIX ${CMAKE_SOURCE_DIR}/build/_exts + GIT_REPOSITORY https://${GIT_HOST}/google/googletest.git + GIT_TAG release-1.11.0 + GIT_PROGRESS TRUE + DOWNLOAD_NO_PROGRESS FALSE + CMAKE_ARGS + -DCMAKE_INSTALL_PREFIX= + -Dgtest_force_shared_crt=ON + LOG_DOWNLOAD ON + LOG_CONFIGURE ON + LOG_BUILD ON) +ExternalProject_Get_Property(googletest install_dir) +include_directories(${install_dir}/include) + +add_library(libgoogletest::gtest_main STATIC IMPORTED) +set_property( + TARGET libgoogletest::gtest_main + PROPERTY IMPORTED_LOCATION + ${install_dir}/lib/libgtest_main.a) + +add_library(libgoogletest::gmock_main STATIC IMPORTED) +set_property( + TARGET libgoogletest::gmock_main + PROPERTY IMPORTED_LOCATION + ${install_dir}/lib/libgmock_main.a) + +add_library(libgoogletest::gtest STATIC IMPORTED) +set_property( + TARGET libgoogletest::gtest + PROPERTY IMPORTED_LOCATION + ${install_dir}/lib/libgtest.a) + +add_library(libgoogletest::gmock STATIC IMPORTED) +set_property( + TARGET libgoogletest::gmock + PROPERTY IMPORTED_LOCATION + ${install_dir}/lib/libgmock.a) + +add_library(libgoogletest::libgoogletest INTERFACE IMPORTED) +set_property( + TARGET libgoogletest::libgoogletest + PROPERTY INTERFACE_LINK_LIBRARIES + libgoogletest::gtest_main libgoogletest::gmock_main + libgoogletest::gtest libgoogletest::gmock) +add_dependencies(libgoogletest::libgoogletest googletest) add_executable(feather_tests test.cpp) target_link_libraries(feather_tests PRIVATE feather) -#target_link_libraries(feather_tests PRIVATE pybind11::module pybind11::lto pybind11::windows_extras pybind11::embed) -#target_link_libraries(feather_tests PRIVATE pybind11::embed) target_link_libraries(feather_tests PRIVATE absl::strings) -#target_link_libraries(feather_tests PRIVATE ${googletest}) -target_link_libraries(feather_tests - PRIVATE GTest::gmock GTest::gtest GTest::gmock_main GTest::gtest_main) - +target_link_libraries(feather_tests PRIVATE libgoogletest::libgoogletest) add_test(NAME FEATHER_TESTS COMMAND ./test/cpp/feather_test) diff --git a/vcpkg.json b/vcpkg.json new file mode 100644 index 0000000..e0a7e8a --- /dev/null +++ b/vcpkg.json @@ -0,0 +1,23 @@ +{ + "name": "feather", + "version": "0.0.5", + "dependencies": [ + { + "name": "abseil", + "version>=": "2020-09-23#2" + }, + { + "name": "spdlog", + "version>=": "1.8.0#2" + }, + { + "name": "nlohmann-json", + "version>=": "3.9.1" + }, + { + "name": "xxhash", + "version>=": "0.8.0" + } + ], + "builtin-baseline": "3426db05b996481ca31e95fff3734cf23e0f51bc" +}