CMakeLists.txt 8.0 KB

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