Skip to content

Commit a60584b

Browse files
authored
Delete "dummy" wasm cc_toolchain (#3892)
After #3665 there should no longer be a need to register a fake cc_toolchain to target wasm platforms. closes #1601
1 parent ec3e912 commit a60584b

17 files changed

Lines changed: 263 additions & 218 deletions

File tree

extensions/wasm_bindgen/MODULE.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ use_repo(
5454

5555
register_toolchains(
5656
"//:default_wasm_bindgen_toolchain",
57-
"@rules_rust//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain",
58-
"@rules_rust//rust/private/dummy_cc_toolchain:dummy_cc_wasm64_toolchain",
5957
)
6058

6159
bazel_dep(name = "bazel_ci_rules", version = "1.0.0", dev_dependency = True)

extensions/wasm_bindgen/defs.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ load(
4545
_rust_wasm_bindgen_toolchain = "rust_wasm_bindgen_toolchain",
4646
)
4747
load(
48-
"//private:wasm_bindgen_test.bzl",
48+
"//private:wasm_bindgen_test_wrapper.bzl",
4949
_rust_wasm_bindgen_test = "rust_wasm_bindgen_test",
5050
)
5151

extensions/wasm_bindgen/private/BUILD.bazel

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ bzl_library(
77
"transitions.bzl",
88
"wasm_bindgen.bzl",
99
"wasm_bindgen_test.bzl",
10+
"wasm_bindgen_test_wrapper.bzl",
1011
],
1112
visibility = ["//:__pkg__"],
1213
deps = [
@@ -15,8 +16,8 @@ bzl_library(
1516
)
1617

1718
rust_binary(
18-
name = "wasm_bindgen_test_wrapper",
19-
srcs = ["wasm_bindgen_test_wrapper.rs"],
19+
name = "wasm_bindgen_test_runner",
20+
srcs = ["wasm_bindgen_test_runner.rs"],
2021
edition = "2021",
2122
visibility = ["//visibility:public"],
2223
deps = [

extensions/wasm_bindgen/private/transitions.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@ wasm_bindgen_transition = transition(
1919
inputs = [],
2020
outputs = ["//command_line_option:platforms"],
2121
)
22+
23+
def _opt_transition(_settings, _attr):
24+
return {"//command_line_option:compilation_mode": "opt"}
25+
26+
opt_transition = transition(
27+
implementation = _opt_transition,
28+
inputs = [],
29+
outputs = ["//command_line_option:compilation_mode"],
30+
)

extensions/wasm_bindgen/private/wasm_bindgen.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ WASM_BINDGEN_ATTR = {
176176
mandatory = True,
177177
),
178178
"_allowlist_function_transition": attr.label(
179-
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
179+
default = Label("@bazel_tools//tools/allowlists/function_transition_allowlist"),
180180
),
181181
}
182182

extensions/wasm_bindgen/private/wasm_bindgen_test.bzl

Lines changed: 106 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ load(
2020
"transform_sources",
2121
)
2222
load("//:providers.bzl", "RustWasmBindgenInfo")
23-
load("//private:transitions.bzl", "wasm_bindgen_transition")
23+
load("//private:transitions.bzl", "opt_transition", "wasm_bindgen_transition")
2424

2525
WasmBindgenTestCrateInfo = provider(
2626
doc = "A provider encompassing the crate from a `rust_wasm_bindgen` target.",
@@ -56,11 +56,8 @@ def _rlocationpath(file, workspace_name):
5656

5757
return "{}/{}".format(workspace_name, file.short_path)
5858

59-
def _rust_wasm_bindgen_test_impl(ctx):
59+
def _rust_wasm_bindgen_test_binary_impl(ctx):
6060
wb_toolchain = ctx.toolchains[Label("//:toolchain_type")]
61-
if not wb_toolchain.webdriver:
62-
fail("The currently registered wasm_bindgen_toolchain does not have a webdriver assigned. Tests are unavailable without one.")
63-
6461
toolchain = find_toolchain(ctx)
6562

6663
crate_type = "bin"
@@ -142,62 +139,12 @@ def _rust_wasm_bindgen_test_impl(ctx):
142139
rust_flags = get_rust_test_flags(ctx.attr),
143140
skip_expanding_rustc_env = True,
144141
)
145-
data = getattr(ctx.attr, "data", [])
146-
147-
env = expand_dict_value_locations(
148-
ctx,
149-
getattr(ctx.attr, "env", {}),
150-
data,
151-
{},
152-
)
153-
154-
components = "{}/{}".format(ctx.label.workspace_root, ctx.label.package).split("/")
155-
env["CARGO_MANIFEST_DIR"] = "/".join([c for c in components if c])
156-
157-
wrapper = ctx.actions.declare_file(ctx.label.name)
158-
ctx.actions.symlink(
159-
output = wrapper,
160-
target_file = ctx.executable._wrapper,
161-
is_executable = True,
162-
)
163-
164-
if wb_toolchain.browser:
165-
env["BROWSER"] = _rlocationpath(wb_toolchain.browser, ctx.workspace_name)
166-
167-
env["BROWSER_TYPE"] = wb_toolchain.browser_type
168-
env["WEBDRIVER"] = _rlocationpath(wb_toolchain.webdriver, ctx.workspace_name)
169-
env["WEBDRIVER_ARGS"] = " ".join(wb_toolchain.webdriver_args)
170-
env["WEBDRIVER_JSON"] = _rlocationpath(wb_toolchain.webdriver_json, ctx.workspace_name)
171-
env["WASM_BINDGEN_TEST_RUNNER"] = _rlocationpath(wb_toolchain.wasm_bindgen_test_runner, ctx.workspace_name)
172-
173-
# Force the use of a browser for now as there is no node integration.
174-
env["WASM_BINDGEN_USE_BROWSER"] = "1"
175142

176-
providers = []
143+
return crate_providers
177144

178-
for prov in crate_providers:
179-
if type(prov) == "DefaultInfo":
180-
files = prov.files.to_list()
181-
if len(files) != 1:
182-
fail("Unexpected number of output files for `{}`: {}".format(ctx.label, files))
183-
wasm_file = files[0]
184-
env["TEST_WASM_BINARY"] = _rlocationpath(files[0], ctx.workspace_name)
185-
providers.append(DefaultInfo(
186-
files = prov.files,
187-
runfiles = prov.default_runfiles.merge(ctx.runfiles(files = [wasm_file], transitive_files = wb_toolchain.all_test_files)),
188-
executable = wrapper,
189-
))
190-
else:
191-
providers.append(prov)
192-
193-
providers.append(testing.TestEnvironment(env))
194-
195-
return providers
196-
197-
rust_wasm_bindgen_test = rule(
145+
rust_wasm_bindgen_test_binary = rule(
198146
doc = "Rules for running [wasm-bindgen tests](https://rustwasm.github.io/wasm-bindgen/wasm-bindgen-test/index.html).",
199-
implementation = _rust_wasm_bindgen_test_impl,
200-
cfg = wasm_bindgen_transition,
147+
implementation = _rust_wasm_bindgen_test_binary_impl,
201148
attrs = {
202149
"aliases": attr.label_keyed_string_dict(
203150
doc = """\
@@ -305,11 +252,6 @@ rust_wasm_bindgen_test = rule(
305252
file of arguments to rustc: `@$(location //package:target)`.
306253
""",
307254
),
308-
"target_arch": attr.string(
309-
doc = "The target architecture to use for the wasm-bindgen command line option.",
310-
default = "wasm32",
311-
values = ["wasm32", "wasm64"],
312-
),
313255
"version": attr.string(
314256
doc = "A version to inject in the cargo environment variable.",
315257
default = "0.0.0",
@@ -320,18 +262,113 @@ rust_wasm_bindgen_test = rule(
320262
providers = [RustWasmBindgenInfo],
321263
mandatory = True,
322264
),
323-
"_wrapper": attr.label(
324-
doc = "The process wrapper for wasm-bindgen-test-runner.",
325-
cfg = "exec",
326-
executable = True,
327-
default = Label("//private:wasm_bindgen_test_wrapper"),
328-
),
329265
} | RUSTC_ATTRS,
330266
fragments = ["cpp"],
331267
toolchains = [
332268
str(Label("//:toolchain_type")),
333269
"@rules_rust//rust:toolchain_type",
334270
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
335271
],
272+
executable = True,
273+
)
274+
275+
def _rust_wasm_bindgen_test_impl(ctx):
276+
wb_toolchain = ctx.toolchains[Label("//:toolchain_type")]
277+
if not wb_toolchain.webdriver:
278+
fail("The currently registered wasm_bindgen_toolchain does not have a webdriver assigned. Tests are unavailable without one.")
279+
280+
data = getattr(ctx.attr, "data", [])
281+
282+
env = expand_dict_value_locations(
283+
ctx,
284+
getattr(ctx.attr, "env", {}),
285+
data,
286+
{},
287+
)
288+
289+
components = "{}/{}".format(ctx.label.workspace_root, ctx.label.package).split("/")
290+
env["CARGO_MANIFEST_DIR"] = "/".join([c for c in components if c])
291+
292+
wrapper = ctx.actions.declare_file(ctx.label.name)
293+
ctx.actions.symlink(
294+
output = wrapper,
295+
target_file = ctx.executable._runner,
296+
is_executable = True,
297+
)
298+
299+
if wb_toolchain.browser:
300+
env["BROWSER"] = _rlocationpath(wb_toolchain.browser, ctx.workspace_name)
301+
302+
env["BROWSER_TYPE"] = wb_toolchain.browser_type
303+
env["WEBDRIVER"] = _rlocationpath(wb_toolchain.webdriver, ctx.workspace_name)
304+
env["WEBDRIVER_ARGS"] = " ".join(wb_toolchain.webdriver_args)
305+
env["WEBDRIVER_JSON"] = _rlocationpath(wb_toolchain.webdriver_json, ctx.workspace_name)
306+
env["WASM_BINDGEN_TEST_RUNNER"] = _rlocationpath(wb_toolchain.wasm_bindgen_test_runner, ctx.workspace_name)
307+
308+
# Force the use of a browser for now as there is no node integration.
309+
env["WASM_BINDGEN_USE_BROWSER"] = "1"
310+
311+
wasm_file = ctx.executable.wasm
312+
313+
env["TEST_WASM_BINARY"] = _rlocationpath(wasm_file, ctx.workspace_name)
314+
return [
315+
DefaultInfo(
316+
files = ctx.attr.wasm[0][DefaultInfo].files,
317+
runfiles = ctx.attr.wasm[0][DefaultInfo].default_runfiles.merge(ctx.runfiles(files = [wasm_file], transitive_files = wb_toolchain.all_test_files)),
318+
executable = wrapper,
319+
),
320+
RunEnvironmentInfo(
321+
environment = env,
322+
inherited_environment = ctx.attr.env_inherit,
323+
),
324+
]
325+
326+
rust_wasm_bindgen_test = rule(
327+
doc = "A test rule for running [wasm-bindgen tests](https://rustwasm.github.io/wasm-bindgen/wasm-bindgen-test/index.html).",
328+
implementation = _rust_wasm_bindgen_test_impl,
329+
attrs = {
330+
"data": attr.label_list(
331+
doc = """\
332+
List of files used by this rule at compile time and runtime.
333+
334+
If including data at compile time with include_str!() and similar,
335+
prefer `compile_data` over `data`, to prevent the data also being included
336+
in the runfiles.
337+
""",
338+
allow_files = True,
339+
),
340+
"env": attr.string_dict(
341+
mandatory = False,
342+
doc = """\
343+
Specifies additional environment variables to set when the test is executed by bazel test.
344+
Values are subject to `$(rootpath)`, `$(execpath)`, location, and
345+
["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution.
346+
""",
347+
),
348+
"env_inherit": attr.string_list(
349+
doc = "Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test.",
350+
),
351+
"target_arch": attr.string(
352+
doc = "The target architecture to use for the wasm-bindgen command line option.",
353+
default = "wasm32",
354+
values = ["wasm32", "wasm64"],
355+
),
356+
"wasm": attr.label(
357+
doc = "The wasm target to test.",
358+
executable = True,
359+
cfg = wasm_bindgen_transition,
360+
mandatory = True,
361+
),
362+
"_runner": attr.label(
363+
doc = "The process wrapper for wasm-bindgen-test-runner.",
364+
# Try to get as close to `exec` as possible.
365+
cfg = opt_transition,
366+
executable = True,
367+
default = Label("//private:wasm_bindgen_test_runner"),
368+
),
369+
},
370+
toolchains = [
371+
str(Label("//:toolchain_type")),
372+
],
336373
test = True,
337374
)

extensions/wasm_bindgen/private/wasm_bindgen_test_wrapper.rs renamed to extensions/wasm_bindgen/private/wasm_bindgen_test_runner.rs

File renamed without changes.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""wasm_bindgen_test_wrapper"""
2+
3+
load(
4+
":wasm_bindgen_test.bzl",
5+
"rust_wasm_bindgen_test_binary",
6+
_rust_wasm_bindgen_test = "rust_wasm_bindgen_test",
7+
)
8+
9+
def rust_wasm_bindgen_test(
10+
*,
11+
name,
12+
aliases = {},
13+
compile_data = [],
14+
crate_features = [],
15+
data = [],
16+
edition = None,
17+
env = {},
18+
env_inherit = [],
19+
proc_macro_deps = [],
20+
rustc_env = {},
21+
rustc_env_files = [],
22+
rustc_flags = [],
23+
target_arch = None,
24+
version = "0.0.0",
25+
wasm = None,
26+
tags = [],
27+
**kwargs):
28+
""""A test rule for running [wasm-bindgen tests](https://rustwasm.github.io/wasm-bindgen/wasm-bindgen-test/index.html)."
29+
30+
Args:
31+
name (str): A unique name for this target.
32+
aliases (dict, optional): Remap crates to a new name or moniker for linkage to this target.
33+
compile_data (list, optional): List of files used by this rule at compile time.
34+
crate_features (list, optional): List of features to enable for this crate.
35+
data (list, optional): List of files used by this rule at compile time and runtime.
36+
edition (str, optional): The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain.
37+
env (dict, optional): Specifies additional environment variables to set when the test is executed by bazel test.
38+
env_inherit (list, optional): Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test.
39+
proc_macro_deps (list, optional): List of `rust_proc_macro` targets used to help build this library target.
40+
rustc_env (dict, optional): Dictionary of additional `"key": "value"` environment variables to set for rustc.
41+
rustc_env_files (list, optional): Files containing additional environment variables to set for rustc.
42+
rustc_flags (list, optional): List of compiler flags passed to `rustc`.
43+
target_arch (str, optional): The target architecture to use for the wasm-bindgen command line option.
44+
version (str, optional): A version to inject in the cargo environment variable.
45+
wasm (Label, optional): The wasm target to test.
46+
tags (list, optional): Tags to apply to the target.
47+
**kwargs (dict): Additional keyword arguments.
48+
"""
49+
visibility = kwargs.pop("visibility", [])
50+
51+
# Create a test binary for `wasm-bindgen-test-runner` to invoke.
52+
# Ideally this target would be produced within the `wasm_bindgen_test`
53+
# rule directly but the design of that rule is to consume wasm files
54+
# and run a test on the target environment.
55+
rust_wasm_bindgen_test_binary(
56+
name = name + ".bin",
57+
aliases = aliases,
58+
compile_data = compile_data,
59+
crate_features = crate_features,
60+
data = data,
61+
edition = edition,
62+
env = env,
63+
env_inherit = env_inherit,
64+
proc_macro_deps = proc_macro_deps,
65+
rustc_env = rustc_env,
66+
rustc_env_files = rustc_env_files,
67+
rustc_flags = rustc_flags,
68+
version = version,
69+
wasm = wasm,
70+
tags = depset(tags + ["manual"]).to_list(),
71+
visibility = ["//visibility:private"],
72+
target_compatible_with = select({
73+
"@platforms//cpu:wasm32": [],
74+
"@platforms//cpu:wasm64": [],
75+
"//conditions:default": ["@platforms//:incompatible"],
76+
}),
77+
**kwargs
78+
)
79+
80+
_rust_wasm_bindgen_test(
81+
name = name,
82+
wasm = name + ".bin",
83+
target_arch = target_arch,
84+
tags = tags,
85+
env = env,
86+
visibility = visibility,
87+
**kwargs
88+
)

extensions/wasm_bindgen/test/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_shared_library")
16-
load("@rules_rust_wasm_bindgen//:defs.bzl", "rust_wasm_bindgen", "rust_wasm_bindgen_test")
16+
load("//:defs.bzl", "rust_wasm_bindgen", "rust_wasm_bindgen_test")
1717

1818
package(default_visibility = ["//:__subpackages__"])
1919

0 commit comments

Comments
 (0)