Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | """ tfm_build_manager.py: |
| 4 | |
| 5 | Controlling class managing multiple build configruations for tfm """ |
| 6 | |
| 7 | from __future__ import print_function |
Xinyu Zhang | 433771e | 2022-04-01 16:49:17 +0800 | [diff] [blame] | 8 | from json import tool |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 9 | |
| 10 | __copyright__ = """ |
| 11 | /* |
Feder Liang | 357b160 | 2022-01-11 16:47:49 +0800 | [diff] [blame] | 12 | * Copyright (c) 2018-2022, Arm Limited. All rights reserved. |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 13 | * |
| 14 | * SPDX-License-Identifier: BSD-3-Clause |
| 15 | * |
| 16 | */ |
| 17 | """ |
Karl Zhang | 08681e6 | 2020-10-30 13:56:03 +0800 | [diff] [blame] | 18 | |
| 19 | __author__ = "tf-m@lists.trustedfirmware.org" |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 20 | __project__ = "Trusted Firmware-M Open CI" |
Xinyu Zhang | 06286a9 | 2021-07-22 14:00:51 +0800 | [diff] [blame] | 21 | __version__ = "1.4.0" |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 22 | |
| 23 | import os |
| 24 | import sys |
Karl Zhang | aff558a | 2020-05-15 14:28:23 +0100 | [diff] [blame] | 25 | from .utils import * |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 26 | from time import time |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 27 | from copy import deepcopy |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 28 | from .structured_task import structuredTask |
| 29 | from .tfm_builder import TFM_Builder |
Xinyu Zhang | 1fa7f98 | 2022-04-20 17:46:17 +0800 | [diff] [blame] | 30 | from build_helper.build_helper_config_maps import * |
Xinyu Zhang | fd2e115 | 2021-12-17 18:09:01 +0800 | [diff] [blame] | 31 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 32 | class TFM_Build_Manager(structuredTask): |
| 33 | """ Class that will load a configuration out of a json file, schedule |
| 34 | the builds, and produce a report """ |
| 35 | |
| 36 | def __init__(self, |
| 37 | tfm_dir, # TFM root directory |
| 38 | work_dir, # Current working directory(ie logs) |
| 39 | cfg_dict, # Input config dictionary of the following form |
| 40 | # input_dict = {"PROJ_CONFIG": "ConfigRegression", |
| 41 | # "TARGET_PLATFORM": "MUSCA_A", |
| 42 | # "COMPILER": "ARMCLANG", |
| 43 | # "CMAKE_BUILD_TYPE": "Debug"} |
| 44 | report=None, # File to produce report |
| 45 | parallel_builds=3, # Number of builds to run in parallel |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 46 | build_threads=3, # Number of threads used per build |
| 47 | install=False, # Install libraries after build |
| 48 | img_sizes=False, # Use arm-none-eabi-size for size info |
| 49 | relative_paths=False): # Store relative paths in report |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 50 | self._tbm_build_threads = build_threads |
| 51 | self._tbm_conc_builds = parallel_builds |
| 52 | self._tbm_install = install |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 53 | self._tbm_img_sizes = img_sizes |
| 54 | self._tbm_relative_paths = relative_paths |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 55 | |
| 56 | # Required by other methods, always set working directory first |
| 57 | self._tbm_work_dir = os.path.abspath(os.path.expanduser(work_dir)) |
| 58 | |
| 59 | self._tbm_tfm_dir = os.path.abspath(os.path.expanduser(tfm_dir)) |
| 60 | |
Karl Zhang | aff558a | 2020-05-15 14:28:23 +0100 | [diff] [blame] | 61 | print("bm param tfm_dir %s" % tfm_dir) |
| 62 | print("bm %s %s %s" % (work_dir, cfg_dict, self._tbm_work_dir)) |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 63 | # Internal flag to tag simple (non combination formatted configs) |
| 64 | self.simple_config = False |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 65 | self._tbm_report = report |
| 66 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 67 | self._tbm_cfg = self.load_config(cfg_dict, self._tbm_work_dir) |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 68 | self._tbm_build_cfg, \ |
| 69 | self.tbm_common_cfg = self.parse_config(self._tbm_cfg) |
Karl Zhang | aff558a | 2020-05-15 14:28:23 +0100 | [diff] [blame] | 70 | self._tfb_code_base_updated = False |
| 71 | self._tfb_log_f = "CodeBasePrepare.log" |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 72 | |
| 73 | super(TFM_Build_Manager, self).__init__(name="TFM_Build_Manager") |
| 74 | |
Xinyu Zhang | 8558852 | 2023-10-31 13:58:04 +0800 | [diff] [blame^] | 75 | def choose_toolchain(self, compiler, s_build): |
Xinyu Zhang | 433771e | 2022-04-01 16:49:17 +0800 | [diff] [blame] | 76 | toolchain = "" |
Xinyu Zhang | 8558852 | 2023-10-31 13:58:04 +0800 | [diff] [blame^] | 77 | if s_build: |
| 78 | if "GCC"in compiler: |
| 79 | toolchain = "toolchain_GNUARM.cmake" |
| 80 | elif "ARMCLANG" in compiler: |
| 81 | toolchain = "toolchain_ARMCLANG.cmake" |
| 82 | else: |
| 83 | if "GCC"in compiler: |
| 84 | toolchain = "toolchain_ns_GNUARM.cmake" |
| 85 | elif "ARMCLANG" in compiler: |
| 86 | toolchain = "toolchain_ns_ARMCLANG.cmake" |
Xinyu Zhang | ff5d771 | 2022-01-14 13:48:59 +0800 | [diff] [blame] | 87 | |
Xinyu Zhang | 433771e | 2022-04-01 16:49:17 +0800 | [diff] [blame] | 88 | return toolchain |
| 89 | |
| 90 | def get_compiler_name(self, compiler): |
| 91 | compiler_name = "" |
| 92 | if "GCC"in compiler: |
| 93 | compiler_name = "arm-none-eabi-gcc" |
| 94 | elif "ARMCLANG" in compiler: |
| 95 | compiler_name = "armclang" |
| 96 | |
| 97 | return compiler_name |
Xinyu Zhang | ff5d771 | 2022-01-14 13:48:59 +0800 | [diff] [blame] | 98 | |
Xinyu Zhang | b18ae74 | 2023-04-25 14:33:27 +0800 | [diff] [blame] | 99 | def map_params(self, params, maps): |
| 100 | build_configs = "" |
Xinyu Zhang | fc061dd | 2022-07-26 14:52:56 +0800 | [diff] [blame] | 101 | param_list = params.split(", ") |
| 102 | for param in param_list: |
Xinyu Zhang | b18ae74 | 2023-04-25 14:33:27 +0800 | [diff] [blame] | 103 | build_configs += maps[param] |
| 104 | return build_configs |
Xinyu Zhang | fc061dd | 2022-07-26 14:52:56 +0800 | [diff] [blame] | 105 | |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 106 | def get_config(self): |
| 107 | return list(self._tbm_build_cfg.keys()) |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 108 | |
Xinyu Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 109 | def get_build_configs(self, config, silence_stderr=False): |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 110 | """ |
Xinyu Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 111 | Return build config variables needed by the input config. |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 112 | """ |
| 113 | if config not in self._tbm_build_cfg: |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 114 | if not silence_stderr: |
| 115 | print("Error: no such config {}".format(config), file=sys.stderr) |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 116 | sys.exit(1) |
| 117 | config_details = self._tbm_build_cfg[config] |
Xinyu Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 118 | config_params = { |
| 119 | "CONFIG_NAME": config, |
| 120 | "TFM_PLATFORM": config_details.tfm_platform, |
| 121 | "COMPILER": config_details.compiler, |
| 122 | "ISOLATION_LEVEL": config_details.isolation_level, |
| 123 | "TEST_REGRESSION": config_details.test_regression, |
| 124 | "TEST_PSA_API": config_details.test_psa_api, |
| 125 | "CMAKE_BUILD_TYPE": config_details.cmake_build_type, |
| 126 | "BL2": config_details.with_bl2, |
| 127 | "PROFILE": "N.A" if not config_details.profile else config_details.profile, |
| 128 | "EXTRA_PARAMS": "N.A" if not config_details.extra_params else config_details.extra_params, |
| 129 | } |
| 130 | return config_params |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 131 | |
Xinyu Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 132 | def get_build_commands(self, config, silence_stderr=False, jobs=None): |
| 133 | """ |
| 134 | Return selected type of commands to be run to build the input config. |
| 135 | """ |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 136 | config_details = self._tbm_build_cfg[config] |
| 137 | codebase_dir = os.path.join(os.getcwd(),"trusted-firmware-m") |
Xinyu Zhang | 433771e | 2022-04-01 16:49:17 +0800 | [diff] [blame] | 138 | build_config = self.get_build_config(config_details, config, \ |
| 139 | silence=silence_stderr, \ |
Paul Sokolovsky | cba7ee4 | 2023-04-19 13:21:33 +0300 | [diff] [blame] | 140 | codebase_dir=codebase_dir, \ |
| 141 | jobs=jobs) |
Xinyu Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 142 | build_commands = { |
Xinyu Zhang | a88a2eb | 2023-08-15 17:43:51 +0800 | [diff] [blame] | 143 | 'set_compiler': build_config['set_compiler_path'], |
| 144 | 'spe_cmake_config': build_config['spe_config_template'], |
| 145 | 'nspe_cmake_config': build_config['nspe_config_template'], |
| 146 | 'spe_cmake_build': build_config['spe_cmake_build'], |
| 147 | 'nspe_cmake_build': build_config['nspe_cmake_build'], |
| 148 | 'post_build': build_config['post_build'] |
Xinyu Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 149 | } |
| 150 | return build_commands |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 151 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 152 | def pre_eval(self): |
| 153 | """ Tests that need to be run in set-up state """ |
| 154 | return True |
| 155 | |
| 156 | def pre_exec(self, eval_ret): |
| 157 | """ """ |
| 158 | |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 159 | def override_tbm_cfg_params(self, config, override_keys, **params): |
| 160 | """ Using a dictionay as input, for each key defined in |
| 161 | override_keys it will replace the config[key] entries with |
| 162 | the key=value parameters provided """ |
| 163 | |
| 164 | for key in override_keys: |
| 165 | if isinstance(config[key], list): |
| 166 | config[key] = [n % params for n in config[key]] |
| 167 | elif isinstance(config[key], str): |
| 168 | config[key] = config[key] % params |
| 169 | else: |
| 170 | raise Exception("Config does not contain key %s " |
| 171 | "of type %s" % (key, config[key])) |
| 172 | return config |
| 173 | |
Karl Zhang | aff558a | 2020-05-15 14:28:23 +0100 | [diff] [blame] | 174 | def pre_build(self, build_cfg): |
| 175 | print("pre_build start %s \r\nself._tfb_cfg %s\r\n" % |
| 176 | (self, build_cfg)) |
| 177 | |
| 178 | try: |
| 179 | if self._tfb_code_base_updated: |
| 180 | print("Code base has been updated") |
| 181 | return True |
| 182 | |
| 183 | self._tfb_code_base_updated = True |
| 184 | |
| 185 | if "build_psa_api" in build_cfg: |
| 186 | # FF IPC build needs repo manifest update for TFM and PSA arch test |
| 187 | if "build_ff_ipc" in build_cfg: |
| 188 | print("Checkout to FF IPC code base") |
| 189 | os.chdir(build_cfg["codebase_root_dir"] + "/../psa-arch-tests/api-tests") |
| 190 | _api_test_manifest = "git checkout . ; python3 tools/scripts/manifest_update.py" |
| 191 | if subprocess_log(_api_test_manifest, |
| 192 | self._tfb_log_f, |
| 193 | append=True, |
| 194 | prefix=_api_test_manifest): |
| 195 | |
| 196 | raise Exception("Python Failed please check log: %s" % |
| 197 | self._tfb_log_f) |
| 198 | |
| 199 | _api_test_manifest_tfm = "python3 tools/tfm_parse_manifest_list.py -m tools/tfm_psa_ff_test_manifest_list.yaml append" |
| 200 | os.chdir(build_cfg["codebase_root_dir"]) |
| 201 | if subprocess_log(_api_test_manifest_tfm, |
| 202 | self._tfb_log_f, |
| 203 | append=True, |
| 204 | prefix=_api_test_manifest_tfm): |
| 205 | |
| 206 | raise Exception("Python TFM Failed please check log: %s" % |
| 207 | self._tfb_log_f) |
| 208 | return True |
| 209 | |
| 210 | print("Checkout to default code base") |
| 211 | os.chdir(build_cfg["codebase_root_dir"] + "/../psa-arch-tests/api-tests") |
| 212 | _api_test_manifest = "git checkout ." |
| 213 | if subprocess_log(_api_test_manifest, |
| 214 | self._tfb_log_f, |
| 215 | append=True, |
| 216 | prefix=_api_test_manifest): |
| 217 | |
| 218 | raise Exception("Python Failed please check log: %s" % |
| 219 | self._tfb_log_f) |
| 220 | |
| 221 | _api_test_manifest_tfm = "python3 tools/tfm_parse_manifest_list.py" |
| 222 | os.chdir(build_cfg["codebase_root_dir"]) |
| 223 | if subprocess_log(_api_test_manifest_tfm, |
| 224 | self._tfb_log_f, |
| 225 | append=True, |
| 226 | prefix=_api_test_manifest_tfm): |
| 227 | |
| 228 | raise Exception("Python TFM Failed please check log: %s" % |
| 229 | self._tfb_log_f) |
| 230 | finally: |
| 231 | print("python pass after builder prepare") |
| 232 | os.chdir(build_cfg["codebase_root_dir"] + "/../") |
| 233 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 234 | def task_exec(self): |
| 235 | """ Create a build pool and execute them in parallel """ |
| 236 | |
| 237 | build_pool = [] |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 238 | |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 239 | # When a config is flagged as a single build config. |
| 240 | # Name is evaluated by config type |
| 241 | if self.simple_config: |
| 242 | |
| 243 | build_cfg = deepcopy(self.tbm_common_cfg) |
| 244 | |
| 245 | # Extract the common for all elements of config |
Xinyu Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 246 | try: |
| 247 | build_cfg["required_artefacts"] = build_cfg["required_artefacts"]["all"] |
| 248 | except KeyError: |
| 249 | build_cfg["required_artefacts"] = [] |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 250 | name = build_cfg["config_type"] |
| 251 | |
| 252 | # Override _tbm_xxx paths in commands |
| 253 | # plafrom in not guaranteed without seeds so _tbm_target_platform |
| 254 | # is ignored |
| 255 | over_dict = {"_tbm_build_dir_": os.path.join(self._tbm_work_dir, |
| 256 | name), |
| 257 | "_tbm_code_dir_": build_cfg["codebase_root_dir"]} |
| 258 | |
| 259 | build_cfg = self.override_tbm_cfg_params(build_cfg, |
Xinyu Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 260 | ["post_build", |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 261 | "artifact_capture_rex"], |
| 262 | **over_dict) |
| 263 | |
| 264 | # Overrides path in expected artefacts |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 265 | print("Loading config %s" % name) |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 266 | |
| 267 | build_pool.append(TFM_Builder( |
| 268 | name=name, |
| 269 | work_dir=self._tbm_work_dir, |
| 270 | cfg_dict=build_cfg, |
| 271 | build_threads=self._tbm_build_threads, |
| 272 | img_sizes=self._tbm_img_sizes, |
| 273 | relative_paths=self._tbm_relative_paths)) |
| 274 | # When a seed pool is provided iterate through the entries |
| 275 | # and update platform spefific parameters |
| 276 | elif len(self._tbm_build_cfg): |
Karl Zhang | aff558a | 2020-05-15 14:28:23 +0100 | [diff] [blame] | 277 | print("\r\n_tbm_build_cfg %s\r\n tbm_common_cfg %s\r\n" \ |
| 278 | % (self._tbm_build_cfg, self.tbm_common_cfg)) |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 279 | for name, i in self._tbm_build_cfg.items(): |
| 280 | # Do not modify the original config |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 281 | build_cfg = self.get_build_config(i, name) |
Karl Zhang | aff558a | 2020-05-15 14:28:23 +0100 | [diff] [blame] | 282 | self.pre_build(build_cfg) |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 283 | # Overrides path in expected artefacts |
| 284 | print("Loading config %s" % name) |
| 285 | |
| 286 | build_pool.append(TFM_Builder( |
| 287 | name=name, |
| 288 | work_dir=self._tbm_work_dir, |
| 289 | cfg_dict=build_cfg, |
| 290 | build_threads=self._tbm_build_threads, |
| 291 | img_sizes=self._tbm_img_sizes, |
| 292 | relative_paths=self._tbm_relative_paths)) |
| 293 | else: |
| 294 | print("Could not find any configuration. Check the rejection list") |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 295 | |
| 296 | status_rep = {} |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 297 | build_rep = {} |
| 298 | completed_build_count = 0 |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 299 | print("Build: Running %d parallel build jobs" % self._tbm_conc_builds) |
| 300 | for build_pool_slice in list_chunks(build_pool, self._tbm_conc_builds): |
| 301 | |
| 302 | # Start the builds |
| 303 | for build in build_pool_slice: |
| 304 | # Only produce output for the first build |
| 305 | if build_pool_slice.index(build) != 0: |
| 306 | build.mute() |
| 307 | print("Build: Starting %s" % build.get_name()) |
| 308 | build.start() |
| 309 | |
| 310 | # Wait for the builds to complete |
| 311 | for build in build_pool_slice: |
| 312 | # Wait for build to finish |
| 313 | build.join() |
| 314 | # Similarly print the logs of the other builds as they complete |
| 315 | if build_pool_slice.index(build) != 0: |
| 316 | build.log() |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 317 | completed_build_count += 1 |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 318 | print("Build: Finished %s" % build.get_name()) |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 319 | print("Build Progress:") |
| 320 | show_progress(completed_build_count, len(build_pool)) |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 321 | |
| 322 | # Store status in report |
| 323 | status_rep[build.get_name()] = build.get_status() |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 324 | build_rep[build.get_name()] = build.report() |
| 325 | |
| 326 | # Include the original input configuration in the report |
| 327 | |
| 328 | metadata = {"input_build_cfg": self._tbm_cfg, |
| 329 | "build_dir": self._tbm_work_dir |
| 330 | if not self._tbm_relative_paths |
| 331 | else resolve_rel_path(self._tbm_work_dir), |
| 332 | "time": time()} |
| 333 | |
| 334 | full_rep = {"report": build_rep, |
| 335 | "_metadata_": metadata} |
| 336 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 337 | # Store the report |
| 338 | self.stash("Build Status", status_rep) |
| 339 | self.stash("Build Report", full_rep) |
| 340 | |
| 341 | if self._tbm_report: |
| 342 | print("Exported build report to file:", self._tbm_report) |
| 343 | save_json(self._tbm_report, full_rep) |
| 344 | |
Xinyu Zhang | a88a2eb | 2023-08-15 17:43:51 +0800 | [diff] [blame] | 345 | def get_build_config(self, i, name, silence=False, codebase_dir=None, jobs=None): |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 346 | build_cfg = deepcopy(self.tbm_common_cfg) |
| 347 | if not codebase_dir: |
| 348 | codebase_dir = build_cfg["codebase_root_dir"] |
| 349 | else: |
| 350 | # Would prefer to do all with the new variable |
| 351 | # However, many things use this from build_cfg elsewhere |
| 352 | build_cfg["codebase_root_dir"] = codebase_dir |
| 353 | # Extract the common for all elements of config |
Xinyu Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 354 | try: |
| 355 | build_cfg["required_artefacts"] = deepcopy(self.tbm_common_cfg["required_artefacts"]["all"]) |
| 356 | except KeyError as E: |
| 357 | build_cfg["required_artefacts"] = [] |
| 358 | build_cfg["post_build"] = "" |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 359 | # Extract the platform specific elements of config |
Xinyu Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 360 | for key in ["post_build", "required_artefacts"]: |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 361 | try: |
Xinyu Zhang | fb80b5d | 2022-07-26 15:42:26 +0800 | [diff] [blame] | 362 | if i.tfm_platform in self.tbm_common_cfg[key].keys(): |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 363 | build_cfg[key] += deepcopy(self.tbm_common_cfg[key] |
Xinyu Zhang | b708f57 | 2020-09-15 11:43:46 +0800 | [diff] [blame] | 364 | [i.tfm_platform]) |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 365 | except Exception as E: |
| 366 | pass |
Karl Zhang | 1eed632 | 2020-07-01 15:38:10 +0800 | [diff] [blame] | 367 | |
Paul Sokolovsky | cba7ee4 | 2023-04-19 13:21:33 +0300 | [diff] [blame] | 368 | if jobs is None: |
| 369 | if os.cpu_count() >= 8: |
| 370 | #run in a serviver with scripts, parallel build will use CPU numbers |
| 371 | jobs = 2 |
| 372 | else: |
| 373 | #run in a docker, usually docker with CPUs less than 8 |
| 374 | jobs = os.cpu_count() |
| 375 | |
| 376 | thread_no = " -j {} ".format(jobs) |
Xinyu Zhang | a88a2eb | 2023-08-15 17:43:51 +0800 | [diff] [blame] | 377 | build_cfg["spe_cmake_build"] += thread_no |
| 378 | build_cfg["nspe_cmake_build"] += thread_no |
Xinyu Zhang | 433771e | 2022-04-01 16:49:17 +0800 | [diff] [blame] | 379 | |
| 380 | # Overwrite command lines to set compiler |
| 381 | build_cfg["set_compiler_path"] %= {"compiler": i.compiler} |
| 382 | build_cfg["set_compiler_path"] += " ;\n{} --version".format(self.get_compiler_name(i.compiler)) |
| 383 | |
Xinyu Zhang | a88a2eb | 2023-08-15 17:43:51 +0800 | [diff] [blame] | 384 | # Overwrite parameters of build configs |
| 385 | overwrite_params = {"codebase_root_dir": build_cfg["codebase_root_dir"], |
| 386 | "tfm_tests_root_dir": build_cfg["codebase_root_dir"] + "/../tf-m-tests", |
| 387 | "ci_build_root_dir": build_cfg["codebase_root_dir"] + "/../ci_build", |
Xinyu Zhang | b708f57 | 2020-09-15 11:43:46 +0800 | [diff] [blame] | 388 | "tfm_platform": i.tfm_platform, |
Xinyu Zhang | 8558852 | 2023-10-31 13:58:04 +0800 | [diff] [blame^] | 389 | "s_compiler": self.choose_toolchain(i.compiler, s_build = True), |
| 390 | "ns_compiler": self.choose_toolchain(i.compiler, s_build = False), |
Xinyu Zhang | b708f57 | 2020-09-15 11:43:46 +0800 | [diff] [blame] | 391 | "isolation_level": i.isolation_level, |
Xinyu Zhang | b18ae74 | 2023-04-25 14:33:27 +0800 | [diff] [blame] | 392 | "test_regression": self.map_params(i.test_regression, mapRegTest), |
Xinyu Zhang | b708f57 | 2020-09-15 11:43:46 +0800 | [diff] [blame] | 393 | "test_psa_api": i.test_psa_api, |
| 394 | "cmake_build_type": i.cmake_build_type, |
Xinyu Zhang | b708f57 | 2020-09-15 11:43:46 +0800 | [diff] [blame] | 395 | "with_bl2": i.with_bl2, |
Bence Balogh | 79fda44 | 2022-10-14 18:01:37 +0200 | [diff] [blame] | 396 | "profile": "" if i.profile=="N.A" else i.profile} |
| 397 | # The extra params can also contain paths with "codebase_root_dir" and |
| 398 | # these also need to be substituted |
Xinyu Zhang | b18ae74 | 2023-04-25 14:33:27 +0800 | [diff] [blame] | 399 | overwrite_params["extra_params"] = self.map_params(i.extra_params, mapExtraParams) % overwrite_params |
Bence Balogh | 79fda44 | 2022-10-14 18:01:37 +0200 | [diff] [blame] | 400 | |
Xinyu Zhang | a008602 | 2020-11-10 18:11:12 +0800 | [diff] [blame] | 401 | if i.test_psa_api == "IPC": |
Xinyu Zhang | cd1ed96 | 2020-11-11 16:00:52 +0800 | [diff] [blame] | 402 | overwrite_params["test_psa_api"] += " -DINCLUDE_PANIC_TESTS=1" |
Xinyu Zhang | 5f9fa96 | 2022-04-12 16:54:35 +0800 | [diff] [blame] | 403 | if i.test_psa_api == "CRYPTO" and "musca" in i.tfm_platform: |
| 404 | overwrite_params["test_psa_api"] += " -DCC312_LEGACY_DRIVER_API_ENABLED=OFF" |
Mark Horvath | ef57baa | 2022-09-12 13:36:36 +0200 | [diff] [blame] | 405 | if i.tfm_platform == "arm/musca_b1": |
Xinyu Zhang | ab9d1ea | 2022-12-23 17:11:22 +0800 | [diff] [blame] | 406 | overwrite_params["test_psa_api"] += " -DOTP_NV_COUNTERS_RAM_EMULATION=ON" |
Xinyu Zhang | a88a2eb | 2023-08-15 17:43:51 +0800 | [diff] [blame] | 407 | |
| 408 | # Test root dir |
| 409 | if i.test_psa_api != "OFF": |
| 410 | overwrite_params["test_root_dir"] = "tests_psa_arch" |
| 411 | else: |
| 412 | overwrite_params["test_root_dir"] = "tests_reg" |
| 413 | |
| 414 | # Overwrite commands for building TF-M image |
| 415 | build_cfg["spe_config_template"] %= overwrite_params |
| 416 | build_cfg["nspe_config_template"] %= overwrite_params |
| 417 | build_cfg["spe_cmake_build"] %= overwrite_params |
| 418 | build_cfg["nspe_cmake_build"] %= overwrite_params |
| 419 | build_cfg["post_build"] %= overwrite_params |
Xinyu Zhang | 433771e | 2022-04-01 16:49:17 +0800 | [diff] [blame] | 420 | |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 421 | return build_cfg |
| 422 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 423 | def post_eval(self): |
| 424 | """ If a single build failed fail the test """ |
| 425 | try: |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 426 | status_dict = self.unstash("Build Status") |
| 427 | if not status_dict: |
| 428 | raise Exception() |
| 429 | retcode_sum = sum(status_dict.values()) |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 430 | if retcode_sum != 0: |
| 431 | raise Exception() |
| 432 | return True |
| 433 | except Exception as e: |
| 434 | return False |
| 435 | |
| 436 | def post_exec(self, eval_ret): |
| 437 | """ Generate a report and fail the script if build == unsuccessfull""" |
| 438 | |
| 439 | self.print_summary() |
| 440 | if not eval_ret: |
| 441 | print("ERROR: ====> Build Failed! %s" % self.get_name()) |
| 442 | self.set_status(1) |
| 443 | else: |
| 444 | print("SUCCESS: ====> Build Complete!") |
| 445 | self.set_status(0) |
| 446 | |
| 447 | def get_report(self): |
| 448 | """ Expose the internal report to a new object for external classes """ |
| 449 | return deepcopy(self.unstash("Build Report")) |
| 450 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 451 | def load_config(self, config, work_dir): |
| 452 | try: |
| 453 | # passing config_name param supersseeds fileparam |
| 454 | if isinstance(config, dict): |
| 455 | ret_cfg = deepcopy(config) |
| 456 | elif isinstance(config, str): |
| 457 | # If the string does not descrive a file try to look for it in |
| 458 | # work directory |
| 459 | if not os.path.isfile(config): |
| 460 | # remove path from file |
| 461 | config_2 = os.path.split(config)[-1] |
| 462 | # look in the current working directory |
| 463 | config_2 = os.path.join(work_dir, config_2) |
| 464 | if not os.path.isfile(config_2): |
| 465 | m = "Could not find cfg in %s or %s " % (config, |
| 466 | config_2) |
| 467 | raise Exception(m) |
| 468 | # If fille exists in working directory |
| 469 | else: |
| 470 | config = config_2 |
| 471 | ret_cfg = load_json(config) |
| 472 | |
| 473 | else: |
| 474 | raise Exception("Need to provide a valid config name or file." |
| 475 | "Please use --config/--config-file parameter.") |
| 476 | except Exception as e: |
| 477 | print("Error:%s \nCould not load a valid config" % e) |
| 478 | sys.exit(1) |
| 479 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 480 | return ret_cfg |
| 481 | |
| 482 | def parse_config(self, cfg): |
| 483 | """ Parse a valid configuration file into a set of build dicts """ |
| 484 | |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 485 | ret_cfg = {} |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 486 | |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 487 | # Config entries which are not subject to changes during combinations |
| 488 | static_cfg = cfg["common_params"] |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 489 | |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 490 | # Converth the code path to absolute path |
| 491 | abs_code_dir = static_cfg["codebase_root_dir"] |
| 492 | abs_code_dir = os.path.abspath(os.path.expanduser(abs_code_dir)) |
| 493 | static_cfg["codebase_root_dir"] = abs_code_dir |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 494 | |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 495 | # seed_params is an optional field. Do not proccess if it is missing |
| 496 | if "seed_params" in cfg: |
| 497 | comb_cfg = cfg["seed_params"] |
| 498 | # Generate a list of all possible confugration combinations |
| 499 | ret_cfg = TFM_Build_Manager.generate_config_list(comb_cfg, |
| 500 | static_cfg) |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 501 | |
Xinyu Zhang | 2c63ce7 | 2021-07-23 14:01:59 +0800 | [diff] [blame] | 502 | # valid is an optional field. Do not proccess if it is missing |
| 503 | if "valid" in cfg: |
| 504 | # Valid configurations(Need to build) |
| 505 | valid_cfg = cfg["valid"] |
| 506 | # Add valid configs to build list |
| 507 | ret_cfg.update(TFM_Build_Manager.generate_optional_list( |
| 508 | comb_cfg, |
| 509 | static_cfg, |
| 510 | valid_cfg)) |
| 511 | |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 512 | # invalid is an optional field. Do not proccess if it is missing |
| 513 | if "invalid" in cfg: |
| 514 | # Invalid configurations(Do not build) |
| 515 | invalid_cfg = cfg["invalid"] |
| 516 | # Remove the rejected entries from the test list |
Xinyu Zhang | 0581b08 | 2021-05-17 10:46:57 +0800 | [diff] [blame] | 517 | rejection_cfg = TFM_Build_Manager.generate_optional_list( |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 518 | comb_cfg, |
| 519 | static_cfg, |
| 520 | invalid_cfg) |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 521 | |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 522 | # Subtract the two configurations |
| 523 | ret_cfg = {k: v for k, v in ret_cfg.items() |
| 524 | if k not in rejection_cfg} |
| 525 | self.simple_config = False |
| 526 | else: |
| 527 | self.simple_config = True |
| 528 | return ret_cfg, static_cfg |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 529 | |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 530 | # ----- Override bellow methods when subclassing for other projects ----- # |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 531 | |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 532 | def print_summary(self): |
| 533 | """ Print an comprehensive list of the build jobs with their status """ |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 534 | |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 535 | try: |
| 536 | full_rep = self.unstash("Build Report")["report"] |
| 537 | fl = ([k for k, v in full_rep.items() if v['status'] == 'Failed']) |
| 538 | ps = ([k for k, v in full_rep.items() if v['status'] == 'Success']) |
| 539 | except Exception as E: |
Karl Zhang | aff558a | 2020-05-15 14:28:23 +0100 | [diff] [blame] | 540 | print("No report generated", E) |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 541 | return |
| 542 | if fl: |
| 543 | print_test(t_list=fl, status="failed", tname="Builds") |
| 544 | if ps: |
| 545 | print_test(t_list=ps, status="passed", tname="Builds") |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 546 | |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 547 | @staticmethod |
| 548 | def generate_config_list(seed_config, static_config): |
| 549 | """ Generate all possible configuration combinations from a group of |
| 550 | lists of compiler options""" |
| 551 | config_list = [] |
| 552 | |
| 553 | if static_config["config_type"] == "tf-m": |
| 554 | cfg_name = "TFM_Build_CFG" |
| 555 | # Ensure the fieds are sorted in the desired order |
| 556 | # seed_config can be a subset of sort order for configurations with |
| 557 | # optional parameters. |
| 558 | tags = [n for n in static_config["sort_order"] |
| 559 | if n in seed_config.keys()] |
Karl Zhang | aff558a | 2020-05-15 14:28:23 +0100 | [diff] [blame] | 560 | print("!!!!!!!!!!!gen list %s\r\n" % tags) |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 561 | |
| 562 | data = [] |
| 563 | for key in tags: |
| 564 | data.append(seed_config[key]) |
| 565 | config_list = gen_cfg_combinations(cfg_name, |
| 566 | " ".join(tags), |
| 567 | *data) |
| 568 | else: |
| 569 | print("Not information for project type: %s." |
| 570 | " Please check config" % static_config["config_type"]) |
| 571 | |
| 572 | ret_cfg = {} |
| 573 | # Notify the user for the rejected configuations |
| 574 | for i in config_list: |
Xinyu Zhang | 1078e81 | 2020-10-15 11:52:36 +0800 | [diff] [blame] | 575 | # Convert named tuples to string in a brief format |
| 576 | config_param = [] |
| 577 | config_param.append(mapPlatform[list(i)[0]]) |
Xinyu Zhang | 433771e | 2022-04-01 16:49:17 +0800 | [diff] [blame] | 578 | config_param.append(list(i)[1].split("_")[0]) |
Summer Qin | 379abb6 | 2022-10-08 16:41:54 +0800 | [diff] [blame] | 579 | config_param.append(list(i)[2]) # ISOLATION_LEVEL |
Xinyu Zhang | b18ae74 | 2023-04-25 14:33:27 +0800 | [diff] [blame] | 580 | if list(i)[3] != "OFF": # TEST_REGRESSION |
| 581 | config_param.append(list(i)[3].replace(", ", "_")) |
Summer Qin | 379abb6 | 2022-10-08 16:41:54 +0800 | [diff] [blame] | 582 | if list(i)[4] != "OFF": #TEST_PSA_API |
| 583 | config_param.append(mapTestPsaApi[list(i)[4]]) |
| 584 | config_param.append(list(i)[5]) # BUILD_TYPE |
| 585 | if list(i)[6]: # BL2 |
Xinyu Zhang | 1078e81 | 2020-10-15 11:52:36 +0800 | [diff] [blame] | 586 | config_param.append("BL2") |
Summer Qin | 379abb6 | 2022-10-08 16:41:54 +0800 | [diff] [blame] | 587 | if list(i)[7]: # PROFILE |
| 588 | config_param.append(mapProfile[list(i)[7]]) |
| 589 | if list(i)[8]: # EXTRA_PARAMS |
| 590 | config_param.append(list(i)[8].replace(", ", "_")) |
Xinyu Zhang | 1078e81 | 2020-10-15 11:52:36 +0800 | [diff] [blame] | 591 | i_str = "_".join(config_param) |
Karl Zhang | aff558a | 2020-05-15 14:28:23 +0100 | [diff] [blame] | 592 | ret_cfg[i_str] = i |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 593 | return ret_cfg |
| 594 | |
| 595 | @staticmethod |
Xinyu Zhang | 0581b08 | 2021-05-17 10:46:57 +0800 | [diff] [blame] | 596 | def generate_optional_list(seed_config, |
| 597 | static_config, |
| 598 | optional_list): |
| 599 | optional_cfg = {} |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 600 | |
| 601 | if static_config["config_type"] == "tf-m": |
| 602 | |
Xinyu Zhang | 0581b08 | 2021-05-17 10:46:57 +0800 | [diff] [blame] | 603 | # If optional list is empty do nothing |
| 604 | if not optional_list: |
| 605 | return optional_cfg |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 606 | |
| 607 | tags = [n for n in static_config["sort_order"] |
| 608 | if n in seed_config.keys()] |
| 609 | sorted_default_lst = [seed_config[k] for k in tags] |
| 610 | |
Xinyu Zhang | 0581b08 | 2021-05-17 10:46:57 +0800 | [diff] [blame] | 611 | # If tags are not alligned with optional list entries quit |
| 612 | if len(tags) != len(optional_list[0]): |
| 613 | print(len(tags), len(optional_list[0])) |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 614 | print("Error, tags should be assigned to each " |
Xinyu Zhang | 0581b08 | 2021-05-17 10:46:57 +0800 | [diff] [blame] | 615 | "of the optional inputs") |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 616 | return [] |
| 617 | |
| 618 | # Replace wildcard ( "*") entries with every |
| 619 | # inluded in cfg variant |
Xinyu Zhang | 0581b08 | 2021-05-17 10:46:57 +0800 | [diff] [blame] | 620 | for k in optional_list: |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 621 | # Pad the omitted values with wildcard char * |
| 622 | res_list = list(k) + ["*"] * (5 - len(k)) |
Xinyu Zhang | 0581b08 | 2021-05-17 10:46:57 +0800 | [diff] [blame] | 623 | print("Working on optional input: %s" % (res_list)) |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 624 | |
| 625 | for n in range(len(res_list)): |
| 626 | |
| 627 | res_list[n] = [res_list[n]] if res_list[n] != "*" \ |
| 628 | else sorted_default_lst[n] |
| 629 | |
| 630 | # Generate a configuration and a name for the completed array |
Xinyu Zhang | 0581b08 | 2021-05-17 10:46:57 +0800 | [diff] [blame] | 631 | op_cfg = TFM_Build_Manager.generate_config_list( |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 632 | dict(zip(tags, res_list)), |
| 633 | static_config) |
| 634 | |
| 635 | # Append the configuration to the existing ones |
Xinyu Zhang | 0581b08 | 2021-05-17 10:46:57 +0800 | [diff] [blame] | 636 | optional_cfg = dict(optional_cfg, **op_cfg) |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 637 | |
Xinyu Zhang | 0581b08 | 2021-05-17 10:46:57 +0800 | [diff] [blame] | 638 | # Notify the user for the optional configuations |
| 639 | for i in optional_cfg.keys(): |
| 640 | print("Generating optional config %s" % i) |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 641 | else: |
| 642 | print("Not information for project type: %s." |
| 643 | " Please check config" % static_config["config_type"]) |
Xinyu Zhang | 0581b08 | 2021-05-17 10:46:57 +0800 | [diff] [blame] | 644 | return optional_cfg |