Skip to content

Commit 04015fe

Browse files
committed
[Dependency Scanning] Adopt new clang scanner API to place stable modules into an SDK-specific module cache
1 parent 88dec51 commit 04015fe

File tree

4 files changed

+48
-21
lines changed

4 files changed

+48
-21
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,8 +1202,8 @@ class ModuleDependenciesCache {
12021202
void addSeenClangModule(clang::tooling::dependencies::ModuleID newModule) {
12031203
alreadySeenClangModules.insert(newModule);
12041204
}
1205-
std::string getModuleOutputPath() const { return moduleOutputPath; }
1206-
std::string getSDKModuleOutputPath() const { return sdkModuleOutputPath; }
1205+
StringRef getModuleOutputPath() const { return moduleOutputPath; }
1206+
StringRef getSDKModuleOutputPath() const { return sdkModuleOutputPath; }
12071207

12081208
/// Query all dependencies
12091209
ModuleDependencyIDSetVector

include/swift/ClangImporter/ClangImporter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,8 @@ class ClangImporter final : public ClangModuleLoader {
485485
bridgeClangModuleDependencies(
486486
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
487487
clang::tooling::dependencies::ModuleDepsGraph &clangDependencies,
488-
StringRef moduleOutputPath, RemapPathCallback remapPath = nullptr);
488+
StringRef moduleOutputPath, StringRef stableModuleOutputPath,
489+
RemapPathCallback remapPath = nullptr);
489490

490491
llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
491492
getModuleDependencies(Identifier moduleName, StringRef moduleOutputPath, StringRef sdkModuleOutputPath,

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ using namespace clang::tooling;
3838
using namespace clang::tooling::dependencies;
3939

4040
static std::string
41-
moduleCacheRelativeLookupModuleOutput(const ModuleID &MID, ModuleOutputKind MOK,
42-
const StringRef moduleCachePath) {
41+
moduleCacheRelativeLookupModuleOutput(const ModuleDeps &MD, ModuleOutputKind MOK,
42+
const StringRef moduleCachePath,
43+
const StringRef stableModuleCachePath) {
4344
llvm::SmallString<128> outputPath(moduleCachePath);
44-
llvm::sys::path::append(outputPath, MID.ModuleName + "-" + MID.ContextHash);
45+
if (MD.IsInStableDirectories)
46+
outputPath = stableModuleCachePath;
47+
48+
llvm::sys::path::append(outputPath, MD.ID.ModuleName + "-" + MD.ID.ContextHash);
4549
switch (MOK) {
4650
case ModuleOutputKind::ModuleFile:
4751
llvm::sys::path::replace_extension(
@@ -52,7 +56,7 @@ moduleCacheRelativeLookupModuleOutput(const ModuleID &MID, ModuleOutputKind MOK,
5256
outputPath, getExtension(swift::file_types::TY_Dependencies));
5357
break;
5458
case ModuleOutputKind::DependencyTargets:
55-
return MID.ModuleName + "-" + MID.ContextHash;
59+
return MD.ID.ModuleName + "-" + MD.ID.ContextHash;
5660
case ModuleOutputKind::DiagnosticSerializationFile:
5761
llvm::sys::path::replace_extension(
5862
outputPath, getExtension(swift::file_types::TY_SerializedDiagnostics));
@@ -137,7 +141,8 @@ getClangPrefixMapper(DependencyScanningTool &clangScanningTool,
137141
ModuleDependencyVector ClangImporter::bridgeClangModuleDependencies(
138142
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
139143
clang::tooling::dependencies::ModuleDepsGraph &clangDependencies,
140-
StringRef moduleOutputPath, RemapPathCallback callback) {
144+
StringRef moduleOutputPath, StringRef stableModuleOutputPath,
145+
RemapPathCallback callback) {
141146
const auto &ctx = Impl.SwiftContext;
142147
ModuleDependencyVector result;
143148

@@ -168,7 +173,8 @@ ModuleDependencyVector ClangImporter::bridgeClangModuleDependencies(
168173
swiftArgs.push_back(clangModuleDep.ID.ModuleName);
169174

170175
auto pcmPath = moduleCacheRelativeLookupModuleOutput(
171-
clangModuleDep.ID, ModuleOutputKind::ModuleFile, moduleOutputPath);
176+
clangModuleDep, ModuleOutputKind::ModuleFile, moduleOutputPath,
177+
stableModuleOutputPath);
172178
swiftArgs.push_back("-o");
173179
swiftArgs.push_back(pcmPath);
174180

@@ -421,11 +427,10 @@ ClangImporter::getModuleDependencies(Identifier moduleName,
421427
}
422428
std::string workingDir = *optionalWorkingDir;
423429
auto lookupModuleOutput =
424-
[moduleOutputPath](const ModuleDeps &MD,
425-
ModuleOutputKind MOK) -> std::string {
426-
// ACTODO: Once the clang scanner gets the required functionality,
427-
// use sdkModuleOutputPath for modules whose modulemap is a part of the SDK.
428-
return moduleCacheRelativeLookupModuleOutput(MD.ID, MOK, moduleOutputPath);
430+
[moduleOutputPath, sdkModuleOutputPath]
431+
(const ModuleDeps &MD, ModuleOutputKind MOK) -> std::string {
432+
return moduleCacheRelativeLookupModuleOutput(MD, MOK, moduleOutputPath,
433+
sdkModuleOutputPath);
429434
};
430435

431436
auto clangModuleDependencies =
@@ -446,7 +451,8 @@ ClangImporter::getModuleDependencies(Identifier moduleName,
446451

447452
return bridgeClangModuleDependencies(clangScanningTool,
448453
*clangModuleDependencies,
449-
moduleOutputPath, [&](StringRef path) {
454+
moduleOutputPath, sdkModuleOutputPath,
455+
[&](StringRef path) {
450456
if (mapper)
451457
return mapper->mapToString(path);
452458
return path.str();
@@ -477,11 +483,13 @@ bool ClangImporter::getHeaderDependencies(
477483
}
478484
std::string workingDir = *optionalWorkingDir;
479485
auto moduleOutputPath = cache.getModuleOutputPath();
486+
auto sdkModuleOutputPath = cache.getSDKModuleOutputPath();
480487
auto lookupModuleOutput =
481-
[moduleOutputPath](const ModuleDeps &MD,
482-
ModuleOutputKind MOK) -> std::string {
483-
return moduleCacheRelativeLookupModuleOutput(MD.ID, MOK,
484-
moduleOutputPath);
488+
[moduleOutputPath, sdkModuleOutputPath]
489+
(const ModuleDeps &MD, ModuleOutputKind MOK) -> std::string {
490+
return moduleCacheRelativeLookupModuleOutput(MD, MOK,
491+
moduleOutputPath,
492+
sdkModuleOutputPath);
485493
};
486494
auto dependencies = clangScanningTool.getTranslationUnitDependencies(
487495
commandLineArgs, workingDir, cache.getAlreadySeenClangModules(),
@@ -492,7 +500,7 @@ bool ClangImporter::getHeaderDependencies(
492500
// Record module dependencies for each new module we found.
493501
auto bridgedDeps = bridgeClangModuleDependencies(
494502
clangScanningTool, dependencies->ModuleGraph,
495-
cache.getModuleOutputPath(),
503+
moduleOutputPath, sdkModuleOutputPath,
496504
[&cache](StringRef path) {
497505
return cache.getScanService().remapPath(path);
498506
});

test/ScanDependencies/separate_sdk_module_cache.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,38 @@
33
// RUN: %empty-directory(%t/module-cache)
44
// RUN: %empty-directory(%t/sdk-module-cache)
55
// RUN: %empty-directory(%t/mock.sdk/System/Library/Frameworks/FooBar.framework/Modules/FooBar.swiftmodule)
6+
// RUN: %empty-directory(%t/mock.sdk/System/Library/Frameworks/Baz.framework/Modules)
7+
// RUN: %empty-directory(%t/mock.sdk/System/Library/Frameworks/Baz.framework/Headers)
8+
69
// RUN: split-file %s %t
710
// RUN: cp %t/FooBar.swiftinterface %t/mock.sdk/System/Library/Frameworks/FooBar.framework/Modules/FooBar.swiftmodule/%target-swiftinterface-name
11+
// RUN: cp %t/module.modulemap %t/mock.sdk/System/Library/Frameworks/Baz.framework/Modules/module.modulemap
12+
// RUN: cp %t/Baz.h %t/mock.sdk/System/Library/Frameworks/Baz.framework/Headers/Baz.h
813

9-
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/module-cache %t/test.swift -o %t/deps.json -I %S/Inputs/Swift -sdk %t/mock.sdk -sdk-module-cache-path %t/sdk-module-cache
14+
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/module-cache %t/test.swift -o %t/deps.json -I %S/Inputs/Swift -I %S/Inputs/CHeaders -sdk %t/mock.sdk -sdk-module-cache-path %t/sdk-module-cache
1015
// RUN: %validate-json %t/deps.json | %FileCheck %s
1116

17+
//--- module.modulemap
18+
framework module Baz {
19+
header "Baz.h"
20+
export *
21+
}
22+
23+
//--- Baz.h
24+
void funcBaz(void);
25+
1226
//--- FooBar.swiftinterface
1327
// swift-interface-format-version: 1.0
1428
// swift-module-flags: -module-name FooBar -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -user-module-version 1.0
1529
public func foo() {}
1630

1731
//--- test.swift
1832
import E
33+
import X
1934
import FooBar
35+
import Baz
2036

2137
// CHECK-DAG: "modulePath": "{{.*}}{{/|\\}}module-cache{{/|\\}}E-{{.*}}.swiftmodule"
38+
// CHECK-DAG: "modulePath": "{{.*}}{{/|\\}}module-cache{{/|\\}}X-{{.*}}.pcm"
2239
// CHECK-DAG: "modulePath": "{{.*}}{{/|\\}}sdk-module-cache{{/|\\}}FooBar-{{.*}}.swiftmodule"
40+
// CHECK-DAG: "modulePath": "{{.*}}{{/|\\}}sdk-module-cache{{/|\\}}Baz-{{.*}}.pcm

0 commit comments

Comments
 (0)