Debian Codegraph @

crt/aws-c-common/cmake/AwsTestHarness.cmake

raw
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0.
3
4 include(AwsCFlags)
5 include(AwsSanitizers)
6
7 option(ENABLE_NET_TESTS "Run tests requiring an internet connection." ON)
8
9 # Maintain a global list of AWS_TEST_CASES
10 define_property(GLOBAL PROPERTY AWS_TEST_CASES BRIEF_DOCS "Test Cases" FULL_DOCS "Test Cases")
11 set(AWS_TEST_CASES "" CACHE INTERNAL "Test cases valid for this configuration")
12
13 # The return value for the skipped test cases. Refer to the return code defined in aws_test_harness.h:
14 # #define SKIP (103)
15 set(SKIP_RETURN_CODE_VALUE 103)
16
17 # Registers a test case by name (the first argument to the AWS_TEST_CASE macro in aws_test_harness.h)
18 macro(add_test_case name)
19 list(APPEND TEST_CASES "${name}")
20 list(APPEND AWS_TEST_CASES "${name}")
21 set_property(GLOBAL PROPERTY AWS_TEST_CASES ${AWS_TEST_CASES})
22 endmacro()
23
24 # Like add_test_case, but for tests that require a working internet connection.
25 macro(add_net_test_case name)
26 if (ENABLE_NET_TESTS)
27 list(APPEND TEST_CASES "${name}")
28 list(APPEND AWS_TEST_CASES "${name}")
29 set_property(GLOBAL PROPERTY AWS_TEST_CASES ${AWS_TEST_CASES})
30 endif()
31 endmacro()
32
33 # Generate a test driver executable with the given name
34 function(generate_test_driver driver_exe_name)
35 create_test_sourcelist(test_srclist test_runner.c ${TEST_CASES})
36 # Write clang tidy file that disables all but one check to avoid false positives
37 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/.clang-tidy" "Checks: '-*,misc-static-assert'")
38
39 add_executable(${driver_exe_name} ${CMAKE_CURRENT_BINARY_DIR}/test_runner.c ${TESTS})
40 aws_set_common_properties(${driver_exe_name} NO_WEXTRA NO_PEDANTIC)
41
42 # Some versions of CMake (3.9-3.11) generate a test_runner.c file with
43 # a strncpy() call that triggers the "stringop-overflow" warning in GCC 8.1+
44 # This warning doesn't exist until GCC 7 though, so test for it before disabling.
45 if (NOT MSVC)
46 check_c_compiler_flag(-Wno-stringop-overflow HAS_WNO_STRINGOP_OVERFLOW)
47 if (HAS_WNO_STRINGOP_OVERFLOW)
48 SET_SOURCE_FILES_PROPERTIES(test_runner.c PROPERTIES COMPILE_FLAGS -Wno-stringop-overflow)
49 endif()
50 endif()
51
52 aws_add_sanitizers(${driver_exe_name} ${${PROJECT_NAME}_SANITIZERS})
53
54 target_link_libraries(${driver_exe_name} PRIVATE ${PROJECT_NAME})
55
56 set_target_properties(${driver_exe_name} PROPERTIES
57 LINKER_LANGUAGE C
58 C_STANDARD 99
59 # Our ASSERT(...) macros rely on (widely supported) ##__VA_ARGS__ extension
60 C_EXTENSIONS ON
61 )
62
63 target_compile_definitions(${driver_exe_name} PRIVATE AWS_UNSTABLE_TESTING_API=1)
64 target_include_directories(${driver_exe_name} PRIVATE ${CMAKE_CURRENT_LIST_DIR})
65
66 # Export symbols so that backtraces are more likely to show function names.
67 set_target_properties(${driver_exe_name} PROPERTIES ENABLE_EXPORTS ON)
68
69 foreach(name IN LISTS TEST_CASES)
70 add_test(${name} ${driver_exe_name} "${name}")
71 set_tests_properties("${name}" PROPERTIES SKIP_RETURN_CODE ${SKIP_RETURN_CODE_VALUE})
72 endforeach()
73
74 # Clear test cases in case another driver needs to be generated
75 unset(TEST_CASES PARENT_SCOPE)
76 endfunction()
77
78 function(generate_cpp_test_driver driver_exe_name)
79 create_test_sourcelist(test_srclist test_runner.cpp ${TEST_CASES})
80
81 add_executable(${driver_exe_name} ${CMAKE_CURRENT_BINARY_DIR}/test_runner.cpp ${TESTS})
82 target_link_libraries(${driver_exe_name} PRIVATE ${PROJECT_NAME})
83
84 set_target_properties(${driver_exe_name} PROPERTIES
85 LINKER_LANGUAGE CXX
86 # Our ASSERT(...) macros rely on (widely supported) ##__VA_ARGS__ extension
87 CXX_EXTENSIONS ON
88 )
89 if (MSVC)
90 if(AWS_STATIC_MSVC_RUNTIME_LIBRARY OR STATIC_CRT)
91 target_compile_options(${driver_exe_name} PRIVATE "/MT$<$<CONFIG:Debug>:d>")
92 else()
93 target_compile_options(${driver_exe_name} PRIVATE "/MD$<$<CONFIG:Debug>:d>")
94 endif()
95 endif()
96 target_compile_definitions(${driver_exe_name} PRIVATE AWS_UNSTABLE_TESTING_API=1)
97 target_include_directories(${driver_exe_name} PRIVATE ${CMAKE_CURRENT_LIST_DIR})
98
99 foreach(name IN LISTS TEST_CASES)
100 add_test(${name} ${driver_exe_name} "${name}")
101 set_tests_properties("${name}" PROPERTIES SKIP_RETURN_CODE ${SKIP_RETURN_CODE_VALUE})
102 endforeach()
103
104 # Clear test cases in case another driver needs to be generated
105 unset(TEST_CASES PARENT_SCOPE)
106 endfunction()