Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions extensions/wasm_bindgen/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ use_repo(

register_toolchains(
"//:default_wasm_bindgen_toolchain",
"@rules_rust//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain",
"@rules_rust//rust/private/dummy_cc_toolchain:dummy_cc_wasm64_toolchain",
)

bazel_dep(name = "bazel_ci_rules", version = "1.0.0", dev_dependency = True)
Expand Down
2 changes: 1 addition & 1 deletion extensions/wasm_bindgen/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ load(
_rust_wasm_bindgen_toolchain = "rust_wasm_bindgen_toolchain",
)
load(
"//private:wasm_bindgen_test.bzl",
"//private:wasm_bindgen_test_wrapper.bzl",
_rust_wasm_bindgen_test = "rust_wasm_bindgen_test",
)

Expand Down
5 changes: 3 additions & 2 deletions extensions/wasm_bindgen/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ bzl_library(
"transitions.bzl",
"wasm_bindgen.bzl",
"wasm_bindgen_test.bzl",
"wasm_bindgen_test_wrapper.bzl",
],
visibility = ["//:__pkg__"],
deps = [
Expand All @@ -15,8 +16,8 @@ bzl_library(
)

rust_binary(
name = "wasm_bindgen_test_wrapper",
srcs = ["wasm_bindgen_test_wrapper.rs"],
name = "wasm_bindgen_test_runner",
srcs = ["wasm_bindgen_test_runner.rs"],
edition = "2021",
visibility = ["//visibility:public"],
deps = [
Expand Down
9 changes: 9 additions & 0 deletions extensions/wasm_bindgen/private/transitions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ wasm_bindgen_transition = transition(
inputs = [],
outputs = ["//command_line_option:platforms"],
)

def _opt_transition(_settings, _attr):
return {"//command_line_option:compilation_mode": "opt"}

opt_transition = transition(
implementation = _opt_transition,
inputs = [],
outputs = ["//command_line_option:compilation_mode"],
)
2 changes: 1 addition & 1 deletion extensions/wasm_bindgen/private/wasm_bindgen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ WASM_BINDGEN_ATTR = {
mandatory = True,
),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
default = Label("@bazel_tools//tools/allowlists/function_transition_allowlist"),
),
}

Expand Down
175 changes: 106 additions & 69 deletions extensions/wasm_bindgen/private/wasm_bindgen_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ load(
"transform_sources",
)
load("//:providers.bzl", "RustWasmBindgenInfo")
load("//private:transitions.bzl", "wasm_bindgen_transition")
load("//private:transitions.bzl", "opt_transition", "wasm_bindgen_transition")

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

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

def _rust_wasm_bindgen_test_impl(ctx):
def _rust_wasm_bindgen_test_binary_impl(ctx):
wb_toolchain = ctx.toolchains[Label("//:toolchain_type")]
if not wb_toolchain.webdriver:
fail("The currently registered wasm_bindgen_toolchain does not have a webdriver assigned. Tests are unavailable without one.")

toolchain = find_toolchain(ctx)

crate_type = "bin"
Expand Down Expand Up @@ -142,62 +139,12 @@ def _rust_wasm_bindgen_test_impl(ctx):
rust_flags = get_rust_test_flags(ctx.attr),
skip_expanding_rustc_env = True,
)
data = getattr(ctx.attr, "data", [])

env = expand_dict_value_locations(
ctx,
getattr(ctx.attr, "env", {}),
data,
{},
)

components = "{}/{}".format(ctx.label.workspace_root, ctx.label.package).split("/")
env["CARGO_MANIFEST_DIR"] = "/".join([c for c in components if c])

wrapper = ctx.actions.declare_file(ctx.label.name)
ctx.actions.symlink(
output = wrapper,
target_file = ctx.executable._wrapper,
is_executable = True,
)

if wb_toolchain.browser:
env["BROWSER"] = _rlocationpath(wb_toolchain.browser, ctx.workspace_name)

env["BROWSER_TYPE"] = wb_toolchain.browser_type
env["WEBDRIVER"] = _rlocationpath(wb_toolchain.webdriver, ctx.workspace_name)
env["WEBDRIVER_ARGS"] = " ".join(wb_toolchain.webdriver_args)
env["WEBDRIVER_JSON"] = _rlocationpath(wb_toolchain.webdriver_json, ctx.workspace_name)
env["WASM_BINDGEN_TEST_RUNNER"] = _rlocationpath(wb_toolchain.wasm_bindgen_test_runner, ctx.workspace_name)

# Force the use of a browser for now as there is no node integration.
env["WASM_BINDGEN_USE_BROWSER"] = "1"

providers = []
return crate_providers

for prov in crate_providers:
if type(prov) == "DefaultInfo":
files = prov.files.to_list()
if len(files) != 1:
fail("Unexpected number of output files for `{}`: {}".format(ctx.label, files))
wasm_file = files[0]
env["TEST_WASM_BINARY"] = _rlocationpath(files[0], ctx.workspace_name)
providers.append(DefaultInfo(
files = prov.files,
runfiles = prov.default_runfiles.merge(ctx.runfiles(files = [wasm_file], transitive_files = wb_toolchain.all_test_files)),
executable = wrapper,
))
else:
providers.append(prov)

providers.append(testing.TestEnvironment(env))

return providers

rust_wasm_bindgen_test = rule(
rust_wasm_bindgen_test_binary = rule(
doc = "Rules for running [wasm-bindgen tests](https://rustwasm.github.io/wasm-bindgen/wasm-bindgen-test/index.html).",
implementation = _rust_wasm_bindgen_test_impl,
cfg = wasm_bindgen_transition,
implementation = _rust_wasm_bindgen_test_binary_impl,
attrs = {
"aliases": attr.label_keyed_string_dict(
doc = """\
Expand Down Expand Up @@ -305,11 +252,6 @@ rust_wasm_bindgen_test = rule(
file of arguments to rustc: `@$(location //package:target)`.
""",
),
"target_arch": attr.string(
doc = "The target architecture to use for the wasm-bindgen command line option.",
default = "wasm32",
values = ["wasm32", "wasm64"],
),
"version": attr.string(
doc = "A version to inject in the cargo environment variable.",
default = "0.0.0",
Expand All @@ -320,18 +262,113 @@ rust_wasm_bindgen_test = rule(
providers = [RustWasmBindgenInfo],
mandatory = True,
),
"_wrapper": attr.label(
doc = "The process wrapper for wasm-bindgen-test-runner.",
cfg = "exec",
executable = True,
default = Label("//private:wasm_bindgen_test_wrapper"),
),
} | RUSTC_ATTRS,
fragments = ["cpp"],
toolchains = [
str(Label("//:toolchain_type")),
"@rules_rust//rust:toolchain_type",
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
],
executable = True,
)

def _rust_wasm_bindgen_test_impl(ctx):
wb_toolchain = ctx.toolchains[Label("//:toolchain_type")]
if not wb_toolchain.webdriver:
fail("The currently registered wasm_bindgen_toolchain does not have a webdriver assigned. Tests are unavailable without one.")

data = getattr(ctx.attr, "data", [])

env = expand_dict_value_locations(
ctx,
getattr(ctx.attr, "env", {}),
data,
{},
)

components = "{}/{}".format(ctx.label.workspace_root, ctx.label.package).split("/")
env["CARGO_MANIFEST_DIR"] = "/".join([c for c in components if c])

wrapper = ctx.actions.declare_file(ctx.label.name)
ctx.actions.symlink(
output = wrapper,
target_file = ctx.executable._runner,
is_executable = True,
)

if wb_toolchain.browser:
env["BROWSER"] = _rlocationpath(wb_toolchain.browser, ctx.workspace_name)

env["BROWSER_TYPE"] = wb_toolchain.browser_type
env["WEBDRIVER"] = _rlocationpath(wb_toolchain.webdriver, ctx.workspace_name)
env["WEBDRIVER_ARGS"] = " ".join(wb_toolchain.webdriver_args)
env["WEBDRIVER_JSON"] = _rlocationpath(wb_toolchain.webdriver_json, ctx.workspace_name)
env["WASM_BINDGEN_TEST_RUNNER"] = _rlocationpath(wb_toolchain.wasm_bindgen_test_runner, ctx.workspace_name)

# Force the use of a browser for now as there is no node integration.
env["WASM_BINDGEN_USE_BROWSER"] = "1"

wasm_file = ctx.executable.wasm

env["TEST_WASM_BINARY"] = _rlocationpath(wasm_file, ctx.workspace_name)
return [
DefaultInfo(
files = ctx.attr.wasm[0][DefaultInfo].files,
runfiles = ctx.attr.wasm[0][DefaultInfo].default_runfiles.merge(ctx.runfiles(files = [wasm_file], transitive_files = wb_toolchain.all_test_files)),
executable = wrapper,
),
RunEnvironmentInfo(
environment = env,
inherited_environment = ctx.attr.env_inherit,
),
]

rust_wasm_bindgen_test = rule(
doc = "A test rule for running [wasm-bindgen tests](https://rustwasm.github.io/wasm-bindgen/wasm-bindgen-test/index.html).",
implementation = _rust_wasm_bindgen_test_impl,
attrs = {
"data": attr.label_list(
doc = """\
List of files used by this rule at compile time and runtime.

If including data at compile time with include_str!() and similar,
prefer `compile_data` over `data`, to prevent the data also being included
in the runfiles.
""",
allow_files = True,
),
"env": attr.string_dict(
mandatory = False,
doc = """\
Specifies additional environment variables to set when the test is executed by bazel test.
Values are subject to `$(rootpath)`, `$(execpath)`, location, and
["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution.
""",
),
"env_inherit": attr.string_list(
doc = "Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test.",
),
"target_arch": attr.string(
doc = "The target architecture to use for the wasm-bindgen command line option.",
default = "wasm32",
values = ["wasm32", "wasm64"],
),
"wasm": attr.label(
doc = "The wasm target to test.",
executable = True,
cfg = wasm_bindgen_transition,
mandatory = True,
),
"_runner": attr.label(
doc = "The process wrapper for wasm-bindgen-test-runner.",
# Try to get as close to `exec` as possible.
cfg = opt_transition,
executable = True,
default = Label("//private:wasm_bindgen_test_runner"),
),
},
toolchains = [
str(Label("//:toolchain_type")),
],
test = True,
)
88 changes: 88 additions & 0 deletions extensions/wasm_bindgen/private/wasm_bindgen_test_wrapper.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""wasm_bindgen_test_wrapper"""

load(
":wasm_bindgen_test.bzl",
"rust_wasm_bindgen_test_binary",
_rust_wasm_bindgen_test = "rust_wasm_bindgen_test",
)

def rust_wasm_bindgen_test(
*,
name,
aliases = {},
compile_data = [],
crate_features = [],
data = [],
edition = None,
env = {},
env_inherit = [],
proc_macro_deps = [],
rustc_env = {},
rustc_env_files = [],
rustc_flags = [],
target_arch = None,
version = "0.0.0",
wasm = None,
tags = [],
**kwargs):
""""A test rule for running [wasm-bindgen tests](https://rustwasm.github.io/wasm-bindgen/wasm-bindgen-test/index.html)."

Args:
name (str): A unique name for this target.
aliases (dict, optional): Remap crates to a new name or moniker for linkage to this target.
compile_data (list, optional): List of files used by this rule at compile time.
crate_features (list, optional): List of features to enable for this crate.
data (list, optional): List of files used by this rule at compile time and runtime.
edition (str, optional): The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain.
env (dict, optional): Specifies additional environment variables to set when the test is executed by bazel test.
env_inherit (list, optional): Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test.
proc_macro_deps (list, optional): List of `rust_proc_macro` targets used to help build this library target.
rustc_env (dict, optional): Dictionary of additional `"key": "value"` environment variables to set for rustc.
rustc_env_files (list, optional): Files containing additional environment variables to set for rustc.
rustc_flags (list, optional): List of compiler flags passed to `rustc`.
target_arch (str, optional): The target architecture to use for the wasm-bindgen command line option.
version (str, optional): A version to inject in the cargo environment variable.
wasm (Label, optional): The wasm target to test.
tags (list, optional): Tags to apply to the target.
**kwargs (dict): Additional keyword arguments.
"""
visibility = kwargs.pop("visibility", [])

# Create a test binary for `wasm-bindgen-test-runner` to invoke.
# Ideally this target would be produced within the `wasm_bindgen_test`
# rule directly but the design of that rule is to consume wasm files
# and run a test on the target environment.
rust_wasm_bindgen_test_binary(
name = name + ".bin",
aliases = aliases,
compile_data = compile_data,
crate_features = crate_features,
data = data,
edition = edition,
env = env,
env_inherit = env_inherit,
proc_macro_deps = proc_macro_deps,
rustc_env = rustc_env,
rustc_env_files = rustc_env_files,
rustc_flags = rustc_flags,
version = version,
wasm = wasm,
tags = depset(tags + ["manual"]).to_list(),
visibility = ["//visibility:private"],
target_compatible_with = select({
"@platforms//cpu:wasm32": [],
"@platforms//cpu:wasm64": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
**kwargs
)

_rust_wasm_bindgen_test(
name = name,
wasm = name + ".bin",
target_arch = target_arch,
tags = tags,
env = env,
visibility = visibility,
**kwargs
)
2 changes: 1 addition & 1 deletion extensions/wasm_bindgen/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_shared_library")
load("@rules_rust_wasm_bindgen//:defs.bzl", "rust_wasm_bindgen", "rust_wasm_bindgen_test")
load("//:defs.bzl", "rust_wasm_bindgen", "rust_wasm_bindgen_test")

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

Expand Down
Loading