2020-09-10 21:42:46 -04:00
# Bazel Emscripten toolchain
## Setup Instructions
2025-04-09 23:38:12 +02:00
Support for depending on emsdk with a WORKSPACE file was removed and last available in [emsdk version 4.0.6 ](https://github.com/emscripten-core/emsdk/tree/24fc909c0da13ef641d5ae75e89b5a97f25e37aa ). Now we only support inclusion as a bzlmod module.
In your `MODULE.bazel` file, put:
2023-08-08 13:14:50 -07:00
```starlark
2025-04-09 23:38:12 +02:00
emsdk_version = "4.0.6"
bazel_dep(name = "emsdk", version = emsdk_version)
git_override(
module_name = "emsdk",
2024-07-24 22:36:24 +03:00
remote = "https://github.com/emscripten-core/emsdk.git",
strip_prefix = "bazel",
2025-04-09 23:38:12 +02:00
tag = emsdk_version,
2021-03-25 18:00:30 +01:00
)
2025-04-09 23:38:12 +02:00
```
2021-03-25 18:00:30 +01:00
2025-04-09 23:38:12 +02:00
You can use a different version of this SDK by changing it in your `MODULE.bazel` file. The Emscripten version is by default the same as the SDK version, but you can use a different one as well by adding to your `MODULE.bazel` :
2022-11-08 14:21:06 -08:00
2020-09-10 21:42:46 -04:00
```
2025-04-09 23:38:12 +02:00
emscripten_deps = use_extension(
"@emsdk//:emscripten_deps .bzl",
"emscripten_deps",
)
emscripten_deps.config(version = "4.0.1")
2024-04-04 21:04:10 +02:00
```
2021-10-14 13:49:36 -04:00
## Building
2020-09-10 21:42:46 -04:00
2022-11-08 14:21:06 -08:00
Put the following line into your `.bazelrc` :
```
build --incompatible_enable_cc_toolchain_resolution
```
Then write a new rule wrapping your `cc_binary` .
2020-09-10 21:42:46 -04:00
2023-08-08 13:14:50 -07:00
```starlark
2020-09-10 21:42:46 -04:00
load("@rules_cc//cc:defs .bzl", "cc_binary")
2021-03-25 18:00:30 +01:00
load("@emsdk//emscripten_toolchain:wasm_rules .bzl", "wasm_cc_binary")
2020-09-10 21:42:46 -04:00
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.
2021-03-25 18:00:30 +01:00
`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.
2021-10-14 13:49:36 -04:00
[Bazel] Support generating a secondary cache (#1405)
This is a working solution for generating a separate Emscripten cache.
Note that this requires an additional entry in the workspace as follows:
```starlark
load("@emsdk//:emscripten_cache.bzl", emsdk_emscripten_cache = "emscripten_cache")
emsdk_emscripten_cache()
```
When used like this, the default Emscripten cache will be used. However,
if the entry is as follows:
```starlark
load("@emsdk//:emscripten_cache.bzl", emsdk_emscripten_cache = "emscripten_cache")
emsdk_emscripten_cache(flags = ["--lto"])
```
Then embuilder will be called to build all system libraries and ports
(i.e., the `ALL` option to embuilder) with the LTO option enabled. This
can take awhile, so I have also made possible to specify which libraries
you want to build explicitly:
```starlark
load("@emsdk//:emscripten_cache.bzl", emsdk_emscripten_cache = "emscripten_cache")
emsdk_emscripten_cache(
flags = ["--lto"],
libraries = [
"crtbegin",
"libprintf_long_double-debug",
"libstubs-debug",
"libnoexit",
"libc-debug",
"libdlmalloc",
"libcompiler_rt",
"libc++-noexcept",
"libc++abi-debug-noexcept",
"libsockets"
]
)
```
Resolves #807, resolves #971, resolves #1099, resolves #1362, resolves
#1401
2024-07-01 18:23:07 +02:00
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
2025-04-09 23:38:12 +02:00
will need to declare the cache in your `MODULE.bazel` as follows. Note
[Bazel] Support generating a secondary cache (#1405)
This is a working solution for generating a separate Emscripten cache.
Note that this requires an additional entry in the workspace as follows:
```starlark
load("@emsdk//:emscripten_cache.bzl", emsdk_emscripten_cache = "emscripten_cache")
emsdk_emscripten_cache()
```
When used like this, the default Emscripten cache will be used. However,
if the entry is as follows:
```starlark
load("@emsdk//:emscripten_cache.bzl", emsdk_emscripten_cache = "emscripten_cache")
emsdk_emscripten_cache(flags = ["--lto"])
```
Then embuilder will be called to build all system libraries and ports
(i.e., the `ALL` option to embuilder) with the LTO option enabled. This
can take awhile, so I have also made possible to specify which libraries
you want to build explicitly:
```starlark
load("@emsdk//:emscripten_cache.bzl", emsdk_emscripten_cache = "emscripten_cache")
emsdk_emscripten_cache(
flags = ["--lto"],
libraries = [
"crtbegin",
"libprintf_long_double-debug",
"libstubs-debug",
"libnoexit",
"libc-debug",
"libdlmalloc",
"libcompiler_rt",
"libc++-noexcept",
"libc++abi-debug-noexcept",
"libsockets"
]
)
```
Resolves #807, resolves #971, resolves #1099, resolves #1362, resolves
#1401
2024-07-01 18:23:07 +02:00
that the configuration consists of the same flags that can be passed to
2025-04-09 23:38:12 +02:00
embuilder. If `targets` is not set, all system libraries and ports will be
[Bazel] Support generating a secondary cache (#1405)
This is a working solution for generating a separate Emscripten cache.
Note that this requires an additional entry in the workspace as follows:
```starlark
load("@emsdk//:emscripten_cache.bzl", emsdk_emscripten_cache = "emscripten_cache")
emsdk_emscripten_cache()
```
When used like this, the default Emscripten cache will be used. However,
if the entry is as follows:
```starlark
load("@emsdk//:emscripten_cache.bzl", emsdk_emscripten_cache = "emscripten_cache")
emsdk_emscripten_cache(flags = ["--lto"])
```
Then embuilder will be called to build all system libraries and ports
(i.e., the `ALL` option to embuilder) with the LTO option enabled. This
can take awhile, so I have also made possible to specify which libraries
you want to build explicitly:
```starlark
load("@emsdk//:emscripten_cache.bzl", emsdk_emscripten_cache = "emscripten_cache")
emsdk_emscripten_cache(
flags = ["--lto"],
libraries = [
"crtbegin",
"libprintf_long_double-debug",
"libstubs-debug",
"libnoexit",
"libc-debug",
"libdlmalloc",
"libcompiler_rt",
"libc++-noexcept",
"libc++abi-debug-noexcept",
"libsockets"
]
)
```
Resolves #807, resolves #971, resolves #1099, resolves #1362, resolves
#1401
2024-07-01 18:23:07 +02:00
built, i.e., the `ALL` option to embuilder.
```starlark
2025-04-09 23:38:12 +02:00
emscripten_cache = use_extension(
"@emsdk//:emscripten_cache .bzl",
"emscripten_cache",
)
emscripten_cache.configuration(flags = ["--lto"])
emscripten_cache.targets(targets = [
"crtbegin",
"libprintf_long_double-debug",
"libstubs-debug",
"libnoexit",
"libc-debug",
"libdlmalloc",
"libcompiler_rt",
"libc++-noexcept",
"libc++abi-debug-noexcept",
"libsockets"
])
[Bazel] Support generating a secondary cache (#1405)
This is a working solution for generating a separate Emscripten cache.
Note that this requires an additional entry in the workspace as follows:
```starlark
load("@emsdk//:emscripten_cache.bzl", emsdk_emscripten_cache = "emscripten_cache")
emsdk_emscripten_cache()
```
When used like this, the default Emscripten cache will be used. However,
if the entry is as follows:
```starlark
load("@emsdk//:emscripten_cache.bzl", emsdk_emscripten_cache = "emscripten_cache")
emsdk_emscripten_cache(flags = ["--lto"])
```
Then embuilder will be called to build all system libraries and ports
(i.e., the `ALL` option to embuilder) with the LTO option enabled. This
can take awhile, so I have also made possible to specify which libraries
you want to build explicitly:
```starlark
load("@emsdk//:emscripten_cache.bzl", emsdk_emscripten_cache = "emscripten_cache")
emsdk_emscripten_cache(
flags = ["--lto"],
libraries = [
"crtbegin",
"libprintf_long_double-debug",
"libstubs-debug",
"libnoexit",
"libc-debug",
"libdlmalloc",
"libcompiler_rt",
"libc++-noexcept",
"libc++abi-debug-noexcept",
"libsockets"
]
)
```
Resolves #807, resolves #971, resolves #1099, resolves #1362, resolves
#1401
2024-07-01 18:23:07 +02:00
```
2021-10-14 13:49:36 -04:00
See `test_external/` for an example using [embind ](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html ).