diff --git a/CMakeLists.txt b/CMakeLists.txt index 46685cc..ee82fe9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -85,3 +85,17 @@ option( if(WITH_PGIS) add_subdirectory(pgis) endif() + +# If set to on, CMake looks for installed nlohmann::json +option( + METACG_USE_EXTERNAL_JSON + "On or off" + OFF +) + +# if set to on, CMake looks for installed spdlog +option( + METACG_USE_EXTERNAL_SPDLOG + "On or off" + OFF +) diff --git a/cmake/ToolchainOptions.cmake b/cmake/ToolchainOptions.cmake index e30e72c..d8895da 100644 --- a/cmake/ToolchainOptions.cmake +++ b/cmake/ToolchainOptions.cmake @@ -23,6 +23,7 @@ find_path(EXTRAP_INCLUDE extrap) include(json) include(cxxopts) +include(spdlog) # External dependencies function(add_clang target) @@ -51,10 +52,6 @@ function(add_extrap target) target_link_libraries(${target} extrap) endfunction() -function(add_spdlog_libraries target) - target_link_libraries(${target} spdlog::spdlog) -endfunction() - function(add_cube target) target_include_directories(${target} SYSTEM PUBLIC ${CUBE_INCLUDE}) target_link_directories( @@ -209,39 +206,18 @@ if(CMAKE_VERSION ) include_directories("${gtest_SOURCE_DIR}/include") endif() - -# Download and unpack spdlog at configure time -configure_file(cmake/spdlog.cmake.in spdlog-download/CMakeLists.txt) -execute_process( - COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . - RESULT_VARIABLE spdresult - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/spdlog-download -) -if(spdresult) - message(FATAL_ERROR "CMake step for spdlog failed: ${spdresult}") -endif() -execute_process( - COMMAND ${CMAKE_COMMAND} --build . - RESULT_VARIABLE spdresult - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/spdlog-download -) -if(spdresult) - message(FATAL_ERROR "Build step for spdlog failed: ${spdresult}") -endif() - -# Prevent overriding the parent project's compiler/linker settings on Windows -set(spdlog_force_shared_crt - ON - CACHE BOOL - "" - FORCE -) - -# Add spdlog directly to our build. -add_subdirectory( - ${CMAKE_CURRENT_BINARY_DIR}/spdlog-src - ${CMAKE_CURRENT_BINARY_DIR}/spdlog-build - EXCLUDE_FROM_ALL -) - -install(TARGETS spdlog DESTINATION lib) +# +# Download and unpack spdlog at configure time configure_file(cmake/spdlog.cmake spdlog-download/CMakeLists.txt) +# execute_process( COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . RESULT_VARIABLE spdresult WORKING_DIRECTORY +# ${CMAKE_CURRENT_BINARY_DIR}/spdlog-download ) if(spdresult) message(FATAL_ERROR "CMake step for spdlog failed: +# ${spdresult}") endif() execute_process( COMMAND ${CMAKE_COMMAND} --build . RESULT_VARIABLE spdresult WORKING_DIRECTORY +# ${CMAKE_CURRENT_BINARY_DIR}/spdlog-download ) if(spdresult) message(FATAL_ERROR "Build step for spdlog failed: +# ${spdresult}") endif() +# +# Prevent overriding the parent project's compiler/linker settings on Windows set(spdlog_force_shared_crt ON CACHE BOOL +# "" FORCE ) +# +# Add spdlog directly to our build. add_subdirectory( ${CMAKE_CURRENT_BINARY_DIR}/spdlog-src +# ${CMAKE_CURRENT_BINARY_DIR}/spdlog-build EXCLUDE_FROM_ALL ) +# +# install(TARGETS spdlog DESTINATION lib) diff --git a/cmake/json.cmake b/cmake/json.cmake index 7078f5c..e7c84da 100644 --- a/cmake/json.cmake +++ b/cmake/json.cmake @@ -1,31 +1,23 @@ -include(ExternalProject) +include(FetchContent) -if(DEFINED JSON_INCLUDE) - message("JSON_INCLUDE predefined: ${JSON_INCLUDE}") +if(METACG_USE_EXTERNAL_JSON) + message("Using externally found json library") + # Taken from https://cmake.org/cmake/help/v3.16/command/find_package.html#version-selection Should enable to use the + # highest available version number, should the package provide sorting + set(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL) + set(CMAKE_FIND_PACKAGE_SORT_DIRECTION DEC) + find_package( + nlohmann_json + 3.10 + REQUIRED + ) else() - find_path(JSON_LIBRARY NAMES json) - if(JSON_LIBRARY) - set(JSON_INCLUDE ${JSON_LIBRARY}/json/single_include) - message("JSON found in ${JSON_INCLUDE}") - else() - message("JSON library not found, download into extern during make") - ExternalProject_Add( - json - SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/extern/json - GIT_REPOSITORY "https://github.com/nlohmann/json.git" - GIT_TAG master - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - TEST_COMMAND "" - GIT_SHALLOW true - ) - set(JSON_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/extern/json/single_include) - endif() + message("Using fetched release version of json library") + + FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.10.5/json.tar.xz) + FetchContent_MakeAvailable(json) endif() function(add_json target) - add_dependencies(${target} json) - - target_include_directories(${target} SYSTEM PUBLIC ${JSON_INCLUDE}) + target_link_libraries(${target} nlohmann_json::nlohmann_json) endfunction() diff --git a/cmake/spdlog.cmake b/cmake/spdlog.cmake new file mode 100644 index 0000000..424785a --- /dev/null +++ b/cmake/spdlog.cmake @@ -0,0 +1,26 @@ +include(FetchContent) + +if(METACG_USE_EXTERNAL_SPDLOG) + message("Using externally found spdlog library") + find_package( + spdlog + 1.8.2 + REQUIRED + ) +else() + message("Using fetched release version of spdlog library") + + FetchContent_Declare(spdlog URL https://github.com/gabime/spdlog/archive/refs/tags/v1.8.2.tar.gz) + FetchContent_MakeAvailable(spdlog) + + # Only install when spdlog is desired as automatically downloaded library + install( + TARGETS spdlog + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib + ) +endif() + +function(add_spdlog_libraries target) + target_link_libraries(${target} spdlog::spdlog) +endfunction() diff --git a/cmake/spdlog.cmake.in b/cmake/spdlog.cmake.in deleted file mode 100644 index 60cc3ea..0000000 --- a/cmake/spdlog.cmake.in +++ /dev/null @@ -1,19 +0,0 @@ -cmake_minimum_required(VERSION 3.2) - -project(spdlog-download NONE) - -include(ExternalProject) -# cmake-format: off -ExternalProject_Add( - spdlog - GIT_REPOSITORY https://github.com/gabime/spdlog.git - GIT_TAG v1.8.2 - SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/spdlog-src" - BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/spdlog-build" - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - TEST_COMMAND "" - CMAKE_ARGS -DSPDLOG_BUILD_SHARED=ON -) -# cmake-format: on