Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | Script for querying specific build configurations of TF-M. |
| 4 | Can list available build configurations, and can give environment |
| 5 | variables to build a specific build configuration using run-build.sh |
| 6 | """ |
| 7 | |
| 8 | import argparse |
| 9 | import os |
| 10 | import sys |
| 11 | |
| 12 | from tfm_ci_pylib.tfm_build_manager import TFM_Build_Manager |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 13 | from build_helper.build_helper_configs import _builtin_configs |
| 14 | |
| 15 | |
| 16 | __copyright__ = """ |
| 17 | /* |
Xinyu Zhang | 0aebb3d | 2022-04-11 18:27:12 +0800 | [diff] [blame] | 18 | * Copyright (c) 2020-2022, Arm Limited. All rights reserved. |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 19 | * |
| 20 | * SPDX-License-Identifier: BSD-3-Clause |
| 21 | * |
| 22 | */ |
| 23 | """ |
| 24 | |
| 25 | |
| 26 | def get_build_manager(group=None): |
| 27 | """Get a TFM_Build_Manager instance, silencing stdout""" |
Xinyu Zhang | 0aebb3d | 2022-04-11 18:27:12 +0800 | [diff] [blame] | 28 | if not group: |
| 29 | print("No config group selected!") |
| 30 | return |
| 31 | config = _builtin_configs[group] |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 32 | _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 | |
| 41 | def list_configs(group): |
| 42 | """Lists available configurations""" |
| 43 | build_manager = get_build_manager(group) |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 44 | return build_manager.get_config() |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 45 | |
| 46 | |
Xinyu Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 47 | def print_build_configs(config, group=None, silence_stderr=False): |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 48 | """Prints particular configuration environment variables""" |
| 49 | build_manager = get_build_manager(group) |
Xinyu Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 50 | params = build_manager.get_build_configs(config, silence_stderr=silence_stderr) |
| 51 | for name in params: |
| 52 | print("{}={}".format(name, params[name])) |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 53 | |
| 54 | |
Xinyu Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 55 | def print_build_commands(config, cmd_type, group=None, jobs=None): |
| 56 | """Prints particular commands to be run""" |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 57 | build_manager = get_build_manager(group) |
Xinyu Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 58 | cmd = build_manager.get_build_commands(config, silence_stderr=True, jobs=jobs) |
| 59 | print(cmd[cmd_type]) |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 60 | |
| 61 | |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 62 | if __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 Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 73 | "-b", |
| 74 | "--build_commands", |
| 75 | default=None, |
Xinyu Zhang | a88a2eb | 2023-08-15 17:43:51 +0800 | [diff] [blame] | 76 | choices=['set_compiler', 'spe_cmake_config', 'nspe_cmake_config', 'spe_cmake_build', 'nspe_cmake_build', 'post_build'], |
Xinyu Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 77 | 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 Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 82 | action='store_true', |
Xinyu Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 83 | help="List config parameters of current configuration." |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 84 | ) |
| 85 | PARSER.add_argument( |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 86 | "-g", |
| 87 | "--group", |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 88 | default=[], |
| 89 | action="append", |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 90 | help="Only list configurations under a certain group. " |
| 91 | "'all' will look through all configurations. " |
Xinyu Zhang | 0aebb3d | 2022-04-11 18:27:12 +0800 | [diff] [blame] | 92 | "Leaving blank will just look at config 'all'.", |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 93 | choices=list(_builtin_configs.keys())+['all'], |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 94 | ) |
Paul Sokolovsky | cba7ee4 | 2023-04-19 13:21:33 +0300 | [diff] [blame] | 95 | PARSER.add_argument( |
| 96 | "-j", |
| 97 | "--jobs", |
| 98 | help="Pass -j option down to the build system (# of parallel jobs)." |
| 99 | ) |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 100 | ARGS = PARSER.parse_args() |
Xinyu Zhang | 0aebb3d | 2022-04-11 18:27:12 +0800 | [diff] [blame] | 101 | if not ARGS.group or ARGS.group == ['all']: |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 102 | ARGS.group = list(_builtin_configs.keys()) |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 103 | |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 104 | 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 Zhang | 46b3718 | 2023-06-30 15:36:44 +0800 | [diff] [blame] | 111 | 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 Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 117 | except (SystemExit, KeyError): |
| 118 | if group == ARGS.group[-1] or ARGS.group == []: |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 119 | 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) |