blob: f78c9e8b2363ba3e780c3fa67f3c01c03e542e15 [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
Xinyu Zhang433771e2022-04-01 16:49:17 +080075 def choose_toolchain(self, compiler):
76 toolchain = ""
77 if "GCC"in compiler:
78 toolchain = "toolchain_GNUARM.cmake"
79 elif "ARMCLANG" in compiler:
80 toolchain = "toolchain_ARMCLANG.cmake"
Xinyu Zhangff5d7712022-01-14 13:48:59 +080081
Xinyu Zhang433771e2022-04-01 16:49:17 +080082 return toolchain
83
84 def get_compiler_name(self, compiler):
85 compiler_name = ""
86 if "GCC"in compiler:
87 compiler_name = "arm-none-eabi-gcc"
88 elif "ARMCLANG" in compiler:
89 compiler_name = "armclang"
90
91 return compiler_name
Xinyu Zhangff5d7712022-01-14 13:48:59 +080092
Dean Bircha6ede7e2020-03-13 14:00:33 +000093 def get_config(self):
94 return list(self._tbm_build_cfg.keys())
Dean Birch5cb5a882020-01-24 11:37:13 +000095
Dean Bircha6ede7e2020-03-13 14:00:33 +000096 def print_config_environment(self, config, silence_stderr=False):
Dean Birch5cb5a882020-01-24 11:37:13 +000097 """
98 For a given build configuration from output of print_config
99 method, print environment variables to build.
100 """
101 if config not in self._tbm_build_cfg:
Dean Bircha6ede7e2020-03-13 14:00:33 +0000102 if not silence_stderr:
103 print("Error: no such config {}".format(config), file=sys.stderr)
Dean Birch5cb5a882020-01-24 11:37:13 +0000104 sys.exit(1)
105 config_details = self._tbm_build_cfg[config]
106 argument_list = [
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000107 "CONFIG_NAME={}",
Xinyu Zhangb708f572020-09-15 11:43:46 +0800108 "TFM_PLATFORM={}",
Xinyu Zhang433771e2022-04-01 16:49:17 +0800109 "COMPILER={}",
Xinyu Zhang73ed2992021-09-15 11:38:23 +0800110 "LIB_MODEL={}",
Xinyu Zhangb708f572020-09-15 11:43:46 +0800111 "ISOLATION_LEVEL={}",
112 "TEST_REGRESSION={}",
113 "TEST_PSA_API={}",
Dean Birch5cb5a882020-01-24 11:37:13 +0000114 "CMAKE_BUILD_TYPE={}",
115 "BL2={}",
Xinyu Zhangb708f572020-09-15 11:43:46 +0800116 "NS={}",
Xinyu Zhang9fd74242020-10-22 11:30:50 +0800117 "PROFILE={}",
Xinyu Zhang9bfe8a92021-10-28 16:27:12 +0800118 "PARTITION_PS={}",
Xinyu Zhangfd2e1152021-12-17 18:09:01 +0800119 "EXTRA_PARAMS={}"
Dean Birch5cb5a882020-01-24 11:37:13 +0000120 ]
121 print(
122 "\n".join(argument_list)
123 .format(
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000124 config,
Xinyu Zhangb708f572020-09-15 11:43:46 +0800125 config_details.tfm_platform,
Xinyu Zhang433771e2022-04-01 16:49:17 +0800126 config_details.compiler,
Xinyu Zhang73ed2992021-09-15 11:38:23 +0800127 config_details.lib_model,
Xinyu Zhangb708f572020-09-15 11:43:46 +0800128 config_details.isolation_level,
129 config_details.test_regression,
130 config_details.test_psa_api,
Dean Birch5cb5a882020-01-24 11:37:13 +0000131 config_details.cmake_build_type,
Xinyu Zhangb708f572020-09-15 11:43:46 +0800132 config_details.with_bl2,
133 config_details.with_ns,
Xinyu Zhang9fd74242020-10-22 11:30:50 +0800134 "N.A" if not config_details.profile else config_details.profile,
Xinyu Zhang9bfe8a92021-10-28 16:27:12 +0800135 config_details.partition_ps,
Xinyu Zhangfd2e1152021-12-17 18:09:01 +0800136 "N.A" if not config_details.extra_params else config_details.extra_params,
Dean Birch5cb5a882020-01-24 11:37:13 +0000137 )
138 .strip()
139 )
140
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000141 def print_build_commands(self, config, silence_stderr=False):
142 config_details = self._tbm_build_cfg[config]
143 codebase_dir = os.path.join(os.getcwd(),"trusted-firmware-m")
Xinyu Zhangb708f572020-09-15 11:43:46 +0800144 build_dir=os.path.join(os.getcwd(),"trusted-firmware-m/build")
Xinyu Zhang433771e2022-04-01 16:49:17 +0800145 build_config = self.get_build_config(config_details, config, \
146 silence=silence_stderr, \
147 build_dir=build_dir, \
148 codebase_dir=codebase_dir)
149 build_commands = [build_config["set_compiler_path"], \
150 build_config["config_template"]]
Xinyu Zhang694eb492020-11-04 18:29:08 +0800151 for command in build_config["build_cmds"]:
152 build_commands.append(command)
Xinyu Zhangb708f572020-09-15 11:43:46 +0800153 print(" ;\n".join(build_commands))
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000154
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100155 def pre_eval(self):
156 """ Tests that need to be run in set-up state """
157 return True
158
159 def pre_exec(self, eval_ret):
160 """ """
161
Minos Galanakisea421232019-06-20 17:11:28 +0100162 def override_tbm_cfg_params(self, config, override_keys, **params):
163 """ Using a dictionay as input, for each key defined in
164 override_keys it will replace the config[key] entries with
165 the key=value parameters provided """
166
167 for key in override_keys:
168 if isinstance(config[key], list):
169 config[key] = [n % params for n in config[key]]
170 elif isinstance(config[key], str):
171 config[key] = config[key] % params
172 else:
173 raise Exception("Config does not contain key %s "
174 "of type %s" % (key, config[key]))
175 return config
176
Karl Zhangaff558a2020-05-15 14:28:23 +0100177 def pre_build(self, build_cfg):
178 print("pre_build start %s \r\nself._tfb_cfg %s\r\n" %
179 (self, build_cfg))
180
181 try:
182 if self._tfb_code_base_updated:
183 print("Code base has been updated")
184 return True
185
186 self._tfb_code_base_updated = True
187
188 if "build_psa_api" in build_cfg:
189 # FF IPC build needs repo manifest update for TFM and PSA arch test
190 if "build_ff_ipc" in build_cfg:
191 print("Checkout to FF IPC code base")
192 os.chdir(build_cfg["codebase_root_dir"] + "/../psa-arch-tests/api-tests")
193 _api_test_manifest = "git checkout . ; python3 tools/scripts/manifest_update.py"
194 if subprocess_log(_api_test_manifest,
195 self._tfb_log_f,
196 append=True,
197 prefix=_api_test_manifest):
198
199 raise Exception("Python Failed please check log: %s" %
200 self._tfb_log_f)
201
202 _api_test_manifest_tfm = "python3 tools/tfm_parse_manifest_list.py -m tools/tfm_psa_ff_test_manifest_list.yaml append"
203 os.chdir(build_cfg["codebase_root_dir"])
204 if subprocess_log(_api_test_manifest_tfm,
205 self._tfb_log_f,
206 append=True,
207 prefix=_api_test_manifest_tfm):
208
209 raise Exception("Python TFM Failed please check log: %s" %
210 self._tfb_log_f)
211 return True
212
213 print("Checkout to default code base")
214 os.chdir(build_cfg["codebase_root_dir"] + "/../psa-arch-tests/api-tests")
215 _api_test_manifest = "git checkout ."
216 if subprocess_log(_api_test_manifest,
217 self._tfb_log_f,
218 append=True,
219 prefix=_api_test_manifest):
220
221 raise Exception("Python Failed please check log: %s" %
222 self._tfb_log_f)
223
224 _api_test_manifest_tfm = "python3 tools/tfm_parse_manifest_list.py"
225 os.chdir(build_cfg["codebase_root_dir"])
226 if subprocess_log(_api_test_manifest_tfm,
227 self._tfb_log_f,
228 append=True,
229 prefix=_api_test_manifest_tfm):
230
231 raise Exception("Python TFM Failed please check log: %s" %
232 self._tfb_log_f)
233 finally:
234 print("python pass after builder prepare")
235 os.chdir(build_cfg["codebase_root_dir"] + "/../")
236
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100237 def task_exec(self):
238 """ Create a build pool and execute them in parallel """
239
240 build_pool = []
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100241
Minos Galanakisea421232019-06-20 17:11:28 +0100242 # When a config is flagged as a single build config.
243 # Name is evaluated by config type
244 if self.simple_config:
245
246 build_cfg = deepcopy(self.tbm_common_cfg)
247
248 # Extract the common for all elements of config
249 for key in ["build_cmds", "required_artefacts"]:
250 try:
251 build_cfg[key] = build_cfg[key]["all"]
252 except KeyError:
253 build_cfg[key] = []
254 name = build_cfg["config_type"]
255
256 # Override _tbm_xxx paths in commands
257 # plafrom in not guaranteed without seeds so _tbm_target_platform
258 # is ignored
259 over_dict = {"_tbm_build_dir_": os.path.join(self._tbm_work_dir,
260 name),
261 "_tbm_code_dir_": build_cfg["codebase_root_dir"]}
262
263 build_cfg = self.override_tbm_cfg_params(build_cfg,
264 ["build_cmds",
265 "required_artefacts",
266 "artifact_capture_rex"],
267 **over_dict)
268
269 # Overrides path in expected artefacts
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100270 print("Loading config %s" % name)
Minos Galanakisea421232019-06-20 17:11:28 +0100271
272 build_pool.append(TFM_Builder(
273 name=name,
274 work_dir=self._tbm_work_dir,
275 cfg_dict=build_cfg,
276 build_threads=self._tbm_build_threads,
277 img_sizes=self._tbm_img_sizes,
278 relative_paths=self._tbm_relative_paths))
279 # When a seed pool is provided iterate through the entries
280 # and update platform spefific parameters
281 elif len(self._tbm_build_cfg):
Karl Zhangaff558a2020-05-15 14:28:23 +0100282 print("\r\n_tbm_build_cfg %s\r\n tbm_common_cfg %s\r\n" \
283 % (self._tbm_build_cfg, self.tbm_common_cfg))
Minos Galanakisea421232019-06-20 17:11:28 +0100284 for name, i in self._tbm_build_cfg.items():
285 # Do not modify the original config
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000286 build_cfg = self.get_build_config(i, name)
Karl Zhangaff558a2020-05-15 14:28:23 +0100287 self.pre_build(build_cfg)
Minos Galanakisea421232019-06-20 17:11:28 +0100288 # Overrides path in expected artefacts
289 print("Loading config %s" % name)
290
291 build_pool.append(TFM_Builder(
292 name=name,
293 work_dir=self._tbm_work_dir,
294 cfg_dict=build_cfg,
295 build_threads=self._tbm_build_threads,
296 img_sizes=self._tbm_img_sizes,
297 relative_paths=self._tbm_relative_paths))
298 else:
299 print("Could not find any configuration. Check the rejection list")
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100300
301 status_rep = {}
Minos Galanakisea421232019-06-20 17:11:28 +0100302 build_rep = {}
303 completed_build_count = 0
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100304 print("Build: Running %d parallel build jobs" % self._tbm_conc_builds)
305 for build_pool_slice in list_chunks(build_pool, self._tbm_conc_builds):
306
307 # Start the builds
308 for build in build_pool_slice:
309 # Only produce output for the first build
310 if build_pool_slice.index(build) != 0:
311 build.mute()
312 print("Build: Starting %s" % build.get_name())
313 build.start()
314
315 # Wait for the builds to complete
316 for build in build_pool_slice:
317 # Wait for build to finish
318 build.join()
319 # Similarly print the logs of the other builds as they complete
320 if build_pool_slice.index(build) != 0:
321 build.log()
Minos Galanakisea421232019-06-20 17:11:28 +0100322 completed_build_count += 1
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100323 print("Build: Finished %s" % build.get_name())
Minos Galanakisea421232019-06-20 17:11:28 +0100324 print("Build Progress:")
325 show_progress(completed_build_count, len(build_pool))
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100326
327 # Store status in report
328 status_rep[build.get_name()] = build.get_status()
Minos Galanakisea421232019-06-20 17:11:28 +0100329 build_rep[build.get_name()] = build.report()
330
331 # Include the original input configuration in the report
332
333 metadata = {"input_build_cfg": self._tbm_cfg,
334 "build_dir": self._tbm_work_dir
335 if not self._tbm_relative_paths
336 else resolve_rel_path(self._tbm_work_dir),
337 "time": time()}
338
339 full_rep = {"report": build_rep,
340 "_metadata_": metadata}
341
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100342 # Store the report
343 self.stash("Build Status", status_rep)
344 self.stash("Build Report", full_rep)
345
346 if self._tbm_report:
347 print("Exported build report to file:", self._tbm_report)
348 save_json(self._tbm_report, full_rep)
349
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000350 def get_build_config(self, i, name, silence=False, build_dir=None, codebase_dir=None):
351 psa_build_dir = self._tbm_work_dir + "/" + name + "/BUILD"
352 if not build_dir:
353 build_dir = os.path.join(self._tbm_work_dir, name)
354 else:
355 psa_build_dir = os.path.join(build_dir, "../../psa-arch-tests/api-tests/build")
356 build_cfg = deepcopy(self.tbm_common_cfg)
357 if not codebase_dir:
358 codebase_dir = build_cfg["codebase_root_dir"]
359 else:
360 # Would prefer to do all with the new variable
361 # However, many things use this from build_cfg elsewhere
362 build_cfg["codebase_root_dir"] = codebase_dir
363 # Extract the common for all elements of config
364 for key in ["build_cmds", "required_artefacts"]:
365 try:
366 build_cfg[key] = deepcopy(self.tbm_common_cfg[key]
367 ["all"])
368 except KeyError as E:
369 build_cfg[key] = []
370 # Extract the platform specific elements of config
371 for key in ["build_cmds", "required_artefacts"]:
372 try:
Xinyu Zhang694eb492020-11-04 18:29:08 +0800373 if i.tfm_platform in self.tbm_common_cfg[key].keys() and i.with_ns:
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000374 build_cfg[key] += deepcopy(self.tbm_common_cfg[key]
Xinyu Zhangb708f572020-09-15 11:43:46 +0800375 [i.tfm_platform])
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000376 except Exception as E:
377 pass
Karl Zhang1eed6322020-07-01 15:38:10 +0800378
379 if os.cpu_count() >= 8:
380 #run in a serviver with scripts, parallel build will use CPU numbers
381 thread_no = " -j 2"
382 else:
383 #run in a docker, usually docker with CPUs less than 8
384 thread_no = " -j " + str(os.cpu_count())
Xinyu Zhangb708f572020-09-15 11:43:46 +0800385 build_cfg["build_cmds"][0] += thread_no
Xinyu Zhang433771e2022-04-01 16:49:17 +0800386
387 # Overwrite command lines to set compiler
388 build_cfg["set_compiler_path"] %= {"compiler": i.compiler}
389 build_cfg["set_compiler_path"] += " ;\n{} --version".format(self.get_compiler_name(i.compiler))
390
391 # Overwrite command lines of cmake
Xinyu Zhangb708f572020-09-15 11:43:46 +0800392 overwrite_params = {"codebase_root_dir": build_cfg["codebase_root_dir"],
393 "tfm_platform": i.tfm_platform,
Xinyu Zhang433771e2022-04-01 16:49:17 +0800394 "compiler": self.choose_toolchain(i.compiler),
Xinyu Zhang73ed2992021-09-15 11:38:23 +0800395 "lib_model": i.lib_model,
Xinyu Zhangb708f572020-09-15 11:43:46 +0800396 "isolation_level": i.isolation_level,
397 "test_regression": i.test_regression,
398 "test_psa_api": i.test_psa_api,
399 "cmake_build_type": i.cmake_build_type,
Xinyu Zhangb708f572020-09-15 11:43:46 +0800400 "with_bl2": i.with_bl2,
401 "with_ns": i.with_ns,
Xinyu Zhang9fd74242020-10-22 11:30:50 +0800402 "profile": "" if i.profile=="N.A" else i.profile,
Xinyu Zhang9bfe8a92021-10-28 16:27:12 +0800403 "partition_ps": i.partition_ps,
Xinyu Zhangfd2e1152021-12-17 18:09:01 +0800404 "extra_params": mapExtraParams[i.extra_params]}
Xinyu Zhanga0086022020-11-10 18:11:12 +0800405 if i.test_psa_api == "IPC":
Xinyu Zhangcd1ed962020-11-11 16:00:52 +0800406 overwrite_params["test_psa_api"] += " -DINCLUDE_PANIC_TESTS=1"
Xinyu Zhang5f9fa962022-04-12 16:54:35 +0800407 if i.test_psa_api == "CRYPTO" and "musca" in i.tfm_platform:
408 overwrite_params["test_psa_api"] += " -DCC312_LEGACY_DRIVER_API_ENABLED=OFF"
Xinyu Zhang6ac0eb02022-03-31 13:19:07 +0800409 if i.tfm_platform == "arm/musca_b1/sse_200":
410 overwrite_params["test_psa_api"] += " -DITS_RAM_FS=ON -DPS_RAM_FS=ON"
Xinyu Zhangaa335912022-07-04 10:28:51 +0800411 if i.tfm_platform == "stm/stm32l562e_dk":
412 overwrite_params["test_psa_api"] += " -DITS_RAM_FS=ON -DPS_RAM_FS=ON"
Xinyu Zhangb708f572020-09-15 11:43:46 +0800413 build_cfg["config_template"] %= overwrite_params
Xinyu Zhang694eb492020-11-04 18:29:08 +0800414 if len(build_cfg["build_cmds"]) > 1:
415 overwrite_build_dir = {"_tbm_build_dir_": build_dir}
416 build_cfg["build_cmds"][1] %= overwrite_build_dir
Xinyu Zhang433771e2022-04-01 16:49:17 +0800417
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000418 return build_cfg
419
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100420 def post_eval(self):
421 """ If a single build failed fail the test """
422 try:
Minos Galanakisea421232019-06-20 17:11:28 +0100423 status_dict = self.unstash("Build Status")
424 if not status_dict:
425 raise Exception()
426 retcode_sum = sum(status_dict.values())
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100427 if retcode_sum != 0:
428 raise Exception()
429 return True
430 except Exception as e:
431 return False
432
433 def post_exec(self, eval_ret):
434 """ Generate a report and fail the script if build == unsuccessfull"""
435
436 self.print_summary()
437 if not eval_ret:
438 print("ERROR: ====> Build Failed! %s" % self.get_name())
439 self.set_status(1)
440 else:
441 print("SUCCESS: ====> Build Complete!")
442 self.set_status(0)
443
444 def get_report(self):
445 """ Expose the internal report to a new object for external classes """
446 return deepcopy(self.unstash("Build Report"))
447
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100448 def load_config(self, config, work_dir):
449 try:
450 # passing config_name param supersseeds fileparam
451 if isinstance(config, dict):
452 ret_cfg = deepcopy(config)
453 elif isinstance(config, str):
454 # If the string does not descrive a file try to look for it in
455 # work directory
456 if not os.path.isfile(config):
457 # remove path from file
458 config_2 = os.path.split(config)[-1]
459 # look in the current working directory
460 config_2 = os.path.join(work_dir, config_2)
461 if not os.path.isfile(config_2):
462 m = "Could not find cfg in %s or %s " % (config,
463 config_2)
464 raise Exception(m)
465 # If fille exists in working directory
466 else:
467 config = config_2
468 ret_cfg = load_json(config)
469
470 else:
471 raise Exception("Need to provide a valid config name or file."
472 "Please use --config/--config-file parameter.")
473 except Exception as e:
474 print("Error:%s \nCould not load a valid config" % e)
475 sys.exit(1)
476
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100477 return ret_cfg
478
479 def parse_config(self, cfg):
480 """ Parse a valid configuration file into a set of build dicts """
481
Minos Galanakisea421232019-06-20 17:11:28 +0100482 ret_cfg = {}
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100483
Minos Galanakisea421232019-06-20 17:11:28 +0100484 # Config entries which are not subject to changes during combinations
485 static_cfg = cfg["common_params"]
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100486
Minos Galanakisea421232019-06-20 17:11:28 +0100487 # Converth the code path to absolute path
488 abs_code_dir = static_cfg["codebase_root_dir"]
489 abs_code_dir = os.path.abspath(os.path.expanduser(abs_code_dir))
490 static_cfg["codebase_root_dir"] = abs_code_dir
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100491
Minos Galanakisea421232019-06-20 17:11:28 +0100492 # seed_params is an optional field. Do not proccess if it is missing
493 if "seed_params" in cfg:
494 comb_cfg = cfg["seed_params"]
495 # Generate a list of all possible confugration combinations
496 ret_cfg = TFM_Build_Manager.generate_config_list(comb_cfg,
497 static_cfg)
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100498
Xinyu Zhang2c63ce72021-07-23 14:01:59 +0800499 # valid is an optional field. Do not proccess if it is missing
500 if "valid" in cfg:
501 # Valid configurations(Need to build)
502 valid_cfg = cfg["valid"]
503 # Add valid configs to build list
504 ret_cfg.update(TFM_Build_Manager.generate_optional_list(
505 comb_cfg,
506 static_cfg,
507 valid_cfg))
508
Minos Galanakisea421232019-06-20 17:11:28 +0100509 # invalid is an optional field. Do not proccess if it is missing
510 if "invalid" in cfg:
511 # Invalid configurations(Do not build)
512 invalid_cfg = cfg["invalid"]
513 # Remove the rejected entries from the test list
Xinyu Zhang0581b082021-05-17 10:46:57 +0800514 rejection_cfg = TFM_Build_Manager.generate_optional_list(
Minos Galanakisea421232019-06-20 17:11:28 +0100515 comb_cfg,
516 static_cfg,
517 invalid_cfg)
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100518
Minos Galanakisea421232019-06-20 17:11:28 +0100519 # Subtract the two configurations
520 ret_cfg = {k: v for k, v in ret_cfg.items()
521 if k not in rejection_cfg}
522 self.simple_config = False
523 else:
524 self.simple_config = True
525 return ret_cfg, static_cfg
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100526
Minos Galanakisea421232019-06-20 17:11:28 +0100527 # ----- Override bellow methods when subclassing for other projects ----- #
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100528
Minos Galanakisea421232019-06-20 17:11:28 +0100529 def print_summary(self):
530 """ Print an comprehensive list of the build jobs with their status """
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100531
Minos Galanakisea421232019-06-20 17:11:28 +0100532 try:
533 full_rep = self.unstash("Build Report")["report"]
534 fl = ([k for k, v in full_rep.items() if v['status'] == 'Failed'])
535 ps = ([k for k, v in full_rep.items() if v['status'] == 'Success'])
536 except Exception as E:
Karl Zhangaff558a2020-05-15 14:28:23 +0100537 print("No report generated", E)
Minos Galanakisea421232019-06-20 17:11:28 +0100538 return
539 if fl:
540 print_test(t_list=fl, status="failed", tname="Builds")
541 if ps:
542 print_test(t_list=ps, status="passed", tname="Builds")
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100543
Minos Galanakisea421232019-06-20 17:11:28 +0100544 @staticmethod
545 def generate_config_list(seed_config, static_config):
546 """ Generate all possible configuration combinations from a group of
547 lists of compiler options"""
548 config_list = []
549
550 if static_config["config_type"] == "tf-m":
551 cfg_name = "TFM_Build_CFG"
552 # Ensure the fieds are sorted in the desired order
553 # seed_config can be a subset of sort order for configurations with
554 # optional parameters.
555 tags = [n for n in static_config["sort_order"]
556 if n in seed_config.keys()]
Karl Zhangaff558a2020-05-15 14:28:23 +0100557 print("!!!!!!!!!!!gen list %s\r\n" % tags)
Minos Galanakisea421232019-06-20 17:11:28 +0100558
559 data = []
560 for key in tags:
561 data.append(seed_config[key])
562 config_list = gen_cfg_combinations(cfg_name,
563 " ".join(tags),
564 *data)
565 else:
566 print("Not information for project type: %s."
567 " Please check config" % static_config["config_type"])
568
569 ret_cfg = {}
570 # Notify the user for the rejected configuations
571 for i in config_list:
Xinyu Zhang1078e812020-10-15 11:52:36 +0800572 # Convert named tuples to string in a brief format
573 config_param = []
574 config_param.append(mapPlatform[list(i)[0]])
Xinyu Zhang433771e2022-04-01 16:49:17 +0800575 config_param.append(list(i)[1].split("_")[0])
Xinyu Zhang73ed2992021-09-15 11:38:23 +0800576 if list(i)[2]: # LIB_MODEL
577 config_param.append("LIB")
578 else:
579 config_param.append("IPC")
Xinyu Zhang1078e812020-10-15 11:52:36 +0800580 config_param.append(list(i)[3]) # ISOLATION_LEVEL
581 if list(i)[4]: # TEST_REGRESSION
582 config_param.append("REG")
583 if list(i)[5] != "OFF": #TEST_PSA_API
584 config_param.append(mapTestPsaApi[list(i)[5]])
585 config_param.append(list(i)[6]) # BUILD_TYPE
Xinyu Zhang589fd052022-04-19 17:54:16 +0800586 if list(i)[7]: # BL2
Xinyu Zhang1078e812020-10-15 11:52:36 +0800587 config_param.append("BL2")
Xinyu Zhang589fd052022-04-19 17:54:16 +0800588 if list(i)[8]: # NS
Xinyu Zhang1078e812020-10-15 11:52:36 +0800589 config_param.append("NS")
Xinyu Zhang589fd052022-04-19 17:54:16 +0800590 if list(i)[9]: # PROFILE
591 config_param.append(mapProfile[list(i)[9]])
592 if list(i)[10] == "OFF": #PARTITION_PS
Xinyu Zhang9fd74242020-10-22 11:30:50 +0800593 config_param.append("PSOFF")
Xinyu Zhang589fd052022-04-19 17:54:16 +0800594 if list(i)[11]: # EXTRA_PARAMS
595 config_param.append(list(i)[11])
Xinyu Zhang1078e812020-10-15 11:52:36 +0800596 i_str = "_".join(config_param)
Karl Zhangaff558a2020-05-15 14:28:23 +0100597 ret_cfg[i_str] = i
Minos Galanakisea421232019-06-20 17:11:28 +0100598 return ret_cfg
599
600 @staticmethod
Xinyu Zhang0581b082021-05-17 10:46:57 +0800601 def generate_optional_list(seed_config,
602 static_config,
603 optional_list):
604 optional_cfg = {}
Minos Galanakisea421232019-06-20 17:11:28 +0100605
606 if static_config["config_type"] == "tf-m":
607
Xinyu Zhang0581b082021-05-17 10:46:57 +0800608 # If optional list is empty do nothing
609 if not optional_list:
610 return optional_cfg
Minos Galanakisea421232019-06-20 17:11:28 +0100611
612 tags = [n for n in static_config["sort_order"]
613 if n in seed_config.keys()]
614 sorted_default_lst = [seed_config[k] for k in tags]
615
Xinyu Zhang0581b082021-05-17 10:46:57 +0800616 # If tags are not alligned with optional list entries quit
617 if len(tags) != len(optional_list[0]):
618 print(len(tags), len(optional_list[0]))
Minos Galanakisea421232019-06-20 17:11:28 +0100619 print("Error, tags should be assigned to each "
Xinyu Zhang0581b082021-05-17 10:46:57 +0800620 "of the optional inputs")
Minos Galanakisea421232019-06-20 17:11:28 +0100621 return []
622
623 # Replace wildcard ( "*") entries with every
624 # inluded in cfg variant
Xinyu Zhang0581b082021-05-17 10:46:57 +0800625 for k in optional_list:
Minos Galanakisea421232019-06-20 17:11:28 +0100626 # Pad the omitted values with wildcard char *
627 res_list = list(k) + ["*"] * (5 - len(k))
Xinyu Zhang0581b082021-05-17 10:46:57 +0800628 print("Working on optional input: %s" % (res_list))
Minos Galanakisea421232019-06-20 17:11:28 +0100629
630 for n in range(len(res_list)):
631
632 res_list[n] = [res_list[n]] if res_list[n] != "*" \
633 else sorted_default_lst[n]
634
635 # Generate a configuration and a name for the completed array
Xinyu Zhang0581b082021-05-17 10:46:57 +0800636 op_cfg = TFM_Build_Manager.generate_config_list(
Minos Galanakisea421232019-06-20 17:11:28 +0100637 dict(zip(tags, res_list)),
638 static_config)
639
640 # Append the configuration to the existing ones
Xinyu Zhang0581b082021-05-17 10:46:57 +0800641 optional_cfg = dict(optional_cfg, **op_cfg)
Minos Galanakisea421232019-06-20 17:11:28 +0100642
Xinyu Zhang0581b082021-05-17 10:46:57 +0800643 # Notify the user for the optional configuations
644 for i in optional_cfg.keys():
645 print("Generating optional config %s" % i)
Minos Galanakisea421232019-06-20 17:11:28 +0100646 else:
647 print("Not information for project type: %s."
648 " Please check config" % static_config["config_type"])
Xinyu Zhang0581b082021-05-17 10:46:57 +0800649 return optional_cfg