blob: a041a8f83d73d7ee540834202a909a63031ca423 [file] [log] [blame]
Balint Dobszay3c52ce62021-05-10 16:27:18 +02001#-------------------------------------------------------------------------------
Imre Kis37676262024-06-05 11:53:03 +02002# Copyright (c) 2020-2024, 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 Kis37676262024-06-05 11:53:03 +020010set(MBEDTLS_REFSPEC "mbedtls-3.6.0"
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
Balint Dobszay3c52ce62021-05-10 16:27:18 +020035)
36
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020037# Only pass libc settings to Mbed TLS if needed. For environments where the standard
38# library is not overridden, this is not needed.
39if(TARGET stdlib::c)
Gyorgy Szingeb0507d2022-03-05 01:11:27 +000040 include(${TS_ROOT}/tools/cmake/common/PropertyCopy.cmake)
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020041 # Save libc settings
42 save_interface_target_properties(TGT stdlib::c PREFIX LIBC)
43 # Translate libc settings to CMake code fragment. Will be inserted into
44 # mbedtls-init-cache.cmake.in when LazyFetch configures the file.
45 translate_interface_target_properties(PREFIX LIBC RES _cmake_fragment)
46 unset_saved_properties(LIBC)
Balint Dobszay3c52ce62021-05-10 16:27:18 +020047endif()
48
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020049include(${TS_ROOT}/tools/cmake/common/LazyFetch.cmake REQUIRED)
50LazyFetch_MakeAvailable(DEP_NAME MbedTLS
51 FETCH_OPTIONS ${GIT_OPTIONS}
52 INSTALL_DIR ${MBEDTLS_INSTALL_DIR}
Balint Dobszay21ce26e2023-05-19 10:12:49 +020053 PACKAGE_DIR ${MBEDTLS_INSTALL_DIR}
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020054 CACHE_FILE "${TS_ROOT}/external/MbedTLS/mbedtls-init-cache.cmake.in"
Gyorgy Szing9c8daca2023-03-28 17:09:33 +020055 SOURCE_DIR "${MBEDTLS_SOURCE_DIR}"
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020056)
57unset(_cmake_fragment)
Gyorgy Szing2d924132022-09-15 18:55:30 +020058
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020059# Link the libraries created by Mbed TLS to libc if needed. For environments where the standard
60# library is not overridden, this is not needed.
Gyorgy Szing28bca0b2023-02-15 11:17:07 +010061if(TARGET stdlib::c)
Gyorgy Szing9c8daca2023-03-28 17:09:33 +020062 foreach(_mbedtls_tgt IN ITEMS "MbedTLS::mbedcrypto")
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020063 target_link_libraries(${_mbedtls_tgt} INTERFACE stdlib::c)
64 endforeach()
65 unset(_mbedtls_tgt)
66endif()
67
Balint Dobszayabec4642024-01-16 10:51:51 +010068# Advertise Mbed TLS provided psa crypto api header file. Can be used with #include MBEDTLS_PSA_CRYPTO_H
69# when it is necessary to explicitly include the mbedtls provided version of psa/crypto.h.
70add_compile_definitions(MBEDTLS_PSA_CRYPTO_H="${MBEDTLS_INSTALL_DIR}/include/psa/crypto.h")
71
72# Advertise the public interface path to allow a deployment to determine what scope to give it
73set(MBEDTLS_PUBLIC_INCLUDE_PATH "${MBEDTLS_INSTALL_DIR}/include" CACHE STRING "Mbedtls public include path")