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 | |
| 8 | set(MBEDTLS_URL "https://github.com/ARMmbed/mbedtls.git" CACHE STRING "Mbed TLS repository URL") |
| 9 | set(MBEDTLS_REFSPEC "mbedtls-2.26.0" CACHE STRING "Mbed TLS git refspec") |
| 10 | set(MBEDTLS_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/mbedtls_install" CACHE PATH "Mbed TLS installation directory") |
| 11 | set(MBEDTLS_PACKAGE_PATH "${MBEDTLS_INSTALL_PATH}/lib/mbedtls/cmake" CACHE PATH "Mbed TLS CMake package directory") |
| 12 | |
| 13 | include(FetchContent) |
| 14 | |
| 15 | # Checking git |
| 16 | find_program(GIT_COMMAND "git") |
| 17 | if (NOT GIT_COMMAND) |
| 18 | message(FATAL_ERROR "Please install git") |
| 19 | endif() |
| 20 | |
| 21 | # Fetching Mbed TLS |
| 22 | FetchContent_Declare( |
| 23 | mbedtls |
| 24 | GIT_REPOSITORY ${MBEDTLS_URL} |
| 25 | GIT_TAG ${MBEDTLS_REFSPEC} |
| 26 | GIT_SHALLOW TRUE |
| 27 | ) |
| 28 | |
| 29 | # FetchContent_GetProperties exports mbedtls_SOURCE_DIR and mbedtls_BINARY_DIR variables |
| 30 | FetchContent_GetProperties(mbedtls) |
| 31 | if(NOT mbedtls_POPULATED) |
| 32 | message(STATUS "Fetching Mbed TLS") |
| 33 | FetchContent_Populate(mbedtls) |
| 34 | endif() |
| 35 | |
| 36 | # Convert the include path list to a string. Needed to make parameter passing to |
| 37 | # Mbed TLS build work fine. |
| 38 | string(REPLACE ";" "\\;" MBEDTLS_EXTRA_INCLUDES "${MBEDTLS_EXTRA_INCLUDES}") |
| 39 | |
| 40 | find_package(Python3 COMPONENTS Interpreter) |
| 41 | if (NOT Python3_Interpreter_FOUND) |
| 42 | message(FATAL_ERROR "Python 3 interpreter not found.") |
| 43 | endif() |
| 44 | |
| 45 | #Configure Mbed TLS to build only mbedcrypto lib |
| 46 | execute_process(COMMAND ${Python3_EXECUTABLE} scripts/config.py crypto WORKING_DIRECTORY ${mbedtls_SOURCE_DIR}) |
| 47 | |
| 48 | #Configure the library |
| 49 | if(NOT CMAKE_CROSSCOMPILING) |
| 50 | execute_process(COMMAND |
| 51 | ${CMAKE_COMMAND} |
| 52 | -DENABLE_PROGRAMS=OFF |
| 53 | -DENABLE_TESTING=OFF |
| 54 | -DUNSAFE_BUILD=ON |
| 55 | -DCMAKE_INSTALL_PREFIX=${MBEDTLS_INSTALL_PATH} |
| 56 | -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE} |
| 57 | -Dthirdparty_def=-DMBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}" |
| 58 | -Dthirdparty_inc=${MBEDTLS_EXTRA_INCLUDES} |
| 59 | -GUnix\ Makefiles |
| 60 | ${mbedtls_SOURCE_DIR} |
| 61 | WORKING_DIRECTORY |
| 62 | ${mbedtls_BINARY_DIR} |
| 63 | ) |
| 64 | else() |
| 65 | execute_process(COMMAND |
| 66 | ${CMAKE_COMMAND} |
| 67 | -DENABLE_PROGRAMS=OFF |
| 68 | -DENABLE_TESTING=OFF |
| 69 | -DUNSAFE_BUILD=ON |
| 70 | -DCMAKE_INSTALL_PREFIX=${MBEDTLS_INSTALL_PATH} |
| 71 | -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE} |
| 72 | -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY |
| 73 | -Dthirdparty_def=-DMBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}" |
| 74 | -Dthirdparty_inc=${MBEDTLS_EXTRA_INCLUDES} |
| 75 | -GUnix\ Makefiles |
| 76 | ${mbedtls_SOURCE_DIR} |
| 77 | WORKING_DIRECTORY |
| 78 | ${mbedtls_BINARY_DIR} |
| 79 | RESULT_VARIABLE _exec_error |
| 80 | ) |
| 81 | |
| 82 | if (_exec_error) |
| 83 | message(FATAL_ERROR "Configuration step of Mbed TLS failed with ${_exec_error}.") |
| 84 | endif() |
| 85 | endif() |
| 86 | |
| 87 | #TODO: add dependnecy to generated project on this file! |
| 88 | #TODO: add custom target to rebuild Mbed TLS |
| 89 | |
| 90 | #Build the library |
| 91 | execute_process(COMMAND |
| 92 | ${CMAKE_COMMAND} --build ${mbedtls_BINARY_DIR} -- install -j8 |
| 93 | RESULT_VARIABLE _exec_error |
| 94 | ) |
| 95 | if (_exec_error) |
| 96 | message(FATAL_ERROR "Build step of Mbed TLS failed with ${_exec_error}.") |
| 97 | endif() |
| 98 | |
| 99 | #Create an imported target to have clean abstraction in the build-system. |
| 100 | add_library(mbedcrypto STATIC IMPORTED) |
| 101 | set_property(TARGET mbedcrypto PROPERTY IMPORTED_LOCATION "${MBEDTLS_INSTALL_PATH}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mbedcrypto${CMAKE_STATIC_LIBRARY_SUFFIX}") |
| 102 | set_property(TARGET mbedcrypto PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INSTALL_PATH}/include") |