Skip to content

Commit 82d41a7

Browse files
authored
[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
1 parent 7fbd555 commit 82d41a7

File tree

14 files changed

+199
-3
lines changed

14 files changed

+199
-3
lines changed

bazel/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,30 @@ rules.
6666
and all of its dependencies, and does not require amending `.bazelrc`. This
6767
is the preferred way, since it also unpacks the resulting tarball.
6868

69+
The Emscripten cache shipped by default does not include LTO, 64-bit or PIC
70+
builds of the system libraries and ports. If you wish to use these features you
71+
will need to declare the cache when you register the toolchain as follows. Note
72+
that the configuration consists of the same flags that can be passed to
73+
embuilder. If `targets` is not provided, all system libraries and ports will be
74+
built, i.e., the `ALL` option to embuilder.
75+
76+
```starlark
77+
load("@emsdk//:toolchains.bzl", "register_emscripten_toolchains")
78+
register_emscripten_toolchains(cache = {
79+
"configuration": ["--lto"],
80+
"targets": [
81+
"crtbegin",
82+
"libprintf_long_double-debug",
83+
"libstubs-debug",
84+
"libnoexit",
85+
"libc-debug",
86+
"libdlmalloc",
87+
"libcompiler_rt",
88+
"libc++-noexcept",
89+
"libc++abi-debug-noexcept",
90+
"libsockets"
91+
]
92+
})
93+
```
94+
6995
See `test_external/` for an example using [embind](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html).

bazel/emscripten_deps.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ filegroup(
2727
name = "emcc_common",
2828
srcs = [
2929
"emscripten/emcc.py",
30+
"emscripten/embuilder.py",
3031
"emscripten/emscripten-version.txt",
3132
"emscripten/cache/sysroot_install.stamp",
3233
"emscripten/src/settings.js",

bazel/emscripten_toolchain/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package(default_visibility = ["//visibility:public"])
55
filegroup(
66
name = "common_files",
77
srcs = [
8-
"emscripten_config",
8+
"@emscripten_cache//:emscripten_config",
99
"env.sh",
1010
"env.bat",
1111
"@nodejs//:node_files",
@@ -60,7 +60,7 @@ cc_library(name = "malloc")
6060
emscripten_cc_toolchain_config_rule(
6161
name = "wasm",
6262
cpu = "wasm",
63-
em_config = "emscripten_config",
63+
em_config = "@emscripten_cache//:emscripten_config",
6464
emscripten_binaries = "@emsdk//:compiler_files",
6565
script_extension = select({
6666
"@bazel_tools//src/conditions:host_windows": "bat",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build --incompatible_enable_cc_toolchain_resolution
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
bazel-bin
2+
bazel-out
3+
bazel-test_secondary_lto_cache
4+
bazel-testlogs

bazel/test_secondary_lto_cache/BUILD

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")
2+
3+
cc_binary(
4+
name = "hello-world",
5+
srcs = ["hello-world.cc"],
6+
linkopts = [
7+
"-sAUTO_NATIVE_LIBRARIES=0",
8+
"-flto",
9+
],
10+
)
11+
12+
wasm_cc_binary(
13+
name = "hello-world-wasm",
14+
cc_target = ":hello-world",
15+
outputs = [
16+
"hello-world.js",
17+
"hello-world.wasm",
18+
],
19+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bazel_dep(name = "platforms", version = "0.0.9")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
local_repository(
2+
name = "emsdk",
3+
path = "..",
4+
)
5+
6+
load("@emsdk//:deps.bzl", "deps")
7+
8+
deps()
9+
10+
load("@emsdk//:emscripten_deps.bzl", "emscripten_deps")
11+
12+
emscripten_deps()
13+
14+
load("@emsdk//:toolchains.bzl", "register_emscripten_toolchains")
15+
16+
register_emscripten_toolchains(cache = {
17+
"configuration": ["--lto"],
18+
"targets": [
19+
"crtbegin",
20+
"libprintf_long_double-debug",
21+
"libstubs-debug",
22+
"libnoexit",
23+
"libc-debug",
24+
"libdlmalloc",
25+
"libcompiler_rt",
26+
"libc++-noexcept",
27+
"libc++abi-debug-noexcept",
28+
"libsockets"
29+
]
30+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <iostream>
2+
3+
int main(int argc, char** argv) {
4+
std::cout << "hello world!" << std::endl;
5+
return 0;
6+
}

0 commit comments

Comments
 (0)