Skip to content

Commit 1a9a15f

Browse files
authored
Merge pull request #62116 from artemcm/DepScanSwiftOutputPath
[Dependency Scanning] Produce canonical output path for Swift binary modules.
2 parents 4770622 + 729ad40 commit 1a9a15f

14 files changed

+65
-37
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ struct CommonSwiftTextualModuleDependencyDetails {
125125
class SwiftInterfaceModuleDependenciesStorage :
126126
public ModuleDependenciesStorageBase {
127127
public:
128+
/// Destination output path
129+
const std::string moduleOutputPath;
130+
128131
/// The Swift interface file to be used to generate the module file.
129132
const std::string swiftInterfaceFile;
130133

@@ -145,13 +148,15 @@ class SwiftInterfaceModuleDependenciesStorage :
145148
CommonSwiftTextualModuleDependencyDetails textualModuleDetails;
146149

147150
SwiftInterfaceModuleDependenciesStorage(
151+
const std::string moduleOutputPath,
148152
const std::string swiftInterfaceFile,
149153
ArrayRef<std::string> compiledModuleCandidates,
150154
ArrayRef<StringRef> buildCommandLine,
151155
ArrayRef<StringRef> extraPCMArgs,
152156
StringRef contextHash,
153157
bool isFramework
154158
) : ModuleDependenciesStorageBase(ModuleDependenciesKind::SwiftInterface),
159+
moduleOutputPath(moduleOutputPath),
155160
swiftInterfaceFile(swiftInterfaceFile),
156161
compiledModuleCandidates(compiledModuleCandidates.begin(),
157162
compiledModuleCandidates.end()),
@@ -340,15 +345,16 @@ class ModuleDependencies {
340345
/// Describe the module dependencies for a Swift module that can be
341346
/// built from a Swift interface file (\c .swiftinterface).
342347
static ModuleDependencies forSwiftInterfaceModule(
343-
std::string swiftInterfaceFile,
348+
const std::string &moduleOutputPath,
349+
const std::string &swiftInterfaceFile,
344350
ArrayRef<std::string> compiledCandidates,
345351
ArrayRef<StringRef> buildCommands,
346352
ArrayRef<StringRef> extraPCMArgs,
347353
StringRef contextHash,
348354
bool isFramework) {
349355
return ModuleDependencies(
350356
std::make_unique<SwiftInterfaceModuleDependenciesStorage>(
351-
swiftInterfaceFile, compiledCandidates, buildCommands,
357+
moduleOutputPath, swiftInterfaceFile, compiledCandidates, buildCommands,
352358
extraPCMArgs, contextHash, isFramework));
353359
}
354360

include/swift/DependencyScan/SerializedModuleDependencyCacheFormat.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ using ModuleInfoLayout =
124124

125125
using SwiftInterfaceModuleDetailsLayout =
126126
BCRecordLayout<SWIFT_INTERFACE_MODULE_DETAILS_NODE, // ID
127-
FileIDField, // swiftInterfaceFile
127+
FileIDField, // outputFilePath
128+
FileIDField, // swiftInterfaceFile
128129
FileIDArrayIDField, // compiledModuleCandidates
129130
FlagIDArrayIDField, // buildCommandLine
130131
FlagIDArrayIDField, // extraPCMArgs

include/swift/Serialization/ModuleDependencyScanner.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,21 @@ namespace swift {
3737
Twine moduleInterfacePath, bool isFramework);
3838

3939
InterfaceSubContextDelegate &astDelegate;
40+
41+
/// Location where pre-built moduels are to be built into.
42+
std::string moduleCachePath;
4043
public:
4144
Optional<ModuleDependencies> dependencies;
4245

43-
ModuleDependencyScanner(
44-
ASTContext &ctx, ModuleLoadingMode LoadMode, Identifier moduleName,
45-
InterfaceSubContextDelegate &astDelegate,
46-
ScannerKind kind = MDS_plain)
46+
ModuleDependencyScanner(ASTContext &ctx, ModuleLoadingMode LoadMode,
47+
Identifier moduleName,
48+
InterfaceSubContextDelegate &astDelegate,
49+
ScannerKind kind = MDS_plain)
4750
: SerializedModuleLoaderBase(ctx, nullptr, LoadMode,
4851
/*IgnoreSwiftSourceInfoFile=*/true),
49-
kind(kind), moduleName(moduleName), astDelegate(astDelegate) {}
52+
kind(kind), moduleName(moduleName), astDelegate(astDelegate),
53+
moduleCachePath(getModuleCachePathFromClang(
54+
ctx.getClangModuleLoader()->getClangInstance())) {}
5055

5156
std::error_code findModuleFilesInDirectory(
5257
ImportPath::Element ModuleID,

lib/DependencyScan/ModuleDependencyCacheSerialization.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,19 @@ bool Deserializer::readGraph(GlobalModuleDependenciesCache &cache) {
227227
llvm::report_fatal_error(
228228
"Unexpected SWIFT_TEXTUAL_MODULE_DETAILS_NODE record");
229229
cache.configureForTriple(getTriple());
230-
unsigned interfaceFileID, compiledModuleCandidatesArrayID,
230+
unsigned outputPathFileID, interfaceFileID, compiledModuleCandidatesArrayID,
231231
buildCommandLineArrayID, extraPCMArgsArrayID, contextHashID,
232232
isFramework, bridgingHeaderFileID, sourceFilesArrayID,
233233
bridgingSourceFilesArrayID, bridgingModuleDependenciesArrayID;
234234
SwiftInterfaceModuleDetailsLayout::readRecord(
235-
Scratch, interfaceFileID, compiledModuleCandidatesArrayID,
235+
Scratch, outputPathFileID, interfaceFileID, compiledModuleCandidatesArrayID,
236236
buildCommandLineArrayID, extraPCMArgsArrayID, contextHashID,
237237
isFramework, bridgingHeaderFileID, sourceFilesArrayID,
238238
bridgingSourceFilesArrayID, bridgingModuleDependenciesArrayID);
239239

240+
auto outputModulePath = getIdentifier(outputPathFileID);
241+
if (!outputModulePath)
242+
llvm::report_fatal_error("Bad .swiftmodule output path");
240243
Optional<std::string> optionalSwiftInterfaceFile;
241244
if (interfaceFileID != 0) {
242245
auto swiftInterfaceFile = getIdentifier(interfaceFileID);
@@ -267,6 +270,7 @@ bool Deserializer::readGraph(GlobalModuleDependenciesCache &cache) {
267270

268271
// Form the dependencies storage object
269272
auto moduleDep = ModuleDependencies::forSwiftInterfaceModule(
273+
outputModulePath.getValue(),
270274
optionalSwiftInterfaceFile.getValue(), *compiledModuleCandidates,
271275
buildCommandRefs, extraPCMRefs, *contextHash, isFramework);
272276

@@ -782,6 +786,8 @@ void Serializer::writeModuleInfo(ModuleDependencyID moduleID,
782786
assert(triple.hasValue() && "Expected triple for serializing MODULE_NODE");
783787
auto swiftTextDeps = dependencyInfo.getAsSwiftInterfaceModule();
784788
assert(swiftTextDeps);
789+
unsigned outputModulePathFileId =
790+
getIdentifier(swiftTextDeps->moduleOutputPath);
785791
unsigned swiftInterfaceFileId =
786792
getIdentifier(swiftTextDeps->swiftInterfaceFile);
787793
unsigned bridgingHeaderFileId =
@@ -791,6 +797,7 @@ void Serializer::writeModuleInfo(ModuleDependencyID moduleID,
791797
: 0;
792798
SwiftInterfaceModuleDetailsLayout::emitRecord(
793799
Out, ScratchRecord, AbbrCodes[SwiftInterfaceModuleDetailsLayout::Code],
800+
outputModulePathFileId,
794801
swiftInterfaceFileId,
795802
getArray(moduleID, ModuleIdentifierArrayKind::CompiledModuleCandidates),
796803
getArray(moduleID, ModuleIdentifierArrayKind::BuildCommandLine),
@@ -981,6 +988,7 @@ void Serializer::collectStringsAndArrays(
981988
case swift::ModuleDependenciesKind::SwiftInterface: {
982989
auto swiftTextDeps = dependencyInfo.getAsSwiftInterfaceModule();
983990
assert(swiftTextDeps);
991+
addIdentifier(swiftTextDeps->moduleOutputPath);
984992
addIdentifier(swiftTextDeps->swiftInterfaceFile);
985993
addArray(moduleID,
986994
ModuleIdentifierArrayKind::CompiledModuleCandidates,

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ static void writeJSON(llvm::raw_ostream &out,
590590
modulePath = get_C_string(swiftPlaceholderDeps->compiled_module_path);
591591
else if (swiftBinaryDeps)
592592
modulePath = get_C_string(swiftBinaryDeps->compiled_module_path);
593-
else if (clangDeps)
593+
else if (clangDeps || swiftTextualDeps)
594594
modulePath = get_C_string(moduleInfo.module_path);
595595
else
596596
modulePath = moduleName + modulePathSuffix;
@@ -829,7 +829,9 @@ generateFullDependencyGraph(CompilerInstance &instance,
829829
const char *modulePathSuffix =
830830
moduleDeps.isSwiftModule() ? ".swiftmodule" : ".pcm";
831831
std::string modulePath;
832-
if (swiftPlaceholderDeps)
832+
if (swiftTextualDeps)
833+
modulePath = swiftTextualDeps->moduleOutputPath;
834+
else if (swiftPlaceholderDeps)
833835
modulePath = swiftPlaceholderDeps->compiledModulePath;
834836
else if (swiftBinaryDeps)
835837
modulePath = swiftBinaryDeps->compiledModulePath;
@@ -1261,6 +1263,9 @@ bool swift::dependencies::scanDependencies(CompilerInstance &instance) {
12611263
if (opts.ReuseDependencyScannerCache)
12621264
deserializeDependencyCache(instance, globalCache);
12631265

1266+
auto ModuleCachePath = getModuleCachePathFromClang(
1267+
Context.getClangModuleLoader()->getClangInstance());
1268+
12641269
ModuleDependenciesCache cache(globalCache,
12651270
instance.getMainModule()->getNameStr());
12661271

lib/Serialization/ModuleDependencyScanner.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,15 @@ ErrorOr<ModuleDependencies> ModuleDependencyScanner::scanInterfaceFile(
118118
std::string InPath = moduleInterfacePath.str();
119119
auto compiledCandidates = getCompiledCandidates(Ctx, realModuleName.str(),
120120
InPath);
121-
Result = ModuleDependencies::forSwiftInterfaceModule(InPath,
122-
compiledCandidates,
123-
Args,
124-
PCMArgs,
125-
Hash,
126-
isFramework);
121+
122+
SmallString<128> outputPathBase(moduleCachePath);
123+
llvm::sys::path::append(
124+
outputPathBase,
125+
moduleName.str() + "-" + Hash + "." +
126+
file_types::getExtension(file_types::TY_SwiftModuleFile));
127+
Result = ModuleDependencies::forSwiftInterfaceModule(
128+
outputPathBase.str().str(), InPath, compiledCandidates, Args, PCMArgs,
129+
Hash, isFramework);
127130
// Open the interface file.
128131
auto &fs = *Ctx.SourceMgr.getFileSystem();
129132
auto interfaceBuf = fs.getBufferForFile(moduleInterfacePath);

test/ModuleInterface/clang-args-transitive-availability.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import ImportsMacroSpecificClangModule
2525
//CHECK: "swift": "ImportsMacroSpecificClangModule"
2626
//CHECK-NEXT: },
2727
//CHECK-NEXT: {
28-
//CHECK-NEXT: "modulePath": "ImportsMacroSpecificClangModule.swiftmodule",
28+
//CHECK-NEXT: "modulePath": "{{.*}}{{/|\\}}ImportsMacroSpecificClangModule-{{.*}}.swiftmodule",
2929
//CHECK-NEXT: "sourceFiles": [
3030
//CHECK-NEXT: ],
3131
//CHECK-NEXT: "directDependencies": [

test/ModuleInterface/extension-transitive-availability.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func foo() {
2727
// CHECK: "swift": "ExtensionAvailable"
2828
// CHECK-NEXT: },
2929
// CHECK-NEXT: {
30-
// CHECK-NEXT: "modulePath": "ExtensionAvailable.swiftmodule",
30+
// CHECK-NEXT: "modulePath": "{{.*}}{{/|\\}}ExtensionAvailable-{{.*}}.swiftmodule",
3131
// CHECK-NEXT: "sourceFiles": [
3232
// CHECK-NEXT: ],
3333
// CHECK-NEXT: "directDependencies": [

test/ScanDependencies/batch_module_scan.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
// CHECK-PCM-NEXT: "clang": "F"
2828
// CHECK-PCM-NEXT: },
2929
// CHECK-PCM-NEXT: {
30-
// CHECK-PCM-NEXT: "modulePath": "{{.*}}F-{{.*}}.pcm",
30+
// CHECK-PCM-NEXT: "modulePath": "{{.*}}{{/|\\}}F-{{.*}}.pcm",
3131
// CHECK-PCM: "-I
3232

3333
// CHECK-SWIFT: {
@@ -37,4 +37,4 @@
3737
// CHECK-SWIFT-NEXT: "swift": "F"
3838
// CHECK-SWIFT-NEXT: },
3939
// CHECK-SWIFT-NEXT: {
40-
// CHECK-SWIFT-NEXT: "modulePath": "F.swiftmodule",
40+
// CHECK-SWIFT-NEXT: "modulePath": "{{.*}}{{/|\\}}F-{{.*}}.swiftmodule",

test/ScanDependencies/module_deps.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ import SubE
9696
// CHECK-NEXT: ]
9797

9898
/// --------Swift module A
99-
// CHECK-LABEL: "modulePath": "A.swiftmodule",
99+
// CHECK-LABEL: "modulePath": "{{.*}}{{/|\\}}A-{{.*}}.swiftmodule",
100100

101101
// CHECK: directDependencies
102102
// CHECK-NEXT: {
@@ -145,7 +145,7 @@ import SubE
145145

146146
/// --------Swift module E
147147
// CHECK: "swift": "E"
148-
// CHECK-LABEL: modulePath": "E.swiftmodule"
148+
// CHECK-LABEL: modulePath": "{{.*}}{{/|\\}}E-{{.*}}.swiftmodule"
149149
// CHECK: "directDependencies"
150150
// CHECK-NEXT: {
151151
// CHECK-NEXT: "swift": "Swift"
@@ -154,7 +154,7 @@ import SubE
154154
// CHECK-SAME: E.swiftinterface
155155

156156
/// --------Swift module F
157-
// CHECK: "modulePath": "F.swiftmodule",
157+
// CHECK: "modulePath": "{{.*}}{{/|\\}}F-{{.*}}.swiftmodule",
158158
// CHECK-NEXT: "sourceFiles": [
159159
// CHECK-NEXT: ],
160160
// CHECK-NEXT: "directDependencies": [
@@ -170,7 +170,7 @@ import SubE
170170
// CHECK-NEXT: ],
171171

172172
/// --------Swift module G
173-
// CHECK-LABEL: "modulePath": "G.swiftmodule"
173+
// CHECK-LABEL: "modulePath": "{{.*}}{{/|\\}}G-{{.*}}.swiftmodule"
174174
// CHECK: "directDependencies"
175175
// CHECK-NEXT: {
176176
// CHECK-NEXT: "clang": "G"
@@ -202,7 +202,7 @@ import SubE
202202
// CHECK_CLANG_TARGET-NEXT: ]
203203

204204
/// --------Swift module Swift
205-
// CHECK-LABEL: "modulePath": "Swift.swiftmodule",
205+
// CHECK-LABEL: "modulePath": "{{.*}}{{/|\\}}Swift-{{.*}}.swiftmodule",
206206

207207
// CHECK: directDependencies
208208
// CHECK-NEXT: {

test/ScanDependencies/module_deps_cache_reuse.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ import SubE
8383
// CHECK-NEXT: ]
8484

8585
/// --------Swift module A
86-
// CHECK-LABEL: "modulePath": "A.swiftmodule",
86+
// CHECK-LABEL: "modulePath": "{{.*}}{{/|\\}}A-{{.*}}.swiftmodule",
8787

8888
// CHECK: directDependencies
8989
// CHECK-NEXT: {
@@ -120,7 +120,7 @@ import SubE
120120

121121
/// --------Swift module E
122122
// CHECK: "swift": "E"
123-
// CHECK-LABEL: modulePath": "E.swiftmodule"
123+
// CHECK-LABEL: modulePath": "{{.*}}{{/|\\}}E-{{.*}}.swiftmodule"
124124
// CHECK: "directDependencies"
125125
// CHECK-NEXT: {
126126
// CHECK-NEXT: "swift": "Swift"
@@ -129,7 +129,7 @@ import SubE
129129
// CHECK-SAME: E.swiftinterface
130130

131131
/// --------Swift module F
132-
// CHECK-LABEL: "modulePath": "F.swiftmodule",
132+
// CHECK-LABEL: "modulePath": "{{.*}}{{/|\\}}F-{{.*}}.swiftmodule",
133133
// CHECK-NEXT: "sourceFiles": [
134134
// CHECK-NEXT: ],
135135
// CHECK-NEXT: "directDependencies": [
@@ -145,7 +145,7 @@ import SubE
145145
// CHECK-NEXT: ],
146146

147147
/// --------Swift module G
148-
// CHECK-LABEL: "modulePath": "G.swiftmodule"
148+
// CHECK-LABEL: "modulePath": "{{.*}}{{/|\\}}G-{{.*}}.swiftmodule"
149149
// CHECK: "directDependencies"
150150
// CHECK-NEXT: {
151151
// CHECK-NEXT: "clang": "G"
@@ -174,7 +174,7 @@ import SubE
174174
// CHECK" ]
175175

176176
/// --------Swift module Swift
177-
// CHECK-LABEL: "modulePath": "Swift.swiftmodule",
177+
// CHECK-LABEL: "modulePath": "{{.*}}{{/|\\}}Swift-{{.*}}.swiftmodule",
178178

179179
// CHECK: directDependencies
180180
// CHECK-NEXT: {

test/ScanDependencies/module_deps_different_paths_no_reuse.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import A
1818

19-
// CHECK-INITIAL-SCAN: "modulePath": "A.swiftmodule",
19+
// CHECK-INITIAL-SCAN: "modulePath": "{{.*}}{{/|\\}}A-{{.*}}.swiftmodule",
2020
// CHECK-INITIAL-SCAN-NEXT: "sourceFiles": [
2121
// CHECK-INITIAL-SCAN-NEXT: ],
2222
// CHECK-INITIAL-SCAN-NEXT: "directDependencies": [
@@ -34,7 +34,7 @@ import A
3434
// CHECK-INITIAL-SCAN-NEXT: "swift": {
3535
// CHECK-INITIAL-SCAN-NEXT: "moduleInterfacePath": "{{.*}}/Swift/A.swiftinterface",
3636

37-
// CHECK-DIFFERENT: "modulePath": "A.swiftmodule",
37+
// CHECK-DIFFERENT: "modulePath": "{{.*}}{{/|\\}}A-{{.*}}.swiftmodule",
3838
// CHECK-DIFFERENT-NEXT: "sourceFiles": [
3939
// CHECK-DIFFERENT-NEXT: ],
4040
// CHECK-DIFFERENT-NEXT: "directDependencies": [

test/ScanDependencies/module_deps_external.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ import SomeExternalModule
8282
// CHECK-NEXT: ]
8383

8484
/// --------Swift external module SomeExternalModule
85-
// CHECK-LABEL: "modulePath": "BUILD_DIR/{{.*}}/ScanDependencies/Output/module_deps_external.swift.tmp/inputs/SomeExternalModule.swiftmodule",
85+
// CHECK-LABEL: "modulePath": "{{.*}}{{/|\\}}SomeExternalModule.swiftmodule",
8686
// CHECK-NEXT: "details": {
8787
// CHECK-NEXT: "swiftPlaceholder": {
8888
// CHECK-NEXT: "moduleDocPath": "BUILD_DIR/{{.*}}/ScanDependencies/Output/module_deps_external.swift.tmp/inputs/SomeExternalModule.swiftdoc",
8989
// CHECK-NEXT: "moduleSourceInfoPath": "BUILD_DIR/{{.*}}/ScanDependencies/Output/module_deps_external.swift.tmp/inputs/SomeExternalModule.swiftsourceinfo"
9090

9191
/// --------Swift module Swift
92-
// CHECK-LABEL: "modulePath": "Swift.swiftmodule",
92+
// CHECK-LABEL: "modulePath": "{{.*}}{{/|\\}}Swift-{{.*}}.swiftmodule",
9393

9494
// CHECK: directDependencies
9595
// CHECK-NEXT: {

test/ScanDependencies/placholder_overlay_deps.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import Metal
2323

2424
// Ensure the dependency on Darwin is captured even though it is a placeholder
2525

26-
// CHECK: "modulePath": "Metal.swiftmodule",
26+
// CHECK: "modulePath": "{{.*}}{{/|\\}}Metal-{{.*}}.swiftmodule",
2727
// CHECK-NEXT: "sourceFiles": [
2828
// CHECK-NEXT: ],
2929
// CHECK: {

0 commit comments

Comments
 (0)