emsdk/bazel
Piotr Sikora 0ea8f8a870
Provide "executable" in wasm_cc_binary's DefaultInfo. (#998)
This is needed to allow custom rules and/or transitions on top of wasm_cc_binary (e.g. to workaround #972).

It's debatable whether .wasm is the correct executable target for non-standalone Wasm builds, but we don't have a way to differentiate those in wasm_cc_binary, and considering that this attribute isn't exported now, it shouldn't break anything.
2022-03-09 07:09:39 -08:00
..
emscripten_toolchain Provide "executable" in wasm_cc_binary's DefaultInfo. (#998) 2022-03-09 07:09:39 -08:00
hello-world make hello world simd a bit cleaner (#701) 2021-02-08 15:34:32 -05:00
test_external Add closure example for wasm_cc_binary (#941) 2022-02-17 13:16:18 -05:00
.gitignore Bazel Windows Support (#929) 2021-12-20 14:24:56 -05:00
BUILD Support Bazel builds on Apple silicon (#978) 2022-02-11 14:18:16 -05:00
README.md Add embind example to Bazel docs (#910) 2021-10-14 13:49:36 -04:00
WORKSPACE Run buildifier on bazel/ (#913) 2021-10-19 15:56:55 -04:00
bazelrc add a wasm bazel toolchain (#603) 2020-09-10 18:42:46 -07:00
deps.bzl Update build_bazel_rules_nodejs to fix closure compiles (#912) 2021-10-19 14:44:48 -07:00
emscripten_deps.bzl Support Bazel builds on Apple silicon (#978) 2022-02-11 14:18:16 -05:00
revisions.bzl 3.1.7 (#997) 2022-03-07 08:19:27 -08:00

README.md

Bazel Emscripten toolchain

Setup Instructions

In WORKSPACE file, put:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
    name = "emsdk",
    sha256 = "d55e3c73fc4f8d1fecb7aabe548de86bdb55080fe6b12ce593d63b8bade54567",
    strip_prefix = "emsdk-3891e7b04bf8cbb3bc62758e9c575ae096a9a518/bazel",
    url = "https://github.com/emscripten-core/emsdk/archive/3891e7b04bf8cbb3bc62758e9c575ae096a9a518.tar.gz",
)

load("@emsdk//:deps.bzl", emsdk_deps = "deps")
emsdk_deps()

load("@emsdk//:emscripten_deps.bzl", emsdk_emscripten_deps = "emscripten_deps")
emsdk_emscripten_deps(emscripten_version = "2.0.31")

The SHA1 hash in the above strip_prefix and url parameters correspond to the git revision of emsdk 2.0.31. To get access to newer versions, you'll need to update those. To make use of older versions, change the parameter of emsdk_emscripten_deps(). Supported versions are listed in revisions.bzl

Building

Using wasm_cc_binary (preferred)

First, write a new rule wrapping your cc_binary.

load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@emsdk//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.

wasm_cc_binary uses transition to use emscripten toolchain on cc_target and all of its dependencies, and does not require amending .bazelrc. This is the preferred way, since it also unpacks the resulting tarball.

See test_external/ for an example using embind.

Using --config=wasm

Put the following lines into your .bazelrc:

build:wasm --crosstool_top=@emsdk//emscripten_toolchain:everything
build:wasm --cpu=wasm
build:wasm --host_crosstool_top=@bazel_tools//tools/cpp:toolchain

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. See the Bazel documentation for more details