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") |
| 10 | set(MBEDTLS_REFSPEC "mbedtls-3.0.0" |
| 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} |
| 68 | GIT_SHALLOW TRUE |
| 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 | |
| 102 | #Configure the library |
| 103 | execute_process(COMMAND |
| 104 | ${CMAKE_COMMAND} -E env CROSS_COMPILE=${CROSS_COMPILE} |
| 105 | ${CMAKE_COMMAND} |
| 106 | -DENABLE_PROGRAMS=OFF |
| 107 | -DENABLE_TESTING=OFF |
| 108 | -DUNSAFE_BUILD=ON |
| 109 | -DCMAKE_INSTALL_PREFIX=${MBEDTLS_INSTALL_DIR} |
| 110 | -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE} |
| 111 | -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY |
| 112 | -DEXTERNAL_DEFINITIONS=-DMBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}" |
| 113 | -DEXTERNAL_INCLUDE_PATHS=${MBEDTLS_EXTRA_INCLUDES} |
| 114 | -GUnix\ Makefiles |
| 115 | ${MBEDTLS_SOURCE_DIR} |
| 116 | WORKING_DIRECTORY |
| 117 | ${MBEDTLS_BINARY_DIR} |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 118 | RESULT_VARIABLE _exec_error |
| 119 | ) |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame^] | 120 | |
| 121 | if (_exec_error) |
| 122 | message(FATAL_ERROR "Configuration step of Mbed TLS failed with ${_exec_error}.") |
| 123 | endif() |
| 124 | |
| 125 | #Build the library |
| 126 | execute_process(COMMAND |
| 127 | ${CMAKE_COMMAND} --build ${MBEDTLS_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install |
| 128 | RESULT_VARIABLE _exec_error |
| 129 | ) |
| 130 | |
| 131 | if (_exec_error) |
| 132 | message(FATAL_ERROR "Build step of Mbed TLS failed with ${_exec_error}.") |
| 133 | endif() |
| 134 | |
| 135 | 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] | 136 | endif() |
| 137 | |
| 138 | #Create an imported target to have clean abstraction in the build-system. |
| 139 | add_library(mbedcrypto STATIC IMPORTED) |
Gyorgy Szing | afb3c6e | 2021-12-08 04:20:34 +0100 | [diff] [blame^] | 140 | set_property(DIRECTORY ${CMAKE_SOURCE_DIR} APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${MBEDCRYPTO_LIB_FILE}) |
| 141 | set_property(TARGET mbedcrypto PROPERTY IMPORTED_LOCATION ${MBEDCRYPTO_LIB_FILE}) |
| 142 | set_property(TARGET mbedcrypto PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INSTALL_DIR}/include") |