-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathBuildPlan.swift
1317 lines (1165 loc) · 54 KB
/
BuildPlan.swift
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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2015-2023 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
//
//===----------------------------------------------------------------------===//
import _Concurrency
import Basics
import Foundation
import LLBuildManifest
import OrderedCollections
import PackageGraph
import PackageLoading
import PackageModel
import SPMBuildCore
#if USE_IMPL_ONLY_IMPORTS
internal import SwiftDriver
#else
import SwiftDriver
#endif
import enum TSCUtility.Diagnostics
import var TSCUtility.verbosity
extension String {
var asSwiftStringLiteralConstant: String {
unicodeScalars.reduce("") { $0 + $1.escaped(asASCII: false) }
}
}
extension [String] {
/// Converts a set of C compiler flags into an equivalent set to be
/// indirected through the Swift compiler instead.
func asSwiftcCCompilerFlags() -> Self {
self.flatMap { ["-Xcc", $0] }
}
/// Converts a set of C++ compiler flags into an equivalent set to be
/// indirected through the Swift compiler instead.
func asSwiftcCXXCompilerFlags() -> Self {
_ = self.flatMap { ["-Xcxx", $0] }
// TODO: Pass -Xcxx flags to swiftc (#6491)
// Remove fatal error when downstream support arrives.
fatalError("swiftc does support -Xcxx flags yet.")
}
/// Converts a set of linker flags into an equivalent set to be indirected
/// through the Swift compiler instead.
///
/// Some arguments can be passed directly to the Swift compiler. We omit
/// prefixing these arguments (in both the "-option value" and
/// "-option[=]value" forms) with "-Xlinker". All other arguments are
/// prefixed with "-Xlinker".
func asSwiftcLinkerFlags() -> Self {
// Arguments that can be passed directly to the Swift compiler and
// doesn't require -Xlinker prefix.
//
// We do this to avoid sending flags like linker search path at the end
// of the search list.
let directSwiftLinkerArgs = ["-L"]
var flags: [String] = []
var it = self.makeIterator()
while let flag = it.next() {
if directSwiftLinkerArgs.contains(flag) {
// `<option> <value>` variant.
flags.append(flag)
guard let nextFlag = it.next() else {
// We expected a flag but don't have one.
continue
}
flags.append(nextFlag)
} else if directSwiftLinkerArgs.contains(where: { flag.hasPrefix($0) }) {
// `<option>[=]<value>` variant.
flags.append(flag)
} else {
flags += ["-Xlinker", flag]
}
}
return flags
}
}
extension BuildParameters {
/// Returns the directory to be used for module cache.
public var moduleCache: AbsolutePath {
get throws {
// FIXME: We use this hack to let swiftpm's functional test use shared
// cache so it doesn't become painfully slow.
if let path = Environment.current["SWIFTPM_TESTS_MODULECACHE"] {
return try AbsolutePath(validating: path)
}
return buildPath.appending("ModuleCache")
}
}
/// Returns the compiler arguments for the index store, if enabled.
func indexStoreArguments(for target: ResolvedModule) -> [String] {
let addIndexStoreArguments: Bool
switch indexStoreMode {
case .on:
addIndexStoreArguments = true
case .off:
addIndexStoreArguments = false
case .auto:
if configuration == .debug {
addIndexStoreArguments = true
} else if target.type == .test {
// Test discovery requires an index store for the test target to discover the tests
addIndexStoreArguments = true
} else {
addIndexStoreArguments = false
}
}
if addIndexStoreArguments {
return ["-index-store-path", indexStore.pathString]
}
return []
}
/// Computes the target triple arguments for a given resolved target.
public func tripleArgs(for target: ResolvedModule) throws -> [String] {
// confusingly enough this is the triple argument, not the target argument
var args = ["-target"]
// Compute the triple string for Darwin platform using the platform version.
if self.triple.isDarwin() {
let platform = self.buildEnvironment.platform
let supportedPlatform = target.getSupportedPlatform(for: platform, usingXCTest: target.type == .test)
args += [self.triple.tripleString(forPlatformVersion: supportedPlatform.version.versionString)]
} else {
args += [self.triple.tripleString]
}
return args
}
/// Computes the linker flags to use in order to rename a module-named main function to 'main' for the target
/// platform, or nil if the linker doesn't support it for the platform.
func linkerFlagsForRenamingMainFunction(of target: ResolvedModule) -> [String]? {
let args: [String]
switch self.triple.objectFormat {
case .macho:
args = ["-alias", "_\(target.c99name)_main", "_main"]
case .elf:
args = ["--defsym", "main=\(target.c99name)_main"]
default:
return nil
}
return args.asSwiftcLinkerFlags()
}
/// Returns the scoped view of build settings for a given target.
func createScope(for target: ResolvedModule) -> BuildSettings.Scope {
BuildSettings.Scope(target.underlying.buildSettings, environment: buildEnvironment)
}
}
/// A build plan for a package graph.
public class BuildPlan: SPMBuildCore.BuildPlan {
/// Return value of `inputs()`
package enum Input {
/// Any file in this directory affects the build plan
case directoryStructure(AbsolutePath)
/// The file at the given path affects the build plan
case file(AbsolutePath)
}
public enum Error: Swift.Error, CustomStringConvertible, Equatable {
/// There is no buildable target in the graph.
case noBuildableTarget
public var description: String {
switch self {
case .noBuildableTarget:
return """
The package does not contain a buildable target.
Add at least one `.target` or `.executableTarget` to your `Package.swift`.
"""
}
}
}
/// Build parameters used for products.
public let destinationBuildParameters: BuildParameters
/// Build parameters used for tools.
public let toolsBuildParameters: BuildParameters
/// The package graph.
public let graph: ModulesGraph
/// The target build description map.
public let targetMap: IdentifiableSet<ModuleBuildDescription>
/// The product build description map.
public let productMap: IdentifiableSet<ProductBuildDescription>
/// The plugin descriptions. Plugins are represented in the package graph
/// as targets, but they are not directly included in the build graph.
public let pluginDescriptions: [PluginBuildDescription]
/// The build targets.
public var targets: AnySequence<ModuleBuildDescription> {
AnySequence(self.targetMap.values)
}
/// The products in this plan.
public var buildProducts: AnySequence<SPMBuildCore.ProductBuildDescription> {
AnySequence(self.productMap.values.map { $0 as SPMBuildCore.ProductBuildDescription })
}
public var buildModules: AnySequence<SPMBuildCore.ModuleBuildDescription> {
AnySequence(self.targetMap.values.map { $0 as SPMBuildCore.ModuleBuildDescription })
}
/// The results of invoking any build tool plugins used by targets in this build.
public let buildToolPluginInvocationResults: [ResolvedModule.ID: [BuildToolPluginInvocationResult]]
/// The results of running any prebuild commands for the targets in this build. This includes any derived
/// source files as well as directories to which any changes should cause us to reevaluate the build plan.
public let prebuildCommandResults: [ResolvedModule.ID: [CommandPluginResult]]
@_spi(SwiftPMInternal)
public private(set) var derivedTestTargetsMap: [ResolvedProduct.ID: [ResolvedModule]] = [:]
/// Cache for pkgConfig flags.
private var pkgConfigCache = [SystemLibraryModule: (cFlags: [String], libs: [String])]()
/// Cache for library information.
private var externalLibrariesCache = [BinaryModule: [LibraryInfo]]()
/// Cache for tools information.
var externalExecutablesCache = [BinaryModule: [ExecutableInfo]]()
/// Whether to disable sandboxing (e.g. for macros).
private let shouldDisableSandbox: Bool
/// The filesystem to operate on.
let fileSystem: any FileSystem
/// ObservabilityScope with which to emit diagnostics
let observabilityScope: ObservabilityScope
@available(*, deprecated, renamed: "init(destinationBuildParameters:toolsBuildParameters:graph:fileSystem:observabilityScope:)")
public convenience init(
productsBuildParameters: BuildParameters,
toolsBuildParameters: BuildParameters,
graph: ModulesGraph,
fileSystem: any FileSystem,
observabilityScope: ObservabilityScope
) async throws {
try await self.init(
destinationBuildParameters: productsBuildParameters,
toolsBuildParameters: toolsBuildParameters,
graph: graph,
pluginConfiguration: nil,
fileSystem: fileSystem,
observabilityScope: observabilityScope
)
}
/// Create a build plan with a package graph and explicitly distinct build parameters for destination platform and
/// tools platform.
public init(
destinationBuildParameters: BuildParameters,
toolsBuildParameters: BuildParameters,
graph: ModulesGraph,
pluginConfiguration: PluginConfiguration? = nil,
pluginTools: [ResolvedModule.ID: [String: PluginTool]] = [:],
additionalFileRules: [FileRuleDescription] = [],
pkgConfigDirectories: [AbsolutePath] = [],
disableSandbox: Bool = false,
fileSystem: any FileSystem,
observabilityScope: ObservabilityScope
) async throws {
self.destinationBuildParameters = destinationBuildParameters
self.toolsBuildParameters = toolsBuildParameters
self.graph = graph
self.shouldDisableSandbox = disableSandbox
self.fileSystem = fileSystem
var buildToolPluginInvocationResults: [ResolvedModule.ID: [BuildToolPluginInvocationResult]] = [:]
var prebuildCommandResults: [ResolvedModule.ID: [CommandPluginResult]] = [:]
// Create product description for each product we have in the package graph that is eligible.
var productMap = IdentifiableSet<ProductBuildDescription>()
// Create build target description for each target which we need to plan.
// Plugin targets are noted, since they need to be compiled, but they do
// not get directly incorporated into the build description that will be
// given to LLBuild.
var targetMap = IdentifiableSet<ModuleBuildDescription>()
var pluginDescriptions = [PluginBuildDescription]()
var shouldGenerateTestObservation = true
let planningObservabilityScope = observabilityScope.makeChildScope(description: "Planning")
try await Self.computeDestinations(
graph: graph,
onProduct: { product, destination in
if !product.shouldCreateProductDescription {
return
}
guard let package = graph.package(for: product) else {
throw InternalError("Package not found for product: \(product.name)")
}
try productMap.insert(ProductBuildDescription(
package: package,
product: product,
toolsVersion: package.manifest.toolsVersion,
buildParameters: destination == .host ? toolsBuildParameters : destinationBuildParameters,
fileSystem: fileSystem,
observabilityScope: planningObservabilityScope
))
},
onModule: { module, destination in
guard let package = graph.package(for: module) else {
throw InternalError("Package not found for module: \(module.name)")
}
let buildParameters = destination == .host ? toolsBuildParameters : destinationBuildParameters
// Validate the product dependencies of this target.
for dependency in module.dependencies {
guard dependency.satisfies(buildParameters.buildEnvironment) else {
continue
}
switch dependency {
case .module: break
case .product(let product, _):
if buildParameters.triple.isDarwin() {
try BuildPlan.validateDeploymentVersionOfProductDependency(
product: product,
forTarget: module,
buildEnvironment: buildParameters.buildEnvironment,
observabilityScope: planningObservabilityScope
.makeChildScope(description: "Validate Deployment of Dependency")
)
}
}
}
if let pluginConfiguration, !buildParameters.shouldSkipBuilding {
let pluginInvocationResults = try await Self.invokeBuildToolPlugins(
for: module,
destination: destination,
configuration: pluginConfiguration,
buildParameters: toolsBuildParameters,
modulesGraph: graph,
tools: pluginTools,
additionalFileRules: additionalFileRules,
pkgConfigDirectories: pkgConfigDirectories,
fileSystem: fileSystem,
observabilityScope: planningObservabilityScope,
surfaceDiagnostics: true
)
if pluginInvocationResults.contains(where: { !$0.succeeded }) {
throw StringError("build planning stopped due to build-tool plugin failures")
}
buildToolPluginInvocationResults[module.id] = pluginInvocationResults
prebuildCommandResults[module.id] = try Self.runCommandPlugins(
using: pluginConfiguration,
for: pluginInvocationResults,
fileSystem: fileSystem,
observabilityScope: planningObservabilityScope
)
}
switch module.underlying {
case is SwiftModule:
var generateTestObservation = false
if module.type == .test && shouldGenerateTestObservation {
generateTestObservation = true
shouldGenerateTestObservation = false // Only generate the code once.
}
try targetMap.insert(.swift(
SwiftModuleBuildDescription(
package: package,
target: module,
toolsVersion: package.manifest.toolsVersion,
additionalFileRules: additionalFileRules,
buildParameters: buildParameters,
macroBuildParameters: toolsBuildParameters,
buildToolPluginInvocationResults: buildToolPluginInvocationResults[module.id] ?? [],
prebuildCommandResults: prebuildCommandResults[module.id] ?? [],
shouldGenerateTestObservation: generateTestObservation,
shouldDisableSandbox: disableSandbox,
fileSystem: fileSystem,
observabilityScope: planningObservabilityScope
)
))
case is ClangModule:
try targetMap.insert(.clang(
ClangModuleBuildDescription(
package: package,
target: module,
toolsVersion: package.manifest.toolsVersion,
additionalFileRules: additionalFileRules,
buildParameters: buildParameters,
buildToolPluginInvocationResults: buildToolPluginInvocationResults[module.id] ?? [],
prebuildCommandResults: prebuildCommandResults[module.id] ?? [],
fileSystem: fileSystem,
observabilityScope: planningObservabilityScope
)
))
case is PluginModule:
try module.dependencies.compactMap {
switch $0 {
case .module(let moduleDependency, _):
if moduleDependency.type == .executable {
return graph.product(for: moduleDependency.name)
}
return nil
default:
return nil
}
}.forEach {
try productMap.insert(ProductBuildDescription(
package: package,
product: $0,
toolsVersion: package.manifest.toolsVersion,
buildParameters: toolsBuildParameters,
fileSystem: fileSystem,
observabilityScope: planningObservabilityScope
))
}
try pluginDescriptions.append(PluginBuildDescription(
module: module,
products: package.products.filter { $0.modules.contains(id: module.id) },
package: package,
toolsVersion: package.manifest.toolsVersion,
fileSystem: fileSystem
))
case is SystemLibraryModule, is BinaryModule:
break
default:
throw InternalError("unhandled \(module.underlying)")
}
}
)
/// Ensure we have at least one buildable target.
guard !targetMap.isEmpty else {
throw Error.noBuildableTarget
}
// Abort now if we have any diagnostics at this point.
guard !planningObservabilityScope.errorsReported else {
throw Diagnostics.fatalError
}
self.observabilityScope = observabilityScope.makeChildScope(description: "Build Plan")
// Plan the derived test targets, if necessary.
let derivedTestTargets = try Self.makeDerivedTestTargets(
testProducts: productMap.filter {
$0.product.type == .test
},
destinationBuildParameters: destinationBuildParameters,
toolsBuildParameters: toolsBuildParameters,
shouldDisableSandbox: self.shouldDisableSandbox,
self.fileSystem,
self.observabilityScope
)
for item in derivedTestTargets {
var derivedTestTargets = [item.entryPointTargetBuildDescription.target]
targetMap.insert(.swift(
item.entryPointTargetBuildDescription
))
if let discoveryTargetBuildDescription = item.discoveryTargetBuildDescription {
targetMap.insert(.swift(discoveryTargetBuildDescription))
derivedTestTargets.append(discoveryTargetBuildDescription.target)
}
self.derivedTestTargetsMap[item.product.id] = derivedTestTargets
}
self.buildToolPluginInvocationResults = buildToolPluginInvocationResults
self.prebuildCommandResults = prebuildCommandResults
self.productMap = productMap
self.targetMap = targetMap
self.pluginDescriptions = pluginDescriptions
// Finally plan these targets.
try self.plan()
}
static func validateDeploymentVersionOfProductDependency(
product: ResolvedProduct,
forTarget target: ResolvedModule,
buildEnvironment: BuildEnvironment,
observabilityScope: ObservabilityScope
) throws {
// Supported platforms are defined at the package (e.g., build environment) level.
// This will need to become a bit complicated once we have target-level or product-level platform support.
let productPlatform = product.getSupportedPlatform(
for: buildEnvironment.platform,
usingXCTest: product.isLinkingXCTest
)
let targetPlatform = target.getSupportedPlatform(
for: buildEnvironment.platform,
usingXCTest: target.type == .test
)
// Check if the version requirement is satisfied.
//
// If the product's platform version is greater than ours, then it is incompatible.
if productPlatform.version > targetPlatform.version {
observabilityScope.emit(.productRequiresHigherPlatformVersion(
target: target,
targetPlatform: targetPlatform,
product: product.name,
productPlatform: productPlatform
))
}
}
/// Plan the targets and products.
private func plan() throws {
// Plan targets.
for buildTarget in self.targets {
switch buildTarget {
case .swift(let target):
try self.plan(swiftTarget: target)
case .clang(let target):
try self.plan(clangTarget: target)
}
}
// Plan products.
for buildProduct in self.buildProducts {
try self.plan(buildProduct: buildProduct as! ProductBuildDescription)
}
// FIXME: We need to find out if any product has a target on which it depends
// both static and dynamically and then issue a suitable diagnostic or auto
// handle that situation.
// Ensure modules in Windows DLLs export their symbols
for product in productMap.values where product.product.type == .library(.dynamic) && product.buildParameters.triple.isWindows() {
for target in product.product.modules {
let targetId: ModuleBuildDescription.ID = .init(moduleID: target.id, destination: product.buildParameters.destination)
if case let .swift(buildDescription) = targetMap[targetId] {
buildDescription.isWindowsStatic = false
}
}
}
}
public func createAPIToolCommonArgs(includeLibrarySearchPaths: Bool) throws -> [String] {
// API tool runs on products, hence using `self.productsBuildParameters`, not `self.toolsBuildParameters`
let buildPath = self.destinationBuildParameters.buildPath.pathString
var arguments = ["-I", buildPath]
// swift-symbolgraph-extract does not support parsing `-use-ld=lld` and
// will silently error failing the operation. Filter out this flag
// similar to how we filter out the library search path unless
// explicitly requested.
var extraSwiftCFlags = self.destinationBuildParameters.toolchain.extraFlags.swiftCompilerFlags
.filter { !$0.starts(with: "-use-ld=") }
if !includeLibrarySearchPaths {
for index in extraSwiftCFlags.indices.dropLast().reversed() {
if extraSwiftCFlags[index] == "-L" {
// Remove the flag
extraSwiftCFlags.remove(at: index)
// Remove the argument
extraSwiftCFlags.remove(at: index)
}
}
}
arguments += extraSwiftCFlags
// Add search paths to the directories containing module maps and Swift modules.
for target in self.targets {
switch target {
case .swift(let targetDescription):
arguments += ["-I", targetDescription.moduleOutputPath.parentDirectory.pathString]
case .clang(let targetDescription):
if let includeDir = targetDescription.moduleMap?.parentDirectory {
arguments += ["-I", includeDir.pathString]
}
arguments += ["-I", targetDescription.clangTarget.includeDir.pathString]
}
}
// Add search paths from the system library targets.
for target in self.graph.reachableModules {
if let systemLib = target.underlying as? SystemLibraryModule {
try arguments.append(contentsOf: self.pkgConfig(for: systemLib).cFlags)
// Add the path to the module map.
arguments += ["-I", systemLib.moduleMapPath.parentDirectory.pathString]
}
}
return arguments
}
/// Creates arguments required to launch the Swift REPL that will allow
/// importing the modules in the package graph.
public func createREPLArguments() throws -> [String] {
let buildPath = self.toolsBuildParameters.buildPath.pathString
var arguments = ["repl", "-I" + buildPath, "-L" + buildPath]
// Link the special REPL product that contains all of the library targets.
let replProductName = self.graph.rootPackages[self.graph.rootPackages.startIndex].identity.description +
Product.replProductSuffix
arguments.append("-l" + replProductName)
// The graph should have the REPL product.
assert(self.graph.product(for: replProductName) != nil)
// Add the search path to the directory containing the modulemap file.
for target in self.targets {
switch target {
case .swift: break
case .clang(let targetDescription):
if let includeDir = targetDescription.moduleMap?.parentDirectory {
arguments += ["-I\(includeDir.pathString)"]
}
}
}
// Add search paths from the system library targets.
for target in self.graph.reachableModules {
if let systemLib = target.underlying as? SystemLibraryModule {
arguments += try self.pkgConfig(for: systemLib).cFlags
}
}
return arguments
}
/// Get pkgConfig arguments for a system library target.
func pkgConfig(for target: SystemLibraryModule) throws -> (cFlags: [String], libs: [String]) {
// If we already have these flags, we're done.
if let flags = pkgConfigCache[target] {
return flags
} else {
self.pkgConfigCache[target] = ([], [])
}
let results = try pkgConfigArgs(
for: target,
pkgConfigDirectories: self.destinationBuildParameters.pkgConfigDirectories,
sdkRootPath: self.destinationBuildParameters.toolchain.sdkRootPath,
fileSystem: self.fileSystem,
observabilityScope: self.observabilityScope
)
var ret: [(cFlags: [String], libs: [String])] = []
for result in results {
ret.append((result.cFlags, result.libs))
}
// Build cache
var cflagsCache: OrderedCollections.OrderedSet<String> = []
var libsCache: [String] = []
for tuple in ret {
for cFlag in tuple.cFlags {
cflagsCache.append(cFlag)
}
libsCache.append(contentsOf: tuple.libs)
}
let result = ([String](cflagsCache), libsCache)
self.pkgConfigCache[target] = result
return result
}
/// Extracts the library information from an XCFramework.
func parseXCFramework(for binaryTarget: BinaryModule, triple: Basics.Triple) throws -> [LibraryInfo] {
try self.externalLibrariesCache.memoize(key: binaryTarget) {
try binaryTarget.parseXCFrameworks(for: triple, fileSystem: self.fileSystem)
}
}
/// Returns the files and directories that affect the build process of this build plan.
package var inputs: [Input] {
var inputs: [Input] = []
for package in self.graph.rootPackages {
inputs += package.modules
.map(\.sources.root)
.sorted()
.map { .directoryStructure($0) }
// Add the output paths of any prebuilds that were run, so that we redo the plan if they change.
var derivedSourceDirPaths: [AbsolutePath] = []
for result in self.prebuildCommandResults.values.flatMap({ $0 }) {
derivedSourceDirPaths.append(contentsOf: result.outputDirectories)
}
inputs.append(contentsOf: derivedSourceDirPaths.sorted().map { .directoryStructure($0) })
// FIXME: Need to handle version-specific manifests.
inputs.append(.file(package.manifest.path))
// FIXME: This won't be the location of Package.resolved for multiroot packages.
inputs.append(.file(package.path.appending("Package.resolved")))
// FIXME: Add config file as an input
}
return inputs
}
public func description(
for product: ResolvedProduct,
context: BuildParameters.Destination
) -> ProductBuildDescription? {
let destination: BuildParameters.Destination = switch product.type {
case .macro, .plugin:
.host
default:
context
}
return self.productMap[.init(productID: product.id, destination: destination)]
}
public func description(
for module: ResolvedModule,
context: BuildParameters.Destination
) -> ModuleBuildDescription? {
let destination: BuildParameters.Destination = switch module.type {
case .macro, .plugin:
.host
default:
context
}
return self.targetMap[.init(moduleID: module.id, destination: destination)]
}
}
extension BuildPlan {
/// Applies plugins to the given module as needed. Each plugin is passed an input context that provides
/// information about the module to which it is being applied (along with some information about that
/// module's dependency closure). The plugin is expected to generate an output in the form of commands
/// that will later be run before or during the build, and can also emit debug output and diagnostics.
///
/// Each result returned by this function includes an ordered list of commands to run before the build
/// of the module, and another list of the commands to incorporate into the build graph so they run
/// at the appropriate times during the build.
///
/// Any warnings and errors related to running the plugin will be emitted to `diagnostics` when
/// `surfaceDiagnostics` parameter is set to `true`.
///
/// Note that warnings emitted by the the plugin itself will be returned in the `BuildToolPluginInvocationResult`
/// structures for later showing to the user, and not added directly to the diagnostics engine.
static func invokeBuildToolPlugins(
for module: ResolvedModule,
destination: BuildParameters.Destination,
configuration: PluginConfiguration,
buildParameters: BuildParameters,
modulesGraph: ModulesGraph,
tools: [ResolvedModule.ID: [String: PluginTool]],
additionalFileRules: [FileRuleDescription],
pkgConfigDirectories: [AbsolutePath],
fileSystem: any FileSystem,
observabilityScope: ObservabilityScope,
surfaceDiagnostics: Bool = false
) async throws -> [BuildToolPluginInvocationResult] {
let outputDir = configuration.workDirectory.appending("outputs")
/// Determine the package that contains the target.
guard let package = modulesGraph.package(for: module) else {
throw InternalError("could not determine package for module \(self)")
}
// Apply each build tool plugin used by the target in order,
// creating a list of results (one for each plugin usage).
var buildToolPluginResults: [BuildToolPluginInvocationResult] = []
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 = tools[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,
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.
(pluginDerivedSources, pluginDerivedResources) = ModulesGraph.computePluginGeneratedFiles(
target: module,
toolsVersion: package.manifest.toolsVersion,
additionalFileRules: additionalFileRules,
buildParameters: buildParameters,
buildToolPluginInvocationResults: buildToolPluginResults,
prebuildCommandResults: [],
observabilityScope: observability.topScope
)
} 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,
scriptRunner: configuration.scriptRunner,
workingDirectory: package.path,
outputDirectory: pluginOutputDir,
toolSearchDirectories: [buildParameters.toolchain.swiftCompilerPath.parentDirectory],
accessibleTools: accessibleTools,
writableDirectories: writableDirectories,
readOnlyDirectories: readOnlyDirectories,
allowNetworkConnections: [],
pkgConfigDirectories: pkgConfigDirectories,
sdkRootPath: buildParameters.toolchain.sdkRootPath,
fileSystem: fileSystem,
modulesGraph: modulesGraph,
observabilityScope: observabilityScope
)
if surfaceDiagnostics {
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)
}
}
// Add a BuildToolPluginInvocationResult to the mapping.
buildToolPluginResults.append(result)
}
return buildToolPluginResults
}
/// Runs any command plugins associated with the given list of plugin invocation results,
/// in order, and returns the results of running those prebuild commands.
fileprivate static func runCommandPlugins(
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: [AbsolutePath] = []
var prebuildOutputDirs: [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)
}
}
}
extension BuildPlan {
fileprivate typealias Destination = BuildParameters.Destination
enum TraversalNode: Hashable {
case package(ResolvedPackage)
case product(ResolvedProduct, BuildParameters.Destination)
case module(ResolvedModule, BuildParameters.Destination)
var destination: BuildParameters.Destination {
switch self {
case .package:
.target
case .product(_, let destination):
destination
case .module(_, let destination):
destination
}
}
init(
product: ResolvedProduct,
context destination: BuildParameters.Destination
) {
switch product.type {
case .macro, .plugin:
self = .product(product, .host)
case .test:
self = .product(product, product.hasDirectMacroDependencies ? .host : destination)
default:
self = .product(product, destination)
}
}
init(
module: ResolvedModule,
context destination: BuildParameters.Destination
) {
switch module.type {
case .macro, .plugin:
// Macros and plugins are ways built for host
self = .module(module, .host)
case .test:
self = .module(module, module.hasDirectMacroDependencies ? .host : destination)
default:
// By default assume the destination of the context.
// This means that i.e. test products that reference macros
// would force all of their successors to be `host`
self = .module(module, destination)
}
}
}
/// Traverse the modules graph and find a destination for every product and module.
/// All non-macro/plugin products and modules have `target` destination with one
/// notable exception - test products/modules with direct macro dependency.
fileprivate static func computeDestinations(
graph: ModulesGraph,
onProduct: (ResolvedProduct, Destination) throws -> Void,