Skip to content

[5.9] rdar://105991217 (Make macros testable) #6334

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 1 commit into from
Mar 27, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
// Support for linking tests against executables is conditional on the tools
// version of the package that defines the executable product.
let executableTarget = try product.executableTarget
if executableTarget.underlyingTarget is SwiftTarget, self.toolsVersion >= .v5_5,
self.buildParameters.canRenameEntrypointFunctionName, executableTarget.underlyingTarget.type != .macro
if let target = executableTarget.underlyingTarget as? SwiftTarget, self.toolsVersion >= .v5_5,
self.buildParameters.canRenameEntrypointFunctionName, target.supportsTestableExecutablesFeature
{
if let flags = buildParameters.linkerFlagsForRenamingMainFunction(of: executableTarget) {
args += flags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public final class SwiftTargetBuildDescription {
var moduleOutputPath: AbsolutePath {
// If we're an executable and we're not allowing test targets to link against us, we hide the module.
let allowLinkingAgainstExecutables = (buildParameters.triple.isDarwin() || self.buildParameters.triple
.isLinux() || self.buildParameters.triple.isWindows()) && self.toolsVersion >= .v5_5 && target.type != .macro
.isLinux() || self.buildParameters.triple.isWindows()) && self.toolsVersion >= .v5_5
let dirPath = (target.type == .executable && !allowLinkingAgainstExecutables) ? self.tempsPath : self
.buildParameters.buildPath
return dirPath.appending(component: self.target.c99name + ".swiftmodule")
Expand Down Expand Up @@ -450,7 +450,7 @@ public final class SwiftTargetBuildDescription {
// when we link the executable, we will ask the linker to rename the entry point
// symbol to just `_main` again (or if the linker doesn't support it, we'll
// generate a source containing a redirect).
if (self.target.type == .executable || self.target.type == .snippet)
if (self.target.underlyingTarget as? SwiftTarget)?.supportsTestableExecutablesFeature == true
&& !self.isTestTarget && self.toolsVersion >= .v5_5
{
// We only do this if the linker supports it, as indicated by whether we
Expand Down
4 changes: 2 additions & 2 deletions Sources/Build/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -731,10 +731,10 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
// In tool version .v5_5 or greater, we also include executable modules implemented in Swift in
// any test products... this is to allow testing of executables. Note that they are also still
// built as separate products that the test can invoke as subprocesses.
case .executable, .snippet:
case .executable, .snippet, .macro:
if product.targets.contains(target) {
staticTargets.append(target)
} else if product.type == .test && target.underlyingTarget is SwiftTarget {
} else if product.type == .test && (target.underlyingTarget as? SwiftTarget)?.supportsTestableExecutablesFeature == true {
if let toolsVersion = graph.package(for: product)?.manifest.toolsVersion, toolsVersion >= .v5_5 {
staticTargets.append(target)
}
Expand Down
9 changes: 9 additions & 0 deletions Sources/PackageModel/Target.swift
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,15 @@ public final class SwiftTarget: Target {
self.swiftVersion = try container.decode(SwiftLanguageVersion.self, forKey: .swiftVersion)
try super.init(from: decoder)
}

public var supportsTestableExecutablesFeature: Bool {
// Exclude macros from testable executables if they are built as dylibs.
#if BUILD_MACROS_AS_DYLIBS
return type == .executable || type == .snippet
#else
return type == .executable || type == .macro || type == .snippet
#endif
}
}

public final class SystemLibraryTarget: Target {
Expand Down