blob: af9577f70dbfe7f095b0a0d570fddc684049bef5 [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
6find_package(Python3 COMPONENTS Interpreter REQUIRED)
Mate Toth-Palc69951d2023-03-17 17:30:50 +01007find_package(Git)
Soby Mathew73dad842022-12-16 12:51:01 +00008
Arunachalam Ganapathyd9e8e1b2024-04-19 16:27:14 +01009list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/tools/common")
10include(GitUtils)
11
Mate Toth-Pal83a45bd2023-09-01 11:17:19 +020012# The Mbed TLS library is not included in the CBMC analysis
13if(RMM_CBMC_STATIC_ANALYSIS)
14 add_library(rmm-mbedtls INTERFACE)
15 return()
16endif()
17
Soby Mathew73dad842022-12-16 12:51:01 +000018add_library(rmm-mbedtls INTERFACE)
Mate Toth-Palc69951d2023-03-17 17:30:50 +010019
20set(MBEDTLS_SRC_DIR "${RMM_SOURCE_DIR}/ext/mbedtls")
21
Soby Mathew73dad842022-12-16 12:51:01 +000022target_include_directories(rmm-mbedtls INTERFACE ${RMM_SOURCE_DIR}/configs/mbedtls)
23target_compile_definitions(rmm-mbedtls INTERFACE MBEDTLS_CONFIG_FILE=<mbedtls_config.h>)
24
25include_directories(${RMM_SOURCE_DIR}/configs/mbedtls)
26add_compile_definitions(MBEDTLS_CONFIG_FILE=<mbedtls_config.h>)
27
Soby Mathewd2d394e2023-01-04 14:41:17 +000028#
29# Workaround for CMake not detecting size of pointer on AArch64 toolchain and
30# causing MbedTLS compilation to print warning during configuration.
31#
32if(RMM_ARCH STREQUAL aarch64)
33 set(CMAKE_SIZEOF_VOID_P 8)
34endif()
35
Soby Mathew73dad842022-12-16 12:51:01 +000036set(ENABLE_PROGRAMS OFF CACHE BOOL "Setting for mbedtls program")
37set(ENABLE_TESTING OFF CACHE BOOL "Setting for mbedtls tests")
38
Soby Mathew02443e92023-05-12 10:49:27 +010039# Ensure that the CFlags modifications are local in scope and only apply to
40# MbedTLS build
41set(BACKUP_C_CLAGS "${CMAKE_C_FLAGS}")
42
Soby Mathew73dad842022-12-16 12:51:01 +000043if(RMM_FPU_USE_AT_REL2)
44 # Enable using floating point registers for mbed TLS
45 string(REPLACE "-mgeneral-regs-only" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
46 # Enable using crypto and sha instructions
AlexeiFedorovd0da0072024-02-12 11:10:52 +000047 string(REGEX REPLACE "(march=[^\\ ]*)" "\\1+sha3+crypto+nosve" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
Soby Mathew73dad842022-12-16 12:51:01 +000048 # Enable using SHA256 and SHA512 instructions in MbedTLS
49 string(APPEND CMAKE_C_FLAGS
50 " -DMBEDTLS_SHA256_USE_A64_CRYPTO_ONLY=1 "
51 " -DMBEDTLS_SHA512_USE_A64_CRYPTO_ONLY=1 ")
52endif()
53
Mate Toth-Palc69951d2023-03-17 17:30:50 +010054# Patch Mbed TLS if it is not patched yet.
55# For details see commit messages in patch files.
56set(MBEDTLS_PATCH_DIR "${RMM_SOURCE_DIR}/configs/mbedtls")
57set(MBEDTLS_PATCH_FILES
58 "${MBEDTLS_PATCH_DIR}/0001-Remove-compiler-options-for-clang.patch"
Arunachalam Ganapathyd9e8e1b2024-04-19 16:27:14 +010059 "${MBEDTLS_PATCH_DIR}/0002-Disable-explicit_bzero.patch")
60Git_Apply_Patches(${MBEDTLS_SRC_DIR} "${MBEDTLS_PATCH_FILES}")
Mate Toth-Palc69951d2023-03-17 17:30:50 +010061
Soby Mathew73dad842022-12-16 12:51:01 +000062#
63# Add the mbedtls subdirectory and exclude all targets in mbedtls from
64# default `all` target
65#
Mate Toth-Palc69951d2023-03-17 17:30:50 +010066add_subdirectory("${MBEDTLS_SRC_DIR}" "${CMAKE_BINARY_DIR}/ext/mbedtls" EXCLUDE_FROM_ALL)
Soby Mathew73dad842022-12-16 12:51:01 +000067
68target_link_libraries(rmm-mbedtls INTERFACE mbedtls)
Soby Mathew02443e92023-05-12 10:49:27 +010069
70# Restore the original CXX flags.
71set(CMAKE_C_FLAGS "${BACKUP_C_FLAGS}")