1cmake_minimum_required(VERSION 3.13...3.14) 2 3option(JSON_Valgrind "Execute test suite with Valgrind." OFF) 4option(JSON_FastTests "Skip expensive/slow tests." OFF) 5 6set(JSON_32bitTest AUTO CACHE STRING "Enable the 32bit unit test (ON/OFF/AUTO/ONLY).") 7set(JSON_TestStandards "" CACHE STRING "The list of standards to test explicitly.") 8 9# using an env var, since this will also affect targets executing cmake (such as "ci_test_compiler_default") 10set(JSON_FORCED_GLOBAL_COMPILE_OPTIONS $ENV{JSON_FORCED_GLOBAL_COMPILE_OPTIONS}) 11if (NOT "" STREQUAL "$ENV{JSON_FORCED_GLOBAL_COMPILE_OPTIONS}") 12 add_compile_options($ENV{JSON_FORCED_GLOBAL_COMPILE_OPTIONS}) 13endif() 14if (NOT "" STREQUAL "$ENV{JSON_FORCED_GLOBAL_LINK_OPTIONS}") 15 add_link_options($ENV{JSON_FORCED_GLOBAL_LINK_OPTIONS}) 16endif() 17 18include(test) 19 20############################################################################# 21# override standard support 22############################################################################# 23 24# Clang only supports C++17 starting from Clang 5.0 25if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0) 26 unset(compiler_supports_cpp_17) 27endif() 28# MSVC 2015 (14.0) does not support C++17 29if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.1) 30 unset(compiler_supports_cpp_17) 31endif() 32 33# Clang C++20 support appears insufficient prior to Clang 9.0 (based on CI build failure) 34if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0) 35 unset(compiler_supports_cpp_20) 36endif() 37# GCC started supporting C++20 features in 8.0 but a test for #3070 segfaults prior to 9.0 38if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0) 39 unset(compiler_supports_cpp_20) 40endif() 41 42############################################################################# 43# test_main library with shared code to speed up build and common settings 44############################################################################# 45 46add_library(test_main OBJECT src/unit.cpp) 47target_compile_definitions(test_main PUBLIC 48 DOCTEST_CONFIG_SUPER_FAST_ASSERTS 49 JSON_TEST_KEEP_MACROS 50 JSON_TEST_USING_MULTIPLE_HEADERS=$<BOOL:${JSON_MultipleHeaders}>) 51target_compile_features(test_main PRIVATE cxx_std_11) 52target_compile_options(test_main PUBLIC 53 $<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>> 54 # MSVC: Force to always compile with W4 55 # Disable warning C4566: character represented by universal-character-name '\uFF01' 56 # cannot be represented in the current code page (1252) 57 # Disable warning C4996: 'nlohmann::basic_json<...>::operator <<': was declared deprecated 58 $<$<CXX_COMPILER_ID:MSVC>:/W4 /wd4566 /wd4996> 59 # https://github.com/nlohmann/json/issues/1114 60 $<$<CXX_COMPILER_ID:MSVC>:/bigobj> $<$<BOOL:${MINGW}>:-Wa,-mbig-obj> 61 62 # https://github.com/nlohmann/json/pull/3229 63 $<$<CXX_COMPILER_ID:Intel>:-diag-disable=2196> 64 65 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated;-Wno-float-equal> 66 $<$<CXX_COMPILER_ID:GNU>:-Wno-deprecated-declarations> 67 $<$<CXX_COMPILER_ID:Intel>:-diag-disable=1786>) 68target_include_directories(test_main PUBLIC 69 thirdparty/doctest 70 thirdparty/fifo_map 71 ${PROJECT_BINARY_DIR}/include) 72target_link_libraries(test_main PUBLIC ${NLOHMANN_JSON_TARGET_NAME}) 73 74############################################################################# 75# define test- and standard-specific build settings 76############################################################################# 77 78if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" 79 AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 8.0 80 AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0 AND NOT MINGW) 81 # fix for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90050 82 json_test_set_test_options(all CXX_STANDARDS 17 LINK_LIBRARIES stdc++fs) 83endif() 84 85if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") 86 # avoid stack overflow, see https://github.com/nlohmann/json/issues/2955 87 json_test_set_test_options("test-cbor;test-msgpack;test-ubjson;test-bjdata;test-binary_formats" LINK_OPTIONS /STACK:4000000) 88endif() 89 90# disable exceptions for test-disabled_exceptions 91json_test_set_test_options(test-disabled_exceptions 92 COMPILE_DEFINITIONS 93 JSON_NOEXCEPTION 94 # disabled due to https://github.com/nlohmann/json/discussions/2824 95 #$<$<CXX_COMPILER_ID:MSVC>:_HAS_EXCEPTIONS=0> 96 COMPILE_OPTIONS 97 $<$<CXX_COMPILER_ID:AppleClang>:-fno-exceptions> $<$<CXX_COMPILER_ID:Clang>:-fno-exceptions> 98 $<$<CXX_COMPILER_ID:GNU>:-fno-exceptions> 99 $<$<CXX_COMPILER_ID:Intel>:-fno-exceptions> $<$<CXX_COMPILER_ID:IntelLLVM>:-fno-exceptions> 100 # disabled due to https://github.com/nlohmann/json/discussions/2824 101 #$<$<CXX_COMPILER_ID:MSVC>:/EH> 102) 103 104# raise timeout of expensive Unicode test 105json_test_set_test_options(test-unicode4 TEST_PROPERTIES TIMEOUT 3000) 106 107############################################################################# 108# add unit tests 109############################################################################# 110 111if("${JSON_TestStandards}" STREQUAL "") 112 set(test_cxx_standards 11 14 17 20 23) 113 unset(test_force) 114else() 115 set(test_cxx_standards ${JSON_TestStandards}) 116 set(test_force FORCE) 117endif() 118 119# Print selected standards marking unavailable ones with brackets 120set(msg_standards "") 121foreach(cxx_standard ${test_cxx_standards}) 122 if(compiler_supports_cpp_${cxx_standard}) 123 list(APPEND msg_standards ${cxx_standard}) 124 else() 125 list(APPEND msg_standards [${cxx_standard}]) 126 endif() 127endforeach() 128string(JOIN " " msg_standards ${msg_standards}) 129set(msg "Testing standards: ${msg_standards}") 130if(test_force) 131 string(APPEND msg " (forced)") 132endif() 133message(STATUS "${msg}") 134 135# *DO* use json_test_set_test_options() above this line 136 137json_test_should_build_32bit_test(json_32bit_test json_32bit_test_only "${JSON_32bitTest}") 138file(GLOB files src/unit-*.cpp) 139if(json_32bit_test_only) 140 set(files src/unit-32bit.cpp) 141elseif(NOT json_32bit_test) 142 list(FILTER files EXCLUDE REGEX src/unit-32bit.cpp) 143endif() 144 145foreach(file ${files}) 146 json_test_add_test_for(${file} MAIN test_main CXX_STANDARDS ${test_cxx_standards} ${test_force}) 147endforeach() 148 149if(json_32bit_test_only) 150 # Skip all other tests in this file 151 return() 152endif() 153 154# test legacy comparison of discarded values 155json_test_set_test_options(test-comparison_legacy 156 COMPILE_DEFINITIONS JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON=1 157) 158json_test_add_test_for(src/unit-comparison.cpp 159 NAME test-comparison_legacy 160 MAIN test_main CXX_STANDARDS ${test_cxx_standards} ${test_force} 161) 162 163# *DO NOT* use json_test_set_test_options() below this line 164 165############################################################################# 166# test ABI compatibility 167############################################################################# 168 169add_subdirectory(abi) 170 171############################################################################# 172# Test the generated build configs 173############################################################################# 174 175# these tests depend on the generated file nlohmann_jsonConfig.cmake 176if (JSON_Install) 177 add_subdirectory(cmake_import) 178 add_subdirectory(cmake_import_minver) 179endif() 180 181add_subdirectory(cmake_add_subdirectory) 182add_subdirectory(cmake_fetch_content) 183add_subdirectory(cmake_fetch_content2) 184add_subdirectory(cmake_target_include_directories) 185