blob: f21369bceb136aba8181e547736130ba3edbba1b [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
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
Soby Mathew73dad842022-12-16 12:51:01 +000015add_library(rmm-mbedtls INTERFACE)
Mate Toth-Palc69951d2023-03-17 17:30:50 +010016
17set(MBEDTLS_SRC_DIR "${RMM_SOURCE_DIR}/ext/mbedtls")
18
Soby Mathew73dad842022-12-16 12:51:01 +000019target_include_directories(rmm-mbedtls INTERFACE ${RMM_SOURCE_DIR}/configs/mbedtls)
20target_compile_definitions(rmm-mbedtls INTERFACE MBEDTLS_CONFIG_FILE=<mbedtls_config.h>)
21
22include_directories(${RMM_SOURCE_DIR}/configs/mbedtls)
23add_compile_definitions(MBEDTLS_CONFIG_FILE=<mbedtls_config.h>)
24
Soby Mathewd2d394e2023-01-04 14:41:17 +000025#
26# Workaround for CMake not detecting size of pointer on AArch64 toolchain and
27# causing MbedTLS compilation to print warning during configuration.
28#
29if(RMM_ARCH STREQUAL aarch64)
30 set(CMAKE_SIZEOF_VOID_P 8)
31endif()
32
Soby Mathew73dad842022-12-16 12:51:01 +000033set(ENABLE_PROGRAMS OFF CACHE BOOL "Setting for mbedtls program")
34set(ENABLE_TESTING OFF CACHE BOOL "Setting for mbedtls tests")
35
Soby Mathew02443e92023-05-12 10:49:27 +010036# Ensure that the CFlags modifications are local in scope and only apply to
37# MbedTLS build
38set(BACKUP_C_CLAGS "${CMAKE_C_FLAGS}")
39
Soby Mathew73dad842022-12-16 12:51:01 +000040if(RMM_FPU_USE_AT_REL2)
41 # Enable using floating point registers for mbed TLS
42 string(REPLACE "-mgeneral-regs-only" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
43 # Enable using crypto and sha instructions
AlexeiFedorovd0da0072024-02-12 11:10:52 +000044 string(REGEX REPLACE "(march=[^\\ ]*)" "\\1+sha3+crypto+nosve" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
Soby Mathew73dad842022-12-16 12:51:01 +000045 # Enable using SHA256 and SHA512 instructions in MbedTLS
46 string(APPEND CMAKE_C_FLAGS
47 " -DMBEDTLS_SHA256_USE_A64_CRYPTO_ONLY=1 "
48 " -DMBEDTLS_SHA512_USE_A64_CRYPTO_ONLY=1 ")
49endif()
50
Mate Toth-Palc69951d2023-03-17 17:30:50 +010051# Patch Mbed TLS if it is not patched yet.
52# For details see commit messages in patch files.
53set(MBEDTLS_PATCH_DIR "${RMM_SOURCE_DIR}/configs/mbedtls")
54set(MBEDTLS_PATCH_FILES
55 "${MBEDTLS_PATCH_DIR}/0001-Remove-compiler-options-for-clang.patch"
56 "${MBEDTLS_PATCH_DIR}/0002-Disable-explicit_bzero.patch"
57)
Soby Mathewbb147112023-06-05 10:44:49 +010058
59if(NOT EXISTS ${MBEDTLS_SRC_DIR}/.git)
60 message(FATAL_ERROR "MbedTLS submodule not found")
61endif()
62
63set(REVERT_COMMAND "${GIT_EXECUTABLE}" checkout .)
64execute_process(COMMAND ${REVERT_COMMAND}
Mate Toth-Palc69951d2023-03-17 17:30:50 +010065 WORKING_DIRECTORY ${MBEDTLS_SRC_DIR}
Mate Toth-Palc69951d2023-03-17 17:30:50 +010066)
Soby Mathewbb147112023-06-05 10:44:49 +010067
68foreach (MBEDTLS_PATCH_FILE ${MBEDTLS_PATCH_FILES})
69 set(EXECUTE_COMMAND "${GIT_EXECUTABLE}" apply --verbose ${MBEDTLS_PATCH_FILE})
70 execute_process(COMMAND ${EXECUTE_COMMAND}
71 WORKING_DIRECTORY ${MBEDTLS_SRC_DIR}
72 RESULT_VARIABLE PATCH_STATUS
73 COMMAND_ECHO STDOUT
74 )
75 if (NOT PATCH_STATUS EQUAL 0)
76 message( FATAL_ERROR "Failed to apply patches at ${WORKING_DIRECTORY}" )
77 endif()
78endforeach()
Mate Toth-Palc69951d2023-03-17 17:30:50 +010079
Soby Mathew73dad842022-12-16 12:51:01 +000080#
81# Add the mbedtls subdirectory and exclude all targets in mbedtls from
82# default `all` target
83#
Mate Toth-Palc69951d2023-03-17 17:30:50 +010084add_subdirectory("${MBEDTLS_SRC_DIR}" "${CMAKE_BINARY_DIR}/ext/mbedtls" EXCLUDE_FROM_ALL)
Soby Mathew73dad842022-12-16 12:51:01 +000085
86target_link_libraries(rmm-mbedtls INTERFACE mbedtls)
Soby Mathew02443e92023-05-12 10:49:27 +010087
88# Restore the original CXX flags.
89set(CMAKE_C_FLAGS "${BACKUP_C_FLAGS}")