blob: 3193a07d28f6b02c847832afd3a1be752c82bda3 [file] [log] [blame]
Balint Dobszay3c52ce62021-05-10 16:27:18 +02001#-------------------------------------------------------------------------------
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +01002# Copyright (c) 2020-2022, 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
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +01008set(MBEDTLS_URL "https://github.com/ARMmbed/mbedtls.git"
9 CACHE STRING "Mbed TLS repository URL")
Julian Hall272f9c12022-02-11 13:42:59 +000010set(MBEDTLS_REFSPEC "mbedtls-3.1.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 Szing2247d242021-09-03 16:17:25 +020016
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010017find_library(MBEDCRYPTO_LIB_FILE
18 NAMES libmbedcrypto.a mbedcrypto.a libmbedcrypto.lib mbedcrypto.lib
19 PATHS ${MBEDTLS_INSTALL_DIR}
20 PATH_SUFFIXES "lib"
21 DOC "Location of mberdrypto library."
22 NO_DEFAULT_PATH
Balint Dobszay3c52ce62021-05-10 16:27:18 +020023)
24
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010025set(MBEDCRYPTO_LIB_FILE ${MBEDCRYPTO_LIB_FILE})
26unset(MBEDCRYPTO_LIB_FILE CACHE)
Balint Dobszay3c52ce62021-05-10 16:27:18 +020027
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010028set(MBEDTLS_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/mbedtls-build")
Balint Dobszay3c52ce62021-05-10 16:27:18 +020029
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010030# Binary not found and it needs to be built.
31if (NOT MBEDCRYPTO_LIB_FILE)
32 # Determine the number of processes to run while running parallel builds.
33 # Pass -DPROCESSOR_COUNT=<n> to cmake to override.
34 if(NOT DEFINED PROCESSOR_COUNT)
35 include(ProcessorCount)
36 ProcessorCount(PROCESSOR_COUNT)
37 set(PROCESSOR_COUNT ${PROCESSOR_COUNT}
38 CACHE STRING "Number of cores to use for parallel builds.")
39 endif()
Balint Dobszay3c52ce62021-05-10 16:27:18 +020040
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010041 # See if the source is available locally
42 find_file(MBEDCRYPTO_HEADER_FILE
43 NAMES crypto.h
44 PATHS ${MBEDTLS_SOURCE_DIR}
45 PATH_SUFFIXES "include/psa"
46 NO_DEFAULT_PATH
47 )
48 set(MBEDCRYPTO_HEADER_FILE ${MBEDCRYPTO_HEADER_FILE})
49 unset(MBEDCRYPTO_HEADER_FILE CACHE)
Balint Dobszay3c52ce62021-05-10 16:27:18 +020050
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010051 # Source not found, fetch it.
52 if (NOT MBEDCRYPTO_HEADER_FILE)
53 include(FetchContent)
Julian Hall827d4472021-05-11 11:31:37 +010054
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010055 # Checking git
56 find_program(GIT_COMMAND "git")
57 if (NOT GIT_COMMAND)
58 message(FATAL_ERROR "Please install git")
59 endif()
Balint Dobszay3c52ce62021-05-10 16:27:18 +020060
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010061 # Fetching Mbed TLS
62 FetchContent_Declare(
63 mbedtls
64 SOURCE_DIR ${MBEDTLS_SOURCE_DIR}
65 BINARY_DIR ${MBEDTLS_BINARY_DIR}
66 GIT_REPOSITORY ${MBEDTLS_URL}
67 GIT_TAG ${MBEDTLS_REFSPEC}
Julian Halla628af32022-04-01 10:08:18 +010068 GIT_SHALLOW FALSE
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010069 )
Balint Dobszay3c52ce62021-05-10 16:27:18 +020070
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010071 # FetchContent_GetProperties exports mbedtls_SOURCE_DIR and mbedtls_BINARY_DIR variables
72 FetchContent_GetProperties(mbedtls)
73 # FetchContent_Populate will fail if the source directory is removed since it will try to
74 # do an "update" and not a "populate" action. As a workaround, remove the subbuild directory.
75 # Note: this fix assumes, the default subbuild location is used.
76 file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/_deps/mbedtls-subbuild")
Balint Dobszay3c52ce62021-05-10 16:27:18 +020077
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +010078 # If the source directory has been moved, the binary dir must be regenerated from scratch.
79 file(REMOVE_RECURSE "${MBEDTLS_BINARY_DIR}")
80
81 if (NOT mbedtls_POPULATED)
82 message(STATUS "Fetching Mbed TLS")
83 FetchContent_Populate(mbedtls)
84 endif()
85 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${MBEDTLS_SOURCE_DIR})
86 endif()
87
88 # Build mbedcrypto library
89
90 # Convert the include path list to a string. Needed to make parameter passing to
91 # Mbed TLS build work fine.
92 string(REPLACE ";" "\\;" MBEDTLS_EXTRA_INCLUDES "${MBEDTLS_EXTRA_INCLUDES}")
93
94 find_package(Python3 REQUIRED COMPONENTS Interpreter)
95
96 #Configure Mbed TLS to build only mbedcrypto lib
97 execute_process(COMMAND ${Python3_EXECUTABLE} scripts/config.py crypto WORKING_DIRECTORY ${MBEDTLS_SOURCE_DIR})
98
99 # Advertise Mbed TLS as the provider of the psa crypto API
100 set(PSA_CRYPTO_API_INCLUDE "${MBEDTLS_INSTALL_DIR}/include" CACHE STRING "PSA Crypto API include path")
101
Gyorgy Szingeb0507d2022-03-05 01:11:27 +0000102 include(${TS_ROOT}/tools/cmake/common/PropertyCopy.cmake)
103
104 # Only pass libc settings to MbedTLS if needed. For environments where the standard
105 # library is not overridden, this is not needed.
106 if(TARGET stdlib::c)
107 # Save libc settings
108 save_interface_target_properties(TGT stdlib::c PREFIX MBEDTLS)
109 # Translate libc settings to set of lists. _lists is not used since we know exactly which translated
110 # variables we need to pass to MbedTLS.
111 translate_interface_target_properties(PREFIX MBEDTLS RES _lists TO_LIST)
112 unset_saved_properties(MBEDTLS)
113 string(REPLACE ";" " " MBEDTLS_CMAKE_C_FLAGS_INIT "${MBEDTLS_CMAKE_C_FLAGS_INIT}")
114 string(REPLACE ";" " " MBEDTLS_CMAKE_EXE_LINKER_FLAGS_INIT "${MBEDTLS_CMAKE_EXE_LINKER_FLAGS_INIT}")
115 else()
116 set(MBEDTLS_CMAKE_C_FLAGS_INIT "")
117 set(MBEDTLS_CMAKE_EXE_LINKER_FLAGS_INIT "")
118 # _lists is set to allow namespace clean-up by calling unset_translated_lists()
119 set(_lists "MBEDTLS_CMAKE_C_FLAGS_INIT;MBEDTLS_CMAKE_EXE_LINKER_FLAGS_INIT")
120 endif()
121
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +0100122 #Configure the library
123 execute_process(COMMAND
Gyorgy Szing8d318ff2022-02-04 12:54:57 +0000124 ${CMAKE_COMMAND} -E env "CROSS_COMPILE=${CROSS_COMPILE}"
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +0100125 ${CMAKE_COMMAND}
126 -DENABLE_PROGRAMS=OFF
127 -DENABLE_TESTING=OFF
128 -DUNSAFE_BUILD=ON
129 -DCMAKE_INSTALL_PREFIX=${MBEDTLS_INSTALL_DIR}
130 -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
131 -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY
132 -DEXTERNAL_DEFINITIONS=-DMBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}"
133 -DEXTERNAL_INCLUDE_PATHS=${MBEDTLS_EXTRA_INCLUDES}
Gyorgy Szingeb0507d2022-03-05 01:11:27 +0000134 -DCMAKE_C_FLAGS_INIT=${MBEDTLS_CMAKE_C_FLAGS_INIT}
135 -DCMAKE_EXE_LINKER_FLAGS_INIT=${MBEDTLS_CMAKE_EXE_LINKER_FLAGS_INIT}
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +0100136 -GUnix\ Makefiles
Gyorgy Szing31341452022-02-08 21:58:59 +0000137 -S ${MBEDTLS_SOURCE_DIR}
138 -B ${MBEDTLS_BINARY_DIR}
Balint Dobszay3c52ce62021-05-10 16:27:18 +0200139 RESULT_VARIABLE _exec_error
140 )
Gyorgy Szingeb0507d2022-03-05 01:11:27 +0000141 unset_translated_lists(_lists)
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +0100142
143 if (_exec_error)
144 message(FATAL_ERROR "Configuration step of Mbed TLS failed with ${_exec_error}.")
145 endif()
146
147 #Build the library
148 execute_process(COMMAND
149 ${CMAKE_COMMAND} --build ${MBEDTLS_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install
150 RESULT_VARIABLE _exec_error
151 )
152
153 if (_exec_error)
154 message(FATAL_ERROR "Build step of Mbed TLS failed with ${_exec_error}.")
155 endif()
156
157 set(MBEDCRYPTO_LIB_FILE "${MBEDTLS_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mbedcrypto${CMAKE_STATIC_LIBRARY_SUFFIX}")
Balint Dobszay3c52ce62021-05-10 16:27:18 +0200158endif()
159
160#Create an imported target to have clean abstraction in the build-system.
161add_library(mbedcrypto STATIC IMPORTED)
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +0100162set_property(DIRECTORY ${CMAKE_SOURCE_DIR} APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${MBEDCRYPTO_LIB_FILE})
163set_property(TARGET mbedcrypto PROPERTY IMPORTED_LOCATION ${MBEDCRYPTO_LIB_FILE})
164set_property(TARGET mbedcrypto PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INSTALL_DIR}/include")