Files
ci-emsdk/bazel
Eric Higgins c7d78534d2 bazel: update rules_nodejs and migrate to rules_js (#1436)
This finishes the work started in
https://github.com/emscripten-core/emsdk/pull/1388 by fixing CI. It
avoids a breaking change by:
* Using the latest rules_js 1.x.x version, instead of updating to
rules_js 2 (which removes support for bazel 5).
* Copying the contents of
[rules_js_dependencies](https://github.com/aspect-build/rules_js/blob/main/js/repositories.bzl)
instead of calling it, as the call would need to be added by users in
their `WORKSPACE` files

Context from the previous PR:

> Bazel's Node.js dependency comes from
[rules_nodejs](https://github.com/bazelbuild/rules_nodejs/). Previously,
bazel/deps.bzl was using rules_nodejs 5.8.0, released in 2022 and only
supported Node.js toolchains up to 18.12.1.

> This PR bumps rules_nodejs to latest 6.1.1. It also replaces
build_bazel_rules_nodejs with
[rules_js](https://github.com/aspect-build/rules_js), since npm_install
that bazel/emscripten_deps.bzl used was deprecated. The README of
rules_nodejs now recommends migrating to rules_js for everything other
than the Node.js toolchain:
(371e8cab15)

> Impetus
Our repo builds with Bazel and uses Emscripten and Node.js. Tried to
upgrade Node.js 18 to Node.js 20 and saw that emsdk didn't support
rules_nodejs 6+ in the same workspace.

Similarly, it's not possible to update to rules_js v2 in a workspace
that also references `emsdk`.
2024-12-05 02:59:04 -05:00
..
2024-08-22 15:08:35 -07:00
2021-12-20 14:24:56 -05:00
2024-11-28 01:32:35 +00:00

Bazel Emscripten toolchain

Setup Instructions

In WORKSPACE file, put:

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
    name = "emsdk",
    remote = "https://github.com/emscripten-core/emsdk.git",
    tag = "3.1.64",
    strip_prefix = "bazel",
)

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

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

load("@emsdk//:toolchains.bzl", "register_emscripten_toolchains")
register_emscripten_toolchains()

The tag and emscripten_version parameters correspond to the git revision of emsdk 3.1.64. To get access to newer versions, you'll need to update those. To make use of older versions, change the parameter of git_repository and emsdk_emscripten_deps(). Supported versions are listed in revisions.bzl

Bazel 7+ additionally requires platforms dependencies in the MODULE.bazel file.

bazel_dep(name = "platforms", version = "0.0.9")

Building

Put the following line into your .bazelrc:

build --incompatible_enable_cc_toolchain_resolution

Then 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.

The Emscripten cache shipped by default does not include LTO, 64-bit or PIC builds of the system libraries and ports. If you wish to use these features you will need to declare the cache when you register the toolchain as follows. Note that the configuration consists of the same flags that can be passed to embuilder. If targets is not provided, all system libraries and ports will be built, i.e., the ALL option to embuilder.

load("@emsdk//:toolchains.bzl", "register_emscripten_toolchains")
register_emscripten_toolchains(cache = {
    "configuration": ["--lto"],
    "targets": [
        "crtbegin",
        "libprintf_long_double-debug",
        "libstubs-debug",
        "libnoexit",
        "libc-debug",
        "libdlmalloc",
        "libcompiler_rt",
        "libc++-noexcept",
        "libc++abi-debug-noexcept",
        "libsockets"
    ]
})

See test_external/ for an example using embind.