[memprof] Upgrade a unit test to MemProf Version 3 (#117063)
[llvm-project.git] / clang / utils / module-deps-to-rsp.py
blobb57a44e5c87809b40de7e054fe008cc3688c2be2
1 #!/usr/bin/env python3
3 # Converts clang-scan-deps output into response files.
5 # Usage:
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
13 import argparse
14 import json
15 import sys
18 class ModuleNotFoundError(Exception):
19 def __init__(self, module_name):
20 self.module_name = module_name
23 class FullDeps:
24 def __init__(self):
25 self.modules = {}
26 self.translation_units = []
29 def findModule(module_name, full_deps):
30 for m in full_deps.modules.values():
31 if m["name"] == module_name:
32 return m
33 raise ModuleNotFoundError(module_name)
36 def parseFullDeps(json):
37 ret = FullDeps()
38 for m in json["modules"]:
39 ret.modules[m["name"] + "-" + m["context-hash"]] = m
40 ret.translation_units = json["translation-units"]
41 return ret
44 def quote(str):
45 return '"' + str.replace("\\", "\\\\") + '"'
48 def main():
49 parser = argparse.ArgumentParser()
50 parser.add_argument(
51 "full_deps_file", help="Path to the full dependencies json file", type=str
53 action = parser.add_mutually_exclusive_group(required=True)
54 action.add_argument(
55 "--module-name", help="The name of the module to get arguments for", type=str
57 action.add_argument(
58 "--tu-index",
59 help="The index of the translation unit to get arguments for",
60 type=int,
62 parser.add_argument(
63 "--tu-cmd-index",
64 help="The index of the command within the translation unit (default=0)",
65 type=int,
66 default=0,
68 args = parser.parse_args()
70 full_deps = parseFullDeps(json.load(open(args.full_deps_file, "r")))
72 try:
73 cmd = []
75 if args.module_name:
76 cmd = findModule(args.module_name, full_deps)["command-line"]
77 elif args.tu_index is not None:
78 tu = full_deps.translation_units[args.tu_index]
79 cmd = tu["commands"][args.tu_cmd_index]["command-line"]
81 print(" ".join(map(quote, cmd)))
82 except:
83 print("Unexpected error:", sys.exc_info()[0])
84 raise
87 if __name__ == "__main__":
88 main()