Skip to content

Commit 592171e

Browse files
committed
Implement emscripten_cache
1 parent ca7b40a commit 592171e

File tree

6 files changed

+79
-2
lines changed

6 files changed

+79
-2
lines changed

bazel/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ emsdk_deps()
1818
load("@emsdk//:emscripten_deps.bzl", emsdk_emscripten_deps = "emscripten_deps")
1919
emsdk_emscripten_deps(emscripten_version = "2.0.31")
2020

21+
load("@emsdk//:emscripten_cache.bzl", emsdk_emscripten_cache = "emscripten_cache")
22+
emsdk_emscripten_cache()
23+
2124
load("@emsdk//:toolchains.bzl", "register_emscripten_toolchains")
2225
register_emscripten_toolchains()
2326
```
@@ -66,4 +69,14 @@ rules.
6669
and all of its dependencies, and does not require amending `.bazelrc`. This
6770
is the preferred way, since it also unpacks the resulting tarball.
6871

72+
The Emscripten cache shipped by default does not include LTO, 64-bit or PIC
73+
builds of the system libraries and ports. If you wish to use these features you
74+
will need to create a secondary cache as follows. Note that the flags are the
75+
same flags that can be passed to embuilder.
76+
77+
```starlark
78+
load("@emsdk//:emscripten_cache.bzl", emsdk_emscripten_cache = "emscripten_cache")
79+
emsdk_emscripten_cache(flags = "--lto")
80+
```
81+
6982
See `test_external/` for an example using [embind](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html).

bazel/WORKSPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ load(":emscripten_deps.bzl", "emscripten_deps")
88

99
emscripten_deps()
1010

11+
load(":emscripten_cache.bzl", "emscripten_cache")
12+
13+
emscripten_cache()
14+
1115
load(":toolchains.bzl", "register_emscripten_toolchains")
1216

1317
register_emscripten_toolchains()

bazel/emscripten_cache.bzl

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
BUILD_FILE_CONTENT_TEMPLATE = """
2+
package(default_visibility = ['//visibility:public'])
3+
exports_files(['emscripten_config'])
4+
"""
5+
6+
def _emscripten_cache_impl(repository_ctx):
7+
# Read the default emscripten configuration file
8+
default_config = repository_ctx.read(
9+
repository_ctx.path(
10+
Label("@emsdk//emscripten_toolchain:default_config")
11+
)
12+
)
13+
14+
# TODO I need a cross platform way to get embuilder and bin/node
15+
if repository_ctx.attr.libraries or repository_ctx.attr.flags:
16+
# Get paths to tools (toolchain is not yet setup, so we cannot use emscripten_config)
17+
embuilder_path = repository_ctx.path(Label("@emscripten_bin_linux//:emscripten/embuilder.py"))
18+
binaryen_root = embuilder_path.dirname.dirname
19+
llvm_root = binaryen_root.get_child("bin")
20+
nodejs = repository_ctx.path(Label("@nodejs//:node_files")).dirname.get_child('bin/node')
21+
# Create configuration file
22+
embuilder_config_content = "LLVM_ROOT = '{}'\n".format(llvm_root)
23+
embuilder_config_content += "NODE_JS = '{}'\n".format(nodejs)
24+
embuilder_config_content += "BINARYEN_ROOT = '{}'\n".format(binaryen_root)
25+
embuilder_config_content += "CACHE = 'cache'\n"
26+
repository_ctx.file('embuilder_config', embuilder_config_content)
27+
embuilder_config_path = repository_ctx.path('embuilder_config')
28+
# Prepare the command line
29+
if repository_ctx.attr.libraries:
30+
libraries = repository_ctx.attr.libraries
31+
else:
32+
# if no libraries are requested, build everything
33+
libraries = ["ALL"]
34+
flags = ["--em-config", embuilder_config_path] + repository_ctx.attr.flags
35+
embuilder_args = [embuilder_path] + flags + ["build"] + libraries
36+
# Run embuilder
37+
repository_ctx.report_progress("Building secondary cache")
38+
repository_ctx.execute(embuilder_args, quiet=False)
39+
# Override Emscripten's cache with the secondary cache
40+
default_config += "CACHE = '{}'\n".format(repository_ctx.path('cache'))
41+
# Create the configuration file for the toolchain and export
42+
repository_ctx.file('emscripten_config', default_config)
43+
repository_ctx.file('BUILD.bazel', BUILD_FILE_CONTENT_TEMPLATE)
44+
45+
_emscripten_cache = repository_rule(
46+
implementation = _emscripten_cache_impl,
47+
attrs = {
48+
"flags": attr.string_list(),
49+
"libraries": attr.string_list(),
50+
},
51+
local = True
52+
)
53+
54+
def emscripten_cache(flags = [], libraries = []):
55+
_emscripten_cache(
56+
name = "emscripten_cache",
57+
flags = flags,
58+
libraries = libraries,
59+
)

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",

0 commit comments

Comments
 (0)