blob: 45c12517284658e69a9725c722b88ec5245bf9ae [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
60if(NOT CMAKE_CROSSCOMPILING)
61 execute_process(COMMAND
62 ${CMAKE_COMMAND}
63 -DENABLE_PROGRAMS=OFF
64 -DENABLE_TESTING=OFF
65 -DUNSAFE_BUILD=ON
66 -DCMAKE_INSTALL_PREFIX=${MBEDTLS_INSTALL_PATH}
67 -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
68 -Dthirdparty_def=-DMBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}"
69 -Dthirdparty_inc=${MBEDTLS_EXTRA_INCLUDES}
70 -GUnix\ Makefiles
71 ${mbedtls_SOURCE_DIR}
72 WORKING_DIRECTORY
73 ${mbedtls_BINARY_DIR}
74 )
75else()
76 execute_process(COMMAND
77 ${CMAKE_COMMAND}
78 -DENABLE_PROGRAMS=OFF
79 -DENABLE_TESTING=OFF
80 -DUNSAFE_BUILD=ON
81 -DCMAKE_INSTALL_PREFIX=${MBEDTLS_INSTALL_PATH}
82 -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
83 -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY
84 -Dthirdparty_def=-DMBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}"
85 -Dthirdparty_inc=${MBEDTLS_EXTRA_INCLUDES}
86 -GUnix\ Makefiles
87 ${mbedtls_SOURCE_DIR}
88 WORKING_DIRECTORY
89 ${mbedtls_BINARY_DIR}
90 RESULT_VARIABLE _exec_error
91 )
92
93 if (_exec_error)
94 message(FATAL_ERROR "Configuration step of Mbed TLS failed with ${_exec_error}.")
95 endif()
96endif()
97
Gyorgy Szing2247d242021-09-03 16:17:25 +020098#TODO: add dependency to generated project on this file!
Balint Dobszay3c52ce62021-05-10 16:27:18 +020099#TODO: add custom target to rebuild Mbed TLS
100
101#Build the library
102execute_process(COMMAND
Gyorgy Szing2247d242021-09-03 16:17:25 +0200103 ${CMAKE_COMMAND} --build ${mbedtls_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install
Balint Dobszay3c52ce62021-05-10 16:27:18 +0200104 RESULT_VARIABLE _exec_error
105 )
106if (_exec_error)
107 message(FATAL_ERROR "Build step of Mbed TLS failed with ${_exec_error}.")
108endif()
109
110#Create an imported target to have clean abstraction in the build-system.
111add_library(mbedcrypto STATIC IMPORTED)
112set_property(TARGET mbedcrypto PROPERTY IMPORTED_LOCATION "${MBEDTLS_INSTALL_PATH}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mbedcrypto${CMAKE_STATIC_LIBRARY_SUFFIX}")
113set_property(TARGET mbedcrypto PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INSTALL_PATH}/include")