blob: 041af5c65efff346255cd29d545653c3f67d1abe [file] [log] [blame]
Balint Dobszay3c52ce62021-05-10 16:27:18 +02001#-------------------------------------------------------------------------------
2# Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
Gyorgy Szing2247d242021-09-03 16:17:25 +02008# Determine the number of processes to run while running parallel builds.
9# Pass -DPROCESSOR_COUNT=<n> to cmake to override.
10if(NOT DEFINED PROCESSOR_COUNT)
11 include(ProcessorCount)
12 ProcessorCount(PROCESSOR_COUNT)
13 set(PROCESSOR_COUNT ${PROCESSOR_COUNT} CACHE STRING "Number of cores to use for parallel builds.")
14endif()
15
Balint Dobszay3c52ce62021-05-10 16:27:18 +020016set(MBEDTLS_URL "https://github.com/ARMmbed/mbedtls.git" CACHE STRING "Mbed TLS repository URL")
17set(MBEDTLS_REFSPEC "mbedtls-2.26.0" CACHE STRING "Mbed TLS git refspec")
18set(MBEDTLS_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/mbedtls_install" CACHE PATH "Mbed TLS installation directory")
19set(MBEDTLS_PACKAGE_PATH "${MBEDTLS_INSTALL_PATH}/lib/mbedtls/cmake" CACHE PATH "Mbed TLS CMake package directory")
20
21include(FetchContent)
22
23# Checking git
24find_program(GIT_COMMAND "git")
25if (NOT GIT_COMMAND)
26 message(FATAL_ERROR "Please install git")
27endif()
28
29# Fetching Mbed TLS
30FetchContent_Declare(
31 mbedtls
32 GIT_REPOSITORY ${MBEDTLS_URL}
33 GIT_TAG ${MBEDTLS_REFSPEC}
34 GIT_SHALLOW TRUE
35)
36
37# FetchContent_GetProperties exports mbedtls_SOURCE_DIR and mbedtls_BINARY_DIR variables
38FetchContent_GetProperties(mbedtls)
39if(NOT mbedtls_POPULATED)
40 message(STATUS "Fetching Mbed TLS")
41 FetchContent_Populate(mbedtls)
42endif()
43
44# Convert the include path list to a string. Needed to make parameter passing to
45# Mbed TLS build work fine.
46string(REPLACE ";" "\\;" MBEDTLS_EXTRA_INCLUDES "${MBEDTLS_EXTRA_INCLUDES}")
47
48find_package(Python3 COMPONENTS Interpreter)
49if (NOT Python3_Interpreter_FOUND)
50 message(FATAL_ERROR "Python 3 interpreter not found.")
51endif()
52
53#Configure Mbed TLS to build only mbedcrypto lib
54execute_process(COMMAND ${Python3_EXECUTABLE} scripts/config.py crypto WORKING_DIRECTORY ${mbedtls_SOURCE_DIR})
55
Julian Hall827d4472021-05-11 11:31:37 +010056# Advertise Mbed TLS as the provider of the psa crypto API
57set(PSA_CRYPTO_API_INCLUDE "${MBEDTLS_INSTALL_PATH}/include" CACHE STRING "PSA Crypto API include path")
58
Balint Dobszay3c52ce62021-05-10 16:27:18 +020059#Configure the library
Julian Hall0051ed12021-07-22 13:59:24 +010060execute_process(COMMAND
61 ${CMAKE_COMMAND}
62 -DENABLE_PROGRAMS=OFF
63 -DENABLE_TESTING=OFF
64 -DUNSAFE_BUILD=ON
65 -DCMAKE_INSTALL_PREFIX=${MBEDTLS_INSTALL_PATH}
66 -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
67 -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY
68 -DEXTERNAL_DEFINITIONS=-DMBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}"
69 -DEXTERNAL_INCLUDE_PATHS=${MBEDTLS_EXTRA_INCLUDES}
70 -GUnix\ Makefiles
71 ${mbedtls_SOURCE_DIR}
72 WORKING_DIRECTORY
73 ${mbedtls_BINARY_DIR}
74 RESULT_VARIABLE _exec_error
75)
Balint Dobszay3c52ce62021-05-10 16:27:18 +020076
Julian Hall0051ed12021-07-22 13:59:24 +010077if (_exec_error)
78 message(FATAL_ERROR "Configuration step of Mbed TLS failed with ${_exec_error}.")
Balint Dobszay3c52ce62021-05-10 16:27:18 +020079endif()
80
Gyorgy Szing2247d242021-09-03 16:17:25 +020081#TODO: add dependency to generated project on this file!
Balint Dobszay3c52ce62021-05-10 16:27:18 +020082#TODO: add custom target to rebuild Mbed TLS
83
84#Build the library
85execute_process(COMMAND
Gyorgy Szing2247d242021-09-03 16:17:25 +020086 ${CMAKE_COMMAND} --build ${mbedtls_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install
Balint Dobszay3c52ce62021-05-10 16:27:18 +020087 RESULT_VARIABLE _exec_error
88 )
89if (_exec_error)
90 message(FATAL_ERROR "Build step of Mbed TLS failed with ${_exec_error}.")
91endif()
92
93#Create an imported target to have clean abstraction in the build-system.
94add_library(mbedcrypto STATIC IMPORTED)
95set_property(TARGET mbedcrypto PROPERTY IMPORTED_LOCATION "${MBEDTLS_INSTALL_PATH}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mbedcrypto${CMAKE_STATIC_LIBRARY_SUFFIX}")
96set_property(TARGET mbedcrypto PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INSTALL_PATH}/include")