1option(JSON_Sanitizer "Build test suite with Clang sanitizer" OFF) 2option(JSON_Valgrind "Execute test suite with Valgrind" OFF) 3option(JSON_NoExceptions "Build test suite without exceptions" OFF) 4option(JSON_Coverage "Build test suite with coverage information" OFF) 5 6# download test data 7include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/download_test_data.cmake) 8 9# test fixture to download test data 10add_test(NAME "download_test_data" COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target download_test_data) 11set_tests_properties(download_test_data PROPERTIES FIXTURES_SETUP TEST_DATA) 12 13if(JSON_Sanitizer) 14 message(STATUS "Building test suite with Clang sanitizer") 15 if(NOT MSVC) 16 set(CMAKE_CXX_FLAGS "-g -O0 -fsanitize=address -fsanitize=undefined -fsanitize=integer -fsanitize=nullability -fno-omit-frame-pointer -fno-sanitize-recover=all -fsanitize-recover=unsigned-integer-overflow") 17 endif() 18endif() 19 20if(JSON_Valgrind) 21 find_program(CMAKE_MEMORYCHECK_COMMAND valgrind) 22 message(STATUS "Executing test suite with Valgrind (${CMAKE_MEMORYCHECK_COMMAND})") 23 set(memcheck_command "${CMAKE_MEMORYCHECK_COMMAND} ${CMAKE_MEMORYCHECK_COMMAND_OPTIONS} --error-exitcode=1 --leak-check=full") 24 separate_arguments(memcheck_command) 25endif() 26 27if(JSON_NoExceptions) 28 message(STATUS "Building test suite without exceptions") 29 if(NOT MSVC) 30 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DJSON_NOEXCEPTION") 31 endif() 32 set(DOCTEST_TEST_FILTER --no-throw) 33endif() 34 35if(JSON_Coverage) 36 message(STATUS "Building test suite with coverage information") 37 38 # from https://github.com/RWTH-HPC/CMake-codecov/blob/master/cmake/FindGcov.cmake 39 get_filename_component(COMPILER_PATH "${CMAKE_CXX_COMPILER}" PATH) 40 string(REGEX MATCH "^[0-9]+" GCC_VERSION "${CMAKE_CXX_COMPILER_VERSION}") 41 find_program(GCOV_BIN NAMES gcov-${GCC_VERSION} gcov HINTS ${COMPILER_PATH}) 42 43 # collect all source files from the chosen include dir 44 file(GLOB_RECURSE SOURCE_FILES ${NLOHMANN_JSON_INCLUDE_BUILD_DIR}*.hpp) 45 46 # add target to collect coverage information and generate HTML file 47 # (filter script from https://stackoverflow.com/a/43726240/266378) 48 add_custom_target(lcov_html 49 COMMAND lcov --directory . --capture --output-file json.info --rc lcov_branch_coverage=1 50 COMMAND lcov -e json.info ${SOURCE_FILES} --output-file json.info.filtered --gcov-tool ${GCOV_BIN} --rc lcov_branch_coverage=1 51 COMMAND ${CMAKE_SOURCE_DIR}/test/thirdparty/imapdl/filterbr.py json.info.filtered > json.info.filtered.noexcept 52 COMMAND genhtml --title "JSON for Modern C++" --legend --demangle-cpp --output-directory html --show-details --branch-coverage json.info.filtered.noexcept 53 COMMENT "Generating HTML report test/html/index.html" 54 ) 55endif() 56 57############################################################################# 58# doctest library with the main function to speed up build 59############################################################################# 60 61add_library(doctest_main OBJECT src/unit.cpp) 62set_target_properties(doctest_main PROPERTIES 63 COMPILE_DEFINITIONS "$<$<CXX_COMPILER_ID:MSVC>:_SCL_SECURE_NO_WARNINGS>" 64 COMPILE_OPTIONS "$<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>>" 65) 66if (${CMAKE_VERSION} VERSION_LESS "3.8.0") 67 target_compile_features(doctest_main PUBLIC cxx_range_for) 68else() 69 target_compile_features(doctest_main PUBLIC cxx_std_11) 70endif() 71target_include_directories(doctest_main PRIVATE "thirdparty/doctest") 72 73# https://stackoverflow.com/questions/2368811/how-to-set-warning-level-in-cmake 74if(MSVC) 75 # Force to always compile with W4 76 if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") 77 string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 78 else() 79 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") 80 endif() 81 82 # Disable warning C4566: character represented by universal-character-name '\uFF01' cannot be represented in the current code page (1252) 83 # Disable warning C4996: 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::operator <<': was declared deprecated 84 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4566 /wd4996") 85 86 # https://github.com/nlohmann/json/issues/1114 87 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /bigobj") 88endif() 89 90############################################################################# 91# one executable for each unit test file 92############################################################################# 93 94set(files 95 src/unit-algorithms.cpp 96 src/unit-allocator.cpp 97 src/unit-alt-string.cpp 98 src/unit-assert_macro.cpp 99 src/unit-bson.cpp 100 src/unit-capacity.cpp 101 src/unit-cbor.cpp 102 src/unit-class_const_iterator.cpp 103 src/unit-class_iterator.cpp 104 src/unit-class_lexer.cpp 105 src/unit-class_parser.cpp 106 src/unit-comparison.cpp 107 src/unit-concepts.cpp 108 src/unit-constructor1.cpp 109 src/unit-constructor2.cpp 110 src/unit-convenience.cpp 111 src/unit-conversions.cpp 112 src/unit-deserialization.cpp 113 src/unit-element_access1.cpp 114 src/unit-element_access2.cpp 115 src/unit-hash.cpp 116 src/unit-inspection.cpp 117 src/unit-items.cpp 118 src/unit-iterators1.cpp 119 src/unit-iterators2.cpp 120 src/unit-json_patch.cpp 121 src/unit-json_pointer.cpp 122 src/unit-large_json.cpp 123 src/unit-merge_patch.cpp 124 src/unit-meta.cpp 125 src/unit-modifiers.cpp 126 src/unit-msgpack.cpp 127 src/unit-noexcept.cpp 128 src/unit-ordered_json.cpp 129 src/unit-ordered_map.cpp 130 src/unit-pointer_access.cpp 131 src/unit-readme.cpp 132 src/unit-reference_access.cpp 133 src/unit-regression1.cpp 134 src/unit-regression2.cpp 135 src/unit-serialization.cpp 136 src/unit-testsuites.cpp 137 src/unit-to_chars.cpp 138 src/unit-ubjson.cpp 139 src/unit-udt.cpp 140 src/unit-udt_macro.cpp 141 src/unit-unicode.cpp 142 src/unit-user_defined_input.cpp 143 src/unit-wstring.cpp) 144 145foreach(file ${files}) 146 get_filename_component(file_basename ${file} NAME_WE) 147 string(REGEX REPLACE "unit-([^$]+)" "test-\\1" testcase ${file_basename}) 148 149 add_executable(${testcase} $<TARGET_OBJECTS:doctest_main> ${file}) 150 target_compile_definitions(${testcase} PRIVATE DOCTEST_CONFIG_SUPER_FAST_ASSERTS) 151 target_compile_options(${testcase} PRIVATE 152 $<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>> 153 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated;-Wno-float-equal> 154 $<$<CXX_COMPILER_ID:GNU>:-Wno-deprecated-declarations> 155 ) 156 target_include_directories(${testcase} PRIVATE ${CMAKE_BINARY_DIR}/include thirdparty/doctest thirdparty/fifo_map) 157 target_link_libraries(${testcase} PRIVATE ${NLOHMANN_JSON_TARGET_NAME}) 158 159 if (JSON_Coverage) 160 target_compile_options(${testcase} PRIVATE --coverage) 161 target_link_libraries(${testcase} PRIVATE --coverage) 162 endif() 163 164 add_test(NAME "${testcase}" 165 COMMAND ${testcase} ${DOCTEST_TEST_FILTER} --no-skip 166 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} 167 ) 168 set_tests_properties("${testcase}" PROPERTIES LABELS "all" FIXTURES_REQUIRED TEST_DATA) 169 170 if(JSON_Valgrind) 171 add_test(NAME "${testcase}_valgrind" 172 COMMAND ${memcheck_command} ${CMAKE_CURRENT_BINARY_DIR}/${testcase} ${DOCTEST_TEST_FILTER} 173 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} 174 ) 175 set_tests_properties("${testcase}_valgrind" PROPERTIES LABELS "valgrind") 176 endif() 177endforeach() 178 179add_executable(json_unit EXCLUDE_FROM_ALL $<TARGET_OBJECTS:doctest_main> ${files}) 180target_compile_definitions(json_unit PRIVATE DOCTEST_CONFIG_SUPER_FAST_ASSERTS) 181target_compile_options(json_unit PRIVATE 182 $<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>> 183 $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated;-Wno-float-equal> 184 $<$<CXX_COMPILER_ID:GNU>:-Wno-deprecated-declarations> 185) 186target_include_directories(json_unit PRIVATE ${CMAKE_BINARY_DIR}/include thirdparty/doctest thirdparty/fifo_map) 187target_link_libraries(json_unit ${NLOHMANN_JSON_TARGET_NAME}) 188add_dependencies(json_unit download_test_data) 189 190############################################################################# 191# Test the generated build configs 192############################################################################# 193 194add_subdirectory(cmake_import) 195add_subdirectory(cmake_import_minver) 196add_subdirectory(cmake_add_subdirectory) 197add_subdirectory(cmake_fetch_content) 198add_subdirectory(cmake_target_include_directories) 199