blob: db266d0f32960010b60a578259e3bac1cc9e4f19 [file] [log] [blame]
Tamas Ban581034a2017-12-19 19:54:37 +00001#-------------------------------------------------------------------------------
Qixiang Xua0f505c2019-02-21 17:08:50 +08002# Copyright (c) 2017-2019, Arm Limited. All rights reserved.
Tamas Ban581034a2017-12-19 19:54:37 +00003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8#When included, this file will add a target to build the mbedtls libraries with
9#the same compilation setting as used by the file including this one.
10cmake_minimum_required(VERSION 3.7)
11
12#Define where mbedtls intermediate output files are stored.
13set (MBEDTLS_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/mbedtls")
14
15#Check input variables
Jamie Fox287885f2018-10-24 14:09:34 +010016if(NOT DEFINED MBEDTLS_DEBUG)
17 message(FATAL_ERROR "Please set MBEDTLS_DEBUG to 'OFF' or 'ON' before including this file.")
Tamas Ban581034a2017-12-19 19:54:37 +000018endif()
19
20if(NOT DEFINED MBEDTLS_SOURCE_DIR)
21 message(FATAL_ERROR "Please set MBEDTLS_SOURCE_DIR before including this file.")
22endif()
23
24if(NOT DEFINED MBEDTLS_INSTALL_DIR)
25 message(FATAL_ERROR "Please set MBEDTLS_INSTALL_DIR before including this file.")
26endif()
27
28if(NOT DEFINED MBEDTLS_C_FLAGS)
29 message(FATAL_ERROR "Please set MBEDTLS_C_FLAGS before including this file.")
30endif()
31
32if(NOT DEFINED MBEDTLS_TARGET_NAME)
33 message(FATAL_ERROR "Please set MBEDTLS_TARGET_NAME before including this file.")
34endif()
35
Jamie Fox287885f2018-10-24 14:09:34 +010036if(MBEDTLS_DEBUG)
37 set(MBEDTLS_BUILD_TYPE "Debug")
38else()
39 set(MBEDTLS_BUILD_TYPE "MinSizeRel")
40endif()
41
Antonio de Angelisbec29882018-05-24 11:28:02 +010042#Based on preinclude input variables, decide if preinclude flags need to be appended
43if((NOT DEFINED MBEDTLS_PREINCLUDE_PREFIX) OR (NOT DEFINED MBEDTLS_PREINCLUDE_HEADER))
44 message(STATUS "Building mbedTLS without pre-included headers and global symbols prefixing.")
45else()
TTornblomc640e072019-06-14 14:33:51 +020046 compiler_get_preinclude_option_string(${MBEDTLS_PREINCLUDE_HEADER} _PRE_INC_STRING)
47 set(MBEDTLS_PREINCLUDE_C_FLAGS " -DLIB_PREFIX_NAME=${MBEDTLS_PREINCLUDE_PREFIX} ${_PRE_INC_STRING}")
Antonio de Angelisbec29882018-05-24 11:28:02 +010048 string(APPEND MBEDTLS_C_FLAGS ${MBEDTLS_PREINCLUDE_C_FLAGS})
49endif()
50
Qixiang Xua0f505c2019-02-21 17:08:50 +080051function(get_mbedtls_version)
52 file(READ "${MBEDTLS_SOURCE_DIR}/include/mbedtls/version.h" MBEDTLS_VER_FILE)
53 string(REGEX REPLACE ".*#define[ ]+MBEDTLS_VERSION_STRING[^\"]+\"+([0-9.]+)\".*" "\\1"
54 _MBEDTLS_VER ${MBEDTLS_VER_FILE})
55 set(MBEDTLS_VERSION ${_MBEDTLS_VER} PARENT_SCOPE)
56endfunction()
57
58get_mbedtls_version()
59
Tamas Ban581034a2017-12-19 19:54:37 +000060string(APPEND MBEDTLS_C_FLAGS ${CMAKE_C_FLAGS})
61
Jamie Fox30654e82018-10-24 16:06:49 +010062# Workaround Mbed TLS issue https://github.com/ARMmbed/mbedtls/issues/1077
David Hub0aa3282019-06-17 17:50:43 +080063if ((${ARM_CPU_ARCHITECTURE} STREQUAL "ARMv8-M.BASE") OR
64 (${ARM_CPU_ARCHITECTURE} STREQUAL "ARMv6-M"))
Jamie Fox30654e82018-10-24 16:06:49 +010065 string(APPEND MBEDTLS_C_FLAGS " -DMULADDC_CANNOT_USE_R7")
66endif()
67
Tamas Ban581034a2017-12-19 19:54:37 +000068if (TARGET ${MBEDTLS_TARGET_NAME})
69 message(FATAL_ERROR "A target with name ${MBEDTLS_TARGET_NAME} is already\
70defined. Please set MBEDTLS_TARGET_NAME to a unique value.")
71endif()
72
73#Build mbedtls as external project.
74#This ensures mbedtls is built with exactly defined settings.
75#mbedtls will be used from is't install location
76include(ExternalProject)
77# Add mbed TLS files to the build.
78set(_static_lib_command ${CMAKE_C_CREATE_STATIC_LIBRARY})
79externalproject_add(${MBEDTLS_TARGET_NAME}
80 SOURCE_DIR ${MBEDTLS_SOURCE_DIR}
81 #Set mbedtls features
82 CMAKE_ARGS -DENABLE_TESTING=OFF -DENABLE_PROGRAMS=OFF
83 #Enforce our build system's settings.
84 CMAKE_ARGS -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH} -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
Qixiang Xua0f505c2019-02-21 17:08:50 +080085 #Workaround for MbedTLS issue https://github.com/ARMmbed/mbedtls/issues/1496
86 if(MBEDTLS_VERSION VERSION_GREATER "2.7.0")
87 CMAKE_ARGS -DCMAKE_HOST_UNIX:bool=true
88 endif()
Tamas Ban581034a2017-12-19 19:54:37 +000089 #Inherit the build setting of this project
90 CMAKE_ARGS -DCMAKE_BUILD_TYPE=${MBEDTLS_BUILD_TYPE}
91 #C compiler settings
92 CMAKE_CACHE_ARGS -DCMAKE_C_COMPILER:string=${CMAKE_C_COMPILER}
93 CMAKE_CACHE_ARGS -DCMAKE_C_COMPILER_ID:string=${CMAKE_C_COMPILER_ID}
94 CMAKE_CACHE_ARGS -DCMAKE_C_FLAGS:string=${MBEDTLS_C_FLAGS}
95 CMAKE_CACHE_ARGS -DCMAKE_C_FLAGS_DEBUG:string=${CMAKE_C_FLAGS_DEBUG}
Jamie Fox287885f2018-10-24 14:09:34 +010096 CMAKE_CACHE_ARGS -DCMAKE_C_FLAGS_MINSIZEREL:string=${CMAKE_C_FLAGS_MINSIZEREL}
Tamas Ban581034a2017-12-19 19:54:37 +000097 CMAKE_CACHE_ARGS -DCMAKE_C_FLAGS_RELEASE:string=${CMAKE_C_FLAGS_RELEASE}
98 CMAKE_CACHE_ARGS -DCMAKE_C_OUTPUT_EXTENSION:string=.o
99 CMAKE_CACHE_ARGS -DCMAKE_C_COMPILER_WORKS:bool=true
100 #Archiver settings
101 CMAKE_CACHE_ARGS -DCMAKE_AR:string=${CMAKE_AR}
102 CMAKE_CACHE_ARGS -DCMAKE_C_CREATE_STATIC_LIBRARY:internal=${_static_lib_command}
103 CMAKE_CACHE_ARGS -DCMAKE_C_LINK_EXECUTABLE:string=${CMAKE_C_LINK_EXECUTABLE}
104 CMAKE_CACHE_ARGS -DCMAKE_STATIC_LIBRARY_PREFIX_C:string=${CMAKE_STATIC_LIBRARY_PREFIX_C}
105 CMAKE_CACHE_ARGS -DCMAKE_STATIC_LIBRARY_PREFIX_CXX:string=${CMAKE_STATIC_LIBRARY_PREFIX_CXX}
106 #Install location
107 CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:string=${MBEDTLS_INSTALL_DIR}
108 #Place for intermediate build files
109 BINARY_DIR ${MBEDTLS_BINARY_DIR})
110
111#Add an install target to force installation after each mbedtls build. Without
112#this target installation happens only when a clean mbedtls build is executed.
113add_custom_target(${MBEDTLS_TARGET_NAME}_install
114 COMMAND ${CMAKE_COMMAND} --build ${CMAKE_CURRENT_BINARY_DIR}/mbedtls -- install
115 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/mbedtls
116 COMMENT "Installing mbedtls to ${MBEDTLS_INSTALL_DIR}"
117 VERBATIM)
118#Make install rule depend on mbedtls library build
119add_dependencies(${MBEDTLS_TARGET_NAME}_install ${MBEDTLS_TARGET_NAME})