Mercurial
comparison third_party/emsdk/bazel/README.md @ 179:8d17f6e6e290
[ThirdParty] Added emsdk bazel rules that can be supported by bazel 9.0.0
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Thu, 22 Jan 2026 21:23:17 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 178:94705b5986b3 | 179:8d17f6e6e290 |
|---|---|
| 1 # Bazel Emscripten toolchain | |
| 2 | |
| 3 ## Setup Instructions | |
| 4 | |
| 5 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. | |
| 6 | |
| 7 In your `MODULE.bazel` file, put: | |
| 8 ```starlark | |
| 9 emsdk_version = "4.0.6" | |
| 10 bazel_dep(name = "emsdk", version = emsdk_version) | |
| 11 git_override( | |
| 12 module_name = "emsdk", | |
| 13 remote = "https://github.com/emscripten-core/emsdk.git", | |
| 14 strip_prefix = "bazel", | |
| 15 tag = emsdk_version, | |
| 16 ) | |
| 17 ``` | |
| 18 | |
| 19 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`: | |
| 20 | |
| 21 ``` | |
| 22 emscripten_deps = use_extension( | |
| 23 "@emsdk//:emscripten_deps.bzl", | |
| 24 "emscripten_deps", | |
| 25 ) | |
| 26 emscripten_deps.config(version = "4.0.1") | |
| 27 ``` | |
| 28 | |
| 29 ## Building | |
| 30 | |
| 31 Put the following line into your `.bazelrc`: | |
| 32 | |
| 33 ``` | |
| 34 build --incompatible_enable_cc_toolchain_resolution | |
| 35 ``` | |
| 36 | |
| 37 Then write a new rule wrapping your `cc_binary`. | |
| 38 | |
| 39 ```starlark | |
| 40 load("@rules_cc//cc:defs.bzl", "cc_binary") | |
| 41 load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary") | |
| 42 | |
| 43 cc_binary( | |
| 44 name = "hello-world", | |
| 45 srcs = ["hello-world.cc"], | |
| 46 ) | |
| 47 | |
| 48 wasm_cc_binary( | |
| 49 name = "hello-world-wasm", | |
| 50 cc_target = ":hello-world", | |
| 51 ) | |
| 52 ``` | |
| 53 | |
| 54 Now you can run `bazel build :hello-world-wasm`. The result of this build will | |
| 55 be the individual files produced by emscripten. Note that some of these files | |
| 56 may be empty. This is because bazel has no concept of optional outputs for | |
| 57 rules. | |
| 58 | |
| 59 `wasm_cc_binary` uses transition to use emscripten toolchain on `cc_target` | |
| 60 and all of its dependencies, and does not require amending `.bazelrc`. This | |
| 61 is the preferred way, since it also unpacks the resulting tarball. | |
| 62 | |
| 63 The Emscripten cache shipped by default does not include LTO, 64-bit or PIC | |
| 64 builds of the system libraries and ports. If you wish to use these features you | |
| 65 will need to declare the cache in your `MODULE.bazel` as follows. Note | |
| 66 that the configuration consists of the same flags that can be passed to | |
| 67 embuilder. If `targets` is not set, all system libraries and ports will be | |
| 68 built, i.e., the `ALL` option to embuilder. | |
| 69 | |
| 70 ```starlark | |
| 71 emscripten_cache = use_extension( | |
| 72 "@emsdk//:emscripten_cache.bzl", | |
| 73 "emscripten_cache", | |
| 74 ) | |
| 75 emscripten_cache.configuration(flags = ["--lto"]) | |
| 76 emscripten_cache.targets(targets = [ | |
| 77 "crtbegin", | |
| 78 "libprintf_long_double-debug", | |
| 79 "libstubs-debug", | |
| 80 "libnoexit", | |
| 81 "libc-debug", | |
| 82 "libdlmalloc", | |
| 83 "libcompiler_rt", | |
| 84 "libc++-noexcept", | |
| 85 "libc++abi-debug-noexcept", | |
| 86 "libsockets" | |
| 87 ]) | |
| 88 ``` | |
| 89 | |
| 90 See `test_external/` for an example using [embind](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html). |