| 1 |
# Helper so we don't have to repeat ourselves so much |
| 2 |
# Usage: add_cpp_test(testname [COMPILE_ONLY] [SOURCES a.cpp b.cpp ...] [LABELS acceptance per_implementation ...]) |
| 3 |
# SOURCES defaults to testname.cpp if not specified. |
| 4 |
function(add_cpp_test TEST_NAME) |
| 5 |
# Parse arguments |
| 6 |
cmake_parse_arguments(PARSE_ARGV 1 ARGS "COMPILE_ONLY;LIBRARY;WILL_FAIL" "" "SOURCES;LABELS;DEPENDENCY_OF") |
| 7 |
if (NOT ARGS_SOURCES) |
| 8 |
list(APPEND ARGS_SOURCES ${TEST_NAME}.cpp) |
| 9 |
endif() |
| 10 |
if (ARGS_COMPILE_ONLY) |
| 11 |
list(APPEND ${ARGS_LABELS} compile_only) |
| 12 |
endif() |
| 13 |
if (IS_UTF8_SANITIZE) |
| 14 |
add_compile_options(-fsanitize=address -fno-omit-frame-pointer -fno-sanitize-recover=all) |
| 15 |
add_compile_definitions(ASAN_OPTIONS=detect_leaks=1) |
| 16 |
endif() |
| 17 |
# Add the compile target |
| 18 |
if (ARGS_LIBRARY) |
| 19 |
add_library(${TEST_NAME} STATIC ${ARGS_SOURCES}) |
| 20 |
else(ARGS_LIBRARY) |
| 21 |
add_executable(${TEST_NAME} ${ARGS_SOURCES}) |
| 22 |
endif(ARGS_LIBRARY) |
| 23 |
|
| 24 |
# Add test |
| 25 |
if (ARGS_COMPILE_ONLY OR ARGS_LIBRARY) |
| 26 |
add_test( |
| 27 |
NAME ${TEST_NAME} |
| 28 |
COMMAND ${CMAKE_COMMAND} --build . --target ${TEST_NAME} --config $<CONFIGURATION> |
| 29 |
WORKING_DIRECTORY ${PROJECT_BINARY_DIR} |
| 30 |
) |
| 31 |
set_target_properties(${TEST_NAME} PROPERTIES EXCLUDE_FROM_ALL TRUE EXCLUDE_FROM_DEFAULT_BUILD TRUE) |
| 32 |
else() |
| 33 |
add_test(${TEST_NAME} ${TEST_NAME}) |
| 34 |
|
| 35 |
# Add to <label>_tests make targets |
| 36 |
foreach(label ${ARGS_LABELS}) |
| 37 |
list(APPEND ARGS_DEPENDENCY_OF ${label}) |
| 38 |
endforeach(label ${ARGS_LABELS}) |
| 39 |
endif() |
| 40 |
|
| 41 |
# Add to test labels |
| 42 |
if (ARGS_LABELS) |
| 43 |
set_property(TEST ${TEST_NAME} APPEND PROPERTY LABELS ${ARGS_LABELS}) |
| 44 |
endif() |
| 45 |
|
| 46 |
# Add as a dependency of given targets |
| 47 |
foreach(dependency_of ${ARGS_DEPENDENCY_OF}) |
| 48 |
if (NOT TARGET ${dependency_of}_tests) |
| 49 |
add_custom_target(${dependency_of}_tests) |
| 50 |
add_dependencies(all_tests ${dependency_of}_tests) |
| 51 |
endif(NOT TARGET ${dependency_of}_tests) |
| 52 |
add_dependencies(${dependency_of}_tests ${TEST_NAME}) |
| 53 |
endforeach(dependency_of ${ARGS_DEPENDENCY_OF}) |
| 54 |
|
| 55 |
# If it will fail, mark the test as such |
| 56 |
if (ARGS_WILL_FAIL) |
| 57 |
set_property(TEST ${TEST_NAME} PROPERTY WILL_FAIL TRUE) |
| 58 |
endif() |
| 59 |
endfunction() |