From 6e465ee6003ddbc894f725a8966afebc66ec7486 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 31 Mar 2024 17:23:04 +0800 Subject: [PATCH] build: refactor cmake configure --- CMakeLists.txt | 56 ++++++++++++++--------------------- src/CMakeLists.txt | 14 ++++----- src/core/CMakeLists.txt | 39 +++++------------------- src/core_test/CMakeLists.txt | 26 +++++++--------- third_party/ThirdParty.cmake | 34 +++++++++++++++++++++ third_party/third_party.cmake | 26 ---------------- 6 files changed, 81 insertions(+), 114 deletions(-) create mode 100644 third_party/ThirdParty.cmake delete mode 100644 third_party/third_party.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index ee671d2..069d546 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,47 +1,37 @@ -cmake_minimum_required(VERSION 3.0) - +cmake_minimum_required(VERSION 3.12) project(klotski) -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release) -endif() +# ------------------------------------------------------------------------------------ # -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") +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) -get_filename_component(KLOTSKI_ROOT ${CMAKE_CURRENT_SOURCE_DIR} ABSOLUTE) +# ------------------------------------------------------------------------------------ # -set(KLOTSKI_UNIT_TEST ON) - -include(third_party/third_party.cmake) +if (NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +endif() -################################################################################ +add_compile_options(-Wall -Wextra) +add_compile_options(-flto) # TODO: enabled by LTO option -set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) -set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) +get_filename_component(KLOTSKI_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR} ABSOLUTE) -file( - COPY ${PROJECT_SOURCE_DIR}/src/klotski_core/klotski.h - DESTINATION ${PROJECT_SOURCE_DIR}/bin - FOLLOW_SYMLINK_CHAIN -) +include(third_party/ThirdParty.cmake) -################################################################################ +# ------------------------------------------------------------------------------------ # -option(BUILD_DYN "build shared klotski core library" OFF) -if (BUILD_DYN) - add_compile_options(-fPIC) -endif() +#set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) +#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) +#file( +# COPY ${PROJECT_SOURCE_DIR}/src/klotski_core/klotski.h +# DESTINATION ${PROJECT_SOURCE_DIR}/bin +# FOLLOW_SYMLINK_CHAIN +#) add_subdirectory(src) -################################################################################ - -#option(CORE_TEST "build test for klotski core" ON) -#if (CORE_TEST) -# include_directories(third_party/googletest/googletest/include) -# add_subdirectory(third_party/googletest EXCLUDE_FROM_ALL) -# add_subdirectory(test) -#endif() - -################################################################################ +# ------------------------------------------------------------------------------------ # diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a91a5f6..81ce5a1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,17 +1,13 @@ -cmake_minimum_required(VERSION 3.0) - -project(klotski-cli LANGUAGES C) +cmake_minimum_required(VERSION 3.12) add_subdirectory(core) add_subdirectory(core_test) -#add_subdirectory(klotski_core) -#include_directories(klotski_core) - -set(CMAKE_C_STANDARD 90) +#project(klotski-cli LANGUAGES C) +#set(CMAKE_C_STANDARD 90) -add_executable(klotski_cli main.c) -target_link_libraries(klotski_cli PRIVATE klotski-core) +#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 8f39fae..32f0780 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -1,21 +1,9 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.12) +project(core VERSION 0.2.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 23) -project(klotski-core VERSION 0.9.1 LANGUAGES CXX) - -add_compile_options(-fno-exceptions) - -#set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) - -include_directories(utils) -include_directories(all_cases) - -include_directories(raw_code) -include_directories(short_code) -include_directories(common_code) - -add_library(${PROJECT_NAME} +set(KLOTSKI_CORE_SRC all_cases/basic_ranges.cc all_cases/all_cases.cc ffi/all_cases.cc @@ -31,20 +19,9 @@ add_library(${PROJECT_NAME} short_code/short_code.cc short_code/sundry.cc ) +add_library(klotski_core STATIC ${KLOTSKI_CORE_SRC}) +target_compile_options(klotski_core PRIVATE -fno-rtti -fno-exceptions) -add_executable(${PROJECT_NAME}_cli main.cc - all_cases/basic_ranges.cc - all_cases/all_cases.cc - ffi/all_cases.cc - common_code/common_code.cc - common_code/serialize.cc - common_code/sundry.cc - raw_code/raw_code.cc - raw_code/convert.cc - raw_code/sundry.cc - raw_code/mirror.cc - short_code/convert.cc - short_code/serialize.cc - short_code/short_code.cc - short_code/sundry.cc -) +target_include_directories(klotski_core PUBLIC + utils all_cases + raw_code short_code common_code) diff --git a/src/core_test/CMakeLists.txt b/src/core_test/CMakeLists.txt index 2cd3943..d6d1fb6 100644 --- a/src/core_test/CMakeLists.txt +++ b/src/core_test/CMakeLists.txt @@ -1,22 +1,18 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.5 FATAL_ERROR) +project(core-test LANGUAGES CXX) -set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD 23) -set(KLOTSKI_TEST_DEPS klotski-core gtest gtest_main md5sum xxHash::xxhash) +set(KLOTSKI_TEST_DEPS klotski_core + GTest::gtest_main bs::thread_pool md5sum xxHash::xxhash) -############################################################################################### +# ------------------------------------------------------------------------------------ # include_directories(utils) -include_directories(${KLOTSKI_ROOT}/src/core/ffi) -include_directories(${KLOTSKI_ROOT}/src/core/utils) -include_directories(${KLOTSKI_ROOT}/src/core/all_cases) +include_directories(${KLOTSKI_ROOT_DIR}/src/core/ffi) -include_directories(${KLOTSKI_ROOT}/src/core/raw_code) -include_directories(${KLOTSKI_ROOT}/src/core/short_code) -include_directories(${KLOTSKI_ROOT}/src/core/common_code) - -############################################################################################### +# ------------------------------------------------------------------------------------ # set(KLOTSKI_TEST_CASES_SRC cases/all_cases.cc @@ -26,7 +22,7 @@ add_executable(test_klotski_cases ${KLOTSKI_TEST_CASES_SRC}) target_link_libraries(test_klotski_cases PRIVATE ${KLOTSKI_TEST_DEPS}) add_test(NAME klotski_cases COMMAND test_klotski_cases) -############################################################################################### +# ------------------------------------------------------------------------------------ # set(KLOTSKI_TEST_FFI_SRC ffi/all_cases.cc @@ -36,7 +32,7 @@ add_executable(test_klotski_ffi ${KLOTSKI_TEST_FFI_SRC}) target_link_libraries(test_klotski_ffi PRIVATE ${KLOTSKI_TEST_DEPS}) add_test(NAME klotski_ffi COMMAND test_klotski_ffi) -############################################################################################### +# ------------------------------------------------------------------------------------ # set(KLOTSKI_TEST_CODEC_SRC codec/mirror.cc @@ -49,4 +45,4 @@ add_executable(test_klotski_codec ${KLOTSKI_TEST_CODEC_SRC}) target_link_libraries(test_klotski_codec PRIVATE ${KLOTSKI_TEST_DEPS}) add_test(NAME klotski_codec COMMAND test_klotski_codec) -############################################################################################### +# ------------------------------------------------------------------------------------ # diff --git a/third_party/ThirdParty.cmake b/third_party/ThirdParty.cmake new file mode 100644 index 0000000..6a14bdf --- /dev/null +++ b/third_party/ThirdParty.cmake @@ -0,0 +1,34 @@ +set(KLOTSKI_THIRD_PARTY ${KLOTSKI_ROOT_DIR}/third_party) + +# abseil library +set(ABSL_PROPAGATE_CXX_STD ON) +add_subdirectory(${KLOTSKI_THIRD_PARTY}/abseil-cpp EXCLUDE_FROM_ALL) + +if (KLOTSKI_ENABLE_BENCHMARK) + # google benchmark framework + set(BENCHMARK_ENABLE_TESTING OFF) + set(BENCHMARK_ENABLE_EXCEPTIONS OFF) + add_subdirectory(${KLOTSKI_THIRD_PARTY}/benchmark EXCLUDE_FROM_ALL) +endif() + +if (KLOTSKI_ENABLE_TESTING) + # BS thread pool + add_library(thread_pool INTERFACE) + target_include_directories(thread_pool INTERFACE + ${KLOTSKI_THIRD_PARTY}/thread-pool/include) + add_library(bs::thread_pool ALIAS thread_pool) + + # md5sum implementation + add_subdirectory(${KLOTSKI_THIRD_PARTY}/md5sum EXCLUDE_FROM_ALL) + target_include_directories(md5sum INTERFACE ${KLOTSKI_THIRD_PARTY}/md5sum) + + # google test framework + add_subdirectory(${KLOTSKI_THIRD_PARTY}/googletest EXCLUDE_FROM_ALL) + + # xxHash implementation + set(BUILD_SHARED_LIBS OFF) + set(XXHASH_BUILD_XXHSUM OFF) + set(XXHASH_BUILD_ENABLE_INLINE_API ON) + add_subdirectory(${KLOTSKI_THIRD_PARTY}/xxHash/cmake_unofficial EXCLUDE_FROM_ALL) + target_include_directories(xxhash INTERFACE ${KLOTSKI_THIRD_PARTY}/xxHash) +endif() diff --git a/third_party/third_party.cmake b/third_party/third_party.cmake deleted file mode 100644 index e512f2d..0000000 --- a/third_party/third_party.cmake +++ /dev/null @@ -1,26 +0,0 @@ -set(KLOTSKI_THIRD_PARTY ${KLOTSKI_ROOT}/third_party) - -set(ABSL_PROPAGATE_CXX_STD ON) -include_directories(abseil-cpp) -add_subdirectory(${KLOTSKI_THIRD_PARTY}/abseil-cpp EXCLUDE_FROM_ALL) - -if (KLOTSKI_UNIT_TEST) - include_directories(${KLOTSKI_THIRD_PARTY}/md5sum) - add_subdirectory(${KLOTSKI_THIRD_PARTY}/md5sum EXCLUDE_FROM_ALL) -endif (KLOTSKI_UNIT_TEST) - -if (KLOTSKI_UNIT_TEST) - include_directories(${KLOTSKI_THIRD_PARTY}/googletest/googletest/include) - add_subdirectory(${KLOTSKI_THIRD_PARTY}/googletest EXCLUDE_FROM_ALL) -endif (KLOTSKI_UNIT_TEST) - -if (KLOTSKI_UNIT_TEST) - include_directories(${KLOTSKI_THIRD_PARTY}/thread-pool/include) -endif (KLOTSKI_UNIT_TEST) - -if (KLOTSKI_UNIT_TEST) - set(XXHASH_BUILD_XXHSUM OFF) - set(XXHASH_BUILD_ENABLE_INLINE_API OFF) - include_directories(${KLOTSKI_THIRD_PARTY}/xxHash) - add_subdirectory(${KLOTSKI_THIRD_PARTY}/xxHash/cmake_unofficial EXCLUDE_FROM_ALL) -endif (KLOTSKI_UNIT_TEST)