From 623d8de6c8d6e76cf4bcf4bd7818da8ebc8248ca Mon Sep 17 00:00:00 2001 From: Boris Buegling Date: Tue, 28 Feb 2023 18:40:27 -0800 Subject: [PATCH] Support macros as executables This adds support for building macros as executables (see https://github.com/apple/swift-syntax/pull/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. --- .../SwiftTargetBuildDescription.swift | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift b/Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift index 28ad3dc3818..65f2df7d6a6 100644 --- a/Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift +++ b/Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift @@ -156,7 +156,7 @@ public final class SwiftTargetBuildDescription { switch self.target.type { case .library, .test: return true - case .executable, .snippet: + case .executable, .snippet, .macro: // This deactivates heuristics in the Swift compiler that treats single-file modules and source files // named "main.swift" specially w.r.t. whether they can have an entry point. // @@ -394,8 +394,13 @@ public final class SwiftTargetBuildDescription { args += ["-Xfrontend", "-load-plugin-library", "-Xfrontend", self.buildParameters.binaryPath(for: macro).pathString] } #else - if self.requiredMacroProducts.isEmpty == false { - throw InternalError("building macros is not supported yet") + try self.requiredMacroProducts.forEach { macro in + if let macroTarget = macro.targets.first { + let executablePath = self.buildParameters.binaryPath(for: macro).pathString + args += ["-Xfrontend", "-load-plugin-executable", "-Xfrontend", "\(executablePath)#\(macroTarget.c99name)"] + } else { + throw InternalError("macro product \(macro.name) has no targets") // earlier validation should normally catch this + } } #endif