From c2ab398d01cf7bec9fda5796c08cc3043c71a570 Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Wed, 17 Jan 2024 12:25:19 +0000 Subject: [PATCH] Request C11 in CMake (but only for tests) Set the C_STANDARD property on the mbedtls_test target to 11. This requests C11 for the tests only. If C11 is not supported the build will not fail, since C_STANDARD_REQUIRED is not set, and memory poisoning will be disabled by a preprocessor check on __STDC_VERSION__. Additionally, reintroduce previous C99 enforcement on the rest of the library. Signed-off-by: David Horstmann --- CMakeLists.txt | 5 +++++ programs/test/CMakeLists.txt | 3 +++ tests/CMakeLists.txt | 2 ++ 3 files changed, 10 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 210cc38f34..0cb208e988 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -177,6 +177,9 @@ string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}") include(CheckCCompilerFlag) +set(CMAKE_C_EXTENSIONS OFF) +set(CMAKE_C_STANDARD 99) + if(CMAKE_COMPILER_IS_GNU) # some warnings we want are not available with old GCC versions # note: starting with CMake 2.8 we could use CMAKE_C_COMPILER_VERSION @@ -294,6 +297,8 @@ if(ENABLE_TESTING OR ENABLE_PROGRAMS) PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/tests/include PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/library) + # Request C11, needed for memory poisoning tests + set_target_properties(mbedtls_test PROPERTIES C_STANDARD 11) file(GLOB MBEDTLS_TEST_HELPER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/tests/src/test_helpers/*.c) diff --git a/programs/test/CMakeLists.txt b/programs/test/CMakeLists.txt index 0778731125..5a26821b51 100644 --- a/programs/test/CMakeLists.txt +++ b/programs/test/CMakeLists.txt @@ -78,6 +78,9 @@ foreach(exe IN LISTS executables_libs executables_mbedcrypto) target_include_directories(${exe} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) endif() + # Request C11, required for memory poisoning + set_target_properties(${exe} PROPERTIES C_STANDARD 11) + # This emulates "if ( ... IN_LIST ... )" which becomes available in CMake 3.3 list(FIND executables_libs ${exe} exe_index) if (${exe_index} GREATER -1) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0869aaa018..188d5d595f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -251,6 +251,8 @@ function(add_test_suite suite_name) target_include_directories(test_suite_${data_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../library) + # Request C11, which is needed for memory poisoning tests + set_target_properties(test_suite_${data_name} PROPERTIES C_STANDARD 11) if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX}) message(STATUS "The test suite ${data_name} will not be executed.")