blob: 5c97a15907acd2c20ddaa611567b3d9f4c8c39dc [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")
Balint Dobszayddcca1e2023-01-11 13:27:52 +010010set(MBEDTLS_REFSPEC "mbedtls-3.3.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
30 PATCH_COMMAND ${Python3_EXECUTABLE} scripts/config.py crypto
Balint Dobszay3c52ce62021-05-10 16:27:18 +020031)
32
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020033# Only pass libc settings to Mbed TLS if needed. For environments where the standard
34# library is not overridden, this is not needed.
35if(TARGET stdlib::c)
Gyorgy Szingeb0507d2022-03-05 01:11:27 +000036 include(${TS_ROOT}/tools/cmake/common/PropertyCopy.cmake)
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020037 # Save libc settings
38 save_interface_target_properties(TGT stdlib::c PREFIX LIBC)
39 # Translate libc settings to CMake code fragment. Will be inserted into
40 # mbedtls-init-cache.cmake.in when LazyFetch configures the file.
41 translate_interface_target_properties(PREFIX LIBC RES _cmake_fragment)
42 unset_saved_properties(LIBC)
Balint Dobszay3c52ce62021-05-10 16:27:18 +020043endif()
44
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020045include(${TS_ROOT}/tools/cmake/common/LazyFetch.cmake REQUIRED)
46LazyFetch_MakeAvailable(DEP_NAME MbedTLS
47 FETCH_OPTIONS ${GIT_OPTIONS}
48 INSTALL_DIR ${MBEDTLS_INSTALL_DIR}
49 PACKAGE_DIR ${MBEDTLS_INSTALL_DIR}/cmake
50 CACHE_FILE "${TS_ROOT}/external/MbedTLS/mbedtls-init-cache.cmake.in"
51)
52unset(_cmake_fragment)
Gyorgy Szing2d924132022-09-15 18:55:30 +020053
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020054# Link the libraries created by Mbed TLS to libc if needed. For environments where the standard
55# library is not overridden, this is not needed.
Gyorgy Szing28bca0b2023-02-15 11:17:07 +010056if(TARGET stdlib::c)
Balint Dobszayff8e0cd2022-08-11 15:07:52 +020057 foreach(_mbedtls_tgt IN ITEMS "MbedTLS::mbedcrypto" "MbedTLS::mbedx509" "MbedTLS::mbedtls")
58 target_link_libraries(${_mbedtls_tgt} INTERFACE stdlib::c)
59 endforeach()
60 unset(_mbedtls_tgt)
61endif()
62
63# Advertise Mbed TLS as the provider of the PSA Crypto API
64set(PSA_CRYPTO_API_INCLUDE "${MBEDTLS_INSTALL_DIR}/include"
65 CACHE STRING "PSA Crypto API include path")