Skip to content

Commit 46fdaaf

Browse files
authored
Suppress .unsafeFlags() in Package.swift when tagging for release. (#991)
This PR removes the unsafe flags we specify in our Package.swift manifest when the package has been tagged in Git (which indicates it's a release or prerelease.) This allows a package to add Swift Testing as a package dependency without breaking its own ability to be added as a package dependency due to the use of unsafe flags. ### Checklist: - [ ] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [ ] If public symbols are renamed or modified, DocC references should be updated.
1 parent d2a1019 commit 46fdaaf

File tree

1 file changed

+46
-24
lines changed

1 file changed

+46
-24
lines changed

Diff for: Package.swift

+46-24
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
import PackageDescription
1414
import CompilerPluginSupport
1515

16+
/// Information about the current state of the package's git repository.
17+
let git = Context.gitInformation
18+
19+
/// Whether or not this package is being built for development rather than
20+
/// distribution as a package dependency.
21+
let buildingForDevelopment = (git?.currentTag == nil)
22+
1623
let package = Package(
1724
name: "swift-testing",
1825

@@ -55,9 +62,7 @@ let package = Package(
5562
],
5663
exclude: ["CMakeLists.txt", "Testing.swiftcrossimport"],
5764
cxxSettings: .packageSettings,
58-
swiftSettings: .packageSettings + [
59-
.enableLibraryEvolution(),
60-
],
65+
swiftSettings: .packageSettings + .enableLibraryEvolution(),
6166
linkerSettings: [
6267
.linkedLibrary("execinfo", .when(platforms: [.custom("freebsd"), .openbsd]))
6368
]
@@ -86,19 +91,21 @@ let package = Package(
8691
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
8792
],
8893
exclude: ["CMakeLists.txt"],
89-
swiftSettings: .packageSettings + [
90-
// When building as a package, the macro plugin always builds as an
91-
// executable rather than a library.
92-
.define("SWT_NO_LIBRARY_MACRO_PLUGINS"),
94+
swiftSettings: .packageSettings + {
95+
var result = [PackageDescription.SwiftSetting]()
9396

9497
// The only target which needs the ability to import this macro
9598
// implementation target's module is its unit test target. Users of the
9699
// macros this target implements use them via their declarations in the
97100
// Testing module. This target's module is never distributed to users,
98101
// but as an additional guard against accidental misuse, this specifies
99102
// the unit test target as the only allowable client.
100-
.unsafeFlags(["-Xfrontend", "-allowable-client", "-Xfrontend", "TestingMacrosTests"]),
101-
]
103+
if buildingForDevelopment {
104+
result.append(.unsafeFlags(["-Xfrontend", "-allowable-client", "-Xfrontend", "TestingMacrosTests"]))
105+
}
106+
107+
return result
108+
}()
102109
),
103110

104111
// "Support" targets: These contain C family code and are used exclusively
@@ -116,9 +123,7 @@ let package = Package(
116123
"Testing",
117124
],
118125
path: "Sources/Overlays/_Testing_CoreGraphics",
119-
swiftSettings: .packageSettings + [
120-
.enableLibraryEvolution(),
121-
]
126+
swiftSettings: .packageSettings + .enableLibraryEvolution()
122127
),
123128
.target(
124129
name: "_Testing_Foundation",
@@ -127,12 +132,10 @@ let package = Package(
127132
],
128133
path: "Sources/Overlays/_Testing_Foundation",
129134
exclude: ["CMakeLists.txt"],
130-
swiftSettings: .packageSettings + [
131-
// The Foundation module only has Library Evolution enabled on Apple
132-
// platforms, and since this target's module publicly imports Foundation,
133-
// it can only enable Library Evolution itself on those platforms.
134-
.enableLibraryEvolution(applePlatformsOnly: true),
135-
]
135+
// The Foundation module only has Library Evolution enabled on Apple
136+
// platforms, and since this target's module publicly imports Foundation,
137+
// it can only enable Library Evolution itself on those platforms.
138+
swiftSettings: .packageSettings + .enableLibraryEvolution(applePlatformsOnly: true),
136139
),
137140

138141
// Utility targets: These are utilities intended for use when developing
@@ -167,14 +170,23 @@ extension Array where Element == PackageDescription.SwiftSetting {
167170
/// Settings intended to be applied to every Swift target in this package.
168171
/// Analogous to project-level build settings in an Xcode project.
169172
static var packageSettings: Self {
170-
availabilityMacroSettings + [
171-
.unsafeFlags(["-require-explicit-sendable"]),
173+
var result = availabilityMacroSettings
174+
175+
if buildingForDevelopment {
176+
result.append(.unsafeFlags(["-require-explicit-sendable"]))
177+
}
178+
179+
result += [
172180
.enableUpcomingFeature("ExistentialAny"),
173181
.enableExperimentalFeature("SuppressedAssociatedTypes"),
174182

175183
.enableExperimentalFeature("AccessLevelOnImport"),
176184
.enableUpcomingFeature("InternalImportsByDefault"),
177185

186+
// When building as a package, the macro plugin always builds as an
187+
// executable rather than a library.
188+
.define("SWT_NO_LIBRARY_MACRO_PLUGINS"),
189+
178190
.define("SWT_TARGET_OS_APPLE", .when(platforms: [.macOS, .iOS, .macCatalyst, .watchOS, .tvOS, .visionOS])),
179191

180192
.define("SWT_NO_EXIT_TESTS", .when(platforms: [.iOS, .watchOS, .tvOS, .visionOS, .wasi, .android])),
@@ -183,6 +195,8 @@ extension Array where Element == PackageDescription.SwiftSetting {
183195
.define("SWT_NO_DYNAMIC_LINKING", .when(platforms: [.wasi])),
184196
.define("SWT_NO_PIPES", .when(platforms: [.wasi])),
185197
]
198+
199+
return result
186200
}
187201

188202
/// Settings which define commonly-used OS availability macros.
@@ -203,17 +217,25 @@ extension Array where Element == PackageDescription.SwiftSetting {
203217
.enableExperimentalFeature("AvailabilityMacro=_distantFuture:macOS 99.0, iOS 99.0, watchOS 99.0, tvOS 99.0, visionOS 99.0"),
204218
]
205219
}
206-
}
207220

208-
extension PackageDescription.SwiftSetting {
209221
/// Create a Swift setting which enables Library Evolution, optionally
210222
/// constraining it to only Apple platforms.
211223
///
212224
/// - Parameters:
213225
/// - applePlatformsOnly: Whether to constrain this setting to only Apple
214226
/// platforms.
215227
static func enableLibraryEvolution(applePlatformsOnly: Bool = false) -> Self {
216-
unsafeFlags(["-enable-library-evolution"], .when(platforms: applePlatformsOnly ? [.macOS, .iOS, .macCatalyst, .watchOS, .tvOS, .visionOS] : []))
228+
var result = [PackageDescription.SwiftSetting]()
229+
230+
if buildingForDevelopment {
231+
var condition: BuildSettingCondition?
232+
if applePlatformsOnly {
233+
condition = .when(platforms: [.macOS, .iOS, .macCatalyst, .watchOS, .tvOS, .visionOS])
234+
}
235+
result.append(.unsafeFlags(["-enable-library-evolution"], condition))
236+
}
237+
238+
return result
217239
}
218240
}
219241

@@ -232,7 +254,7 @@ extension Array where Element == PackageDescription.CXXSetting {
232254
]
233255

234256
// Capture the testing library's version as a C++ string constant.
235-
if let git = Context.gitInformation {
257+
if let git {
236258
let testingLibraryVersion = if let tag = git.currentTag {
237259
tag
238260
} else if git.hasUncommittedChanges {

0 commit comments

Comments
 (0)