Skip to content

Commit

Permalink
Merge branch 'fix/42' into 'devel'
Browse files Browse the repository at this point in the history
Enable externally installed nlohmann-json and spdlog dependencies

Closes #42

See merge request tuda-sc/projects/metacg!64
  • Loading branch information
jplehr committed Apr 1, 2022
2 parents de6f3ab + f327271 commit ce69511
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 84 deletions.
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
56 changes: 16 additions & 40 deletions cmake/ToolchainOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ find_path(EXTRAP_INCLUDE extrap)

include(json)
include(cxxopts)
include(spdlog)

# External dependencies
function(add_clang target)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
42 changes: 17 additions & 25 deletions cmake/json.cmake
Original file line number Diff line number Diff line change
@@ -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()
26 changes: 26 additions & 0 deletions cmake/spdlog.cmake
Original file line number Diff line number Diff line change
@@ -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()
19 changes: 0 additions & 19 deletions cmake/spdlog.cmake.in

This file was deleted.

0 comments on commit ce69511

Please sign in to comment.