This change comes with fairly major change to the bazel toolchain. - Use pre-built cache that comes with emsdk - Mark cache as readonly using FROZEN_CACHE - Pass `--sysroot` to match upstream emscripten change
Bazel Emscripten toolchain
Setup Instructions
- Merge the
WORKSPACEfile in with your own at the root of your bazel directory structure. If you don't have one, simply copy the file. - Merge the
bazelrcfile in with your.bazelrcfile at the root of your bazel directory structure. If you don't have one, simply copy the file and rename it to.bazelrc. (Note the.) - Copy the
emscripten_toolchainfolder along with its contents to the root of your bazel directory.
Your directory structure should look like this:
bazel_root/
├── .bazelrc
├── WORKSPACE
├── emscripten_toolchain/
│ ├── BUILD.bazel
│ ├── builddefs.bzl
│ ├── crosstool.bzl
│ ├── emar.sh
│ ├── emcc.sh
│ ├── emcc_link.sh
│ ├── emscripten.BUILD
│ ├── emscripten_config
│ ├── env.sh
│ ├── link_wrapper.py
│ ├── wasm_binary.py
│ ├── wasm_cc_binary.bzl
│ ├── wasm_rules.bzl
├── your_project_folder/
│ ├── your_project.file
Building
Using --config=wasm
Simply pass --config=wasm when building a normal cc_binary. The result of
this build will be a tar archive containing any files produced by emscripten.
Using wasm_cc_binary
First, write a new rule wrapping your cc_binary.
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")
cc_binary(
name = "hello-world",
srcs = ["hello-world.cc"],
)
wasm_cc_binary(
name = "hello-world-wasm",
cc_target = ":hello-world",
)
Now you can run bazel build :hello-world-wasm. The result of this build will
be the individual files produced by emscripten. Note that some of these files
may be empty. This is because bazel has no concept of optional outputs for
rules.