blob: 17d29814b063de66ebe4ede21597700c5645411d [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
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010018find_library(MBEDCRYPTO_LIB_FILE
19 NAMES libmbedcrypto.a mbedcrypto.a libmbedcrypto.lib mbedcrypto.lib
20 PATHS ${MBEDTLS_INSTALL_DIR}
21 PATH_SUFFIXES "lib"
22 DOC "Location of mberdrypto library."
23 NO_DEFAULT_PATH
Balint Dobszay3c52ce62021-05-10 16:27:18 +020024)
25
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010026set(MBEDCRYPTO_LIB_FILE ${MBEDCRYPTO_LIB_FILE})
27unset(MBEDCRYPTO_LIB_FILE CACHE)
Balint Dobszay3c52ce62021-05-10 16:27:18 +020028
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010029set(MBEDTLS_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/mbedtls-build")
Balint Dobszay3c52ce62021-05-10 16:27:18 +020030
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010031# Binary not found and it needs to be built.
32if (NOT MBEDCRYPTO_LIB_FILE)
33 # Determine the number of processes to run while running parallel builds.
34 # Pass -DPROCESSOR_COUNT=<n> to cmake to override.
35 if(NOT DEFINED PROCESSOR_COUNT)
36 include(ProcessorCount)
37 ProcessorCount(PROCESSOR_COUNT)
38 set(PROCESSOR_COUNT ${PROCESSOR_COUNT}
39 CACHE STRING "Number of cores to use for parallel builds.")
40 endif()
Balint Dobszay3c52ce62021-05-10 16:27:18 +020041
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010042 # See if the source is available locally
43 find_file(MBEDCRYPTO_HEADER_FILE
44 NAMES crypto.h
45 PATHS ${MBEDTLS_SOURCE_DIR}
46 PATH_SUFFIXES "include/psa"
47 NO_DEFAULT_PATH
48 )
49 set(MBEDCRYPTO_HEADER_FILE ${MBEDCRYPTO_HEADER_FILE})
50 unset(MBEDCRYPTO_HEADER_FILE CACHE)
Balint Dobszay3c52ce62021-05-10 16:27:18 +020051
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010052 # Source not found, fetch it.
53 if (NOT MBEDCRYPTO_HEADER_FILE)
54 include(FetchContent)
Julian Hall827d4472021-05-11 11:31:37 +010055
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010056 # Checking git
57 find_program(GIT_COMMAND "git")
58 if (NOT GIT_COMMAND)
59 message(FATAL_ERROR "Please install git")
60 endif()
Balint Dobszay3c52ce62021-05-10 16:27:18 +020061
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010062 # Fetching Mbed TLS
63 FetchContent_Declare(
64 mbedtls
65 SOURCE_DIR ${MBEDTLS_SOURCE_DIR}
66 BINARY_DIR ${MBEDTLS_BINARY_DIR}
67 GIT_REPOSITORY ${MBEDTLS_URL}
68 GIT_TAG ${MBEDTLS_REFSPEC}
Julian Halla628af32022-04-01 10:08:18 +010069 GIT_SHALLOW FALSE
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010070 )
Balint Dobszay3c52ce62021-05-10 16:27:18 +020071
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010072 # FetchContent_GetProperties exports mbedtls_SOURCE_DIR and mbedtls_BINARY_DIR variables
73 FetchContent_GetProperties(mbedtls)
74 # FetchContent_Populate will fail if the source directory is removed since it will try to
75 # do an "update" and not a "populate" action. As a workaround, remove the subbuild directory.
76 # Note: this fix assumes, the default subbuild location is used.
77 file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/_deps/mbedtls-subbuild")
Balint Dobszay3c52ce62021-05-10 16:27:18 +020078
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010079 # If the source directory has been moved, the binary dir must be regenerated from scratch.
80 file(REMOVE_RECURSE "${MBEDTLS_BINARY_DIR}")
81
82 if (NOT mbedtls_POPULATED)
83 message(STATUS "Fetching Mbed TLS")
84 FetchContent_Populate(mbedtls)
85 endif()
86 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${MBEDTLS_SOURCE_DIR})
87 endif()
88
89 # Build mbedcrypto library
90
91 # Convert the include path list to a string. Needed to make parameter passing to
92 # Mbed TLS build work fine.
93 string(REPLACE ";" "\\;" MBEDTLS_EXTRA_INCLUDES "${MBEDTLS_EXTRA_INCLUDES}")
94
95 find_package(Python3 REQUIRED COMPONENTS Interpreter)
96
97 #Configure Mbed TLS to build only mbedcrypto lib
98 execute_process(COMMAND ${Python3_EXECUTABLE} scripts/config.py crypto WORKING_DIRECTORY ${MBEDTLS_SOURCE_DIR})
99
Gyorgy Szingeb0507d2022-03-05 01:11:27 +0000100 include(${TS_ROOT}/tools/cmake/common/PropertyCopy.cmake)
101
102 # Only pass libc settings to MbedTLS if needed. For environments where the standard
103 # library is not overridden, this is not needed.
104 if(TARGET stdlib::c)
105 # Save libc settings
106 save_interface_target_properties(TGT stdlib::c PREFIX MBEDTLS)
107 # Translate libc settings to set of lists. _lists is not used since we know exactly which translated
108 # variables we need to pass to MbedTLS.
109 translate_interface_target_properties(PREFIX MBEDTLS RES _lists TO_LIST)
110 unset_saved_properties(MBEDTLS)
111 string(REPLACE ";" " " MBEDTLS_CMAKE_C_FLAGS_INIT "${MBEDTLS_CMAKE_C_FLAGS_INIT}")
112 string(REPLACE ";" " " MBEDTLS_CMAKE_EXE_LINKER_FLAGS_INIT "${MBEDTLS_CMAKE_EXE_LINKER_FLAGS_INIT}")
113 else()
114 set(MBEDTLS_CMAKE_C_FLAGS_INIT "")
115 set(MBEDTLS_CMAKE_EXE_LINKER_FLAGS_INIT "")
116 # _lists is set to allow namespace clean-up by calling unset_translated_lists()
117 set(_lists "MBEDTLS_CMAKE_C_FLAGS_INIT;MBEDTLS_CMAKE_EXE_LINKER_FLAGS_INIT")
118 endif()
119
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +0100120 #Configure the library
121 execute_process(COMMAND
Gyorgy Szing8d318ff2022-02-04 12:54:57 +0000122 ${CMAKE_COMMAND} -E env "CROSS_COMPILE=${CROSS_COMPILE}"
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +0100123 ${CMAKE_COMMAND}
Gyorgy Szing34aaf212022-10-20 07:26:23 +0200124 -DCMAKE_BUILD_TYPE=${MBEDTLS_BUILD_TYPE}
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +0100125 -DENABLE_PROGRAMS=OFF
126 -DENABLE_TESTING=OFF
127 -DUNSAFE_BUILD=ON
128 -DCMAKE_INSTALL_PREFIX=${MBEDTLS_INSTALL_DIR}
129 -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
130 -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY
131 -DEXTERNAL_DEFINITIONS=-DMBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}"
132 -DEXTERNAL_INCLUDE_PATHS=${MBEDTLS_EXTRA_INCLUDES}
Gyorgy Szingeb0507d2022-03-05 01:11:27 +0000133 -DCMAKE_C_FLAGS_INIT=${MBEDTLS_CMAKE_C_FLAGS_INIT}
134 -DCMAKE_EXE_LINKER_FLAGS_INIT=${MBEDTLS_CMAKE_EXE_LINKER_FLAGS_INIT}
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +0100135 -GUnix\ Makefiles
Gyorgy Szing31341452022-02-08 21:58:59 +0000136 -S ${MBEDTLS_SOURCE_DIR}
137 -B ${MBEDTLS_BINARY_DIR}
Balint Dobszay3c52ce62021-05-10 16:27:18 +0200138 RESULT_VARIABLE _exec_error
139 )
Gyorgy Szingeb0507d2022-03-05 01:11:27 +0000140 unset_translated_lists(_lists)
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +0100141
142 if (_exec_error)
143 message(FATAL_ERROR "Configuration step of Mbed TLS failed with ${_exec_error}.")
144 endif()
145
146 #Build the library
147 execute_process(COMMAND
148 ${CMAKE_COMMAND} --build ${MBEDTLS_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install
149 RESULT_VARIABLE _exec_error
150 )
151
152 if (_exec_error)
153 message(FATAL_ERROR "Build step of Mbed TLS failed with ${_exec_error}.")
154 endif()
155
156 set(MBEDCRYPTO_LIB_FILE "${MBEDTLS_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mbedcrypto${CMAKE_STATIC_LIBRARY_SUFFIX}")
Balint Dobszay3c52ce62021-05-10 16:27:18 +0200157endif()
158
Gyorgy Szing2d924132022-09-15 18:55:30 +0200159# Advertise Mbed TLS as the provider of the psa crypto API
160set(PSA_CRYPTO_API_INCLUDE "${MBEDTLS_INSTALL_DIR}/include" CACHE STRING "PSA Crypto API include path")
161
Balint Dobszay3c52ce62021-05-10 16:27:18 +0200162#Create an imported target to have clean abstraction in the build-system.
163add_library(mbedcrypto STATIC IMPORTED)
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +0100164set_property(DIRECTORY ${CMAKE_SOURCE_DIR} APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${MBEDCRYPTO_LIB_FILE})
165set_property(TARGET mbedcrypto PROPERTY IMPORTED_LOCATION ${MBEDCRYPTO_LIB_FILE})
166set_property(TARGET mbedcrypto PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INSTALL_DIR}/include")
Gyorgy Szing28bca0b2023-02-15 11:17:07 +0100167if(TARGET stdlib::c)
168 target_link_libraries(mbedcrypto INTERFACE stdlib::c)
169endif()