blob: f759e19f712b6e827b675611b0cd08bbb22618d1 [file] [log] [blame]
Balint Dobszay3c52ce62021-05-10 16:27:18 +02001#-------------------------------------------------------------------------------
Gyorgy Szing28bca0b2023-02-15 11:17:07 +01002# Copyright (c) 2020-2023, Arm Limited and Contributors. All rights reserved.
Balint Dobszay3c52ce62021-05-10 16:27:18 +02003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
Balint Dobszayddcca1e2023-01-11 13:27:52 +01008set(MBEDTLS_URL "https://github.com/Mbed-TLS/mbedtls.git"
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +01009 CACHE STRING "Mbed TLS repository URL")
Imre Kis8a86a002024-01-22 15:43:11 +010010set(MBEDTLS_REFSPEC "mbedtls-3.5.1"
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010011 CACHE STRING "Mbed TLS git refspec")
12set(MBEDTLS_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/mbedtls-src"
13 CACHE PATH "MbedTLS source directory")
14set(MBEDTLS_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/mbedtls_install"
15 CACHE PATH "Mbed TLS installation directory")
Gyorgy Szing34aaf212022-10-20 07:26:23 +020016set(MBEDTLS_BUILD_TYPE "Release" CACHE STRING "Mbed TLS build type")
Gyorgy Szing2247d242021-09-03 16:17:25 +020017
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020018find_package(Python3 REQUIRED COMPONENTS Interpreter)
19
20# Mbed TLS has a custom config script that must be ran before invoking CMake.
21# This script configures which components of the project will get built, in our
22# use case only mbedcrypto is necessary. LazyFetch has a PATCH_COMMAND option
23# that was intended to be used for patching the repo after fetch, but before
24# running CMake. However, it can be "misused" in this case to run the Mbed TLS
25# config script.
26set(GIT_OPTIONS
27 GIT_REPOSITORY ${MBEDTLS_URL}
28 GIT_TAG ${MBEDTLS_REFSPEC}
29 GIT_SHALLOW FALSE
Gyorgy Szing9c8daca2023-03-28 17:09:33 +020030 PATCH_COMMAND
31 git stash
32 COMMAND git branch -f bf-am
Julian Hall29620bf2022-06-09 10:26:37 +010033 COMMAND git am ${CMAKE_CURRENT_LIST_DIR}/0001-Add-capability-to-selectively-build-libraries.patch
Gyorgy Szing9c8daca2023-03-28 17:09:33 +020034 COMMAND git reset bf-am
35 COMMAND ${Python3_EXECUTABLE} scripts/config.py crypto
Balint Dobszay3c52ce62021-05-10 16:27:18 +020036)
37
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020038# Only pass libc settings to Mbed TLS if needed. For environments where the standard
39# library is not overridden, this is not needed.
40if(TARGET stdlib::c)
Gyorgy Szingeb0507d2022-03-05 01:11:27 +000041 include(${TS_ROOT}/tools/cmake/common/PropertyCopy.cmake)
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020042 # Save libc settings
43 save_interface_target_properties(TGT stdlib::c PREFIX LIBC)
44 # Translate libc settings to CMake code fragment. Will be inserted into
45 # mbedtls-init-cache.cmake.in when LazyFetch configures the file.
46 translate_interface_target_properties(PREFIX LIBC RES _cmake_fragment)
47 unset_saved_properties(LIBC)
Balint Dobszay3c52ce62021-05-10 16:27:18 +020048endif()
49
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020050include(${TS_ROOT}/tools/cmake/common/LazyFetch.cmake REQUIRED)
51LazyFetch_MakeAvailable(DEP_NAME MbedTLS
52 FETCH_OPTIONS ${GIT_OPTIONS}
53 INSTALL_DIR ${MBEDTLS_INSTALL_DIR}
Balint Dobszay21ce26e2023-05-19 10:12:49 +020054 PACKAGE_DIR ${MBEDTLS_INSTALL_DIR}
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020055 CACHE_FILE "${TS_ROOT}/external/MbedTLS/mbedtls-init-cache.cmake.in"
Gyorgy Szing9c8daca2023-03-28 17:09:33 +020056 SOURCE_DIR "${MBEDTLS_SOURCE_DIR}"
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020057)
58unset(_cmake_fragment)
Gyorgy Szing2d924132022-09-15 18:55:30 +020059
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020060# Link the libraries created by Mbed TLS to libc if needed. For environments where the standard
61# library is not overridden, this is not needed.
Gyorgy Szing28bca0b2023-02-15 11:17:07 +010062if(TARGET stdlib::c)
Gyorgy Szing9c8daca2023-03-28 17:09:33 +020063 foreach(_mbedtls_tgt IN ITEMS "MbedTLS::mbedcrypto")
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020064 target_link_libraries(${_mbedtls_tgt} INTERFACE stdlib::c)
65 endforeach()
66 unset(_mbedtls_tgt)
67endif()
68
Balint Dobszayabec4642024-01-16 10:51:51 +010069# Advertise Mbed TLS provided psa crypto api header file. Can be used with #include MBEDTLS_PSA_CRYPTO_H
70# when it is necessary to explicitly include the mbedtls provided version of psa/crypto.h.
71add_compile_definitions(MBEDTLS_PSA_CRYPTO_H="${MBEDTLS_INSTALL_DIR}/include/psa/crypto.h")
72
73# Advertise the public interface path to allow a deployment to determine what scope to give it
74set(MBEDTLS_PUBLIC_INCLUDE_PATH "${MBEDTLS_INSTALL_DIR}/include" CACHE STRING "Mbedtls public include path")