blob: 369b2d197aa5c7dfd42b0509e6c87f22eb6baa9c [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
8set(MBEDTLS_URL "https://github.com/ARMmbed/mbedtls.git" CACHE STRING "Mbed TLS repository URL")
9set(MBEDTLS_REFSPEC "mbedtls-2.26.0" CACHE STRING "Mbed TLS git refspec")
10set(MBEDTLS_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/mbedtls_install" CACHE PATH "Mbed TLS installation directory")
11set(MBEDTLS_PACKAGE_PATH "${MBEDTLS_INSTALL_PATH}/lib/mbedtls/cmake" CACHE PATH "Mbed TLS CMake package directory")
12
13include(FetchContent)
14
15# Checking git
16find_program(GIT_COMMAND "git")
17if (NOT GIT_COMMAND)
18 message(FATAL_ERROR "Please install git")
19endif()
20
21# Fetching Mbed TLS
22FetchContent_Declare(
23 mbedtls
24 GIT_REPOSITORY ${MBEDTLS_URL}
25 GIT_TAG ${MBEDTLS_REFSPEC}
26 GIT_SHALLOW TRUE
27)
28
29# FetchContent_GetProperties exports mbedtls_SOURCE_DIR and mbedtls_BINARY_DIR variables
30FetchContent_GetProperties(mbedtls)
31if(NOT mbedtls_POPULATED)
32 message(STATUS "Fetching Mbed TLS")
33 FetchContent_Populate(mbedtls)
34endif()
35
36# Convert the include path list to a string. Needed to make parameter passing to
37# Mbed TLS build work fine.
38string(REPLACE ";" "\\;" MBEDTLS_EXTRA_INCLUDES "${MBEDTLS_EXTRA_INCLUDES}")
39
40find_package(Python3 COMPONENTS Interpreter)
41if (NOT Python3_Interpreter_FOUND)
42 message(FATAL_ERROR "Python 3 interpreter not found.")
43endif()
44
45#Configure Mbed TLS to build only mbedcrypto lib
46execute_process(COMMAND ${Python3_EXECUTABLE} scripts/config.py crypto WORKING_DIRECTORY ${mbedtls_SOURCE_DIR})
47
48#Configure the library
49if(NOT CMAKE_CROSSCOMPILING)
50 execute_process(COMMAND
51 ${CMAKE_COMMAND}
52 -DENABLE_PROGRAMS=OFF
53 -DENABLE_TESTING=OFF
54 -DUNSAFE_BUILD=ON
55 -DCMAKE_INSTALL_PREFIX=${MBEDTLS_INSTALL_PATH}
56 -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
57 -Dthirdparty_def=-DMBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}"
58 -Dthirdparty_inc=${MBEDTLS_EXTRA_INCLUDES}
59 -GUnix\ Makefiles
60 ${mbedtls_SOURCE_DIR}
61 WORKING_DIRECTORY
62 ${mbedtls_BINARY_DIR}
63 )
64else()
65 execute_process(COMMAND
66 ${CMAKE_COMMAND}
67 -DENABLE_PROGRAMS=OFF
68 -DENABLE_TESTING=OFF
69 -DUNSAFE_BUILD=ON
70 -DCMAKE_INSTALL_PREFIX=${MBEDTLS_INSTALL_PATH}
71 -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
72 -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY
73 -Dthirdparty_def=-DMBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}"
74 -Dthirdparty_inc=${MBEDTLS_EXTRA_INCLUDES}
75 -GUnix\ Makefiles
76 ${mbedtls_SOURCE_DIR}
77 WORKING_DIRECTORY
78 ${mbedtls_BINARY_DIR}
79 RESULT_VARIABLE _exec_error
80 )
81
82 if (_exec_error)
83 message(FATAL_ERROR "Configuration step of Mbed TLS failed with ${_exec_error}.")
84 endif()
85endif()
86
87#TODO: add dependnecy to generated project on this file!
88#TODO: add custom target to rebuild Mbed TLS
89
90#Build the library
91execute_process(COMMAND
92 ${CMAKE_COMMAND} --build ${mbedtls_BINARY_DIR} -- install -j8
93 RESULT_VARIABLE _exec_error
94 )
95if (_exec_error)
96 message(FATAL_ERROR "Build step of Mbed TLS failed with ${_exec_error}.")
97endif()
98
99#Create an imported target to have clean abstraction in the build-system.
100add_library(mbedcrypto STATIC IMPORTED)
101set_property(TARGET mbedcrypto PROPERTY IMPORTED_LOCATION "${MBEDTLS_INSTALL_PATH}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mbedcrypto${CMAKE_STATIC_LIBRARY_SUFFIX}")
102set_property(TARGET mbedcrypto PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${MBEDTLS_INSTALL_PATH}/include")