blob: 091e855b5e3c9317e399f5b9c4ca7747d9bb9e8e [file] [log] [blame]
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +01001#!/usr/bin/env python3
2
3""" tfm_build_manager.py:
4
5 Controlling class managing multiple build configruations for tfm """
6
7from __future__ import print_function
Xinyu Zhang433771e2022-04-01 16:49:17 +08008from json import tool
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +01009
10__copyright__ = """
11/*
Feder Liang357b1602022-01-11 16:47:49 +080012 * Copyright (c) 2018-2022, Arm Limited. All rights reserved.
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010013 *
14 * SPDX-License-Identifier: BSD-3-Clause
15 *
16 */
17 """
Karl Zhang08681e62020-10-30 13:56:03 +080018
19__author__ = "tf-m@lists.trustedfirmware.org"
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010020__project__ = "Trusted Firmware-M Open CI"
Xinyu Zhang06286a92021-07-22 14:00:51 +080021__version__ = "1.4.0"
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010022
23import os
24import sys
Karl Zhangaff558a2020-05-15 14:28:23 +010025from .utils import *
Minos Galanakisea421232019-06-20 17:11:28 +010026from time import time
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010027from copy import deepcopy
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010028from .structured_task import structuredTask
29from .tfm_builder import TFM_Builder
Xinyu Zhang1fa7f982022-04-20 17:46:17 +080030from build_helper.build_helper_config_maps import *
Xinyu Zhangfd2e1152021-12-17 18:09:01 +080031
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010032class 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 Galanakisea421232019-06-20 17:11:28 +010046 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 Galanakisf4ca6ac2017-12-11 02:39:21 +010050 self._tbm_build_threads = build_threads
51 self._tbm_conc_builds = parallel_builds
52 self._tbm_install = install
Minos Galanakisea421232019-06-20 17:11:28 +010053 self._tbm_img_sizes = img_sizes
54 self._tbm_relative_paths = relative_paths
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010055
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 Zhangaff558a2020-05-15 14:28:23 +010061 print("bm param tfm_dir %s" % tfm_dir)
62 print("bm %s %s %s" % (work_dir, cfg_dict, self._tbm_work_dir))
Minos Galanakisea421232019-06-20 17:11:28 +010063 # Internal flag to tag simple (non combination formatted configs)
64 self.simple_config = False
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010065 self._tbm_report = report
66
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010067 self._tbm_cfg = self.load_config(cfg_dict, self._tbm_work_dir)
Minos Galanakisea421232019-06-20 17:11:28 +010068 self._tbm_build_cfg, \
69 self.tbm_common_cfg = self.parse_config(self._tbm_cfg)
Karl Zhangaff558a2020-05-15 14:28:23 +010070 self._tfb_code_base_updated = False
71 self._tfb_log_f = "CodeBasePrepare.log"
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010072
73 super(TFM_Build_Manager, self).__init__(name="TFM_Build_Manager")
74
David Hud5fea0d2023-10-17 18:08:35 +080075 def choose_toolchain(self, compiler, s_build):
Xinyu Zhang433771e2022-04-01 16:49:17 +080076 toolchain = ""
David Hud5fea0d2023-10-17 18:08:35 +080077 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 Zhangff5d7712022-01-14 13:48:59 +080087
Xinyu Zhang433771e2022-04-01 16:49:17 +080088 return toolchain
89
David Hu90dc2842023-10-17 20:39:42 +080090 def choose_ns_target(self, with_bl2):
91 ns_target = ""
92 if with_bl2:
93 ns_target = "tfm_app_binaries"
94 else:
95 ns_target = "tfm_ns"
96
97 return ns_target
98
Xinyu Zhang433771e2022-04-01 16:49:17 +080099 def get_compiler_name(self, compiler):
100 compiler_name = ""
101 if "GCC"in compiler:
102 compiler_name = "arm-none-eabi-gcc"
103 elif "ARMCLANG" in compiler:
104 compiler_name = "armclang"
105
106 return compiler_name
Xinyu Zhangff5d7712022-01-14 13:48:59 +0800107
Xinyu Zhangb18ae742023-04-25 14:33:27 +0800108 def map_params(self, params, maps):
109 build_configs = ""
Xinyu Zhangfc061dd2022-07-26 14:52:56 +0800110 param_list = params.split(", ")
111 for param in param_list:
Xinyu Zhangb18ae742023-04-25 14:33:27 +0800112 build_configs += maps[param]
113 return build_configs
Xinyu Zhangfc061dd2022-07-26 14:52:56 +0800114
Dean Bircha6ede7e2020-03-13 14:00:33 +0000115 def get_config(self):
116 return list(self._tbm_build_cfg.keys())
Dean Birch5cb5a882020-01-24 11:37:13 +0000117
Xinyu Zhang46b37182023-06-30 15:36:44 +0800118 def get_build_configs(self, config, silence_stderr=False):
Dean Birch5cb5a882020-01-24 11:37:13 +0000119 """
Xinyu Zhang46b37182023-06-30 15:36:44 +0800120 Return build config variables needed by the input config.
Dean Birch5cb5a882020-01-24 11:37:13 +0000121 """
122 if config not in self._tbm_build_cfg:
Dean Bircha6ede7e2020-03-13 14:00:33 +0000123 if not silence_stderr:
124 print("Error: no such config {}".format(config), file=sys.stderr)
Dean Birch5cb5a882020-01-24 11:37:13 +0000125 sys.exit(1)
126 config_details = self._tbm_build_cfg[config]
Xinyu Zhang46b37182023-06-30 15:36:44 +0800127 config_params = {
128 "CONFIG_NAME": config,
129 "TFM_PLATFORM": config_details.tfm_platform,
130 "COMPILER": config_details.compiler,
131 "ISOLATION_LEVEL": config_details.isolation_level,
132 "TEST_REGRESSION": config_details.test_regression,
133 "TEST_PSA_API": config_details.test_psa_api,
134 "CMAKE_BUILD_TYPE": config_details.cmake_build_type,
135 "BL2": config_details.with_bl2,
136 "PROFILE": "N.A" if not config_details.profile else config_details.profile,
137 "EXTRA_PARAMS": "N.A" if not config_details.extra_params else config_details.extra_params,
138 }
139 return config_params
Dean Birch5cb5a882020-01-24 11:37:13 +0000140
Xinyu Zhang46b37182023-06-30 15:36:44 +0800141 def get_build_commands(self, config, silence_stderr=False, jobs=None):
142 """
143 Return selected type of commands to be run to build the input config.
144 """
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000145 config_details = self._tbm_build_cfg[config]
146 codebase_dir = os.path.join(os.getcwd(),"trusted-firmware-m")
Xinyu Zhang433771e2022-04-01 16:49:17 +0800147 build_config = self.get_build_config(config_details, config, \
148 silence=silence_stderr, \
Paul Sokolovskycba7ee42023-04-19 13:21:33 +0300149 codebase_dir=codebase_dir, \
150 jobs=jobs)
Xinyu Zhang46b37182023-06-30 15:36:44 +0800151 build_commands = {
Xinyu Zhangdc3b4ca2023-08-15 17:43:51 +0800152 'set_compiler': build_config['set_compiler_path'],
153 'spe_cmake_config': build_config['spe_config_template'],
154 'nspe_cmake_config': build_config['nspe_config_template'],
155 'spe_cmake_build': build_config['spe_cmake_build'],
156 'nspe_cmake_build': build_config['nspe_cmake_build'],
157 'post_build': build_config['post_build']
Xinyu Zhang46b37182023-06-30 15:36:44 +0800158 }
159 return build_commands
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000160
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100161 def pre_eval(self):
162 """ Tests that need to be run in set-up state """
163 return True
164
165 def pre_exec(self, eval_ret):
166 """ """
167
Minos Galanakisea421232019-06-20 17:11:28 +0100168 def override_tbm_cfg_params(self, config, override_keys, **params):
169 """ Using a dictionay as input, for each key defined in
170 override_keys it will replace the config[key] entries with
171 the key=value parameters provided """
172
173 for key in override_keys:
174 if isinstance(config[key], list):
175 config[key] = [n % params for n in config[key]]
176 elif isinstance(config[key], str):
177 config[key] = config[key] % params
178 else:
179 raise Exception("Config does not contain key %s "
180 "of type %s" % (key, config[key]))
181 return config
182
Karl Zhangaff558a2020-05-15 14:28:23 +0100183 def pre_build(self, build_cfg):
184 print("pre_build start %s \r\nself._tfb_cfg %s\r\n" %
185 (self, build_cfg))
186
187 try:
188 if self._tfb_code_base_updated:
189 print("Code base has been updated")
190 return True
191
192 self._tfb_code_base_updated = True
193
194 if "build_psa_api" in build_cfg:
195 # FF IPC build needs repo manifest update for TFM and PSA arch test
196 if "build_ff_ipc" in build_cfg:
197 print("Checkout to FF IPC code base")
198 os.chdir(build_cfg["codebase_root_dir"] + "/../psa-arch-tests/api-tests")
199 _api_test_manifest = "git checkout . ; python3 tools/scripts/manifest_update.py"
200 if subprocess_log(_api_test_manifest,
201 self._tfb_log_f,
202 append=True,
203 prefix=_api_test_manifest):
204
205 raise Exception("Python Failed please check log: %s" %
206 self._tfb_log_f)
207
208 _api_test_manifest_tfm = "python3 tools/tfm_parse_manifest_list.py -m tools/tfm_psa_ff_test_manifest_list.yaml append"
209 os.chdir(build_cfg["codebase_root_dir"])
210 if subprocess_log(_api_test_manifest_tfm,
211 self._tfb_log_f,
212 append=True,
213 prefix=_api_test_manifest_tfm):
214
215 raise Exception("Python TFM Failed please check log: %s" %
216 self._tfb_log_f)
217 return True
218
219 print("Checkout to default code base")
220 os.chdir(build_cfg["codebase_root_dir"] + "/../psa-arch-tests/api-tests")
221 _api_test_manifest = "git checkout ."
222 if subprocess_log(_api_test_manifest,
223 self._tfb_log_f,
224 append=True,
225 prefix=_api_test_manifest):
226
227 raise Exception("Python Failed please check log: %s" %
228 self._tfb_log_f)
229
230 _api_test_manifest_tfm = "python3 tools/tfm_parse_manifest_list.py"
231 os.chdir(build_cfg["codebase_root_dir"])
232 if subprocess_log(_api_test_manifest_tfm,
233 self._tfb_log_f,
234 append=True,
235 prefix=_api_test_manifest_tfm):
236
237 raise Exception("Python TFM Failed please check log: %s" %
238 self._tfb_log_f)
239 finally:
240 print("python pass after builder prepare")
241 os.chdir(build_cfg["codebase_root_dir"] + "/../")
242
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100243 def task_exec(self):
244 """ Create a build pool and execute them in parallel """
245
246 build_pool = []
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100247
Minos Galanakisea421232019-06-20 17:11:28 +0100248 # When a config is flagged as a single build config.
249 # Name is evaluated by config type
250 if self.simple_config:
251
252 build_cfg = deepcopy(self.tbm_common_cfg)
253
254 # Extract the common for all elements of config
Xinyu Zhang46b37182023-06-30 15:36:44 +0800255 try:
256 build_cfg["required_artefacts"] = build_cfg["required_artefacts"]["all"]
257 except KeyError:
258 build_cfg["required_artefacts"] = []
Minos Galanakisea421232019-06-20 17:11:28 +0100259 name = build_cfg["config_type"]
260
261 # Override _tbm_xxx paths in commands
262 # plafrom in not guaranteed without seeds so _tbm_target_platform
263 # is ignored
264 over_dict = {"_tbm_build_dir_": os.path.join(self._tbm_work_dir,
265 name),
266 "_tbm_code_dir_": build_cfg["codebase_root_dir"]}
267
268 build_cfg = self.override_tbm_cfg_params(build_cfg,
Xinyu Zhang46b37182023-06-30 15:36:44 +0800269 ["post_build",
Minos Galanakisea421232019-06-20 17:11:28 +0100270 "artifact_capture_rex"],
271 **over_dict)
272
273 # Overrides path in expected artefacts
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100274 print("Loading config %s" % name)
Minos Galanakisea421232019-06-20 17:11:28 +0100275
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 Zhangaff558a2020-05-15 14:28:23 +0100286 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 Galanakisea421232019-06-20 17:11:28 +0100288 for name, i in self._tbm_build_cfg.items():
289 # Do not modify the original config
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000290 build_cfg = self.get_build_config(i, name)
Karl Zhangaff558a2020-05-15 14:28:23 +0100291 self.pre_build(build_cfg)
Minos Galanakisea421232019-06-20 17:11:28 +0100292 # 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 Galanakisf4ca6ac2017-12-11 02:39:21 +0100304
305 status_rep = {}
Minos Galanakisea421232019-06-20 17:11:28 +0100306 build_rep = {}
307 completed_build_count = 0
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100308 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 Galanakisea421232019-06-20 17:11:28 +0100326 completed_build_count += 1
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100327 print("Build: Finished %s" % build.get_name())
Minos Galanakisea421232019-06-20 17:11:28 +0100328 print("Build Progress:")
329 show_progress(completed_build_count, len(build_pool))
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100330
331 # Store status in report
332 status_rep[build.get_name()] = build.get_status()
Minos Galanakisea421232019-06-20 17:11:28 +0100333 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 Galanakisf4ca6ac2017-12-11 02:39:21 +0100346 # 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
Xinyu Zhangdc3b4ca2023-08-15 17:43:51 +0800354 def get_build_config(self, i, name, silence=False, codebase_dir=None, jobs=None):
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000355 build_cfg = deepcopy(self.tbm_common_cfg)
356 if not codebase_dir:
357 codebase_dir = build_cfg["codebase_root_dir"]
358 else:
359 # Would prefer to do all with the new variable
360 # However, many things use this from build_cfg elsewhere
361 build_cfg["codebase_root_dir"] = codebase_dir
362 # Extract the common for all elements of config
Xinyu Zhang46b37182023-06-30 15:36:44 +0800363 try:
364 build_cfg["required_artefacts"] = deepcopy(self.tbm_common_cfg["required_artefacts"]["all"])
365 except KeyError as E:
366 build_cfg["required_artefacts"] = []
367 build_cfg["post_build"] = ""
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000368 # Extract the platform specific elements of config
Xinyu Zhang46b37182023-06-30 15:36:44 +0800369 for key in ["post_build", "required_artefacts"]:
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000370 try:
Xinyu Zhangfb80b5d2022-07-26 15:42:26 +0800371 if i.tfm_platform in self.tbm_common_cfg[key].keys():
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000372 build_cfg[key] += deepcopy(self.tbm_common_cfg[key]
Xinyu Zhangb708f572020-09-15 11:43:46 +0800373 [i.tfm_platform])
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000374 except Exception as E:
375 pass
Karl Zhang1eed6322020-07-01 15:38:10 +0800376
Paul Sokolovskycba7ee42023-04-19 13:21:33 +0300377 if jobs is None:
378 if os.cpu_count() >= 8:
379 #run in a serviver with scripts, parallel build will use CPU numbers
380 jobs = 2
381 else:
382 #run in a docker, usually docker with CPUs less than 8
383 jobs = os.cpu_count()
384
385 thread_no = " -j {} ".format(jobs)
Xinyu Zhangdc3b4ca2023-08-15 17:43:51 +0800386 build_cfg["spe_cmake_build"] += thread_no
387 build_cfg["nspe_cmake_build"] += thread_no
Xinyu Zhang433771e2022-04-01 16:49:17 +0800388
389 # Overwrite command lines to set compiler
390 build_cfg["set_compiler_path"] %= {"compiler": i.compiler}
391 build_cfg["set_compiler_path"] += " ;\n{} --version".format(self.get_compiler_name(i.compiler))
392
Xinyu Zhangdc3b4ca2023-08-15 17:43:51 +0800393 # Overwrite parameters of build configs
394 overwrite_params = {"codebase_root_dir": build_cfg["codebase_root_dir"],
395 "tfm_tests_root_dir": build_cfg["codebase_root_dir"] + "/../tf-m-tests",
396 "ci_build_root_dir": build_cfg["codebase_root_dir"] + "/../ci_build",
Xinyu Zhangb708f572020-09-15 11:43:46 +0800397 "tfm_platform": i.tfm_platform,
David Hud5fea0d2023-10-17 18:08:35 +0800398 "s_compiler": self.choose_toolchain(i.compiler, s_build = True),
399 "ns_compiler": self.choose_toolchain(i.compiler, s_build = False),
David Hu90dc2842023-10-17 20:39:42 +0800400 "ns_target": self.choose_ns_target(i.with_bl2),
Xinyu Zhangb708f572020-09-15 11:43:46 +0800401 "isolation_level": i.isolation_level,
Xinyu Zhangb18ae742023-04-25 14:33:27 +0800402 "test_regression": self.map_params(i.test_regression, mapRegTest),
Xinyu Zhangb708f572020-09-15 11:43:46 +0800403 "test_psa_api": i.test_psa_api,
404 "cmake_build_type": i.cmake_build_type,
Xinyu Zhangb708f572020-09-15 11:43:46 +0800405 "with_bl2": i.with_bl2,
Bence Balogh79fda442022-10-14 18:01:37 +0200406 "profile": "" if i.profile=="N.A" else i.profile}
407 # The extra params can also contain paths with "codebase_root_dir" and
408 # these also need to be substituted
Xinyu Zhangb18ae742023-04-25 14:33:27 +0800409 overwrite_params["extra_params"] = self.map_params(i.extra_params, mapExtraParams) % overwrite_params
Bence Balogh79fda442022-10-14 18:01:37 +0200410
Xinyu Zhanga0086022020-11-10 18:11:12 +0800411 if i.test_psa_api == "IPC":
Xinyu Zhangcd1ed962020-11-11 16:00:52 +0800412 overwrite_params["test_psa_api"] += " -DINCLUDE_PANIC_TESTS=1"
Xinyu Zhang5f9fa962022-04-12 16:54:35 +0800413 if i.test_psa_api == "CRYPTO" and "musca" in i.tfm_platform:
414 overwrite_params["test_psa_api"] += " -DCC312_LEGACY_DRIVER_API_ENABLED=OFF"
Mark Horvathef57baa2022-09-12 13:36:36 +0200415 if i.tfm_platform == "arm/musca_b1":
Xinyu Zhangab9d1ea2022-12-23 17:11:22 +0800416 overwrite_params["test_psa_api"] += " -DOTP_NV_COUNTERS_RAM_EMULATION=ON"
Xinyu Zhangdc3b4ca2023-08-15 17:43:51 +0800417
418 # Test root dir
419 if i.test_psa_api != "OFF":
420 overwrite_params["test_root_dir"] = "tests_psa_arch"
421 else:
422 overwrite_params["test_root_dir"] = "tests_reg"
423
424 # Overwrite commands for building TF-M image
425 build_cfg["spe_config_template"] %= overwrite_params
426 build_cfg["nspe_config_template"] %= overwrite_params
427 build_cfg["spe_cmake_build"] %= overwrite_params
428 build_cfg["nspe_cmake_build"] %= overwrite_params
429 build_cfg["post_build"] %= overwrite_params
Xinyu Zhang433771e2022-04-01 16:49:17 +0800430
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000431 return build_cfg
432
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100433 def post_eval(self):
434 """ If a single build failed fail the test """
435 try:
Minos Galanakisea421232019-06-20 17:11:28 +0100436 status_dict = self.unstash("Build Status")
437 if not status_dict:
438 raise Exception()
439 retcode_sum = sum(status_dict.values())
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100440 if retcode_sum != 0:
441 raise Exception()
442 return True
443 except Exception as e:
444 return False
445
446 def post_exec(self, eval_ret):
447 """ Generate a report and fail the script if build == unsuccessfull"""
448
449 self.print_summary()
450 if not eval_ret:
451 print("ERROR: ====> Build Failed! %s" % self.get_name())
452 self.set_status(1)
453 else:
454 print("SUCCESS: ====> Build Complete!")
455 self.set_status(0)
456
457 def get_report(self):
458 """ Expose the internal report to a new object for external classes """
459 return deepcopy(self.unstash("Build Report"))
460
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100461 def load_config(self, config, work_dir):
462 try:
463 # passing config_name param supersseeds fileparam
464 if isinstance(config, dict):
465 ret_cfg = deepcopy(config)
466 elif isinstance(config, str):
467 # If the string does not descrive a file try to look for it in
468 # work directory
469 if not os.path.isfile(config):
470 # remove path from file
471 config_2 = os.path.split(config)[-1]
472 # look in the current working directory
473 config_2 = os.path.join(work_dir, config_2)
474 if not os.path.isfile(config_2):
475 m = "Could not find cfg in %s or %s " % (config,
476 config_2)
477 raise Exception(m)
478 # If fille exists in working directory
479 else:
480 config = config_2
481 ret_cfg = load_json(config)
482
483 else:
484 raise Exception("Need to provide a valid config name or file."
485 "Please use --config/--config-file parameter.")
486 except Exception as e:
487 print("Error:%s \nCould not load a valid config" % e)
488 sys.exit(1)
489
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100490 return ret_cfg
491
492 def parse_config(self, cfg):
493 """ Parse a valid configuration file into a set of build dicts """
494
Minos Galanakisea421232019-06-20 17:11:28 +0100495 ret_cfg = {}
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100496
Minos Galanakisea421232019-06-20 17:11:28 +0100497 # Config entries which are not subject to changes during combinations
498 static_cfg = cfg["common_params"]
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100499
Minos Galanakisea421232019-06-20 17:11:28 +0100500 # Converth the code path to absolute path
501 abs_code_dir = static_cfg["codebase_root_dir"]
502 abs_code_dir = os.path.abspath(os.path.expanduser(abs_code_dir))
503 static_cfg["codebase_root_dir"] = abs_code_dir
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100504
Minos Galanakisea421232019-06-20 17:11:28 +0100505 # seed_params is an optional field. Do not proccess if it is missing
506 if "seed_params" in cfg:
507 comb_cfg = cfg["seed_params"]
508 # Generate a list of all possible confugration combinations
509 ret_cfg = TFM_Build_Manager.generate_config_list(comb_cfg,
510 static_cfg)
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100511
Xinyu Zhang2c63ce72021-07-23 14:01:59 +0800512 # valid is an optional field. Do not proccess if it is missing
513 if "valid" in cfg:
514 # Valid configurations(Need to build)
515 valid_cfg = cfg["valid"]
516 # Add valid configs to build list
517 ret_cfg.update(TFM_Build_Manager.generate_optional_list(
518 comb_cfg,
519 static_cfg,
520 valid_cfg))
521
Minos Galanakisea421232019-06-20 17:11:28 +0100522 # invalid is an optional field. Do not proccess if it is missing
523 if "invalid" in cfg:
524 # Invalid configurations(Do not build)
525 invalid_cfg = cfg["invalid"]
526 # Remove the rejected entries from the test list
Xinyu Zhang0581b082021-05-17 10:46:57 +0800527 rejection_cfg = TFM_Build_Manager.generate_optional_list(
Minos Galanakisea421232019-06-20 17:11:28 +0100528 comb_cfg,
529 static_cfg,
530 invalid_cfg)
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100531
Minos Galanakisea421232019-06-20 17:11:28 +0100532 # Subtract the two configurations
533 ret_cfg = {k: v for k, v in ret_cfg.items()
534 if k not in rejection_cfg}
535 self.simple_config = False
536 else:
537 self.simple_config = True
538 return ret_cfg, static_cfg
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100539
Minos Galanakisea421232019-06-20 17:11:28 +0100540 # ----- Override bellow methods when subclassing for other projects ----- #
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100541
Minos Galanakisea421232019-06-20 17:11:28 +0100542 def print_summary(self):
543 """ Print an comprehensive list of the build jobs with their status """
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100544
Minos Galanakisea421232019-06-20 17:11:28 +0100545 try:
546 full_rep = self.unstash("Build Report")["report"]
547 fl = ([k for k, v in full_rep.items() if v['status'] == 'Failed'])
548 ps = ([k for k, v in full_rep.items() if v['status'] == 'Success'])
549 except Exception as E:
Karl Zhangaff558a2020-05-15 14:28:23 +0100550 print("No report generated", E)
Minos Galanakisea421232019-06-20 17:11:28 +0100551 return
552 if fl:
553 print_test(t_list=fl, status="failed", tname="Builds")
554 if ps:
555 print_test(t_list=ps, status="passed", tname="Builds")
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100556
Minos Galanakisea421232019-06-20 17:11:28 +0100557 @staticmethod
558 def generate_config_list(seed_config, static_config):
559 """ Generate all possible configuration combinations from a group of
560 lists of compiler options"""
561 config_list = []
562
563 if static_config["config_type"] == "tf-m":
564 cfg_name = "TFM_Build_CFG"
565 # Ensure the fieds are sorted in the desired order
566 # seed_config can be a subset of sort order for configurations with
567 # optional parameters.
568 tags = [n for n in static_config["sort_order"]
569 if n in seed_config.keys()]
Karl Zhangaff558a2020-05-15 14:28:23 +0100570 print("!!!!!!!!!!!gen list %s\r\n" % tags)
Minos Galanakisea421232019-06-20 17:11:28 +0100571
572 data = []
573 for key in tags:
574 data.append(seed_config[key])
575 config_list = gen_cfg_combinations(cfg_name,
576 " ".join(tags),
577 *data)
578 else:
579 print("Not information for project type: %s."
580 " Please check config" % static_config["config_type"])
581
582 ret_cfg = {}
583 # Notify the user for the rejected configuations
584 for i in config_list:
Xinyu Zhang1078e812020-10-15 11:52:36 +0800585 # Convert named tuples to string in a brief format
586 config_param = []
587 config_param.append(mapPlatform[list(i)[0]])
Xinyu Zhang433771e2022-04-01 16:49:17 +0800588 config_param.append(list(i)[1].split("_")[0])
Summer Qin379abb62022-10-08 16:41:54 +0800589 config_param.append(list(i)[2]) # ISOLATION_LEVEL
Xinyu Zhangb18ae742023-04-25 14:33:27 +0800590 if list(i)[3] != "OFF": # TEST_REGRESSION
591 config_param.append(list(i)[3].replace(", ", "_"))
Summer Qin379abb62022-10-08 16:41:54 +0800592 if list(i)[4] != "OFF": #TEST_PSA_API
593 config_param.append(mapTestPsaApi[list(i)[4]])
594 config_param.append(list(i)[5]) # BUILD_TYPE
595 if list(i)[6]: # BL2
Xinyu Zhang1078e812020-10-15 11:52:36 +0800596 config_param.append("BL2")
Summer Qin379abb62022-10-08 16:41:54 +0800597 if list(i)[7]: # PROFILE
598 config_param.append(mapProfile[list(i)[7]])
599 if list(i)[8]: # EXTRA_PARAMS
600 config_param.append(list(i)[8].replace(", ", "_"))
Xinyu Zhang1078e812020-10-15 11:52:36 +0800601 i_str = "_".join(config_param)
Karl Zhangaff558a2020-05-15 14:28:23 +0100602 ret_cfg[i_str] = i
Minos Galanakisea421232019-06-20 17:11:28 +0100603 return ret_cfg
604
605 @staticmethod
Xinyu Zhang0581b082021-05-17 10:46:57 +0800606 def generate_optional_list(seed_config,
607 static_config,
608 optional_list):
609 optional_cfg = {}
Minos Galanakisea421232019-06-20 17:11:28 +0100610
611 if static_config["config_type"] == "tf-m":
612
Xinyu Zhang0581b082021-05-17 10:46:57 +0800613 # If optional list is empty do nothing
614 if not optional_list:
615 return optional_cfg
Minos Galanakisea421232019-06-20 17:11:28 +0100616
617 tags = [n for n in static_config["sort_order"]
618 if n in seed_config.keys()]
619 sorted_default_lst = [seed_config[k] for k in tags]
620
Xinyu Zhang0581b082021-05-17 10:46:57 +0800621 # If tags are not alligned with optional list entries quit
622 if len(tags) != len(optional_list[0]):
623 print(len(tags), len(optional_list[0]))
Minos Galanakisea421232019-06-20 17:11:28 +0100624 print("Error, tags should be assigned to each "
Xinyu Zhang0581b082021-05-17 10:46:57 +0800625 "of the optional inputs")
Minos Galanakisea421232019-06-20 17:11:28 +0100626 return []
627
628 # Replace wildcard ( "*") entries with every
629 # inluded in cfg variant
Xinyu Zhang0581b082021-05-17 10:46:57 +0800630 for k in optional_list:
Minos Galanakisea421232019-06-20 17:11:28 +0100631 # Pad the omitted values with wildcard char *
632 res_list = list(k) + ["*"] * (5 - len(k))
Xinyu Zhang0581b082021-05-17 10:46:57 +0800633 print("Working on optional input: %s" % (res_list))
Minos Galanakisea421232019-06-20 17:11:28 +0100634
635 for n in range(len(res_list)):
636
637 res_list[n] = [res_list[n]] if res_list[n] != "*" \
638 else sorted_default_lst[n]
639
640 # Generate a configuration and a name for the completed array
Xinyu Zhang0581b082021-05-17 10:46:57 +0800641 op_cfg = TFM_Build_Manager.generate_config_list(
Minos Galanakisea421232019-06-20 17:11:28 +0100642 dict(zip(tags, res_list)),
643 static_config)
644
645 # Append the configuration to the existing ones
Xinyu Zhang0581b082021-05-17 10:46:57 +0800646 optional_cfg = dict(optional_cfg, **op_cfg)
Minos Galanakisea421232019-06-20 17:11:28 +0100647
Xinyu Zhang0581b082021-05-17 10:46:57 +0800648 # Notify the user for the optional configuations
649 for i in optional_cfg.keys():
650 print("Generating optional config %s" % i)
Minos Galanakisea421232019-06-20 17:11:28 +0100651 else:
652 print("Not information for project type: %s."
653 " Please check config" % static_config["config_type"])
Xinyu Zhang0581b082021-05-17 10:46:57 +0800654 return optional_cfg