blob: 53ef5c6493968c6a0b16de50b7633010d605c583 [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
Xinyu Zhangb18ae742023-04-25 14:33:27 +080093 def map_params(self, params, maps):
94 build_configs = ""
Xinyu Zhangfc061dd2022-07-26 14:52:56 +080095 param_list = params.split(", ")
96 for param in param_list:
Xinyu Zhangb18ae742023-04-25 14:33:27 +080097 build_configs += maps[param]
98 return build_configs
Xinyu Zhangfc061dd2022-07-26 14:52:56 +080099
Dean Bircha6ede7e2020-03-13 14:00:33 +0000100 def get_config(self):
101 return list(self._tbm_build_cfg.keys())
Dean Birch5cb5a882020-01-24 11:37:13 +0000102
Xinyu Zhang46b37182023-06-30 15:36:44 +0800103 def get_build_configs(self, config, silence_stderr=False):
Dean Birch5cb5a882020-01-24 11:37:13 +0000104 """
Xinyu Zhang46b37182023-06-30 15:36:44 +0800105 Return build config variables needed by the input config.
Dean Birch5cb5a882020-01-24 11:37:13 +0000106 """
107 if config not in self._tbm_build_cfg:
Dean Bircha6ede7e2020-03-13 14:00:33 +0000108 if not silence_stderr:
109 print("Error: no such config {}".format(config), file=sys.stderr)
Dean Birch5cb5a882020-01-24 11:37:13 +0000110 sys.exit(1)
111 config_details = self._tbm_build_cfg[config]
Xinyu Zhang46b37182023-06-30 15:36:44 +0800112 config_params = {
113 "CONFIG_NAME": config,
114 "TFM_PLATFORM": config_details.tfm_platform,
115 "COMPILER": config_details.compiler,
116 "ISOLATION_LEVEL": config_details.isolation_level,
117 "TEST_REGRESSION": config_details.test_regression,
118 "TEST_PSA_API": config_details.test_psa_api,
119 "CMAKE_BUILD_TYPE": config_details.cmake_build_type,
120 "BL2": config_details.with_bl2,
121 "PROFILE": "N.A" if not config_details.profile else config_details.profile,
122 "EXTRA_PARAMS": "N.A" if not config_details.extra_params else config_details.extra_params,
123 }
124 return config_params
Dean Birch5cb5a882020-01-24 11:37:13 +0000125
Xinyu Zhang46b37182023-06-30 15:36:44 +0800126 def get_build_commands(self, config, silence_stderr=False, jobs=None):
127 """
128 Return selected type of commands to be run to build the input config.
129 """
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000130 config_details = self._tbm_build_cfg[config]
131 codebase_dir = os.path.join(os.getcwd(),"trusted-firmware-m")
Xinyu Zhangb708f572020-09-15 11:43:46 +0800132 build_dir=os.path.join(os.getcwd(),"trusted-firmware-m/build")
Xinyu Zhang433771e2022-04-01 16:49:17 +0800133 build_config = self.get_build_config(config_details, config, \
134 silence=silence_stderr, \
135 build_dir=build_dir, \
Paul Sokolovskycba7ee42023-04-19 13:21:33 +0300136 codebase_dir=codebase_dir, \
137 jobs=jobs)
Xinyu Zhang46b37182023-06-30 15:36:44 +0800138 build_commands = {
139 'set_compiler': build_config['set_compiler_path'],
140 'cmake_config': build_config['config_template'],
141 'cmake_build': build_config['cmake_build'],
142 'post_build': build_config['post_build']
143 }
144 return build_commands
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000145
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100146 def pre_eval(self):
147 """ Tests that need to be run in set-up state """
148 return True
149
150 def pre_exec(self, eval_ret):
151 """ """
152
Minos Galanakisea421232019-06-20 17:11:28 +0100153 def override_tbm_cfg_params(self, config, override_keys, **params):
154 """ Using a dictionay as input, for each key defined in
155 override_keys it will replace the config[key] entries with
156 the key=value parameters provided """
157
158 for key in override_keys:
159 if isinstance(config[key], list):
160 config[key] = [n % params for n in config[key]]
161 elif isinstance(config[key], str):
162 config[key] = config[key] % params
163 else:
164 raise Exception("Config does not contain key %s "
165 "of type %s" % (key, config[key]))
166 return config
167
Karl Zhangaff558a2020-05-15 14:28:23 +0100168 def pre_build(self, build_cfg):
169 print("pre_build start %s \r\nself._tfb_cfg %s\r\n" %
170 (self, build_cfg))
171
172 try:
173 if self._tfb_code_base_updated:
174 print("Code base has been updated")
175 return True
176
177 self._tfb_code_base_updated = True
178
179 if "build_psa_api" in build_cfg:
180 # FF IPC build needs repo manifest update for TFM and PSA arch test
181 if "build_ff_ipc" in build_cfg:
182 print("Checkout to FF IPC code base")
183 os.chdir(build_cfg["codebase_root_dir"] + "/../psa-arch-tests/api-tests")
184 _api_test_manifest = "git checkout . ; python3 tools/scripts/manifest_update.py"
185 if subprocess_log(_api_test_manifest,
186 self._tfb_log_f,
187 append=True,
188 prefix=_api_test_manifest):
189
190 raise Exception("Python Failed please check log: %s" %
191 self._tfb_log_f)
192
193 _api_test_manifest_tfm = "python3 tools/tfm_parse_manifest_list.py -m tools/tfm_psa_ff_test_manifest_list.yaml append"
194 os.chdir(build_cfg["codebase_root_dir"])
195 if subprocess_log(_api_test_manifest_tfm,
196 self._tfb_log_f,
197 append=True,
198 prefix=_api_test_manifest_tfm):
199
200 raise Exception("Python TFM Failed please check log: %s" %
201 self._tfb_log_f)
202 return True
203
204 print("Checkout to default code base")
205 os.chdir(build_cfg["codebase_root_dir"] + "/../psa-arch-tests/api-tests")
206 _api_test_manifest = "git checkout ."
207 if subprocess_log(_api_test_manifest,
208 self._tfb_log_f,
209 append=True,
210 prefix=_api_test_manifest):
211
212 raise Exception("Python Failed please check log: %s" %
213 self._tfb_log_f)
214
215 _api_test_manifest_tfm = "python3 tools/tfm_parse_manifest_list.py"
216 os.chdir(build_cfg["codebase_root_dir"])
217 if subprocess_log(_api_test_manifest_tfm,
218 self._tfb_log_f,
219 append=True,
220 prefix=_api_test_manifest_tfm):
221
222 raise Exception("Python TFM Failed please check log: %s" %
223 self._tfb_log_f)
224 finally:
225 print("python pass after builder prepare")
226 os.chdir(build_cfg["codebase_root_dir"] + "/../")
227
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100228 def task_exec(self):
229 """ Create a build pool and execute them in parallel """
230
231 build_pool = []
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100232
Minos Galanakisea421232019-06-20 17:11:28 +0100233 # When a config is flagged as a single build config.
234 # Name is evaluated by config type
235 if self.simple_config:
236
237 build_cfg = deepcopy(self.tbm_common_cfg)
238
239 # Extract the common for all elements of config
Xinyu Zhang46b37182023-06-30 15:36:44 +0800240 try:
241 build_cfg["required_artefacts"] = build_cfg["required_artefacts"]["all"]
242 except KeyError:
243 build_cfg["required_artefacts"] = []
Minos Galanakisea421232019-06-20 17:11:28 +0100244 name = build_cfg["config_type"]
245
246 # Override _tbm_xxx paths in commands
247 # plafrom in not guaranteed without seeds so _tbm_target_platform
248 # is ignored
249 over_dict = {"_tbm_build_dir_": os.path.join(self._tbm_work_dir,
250 name),
251 "_tbm_code_dir_": build_cfg["codebase_root_dir"]}
252
253 build_cfg = self.override_tbm_cfg_params(build_cfg,
Xinyu Zhang46b37182023-06-30 15:36:44 +0800254 ["post_build",
Minos Galanakisea421232019-06-20 17:11:28 +0100255 "required_artefacts",
256 "artifact_capture_rex"],
257 **over_dict)
258
259 # Overrides path in expected artefacts
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100260 print("Loading config %s" % name)
Minos Galanakisea421232019-06-20 17:11:28 +0100261
262 build_pool.append(TFM_Builder(
263 name=name,
264 work_dir=self._tbm_work_dir,
265 cfg_dict=build_cfg,
266 build_threads=self._tbm_build_threads,
267 img_sizes=self._tbm_img_sizes,
268 relative_paths=self._tbm_relative_paths))
269 # When a seed pool is provided iterate through the entries
270 # and update platform spefific parameters
271 elif len(self._tbm_build_cfg):
Karl Zhangaff558a2020-05-15 14:28:23 +0100272 print("\r\n_tbm_build_cfg %s\r\n tbm_common_cfg %s\r\n" \
273 % (self._tbm_build_cfg, self.tbm_common_cfg))
Minos Galanakisea421232019-06-20 17:11:28 +0100274 for name, i in self._tbm_build_cfg.items():
275 # Do not modify the original config
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000276 build_cfg = self.get_build_config(i, name)
Karl Zhangaff558a2020-05-15 14:28:23 +0100277 self.pre_build(build_cfg)
Minos Galanakisea421232019-06-20 17:11:28 +0100278 # Overrides path in expected artefacts
279 print("Loading config %s" % name)
280
281 build_pool.append(TFM_Builder(
282 name=name,
283 work_dir=self._tbm_work_dir,
284 cfg_dict=build_cfg,
285 build_threads=self._tbm_build_threads,
286 img_sizes=self._tbm_img_sizes,
287 relative_paths=self._tbm_relative_paths))
288 else:
289 print("Could not find any configuration. Check the rejection list")
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100290
291 status_rep = {}
Minos Galanakisea421232019-06-20 17:11:28 +0100292 build_rep = {}
293 completed_build_count = 0
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100294 print("Build: Running %d parallel build jobs" % self._tbm_conc_builds)
295 for build_pool_slice in list_chunks(build_pool, self._tbm_conc_builds):
296
297 # Start the builds
298 for build in build_pool_slice:
299 # Only produce output for the first build
300 if build_pool_slice.index(build) != 0:
301 build.mute()
302 print("Build: Starting %s" % build.get_name())
303 build.start()
304
305 # Wait for the builds to complete
306 for build in build_pool_slice:
307 # Wait for build to finish
308 build.join()
309 # Similarly print the logs of the other builds as they complete
310 if build_pool_slice.index(build) != 0:
311 build.log()
Minos Galanakisea421232019-06-20 17:11:28 +0100312 completed_build_count += 1
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100313 print("Build: Finished %s" % build.get_name())
Minos Galanakisea421232019-06-20 17:11:28 +0100314 print("Build Progress:")
315 show_progress(completed_build_count, len(build_pool))
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100316
317 # Store status in report
318 status_rep[build.get_name()] = build.get_status()
Minos Galanakisea421232019-06-20 17:11:28 +0100319 build_rep[build.get_name()] = build.report()
320
321 # Include the original input configuration in the report
322
323 metadata = {"input_build_cfg": self._tbm_cfg,
324 "build_dir": self._tbm_work_dir
325 if not self._tbm_relative_paths
326 else resolve_rel_path(self._tbm_work_dir),
327 "time": time()}
328
329 full_rep = {"report": build_rep,
330 "_metadata_": metadata}
331
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100332 # Store the report
333 self.stash("Build Status", status_rep)
334 self.stash("Build Report", full_rep)
335
336 if self._tbm_report:
337 print("Exported build report to file:", self._tbm_report)
338 save_json(self._tbm_report, full_rep)
339
Paul Sokolovskycba7ee42023-04-19 13:21:33 +0300340 def get_build_config(self, i, name, silence=False, build_dir=None, codebase_dir=None, jobs=None):
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000341 psa_build_dir = self._tbm_work_dir + "/" + name + "/BUILD"
342 if not build_dir:
343 build_dir = os.path.join(self._tbm_work_dir, name)
344 else:
345 psa_build_dir = os.path.join(build_dir, "../../psa-arch-tests/api-tests/build")
346 build_cfg = deepcopy(self.tbm_common_cfg)
347 if not codebase_dir:
348 codebase_dir = build_cfg["codebase_root_dir"]
349 else:
350 # Would prefer to do all with the new variable
351 # However, many things use this from build_cfg elsewhere
352 build_cfg["codebase_root_dir"] = codebase_dir
353 # Extract the common for all elements of config
Xinyu Zhang46b37182023-06-30 15:36:44 +0800354 try:
355 build_cfg["required_artefacts"] = deepcopy(self.tbm_common_cfg["required_artefacts"]["all"])
356 except KeyError as E:
357 build_cfg["required_artefacts"] = []
358 build_cfg["post_build"] = ""
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000359 # Extract the platform specific elements of config
Xinyu Zhang46b37182023-06-30 15:36:44 +0800360 for key in ["post_build", "required_artefacts"]:
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000361 try:
Xinyu Zhangfb80b5d2022-07-26 15:42:26 +0800362 if i.tfm_platform in self.tbm_common_cfg[key].keys():
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000363 build_cfg[key] += deepcopy(self.tbm_common_cfg[key]
Xinyu Zhangb708f572020-09-15 11:43:46 +0800364 [i.tfm_platform])
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000365 except Exception as E:
366 pass
Karl Zhang1eed6322020-07-01 15:38:10 +0800367
Paul Sokolovskycba7ee42023-04-19 13:21:33 +0300368 if jobs is None:
369 if os.cpu_count() >= 8:
370 #run in a serviver with scripts, parallel build will use CPU numbers
371 jobs = 2
372 else:
373 #run in a docker, usually docker with CPUs less than 8
374 jobs = os.cpu_count()
375
376 thread_no = " -j {} ".format(jobs)
Xinyu Zhang46b37182023-06-30 15:36:44 +0800377 build_cfg["cmake_build"] += thread_no
Xinyu Zhang433771e2022-04-01 16:49:17 +0800378
379 # Overwrite command lines to set compiler
380 build_cfg["set_compiler_path"] %= {"compiler": i.compiler}
381 build_cfg["set_compiler_path"] += " ;\n{} --version".format(self.get_compiler_name(i.compiler))
382
383 # Overwrite command lines of cmake
Xinyu Zhangb708f572020-09-15 11:43:46 +0800384 overwrite_params = {"codebase_root_dir": build_cfg["codebase_root_dir"],
385 "tfm_platform": i.tfm_platform,
Xinyu Zhang433771e2022-04-01 16:49:17 +0800386 "compiler": self.choose_toolchain(i.compiler),
Xinyu Zhangb708f572020-09-15 11:43:46 +0800387 "isolation_level": i.isolation_level,
Xinyu Zhangb18ae742023-04-25 14:33:27 +0800388 "test_regression": self.map_params(i.test_regression, mapRegTest),
Xinyu Zhangb708f572020-09-15 11:43:46 +0800389 "test_psa_api": i.test_psa_api,
390 "cmake_build_type": i.cmake_build_type,
Xinyu Zhangb708f572020-09-15 11:43:46 +0800391 "with_bl2": i.with_bl2,
Bence Balogh79fda442022-10-14 18:01:37 +0200392 "profile": "" if i.profile=="N.A" else i.profile}
393 # The extra params can also contain paths with "codebase_root_dir" and
394 # these also need to be substituted
Xinyu Zhangb18ae742023-04-25 14:33:27 +0800395 overwrite_params["extra_params"] = self.map_params(i.extra_params, mapExtraParams) % overwrite_params
Bence Balogh79fda442022-10-14 18:01:37 +0200396
Xinyu Zhanga0086022020-11-10 18:11:12 +0800397 if i.test_psa_api == "IPC":
Xinyu Zhangcd1ed962020-11-11 16:00:52 +0800398 overwrite_params["test_psa_api"] += " -DINCLUDE_PANIC_TESTS=1"
Xinyu Zhang5f9fa962022-04-12 16:54:35 +0800399 if i.test_psa_api == "CRYPTO" and "musca" in i.tfm_platform:
400 overwrite_params["test_psa_api"] += " -DCC312_LEGACY_DRIVER_API_ENABLED=OFF"
Mark Horvathef57baa2022-09-12 13:36:36 +0200401 if i.tfm_platform == "arm/musca_b1":
Xinyu Zhangab9d1ea2022-12-23 17:11:22 +0800402 overwrite_params["test_psa_api"] += " -DOTP_NV_COUNTERS_RAM_EMULATION=ON"
Xinyu Zhangb708f572020-09-15 11:43:46 +0800403 build_cfg["config_template"] %= overwrite_params
Xinyu Zhang46b37182023-06-30 15:36:44 +0800404 build_cfg["post_build"] %= {"_tbm_build_dir_": build_dir}
Xinyu Zhang433771e2022-04-01 16:49:17 +0800405
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000406 return build_cfg
407
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100408 def post_eval(self):
409 """ If a single build failed fail the test """
410 try:
Minos Galanakisea421232019-06-20 17:11:28 +0100411 status_dict = self.unstash("Build Status")
412 if not status_dict:
413 raise Exception()
414 retcode_sum = sum(status_dict.values())
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100415 if retcode_sum != 0:
416 raise Exception()
417 return True
418 except Exception as e:
419 return False
420
421 def post_exec(self, eval_ret):
422 """ Generate a report and fail the script if build == unsuccessfull"""
423
424 self.print_summary()
425 if not eval_ret:
426 print("ERROR: ====> Build Failed! %s" % self.get_name())
427 self.set_status(1)
428 else:
429 print("SUCCESS: ====> Build Complete!")
430 self.set_status(0)
431
432 def get_report(self):
433 """ Expose the internal report to a new object for external classes """
434 return deepcopy(self.unstash("Build Report"))
435
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100436 def load_config(self, config, work_dir):
437 try:
438 # passing config_name param supersseeds fileparam
439 if isinstance(config, dict):
440 ret_cfg = deepcopy(config)
441 elif isinstance(config, str):
442 # If the string does not descrive a file try to look for it in
443 # work directory
444 if not os.path.isfile(config):
445 # remove path from file
446 config_2 = os.path.split(config)[-1]
447 # look in the current working directory
448 config_2 = os.path.join(work_dir, config_2)
449 if not os.path.isfile(config_2):
450 m = "Could not find cfg in %s or %s " % (config,
451 config_2)
452 raise Exception(m)
453 # If fille exists in working directory
454 else:
455 config = config_2
456 ret_cfg = load_json(config)
457
458 else:
459 raise Exception("Need to provide a valid config name or file."
460 "Please use --config/--config-file parameter.")
461 except Exception as e:
462 print("Error:%s \nCould not load a valid config" % e)
463 sys.exit(1)
464
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100465 return ret_cfg
466
467 def parse_config(self, cfg):
468 """ Parse a valid configuration file into a set of build dicts """
469
Minos Galanakisea421232019-06-20 17:11:28 +0100470 ret_cfg = {}
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100471
Minos Galanakisea421232019-06-20 17:11:28 +0100472 # Config entries which are not subject to changes during combinations
473 static_cfg = cfg["common_params"]
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100474
Minos Galanakisea421232019-06-20 17:11:28 +0100475 # Converth the code path to absolute path
476 abs_code_dir = static_cfg["codebase_root_dir"]
477 abs_code_dir = os.path.abspath(os.path.expanduser(abs_code_dir))
478 static_cfg["codebase_root_dir"] = abs_code_dir
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100479
Minos Galanakisea421232019-06-20 17:11:28 +0100480 # seed_params is an optional field. Do not proccess if it is missing
481 if "seed_params" in cfg:
482 comb_cfg = cfg["seed_params"]
483 # Generate a list of all possible confugration combinations
484 ret_cfg = TFM_Build_Manager.generate_config_list(comb_cfg,
485 static_cfg)
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100486
Xinyu Zhang2c63ce72021-07-23 14:01:59 +0800487 # valid is an optional field. Do not proccess if it is missing
488 if "valid" in cfg:
489 # Valid configurations(Need to build)
490 valid_cfg = cfg["valid"]
491 # Add valid configs to build list
492 ret_cfg.update(TFM_Build_Manager.generate_optional_list(
493 comb_cfg,
494 static_cfg,
495 valid_cfg))
496
Minos Galanakisea421232019-06-20 17:11:28 +0100497 # invalid is an optional field. Do not proccess if it is missing
498 if "invalid" in cfg:
499 # Invalid configurations(Do not build)
500 invalid_cfg = cfg["invalid"]
501 # Remove the rejected entries from the test list
Xinyu Zhang0581b082021-05-17 10:46:57 +0800502 rejection_cfg = TFM_Build_Manager.generate_optional_list(
Minos Galanakisea421232019-06-20 17:11:28 +0100503 comb_cfg,
504 static_cfg,
505 invalid_cfg)
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100506
Minos Galanakisea421232019-06-20 17:11:28 +0100507 # Subtract the two configurations
508 ret_cfg = {k: v for k, v in ret_cfg.items()
509 if k not in rejection_cfg}
510 self.simple_config = False
511 else:
512 self.simple_config = True
513 return ret_cfg, static_cfg
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100514
Minos Galanakisea421232019-06-20 17:11:28 +0100515 # ----- Override bellow methods when subclassing for other projects ----- #
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100516
Minos Galanakisea421232019-06-20 17:11:28 +0100517 def print_summary(self):
518 """ Print an comprehensive list of the build jobs with their status """
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100519
Minos Galanakisea421232019-06-20 17:11:28 +0100520 try:
521 full_rep = self.unstash("Build Report")["report"]
522 fl = ([k for k, v in full_rep.items() if v['status'] == 'Failed'])
523 ps = ([k for k, v in full_rep.items() if v['status'] == 'Success'])
524 except Exception as E:
Karl Zhangaff558a2020-05-15 14:28:23 +0100525 print("No report generated", E)
Minos Galanakisea421232019-06-20 17:11:28 +0100526 return
527 if fl:
528 print_test(t_list=fl, status="failed", tname="Builds")
529 if ps:
530 print_test(t_list=ps, status="passed", tname="Builds")
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100531
Minos Galanakisea421232019-06-20 17:11:28 +0100532 @staticmethod
533 def generate_config_list(seed_config, static_config):
534 """ Generate all possible configuration combinations from a group of
535 lists of compiler options"""
536 config_list = []
537
538 if static_config["config_type"] == "tf-m":
539 cfg_name = "TFM_Build_CFG"
540 # Ensure the fieds are sorted in the desired order
541 # seed_config can be a subset of sort order for configurations with
542 # optional parameters.
543 tags = [n for n in static_config["sort_order"]
544 if n in seed_config.keys()]
Karl Zhangaff558a2020-05-15 14:28:23 +0100545 print("!!!!!!!!!!!gen list %s\r\n" % tags)
Minos Galanakisea421232019-06-20 17:11:28 +0100546
547 data = []
548 for key in tags:
549 data.append(seed_config[key])
550 config_list = gen_cfg_combinations(cfg_name,
551 " ".join(tags),
552 *data)
553 else:
554 print("Not information for project type: %s."
555 " Please check config" % static_config["config_type"])
556
557 ret_cfg = {}
558 # Notify the user for the rejected configuations
559 for i in config_list:
Xinyu Zhang1078e812020-10-15 11:52:36 +0800560 # Convert named tuples to string in a brief format
561 config_param = []
562 config_param.append(mapPlatform[list(i)[0]])
Xinyu Zhang433771e2022-04-01 16:49:17 +0800563 config_param.append(list(i)[1].split("_")[0])
Summer Qin379abb62022-10-08 16:41:54 +0800564 config_param.append(list(i)[2]) # ISOLATION_LEVEL
Xinyu Zhangb18ae742023-04-25 14:33:27 +0800565 if list(i)[3] != "OFF": # TEST_REGRESSION
566 config_param.append(list(i)[3].replace(", ", "_"))
Summer Qin379abb62022-10-08 16:41:54 +0800567 if list(i)[4] != "OFF": #TEST_PSA_API
568 config_param.append(mapTestPsaApi[list(i)[4]])
569 config_param.append(list(i)[5]) # BUILD_TYPE
570 if list(i)[6]: # BL2
Xinyu Zhang1078e812020-10-15 11:52:36 +0800571 config_param.append("BL2")
Summer Qin379abb62022-10-08 16:41:54 +0800572 if list(i)[7]: # PROFILE
573 config_param.append(mapProfile[list(i)[7]])
574 if list(i)[8]: # EXTRA_PARAMS
575 config_param.append(list(i)[8].replace(", ", "_"))
Xinyu Zhang1078e812020-10-15 11:52:36 +0800576 i_str = "_".join(config_param)
Karl Zhangaff558a2020-05-15 14:28:23 +0100577 ret_cfg[i_str] = i
Minos Galanakisea421232019-06-20 17:11:28 +0100578 return ret_cfg
579
580 @staticmethod
Xinyu Zhang0581b082021-05-17 10:46:57 +0800581 def generate_optional_list(seed_config,
582 static_config,
583 optional_list):
584 optional_cfg = {}
Minos Galanakisea421232019-06-20 17:11:28 +0100585
586 if static_config["config_type"] == "tf-m":
587
Xinyu Zhang0581b082021-05-17 10:46:57 +0800588 # If optional list is empty do nothing
589 if not optional_list:
590 return optional_cfg
Minos Galanakisea421232019-06-20 17:11:28 +0100591
592 tags = [n for n in static_config["sort_order"]
593 if n in seed_config.keys()]
594 sorted_default_lst = [seed_config[k] for k in tags]
595
Xinyu Zhang0581b082021-05-17 10:46:57 +0800596 # If tags are not alligned with optional list entries quit
597 if len(tags) != len(optional_list[0]):
598 print(len(tags), len(optional_list[0]))
Minos Galanakisea421232019-06-20 17:11:28 +0100599 print("Error, tags should be assigned to each "
Xinyu Zhang0581b082021-05-17 10:46:57 +0800600 "of the optional inputs")
Minos Galanakisea421232019-06-20 17:11:28 +0100601 return []
602
603 # Replace wildcard ( "*") entries with every
604 # inluded in cfg variant
Xinyu Zhang0581b082021-05-17 10:46:57 +0800605 for k in optional_list:
Minos Galanakisea421232019-06-20 17:11:28 +0100606 # Pad the omitted values with wildcard char *
607 res_list = list(k) + ["*"] * (5 - len(k))
Xinyu Zhang0581b082021-05-17 10:46:57 +0800608 print("Working on optional input: %s" % (res_list))
Minos Galanakisea421232019-06-20 17:11:28 +0100609
610 for n in range(len(res_list)):
611
612 res_list[n] = [res_list[n]] if res_list[n] != "*" \
613 else sorted_default_lst[n]
614
615 # Generate a configuration and a name for the completed array
Xinyu Zhang0581b082021-05-17 10:46:57 +0800616 op_cfg = TFM_Build_Manager.generate_config_list(
Minos Galanakisea421232019-06-20 17:11:28 +0100617 dict(zip(tags, res_list)),
618 static_config)
619
620 # Append the configuration to the existing ones
Xinyu Zhang0581b082021-05-17 10:46:57 +0800621 optional_cfg = dict(optional_cfg, **op_cfg)
Minos Galanakisea421232019-06-20 17:11:28 +0100622
Xinyu Zhang0581b082021-05-17 10:46:57 +0800623 # Notify the user for the optional configuations
624 for i in optional_cfg.keys():
625 print("Generating optional config %s" % i)
Minos Galanakisea421232019-06-20 17:11:28 +0100626 else:
627 print("Not information for project type: %s."
628 " Please check config" % static_config["config_type"])
Xinyu Zhang0581b082021-05-17 10:46:57 +0800629 return optional_cfg