Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: BSD-3-Clause |
| 3 | # SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | # |
| 5 | |
| 6 | find_package(Python3 COMPONENTS Interpreter REQUIRED) |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 7 | find_package(Git) |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 8 | |
Arunachalam Ganapathy | d9e8e1b | 2024-04-19 16:27:14 +0100 | [diff] [blame^] | 9 | list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/tools/common") |
| 10 | include(GitUtils) |
| 11 | |
Mate Toth-Pal | 83a45bd | 2023-09-01 11:17:19 +0200 | [diff] [blame] | 12 | # The Mbed TLS library is not included in the CBMC analysis |
| 13 | if(RMM_CBMC_STATIC_ANALYSIS) |
| 14 | add_library(rmm-mbedtls INTERFACE) |
| 15 | return() |
| 16 | endif() |
| 17 | |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 18 | add_library(rmm-mbedtls INTERFACE) |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 19 | |
| 20 | set(MBEDTLS_SRC_DIR "${RMM_SOURCE_DIR}/ext/mbedtls") |
| 21 | |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 22 | target_include_directories(rmm-mbedtls INTERFACE ${RMM_SOURCE_DIR}/configs/mbedtls) |
| 23 | target_compile_definitions(rmm-mbedtls INTERFACE MBEDTLS_CONFIG_FILE=<mbedtls_config.h>) |
| 24 | |
| 25 | include_directories(${RMM_SOURCE_DIR}/configs/mbedtls) |
| 26 | add_compile_definitions(MBEDTLS_CONFIG_FILE=<mbedtls_config.h>) |
| 27 | |
Soby Mathew | d2d394e | 2023-01-04 14:41:17 +0000 | [diff] [blame] | 28 | # |
| 29 | # Workaround for CMake not detecting size of pointer on AArch64 toolchain and |
| 30 | # causing MbedTLS compilation to print warning during configuration. |
| 31 | # |
| 32 | if(RMM_ARCH STREQUAL aarch64) |
| 33 | set(CMAKE_SIZEOF_VOID_P 8) |
| 34 | endif() |
| 35 | |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 36 | set(ENABLE_PROGRAMS OFF CACHE BOOL "Setting for mbedtls program") |
| 37 | set(ENABLE_TESTING OFF CACHE BOOL "Setting for mbedtls tests") |
| 38 | |
Soby Mathew | 02443e9 | 2023-05-12 10:49:27 +0100 | [diff] [blame] | 39 | # Ensure that the CFlags modifications are local in scope and only apply to |
| 40 | # MbedTLS build |
| 41 | set(BACKUP_C_CLAGS "${CMAKE_C_FLAGS}") |
| 42 | |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 43 | if(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 |
AlexeiFedorov | d0da007 | 2024-02-12 11:10:52 +0000 | [diff] [blame] | 47 | string(REGEX REPLACE "(march=[^\\ ]*)" "\\1+sha3+crypto+nosve" CMAKE_C_FLAGS ${CMAKE_C_FLAGS}) |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 48 | # 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 ") |
| 52 | endif() |
| 53 | |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 54 | # Patch Mbed TLS if it is not patched yet. |
| 55 | # For details see commit messages in patch files. |
| 56 | set(MBEDTLS_PATCH_DIR "${RMM_SOURCE_DIR}/configs/mbedtls") |
| 57 | set(MBEDTLS_PATCH_FILES |
| 58 | "${MBEDTLS_PATCH_DIR}/0001-Remove-compiler-options-for-clang.patch" |
Arunachalam Ganapathy | d9e8e1b | 2024-04-19 16:27:14 +0100 | [diff] [blame^] | 59 | "${MBEDTLS_PATCH_DIR}/0002-Disable-explicit_bzero.patch") |
| 60 | Git_Apply_Patches(${MBEDTLS_SRC_DIR} "${MBEDTLS_PATCH_FILES}") |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 61 | |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 62 | # |
| 63 | # Add the mbedtls subdirectory and exclude all targets in mbedtls from |
| 64 | # default `all` target |
| 65 | # |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 66 | add_subdirectory("${MBEDTLS_SRC_DIR}" "${CMAKE_BINARY_DIR}/ext/mbedtls" EXCLUDE_FROM_ALL) |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 67 | |
| 68 | target_link_libraries(rmm-mbedtls INTERFACE mbedtls) |
Soby Mathew | 02443e9 | 2023-05-12 10:49:27 +0100 | [diff] [blame] | 69 | |
| 70 | # Restore the original CXX flags. |
| 71 | set(CMAKE_C_FLAGS "${BACKUP_C_FLAGS}") |