Skip to content

Add wasm32-unknown-emscripten triple support#2105

Merged
MaxDesiatov merged 6 commits into
mainfrom
maxd/emscripten-support
May 13, 2026
Merged

Add wasm32-unknown-emscripten triple support#2105
MaxDesiatov merged 6 commits into
mainfrom
maxd/emscripten-support

Conversation

@MaxDesiatov
Copy link
Copy Markdown
Contributor

@MaxDesiatov MaxDesiatov commented Mar 16, 2026

One of the necessary steps for implementing the Emscripten pitch: https://forums.swift.org/t/pitch-emscripten-target-support-for-swift/85310.

Depends on swiftlang/swift#88732

@MaxDesiatov
Copy link
Copy Markdown
Contributor Author

@swift-ci test

@jakepetroules
Copy link
Copy Markdown
Contributor

Can you also add support for this platform in Swift Build's SWBWebAssembly plugin?

@MaxDesiatov
Copy link
Copy Markdown
Contributor Author

Can you also add support for this platform in Swift Build's SWBWebAssembly plugin?

Done in swiftlang/swift-build#1335

@MaxDesiatov MaxDesiatov marked this pull request as ready for review April 28, 2026 15:22
@MaxDesiatov MaxDesiatov requested a review from a team as a code owner April 28, 2026 15:22
@MaxDesiatov
Copy link
Copy Markdown
Contributor Author

swiftlang/swift#87797
@swift-ci test

@MaxDesiatov
Copy link
Copy Markdown
Contributor Author

@swift-ci test

@MaxDesiatov
Copy link
Copy Markdown
Contributor Author

@swift-ci test

@MaxDesiatov
Copy link
Copy Markdown
Contributor Author

@swift-ci test windows

@MaxDesiatov MaxDesiatov enabled auto-merge May 11, 2026 16:04

public let dummyForTestingObjectFormat = Triple.ObjectFormat.wasm

public var targetTriple: Triple?
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we parameterize makeLinkerOutputFilename instead? Even though it means updating unrelated toolchains, I'd rather we thread the triple to the decision point where we need it, instead of this kind of mutable state that's post-hoc initialized.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a separate EmscriptenToolchain should address this as well.

components: "embedded", targetTriple.triple
)
commandLine.append(.flag("-Xlinker"))
if !isEmscripten {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if I have a better suggestion, but it feels a bit unwieldy to special case things like this. Would there be too much duplication if we factor out the emcc code path out of here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by creating separate EmscriptenToolchain.

@MaxDesiatov
Copy link
Copy Markdown
Contributor Author

@swift-ci test

@MaxDesiatov
Copy link
Copy Markdown
Contributor Author

@swift-ci test windows

@MaxDesiatov MaxDesiatov requested a review from artemcm May 12, 2026 16:02
commandLine.appendFlag(linkerOpt.argument.asSingle)
}
// `-Xemcc-linker` passes arguments directly to `emcc`.
try commandLine.appendAllArguments(.XemccLinker, from: &parsedOptions)
Copy link
Copy Markdown
Member

@kateinoigakukun kateinoigakukun May 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we pass -Xemcc-linker values to emcc, then I'm wondering if we really need a separate option name for emcc. Can't we just use -Xclang-linker? We can treat it just like "X flag for linker driver, not the linker itself" rather than strictly treating it only for "clang linker driver". emcc is meant to be used as drop-in-replacement as clang command, so this model should work I believe. Otherwise the build invoker has to switch the option based on if a target is emscripten or not, which is something I'm not a big fan of.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a drop-in replacement unfortunately, flags and options it supports differ substantially. I think keeping -Xclang-linker for it would be very confusing, as very few of the existing options work and require explicit updates. I'm totally fine with introducing -Xlinker-driver separately though.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a drop-in replacement unfortunately, flags and options it supports differ substantially.

Oh, really. I know many options specific to emcc but don't know much about clang options not working for emcc. Do you have examples?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know of at least 3 cases that break or warn:

  • -resource-dir PATH and --sysroot PATH: emcc has a silent flag-drop: it swallows the flag word but lets the path leak as a positional input leading to wasm-ld: error: cannot open PATH: Is a directory. Same bug hits -B PATH. Joined forms work.
  • -nostdlib: accepted but semantically incompatible (emcc's runtime needs symbols -nostdlib drops)
  • -Wl,-z,max-page-size=16384: wasm-ld warns and no-ops

As for the asymmetry: emcc-only flags like -sENVIRONMENT=shell, --shell-file= are hard errors under clang (unknown argument).

public static let Wwarning: Option = Option("-Wwarning", .separate, attributes: [.helpHidden, .frontend], metaVar: "<diagnostic_group>", helpText: "Treat this warning group as warning", group: .warningTreating)
public static let Xcc: Option = Option("-Xcc", .separate, attributes: [.frontend, .synthesizeInterface], metaVar: "<arg>", helpText: "Pass <arg> to the C/C++/Objective-C compiler")
public static let XclangLinker: Option = Option("-Xclang-linker", .separate, attributes: [.helpHidden], metaVar: "<arg>", helpText: "Pass <arg> to Clang when it is used for linking.")
public static let XemccLinker: Option = Option("-Xemcc-linker", .separate, attributes: [.helpHidden], metaVar: "<arg>", helpText: "Pass <arg> to emcc when it is used for linking.")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

@artemcm artemcm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much cleaner, thanks!

)

// `emcc` supports `-Xlinker` for passing flags to `wasm-ld`, but
// `-Xclang-linker` is `clang`-specific and must not be forwarded.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we warn when -Xclang-linker is used and we are on the emcc code path, instead of dropping them silently?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does emit a working in EmscriptenToolchain validation function, I've updated this comment to mention that in #2136


// Delegate to Clang for sanitizers. It will figure out the correct linker
// options.
if linkerOutputType == .executable && !sanitizers.isEmpty {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can tell, in the code as-is, linkerOutputType == .executable is always going to be true. Do we think that can change in the future? If not, do we still need linkerOutputType here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in #2136

@MaxDesiatov MaxDesiatov merged commit f71edf5 into main May 13, 2026
22 of 26 checks passed
@MaxDesiatov
Copy link
Copy Markdown
Contributor Author

Ah, sorry about the auto-merge that I forgot to disable, I'll submit a follow up PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants