blob: 707757bd8f14d16d687be9c1d423bc2b048b7a0b [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
Mate Toth-Pal83a45bd2023-09-01 11:17:19 +02006# The Mbed TLS library is not included in the CBMC analysis
7if(RMM_CBMC_STATIC_ANALYSIS)
8 add_library(rmm-mbedtls INTERFACE)
9 return()
10endif()
11
Mate Toth-Palc69951d2023-03-17 17:30:50 +010012set(MBEDTLS_SRC_DIR "${RMM_SOURCE_DIR}/ext/mbedtls")
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010013set(MBEDTLS_BIN_DIR "${CMAKE_BINARY_DIR}/ext/mbedtls")
Mate Toth-Palc69951d2023-03-17 17:30:50 +010014
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010015# Patch Mbed TLS before add_subdirectory() this allows to patch CMake files.
Mate Toth-Palc69951d2023-03-17 17:30:50 +010016# For details see commit messages in patch files.
17set(MBEDTLS_PATCH_DIR "${RMM_SOURCE_DIR}/configs/mbedtls")
18set(MBEDTLS_PATCH_FILES
19 "${MBEDTLS_PATCH_DIR}/0001-Remove-compiler-options-for-clang.patch"
Mate Toth-Pal01b8fb22025-02-19 17:24:30 +010020 "${MBEDTLS_PATCH_DIR}/0002-Disable-explicit_bzero.patch"
21 "${MBEDTLS_PATCH_DIR}/0003-refactor-alloc-Use-the-heap-structure-provided-by-ap.patch")
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010022
Arunachalam Ganapathyd9e8e1b2024-04-19 16:27:14 +010023Git_Apply_Patches(${MBEDTLS_SRC_DIR} "${MBEDTLS_PATCH_FILES}")
Mate Toth-Palc69951d2023-03-17 17:30:50 +010024
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010025# Starting Mbed TLS 3.x release, mbedtls/library/CMakeLists.txt has dependency on
26# link_to_source() function that is in mbedtls/CMakeLists.txt, so define a dummy
27# link_to_source here.
28function(link_to_source base_name)
29endfunction(link_to_source)
Soby Mathew73dad842022-12-16 12:51:01 +000030
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010031# Ensure that the CFlags modifications are local in scope and only apply to
32# MbedTLS build
33if(RMM_FPU_USE_AT_REL2)
34 set(BACKUP_C_FLAGS "${CMAKE_C_FLAGS}")
35 # Enable using floating point registers for mbed TLS
36 string(REPLACE "-mgeneral-regs-only" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
37 # Enable using crypto and sha instructions
38 string(REGEX REPLACE "(march=[^\\ ]*)" "\\1+sha3+crypto+nosve" CMAKE_C_FLAGS
39 ${CMAKE_C_FLAGS})
Soby Mathew02443e92023-05-12 10:49:27 +010040
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010041 add_subdirectory("${MBEDTLS_SRC_DIR}/library" "${MBEDTLS_BIN_DIR}/library"
42 EXCLUDE_FROM_ALL)
43
44 set(CMAKE_C_FLAGS "${BACKUP_C_FLAGS}")
45else()
46 add_subdirectory("${MBEDTLS_SRC_DIR}/library" "${MBEDTLS_BIN_DIR}/library"
47 EXCLUDE_FROM_ALL)
48endif()
49
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010050add_library(rmm-mbedtls INTERFACE)
51target_include_directories(rmm-mbedtls INTERFACE
52 "${RMM_SOURCE_DIR}/configs/mbedtls"
53 "${MBEDTLS_SRC_DIR}/include")
54target_compile_definitions(rmm-mbedtls INTERFACE
Arunachalam Ganapathy2da0ed82024-05-31 13:55:06 +010055 "-DMBEDTLS_CONFIG_FILE=<rmm_mbedtls_config.h>")
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010056
Arunachalam Ganapathy33e3bdc2024-04-10 20:55:40 +010057# Include targets mbedcrypto, mbedx509
58set(MBEDTLS_LIBS mbedcrypto)
Arunachalam Ganapathy56aeccc2024-10-24 15:03:55 +010059if(RMM_V1_1)
Arunachalam Ganapathy33e3bdc2024-04-10 20:55:40 +010060 list(APPEND MBEDTLS_LIBS mbedx509)
61endif()
62
63foreach(MBEDTLS_LIB IN ITEMS ${MBEDTLS_LIBS})
64 target_include_directories(${MBEDTLS_LIB} PUBLIC
65 "${RMM_SOURCE_DIR}/configs/mbedtls"
66 "${MBEDTLS_SRC_DIR}/include")
67 target_compile_definitions(${MBEDTLS_LIB} PUBLIC
68 "-DMBEDTLS_CONFIG_FILE=<rmm_mbedtls_config.h>")
69 target_link_libraries(rmm-mbedtls INTERFACE ${MBEDTLS_LIB})
Mate Toth-Palc83c3052025-02-06 10:32:10 +010070 target_link_libraries(${MBEDTLS_LIB} PRIVATE rmm-lib-allocator)
Arunachalam Ganapathy33e3bdc2024-04-10 20:55:40 +010071endforeach()