blob: 0af40bdaa10bc13cfab68b0c4aefd4e89665e389 [file] [log] [blame]
Julian Hall827d4472021-05-11 11:31:37 +01001#-------------------------------------------------------------------------------
2# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6# t_cose is a library for signing CBOR tokens using COSE_Sign1
7#-------------------------------------------------------------------------------
8
Gyorgy Szing2247d242021-09-03 16:17:25 +02009# Determine the number of processes to run while running parallel builds.
10# Pass -DPROCESSOR_COUNT=<n> to cmake to override.
11if(NOT DEFINED PROCESSOR_COUNT)
12 include(ProcessorCount)
13 ProcessorCount(PROCESSOR_COUNT)
14 set(PROCESSOR_COUNT ${PROCESSOR_COUNT} CACHE STRING "Number of cores to use for parallel builds.")
15endif()
16
Julian Hall827d4472021-05-11 11:31:37 +010017# External component details
18set(T_COSE_URL "https://github.com/laurencelundblade/t_cose.git" CACHE STRING "t_cose repository URL")
19set(T_COSE_REFSPEC "master" CACHE STRING "t_cose git refspec")
20set(T_COSE_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/t_cose_install" CACHE PATH "t_cose installation directory")
21set(T_COSE_PACKAGE_PATH "${T_COSE_INSTALL_PATH}/libt_cose/cmake" CACHE PATH "t_cose CMake package directory")
22
23include(FetchContent)
24
25# Checking git
26find_program(GIT_COMMAND "git")
27if (NOT GIT_COMMAND)
28 message(FATAL_ERROR "Please install git")
29endif()
30
31# Fetching t_cose
32FetchContent_Declare(
33 t_cose
34 GIT_REPOSITORY ${T_COSE_URL}
35 GIT_TAG ${T_COSE_REFSPEC}
36 GIT_SHALLOW TRUE
37
38 PATCH_COMMAND git stash
39 COMMAND git am ${CMAKE_CURRENT_LIST_DIR}/0001-add-install-definition.patch
40 COMMAND git reset HEAD~1
41
42)
43
44# FetchContent_GetProperties exports t_cose_SOURCE_DIR and t_cose_BINARY_DIR variables
45FetchContent_GetProperties(t_cose)
46if(NOT t_cose_POPULATED)
47 message(STATUS "Fetching t_cose")
48 FetchContent_Populate(t_cose)
49endif()
50
Gyorgy Szing2247d242021-09-03 16:17:25 +020051# Prepare include paths for dependencies that t_codse has on external components
Julian Hall827d4472021-05-11 11:31:37 +010052get_target_property(_qcbor_inc qcbor INTERFACE_INCLUDE_DIRECTORIES)
53set(_ext_inc_paths
54 ${_qcbor_inc}
55 ${PSA_CRYPTO_API_INCLUDE})
56
Andrew Beggs97a00d42021-06-15 15:45:46 +000057if (NOT TCOSE_EXTERNAL_INCLUDE_PATHS STREQUAL "")
58 list(APPEND _ext_inc_paths "${TCOSE_EXTERNAL_INCLUDE_PATHS}")
59 unset(TCOSE_EXTERNAL_INCLUDE_PATHS)
60endif()
61
Julian Hall827d4472021-05-11 11:31:37 +010062string(REPLACE ";" "\\;" _ext_inc_paths "${_ext_inc_paths}")
63
64# Configure the t_cose library
65execute_process(COMMAND
66${CMAKE_COMMAND}
67 -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
68 -Dthirdparty_inc=${_ext_inc_paths}
69 -DCMAKE_INSTALL_PREFIX=${T_COSE_INSTALL_PATH}
70 -DMBEDTLS=On
71 -GUnix\ Makefiles
72 ${t_cose_SOURCE_DIR}
73WORKING_DIRECTORY
74 ${t_cose_BINARY_DIR}
75)
76
77# Build the library
78execute_process(COMMAND
Gyorgy Szing2247d242021-09-03 16:17:25 +020079 ${CMAKE_COMMAND} --build ${t_cose_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install
Julian Hall827d4472021-05-11 11:31:37 +010080 RESULT_VARIABLE _exec_error
81 )
82if (_exec_error)
83 message(FATAL_ERROR "Build step of t_cose failed with ${_exec_error}.")
84endif()
85
86# Create an imported target to have clean abstraction in the build-system.
87add_library(t_cose STATIC IMPORTED)
88set_property(TARGET t_cose PROPERTY IMPORTED_LOCATION "${T_COSE_INSTALL_PATH}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}t_cose${CMAKE_STATIC_LIBRARY_SUFFIX}")
89set_property(TARGET t_cose PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${T_COSE_INSTALL_PATH}/include")