Skip to content

Commit c1cf698

Browse files
committed
[Explicit Module Builds] Cache explicit dependency additions to main module command-lines
Cache input files and command-line argument additions describing main module explicit module dependencies to each individual job. Instead of re-computing them for each compile task, which includes serializing a whole new `.json` file with the inputs.
1 parent c283179 commit c1cf698

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

Sources/SwiftDriver/ExplicitModuleBuilds/ExplicitDependencyBuildPlanner.swift

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
5555
/// Does this compile support `.explicitInterfaceModuleBuild`
5656
private var supportsExplicitInterfaceBuild: Bool
5757

58+
/// Cached command-line additions for all main module compile jobs
59+
private struct ResolvedModuleDependenciesCommandLineComponents {
60+
let inputs: [TypedVirtualPath]
61+
let commandLine: [Job.ArgTemplate]
62+
}
63+
private var resolvedMainModuleDependenciesArgs: ResolvedModuleDependenciesCommandLineComponents? = nil
64+
5865
public init(dependencyGraph: InterModuleDependencyGraph,
5966
toolchain: Toolchain,
6067
dependencyOracle: InterModuleDependencyOracle,
@@ -396,18 +403,30 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
396403
/// inputs and command line flags.
397404
public mutating func resolveMainModuleDependencies(inputs: inout [TypedVirtualPath],
398405
commandLine: inout [Job.ArgTemplate]) throws {
399-
let mainModuleId: ModuleDependencyId = .swift(dependencyGraph.mainModuleName)
400-
401-
let mainModuleDetails = try dependencyGraph.swiftModuleDetails(of: mainModuleId)
402-
if let additionalArgs = mainModuleDetails.commandLine {
403-
additionalArgs.forEach { commandLine.appendFlag($0) }
406+
// If not previously computed, gather all dependency input files and command-line arguments
407+
if resolvedMainModuleDependenciesArgs == nil {
408+
var inputAdditions: [TypedVirtualPath] = []
409+
var commandLineAdditions: [Job.ArgTemplate] = []
410+
let mainModuleId: ModuleDependencyId = .swift(dependencyGraph.mainModuleName)
411+
let mainModuleDetails = try dependencyGraph.swiftModuleDetails(of: mainModuleId)
412+
if let additionalArgs = mainModuleDetails.commandLine {
413+
additionalArgs.forEach { commandLine.appendFlag($0) }
414+
}
415+
commandLineAdditions.appendFlags("-disable-implicit-swift-modules",
416+
"-Xcc", "-fno-implicit-modules",
417+
"-Xcc", "-fno-implicit-module-maps")
418+
try resolveExplicitModuleDependencies(moduleId: mainModuleId,
419+
inputs: &inputAdditions,
420+
commandLine: &commandLineAdditions)
421+
resolvedMainModuleDependenciesArgs = ResolvedModuleDependenciesCommandLineComponents(
422+
inputs: inputAdditions,
423+
commandLine: commandLineAdditions
424+
)
404425
}
405-
commandLine.appendFlags("-disable-implicit-swift-modules",
406-
"-Xcc", "-fno-implicit-modules",
407-
"-Xcc", "-fno-implicit-module-maps")
408-
try resolveExplicitModuleDependencies(moduleId: mainModuleId,
409-
inputs: &inputs,
410-
commandLine: &commandLine)
426+
427+
inputs.append(contentsOf: resolvedMainModuleDependenciesArgs!.inputs)
428+
commandLine.append(contentsOf: resolvedMainModuleDependenciesArgs!.commandLine)
429+
return
411430
}
412431

413432
/// Resolve all module dependencies of the main module and add them to the lists of

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -736,30 +736,21 @@ extension Driver {
736736

737737
/// Adds all dependencies required for an explicit module build
738738
/// to inputs and command line arguments of a compile job.
739-
func addExplicitModuleBuildArguments(inputs: inout [TypedVirtualPath],
740-
commandLine: inout [Job.ArgTemplate]) throws {
741-
guard var dependencyPlanner = explicitDependencyBuildPlanner else {
742-
fatalError("No dependency planner in Explicit Module Build mode.")
743-
}
744-
try dependencyPlanner.resolveMainModuleDependencies(inputs: &inputs, commandLine: &commandLine)
739+
mutating func addExplicitModuleBuildArguments(inputs: inout [TypedVirtualPath],
740+
commandLine: inout [Job.ArgTemplate]) throws {
741+
try explicitDependencyBuildPlanner?.resolveMainModuleDependencies(inputs: &inputs, commandLine: &commandLine)
745742
}
746743

747744
/// Adds all dependencies required for an explicit module build of the bridging header
748745
/// to inputs and command line arguments of a compile job.
749-
func addExplicitPCHBuildArguments(inputs: inout [TypedVirtualPath],
750-
commandLine: inout [Job.ArgTemplate]) throws {
751-
guard var dependencyPlanner = explicitDependencyBuildPlanner else {
752-
fatalError("No dependency planner in Explicit Module Build mode.")
753-
}
754-
try dependencyPlanner.resolveBridgingHeaderDependencies(inputs: &inputs, commandLine: &commandLine)
746+
mutating func addExplicitPCHBuildArguments(inputs: inout [TypedVirtualPath],
747+
commandLine: inout [Job.ArgTemplate]) throws {
748+
try explicitDependencyBuildPlanner?.resolveBridgingHeaderDependencies(inputs: &inputs, commandLine: &commandLine)
755749
}
756750

757751
/// If explicit dependency planner supports creating bridging header pch command.
758752
public func supportsBridgingHeaderPCHCommand() throws -> Bool {
759-
guard let dependencyPlanner = explicitDependencyBuildPlanner else {
760-
return false
761-
}
762-
return try dependencyPlanner.supportsBridgingHeaderPCHCommand()
753+
return try explicitDependencyBuildPlanner?.supportsBridgingHeaderPCHCommand() ?? false
763754
}
764755

765756
/// In Explicit Module Build mode, distinguish between main module jobs and intermediate dependency build jobs,

0 commit comments

Comments
 (0)