@@ -29,6 +29,7 @@ import protocol TSCUtility.ProgressAnimationProtocol
29
29
@_implementationOnly import SwiftDriver
30
30
31
31
public final class BuildOperation : PackageStructureDelegate , SPMBuildCore . BuildSystem , BuildErrorAdviceProvider {
32
+
32
33
/// The delegate used by the build system.
33
34
public weak var delegate : SPMBuildCore . BuildSystemDelegate ?
34
35
@@ -38,8 +39,14 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
38
39
/// The closure for loading the package graph.
39
40
let packageGraphLoader : ( ) throws -> PackageGraph
40
41
41
- /// the plugin configuration for build plugins
42
- let pluginConfiguration : PluginConfiguration ?
42
+ /// Entity responsible for compiling and running plugin scripts.
43
+ let pluginScriptRunner : PluginScriptRunner
44
+
45
+ /// Directory where plugin intermediate files are stored.
46
+ let pluginWorkDirectory : AbsolutePath
47
+
48
+ /// Whether to sandbox commands from build tool plugins.
49
+ public let disableSandboxForPluginCommands : Bool
43
50
44
51
/// The llbuild build delegate reference.
45
52
private var buildSystemDelegate : BuildOperationBuildSystemDelegateHandler ?
@@ -92,8 +99,10 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
92
99
buildParameters: BuildParameters ,
93
100
cacheBuildManifest: Bool ,
94
101
packageGraphLoader: @escaping ( ) throws -> PackageGraph ,
95
- pluginConfiguration: PluginConfiguration ? = . none,
96
102
additionalFileRules: [ FileRuleDescription ] ,
103
+ pluginScriptRunner: PluginScriptRunner ,
104
+ pluginWorkDirectory: AbsolutePath ,
105
+ disableSandboxForPluginCommands: Bool ,
97
106
outputStream: OutputByteStream ,
98
107
logLevel: Basics . Diagnostic . Severity ,
99
108
fileSystem: TSCBasic . FileSystem ,
@@ -107,7 +116,9 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
107
116
self . cacheBuildManifest = cacheBuildManifest
108
117
self . packageGraphLoader = packageGraphLoader
109
118
self . additionalFileRules = additionalFileRules
110
- self . pluginConfiguration = pluginConfiguration
119
+ self . pluginScriptRunner = pluginScriptRunner
120
+ self . pluginWorkDirectory = pluginWorkDirectory
121
+ self . disableSandboxForPluginCommands = disableSandboxForPluginCommands
111
122
self . outputStream = outputStream
112
123
self . logLevel = logLevel
113
124
self . fileSystem = fileSystem
@@ -312,10 +323,6 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
312
323
// Compiles a single plugin, emitting its output and throwing an error if it
313
324
// fails.
314
325
func compilePlugin( _ plugin: PluginDescription ) throws {
315
- guard let pluginConfiguration = self . pluginConfiguration else {
316
- throw InternalError ( " unknown plugin script runner " )
317
-
318
- }
319
326
// Compile the plugin, getting back a PluginCompilationResult.
320
327
class Delegate : PluginScriptCompilerDelegate {
321
328
let preparationStepName : String
@@ -344,7 +351,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
344
351
}
345
352
let delegate = Delegate ( preparationStepName: " Compiling plugin \( plugin. targetName) " , buildSystemDelegate: self . buildSystemDelegate)
346
353
let result = try tsc_await {
347
- pluginConfiguration . scriptRunner . compilePluginScript (
354
+ self . pluginScriptRunner . compilePluginScript (
348
355
sourceFiles: plugin. sources. paths,
349
356
pluginName: plugin. targetName,
350
357
toolsVersion: plugin. toolsVersion,
@@ -386,48 +393,41 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
386
393
// Load the package graph.
387
394
let graph = try getPackageGraph ( )
388
395
389
- let buildToolPluginInvocationResults : [ ResolvedTarget : [ BuildToolPluginInvocationResult ] ]
390
- let prebuildCommandResults : [ ResolvedTarget : [ PrebuildCommandResult ] ]
391
396
// Invoke any build tool plugins in the graph to generate prebuild commands and build commands.
392
- if let pluginConfiguration = self . pluginConfiguration {
393
- buildToolPluginInvocationResults = try graph. invokeBuildToolPlugins (
394
- outputDir: pluginConfiguration. workDirectory. appending ( component: " outputs " ) ,
395
- builtToolsDir: self . buildParameters. buildPath,
396
- buildEnvironment: self . buildParameters. buildEnvironment,
397
- toolSearchDirectories: [ self . buildParameters. toolchain. swiftCompilerPath. parentDirectory] ,
398
- pluginScriptRunner: pluginConfiguration. scriptRunner,
399
- observabilityScope: self . observabilityScope,
400
- fileSystem: self . fileSystem
401
- )
402
-
403
- // Surface any diagnostics from build tool plugins.
404
- for (target, results) in buildToolPluginInvocationResults {
405
- // There is one result for each plugin that gets applied to a target.
406
- for result in results {
407
- let diagnosticsEmitter = self . observabilityScope. makeDiagnosticsEmitter {
408
- var metadata = ObservabilityMetadata ( )
409
- metadata. targetName = target. name
410
- metadata. pluginName = result. plugin. name
411
- return metadata
412
- }
413
- for line in result. textOutput. split ( separator: " \n " ) {
414
- diagnosticsEmitter. emit ( info: line)
415
- }
416
- for diag in result. diagnostics {
417
- diagnosticsEmitter. emit ( diag)
418
- }
397
+ let buildToolPluginInvocationResults = try graph. invokeBuildToolPlugins (
398
+ outputDir: self . pluginWorkDirectory. appending ( component: " outputs " ) ,
399
+ builtToolsDir: self . buildParameters. buildPath,
400
+ buildEnvironment: self . buildParameters. buildEnvironment,
401
+ toolSearchDirectories: [ self . buildParameters. toolchain. swiftCompilerPath. parentDirectory] ,
402
+ pluginScriptRunner: self . pluginScriptRunner,
403
+ observabilityScope: self . observabilityScope,
404
+ fileSystem: self . fileSystem
405
+ )
406
+
407
+ // Surface any diagnostics from build tool plugins.
408
+ for (target, results) in buildToolPluginInvocationResults {
409
+ // There is one result for each plugin that gets applied to a target.
410
+ for result in results {
411
+ let diagnosticsEmitter = self . observabilityScope. makeDiagnosticsEmitter {
412
+ var metadata = ObservabilityMetadata ( )
413
+ metadata. targetName = target. name
414
+ metadata. pluginName = result. plugin. name
415
+ return metadata
416
+ }
417
+ for line in result. textOutput. split ( separator: " \n " ) {
418
+ diagnosticsEmitter. emit ( info: line)
419
+ }
420
+ for diag in result. diagnostics {
421
+ diagnosticsEmitter. emit ( diag)
419
422
}
420
423
}
421
-
422
- // Run any prebuild commands provided by build tool plugins. Any failure stops the build.
423
- prebuildCommandResults = try graph. reachableTargets. reduce ( into: [ : ] , { partial, target in
424
- partial [ target] = try buildToolPluginInvocationResults [ target] . map { try self . runPrebuildCommands ( for: $0) }
425
- } )
426
- } else {
427
- buildToolPluginInvocationResults = [ : ]
428
- prebuildCommandResults = [ : ]
429
424
}
430
425
426
+ // Run any prebuild commands provided by build tool plugins. Any failure stops the build.
427
+ let prebuildCommandResults = try graph. reachableTargets. reduce ( into: [ : ] , { partial, target in
428
+ partial [ target] = try buildToolPluginInvocationResults [ target] . map { try self . runPrebuildCommands ( for: $0) }
429
+ } )
430
+
431
431
// Emit warnings about any unhandled files in authored packages. We do this after applying build tool plugins, once we know what files they handled.
432
432
for package in graph. rootPackages where package . manifest. toolsVersion >= . v5_3 {
433
433
for target in package . targets {
@@ -472,7 +472,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
472
472
473
473
let ( buildDescription, buildManifest) = try BuildDescription . create (
474
474
with: plan,
475
- disableSandboxForPluginCommands: self . pluginConfiguration ? . disableSandbox ?? false ,
475
+ disableSandboxForPluginCommands: self . disableSandboxForPluginCommands ,
476
476
fileSystem: self . fileSystem,
477
477
observabilityScope: self . observabilityScope
478
478
)
@@ -526,7 +526,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
526
526
buildFile: buildParameters. llbuildManifest. pathString,
527
527
databaseFile: databasePath,
528
528
delegate: buildSystemDelegate,
529
- schedulerLanes: buildParameters. workers
529
+ schedulerLanes: buildParameters. jobs
530
530
)
531
531
532
532
// TODO: this seems fragile, perhaps we replace commandFailureHandler by adding relevant calls in the delegates chain
@@ -541,10 +541,6 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
541
541
/// Runs any prebuild commands associated with the given list of plugin invocation results, in order, and returns the
542
542
/// results of running those prebuild commands.
543
543
private func runPrebuildCommands( for pluginResults: [ BuildToolPluginInvocationResult ] ) throws -> [ PrebuildCommandResult ] {
544
- guard let pluginConfiguration = self . pluginConfiguration else {
545
- throw InternalError ( " unknown plugin script runner " )
546
-
547
- }
548
544
// Run through all the commands from all the plugin usages in the target.
549
545
return try pluginResults. map { pluginResult in
550
546
// As we go we will collect a list of prebuild output directories whose contents should be input to the build,
@@ -557,7 +553,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
557
553
// Run the command configuration as a subshell. This doesn't return until it is done.
558
554
// TODO: We need to also use any working directory, but that support isn't yet available on all platforms at a lower level.
559
555
var commandLine = [ command. configuration. executable. pathString] + command. configuration. arguments
560
- if !pluginConfiguration . disableSandbox {
556
+ if !self . disableSandboxForPluginCommands {
561
557
commandLine = try Sandbox . apply ( command: commandLine, strictness: . writableTemporaryDirectory, writableDirectories: [ pluginResult. pluginOutputDirectory] )
562
558
}
563
559
let processResult = try TSCBasic . Process. popen ( arguments: commandLine, environment: command. configuration. environment)
@@ -617,25 +613,6 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
617
613
}
618
614
}
619
615
620
- extension BuildOperation {
621
- public struct PluginConfiguration {
622
- /// Entity responsible for compiling and running plugin scripts.
623
- let scriptRunner : PluginScriptRunner
624
-
625
- /// Directory where plugin intermediate files are stored.
626
- let workDirectory : AbsolutePath
627
-
628
- /// Whether to sandbox commands from build tool plugins.
629
- let disableSandbox : Bool
630
-
631
- public init ( scriptRunner: PluginScriptRunner , workDirectory: AbsolutePath , disableSandbox: Bool ) {
632
- self . scriptRunner = scriptRunner
633
- self . workDirectory = workDirectory
634
- self . disableSandbox = disableSandbox
635
- }
636
- }
637
- }
638
-
639
616
extension BuildDescription {
640
617
static func create( with plan: BuildPlan , disableSandboxForPluginCommands: Bool , fileSystem: TSCBasic . FileSystem , observabilityScope: ObservabilityScope ) throws -> ( BuildDescription , LLBuildManifest . BuildManifest ) {
641
618
// Generate the llbuild manifest.
0 commit comments