forked from swiftlang/swift-package-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPIFBuilder.swift
More file actions
864 lines (734 loc) · 38 KB
/
PIFBuilder.swift
File metadata and controls
864 lines (734 loc) · 38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
@_spi(SwiftPMInternal) import Basics
import Foundation
import PackageGraph
import PackageLoading
import PackageModel
import TSCUtility
@_spi(SwiftPMInternal)
import SPMBuildCore
import func TSCBasic.topologicalSort
import var TSCBasic.stdoutStream
import enum SwiftBuild.ProjectModel
public struct PIFGenerationResult {
public var pif: String
public var accompanyingMetadata: [PackagePIFBuilder.ModuleOrProduct]
}
fileprivate func memoize<T>(to cache: inout T?, build: () async throws -> T) async rethrows -> T {
if let value = cache {
return value
} else {
let value = try await build()
cache = value
return value
}
}
/// The parameters required by `PIFBuilder`.
package struct PIFBuilderParameters {
/// Whether the toolchain supports `-package-name` option.
let isPackageAccessModifierSupported: Bool
/// Whether or not build for testability is enabled.
let enableTestability: Bool
/// Whether to create dylibs for dynamic library products.
let shouldCreateDylibForDynamicProducts: Bool
/// Eagerly materialize static archive products.
let materializeStaticArchiveProductsForRootPackages: Bool
/// Create dynamic library variants for automatic library products.
let createDynamicVariantsForLibraryProducts: Bool
/// The path to the library directory of the active toolchain.
let toolchainLibDir: AbsolutePath
/// An array of paths to search for pkg-config `.pc` files.
let pkgConfigDirectories: [AbsolutePath]
/// The Swift language versions supported by the SwiftBuild being used for the build.
let supportedSwiftVersions: [SwiftLanguageVersion]
/// The plugin script runner that will compile and run plugins.
let pluginScriptRunner: PluginScriptRunner
/// Disable the sandbox for the custom tasks
let disableSandbox: Bool
/// The working directory where the plugins should produce their results
let pluginWorkingDirectory: AbsolutePath
/// Additional rules for including a source or resource file in a target
let additionalFileRules: [FileRuleDescription]
/// Add rpaths which allow loading libraries adjacent to the current image at runtime. This is desirable
/// when launching build products from the build directory, but should often be disabled when deploying
/// the build products to a different location.
let addLocalRpaths: Bool
package init(isPackageAccessModifierSupported: Bool, enableTestability: Bool, shouldCreateDylibForDynamicProducts: Bool, materializeStaticArchiveProductsForRootPackages: Bool, createDynamicVariantsForLibraryProducts: Bool, toolchainLibDir: AbsolutePath, pkgConfigDirectories: [AbsolutePath], supportedSwiftVersions: [SwiftLanguageVersion], pluginScriptRunner: PluginScriptRunner, disableSandbox: Bool, pluginWorkingDirectory: AbsolutePath, additionalFileRules: [FileRuleDescription], addLocalRPaths: Bool) {
self.isPackageAccessModifierSupported = isPackageAccessModifierSupported
self.enableTestability = enableTestability
self.shouldCreateDylibForDynamicProducts = shouldCreateDylibForDynamicProducts
self.materializeStaticArchiveProductsForRootPackages = materializeStaticArchiveProductsForRootPackages
self.createDynamicVariantsForLibraryProducts = createDynamicVariantsForLibraryProducts
self.toolchainLibDir = toolchainLibDir
self.pkgConfigDirectories = pkgConfigDirectories
self.supportedSwiftVersions = supportedSwiftVersions
self.pluginScriptRunner = pluginScriptRunner
self.disableSandbox = disableSandbox
self.pluginWorkingDirectory = pluginWorkingDirectory
self.additionalFileRules = additionalFileRules
self.addLocalRpaths = addLocalRPaths
}
}
/// PIF object builder for a package graph.
public final class PIFBuilder {
/// Name of the PIF target aggregating all targets (*excluding* tests).
public static let allExcludingTestsTargetName = "AllExcludingTests"
/// Name of the PIF target aggregating all targets (*including* tests).
public static let allIncludingTestsTargetName = "AllIncludingTests"
/// The package graph to build from.
let graph: ModulesGraph
/// The parameters used to configure the PIF.
let parameters: PIFBuilderParameters
/// The ObservabilityScope to emit diagnostics to.
let observabilityScope: ObservabilityScope
/// The file system to read from.
let fileSystem: FileSystem
/// Configuration for building and invoking plugins.
private let pluginConfiguration: PluginConfiguration
/// Creates a `PIFBuilder` instance.
/// - Parameters:
/// - graph: The package graph to build from.
/// - parameters: The parameters used to configure the PIF.
/// - fileSystem: The file system to read from.
/// - observabilityScope: The ObservabilityScope to emit diagnostics to.
package init(
graph: ModulesGraph,
parameters: PIFBuilderParameters,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope,
) {
self.graph = graph
self.parameters = parameters
self.fileSystem = fileSystem
self.observabilityScope = observabilityScope.makeChildScope(description: "PIF Builder")
self.pluginConfiguration = PluginConfiguration(
scriptRunner: parameters.pluginScriptRunner,
workDirectory: parameters.pluginWorkingDirectory,
disableSandbox: parameters.disableSandbox,
)
}
/// Generates the PIF representation.
/// - Parameters:
/// - prettyPrint: Whether to return a formatted JSON.
/// - preservePIFModelStructure: Whether to preserve model structure.
/// - Returns: The package graph in the JSON PIF format.
package func generatePIF(
prettyPrint: Bool = true,
preservePIFModelStructure: Bool = false,
printPIFManifestGraphviz: Bool = false,
buildParameters: BuildParameters,
hostBuildParameters: BuildParameters
) async throws -> PIFGenerationResult {
let encoder = prettyPrint ? JSONEncoder.makeWithDefaults() : JSONEncoder()
if !preservePIFModelStructure {
encoder.userInfo[.encodeForSwiftBuild] = true
}
let (topLevelObject, modulesAndProducts) = try await self.constructPIF(buildParameters: buildParameters, hostBuildParameters: hostBuildParameters)
// Sign the PIF objects before encoding it for Swift Build.
try PIF.sign(workspace: topLevelObject.workspace)
let pifData = try encoder.encode(topLevelObject)
let pifString = String(decoding: pifData, as: UTF8.self)
if printPIFManifestGraphviz {
// Print dot graph to stdout.
writePIF(topLevelObject.workspace, toDOT: stdoutStream)
stdoutStream.flush()
// Abort the build process, ensuring we don't add
// further noise to stdout (and break `dot` graph parsing).
throw PIFGenerationError.printedPIFManifestGraphviz
}
return PIFGenerationResult(pif: pifString, accompanyingMetadata: modulesAndProducts)
}
private var cachedPIF: (PIF.TopLevelObject, [PackagePIFBuilder.ModuleOrProduct])?
/// Compute the available build tools, and their destination build path for host for each plugin.
private func availableBuildPluginTools(
graph: ModulesGraph,
buildParameters: BuildParameters,
hostBuildParameters: BuildParameters,
pluginsPerModule: [ResolvedModule.ID: [ResolvedModule]],
hostTriple: Basics.Triple
) async throws -> [ResolvedModule.ID: [String: PluginTool]] {
var accessibleToolsPerPlugin: [ResolvedModule.ID: [String: PluginTool]] = [:]
for (_, plugins) in pluginsPerModule {
for plugin in plugins where accessibleToolsPerPlugin[plugin.id] == nil {
// Determine the tools to which this plugin has access, and create a name-to-path mapping from tool
// names to the corresponding paths. Built tools are assumed to be in the build tools directory.
let accessibleTools = try await plugin.preparePluginTools(
fileSystem: fileSystem,
environment: buildParameters.buildEnvironment,
for: hostTriple
) { name, path in
return hostBuildParameters.buildPath.appending(path)
}
accessibleToolsPerPlugin[plugin.id] = accessibleTools
}
}
return accessibleToolsPerPlugin
}
/// Constructs all `PackagePIFBuilder` objects used by the `constructPIF` function.
/// In particular, this is useful for unit testing the complex `PIFBuilder` class.
func makePIFBuilders(
buildParameters: BuildParameters,
hostBuildParameters: BuildParameters
) async throws -> [(ResolvedPackage, PackagePIFBuilder, any PackagePIFBuilder.BuildDelegate)] {
let pluginScriptRunner = self.parameters.pluginScriptRunner
let outputDir = self.parameters.pluginWorkingDirectory.appending("outputs")
let pluginsPerModule = graph.pluginsPerModule(
satisfying: buildParameters.buildEnvironment // .buildEnvironment(for: .host)
)
let availablePluginTools = try await availableBuildPluginTools(
graph: graph,
buildParameters: buildParameters,
hostBuildParameters: hostBuildParameters,
pluginsPerModule: pluginsPerModule,
hostTriple: try pluginScriptRunner.hostTriple
)
let sortedPackages = self.graph.packages
.sorted { $0.manifest.displayName < $1.manifest.displayName } // TODO: use identity instead?
var packagesAndBuilders: [(ResolvedPackage, PackagePIFBuilder, any PackagePIFBuilder.BuildDelegate)] = []
for package in sortedPackages {
var buildToolPluginResultsByTargetName: [String: [PackagePIFBuilder.BuildToolPluginInvocationResult]] = [:]
for module in package.modules {
// Apply each build tool plugin used by the target in order,
// creating a list of results (one for each plugin usage).
var buildToolPluginResults: [BuildToolPluginInvocationResult] = []
var buildCommands: [PackagePIFBuilder.CustomBuildCommand] = []
var prebuildCommands: [BuildToolPluginInvocationResult.PrebuildCommand] = []
for plugin in module.pluginDependencies(satisfying: buildParameters.buildEnvironment) {
let pluginModule = plugin.underlying as! PluginModule
// Determine the tools to which this plugin has access, and create a name-to-path mapping from tool
// names to the corresponding paths. Built tools are assumed to be in the build tools directory.
guard let accessibleTools = availablePluginTools[plugin.id] else {
throw InternalError("No tools found for plugin \(plugin.name)")
}
// Assign a plugin working directory based on the package, target, and plugin.
let pluginOutputDir = outputDir.appending(
components: [
package.identity.description,
module.name,
buildParameters.destination == .host ? "tools" : "destination",
plugin.name,
]
)
// Determine the set of directories under which plugins are allowed to write.
// We always include just the output directory, and for now there is no possibility
// of opting into others.
let writableDirectories = [outputDir]
// Determine a set of further directories under which plugins are never allowed
// to write, even if they are covered by other rules (such as being able to write
// to the temporary directory).
let readOnlyDirectories = [package.path]
// In tools version 6.0 and newer, we vend the list of files generated by previous plugins.
let pluginDerivedSources: Sources
let pluginDerivedResources: [Resource]
if package.manifest.toolsVersion >= .v6_0 {
// Set up dummy observability because we don't want to emit diagnostics for this before the actual
// build.
let observability = ObservabilitySystem { _, _ in }
// Compute the generated files based on all results we have computed so far.
let pluginGeneratedFiles = ModulesGraph.computePluginGeneratedFiles(
target: module,
toolsVersion: package.manifest.toolsVersion,
additionalFileRules: self.parameters.additionalFileRules,
buildParameters: buildParameters,
buildToolPluginInvocationResults: buildToolPluginResults,
prebuildCommandResults: [],
observabilityScope: observability.topScope
)
pluginDerivedSources = Sources(
paths: pluginGeneratedFiles.sources.map(\.self),
root: buildParameters.dataPath
)
pluginDerivedResources = pluginGeneratedFiles.resources.values.map(\.self)
} else {
pluginDerivedSources = .init(paths: [], root: package.path)
pluginDerivedResources = []
}
let result = try await pluginModule.invoke(
module: plugin,
action: .createBuildToolCommands(
package: package,
target: module,
pluginGeneratedSources: pluginDerivedSources.paths,
pluginGeneratedResources: pluginDerivedResources.map(\.path)
),
buildEnvironment: buildParameters.buildEnvironment,
workers: buildParameters.workers,
scriptRunner: pluginScriptRunner,
workingDirectory: package.path,
outputDirectory: pluginOutputDir,
toolSearchDirectories: [buildParameters.toolchain.swiftCompilerPath.parentDirectory],
accessibleTools: accessibleTools,
writableDirectories: writableDirectories,
readOnlyDirectories: readOnlyDirectories,
allowNetworkConnections: [],
pkgConfigDirectories: self.parameters.pkgConfigDirectories,
sdkRootPath: buildParameters.toolchain.sdkRootPath,
fileSystem: fileSystem,
modulesGraph: self.graph,
observabilityScope: observabilityScope
)
buildToolPluginResults.append(result)
let diagnosticsEmitter = observabilityScope.makeDiagnosticsEmitter {
var metadata = ObservabilityMetadata()
metadata.moduleName = module.name
metadata.pluginName = result.plugin.name
return metadata
}
for line in result.textOutput.split(whereSeparator: { $0.isNewline }) {
diagnosticsEmitter.emit(info: line)
}
for diag in result.diagnostics {
diagnosticsEmitter.emit(diag)
}
prebuildCommands.append(contentsOf: result.prebuildCommands)
buildCommands.append(contentsOf: result.buildCommands.map( { buildCommand in
var newEnv: Environment = buildCommand.configuration.environment
// FIXME: This is largely a workaround for improper rpath setup on Linux. It should be
// removed once the Swift Build backend switches to use swiftc as the linker driver
// for targets with Swift sources. For now, limit the scope to non-macOS, so that
// plugins do not inadvertently use the toolchain stdlib instead of the OS stdlib
// when built with a Swift.org toolchain.
#if !os(macOS)
let runtimeLibPaths = buildParameters.toolchain.runtimeLibraryPaths
// Add paths to swift standard runtime libraries to the library path so that they can be found at runtime
for libPath in runtimeLibPaths {
newEnv.appendPath(key: .libraryPath, value: libPath.pathString)
}
#endif
// Append the system path at the end so that necessary system tool paths can be found
if let pathValue = Environment.current[EnvironmentKey.path] {
newEnv.appendPath(key: .path, value: pathValue)
}
let writableDirectories: [AbsolutePath] = [pluginOutputDir]
return PackagePIFBuilder.CustomBuildCommand(
displayName: buildCommand.configuration.displayName,
executable: buildCommand.configuration.executable.pathString,
arguments: buildCommand.configuration.arguments,
environment: .init(newEnv),
workingDir: package.path,
inputPaths: buildCommand.inputFiles,
outputPaths: buildCommand.outputFiles.map(\.pathString),
pluginOutputDir: pluginOutputDir,
sandboxProfile:
self.parameters.disableSandbox ?
nil :
.init(
strictness: .writableTemporaryDirectory,
writableDirectories: writableDirectories,
readOnlyDirectories: buildCommand.inputFiles
)
)
}))
}
// Run the prebuild commands generated from the plugin invocation now for this module. This will
// also give use the derived source code files needed for PIF generation.
let runResults = try Self.runPluginCommands(
using: self.pluginConfiguration,
for: buildToolPluginResults,
fileSystem: fileSystem,
observabilityScope: observabilityScope
)
let result = PackagePIFBuilder.BuildToolPluginInvocationResult(
prebuildCommandOutputPaths: runResults.flatMap( { $0.derivedFiles }),
buildCommands: buildCommands
)
// Add a BuildToolPluginInvocationResult to the mapping.
if var existingResults = buildToolPluginResultsByTargetName[module.name] {
existingResults.append(result)
} else {
buildToolPluginResultsByTargetName[module.name] = [result]
}
}
let packagePIFBuilderDelegate = PackagePIFBuilderDelegate(
package: package
)
let packagePIFBuilder = PackagePIFBuilder(
modulesGraph: self.graph,
resolvedPackage: package,
packageManifest: package.manifest,
delegate: packagePIFBuilderDelegate,
buildToolPluginResultsByTargetName: buildToolPluginResultsByTargetName,
createDylibForDynamicProducts: self.parameters.shouldCreateDylibForDynamicProducts,
materializeStaticArchiveProductsForRootPackages: self.parameters.materializeStaticArchiveProductsForRootPackages,
createDynamicVariantsForLibraryProducts: self.parameters.createDynamicVariantsForLibraryProducts,
addLocalRpaths: self.parameters.addLocalRpaths,
packageDisplayVersion: package.manifest.displayName,
pkgConfigDirectories: self.parameters.pkgConfigDirectories,
fileSystem: self.fileSystem,
observabilityScope: self.observabilityScope
)
packagesAndBuilders.append((package, packagePIFBuilder, packagePIFBuilderDelegate))
}
return packagesAndBuilders
}
/// Constructs a `PIF.TopLevelObject` representing the package graph.
package func constructPIF(
buildParameters: BuildParameters,
hostBuildParameters: BuildParameters
) async throws -> (PIF.TopLevelObject, [PackagePIFBuilder.ModuleOrProduct]) {
return try await memoize(to: &self.cachedPIF) {
let rootPackages = self.graph.rootPackages
guard !rootPackages.isEmpty else {
throw PIFGenerationError.rootPackageNotFound
}
let packagesAndPIFBuilders = try await makePIFBuilders(buildParameters: buildParameters, hostBuildParameters: hostBuildParameters)
var modulesAndProducts: [PackagePIFBuilder.ModuleOrProduct] = []
let packagesAndPIFProjects = try packagesAndPIFBuilders.map { (package, pifBuilder, _) in
let builtModulesAndProducts = try pifBuilder.build()
modulesAndProducts.append(contentsOf: builtModulesAndProducts)
let pifProject: ProjectModel.Project = pifBuilder.pifProject
return (package, pifProject)
}
var pifProjects: [ProjectModel.Project] = packagesAndPIFProjects.map(\.1)
pifProjects.append(
try buildAggregatePIFProject(
packagesAndProjects: packagesAndPIFProjects,
observabilityScope: observabilityScope,
modulesGraph: graph,
buildParameters: buildParameters
)
)
let rootPackagesSorted = rootPackages.sorted()
let rootPackagesPaths = rootPackagesSorted.map { $0.path }
let ids: String = rootPackagesPaths.map { $0.pathString}.joined(separator: ",")
let names = rootPackagesSorted.map { $0.manifest.displayName }.joined(separator: ",")
let workspace = PIF.Workspace(
id: "Workspace:\(ids)",
name: names,
path: try getCommonParentDirectory(paths: rootPackagesPaths),
projects: pifProjects
)
return (PIF.TopLevelObject(workspace: workspace), modulesAndProducts)
}
}
/// Runs any commands associated with the given list of plugin invocation results,
/// in order, and returns the results of running those prebuild commands.
fileprivate static func runPluginCommands(
using pluginConfiguration: PluginConfiguration,
for pluginResults: [BuildToolPluginInvocationResult],
fileSystem: any FileSystem,
observabilityScope: ObservabilityScope
) throws -> [CommandPluginResult] {
// Run through all the commands from all the plugin usages in the target.
try pluginResults.map { pluginResult in
// As we go we will collect a list of prebuild output directories whose contents should be input to the
// build, and a list of the files in those directories after running the commands.
var derivedFiles: [Basics.AbsolutePath] = []
var prebuildOutputDirs: [Basics.AbsolutePath] = []
for command in pluginResult.prebuildCommands {
observabilityScope
.emit(
info: "Running " +
(command.configuration.displayName ?? command.configuration.executable.basename)
)
// Run the command configuration as a subshell. This doesn't return until it is done.
// TODO: We need to also use any working directory, but that support isn't yet available on all platforms at a lower level.
var commandLine = [command.configuration.executable.pathString] + command.configuration.arguments
if !pluginConfiguration.disableSandbox {
commandLine = try Sandbox.apply(
command: commandLine,
fileSystem: fileSystem,
strictness: .writableTemporaryDirectory,
writableDirectories: [pluginResult.pluginOutputDirectory]
)
}
let processResult = try AsyncProcess.popen(
arguments: commandLine,
environment: command.configuration.environment
)
let output = try processResult.utf8Output() + processResult.utf8stderrOutput()
if processResult.exitStatus != .terminated(code: 0) {
throw StringError("failed: \(command)\n\n\(output)")
}
// Add any files found in the output directory declared for the prebuild command after the command ends.
let outputFilesDir = command.outputFilesDirectory
if let swiftFiles = try? fileSystem.getDirectoryContents(outputFilesDir).sorted() {
derivedFiles.append(contentsOf: swiftFiles.map { outputFilesDir.appending(component: $0) })
}
// Add the output directory to the list of directories whose structure should affect the build plan.
prebuildOutputDirs.append(outputFilesDir)
}
// Add the results of running any prebuild commands for this invocation.
return CommandPluginResult(derivedFiles: derivedFiles, outputDirectories: prebuildOutputDirs)
}
}
// Convenience method for generating PIF.
public static func generatePIF(
buildParameters: BuildParameters,
hostBuildParameters: BuildParameters,
packageGraph: ModulesGraph,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope,
preservePIFModelStructure: Bool,
pluginScriptRunner: PluginScriptRunner,
disableSandbox: Bool,
pluginWorkingDirectory: AbsolutePath,
pkgConfigDirectories: [Basics.AbsolutePath],
additionalFileRules: [FileRuleDescription],
addLocalRpaths: Bool,
materializeStaticArchiveProductsForRootPackages: Bool,
createDynamicVariantsForLibraryProducts: Bool
) async throws -> PIFGenerationResult {
let parameters = PIFBuilderParameters(
buildParameters,
supportedSwiftVersions: [],
pluginScriptRunner: pluginScriptRunner,
disableSandbox: disableSandbox,
pluginWorkingDirectory: pluginWorkingDirectory,
additionalFileRules: additionalFileRules,
addLocalRpaths: addLocalRpaths,
materializeStaticArchiveProductsForRootPackages: materializeStaticArchiveProductsForRootPackages,
createDynamicVariantsForLibraryProducts: createDynamicVariantsForLibraryProducts
)
let builder = Self(
graph: packageGraph,
parameters: parameters,
fileSystem: fileSystem,
observabilityScope: observabilityScope
)
return try await builder.generatePIF(preservePIFModelStructure: preservePIFModelStructure, buildParameters: buildParameters, hostBuildParameters: hostBuildParameters)
}
}
fileprivate final class PackagePIFBuilderDelegate: PackagePIFBuilder.BuildDelegate {
let package: ResolvedPackage
init(package: ResolvedPackage) {
self.package = package
}
var isRootPackage: Bool {
self.package.manifest.packageKind.isRoot
}
var isRemote: Bool {
self.package.manifest.packageKind.isRemote
}
var hostsOnlyPackages: Bool {
false
}
var isUserManaged: Bool {
true
}
var isBranchOrRevisionBased: Bool {
false
}
func customProductType(forExecutable product: PackageModel.Product) -> ProjectModel.Target.ProductType? {
nil
}
func deviceFamilyIDs() -> Set<Int> {
[]
}
func shouldPackagesBuildForARM64e(platform: PackageModel.Platform) -> Bool {
false
}
var isPluginExecutionSandboxingDisabled: Bool {
false
}
func configureProjectBuildSettings(_ buildSettings: inout ProjectModel.BuildSettings) {
/* empty */
}
func configureSourceModuleBuildSettings(sourceModule: ResolvedModule, settings: inout ProjectModel.BuildSettings) {
settings[.SYMBOL_GRAPH_EXTRACTOR_OUTPUT_DIR] = "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/\(sourceModule.name).symbolgraphs"
}
func customInstallPath(product: PackageModel.Product) -> String? {
nil
}
func customProductName(forFramework product: PackageModel.Product) -> String? {
nil
}
func customBundleIdentifierPrefix(forFramework product: PackageModel.Product) -> String? {
nil
}
func customLibraryType(product: PackageModel.Product) -> PackageModel.ProductType.LibraryType? {
nil
}
func customSDKOptions(forPlatform: PackageModel.Platform) -> [String] {
[]
}
func addCustomTargets(pifProject: inout SwiftBuild.ProjectModel.Project) throws -> [PackagePIFBuilder.ModuleOrProduct] {
return []
}
func shouldSuppressProductDependency(product: PackageModel.Product, buildSettings: inout SwiftBuild.ProjectModel.BuildSettings) -> Bool {
false
}
func shouldSetInstallPathForDynamicLib(productName: String) -> Bool {
false
}
func configureLibraryProduct(
product: PackageModel.Product,
project: inout ProjectModel.Project,
target: WritableKeyPath<ProjectModel.Project, ProjectModel.Target>,
additionalFiles: WritableKeyPath<ProjectModel.Group, ProjectModel.Group>
) {
/* empty */
}
func suggestAlignedPlatformVersionGiveniOSVersion(platform: PackageModel.Platform, iOSVersion: PackageModel.PlatformVersion) -> String? {
nil
}
func validateMacroFingerprint(for macroModule: ResolvedModule) -> Bool {
true
}
}
fileprivate func buildAggregatePIFProject(
packagesAndProjects: [(package: ResolvedPackage, project: ProjectModel.Project)],
observabilityScope: ObservabilityScope,
modulesGraph: ModulesGraph,
buildParameters: BuildParameters
) throws -> ProjectModel.Project {
precondition(!packagesAndProjects.isEmpty)
var aggregateProject = ProjectModel.Project(
id: "AGGREGATE",
path: packagesAndProjects[0].project.path,
projectDir: packagesAndProjects[0].project.projectDir,
name: "Aggregate",
developmentRegion: "en"
)
observabilityScope.logPIF(.debug, "Created project '\(aggregateProject.id)' with name '\(aggregateProject.name)'")
var settings = ProjectModel.BuildSettings()
settings[.PRODUCT_NAME] = "$(TARGET_NAME)"
settings[.SUPPORTED_PLATFORMS] = ["$(AVAILABLE_PLATFORMS)"]
settings[.SDKROOT] = "auto"
settings[.SDK_VARIANT] = "auto"
settings[.SKIP_INSTALL] = "YES"
aggregateProject.addBuildConfig { id in BuildConfig(id: id, name: "Debug", settings: settings) }
aggregateProject.addBuildConfig { id in BuildConfig(id: id, name: "Release", settings: settings) }
func addEmptyBuildConfig(
to targetKeyPath: WritableKeyPath<ProjectModel.Project, ProjectModel.AggregateTarget>,
name: String
) {
let emptySettings = BuildSettings()
aggregateProject[keyPath: targetKeyPath].common.addBuildConfig { id in
BuildConfig(id: id, name: name, settings: emptySettings)
}
}
let allIncludingTestsTargetKeyPath = try aggregateProject.addAggregateTarget { _ in
ProjectModel.AggregateTarget(
id: "ALL-INCLUDING-TESTS",
name: PIFBuilder.allIncludingTestsTargetName
)
}
addEmptyBuildConfig(to: allIncludingTestsTargetKeyPath, name: "Debug")
addEmptyBuildConfig(to: allIncludingTestsTargetKeyPath, name: "Release")
let allExcludingTestsTargetKeyPath = try aggregateProject.addAggregateTarget { _ in
ProjectModel.AggregateTarget(
id: "ALL-EXCLUDING-TESTS",
name: PIFBuilder.allExcludingTestsTargetName
)
}
addEmptyBuildConfig(to: allExcludingTestsTargetKeyPath, name: "Debug")
addEmptyBuildConfig(to: allExcludingTestsTargetKeyPath, name: "Release")
for (package, packageProject) in packagesAndProjects where package.manifest.packageKind.isRoot {
for target in packageProject.targets {
switch target {
case .target(let target):
guard !target.id.hasSuffix(.dynamic) else {
// Otherwise we hit a bunch of "Unknown multiple commands produce: ..." errors,
// as the build artifacts from "PACKAGE-TARGET:Foo"
// conflicts with those from "PACKAGE-TARGET:Foo-dynamic".
continue
}
if let resolvedModule = modulesGraph.module(for: target.name) {
guard modulesGraph.isInRootPackages(resolvedModule, satisfying: buildParameters.buildEnvironment) else {
// Disconnected target, possibly due to platform when condition that isn't satisfied
continue
}
}
aggregateProject[keyPath: allIncludingTestsTargetKeyPath].common.addDependency(
on: target.id,
platformFilters: [],
linkProduct: false
)
if ![.unitTest, .swiftpmTestRunner].contains(target.productType) {
aggregateProject[keyPath: allExcludingTestsTargetKeyPath].common.addDependency(
on: target.id,
platformFilters: [],
linkProduct: false
)
}
case .aggregate:
break
}
}
}
do {
let allIncludingTests = aggregateProject[keyPath: allIncludingTestsTargetKeyPath]
let allExcludingTests = aggregateProject[keyPath: allExcludingTestsTargetKeyPath]
observabilityScope.logPIF(
.debug,
indent: 1,
"Created target '\(allIncludingTests.id)' with name '\(allIncludingTests.name)' " +
"and \(allIncludingTests.common.dependencies.count) (unlinked) dependencies"
)
observabilityScope.logPIF(
.debug,
indent: 1,
"Created target '\(allExcludingTests.id)' with name '\(allExcludingTests.name)' " +
"and \(allExcludingTests.common.dependencies.count) (unlinked) dependencies"
)
}
return aggregateProject
}
public enum PIFGenerationError: Error {
case rootPackageNotFound
case unsupportedSwiftLanguageVersions(
targetName: String,
versions: [SwiftLanguageVersion],
supportedVersions: [SwiftLanguageVersion]
)
/// Early build termination when using `--print-pif-manifest-graph`.
case printedPIFManifestGraphviz
}
extension PIFGenerationError: CustomStringConvertible {
public var description: String {
switch self {
case .rootPackageNotFound:
"No root package was found"
case .unsupportedSwiftLanguageVersions(
targetName: let target,
versions: let given,
supportedVersions: let supported
):
"None of the Swift language versions used in target '\(target)' settings are supported." +
" (given: \(given), supported: \(supported))"
case .printedPIFManifestGraphviz:
"Printed PIF manifest as graphviz"
}
}
}
// MARK: - Helpers
extension PIFBuilderParameters {
init(
_ buildParameters: BuildParameters,
supportedSwiftVersions: [SwiftLanguageVersion],
pluginScriptRunner: PluginScriptRunner,
disableSandbox: Bool,
pluginWorkingDirectory: AbsolutePath,
additionalFileRules: [FileRuleDescription],
addLocalRpaths: Bool,
materializeStaticArchiveProductsForRootPackages: Bool,
createDynamicVariantsForLibraryProducts: Bool
) {
self.init(
isPackageAccessModifierSupported: buildParameters.driverParameters.isPackageAccessModifierSupported,
enableTestability: buildParameters.enableTestability,
shouldCreateDylibForDynamicProducts: buildParameters.shouldCreateDylibForDynamicProducts,
materializeStaticArchiveProductsForRootPackages: materializeStaticArchiveProductsForRootPackages,
createDynamicVariantsForLibraryProducts: createDynamicVariantsForLibraryProducts,
toolchainLibDir: (try? buildParameters.toolchain.toolchainLibDir) ?? .root,
pkgConfigDirectories: buildParameters.pkgConfigDirectories,
supportedSwiftVersions: supportedSwiftVersions,
pluginScriptRunner: pluginScriptRunner,
disableSandbox: disableSandbox,
pluginWorkingDirectory: pluginWorkingDirectory,
additionalFileRules: additionalFileRules,
addLocalRPaths: addLocalRpaths,
)
}
}