Raef Coles | 59cf5d8 | 2024-12-09 15:41:13 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | #------------------------------------------------------------------------------- |
| 3 | # SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors |
| 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | #------------------------------------------------------------------------------- |
| 8 | |
| 9 | import os |
| 10 | import json |
| 11 | |
Raef Coles | cfc3124 | 2025-04-04 09:38:47 +0100 | [diff] [blame^] | 12 | import logging |
| 13 | logger = logging.getLogger("TF-M.{}".format(__name__)) |
| 14 | |
Raef Coles | 59cf5d8 | 2024-12-09 15:41:13 +0000 | [diff] [blame] | 15 | def get_compile_command(compile_commands_file, c_file): |
| 16 | with open(compile_commands_file, "rt") as f: |
| 17 | compile_commands = json.load(f) |
| 18 | |
| 19 | return [x['command'] for x in compile_commands if c_file in x['file']][0] |
| 20 | |
| 21 | def get_includes(compile_commands_file, c_file): |
| 22 | compile_command = get_compile_command(compile_commands_file, c_file).split() |
| 23 | return [x[2:].strip() for x in compile_command if x.rstrip()[:2] == "-I"] |
| 24 | |
| 25 | def get_defines(compile_commands_file, c_file): |
| 26 | compile_command = get_compile_command(compile_commands_file, c_file).split() |
| 27 | return [x[2:].strip() for x in compile_command if x.rstrip()[:2] == "-D"] |
| 28 | |
| 29 | |
| 30 | if __name__ == '__main__': |
| 31 | import argparse |
| 32 | |
| 33 | parser = argparse.ArgumentParser(allow_abbrev=False) |
| 34 | parser.add_argument("--compile_commands_file", help="path to compile_command.json", required=True) |
| 35 | parser.add_argument("--c_file", help="name of the c file to take", required=True) |
| 36 | parser.add_argument("--log_level", help="log level", required=False, default="ERROR", choices=logging._levelToName.values()) |
| 37 | args = parser.parse_args() |
Raef Coles | cfc3124 | 2025-04-04 09:38:47 +0100 | [diff] [blame^] | 38 | logging.getLogger("TF-M").setLevel(args.log_level) |
| 39 | logger.addHandler(logging.StreamHandler()) |
Raef Coles | 59cf5d8 | 2024-12-09 15:41:13 +0000 | [diff] [blame] | 40 | |
| 41 | print(get_compile_command(args.compile_commands_file, args.c_file)) |
| 42 | print(get_includes(args.compile_commands_file, args.c_file)) |
| 43 | print(get_defines(args.compile_commands_file, args.c_file)) |