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