diff --git a/CMakeLists.txt b/CMakeLists.txt index f942e04ab..a83bca5b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,115 +1,47 @@ cmake_minimum_required(VERSION 3.1) -## -## PROJECT -## name and version -## -project(nlohmann_json VERSION 3.11.2 LANGUAGES CXX) - -## -## 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) -if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) - set(MAIN_PROJECT ON) -endif() - -## -## INCLUDE -## -## -set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) -include(ExternalProject) - -## -## OPTIONS -## +############################################################################# +# 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 () -# 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)) - 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." ${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) +# set the CMake module path +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) -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/") - 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}") +# 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() -if (NOT JSON_ImplicitConversions) - message(STATUS "Implicit conversions are disabled") -endif() +# set project name and version +project(nlohmann_json VERSION 3.11.2 LANGUAGES CXX) -if (JSON_DisableEnumSerialization) - message(STATUS "Enum integer serialization is disabled") -endif() +# print CMake, system, and compiler information +include(json_info) -if (JSON_LegacyDiscardedValueComparison) - message(STATUS "Legacy discarded value comparison enabled") -endif() +# handle options and configuration +include(json_opts) -if (JSON_Diagnostics) - message(STATUS "Diagnostics enabled") -endif() +############################################################################# +# add library targets +############################################################################# -if (JSON_SystemInclude) - set(NLOHMANN_JSON_SYSTEM_INCLUDE "SYSTEM") -endif() - -## -## 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}) +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(${NLOHMANN_JSON_TARGET_NAME} INTERFACE cxx_range_for) + target_compile_features(nlohmann_json INTERFACE cxx_range_for) else() - target_compile_features(${NLOHMANN_JSON_TARGET_NAME} INTERFACE cxx_std_11) + target_compile_features(nlohmann_json INTERFACE cxx_std_11) endif() target_compile_definitions( - ${NLOHMANN_JSON_TARGET_NAME} + nlohmann_json INTERFACE $<$>:JSON_USE_GLOBAL_UDLS=0> $<$>:JSON_USE_IMPLICIT_CONVERSIONS=0> @@ -119,91 +51,131 @@ target_compile_definitions( ) target_include_directories( - ${NLOHMANN_JSON_TARGET_NAME} - ${NLOHMANN_JSON_SYSTEM_INCLUDE} INTERFACE - $ - $ + 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( - ${NLOHMANN_JSON_TARGET_NAME} + nlohmann_json INTERFACE - $ - $ + $ + $ ) 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 +############################################################################# -## -## TESTS -## create and configure the unit test target -## if (JSON_BuildTests) include(CTest) enable_testing() add_subdirectory(tests) endif() -## -## INSTALL -## install header files, generate and install cmake config files for find_package() -## -include(CMakePackageConfigHelpers) -# 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 +# add CI targets +if(JSON_CI) + include(json_ci) +endif() + +############################################################################# +# generate package configuration files +############################################################################# + +# generate pkg-config file configure_file( - "cmake/nlohmann_jsonConfigVersion.cmake.in" - ${NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE} - @ONLY -) -configure_file( - ${NLOHMANN_JSON_CMAKE_CONFIG_TEMPLATE} - ${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE} - @ONLY + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/pkg-config.pc.in" + "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_json.pc" ) -if(JSON_Install) - install( - DIRECTORY ${NLOHMANN_JSON_INCLUDE_BUILD_DIR} - DESTINATION ${NLOHMANN_JSON_INCLUDE_INSTALL_DIR} +include(CMakePackageConfigHelpers) + +# generate CMake module configuration file +configure_package_config_file( + "${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( + "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_jsonConfigVersion.cmake" + COMPATIBILITY SameMajorVersion + ARCH_INDEPENDENT ) - install( - FILES ${NLOHMANN_JSON_CMAKE_PROJECT_CONFIG_FILE} ${NLOHMANN_JSON_CMAKE_VERSION_CONFIG_FILE} - DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR} - ) - if (NLOHMANN_ADD_NATVIS) - install( - FILES ${NLOHMANN_NATVIS_FILE} - DESTINATION . - ) - endif() - export( - TARGETS ${NLOHMANN_JSON_TARGET_NAME} - NAMESPACE ${PROJECT_NAME}:: - FILE ${NLOHMANN_JSON_CMAKE_PROJECT_TARGETS_FILE} - ) - install( - TARGETS ${NLOHMANN_JSON_TARGET_NAME} - EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME} - INCLUDES DESTINATION ${NLOHMANN_JSON_INCLUDE_INSTALL_DIR} - ) - install( - EXPORT ${NLOHMANN_JSON_TARGETS_EXPORT_NAME} - NAMESPACE ${PROJECT_NAME}:: - DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR} - ) - install( - FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" - DESTINATION ${NLOHMANN_JSON_PKGCONFIG_INSTALL_DIR} +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_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( + FILES "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_json.pc" + DESTINATION "${JSON_INSTALL_CONFIG_DIR}/pkgconfig" + ) + + # install CMake module configuration and version files + install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_jsonConfig.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/nlohmann_jsonConfigVersion.cmake" + DESTINATION "${JSON_INSTALL_CONFIG_DIR}/cmake/nlohmann_json" + ) + + # 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_json.natvis" + DESTINATION . + ) + endif() +endif() + +############################################################################# +# print feature summary +############################################################################# + +include(json_summary) diff --git a/cmake/config.cmake.in b/cmake/config.cmake.in deleted file mode 100644 index 9a17a7d7b..000000000 --- a/cmake/config.cmake.in +++ /dev/null @@ -1,15 +0,0 @@ -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 - (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@ - ) - endif() -endif() diff --git a/cmake/download_test_data.cmake b/cmake/download_test_data.cmake deleted file mode 100644 index 1bb998dae..000000000 --- a/cmake/download_test_data.cmake +++ /dev/null @@ -1,56 +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() - -# 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/ci.cmake b/cmake/json_ci.cmake similarity index 100% rename from cmake/ci.cmake rename to cmake/json_ci.cmake 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() diff --git a/cmake/json_opts.cmake b/cmake/json_opts.cmake new file mode 100644 index 000000000..f5769d4cb --- /dev/null +++ b/cmake/json_opts.cmake @@ -0,0 +1,63 @@ +include(CMakeDependentOption) +include(GNUInstallDirs) + +############################################################################# +# 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) +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.") + +############################################################################# +# 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 +############################################################################# + +# 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_BUILD_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include/") +else() + set(JSON_BUILD_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/single_include/") +endif() + +if (JSON_SystemInclude) + set(JSON_SYSTEM_INCLUDE "SYSTEM") +endif() + +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 new file mode 100644 index 000000000..cef785b8b --- /dev/null +++ b/cmake/json_summary.cmake @@ -0,0 +1,93 @@ +# 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?") + + if(JSON_TestDataDirectory) + message(" Test data directory: ${JSON_TestDataDirectory}") + else() + message(" Test data source: ${JSON_TEST_DATA_URL} (v${JSON_TEST_DATA_VERSION})") + endif() + + json_feature(JSON_Valgrind "Execute test suite with Valgrind?") + if(JSON_Valgrind) + string (REPLACE ";" " " memcheck_command "${JSON_MEMORYCHECK_COMMAND};${JSON_MEMORYCHECK_COMMAND_OPTIONS}") + message(" Valgrind command: ${memcheck_command}") + endif() + + 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: ${JSON_BUILD_INCLUDE_DIR}") +json_feature(JSON_SystemInclude "Include as system headers?") diff --git a/cmake/test.cmake b/cmake/json_test.cmake similarity index 87% rename from cmake/test.cmake rename to cmake/json_test.cmake index bb840c6c0..9c35a3d14 100644 --- a/cmake/test.cmake +++ b/cmake/json_test.cmake @@ -4,7 +4,25 @@ 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_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 REQUIRED) + # target to download test data + add_custom_target(download_test_data + 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} + ) + # 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} @@ -12,12 +30,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 @@ -175,7 +192,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 diff --git a/cmake/nlohmann_jsonConfig.cmake.in b/cmake/nlohmann_jsonConfig.cmake.in new file mode 100644 index 000000000..93a778ca1 --- /dev/null +++ b/cmake/nlohmann_jsonConfig.cmake.in @@ -0,0 +1,16 @@ +@PACKAGE_INIT@ + +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) 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() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1afb000ae..c6ee45c69 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,12 +1,6 @@ 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) +include(json_test) ############################################################################# # override standard support @@ -60,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 nlohmann_json) ############################################################################# # define test- and standard-specific build settings @@ -101,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 diff --git a/tests/abi/CMakeLists.txt b/tests/abi/CMakeLists.txt index ba90837cb..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 ${NLOHMANN_JSON_TARGET_NAME}) +target_link_libraries(abi_compat_common INTERFACE nlohmann_json) # shared main() add_library(abi_compat_main STATIC main.cpp)