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