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 |
| 13 | from build_helper.build_helper_configs import config_full |
| 14 | from build_helper.build_helper_configs import _builtin_configs |
| 15 | |
| 16 | |
| 17 | __copyright__ = """ |
| 18 | /* |
| 19 | * Copyright (c) 2020, Arm Limited. All rights reserved. |
| 20 | * |
| 21 | * SPDX-License-Identifier: BSD-3-Clause |
| 22 | * |
| 23 | */ |
| 24 | """ |
| 25 | |
| 26 | |
| 27 | def get_build_manager(group=None): |
| 28 | """Get a TFM_Build_Manager instance, silencing stdout""" |
| 29 | config = config_full |
| 30 | if group: |
| 31 | config = _builtin_configs[group] |
| 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 | |
| 53 | if __name__ == "__main__": |
| 54 | PARSER = argparse.ArgumentParser(description="Extract build configurations.") |
| 55 | PARSER.add_argument( |
| 56 | "config", |
| 57 | default=None, |
| 58 | nargs="?", |
| 59 | help="Configuration to print environment variables for. " |
| 60 | "Then run-build.sh can be run directly with these set. " |
| 61 | "If not specified, the available configurations are printed", |
| 62 | ) |
| 63 | PARSER.add_argument( |
| 64 | "-g", |
| 65 | "--group", |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame^] | 66 | default=[], |
| 67 | action="append", |
Dean Birch | 5cb5a88 | 2020-01-24 11:37:13 +0000 | [diff] [blame] | 68 | help="Only list configurations under a certain group. ", |
| 69 | choices=list(_builtin_configs.keys()), |
| 70 | ) |
| 71 | ARGS = PARSER.parse_args() |
| 72 | |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame^] | 73 | all_configs = set() |
| 74 | for group in ARGS.group: |
| 75 | # By default print available configs |
| 76 | if not ARGS.config: |
| 77 | all_configs.update(list_configs(group)) |
| 78 | else: |
| 79 | try: |
| 80 | print_config_environment(ARGS.config, group=group, silence_stderr=True) |
| 81 | break |
| 82 | except SystemExit: |
| 83 | if group == ARGS.group[-1]: |
| 84 | print( |
| 85 | "Could not find configuration {} in groups {}".format( |
| 86 | ARGS.config, ARGS.group |
| 87 | ) |
| 88 | ) |
| 89 | |
| 90 | for config in all_configs: |
| 91 | print(config) |