Skip to content

Commit 623d8de

Browse files
committed
Support macros as executables
This adds support for building macros as executables (see swiftlang/swift-syntax#1359). Right now, this requires adding a package dependency on swift-syntax and corresponding product dependencies to the macro itself which makes testing difficult, so `BUILD_MACROS_AS_DYLIBS` is kept as the default mode.
1 parent 51e9722 commit 623d8de

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public final class SwiftTargetBuildDescription {
156156
switch self.target.type {
157157
case .library, .test:
158158
return true
159-
case .executable, .snippet:
159+
case .executable, .snippet, .macro:
160160
// This deactivates heuristics in the Swift compiler that treats single-file modules and source files
161161
// named "main.swift" specially w.r.t. whether they can have an entry point.
162162
//
@@ -394,8 +394,13 @@ public final class SwiftTargetBuildDescription {
394394
args += ["-Xfrontend", "-load-plugin-library", "-Xfrontend", self.buildParameters.binaryPath(for: macro).pathString]
395395
}
396396
#else
397-
if self.requiredMacroProducts.isEmpty == false {
398-
throw InternalError("building macros is not supported yet")
397+
try self.requiredMacroProducts.forEach { macro in
398+
if let macroTarget = macro.targets.first {
399+
let executablePath = self.buildParameters.binaryPath(for: macro).pathString
400+
args += ["-Xfrontend", "-load-plugin-executable", "-Xfrontend", "\(executablePath)#\(macroTarget.c99name)"]
401+
} else {
402+
throw InternalError("macro product \(macro.name) has no targets") // earlier validation should normally catch this
403+
}
399404
}
400405
#endif
401406

0 commit comments

Comments
 (0)