blob: 33ba3ff8aac79d04986097e3cf02f14a14407701 [file] [log] [blame]
Soby Mathew73dad842022-12-16 12:51:01 +00001#
2# SPDX-License-Identifier: BSD-3-Clause
3# SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4#
5
Arunachalam Ganapathyd9e8e1b2024-04-19 16:27:14 +01006list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/tools/common")
7include(GitUtils)
8
Mate Toth-Pal83a45bd2023-09-01 11:17:19 +02009# The Mbed TLS library is not included in the CBMC analysis
10if(RMM_CBMC_STATIC_ANALYSIS)
11 add_library(rmm-mbedtls INTERFACE)
12 return()
13endif()
14
Mate Toth-Palc69951d2023-03-17 17:30:50 +010015set(MBEDTLS_SRC_DIR "${RMM_SOURCE_DIR}/ext/mbedtls")
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010016set(MBEDTLS_BIN_DIR "${CMAKE_BINARY_DIR}/ext/mbedtls")
Mate Toth-Palc69951d2023-03-17 17:30:50 +010017
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010018# Patch Mbed TLS before add_subdirectory() this allows to patch CMake files.
Mate Toth-Palc69951d2023-03-17 17:30:50 +010019# For details see commit messages in patch files.
20set(MBEDTLS_PATCH_DIR "${RMM_SOURCE_DIR}/configs/mbedtls")
21set(MBEDTLS_PATCH_FILES
22 "${MBEDTLS_PATCH_DIR}/0001-Remove-compiler-options-for-clang.patch"
Arunachalam Ganapathyd9e8e1b2024-04-19 16:27:14 +010023 "${MBEDTLS_PATCH_DIR}/0002-Disable-explicit_bzero.patch")
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010024
Arunachalam Ganapathyd9e8e1b2024-04-19 16:27:14 +010025Git_Apply_Patches(${MBEDTLS_SRC_DIR} "${MBEDTLS_PATCH_FILES}")
Mate Toth-Palc69951d2023-03-17 17:30:50 +010026
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010027# Starting Mbed TLS 3.x release, mbedtls/library/CMakeLists.txt has dependency on
28# link_to_source() function that is in mbedtls/CMakeLists.txt, so define a dummy
29# link_to_source here.
30function(link_to_source base_name)
31endfunction(link_to_source)
Soby Mathew73dad842022-12-16 12:51:01 +000032
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010033# Ensure that the CFlags modifications are local in scope and only apply to
34# MbedTLS build
35if(RMM_FPU_USE_AT_REL2)
36 set(BACKUP_C_FLAGS "${CMAKE_C_FLAGS}")
37 # Enable using floating point registers for mbed TLS
38 string(REPLACE "-mgeneral-regs-only" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
39 # Enable using crypto and sha instructions
40 string(REGEX REPLACE "(march=[^\\ ]*)" "\\1+sha3+crypto+nosve" CMAKE_C_FLAGS
41 ${CMAKE_C_FLAGS})
Soby Mathew02443e92023-05-12 10:49:27 +010042
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010043 add_subdirectory("${MBEDTLS_SRC_DIR}/library" "${MBEDTLS_BIN_DIR}/library"
44 EXCLUDE_FROM_ALL)
45
46 set(CMAKE_C_FLAGS "${BACKUP_C_FLAGS}")
47else()
48 add_subdirectory("${MBEDTLS_SRC_DIR}/library" "${MBEDTLS_BIN_DIR}/library"
49 EXCLUDE_FROM_ALL)
50endif()
51
52# Include targets mbedcrypto
53add_library(rmm-mbedtls INTERFACE)
54target_include_directories(rmm-mbedtls INTERFACE
55 "${RMM_SOURCE_DIR}/configs/mbedtls"
56 "${MBEDTLS_SRC_DIR}/include")
57target_compile_definitions(rmm-mbedtls INTERFACE
58 "-DMBEDTLS_CONFIG_FILE=<mbedtls_config.h>")
59
60target_include_directories(mbedcrypto PUBLIC
61 "${RMM_SOURCE_DIR}/configs/mbedtls"
62 "${MBEDTLS_SRC_DIR}/include")
63target_compile_definitions(mbedcrypto PUBLIC
64 "-DMBEDTLS_CONFIG_FILE=<mbedtls_config.h>")
65target_link_libraries(rmm-mbedtls INTERFACE mbedcrypto)