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 | ) |
| 52 | set(EXECUTE_COMMAND "${GIT_EXECUTABLE}" diff "library/CMakeLists.txt") |
| 53 | execute_process(COMMAND ${EXECUTE_COMMAND} |
| 54 | WORKING_DIRECTORY ${MBEDTLS_SRC_DIR} |
| 55 | RESULT_VARIABLE DIFF_STATUS |
| 56 | OUTPUT_VARIABLE DIFF_OUTPUT |
| 57 | ) |
| 58 | if (NOT DIFF_STATUS EQUAL 0) |
| 59 | message( FATAL_ERROR "Failed to check applied Mbed TLS patch") |
| 60 | endif() |
| 61 | if("${DIFF_OUTPUT}" STREQUAL "") |
| 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() |
| 73 | endif() |
| 74 | |
Soby Mathew | 73dad84 | 2022-12-16 12:51:01 +0000 | [diff] [blame] | 75 | # |
| 76 | # Add the mbedtls subdirectory and exclude all targets in mbedtls from |
| 77 | # default `all` target |
| 78 | # |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame^] | 79 | 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] | 80 | |
| 81 | target_link_libraries(rmm-mbedtls INTERFACE mbedtls) |
Soby Mathew | 02443e9 | 2023-05-12 10:49:27 +0100 | [diff] [blame] | 82 | |
| 83 | # Restore the original CXX flags. |
| 84 | set(CMAKE_C_FLAGS "${BACKUP_C_FLAGS}") |