From e1a815f35086c26e320cd7eadbdb7525ceaf08e4 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 22 Sep 2022 11:21:52 +0200 Subject: [PATCH 01/19] Rename MAIN_PROJECT to JSON_MAIN_PROJECT --- CMakeLists.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f942e04ab..b8fc6b57c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,12 +7,12 @@ cmake_minimum_required(VERSION 3.1) project(nlohmann_json VERSION 3.11.2 LANGUAGES CXX) ## -## MAIN_PROJECT CHECK +## JSON_MAIN_PROJECT CHECK ## determine if nlohmann_json is built as a subproject (using add_subdirectory) or if it is the main project ## -set(MAIN_PROJECT OFF) +set(JSON_MAIN_PROJECT OFF) if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) - set(MAIN_PROJECT ON) + set(JSON_MAIN_PROJECT ON) endif() ## @@ -32,7 +32,7 @@ if (POLICY CMP0077) endif () # VERSION_GREATER_EQUAL is not available in CMake 3.1 -if(${MAIN_PROJECT} AND (${CMAKE_VERSION} VERSION_EQUAL 3.13 OR ${CMAKE_VERSION} VERSION_GREATER 3.13)) +if(${JSON_MAIN_PROJECT} AND (${CMAKE_VERSION} VERSION_EQUAL 3.13 OR ${CMAKE_VERSION} VERSION_GREATER 3.13)) set(JSON_BuildTests_INIT ON) else() set(JSON_BuildTests_INIT OFF) @@ -44,7 +44,7 @@ option(JSON_GlobalUDLs "Place use-defined string literals in option(JSON_ImplicitConversions "Enable implicit conversions." ON) option(JSON_DisableEnumSerialization "Disable default integer enum serialization." OFF) option(JSON_LegacyDiscardedValueComparison "Enable legacy discarded value comparison." OFF) -option(JSON_Install "Install CMake targets during install step." ${MAIN_PROJECT}) +option(JSON_Install "Install CMake targets during install step." ${JSON_MAIN_PROJECT}) option(JSON_MultipleHeaders "Use non-amalgamated version of the library." ON) option(JSON_SystemInclude "Include as system headers (skip for clang-tidy)." OFF) From c14c48430324f7c38ef8954481530485613fc908 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 22 Sep 2022 11:40:10 +0200 Subject: [PATCH 02/19] Move CMake policies --- CMakeLists.txt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b8fc6b57c..71fb12242 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,14 @@ cmake_minimum_required(VERSION 3.1) +## +## POLICIES +## + +if (POLICY CMP0077) + # Allow CMake 3.13+ to override options when using FetchContent / add_subdirectory. + cmake_policy(SET CMP0077 NEW) +endif () + ## ## PROJECT ## name and version @@ -26,11 +35,6 @@ include(ExternalProject) ## OPTIONS ## -if (POLICY CMP0077) - # Allow CMake 3.13+ to override options when using FetchContent / add_subdirectory. - cmake_policy(SET CMP0077 NEW) -endif () - # VERSION_GREATER_EQUAL is not available in CMake 3.1 if(${JSON_MAIN_PROJECT} AND (${CMAKE_VERSION} VERSION_EQUAL 3.13 OR ${CMAKE_VERSION} VERSION_GREATER 3.13)) set(JSON_BuildTests_INIT ON) From 475c8d34b7db9380dcf4288e46be17a23ac80667 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 22 Sep 2022 12:02:35 +0200 Subject: [PATCH 03/19] Move CMake options into cmake/json_opts.cmake --- CMakeLists.txt | 18 +------------ cmake/json_opts.cmake | 63 +++++++++++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 6 ----- 3 files changed, 64 insertions(+), 23 deletions(-) create mode 100644 cmake/json_opts.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 71fb12242..1d689298b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,23 +34,7 @@ include(ExternalProject) ## ## OPTIONS ## - -# VERSION_GREATER_EQUAL is not available in CMake 3.1 -if(${JSON_MAIN_PROJECT} AND (${CMAKE_VERSION} VERSION_EQUAL 3.13 OR ${CMAKE_VERSION} VERSION_GREATER 3.13)) - set(JSON_BuildTests_INIT ON) -else() - set(JSON_BuildTests_INIT OFF) -endif() -option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enabled." ${JSON_BuildTests_INIT}) -option(JSON_CI "Enable CI build targets." OFF) -option(JSON_Diagnostics "Use extended diagnostic messages." OFF) -option(JSON_GlobalUDLs "Place use-defined string literals in the global namespace." ON) -option(JSON_ImplicitConversions "Enable implicit conversions." ON) -option(JSON_DisableEnumSerialization "Disable default integer enum serialization." OFF) -option(JSON_LegacyDiscardedValueComparison "Enable legacy discarded value comparison." OFF) -option(JSON_Install "Install CMake targets during install step." ${JSON_MAIN_PROJECT}) -option(JSON_MultipleHeaders "Use non-amalgamated version of the library." ON) -option(JSON_SystemInclude "Include as system headers (skip for clang-tidy)." OFF) +include (json_opts) if (JSON_CI) include(ci) diff --git a/cmake/json_opts.cmake b/cmake/json_opts.cmake new file mode 100644 index 000000000..ea5e59238 --- /dev/null +++ b/cmake/json_opts.cmake @@ -0,0 +1,63 @@ +include(CMakeDependentOption) + +############################################################################# +# test options +############################################################################# + +# VERSION_GREATER_EQUAL is not available in CMake 3.1 +if(${JSON_MAIN_PROJECT} AND (${CMAKE_VERSION} VERSION_EQUAL 3.13 OR ${CMAKE_VERSION} VERSION_GREATER 3.13)) + set(JSON_BuildTests_INIT ON) +else() + set(JSON_BuildTests_INIT OFF) +endif() +option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enabled." ${JSON_BuildTests_INIT}) +set(JSON_32bitTest AUTO CACHE STRING "Enable the 32bit unit test (ON/OFF/AUTO/ONLY).") +cmake_dependent_option(JSON_FastTests "Skip expensive/slow tests." OFF "JSON_BuildTests" OFF) +cmake_dependent_option(JSON_Valgrind "Execute test suite with Valgrind." OFF "JSON_BuildTests" OFF) + +set(JSON_TestStandards "" CACHE STRING "The list of standards to test explicitly.") + +############################################################################# +# CI options +############################################################################# + +option(JSON_CI "Enable CI build targets." OFF) + +############################################################################# +# build & install options +############################################################################# + +option(JSON_Diagnostics "Use extended diagnostic messages." OFF) +option(JSON_DisableEnumSerialization "Disable default integer enum serialization." OFF) +option(JSON_GlobalUDLs "Place user-defined string literals in the global namespace." ON) +option(JSON_ImplicitConversions "Enable implicit conversions." ON) +option(JSON_LegacyDiscardedValueComparison "Enable legacy discarded value comparison." OFF) + +option(JSON_Install "Install CMake targets during install step." ${JSON_MAIN_PROJECT}) +option(JSON_MultipleHeaders "Use non-amalgamated version of the library." ON) +option(JSON_SystemInclude "Include as system headers (skip for clang-tidy)." OFF) + +############################################################################# +# configuration +############################################################################# + +set(NLOHMANN_JSON_TARGET_NAME ${PROJECT_NAME}) +set(NLOHMANN_JSON_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}" CACHE INTERNAL "") +set(NLOHMANN_JSON_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}") +set(NLOHMANN_JSON_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets") +set(NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE "cmake/config.cmake.in") +set(NLOHMANN_JSON_CMAKE_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}") +set(NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}ConfigVersion.cmake") +set(NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Config.cmake") +set(NLOHMANN_JSON_CMAKE_PROJECT_TARGETS_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Targets.cmake") +set(NLOHMANN_JSON_PKGCONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/pkgconfig") + +if (JSON_MultipleHeaders) + set(NLOHMANN_JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/include/") +else() + set(NLOHMANN_JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/single_include/") +endif() + +if (JSON_SystemInclude) + set(NLOHMANN_JSON_SYSTEM_INCLUDE "SYSTEM") +endif() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1afb000ae..99f0d3efd 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,11 +1,5 @@ cmake_minimum_required(VERSION 3.13) -option(JSON_Valgrind "Execute test suite with Valgrind." OFF) -option(JSON_FastTests "Skip expensive/slow tests." OFF) - -set(JSON_32bitTest AUTO CACHE STRING "Enable the 32bit unit test (ON/OFF/AUTO/ONLY).") -set(JSON_TestStandards "" CACHE STRING "The list of standards to test explicitly.") - include(test) ############################################################################# From d30cd9d4d330b1ed431bc66cac4b24d67ce95324 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 22 Sep 2022 13:47:31 +0200 Subject: [PATCH 04/19] Add custom CMake feature summary --- CMakeLists.txt | 20 +--------- cmake/json_summary.cmake | 82 ++++++++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 18 ++++----- 3 files changed, 91 insertions(+), 29 deletions(-) create mode 100644 cmake/json_summary.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d689298b..dbde03798 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,26 +58,8 @@ set(NLOHMANN_JSON_PKGCONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/pkgconfig" if (JSON_MultipleHeaders) set(NLOHMANN_JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/include/") - message(STATUS "Using the multi-header code from ${NLOHMANN_JSON_INCLUDE_BUILD_DIR}") else() set(NLOHMANN_JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/single_include/") - message(STATUS "Using the single-header code from ${NLOHMANN_JSON_INCLUDE_BUILD_DIR}") -endif() - -if (NOT JSON_ImplicitConversions) - message(STATUS "Implicit conversions are disabled") -endif() - -if (JSON_DisableEnumSerialization) - message(STATUS "Enum integer serialization is disabled") -endif() - -if (JSON_LegacyDiscardedValueComparison) - message(STATUS "Legacy discarded value comparison enabled") -endif() - -if (JSON_Diagnostics) - message(STATUS "Diagnostics enabled") endif() if (JSON_SystemInclude) @@ -195,3 +177,5 @@ if(JSON_Install) DESTINATION ${NLOHMANN_JSON_PKGCONFIG_INSTALL_DIR} ) endif() + +include(json_summary) diff --git a/cmake/json_summary.cmake b/cmake/json_summary.cmake new file mode 100644 index 000000000..b20210f1b --- /dev/null +++ b/cmake/json_summary.cmake @@ -0,0 +1,82 @@ +# ANSI codes +set(rst "") +set(bld "") +# no color output on Windows or if disabled via CLICOLOR=0/CLICOLOR_FORCE=0 +if(NOT WIN32 AND NOT ("$ENV{CLICOLOR}" STREQUAL "0" OR "$ENV{CLICOLOR_FORCE}" STREQUAL "0")) + string(ASCII 27 esc) + set(rst "${esc}[0m") # reset + set(bld "${esc}[1m") # bold +endif() + +############################################################################# +# json_feature( +# var text +# [VALUES ...] +# [NEGATE]) +# +# Print feature info using and the boolean value of converted to +# YES/NO. +# +# If additional values are given and matches any of them, is not +# converted to YES/NO. +# +# If NEGATE is specified, the boolean value of is negated. +############################################################################# + +function(json_feature var text) + cmake_parse_arguments(args "NEGATE" "" "VALUES" ${ARGN}) + + set(state NO) + if(args_VALUES) + foreach(value ${args_VALUES}) + if(${var} STREQUAL value) + set(state ${value}) + break() + endif() + endforeach() + elseif(${args_NEGATE} AND NOT ${var} OR ${var}) + set(state YES) + endif() + + message(" ${text} ${bld}${state}${rst}") +endfunction() + +############################################################################# +# print feature summary +############################################################################# + +message(STATUS "[nohmann_json]: Feature summary:") + +json_feature(JSON_BuildTests "Build tests?") +if(JSON_BuildTests) + json_feature(JSON_32bitTest "Build the 32bit unit test?" VALUES AUTO ONLY) + json_feature(JSON_FastTests "Skip expensive/slow tests?") + json_feature(JSON_Valgrind "Execute test suite with Valgrind?") + + set(test_cxx_standards "") + foreach(cxx_standard ${JSON_TEST_CXX_STANDARDS_FEATURE_INFO}) + if(NOT cxx_standard MATCHES "^[\[].+[\]]$") + set(cxx_standard "${bld}${cxx_standard}${rst}") + endif() + set(test_cxx_standards "${test_cxx_standards} ${cxx_standard}") + endforeach() + + if(JSON_TEST_CXX_STANDARDS_FORCED) + set(test_cxx_standards "${test_cxx_standards} ${bld}(forced)${rst}") + endif() + message(" Test C++ standards:${test_cxx_standards}") +endif() + +message("") + +json_feature(JSON_Diagnostics "Diagnostics enabled?" NEGATE) +json_feature(JSON_DisableEnumSerialization "Default integer enum serialization enabled?") +json_feature(JSON_GlobalUDLs "Define user-defined string literals globally?") +json_feature(JSON_ImplicitConversions "Implicit conversions enabled?") +json_feature(JSON_LegacyDiscardedValueComparison "Legacy discarded value comparison enabled?") + +message("") + +json_feature(JSON_MultipleHeaders "Use the multi-header code?") +message(" Include directory: ${NLOHMANN_JSON_INCLUDE_BUILD_DIR}") +json_feature(JSON_SystemInclude "Include as system headers?") diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 99f0d3efd..d85db0451 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -95,27 +95,23 @@ json_test_set_test_options(test-unicode4 TEST_PROPERTIES TIMEOUT 3000) if("${JSON_TestStandards}" STREQUAL "") set(test_cxx_standards 11 14 17 20 23) - unset(test_force) + set(test_force "") else() set(test_cxx_standards ${JSON_TestStandards}) set(test_force FORCE) endif() -# Print selected standards marking unavailable ones with brackets -set(msg_standards "") +# create list of selected C++ standards for feature summary +set(info "") foreach(cxx_standard ${test_cxx_standards}) if(compiler_supports_cpp_${cxx_standard}) - list(APPEND msg_standards ${cxx_standard}) + list(APPEND info ${cxx_standard}) else() - list(APPEND msg_standards [${cxx_standard}]) + list(APPEND info [${cxx_standard}]) endif() endforeach() -string(JOIN " " msg_standards ${msg_standards}) -set(msg "Testing standards: ${msg_standards}") -if(test_force) - string(APPEND msg " (forced)") -endif() -message(STATUS "${msg}") +set(JSON_TEST_CXX_STANDARDS_FEATURE_INFO "${info}" PARENT_SCOPE) +set(JSON_TEST_CXX_STANDARDS_FORCED ${test_force} PARENT_SCOPE) # *DO* use json_test_set_test_options() above this line From 84b8c29b91a2e79cf2d317f46bcdbad4598d12ce Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 22 Sep 2022 15:04:02 +0200 Subject: [PATCH 05/19] Move CMake system info into cmake/json_info.cmake --- CMakeLists.txt | 2 ++ cmake/download_test_data.cmake | 37 ------------------------ cmake/json_info.cmake | 51 ++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 37 deletions(-) create mode 100644 cmake/json_info.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index dbde03798..9ec00fa5f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,8 @@ endif() set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) include(ExternalProject) +include(json_info) + ## ## OPTIONS ## diff --git a/cmake/download_test_data.cmake b/cmake/download_test_data.cmake index 1bb998dae..224d7db2c 100644 --- a/cmake/download_test_data.cmake +++ b/cmake/download_test_data.cmake @@ -17,40 +17,3 @@ else() # create a header with the path to the downloaded test data file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${CMAKE_BINARY_DIR}/json_test_data\"\n") endif() - -# determine the operating system (for debug and support purposes) -find_program(UNAME_COMMAND uname) -find_program(VER_COMMAND ver) -find_program(LSB_RELEASE_COMMAND lsb_release) -find_program(SW_VERS_COMMAND sw_vers) -set(OS_VERSION_STRINGS "${CMAKE_SYSTEM}") -if (VER_COMMAND) - execute_process(COMMAND ${VER_COMMAND} OUTPUT_VARIABLE VER_COMMAND_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE) - set(OS_VERSION_STRINGS "${OS_VERSION_STRINGS}; ${VER_COMMAND_RESULT}") -endif() -if (SW_VERS_COMMAND) - execute_process(COMMAND ${SW_VERS_COMMAND} OUTPUT_VARIABLE SW_VERS_COMMAND_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) - string(REGEX REPLACE "[ ]*\n" "; " SW_VERS_COMMAND_RESULT "${SW_VERS_COMMAND_RESULT}") - set(OS_VERSION_STRINGS "${OS_VERSION_STRINGS}; ${SW_VERS_COMMAND_RESULT}") -endif() -if (LSB_RELEASE_COMMAND) - execute_process(COMMAND ${LSB_RELEASE_COMMAND} -a OUTPUT_VARIABLE LSB_RELEASE_COMMAND_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) - string(REGEX REPLACE "[ ]*\n" "; " LSB_RELEASE_COMMAND_RESULT "${LSB_RELEASE_COMMAND_RESULT}") - set(OS_VERSION_STRINGS "${OS_VERSION_STRINGS}; ${LSB_RELEASE_COMMAND_RESULT}") -endif() -if (UNAME_COMMAND) - execute_process(COMMAND ${UNAME_COMMAND} -a OUTPUT_VARIABLE UNAME_COMMAND_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) - set(OS_VERSION_STRINGS "${OS_VERSION_STRINGS}; ${UNAME_COMMAND_RESULT}") -endif() - -message(STATUS "Operating system: ${OS_VERSION_STRINGS}") - -# determine the compiler (for debug and support purposes) -if (MSVC) - execute_process(COMMAND ${CMAKE_CXX_COMPILER} OUTPUT_VARIABLE CXX_VERSION_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_VARIABLE CXX_VERSION_RESULT ERROR_STRIP_TRAILING_WHITESPACE) - set(CXX_VERSION_RESULT "${CXX_VERSION_RESULT}; MSVC_VERSION=${MSVC_VERSION}; MSVC_TOOLSET_VERSION=${MSVC_TOOLSET_VERSION}") -else() - execute_process(COMMAND ${CMAKE_CXX_COMPILER} --version OUTPUT_VARIABLE CXX_VERSION_RESULT OUTPUT_STRIP_TRAILING_WHITESPACE) -endif() -string(REGEX REPLACE "[ ]*\n" "; " CXX_VERSION_RESULT "${CXX_VERSION_RESULT}") -message(STATUS "Compiler: ${CXX_VERSION_RESULT}") diff --git a/cmake/json_info.cmake b/cmake/json_info.cmake new file mode 100644 index 000000000..b028a446b --- /dev/null +++ b/cmake/json_info.cmake @@ -0,0 +1,51 @@ +############################################################################# +# cmake info +############################################################################# + +message(STATUS "[nohmann_json]: CMake ${CMAKE_VERSION}") + +############################################################################# +# system info +############################################################################# + +set(distrib_name "") +if(CMAKE_VERSION VERSION_EQUAL "3.22" OR CMAKE_VERSION VERSION_GREATER "3.22") + cmake_host_system_information(RESULT distrib_name QUERY DISTRIB_PRETTY_NAME) + if(distrib_name) + set(distrib_name " (${distrib_name})") + endif() +endif() + +message(STATUS "[nohmann_json]: Host system: ${CMAKE_HOST_SYSTEM}${distrib_name}") +if(NOT CMAKE_SYSTEM STREQUAL CMAKE_HOST_SYSTEM) + message(STATUS "[nohmann_json]: Target system: ${CMAKE_SYSTEM}") +endif() + +if(DEFINED ENV{CI}) + # print additional info in CI environment + cmake_host_system_information(RESULT num_cores QUERY NUMBER_OF_PHYSICAL_CORES) + cmake_host_system_information(RESULT num_threads QUERY NUMBER_OF_LOGICAL_CORES) + if(num_threads) + set(num_threads " (${num_threads})") + endif() + if(num_cores) + message(STATUS "[nohmann_json]: Processor cores: ${num_cores}${num_threads}") + endif() +endif() + +############################################################################# +# compiler info +############################################################################# + +execute_process(COMMAND ${CMAKE_CXX_COMPILER} --version OUTPUT_VARIABLE cxx_version_result OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_VARIABLE cxx_version_result_error ERROR_STRIP_TRAILING_WHITESPACE) +if(NOT cxx_version_result_error) + string(REGEX REPLACE ";" "\\\\;" cxx_version_result "${cxx_version_result}") + string(REGEX REPLACE "\n" ";" cxx_version_result "${cxx_version_result}") + list(GET cxx_version_result 0 cxx_version_result) + message(STATUS "[nohmann_json]: C++ compiler: ${cxx_version_result}") +endif() + +if(MSVC) + message(STATUS "[nohmann_json]: MSVC version: ${MSVC_VERSION}") + message(STATUS "[nohmann_json]: MSVC toolset version: ${MSVC_TOOLSET_VERSION}") +endif() From 67826ba64813a5439c28d2e3d1bfd52d3ff04b8a Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 22 Sep 2022 19:18:38 +0200 Subject: [PATCH 06/19] Move CMake configuration section into cmake/json_opts.cmake --- CMakeLists.txt | 26 -------------------------- cmake/json_opts.cmake | 1 + 2 files changed, 1 insertion(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ec00fa5f..074960792 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,32 +42,6 @@ if (JSON_CI) include(ci) endif () -## -## CONFIGURATION -## -include(GNUInstallDirs) - -set(NLOHMANN_JSON_TARGET_NAME ${PROJECT_NAME}) -set(NLOHMANN_JSON_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}" CACHE INTERNAL "") -set(NLOHMANN_JSON_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}") -set(NLOHMANN_JSON_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets") -set(NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE "cmake/config.cmake.in") -set(NLOHMANN_JSON_CMAKE_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}") -set(NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}ConfigVersion.cmake") -set(NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Config.cmake") -set(NLOHMANN_JSON_CMAKE_PROJECT_TARGETS_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Targets.cmake") -set(NLOHMANN_JSON_PKGCONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/pkgconfig") - -if (JSON_MultipleHeaders) - set(NLOHMANN_JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/include/") -else() - set(NLOHMANN_JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/single_include/") -endif() - -if (JSON_SystemInclude) - set(NLOHMANN_JSON_SYSTEM_INCLUDE "SYSTEM") -endif() - ## ## TARGET ## create target and add include path diff --git a/cmake/json_opts.cmake b/cmake/json_opts.cmake index ea5e59238..f22680fec 100644 --- a/cmake/json_opts.cmake +++ b/cmake/json_opts.cmake @@ -1,4 +1,5 @@ include(CMakeDependentOption) +include(GNUInstallDirs) ############################################################################# # test options From 601b5913ed87ca7e63cfd4e0833650e81ac280ca Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 22 Sep 2022 19:52:27 +0200 Subject: [PATCH 07/19] Reorganize main CMake list file --- CMakeLists.txt | 67 +++++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 074960792..cf9184080 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,51 +1,42 @@ cmake_minimum_required(VERSION 3.1) -## -## POLICIES -## +############################################################################# +# set up +############################################################################# +# set policies if (POLICY CMP0077) # Allow CMake 3.13+ to override options when using FetchContent / add_subdirectory. cmake_policy(SET CMP0077 NEW) endif () -## -## PROJECT -## name and version -## -project(nlohmann_json VERSION 3.11.2 LANGUAGES CXX) +# set the CMake module path +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) -## -## JSON_MAIN_PROJECT CHECK -## determine if nlohmann_json is built as a subproject (using add_subdirectory) or if it is the main project -## +# determine if nlohmann_json is the main project or built as a subproject (using add_subdirectory) set(JSON_MAIN_PROJECT OFF) if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) set(JSON_MAIN_PROJECT ON) endif() -## -## INCLUDE -## -## -set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) -include(ExternalProject) +# set project name and version +project(nlohmann_json VERSION 3.11.2 LANGUAGES CXX) +# print CMake, system, and compiler information include(json_info) -## -## OPTIONS -## -include (json_opts) +# handle options and configuration +include(json_opts) -if (JSON_CI) +# add CI targets +if(JSON_CI) include(ci) -endif () +endif() + +############################################################################# +# add library targets +############################################################################# -## -## TARGET -## create target and add include path -## add_library(${NLOHMANN_JSON_TARGET_NAME} INTERFACE) add_library(${PROJECT_NAME}::${NLOHMANN_JSON_TARGET_NAME} ALIAS ${NLOHMANN_JSON_TARGET_NAME}) if (${CMAKE_VERSION} VERSION_LESS "3.8.0") @@ -89,20 +80,20 @@ CONFIGURE_FILE( "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" ) -## -## TESTS -## create and configure the unit test target -## +############################################################################# +# add tests +############################################################################# + if (JSON_BuildTests) include(CTest) enable_testing() add_subdirectory(tests) endif() -## -## INSTALL -## install header files, generate and install cmake config files for find_package() -## +############################################################################# +# install files and targets +############################################################################# + include(CMakePackageConfigHelpers) # use a custom package version config file instead of # write_basic_package_version_file to ensure that it's architecture-independent @@ -154,4 +145,8 @@ if(JSON_Install) ) endif() +############################################################################# +# print feature summary +############################################################################# + include(json_summary) From e12968280719f66b48b6ba1f3de951b0c3125d4c Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 22 Sep 2022 21:34:23 +0200 Subject: [PATCH 08/19] Merge cmake/download_test_data.cmake into cmake/test.cmake --- cmake/download_test_data.cmake | 19 ------------------- cmake/json_opts.cmake | 9 +++++++++ cmake/json_summary.cmake | 10 ++++++++++ cmake/test.cmake | 16 +++++++++++++++- 4 files changed, 34 insertions(+), 20 deletions(-) delete mode 100644 cmake/download_test_data.cmake diff --git a/cmake/download_test_data.cmake b/cmake/download_test_data.cmake deleted file mode 100644 index 224d7db2c..000000000 --- a/cmake/download_test_data.cmake +++ /dev/null @@ -1,19 +0,0 @@ -set(JSON_TEST_DATA_URL https://github.com/nlohmann/json_test_data) -set(JSON_TEST_DATA_VERSION 3.1.0) - -# if variable is set, use test data from given directory rather than downloading them -if(JSON_TestDataDirectory) - message(STATUS "Using test data in ${JSON_TestDataDirectory}.") - add_custom_target(download_test_data) - file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${JSON_TestDataDirectory}\"\n") -else() - find_package(Git) - # target to download test data - add_custom_target(download_test_data - COMMAND test -d json_test_data || ${GIT_EXECUTABLE} clone -c advice.detachedHead=false --branch v${JSON_TEST_DATA_VERSION} ${JSON_TEST_DATA_URL}.git --quiet --depth 1 - COMMENT "Downloading test data from ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})" - WORKING_DIRECTORY ${CMAKE_BINARY_DIR} - ) - # create a header with the path to the downloaded test data - file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${CMAKE_BINARY_DIR}/json_test_data\"\n") -endif() diff --git a/cmake/json_opts.cmake b/cmake/json_opts.cmake index f22680fec..3483b15f3 100644 --- a/cmake/json_opts.cmake +++ b/cmake/json_opts.cmake @@ -62,3 +62,12 @@ endif() if (JSON_SystemInclude) set(NLOHMANN_JSON_SYSTEM_INCLUDE "SYSTEM") endif() + +if(JSON_TestDataDirectory) + set(JSON_TEST_DATA_DIRECTORY "${JSON_TestDataDirectory}" CACHE INTERNAL "") +elseif(DEFINED ENV{JSON_TEST_DATA_DIRECTORY}) + set(JSON_TEST_DATA_DIRECTORY "$ENV{JSON_TEST_DATA_DIRECTORY}" CACHE INTERNAL "") +endif() + +set(JSON_TEST_DATA_URL https://github.com/nlohmann/json_test_data CACHE INTERNAL "") +set(JSON_TEST_DATA_VERSION 3.1.0 CACHE INTERNAL "") diff --git a/cmake/json_summary.cmake b/cmake/json_summary.cmake index b20210f1b..3abbe1a27 100644 --- a/cmake/json_summary.cmake +++ b/cmake/json_summary.cmake @@ -51,7 +51,17 @@ json_feature(JSON_BuildTests "Build tests?") if(JSON_BuildTests) json_feature(JSON_32bitTest "Build the 32bit unit test?" VALUES AUTO ONLY) json_feature(JSON_FastTests "Skip expensive/slow tests?") + + if(JSON_TEST_DATA_DIRECTORY) + message(" Test data: ${JSON_TEST_DATA_DIRECTORY}") + else() + message(" Test data: ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})") + endif() + json_feature(JSON_Valgrind "Execute test suite with Valgrind?") + if(JSON_Valgrind) + message(" Valgrind command: ${CMAKE_MEMORYCHECK_COMMAND}" ${CMAKE_MEMORYCHECK_COMMAND_OPTIONS}) + endif() set(test_cxx_standards "") foreach(cxx_standard ${JSON_TEST_CXX_STANDARDS_FEATURE_INFO}) diff --git a/cmake/test.cmake b/cmake/test.cmake index bb840c6c0..8a0240a06 100644 --- a/cmake/test.cmake +++ b/cmake/test.cmake @@ -4,7 +4,21 @@ set(_json_test_cmake_list_file ${CMAKE_CURRENT_LIST_FILE}) # download test data ############################################################################# -include(download_test_data) +# if variable is set, use test data from given directory rather than downloading them +if(JSON_TEST_DATA_DIRECTORY) + add_custom_target(download_test_data) + file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${JSON_TEST_DATA_DIRECTORY}\"\n") +else() + find_package(Git REQUIRED) + # target to download test data + add_custom_target(download_test_data + COMMAND test -d json_test_data || ${GIT_EXECUTABLE} clone -c advice.detachedHead=false --branch v${JSON_TEST_DATA_VERSION} ${JSON_TEST_DATA_URL}.git --quiet --depth 1 + COMMENT "Downloading test data from ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})" + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + ) + # create a header with the path to the downloaded test data + file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${CMAKE_BINARY_DIR}/json_test_data\"\n") +endif() # test fixture to download test data add_test(NAME "download_test_data" COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} From 495176d67b1d613ef47d23be929e86870c49b640 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 22 Sep 2022 21:37:00 +0200 Subject: [PATCH 09/19] Rename cmake/test.cmake to cmake/json_test.cmake --- cmake/{test.cmake => json_test.cmake} | 0 tests/CMakeLists.txt | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename cmake/{test.cmake => json_test.cmake} (100%) diff --git a/cmake/test.cmake b/cmake/json_test.cmake similarity index 100% rename from cmake/test.cmake rename to cmake/json_test.cmake diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d85db0451..ab476fc80 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.13) -include(test) +include(json_test) ############################################################################# # override standard support From 18171c156c2e0e998a9631d6f22a09d828f7115f Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 22 Sep 2022 21:38:05 +0200 Subject: [PATCH 10/19] Rename cmake/ci.cmake to cmake/json_ci.cmake --- CMakeLists.txt | 2 +- cmake/{ci.cmake => json_ci.cmake} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename cmake/{ci.cmake => json_ci.cmake} (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index cf9184080..667a7dbf1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,7 @@ include(json_opts) # add CI targets if(JSON_CI) - include(ci) + include(json_ci) endif() ############################################################################# diff --git a/cmake/ci.cmake b/cmake/json_ci.cmake similarity index 100% rename from cmake/ci.cmake rename to cmake/json_ci.cmake From 9fc6d2b0127ac0bb40bc09e0dfe609361c2f01aa Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sat, 24 Sep 2022 11:57:52 +0200 Subject: [PATCH 11/19] Move CMake CI targets into tests section --- CMakeLists.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 667a7dbf1..f77b33870 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,11 +28,6 @@ include(json_info) # handle options and configuration include(json_opts) -# add CI targets -if(JSON_CI) - include(json_ci) -endif() - ############################################################################# # add library targets ############################################################################# @@ -90,6 +85,11 @@ if (JSON_BuildTests) add_subdirectory(tests) endif() +# add CI targets +if(JSON_CI) + include(json_ci) +endif() + ############################################################################# # install files and targets ############################################################################# From 9ea2eb70279819fdc9e13387b361ad08b61b27d9 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sat, 24 Sep 2022 13:49:16 +0200 Subject: [PATCH 12/19] Refactor CMake test data directory code * Add a cache variable and set default from environment. * Add '.git' suffix to URL variable. --- cmake/json_opts.cmake | 11 +++-------- cmake/json_summary.cmake | 6 +++--- cmake/json_test.cmake | 6 +++--- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/cmake/json_opts.cmake b/cmake/json_opts.cmake index 3483b15f3..b79e1c9f6 100644 --- a/cmake/json_opts.cmake +++ b/cmake/json_opts.cmake @@ -14,6 +14,7 @@ endif() option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enabled." ${JSON_BuildTests_INIT}) set(JSON_32bitTest AUTO CACHE STRING "Enable the 32bit unit test (ON/OFF/AUTO/ONLY).") cmake_dependent_option(JSON_FastTests "Skip expensive/slow tests." OFF "JSON_BuildTests" OFF) +set(JSON_TestDataDirectory "$ENV{JSON_TEST_DATA_DIRECTORY}" CACHE FILEPATH "Test data directory for the unit tests (will be downloaded if not specified).") cmake_dependent_option(JSON_Valgrind "Execute test suite with Valgrind." OFF "JSON_BuildTests" OFF) set(JSON_TestStandards "" CACHE STRING "The list of standards to test explicitly.") @@ -63,11 +64,5 @@ if (JSON_SystemInclude) set(NLOHMANN_JSON_SYSTEM_INCLUDE "SYSTEM") endif() -if(JSON_TestDataDirectory) - set(JSON_TEST_DATA_DIRECTORY "${JSON_TestDataDirectory}" CACHE INTERNAL "") -elseif(DEFINED ENV{JSON_TEST_DATA_DIRECTORY}) - set(JSON_TEST_DATA_DIRECTORY "$ENV{JSON_TEST_DATA_DIRECTORY}" CACHE INTERNAL "") -endif() - -set(JSON_TEST_DATA_URL https://github.com/nlohmann/json_test_data CACHE INTERNAL "") -set(JSON_TEST_DATA_VERSION 3.1.0 CACHE INTERNAL "") +set(JSON_TEST_DATA_URL https://github.com/nlohmann/json_test_data.git) +set(JSON_TEST_DATA_VERSION 3.1.0) diff --git a/cmake/json_summary.cmake b/cmake/json_summary.cmake index 3abbe1a27..58075f97e 100644 --- a/cmake/json_summary.cmake +++ b/cmake/json_summary.cmake @@ -52,10 +52,10 @@ if(JSON_BuildTests) json_feature(JSON_32bitTest "Build the 32bit unit test?" VALUES AUTO ONLY) json_feature(JSON_FastTests "Skip expensive/slow tests?") - if(JSON_TEST_DATA_DIRECTORY) - message(" Test data: ${JSON_TEST_DATA_DIRECTORY}") + if(JSON_TestDataDirectory) + message(" Test data directory: ${JSON_TestDataDirectory}") else() - message(" Test data: ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})") + message(" Test data source: ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})") endif() json_feature(JSON_Valgrind "Execute test suite with Valgrind?") diff --git a/cmake/json_test.cmake b/cmake/json_test.cmake index 8a0240a06..6ece47d59 100644 --- a/cmake/json_test.cmake +++ b/cmake/json_test.cmake @@ -5,14 +5,14 @@ set(_json_test_cmake_list_file ${CMAKE_CURRENT_LIST_FILE}) ############################################################################# # if variable is set, use test data from given directory rather than downloading them -if(JSON_TEST_DATA_DIRECTORY) +if(JSON_TestDataDirectory) add_custom_target(download_test_data) - file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${JSON_TEST_DATA_DIRECTORY}\"\n") + file(WRITE ${CMAKE_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${JSON_TestDataDirectory}\"\n") else() find_package(Git REQUIRED) # target to download test data add_custom_target(download_test_data - COMMAND test -d json_test_data || ${GIT_EXECUTABLE} clone -c advice.detachedHead=false --branch v${JSON_TEST_DATA_VERSION} ${JSON_TEST_DATA_URL}.git --quiet --depth 1 + COMMAND test -d json_test_data || ${GIT_EXECUTABLE} clone -c advice.detachedHead=false --branch v${JSON_TEST_DATA_VERSION} ${JSON_TEST_DATA_URL} --quiet --depth 1 COMMENT "Downloading test data from ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})" WORKING_DIRECTORY ${CMAKE_BINARY_DIR} ) From 61684f8fb7fcab26aba6719f9ef3182202ede5a6 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Fri, 23 Sep 2022 09:15:34 +0200 Subject: [PATCH 13/19] Rename config.cmake.in to nlohmann_jsonConfig.cmake.in --- cmake/json_opts.cmake | 2 +- cmake/{config.cmake.in => nlohmann_jsonConfig.cmake.in} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename cmake/{config.cmake.in => nlohmann_jsonConfig.cmake.in} (100%) diff --git a/cmake/json_opts.cmake b/cmake/json_opts.cmake index b79e1c9f6..617b52b65 100644 --- a/cmake/json_opts.cmake +++ b/cmake/json_opts.cmake @@ -47,7 +47,7 @@ set(NLOHMANN_JSON_TARGET_NAME ${PROJECT_NAME}) set(NLOHMANN_JSON_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}" CACHE INTERNAL "") set(NLOHMANN_JSON_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}") set(NLOHMANN_JSON_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets") -set(NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE "cmake/config.cmake.in") +set(NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE "cmake/nlohmann_jsonConfig.cmake.in") set(NLOHMANN_JSON_CMAKE_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}") set(NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}ConfigVersion.cmake") set(NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Config.cmake") diff --git a/cmake/config.cmake.in b/cmake/nlohmann_jsonConfig.cmake.in similarity index 100% rename from cmake/config.cmake.in rename to cmake/nlohmann_jsonConfig.cmake.in From d14e22c40bddb1f347aa56b1d429882768542dc6 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Fri, 23 Sep 2022 09:23:08 +0200 Subject: [PATCH 14/19] Shorten NLOHMANN_JSON_* variables to JSON_* --- CMakeLists.txt | 48 +++++++++++++++--------------- cmake/json_opts.cmake | 26 ++++++++-------- cmake/json_summary.cmake | 2 +- cmake/nlohmann_jsonConfig.cmake.in | 12 ++++---- tests/CMakeLists.txt | 2 +- tests/abi/CMakeLists.txt | 2 +- 6 files changed, 46 insertions(+), 46 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f77b33870..7707db617 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,16 +32,16 @@ include(json_opts) # add library targets ############################################################################# -add_library(${NLOHMANN_JSON_TARGET_NAME} INTERFACE) -add_library(${PROJECT_NAME}::${NLOHMANN_JSON_TARGET_NAME} ALIAS ${NLOHMANN_JSON_TARGET_NAME}) +add_library(${JSON_TARGET_NAME} INTERFACE) +add_library(${PROJECT_NAME}::${JSON_TARGET_NAME} ALIAS ${JSON_TARGET_NAME}) if (${CMAKE_VERSION} VERSION_LESS "3.8.0") - target_compile_features(${NLOHMANN_JSON_TARGET_NAME} INTERFACE cxx_range_for) + target_compile_features(${JSON_TARGET_NAME} INTERFACE cxx_range_for) else() - target_compile_features(${NLOHMANN_JSON_TARGET_NAME} INTERFACE cxx_std_11) + target_compile_features(${JSON_TARGET_NAME} INTERFACE cxx_std_11) endif() target_compile_definitions( - ${NLOHMANN_JSON_TARGET_NAME} + ${JSON_TARGET_NAME} INTERFACE $<$>:JSON_USE_GLOBAL_UDLS=0> $<$>:JSON_USE_IMPLICIT_CONVERSIONS=0> @@ -51,9 +51,9 @@ target_compile_definitions( ) target_include_directories( - ${NLOHMANN_JSON_TARGET_NAME} - ${NLOHMANN_JSON_SYSTEM_INCLUDE} INTERFACE - $ + ${JSON_TARGET_NAME} + ${JSON_SYSTEM_INCLUDE} INTERFACE + $ $ ) @@ -62,7 +62,7 @@ if (MSVC) set(NLOHMANN_ADD_NATVIS TRUE) set(NLOHMANN_NATVIS_FILE "nlohmann_json.natvis") target_sources( - ${NLOHMANN_JSON_TARGET_NAME} + ${JSON_TARGET_NAME} INTERFACE $ $ @@ -100,23 +100,23 @@ include(CMakePackageConfigHelpers) # https://github.com/nlohmann/json/issues/1697 configure_file( "cmake/nlohmann_jsonConfigVersion.cmake.in" - ${NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE} + ${JSON_CMAKE_VERSION_CONFIG_FILE} @ONLY ) configure_file( - ${NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE} - ${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE} + ${JSON_CMAKE_CONFIG_TEMPLATE} + ${JSON_CMAKE_PROJECT_CONFIG_FILE} @ONLY ) if(JSON_Install) install( - DIRECTORY ${NLOHMANN_JSON_INCLUDE_BUILD_DIR} - DESTINATION ${NLOHMANN_JSON_INCLUDE_INSTALL_DIR} + DIRECTORY ${JSON_INCLUDE_BUILD_DIR} + DESTINATION ${JSON_INCLUDE_INSTALL_DIR} ) install( - FILES ${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE} ${NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE} - DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR} + FILES ${JSON_CMAKE_PROJECT_CONFIG_FILE} ${JSON_CMAKE_VERSION_CONFIG_FILE} + DESTINATION ${JSON_CONFIG_INSTALL_DIR} ) if (NLOHMANN_ADD_NATVIS) install( @@ -125,23 +125,23 @@ if(JSON_Install) ) endif() export( - TARGETS ${NLOHMANN_JSON_TARGET_NAME} + TARGETS ${JSON_TARGET_NAME} NAMESPACE ${PROJECT_NAME}:: - FILE ${NLOHMANN_JSON_CMAKE_PROJECT_TARGETS_FILE} + FILE ${JSON_CMAKE_PROJECT_TARGETS_FILE} ) install( - TARGETS ${NLOHMANN_JSON_TARGET_NAME} - EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME} - INCLUDES DESTINATION ${NLOHMANN_JSON_INCLUDE_INSTALL_DIR} + TARGETS ${JSON_TARGET_NAME} + EXPORT ${JSON_TARGETS_EXPORT_NAME} + INCLUDES DESTINATION ${JSON_INCLUDE_INSTALL_DIR} ) install( - EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME} + EXPORT ${JSON_TARGETS_EXPORT_NAME} NAMESPACE ${PROJECT_NAME}:: - DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR} + DESTINATION ${JSON_CONFIG_INSTALL_DIR} ) install( FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" - DESTINATION ${NLOHMANN_JSON_PKGCONFIG_INSTALL_DIR} + DESTINATION ${JSON_PKGCONFIG_INSTALL_DIR} ) endif() diff --git a/cmake/json_opts.cmake b/cmake/json_opts.cmake index 617b52b65..1e6f55445 100644 --- a/cmake/json_opts.cmake +++ b/cmake/json_opts.cmake @@ -43,25 +43,25 @@ option(JSON_SystemInclude "Include as system headers (skip for clang-tidy)." OFF # configuration ############################################################################# -set(NLOHMANN_JSON_TARGET_NAME ${PROJECT_NAME}) -set(NLOHMANN_JSON_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}" CACHE INTERNAL "") -set(NLOHMANN_JSON_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}") -set(NLOHMANN_JSON_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets") -set(NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE "cmake/nlohmann_jsonConfig.cmake.in") -set(NLOHMANN_JSON_CMAKE_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}") -set(NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}ConfigVersion.cmake") -set(NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Config.cmake") -set(NLOHMANN_JSON_CMAKE_PROJECT_TARGETS_FILE "${NLOHMANN_JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Targets.cmake") -set(NLOHMANN_JSON_PKGCONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/pkgconfig") +set(JSON_TARGET_NAME ${PROJECT_NAME}) +set(JSON_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}" CACHE INTERNAL "") +set(JSON_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}") +set(JSON_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets") +set(JSON_CMAKE_CONFIG_TEMPLATE "cmake/nlohmann_jsonConfig.cmake.in") +set(JSON_CMAKE_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}") +set(JSON_CMAKE_VERSION_CONFIG_FILE "${JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}ConfigVersion.cmake") +set(JSON_CMAKE_PROJECT_CONFIG_FILE "${JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Config.cmake") +set(JSON_CMAKE_PROJECT_TARGETS_FILE "${JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Targets.cmake") +set(JSON_PKGCONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/pkgconfig") if (JSON_MultipleHeaders) - set(NLOHMANN_JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/include/") + set(JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/include/") else() - set(NLOHMANN_JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/single_include/") + set(JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/single_include/") endif() if (JSON_SystemInclude) - set(NLOHMANN_JSON_SYSTEM_INCLUDE "SYSTEM") + set(JSON_SYSTEM_INCLUDE "SYSTEM") endif() set(JSON_TEST_DATA_URL https://github.com/nlohmann/json_test_data.git) diff --git a/cmake/json_summary.cmake b/cmake/json_summary.cmake index 58075f97e..4cdaed8e4 100644 --- a/cmake/json_summary.cmake +++ b/cmake/json_summary.cmake @@ -88,5 +88,5 @@ json_feature(JSON_LegacyDiscardedValueComparison "Legacy discarded value compari message("") json_feature(JSON_MultipleHeaders "Use the multi-header code?") -message(" Include directory: ${NLOHMANN_JSON_INCLUDE_BUILD_DIR}") +message(" Include directory: ${JSON_INCLUDE_BUILD_DIR}") json_feature(JSON_SystemInclude "Include as system headers?") diff --git a/cmake/nlohmann_jsonConfig.cmake.in b/cmake/nlohmann_jsonConfig.cmake.in index 9a17a7d7b..7aa4a2e6f 100644 --- a/cmake/nlohmann_jsonConfig.cmake.in +++ b/cmake/nlohmann_jsonConfig.cmake.in @@ -2,14 +2,14 @@ include(FindPackageHandleStandardArgs) set(${CMAKE_FIND_PACKAGE_NAME}_CONFIG ${CMAKE_CURRENT_LIST_FILE}) find_package_handle_standard_args(@PROJECT_NAME@ CONFIG_MODE) -if(NOT TARGET @PROJECT_NAME@::@NLOHMANN_JSON_TARGET_NAME@) - include("${CMAKE_CURRENT_LIST_DIR}/@NLOHMANN_JSON_TARGETS_EXPORT_NAME@.cmake") - if((NOT TARGET @NLOHMANN_JSON_TARGET_NAME@) AND +if(NOT TARGET @PROJECT_NAME@::@JSON_TARGET_NAME@) + include("${CMAKE_CURRENT_LIST_DIR}/@JSON_TARGETS_EXPORT_NAME@.cmake") + if((NOT TARGET @JSON_TARGET_NAME@) AND (NOT @PROJECT_NAME@_FIND_VERSION OR @PROJECT_NAME@_FIND_VERSION VERSION_LESS 3.2.0)) - add_library(@NLOHMANN_JSON_TARGET_NAME@ INTERFACE IMPORTED) - set_target_properties(@NLOHMANN_JSON_TARGET_NAME@ PROPERTIES - INTERFACE_LINK_LIBRARIES @PROJECT_NAME@::@NLOHMANN_JSON_TARGET_NAME@ + add_library(@JSON_TARGET_NAME@ INTERFACE IMPORTED) + set_target_properties(@JSON_TARGET_NAME@ PROPERTIES + INTERFACE_LINK_LIBRARIES @PROJECT_NAME@::@JSON_TARGET_NAME@ ) endif() endif() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ab476fc80..58f840e62 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -54,7 +54,7 @@ target_include_directories(test_main PUBLIC thirdparty/doctest thirdparty/fifo_map ${PROJECT_BINARY_DIR}/include) -target_link_libraries(test_main PUBLIC ${NLOHMANN_JSON_TARGET_NAME}) +target_link_libraries(test_main PUBLIC ${JSON_TARGET_NAME}) ############################################################################# # define test- and standard-specific build settings diff --git a/tests/abi/CMakeLists.txt b/tests/abi/CMakeLists.txt index ba90837cb..964b19776 100644 --- a/tests/abi/CMakeLists.txt +++ b/tests/abi/CMakeLists.txt @@ -18,7 +18,7 @@ target_compile_options(abi_compat_common INTERFACE target_include_directories(abi_compat_common SYSTEM INTERFACE ../thirdparty/doctest include) -target_link_libraries(abi_compat_common INTERFACE ${NLOHMANN_JSON_TARGET_NAME}) +target_link_libraries(abi_compat_common INTERFACE ${JSON_TARGET_NAME}) # shared main() add_library(abi_compat_main STATIC main.cpp) From 37453d0903554a84fd7e8f7f64d171fbb5c12219 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Fri, 23 Sep 2022 10:12:42 +0200 Subject: [PATCH 15/19] Move generating of package configuration into separate section --- CMakeLists.txt | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7707db617..ef0d418bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,12 +69,6 @@ if (MSVC) ) endif() -# Install a pkg-config file, so other tools can find this. -CONFIGURE_FILE( - "${CMAKE_CURRENT_SOURCE_DIR}/cmake/pkg-config.pc.in" - "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" -) - ############################################################################# # add tests ############################################################################# @@ -91,10 +85,22 @@ if(JSON_CI) endif() ############################################################################# -# install files and targets +# generate package configuration files ############################################################################# -include(CMakePackageConfigHelpers) +# generate pkg-config file +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/pkg-config.pc.in" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" +) + +# generate CMake module configuration and version files +configure_file( + ${JSON_CMAKE_CONFIG_TEMPLATE} + ${JSON_CMAKE_PROJECT_CONFIG_FILE} + @ONLY +) + # use a custom package version config file instead of # write_basic_package_version_file to ensure that it's architecture-independent # https://github.com/nlohmann/json/issues/1697 @@ -103,11 +109,10 @@ configure_file( ${JSON_CMAKE_VERSION_CONFIG_FILE} @ONLY ) -configure_file( - ${JSON_CMAKE_CONFIG_TEMPLATE} - ${JSON_CMAKE_PROJECT_CONFIG_FILE} - @ONLY -) + +############################################################################# +# install files and targets +############################################################################# if(JSON_Install) install( From 3efc881192c1bc7e74586e9c4dba767b5dbf495f Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Fri, 23 Sep 2022 12:11:50 +0200 Subject: [PATCH 16/19] Use CMakePackageConfigHelpers to generate module configuration --- CMakeLists.txt | 34 ++++++++++++++++++++---------- cmake/nlohmann_jsonConfig.cmake.in | 25 +++++++++++----------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ef0d418bb..3ba817ab6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,21 +94,33 @@ configure_file( "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" ) -# generate CMake module configuration and version files -configure_file( +include(CMakePackageConfigHelpers) + +# generate CMake module configuration file +configure_package_config_file( ${JSON_CMAKE_CONFIG_TEMPLATE} ${JSON_CMAKE_PROJECT_CONFIG_FILE} - @ONLY + INSTALL_DESTINATION ${JSON_CONFIG_INSTALL_DIR} + NO_SET_AND_CHECK_MACRO ) -# use a custom package version config file instead of -# write_basic_package_version_file to ensure that it's architecture-independent -# https://github.com/nlohmann/json/issues/1697 -configure_file( - "cmake/nlohmann_jsonConfigVersion.cmake.in" - ${JSON_CMAKE_VERSION_CONFIG_FILE} - @ONLY -) +# generate CMake module version file +if(CMAKE_VERSION VERSION_EQUAL "3.14" OR CMAKE_VERSION VERSION_GREATER "3.14") + write_basic_package_version_file( + ${JSON_CMAKE_VERSION_CONFIG_FILE} + COMPATIBILITY SameMajorVersion + ARCH_INDEPENDENT + ) +else() + # use a custom package version config file instead of + # write_basic_package_version_file to ensure that it's architecture-independent + # https://github.com/nlohmann/json/issues/1697 + configure_file( + "cmake/nlohmann_jsonConfigVersion.cmake.in" + ${JSON_CMAKE_VERSION_CONFIG_FILE} + @ONLY + ) +endif() ############################################################################# # install files and targets diff --git a/cmake/nlohmann_jsonConfig.cmake.in b/cmake/nlohmann_jsonConfig.cmake.in index 7aa4a2e6f..93a778ca1 100644 --- a/cmake/nlohmann_jsonConfig.cmake.in +++ b/cmake/nlohmann_jsonConfig.cmake.in @@ -1,15 +1,16 @@ -include(FindPackageHandleStandardArgs) -set(${CMAKE_FIND_PACKAGE_NAME}_CONFIG ${CMAKE_CURRENT_LIST_FILE}) -find_package_handle_standard_args(@PROJECT_NAME@ CONFIG_MODE) +@PACKAGE_INIT@ -if(NOT TARGET @PROJECT_NAME@::@JSON_TARGET_NAME@) - include("${CMAKE_CURRENT_LIST_DIR}/@JSON_TARGETS_EXPORT_NAME@.cmake") - if((NOT TARGET @JSON_TARGET_NAME@) AND - (NOT @PROJECT_NAME@_FIND_VERSION OR - @PROJECT_NAME@_FIND_VERSION VERSION_LESS 3.2.0)) - add_library(@JSON_TARGET_NAME@ INTERFACE IMPORTED) - set_target_properties(@JSON_TARGET_NAME@ PROPERTIES - INTERFACE_LINK_LIBRARIES @PROJECT_NAME@::@JSON_TARGET_NAME@ - ) +include(FindPackageHandleStandardArgs) +set(nlohmann_json_CONFIG ${CMAKE_CURRENT_LIST_FILE}) +find_package_handle_standard_args(nlohmann_json CONFIG_MODE) + +if(NOT TARGET nlohmann_json::nlohmann_json) + include("${CMAKE_CURRENT_LIST_DIR}/nlohmann_jsonTargets.cmake") + if((NOT TARGET nlohmann_json) + AND (NOT nlohmann_json_FIND_VERSION OR nlohmann_json_FIND_VERSION VERSION_LESS 3.2.0)) + add_library(nlohmann_json INTERFACE IMPORTED) + set_target_properties(nlohmann_json PROPERTIES INTERFACE_LINK_LIBRARIES nlohmann_json::nlohmann_json) endif() endif() + +check_required_components(nlohmann_json) From 4ece73cd76016c86cde9071dded432f65cce8978 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Fri, 23 Sep 2022 13:57:44 +0200 Subject: [PATCH 17/19] Clean up installation and inline variables --- CMakeLists.txt | 106 ++++++++++++++++++++++----------------- cmake/json_opts.cmake | 18 +++---- cmake/json_summary.cmake | 2 +- tests/CMakeLists.txt | 2 +- tests/abi/CMakeLists.txt | 2 +- 5 files changed, 68 insertions(+), 62 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ba817ab6..a83bca5b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,16 +32,16 @@ include(json_opts) # add library targets ############################################################################# -add_library(${JSON_TARGET_NAME} INTERFACE) -add_library(${PROJECT_NAME}::${JSON_TARGET_NAME} ALIAS ${JSON_TARGET_NAME}) +add_library(nlohmann_json INTERFACE) +add_library(nlohmann_json::nlohmann_json ALIAS nlohmann_json) if (${CMAKE_VERSION} VERSION_LESS "3.8.0") - target_compile_features(${JSON_TARGET_NAME} INTERFACE cxx_range_for) + target_compile_features(nlohmann_json INTERFACE cxx_range_for) else() - target_compile_features(${JSON_TARGET_NAME} INTERFACE cxx_std_11) + target_compile_features(nlohmann_json INTERFACE cxx_std_11) endif() target_compile_definitions( - ${JSON_TARGET_NAME} + nlohmann_json INTERFACE $<$>:JSON_USE_GLOBAL_UDLS=0> $<$>:JSON_USE_IMPLICIT_CONVERSIONS=0> @@ -51,21 +51,19 @@ target_compile_definitions( ) target_include_directories( - ${JSON_TARGET_NAME} + nlohmann_json ${JSON_SYSTEM_INCLUDE} INTERFACE - $ - $ + $ + $ ) -## add debug view definition file for msvc (natvis) +# add Natvis debug view definition file for MSVC if (MSVC) - set(NLOHMANN_ADD_NATVIS TRUE) - set(NLOHMANN_NATVIS_FILE "nlohmann_json.natvis") target_sources( - ${JSON_TARGET_NAME} + nlohmann_json INTERFACE - $ - $ + $ + $ ) endif() @@ -91,23 +89,23 @@ endif() # generate pkg-config file configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/pkg-config.pc.in" - "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" + "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_json.pc" ) include(CMakePackageConfigHelpers) # generate CMake module configuration file configure_package_config_file( - ${JSON_CMAKE_CONFIG_TEMPLATE} - ${JSON_CMAKE_PROJECT_CONFIG_FILE} - INSTALL_DESTINATION ${JSON_CONFIG_INSTALL_DIR} + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/nlohmann_jsonConfig.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_jsonConfig.cmake" + INSTALL_DESTINATION "${JSON_INSTALL_CONFIG_DIR}/cmake/nlohmann_json" NO_SET_AND_CHECK_MACRO ) # generate CMake module version file if(CMAKE_VERSION VERSION_EQUAL "3.14" OR CMAKE_VERSION VERSION_GREATER "3.14") write_basic_package_version_file( - ${JSON_CMAKE_VERSION_CONFIG_FILE} + "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_jsonConfigVersion.cmake" COMPATIBILITY SameMajorVersion ARCH_INDEPENDENT ) @@ -116,50 +114,64 @@ else() # write_basic_package_version_file to ensure that it's architecture-independent # https://github.com/nlohmann/json/issues/1697 configure_file( - "cmake/nlohmann_jsonConfigVersion.cmake.in" - ${JSON_CMAKE_VERSION_CONFIG_FILE} + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/nlohmann_jsonConfigVersion.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_jsonConfigVersion.cmake" @ONLY ) endif() +# generate CMake module targets file for use without installation +export( + TARGETS nlohmann_json + NAMESPACE nlohmann_json:: + FILE "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_jsonTargets.cmake" +) + ############################################################################# # install files and targets ############################################################################# if(JSON_Install) + # install pkg-config file install( - DIRECTORY ${JSON_INCLUDE_BUILD_DIR} - DESTINATION ${JSON_INCLUDE_INSTALL_DIR} + FILES "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_json.pc" + DESTINATION "${JSON_INSTALL_CONFIG_DIR}/pkgconfig" ) + + # install CMake module configuration and version files install( - FILES ${JSON_CMAKE_PROJECT_CONFIG_FILE} ${JSON_CMAKE_VERSION_CONFIG_FILE} - DESTINATION ${JSON_CONFIG_INSTALL_DIR} + FILES "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_jsonConfig.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_jsonConfigVersion.cmake" + DESTINATION "${JSON_INSTALL_CONFIG_DIR}/cmake/nlohmann_json" ) - if (NLOHMANN_ADD_NATVIS) + + # install targets + install( + TARGETS nlohmann_json + EXPORT nlohmann_jsonTargets + INCLUDES DESTINATION "${JSON_INSTALL_INCLUDE_DIR}" + ) + + # generate and install CMake module targets file(s) + install( + EXPORT nlohmann_jsonTargets + NAMESPACE nlohmann_json:: + DESTINATION "${JSON_INSTALL_CONFIG_DIR}/cmake/nlohmann_json" + ) + + # install header files + install( + DIRECTORY "${JSON_BUILD_INCLUDE_DIR}" + DESTINATION "${JSON_INSTALL_INCLUDE_DIR}" + ) + + # install Natvis debug view definition file for MSVC + if (MSVC) install( - FILES ${NLOHMANN_NATVIS_FILE} + FILES "nlohmann_json.natvis" DESTINATION . - ) + ) endif() - export( - TARGETS ${JSON_TARGET_NAME} - NAMESPACE ${PROJECT_NAME}:: - FILE ${JSON_CMAKE_PROJECT_TARGETS_FILE} - ) - install( - TARGETS ${JSON_TARGET_NAME} - EXPORT ${JSON_TARGETS_EXPORT_NAME} - INCLUDES DESTINATION ${JSON_INCLUDE_INSTALL_DIR} - ) - install( - EXPORT ${JSON_TARGETS_EXPORT_NAME} - NAMESPACE ${PROJECT_NAME}:: - DESTINATION ${JSON_CONFIG_INSTALL_DIR} - ) - install( - FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" - DESTINATION ${JSON_PKGCONFIG_INSTALL_DIR} - ) endif() ############################################################################# diff --git a/cmake/json_opts.cmake b/cmake/json_opts.cmake index 1e6f55445..ac0b2043b 100644 --- a/cmake/json_opts.cmake +++ b/cmake/json_opts.cmake @@ -43,21 +43,15 @@ option(JSON_SystemInclude "Include as system headers (skip for clang-tidy)." OFF # configuration ############################################################################# -set(JSON_TARGET_NAME ${PROJECT_NAME}) -set(JSON_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}" CACHE INTERNAL "") -set(JSON_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}") -set(JSON_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets") -set(JSON_CMAKE_CONFIG_TEMPLATE "cmake/nlohmann_jsonConfig.cmake.in") -set(JSON_CMAKE_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}") -set(JSON_CMAKE_VERSION_CONFIG_FILE "${JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}ConfigVersion.cmake") -set(JSON_CMAKE_PROJECT_CONFIG_FILE "${JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Config.cmake") -set(JSON_CMAKE_PROJECT_TARGETS_FILE "${JSON_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Targets.cmake") -set(JSON_PKGCONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/pkgconfig") +# package configuration install base directory +set(JSON_INSTALL_CONFIG_DIR "${CMAKE_INSTALL_DATADIR}") +# include directories +set(JSON_INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}") if (JSON_MultipleHeaders) - set(JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/include/") + set(JSON_BUILD_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include/") else() - set(JSON_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/single_include/") + set(JSON_BUILD_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/single_include/") endif() if (JSON_SystemInclude) diff --git a/cmake/json_summary.cmake b/cmake/json_summary.cmake index 4cdaed8e4..758dda535 100644 --- a/cmake/json_summary.cmake +++ b/cmake/json_summary.cmake @@ -88,5 +88,5 @@ json_feature(JSON_LegacyDiscardedValueComparison "Legacy discarded value compari message("") json_feature(JSON_MultipleHeaders "Use the multi-header code?") -message(" Include directory: ${JSON_INCLUDE_BUILD_DIR}") +message(" Include directory: ${JSON_BUILD_INCLUDE_DIR}") json_feature(JSON_SystemInclude "Include as system headers?") diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 58f840e62..c6ee45c69 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -54,7 +54,7 @@ target_include_directories(test_main PUBLIC thirdparty/doctest thirdparty/fifo_map ${PROJECT_BINARY_DIR}/include) -target_link_libraries(test_main PUBLIC ${JSON_TARGET_NAME}) +target_link_libraries(test_main PUBLIC nlohmann_json) ############################################################################# # define test- and standard-specific build settings diff --git a/tests/abi/CMakeLists.txt b/tests/abi/CMakeLists.txt index 964b19776..26bfd6e7d 100644 --- a/tests/abi/CMakeLists.txt +++ b/tests/abi/CMakeLists.txt @@ -18,7 +18,7 @@ target_compile_options(abi_compat_common INTERFACE target_include_directories(abi_compat_common SYSTEM INTERFACE ../thirdparty/doctest include) -target_link_libraries(abi_compat_common INTERFACE ${JSON_TARGET_NAME}) +target_link_libraries(abi_compat_common INTERFACE nlohmann_json) # shared main() add_library(abi_compat_main STATIC main.cpp) From 3c77b378e6bff4a760124743939d157f0b32bb44 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sat, 24 Sep 2022 13:17:31 +0200 Subject: [PATCH 18/19] Print valgrind command with options in feature summary --- cmake/json_opts.cmake | 1 + cmake/json_summary.cmake | 3 ++- cmake/json_test.cmake | 14 +++++++------- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/cmake/json_opts.cmake b/cmake/json_opts.cmake index ac0b2043b..f5769d4cb 100644 --- a/cmake/json_opts.cmake +++ b/cmake/json_opts.cmake @@ -16,6 +16,7 @@ set(JSON_32bitTest AUTO CACHE STRING "Enable the 32bit unit test (ON/OFF/AUTO/ON cmake_dependent_option(JSON_FastTests "Skip expensive/slow tests." OFF "JSON_BuildTests" OFF) set(JSON_TestDataDirectory "$ENV{JSON_TEST_DATA_DIRECTORY}" CACHE FILEPATH "Test data directory for the unit tests (will be downloaded if not specified).") cmake_dependent_option(JSON_Valgrind "Execute test suite with Valgrind." OFF "JSON_BuildTests" OFF) +set(JSON_MEMORYCHECK_COMMAND_OPTIONS "--error-exitcode=1;--leak-check=full" CACHE STRING "Options passed to the memory check command (valgrind).") set(JSON_TestStandards "" CACHE STRING "The list of standards to test explicitly.") diff --git a/cmake/json_summary.cmake b/cmake/json_summary.cmake index 758dda535..cef785b8b 100644 --- a/cmake/json_summary.cmake +++ b/cmake/json_summary.cmake @@ -60,7 +60,8 @@ if(JSON_BuildTests) json_feature(JSON_Valgrind "Execute test suite with Valgrind?") if(JSON_Valgrind) - message(" Valgrind command: ${CMAKE_MEMORYCHECK_COMMAND}" ${CMAKE_MEMORYCHECK_COMMAND_OPTIONS}) + string (REPLACE ";" " " memcheck_command "${JSON_MEMORYCHECK_COMMAND};${JSON_MEMORYCHECK_COMMAND_OPTIONS}") + message(" Valgrind command: ${memcheck_command}") endif() set(test_cxx_standards "") diff --git a/cmake/json_test.cmake b/cmake/json_test.cmake index 6ece47d59..d64affdca 100644 --- a/cmake/json_test.cmake +++ b/cmake/json_test.cmake @@ -26,12 +26,11 @@ add_test(NAME "download_test_data" COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINA ) set_tests_properties(download_test_data PROPERTIES FIXTURES_SETUP TEST_DATA) -if(JSON_Valgrind) - find_program(CMAKE_MEMORYCHECK_COMMAND valgrind) - message(STATUS "Executing test suite with Valgrind (${CMAKE_MEMORYCHECK_COMMAND})") - set(memcheck_command "${CMAKE_MEMORYCHECK_COMMAND} ${CMAKE_MEMORYCHECK_COMMAND_OPTIONS} --error-exitcode=1 --leak-check=full") - separate_arguments(memcheck_command) -endif() +############################################################################# +# locate memory check command +############################################################################# + +find_program(JSON_MEMORYCHECK_COMMAND valgrind) ############################################################################# # detect standard support @@ -189,7 +188,8 @@ function(_json_test_add_test test_name file main cxx_standard) if(JSON_Valgrind) add_test(NAME ${test_target}_valgrind - COMMAND ${memcheck_command} $ ${DOCTEST_TEST_FILTER} + COMMAND ${JSON_MEMORYCHECK_COMMAND} ${JSON_MEMORYCHECK_COMMAND_OPTIONS} + -- $ ${DOCTEST_TEST_FILTER} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} ) set_tests_properties(${test_target}_valgrind PROPERTIES From 875cc362e0e0b2a33873efa3d9b20f2c3ad572c3 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sat, 24 Sep 2022 14:35:07 +0200 Subject: [PATCH 19/19] Move test data download into CMake script Avoid using shell commands to check if the test directory exists by using a CMake script. --- cmake/json_test.cmake | 6 +++++- cmake/scripts/clone_test_data.cmake | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 cmake/scripts/clone_test_data.cmake diff --git a/cmake/json_test.cmake b/cmake/json_test.cmake index d64affdca..9c35a3d14 100644 --- a/cmake/json_test.cmake +++ b/cmake/json_test.cmake @@ -12,7 +12,11 @@ else() find_package(Git REQUIRED) # target to download test data add_custom_target(download_test_data - COMMAND test -d json_test_data || ${GIT_EXECUTABLE} clone -c advice.detachedHead=false --branch v${JSON_TEST_DATA_VERSION} ${JSON_TEST_DATA_URL} --quiet --depth 1 + COMMAND ${CMAKE_COMMAND} + -DGIT_EXECUTABLE=${GIT_EXECUTABLE} + -DJSON_TEST_DATA_VERSION=${JSON_TEST_DATA_VERSION} + -DJSON_TEST_DATA_URL=${JSON_TEST_DATA_URL} + -P ${PROJECT_SOURCE_DIR}/cmake/scripts/clone_test_data.cmake COMMENT "Downloading test data from ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})" WORKING_DIRECTORY ${CMAKE_BINARY_DIR} ) diff --git a/cmake/scripts/clone_test_data.cmake b/cmake/scripts/clone_test_data.cmake new file mode 100644 index 000000000..c6cc90eb9 --- /dev/null +++ b/cmake/scripts/clone_test_data.cmake @@ -0,0 +1,16 @@ +# clone test data + +get_filename_component(test_data_dir json_test_data ABSOLUTE) + +if(NOT EXISTS ${test_data_dir}) + execute_process(COMMAND ${GIT_EXECUTABLE} clone + -q -c advice.detachedHead=false -b v${JSON_TEST_DATA_VERSION} --depth 1 + -- ${JSON_TEST_DATA_URL} ${test_data_dir} + RESULT_VARIABLE git_result + OUTPUT_VARIABLE git_output + ERROR_VARIABLE git_output) + + if(NOT git_result EQUAL 0) + message(FATAL_ERROR "git failed:\n${git_output}") + endif() +endif()