Skip to content

Commit 6ef79c4

Browse files
committed
[Dependency Scanning] Specify Explicit Module output path to the scanner explicitly
Instead of the code querying the compiler's built-in Clang instance, refactor the dependency scanner to explicitly keep track of module output path. It is still set according to '-module-cache-path' as it has been prior to this change, but now the scanner can use a different module cache for scanning PCMs, as specified with '-clang-scanner-module-cache-path', without affecting module output path. Resolves rdar://113222853
1 parent 8501f99 commit 6ef79c4

File tree

12 files changed

+79
-55
lines changed

12 files changed

+79
-55
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/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: 1 addition & 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]>,

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/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);

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,9 +1626,13 @@ forEachBatchEntry(CompilerInstance &invocationInstance,
16261626
return true;
16271627
}
16281628
auto mainModuleName = newInstance->getMainModule()->getNameStr();
1629-
auto scanContextHash = newInstance->getInvocation().getModuleScanningHash();
1629+
auto scanContextHash =
1630+
newInstance->getInvocation().getModuleScanningHash();
1631+
auto moduleOutputPath = newInstance->getInvocation()
1632+
.getFrontendOptions()
1633+
.ExplicitModulesOutputPath;
16301634
auto newLocalCache = std::make_unique<ModuleDependenciesCache>(
1631-
*newService, mainModuleName.str(), scanContextHash);
1635+
*newService, mainModuleName.str(), moduleOutputPath, scanContextHash);
16321636
pInstance = newInstance.get();
16331637
pCache = newLocalCache.get();
16341638
subInstanceMap->insert(
@@ -1800,11 +1804,10 @@ bool swift::dependencies::scanDependencies(CompilerInstance &instance) {
18001804
if (service.setupCachingDependencyScanningService(instance))
18011805
return true;
18021806

1803-
ModuleDependenciesCache cache(service,
1804-
instance.getMainModule()->getNameStr().str(),
1805-
instance.getInvocation().getModuleScanningHash());
1806-
auto ModuleCachePath = getModuleCachePathFromClang(
1807-
Context.getClangModuleLoader()->getClangInstance());
1807+
ModuleDependenciesCache cache(
1808+
service, instance.getMainModule()->getNameStr().str(),
1809+
instance.getInvocation().getFrontendOptions().ExplicitModulesOutputPath,
1810+
instance.getInvocation().getModuleScanningHash());
18081811

18091812
// Execute scan
18101813
auto dependenciesOrErr = performModuleScan(instance, cache);
@@ -1837,9 +1840,10 @@ bool swift::dependencies::prescanDependencies(CompilerInstance &instance) {
18371840
// `-scan-dependencies` invocations use a single new instance
18381841
// of a module cache
18391842
SwiftDependencyScanningService singleUseService;
1840-
ModuleDependenciesCache cache(singleUseService,
1841-
instance.getMainModule()->getNameStr().str(),
1842-
instance.getInvocation().getModuleScanningHash());
1843+
ModuleDependenciesCache cache(
1844+
singleUseService, instance.getMainModule()->getNameStr().str(),
1845+
instance.getInvocation().getFrontendOptions().ExplicitModulesOutputPath,
1846+
instance.getInvocation().getModuleScanningHash());
18431847

18441848
// Execute import prescan, and write JSON output to the output stream
18451849
auto importSetOrErr = performModulePrescan(instance);
@@ -1870,9 +1874,10 @@ bool swift::dependencies::batchScanDependencies(
18701874
if (singleUseService.setupCachingDependencyScanningService(instance))
18711875
return true;
18721876

1873-
ModuleDependenciesCache cache(singleUseService,
1874-
instance.getMainModule()->getNameStr().str(),
1875-
instance.getInvocation().getModuleScanningHash());
1877+
ModuleDependenciesCache cache(
1878+
singleUseService, instance.getMainModule()->getNameStr().str(),
1879+
instance.getInvocation().getFrontendOptions().ExplicitModulesOutputPath,
1880+
instance.getInvocation().getModuleScanningHash());
18761881
(void)instance.getMainModule();
18771882
llvm::BumpPtrAllocator alloc;
18781883
llvm::StringSaver saver(alloc);
@@ -1906,9 +1911,10 @@ bool swift::dependencies::batchPrescanDependencies(
19061911
// The primary cache used for scans carried out with the compiler instance
19071912
// we have created
19081913
SwiftDependencyScanningService singleUseService;
1909-
ModuleDependenciesCache cache(singleUseService,
1910-
instance.getMainModule()->getNameStr().str(),
1911-
instance.getInvocation().getModuleScanningHash());
1914+
ModuleDependenciesCache cache(
1915+
singleUseService, instance.getMainModule()->getNameStr().str(),
1916+
instance.getInvocation().getFrontendOptions().ExplicitModulesOutputPath,
1917+
instance.getInvocation().getModuleScanningHash());
19121918
(void)instance.getMainModule();
19131919
llvm::BumpPtrAllocator alloc;
19141920
llvm::StringSaver saver(alloc);
@@ -1984,14 +1990,14 @@ swift::dependencies::performModuleScan(CompilerInstance &instance,
19841990
cache.recordDependency(mainModuleName, std::move(*mainDependencies));
19851991
}
19861992

1987-
auto ModuleCachePath = getModuleCachePathFromClang(
1993+
auto ClangModuleCachePath = getModuleCachePathFromClang(
19881994
ctx.getClangModuleLoader()->getClangInstance());
19891995
auto &FEOpts = instance.getInvocation().getFrontendOptions();
19901996
ModuleInterfaceLoaderOptions LoaderOpts(FEOpts);
19911997
InterfaceSubContextDelegateImpl ASTDelegate(
19921998
ctx.SourceMgr, &ctx.Diags, ctx.SearchPathOpts, ctx.LangOpts,
19931999
ctx.ClangImporterOpts, LoaderOpts,
1994-
/*buildModuleCacheDirIfAbsent*/ false, ModuleCachePath,
2000+
/*buildModuleCacheDirIfAbsent*/ false, ClangModuleCachePath,
19952001
FEOpts.PrebuiltModuleCachePath,
19962002
FEOpts.BackupModuleInterfaceDir,
19972003
FEOpts.SerializeModuleInterfaceDependencyHashes,
@@ -2114,14 +2120,14 @@ swift::dependencies::performBatchModuleScan(
21142120
ASTContext &ctx = instance.getASTContext();
21152121
auto &FEOpts = instance.getInvocation().getFrontendOptions();
21162122
ModuleInterfaceLoaderOptions LoaderOpts(FEOpts);
2117-
auto ModuleCachePath = getModuleCachePathFromClang(
2123+
auto ClangModuleCachePath = getModuleCachePathFromClang(
21182124
ctx.getClangModuleLoader()->getClangInstance());
21192125

21202126
ModuleDependencyIDSetVector allModules;
21212127
InterfaceSubContextDelegateImpl ASTDelegate(
21222128
ctx.SourceMgr, &ctx.Diags, ctx.SearchPathOpts, ctx.LangOpts,
21232129
ctx.ClangImporterOpts, LoaderOpts,
2124-
/*buildModuleCacheDirIfAbsent*/ false, ModuleCachePath,
2130+
/*buildModuleCacheDirIfAbsent*/ false, ClangModuleCachePath,
21252131
FEOpts.PrebuiltModuleCachePath,
21262132
FEOpts.BackupModuleInterfaceDir,
21272133
FEOpts.SerializeModuleInterfaceDependencyHashes,
@@ -2183,13 +2189,13 @@ swift::dependencies::performBatchModulePrescan(
21832189
ASTContext &ctx = instance.getASTContext();
21842190
auto &FEOpts = instance.getInvocation().getFrontendOptions();
21852191
ModuleInterfaceLoaderOptions LoaderOpts(FEOpts);
2186-
auto ModuleCachePath = getModuleCachePathFromClang(
2192+
auto ClangModuleCachePath = getModuleCachePathFromClang(
21872193
ctx.getClangModuleLoader()->getClangInstance());
21882194
ModuleDependencyIDSetVector allModules;
21892195
InterfaceSubContextDelegateImpl ASTDelegate(
21902196
ctx.SourceMgr, &ctx.Diags, ctx.SearchPathOpts, ctx.LangOpts,
21912197
ctx.ClangImporterOpts, LoaderOpts,
2192-
/*buildModuleCacheDirIfAbsent*/ false, ModuleCachePath,
2198+
/*buildModuleCacheDirIfAbsent*/ false, ClangModuleCachePath,
21932199
FEOpts.PrebuiltModuleCachePath,
21942200
FEOpts.BackupModuleInterfaceDir,
21952201
FEOpts.SerializeModuleInterfaceDependencyHashes,

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ bool ArgsToFrontendOptionsConverter::convert(
6464
if (const Arg *A = Args.getLastArg(OPT_prebuilt_module_cache_path)) {
6565
Opts.PrebuiltModuleCachePath = A->getValue();
6666
}
67+
if (const Arg *A = Args.getLastArg(OPT_module_cache_path)) {
68+
Opts.ExplicitModulesOutputPath = A->getValue();
69+
}
6770
if (const Arg *A = Args.getLastArg(OPT_backup_module_interface_path)) {
6871
Opts.BackupModuleInterfaceDir = A->getValue();
6972
}

lib/Frontend/Frontend.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -751,30 +751,28 @@ bool CompilerInstance::setUpModuleLoaders() {
751751

752752
Context->addModuleLoader(std::move(clangImporter), /*isClang*/ true);
753753

754-
// When scanning for dependencies, we must add the scanner loaders in order to handle
755-
// ASTContext operations such as canImportModule
754+
// When scanning for dependencies, we must add the scanner loaders in order to
755+
// handle ASTContext operations such as canImportModule
756756
if (Invocation.getFrontendOptions().RequestedAction ==
757757
FrontendOptions::ActionType::ScanDependencies) {
758-
auto ModuleCachePath = getModuleCachePathFromClang(Context
759-
->getClangModuleLoader()->getClangInstance());
758+
auto ClangModuleCachePath = getModuleCachePathFromClang(
759+
Context->getClangModuleLoader()->getClangInstance());
760760
auto &FEOpts = Invocation.getFrontendOptions();
761761
ModuleInterfaceLoaderOptions LoaderOpts(FEOpts);
762762
InterfaceSubContextDelegateImpl ASTDelegate(
763763
Context->SourceMgr, &Context->Diags, Context->SearchPathOpts,
764764
Context->LangOpts, Context->ClangImporterOpts, LoaderOpts,
765-
/*buildModuleCacheDirIfAbsent*/ false, ModuleCachePath,
766-
FEOpts.PrebuiltModuleCachePath,
767-
FEOpts.BackupModuleInterfaceDir,
765+
/*buildModuleCacheDirIfAbsent*/ false, ClangModuleCachePath,
766+
FEOpts.PrebuiltModuleCachePath, FEOpts.BackupModuleInterfaceDir,
768767
FEOpts.SerializeModuleInterfaceDependencyHashes,
769768
FEOpts.shouldTrackSystemDependencies(),
770769
RequireOSSAModules_t(Invocation.getSILOptions()));
771770
auto mainModuleName = Context->getIdentifier(FEOpts.ModuleName);
772771
std::unique_ptr<PlaceholderSwiftModuleScanner> PSMS =
773-
std::make_unique<PlaceholderSwiftModuleScanner>(*Context,
774-
MLM,
775-
mainModuleName,
776-
Context->SearchPathOpts.PlaceholderDependencyModuleMap,
777-
ASTDelegate);
772+
std::make_unique<PlaceholderSwiftModuleScanner>(
773+
*Context, MLM, mainModuleName,
774+
Context->SearchPathOpts.PlaceholderDependencyModuleMap, ASTDelegate,
775+
getInvocation().getFrontendOptions().ExplicitModulesOutputPath);
778776
Context->addModuleLoader(std::move(PSMS));
779777
}
780778

0 commit comments

Comments
 (0)