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