-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathPackageDescription.swift
588 lines (541 loc) · 24.7 KB
/
PackageDescription.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2018-2021 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
//
//===----------------------------------------------------------------------===//
#if canImport(ucrt) && canImport(WinSDK)
internal import ucrt
internal import struct WinSDK.HANDLE
#endif
internal import Foundation
/// The configuration of a Swift package.
///
/// Pass configuration options as parameters to your package's initializer
/// statement to provide the name of the package, its targets, products,
/// dependencies, and other configuration options.
///
/// By convention, you need to define the properties of a package in a single
/// nested initializer statement. Don't modify it after initialization. The
/// following package manifest shows the initialization of a simple package
/// object for the MyLibrary Swift package:
///
/// ```swift
/// // swift-tools-version:5.3
/// import PackageDescription
///
/// let package = Package(
/// name: "MyLibrary",
/// platforms: [
/// .macOS(.v10_15),
/// ],
/// products: [
/// .library(name: "MyLibrary", targets: ["MyLibrary"])
/// ],
/// dependencies: [
/// .package(url: "https://url/of/another/package/named/utility", from: "1.0.0")
/// ],
/// targets: [
/// .target(name: "MyLibrary", dependencies: ["Utility"]),
/// .testTarget(name: "MyLibraryTests", dependencies: ["MyLibrary"])
/// ]
/// )
/// ```
///
/// In Swift tools versions earlier than 5.4, the package manifest must begin with the string `// swift-tools-version:`
/// followed by a version number specifier. Version 5.4 and later has relaxed the whitespace requirements.
/// The following code listing shows a few examples of valid declarations of the Swift tools version:
///
/// ```swift
/// // swift-tools-version:3.0.2
/// // swift-tools-version:3.1
/// // swift-tools-version:4.0
/// // swift-tools-version:5.3
/// // swift-tools-version: 5.6
/// ```
///
/// The Swift tools version declares the version of the `PackageDescription`
/// library, the minimum version of the Swift tools and Swift language
/// compatibility version to process the manifest, and the required minimum
/// version of the Swift tools to use the Swift package. Each version of Swift
/// can introduce updates to the PackageDescription framework, but the previous
/// API version is available to packages which declare a prior tools version.
/// This behavior means you can take advantage of new releases of Swift, the Swift
/// tools, and the PackageDescription library, without having to update your
/// package's manifest or losing access to existing packages.
public final class Package {
/// The name of the Swift package.
///
/// If the name of the package is `nil`, Swift Package Manager deduces the name of the
/// package using its Git URL.
public var name: String
/// The list of minimum versions for platforms supported by the package.
@available(_PackageDescription, introduced: 5)
public var platforms: [SupportedPlatform]?
/// The default localization for resources.
@available(_PackageDescription, introduced: 5.3)
public var defaultLocalization: LanguageTag?
/// The name to use for C modules.
///
/// If present, the Swift Package Manager searches for a `<name>.pc` file to
/// get the required additional flags for a system target.
public var pkgConfig: String?
/// An array of providers for a system target.
public var providers: [SystemPackageProvider]?
/// The list of targets that are part of this package.
public var targets: [Target]
/// The list of products that this package vends and that clients can use.
public var products: [Product]
/// The set of traits of this package.
@available(_PackageDescription, introduced: 6.1)
public var traits: Set<Trait>
/// The list of package dependencies.
public var dependencies: [Dependency]
/// The list of Swift language modes with which this package is compatible.
public var swiftLanguageModes: [SwiftLanguageMode]?
/// Legacy property name, accesses value of `swiftLanguageModes`
@available(_PackageDescription, deprecated: 6, renamed: "swiftLanguageModes")
public var swiftLanguageVersions: [SwiftVersion]? {
get { swiftLanguageModes }
set { swiftLanguageModes = newValue }
}
/// The C language standard to use for all C targets in this package.
public var cLanguageStandard: CLanguageStandard?
/// The C++ language standard to use for all C++ targets in this package.
public var cxxLanguageStandard: CXXLanguageStandard?
/// Initializes a Swift package with configuration options you provide.
///
/// - Parameters:
/// - name: The name of the Swift package, or `nil`, if you want the Swift Package Manager to deduce the
/// name from the package's Git URL.
/// - pkgConfig: The name to use for C modules. If present, the Swift
/// Package Manager searches for a `<name>.pc` file to get the
/// additional flags required for a system target.
/// - providers: The package providers for a system package.
/// - products: The list of products that this package vends and that clients can use.
/// - dependencies: The list of package dependencies.
/// - targets: The list of targets that are part of this package.
/// - swiftLanguageVersions: The list of Swift versions that this package is compatible with.
/// - cLanguageStandard: The C language standard to use for all C targets in this package.
/// - cxxLanguageStandard: The C++ language standard to use for all C++ targets in this package.
@available(_PackageDescription, obsoleted: 4.2)
public init(
name: String,
pkgConfig: String? = nil,
providers: [SystemPackageProvider]? = nil,
products: [Product] = [],
dependencies: [Dependency] = [],
targets: [Target] = [],
swiftLanguageVersions: [Int]? = nil,
cLanguageStandard: CLanguageStandard? = nil,
cxxLanguageStandard: CXXLanguageStandard? = nil
) {
self.name = name
self.pkgConfig = pkgConfig
self.providers = providers
self.products = products
self.dependencies = dependencies
self.targets = targets
self.traits = []
self.swiftLanguageModes = swiftLanguageVersions.map{ $0.map{ .version("\($0)") } }
self.cLanguageStandard = cLanguageStandard
self.cxxLanguageStandard = cxxLanguageStandard
registerExitHandler()
}
/// Initializes a Swift package with configuration options you provide.
///
/// - Parameters:
/// - name: The name of the Swift package, or `nil`, if you want the Swift Package Manager to deduce the
/// name from the package's Git URL.
/// - pkgConfig: The name to use for C modules. If present, the Swift
/// Package Manager searches for a `<name>.pc` file to get the
/// additional flags required for a system target.
/// - products: The list of products that this package makes available for clients to use.
/// - dependencies: The list of package dependencies.
/// - targets: The list of targets that are part of this package.
/// - swiftLanguageVersions: The list of Swift versions that this package is compatible with.
/// - cLanguageStandard: The C language standard to use for all C targets in this package.
/// - cxxLanguageStandard: The C++ language standard to use for all C++ targets in this package.
@available(_PackageDescription, introduced: 4.2, obsoleted: 5)
public init(
name: String,
pkgConfig: String? = nil,
providers: [SystemPackageProvider]? = nil,
products: [Product] = [],
dependencies: [Dependency] = [],
targets: [Target] = [],
swiftLanguageVersions: [SwiftVersion]? = nil,
cLanguageStandard: CLanguageStandard? = nil,
cxxLanguageStandard: CXXLanguageStandard? = nil
) {
self.name = name
self.pkgConfig = pkgConfig
self.providers = providers
self.products = products
self.dependencies = dependencies
self.targets = targets
self.traits = []
self.swiftLanguageModes = swiftLanguageVersions
self.cLanguageStandard = cLanguageStandard
self.cxxLanguageStandard = cxxLanguageStandard
registerExitHandler()
}
/// Initializes a Swift package with configuration options you provide.
///
/// - Parameters:
/// - name: The name of the Swift package, or `nil`, if you want the Swift Package Manager to deduce the
/// name from the package's Git URL.
/// - platforms: The list of supported platforms that have a custom deployment target.
/// - pkgConfig: The name to use for C modules. If present, the Swift
/// Package Manager searches for a `<name>.pc` file to get the
/// additional flags required for a system target.
/// - products: The list of products that this package makes available for clients to use.
/// - dependencies: The list of package dependencies.
/// - targets: The list of targets that are part of this package.
/// - swiftLanguageVersions: The list of Swift versions that this package is compatible with.
/// - cLanguageStandard: The C language standard to use for all C targets in this package.
/// - cxxLanguageStandard: The C++ language standard to use for all C++ targets in this package.
@available(_PackageDescription, introduced: 5, obsoleted: 5.3)
public init(
name: String,
platforms: [SupportedPlatform]? = nil,
pkgConfig: String? = nil,
providers: [SystemPackageProvider]? = nil,
products: [Product] = [],
dependencies: [Dependency] = [],
targets: [Target] = [],
swiftLanguageVersions: [SwiftVersion]? = nil,
cLanguageStandard: CLanguageStandard? = nil,
cxxLanguageStandard: CXXLanguageStandard? = nil
) {
self.name = name
self.platforms = platforms
self.pkgConfig = pkgConfig
self.providers = providers
self.products = products
self.dependencies = dependencies
self.targets = targets
self.traits = []
self.swiftLanguageModes = swiftLanguageVersions
self.cLanguageStandard = cLanguageStandard
self.cxxLanguageStandard = cxxLanguageStandard
registerExitHandler()
}
/// Initializes a Swift package with configuration options you provide.
///
/// - Parameters:
/// - name: The name of the Swift package, or `nil` to use the package's Git URL to deduce the name.
/// - defaultLocalization: The default localization for resources.
/// - platforms: The list of supported platforms with a custom deployment target.
/// - pkgConfig: The name to use for C modules. If present, Swift Package Manager searches for a
/// `<name>.pc` file to get the additional flags required for a system target.
/// - providers: The package providers for a system target.
/// - products: The list of products that this package makes available for clients to use.
/// - dependencies: The list of package dependencies.
/// - targets: The list of targets that are part of this package.
/// - swiftLanguageVersions: The list of Swift versions with which this package is compatible.
/// - cLanguageStandard: The C language standard to use for all C targets in this package.
/// - cxxLanguageStandard: The C++ language standard to use for all C++ targets in this package.
@_disfavoredOverload
@available(_PackageDescription, introduced: 5.3)
@available(_PackageDescription, deprecated: 6, renamed:"init(name:defaultLocalization:platforms:pkgConfig:providers:products:dependencies:targets:swiftLanguageModes:cLanguageStandard:cxxLanguageStandard:)")
public init(
name: String,
defaultLocalization: LanguageTag? = nil,
platforms: [SupportedPlatform]? = nil,
pkgConfig: String? = nil,
providers: [SystemPackageProvider]? = nil,
products: [Product] = [],
dependencies: [Dependency] = [],
targets: [Target] = [],
swiftLanguageVersions: [SwiftVersion]? = nil,
cLanguageStandard: CLanguageStandard? = nil,
cxxLanguageStandard: CXXLanguageStandard? = nil
) {
self.name = name
self.defaultLocalization = defaultLocalization
self.platforms = platforms
self.pkgConfig = pkgConfig
self.providers = providers
self.products = products
self.dependencies = dependencies
self.targets = targets
self.traits = []
self.swiftLanguageModes = swiftLanguageVersions
self.cLanguageStandard = cLanguageStandard
self.cxxLanguageStandard = cxxLanguageStandard
registerExitHandler()
}
/// Initializes a Swift package with configuration options you provide.
///
/// - Parameters:
/// - name: The name of the Swift package, or `nil` to use the package's Git URL to deduce the name.
/// - defaultLocalization: The default localization for resources.
/// - platforms: The list of supported platforms with a custom deployment target.
/// - pkgConfig: The name to use for C modules. If present, Swift Package Manager searches for a
/// `<name>.pc` file to get the additional flags required for a system target.
/// - providers: The package providers for a system target.
/// - products: The list of products that this package makes available for clients to use.
/// - dependencies: The list of package dependencies.
/// - targets: The list of targets that are part of this package.
/// - swiftLanguageModes: The list of Swift language modes with which this package is compatible.
/// - cLanguageStandard: The C language standard to use for all C targets in this package.
/// - cxxLanguageStandard: The C++ language standard to use for all C++ targets in this package.
@available(_PackageDescription, introduced: 6)
public init(
name: String,
defaultLocalization: LanguageTag? = nil,
platforms: [SupportedPlatform]? = nil,
pkgConfig: String? = nil,
providers: [SystemPackageProvider]? = nil,
products: [Product] = [],
dependencies: [Dependency] = [],
targets: [Target] = [],
swiftLanguageModes: [SwiftLanguageMode]? = nil,
cLanguageStandard: CLanguageStandard? = nil,
cxxLanguageStandard: CXXLanguageStandard? = nil
) {
self.name = name
self.defaultLocalization = defaultLocalization
self.platforms = platforms
self.pkgConfig = pkgConfig
self.providers = providers
self.products = products
self.dependencies = dependencies
self.targets = targets
self.traits = []
self.swiftLanguageModes = swiftLanguageModes
self.cLanguageStandard = cLanguageStandard
self.cxxLanguageStandard = cxxLanguageStandard
registerExitHandler()
}
/// Initializes a Swift package with configuration options you provide.
///
/// - Parameters:
/// - name: The name of the Swift package, or `nil` to use the package's Git URL to deduce the name.
/// - defaultLocalization: The default localization for resources.
/// - platforms: The list of supported platforms with a custom deployment target.
/// - pkgConfig: The name to use for C modules. If present, Swift Package Manager searches for a
/// `<name>.pc` file to get the additional flags required for a system target.
/// - providers: The package providers for a system target.
/// - products: The list of products that this package makes available for clients to use.
/// - traits: The set of traits of this package.
/// - dependencies: The list of package dependencies.
/// - targets: The list of targets that are part of this package.
/// - swiftLanguageModes: The list of Swift language modes with which this package is compatible.
/// - cLanguageStandard: The C language standard to use for all C targets in this package.
/// - cxxLanguageStandard: The C++ language standard to use for all C++ targets in this package.
@available(_PackageDescription, introduced: 6.1)
public init(
name: String,
defaultLocalization: LanguageTag? = nil,
platforms: [SupportedPlatform]? = nil,
pkgConfig: String? = nil,
providers: [SystemPackageProvider]? = nil,
products: [Product] = [],
traits: Set<Trait> = [],
dependencies: [Dependency] = [],
targets: [Target] = [],
swiftLanguageModes: [SwiftLanguageMode]? = nil,
cLanguageStandard: CLanguageStandard? = nil,
cxxLanguageStandard: CXXLanguageStandard? = nil
) {
self.name = name
self.defaultLocalization = defaultLocalization
self.platforms = platforms
self.pkgConfig = pkgConfig
self.providers = providers
self.products = products
self.traits = traits
self.dependencies = dependencies
self.targets = targets
self.swiftLanguageModes = swiftLanguageModes
self.cLanguageStandard = cLanguageStandard
self.cxxLanguageStandard = cxxLanguageStandard
registerExitHandler()
}
private func registerExitHandler() {
// Add a custom exit handler to cause the package's JSON representation
// to be dumped at exit, if requested. Emitting it to a separate file
// descriptor from stdout keeps any of the manifest's stdout output from
// interfering with it.
//
// FIXME: This doesn't belong here, but for now is the mechanism we use
// to get the interpreter to dump the package when attempting to load a
// manifest.
//
// Warning: The `-fileno` flag is a contract between PackageDescription
// and libSwiftPM, and since different versions of the two can be used
// together, it isn't safe to rename or remove it.
//
// Note: `-fileno` is not viable on Windows. Instead, we pass the file
// handle through the `-handle` option.
#if os(Windows)
if let index = CommandLine.arguments.firstIndex(of: "-handle") {
if let handle = Int(CommandLine.arguments[index + 1], radix: 16) {
dumpPackageAtExit(self, to: handle)
}
}
#else
if let optIdx = CommandLine.arguments.firstIndex(of: "-fileno") {
if let jsonOutputFileDesc = Int32(CommandLine.arguments[optIdx + 1]) {
dumpPackageAtExit(self, to: jsonOutputFileDesc)
}
}
#endif
}
}
/// A wrapper around an IETF language tag.
///
/// To learn more about the IETF worldwide standard for language tags, see
/// [RFC5646](https://tools.ietf.org/html/rfc5646).
public struct LanguageTag: Hashable {
/// An IETF BCP 47 standard language tag.
let tag: String
/// Creates a language tag from its [IETF BCP 47](https://datatracker.ietf.org/doc/html/rfc5646) string representation.
///
/// - Parameter tag: The string representation of an IETF language tag.
private init(_ tag: String) {
self.tag = tag
}
}
extension LanguageTag: RawRepresentable {
public var rawValue: String { tag }
/// Creates a new instance with the specified raw value.
///
/// If there's no value of the type that corresponds with the specified raw
/// value, this initializer returns `nil`.
///
/// - Parameter rawValue: The raw value to use for the new instance.
public init?(rawValue: String) {
tag = rawValue
}
}
/// ExpressibleByStringLiteral implementation.
extension LanguageTag: ExpressibleByStringLiteral {
/// Creates an instance initialized to the given value.
///
/// - Parameter value: The value of the new instance.
public init(stringLiteral value: String) {
tag = value
}
/// Creates an instance initialized to the given value.
/// - Parameter value: The value of the new instance.
public init(extendedGraphemeClusterLiteral value: String) {
self.init(stringLiteral: value)
}
/// Creates an instance initialized to the given value.
///
/// - Parameter value: The value of the new instance.
public init(unicodeScalarLiteral value: String) {
self.init(stringLiteral: value)
}
}
extension LanguageTag: CustomStringConvertible {
/// A textual representation of the language tag.
public var description: String { tag }
}
/// The system package providers that this package uses.
public enum SystemPackageProvider {
/// Packages installable by the HomeBrew package manager.
case brewItem([String])
/// Packages installable by the apt-get package manager.
case aptItem([String])
/// Packages installable by the Yellowdog Updated, Modified (YUM) package manager.
@available(_PackageDescription, introduced: 5.3)
case yumItem([String])
/// Packages installable by the NuGet package manager.
@available(_PackageDescription, introduced: 999.0)
case nugetItem([String])
/// Creates a system package provider with a list of installable packages
/// for people who use the HomeBrew package manager on macOS.
///
/// - Parameter packages: The list of package names.
///
/// - Returns: A package provider.
public static func brew(_ packages: [String]) -> SystemPackageProvider {
return .brewItem(packages)
}
/// Creates a system package provider with a list of installable packages
/// for users of the apt-get package manager on Ubuntu Linux.
///
/// - Parameter packages: The list of package names.
///
/// - Returns: A package provider.
public static func apt(_ packages: [String]) -> SystemPackageProvider {
return .aptItem(packages)
}
/// Creates a system package provider with a list of installable packages
/// for users of the yum package manager on Red Hat Enterprise Linux or
/// CentOS.
///
/// - Parameter packages: The list of package names.
///
/// - Returns: A package provider.
@available(_PackageDescription, introduced: 5.3)
public static func yum(_ packages: [String]) -> SystemPackageProvider {
return .yumItem(packages)
}
/// Creates a system package provider with a list of installable packages
/// for users of the NuGet package manager on Linux or Windows.
///
/// - Parameter packages: The list of package names.
///
/// - Returns: A package provider.
@available(_PackageDescription, introduced: 999.0)
public static func nuget(_ packages: [String]) -> SystemPackageProvider {
return .nugetItem(packages)
}
}
// MARK: - Package Dumping
private func manifestToJSON(_ package: Package) -> String {
struct Output: Codable {
let package: Serialization.Package
let errors: [String]
let version: Int
}
let encoder = JSONEncoder()
encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes]
let data = try! encoder.encode(Output(package: .init(package), errors: errors, version: 2))
return String(decoding: data, as: UTF8.self)
}
var errors: [String] = []
#if os(Windows)
private var dumpInfo: (package: Package, handle: Int)?
private func dumpPackageAtExit(_ package: Package, to handle: Int) {
let dump: @convention(c) () -> Void = {
guard let dumpInfo else { return }
let hFile: HANDLE = HANDLE(bitPattern: dumpInfo.handle)!
// NOTE: `_open_osfhandle` transfers ownership of the `HANDLE` to the file
// descriptor. DO NOT invoke `CloseHandle` on `hFile`.
let fd: CInt = _open_osfhandle(Int(bitPattern: hFile), _O_APPEND)
// NOTE: `_fdopen` transfers ownership of the file descriptor to the
// `FILE *`. DO NOT invoke `_close` on the `fd`.
guard let fp = _fdopen(fd, "w") else {
_close(fd)
return
}
defer { fclose(fp) }
fputs(manifestToJSON(dumpInfo.package), fp)
}
dumpInfo = (package, handle)
atexit(dump)
}
#else
private var dumpInfo: (package: Package, fileDesc: Int32)?
private func dumpPackageAtExit(_ package: Package, to fileDesc: Int32) {
func dump() {
guard let dumpInfo else { return }
guard let fd = fdopen(dumpInfo.fileDesc, "w") else { return }
fputs(manifestToJSON(dumpInfo.package), fd)
fclose(fd)
}
dumpInfo = (package, fileDesc)
atexit(dump)
}
#endif