@@ -20,17 +20,49 @@ let git = Context.gitInformation
20
20
/// distribution as a package dependency.
21
21
let buildingForDevelopment = ( git? . currentTag == nil )
22
22
23
+ /// Whether or not this package is being built for Embedded Swift.
24
+ ///
25
+ /// This value is `true` if `SWT_EMBEDDED` is set in the environment to `true`
26
+ /// when `swift build` is invoked. This inference is experimental and is subject
27
+ /// to change in the future.
28
+ ///
29
+ /// - Bug: There is currently no way for us to tell if we are being asked to
30
+ /// build for an Embedded Swift target at the package manifest level.
31
+ /// ([swift-syntax-#8431](https://github.com/swiftlang/swift-package-manager/issues/8431))
32
+ let buildingForEmbedded : Bool = {
33
+ guard let envvar = Context . environment [ " SWT_EMBEDDED " ] else {
34
+ return false
35
+ }
36
+ return Bool ( envvar) ?? ( ( Int ( envvar) ?? 0 ) != 0 )
37
+ } ( )
38
+
23
39
let package = Package (
24
40
name: " swift-testing " ,
25
41
26
- platforms: [
27
- . macOS( . v10_15) ,
28
- . iOS( . v13) ,
29
- . watchOS( . v6) ,
30
- . tvOS( . v13) ,
31
- . macCatalyst( . v13) ,
32
- . visionOS( . v1) ,
33
- ] ,
42
+ platforms: {
43
+ if !buildingForEmbedded {
44
+ [
45
+ . macOS( . v10_15) ,
46
+ . iOS( . v13) ,
47
+ . watchOS( . v6) ,
48
+ . tvOS( . v13) ,
49
+ . macCatalyst( . v13) ,
50
+ . visionOS( . v1) ,
51
+ ]
52
+ } else {
53
+ // Open-source main-branch toolchains (currently required to build this
54
+ // package for Embedded Swift) have higher Apple platform deployment
55
+ // targets than we would otherwise require.
56
+ [
57
+ . macOS( . v14) ,
58
+ . iOS( . v18) ,
59
+ . watchOS( . v10) ,
60
+ . tvOS( . v18) ,
61
+ . macCatalyst( . v18) ,
62
+ . visionOS( . v1) ,
63
+ ]
64
+ }
65
+ } ( ) ,
34
66
35
67
products: {
36
68
var result = [ Product] ( )
@@ -185,6 +217,31 @@ package.targets.append(contentsOf: [
185
217
] )
186
218
#endif
187
219
220
+ extension BuildSettingCondition {
221
+ /// Creates a build setting condition that evaluates to `true` for Embedded
222
+ /// Swift.
223
+ ///
224
+ /// - Parameters:
225
+ /// - nonEmbeddedCondition: The value to return if the target is not
226
+ /// Embedded Swift. If `nil`, the build condition evaluates to `false`.
227
+ ///
228
+ /// - Returns: A build setting condition that evaluates to `true` for Embedded
229
+ /// Swift or is equal to `nonEmbeddedCondition` for non-Embedded Swift.
230
+ static func whenEmbedded( or nonEmbeddedCondition: @autoclosure ( ) -> Self ? = nil ) -> Self ? {
231
+ if !buildingForEmbedded {
232
+ if let nonEmbeddedCondition = nonEmbeddedCondition ( ) {
233
+ nonEmbeddedCondition
234
+ } else {
235
+ // The caller did not supply a fallback.
236
+ . when( platforms: [ ] )
237
+ }
238
+ } else {
239
+ // Enable unconditionally because the target is Embedded Swift.
240
+ nil
241
+ }
242
+ }
243
+ }
244
+
188
245
extension Array where Element == PackageDescription . SwiftSetting {
189
246
/// Settings intended to be applied to every Swift target in this package.
190
247
/// Analogous to project-level build settings in an Xcode project.
@@ -195,6 +252,10 @@ extension Array where Element == PackageDescription.SwiftSetting {
195
252
result. append ( . unsafeFlags( [ " -require-explicit-sendable " ] ) )
196
253
}
197
254
255
+ if buildingForEmbedded {
256
+ result. append ( . enableExperimentalFeature( " Embedded " ) )
257
+ }
258
+
198
259
result += [
199
260
. enableUpcomingFeature( " ExistentialAny " ) ,
200
261
@@ -214,11 +275,14 @@ extension Array where Element == PackageDescription.SwiftSetting {
214
275
215
276
. define( " SWT_TARGET_OS_APPLE " , . when( platforms: [ . macOS, . iOS, . macCatalyst, . watchOS, . tvOS, . visionOS] ) ) ,
216
277
217
- . define( " SWT_NO_EXIT_TESTS " , . when( platforms: [ . iOS, . watchOS, . tvOS, . visionOS, . wasi, . android] ) ) ,
218
- . define( " SWT_NO_PROCESS_SPAWNING " , . when( platforms: [ . iOS, . watchOS, . tvOS, . visionOS, . wasi, . android] ) ) ,
219
- . define( " SWT_NO_SNAPSHOT_TYPES " , . when( platforms: [ . linux, . custom( " freebsd " ) , . openbsd, . windows, . wasi, . android] ) ) ,
220
- . define( " SWT_NO_DYNAMIC_LINKING " , . when( platforms: [ . wasi] ) ) ,
221
- . define( " SWT_NO_PIPES " , . when( platforms: [ . wasi] ) ) ,
278
+ . define( " SWT_NO_EXIT_TESTS " , . whenEmbedded( or: . when( platforms: [ . iOS, . watchOS, . tvOS, . visionOS, . wasi, . android] ) ) ) ,
279
+ . define( " SWT_NO_PROCESS_SPAWNING " , . whenEmbedded( or: . when( platforms: [ . iOS, . watchOS, . tvOS, . visionOS, . wasi, . android] ) ) ) ,
280
+ . define( " SWT_NO_SNAPSHOT_TYPES " , . whenEmbedded( or: . when( platforms: [ . linux, . custom( " freebsd " ) , . openbsd, . windows, . wasi, . android] ) ) ) ,
281
+ . define( " SWT_NO_DYNAMIC_LINKING " , . whenEmbedded( or: . when( platforms: [ . wasi] ) ) ) ,
282
+ . define( " SWT_NO_PIPES " , . whenEmbedded( or: . when( platforms: [ . wasi] ) ) ) ,
283
+
284
+ . define( " SWT_NO_LEGACY_TEST_DISCOVERY " , . whenEmbedded( ) ) ,
285
+ . define( " SWT_NO_LIBDISPATCH " , . whenEmbedded( ) ) ,
222
286
]
223
287
224
288
return result
@@ -271,11 +335,14 @@ extension Array where Element == PackageDescription.CXXSetting {
271
335
var result = Self ( )
272
336
273
337
result += [
274
- . define( " SWT_NO_EXIT_TESTS " , . when( platforms: [ . iOS, . watchOS, . tvOS, . visionOS, . wasi, . android] ) ) ,
275
- . define( " SWT_NO_PROCESS_SPAWNING " , . when( platforms: [ . iOS, . watchOS, . tvOS, . visionOS, . wasi, . android] ) ) ,
276
- . define( " SWT_NO_SNAPSHOT_TYPES " , . when( platforms: [ . linux, . custom( " freebsd " ) , . openbsd, . windows, . wasi, . android] ) ) ,
277
- . define( " SWT_NO_DYNAMIC_LINKING " , . when( platforms: [ . wasi] ) ) ,
278
- . define( " SWT_NO_PIPES " , . when( platforms: [ . wasi] ) ) ,
338
+ . define( " SWT_NO_EXIT_TESTS " , . whenEmbedded( or: . when( platforms: [ . iOS, . watchOS, . tvOS, . visionOS, . wasi, . android] ) ) ) ,
339
+ . define( " SWT_NO_PROCESS_SPAWNING " , . whenEmbedded( or: . when( platforms: [ . iOS, . watchOS, . tvOS, . visionOS, . wasi, . android] ) ) ) ,
340
+ . define( " SWT_NO_SNAPSHOT_TYPES " , . whenEmbedded( or: . when( platforms: [ . linux, . custom( " freebsd " ) , . openbsd, . windows, . wasi, . android] ) ) ) ,
341
+ . define( " SWT_NO_DYNAMIC_LINKING " , . whenEmbedded( or: . when( platforms: [ . wasi] ) ) ) ,
342
+ . define( " SWT_NO_PIPES " , . whenEmbedded( or: . when( platforms: [ . wasi] ) ) ) ,
343
+
344
+ . define( " SWT_NO_LEGACY_TEST_DISCOVERY " , . whenEmbedded( ) ) ,
345
+ . define( " SWT_NO_LIBDISPATCH " , . whenEmbedded( ) ) ,
279
346
]
280
347
281
348
// Capture the testing library's version as a C++ string constant.
0 commit comments