[clang][deps] Add argument for customizing PCM paths

Dependency scanning currently performs an implicit build. When testing that Clang can build modules with the command-lines generated by `clang-scan-deps`, the actual compilation would overwrite artifacts created during the scan, which makes debugging harder than it should be and can lead to errors in multi-step builds.

To prevent this, this patch adds new flag to `clang-scan-deps` that allows developers to customize the directory to use when generating module map paths, instead of always using the module cache. Moreover, the explicit context hash in now part of the PCM path, which will be useful in D102488, where the context hash can change due to command-line pruning.

Reviewed By: Bigcheese

Differential Revision: https://reviews.llvm.org/D103516
This commit is contained in:
Jan Svoboda 2021-06-02 14:07:14 +02:00
parent cd093cbb11
commit 93a058190c
3 changed files with 52 additions and 1 deletions

View File

@ -42,6 +42,16 @@ struct ModuleID {
/// Modules with the same name but a different \c ContextHash should be
/// treated as separate modules for the purpose of a build.
std::string ContextHash;
bool operator==(const ModuleID &Other) const {
return ModuleName == Other.ModuleName && ContextHash == Other.ContextHash;
}
};
struct ModuleIDHasher {
std::size_t operator()(const ModuleID &MID) const {
return llvm::hash_combine(MID.ModuleName, MID.ContextHash);
}
};
struct ModuleDeps {

View File

@ -20,6 +20,12 @@
// RUN: -generate-modules-path-args -mode preprocess-minimized-sources >> %t.result
// RUN: cat %t.result | sed 's/\\/\//g' | FileCheck --check-prefixes=CHECK,CHECK-ABS %s
//
// RUN: echo %t.dir > %t.result
// RUN: clang-scan-deps -compilation-database %t.cdb -j 4 -format experimental-full \
// RUN: -generate-modules-path-args -module-files-dir %t.dir/custom \
// RUN: -mode preprocess-minimized-sources >> %t.result
// RUN: cat %t.result | sed 's/\\/\//g' | FileCheck --check-prefixes=CHECK,CHECK-CUSTOM %s
//
// RUN: echo %t.dir > %t_clangcl.result
// RUN: clang-scan-deps -compilation-database %t_clangcl.cdb -j 4 -format experimental-full \
// RUN: -mode preprocess-minimized-sources >> %t_clangcl.result
@ -45,9 +51,11 @@
// CHECK-NEXT: "-cc1"
// CHECK-NO-ABS-NOT: "-fmodule-map-file={{.*}}"
// CHECK-ABS: "-fmodule-map-file=[[PREFIX]]/Inputs/module.modulemap"
// CHECK-CUSTOM: "-fmodule-map-file=[[PREFIX]]/Inputs/module.modulemap"
// CHECK: "-emit-module"
// CHECK-NO-ABS-NOT: "-fmodule-file={{.*}}"
// CHECK-ABS: "-fmodule-file=[[PREFIX]]/module-cache{{(_clangcl)?}}/[[CONTEXT_HASH_H1]]/header2-{{[A-Z0-9]+}}.pcm"
// CHECK-CUSTOM: "-fmodule-file=[[PREFIX]]/custom/[[CONTEXT_HASH_H1]]/header2-{{[A-Z0-9]+}}.pcm"
// CHECK: "-fmodule-name=header1"
// CHECK: "-fno-implicit-modules"
// CHECK: ],
@ -105,8 +113,10 @@
// CHECK-NEXT: "-fno-implicit-module-maps"
// CHECK-NO-ABS-NOT: "-fmodule-file={{.*}}"
// CHECK-ABS-NEXT: "-fmodule-file=[[PREFIX]]/module-cache{{(_clangcl)?}}/[[CONTEXT_HASH_H2]]/header1-{{[A-Z0-9]+}}.pcm"
// CHECK-CUSTOM-NEXT: "-fmodule-file=[[PREFIX]]/custom/[[CONTEXT_HASH_H2]]/header1-{{[A-Z0-9]+}}.pcm"
// CHECK-NO-ABS-NOT: "-fmodule-map-file={{.*}}"
// CHECK-ABS-NEXT: "-fmodule-map-file=[[PREFIX]]/Inputs/module.modulemap"
// CHECK-CUSTOM-NEXT: "-fmodule-map-file=[[PREFIX]]/Inputs/module.modulemap"
// CHECK-NEXT: ],
// CHECK-NEXT: "file-deps": [
// CHECK-NEXT: "[[PREFIX]]/modules_cdb_input.cpp"
@ -126,8 +136,10 @@
// CHECK-NEXT: "-fno-implicit-module-maps"
// CHECK-NO-ABS-NOT: "-fmodule-file={{.*}},
// CHECK-ABS-NEXT: "-fmodule-file=[[PREFIX]]/module-cache{{(_clangcl)?}}/[[CONTEXT_HASH_H2]]/header1-{{[A-Z0-9]+}}.pcm"
// CHECK-CUSTOM-NEXT: "-fmodule-file=[[PREFIX]]/custom/[[CONTEXT_HASH_H2]]/header1-{{[A-Z0-9]+}}.pcm"
// CHECK-NO-ABS-NOT: "-fmodule-map-file={{.*}}
// CHECK-ABS-NEXT: "-fmodule-map-file=[[PREFIX]]/Inputs/module.modulemap"
// CHECK-CUSTOM-NEXT: "-fmodule-map-file=[[PREFIX]]/Inputs/module.modulemap"
// CHECK-NEXT: ],
// CHECK-NEXT: "file-deps": [
// CHECK-NEXT: "[[PREFIX]]/modules_cdb_input.cpp"
@ -147,8 +159,10 @@
// CHECK-NEXT: "-fno-implicit-module-maps"
// CHECK-NO-ABS-NOT: "-fmodule-file={{.*}}"
// CHECK-ABS-NEXT: "-fmodule-file=[[PREFIX]]/module-cache{{(_clangcl)?}}/[[CONTEXT_HASH_H2]]/header1-{{[A-Z0-9]+}}.pcm"
// CHECK-CUSTOM-NEXT: "-fmodule-file=[[PREFIX]]/custom/[[CONTEXT_HASH_H2]]/header1-{{[A-Z0-9]+}}.pcm"
// CHECK-NO-ABS-NOT: "-fmodule-map-file={{.*}}"
// CHECK-ABS-NEXT: "-fmodule-map-file=[[PREFIX]]/Inputs/module.modulemap"
// CHECK-CUSTOM-NEXT: "-fmodule-map-file=[[PREFIX]]/Inputs/module.modulemap"
// CHECK-NEXT: ],
// CHECK-NEXT: "file-deps": [
// CHECK-NEXT: "[[PREFIX]]/modules_cdb_input.cpp"
@ -169,9 +183,13 @@
// CHECK-NO-ABS-NOT: "-fmodule-file={{.*}}"
// CHECK-ABS-NEXT: "-fmodule-file=[[PREFIX]]/module-cache{{(_clangcl)?}}/[[CONTEXT_HASH_H1]]/header2-{{[A-Z0-9]+}}.pcm"
// CHECK-ABS-NEXT: "-fmodule-file=[[PREFIX]]/module-cache{{(_clangcl)?}}/[[CONTEXT_HASH_H1]]/header1-{{[A-Z0-9]+}}.pcm"
// CHECK-CUSTOM-NEXT: "-fmodule-file=[[PREFIX]]/custom/[[CONTEXT_HASH_H1]]/header2-{{[A-Z0-9]+}}.pcm"
// CHECK-CUSTOM-NEXT: "-fmodule-file=[[PREFIX]]/custom/[[CONTEXT_HASH_H1]]/header1-{{[A-Z0-9]+}}.pcm"
// CHECK-NO-ABS-NOT: "-fmodule-map-file={{.*}}"
// CHECK-ABS-NEXT: "-fmodule-map-file=[[PREFIX]]/Inputs/module.modulemap"
// CHECK-ABS-NEXT: "-fmodule-map-file=[[PREFIX]]/Inputs/module.modulemap"
// CHECK-CUSTOM-NEXT: "-fmodule-map-file=[[PREFIX]]/Inputs/module.modulemap"
// CHECK-CUSTOM-NEXT: "-fmodule-map-file=[[PREFIX]]/Inputs/module.modulemap"
// CHECK-NEXT: ],
// CHECK-NEXT: "file-deps": [
// CHECK-NEXT: "[[PREFIX]]/modules_cdb_input2.cpp"

View File

@ -163,6 +163,13 @@ static llvm::cl::opt<bool> GenerateModulesPathArgs(
"'-fmodule-file=', '-o', '-fmodule-map-file='."),
llvm::cl::init(false), llvm::cl::cat(DependencyScannerCategory));
static llvm::cl::opt<std::string> ModuleFilesDir(
"module-files-dir",
llvm::cl::desc("With '-generate-modules-path-args', paths to module files "
"in the generated command lines will begin with the "
"specified directory instead the module cache directory."),
llvm::cl::cat(DependencyScannerCategory));
llvm::cl::opt<unsigned>
NumThreads("j", llvm::cl::Optional,
llvm::cl::desc("Number of worker threads to use (default: use "
@ -357,7 +364,22 @@ public:
private:
StringRef lookupPCMPath(ModuleID MID) {
return Modules[IndexedModuleID{MID, 0}].ImplicitModulePCMPath;
auto PCMPath = PCMPaths.insert({MID, ""});
if (PCMPath.second)
PCMPath.first->second = constructPCMPath(lookupModuleDeps(MID));
return PCMPath.first->second;
}
/// Construct a path for the explicitly built PCM.
std::string constructPCMPath(const ModuleDeps &MD) const {
StringRef Filename = llvm::sys::path::filename(MD.ImplicitModulePCMPath);
SmallString<256> ExplicitPCMPath(
!ModuleFilesDir.empty()
? ModuleFilesDir
: MD.Invocation.getHeaderSearchOpts().ModuleCachePath);
llvm::sys::path::append(ExplicitPCMPath, MD.ID.ContextHash, Filename);
return std::string(ExplicitPCMPath);
}
const ModuleDeps &lookupModuleDeps(ModuleID MID) {
@ -395,6 +417,7 @@ private:
std::mutex Lock;
std::unordered_map<IndexedModuleID, ModuleDeps, IndexedModuleIDHasher>
Modules;
std::unordered_map<ModuleID, std::string, ModuleIDHasher> PCMPaths;
std::vector<InputDeps> Inputs;
};