Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 2 | # Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved. |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 8 | set(MBEDTLS_URL "https://github.com/ARMmbed/mbedtls.git" |
| 9 | CACHE STRING "Mbed TLS repository URL") |
Julian Hall | 272f9c1 | 2022-02-11 13:42:59 +0000 | [diff] [blame] | 10 | set(MBEDTLS_REFSPEC "mbedtls-3.1.0" |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 11 | CACHE STRING "Mbed TLS git refspec") |
| 12 | set(MBEDTLS_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/mbedtls-src" |
| 13 | CACHE PATH "MbedTLS source directory") |
| 14 | set(MBEDTLS_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/mbedtls_install" |
| 15 | CACHE PATH "Mbed TLS installation directory") |
Gyorgy Szing | 34aaf21 | 2022-10-20 07:26:23 +0200 | [diff] [blame^] | 16 | set(MBEDTLS_BUILD_TYPE "Release" CACHE STRING "Mbed TLS build type") |
Gyorgy Szing | 2247d24 | 2021-09-03 16:17:25 +0200 | [diff] [blame] | 17 | |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 18 | find_library(MBEDCRYPTO_LIB_FILE |
| 19 | NAMES libmbedcrypto.a mbedcrypto.a libmbedcrypto.lib mbedcrypto.lib |
| 20 | PATHS ${MBEDTLS_INSTALL_DIR} |
| 21 | PATH_SUFFIXES "lib" |
| 22 | DOC "Location of mberdrypto library." |
| 23 | NO_DEFAULT_PATH |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 24 | ) |
| 25 | |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 26 | set(MBEDCRYPTO_LIB_FILE ${MBEDCRYPTO_LIB_FILE}) |
| 27 | unset(MBEDCRYPTO_LIB_FILE CACHE) |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 28 | |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 29 | set(MBEDTLS_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/mbedtls-build") |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 30 | |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 31 | # Binary not found and it needs to be built. |
| 32 | if (NOT MBEDCRYPTO_LIB_FILE) |
| 33 | # Determine the number of processes to run while running parallel builds. |
| 34 | # Pass -DPROCESSOR_COUNT=<n> to cmake to override. |
| 35 | if(NOT DEFINED PROCESSOR_COUNT) |
| 36 | include(ProcessorCount) |
| 37 | ProcessorCount(PROCESSOR_COUNT) |
| 38 | set(PROCESSOR_COUNT ${PROCESSOR_COUNT} |
| 39 | CACHE STRING "Number of cores to use for parallel builds.") |
| 40 | endif() |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 41 | |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 42 | # See if the source is available locally |
| 43 | find_file(MBEDCRYPTO_HEADER_FILE |
| 44 | NAMES crypto.h |
| 45 | PATHS ${MBEDTLS_SOURCE_DIR} |
| 46 | PATH_SUFFIXES "include/psa" |
| 47 | NO_DEFAULT_PATH |
| 48 | ) |
| 49 | set(MBEDCRYPTO_HEADER_FILE ${MBEDCRYPTO_HEADER_FILE}) |
| 50 | unset(MBEDCRYPTO_HEADER_FILE CACHE) |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 51 | |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 52 | # Source not found, fetch it. |
| 53 | if (NOT MBEDCRYPTO_HEADER_FILE) |
| 54 | include(FetchContent) |
Julian Hall | 827d447 | 2021-05-11 11:31:37 +0100 | [diff] [blame] | 55 | |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 56 | # Checking git |
| 57 | find_program(GIT_COMMAND "git") |
| 58 | if (NOT GIT_COMMAND) |
| 59 | message(FATAL_ERROR "Please install git") |
| 60 | endif() |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 61 | |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 62 | # Fetching Mbed TLS |
| 63 | FetchContent_Declare( |
| 64 | mbedtls |
| 65 | SOURCE_DIR ${MBEDTLS_SOURCE_DIR} |
| 66 | BINARY_DIR ${MBEDTLS_BINARY_DIR} |
| 67 | GIT_REPOSITORY ${MBEDTLS_URL} |
| 68 | GIT_TAG ${MBEDTLS_REFSPEC} |
Julian Hall | a628af3 | 2022-04-01 10:08:18 +0100 | [diff] [blame] | 69 | GIT_SHALLOW FALSE |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 70 | ) |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 71 | |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 72 | # FetchContent_GetProperties exports mbedtls_SOURCE_DIR and mbedtls_BINARY_DIR variables |
| 73 | FetchContent_GetProperties(mbedtls) |
| 74 | # FetchContent_Populate will fail if the source directory is removed since it will try to |
| 75 | # do an "update" and not a "populate" action. As a workaround, remove the subbuild directory. |
| 76 | # Note: this fix assumes, the default subbuild location is used. |
| 77 | file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/_deps/mbedtls-subbuild") |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 78 | |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 79 | # If the source directory has been moved, the binary dir must be regenerated from scratch. |
| 80 | file(REMOVE_RECURSE "${MBEDTLS_BINARY_DIR}") |
| 81 | |
| 82 | if (NOT mbedtls_POPULATED) |
| 83 | message(STATUS "Fetching Mbed TLS") |
| 84 | FetchContent_Populate(mbedtls) |
| 85 | endif() |
| 86 | set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${MBEDTLS_SOURCE_DIR}) |
| 87 | endif() |
| 88 | |
| 89 | # Build mbedcrypto library |
| 90 | |
| 91 | # Convert the include path list to a string. Needed to make parameter passing to |
| 92 | # Mbed TLS build work fine. |
| 93 | string(REPLACE ";" "\\;" MBEDTLS_EXTRA_INCLUDES "${MBEDTLS_EXTRA_INCLUDES}") |
| 94 | |
| 95 | find_package(Python3 REQUIRED COMPONENTS Interpreter) |
| 96 | |
| 97 | #Configure Mbed TLS to build only mbedcrypto lib |
| 98 | execute_process(COMMAND ${Python3_EXECUTABLE} scripts/config.py crypto WORKING_DIRECTORY ${MBEDTLS_SOURCE_DIR}) |
| 99 | |
Gyorgy Szing | eb0507d | 2022-03-05 01:11:27 +0000 | [diff] [blame] | 100 | include(${TS_ROOT}/tools/cmake/common/PropertyCopy.cmake) |
| 101 | |
| 102 | # Only pass libc settings to MbedTLS if needed. For environments where the standard |
| 103 | # library is not overridden, this is not needed. |
| 104 | if(TARGET stdlib::c) |
| 105 | # Save libc settings |
| 106 | save_interface_target_properties(TGT stdlib::c PREFIX MBEDTLS) |
| 107 | # Translate libc settings to set of lists. _lists is not used since we know exactly which translated |
| 108 | # variables we need to pass to MbedTLS. |
| 109 | translate_interface_target_properties(PREFIX MBEDTLS RES _lists TO_LIST) |
| 110 | unset_saved_properties(MBEDTLS) |
| 111 | string(REPLACE ";" " " MBEDTLS_CMAKE_C_FLAGS_INIT "${MBEDTLS_CMAKE_C_FLAGS_INIT}") |
| 112 | string(REPLACE ";" " " MBEDTLS_CMAKE_EXE_LINKER_FLAGS_INIT "${MBEDTLS_CMAKE_EXE_LINKER_FLAGS_INIT}") |
| 113 | else() |
| 114 | set(MBEDTLS_CMAKE_C_FLAGS_INIT "") |
| 115 | set(MBEDTLS_CMAKE_EXE_LINKER_FLAGS_INIT "") |
| 116 | # _lists is set to allow namespace clean-up by calling unset_translated_lists() |
| 117 | set(_lists "MBEDTLS_CMAKE_C_FLAGS_INIT;MBEDTLS_CMAKE_EXE_LINKER_FLAGS_INIT") |
| 118 | endif() |
| 119 | |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 120 | #Configure the library |
| 121 | execute_process(COMMAND |
Gyorgy Szing | 8d318ff | 2022-02-04 12:54:57 +0000 | [diff] [blame] | 122 | ${CMAKE_COMMAND} -E env "CROSS_COMPILE=${CROSS_COMPILE}" |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 123 | ${CMAKE_COMMAND} |
Gyorgy Szing | 34aaf21 | 2022-10-20 07:26:23 +0200 | [diff] [blame^] | 124 | -DCMAKE_BUILD_TYPE=${MBEDTLS_BUILD_TYPE} |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 125 | -DENABLE_PROGRAMS=OFF |
| 126 | -DENABLE_TESTING=OFF |
| 127 | -DUNSAFE_BUILD=ON |
| 128 | -DCMAKE_INSTALL_PREFIX=${MBEDTLS_INSTALL_DIR} |
| 129 | -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE} |
| 130 | -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY |
| 131 | -DEXTERNAL_DEFINITIONS=-DMBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}" |
| 132 | -DEXTERNAL_INCLUDE_PATHS=${MBEDTLS_EXTRA_INCLUDES} |
Gyorgy Szing | eb0507d | 2022-03-05 01:11:27 +0000 | [diff] [blame] | 133 | -DCMAKE_C_FLAGS_INIT=${MBEDTLS_CMAKE_C_FLAGS_INIT} |
| 134 | -DCMAKE_EXE_LINKER_FLAGS_INIT=${MBEDTLS_CMAKE_EXE_LINKER_FLAGS_INIT} |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 135 | -GUnix\ Makefiles |
Gyorgy Szing | 3134145 | 2022-02-08 21:58:59 +0000 | [diff] [blame] | 136 | -S ${MBEDTLS_SOURCE_DIR} |
| 137 | -B ${MBEDTLS_BINARY_DIR} |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 138 | RESULT_VARIABLE _exec_error |
| 139 | ) |
Gyorgy Szing | eb0507d | 2022-03-05 01:11:27 +0000 | [diff] [blame] | 140 | unset_translated_lists(_lists) |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 141 | |
| 142 | if (_exec_error) |
| 143 | message(FATAL_ERROR "Configuration step of Mbed TLS failed with ${_exec_error}.") |
| 144 | endif() |
| 145 | |
| 146 | #Build the library |
| 147 | execute_process(COMMAND |
| 148 | ${CMAKE_COMMAND} --build ${MBEDTLS_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install |
| 149 | RESULT_VARIABLE _exec_error |
| 150 | ) |
| 151 | |
| 152 | if (_exec_error) |
| 153 | message(FATAL_ERROR "Build step of Mbed TLS failed with ${_exec_error}.") |
| 154 | endif() |
| 155 | |
| 156 | set(MBEDCRYPTO_LIB_FILE "${MBEDTLS_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mbedcrypto${CMAKE_STATIC_LIBRARY_SUFFIX}") |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 157 | endif() |
| 158 | |
Gyorgy Szing | 2d92413 | 2022-09-15 18:55:30 +0200 | [diff] [blame] | 159 | # Advertise Mbed TLS as the provider of the psa crypto API |
| 160 | set(PSA_CRYPTO_API_INCLUDE "${MBEDTLS_INSTALL_DIR}/include" CACHE STRING "PSA Crypto API include path") |
| 161 | |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 162 | #Create an imported target to have clean abstraction in the build-system. |
| 163 | add_library(mbedcrypto STATIC IMPORTED) |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame] | 164 | set_property(DIRECTORY ${CMAKE_SOURCE_DIR} APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${MBEDCRYPTO_LIB_FILE}) |
| 165 | set_property(TARGET mbedcrypto PROPERTY IMPORTED_LOCATION ${MBEDCRYPTO_LIB_FILE}) |
| 166 | set_property(TARGET mbedcrypto PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INSTALL_DIR}/include") |