2017-08-26 02:19:58 +08:00
|
|
|
option(JSON_Sanitizer "Build test suite with Clang sanitizer" OFF)
|
|
|
|
option(JSON_Valgrind "Execute test suite with Valgrind" OFF)
|
2017-08-26 03:29:27 +08:00
|
|
|
option(JSON_NoExceptions "Build test suite without exceptions" OFF)
|
2017-10-05 04:18:21 +08:00
|
|
|
option(JSON_Coverage "Build test suite with coverage information" OFF)
|
2017-08-26 02:12:21 +08:00
|
|
|
|
|
|
|
if(JSON_Sanitizer)
|
2017-08-26 02:19:58 +08:00
|
|
|
message(STATUS "Building test suite with Clang sanitizer")
|
2017-08-26 02:12:21 +08:00
|
|
|
if(NOT MSVC)
|
2019-03-28 21:22:48 +08:00
|
|
|
set(CMAKE_CXX_FLAGS "-g -O2 -fsanitize=address -fsanitize=undefined -fsanitize=implicit-conversion -fsanitize=integer -fsanitize=nullability -fno-omit-frame-pointer")
|
2017-08-26 02:12:21 +08:00
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(JSON_Valgrind)
|
|
|
|
find_program(CMAKE_MEMORYCHECK_COMMAND valgrind)
|
2017-08-26 02:19:58 +08:00
|
|
|
message(STATUS "Executing test suite with Valgrind (${CMAKE_MEMORYCHECK_COMMAND})")
|
2018-03-29 23:19:21 +08:00
|
|
|
set(memcheck_command "${CMAKE_MEMORYCHECK_COMMAND} ${CMAKE_MEMORYCHECK_COMMAND_OPTIONS} --error-exitcode=1 --leak-check=full")
|
2017-08-26 02:12:21 +08:00
|
|
|
separate_arguments(memcheck_command)
|
|
|
|
endif()
|
|
|
|
|
2017-08-26 03:29:27 +08:00
|
|
|
if(JSON_NoExceptions)
|
|
|
|
message(STATUS "Building test suite without exceptions")
|
|
|
|
if(NOT MSVC)
|
|
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DJSON_NOEXCEPTION")
|
|
|
|
endif()
|
|
|
|
set(CATCH_TEST_FILTER -e)
|
|
|
|
endif()
|
|
|
|
|
2017-10-05 04:18:21 +08:00
|
|
|
if(JSON_Coverage)
|
|
|
|
message(STATUS "Building test suite with coverage information")
|
|
|
|
if(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
|
|
|
|
message(FATAL_ERROR "JSON_Coverage requires GCC.")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# enable profiling
|
|
|
|
set(CMAKE_CXX_FLAGS "--coverage -g -O0 -fprofile-arcs -ftest-coverage")
|
|
|
|
|
|
|
|
# from https://github.com/RWTH-HPC/CMake-codecov/blob/master/cmake/FindGcov.cmake
|
|
|
|
get_filename_component(COMPILER_PATH "${CMAKE_CXX_COMPILER}" PATH)
|
|
|
|
string(REGEX MATCH "^[0-9]+" GCC_VERSION "${CMAKE_CXX_COMPILER_VERSION}")
|
|
|
|
find_program(GCOV_BIN NAMES gcov-${GCC_VERSION} gcov HINTS ${COMPILER_PATH})
|
|
|
|
|
2018-02-10 04:30:15 +08:00
|
|
|
# collect all source files from the chosen include dir
|
|
|
|
file(GLOB_RECURSE SOURCE_FILES ${NLOHMANN_JSON_INCLUDE_BUILD_DIR}*.hpp)
|
|
|
|
|
2017-10-05 04:18:21 +08:00
|
|
|
# add target to collect coverage information and generate HTML file
|
2017-10-06 01:08:15 +08:00
|
|
|
# (filter script from https://stackoverflow.com/a/43726240/266378)
|
2017-10-05 04:18:21 +08:00
|
|
|
add_custom_target(lcov_html
|
|
|
|
COMMAND lcov --directory . --capture --output-file json.info --gcov-tool ${GCOV_BIN} --rc lcov_branch_coverage=1
|
2018-02-10 04:30:15 +08:00
|
|
|
COMMAND lcov -e json.info ${SOURCE_FILES} --output-file json.info.filtered --rc lcov_branch_coverage=1
|
2017-10-06 01:08:15 +08:00
|
|
|
COMMAND ${CMAKE_SOURCE_DIR}/test/thirdparty/imapdl/filterbr.py json.info.filtered > json.info.filtered.noexcept
|
|
|
|
COMMAND genhtml --title "JSON for Modern C++" --legend --demangle-cpp --output-directory html --show-details --branch-coverage json.info.filtered.noexcept
|
2017-10-05 04:18:21 +08:00
|
|
|
COMMENT "Generating HTML report test/html/index.html"
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
|
2017-08-26 00:06:22 +08:00
|
|
|
#############################################################################
|
|
|
|
# Catch library with the main function to speed up build
|
|
|
|
#############################################################################
|
|
|
|
|
2017-02-12 19:46:54 +08:00
|
|
|
add_library(catch_main OBJECT
|
|
|
|
"src/unit.cpp"
|
|
|
|
)
|
|
|
|
set_target_properties(catch_main PROPERTIES
|
2017-08-26 00:06:22 +08:00
|
|
|
COMPILE_DEFINITIONS "$<$<CXX_COMPILER_ID:MSVC>:_SCL_SECURE_NO_WARNINGS>"
|
|
|
|
COMPILE_OPTIONS "$<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>>"
|
2017-02-12 19:46:54 +08:00
|
|
|
)
|
2019-03-28 21:22:48 +08:00
|
|
|
if (${CMAKE_VERSION} VERSION_LESS "3.8.0")
|
2019-01-16 21:21:49 +08:00
|
|
|
target_compile_features(catch_main PUBLIC cxx_range_for)
|
|
|
|
else()
|
|
|
|
target_compile_features(catch_main PUBLIC cxx_std_11)
|
|
|
|
endif()
|
2017-02-12 19:46:54 +08:00
|
|
|
target_include_directories(catch_main PRIVATE "thirdparty/catch")
|
|
|
|
|
2017-10-16 16:02:48 +08:00
|
|
|
# https://stackoverflow.com/questions/2368811/how-to-set-warning-level-in-cmake
|
|
|
|
if(MSVC)
|
|
|
|
# Force to always compile with W4
|
|
|
|
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
|
|
|
|
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
|
|
|
|
else()
|
|
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
|
|
|
|
endif()
|
|
|
|
|
2017-10-18 13:53:35 +08:00
|
|
|
# Disable warning C4389: '==': signed/unsigned mismatch
|
|
|
|
# Disable warning C4309: 'static_cast': truncation of constant value
|
|
|
|
# Disable warning C4566: character represented by universal-character-name '\uFF01' cannot be represented in the current code page (1252)
|
|
|
|
# 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
|
2017-10-17 14:23:55 +08:00
|
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4389 /wd4309 /wd4566 /wd4996")
|
2018-06-29 12:04:41 +08:00
|
|
|
|
|
|
|
# https://github.com/nlohmann/json/issues/1114
|
|
|
|
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /bigobj")
|
2017-10-16 16:02:48 +08:00
|
|
|
endif()
|
|
|
|
|
2017-08-26 00:06:22 +08:00
|
|
|
#############################################################################
|
|
|
|
# one executable for each unit test file
|
|
|
|
#############################################################################
|
2016-05-11 08:25:54 +08:00
|
|
|
|
2017-08-26 00:06:22 +08:00
|
|
|
file(GLOB files "src/unit-*.cpp")
|
|
|
|
foreach(file ${files})
|
|
|
|
get_filename_component(file_basename ${file} NAME_WE)
|
|
|
|
string(REGEX REPLACE "unit-([^$]+)" "test-\\1" testcase ${file_basename})
|
2016-05-11 08:25:54 +08:00
|
|
|
|
2017-08-26 00:06:22 +08:00
|
|
|
add_executable(${testcase} $<TARGET_OBJECTS:catch_main> ${file})
|
2018-10-01 22:34:23 +08:00
|
|
|
target_compile_definitions(${testcase} PRIVATE
|
|
|
|
CATCH_CONFIG_FAST_COMPILE
|
|
|
|
$<$<CXX_COMPILER_ID:MSVC>:_SCL_SECURE_NO_WARNINGS>
|
|
|
|
)
|
|
|
|
target_compile_options(${testcase} PRIVATE
|
|
|
|
$<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>>
|
|
|
|
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated;-Wno-float-equal>
|
|
|
|
$<$<CXX_COMPILER_ID:GNU>:-Wno-deprecated-declarations>
|
|
|
|
)
|
|
|
|
target_include_directories(${testcase} PRIVATE
|
|
|
|
thirdparty/catch
|
|
|
|
thirdparty/fifo_map
|
2017-02-12 19:46:54 +08:00
|
|
|
)
|
2017-11-26 17:29:51 +08:00
|
|
|
target_link_libraries(${testcase} ${NLOHMANN_JSON_TARGET_NAME})
|
2017-08-26 00:06:22 +08:00
|
|
|
|
|
|
|
add_test(NAME "${testcase}_default"
|
2017-08-26 03:29:27 +08:00
|
|
|
COMMAND ${testcase} ${CATCH_TEST_FILTER}
|
2017-08-26 00:06:22 +08:00
|
|
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
2017-02-12 19:46:54 +08:00
|
|
|
)
|
2017-08-26 00:06:22 +08:00
|
|
|
set_tests_properties("${testcase}_default" PROPERTIES LABELS "default")
|
2017-02-12 19:46:54 +08:00
|
|
|
|
2017-08-26 00:06:22 +08:00
|
|
|
add_test(NAME "${testcase}_all"
|
2018-06-24 23:40:16 +08:00
|
|
|
COMMAND ${testcase} ${CATCH_TEST_FILTER} "*"
|
2017-08-26 00:06:22 +08:00
|
|
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
|
|
|
)
|
|
|
|
set_tests_properties("${testcase}_all" PROPERTIES LABELS "all")
|
2017-08-26 02:12:21 +08:00
|
|
|
|
|
|
|
if(JSON_Valgrind)
|
|
|
|
add_test(NAME "${testcase}_valgrind"
|
2017-08-26 03:29:27 +08:00
|
|
|
COMMAND ${memcheck_command} ${CMAKE_CURRENT_BINARY_DIR}/${testcase} ${CATCH_TEST_FILTER}
|
2017-08-26 02:12:21 +08:00
|
|
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
|
|
|
)
|
|
|
|
set_tests_properties("${testcase}_valgrind" PROPERTIES LABELS "valgrind")
|
|
|
|
endif()
|
2017-08-26 00:06:22 +08:00
|
|
|
endforeach()
|
2018-09-26 22:46:34 +08:00
|
|
|
|
|
|
|
#############################################################################
|
|
|
|
# Test the generated build configs
|
|
|
|
#############################################################################
|
|
|
|
add_subdirectory(cmake_import)
|
|
|
|
add_subdirectory(cmake_import_minver)
|
|
|
|
add_subdirectory(cmake_add_subdirectory)
|