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