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 | |
Mate Toth-Pal | 83a45bd | 2023-09-01 11:17:19 +0200 | [diff] [blame] | 9 | # The Mbed TLS library is not included in the CBMC analysis |
| 10 | if(RMM_CBMC_STATIC_ANALYSIS) |
| 11 | add_library(rmm-mbedtls INTERFACE) |
| 12 | return() |
| 13 | endif() |
| 14 | |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 15 | add_library(rmm-mbedtls INTERFACE) |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 16 | |
| 17 | set(MBEDTLS_SRC_DIR "${RMM_SOURCE_DIR}/ext/mbedtls") |
| 18 | |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 19 | target_include_directories(rmm-mbedtls INTERFACE ${RMM_SOURCE_DIR}/configs/mbedtls) |
| 20 | target_compile_definitions(rmm-mbedtls INTERFACE MBEDTLS_CONFIG_FILE=<mbedtls_config.h>) |
| 21 | |
| 22 | include_directories(${RMM_SOURCE_DIR}/configs/mbedtls) |
| 23 | add_compile_definitions(MBEDTLS_CONFIG_FILE=<mbedtls_config.h>) |
| 24 | |
Soby Mathew | d2d394e | 2023-01-04 14:41:17 +0000 | [diff] [blame] | 25 | # |
| 26 | # Workaround for CMake not detecting size of pointer on AArch64 toolchain and |
| 27 | # causing MbedTLS compilation to print warning during configuration. |
| 28 | # |
| 29 | if(RMM_ARCH STREQUAL aarch64) |
| 30 | set(CMAKE_SIZEOF_VOID_P 8) |
| 31 | endif() |
| 32 | |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 33 | set(ENABLE_PROGRAMS OFF CACHE BOOL "Setting for mbedtls program") |
| 34 | set(ENABLE_TESTING OFF CACHE BOOL "Setting for mbedtls tests") |
| 35 | |
Soby Mathew | 02443e9 | 2023-05-12 10:49:27 +0100 | [diff] [blame] | 36 | # Ensure that the CFlags modifications are local in scope and only apply to |
| 37 | # MbedTLS build |
| 38 | set(BACKUP_C_CLAGS "${CMAKE_C_FLAGS}") |
| 39 | |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 40 | if(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 |
| 44 | string(REGEX REPLACE "(march=[^\\ ]*)" "\\1+sha3+crypto" CMAKE_C_FLAGS ${CMAKE_C_FLAGS}) |
| 45 | # 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 ") |
| 49 | endif() |
| 50 | |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 51 | # Patch Mbed TLS if it is not patched yet. |
| 52 | # For details see commit messages in patch files. |
| 53 | set(MBEDTLS_PATCH_DIR "${RMM_SOURCE_DIR}/configs/mbedtls") |
| 54 | set(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 Mathew | bb14711 | 2023-06-05 10:44:49 +0100 | [diff] [blame] | 58 | |
| 59 | if(NOT EXISTS ${MBEDTLS_SRC_DIR}/.git) |
| 60 | message(FATAL_ERROR "MbedTLS submodule not found") |
| 61 | endif() |
| 62 | |
| 63 | set(REVERT_COMMAND "${GIT_EXECUTABLE}" checkout .) |
| 64 | execute_process(COMMAND ${REVERT_COMMAND} |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 65 | WORKING_DIRECTORY ${MBEDTLS_SRC_DIR} |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 66 | ) |
Soby Mathew | bb14711 | 2023-06-05 10:44:49 +0100 | [diff] [blame] | 67 | |
| 68 | foreach (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() |
| 78 | endforeach() |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 79 | |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 80 | # |
| 81 | # Add the mbedtls subdirectory and exclude all targets in mbedtls from |
| 82 | # default `all` target |
| 83 | # |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 84 | 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] | 85 | |
| 86 | target_link_libraries(rmm-mbedtls INTERFACE mbedtls) |
Soby Mathew | 02443e9 | 2023-05-12 10:49:27 +0100 | [diff] [blame] | 87 | |
| 88 | # Restore the original CXX flags. |
| 89 | set(CMAKE_C_FLAGS "${BACKUP_C_FLAGS}") |