Skip to content

Suppress .unsafeFlags() in Package.swift when tagging for release. #991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 46 additions & 24 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
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",

Expand Down Expand Up @@ -55,9 +62,7 @@ let package = Package(
],
exclude: ["CMakeLists.txt", "Testing.swiftcrossimport"],
cxxSettings: .packageSettings,
swiftSettings: .packageSettings + [
.enableLibraryEvolution(),
],
swiftSettings: .packageSettings + .enableLibraryEvolution(),
linkerSettings: [
.linkedLibrary("execinfo", .when(platforms: [.custom("freebsd"), .openbsd]))
]
Expand Down Expand Up @@ -86,19 +91,21 @@ let package = Package(
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
],
exclude: ["CMakeLists.txt"],
swiftSettings: .packageSettings + [
// When building as a package, the macro plugin always builds as an
// executable rather than a library.
.define("SWT_NO_LIBRARY_MACRO_PLUGINS"),
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.
.unsafeFlags(["-Xfrontend", "-allowable-client", "-Xfrontend", "TestingMacrosTests"]),
]
if buildingForDevelopment {
result.append(.unsafeFlags(["-Xfrontend", "-allowable-client", "-Xfrontend", "TestingMacrosTests"]))
}

return result
}()
),

// "Support" targets: These contain C family code and are used exclusively
Expand All @@ -116,9 +123,7 @@ let package = Package(
"Testing",
],
path: "Sources/Overlays/_Testing_CoreGraphics",
swiftSettings: .packageSettings + [
.enableLibraryEvolution(),
]
swiftSettings: .packageSettings + .enableLibraryEvolution()
),
.target(
name: "_Testing_Foundation",
Expand All @@ -127,12 +132,10 @@ let package = Package(
],
path: "Sources/Overlays/_Testing_Foundation",
exclude: ["CMakeLists.txt"],
swiftSettings: .packageSettings + [
// 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.
.enableLibraryEvolution(applePlatformsOnly: true),
]
// 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
Expand Down Expand Up @@ -167,14 +170,23 @@ 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 {
availabilityMacroSettings + [
.unsafeFlags(["-require-explicit-sendable"]),
var result = availabilityMacroSettings

if buildingForDevelopment {
result.append(.unsafeFlags(["-require-explicit-sendable"]))
}

result += [
.enableUpcomingFeature("ExistentialAny"),
.enableExperimentalFeature("SuppressedAssociatedTypes"),

.enableExperimentalFeature("AccessLevelOnImport"),
.enableUpcomingFeature("InternalImportsByDefault"),

// 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])),
Expand All @@ -183,6 +195,8 @@ extension Array where Element == PackageDescription.SwiftSetting {
.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.
Expand All @@ -203,17 +217,25 @@ extension Array where Element == PackageDescription.SwiftSetting {
.enableExperimentalFeature("AvailabilityMacro=_distantFuture:macOS 99.0, iOS 99.0, watchOS 99.0, tvOS 99.0, visionOS 99.0"),
]
}
}

extension PackageDescription.SwiftSetting {
/// 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 {
unsafeFlags(["-enable-library-evolution"], .when(platforms: applePlatformsOnly ? [.macOS, .iOS, .macCatalyst, .watchOS, .tvOS, .visionOS] : []))
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
}
}

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

// Capture the testing library's version as a C++ string constant.
if let git = Context.gitInformation {
if let git {
let testingLibraryVersion = if let tag = git.currentTag {
tag
} else if git.hasUncommittedChanges {
Expand Down