blob: 0584a3d5df4fa7c1187e85f1ab6567d62e99a645 [file] [log] [blame]
Tamas Ban581034a2017-12-19 19:54:37 +00001#-------------------------------------------------------------------------------
Antonio de Angelisbec29882018-05-24 11:28:02 +01002# Copyright (c) 2017-2018, 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
16if(NOT DEFINED MBEDTLS_BUILD_TYPE)
17 message(FATAL_ERROR "Please set MBEDTLS_BUILD_TYPE to 'Debug' or 'Release' before including this file.")
18endif()
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
Antonio de Angelisbec29882018-05-24 11:28:02 +010036#Based on preinclude input variables, decide if preinclude flags need to be appended
37if((NOT DEFINED MBEDTLS_PREINCLUDE_PREFIX) OR (NOT DEFINED MBEDTLS_PREINCLUDE_HEADER))
38 message(STATUS "Building mbedTLS without pre-included headers and global symbols prefixing.")
39else()
40 set(MBEDTLS_PREINCLUDE_C_FLAGS " -DLIB_PREFIX_NAME=${MBEDTLS_PREINCLUDE_PREFIX} -include ${MBEDTLS_PREINCLUDE_HEADER}")
41 string(APPEND MBEDTLS_C_FLAGS ${MBEDTLS_PREINCLUDE_C_FLAGS})
42endif()
43
Tamas Ban581034a2017-12-19 19:54:37 +000044string(APPEND MBEDTLS_C_FLAGS ${CMAKE_C_FLAGS})
45
46if (TARGET ${MBEDTLS_TARGET_NAME})
47 message(FATAL_ERROR "A target with name ${MBEDTLS_TARGET_NAME} is already\
48defined. Please set MBEDTLS_TARGET_NAME to a unique value.")
49endif()
50
51#Build mbedtls as external project.
52#This ensures mbedtls is built with exactly defined settings.
53#mbedtls will be used from is't install location
54include(ExternalProject)
55# Add mbed TLS files to the build.
56set(_static_lib_command ${CMAKE_C_CREATE_STATIC_LIBRARY})
57externalproject_add(${MBEDTLS_TARGET_NAME}
58 SOURCE_DIR ${MBEDTLS_SOURCE_DIR}
59 #Set mbedtls features
60 CMAKE_ARGS -DENABLE_TESTING=OFF -DENABLE_PROGRAMS=OFF
61 #Enforce our build system's settings.
62 CMAKE_ARGS -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH} -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
63 #Inherit the build setting of this project
64 CMAKE_ARGS -DCMAKE_BUILD_TYPE=${MBEDTLS_BUILD_TYPE}
65 #C compiler settings
66 CMAKE_CACHE_ARGS -DCMAKE_C_COMPILER:string=${CMAKE_C_COMPILER}
67 CMAKE_CACHE_ARGS -DCMAKE_C_COMPILER_ID:string=${CMAKE_C_COMPILER_ID}
68 CMAKE_CACHE_ARGS -DCMAKE_C_FLAGS:string=${MBEDTLS_C_FLAGS}
69 CMAKE_CACHE_ARGS -DCMAKE_C_FLAGS_DEBUG:string=${CMAKE_C_FLAGS_DEBUG}
70 CMAKE_CACHE_ARGS -DCMAKE_C_FLAGS_RELEASE:string=${CMAKE_C_FLAGS_RELEASE}
71 CMAKE_CACHE_ARGS -DCMAKE_C_OUTPUT_EXTENSION:string=.o
72 CMAKE_CACHE_ARGS -DCMAKE_C_COMPILER_WORKS:bool=true
73 #Archiver settings
74 CMAKE_CACHE_ARGS -DCMAKE_AR:string=${CMAKE_AR}
75 CMAKE_CACHE_ARGS -DCMAKE_C_CREATE_STATIC_LIBRARY:internal=${_static_lib_command}
76 CMAKE_CACHE_ARGS -DCMAKE_C_LINK_EXECUTABLE:string=${CMAKE_C_LINK_EXECUTABLE}
77 CMAKE_CACHE_ARGS -DCMAKE_STATIC_LIBRARY_PREFIX_C:string=${CMAKE_STATIC_LIBRARY_PREFIX_C}
78 CMAKE_CACHE_ARGS -DCMAKE_STATIC_LIBRARY_PREFIX_CXX:string=${CMAKE_STATIC_LIBRARY_PREFIX_CXX}
79 #Install location
80 CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:string=${MBEDTLS_INSTALL_DIR}
81 #Place for intermediate build files
82 BINARY_DIR ${MBEDTLS_BINARY_DIR})
83
84#Add an install target to force installation after each mbedtls build. Without
85#this target installation happens only when a clean mbedtls build is executed.
86add_custom_target(${MBEDTLS_TARGET_NAME}_install
87 COMMAND ${CMAKE_COMMAND} --build ${CMAKE_CURRENT_BINARY_DIR}/mbedtls -- install
88 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/mbedtls
89 COMMENT "Installing mbedtls to ${MBEDTLS_INSTALL_DIR}"
90 VERBATIM)
91#Make install rule depend on mbedtls library build
92add_dependencies(${MBEDTLS_TARGET_NAME}_install ${MBEDTLS_TARGET_NAME})