Skip to content

Commit b803e3e

Browse files
authored
[5.9] rdar://105991217 (Make macros testable) (#6334)
Opt macros into testable executables if we're building them as executables. (cherry picked from commit e02a8b5)
1 parent 9d872a3 commit b803e3e

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

Sources/Build/BuildDescription/ProductBuildDescription.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
234234
// Support for linking tests against executables is conditional on the tools
235235
// version of the package that defines the executable product.
236236
let executableTarget = try product.executableTarget
237-
if executableTarget.underlyingTarget is SwiftTarget, self.toolsVersion >= .v5_5,
238-
self.buildParameters.canRenameEntrypointFunctionName, executableTarget.underlyingTarget.type != .macro
237+
if let target = executableTarget.underlyingTarget as? SwiftTarget, self.toolsVersion >= .v5_5,
238+
self.buildParameters.canRenameEntrypointFunctionName, target.supportsTestableExecutablesFeature
239239
{
240240
if let flags = buildParameters.linkerFlagsForRenamingMainFunction(of: executableTarget) {
241241
args += flags

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public final class SwiftTargetBuildDescription {
9393
var moduleOutputPath: AbsolutePath {
9494
// If we're an executable and we're not allowing test targets to link against us, we hide the module.
9595
let allowLinkingAgainstExecutables = (buildParameters.triple.isDarwin() || self.buildParameters.triple
96-
.isLinux() || self.buildParameters.triple.isWindows()) && self.toolsVersion >= .v5_5 && target.type != .macro
96+
.isLinux() || self.buildParameters.triple.isWindows()) && self.toolsVersion >= .v5_5
9797
let dirPath = (target.type == .executable && !allowLinkingAgainstExecutables) ? self.tempsPath : self
9898
.buildParameters.buildPath
9999
return dirPath.appending(component: self.target.c99name + ".swiftmodule")
@@ -450,7 +450,7 @@ public final class SwiftTargetBuildDescription {
450450
// when we link the executable, we will ask the linker to rename the entry point
451451
// symbol to just `_main` again (or if the linker doesn't support it, we'll
452452
// generate a source containing a redirect).
453-
if (self.target.type == .executable || self.target.type == .snippet)
453+
if (self.target.underlyingTarget as? SwiftTarget)?.supportsTestableExecutablesFeature == true
454454
&& !self.isTestTarget && self.toolsVersion >= .v5_5
455455
{
456456
// We only do this if the linker supports it, as indicated by whether we

Sources/Build/BuildPlan.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,10 +731,10 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
731731
// In tool version .v5_5 or greater, we also include executable modules implemented in Swift in
732732
// any test products... this is to allow testing of executables. Note that they are also still
733733
// built as separate products that the test can invoke as subprocesses.
734-
case .executable, .snippet:
734+
case .executable, .snippet, .macro:
735735
if product.targets.contains(target) {
736736
staticTargets.append(target)
737-
} else if product.type == .test && target.underlyingTarget is SwiftTarget {
737+
} else if product.type == .test && (target.underlyingTarget as? SwiftTarget)?.supportsTestableExecutablesFeature == true {
738738
if let toolsVersion = graph.package(for: product)?.manifest.toolsVersion, toolsVersion >= .v5_5 {
739739
staticTargets.append(target)
740740
}

Sources/PackageModel/Target.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,15 @@ public final class SwiftTarget: Target {
426426
self.swiftVersion = try container.decode(SwiftLanguageVersion.self, forKey: .swiftVersion)
427427
try super.init(from: decoder)
428428
}
429+
430+
public var supportsTestableExecutablesFeature: Bool {
431+
// Exclude macros from testable executables if they are built as dylibs.
432+
#if BUILD_MACROS_AS_DYLIBS
433+
return type == .executable || type == .snippet
434+
#else
435+
return type == .executable || type == .macro || type == .snippet
436+
#endif
437+
}
429438
}
430439

431440
public final class SystemLibraryTarget: Target {

0 commit comments

Comments
 (0)