Skip to content

Commit fbfeb4f

Browse files
authored
Merge pull request #67750 from artemcm/SeparateClangScannerCachePath
[Dependency Scanning] Separate module output path from Clang scanner's module cache
2 parents 48ca50a + 6ef79c4 commit fbfeb4f

16 files changed

+114
-52
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,8 @@ class ModuleDependenciesCache {
945945
std::string mainScanModuleName;
946946
/// The context hash of the current scanning invocation
947947
std::string scannerContextHash;
948+
/// The location of where the built modules will be output to
949+
std::string moduleOutputPath;
948950
/// The Clang dependency scanner tool
949951
clang::tooling::dependencies::DependencyScanningTool clangScanningTool;
950952

@@ -958,6 +960,7 @@ class ModuleDependenciesCache {
958960
public:
959961
ModuleDependenciesCache(SwiftDependencyScanningService &globalScanningService,
960962
std::string mainScanModuleName,
963+
std::string moduleOutputPath,
961964
std::string scanningContextHash);
962965
ModuleDependenciesCache(const ModuleDependenciesCache &) = delete;
963966
ModuleDependenciesCache &operator=(const ModuleDependenciesCache &) = delete;
@@ -980,6 +983,9 @@ class ModuleDependenciesCache {
980983
void addSeenClangModule(clang::tooling::dependencies::ModuleID newModule) {
981984
alreadySeenClangModules.insert(newModule);
982985
}
986+
std::string getModuleOutputPath() {
987+
return moduleOutputPath;
988+
}
983989

984990
/// Look for module dependencies for a module with the given name
985991
///

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,9 @@ namespace swift {
811811
/// The module cache path which the Clang importer should use.
812812
std::string ModuleCachePath;
813813

814+
/// The Scanning module cache path which the Clang Dependency Scanner should use.
815+
std::string ClangScannerModuleCachePath;
816+
814817
/// Extra arguments which should be passed to the Clang importer.
815818
std::vector<std::string> ExtraArgs;
816819

include/swift/Frontend/Frontend.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,18 @@ class CompilerInvocation {
168168
ClangImporterOpts.ModuleCachePath = Path.str();
169169
}
170170

171+
void setClangScannerModuleCachePath(StringRef Path) {
172+
ClangImporterOpts.ClangScannerModuleCachePath = Path.str();
173+
}
174+
171175
StringRef getClangModuleCachePath() const {
172176
return ClangImporterOpts.ModuleCachePath;
173177
}
174178

179+
StringRef getClangScannerModuleCachePath() const {
180+
return ClangImporterOpts.ClangScannerModuleCachePath;
181+
}
182+
175183
void setImportSearchPaths(const std::vector<std::string> &Paths) {
176184
SearchPathOpts.setImportSearchPaths(Paths);
177185
}

include/swift/Frontend/FrontendOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ class FrontendOptions {
9191
/// binary module has already been built for use by the compiler.
9292
std::string PrebuiltModuleCachePath;
9393

94+
/// The path to output explicit module dependencies. Only relevant during
95+
/// dependency scanning.
96+
std::string ExplicitModulesOutputPath;
97+
9498
/// The path to look in to find backup .swiftinterface files if those found
9599
/// from SDKs are failing.
96100
std::string BackupModuleInterfaceDir;

include/swift/Option/Options.td

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ def localization_path : Separate<["-"], "localization-path">,
456456
def module_cache_path : Separate<["-"], "module-cache-path">,
457457
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, ArgumentIsPath, SwiftAPIExtractOption,
458458
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
459-
HelpText<"Specifies the Clang module cache path">;
459+
HelpText<"Specifies the module cache path">;
460460

461461
def enable_library_evolution : Flag<["-"], "enable-library-evolution">,
462462
Flags<[FrontendOption, ModuleInterfaceOption]>,
@@ -1838,6 +1838,9 @@ def cas_plugin_option: Separate<["-"], "cas-plugin-option">,
18381838
Flags<[FrontendOption, NewDriverOnlyOption]>,
18391839
HelpText<"Option pass to CAS Plugin">, MetaVarName<"<name>=<option>">;
18401840

1841+
def clang_scanner_module_cache_path : Separate<["-"], "clang-scanner-module-cache-path">,
1842+
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, ArgumentIsPath]>,
1843+
HelpText<"Specifies the Clang dependency scanner module cache path">;
18411844

18421845
// END ONLY SUPPORTED IN NEW DRIVER
18431846

include/swift/Serialization/ModuleDependencyScanner.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace swift {
4141
InterfaceSubContextDelegate &astDelegate;
4242

4343
/// Location where pre-built moduels are to be built into.
44-
std::string moduleCachePath;
44+
std::string moduleOutputPath;
4545

4646
llvm::Optional<SwiftDependencyTracker> dependencyTracker;
4747

@@ -51,13 +51,13 @@ namespace swift {
5151
ModuleDependencyScanner(
5252
ASTContext &ctx, ModuleLoadingMode LoadMode, Identifier moduleName,
5353
InterfaceSubContextDelegate &astDelegate,
54+
StringRef moduleOutputPath,
5455
ScannerKind kind = MDS_plain,
5556
llvm::Optional<SwiftDependencyTracker> tracker = llvm::None)
5657
: SerializedModuleLoaderBase(ctx, nullptr, LoadMode,
5758
/*IgnoreSwiftSourceInfoFile=*/true),
5859
kind(kind), moduleName(moduleName), astDelegate(astDelegate),
59-
moduleCachePath(getModuleCachePathFromClang(
60-
ctx.getClangModuleLoader()->getClangInstance())),
60+
moduleOutputPath(moduleOutputPath),
6161
dependencyTracker(tracker) {}
6262

6363
std::error_code findModuleFilesInDirectory(
@@ -123,9 +123,10 @@ namespace swift {
123123
ASTContext &ctx, ModuleLoadingMode LoadMode, Identifier moduleName,
124124
StringRef PlaceholderDependencyModuleMap,
125125
InterfaceSubContextDelegate &astDelegate,
126+
StringRef moduleOutputPath,
126127
llvm::Optional<SwiftDependencyTracker> tracker = llvm::None)
127128
: ModuleDependencyScanner(ctx, LoadMode, moduleName, astDelegate,
128-
MDS_placeholder, tracker) {
129+
moduleOutputPath, MDS_placeholder, tracker) {
129130

130131
// FIXME: Find a better place for this map to live, to avoid
131132
// doing the parsing on every module.

lib/AST/ModuleDependencies.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,11 @@ ModuleDependenciesCache::getDependencyReferencesMap(
618618

619619
ModuleDependenciesCache::ModuleDependenciesCache(
620620
SwiftDependencyScanningService &globalScanningService,
621-
std::string mainScanModuleName, std::string scannerContextHash)
621+
std::string mainScanModuleName, std::string moduleOutputPath,
622+
std::string scannerContextHash)
622623
: globalScanningService(globalScanningService),
623624
mainScanModuleName(mainScanModuleName),
625+
moduleOutputPath(moduleOutputPath),
624626
scannerContextHash(scannerContextHash),
625627
clangScanningTool(*globalScanningService.ClangScanningService,
626628
globalScanningService.getClangScanningFS()) {

lib/ClangImporter/ClangImporter.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,13 @@ importer::getNormalInvocationArguments(
667667
}
668668

669669
const std::string &moduleCachePath = importerOpts.ModuleCachePath;
670-
if (!moduleCachePath.empty() && !importerOpts.DisableImplicitClangModules) {
670+
const std::string &scannerCachePath = importerOpts.ClangScannerModuleCachePath;
671+
// If a scanner cache is specified, this must be a scanning action. Prefer this
672+
// path for the Clang scanner to cache its Scanning PCMs.
673+
if (!scannerCachePath.empty()) {
674+
invocationArgStrs.push_back("-fmodules-cache-path=");
675+
invocationArgStrs.back().append(scannerCachePath);
676+
} else if (!moduleCachePath.empty() && !importerOpts.DisableImplicitClangModules) {
671677
invocationArgStrs.push_back("-fmodules-cache-path=");
672678
invocationArgStrs.back().append(moduleCachePath);
673679
}

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void ClangImporter::recordModuleDependencies(
174174

175175
auto pcmPath = moduleCacheRelativeLookupModuleOutput(
176176
clangModuleDep.ID, ModuleOutputKind::ModuleFile,
177-
getModuleCachePathFromClang(getClangInstance()));
177+
cache.getModuleOutputPath());
178178
swiftArgs.push_back("-o");
179179
swiftArgs.push_back(pcmPath);
180180

@@ -421,7 +421,7 @@ ClangImporter::getModuleDependencies(StringRef moduleName,
421421
}
422422
std::string workingDir = *optionalWorkingDir;
423423

424-
auto moduleCachePath = getModuleCachePathFromClang(getClangInstance());
424+
auto moduleCachePath = cache.getModuleOutputPath();
425425
auto lookupModuleOutput =
426426
[moduleCachePath](const ModuleID &MID,
427427
ModuleOutputKind MOK) -> std::string {

lib/DependencyScan/DependencyScanningTool.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,10 @@ DependencyScanningTool::getDependencies(
131131
auto Instance = std::move(*InstanceOrErr);
132132

133133
// Local scan cache instance, wrapping the shared global cache.
134-
ModuleDependenciesCache cache(*ScanningService,
135-
Instance->getMainModule()->getNameStr().str(),
136-
Instance->getInvocation().getModuleScanningHash());
134+
ModuleDependenciesCache cache(
135+
*ScanningService, Instance->getMainModule()->getNameStr().str(),
136+
Instance->getInvocation().getFrontendOptions().ExplicitModulesOutputPath,
137+
Instance->getInvocation().getModuleScanningHash());
137138
// Execute the scanning action, retrieving the in-memory result
138139
auto DependenciesOrErr = performModuleScan(*Instance.get(), cache);
139140
if (DependenciesOrErr.getError())
@@ -173,9 +174,10 @@ DependencyScanningTool::getDependencies(
173174
auto Instance = std::move(*InstanceOrErr);
174175

175176
// Local scan cache instance, wrapping the shared global cache.
176-
ModuleDependenciesCache cache(*ScanningService,
177-
Instance->getMainModule()->getNameStr().str(),
178-
Instance->getInvocation().getModuleScanningHash());
177+
ModuleDependenciesCache cache(
178+
*ScanningService, Instance->getMainModule()->getNameStr().str(),
179+
Instance->getInvocation().getFrontendOptions().ExplicitModulesOutputPath,
180+
Instance->getInvocation().getModuleScanningHash());
179181
auto BatchScanResults = performBatchModuleScan(
180182
*Instance.get(), cache, VersionedPCMInstanceCacheCache.get(),
181183
Saver, BatchInput);

0 commit comments

Comments
 (0)