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 | |
| 12 | def get_compile_command(compile_commands_file, c_file): |
| 13 | with open(compile_commands_file, "rt") as f: |
| 14 | compile_commands = json.load(f) |
| 15 | |
| 16 | return [x['command'] for x in compile_commands if c_file in x['file']][0] |
| 17 | |
| 18 | def get_includes(compile_commands_file, c_file): |
| 19 | compile_command = get_compile_command(compile_commands_file, c_file).split() |
| 20 | return [x[2:].strip() for x in compile_command if x.rstrip()[:2] == "-I"] |
| 21 | |
| 22 | def get_defines(compile_commands_file, c_file): |
| 23 | compile_command = get_compile_command(compile_commands_file, c_file).split() |
| 24 | return [x[2:].strip() for x in compile_command if x.rstrip()[:2] == "-D"] |
| 25 | |
| 26 | |
| 27 | if __name__ == '__main__': |
| 28 | import argparse |
| 29 | |
| 30 | parser = argparse.ArgumentParser(allow_abbrev=False) |
| 31 | parser.add_argument("--compile_commands_file", help="path to compile_command.json", required=True) |
| 32 | parser.add_argument("--c_file", help="name of the c file to take", required=True) |
| 33 | parser.add_argument("--log_level", help="log level", required=False, default="ERROR", choices=logging._levelToName.values()) |
| 34 | args = parser.parse_args() |
| 35 | logger.setLevel(args.log_level) |
| 36 | |
| 37 | print(get_compile_command(args.compile_commands_file, args.c_file)) |
| 38 | print(get_includes(args.compile_commands_file, args.c_file)) |
| 39 | print(get_defines(args.compile_commands_file, args.c_file)) |