llvm-project
88 строк · 2.3 Кб
1#!/usr/bin/env python3
2
3# Converts clang-scan-deps output into response files.
4#
5# Usage:
6#
7# clang-scan-deps -compilation-database compile_commands.json ... > deps.json
8# module-deps-to-rsp.py deps.json --module-name=ModuleName > module_name.cc1.rsp
9# module-deps-to-rsp.py deps.json --tu-index=0 > tu.rsp
10# clang @module_name.cc1.rsp
11# clang @tu.rsp
12
13import argparse
14import json
15import sys
16
17
18class ModuleNotFoundError(Exception):
19def __init__(self, module_name):
20self.module_name = module_name
21
22
23class FullDeps:
24def __init__(self):
25self.modules = {}
26self.translation_units = []
27
28
29def findModule(module_name, full_deps):
30for m in full_deps.modules.values():
31if m["name"] == module_name:
32return m
33raise ModuleNotFoundError(module_name)
34
35
36def parseFullDeps(json):
37ret = FullDeps()
38for m in json["modules"]:
39ret.modules[m["name"] + "-" + m["context-hash"]] = m
40ret.translation_units = json["translation-units"]
41return ret
42
43
44def quote(str):
45return '"' + str.replace("\\", "\\\\") + '"'
46
47
48def main():
49parser = argparse.ArgumentParser()
50parser.add_argument(
51"full_deps_file", help="Path to the full dependencies json file", type=str
52)
53action = parser.add_mutually_exclusive_group(required=True)
54action.add_argument(
55"--module-name", help="The name of the module to get arguments for", type=str
56)
57action.add_argument(
58"--tu-index",
59help="The index of the translation unit to get arguments for",
60type=int,
61)
62parser.add_argument(
63"--tu-cmd-index",
64help="The index of the command within the translation unit (default=0)",
65type=int,
66default=0,
67)
68args = parser.parse_args()
69
70full_deps = parseFullDeps(json.load(open(args.full_deps_file, "r")))
71
72try:
73cmd = []
74
75if args.module_name:
76cmd = findModule(args.module_name, full_deps)["command-line"]
77elif args.tu_index != None:
78tu = full_deps.translation_units[args.tu_index]
79cmd = tu["commands"][args.tu_cmd_index]["command-line"]
80
81print(" ".join(map(quote, cmd)))
82except:
83print("Unexpected error:", sys.exc_info()[0])
84raise
85
86
87if __name__ == "__main__":
88main()
89