Skip to content

Re-land [Planning] Avoid batching compile job twice #1829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions Sources/SwiftDriver/Jobs/Planning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,18 @@ extension Driver {
incrementalCompilationState = nil
}

return try (
// For compatibility with swiftpm, the driver produces batched jobs
// for every job, even when run in incremental mode, so that all jobs
// can be returned from `planBuild`.
// But in that case, don't emit lifecycle messages.
formBatchedJobs(jobsInPhases.allJobs,
showJobLifecycle: showJobLifecycle && incrementalCompilationState == nil,
jobCreatingPch: jobsInPhases.allJobs.first(where: {$0.kind == .generatePCH})),
incrementalCompilationState
)
let batchedJobs: [Job]
// If the jobs are batched during the incremental build, reuse the computation rather than computing the batches again.
if let incrementalState = incrementalCompilationState {
// For compatibility reasons, all the jobs planned will be returned, even the incremental state suggests the job is not mandatory.
batchedJobs = incrementalState.skippedJobs + incrementalState.mandatoryJobsInOrder + incrementalState.jobsAfterCompiles
} else {
batchedJobs = try formBatchedJobs(jobsInPhases.allJobs,
showJobLifecycle: showJobLifecycle,
jobCreatingPch: jobsInPhases.allJobs.first(where: {$0.kind == .generatePCH}))
}

return (batchedJobs, incrementalCompilationState)
}

/// If performing an explicit module build, compute an inter-module dependency graph.
Expand Down
19 changes: 19 additions & 0 deletions Tests/SwiftDriverTests/IncrementalCompilationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,25 @@ extension IncrementalCompilationTests {
let expected = try AbsolutePath(validating: "\(module).autolink", relativeTo: derivedDataPath)
XCTAssertEqual(outputs.first!.file.absolutePath, expected)
}

// Null planning should not return an empty compile job for compatibility reason.
// `swift-build` wraps the jobs returned by swift-driver in `Executor` so returning an empty list of compile job will break build system.
func testNullPlanningCompatility() throws {
guard let sdkArgumentsForTesting = try Driver.sdkArgumentsForTesting() else {
throw XCTSkip("Cannot perform this test on this host")
}
var driver = try Driver(args: commonArgs + sdkArgumentsForTesting)
let initialJobs = try driver.planBuild()
try driver.run(jobs: initialJobs)

// Plan the build again without touching any file. This should be a null build but for compatibility reason,
// planBuild() should return all the jobs and supported build system will query incremental state for the actual
// jobs need to be executed.
let replanJobs = try driver.planBuild()
XCTAssertFalse(
replanJobs.filter { $0.kind == .compile }.isEmpty,
"more than one compile job needs to be planned")
}
}

// MARK: - Dot file tests
Expand Down