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