llvm-project/clang/utils/module-deps-to-rsp.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

89 lines
2.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# Converts clang-scan-deps output into response files.
#
# Usage:
#
# clang-scan-deps -compilation-database compile_commands.json ... > deps.json
# module-deps-to-rsp.py deps.json --module-name=ModuleName > module_name.cc1.rsp
# module-deps-to-rsp.py deps.json --tu-index=0 > tu.rsp
# clang @module_name.cc1.rsp
# clang @tu.rsp
import argparse
import json
import sys
class ModuleNotFoundError(Exception):
def __init__(self, module_name):
self.module_name = module_name
class FullDeps:
def __init__(self):
self.modules = {}
self.translation_units = []
def findModule(module_name, full_deps):
for m in full_deps.modules.values():
if m["name"] == module_name:
return m
raise ModuleNotFoundError(module_name)
def parseFullDeps(json):
ret = FullDeps()
for m in json["modules"]:
ret.modules[m["name"] + "-" + m["context-hash"]] = m
ret.translation_units = json["translation-units"]
return ret
def quote(str):
return '"' + str.replace("\\", "\\\\") + '"'
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"full_deps_file", help="Path to the full dependencies json file", type=str
)
action = parser.add_mutually_exclusive_group(required=True)
action.add_argument(
"--module-name", help="The name of the module to get arguments for", type=str
)
action.add_argument(
"--tu-index",
help="The index of the translation unit to get arguments for",
type=int,
)
Reapply "[clang][deps] Split translation units into individual -cc1 or other commands" Attempt to fix the test failures observed in CI: * Add Option dependency, which caused BUILD_SHARED_LIBS builds to fail * Adapt tests that accidentally depended on the host platform: platforms that don't use an integrated assembler (e.g. AIX) get a different set of commands from the driver. Most dependency scanner tests can use -fsyntax-only or -E instead of -c to avoid this, and in the rare case we want to check -c specifically, set an explicit target so the behaviour is independent of the host. Original commit message follows. --- Instead of trying to "fix" the original driver invocation by appending arguments to it, split it into multiple commands, and for each -cc1 command use a CompilerInvocation to give precise control over the invocation. This change should make it easier to (in the future) canonicalize the command-line (e.g. to improve hits in something like ccache), apply optimizations, or start supporting multi-arch builds, which would require different modules for each arch. In the long run it may make sense to treat the TU commands as a dependency graph, each with their own dependencies on modules or earlier TU commands, but for now they are simply a list that is executed in order, and the dependencies are simply duplicated. Since we currently only support single-arch builds, there is no parallelism available in the execution. Differential Revision: https://reviews.llvm.org/D132405
2022-08-25 09:22:31 -07:00
parser.add_argument(
"--tu-cmd-index",
help="The index of the command within the translation unit (default=0)",
type=int,
default=0,
)
args = parser.parse_args()
full_deps = parseFullDeps(json.load(open(args.full_deps_file, "r")))
try:
cmd = []
if args.module_name:
cmd = findModule(args.module_name, full_deps)["command-line"]
elif args.tu_index is not None:
Reapply "[clang][deps] Split translation units into individual -cc1 or other commands" Attempt to fix the test failures observed in CI: * Add Option dependency, which caused BUILD_SHARED_LIBS builds to fail * Adapt tests that accidentally depended on the host platform: platforms that don't use an integrated assembler (e.g. AIX) get a different set of commands from the driver. Most dependency scanner tests can use -fsyntax-only or -E instead of -c to avoid this, and in the rare case we want to check -c specifically, set an explicit target so the behaviour is independent of the host. Original commit message follows. --- Instead of trying to "fix" the original driver invocation by appending arguments to it, split it into multiple commands, and for each -cc1 command use a CompilerInvocation to give precise control over the invocation. This change should make it easier to (in the future) canonicalize the command-line (e.g. to improve hits in something like ccache), apply optimizations, or start supporting multi-arch builds, which would require different modules for each arch. In the long run it may make sense to treat the TU commands as a dependency graph, each with their own dependencies on modules or earlier TU commands, but for now they are simply a list that is executed in order, and the dependencies are simply duplicated. Since we currently only support single-arch builds, there is no parallelism available in the execution. Differential Revision: https://reviews.llvm.org/D132405
2022-08-25 09:22:31 -07:00
tu = full_deps.translation_units[args.tu_index]
cmd = tu["commands"][args.tu_cmd_index]["command-line"]
print(" ".join(map(quote, cmd)))
except:
print("Unexpected error:", sys.exc_info()[0])
raise
if __name__ == "__main__":
main()