Skip to content

Commit 8b4b19b

Browse files
committed
Switch to platforms-based toolchain resolution
Fixes #984. However, this requires `--incompatible_enable_cc_toolchain_resolution` and no longer supports `--crosstool_top=...`. I'm not sure how to support both at the same time.
1 parent e23aac7 commit 8b4b19b

File tree

11 files changed

+32
-29
lines changed

11 files changed

+32
-29
lines changed

bazel/.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build --incompatible_enable_cc_toolchain_resolution

bazel/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,10 @@ alias(
5555
"//conditions:default": ":empty",
5656
}),
5757
)
58+
59+
platform(
60+
name = "platform_wasm",
61+
constraint_values = [
62+
"@platforms//cpu:wasm32",
63+
],
64+
)

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", "register_emscripten_toolchains")
22+
register_emscripten_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", "register_emscripten_toolchains")
12+
13+
register_emscripten_toolchains()

bazel/bazelrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

bazel/emscripten_toolchain/BUILD.bazel

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load(":crosstool.bzl", "emscripten_cc_toolchain_config_rule")
1+
load(":toolchain.bzl", "emscripten_cc_toolchain_config_rule")
22

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

@@ -88,6 +88,13 @@ cc_toolchain_suite(
8888
},
8989
)
9090

91+
toolchain(
92+
name = "cc-toolchain-wasm",
93+
target_compatible_with = ["@platforms//cpu:wasm32"],
94+
toolchain = ":cc-compiler-wasm",
95+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
96+
)
97+
9198
py_binary(
9299
name = "wasm_binary",
93100
srcs = ["wasm_binary.py"],

bazel/emscripten_toolchain/crosstool.bzl renamed to bazel/emscripten_toolchain/toolchain.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ emscripten_cc_toolchain_config_rule = rule(
11051105
attrs = {
11061106
"cpu": attr.string(mandatory = True, values = ["asmjs", "wasm"]),
11071107
"em_config": attr.label(mandatory = True, allow_single_file = True),
1108-
"emscripten_binaries": attr.label(mandatory = True),
1108+
"emscripten_binaries": attr.label(mandatory = True, cfg = "exec"),
11091109
"script_extension": attr.string(mandatory = True, values = ["sh", "bat"]),
11101110
},
11111111
provides = [CcToolchainConfigInfo],

bazel/emscripten_toolchain/wasm_cc_binary.bzl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@ def _wasm_transition_impl(settings, attr):
2626
features.append("wasm_simd")
2727

2828
return {
29-
"//command_line_option:compiler": "emscripten",
30-
"//command_line_option:crosstool_top": "@emsdk//emscripten_toolchain:everything",
31-
"//command_line_option:cpu": "wasm",
3229
"//command_line_option:features": features,
3330
"//command_line_option:dynamic_mode": "off",
3431
"//command_line_option:linkopt": linkopts,
35-
"//command_line_option:platforms": [],
32+
"//command_line_option:platforms": ["@emsdk//:platform_wasm"],
3633
"//command_line_option:custom_malloc": "@emsdk//emscripten_toolchain:malloc",
3734
}
3835

@@ -43,9 +40,6 @@ _wasm_transition = transition(
4340
"//command_line_option:linkopt",
4441
],
4542
outputs = [
46-
"//command_line_option:compiler",
47-
"//command_line_option:cpu",
48-
"//command_line_option:crosstool_top",
4943
"//command_line_option:features",
5044
"//command_line_option:dynamic_mode",
5145
"//command_line_option:linkopt",

bazel/test_external/.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build --incompatible_enable_cc_toolchain_resolution

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", "register_emscripten_toolchains")
15+
16+
register_emscripten_toolchains()

bazel/toolchains.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def register_emscripten_toolchains():
2+
native.register_toolchains(str(Label("//emscripten_toolchain:cc-toolchain-wasm")))

0 commit comments

Comments
 (0)