Skip to content

Commit 6ad3707

Browse files
Address review feedback
1 parent 6a58253 commit 6ad3707

File tree

1 file changed

+37
-58
lines changed

1 file changed

+37
-58
lines changed

Sources/SwiftDriver/ExplicitModuleBuilds/ExplicitDependencyBuildPlanner.swift

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
142142
// Resolve all dependency module inputs for this Swift module
143143
try resolveExplicitModuleDependencies(moduleId: moduleId,
144144
inputs: &inputs,
145-
commandLine: &commandLine,
146-
isMainModule: false)
145+
commandLine: &commandLine)
147146

148147
// Build the .swiftinterfaces file using a list of command line options specified in the
149148
// `details` field.
@@ -199,8 +198,7 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
199198

200199
// Resolve all dependency module inputs for this Clang module
201200
try resolveExplicitModuleDependencies(moduleId: moduleId, inputs: &inputs,
202-
commandLine: &commandLine,
203-
isMainModule: false)
201+
commandLine: &commandLine)
204202

205203
let moduleMapPath = moduleDetails.moduleMapPath.path
206204
let modulePCMPath = moduleInfo.modulePath
@@ -229,8 +227,7 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
229227
/// to use explicitly-built module dependencies.
230228
private mutating func resolveExplicitModuleDependencies(moduleId: ModuleDependencyId,
231229
inputs: inout [TypedVirtualPath],
232-
commandLine: inout [Job.ArgTemplate],
233-
isMainModule: Bool) throws {
230+
commandLine: inout [Job.ArgTemplate]) throws {
234231
// Prohibit the frontend from implicitly building textual modules into binary modules.
235232
var swiftDependencyArtifacts: [SwiftModuleArtifactInfo] = []
236233
var clangDependencyArtifacts: [ClangModuleArtifactInfo] = []
@@ -265,24 +262,25 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
265262

266263
// Swift Main Module dependencies are passed encoded in a JSON file as described by
267264
// SwiftModuleArtifactInfo
268-
if isMainModule {
269-
if enableCAS {
270-
let dependencyFile =
271-
try serializeModuleDependenciesToCAS(for: moduleId,
272-
swiftDependencyArtifacts: swiftDependencyArtifacts,
273-
clangDependencyArtifacts: clangDependencyArtifacts)
274-
commandLine.appendFlag("-explicit-swift-module-map-file")
275-
commandLine.appendFlag(dependencyFile)
276-
} else {
277-
let dependencyFile =
278-
try serializeModuleDependencies(for: moduleId,
279-
swiftDependencyArtifacts: swiftDependencyArtifacts,
280-
clangDependencyArtifacts: clangDependencyArtifacts)
281-
commandLine.appendFlag("-explicit-swift-module-map-file")
282-
commandLine.appendPath(dependencyFile)
283-
inputs.append(TypedVirtualPath(file: dependencyFile.intern(),
284-
type: .jsonSwiftArtifacts))
285-
}
265+
guard moduleId == .swift(dependencyGraph.mainModuleName) else { return }
266+
let dependencyFileContent =
267+
try serializeModuleDependencies(for: moduleId,
268+
swiftDependencyArtifacts: swiftDependencyArtifacts,
269+
clangDependencyArtifacts: clangDependencyArtifacts)
270+
if enableCAS {
271+
// When using a CAS, write JSON into CAS and pass the ID on command-line.
272+
let casID = try swiftScanOracle.store(data: dependencyFileContent)
273+
commandLine.appendFlag("-explicit-swift-module-map-file")
274+
commandLine.appendFlag(casID)
275+
} else {
276+
// Write JSON to a file and add the JSON artifacts to command-line and inputs.
277+
let dependencyFile =
278+
VirtualPath.createUniqueTemporaryFileWithKnownContents(.init("\(moduleId.moduleName)-dependencies.json"),
279+
dependencyFileContent)
280+
commandLine.appendFlag("-explicit-swift-module-map-file")
281+
commandLine.appendPath(dependencyFile)
282+
inputs.append(TypedVirtualPath(file: dependencyFile.intern(),
283+
type: .jsonSwiftArtifacts))
286284
}
287285
}
288286

@@ -386,8 +384,7 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
386384
"-Xcc", "-fno-implicit-module-maps")
387385
try resolveExplicitModuleDependencies(moduleId: mainModuleId,
388386
inputs: &inputs,
389-
commandLine: &commandLine,
390-
isMainModule: true)
387+
commandLine: &commandLine)
391388
}
392389

393390
/// Resolve all module dependencies of the main module and add them to the lists of
@@ -433,53 +430,36 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
433430
}
434431

435432
// Return if depscanner provided build commands.
436-
if enableCAS, let scannerPCHArgs = mainModuleDetails.bridgingPchCommandLine {
433+
if let scannerPCHArgs = mainModuleDetails.bridgingPchCommandLine {
437434
scannerPCHArgs.forEach { commandLine.appendFlag($0) }
438435
return
439436
}
440437

438+
assert(!enableCAS, "Caching build should always return command-line from scanner")
441439
// Prohibit the frontend from implicitly building textual modules into binary modules.
442440
commandLine.appendFlags("-disable-implicit-swift-modules",
443441
"-Xcc", "-fno-implicit-modules",
444442
"-Xcc", "-fno-implicit-module-maps")
445443

446-
if enableCAS {
447-
let dependencyFile =
448-
try serializeModuleDependenciesToCAS(for: mainModuleId,
449-
swiftDependencyArtifacts: swiftDependencyArtifacts,
450-
clangDependencyArtifacts: clangDependencyArtifacts)
451-
commandLine.appendFlag("-explicit-swift-module-map-file")
452-
commandLine.appendFlag(dependencyFile)
453-
} else {
454-
let dependencyFile =
444+
let dependencyFileContent =
455445
try serializeModuleDependencies(for: mainModuleId,
456446
swiftDependencyArtifacts: swiftDependencyArtifacts,
457447
clangDependencyArtifacts: clangDependencyArtifacts)
458-
commandLine.appendFlag("-explicit-swift-module-map-file")
459-
commandLine.appendPath(dependencyFile)
460-
inputs.append(TypedVirtualPath(file: dependencyFile.intern(),
461-
type: .jsonSwiftArtifacts))
462-
}
463-
}
464448

465-
/// Store the output file artifacts for a given module in a JSON file, return the file's path.
466-
private func serializeModuleDependencies(for moduleId: ModuleDependencyId,
467-
swiftDependencyArtifacts: [SwiftModuleArtifactInfo],
468-
clangDependencyArtifacts: [ClangModuleArtifactInfo]
469-
) throws -> VirtualPath {
470-
let allDependencyArtifacts: [ModuleDependencyArtifactInfo] =
471-
swiftDependencyArtifacts.map {ModuleDependencyArtifactInfo.swift($0)} +
472-
clangDependencyArtifacts.map {ModuleDependencyArtifactInfo.clang($0)}
473-
let encoder = JSONEncoder()
474-
encoder.outputFormatting = [.prettyPrinted]
475-
let contents = try encoder.encode(allDependencyArtifacts)
476-
return VirtualPath.createUniqueTemporaryFileWithKnownContents(.init("\(moduleId.moduleName)-dependencies.json"), contents)
449+
let dependencyFile =
450+
VirtualPath.createUniqueTemporaryFileWithKnownContents(.init("\(mainModuleId.moduleName)-dependencies.json"),
451+
dependencyFileContent)
452+
commandLine.appendFlag("-explicit-swift-module-map-file")
453+
commandLine.appendPath(dependencyFile)
454+
inputs.append(TypedVirtualPath(file: dependencyFile.intern(),
455+
type: .jsonSwiftArtifacts))
477456
}
478457

479-
private func serializeModuleDependenciesToCAS(for moduleId: ModuleDependencyId,
458+
/// Serialize the output file artifacts for a given module in JSON format.
459+
private func serializeModuleDependencies(for moduleId: ModuleDependencyId,
480460
swiftDependencyArtifacts: [SwiftModuleArtifactInfo],
481461
clangDependencyArtifacts: [ClangModuleArtifactInfo]
482-
) throws -> String {
462+
) throws -> Data {
483463
// The module dependency map in CAS needs to be stable.
484464
// Sort the dependencies by name.
485465
let allDependencyArtifacts: [ModuleDependencyArtifactInfo] =
@@ -488,8 +468,7 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
488468
let encoder = JSONEncoder()
489469
// Use sorted key to ensure the order of the keys is stable.
490470
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
491-
let contents = try encoder.encode(allDependencyArtifacts)
492-
return try swiftScanOracle.store(data: contents)
471+
return try encoder.encode(allDependencyArtifacts)
493472
}
494473

495474
private func getPCMHashParts(pcmArgs: [String], contextHash: String) -> [String] {

0 commit comments

Comments
 (0)