Skip to content

Commit 24f6391

Browse files
committed
Extract build planning from Driver.run
1 parent 1a1ae07 commit 24f6391

File tree

3 files changed

+37
-38
lines changed

3 files changed

+37
-38
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -551,37 +551,9 @@ extension Driver {
551551
}
552552
}
553553

554-
/// Create a job if needed for simple requests that can be immediately
555-
/// forwarded to the frontend.
556-
public mutating func immediateForwardingJob() throws -> Job? {
557-
if parsedOptions.hasArgument(.printTargetInfo) {
558-
var commandLine: [Job.ArgTemplate] = [.flag("-frontend"),
559-
.flag("-print-target-info")]
560-
try commandLine.appendLast(.target, from: &parsedOptions)
561-
try commandLine.appendLast(.sdk, from: &parsedOptions)
562-
try commandLine.appendLast(.resourceDir, from: &parsedOptions)
563-
return Job(kind: .printTargetInfo,
564-
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
565-
commandLine: commandLine,
566-
inputs: [],
567-
outputs: [],
568-
requiresInPlaceExecution: true)
569-
}
570-
571-
if parsedOptions.hasArgument(.version) || parsedOptions.hasArgument(.version_) {
572-
return Job(kind: .versionRequest,
573-
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
574-
commandLine: [.flag("--version")],
575-
inputs: [],
576-
outputs: [],
577-
requiresInPlaceExecution: true)
578-
}
579-
580-
return nil
581-
}
582-
583554
/// Run the driver.
584555
public mutating func run(
556+
jobs: [Job],
585557
resolver: ArgsResolver,
586558
executorDelegate: JobExecutorDelegate? = nil,
587559
processSet: ProcessSet? = nil
@@ -600,14 +572,6 @@ extension Driver {
600572
try printVersion(outputStream: &stderrStream)
601573
}
602574

603-
let jobs: [Job]
604-
if let job = try immediateForwardingJob() {
605-
jobs = [job]
606-
} else {
607-
// Plan the build.
608-
jobs = try planBuild()
609-
}
610-
611575
if jobs.isEmpty { return }
612576

613577
// If we're only supposed to print the jobs, do so now.

Sources/SwiftDriver/Jobs/Planning.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,42 @@ extension Driver {
165165
return jobs
166166
}
167167

168+
/// Create a job if needed for simple requests that can be immediately
169+
/// forwarded to the frontend.
170+
public mutating func immediateForwardingJob() throws -> Job? {
171+
if parsedOptions.hasArgument(.printTargetInfo) {
172+
var commandLine: [Job.ArgTemplate] = [.flag("-frontend"),
173+
.flag("-print-target-info")]
174+
try commandLine.appendLast(.target, from: &parsedOptions)
175+
try commandLine.appendLast(.sdk, from: &parsedOptions)
176+
try commandLine.appendLast(.resourceDir, from: &parsedOptions)
177+
return Job(kind: .printTargetInfo,
178+
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
179+
commandLine: commandLine,
180+
inputs: [],
181+
outputs: [],
182+
requiresInPlaceExecution: true)
183+
}
184+
185+
if parsedOptions.hasArgument(.version) || parsedOptions.hasArgument(.version_) {
186+
return Job(kind: .versionRequest,
187+
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
188+
commandLine: [.flag("--version")],
189+
inputs: [],
190+
outputs: [],
191+
requiresInPlaceExecution: true)
192+
}
193+
194+
return nil
195+
}
196+
168197
/// Plan a build by producing a set of jobs to complete the build.
169198
public mutating func planBuild() throws -> [Job] {
199+
// Handle invocations which can be trivially forwarded to the frontend.
200+
if let job = try immediateForwardingJob() {
201+
return [job]
202+
}
203+
170204
// Plan the build.
171205
switch compilerMode {
172206
case .repl:

Sources/swift-driver/main.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ do {
4141
}
4242

4343
var driver = try Driver(args: arguments, diagnosticsEngine: diagnosticsEngine)
44+
let jobs = try driver.planBuild()
4445
let resolver = try ArgsResolver()
45-
try driver.run(resolver: resolver, processSet: processSet)
46+
try driver.run(jobs: jobs, resolver: resolver, processSet: processSet)
4647

4748
if driver.diagnosticEngine.hasErrors {
4849
exit(EXIT_FAILURE)

0 commit comments

Comments
 (0)