Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
Antonio de Angelis | bec2988 | 2018-05-24 11:28:02 +0100 | [diff] [blame] | 2 | # Copyright (c) 2017-2018, Arm Limited. All rights reserved. |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 3 | # |
| 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. |
| 10 | cmake_minimum_required(VERSION 3.7) |
| 11 | |
| 12 | #Define where mbedtls intermediate output files are stored. |
| 13 | set (MBEDTLS_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/mbedtls") |
| 14 | |
| 15 | #Check input variables |
Jamie Fox | 287885f | 2018-10-24 14:09:34 +0100 | [diff] [blame] | 16 | if(NOT DEFINED MBEDTLS_DEBUG) |
| 17 | message(FATAL_ERROR "Please set MBEDTLS_DEBUG to 'OFF' or 'ON' before including this file.") |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 18 | endif() |
| 19 | |
| 20 | if(NOT DEFINED MBEDTLS_SOURCE_DIR) |
| 21 | message(FATAL_ERROR "Please set MBEDTLS_SOURCE_DIR before including this file.") |
| 22 | endif() |
| 23 | |
| 24 | if(NOT DEFINED MBEDTLS_INSTALL_DIR) |
| 25 | message(FATAL_ERROR "Please set MBEDTLS_INSTALL_DIR before including this file.") |
| 26 | endif() |
| 27 | |
| 28 | if(NOT DEFINED MBEDTLS_C_FLAGS) |
| 29 | message(FATAL_ERROR "Please set MBEDTLS_C_FLAGS before including this file.") |
| 30 | endif() |
| 31 | |
| 32 | if(NOT DEFINED MBEDTLS_TARGET_NAME) |
| 33 | message(FATAL_ERROR "Please set MBEDTLS_TARGET_NAME before including this file.") |
| 34 | endif() |
| 35 | |
Jamie Fox | 287885f | 2018-10-24 14:09:34 +0100 | [diff] [blame] | 36 | if(MBEDTLS_DEBUG) |
| 37 | set(MBEDTLS_BUILD_TYPE "Debug") |
| 38 | else() |
| 39 | set(MBEDTLS_BUILD_TYPE "MinSizeRel") |
| 40 | endif() |
| 41 | |
Antonio de Angelis | bec2988 | 2018-05-24 11:28:02 +0100 | [diff] [blame] | 42 | #Based on preinclude input variables, decide if preinclude flags need to be appended |
| 43 | if((NOT DEFINED MBEDTLS_PREINCLUDE_PREFIX) OR (NOT DEFINED MBEDTLS_PREINCLUDE_HEADER)) |
| 44 | message(STATUS "Building mbedTLS without pre-included headers and global symbols prefixing.") |
| 45 | else() |
| 46 | set(MBEDTLS_PREINCLUDE_C_FLAGS " -DLIB_PREFIX_NAME=${MBEDTLS_PREINCLUDE_PREFIX} -include ${MBEDTLS_PREINCLUDE_HEADER}") |
| 47 | string(APPEND MBEDTLS_C_FLAGS ${MBEDTLS_PREINCLUDE_C_FLAGS}) |
| 48 | endif() |
| 49 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 50 | string(APPEND MBEDTLS_C_FLAGS ${CMAKE_C_FLAGS}) |
| 51 | |
Jamie Fox | 30654e8 | 2018-10-24 16:06:49 +0100 | [diff] [blame] | 52 | # Workaround Mbed TLS issue https://github.com/ARMmbed/mbedtls/issues/1077 |
Antonio de Angelis | f1f7ebd | 2018-11-23 23:11:41 +0000 | [diff] [blame] | 53 | if(${ARM_CPU_ARCHITECTURE} STREQUAL "ARMv8-M.BASE") |
Jamie Fox | 30654e8 | 2018-10-24 16:06:49 +0100 | [diff] [blame] | 54 | string(APPEND MBEDTLS_C_FLAGS " -DMULADDC_CANNOT_USE_R7") |
| 55 | endif() |
| 56 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 57 | if (TARGET ${MBEDTLS_TARGET_NAME}) |
| 58 | message(FATAL_ERROR "A target with name ${MBEDTLS_TARGET_NAME} is already\ |
| 59 | defined. Please set MBEDTLS_TARGET_NAME to a unique value.") |
| 60 | endif() |
| 61 | |
| 62 | #Build mbedtls as external project. |
| 63 | #This ensures mbedtls is built with exactly defined settings. |
| 64 | #mbedtls will be used from is't install location |
| 65 | include(ExternalProject) |
| 66 | # Add mbed TLS files to the build. |
| 67 | set(_static_lib_command ${CMAKE_C_CREATE_STATIC_LIBRARY}) |
| 68 | externalproject_add(${MBEDTLS_TARGET_NAME} |
| 69 | SOURCE_DIR ${MBEDTLS_SOURCE_DIR} |
| 70 | #Set mbedtls features |
| 71 | CMAKE_ARGS -DENABLE_TESTING=OFF -DENABLE_PROGRAMS=OFF |
| 72 | #Enforce our build system's settings. |
| 73 | CMAKE_ARGS -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH} -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME} |
| 74 | #Inherit the build setting of this project |
| 75 | CMAKE_ARGS -DCMAKE_BUILD_TYPE=${MBEDTLS_BUILD_TYPE} |
| 76 | #C compiler settings |
| 77 | CMAKE_CACHE_ARGS -DCMAKE_C_COMPILER:string=${CMAKE_C_COMPILER} |
| 78 | CMAKE_CACHE_ARGS -DCMAKE_C_COMPILER_ID:string=${CMAKE_C_COMPILER_ID} |
| 79 | CMAKE_CACHE_ARGS -DCMAKE_C_FLAGS:string=${MBEDTLS_C_FLAGS} |
| 80 | CMAKE_CACHE_ARGS -DCMAKE_C_FLAGS_DEBUG:string=${CMAKE_C_FLAGS_DEBUG} |
Jamie Fox | 287885f | 2018-10-24 14:09:34 +0100 | [diff] [blame] | 81 | CMAKE_CACHE_ARGS -DCMAKE_C_FLAGS_MINSIZEREL:string=${CMAKE_C_FLAGS_MINSIZEREL} |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 82 | CMAKE_CACHE_ARGS -DCMAKE_C_FLAGS_RELEASE:string=${CMAKE_C_FLAGS_RELEASE} |
| 83 | CMAKE_CACHE_ARGS -DCMAKE_C_OUTPUT_EXTENSION:string=.o |
| 84 | CMAKE_CACHE_ARGS -DCMAKE_C_COMPILER_WORKS:bool=true |
| 85 | #Archiver settings |
| 86 | CMAKE_CACHE_ARGS -DCMAKE_AR:string=${CMAKE_AR} |
| 87 | CMAKE_CACHE_ARGS -DCMAKE_C_CREATE_STATIC_LIBRARY:internal=${_static_lib_command} |
| 88 | CMAKE_CACHE_ARGS -DCMAKE_C_LINK_EXECUTABLE:string=${CMAKE_C_LINK_EXECUTABLE} |
| 89 | CMAKE_CACHE_ARGS -DCMAKE_STATIC_LIBRARY_PREFIX_C:string=${CMAKE_STATIC_LIBRARY_PREFIX_C} |
| 90 | CMAKE_CACHE_ARGS -DCMAKE_STATIC_LIBRARY_PREFIX_CXX:string=${CMAKE_STATIC_LIBRARY_PREFIX_CXX} |
| 91 | #Install location |
| 92 | CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:string=${MBEDTLS_INSTALL_DIR} |
| 93 | #Place for intermediate build files |
| 94 | BINARY_DIR ${MBEDTLS_BINARY_DIR}) |
| 95 | |
| 96 | #Add an install target to force installation after each mbedtls build. Without |
| 97 | #this target installation happens only when a clean mbedtls build is executed. |
| 98 | add_custom_target(${MBEDTLS_TARGET_NAME}_install |
| 99 | COMMAND ${CMAKE_COMMAND} --build ${CMAKE_CURRENT_BINARY_DIR}/mbedtls -- install |
| 100 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/mbedtls |
| 101 | COMMENT "Installing mbedtls to ${MBEDTLS_INSTALL_DIR}" |
| 102 | VERBATIM) |
| 103 | #Make install rule depend on mbedtls library build |
| 104 | add_dependencies(${MBEDTLS_TARGET_NAME}_install ${MBEDTLS_TARGET_NAME}) |