blob: d0c4c018b3691cae6630f7a7c60464d68fb6db7f [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
Kevin Pengfb1761b2022-05-12 12:11:31 +080011############################### Manifest lists declaration #####################
12list(APPEND MANIFEST_LISTS ${TFM_MANIFEST_LIST})
David Hub2694202021-07-15 14:58:39 +080013
David Hu12f25872021-08-23 14:55:46 +080014if (TFM_NS_REG_TEST OR TFM_S_REG_TEST)
Kevin Pengfb1761b2022-05-12 12:11:31 +080015 list(APPEND MANIFEST_LISTS ${TFM_TEST_PATH}/secure_fw/tfm_test_manifest_list.yaml)
Kevin Peng65064c52021-10-27 17:12:17 +080016endif()
17
18if ("${TEST_PSA_API}" STREQUAL "IPC")
Kevin Pengfb1761b2022-05-12 12:11:31 +080019 # The manifest tool does not recognize CMake varibles. Do configure_file() first.
20 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tfm_psa_ff_test_manifest_list.yaml
21 ${CMAKE_CURRENT_BINARY_DIR}/tfm_psa_ff_test_manifest_list.yaml)
22 list(APPEND MANIFEST_LISTS ${CMAKE_CURRENT_BINARY_DIR}/tfm_psa_ff_test_manifest_list.yaml)
David Hub2694202021-07-15 14:58:39 +080023endif()
Raef Colesf42f0882020-07-10 10:01:58 +010024
David Huf5f12092021-07-12 16:11:47 +080025if (TFM_EXTRA_MANIFEST_LIST_FILES)
Kevin Pengfb1761b2022-05-12 12:11:31 +080026 list(APPEND MANIFEST_LISTS ${TFM_EXTRA_MANIFEST_LIST_FILES})
David Huf5f12092021-07-12 16:11:47 +080027endif()
28
Raef Colesf42f0882020-07-10 10:01:58 +010029############################### File list declaration ##########################
Raef Colesf42f0882020-07-10 10:01:58 +010030set(GENERATED_FILE_LISTS ${CMAKE_CURRENT_SOURCE_DIR}/tfm_generated_file_list.yaml)
31set(GENERATED_FILE_LISTS ${GENERATED_FILE_LISTS} ${TFM_EXTRA_GENERATED_FILE_LIST_PATH})
32
Kevin Pengfb1761b2022-05-12 12:11:31 +080033############################### Functions declaration ##########################
34# Parses the given YAML "files" to find out all the items of the given "field"
35# and put them to the "output_variable" as a list.
Raef Colesf42f0882020-07-10 10:01:58 +010036function(parse_field_from_yaml files field output_variable)
Kevin Peng1b8177b2021-12-07 15:06:25 +080037 set(local_variable "")
Raef Colesf42f0882020-07-10 10:01:58 +010038 foreach(yaml_file ${files})
39 # Load the lines that refer to the key we selected
40 file(STRINGS ${yaml_file} temp_variable REGEX " *\"${field}\":")
41 # Take only the value of the key
42 list(TRANSFORM temp_variable REPLACE " *\"${field}\": *" ";")
43 # Remove all commas
44 list(TRANSFORM temp_variable REPLACE "," "")
45 # Remove all quote marks
46 list(TRANSFORM temp_variable REPLACE "\"" "")
Kevin Peng1b8177b2021-12-07 15:06:25 +080047 list(APPEND local_variable ${temp_variable})
Raef Colesf42f0882020-07-10 10:01:58 +010048 endforeach()
Kevin Peng1b8177b2021-12-07 15:06:25 +080049 set(${output_variable} ${local_variable} PARENT_SCOPE)
Raef Colesf42f0882020-07-10 10:01:58 +010050endfunction()
51
Kevin Pengfb1761b2022-05-12 12:11:31 +080052############################### Dependency generation ##########################
53# Get all the manifest files from manifest lists
54foreach(MANIFEST_LIST ${MANIFEST_LISTS})
55 if (NOT EXISTS ${MANIFEST_LIST})
56 message(FATAL_ERROR "Manifest list ${MANIFEST_LIST} doesn't exist")
57 endif()
58
59 # Get the path of the manifest list
60 get_filename_component(MANIFEST_LIST_PATH ${MANIFEST_LIST} DIRECTORY)
61
62 # Get all the "manifest"
63 parse_field_from_yaml(${MANIFEST_LIST} manifest MANIFESTS)
64
65 foreach(MANIFEST ${MANIFESTS})
66 # Convert to absolute paths
67 if (NOT IS_ABSOLUTE ${MANIFEST})
68 get_filename_component(MANIFEST "${MANIFEST_LIST_PATH}/${MANIFEST}" ABSOLUTE)
69 endif()
70 list(APPEND MANIFEST_FILES ${MANIFEST})
71 endforeach()
72endforeach()
73
Raef Colesf42f0882020-07-10 10:01:58 +010074parse_field_from_yaml("${GENERATED_FILE_LISTS}" template TEMPLATE_FILES)
75# Replace relative paths with absolute paths
Kevin Peng1b8177b2021-12-07 15:06:25 +080076# Paths used in GENERATED_FILE_LISTS are all relative to TF-M root (${CMAKE_SOURCE_DIR})
Ken Liua2ac1f92020-10-12 10:58:21 +080077list(TRANSFORM TEMPLATE_FILES REPLACE "^([^/\\][^:].*)" "${CMAKE_SOURCE_DIR}/\\1")
Raef Colesf42f0882020-07-10 10:01:58 +010078
79parse_field_from_yaml("${GENERATED_FILE_LISTS}" output OUTPUT_FILES)
80# Replace relative paths with absolute paths
Kevin Peng1b8177b2021-12-07 15:06:25 +080081# Paths used in GENERATED_FILE_LISTS are all relative to TF-M root (${CMAKE_SOURCE_DIR})
Ken Liua2ac1f92020-10-12 10:58:21 +080082list(TRANSFORM OUTPUT_FILES REPLACE "^([^/\\][^:].*)" "${CMAKE_BINARY_DIR}/generated/\\1")
Raef Colesf42f0882020-07-10 10:01:58 +010083
Kevin Pengfb1761b2022-05-12 12:11:31 +080084############################### Generate Manifest config header ################
Kevin Peng93efad02022-08-01 17:58:13 +080085
86# The function appends the given `config` to the `out_var` variable.
87# Supported `type` are [BOOL, STRING].
88# The format of contents appended is
89# #cmakedefine01 config for BOOL types
90# #cmakedefine config @config@ for STRING types
91function(append_manifest_config out_var config type)
92 # Operate on a local var and write back to the out_var later
93 set(local_var ${${out_var}})
94
95 # Avoid duplications of configs
96 string(FIND "${local_var}" ${config} config_exists)
97 if(${config_exists} EQUAL -1) # Not found
98 if (${type} STREQUAL "BOOL")
99 string(APPEND local_var "#cmakedefine01 ${config}\r\n")
100 elseif(${type} STREQUAL "STRING")
101 string(APPEND local_var "#cmakedefine ${config} @${config}@\r\n")
102 else()
103 message(FATAL_ERROR "Unsupported config type: ${type}")
104 endif()
105 endif()
106
107 set(${out_var} ${local_var} PARENT_SCOPE)
108endfunction()
109
110# The following build configurations are required to pass to manifest tool via the config header
111# - The isolation level
112# - The SPM backend
113# - "conditional" attributes for every Secure Partition in manifest lists
114# - "stack_size" in manifests
115# - "heap_size" in manifests
116append_manifest_config(MANIFEST_CONFIG_H_CONTENT TFM_ISOLATION_LEVEL STRING)
117append_manifest_config(MANIFEST_CONFIG_H_CONTENT CONFIG_TFM_SPM_BACKEND STRING)
118
Kevin Pengfb1761b2022-05-12 12:11:31 +0800119parse_field_from_yaml("${MANIFEST_LISTS}" conditional CONDITIONS)
Kevin Pengfb1761b2022-05-12 12:11:31 +0800120foreach(CON ${CONDITIONS})
Kevin Peng93efad02022-08-01 17:58:13 +0800121 append_manifest_config(MANIFEST_CONFIG_H_CONTENT ${CON} BOOL)
122endforeach()
123
124parse_field_from_yaml("${MANIFEST_FILES}" stack_size STACK_SIZES)
125foreach(STK_SIZE ${STACK_SIZES})
126 if (NOT STK_SIZE GREATER 0)
127 # The "stack_size" might not be a valid number. Treat it as a configuration
128 append_manifest_config(MANIFEST_CONFIG_H_CONTENT ${STK_SIZE} STRING)
129 endif()
130endforeach()
131
132parse_field_from_yaml("${MANIFEST_FILES}" heap_size HEAP_SIZES)
133foreach(HEAP_SIZE ${HEAP_SIZES})
134 if (NOT HEAP_SIZE GREATER 0)
135 # The "heap_size" might not be a valid number. Treat it as a configuration
136 append_manifest_config(MANIFEST_CONFIG_H_CONTENT ${HEAP_SIZE} STRING)
Kevin Pengfb1761b2022-05-12 12:11:31 +0800137 endif()
Kevin Peng1b8177b2021-12-07 15:06:25 +0800138endforeach()
Raef Colesf42f0882020-07-10 10:01:58 +0100139
Kevin Pengfb1761b2022-05-12 12:11:31 +0800140# Generate the config header
141file(WRITE
142 ${CMAKE_CURRENT_BINARY_DIR}/manifest_config.h.in
143 ${MANIFEST_CONFIG_H_CONTENT})
144
145configure_file(${CMAKE_CURRENT_BINARY_DIR}/manifest_config.h.in
146 ${CMAKE_CURRENT_BINARY_DIR}/manifest_config.h)
147
Raef Colesf42f0882020-07-10 10:01:58 +0100148############################### Command declaration ############################
149
150# Workaround for heap support
151if ("${TEST_PSA_API}" STREQUAL "IPC")
152 execute_process(
153 WORKING_DIRECTORY ${PSA_ARCH_TESTS_PATH}/api-tests
154 COMMAND ${Python3_EXECUTABLE} tools/scripts/manifest_update.py
155 )
156endif()
157
Raef Colese43c0202020-09-28 14:11:53 +0100158add_custom_target(tfm_generated_files
159 SOURCES ${OUTPUT_FILES}
Raef Colesf42f0882020-07-10 10:01:58 +0100160)
161
Kevin Pengc32279d2022-02-10 11:11:55 +0800162if (CONFIG_TFM_PARSE_MANIFEST_QUIET)
Jimmy Brisson89d4f8d2021-06-23 10:17:36 -0500163 set(PARSE_MANIFEST_QUIET_FLAG "-q")
164else()
165 set(PARSE_MANIFEST_QUIET_FLAG "")
166endif()
167
Kevin Peng3dd051c2022-07-13 11:02:03 +0800168set(MANIFEST_COMMAND
169 ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tfm_parse_manifest_list.py
Kevin Pengfb1761b2022-05-12 12:11:31 +0800170 -m ${MANIFEST_LISTS}
Kevin Peng3dd051c2022-07-13 11:02:03 +0800171 -f ${GENERATED_FILE_LISTS}
Kevin Pengfb1761b2022-05-12 12:11:31 +0800172 -c ${CMAKE_CURRENT_BINARY_DIR}/manifest_config.h
Kevin Peng3dd051c2022-07-13 11:02:03 +0800173 -o ${CMAKE_BINARY_DIR}/generated
174 ${PARSE_MANIFEST_QUIET_FLAG})
175
Raef Colesf42f0882020-07-10 10:01:58 +0100176add_custom_command(OUTPUT ${OUTPUT_FILES}
Kevin Peng3dd051c2022-07-13 11:02:03 +0800177 COMMAND ${MANIFEST_COMMAND}
Raef Colesf42f0882020-07-10 10:01:58 +0100178 DEPENDS ${TEMPLATE_FILES} ${MANIFEST_FILES}
Kevin Pengfb1761b2022-05-12 12:11:31 +0800179 DEPENDS ${MANIFEST_LISTS} ${GENERATED_FILE_LISTS}
Raef Colesf42f0882020-07-10 10:01:58 +0100180)
181
182# The files need to be generated before cmake will allow them to be used as
183# sources. Due to issue with custom_command scoping the easiest way to do this
Raef Colese43c0202020-09-28 14:11:53 +0100184# is to run the script at cmake-time.
185execute_process(
Kevin Peng3dd051c2022-07-13 11:02:03 +0800186 COMMAND ${MANIFEST_COMMAND}
Raef Colese43c0202020-09-28 14:11:53 +0100187 RESULT_VARIABLE RET
188)
189
Sherry Zhangf58f2bd2022-01-10 17:21:11 +0800190if(RET EQUAL 0)
191 include(${CMAKE_BINARY_DIR}/generated/tools/config_impl.cmake)
192else()
Kevin Pengfb1761b2022-05-12 12:11:31 +0800193 message(FATAL_ERROR "Manifest tool failed to generate files!")
Raef Colesf42f0882020-07-10 10:01:58 +0100194endif()