Skip to content

Support cross-compiling to WebAssembly with a Swift SDK#1817

Merged
keith merged 6 commits into
bazelbuild:mainfrom
AttilaTheFun:wasm-support
Jul 6, 2026
Merged

Support cross-compiling to WebAssembly with a Swift SDK#1817
keith merged 6 commits into
bazelbuild:mainfrom
AttilaTheFun:wasm-support

Conversation

@AttilaTheFun

@AttilaTheFun AttilaTheFun commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

WebAssembly support, part of the #1809 split, on top of the now-merged foundation
PRs #1820 (swift_binary(linkshared)) and #1821 (Swift-SDK framework). A sibling
of the Android PR (#1818).

What this adds

swift.wasm_sdk: downloads the swift.org WebAssembly Swift SDK bundle matching a
swift.toolchain and, using the shared framework (#1821), defines Swift + C/C++
toolchains targeting wasm32-unknown-wasip1. swift_binary(linkshared = True)
gains the WebAssembly reactor output here (-mexec-model=reactor, no main,
exported functions) — the wasm-specific bit that keith asked to keep out of the
generic swift_binary. examples/cross_compilation builds a reactor and a
browser web app that drives it from JS.

Open questions

  • -swift.file_prefix_map is disabled for the wasm toolchain. With it on, the
    worker's developer-dir remap hard-requires DEVELOPER_DIR on Apple hosts. The
    clean fix is to gate that remap on swift._supports_developer_dir (the
    analysis-time config already is); it touches the shared Apple worker path, so I
    wanted your read first.

@keith keith left a comment

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.

didn't get all the way through yet but leaving what i have so far

Comment thread .bazelci/presubmit.yml Outdated
Comment thread examples/cross_compilation/BUILD.bazel Outdated
Comment thread examples/cross_compilation/BUILD.bazel Outdated
Comment thread examples/cross_compilation/README.md Outdated
@AttilaTheFun AttilaTheFun force-pushed the wasm-support branch 2 times, most recently from d9c388f to cfb60f4 Compare July 4, 2026 17:55

@keith keith left a comment

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.

lgtm. very minor stuff

Comment thread swift/internal/extensions/toolchains.bzl
Comment thread examples/cross_compilation/wasm/reactor_test.sh Outdated
Comment thread examples/cross_compilation/wasm/BUILD.bazel Outdated
Comment thread examples/cross_compilation/wasm/BUILD.bazel Outdated
Builds on the linkshared and Swift SDK framework PRs. Adds the swift.wasm_sdk
module extension tag: it downloads the swift.org WebAssembly Swift SDK artifact
bundle matching a swift.toolchain and, using the shared framework, defines Swift
and C/C++ toolchains targeting wasm32-unknown-wasip1, so plain swift_library /
swift_binary targets build for WebAssembly under --platforms.

swift_binary(linkshared = True) gains the WebAssembly reactor output here
(-mexec-model=reactor, no main, exported functions). examples/cross_compilation
builds a reactor and a static browser web app that drives it from JS.
- Drop the manual tags and the dedicated CI task; the example now builds and
  tests in the normal //examples/... wildcard on macOS and Linux. The
  wasm-only Reactor is target_compatible_with os:wasi so host wildcards skip
  it while the transition still builds it.
- Make the example a real executed test: reactor_test runs the reactor under
  a hermetic wasmtime (dev dependency, downloaded per host) and asserts on
  the value Swift computes.
- Re-enable swift.file_prefix_map for the wasm toolchain: the developer-dir
  remap moved behind a feature the wasm toolchain doesn't enable (bazelbuild#1825), so
  the worker no longer requires DEVELOPER_DIR and debug info stays hermetic.
- Toss the top-level example README and move the example to a wasm/
  subdirectory (done as part of the rebase; the old shared layout was
  reorganized by the Android PR), with its dev MODULE wiring split into
  wasm.MODULE.bazel following the Android pattern.

Claude-Session: https://claude.ai/code/session_01SmG1kqA3qB4WsLGU2xavuJ
CI passes --test_env=HOME, and linux-sandbox mounts the agent's HOME
read-only, so wasmtime's default compilation cache hard-failed with
'failed to create cache directory'. Set XDG_CACHE_HOME to the test
tmpdir, and stop swallowing wasmtime's stderr so failures like this are
visible in the test log.

Claude-Session: https://claude.ai/code/session_01SmG1kqA3qB4WsLGU2xavuJ
- Rebase onto main (dissolves the example bzl_library targets removed by
  bazelbuild#1826 and the embedded file_prefix_map disables removed by bazelbuild#1824).
- Use @bazel_tools//src/conditions for the wasmtime host select instead of
  bespoke config_settings.
- Drop web/README.md (covered by the BUILD file) and trim comments.

Claude-Session: https://claude.ai/code/session_01SmG1kqA3qB4WsLGU2xavuJ
Drop the wasm example's redundant header comment and default_visibility
(nothing outside the package references its targets), and document why the
SDK repository pairs a rules_cc toolchain with the Swift one: it drives the
bundle's own clang, resolves only for the wasm target platform, and a
consumer's root-module registration takes precedence over it.
@AttilaTheFun

Copy link
Copy Markdown
Contributor Author

Rebased onto main (post-#1819) and addressed the remaining comments — the example BUILD nits are applied, and the wasm cc-toolchain registration is kept with the rationale documented inline (wasm-platform-only resolution; root-module registrations take precedence for anyone bringing their own).

@keith keith merged commit 23ea106 into bazelbuild:main Jul 6, 2026
15 checks passed
@AttilaTheFun AttilaTheFun deleted the wasm-support branch July 6, 2026 19:59
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.

2 participants