blob: 935be765b9291d2044aaeb80ebef96775b41a027 [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")
10set(MBEDTLS_REFSPEC "mbedtls-3.0.0"
11 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}
68 GIT_SHALLOW TRUE
69 )
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
102 #Configure the library
103 execute_process(COMMAND
104 ${CMAKE_COMMAND} -E env CROSS_COMPILE=${CROSS_COMPILE}
105 ${CMAKE_COMMAND}
106 -DENABLE_PROGRAMS=OFF
107 -DENABLE_TESTING=OFF
108 -DUNSAFE_BUILD=ON
109 -DCMAKE_INSTALL_PREFIX=${MBEDTLS_INSTALL_DIR}
110 -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
111 -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY
112 -DEXTERNAL_DEFINITIONS=-DMBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}"
113 -DEXTERNAL_INCLUDE_PATHS=${MBEDTLS_EXTRA_INCLUDES}
114 -GUnix\ Makefiles
115 ${MBEDTLS_SOURCE_DIR}
116 WORKING_DIRECTORY
117 ${MBEDTLS_BINARY_DIR}
Balint Dobszay3c52ce62021-05-10 16:27:18 +0200118 RESULT_VARIABLE _exec_error
119 )
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +0100120
121 if (_exec_error)
122 message(FATAL_ERROR "Configuration step of Mbed TLS failed with ${_exec_error}.")
123 endif()
124
125 #Build the library
126 execute_process(COMMAND
127 ${CMAKE_COMMAND} --build ${MBEDTLS_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install
128 RESULT_VARIABLE _exec_error
129 )
130
131 if (_exec_error)
132 message(FATAL_ERROR "Build step of Mbed TLS failed with ${_exec_error}.")
133 endif()
134
135 set(MBEDCRYPTO_LIB_FILE "${MBEDTLS_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mbedcrypto${CMAKE_STATIC_LIBRARY_SUFFIX}")
Balint Dobszay3c52ce62021-05-10 16:27:18 +0200136endif()
137
138#Create an imported target to have clean abstraction in the build-system.
139add_library(mbedcrypto STATIC IMPORTED)
Gyorgy Szingafb3c6e2021-12-08 04:20:34 +0100140set_property(DIRECTORY ${CMAKE_SOURCE_DIR} APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${MBEDCRYPTO_LIB_FILE})
141set_property(TARGET mbedcrypto PROPERTY IMPORTED_LOCATION ${MBEDCRYPTO_LIB_FILE})
142set_property(TARGET mbedcrypto PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INSTALL_DIR}/include")