blob: dbd4887543fdeb6c65df9a0a0a6893292c793595 [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"
Arunachalam Ganapathyd9e8e1b2024-04-19 16:27:14 +010020 "${MBEDTLS_PATCH_DIR}/0002-Disable-explicit_bzero.patch")
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010021
Arunachalam Ganapathyd9e8e1b2024-04-19 16:27:14 +010022Git_Apply_Patches(${MBEDTLS_SRC_DIR} "${MBEDTLS_PATCH_FILES}")
Mate Toth-Palc69951d2023-03-17 17:30:50 +010023
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010024# Starting Mbed TLS 3.x release, mbedtls/library/CMakeLists.txt has dependency on
25# link_to_source() function that is in mbedtls/CMakeLists.txt, so define a dummy
26# link_to_source here.
27function(link_to_source base_name)
28endfunction(link_to_source)
Soby Mathew73dad842022-12-16 12:51:01 +000029
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010030# Ensure that the CFlags modifications are local in scope and only apply to
31# MbedTLS build
32if(RMM_FPU_USE_AT_REL2)
33 set(BACKUP_C_FLAGS "${CMAKE_C_FLAGS}")
34 # Enable using floating point registers for mbed TLS
35 string(REPLACE "-mgeneral-regs-only" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
36 # Enable using crypto and sha instructions
37 string(REGEX REPLACE "(march=[^\\ ]*)" "\\1+sha3+crypto+nosve" CMAKE_C_FLAGS
38 ${CMAKE_C_FLAGS})
Soby Mathew02443e92023-05-12 10:49:27 +010039
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010040 add_subdirectory("${MBEDTLS_SRC_DIR}/library" "${MBEDTLS_BIN_DIR}/library"
41 EXCLUDE_FROM_ALL)
42
43 set(CMAKE_C_FLAGS "${BACKUP_C_FLAGS}")
44else()
45 add_subdirectory("${MBEDTLS_SRC_DIR}/library" "${MBEDTLS_BIN_DIR}/library"
46 EXCLUDE_FROM_ALL)
47endif()
48
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010049add_library(rmm-mbedtls INTERFACE)
50target_include_directories(rmm-mbedtls INTERFACE
51 "${RMM_SOURCE_DIR}/configs/mbedtls"
52 "${MBEDTLS_SRC_DIR}/include")
53target_compile_definitions(rmm-mbedtls INTERFACE
Arunachalam Ganapathy2da0ed82024-05-31 13:55:06 +010054 "-DMBEDTLS_CONFIG_FILE=<rmm_mbedtls_config.h>")
Arunachalam Ganapathy188d9e62024-04-18 13:42:56 +010055
Arunachalam Ganapathy33e3bdc2024-04-10 20:55:40 +010056# Include targets mbedcrypto, mbedx509
57set(MBEDTLS_LIBS mbedcrypto)
Arunachalam Ganapathy56aeccc2024-10-24 15:03:55 +010058if(RMM_V1_1)
Arunachalam Ganapathy33e3bdc2024-04-10 20:55:40 +010059 list(APPEND MBEDTLS_LIBS mbedx509)
60endif()
61
62foreach(MBEDTLS_LIB IN ITEMS ${MBEDTLS_LIBS})
63 target_include_directories(${MBEDTLS_LIB} PUBLIC
64 "${RMM_SOURCE_DIR}/configs/mbedtls"
65 "${MBEDTLS_SRC_DIR}/include")
66 target_compile_definitions(${MBEDTLS_LIB} PUBLIC
67 "-DMBEDTLS_CONFIG_FILE=<rmm_mbedtls_config.h>")
68 target_link_libraries(rmm-mbedtls INTERFACE ${MBEDTLS_LIB})
Mate Toth-Palc83c3052025-02-06 10:32:10 +010069 target_link_libraries(${MBEDTLS_LIB} PRIVATE rmm-lib-allocator)
Arunachalam Ganapathy33e3bdc2024-04-10 20:55:40 +010070endforeach()