blob: c55cd90e19a1a5a26dcd007f2fcf6e836cb53f7c [file] [log] [blame]
Raef Colesf42f0882020-07-10 10:01:58 +01001#-------------------------------------------------------------------------------
Raef Colesd97a7e72021-12-10 14:58:06 +00002# Copyright (c) 2020-2022, Arm Limited. All rights reserved.
Raef Colesf42f0882020-07-10 10:01:58 +01003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
Raef Coles69817322020-10-19 14:14:14 +01008cmake_minimum_required(VERSION 3.15)
Raef Colesf42f0882020-07-10 10:01:58 +01009find_package(Python3)
10
11############################### Manifest declaration ###########################
12
Kevin Peng65064c52021-10-27 17:12:17 +080013list(APPEND TEMP_MANIFEST_LISTS ${TFM_MANIFEST_LIST})
David Hub2694202021-07-15 14:58:39 +080014
David Hu12f25872021-08-23 14:55:46 +080015if (TFM_NS_REG_TEST OR TFM_S_REG_TEST)
Raef Colesd97a7e72021-12-10 14:58:06 +000016 list(APPEND TEMP_MANIFEST_LISTS ${TFM_TEST_PATH}/secure_fw/test_services/tfm_test_manifest_list.yaml)
Kevin Peng65064c52021-10-27 17:12:17 +080017endif()
18
19if ("${TEST_PSA_API}" STREQUAL "IPC")
20 list(APPEND TEMP_MANIFEST_LISTS ${CMAKE_CURRENT_SOURCE_DIR}/tfm_psa_ff_test_manifest_list.yaml)
David Hub2694202021-07-15 14:58:39 +080021endif()
Raef Colesf42f0882020-07-10 10:01:58 +010022
David Huf5f12092021-07-12 16:11:47 +080023if (TFM_EXTRA_MANIFEST_LIST_FILES)
Kevin Peng65064c52021-10-27 17:12:17 +080024 list(APPEND TEMP_MANIFEST_LISTS ${TFM_EXTRA_MANIFEST_LIST_FILES})
David Huf5f12092021-07-12 16:11:47 +080025endif()
26
Kevin Peng1b8177b2021-12-07 15:06:25 +080027# Build up the manifest list arrays:
28# - CONFIGURED_MANIFEST_LISTS:
29# Array of Manifest lists under build directory which are the output of configure_file().
30# - MANIFEST_LIST_PATHS:
31# Array of paths of the input manifest lists of configure_file().
32# They are NOT the paths of CONFIGURED_MANIFEST_LISTS.
33# They can be used to build up manifest file paths if manifest file paths are
34# relative ones in the manifest lists.
35# - COMBINED_LIST:
36# A combined list of the above two, with the following format:
37# [configured_list_a, path_of_list_a, configured_list_b, path_of_list_b ... ]
David Huf5f12092021-07-12 16:11:47 +080038set(POSTFIX 1)
39
Kevin Peng65064c52021-10-27 17:12:17 +080040foreach(MANIFEST_LIST IN LISTS TEMP_MANIFEST_LISTS)
David Huf5f12092021-07-12 16:11:47 +080041 if (NOT EXISTS ${MANIFEST_LIST})
Kevin Peng65064c52021-10-27 17:12:17 +080042 message(FATAL_ERROR "Manifest list ${MANIFEST_LIST} doesn't exist")
David Huf5f12092021-07-12 16:11:47 +080043 endif()
44
45 get_filename_component(MANIFEST_LIST_NAME ${MANIFEST_LIST} NAME_WLE)
Kevin Peng1b8177b2021-12-07 15:06:25 +080046 set(CONFIGURED_LIST
David Huf5f12092021-07-12 16:11:47 +080047 ${CMAKE_CURRENT_BINARY_DIR}/${MANIFEST_LIST_NAME}_${POSTFIX}.yaml)
48
Kevin Peng1b8177b2021-12-07 15:06:25 +080049 configure_file(${MANIFEST_LIST} ${CONFIGURED_LIST})
50 list(APPEND CONFIGURED_MANIFEST_LISTS ${CONFIGURED_LIST})
51 list(APPEND COMBINED_LIST ${CONFIGURED_LIST})
David Huf5f12092021-07-12 16:11:47 +080052
53 get_filename_component(MANIFEST_LIST_PATH ${MANIFEST_LIST} DIRECTORY)
Kevin Peng1b8177b2021-12-07 15:06:25 +080054 list(APPEND MANIFEST_LIST_PATHS ${MANIFEST_LIST_PATH})
55 list(APPEND COMBINED_LIST ${MANIFEST_LIST_PATH})
David Huf5f12092021-07-12 16:11:47 +080056
57 math(EXPR POSTFIX "${POSTFIX} + 1")
58endforeach()
59
Raef Colesf42f0882020-07-10 10:01:58 +010060############################### File list declaration ##########################
61
62set(GENERATED_FILE_LISTS ${CMAKE_CURRENT_SOURCE_DIR}/tfm_generated_file_list.yaml)
63set(GENERATED_FILE_LISTS ${GENERATED_FILE_LISTS} ${TFM_EXTRA_GENERATED_FILE_LIST_PATH})
64
65############################### Dependency generation ##########################
66
67function(parse_field_from_yaml files field output_variable)
Kevin Peng1b8177b2021-12-07 15:06:25 +080068 set(local_variable "")
Raef Colesf42f0882020-07-10 10:01:58 +010069 foreach(yaml_file ${files})
70 # Load the lines that refer to the key we selected
71 file(STRINGS ${yaml_file} temp_variable REGEX " *\"${field}\":")
72 # Take only the value of the key
73 list(TRANSFORM temp_variable REPLACE " *\"${field}\": *" ";")
74 # Remove all commas
75 list(TRANSFORM temp_variable REPLACE "," "")
76 # Remove all quote marks
77 list(TRANSFORM temp_variable REPLACE "\"" "")
Kevin Peng1b8177b2021-12-07 15:06:25 +080078 list(APPEND local_variable ${temp_variable})
Raef Colesf42f0882020-07-10 10:01:58 +010079 endforeach()
Kevin Peng1b8177b2021-12-07 15:06:25 +080080 set(${output_variable} ${local_variable} PARENT_SCOPE)
Raef Colesf42f0882020-07-10 10:01:58 +010081endfunction()
82
83parse_field_from_yaml("${GENERATED_FILE_LISTS}" template TEMPLATE_FILES)
84# Replace relative paths with absolute paths
Kevin Peng1b8177b2021-12-07 15:06:25 +080085# Paths used in GENERATED_FILE_LISTS are all relative to TF-M root (${CMAKE_SOURCE_DIR})
Ken Liua2ac1f92020-10-12 10:58:21 +080086list(TRANSFORM TEMPLATE_FILES REPLACE "^([^/\\][^:].*)" "${CMAKE_SOURCE_DIR}/\\1")
Raef Colesf42f0882020-07-10 10:01:58 +010087
88parse_field_from_yaml("${GENERATED_FILE_LISTS}" output OUTPUT_FILES)
89# Replace relative paths with absolute paths
Kevin Peng1b8177b2021-12-07 15:06:25 +080090# Paths used in GENERATED_FILE_LISTS are all relative to TF-M root (${CMAKE_SOURCE_DIR})
Ken Liua2ac1f92020-10-12 10:58:21 +080091list(TRANSFORM OUTPUT_FILES REPLACE "^([^/\\][^:].*)" "${CMAKE_BINARY_DIR}/generated/\\1")
Raef Colesf42f0882020-07-10 10:01:58 +010092
Kevin Peng1b8177b2021-12-07 15:06:25 +080093# Each manifest list may have different original path
94# Parse them one by one
95set(INDEX 0)
96foreach(CONFIGURED_LIST ${CONFIGURED_MANIFEST_LISTS})
97 list(GET MANIFEST_LIST_PATHS ${INDEX} PATH_OF_LIST)
98
99 parse_field_from_yaml(${CONFIGURED_LIST} manifest MANIFESTS)
100 foreach(MANIFEST ${MANIFESTS})
101 # The path of each manifest must be absolute path or relative path to
102 # the path of manifest list that holds it
103 if (NOT IS_ABSOLUTE ${MANIFEST})
104 set(MANIFEST "${PATH_OF_LIST}/${MANIFEST}")
105 endif()
106 list(APPEND MANIFEST_FILES ${MANIFEST})
107 endforeach()
108
109 math(EXPR INDEX "${INDEX} + 1")
110endforeach()
Raef Colesf42f0882020-07-10 10:01:58 +0100111
112############################### Command declaration ############################
113
114# Workaround for heap support
115if ("${TEST_PSA_API}" STREQUAL "IPC")
116 execute_process(
117 WORKING_DIRECTORY ${PSA_ARCH_TESTS_PATH}/api-tests
118 COMMAND ${Python3_EXECUTABLE} tools/scripts/manifest_update.py
119 )
120endif()
121
Raef Colese43c0202020-09-28 14:11:53 +0100122add_custom_target(tfm_generated_files
123 SOURCES ${OUTPUT_FILES}
Raef Colesf42f0882020-07-10 10:01:58 +0100124)
125
126add_custom_command(OUTPUT ${OUTPUT_FILES}
127 COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tfm_parse_manifest_list.py
Kevin Peng1b8177b2021-12-07 15:06:25 +0800128 -m ${COMBINED_LIST}
Raef Colesf42f0882020-07-10 10:01:58 +0100129 -f ${GENERATED_FILE_LISTS}
130 -o ${CMAKE_BINARY_DIR}/generated
131 DEPENDS ${TEMPLATE_FILES} ${MANIFEST_FILES}
Kevin Peng1b8177b2021-12-07 15:06:25 +0800132 DEPENDS ${TEMP_MANIFEST_LISTS} ${CONFIGURED_MANIFEST_LISTS} ${GENERATED_FILE_LISTS}
Raef Colesf42f0882020-07-10 10:01:58 +0100133)
134
135# The files need to be generated before cmake will allow them to be used as
136# sources. Due to issue with custom_command scoping the easiest way to do this
Raef Colese43c0202020-09-28 14:11:53 +0100137# is to run the script at cmake-time.
138execute_process(
139 COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tfm_parse_manifest_list.py
Kevin Peng1b8177b2021-12-07 15:06:25 +0800140 -m ${COMBINED_LIST}
Raef Colese43c0202020-09-28 14:11:53 +0100141 -f ${GENERATED_FILE_LISTS}
142 -o ${CMAKE_BINARY_DIR}/generated
143 RESULT_VARIABLE RET
144)
145
146if(NOT RET EQUAL 0)
147 message(FATAL_ERROR "File generation failed")
Raef Colesf42f0882020-07-10 10:01:58 +0100148endif()