Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame^] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2017, Arm Limited. All rights reserved. |
| 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 |
| 16 | if(NOT DEFINED MBEDTLS_BUILD_TYPE) |
| 17 | message(FATAL_ERROR "Please set MBEDTLS_BUILD_TYPE to 'Debug' or 'Release' before including this file.") |
| 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 | |
| 36 | string(APPEND MBEDTLS_C_FLAGS ${CMAKE_C_FLAGS}) |
| 37 | |
| 38 | if (TARGET ${MBEDTLS_TARGET_NAME}) |
| 39 | message(FATAL_ERROR "A target with name ${MBEDTLS_TARGET_NAME} is already\ |
| 40 | defined. Please set MBEDTLS_TARGET_NAME to a unique value.") |
| 41 | endif() |
| 42 | |
| 43 | #Build mbedtls as external project. |
| 44 | #This ensures mbedtls is built with exactly defined settings. |
| 45 | #mbedtls will be used from is't install location |
| 46 | include(ExternalProject) |
| 47 | # Add mbed TLS files to the build. |
| 48 | set(_static_lib_command ${CMAKE_C_CREATE_STATIC_LIBRARY}) |
| 49 | externalproject_add(${MBEDTLS_TARGET_NAME} |
| 50 | SOURCE_DIR ${MBEDTLS_SOURCE_DIR} |
| 51 | #Set mbedtls features |
| 52 | CMAKE_ARGS -DENABLE_TESTING=OFF -DENABLE_PROGRAMS=OFF |
| 53 | #Enforce our build system's settings. |
| 54 | CMAKE_ARGS -DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH} -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME} |
| 55 | #Inherit the build setting of this project |
| 56 | CMAKE_ARGS -DCMAKE_BUILD_TYPE=${MBEDTLS_BUILD_TYPE} |
| 57 | #C compiler settings |
| 58 | CMAKE_CACHE_ARGS -DCMAKE_C_COMPILER:string=${CMAKE_C_COMPILER} |
| 59 | CMAKE_CACHE_ARGS -DCMAKE_C_COMPILER_ID:string=${CMAKE_C_COMPILER_ID} |
| 60 | CMAKE_CACHE_ARGS -DCMAKE_C_FLAGS:string=${MBEDTLS_C_FLAGS} |
| 61 | CMAKE_CACHE_ARGS -DCMAKE_C_FLAGS_DEBUG:string=${CMAKE_C_FLAGS_DEBUG} |
| 62 | CMAKE_CACHE_ARGS -DCMAKE_C_FLAGS_RELEASE:string=${CMAKE_C_FLAGS_RELEASE} |
| 63 | CMAKE_CACHE_ARGS -DCMAKE_C_OUTPUT_EXTENSION:string=.o |
| 64 | CMAKE_CACHE_ARGS -DCMAKE_C_COMPILER_WORKS:bool=true |
| 65 | #Archiver settings |
| 66 | CMAKE_CACHE_ARGS -DCMAKE_AR:string=${CMAKE_AR} |
| 67 | CMAKE_CACHE_ARGS -DCMAKE_C_CREATE_STATIC_LIBRARY:internal=${_static_lib_command} |
| 68 | CMAKE_CACHE_ARGS -DCMAKE_C_LINK_EXECUTABLE:string=${CMAKE_C_LINK_EXECUTABLE} |
| 69 | CMAKE_CACHE_ARGS -DCMAKE_STATIC_LIBRARY_PREFIX_C:string=${CMAKE_STATIC_LIBRARY_PREFIX_C} |
| 70 | CMAKE_CACHE_ARGS -DCMAKE_STATIC_LIBRARY_PREFIX_CXX:string=${CMAKE_STATIC_LIBRARY_PREFIX_CXX} |
| 71 | #Install location |
| 72 | CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:string=${MBEDTLS_INSTALL_DIR} |
| 73 | #Place for intermediate build files |
| 74 | BINARY_DIR ${MBEDTLS_BINARY_DIR}) |
| 75 | |
| 76 | #Add an install target to force installation after each mbedtls build. Without |
| 77 | #this target installation happens only when a clean mbedtls build is executed. |
| 78 | add_custom_target(${MBEDTLS_TARGET_NAME}_install |
| 79 | COMMAND ${CMAKE_COMMAND} --build ${CMAKE_CURRENT_BINARY_DIR}/mbedtls -- install |
| 80 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/mbedtls |
| 81 | COMMENT "Installing mbedtls to ${MBEDTLS_INSTALL_DIR}" |
| 82 | VERBATIM) |
| 83 | #Make install rule depend on mbedtls library build |
| 84 | add_dependencies(${MBEDTLS_TARGET_NAME}_install ${MBEDTLS_TARGET_NAME}) |