blob: c73a27758fd5a88b90b5d1154dad12bde3da9f58 [file] [log] [blame]
Ronald Crond3b33a22024-10-19 14:27:48 +02001#
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.
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 tfpsacrypto-apidoc targets.
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.
21cmake_minimum_required(VERSION 3.5.1)
22
Ronald Cronf85882d2024-10-11 19:56:38 +020023include(CMakePackageConfigHelpers)
Ronald Crond3b33a22024-10-19 14:27:48 +020024
25# Include convenience functions for printing properties and variables, like
26# cmake_print_properties(), cmake_print_variables().
27include(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.
32cmake_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.
38cmake_policy(SET CMP0012 NEW)
39
40set(TF_PSA_CRYPTO_VERSION 0.1.0)
41set(TF_PSA_CRYPTO_SOVERSION 0)
42
43if(TEST_CPP)
44 project("TF-PSA-Crypto"
45 LANGUAGES C CXX
46 VERSION ${TF_PSA_CRYPTO_VERSION}
47 )
48else()
49 project("TF-PSA-Crypto"
50 LANGUAGES C
51 VERSION ${TF_PSA_CRYPTO_VERSION}
52 )
53endif()
54
Ronald Cron701faac2024-07-20 14:43:53 +020055include(GNUInstallDirs)
56
Ronald Cron9c847262024-07-20 14:56:49 +020057# Determine if TF-PSA-Crypto is being built as a subproject using add_subdirectory()
58if(NOT DEFINED TF_PSA_CRYPTO_AS_SUBPROJECT)
59 set(TF_PSA_CRYPTO_AS_SUBPROJECT ON)
Ronald Cron701faac2024-07-20 14:43:53 +020060 if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
Ronald Cron9c847262024-07-20 14:56:49 +020061 set(TF_PSA_CRYPTO_AS_SUBPROJECT OFF)
Ronald Cron701faac2024-07-20 14:43:53 +020062 endif()
63endif()
64
Ronald Cron9c847262024-07-20 14:56:49 +020065# Set the project, Mbed TLS and framework root directory.
66set(TF_PSA_CRYPTO_DIR ${CMAKE_CURRENT_SOURCE_DIR})
67set(MBEDTLS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/..)
68set(MBEDTLS_FRAMEWORK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../framework)
Ronald Cron701faac2024-07-20 14:43:53 +020069
Ronald Cronb58c2252024-10-04 17:19:50 +020070# Put the version numbers into relevant files
71set(version_number_files
72 doxygen/input/doc_mainpage.h
73 doxygen/tfpsacrypto.doxyfile)
74foreach(file ${version_number_files})
75 configure_file(${file}.in
76 ${TF_PSA_CRYPTO_DIR}/${file})
77endforeach(file)
78
Ronald Crona9219ff2024-10-23 09:23:46 +020079ADD_CUSTOM_TARGET(${TF_PSA_CRYPTO_TARGET_PREFIX}tfpsacrypto-apidoc
Ronald Cronb58c2252024-10-04 17:19:50 +020080 COMMAND doxygen tfpsacrypto.doxyfile
81 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doxygen)
82
Ronald Cron9c847262024-07-20 14:56:49 +020083option(ENABLE_PROGRAMS "Build TF-PSA-Crypto programs." ON)
Ronald Cron701faac2024-07-20 14:43:53 +020084
Ronald Cron9c847262024-07-20 14:56:49 +020085option(TF_PSA_CRYPTO_FATAL_WARNINGS "Compiler warnings treated as errors" ON)
Ronald Cron701faac2024-07-20 14:43:53 +020086if(CMAKE_HOST_WIN32)
87 # N.B. The comment on the next line is significant! If you change it,
88 # edit the sed command in prepare_release.sh that modifies
89 # CMakeLists.txt.
90 option(GEN_FILES "Generate the auto-generated files as needed" OFF) # off in development
91else()
92 option(GEN_FILES "Generate the auto-generated files as needed" ON)
93endif()
94
Ronald Cron4c3fa0a2024-07-30 10:50:39 +020095# Support for package config and install to be added later.
Ronald Cronf85882d2024-10-11 19:56:38 +020096option(DISABLE_PACKAGE_CONFIG_AND_INSTALL "Disable package configuration, target export and installation" ${TF_PSA_CRYPTO_AS_SUBPROJECT})
Ronald Cron701faac2024-07-20 14:43:53 +020097
98if (CMAKE_C_SIMULATE_ID)
99 set(COMPILER_ID ${CMAKE_C_SIMULATE_ID})
100else()
101 set(COMPILER_ID ${CMAKE_C_COMPILER_ID})
102endif(CMAKE_C_SIMULATE_ID)
103
104string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${COMPILER_ID}")
105string(REGEX MATCH "GNU" CMAKE_COMPILER_IS_GNU "${COMPILER_ID}")
106string(REGEX MATCH "IAR" CMAKE_COMPILER_IS_IAR "${COMPILER_ID}")
107string(REGEX MATCH "MSVC" CMAKE_COMPILER_IS_MSVC "${COMPILER_ID}")
108
109# the test suites currently have compile errors with MSVC
110if(CMAKE_COMPILER_IS_MSVC)
Ronald Cron9c847262024-07-20 14:56:49 +0200111 option(ENABLE_TESTING "Build TF-PSA-Crypto tests." OFF)
Ronald Cron701faac2024-07-20 14:43:53 +0200112else()
Ronald Cron9c847262024-07-20 14:56:49 +0200113 option(ENABLE_TESTING "Build TF-PSA-Crypto tests." ON)
Ronald Cron701faac2024-07-20 14:43:53 +0200114endif()
115
Ronald Cron9c847262024-07-20 14:56:49 +0200116option(USE_STATIC_TF_PSA_CRYPTO_LIBRARY "Build TF-PSA-Crypto static library." ON)
117option(USE_SHARED_TF_PSA_CRYPTO_LIBRARY "Build TF-PSA-Crypto shared library." OFF)
Ronald Cron701faac2024-07-20 14:43:53 +0200118option(LINK_WITH_PTHREAD "Explicitly link Mbed TLS library to pthread." OFF)
119option(LINK_WITH_TRUSTED_STORAGE "Explicitly link Mbed TLS library to trusted_storage." OFF)
120
Ronald Cron9c847262024-07-20 14:56:49 +0200121set(mbedcrypto_target "${TF_PSA_CRYPTO_TARGET_PREFIX}mbedcrypto")
122if (USE_STATIC_TF_PSA_CRYPTO_LIBRARY)
Ronald Cron701faac2024-07-20 14:43:53 +0200123 set(mbedcrypto_static_target ${mbedcrypto_target})
124endif()
Ronald Cron9c847262024-07-20 14:56:49 +0200125if(USE_STATIC_TF_PSA_CRYPTO_LIBRARY AND USE_SHARED_TF_PSA_CRYPTO_LIBRARY)
Ronald Cron701faac2024-07-20 14:43:53 +0200126 string(APPEND mbedcrypto_static_target "_static")
127endif()
128
129# Warning string - created as a list for compatibility with CMake 2.8
130set(CTR_DRBG_128_BIT_KEY_WARN_L1 "**** WARNING! MBEDTLS_CTR_DRBG_USE_128_BIT_KEY defined!\n")
131set(CTR_DRBG_128_BIT_KEY_WARN_L2 "**** Using 128-bit keys for CTR_DRBG limits the security of generated\n")
132set(CTR_DRBG_128_BIT_KEY_WARN_L3 "**** keys and operations that use random values generated to 128-bit security\n")
133
134set(CTR_DRBG_128_BIT_KEY_WARNING "${WARNING_BORDER}"
135 "${CTR_DRBG_128_BIT_KEY_WARN_L1}"
136 "${CTR_DRBG_128_BIT_KEY_WARN_L2}"
137 "${CTR_DRBG_128_BIT_KEY_WARN_L3}"
138 "${WARNING_BORDER}")
139
140# Python 3 is only needed here to check for configuration warnings.
141if(NOT CMAKE_VERSION VERSION_LESS 3.15.0)
142 set(Python3_FIND_STRATEGY LOCATION)
143 find_package(Python3 COMPONENTS Interpreter)
144 if(Python3_Interpreter_FOUND)
Ronald Cron9c847262024-07-20 14:56:49 +0200145 set(TF_PSA_CRYPTO_PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
Ronald Cron701faac2024-07-20 14:43:53 +0200146 endif()
147else()
148 find_package(PythonInterp 3)
149 if(PYTHONINTERP_FOUND)
Ronald Cron9c847262024-07-20 14:56:49 +0200150 set(TF_PSA_CRYPTO_PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
Ronald Cron701faac2024-07-20 14:43:53 +0200151 endif()
152endif()
Ronald Cron9c847262024-07-20 14:56:49 +0200153if(TF_PSA_CRYPTO_PYTHON_EXECUTABLE)
Ronald Cron701faac2024-07-20 14:43:53 +0200154
155 # If 128-bit keys are configured for CTR_DRBG, display an appropriate warning
Ronald Crone9e7b762024-07-20 15:28:39 +0200156 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 Cron701faac2024-07-20 14:43:53 +0200157 RESULT_VARIABLE result)
158 if(${result} EQUAL 0)
159 message(WARNING ${CTR_DRBG_128_BIT_KEY_WARNING})
160 endif()
161
162endif()
163
164# We now potentially need to link all executables against PThreads, if available
165set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
166set(THREADS_PREFER_PTHREAD_FLAG TRUE)
167find_package(Threads)
168
169# If this is the root project add longer list of available CMAKE_BUILD_TYPE values
Ronald Cronf584e972024-10-07 11:38:17 +0200170if(NOT TF_PSA_CRYPTO_AS_SUBPROJECT)
Ronald Cron701faac2024-07-20 14:43:53 +0200171 set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}
172 CACHE STRING "Choose the type of build: None Debug Release Coverage ASan ASanDbg MemSan MemSanDbg Check CheckFull TSan TSanDbg"
173 FORCE)
174endif()
175
Ronald Cron9c847262024-07-20 14:56:49 +0200176# Make TF_PSA_CRYPTO_CONFIG_FILE and TF_PSA_CRYPTO_USER_CONFIG_FILE into PATHs
177set(TF_PSA_CRYPTO_CONFIG_FILE "" CACHE FILEPATH "TF-PSA-Crypto config file (overrides default).")
178set(TF_PSA_CRYPTO_USER_CONFIG_FILE "" CACHE FILEPATH "TF-PSA-Crypto user config file (appended to default).")
Ronald Cron701faac2024-07-20 14:43:53 +0200179
180# Create a symbolic link from ${base_name} in the binary directory
181# to the corresponding path in the source directory.
182# Note: Copies the file(s) on Windows.
183function(link_to_source base_name)
184 set(link "${CMAKE_CURRENT_BINARY_DIR}/${base_name}")
185 set(target "${CMAKE_CURRENT_SOURCE_DIR}/${base_name}")
186
187 # Linking to non-existent file is not desirable. At best you will have a
188 # dangling link, but when building in tree, this can create a symbolic link
189 # to itself.
190 if (EXISTS ${target} AND NOT EXISTS ${link})
191 if (CMAKE_HOST_UNIX)
192 execute_process(COMMAND ln -s ${target} ${link}
193 RESULT_VARIABLE result
194 ERROR_VARIABLE output)
195
196 if (NOT ${result} EQUAL 0)
197 message(FATAL_ERROR "Could not create symbolic link for: ${target} --> ${output}")
198 endif()
199 else()
200 if (IS_DIRECTORY ${target})
201 file(GLOB_RECURSE files FOLLOW_SYMLINKS LIST_DIRECTORIES false RELATIVE ${target} "${target}/*")
202 foreach(file IN LISTS files)
203 configure_file("${target}/${file}" "${link}/${file}" COPYONLY)
204 endforeach(file)
205 else()
206 configure_file(${target} ${link} COPYONLY)
207 endif()
208 endif()
209 endif()
210endfunction(link_to_source)
211
212# Get the filename without the final extension (i.e. convert "a.b.c" to "a.b")
213function(get_name_without_last_ext dest_var full_name)
214 # Split into a list on '.' (but a cmake list is just a ';'-separated string)
215 string(REPLACE "." ";" ext_parts "${full_name}")
216 # Remove the last item if there are more than one
217 list(LENGTH ext_parts ext_parts_len)
218 if (${ext_parts_len} GREATER "1")
219 math(EXPR ext_parts_last_item "${ext_parts_len} - 1")
220 list(REMOVE_AT ext_parts ${ext_parts_last_item})
221 endif()
222 # Convert back to a string by replacing separators with '.'
223 string(REPLACE ";" "." no_ext_name "${ext_parts}")
224 # Copy into the desired variable
225 set(${dest_var} ${no_ext_name} PARENT_SCOPE)
226endfunction(get_name_without_last_ext)
227
228include(CheckCCompilerFlag)
229
230set(CMAKE_C_EXTENSIONS OFF)
231set(CMAKE_C_STANDARD 99)
232
Ronald Cronb2478982024-10-07 16:17:07 +0200233function(set_base_compile_options target)
234 if(CMAKE_COMPILER_IS_GNU)
235 set_gnu_base_compile_options(${target})
Ronald Crond9e11092024-10-09 10:01:46 +0200236 elseif(CMAKE_COMPILER_IS_CLANG)
237 set_clang_base_compile_options(${target})
Ronald Cron6f9d5082024-10-09 14:54:43 +0200238 elseif(CMAKE_COMPILER_IS_IAR)
239 set_iar_base_compile_options(${target})
240 elseif(CMAKE_COMPILER_IS_MSVC)
241 set_msvc_base_compile_options(${target})
Ronald Cronb2478982024-10-07 16:17:07 +0200242 endif()
243endfunction(set_base_compile_options)
244
245function(set_gnu_base_compile_options target)
Ronald Cron701faac2024-07-20 14:43:53 +0200246 # some warnings we want are not available with old GCC versions
247 # note: starting with CMake 2.8 we could use CMAKE_C_COMPILER_VERSION
248 execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
249 OUTPUT_VARIABLE GCC_VERSION)
Ronald Cronb2478982024-10-07 16:17:07 +0200250 target_compile_options(${target} PRIVATE -Wall -Wextra -Wwrite-strings -Wmissing-prototypes)
Ronald Cron701faac2024-07-20 14:43:53 +0200251 if (GCC_VERSION VERSION_GREATER 3.0 OR GCC_VERSION VERSION_EQUAL 3.0)
Ronald Cronb2478982024-10-07 16:17:07 +0200252 target_compile_options(${target} PRIVATE -Wformat=2 -Wno-format-nonliteral)
Ronald Cron701faac2024-07-20 14:43:53 +0200253 endif()
254 if (GCC_VERSION VERSION_GREATER 4.3 OR GCC_VERSION VERSION_EQUAL 4.3)
Ronald Cronb2478982024-10-07 16:17:07 +0200255 target_compile_options(${target} PRIVATE -Wvla)
Ronald Cron701faac2024-07-20 14:43:53 +0200256 endif()
257 if (GCC_VERSION VERSION_GREATER 4.5 OR GCC_VERSION VERSION_EQUAL 4.5)
Ronald Cronb2478982024-10-07 16:17:07 +0200258 target_compile_options(${target} PRIVATE -Wlogical-op)
Ronald Cron701faac2024-07-20 14:43:53 +0200259 endif()
260 if (GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8)
Ronald Cronb2478982024-10-07 16:17:07 +0200261 target_compile_options(${target} PRIVATE -Wshadow)
Ronald Cron701faac2024-07-20 14:43:53 +0200262 endif()
263 if (GCC_VERSION VERSION_GREATER 5.0)
264 CHECK_C_COMPILER_FLAG("-Wformat-signedness" C_COMPILER_SUPPORTS_WFORMAT_SIGNEDNESS)
265 if(C_COMPILER_SUPPORTS_WFORMAT_SIGNEDNESS)
Ronald Cronb2478982024-10-07 16:17:07 +0200266 target_compile_options(${target} PRIVATE -Wformat-signedness)
Ronald Cron701faac2024-07-20 14:43:53 +0200267 endif()
268 endif()
269 if (GCC_VERSION VERSION_GREATER 7.0 OR GCC_VERSION VERSION_EQUAL 7.0)
Ronald Cronb2478982024-10-07 16:17:07 +0200270 target_compile_options(${target} PRIVATE -Wformat-overflow=2 -Wformat-truncation)
Ronald Cron701faac2024-07-20 14:43:53 +0200271 endif()
Ronald Cronb2478982024-10-07 16:17:07 +0200272 target_compile_options(${target} PRIVATE $<$<CONFIG:Release>:-O2>)
273 target_compile_options(${target} PRIVATE $<$<CONFIG:Debug>:-O0 -g3>)
274 target_compile_options(${target} PRIVATE $<$<CONFIG:Coverage>:-O0 -g3 --coverage>)
Ronald Cron4ae24f42024-10-08 17:53:13 +0200275 set_target_properties(${target} PROPERTIES LINK_FLAGS_COVERAGE "--coverage")
Ronald Cronb2478982024-10-07 16:17:07 +0200276 # Old GCC versions hit a performance problem with test_suite_pkwrite
277 # "Private keey write check EC" tests when building with Asan+UBSan
278 # and -O3: those tests take more than 100x time than normal, with
279 # test_suite_pkwrite taking >3h on the CI. Observed with GCC 5.4 on
280 # Ubuntu 16.04 x86_64 and GCC 6.5 on Ubuntu 18.04 x86_64.
281 # GCC 7.5 and above on Ubuntu 18.04 appear fine.
282 # To avoid the performance problem, we use -O2 when GCC version is lower than 7.0.
283 # It doesn't slow down much even with modern compiler versions.
284 target_compile_options(${target} PRIVATE $<$<CONFIG:ASan>:-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all>)
285 if (GCC_VERSION VERSION_LESS 7.0)
286 target_compile_options(${target} PRIVATE $<$<CONFIG:ASan>:-O2>)
287 else()
288 target_compile_options(${target} PRIVATE $<$<CONFIG:ASan>:-O3>)
289 endif()
Ronald Cron4ae24f42024-10-08 17:53:13 +0200290 set_target_properties(${target} PROPERTIES LINK_FLAGS_ASAN "-fsanitize=address -fsanitize=undefined")
Ronald Cronb2478982024-10-07 16:17:07 +0200291 target_compile_options(${target} PRIVATE $<$<CONFIG:ASanDbg>:-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls>)
Ronald Cron4ae24f42024-10-08 17:53:13 +0200292 set_target_properties(${target} PROPERTIES LINK_FLAGS_ASANDBG "-fsanitize=address -fsanitize=undefined")
Ronald Cronb2478982024-10-07 16:17:07 +0200293 target_compile_options(${target} PRIVATE $<$<CONFIG:TSan>:-fsanitize=thread -O3>)
Ronald Cron4ae24f42024-10-08 17:53:13 +0200294 set_target_properties(${target} PROPERTIES LINK_FLAGS_TSAN "-fsanitize=thread")
Ronald Cronb2478982024-10-07 16:17:07 +0200295 target_compile_options(${target} PRIVATE $<$<CONFIG:TSanDbg>:-fsanitize=thread -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls>)
Ronald Cron4ae24f42024-10-08 17:53:13 +0200296 set_target_properties(${target} PROPERTIES LINK_FLAGS_TSANDBG "-fsanitize=thread")
Ronald Cronb2478982024-10-07 16:17:07 +0200297 target_compile_options(${target} PRIVATE $<$<CONFIG:Check>:-Os>)
298 target_compile_options(${target} PRIVATE $<$<CONFIG:CheckFull>:-Os -Wcast-qual>)
Ronald Crond77fad22024-10-08 09:24:31 +0200299
300 if(TF_PSA_CRYPTO_FATAL_WARNINGS)
Ronald Cronb2478982024-10-07 16:17:07 +0200301 target_compile_options(${target} PRIVATE -Werror)
Ronald Crond77fad22024-10-08 09:24:31 +0200302 endif(TF_PSA_CRYPTO_FATAL_WARNINGS)
Ronald Cronb2478982024-10-07 16:17:07 +0200303endfunction(set_gnu_base_compile_options)
Ronald Cron701faac2024-07-20 14:43:53 +0200304
Ronald Crond9e11092024-10-09 10:01:46 +0200305function(set_clang_base_compile_options target)
306 target_compile_options(${target} PRIVATE -Wall -Wextra -Wwrite-strings -Wmissing-prototypes -Wpointer-arith -Wimplicit-fallthrough -Wshadow -Wvla -Wformat=2 -Wno-format-nonliteral)
307 target_compile_options(${target} PRIVATE $<$<CONFIG:Release>:-O2>)
308 target_compile_options(${target} PRIVATE $<$<CONFIG:Debug>:-O0 -g3>)
309 target_compile_options(${target} PRIVATE $<$<CONFIG:Coverage>:-O0 -g3 --coverage>)
310 set_target_properties(${target} PROPERTIES LINK_FLAGS_COVERAGE "--coverage")
311 target_compile_options(${target} PRIVATE $<$<CONFIG:ASan>:-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3>)
312 set_target_properties(${target} PROPERTIES LINK_FLAGS_ASAN "-fsanitize=address -fsanitize=undefined")
313 target_compile_options(${target} PRIVATE $<$<CONFIG:ASanDbg>:-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls>)
314 set_target_properties(${target} PROPERTIES LINK_FLAGS_ASANDBG "-fsanitize=address -fsanitize=undefined")
315 target_compile_options(${target} PRIVATE $<$<CONFIG:MemSan>:-fsanitize=memory>)
316 set_target_properties(${target} PROPERTIES LINK_FLAGS_MEMSAN "-fsanitize=memory")
317 target_compile_options(${target} PRIVATE $<$<CONFIG:MemSanDbg>:-fsanitize=memory -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2>)
318 set_target_properties(${target} PROPERTIES LINK_FLAGS_MEMSANDBG "-fsanitize=memory")
319 target_compile_options(${target} PRIVATE $<$<CONFIG:TSan>:-fsanitize=thread -O3>)
320 set_target_properties(${target} PROPERTIES LINK_FLAGS_TSAN "-fsanitize=thread")
321 target_compile_options(${target} PRIVATE $<$<CONFIG:TSanDbg>:-fsanitize=thread -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls>)
322 set_target_properties(${target} PROPERTIES LINK_FLAGS_TSANDBG "-fsanitize=thread")
323 target_compile_options(${target} PRIVATE $<$<CONFIG:Check>:-Os>)
Ronald Crond77fad22024-10-08 09:24:31 +0200324
Ronald Crond9e11092024-10-09 10:01:46 +0200325 if(MBEDTLS_FATAL_WARNINGS)
326 target_compile_options(${target} PRIVATE -Werror)
327 endif(MBEDTLS_FATAL_WARNINGS)
328endfunction(set_clang_base_compile_options)
Ronald Cron701faac2024-07-20 14:43:53 +0200329
Ronald Cron6f9d5082024-10-09 14:54:43 +0200330function(set_iar_base_compile_options target)
331 target_compile_options(${target} PRIVATE --warn_about_c_style_casts)
332 target_compile_options(${target} PRIVATE $<$<CONFIG:Release>:-Ohz>)
333 target_compile_options(${target} PRIVATE $<$<CONFIG:Debug>:--debug -On>)
Ronald Crond77fad22024-10-08 09:24:31 +0200334
Ronald Cron6f9d5082024-10-09 14:54:43 +0200335 if(MBEDTLS_FATAL_WARNINGS)
336 target_compile_options(${target} PRIVATE --warnings_are_errors)
337 endif(MBEDTLS_FATAL_WARNINGS)
338endfunction(set_iar_base_compile_options)
Ronald Cron701faac2024-07-20 14:43:53 +0200339
Ronald Cron6f9d5082024-10-09 14:54:43 +0200340function(set_msvc_base_compile_options target)
Ronald Cron701faac2024-07-20 14:43:53 +0200341 # Strictest warnings, UTF-8 source and execution charset
Ronald Cron6f9d5082024-10-09 14:54:43 +0200342 target_compile_options(${target} PRIVATE /W3 /utf-8)
Ronald Cron701faac2024-07-20 14:43:53 +0200343
Ronald Cron6f9d5082024-10-09 14:54:43 +0200344 if(MBEDTLS_FATAL_WARNINGS)
345 target_compile_options(${target} PRIVATE /WX)
346 endif(MBEDTLS_FATAL_WARNINGS)
347endfunction(set_msvc_base_compile_options)
Ronald Cron701faac2024-07-20 14:43:53 +0200348
Ronald Cron211bf6d2024-10-23 14:22:03 +0200349function(set_config_files_compile_definitions target)
350 # Pass-through MBEDTLS_CONFIG_FILE, MBEDTLS_USER_CONFIG_FILE,
351 # MBEDTLS_PSA_CRYPTO_CONFIG_FILE and MBEDTLS_PSA_CRYPTO_USER_CONFIG_FILE
352 if(MBEDTLS_CONFIG_FILE)
353 target_compile_definitions(${target}
354 PUBLIC MBEDTLS_CONFIG_FILE="${MBEDTLS_CONFIG_FILE}")
355 endif()
356 if(MBEDTLS_USER_CONFIG_FILE)
357 target_compile_definitions(${target}
358 PUBLIC MBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}")
359 endif()
360 if(MBEDTLS_PSA_CRYPTO_CONFIG_FILE)
361 target_compile_definitions(${target}
362 PUBLIC MBEDTLS_PSA_CRYPTO_CONFIG_FILE="${MBEDTLS_PSA_CRYPTO_CONFIG_FILE}")
363 endif()
364 if(MBEDTLS_PSA_CRYPTO_USER_CONFIG_FILE)
365 target_compile_definitions(${target}
366 PUBLIC MBEDTLS_PSA_CRYPTO_USER_CONFIG_FILE="${MBEDTLS_PSA_CRYPTO_USER_CONFIG_FILE}")
367 endif()
368endfunction(set_config_files_compile_definitions)
369
Ronald Cron701faac2024-07-20 14:43:53 +0200370if(CMAKE_BUILD_TYPE STREQUAL "Check" AND TEST_CPP)
371 set(CMAKE_CXX_STANDARD 11)
372 set(CMAKE_CXX_STANDARD_REQUIRED ON)
373 set(CMAKE_CXX_EXTENSIONS OFF)
374 if(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU)
375 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic")
376 endif()
377endif()
378
Ronald Cron701faac2024-07-20 14:43:53 +0200379if (NOT EXISTS "${MBEDTLS_FRAMEWORK_DIR}/CMakeLists.txt")
380 message(FATAL_ERROR "${MBEDTLS_FRAMEWORK_DIR}/CMakeLists.txt not found. Run `git submodule update --init` from the source tree to fetch the submodule contents.")
381endif()
Ronald Cron701faac2024-07-20 14:43:53 +0200382
383add_subdirectory(include)
Ronald Cron31829a82024-07-29 19:06:40 +0200384add_subdirectory(core)
385add_subdirectory(drivers)
Ronald Cronf85882d2024-10-11 19:56:38 +0200386add_subdirectory(pkgconfig)
Ronald Cron701faac2024-07-20 14:43:53 +0200387
Ronald Cron701faac2024-07-20 14:43:53 +0200388#
389# The C files in tests/src directory contain test code shared among test suites
390# and programs. This shared test code is compiled and linked to test suites and
391# programs objects as a set of compiled objects. The compiled objects are NOT
392# built into a library that the test suite and program objects would link
Ronald Cron9c847262024-07-20 14:56:49 +0200393# against as they link against the tfpsacrypto library. The reason is that such
Ronald Cron97d05e52024-07-20 15:02:50 +0200394# library is expected to have mutual dependencies with the aforementioned
395# library and that there is as of today no portable way of handling such
396# dependencies (only toolchain specific solutions).
Ronald Cron701faac2024-07-20 14:43:53 +0200397#
Ronald Cron2609fe92024-10-07 09:58:57 +0200398# Thus the below definition of the `tf_psa_crypto_test` CMake library of
399# objects target. This library of objects is used by tests and programs CMake
400# files to define the test executables.
Ronald Cron701faac2024-07-20 14:43:53 +0200401#
402if(ENABLE_TESTING OR ENABLE_PROGRAMS)
403 file(GLOB MBEDTLS_TEST_FILES
Ronald Crone9e7b762024-07-20 15:28:39 +0200404 ${MBEDTLS_DIR}/tests/src/*.c
405 ${MBEDTLS_DIR}/tests/src/drivers/*.c)
Ronald Cron2609fe92024-10-07 09:58:57 +0200406 add_library(tf_psa_crypto_test OBJECT ${MBEDTLS_TEST_FILES})
407 set_base_compile_options(tf_psa_crypto_test)
Ronald Cron701faac2024-07-20 14:43:53 +0200408 if(GEN_FILES)
409 add_custom_command(
410 OUTPUT
Ronald Crone9e7b762024-07-20 15:28:39 +0200411 ${MBEDTLS_DIR}/tests/src/test_keys.h
Ronald Cron701faac2024-07-20 14:43:53 +0200412 WORKING_DIRECTORY
Ronald Crone9e7b762024-07-20 15:28:39 +0200413 ${MBEDTLS_DIR}/tests
Ronald Cron701faac2024-07-20 14:43:53 +0200414 COMMAND
Ronald Cron9c847262024-07-20 14:56:49 +0200415 "${TF_PSA_CRYPTO_PYTHON_EXECUTABLE}"
Ronald Cron701faac2024-07-20 14:43:53 +0200416 "${MBEDTLS_FRAMEWORK_DIR}/scripts/generate_test_keys.py"
417 "--output"
Ronald Crone9e7b762024-07-20 15:28:39 +0200418 "${MBEDTLS_DIR}/tests/src/test_keys.h"
Ronald Cron701faac2024-07-20 14:43:53 +0200419 DEPENDS
420 ${MBEDTLS_FRAMEWORK_DIR}/scripts/generate_test_keys.py
421 )
Ronald Cron2609fe92024-10-07 09:58:57 +0200422 add_custom_target(tf_psa_crypto_test_keys_header DEPENDS ${MBEDTLS_DIR}/tests/src/test_keys.h)
Ronald Cron97d05e52024-07-20 15:02:50 +0200423
Ronald Cron701faac2024-07-20 14:43:53 +0200424 add_custom_command(
425 OUTPUT
Ronald Crone9e7b762024-07-20 15:28:39 +0200426 ${MBEDTLS_DIR}/tests/src/test_certs.h
Ronald Cron701faac2024-07-20 14:43:53 +0200427 WORKING_DIRECTORY
Ronald Crone9e7b762024-07-20 15:28:39 +0200428 ${MBEDTLS_DIR}/tests
Ronald Cron701faac2024-07-20 14:43:53 +0200429 COMMAND
Ronald Cron9c847262024-07-20 14:56:49 +0200430 "${TF_PSA_CRYPTO_PYTHON_EXECUTABLE}"
Ronald Cron701faac2024-07-20 14:43:53 +0200431 "${MBEDTLS_FRAMEWORK_DIR}/scripts/generate_test_cert_macros.py"
432 "--output"
Ronald Crone9e7b762024-07-20 15:28:39 +0200433 "${MBEDTLS_DIR}/tests/src/test_certs.h"
Ronald Cron701faac2024-07-20 14:43:53 +0200434 DEPENDS
435 ${MBEDTLS_FRAMEWORK_DIR}/scripts/generate_test_cert_macros.py
436 )
Ronald Cron2609fe92024-10-07 09:58:57 +0200437 add_custom_target(tf_psa_crypto_test_certs_header DEPENDS ${MBEDTLS_DIR}/tests/src/test_certs.h)
438 add_dependencies(tf_psa_crypto_test tf_psa_crypto_test_keys_header tf_psa_crypto_test_certs_header)
Ronald Cron701faac2024-07-20 14:43:53 +0200439 endif()
Ronald Cron2609fe92024-10-07 09:58:57 +0200440 target_include_directories(tf_psa_crypto_test
Ronald Crone9e7b762024-07-20 15:28:39 +0200441 PRIVATE ${MBEDTLS_DIR}/tests/include
442 PRIVATE ${MBEDTLS_DIR}/include
443 PRIVATE include
444 PRIVATE drivers/builtin/include
445 PRIVATE core
446 PRIVATE drivers/builtin/src)
Ronald Cron701faac2024-07-20 14:43:53 +0200447 # Request C11, needed for memory poisoning tests
Ronald Cron2609fe92024-10-07 09:58:57 +0200448 set_target_properties(tf_psa_crypto_test PROPERTIES C_STANDARD 11)
449 set_config_files_compile_definitions(tf_psa_crypto_test)
Ronald Cron701faac2024-07-20 14:43:53 +0200450endif()
451
452if(ENABLE_PROGRAMS)
Ronald Cron701faac2024-07-20 14:43:53 +0200453 add_subdirectory(programs)
454endif()
455
Ronald Cron701faac2024-07-20 14:43:53 +0200456if(ENABLE_TESTING)
457 enable_testing()
458
459 add_subdirectory(tests)
460
461 # additional convenience targets for Unix only
Ronald Cron169393e2024-10-07 12:34:42 +0200462 if(UNIX AND (NOT TF_PSA_CRYPTO_AS_SUBPROJECT))
Ronald Crond916cc92024-10-17 18:33:59 +0200463 # For coverage testing:
464 # 1. Build with:
465 # cmake -D CMAKE_BUILD_TYPE=Coverage /path/to/source && make
466 # 2. Run the relevant tests for the part of the code you're interested in.
467 # For the reference coverage measurement, see
468 # tests/scripts/basic-build-test.sh
469 # 3. Run scripts/lcov.sh to generate an HTML report.
470 ADD_CUSTOM_TARGET(lcov
471 COMMAND ${MBEDTLS_DIR}/scripts/lcov.sh
472 )
473
Ronald Cron701faac2024-07-20 14:43:53 +0200474 ADD_CUSTOM_TARGET(memcheck
475 COMMAND sed -i.bak s+/usr/bin/valgrind+`which valgrind`+ DartConfiguration.tcl
476 COMMAND ctest -O memcheck.log -D ExperimentalMemCheck
477 COMMAND tail -n1 memcheck.log | grep 'Memory checking results:' > /dev/null
478 COMMAND rm -f memcheck.log
479 COMMAND mv DartConfiguration.tcl.bak DartConfiguration.tcl
480 )
Ronald Cron169393e2024-10-07 12:34:42 +0200481 endif()
Ronald Cron701faac2024-07-20 14:43:53 +0200482
483 # Make scripts needed for testing available in an out-of-source build.
484 if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
485 link_to_source(scripts)
486 # Copy (don't link) DartConfiguration.tcl, needed for memcheck, to
487 # keep things simple with the sed commands in the memcheck target.
488 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/DartConfiguration.tcl
489 ${CMAKE_CURRENT_BINARY_DIR}/DartConfiguration.tcl COPYONLY)
490 endif()
491endif()
Ronald Cronf85882d2024-10-11 19:56:38 +0200492
493if(NOT DISABLE_PACKAGE_CONFIG_AND_INSTALL)
494 configure_package_config_file(
495 "cmake/TF-PSA-CryptoConfig.cmake.in"
496 "cmake/TF-PSA-CryptoConfig.cmake"
497 INSTALL_DESTINATION "cmake")
498
499 write_basic_package_version_file(
500 "cmake/TF-PSA-CryptoConfigVersion.cmake"
501 COMPATIBILITY SameMajorVersion
502 VERSION 0.1.0)
503
504 install(
505 FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake/TF-PSA-CryptoConfig.cmake"
506 "${CMAKE_CURRENT_BINARY_DIR}/cmake/TF-PSA-CryptoConfigVersion.cmake"
507 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/TF-PSA-Crypto")
508
509 export(
Ronald Cronba850f32024-10-11 19:39:49 +0200510 EXPORT TF-PSA-CryptoTargets
Ronald Cronf85882d2024-10-11 19:56:38 +0200511 NAMESPACE TF-PSA-Crypto::
512 FILE "cmake/TF-PSA-CryptoTargets.cmake")
513
514 install(
Ronald Cronba850f32024-10-11 19:39:49 +0200515 EXPORT TF-PSA-CryptoTargets
Ronald Cronf85882d2024-10-11 19:56:38 +0200516 NAMESPACE TF-PSA-Crypto::
517 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/TF-PSA-Crypto"
518 FILE "TF-PSA-CryptoTargets.cmake")
519
520 if(CMAKE_VERSION VERSION_GREATER 3.15 OR CMAKE_VERSION VERSION_EQUAL 3.15)
521 # Do not export the package by default
522 cmake_policy(SET CMP0090 NEW)
523
524 # Make this package visible to the system
525 export(PACKAGE TF-PSA-Crypto)
526 endif()
527endif()