Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
Gyorgy Szing | 2247d24 | 2021-09-03 16:17:25 +0200 | [diff] [blame^] | 8 | # Determine the number of processes to run while running parallel builds. |
| 9 | # Pass -DPROCESSOR_COUNT=<n> to cmake to override. |
| 10 | if(NOT DEFINED PROCESSOR_COUNT) |
| 11 | include(ProcessorCount) |
| 12 | ProcessorCount(PROCESSOR_COUNT) |
| 13 | set(PROCESSOR_COUNT ${PROCESSOR_COUNT} CACHE STRING "Number of cores to use for parallel builds.") |
| 14 | endif() |
| 15 | |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 16 | set(MBEDTLS_URL "https://github.com/ARMmbed/mbedtls.git" CACHE STRING "Mbed TLS repository URL") |
| 17 | set(MBEDTLS_REFSPEC "mbedtls-2.26.0" CACHE STRING "Mbed TLS git refspec") |
| 18 | set(MBEDTLS_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/mbedtls_install" CACHE PATH "Mbed TLS installation directory") |
| 19 | set(MBEDTLS_PACKAGE_PATH "${MBEDTLS_INSTALL_PATH}/lib/mbedtls/cmake" CACHE PATH "Mbed TLS CMake package directory") |
| 20 | |
| 21 | include(FetchContent) |
| 22 | |
| 23 | # Checking git |
| 24 | find_program(GIT_COMMAND "git") |
| 25 | if (NOT GIT_COMMAND) |
| 26 | message(FATAL_ERROR "Please install git") |
| 27 | endif() |
| 28 | |
| 29 | # Fetching Mbed TLS |
| 30 | FetchContent_Declare( |
| 31 | mbedtls |
| 32 | GIT_REPOSITORY ${MBEDTLS_URL} |
| 33 | GIT_TAG ${MBEDTLS_REFSPEC} |
| 34 | GIT_SHALLOW TRUE |
| 35 | ) |
| 36 | |
| 37 | # FetchContent_GetProperties exports mbedtls_SOURCE_DIR and mbedtls_BINARY_DIR variables |
| 38 | FetchContent_GetProperties(mbedtls) |
| 39 | if(NOT mbedtls_POPULATED) |
| 40 | message(STATUS "Fetching Mbed TLS") |
| 41 | FetchContent_Populate(mbedtls) |
| 42 | endif() |
| 43 | |
| 44 | # Convert the include path list to a string. Needed to make parameter passing to |
| 45 | # Mbed TLS build work fine. |
| 46 | string(REPLACE ";" "\\;" MBEDTLS_EXTRA_INCLUDES "${MBEDTLS_EXTRA_INCLUDES}") |
| 47 | |
| 48 | find_package(Python3 COMPONENTS Interpreter) |
| 49 | if (NOT Python3_Interpreter_FOUND) |
| 50 | message(FATAL_ERROR "Python 3 interpreter not found.") |
| 51 | endif() |
| 52 | |
| 53 | #Configure Mbed TLS to build only mbedcrypto lib |
| 54 | execute_process(COMMAND ${Python3_EXECUTABLE} scripts/config.py crypto WORKING_DIRECTORY ${mbedtls_SOURCE_DIR}) |
| 55 | |
Julian Hall | 827d447 | 2021-05-11 11:31:37 +0100 | [diff] [blame] | 56 | # Advertise Mbed TLS as the provider of the psa crypto API |
| 57 | set(PSA_CRYPTO_API_INCLUDE "${MBEDTLS_INSTALL_PATH}/include" CACHE STRING "PSA Crypto API include path") |
| 58 | |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 59 | #Configure the library |
| 60 | if(NOT CMAKE_CROSSCOMPILING) |
| 61 | execute_process(COMMAND |
| 62 | ${CMAKE_COMMAND} |
| 63 | -DENABLE_PROGRAMS=OFF |
| 64 | -DENABLE_TESTING=OFF |
| 65 | -DUNSAFE_BUILD=ON |
| 66 | -DCMAKE_INSTALL_PREFIX=${MBEDTLS_INSTALL_PATH} |
| 67 | -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE} |
| 68 | -Dthirdparty_def=-DMBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}" |
| 69 | -Dthirdparty_inc=${MBEDTLS_EXTRA_INCLUDES} |
| 70 | -GUnix\ Makefiles |
| 71 | ${mbedtls_SOURCE_DIR} |
| 72 | WORKING_DIRECTORY |
| 73 | ${mbedtls_BINARY_DIR} |
| 74 | ) |
| 75 | else() |
| 76 | execute_process(COMMAND |
| 77 | ${CMAKE_COMMAND} |
| 78 | -DENABLE_PROGRAMS=OFF |
| 79 | -DENABLE_TESTING=OFF |
| 80 | -DUNSAFE_BUILD=ON |
| 81 | -DCMAKE_INSTALL_PREFIX=${MBEDTLS_INSTALL_PATH} |
| 82 | -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE} |
| 83 | -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY |
| 84 | -Dthirdparty_def=-DMBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}" |
| 85 | -Dthirdparty_inc=${MBEDTLS_EXTRA_INCLUDES} |
| 86 | -GUnix\ Makefiles |
| 87 | ${mbedtls_SOURCE_DIR} |
| 88 | WORKING_DIRECTORY |
| 89 | ${mbedtls_BINARY_DIR} |
| 90 | RESULT_VARIABLE _exec_error |
| 91 | ) |
| 92 | |
| 93 | if (_exec_error) |
| 94 | message(FATAL_ERROR "Configuration step of Mbed TLS failed with ${_exec_error}.") |
| 95 | endif() |
| 96 | endif() |
| 97 | |
Gyorgy Szing | 2247d24 | 2021-09-03 16:17:25 +0200 | [diff] [blame^] | 98 | #TODO: add dependency to generated project on this file! |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 99 | #TODO: add custom target to rebuild Mbed TLS |
| 100 | |
| 101 | #Build the library |
| 102 | execute_process(COMMAND |
Gyorgy Szing | 2247d24 | 2021-09-03 16:17:25 +0200 | [diff] [blame^] | 103 | ${CMAKE_COMMAND} --build ${mbedtls_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install |
Balint Dobszay | 3c52ce6 | 2021-05-10 16:27:18 +0200 | [diff] [blame] | 104 | RESULT_VARIABLE _exec_error |
| 105 | ) |
| 106 | if (_exec_error) |
| 107 | message(FATAL_ERROR "Build step of Mbed TLS failed with ${_exec_error}.") |
| 108 | endif() |
| 109 | |
| 110 | #Create an imported target to have clean abstraction in the build-system. |
| 111 | add_library(mbedcrypto STATIC IMPORTED) |
| 112 | set_property(TARGET mbedcrypto PROPERTY IMPORTED_LOCATION "${MBEDTLS_INSTALL_PATH}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mbedcrypto${CMAKE_STATIC_LIBRARY_SUFFIX}") |
| 113 | set_property(TARGET mbedcrypto PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INSTALL_PATH}/include") |