Tl;dr
I'm failing to compile a SwiftPM package for WASM due to the package's use of macros. Is this expected behaviour? And is there a known workaround?
Cheers!
Context
I'm writing an interpreter for a custom programming language (galah) and I'm attempting to build the interpreter for WASM so that I can make an online playground for people to try out the language (not much to see yet, the language is still pretty simple and very constrained).
In the interpreter's code I've used macros to express a pattern that I couldn't otherwise (the details aren't important).
The issue
I have attempted to build the interpreter for WASM with both of the following commands,
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-04-04-a.xctoolchain/usr/bin/swift build --experimental-swift-sdk 5.10-SNAPSHOT-2024-04-09-a-wasm --target GalahInterpreter
/Users/stackotter/Library/Developer/Toolchains/swift-wasm-5.9.2-RELEASE.xctoolchain/usr/bin/swift build --triple wasm32-unknown-wasi --target GalahInterpreter
In both cases I get the follow errors,
/Users/stackotter/Desktop/Projects/LangDev/galah/.build/checkouts/swift-syntax/Sources/SwiftCompilerPlugin/CompilerPlugin.swift:103:19: error: cannot find 'dup' in scope
let inputFD = dup(fileno(stdin))
^~~
/Users/stackotter/Desktop/Projects/LangDev/galah/.build/checkouts/swift-syntax/Sources/SwiftCompilerPlugin/CompilerPlugin.swift:117:20: error: cannot find 'dup' in scope
let outputFD = dup(fileno(stdout))
^~~
/Users/stackotter/Desktop/Projects/LangDev/galah/.build/checkouts/swift-syntax/Sources/SwiftCompilerPlugin/CompilerPlugin.swift:124:11: error: cannot find 'dup2' in scope
guard dup2(fileno(stderr), fileno(stdout)) >= 0 else {
^~~~
I have run the commands with -v
and found that SwiftPM is building SwiftCompilerPlugin
for WASM even though it's only getting used by macro (which afaik gets compiled for the host platform, so compiling SwiftCompilerPlugin
for WASM is completely unnecessary work, and incorrect if incorporated into the macro).
Package setup
// swift-tools-version: 5.9
import CompilerPluginSupport
import PackageDescription
let package = Package(
name: "galah",
platforms: [.macOS(.v10_15)],
products: [
.library(
name: "GalahInterpreter",
targets: ["GalahInterpreter"]
)
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.3.0"),
.package(
url: "https://github.com/apple/swift-syntax.git",
from: "509.0.0"
),
.package(
url: "https://github.com/stackotter/swift-macro-toolkit",
from: "0.3.1"
),
],
targets: [
.executableTarget(
name: "galah",
dependencies: [
"GalahInterpreter",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]
),
.target(
name: "GalahInterpreter",
dependencies: [
"UtilityMacros"
]
),
.macro(
name: "UtilityMacrosPlugin",
dependencies: [
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
.product(name: "MacroToolkit", package: "swift-macro-toolkit"),
]
),
.target(name: "UtilityMacros", dependencies: ["UtilityMacrosPlugin"]),
.testTarget(
name: "GalahInterpreterTests",
dependencies: ["GalahInterpreter"]
),
]
)
Troubleshooting
I tried splitting the macros into a separate (local) package (to remove the direct swift-syntax dependency from galah
), but got the exact same errors again.