From e14b27d54ab8f5bb52bc3e797485c4aa5b08871d Mon Sep 17 00:00:00 2001 From: Joerg-Christian Boehme Date: Tue, 20 Apr 2021 17:42:00 +0200 Subject: [PATCH 1/3] Improved cmake build to build without to checkout "external/json" manually --- CMakeLists.txt | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7406016..0250904 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.8...3.19) +cmake_minimum_required(VERSION 3.11...3.19) project(taocpp-config VERSION 1.0.0 LANGUAGES CXX) @@ -24,19 +24,42 @@ target_include_directories(taocpp-config INTERFACE # require C++17 target_compile_features(taocpp-config INTERFACE cxx_std_17) +option(TAOCPP_CONFIG_INSTALL "Generate the install target" ${TAOCPP_CONFIG_IS_MAIN_PROJECT}) + # find a suitable taoJSON set(TAOCPP_CONFIG_JSON_MIN_VERSION 1.0.0) find_package(taocpp-json ${TAOCPP_CONFIG_JSON_MIN_VERSION} QUIET CONFIG) if(NOT taocpp-json_FOUND) - # if a compatible version of taoJSON is not already installed, build and install it from the submodule directory - message(STATUS "Adding taoJSON as submodule from external/json") - set(TAOCPP_JSON_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE STRING "Override taoJSON include install directory") - set(TAOCPP_JSON_INSTALL_CMAKE_DIR ${CMAKE_INSTALL_DATAROOTDIR}/json/cmake CACHE STRING "Override taoJSON cmake install directory") - add_subdirectory(external/json) + if(EXISTS external/json/CMakeLists.txt) + # if a compatible version of taoJSON is not already installed, build and install it from the submodule directory + message(STATUS "Adding taoJSON as submodule from external/json") + set(TAOCPP_JSON_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE STRING "Override taoJSON include install directory") + set(TAOCPP_JSON_INSTALL_CMAKE_DIR ${CMAKE_INSTALL_DATAROOTDIR}/json/cmake CACHE STRING "Override taoJSON cmake install directory") + add_subdirectory(external/json) + else() + include(FetchContent) + FetchContent_Declare( + taocpp-json + GIT_REPOSITORY https://github.com/taocpp/json.git + GIT_TAG master + ) + FetchContent_MakeAvailable(taocpp-json) + endif() + if(TAOCPP_CONFIG_INSTALL) + # install and export target + install(TARGETS taocpp-config taocpp-json EXPORT ${PROJECT_NAME}-targets) + endif() + # add taoJSON as a dependency + target_link_libraries(taocpp-config INTERFACE taocpp::json) +else() + if(TAOCPP_CONFIG_INSTALL) + # install and export target + install(TARGETS taocpp-config EXPORT ${PROJECT_NAME}-targets) + endif() + # add taoJSON as a dependency + target_link_libraries(taocpp-config INTERFACE taocpp::taocpp-json) endif() -# add taoJSON as a dependency -target_link_libraries(taocpp-config INTERFACE taocpp::json) # testing option(TAOCPP_CONFIG_BUILD_TESTS "Build test programs" ${TAOCPP_CONFIG_IS_MAIN_PROJECT}) @@ -51,7 +74,6 @@ if(TAOCPP_CONFIG_BUILD_EXAMPLES) add_subdirectory(src/example/config) endif() -option(TAOCPP_CONFIG_INSTALL "Generate the install target" ${TAOCPP_CONFIG_IS_MAIN_PROJECT}) if(TAOCPP_CONFIG_INSTALL) include(CMakePackageConfigHelpers) @@ -68,8 +90,6 @@ if(TAOCPP_CONFIG_INSTALL) # Enable version checks in find_package write_basic_package_version_file(${PROJECT_NAME}-config-version.cmake COMPATIBILITY SameMajorVersion) - # install and export target - install(TARGETS taocpp-config taocpp-json EXPORT ${PROJECT_NAME}-targets) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake From 4c486b65f7c66638a76bfdc076f6854966cd9e4d Mon Sep 17 00:00:00 2001 From: Joerg-Christian Boehme Date: Tue, 20 Apr 2021 23:10:35 +0200 Subject: [PATCH 2/3] Verfiy if cmake version is 3.11 or higher for FetchContent --- CMakeLists.txt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0250904..6df7e8d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.11...3.19) +cmake_minimum_required(VERSION 3.8...3.19) project(taocpp-config VERSION 1.0.0 LANGUAGES CXX) @@ -37,13 +37,17 @@ if(NOT taocpp-json_FOUND) set(TAOCPP_JSON_INSTALL_CMAKE_DIR ${CMAKE_INSTALL_DATAROOTDIR}/json/cmake CACHE STRING "Override taoJSON cmake install directory") add_subdirectory(external/json) else() - include(FetchContent) - FetchContent_Declare( + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.11) + include(FetchContent) + FetchContent_Declare( taocpp-json GIT_REPOSITORY https://github.com/taocpp/json.git GIT_TAG master ) - FetchContent_MakeAvailable(taocpp-json) + FetchContent_MakeAvailable(taocpp-json) + else() + message(FATAL_ERROR "taoJSON headers are not found!\nInstall taoJSON OR\ncheckout the taoJSON project manually into external/json OR\nuse cmake 3.11 or above") + endif() endif() if(TAOCPP_CONFIG_INSTALL) # install and export target From c11299d65d4c63d312039a8c2f87d12bdff5c04b Mon Sep 17 00:00:00 2001 From: Joerg-Christian Boehme Date: Wed, 21 Apr 2021 07:27:44 +0200 Subject: [PATCH 3/3] cmake 3.14.4 or above is needed for FetchContent_MakeAvailable() --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6df7e8d..d346a7a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,7 +37,7 @@ if(NOT taocpp-json_FOUND) set(TAOCPP_JSON_INSTALL_CMAKE_DIR ${CMAKE_INSTALL_DATAROOTDIR}/json/cmake CACHE STRING "Override taoJSON cmake install directory") add_subdirectory(external/json) else() - if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.11) + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.14.4) include(FetchContent) FetchContent_Declare( taocpp-json