Skip to content

Commit f3f4cc6

Browse files
committed
[bazel] Fix and update emscripten_toolchain
* Removes `asm` CPU target. * [feat] Add `wasm32` and `wasm64` CPU targets. * Fix static library linker flags. * Use the new `platform` API for toolchains * Add a `emsdk_register_toolchains` for Platform toolchains.
1 parent 03cec6f commit f3f4cc6

File tree

9 files changed

+104
-57
lines changed

9 files changed

+104
-57
lines changed

bazel/.bazelrc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
build:wasm --crosstool_top=//emscripten_toolchain:everything
2-
build:wasm --cpu=wasm
3-
build:wasm --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
1+
build:wasm32 --incompatible_enable_cc_toolchain_resolution

bazel/BUILD

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package(default_visibility = ["//visibility:public"])
22

3+
platform(
4+
name = "cpu_wasm32",
5+
constraint_values = [
6+
"@platforms//cpu:wasm32",
7+
],
8+
)
9+
10+
platform(
11+
name = "cpu_wasm64",
12+
constraint_values = [
13+
"@platforms//cpu:wasm64",
14+
],
15+
)
16+
317
filegroup(name = "empty")
418

519
alias(

bazel/README.md

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ emsdk_deps()
1717
1818
load("@emsdk//:emscripten_deps.bzl", emsdk_emscripten_deps = "emscripten_deps")
1919
emsdk_emscripten_deps(emscripten_version = "2.0.31")
20+
21+
load("@emsdk//:toolchains.bzl", "emsdk_register_toolchains")
22+
emsdk_register_toolchains()
2023
```
2124
The SHA1 hash in the above `strip_prefix` and `url` parameters correspond to the git revision of
2225
[emsdk 2.0.31](https://github.com/emscripten-core/emsdk/releases/tag/2.0.31). To get access to
@@ -26,7 +29,6 @@ parameter of `emsdk_emscripten_deps()`. Supported versions are listed in `revisi
2629

2730
## Building
2831

29-
### Using wasm_cc_binary (preferred)
3032
First, write a new rule wrapping your `cc_binary`.
3133

3234
```
@@ -54,17 +56,3 @@ and all of its dependencies, and does not require amending `.bazelrc`. This
5456
is the preferred way, since it also unpacks the resulting tarball.
5557

5658
See `test_external/` for an example using [embind](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html).
57-
58-
### Using --config=wasm
59-
60-
Put the following lines into your `.bazelrc`:
61-
```
62-
build:wasm --crosstool_top=@emsdk//emscripten_toolchain:everything
63-
build:wasm --cpu=wasm
64-
build:wasm --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
65-
```
66-
67-
Simply pass `--config=wasm` when building a normal `cc_binary`. The result of
68-
this build will be a tar archive containing any files produced by emscripten.
69-
See the [Bazel documentation](https://docs.bazel.build/versions/main/tutorial/cc-toolchain-config.html)
70-
for more details

bazel/WORKSPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ deps()
77
load(":emscripten_deps.bzl", "emscripten_deps")
88

99
emscripten_deps()
10+
11+
load(":toolchains.bzl", "emsdk_register_toolchains")
12+
13+
emsdk_register_toolchains()

bazel/emscripten_toolchain/BUILD.bazel

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load(":crosstool.bzl", "emscripten_cc_toolchain_config_rule")
1+
load(":crosstool.bzl", "emscripten_cc_toolchain_config")
22

33
package(default_visibility = ["//visibility:public"])
44

@@ -7,44 +7,53 @@ package(default_visibility = ["//visibility:public"])
77
# https://github.com/bazelbuild/rules_nodejs/issues/3375
88
node_files = "@nodejs_host//:node_files" if existing_rule("@nodejs_host//:node_files") else "@nodejs//:node_files"
99

10+
filegroup(
11+
name = "all_files",
12+
srcs = [
13+
":ar_files",
14+
":compiler_files",
15+
":linker_files",
16+
],
17+
)
18+
1019
filegroup(
1120
name = "common_files",
1221
srcs = [
1322
"emscripten_config",
14-
"env.sh",
1523
"env.bat",
24+
"env.sh",
1625
node_files,
1726
],
1827
)
1928

2029
filegroup(
2130
name = "compiler_files",
2231
srcs = [
23-
"emcc.sh",
2432
"emcc.bat",
25-
"@emsdk//:compiler_files",
33+
"emcc.sh",
2634
":common_files",
35+
"@emsdk//:compiler_files",
2736
],
2837
)
2938

3039
filegroup(
3140
name = "linker_files",
3241
srcs = [
33-
"emcc_link.sh",
3442
"emcc_link.bat",
43+
"emcc_link.sh",
3544
"link_wrapper.py",
36-
"@emsdk//:linker_files",
3745
":common_files",
46+
"@emsdk//:linker_files",
3847
],
3948
)
4049

4150
filegroup(
4251
name = "ar_files",
4352
srcs = [
44-
"emar.sh",
4553
"emar.bat",
46-
"@emsdk//:ar_files",
54+
"emar.sh",
4755
":common_files",
56+
"@emsdk//:ar_files",
4857
],
4958
)
5059

@@ -53,9 +62,9 @@ filegroup(name = "empty")
5362
# dlmalloc.bc is implictly added by the emscripten toolchain
5463
cc_library(name = "malloc")
5564

56-
emscripten_cc_toolchain_config_rule(
57-
name = "wasm",
58-
cpu = "wasm",
65+
emscripten_cc_toolchain_config(
66+
name = "wasm32_cc_toolchain_config",
67+
cpu = "wasm32",
5968
em_config = "emscripten_config",
6069
emscripten_binaries = "@emsdk//:compiler_files",
6170
script_extension = select({
@@ -64,26 +73,59 @@ emscripten_cc_toolchain_config_rule(
6473
}),
6574
)
6675

76+
emscripten_cc_toolchain_config(
77+
name = "wasm64_cc_toolchain_config",
78+
cpu = "wasm64",
79+
em_config = "emscripten_config",
80+
emscripten_binaries = "@emsdk//:compiler_files",
81+
script_extension = select({
82+
"@bazel_tools//src/conditions:host_windows": "bat",
83+
"//conditions:default": "sh",
84+
}),
85+
)
86+
87+
cc_toolchain(
88+
name = "wasm32_cc_toolchain",
89+
all_files = ":all_files",
90+
ar_files = ":ar_files",
91+
compiler_files = ":compiler_files",
92+
dwp_files = ":empty",
93+
linker_files = ":linker_files",
94+
objcopy_files = ":empty",
95+
strip_files = ":empty",
96+
toolchain_config = ":wasm32_cc_toolchain_config",
97+
toolchain_identifier = "emscripten-wasm",
98+
)
99+
67100
cc_toolchain(
68-
name = "cc-compiler-wasm",
69-
all_files = ":empty",
101+
name = "wasm64_cc_toolchain",
102+
all_files = ":all_files",
70103
ar_files = ":ar_files",
71-
as_files = ":empty",
72104
compiler_files = ":compiler_files",
73105
dwp_files = ":empty",
74106
linker_files = ":linker_files",
75107
objcopy_files = ":empty",
76108
strip_files = ":empty",
77-
toolchain_config = "wasm",
109+
toolchain_config = ":wasm64_cc_toolchain_config",
78110
toolchain_identifier = "emscripten-wasm",
79111
)
80112

81-
cc_toolchain_suite(
82-
name = "everything",
83-
toolchains = {
84-
"wasm": ":cc-compiler-wasm",
85-
"wasm|emscripten": ":cc-compiler-wasm",
86-
},
113+
toolchain(
114+
name = "cc_wasm32_toolchain",
115+
target_compatible_with = [
116+
"@platforms//cpu:wasm32",
117+
],
118+
toolchain = ":wasm32_cc_toolchain",
119+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
120+
)
121+
122+
toolchain(
123+
name = "cc_wasm64_toolchain",
124+
target_compatible_with = [
125+
"@platforms//cpu:wasm64",
126+
],
127+
toolchain = ":wasm64_cc_toolchain",
128+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
87129
)
88130

89131
py_binary(

bazel/emscripten_toolchain/crosstool.bzl

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,18 @@ CROSSTOOL_DEFAULT_WARNINGS = [
5454
"-Wall",
5555
]
5656

57-
def _impl(ctx):
57+
def _emscripten_cc_toolchain_config(ctx):
5858
target_cpu = ctx.attr.cpu
5959
toolchain_identifier = "emscripten-" + target_cpu
6060
target_system_name = target_cpu + "-unknown-emscripten"
6161

62-
host_system_name = "i686-unknown-linux-gnu"
63-
6462
target_libc = "musl/js"
6563

6664
abi_version = "emscripten_syscalls"
6765

6866
compiler = "emscripten"
6967
abi_libc_version = "default"
7068

71-
cc_target_os = "emscripten"
72-
7369
emscripten_dir = ctx.attr.emscripten_binaries.label.workspace_root
7470

7571
builtin_sysroot = emscripten_dir + "/emscripten/cache/sysroot"
@@ -150,8 +146,9 @@ def _impl(ctx):
150146
flag_sets = [
151147
flag_set(
152148
flag_groups = [
149+
flag_group(flags = ["rcsD"]),
153150
flag_group(
154-
flags = ["rcsD", "%{output_execpath}"],
151+
flags = ["%{output_execpath}"],
155152
expand_if_available = "output_execpath",
156153
),
157154
],
@@ -323,11 +320,11 @@ def _impl(ctx):
323320
implies = ["crosstool_cpu_" + target_cpu],
324321
),
325322
feature(
326-
name = "crosstool_cpu_asmjs",
323+
name = "crosstool_cpu_wasm64",
327324
provides = ["variant:crosstool_cpu"],
328325
),
329326
feature(
330-
name = "crosstool_cpu_wasm",
327+
name = "crosstool_cpu_wasm32",
331328
provides = ["variant:crosstool_cpu"],
332329
),
333330

@@ -409,7 +406,6 @@ def _impl(ctx):
409406
feature(name = "emcc_debug_link"),
410407
feature(
411408
name = "llvm_backend",
412-
requires = [feature_set(features = ["crosstool_cpu_wasm"])],
413409
enabled = True,
414410
),
415411

@@ -523,8 +519,8 @@ def _impl(ctx):
523519
# Emscripten-specific settings:
524520
flag_set(
525521
actions = all_compile_actions + all_link_actions,
526-
flags = ["-s", "WASM=0"],
527-
features = ["crosstool_cpu_asmjs"],
522+
flags = ["-s", "MEMORY64=1"],
523+
features = ["crosstool_cpu_wasm64"],
528524
),
529525
flag_set(
530526
actions = all_compile_actions +
@@ -1087,7 +1083,6 @@ def _impl(ctx):
10871083
artifact_name_patterns = artifact_name_patterns,
10881084
cxx_builtin_include_directories = cxx_builtin_include_directories,
10891085
toolchain_identifier = toolchain_identifier,
1090-
host_system_name = host_system_name,
10911086
target_system_name = target_system_name,
10921087
target_cpu = target_cpu,
10931088
target_libc = target_libc,
@@ -1097,15 +1092,14 @@ def _impl(ctx):
10971092
tool_paths = tool_paths,
10981093
make_variables = make_variables,
10991094
builtin_sysroot = builtin_sysroot,
1100-
cc_target_os = cc_target_os,
11011095
)
11021096

1103-
emscripten_cc_toolchain_config_rule = rule(
1104-
implementation = _impl,
1097+
emscripten_cc_toolchain_config = rule(
1098+
implementation = _emscripten_cc_toolchain_config,
11051099
attrs = {
1106-
"cpu": attr.string(mandatory = True, values = ["asmjs", "wasm"]),
1100+
"cpu": attr.string(mandatory = True, values = ["wasm32", "wasm64"]),
11071101
"em_config": attr.label(mandatory = True, allow_single_file = True),
1108-
"emscripten_binaries": attr.label(mandatory = True),
1102+
"emscripten_binaries": attr.label(mandatory = True, cfg = "exec"),
11091103
"script_extension": attr.string(mandatory = True, values = ["sh", "bat"]),
11101104
},
11111105
provides = [CcToolchainConfigInfo],

bazel/emscripten_toolchain/wasm_cc_binary.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def _wasm_transition_impl(settings, attr):
3232
"//command_line_option:features": features,
3333
"//command_line_option:dynamic_mode": "off",
3434
"//command_line_option:linkopt": linkopts,
35-
"//command_line_option:platforms": [],
35+
"//command_line_option:platforms": ["@emsdk//:cpu_wasm32", "@emsdk//:cpu_wasm64"],
3636
"//command_line_option:custom_malloc": "@emsdk//emscripten_toolchain:malloc",
3737
}
3838

bazel/test_external/WORKSPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ deps()
1010
load("@emsdk//:emscripten_deps.bzl", "emscripten_deps")
1111

1212
emscripten_deps()
13+
14+
load("@emsdk//:toolchains.bzl", "emsdk_register_toolchains")
15+
16+
emsdk_register_toolchains()

bazel/toolchains.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def emsdk_register_toolchains():
2+
native.register_toolchains("//emscripten_toolchain:cc_wasm32_toolchain")
3+
native.register_toolchains("//emscripten_toolchain:cc_wasm64_toolchain")

0 commit comments

Comments
 (0)