2021-05-20 10:54:11 +02:00
|
|
|
#!/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
|
2022-03-12 16:55:38 +01:00
|
|
|
# clang @tu.rsp
|
2021-05-20 10:54:11 +02:00
|
|
|
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
|
2023-05-17 10:56:49 +02:00
|
|
|
|
2021-05-20 10:54:11 +02:00
|
|
|
class ModuleNotFoundError(Exception):
|
|
|
|
def __init__(self, module_name):
|
|
|
|
self.module_name = module_name
|
2023-05-17 10:56:49 +02:00
|
|
|
|
2021-05-20 10:54:11 +02:00
|
|
|
|
|
|
|
class FullDeps:
|
|
|
|
def __init__(self):
|
|
|
|
self.modules = {}
|
|
|
|
self.translation_units = []
|
2023-05-17 10:56:49 +02:00
|
|
|
|
2021-05-20 10:54:11 +02:00
|
|
|
|
|
|
|
def findModule(module_name, full_deps):
|
|
|
|
for m in full_deps.modules.values():
|
|
|
|
if m["name"] == module_name:
|
|
|
|
return m
|
|
|
|
raise ModuleNotFoundError(module_name)
|
2023-05-17 10:56:49 +02:00
|
|
|
|
2021-05-20 10:54:11 +02:00
|
|
|
|
|
|
|
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
|
2023-05-17 10:56:49 +02:00
|
|
|
|
2021-05-20 10:54:11 +02:00
|
|
|
|
|
|
|
def quote(str):
|
|
|
|
return '"' + str.replace("\\", "\\\\") + '"'
|
2023-05-17 10:56:49 +02:00
|
|
|
|
2021-05-20 10:54:11 +02:00
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
2021-05-20 10:54:11 +02:00
|
|
|
args = parser.parse_args()
|
2023-05-17 10:56:49 +02:00
|
|
|
|
2021-05-20 10:54:11 +02:00
|
|
|
full_deps = parseFullDeps(json.load(open(args.full_deps_file, "r")))
|
2023-05-17 10:56:49 +02:00
|
|
|
|
2021-05-20 10:54:11 +02:00
|
|
|
try:
|
|
|
|
cmd = []
|
2023-05-17 10:56:49 +02:00
|
|
|
|
2021-05-20 10:54:11 +02:00
|
|
|
if args.module_name:
|
|
|
|
cmd = findModule(args.module_name, full_deps)["command-line"]
|
2024-09-19 23:33:12 +09:00
|
|
|
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"]
|
2023-05-17 10:56:49 +02:00
|
|
|
|
2021-05-20 10:54:11 +02:00
|
|
|
print(" ".join(map(quote, cmd)))
|
|
|
|
except:
|
|
|
|
print("Unexpected error:", sys.exc_info()[0])
|
|
|
|
raise
|
2023-05-17 10:56:49 +02:00
|
|
|
|
|
|
|
|
2021-05-20 10:54:11 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|