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