| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 1 | # |
| 2 | # CMake build system design considerations: |
| 3 | # |
| 4 | # - Include directories: |
| 5 | # + Do not define include directories globally using the include_directories |
| 6 | # command but rather at the target level using the |
| 7 | # target_include_directories command. That way, it is easier to guarantee |
| 8 | # that targets are built using the proper list of include directories. |
| 9 | # + Use the PUBLIC and PRIVATE keywords to specify the scope of include |
| 10 | # directories. That way, a target linking to a library (using the |
| 11 | # target_link_libraries command) inherits from the library PUBLIC include |
| 12 | # directories and not from the PRIVATE ones. |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 13 | # - TF_PSA_CRYPTO_TARGET_PREFIX: CMake targets are designed to be alterable by |
| 14 | # calling CMake in order to avoid target name clashes, via the use of |
| 15 | # TF_PSA_CRYPTO_TARGET_PREFIX. The value of this variable is prefixed to the |
| 16 | # tfpsacrypto and apidoc targets. |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 17 | # |
| 18 | |
| 19 | # We specify a minimum requirement of 3.10.2, but for now use 3.5.1 here |
| 20 | # until our infrastructure catches up. |
| 21 | cmake_minimum_required(VERSION 3.5.1) |
| 22 | |
| 23 | include(CMakePackageConfigHelpers) |
| 24 | |
| 25 | # Include convenience functions for printing properties and variables, like |
| 26 | # cmake_print_properties(), cmake_print_variables(). |
| 27 | include(CMakePrintHelpers) |
| 28 | |
| 29 | # https://cmake.org/cmake/help/latest/policy/CMP0011.html |
| 30 | # Setting this policy is required in CMake >= 3.18.0, otherwise a warning is generated. The OLD |
| 31 | # policy setting is deprecated, and will be removed in future versions. |
| 32 | cmake_policy(SET CMP0011 NEW) |
| 33 | # https://cmake.org/cmake/help/latest/policy/CMP0012.html |
| 34 | # Setting the CMP0012 policy to NEW is required for FindPython3 to work with CMake 3.18.2 |
| 35 | # (there is a bug in this particular version), otherwise, setting the CMP0012 policy is required |
| 36 | # for CMake versions >= 3.18.3 otherwise a deprecated warning is generated. The OLD policy setting |
| 37 | # is deprecated and will be removed in future versions. |
| 38 | cmake_policy(SET CMP0012 NEW) |
| 39 | |
| 40 | if(TEST_CPP) |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 41 | project("TF-PSA-Crypto" |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 42 | LANGUAGES C CXX |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 43 | VERSION 0.1.0 |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 44 | ) |
| 45 | else() |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 46 | project("TF-PSA-Crypto" |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 47 | LANGUAGES C |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 48 | VERSION 0.1.0 |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 49 | ) |
| 50 | endif() |
| 51 | |
| 52 | include(GNUInstallDirs) |
| 53 | |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 54 | # Determine if TF-PSA-Crypto is being built as a subproject using add_subdirectory() |
| 55 | if(NOT DEFINED TF_PSA_CRYPTO_AS_SUBPROJECT) |
| 56 | set(TF_PSA_CRYPTO_AS_SUBPROJECT ON) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 57 | if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 58 | set(TF_PSA_CRYPTO_AS_SUBPROJECT OFF) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 59 | endif() |
| 60 | endif() |
| 61 | |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 62 | # Set the project, Mbed TLS and framework root directory. |
| 63 | set(TF_PSA_CRYPTO_DIR ${CMAKE_CURRENT_SOURCE_DIR}) |
| 64 | set(MBEDTLS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/..) |
| 65 | set(MBEDTLS_FRAMEWORK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../framework) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 66 | |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 67 | option(ENABLE_PROGRAMS "Build TF-PSA-Crypto programs." ON) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 68 | |
| 69 | option(UNSAFE_BUILD "Allow unsafe builds. These builds ARE NOT SECURE." OFF) |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 70 | option(TF_PSA_CRYPTO_FATAL_WARNINGS "Compiler warnings treated as errors" ON) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 71 | if(CMAKE_HOST_WIN32) |
| 72 | # N.B. The comment on the next line is significant! If you change it, |
| 73 | # edit the sed command in prepare_release.sh that modifies |
| 74 | # CMakeLists.txt. |
| 75 | option(GEN_FILES "Generate the auto-generated files as needed" OFF) # off in development |
| 76 | else() |
| 77 | option(GEN_FILES "Generate the auto-generated files as needed" ON) |
| 78 | endif() |
| 79 | |
| Ronald Cron | 4c3fa0a | 2024-07-30 10:50:39 +0200 | [diff] [blame] | 80 | # Support for package config and install to be added later. |
| 81 | option(DISABLE_PACKAGE_CONFIG_AND_INSTALL "Disable package configuration, target export and installation" ON) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 82 | |
| 83 | if (CMAKE_C_SIMULATE_ID) |
| 84 | set(COMPILER_ID ${CMAKE_C_SIMULATE_ID}) |
| 85 | else() |
| 86 | set(COMPILER_ID ${CMAKE_C_COMPILER_ID}) |
| 87 | endif(CMAKE_C_SIMULATE_ID) |
| 88 | |
| 89 | string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${COMPILER_ID}") |
| 90 | string(REGEX MATCH "GNU" CMAKE_COMPILER_IS_GNU "${COMPILER_ID}") |
| 91 | string(REGEX MATCH "IAR" CMAKE_COMPILER_IS_IAR "${COMPILER_ID}") |
| 92 | string(REGEX MATCH "MSVC" CMAKE_COMPILER_IS_MSVC "${COMPILER_ID}") |
| 93 | |
| 94 | # the test suites currently have compile errors with MSVC |
| 95 | if(CMAKE_COMPILER_IS_MSVC) |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 96 | option(ENABLE_TESTING "Build TF-PSA-Crypto tests." OFF) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 97 | else() |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 98 | option(ENABLE_TESTING "Build TF-PSA-Crypto tests." ON) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 99 | endif() |
| 100 | |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 101 | option(USE_STATIC_TF_PSA_CRYPTO_LIBRARY "Build TF-PSA-Crypto static library." ON) |
| 102 | option(USE_SHARED_TF_PSA_CRYPTO_LIBRARY "Build TF-PSA-Crypto shared library." OFF) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 103 | option(LINK_WITH_PTHREAD "Explicitly link Mbed TLS library to pthread." OFF) |
| 104 | option(LINK_WITH_TRUSTED_STORAGE "Explicitly link Mbed TLS library to trusted_storage." OFF) |
| 105 | |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 106 | set(mbedcrypto_target "${TF_PSA_CRYPTO_TARGET_PREFIX}mbedcrypto") |
| 107 | if (USE_STATIC_TF_PSA_CRYPTO_LIBRARY) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 108 | set(mbedcrypto_static_target ${mbedcrypto_target}) |
| 109 | endif() |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 110 | if(USE_STATIC_TF_PSA_CRYPTO_LIBRARY AND USE_SHARED_TF_PSA_CRYPTO_LIBRARY) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 111 | string(APPEND mbedcrypto_static_target "_static") |
| 112 | endif() |
| 113 | |
| 114 | # Warning string - created as a list for compatibility with CMake 2.8 |
| 115 | set(CTR_DRBG_128_BIT_KEY_WARN_L1 "**** WARNING! MBEDTLS_CTR_DRBG_USE_128_BIT_KEY defined!\n") |
| 116 | set(CTR_DRBG_128_BIT_KEY_WARN_L2 "**** Using 128-bit keys for CTR_DRBG limits the security of generated\n") |
| 117 | set(CTR_DRBG_128_BIT_KEY_WARN_L3 "**** keys and operations that use random values generated to 128-bit security\n") |
| 118 | |
| 119 | set(CTR_DRBG_128_BIT_KEY_WARNING "${WARNING_BORDER}" |
| 120 | "${CTR_DRBG_128_BIT_KEY_WARN_L1}" |
| 121 | "${CTR_DRBG_128_BIT_KEY_WARN_L2}" |
| 122 | "${CTR_DRBG_128_BIT_KEY_WARN_L3}" |
| 123 | "${WARNING_BORDER}") |
| 124 | |
| 125 | # Python 3 is only needed here to check for configuration warnings. |
| 126 | if(NOT CMAKE_VERSION VERSION_LESS 3.15.0) |
| 127 | set(Python3_FIND_STRATEGY LOCATION) |
| 128 | find_package(Python3 COMPONENTS Interpreter) |
| 129 | if(Python3_Interpreter_FOUND) |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 130 | set(TF_PSA_CRYPTO_PYTHON_EXECUTABLE ${Python3_EXECUTABLE}) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 131 | endif() |
| 132 | else() |
| 133 | find_package(PythonInterp 3) |
| 134 | if(PYTHONINTERP_FOUND) |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 135 | set(TF_PSA_CRYPTO_PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE}) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 136 | endif() |
| 137 | endif() |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 138 | if(TF_PSA_CRYPTO_PYTHON_EXECUTABLE) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 139 | |
| 140 | # If 128-bit keys are configured for CTR_DRBG, display an appropriate warning |
| Ronald Cron | e9e7b76 | 2024-07-20 15:28:39 +0200 | [diff] [blame^] | 141 | execute_process(COMMAND ${TF_PSA_CRYPTO_PYTHON_EXECUTABLE} ${MBEDTLS_DIR}/scripts/config.py -f ${MBEDTLS_DIR}/include/mbedtls/mbedtls_config.h get MBEDTLS_CTR_DRBG_USE_128_BIT_KEY |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 142 | RESULT_VARIABLE result) |
| 143 | if(${result} EQUAL 0) |
| 144 | message(WARNING ${CTR_DRBG_128_BIT_KEY_WARNING}) |
| 145 | endif() |
| 146 | |
| 147 | endif() |
| 148 | |
| 149 | # We now potentially need to link all executables against PThreads, if available |
| 150 | set(CMAKE_THREAD_PREFER_PTHREAD TRUE) |
| 151 | set(THREADS_PREFER_PTHREAD_FLAG TRUE) |
| 152 | find_package(Threads) |
| 153 | |
| 154 | # If this is the root project add longer list of available CMAKE_BUILD_TYPE values |
| 155 | if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) |
| 156 | set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} |
| 157 | CACHE STRING "Choose the type of build: None Debug Release Coverage ASan ASanDbg MemSan MemSanDbg Check CheckFull TSan TSanDbg" |
| 158 | FORCE) |
| 159 | endif() |
| 160 | |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 161 | # Make TF_PSA_CRYPTO_CONFIG_FILE and TF_PSA_CRYPTO_USER_CONFIG_FILE into PATHs |
| 162 | set(TF_PSA_CRYPTO_CONFIG_FILE "" CACHE FILEPATH "TF-PSA-Crypto config file (overrides default).") |
| 163 | set(TF_PSA_CRYPTO_USER_CONFIG_FILE "" CACHE FILEPATH "TF-PSA-Crypto user config file (appended to default).") |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 164 | |
| 165 | # Create a symbolic link from ${base_name} in the binary directory |
| 166 | # to the corresponding path in the source directory. |
| 167 | # Note: Copies the file(s) on Windows. |
| 168 | function(link_to_source base_name) |
| 169 | set(link "${CMAKE_CURRENT_BINARY_DIR}/${base_name}") |
| 170 | set(target "${CMAKE_CURRENT_SOURCE_DIR}/${base_name}") |
| 171 | |
| 172 | # Linking to non-existent file is not desirable. At best you will have a |
| 173 | # dangling link, but when building in tree, this can create a symbolic link |
| 174 | # to itself. |
| 175 | if (EXISTS ${target} AND NOT EXISTS ${link}) |
| 176 | if (CMAKE_HOST_UNIX) |
| 177 | execute_process(COMMAND ln -s ${target} ${link} |
| 178 | RESULT_VARIABLE result |
| 179 | ERROR_VARIABLE output) |
| 180 | |
| 181 | if (NOT ${result} EQUAL 0) |
| 182 | message(FATAL_ERROR "Could not create symbolic link for: ${target} --> ${output}") |
| 183 | endif() |
| 184 | else() |
| 185 | if (IS_DIRECTORY ${target}) |
| 186 | file(GLOB_RECURSE files FOLLOW_SYMLINKS LIST_DIRECTORIES false RELATIVE ${target} "${target}/*") |
| 187 | foreach(file IN LISTS files) |
| 188 | configure_file("${target}/${file}" "${link}/${file}" COPYONLY) |
| 189 | endforeach(file) |
| 190 | else() |
| 191 | configure_file(${target} ${link} COPYONLY) |
| 192 | endif() |
| 193 | endif() |
| 194 | endif() |
| 195 | endfunction(link_to_source) |
| 196 | |
| 197 | # Get the filename without the final extension (i.e. convert "a.b.c" to "a.b") |
| 198 | function(get_name_without_last_ext dest_var full_name) |
| 199 | # Split into a list on '.' (but a cmake list is just a ';'-separated string) |
| 200 | string(REPLACE "." ";" ext_parts "${full_name}") |
| 201 | # Remove the last item if there are more than one |
| 202 | list(LENGTH ext_parts ext_parts_len) |
| 203 | if (${ext_parts_len} GREATER "1") |
| 204 | math(EXPR ext_parts_last_item "${ext_parts_len} - 1") |
| 205 | list(REMOVE_AT ext_parts ${ext_parts_last_item}) |
| 206 | endif() |
| 207 | # Convert back to a string by replacing separators with '.' |
| 208 | string(REPLACE ";" "." no_ext_name "${ext_parts}") |
| 209 | # Copy into the desired variable |
| 210 | set(${dest_var} ${no_ext_name} PARENT_SCOPE) |
| 211 | endfunction(get_name_without_last_ext) |
| 212 | |
| 213 | include(CheckCCompilerFlag) |
| 214 | |
| 215 | set(CMAKE_C_EXTENSIONS OFF) |
| 216 | set(CMAKE_C_STANDARD 99) |
| 217 | |
| 218 | if(CMAKE_COMPILER_IS_GNU) |
| 219 | # some warnings we want are not available with old GCC versions |
| 220 | # note: starting with CMake 2.8 we could use CMAKE_C_COMPILER_VERSION |
| 221 | execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion |
| 222 | OUTPUT_VARIABLE GCC_VERSION) |
| 223 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wwrite-strings -Wmissing-prototypes") |
| 224 | if (GCC_VERSION VERSION_GREATER 3.0 OR GCC_VERSION VERSION_EQUAL 3.0) |
| 225 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat=2 -Wno-format-nonliteral") |
| 226 | endif() |
| 227 | if (GCC_VERSION VERSION_GREATER 4.3 OR GCC_VERSION VERSION_EQUAL 4.3) |
| 228 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wvla") |
| 229 | endif() |
| 230 | if (GCC_VERSION VERSION_GREATER 4.5 OR GCC_VERSION VERSION_EQUAL 4.5) |
| 231 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wlogical-op") |
| 232 | endif() |
| 233 | if (GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8) |
| 234 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow") |
| 235 | endif() |
| 236 | if (GCC_VERSION VERSION_GREATER 5.0) |
| 237 | CHECK_C_COMPILER_FLAG("-Wformat-signedness" C_COMPILER_SUPPORTS_WFORMAT_SIGNEDNESS) |
| 238 | if(C_COMPILER_SUPPORTS_WFORMAT_SIGNEDNESS) |
| 239 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat-signedness") |
| 240 | endif() |
| 241 | endif() |
| 242 | if (GCC_VERSION VERSION_GREATER 7.0 OR GCC_VERSION VERSION_EQUAL 7.0) |
| 243 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat-overflow=2 -Wformat-truncation") |
| 244 | endif() |
| 245 | set(CMAKE_C_FLAGS_RELEASE "-O2") |
| 246 | set(CMAKE_C_FLAGS_DEBUG "-O0 -g3") |
| 247 | set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage") |
| 248 | set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3") |
| 249 | set(CMAKE_C_FLAGS_ASANDBG "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls") |
| 250 | set(CMAKE_C_FLAGS_TSAN "-fsanitize=thread -O3") |
| 251 | set(CMAKE_C_FLAGS_TSANDBG "-fsanitize=thread -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls") |
| 252 | set(CMAKE_C_FLAGS_CHECK "-Os") |
| 253 | set(CMAKE_C_FLAGS_CHECKFULL "${CMAKE_C_FLAGS_CHECK} -Wcast-qual") |
| 254 | endif(CMAKE_COMPILER_IS_GNU) |
| 255 | |
| 256 | if(CMAKE_COMPILER_IS_CLANG) |
| 257 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wwrite-strings -Wmissing-prototypes -Wpointer-arith -Wimplicit-fallthrough -Wshadow -Wvla -Wformat=2 -Wno-format-nonliteral") |
| 258 | set(CMAKE_C_FLAGS_RELEASE "-O2") |
| 259 | set(CMAKE_C_FLAGS_DEBUG "-O0 -g3") |
| 260 | set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage") |
| 261 | set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3") |
| 262 | set(CMAKE_C_FLAGS_ASANDBG "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls") |
| 263 | set(CMAKE_C_FLAGS_MEMSAN "-fsanitize=memory -O3") |
| 264 | set(CMAKE_C_FLAGS_MEMSANDBG "-fsanitize=memory -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2") |
| 265 | set(CMAKE_C_FLAGS_TSAN "-fsanitize=thread -O3") |
| 266 | set(CMAKE_C_FLAGS_TSANDBG "-fsanitize=thread -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls") |
| 267 | set(CMAKE_C_FLAGS_CHECK "-Os") |
| 268 | endif(CMAKE_COMPILER_IS_CLANG) |
| 269 | |
| 270 | if(CMAKE_COMPILER_IS_IAR) |
| 271 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --warn_about_c_style_casts") |
| 272 | set(CMAKE_C_FLAGS_RELEASE "-Ohz") |
| 273 | set(CMAKE_C_FLAGS_DEBUG "--debug -On") |
| 274 | endif(CMAKE_COMPILER_IS_IAR) |
| 275 | |
| 276 | if(CMAKE_COMPILER_IS_MSVC) |
| 277 | # Strictest warnings, UTF-8 source and execution charset |
| 278 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3 /utf-8") |
| 279 | endif(CMAKE_COMPILER_IS_MSVC) |
| 280 | |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 281 | if(TF_PSA_CRYPTO_FATAL_WARNINGS) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 282 | if(CMAKE_COMPILER_IS_MSVC) |
| 283 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX") |
| 284 | endif(CMAKE_COMPILER_IS_MSVC) |
| 285 | |
| 286 | if(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU) |
| 287 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror") |
| 288 | if(UNSAFE_BUILD) |
| 289 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=cpp") |
| 290 | set(CMAKE_C_FLAGS_ASAN "${CMAKE_C_FLAGS_ASAN} -Wno-error=cpp") |
| 291 | set(CMAKE_C_FLAGS_ASANDBG "${CMAKE_C_FLAGS_ASANDBG} -Wno-error=cpp") |
| 292 | endif(UNSAFE_BUILD) |
| 293 | endif(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU) |
| 294 | |
| 295 | if (CMAKE_COMPILER_IS_IAR) |
| 296 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --warnings_are_errors") |
| 297 | endif(CMAKE_COMPILER_IS_IAR) |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 298 | endif(TF_PSA_CRYPTO_FATAL_WARNINGS) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 299 | |
| 300 | if(CMAKE_BUILD_TYPE STREQUAL "Check" AND TEST_CPP) |
| 301 | set(CMAKE_CXX_STANDARD 11) |
| 302 | set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 303 | set(CMAKE_CXX_EXTENSIONS OFF) |
| 304 | if(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU) |
| 305 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic") |
| 306 | endif() |
| 307 | endif() |
| 308 | |
| 309 | if(CMAKE_BUILD_TYPE STREQUAL "Coverage") |
| 310 | if(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG) |
| 311 | set(CMAKE_SHARED_LINKER_FLAGS "--coverage") |
| 312 | endif(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG) |
| 313 | endif(CMAKE_BUILD_TYPE STREQUAL "Coverage") |
| 314 | |
| 315 | if(LIB_INSTALL_DIR) |
| 316 | set(CMAKE_INSTALL_LIBDIR "${LIB_INSTALL_DIR}") |
| 317 | endif() |
| 318 | |
| 319 | if (NOT EXISTS "${MBEDTLS_FRAMEWORK_DIR}/CMakeLists.txt") |
| 320 | message(FATAL_ERROR "${MBEDTLS_FRAMEWORK_DIR}/CMakeLists.txt not found. Run `git submodule update --init` from the source tree to fetch the submodule contents.") |
| 321 | endif() |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 322 | |
| 323 | add_subdirectory(include) |
| Ronald Cron | 31829a8 | 2024-07-29 19:06:40 +0200 | [diff] [blame] | 324 | add_subdirectory(core) |
| 325 | add_subdirectory(drivers) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 326 | |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 327 | # |
| 328 | # The C files in tests/src directory contain test code shared among test suites |
| 329 | # and programs. This shared test code is compiled and linked to test suites and |
| 330 | # programs objects as a set of compiled objects. The compiled objects are NOT |
| 331 | # built into a library that the test suite and program objects would link |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 332 | # against as they link against the tfpsacrypto library. The reason is that such |
| Ronald Cron | 97d05e5 | 2024-07-20 15:02:50 +0200 | [diff] [blame] | 333 | # library is expected to have mutual dependencies with the aforementioned |
| 334 | # library and that there is as of today no portable way of handling such |
| 335 | # dependencies (only toolchain specific solutions). |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 336 | # |
| 337 | # Thus the below definition of the `mbedtls_test` CMake library of objects |
| 338 | # target. This library of objects is used by tests and programs CMake files |
| 339 | # to define the test executables. |
| 340 | # |
| 341 | if(ENABLE_TESTING OR ENABLE_PROGRAMS) |
| 342 | file(GLOB MBEDTLS_TEST_FILES |
| Ronald Cron | e9e7b76 | 2024-07-20 15:28:39 +0200 | [diff] [blame^] | 343 | ${MBEDTLS_DIR}/tests/src/*.c |
| 344 | ${MBEDTLS_DIR}/tests/src/drivers/*.c) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 345 | add_library(mbedtls_test OBJECT ${MBEDTLS_TEST_FILES}) |
| 346 | if(GEN_FILES) |
| 347 | add_custom_command( |
| 348 | OUTPUT |
| Ronald Cron | e9e7b76 | 2024-07-20 15:28:39 +0200 | [diff] [blame^] | 349 | ${MBEDTLS_DIR}/tests/src/test_keys.h |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 350 | WORKING_DIRECTORY |
| Ronald Cron | e9e7b76 | 2024-07-20 15:28:39 +0200 | [diff] [blame^] | 351 | ${MBEDTLS_DIR}/tests |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 352 | COMMAND |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 353 | "${TF_PSA_CRYPTO_PYTHON_EXECUTABLE}" |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 354 | "${MBEDTLS_FRAMEWORK_DIR}/scripts/generate_test_keys.py" |
| 355 | "--output" |
| Ronald Cron | e9e7b76 | 2024-07-20 15:28:39 +0200 | [diff] [blame^] | 356 | "${MBEDTLS_DIR}/tests/src/test_keys.h" |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 357 | DEPENDS |
| 358 | ${MBEDTLS_FRAMEWORK_DIR}/scripts/generate_test_keys.py |
| 359 | ) |
| Ronald Cron | e9e7b76 | 2024-07-20 15:28:39 +0200 | [diff] [blame^] | 360 | add_custom_target(test_keys_header DEPENDS ${MBEDTLS_DIR}/tests/src/test_keys.h) |
| Ronald Cron | 97d05e5 | 2024-07-20 15:02:50 +0200 | [diff] [blame] | 361 | |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 362 | add_custom_command( |
| 363 | OUTPUT |
| Ronald Cron | e9e7b76 | 2024-07-20 15:28:39 +0200 | [diff] [blame^] | 364 | ${MBEDTLS_DIR}/tests/src/test_certs.h |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 365 | WORKING_DIRECTORY |
| Ronald Cron | e9e7b76 | 2024-07-20 15:28:39 +0200 | [diff] [blame^] | 366 | ${MBEDTLS_DIR}/tests |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 367 | COMMAND |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 368 | "${TF_PSA_CRYPTO_PYTHON_EXECUTABLE}" |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 369 | "${MBEDTLS_FRAMEWORK_DIR}/scripts/generate_test_cert_macros.py" |
| 370 | "--output" |
| Ronald Cron | e9e7b76 | 2024-07-20 15:28:39 +0200 | [diff] [blame^] | 371 | "${MBEDTLS_DIR}/tests/src/test_certs.h" |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 372 | DEPENDS |
| 373 | ${MBEDTLS_FRAMEWORK_DIR}/scripts/generate_test_cert_macros.py |
| 374 | ) |
| Ronald Cron | e9e7b76 | 2024-07-20 15:28:39 +0200 | [diff] [blame^] | 375 | add_custom_target(test_certs_header DEPENDS ${MBEDTLS_DIR}/tests/src/test_certs.h) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 376 | add_dependencies(mbedtls_test test_keys_header test_certs_header) |
| 377 | endif() |
| 378 | target_include_directories(mbedtls_test |
| Ronald Cron | e9e7b76 | 2024-07-20 15:28:39 +0200 | [diff] [blame^] | 379 | PRIVATE ${MBEDTLS_DIR}/tests/include |
| 380 | PRIVATE ${MBEDTLS_DIR}/include |
| 381 | PRIVATE include |
| 382 | PRIVATE drivers/builtin/include |
| 383 | PRIVATE core |
| 384 | PRIVATE drivers/builtin/src) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 385 | # Request C11, needed for memory poisoning tests |
| 386 | set_target_properties(mbedtls_test PROPERTIES C_STANDARD 11) |
| 387 | |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 388 | # Pass-through TF_PSA_CRYPTO_CONFIG_FILE and TF_PSA_CRYPTO_USER_CONFIG_FILE |
| 389 | if(TF_PSA_CRYPTO_CONFIG_FILE) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 390 | target_compile_definitions(mbedtls_test |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 391 | PUBLIC TF_PSA_CRYPTO_CONFIG_FILE="${TF_PSA_CRYPTO_CONFIG_FILE}") |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 392 | endif() |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 393 | if(TF_PSA_CRYPTO_USER_CONFIG_FILE) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 394 | target_compile_definitions(mbedtls_test |
| Ronald Cron | 9c84726 | 2024-07-20 14:56:49 +0200 | [diff] [blame] | 395 | PUBLIC TF_PSA_CRYPTO_USER_CONFIG_FILE="${TF_PSA_CRYPTO_USER_CONFIG_FILE}") |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 396 | endif() |
| 397 | endif() |
| 398 | |
| 399 | if(ENABLE_PROGRAMS) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 400 | add_subdirectory(programs) |
| 401 | endif() |
| 402 | |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 403 | if(ENABLE_TESTING) |
| 404 | enable_testing() |
| 405 | |
| 406 | add_subdirectory(tests) |
| 407 | |
| 408 | # additional convenience targets for Unix only |
| 409 | if(UNIX) |
| Ronald Cron | 701faac | 2024-07-20 14:43:53 +0200 | [diff] [blame] | 410 | ADD_CUSTOM_TARGET(memcheck |
| 411 | COMMAND sed -i.bak s+/usr/bin/valgrind+`which valgrind`+ DartConfiguration.tcl |
| 412 | COMMAND ctest -O memcheck.log -D ExperimentalMemCheck |
| 413 | COMMAND tail -n1 memcheck.log | grep 'Memory checking results:' > /dev/null |
| 414 | COMMAND rm -f memcheck.log |
| 415 | COMMAND mv DartConfiguration.tcl.bak DartConfiguration.tcl |
| 416 | ) |
| 417 | endif(UNIX) |
| 418 | |
| 419 | # Make scripts needed for testing available in an out-of-source build. |
| 420 | if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) |
| 421 | link_to_source(scripts) |
| 422 | # Copy (don't link) DartConfiguration.tcl, needed for memcheck, to |
| 423 | # keep things simple with the sed commands in the memcheck target. |
| 424 | configure_file(${CMAKE_CURRENT_SOURCE_DIR}/DartConfiguration.tcl |
| 425 | ${CMAKE_CURRENT_BINARY_DIR}/DartConfiguration.tcl COPYONLY) |
| 426 | endif() |
| 427 | endif() |