@@ -20,17 +20,53 @@ let git = Context.gitInformation
2020/// distribution as a package dependency.
2121let buildingForDevelopment = ( git? . currentTag == nil )
2222
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+ let result = Bool ( envvar) ?? ( ( Int ( envvar) ?? 0 ) != 0 )
37+ if result {
38+ print ( " Building for Embedded Swift... " )
39+ }
40+ return result
41+ } ( )
42+
2343let package = Package (
2444 name: " swift-testing " ,
2545
26- platforms: [
27- . macOS( . v10_15) ,
28- . iOS( . v13) ,
29- . watchOS( . v6) ,
30- . tvOS( . v13) ,
31- . macCatalyst( . v13) ,
32- . visionOS( . v1) ,
33- ] ,
46+ platforms: {
47+ if !buildingForEmbedded {
48+ [
49+ . macOS( . v10_15) ,
50+ . iOS( . v13) ,
51+ . watchOS( . v6) ,
52+ . tvOS( . v13) ,
53+ . macCatalyst( . v13) ,
54+ . visionOS( . v1) ,
55+ ]
56+ } else {
57+ // Open-source main-branch toolchains (currently required to build this
58+ // package for Embedded Swift) have higher Apple platform deployment
59+ // targets than we would otherwise require.
60+ [
61+ . macOS( . v14) ,
62+ . iOS( . v18) ,
63+ . watchOS( . v10) ,
64+ . tvOS( . v18) ,
65+ . macCatalyst( . v18) ,
66+ . visionOS( . v1) ,
67+ ]
68+ }
69+ } ( ) ,
3470
3571 products: {
3672 var result = [ Product] ( )
@@ -185,6 +221,31 @@ package.targets.append(contentsOf: [
185221] )
186222#endif
187223
224+ extension BuildSettingCondition {
225+ /// Creates a build setting condition that evaluates to `true` for Embedded
226+ /// Swift.
227+ ///
228+ /// - Parameters:
229+ /// - nonEmbeddedCondition: The value to return if the target is not
230+ /// Embedded Swift. If `nil`, the build condition evaluates to `false`.
231+ ///
232+ /// - Returns: A build setting condition that evaluates to `true` for Embedded
233+ /// Swift or is equal to `nonEmbeddedCondition` for non-Embedded Swift.
234+ static func whenEmbedded( or nonEmbeddedCondition: @autoclosure ( ) -> Self ? = nil ) -> Self ? {
235+ if !buildingForEmbedded {
236+ if let nonEmbeddedCondition = nonEmbeddedCondition ( ) {
237+ nonEmbeddedCondition
238+ } else {
239+ // The caller did not supply a fallback.
240+ . when( platforms: [ ] )
241+ }
242+ } else {
243+ // Enable unconditionally because the target is Embedded Swift.
244+ nil
245+ }
246+ }
247+ }
248+
188249extension Array where Element == PackageDescription . SwiftSetting {
189250 /// Settings intended to be applied to every Swift target in this package.
190251 /// Analogous to project-level build settings in an Xcode project.
@@ -197,6 +258,7 @@ extension Array where Element == PackageDescription.SwiftSetting {
197258
198259 result += [
199260 . enableUpcomingFeature( " ExistentialAny " ) ,
261+ . enableExperimentalFeature( " Embedded " , . whenEmbedded( ) ) ,
200262
201263 . enableExperimentalFeature( " AccessLevelOnImport " ) ,
202264 . enableUpcomingFeature( " InternalImportsByDefault " ) ,
@@ -214,11 +276,14 @@ extension Array where Element == PackageDescription.SwiftSetting {
214276
215277 . define( " SWT_TARGET_OS_APPLE " , . when( platforms: [ . macOS, . iOS, . macCatalyst, . watchOS, . tvOS, . visionOS] ) ) ,
216278
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] ) ) ,
279+ . define( " SWT_NO_EXIT_TESTS " , . whenEmbedded( or: . when( platforms: [ . iOS, . watchOS, . tvOS, . visionOS, . wasi, . android] ) ) ) ,
280+ . define( " SWT_NO_PROCESS_SPAWNING " , . whenEmbedded( or: . when( platforms: [ . iOS, . watchOS, . tvOS, . visionOS, . wasi, . android] ) ) ) ,
281+ . define( " SWT_NO_SNAPSHOT_TYPES " , . whenEmbedded( or: . when( platforms: [ . linux, . custom( " freebsd " ) , . openbsd, . windows, . wasi, . android] ) ) ) ,
282+ . define( " SWT_NO_DYNAMIC_LINKING " , . whenEmbedded( or: . when( platforms: [ . wasi] ) ) ) ,
283+ . define( " SWT_NO_PIPES " , . whenEmbedded( or: . when( platforms: [ . wasi] ) ) ) ,
284+
285+ . define( " SWT_NO_LEGACY_TEST_DISCOVERY " , . whenEmbedded( ) ) ,
286+ . define( " SWT_NO_LIBDISPATCH " , . whenEmbedded( ) ) ,
222287 ]
223288
224289 return result
@@ -271,11 +336,14 @@ extension Array where Element == PackageDescription.CXXSetting {
271336 var result = Self ( )
272337
273338 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] ) ) ,
339+ . define( " SWT_NO_EXIT_TESTS " , . whenEmbedded( or: . when( platforms: [ . iOS, . watchOS, . tvOS, . visionOS, . wasi, . android] ) ) ) ,
340+ . define( " SWT_NO_PROCESS_SPAWNING " , . whenEmbedded( or: . when( platforms: [ . iOS, . watchOS, . tvOS, . visionOS, . wasi, . android] ) ) ) ,
341+ . define( " SWT_NO_SNAPSHOT_TYPES " , . whenEmbedded( or: . when( platforms: [ . linux, . custom( " freebsd " ) , . openbsd, . windows, . wasi, . android] ) ) ) ,
342+ . define( " SWT_NO_DYNAMIC_LINKING " , . whenEmbedded( or: . when( platforms: [ . wasi] ) ) ) ,
343+ . define( " SWT_NO_PIPES " , . whenEmbedded( or: . when( platforms: [ . wasi] ) ) ) ,
344+
345+ . define( " SWT_NO_LEGACY_TEST_DISCOVERY " , . whenEmbedded( ) ) ,
346+ . define( " SWT_NO_LIBDISPATCH " , . whenEmbedded( ) ) ,
279347 ]
280348
281349 // Capture the testing library's version as a C++ string constant.
0 commit comments