Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
Raef Coles | d97a7e7 | 2021-12-10 14:58:06 +0000 | [diff] [blame] | 2 | # Copyright (c) 2020-2022, Arm Limited. All rights reserved. |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
Raef Coles | 6981732 | 2020-10-19 14:14:14 +0100 | [diff] [blame] | 8 | cmake_minimum_required(VERSION 3.15) |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 9 | find_package(Python3) |
| 10 | |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 11 | ############################### Manifest lists declaration ##################### |
| 12 | list(APPEND MANIFEST_LISTS ${TFM_MANIFEST_LIST}) |
David Hu | b269420 | 2021-07-15 14:58:39 +0800 | [diff] [blame] | 13 | |
David Hu | 12f2587 | 2021-08-23 14:55:46 +0800 | [diff] [blame] | 14 | if (TFM_NS_REG_TEST OR TFM_S_REG_TEST) |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 15 | list(APPEND MANIFEST_LISTS ${TFM_TEST_PATH}/secure_fw/tfm_test_manifest_list.yaml) |
Kevin Peng | 65064c5 | 2021-10-27 17:12:17 +0800 | [diff] [blame] | 16 | endif() |
| 17 | |
| 18 | if ("${TEST_PSA_API}" STREQUAL "IPC") |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 19 | # 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 Hu | b269420 | 2021-07-15 14:58:39 +0800 | [diff] [blame] | 23 | endif() |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 24 | |
David Hu | f5f1209 | 2021-07-12 16:11:47 +0800 | [diff] [blame] | 25 | if (TFM_EXTRA_MANIFEST_LIST_FILES) |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 26 | list(APPEND MANIFEST_LISTS ${TFM_EXTRA_MANIFEST_LIST_FILES}) |
David Hu | f5f1209 | 2021-07-12 16:11:47 +0800 | [diff] [blame] | 27 | endif() |
| 28 | |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 29 | ############################### File list declaration ########################## |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 30 | set(GENERATED_FILE_LISTS ${CMAKE_CURRENT_SOURCE_DIR}/tfm_generated_file_list.yaml) |
| 31 | set(GENERATED_FILE_LISTS ${GENERATED_FILE_LISTS} ${TFM_EXTRA_GENERATED_FILE_LIST_PATH}) |
| 32 | |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 33 | ############################### 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 Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 36 | function(parse_field_from_yaml files field output_variable) |
Kevin Peng | 1b8177b | 2021-12-07 15:06:25 +0800 | [diff] [blame] | 37 | set(local_variable "") |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 38 | 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 Peng | 1b8177b | 2021-12-07 15:06:25 +0800 | [diff] [blame] | 47 | list(APPEND local_variable ${temp_variable}) |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 48 | endforeach() |
Kevin Peng | 1b8177b | 2021-12-07 15:06:25 +0800 | [diff] [blame] | 49 | set(${output_variable} ${local_variable} PARENT_SCOPE) |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 50 | endfunction() |
| 51 | |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 52 | ############################### Dependency generation ########################## |
| 53 | # Get all the manifest files from manifest lists |
| 54 | foreach(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() |
| 72 | endforeach() |
| 73 | |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 74 | parse_field_from_yaml("${GENERATED_FILE_LISTS}" template TEMPLATE_FILES) |
| 75 | # Replace relative paths with absolute paths |
Kevin Peng | 1b8177b | 2021-12-07 15:06:25 +0800 | [diff] [blame] | 76 | # Paths used in GENERATED_FILE_LISTS are all relative to TF-M root (${CMAKE_SOURCE_DIR}) |
Ken Liu | a2ac1f9 | 2020-10-12 10:58:21 +0800 | [diff] [blame] | 77 | list(TRANSFORM TEMPLATE_FILES REPLACE "^([^/\\][^:].*)" "${CMAKE_SOURCE_DIR}/\\1") |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 78 | |
| 79 | parse_field_from_yaml("${GENERATED_FILE_LISTS}" output OUTPUT_FILES) |
| 80 | # Replace relative paths with absolute paths |
Kevin Peng | 1b8177b | 2021-12-07 15:06:25 +0800 | [diff] [blame] | 81 | # Paths used in GENERATED_FILE_LISTS are all relative to TF-M root (${CMAKE_SOURCE_DIR}) |
Ken Liu | a2ac1f9 | 2020-10-12 10:58:21 +0800 | [diff] [blame] | 82 | list(TRANSFORM OUTPUT_FILES REPLACE "^([^/\\][^:].*)" "${CMAKE_BINARY_DIR}/generated/\\1") |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 83 | |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 84 | ############################### Generate Manifest config header ################ |
Kevin Peng | 93efad0 | 2022-08-01 17:58:13 +0800 | [diff] [blame^] | 85 | |
| 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 |
| 91 | function(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) |
| 108 | endfunction() |
| 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 |
| 116 | append_manifest_config(MANIFEST_CONFIG_H_CONTENT TFM_ISOLATION_LEVEL STRING) |
| 117 | append_manifest_config(MANIFEST_CONFIG_H_CONTENT CONFIG_TFM_SPM_BACKEND STRING) |
| 118 | |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 119 | parse_field_from_yaml("${MANIFEST_LISTS}" conditional CONDITIONS) |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 120 | foreach(CON ${CONDITIONS}) |
Kevin Peng | 93efad0 | 2022-08-01 17:58:13 +0800 | [diff] [blame^] | 121 | append_manifest_config(MANIFEST_CONFIG_H_CONTENT ${CON} BOOL) |
| 122 | endforeach() |
| 123 | |
| 124 | parse_field_from_yaml("${MANIFEST_FILES}" stack_size STACK_SIZES) |
| 125 | foreach(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() |
| 130 | endforeach() |
| 131 | |
| 132 | parse_field_from_yaml("${MANIFEST_FILES}" heap_size HEAP_SIZES) |
| 133 | foreach(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 Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 137 | endif() |
Kevin Peng | 1b8177b | 2021-12-07 15:06:25 +0800 | [diff] [blame] | 138 | endforeach() |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 139 | |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 140 | # Generate the config header |
| 141 | file(WRITE |
| 142 | ${CMAKE_CURRENT_BINARY_DIR}/manifest_config.h.in |
| 143 | ${MANIFEST_CONFIG_H_CONTENT}) |
| 144 | |
| 145 | configure_file(${CMAKE_CURRENT_BINARY_DIR}/manifest_config.h.in |
| 146 | ${CMAKE_CURRENT_BINARY_DIR}/manifest_config.h) |
| 147 | |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 148 | ############################### Command declaration ############################ |
| 149 | |
| 150 | # Workaround for heap support |
| 151 | if ("${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 | ) |
| 156 | endif() |
| 157 | |
Raef Coles | e43c020 | 2020-09-28 14:11:53 +0100 | [diff] [blame] | 158 | add_custom_target(tfm_generated_files |
| 159 | SOURCES ${OUTPUT_FILES} |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 160 | ) |
| 161 | |
Kevin Peng | c32279d | 2022-02-10 11:11:55 +0800 | [diff] [blame] | 162 | if (CONFIG_TFM_PARSE_MANIFEST_QUIET) |
Jimmy Brisson | 89d4f8d | 2021-06-23 10:17:36 -0500 | [diff] [blame] | 163 | set(PARSE_MANIFEST_QUIET_FLAG "-q") |
| 164 | else() |
| 165 | set(PARSE_MANIFEST_QUIET_FLAG "") |
| 166 | endif() |
| 167 | |
Kevin Peng | 3dd051c | 2022-07-13 11:02:03 +0800 | [diff] [blame] | 168 | set(MANIFEST_COMMAND |
| 169 | ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tfm_parse_manifest_list.py |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 170 | -m ${MANIFEST_LISTS} |
Kevin Peng | 3dd051c | 2022-07-13 11:02:03 +0800 | [diff] [blame] | 171 | -f ${GENERATED_FILE_LISTS} |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 172 | -c ${CMAKE_CURRENT_BINARY_DIR}/manifest_config.h |
Kevin Peng | 3dd051c | 2022-07-13 11:02:03 +0800 | [diff] [blame] | 173 | -o ${CMAKE_BINARY_DIR}/generated |
| 174 | ${PARSE_MANIFEST_QUIET_FLAG}) |
| 175 | |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 176 | add_custom_command(OUTPUT ${OUTPUT_FILES} |
Kevin Peng | 3dd051c | 2022-07-13 11:02:03 +0800 | [diff] [blame] | 177 | COMMAND ${MANIFEST_COMMAND} |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 178 | DEPENDS ${TEMPLATE_FILES} ${MANIFEST_FILES} |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 179 | DEPENDS ${MANIFEST_LISTS} ${GENERATED_FILE_LISTS} |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 180 | ) |
| 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 Coles | e43c020 | 2020-09-28 14:11:53 +0100 | [diff] [blame] | 184 | # is to run the script at cmake-time. |
| 185 | execute_process( |
Kevin Peng | 3dd051c | 2022-07-13 11:02:03 +0800 | [diff] [blame] | 186 | COMMAND ${MANIFEST_COMMAND} |
Raef Coles | e43c020 | 2020-09-28 14:11:53 +0100 | [diff] [blame] | 187 | RESULT_VARIABLE RET |
| 188 | ) |
| 189 | |
Sherry Zhang | f58f2bd | 2022-01-10 17:21:11 +0800 | [diff] [blame] | 190 | if(RET EQUAL 0) |
| 191 | include(${CMAKE_BINARY_DIR}/generated/tools/config_impl.cmake) |
| 192 | else() |
Kevin Peng | fb1761b | 2022-05-12 12:11:31 +0800 | [diff] [blame] | 193 | message(FATAL_ERROR "Manifest tool failed to generate files!") |
Raef Coles | f42f088 | 2020-07-10 10:01:58 +0100 | [diff] [blame] | 194 | endif() |