-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathPackage.swift
295 lines (256 loc) · 9.91 KB
/
Package.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
// swift-tools-version: 6.0
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//
import PackageDescription
import CompilerPluginSupport
/// Information about the current state of the package's git repository.
let git = Context.gitInformation
/// Whether or not this package is being built for development rather than
/// distribution as a package dependency.
let buildingForDevelopment = (git?.currentTag == nil)
let package = Package(
name: "swift-testing",
platforms: [
.macOS(.v10_15),
.iOS(.v13),
.watchOS(.v6),
.tvOS(.v13),
.macCatalyst(.v13),
.visionOS(.v1),
],
products: {
var result = [Product]()
#if os(Windows)
result.append(
.library(
name: "Testing",
type: .dynamic, // needed so Windows exports ABI entry point symbols
targets: ["Testing"]
)
)
#else
result.append(
.library(
name: "Testing",
targets: ["Testing"]
)
)
#endif
result.append(
.library(
name: "_TestDiscovery",
type: .static,
targets: ["_TestDiscovery"]
)
)
return result
}(),
dependencies: [
.package(url: "https://github.com/swiftlang/swift-syntax.git", from: "601.0.0-latest"),
],
targets: [
.target(
name: "Testing",
dependencies: [
"_TestDiscovery",
"_TestingInternals",
"TestingMacros",
],
exclude: ["CMakeLists.txt", "Testing.swiftcrossimport"],
cxxSettings: .packageSettings,
swiftSettings: .packageSettings + .enableLibraryEvolution(),
linkerSettings: [
.linkedLibrary("execinfo", .when(platforms: [.custom("freebsd"), .openbsd]))
]
),
.testTarget(
name: "TestingTests",
dependencies: [
"Testing",
"_Testing_CoreGraphics",
"_Testing_Foundation",
],
swiftSettings: .packageSettings
),
.macro(
name: "TestingMacros",
dependencies: [
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
],
exclude: ["CMakeLists.txt"],
swiftSettings: .packageSettings + {
var result = [PackageDescription.SwiftSetting]()
// The only target which needs the ability to import this macro
// implementation target's module is its unit test target. Users of the
// macros this target implements use them via their declarations in the
// Testing module. This target's module is never distributed to users,
// but as an additional guard against accidental misuse, this specifies
// the unit test target as the only allowable client.
if buildingForDevelopment {
result.append(.unsafeFlags(["-Xfrontend", "-allowable-client", "-Xfrontend", "TestingMacrosTests"]))
}
return result
}()
),
// "Support" targets: These targets are not meant to be used directly by
// test authors.
.target(
name: "_TestingInternals",
exclude: ["CMakeLists.txt"],
cxxSettings: .packageSettings
),
.target(
name: "_TestDiscovery",
dependencies: ["_TestingInternals",],
exclude: ["CMakeLists.txt"],
cxxSettings: .packageSettings,
swiftSettings: .packageSettings
),
// Cross-import overlays (not supported by Swift Package Manager)
.target(
name: "_Testing_CoreGraphics",
dependencies: [
"Testing",
],
path: "Sources/Overlays/_Testing_CoreGraphics",
swiftSettings: .packageSettings + .enableLibraryEvolution()
),
.target(
name: "_Testing_Foundation",
dependencies: [
"Testing",
],
path: "Sources/Overlays/_Testing_Foundation",
exclude: ["CMakeLists.txt"],
// The Foundation module only has Library Evolution enabled on Apple
// platforms, and since this target's module publicly imports Foundation,
// it can only enable Library Evolution itself on those platforms.
swiftSettings: .packageSettings + .enableLibraryEvolution(applePlatformsOnly: true)
),
// Utility targets: These are utilities intended for use when developing
// this package, not for distribution.
.executableTarget(
name: "SymbolShowcase",
dependencies: [
"Testing",
],
swiftSettings: .packageSettings
),
],
cxxLanguageStandard: .cxx20
)
// BUG: swift-package-manager-#6367
#if !os(Windows) && !os(FreeBSD) && !os(OpenBSD)
package.targets.append(contentsOf: [
.testTarget(
name: "TestingMacrosTests",
dependencies: [
"Testing",
"TestingMacros",
],
swiftSettings: .packageSettings
)
])
#endif
extension Array where Element == PackageDescription.SwiftSetting {
/// Settings intended to be applied to every Swift target in this package.
/// Analogous to project-level build settings in an Xcode project.
static var packageSettings: Self {
var result = availabilityMacroSettings
if buildingForDevelopment {
result.append(.unsafeFlags(["-require-explicit-sendable"]))
}
result += [
.enableUpcomingFeature("ExistentialAny"),
.enableExperimentalFeature("AccessLevelOnImport"),
.enableUpcomingFeature("InternalImportsByDefault"),
.enableUpcomingFeature("MemberImportVisibility"),
// This setting is enabled in the package, but not in the toolchain build
// (via CMake). Enabling it is dependent on acceptance of the @section
// proposal via Swift Evolution.
.enableExperimentalFeature("SymbolLinkageMarkers"),
// When building as a package, the macro plugin always builds as an
// executable rather than a library.
.define("SWT_NO_LIBRARY_MACRO_PLUGINS"),
.define("SWT_TARGET_OS_APPLE", .when(platforms: [.macOS, .iOS, .macCatalyst, .watchOS, .tvOS, .visionOS])),
.define("SWT_NO_EXIT_TESTS", .when(platforms: [.iOS, .watchOS, .tvOS, .visionOS, .wasi, .android])),
.define("SWT_NO_PROCESS_SPAWNING", .when(platforms: [.iOS, .watchOS, .tvOS, .visionOS, .wasi, .android])),
.define("SWT_NO_SNAPSHOT_TYPES", .when(platforms: [.linux, .custom("freebsd"), .openbsd, .windows, .wasi, .android])),
.define("SWT_NO_DYNAMIC_LINKING", .when(platforms: [.wasi])),
.define("SWT_NO_PIPES", .when(platforms: [.wasi])),
]
return result
}
/// Settings which define commonly-used OS availability macros.
///
/// These leverage a pseudo-experimental feature in the Swift compiler for
/// setting availability definitions, which was added in
/// [swift#65218](https://github.com/swiftlang/swift/pull/65218).
private static var availabilityMacroSettings: Self {
[
.enableExperimentalFeature("AvailabilityMacro=_mangledTypeNameAPI:macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0"),
.enableExperimentalFeature("AvailabilityMacro=_uttypesAPI:macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0"),
.enableExperimentalFeature("AvailabilityMacro=_backtraceAsyncAPI:macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0"),
.enableExperimentalFeature("AvailabilityMacro=_clockAPI:macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0"),
.enableExperimentalFeature("AvailabilityMacro=_regexAPI:macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0"),
.enableExperimentalFeature("AvailabilityMacro=_swiftVersionAPI:macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0"),
.enableExperimentalFeature("AvailabilityMacro=_typedThrowsAPI:macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0"),
.enableExperimentalFeature("AvailabilityMacro=_distantFuture:macOS 99.0, iOS 99.0, watchOS 99.0, tvOS 99.0, visionOS 99.0"),
]
}
/// Create a Swift setting which enables Library Evolution, optionally
/// constraining it to only Apple platforms.
///
/// - Parameters:
/// - applePlatformsOnly: Whether to constrain this setting to only Apple
/// platforms.
static func enableLibraryEvolution(applePlatformsOnly: Bool = false) -> Self {
var result = [PackageDescription.SwiftSetting]()
if buildingForDevelopment {
var condition: BuildSettingCondition?
if applePlatformsOnly {
condition = .when(platforms: [.macOS, .iOS, .macCatalyst, .watchOS, .tvOS, .visionOS])
}
result.append(.unsafeFlags(["-enable-library-evolution"], condition))
}
return result
}
}
extension Array where Element == PackageDescription.CXXSetting {
/// Settings intended to be applied to every C++ target in this package.
/// Analogous to project-level build settings in an Xcode project.
static var packageSettings: Self {
var result = Self()
result += [
.define("SWT_NO_EXIT_TESTS", .when(platforms: [.iOS, .watchOS, .tvOS, .visionOS, .wasi, .android])),
.define("SWT_NO_PROCESS_SPAWNING", .when(platforms: [.iOS, .watchOS, .tvOS, .visionOS, .wasi, .android])),
.define("SWT_NO_SNAPSHOT_TYPES", .when(platforms: [.linux, .custom("freebsd"), .openbsd, .windows, .wasi, .android])),
.define("SWT_NO_DYNAMIC_LINKING", .when(platforms: [.wasi])),
.define("SWT_NO_PIPES", .when(platforms: [.wasi])),
]
// Capture the testing library's version as a C++ string constant.
if let git {
let testingLibraryVersion = if let tag = git.currentTag {
tag
} else if git.hasUncommittedChanges {
"\(git.currentCommit) (modified)"
} else {
git.currentCommit
}
result.append(.define("SWT_TESTING_LIBRARY_VERSION", to: #""\#(testingLibraryVersion)""#))
}
return result
}
}