Paul Bakker | b06819b | 2011-01-18 16:18:38 +0000 | [diff] [blame] | 1 | set(libs |
Raef Coles | 995c66f | 2020-10-13 16:30:41 +0100 | [diff] [blame] | 2 | ${mbedtls_target} |
Paul Bakker | b06819b | 2011-01-18 16:18:38 +0000 | [diff] [blame] | 3 | ) |
| 4 | |
Ashley Duncan | d85a7e9 | 2019-04-29 20:35:06 +1200 | [diff] [blame] | 5 | # Set the project root directory if it's not already defined, as may happen if |
| 6 | # the tests folder is included directly by a parent project, without including |
| 7 | # the top level CMakeLists.txt. |
| 8 | if(NOT DEFINED MBEDTLS_DIR) |
| 9 | set(MBEDTLS_DIR ${CMAKE_SOURCE_DIR}) |
| 10 | endif() |
| 11 | |
okhowang(王沛文) | 3c1b090 | 2020-03-25 19:55:32 +0800 | [diff] [blame] | 12 | if(NOT MBEDTLS_PYTHON_EXECUTABLE) |
| 13 | message(FATAL_ERROR "Cannot build test suites without Python 3") |
Nicholas Wilson | 733676b | 2015-11-14 13:09:01 +0000 | [diff] [blame] | 14 | endif() |
| 15 | |
Manuel Pégourié-Gonnard | 313bcfc | 2021-09-07 12:16:49 +0200 | [diff] [blame] | 16 | # generated .data files will go there |
| 17 | file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/suites) |
| 18 | |
David Horstmann | 7570d24 | 2021-10-20 16:27:24 +0100 | [diff] [blame] | 19 | # Get base names for generated files (starting at "suites/") |
David Horstmann | 1732b5d | 2021-10-19 16:43:53 +0100 | [diff] [blame] | 20 | execute_process( |
| 21 | COMMAND |
David Horstmann | 3e30ad9 | 2021-10-20 16:53:58 +0100 | [diff] [blame] | 22 | ${MBEDTLS_PYTHON_EXECUTABLE} |
David Horstmann | 1732b5d | 2021-10-19 16:43:53 +0100 | [diff] [blame] | 23 | ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_psa_tests.py |
| 24 | --list-for-cmake |
David Horstmann | 7570d24 | 2021-10-20 16:27:24 +0100 | [diff] [blame] | 25 | --directory suites |
David Horstmann | 1732b5d | 2021-10-19 16:43:53 +0100 | [diff] [blame] | 26 | WORKING_DIRECTORY |
| 27 | ${CMAKE_CURRENT_SOURCE_DIR}/.. |
| 28 | OUTPUT_VARIABLE |
David Horstmann | 7570d24 | 2021-10-20 16:27:24 +0100 | [diff] [blame] | 29 | base_generated_data_files) |
| 30 | |
| 31 | # Derive generated file paths in the build directory |
| 32 | set(generated_data_files "") |
| 33 | foreach(file ${base_generated_data_files}) |
| 34 | list(APPEND generated_data_files ${CMAKE_CURRENT_BINARY_DIR}/${file}) |
| 35 | endforeach() |
David Horstmann | 1732b5d | 2021-10-19 16:43:53 +0100 | [diff] [blame] | 36 | |
David Horstmann | a8d1406 | 2021-10-20 17:14:23 +0100 | [diff] [blame] | 37 | if(GEN_FILES) |
Manuel Pégourié-Gonnard | e90e405 | 2021-09-08 13:27:09 +0200 | [diff] [blame] | 38 | add_custom_command( |
| 39 | OUTPUT |
David Horstmann | ae7bd35 | 2021-10-19 19:05:42 +0100 | [diff] [blame] | 40 | ${generated_data_files} |
Manuel Pégourié-Gonnard | e90e405 | 2021-09-08 13:27:09 +0200 | [diff] [blame] | 41 | WORKING_DIRECTORY |
| 42 | ${CMAKE_CURRENT_SOURCE_DIR}/.. |
| 43 | COMMAND |
| 44 | ${MBEDTLS_PYTHON_EXECUTABLE} |
| 45 | ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_psa_tests.py |
| 46 | --directory ${CMAKE_CURRENT_BINARY_DIR}/suites |
| 47 | DEPENDS |
| 48 | ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_psa_tests.py |
| 49 | ${CMAKE_CURRENT_SOURCE_DIR}/../include/psa/crypto_config.h |
| 50 | ${CMAKE_CURRENT_SOURCE_DIR}/../include/psa/crypto_values.h |
| 51 | ${CMAKE_CURRENT_SOURCE_DIR}/../include/psa/crypto_extra.h |
| 52 | ) |
Jerry Yu | 80bc860 | 2022-01-17 18:26:52 +0800 | [diff] [blame^] | 53 | |
Manuel Pégourié-Gonnard | e90e405 | 2021-09-08 13:27:09 +0200 | [diff] [blame] | 54 | else() |
David Horstmann | 7570d24 | 2021-10-20 16:27:24 +0100 | [diff] [blame] | 55 | foreach(file ${base_generated_data_files}) |
David Horstmann | 1732b5d | 2021-10-19 16:43:53 +0100 | [diff] [blame] | 56 | link_to_source(${file}) |
| 57 | endforeach() |
Manuel Pégourié-Gonnard | e90e405 | 2021-09-08 13:27:09 +0200 | [diff] [blame] | 58 | endif() |
Jerry Yu | 80bc860 | 2022-01-17 18:26:52 +0800 | [diff] [blame^] | 59 | # CMake generate sub-makefile for each target and call it in subprocess. Without |
| 60 | # this command, cmake will generate ruler in each sub makefile. As a result, |
| 61 | # they will conflict under parllel build. |
| 62 | # With this line, only 4 sub-makefiles include above command, that reduces |
| 63 | # conflict risk. |
| 64 | add_custom_target(test_suite_generated_data DEPENDS ${generated_data_files}) |
Gilles Peskine | ac372cc | 2018-11-29 10:15:06 +0000 | [diff] [blame] | 65 | # Test suites caught by SKIP_TEST_SUITES are built but not executed. |
| 66 | # "foo" as a skip pattern skips "test_suite_foo" and "test_suite_foo.bar" |
| 67 | # but not "test_suite_foobar". |
| 68 | string(REGEX REPLACE "[ ,;]" "|" SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES}") |
| 69 | string(REPLACE "." "\\." SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES_REGEX}") |
| 70 | set(SKIP_TEST_SUITES_REGEX "^(${SKIP_TEST_SUITES_REGEX})(\$|\\.)") |
| 71 | |
Paul Bakker | 7304376 | 2011-07-13 15:03:10 +0000 | [diff] [blame] | 72 | function(add_test_suite suite_name) |
| 73 | if(ARGV1) |
Manuel Pégourié-Gonnard | 216a183 | 2015-06-25 09:20:03 +0200 | [diff] [blame] | 74 | set(data_name ${ARGV1}) |
Paul Bakker | 7304376 | 2011-07-13 15:03:10 +0000 | [diff] [blame] | 75 | else() |
Manuel Pégourié-Gonnard | 216a183 | 2015-06-25 09:20:03 +0200 | [diff] [blame] | 76 | set(data_name ${suite_name}) |
Paul Bakker | 46c1794 | 2011-07-13 14:54:54 +0000 | [diff] [blame] | 77 | endif() |
| 78 | |
David Horstmann | ae7bd35 | 2021-10-19 19:05:42 +0100 | [diff] [blame] | 79 | # Get the test names of the tests with generated .data files |
| 80 | # from the generated_data_files list in parent scope. |
| 81 | set(generated_data_names "") |
| 82 | foreach(generated_data_file ${generated_data_files}) |
| 83 | # Get the plain filename |
| 84 | get_filename_component(generated_data_name ${generated_data_file} NAME) |
| 85 | # Remove the ".data" extension |
| 86 | get_name_without_last_ext(generated_data_name ${generated_data_name}) |
| 87 | # Remove leading "test_suite_" |
| 88 | string(SUBSTRING ${generated_data_name} 11 -1 generated_data_name) |
| 89 | list(APPEND generated_data_names ${generated_data_name}) |
| 90 | endforeach() |
| 91 | |
| 92 | if(";${generated_data_names};" MATCHES ";${data_name};") |
Manuel Pégourié-Gonnard | ce3ba8f | 2021-05-14 12:03:37 +0200 | [diff] [blame] | 93 | set(data_file |
| 94 | ${CMAKE_CURRENT_BINARY_DIR}/suites/test_suite_${data_name}.data) |
| 95 | else() |
| 96 | set(data_file |
| 97 | ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data) |
| 98 | endif() |
| 99 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 100 | add_custom_command( |
Manuel Pégourié-Gonnard | 42681f3 | 2021-05-14 10:53:43 +0200 | [diff] [blame] | 101 | OUTPUT |
David Horstmann | f602eb1 | 2021-10-20 16:40:56 +0100 | [diff] [blame] | 102 | # The output filename of generate_test_code.py is derived from the -d |
| 103 | # input argument. |
Manuel Pégourié-Gonnard | 42681f3 | 2021-05-14 10:53:43 +0200 | [diff] [blame] | 104 | test_suite_${data_name}.c |
| 105 | COMMAND |
| 106 | ${MBEDTLS_PYTHON_EXECUTABLE} |
| 107 | ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py |
| 108 | -f ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function |
Manuel Pégourié-Gonnard | ce3ba8f | 2021-05-14 12:03:37 +0200 | [diff] [blame] | 109 | -d ${data_file} |
Manuel Pégourié-Gonnard | 42681f3 | 2021-05-14 10:53:43 +0200 | [diff] [blame] | 110 | -t ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function |
| 111 | -p ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function |
| 112 | -s ${CMAKE_CURRENT_SOURCE_DIR}/suites |
| 113 | --helpers-file ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function |
| 114 | -o . |
| 115 | DEPENDS |
| 116 | ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py |
| 117 | ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function |
Manuel Pégourié-Gonnard | ce3ba8f | 2021-05-14 12:03:37 +0200 | [diff] [blame] | 118 | ${data_file} |
Manuel Pégourié-Gonnard | 42681f3 | 2021-05-14 10:53:43 +0200 | [diff] [blame] | 119 | ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function |
| 120 | ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function |
| 121 | ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function |
| 122 | ${mbedtls_target} |
Manuel Pégourié-Gonnard | 389150d | 2021-09-09 10:51:16 +0200 | [diff] [blame] | 123 | BYPRODUCTS |
| 124 | test_suite_${data_name}.datax |
Paul Bakker | 4c14a25 | 2010-06-18 22:54:05 +0000 | [diff] [blame] | 125 | ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 126 | |
Ronald Cron | f5ea29a | 2020-06-19 10:42:29 +0200 | [diff] [blame] | 127 | add_executable(test_suite_${data_name} test_suite_${data_name}.c $<TARGET_OBJECTS:mbedtls_test>) |
Jerry Yu | 80bc860 | 2022-01-17 18:26:52 +0800 | [diff] [blame^] | 128 | add_dependencies(test_suite_${data_name} test_suite_generated_data) |
Paul Bakker | 7304376 | 2011-07-13 15:03:10 +0000 | [diff] [blame] | 129 | target_link_libraries(test_suite_${data_name} ${libs}) |
Gilles Peskine | 76dd3aa | 2020-07-02 15:58:37 +0200 | [diff] [blame] | 130 | # Include test-specific header files from ./include and private header |
| 131 | # files (used by some invasive tests) from ../library. Public header |
| 132 | # files are automatically included because the library targets declare |
| 133 | # them as PUBLIC. |
Ronald Cron | 8552741 | 2020-05-15 17:20:47 +0200 | [diff] [blame] | 134 | target_include_directories(test_suite_${data_name} |
| 135 | PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include |
| 136 | PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../library) |
| 137 | |
Gilles Peskine | ac372cc | 2018-11-29 10:15:06 +0000 | [diff] [blame] | 138 | if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX}) |
| 139 | message(STATUS "The test suite ${data_name} will not be executed.") |
| 140 | else() |
| 141 | add_test(${data_name}-suite test_suite_${data_name} --verbose) |
| 142 | endif() |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 143 | endfunction(add_test_suite) |
| 144 | |
Manuel Pégourié-Gonnard | 42681f3 | 2021-05-14 10:53:43 +0200 | [diff] [blame] | 145 | # Enable definition of various functions used throughout the testsuite |
| 146 | # (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless |
| 147 | # on non-POSIX platforms. |
| 148 | add_definitions("-D_POSIX_C_SOURCE=200809L") |
| 149 | |
Gilles Peskine | f29019f | 2021-02-23 13:44:41 +0100 | [diff] [blame] | 150 | if(CMAKE_COMPILER_IS_CLANG) |
| 151 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code") |
| 152 | endif(CMAKE_COMPILER_IS_CLANG) |
| 153 | |
Manuel Pégourié-Gonnard | 3083053 | 2015-07-01 17:06:28 +0200 | [diff] [blame] | 154 | if(MSVC) |
Simon B | 9b3e3c4 | 2016-11-03 01:12:50 +0000 | [diff] [blame] | 155 | # If a warning level has been defined, suppress all warnings for test code |
| 156 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W0") |
| 157 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX-") |
Manuel Pégourié-Gonnard | 3083053 | 2015-07-01 17:06:28 +0200 | [diff] [blame] | 158 | endif(MSVC) |
| 159 | |
Gilles Peskine | 29080e8 | 2022-01-25 22:30:25 +0100 | [diff] [blame] | 160 | file(GLOB test_suites RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" suites/*.data) |
| 161 | list(APPEND test_suites ${base_generated_data_files}) |
| 162 | # If the generated .data files are present in the source tree, we just added |
| 163 | # them twice, both through GLOB and through ${base_generated_data_files}. |
| 164 | list(REMOVE_DUPLICATES test_suites) |
| 165 | list(SORT test_suites) |
| 166 | foreach(test_suite ${test_suites}) |
Gilles Peskine | cd55fe0 | 2022-01-25 23:13:49 +0100 | [diff] [blame] | 167 | get_filename_component(data_name ${test_suite} NAME) |
Gilles Peskine | a18fb9d | 2022-01-25 23:24:34 +0100 | [diff] [blame] | 168 | string(REGEX REPLACE "\\.data\$" "" data_name "${data_name}") |
Gilles Peskine | cd55fe0 | 2022-01-25 23:13:49 +0100 | [diff] [blame] | 169 | string(REPLACE "test_suite_" "" data_name "${data_name}") |
| 170 | string(REGEX MATCH "[^.]*" function_name "${data_name}") |
Gilles Peskine | 29080e8 | 2022-01-25 22:30:25 +0100 | [diff] [blame] | 171 | add_test_suite(${function_name} ${data_name}) |
| 172 | endforeach(test_suite) |
Paul Bakker | cd6d69a | 2014-02-06 15:43:21 +0100 | [diff] [blame] | 173 | |
Gilles Peskine | 8405257 | 2018-03-21 12:12:47 +0100 | [diff] [blame] | 174 | # Make scripts and data files needed for testing available in an |
| 175 | # out-of-source build. |
Paul Bakker | cd6d69a | 2014-02-06 15:43:21 +0100 | [diff] [blame] | 176 | if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) |
Andrzej Kurek | 4a71cfa | 2019-05-06 05:06:06 -0400 | [diff] [blame] | 177 | if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/seedfile") |
| 178 | link_to_source(seedfile) |
| 179 | endif() |
Gilles Peskine | 8405257 | 2018-03-21 12:12:47 +0100 | [diff] [blame] | 180 | link_to_source(compat.sh) |
David Brown | c744418 | 2020-10-16 13:19:49 -0600 | [diff] [blame] | 181 | link_to_source(context-info.sh) |
Gilles Peskine | 8405257 | 2018-03-21 12:12:47 +0100 | [diff] [blame] | 182 | link_to_source(data_files) |
| 183 | link_to_source(scripts) |
| 184 | link_to_source(ssl-opt.sh) |
Jerry Yu | 55ee769 | 2021-11-29 13:26:55 +0800 | [diff] [blame] | 185 | link_to_source(opt-testcases) |
Paul Bakker | cd6d69a | 2014-02-06 15:43:21 +0100 | [diff] [blame] | 186 | endif() |