blob: e83c7d9d28996ff54e482123c2424bcc9a3eb647 [file] [log] [blame]
Dean Birch5cb5a882020-01-24 11:37:13 +00001#!/usr/bin/env python
2"""
3Script for querying specific build configurations of TF-M.
4Can list available build configurations, and can give environment
5variables to build a specific build configuration using run-build.sh
6"""
7
8import argparse
9import os
10import sys
11
12from tfm_ci_pylib.tfm_build_manager import TFM_Build_Manager
Dean Birch5cb5a882020-01-24 11:37:13 +000013from build_helper.build_helper_configs import _builtin_configs
14
15
16__copyright__ = """
17/*
Xinyu Zhang0aebb3d2022-04-11 18:27:12 +080018 * Copyright (c) 2020-2022, Arm Limited. All rights reserved.
Dean Birch5cb5a882020-01-24 11:37:13 +000019 *
20 * SPDX-License-Identifier: BSD-3-Clause
21 *
22 */
23 """
24
25
26def get_build_manager(group=None):
27 """Get a TFM_Build_Manager instance, silencing stdout"""
Xinyu Zhang0aebb3d2022-04-11 18:27:12 +080028 if not group:
29 print("No config group selected!")
30 return
31 config = _builtin_configs[group]
Dean Birch5cb5a882020-01-24 11:37:13 +000032 _dir = os.getcwd()
33 # Block default stdout from __init__
34 sys.stdout = open(os.devnull, "w")
35 build_manager = TFM_Build_Manager(_dir, _dir, config)
36 # Allow stdout again
37 sys.stdout = sys.__stdout__
38 return build_manager
39
40
41def list_configs(group):
42 """Lists available configurations"""
43 build_manager = get_build_manager(group)
Dean Bircha6ede7e2020-03-13 14:00:33 +000044 return build_manager.get_config()
Dean Birch5cb5a882020-01-24 11:37:13 +000045
46
Xinyu Zhang46b37182023-06-30 15:36:44 +080047def print_build_configs(config, group=None, silence_stderr=False):
Dean Birch5cb5a882020-01-24 11:37:13 +000048 """Prints particular configuration environment variables"""
49 build_manager = get_build_manager(group)
Xinyu Zhang46b37182023-06-30 15:36:44 +080050 params = build_manager.get_build_configs(config, silence_stderr=silence_stderr)
51 for name in params:
52 print("{}={}".format(name, params[name]))
Dean Birch5cb5a882020-01-24 11:37:13 +000053
54
Xinyu Zhang46b37182023-06-30 15:36:44 +080055def print_build_commands(config, cmd_type, group=None, jobs=None):
56 """Prints particular commands to be run"""
Dean Birchd0f9f8c2020-03-26 11:10:33 +000057 build_manager = get_build_manager(group)
Xinyu Zhang46b37182023-06-30 15:36:44 +080058 cmd = build_manager.get_build_commands(config, silence_stderr=True, jobs=jobs)
59 print(cmd[cmd_type])
Dean Birchd0f9f8c2020-03-26 11:10:33 +000060
61
Dean Birch5cb5a882020-01-24 11:37:13 +000062if __name__ == "__main__":
63 PARSER = argparse.ArgumentParser(description="Extract build configurations.")
64 PARSER.add_argument(
65 "config",
66 default=None,
67 nargs="?",
68 help="Configuration to print environment variables for. "
69 "Then run-build.sh can be run directly with these set. "
70 "If not specified, the available configurations are printed",
71 )
72 PARSER.add_argument(
Dean Birchd0f9f8c2020-03-26 11:10:33 +000073 "-b",
74 "--build_commands",
75 default=None,
Xinyu Zhang2d46a8b2023-08-15 17:43:51 +080076 choices=['set_compiler', 'spe_cmake_config', 'nspe_cmake_config', 'spe_cmake_build', 'nspe_cmake_build', 'post_build'],
Xinyu Zhang46b37182023-06-30 15:36:44 +080077 help="Print selected type of build commands to be run for current configuration."
78 )
79 PARSER.add_argument(
80 "--config_params",
81 default=None,
Dean Birchd0f9f8c2020-03-26 11:10:33 +000082 action='store_true',
Xinyu Zhang46b37182023-06-30 15:36:44 +080083 help="List config parameters of current configuration."
Dean Birchd0f9f8c2020-03-26 11:10:33 +000084 )
85 PARSER.add_argument(
Dean Birch5cb5a882020-01-24 11:37:13 +000086 "-g",
87 "--group",
Dean Bircha6ede7e2020-03-13 14:00:33 +000088 default=[],
89 action="append",
Dean Birchd0f9f8c2020-03-26 11:10:33 +000090 help="Only list configurations under a certain group. "
91 "'all' will look through all configurations. "
Xinyu Zhang0aebb3d2022-04-11 18:27:12 +080092 "Leaving blank will just look at config 'all'.",
Dean Birchd0f9f8c2020-03-26 11:10:33 +000093 choices=list(_builtin_configs.keys())+['all'],
Dean Birch5cb5a882020-01-24 11:37:13 +000094 )
Paul Sokolovskycba7ee42023-04-19 13:21:33 +030095 PARSER.add_argument(
96 "-j",
97 "--jobs",
98 help="Pass -j option down to the build system (# of parallel jobs)."
99 )
Dean Birch5cb5a882020-01-24 11:37:13 +0000100 ARGS = PARSER.parse_args()
Xinyu Zhang0aebb3d2022-04-11 18:27:12 +0800101 if not ARGS.group or ARGS.group == ['all']:
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000102 ARGS.group = list(_builtin_configs.keys())
Dean Birch5cb5a882020-01-24 11:37:13 +0000103
Dean Bircha6ede7e2020-03-13 14:00:33 +0000104 all_configs = set()
105 for group in ARGS.group:
106 # By default print available configs
107 if not ARGS.config:
108 all_configs.update(list_configs(group))
109 else:
110 try:
Xinyu Zhang46b37182023-06-30 15:36:44 +0800111 if ARGS.config_params:
112 print_build_configs(ARGS.config, group=group, silence_stderr=True)
113 break
114 if ARGS.build_commands:
115 print_build_commands(ARGS.config, ARGS.build_commands, group=group, jobs=ARGS.jobs)
116 break
Dean Birchd0f9f8c2020-03-26 11:10:33 +0000117 except (SystemExit, KeyError):
118 if group == ARGS.group[-1] or ARGS.group == []:
Dean Bircha6ede7e2020-03-13 14:00:33 +0000119 print(
120 "Could not find configuration {} in groups {}".format(
121 ARGS.config, ARGS.group
122 )
123 )
124
125 for config in all_configs:
126 print(config)