Skip to content

[WIP] Set C language settings as well as C++ language settings #904

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
99 changes: 71 additions & 28 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ let package = Package(
"TestingMacros",
],
exclude: ["CMakeLists.txt", "Testing.swiftcrossimport"],
cSettings: .packageSettings,
cxxSettings: .packageSettings,
swiftSettings: .packageSettings,
linkerSettings: [
Expand All @@ -67,6 +68,8 @@ let package = Package(
"_Testing_CoreGraphics",
"_Testing_Foundation",
],
cSettings: .packageSettings,
cxxSettings: .packageSettings,
swiftSettings: .packageSettings + [
// For testing test content section discovery only
.enableExperimentalFeature("SymbolLinkageMarkers"),
Expand Down Expand Up @@ -104,7 +107,9 @@ let package = Package(
.target(
name: "_TestingInternals",
exclude: ["CMakeLists.txt"],
cxxSettings: .packageSettings
cSettings: .packageSettings,
cxxSettings: .packageSettings,
swiftSettings: .packageSettings
),

// Cross-import overlays (not supported by Swift Package Manager)
Expand All @@ -114,6 +119,8 @@ let package = Package(
"Testing",
],
path: "Sources/Overlays/_Testing_CoreGraphics",
cSettings: .packageSettings,
cxxSettings: .packageSettings,
swiftSettings: .packageSettings
),
.target(
Expand All @@ -123,6 +130,8 @@ let package = Package(
],
path: "Sources/Overlays/_Testing_Foundation",
exclude: ["CMakeLists.txt"],
cSettings: .packageSettings,
cxxSettings: .packageSettings,
swiftSettings: .packageSettings
),
],
Expand All @@ -139,16 +148,54 @@ package.targets.append(contentsOf: [
"Testing",
"TestingMacros",
],
cSettings: .packageSettings,
cxxSettings: .packageSettings,
swiftSettings: .packageSettings
)
])
#endif

// MARK: - Settings common among languages

protocol LanguageSetting {
static func define(_ name: String, _ condition: PackageDescription.BuildSettingCondition?) -> Self
}

protocol CFamilyLanguageSetting: LanguageSetting {
static func define(_ name: String, to value: String?, _ condition: PackageDescription.BuildSettingCondition?) -> Self
}

extension CFamilyLanguageSetting {
static func define(_ name: String, _ condition: PackageDescription.BuildSettingCondition?) -> Self {
.define(name, to: nil, condition)
}
}

extension PackageDescription.SwiftSetting: LanguageSetting {}
extension PackageDescription.CSetting: CFamilyLanguageSetting {}
extension PackageDescription.CXXSetting: CFamilyLanguageSetting {}

// MARK: - Package-wide settings (to be applied to all targets)

extension Array where Element: LanguageSetting {
/// Settings applied across the different languages that determine if certain
/// features of the testing library are enabled or not.
static var compilerConditionals: Self {
[
.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])),
.define("SWT_NO_DYNAMIC_LINKING", .when(platforms: [.wasi])),
.define("SWT_NO_PIPES", .when(platforms: [.wasi])),
]
}
}

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"]),
.enableUpcomingFeature("ExistentialAny"),
.enableExperimentalFeature("SuppressedAssociatedTypes"),
Expand All @@ -157,13 +204,7 @@ extension Array where Element == PackageDescription.SwiftSetting {
.enableUpcomingFeature("InternalImportsByDefault"),

.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])),
.define("SWT_NO_DYNAMIC_LINKING", .when(platforms: [.wasi])),
.define("SWT_NO_PIPES", .when(platforms: [.wasi])),
]
] + availabilityMacroSettings + compilerConditionals
}

/// Settings which define commonly-used OS availability macros.
Expand All @@ -186,30 +227,32 @@ extension Array where Element == PackageDescription.SwiftSetting {
}
}

extension Array where Element == PackageDescription.CXXSetting {
/// Settings intended to be applied to every C++ target in this package.
/// The testing library's version as determined by the git repository state.
let testingLibraryVersion: String? = if let git = Context.gitInformation {
if let tag = git.currentTag {
tag
} else if git.hasUncommittedChanges {
"\(git.currentCommit) (modified)"
} else {
git.currentCommit
}
} else {
nil
}

extension Array where Element: CFamilyLanguageSetting {
/// Settings intended to be applied to every C or C++ target in this package.
/// Analogous to project-level build settings in an Xcode project.
///
/// The Swift compiler uses C settings (not C++ settings) when importing a C
/// or C++ header.
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])),
.define("SWT_NO_DYNAMIC_LINKING", .when(platforms: [.wasi])),
.define("SWT_NO_PIPES", .when(platforms: [.wasi])),
]
result += compilerConditionals

// Capture the testing library's version as a C++ string constant.
if let git = Context.gitInformation {
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)""#))
if let testingLibraryVersion {
result.append(.define("SWT_TESTING_LIBRARY_VERSION", to: #""\#(testingLibraryVersion)""#, nil))
}

return result
Expand Down
14 changes: 4 additions & 10 deletions Sources/_TestingInternals/include/Discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,19 @@ SWT_IMPORT_FROM_STDLIB void swift_enumerateAllMetadataSections(
);
#endif

#if defined(SWT_NO_DYNAMIC_LINKING)
#pragma mark - Statically-linked section bounds

/// The bounds of the test content section statically linked into the image
/// containing Swift Testing.
///
/// - Note: This symbol is _declared_, but not _defined_, on platforms with
/// dynamic linking because the `SWT_NO_DYNAMIC_LINKING` C++ macro (not the
/// Swift compiler conditional of the same name) is not consistently declared
/// when Swift files import the `_TestingInternals` C++ module.
SWT_EXTERN const void *_Nonnull const SWTTestContentSectionBounds[2];

#if !defined(SWT_NO_LEGACY_TEST_DISCOVERY)
/// The bounds of the type metadata section statically linked into the image
/// containing Swift Testing.
///
/// - Note: This symbol is _declared_, but not _defined_, on platforms with
/// dynamic linking because the `SWT_NO_DYNAMIC_LINKING` C++ macro (not the
/// Swift compiler conditional of the same name) is not consistently declared
/// when Swift files import the `_TestingInternals` C++ module.
SWT_EXTERN const void *_Nonnull const SWTTypeMetadataSectionBounds[2];
#endif
#endif

#pragma mark - Legacy test discovery

Expand Down