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 | |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 47 | def print_config_environment(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) |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 50 | build_manager.print_config_environment(config, silence_stderr=silence_stderr) |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 51 | |
| 52 | |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 53 | def print_build_commands(config, group=None): |
| 54 | """Prints particular configuration environment variables""" |
| 55 | build_manager = get_build_manager(group) |
| 56 | build_manager.print_build_commands(config, silence_stderr=True) |
| 57 | |
| 58 | |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 59 | if __name__ == "__main__": |
| 60 | PARSER = argparse.ArgumentParser(description="Extract build configurations.") |
| 61 | PARSER.add_argument( |
| 62 | "config", |
| 63 | default=None, |
| 64 | nargs="?", |
| 65 | help="Configuration to print environment variables for. " |
| 66 | "Then run-build.sh can be run directly with these set. " |
| 67 | "If not specified, the available configurations are printed", |
| 68 | ) |
| 69 | PARSER.add_argument( |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 70 | "-b", |
| 71 | "--build_commands", |
| 72 | default=None, |
| 73 | action='store_true', |
| 74 | help="Instead of printing environment variables, print raw " |
| 75 | "build commands to be run." |
| 76 | ) |
| 77 | PARSER.add_argument( |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 78 | "-g", |
| 79 | "--group", |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 80 | default=[], |
| 81 | action="append", |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 82 | help="Only list configurations under a certain group. " |
| 83 | "'all' will look through all configurations. " |
Xinyu Zhang | 0aebb3d | 2022-04-11 18:27:12 +0800 | [diff] [blame] | 84 | "Leaving blank will just look at config 'all'.", |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 85 | choices=list(_builtin_configs.keys())+['all'], |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 86 | ) |
| 87 | ARGS = PARSER.parse_args() |
Xinyu Zhang | 0aebb3d | 2022-04-11 18:27:12 +0800 | [diff] [blame] | 88 | if not ARGS.group or ARGS.group == ['all']: |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 89 | ARGS.group = list(_builtin_configs.keys()) |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 90 | |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 91 | all_configs = set() |
| 92 | for group in ARGS.group: |
| 93 | # By default print available configs |
| 94 | if not ARGS.config: |
| 95 | all_configs.update(list_configs(group)) |
| 96 | else: |
| 97 | try: |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 98 | if not ARGS.build_commands: |
| 99 | print_config_environment(ARGS.config, group=group, silence_stderr=True) |
| 100 | else: |
| 101 | print_build_commands(ARGS.config, group=group) |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 102 | break |
Dean Birch | d0f9f8c | 2020-03-26 11:10:33 +0000 | [diff] [blame] | 103 | except (SystemExit, KeyError): |
| 104 | if group == ARGS.group[-1] or ARGS.group == []: |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 105 | print( |
| 106 | "Could not find configuration {} in groups {}".format( |
| 107 | ARGS.config, ARGS.group |
| 108 | ) |
| 109 | ) |
| 110 | |
| 111 | for config in all_configs: |
| 112 | print(config) |