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 | |
| 9 | add_library(rmm-mbedtls INTERFACE) |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 10 | |
| 11 | set(MBEDTLS_SRC_DIR "${RMM_SOURCE_DIR}/ext/mbedtls") |
| 12 | |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 13 | target_include_directories(rmm-mbedtls INTERFACE ${RMM_SOURCE_DIR}/configs/mbedtls) |
| 14 | target_compile_definitions(rmm-mbedtls INTERFACE MBEDTLS_CONFIG_FILE=<mbedtls_config.h>) |
| 15 | |
| 16 | include_directories(${RMM_SOURCE_DIR}/configs/mbedtls) |
| 17 | add_compile_definitions(MBEDTLS_CONFIG_FILE=<mbedtls_config.h>) |
| 18 | |
Soby Mathew | d2d394e | 2023-01-04 14:41:17 +0000 | [diff] [blame] | 19 | # |
| 20 | # Workaround for CMake not detecting size of pointer on AArch64 toolchain and |
| 21 | # causing MbedTLS compilation to print warning during configuration. |
| 22 | # |
| 23 | if(RMM_ARCH STREQUAL aarch64) |
| 24 | set(CMAKE_SIZEOF_VOID_P 8) |
| 25 | endif() |
| 26 | |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 27 | set(ENABLE_PROGRAMS OFF CACHE BOOL "Setting for mbedtls program") |
| 28 | set(ENABLE_TESTING OFF CACHE BOOL "Setting for mbedtls tests") |
| 29 | |
Soby Mathew | 02443e9 | 2023-05-12 10:49:27 +0100 | [diff] [blame] | 30 | # Ensure that the CFlags modifications are local in scope and only apply to |
| 31 | # MbedTLS build |
| 32 | set(BACKUP_C_CLAGS "${CMAKE_C_FLAGS}") |
| 33 | |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 34 | if(RMM_FPU_USE_AT_REL2) |
| 35 | # Enable using floating point registers for mbed TLS |
| 36 | string(REPLACE "-mgeneral-regs-only" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS}) |
| 37 | # Enable using crypto and sha instructions |
| 38 | string(REGEX REPLACE "(march=[^\\ ]*)" "\\1+sha3+crypto" CMAKE_C_FLAGS ${CMAKE_C_FLAGS}) |
| 39 | # Enable using SHA256 and SHA512 instructions in MbedTLS |
| 40 | string(APPEND CMAKE_C_FLAGS |
| 41 | " -DMBEDTLS_SHA256_USE_A64_CRYPTO_ONLY=1 " |
| 42 | " -DMBEDTLS_SHA512_USE_A64_CRYPTO_ONLY=1 ") |
| 43 | endif() |
| 44 | |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 45 | # Patch Mbed TLS if it is not patched yet. |
| 46 | # For details see commit messages in patch files. |
| 47 | set(MBEDTLS_PATCH_DIR "${RMM_SOURCE_DIR}/configs/mbedtls") |
| 48 | set(MBEDTLS_PATCH_FILES |
| 49 | "${MBEDTLS_PATCH_DIR}/0001-Remove-compiler-options-for-clang.patch" |
| 50 | "${MBEDTLS_PATCH_DIR}/0002-Disable-explicit_bzero.patch" |
| 51 | ) |
Soby Mathew | bb14711 | 2023-06-05 10:44:49 +0100 | [diff] [blame^] | 52 | |
| 53 | if(NOT EXISTS ${MBEDTLS_SRC_DIR}/.git) |
| 54 | message(FATAL_ERROR "MbedTLS submodule not found") |
| 55 | endif() |
| 56 | |
| 57 | set(REVERT_COMMAND "${GIT_EXECUTABLE}" checkout .) |
| 58 | execute_process(COMMAND ${REVERT_COMMAND} |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 59 | WORKING_DIRECTORY ${MBEDTLS_SRC_DIR} |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 60 | ) |
Soby Mathew | bb14711 | 2023-06-05 10:44:49 +0100 | [diff] [blame^] | 61 | |
| 62 | foreach (MBEDTLS_PATCH_FILE ${MBEDTLS_PATCH_FILES}) |
| 63 | set(EXECUTE_COMMAND "${GIT_EXECUTABLE}" apply --verbose ${MBEDTLS_PATCH_FILE}) |
| 64 | execute_process(COMMAND ${EXECUTE_COMMAND} |
| 65 | WORKING_DIRECTORY ${MBEDTLS_SRC_DIR} |
| 66 | RESULT_VARIABLE PATCH_STATUS |
| 67 | COMMAND_ECHO STDOUT |
| 68 | ) |
| 69 | if (NOT PATCH_STATUS EQUAL 0) |
| 70 | message( FATAL_ERROR "Failed to apply patches at ${WORKING_DIRECTORY}" ) |
| 71 | endif() |
| 72 | endforeach() |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 73 | |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 74 | # |
| 75 | # Add the mbedtls subdirectory and exclude all targets in mbedtls from |
| 76 | # default `all` target |
| 77 | # |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 78 | 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] | 79 | |
| 80 | target_link_libraries(rmm-mbedtls INTERFACE mbedtls) |
Soby Mathew | 02443e9 | 2023-05-12 10:49:27 +0100 | [diff] [blame] | 81 | |
| 82 | # Restore the original CXX flags. |
| 83 | set(CMAKE_C_FLAGS "${BACKUP_C_FLAGS}") |