Skip to content

Commit 67332d3

Browse files
committed
[Build] BuildOperation: Make createBuildSystem stateless
1 parent 7e6977c commit 67332d3

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

Sources/Build/BuildOperation.swift

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
5555
/// The path to scratch space (.build) directory.
5656
let scratchDirectory: AbsolutePath
5757

58-
/// The llbuild build delegate reference.
59-
private var progressTracker: LLBuildProgressTracker?
60-
61-
/// The llbuild build system reference.
62-
private var buildSystem: SPMLLBuild.BuildSystem?
58+
/// The llbuild build system reference previously created
59+
/// via \c createBuildSystem call.
60+
private var current: (buildSystem: SPMLLBuild.BuildSystem, tracker: LLBuildProgressTracker)?
6361

6462
/// If build manifest caching should be enabled.
6563
public let cacheBuildManifest: Bool
@@ -194,7 +192,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
194192

195193
/// Cancel the active build operation.
196194
public func cancel(deadline: DispatchTime) throws {
197-
buildSystem?.cancel()
195+
current?.buildSystem.cancel()
198196
}
199197

200198
// Emit a warning if a target imports another target in this build
@@ -355,8 +353,10 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
355353
try verifyTargetImports(in: buildDescription)
356354

357355
// Create the build system.
358-
let buildSystem = try self.createBuildSystem(buildDescription: buildDescription)
359-
self.buildSystem = buildSystem
356+
let (buildSystem, progressTracker) = try self.createBuildSystem(
357+
buildDescription: buildDescription
358+
)
359+
self.current = (buildSystem, progressTracker)
360360

361361
// If any plugins are part of the build set, compile them now to surface
362362
// any errors up-front. Returns true if we should proceed with the build
@@ -366,7 +366,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
366366
}
367367

368368
// delegate is only available after createBuildSystem is called
369-
self.progressTracker?.buildStart(configuration: self.productsBuildParameters.configuration)
369+
progressTracker.buildStart(configuration: self.productsBuildParameters.configuration)
370370

371371
// Perform the build.
372372
let llbuildTarget = try computeLLBuildTargetName(for: subset)
@@ -386,7 +386,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
386386
subsetDescriptor = nil
387387
}
388388

389-
self.progressTracker?.buildComplete(
389+
progressTracker.buildComplete(
390390
success: success,
391391
duration: duration,
392392
subsetDescriptor: subsetDescriptor
@@ -496,7 +496,10 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
496496
self.progressTracker?.preparationStepFinished(preparationStepName, result: (cachedResult.succeeded ? .succeeded : .failed))
497497
}
498498
}
499-
let delegate = Delegate(preparationStepName: "Compiling plugin \(plugin.targetName)", progressTracker: self.progressTracker)
499+
let delegate = Delegate(
500+
preparationStepName: "Compiling plugin \(plugin.targetName)",
501+
progressTracker: self.current?.tracker
502+
)
500503
let result = try temp_await {
501504
pluginConfiguration.scriptRunner.compilePluginScript(
502505
sourceFiles: plugin.sources.paths,
@@ -745,8 +748,10 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
745748

746749
/// Build the package structure target.
747750
private func buildPackageStructure() throws -> Bool {
748-
let buildSystem = try self.createBuildSystem(buildDescription: .none)
749-
self.buildSystem = buildSystem
751+
let (buildSystem, tracker) = try self.createBuildSystem(
752+
buildDescription: .none
753+
)
754+
self.current = (buildSystem, tracker)
750755

751756
// Build the package structure target which will re-generate the llbuild manifest, if necessary.
752757
return buildSystem.build(target: "PackageStructure")
@@ -756,7 +761,9 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
756761
///
757762
/// The build description should only be omitted when creating the build system for
758763
/// building the package structure target.
759-
private func createBuildSystem(buildDescription: BuildDescription?) throws -> SPMLLBuild.BuildSystem {
764+
private func createBuildSystem(
765+
buildDescription: BuildDescription?
766+
) throws -> (buildSystem: SPMLLBuild.BuildSystem, tracker: LLBuildProgressTracker) {
760767
// Figure out which progress bar we have to use during the build.
761768
let progressAnimation = ProgressAnimation.ninja(
762769
stream: self.outputStream,
@@ -782,16 +789,17 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
782789
observabilityScope: self.observabilityScope,
783790
delegate: self.delegate
784791
)
785-
self.progressTracker = progressTracker
786792

787793
let databasePath = self.scratchDirectory.appending("build.db").pathString
788794

789-
return SPMLLBuild.BuildSystem(
795+
let llbuildSystem = SPMLLBuild.BuildSystem(
790796
buildFile: self.productsBuildParameters.llbuildManifest.pathString,
791797
databaseFile: databasePath,
792798
delegate: progressTracker,
793799
schedulerLanes: self.productsBuildParameters.workers
794800
)
801+
802+
return (buildSystem: llbuildSystem, tracker: progressTracker)
795803
}
796804

797805
/// Runs any prebuild commands associated with the given list of plugin invocation results, in order, and returns the

0 commit comments

Comments
 (0)