Skip to content

Commit beebf3b

Browse files
Add wasm64 support. (#2866)
This PR adds support for `wasm64-unknown-unknown` although there are no prebuilt `wasm64-unknown-unknown` rustc toolchains. Compiling for `wasm64` will be left up to anyone using this target triple until we get T2 support for wasm64. --------- Co-authored-by: Andre Brisco <andre.brisco@freeform.co>
1 parent f2244a5 commit beebf3b

File tree

13 files changed

+66
-20
lines changed

13 files changed

+66
-20
lines changed

docs/src/flatten.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpu
12341234
## rust_wasm_bindgen
12351235

12361236
<pre>
1237-
rust_wasm_bindgen(<a href="#rust_wasm_bindgen-name">name</a>, <a href="#rust_wasm_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#rust_wasm_bindgen-target">target</a>, <a href="#rust_wasm_bindgen-wasm_file">wasm_file</a>)
1237+
rust_wasm_bindgen(<a href="#rust_wasm_bindgen-name">name</a>, <a href="#rust_wasm_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#rust_wasm_bindgen-target">target</a>, <a href="#rust_wasm_bindgen-target_arch">target_arch</a>, <a href="#rust_wasm_bindgen-wasm_file">wasm_file</a>)
12381238
</pre>
12391239

12401240
Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws].
@@ -1251,6 +1251,7 @@ An example of this rule in use can be seen at [@rules_rust//examples/wasm](../ex
12511251
| <a id="rust_wasm_bindgen-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
12521252
| <a id="rust_wasm_bindgen-bindgen_flags"></a>bindgen_flags | Flags to pass directly to the bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details. | List of strings | optional | `[]` |
12531253
| <a id="rust_wasm_bindgen-target"></a>target | The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details. | String | optional | `"bundler"` |
1254+
| <a id="rust_wasm_bindgen-target_arch"></a>target_arch | The target architecture to use for the wasm-bindgen command line option. | String | optional | `"wasm32"` |
12541255
| <a id="rust_wasm_bindgen-wasm_file"></a>wasm_file | The `.wasm` file or crate to generate bindings for. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
12551256

12561257

docs/src/rust_wasm_bindgen.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ directory contains a `defs.bzl` file that defines the different variants of
5151
## rust_wasm_bindgen
5252

5353
<pre>
54-
rust_wasm_bindgen(<a href="#rust_wasm_bindgen-name">name</a>, <a href="#rust_wasm_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#rust_wasm_bindgen-target">target</a>, <a href="#rust_wasm_bindgen-wasm_file">wasm_file</a>)
54+
rust_wasm_bindgen(<a href="#rust_wasm_bindgen-name">name</a>, <a href="#rust_wasm_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#rust_wasm_bindgen-target">target</a>, <a href="#rust_wasm_bindgen-target_arch">target_arch</a>, <a href="#rust_wasm_bindgen-wasm_file">wasm_file</a>)
5555
</pre>
5656

5757
Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws].
@@ -68,6 +68,7 @@ An example of this rule in use can be seen at [@rules_rust//examples/wasm](../ex
6868
| <a id="rust_wasm_bindgen-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
6969
| <a id="rust_wasm_bindgen-bindgen_flags"></a>bindgen_flags | Flags to pass directly to the bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details. | List of strings | optional | `[]` |
7070
| <a id="rust_wasm_bindgen-target"></a>target | The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details. | String | optional | `"bundler"` |
71+
| <a id="rust_wasm_bindgen-target_arch"></a>target_arch | The target architecture to use for the wasm-bindgen command line option. | String | optional | `"wasm32"` |
7172
| <a id="rust_wasm_bindgen-wasm_file"></a>wasm_file | The `.wasm` file or crate to generate bindings for. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
7273

7374

rust/platform/platform.bzl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,28 @@ def declare_config_settings():
6767
)
6868

6969
native.platform(
70-
name = "wasm",
70+
name = "wasm32",
7171
constraint_values = [
7272
"@platforms//cpu:wasm32",
7373
str(Label("//rust/platform/os:unknown")),
7474
],
7575
)
7676

77+
# Add alias for wasm to maintain backwards compatibility.
78+
native.alias(
79+
name = "wasm",
80+
actual = ":wasm32",
81+
deprecation = "Use `@rules_rust//rust/platform:wasm32` instead",
82+
)
83+
84+
native.platform(
85+
name = "wasm64",
86+
constraint_values = [
87+
"@platforms//cpu:wasm64",
88+
str(Label("//rust/platform/os:unknown")),
89+
],
90+
)
91+
7792
native.platform(
7893
name = "wasi",
7994
constraint_values = [

rust/platform/triple_mappings.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ SUPPORTED_T2_PLATFORM_TRIPLES = [
4949
"x86_64-unknown-none",
5050
]
5151

52+
# Note that only platforms with `std` artifacts should be added
53+
# to this list: https://doc.rust-lang.org/nightly/rustc/platform-support.html#tier-3
5254
SUPPORTED_T3_PLATFORM_TRIPLES = [
5355
"aarch64-unknown-nto-qnx710",
5456
]
@@ -82,6 +84,7 @@ _CPU_ARCH_TO_BUILTIN_PLAT_SUFFIX = {
8284
"thumbv7m": "armv7-m",
8385
"thumbv8m.main": "armv8-m",
8486
"wasm32": None,
87+
"wasm64": None,
8588
"x86_64": "x86_64",
8689
}
8790

@@ -357,6 +360,11 @@ def triple_to_constraint_set(target_triple):
357360
"@platforms//cpu:wasm32",
358361
"@platforms//os:none",
359362
]
363+
if target_triple == "wasm64-unknown-unknown":
364+
return [
365+
"@platforms//cpu:wasm64",
366+
"@platforms//os:none",
367+
]
360368

361369
triple_struct = triple(target_triple)
362370

rust/private/dummy_cc_toolchain/BUILD.bazel

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1+
# When compiling Rust code for wasm (wasm32 or wasm64), we avoid linking to cpp code so we introduce a dummy cc
2+
# toolchain since we know we'll never look it up.
3+
#
4+
# TODO(jedmonds@spotify.com): Need to support linking C code to rust code when compiling for wasm32 or wasm64.
5+
16
load("@rules_cc//cc:defs.bzl", "cc_toolchain")
27
load(":dummy_cc_toolchain.bzl", "dummy_cc_config", "dummy_cc_toolchain")
38

4-
dummy_cc_toolchain(name = "dummy_cc_wasm32")
9+
dummy_cc_toolchain(name = "dummy_cc_wasm")
510

6-
# When compiling Rust code for wasm32, we avoid linking to cpp code so we introduce a dummy cc
7-
# toolchain since we know we'll never look it up.
8-
# TODO(jedmonds@spotify.com): Need to support linking C code to rust code when compiling for wasm32.
911
toolchain(
1012
name = "dummy_cc_wasm32_toolchain",
11-
target_compatible_with = ["//rust/platform/cpu:wasm32"],
12-
toolchain = ":dummy_cc_wasm32_toolchain_cc",
13+
target_compatible_with = ["@platforms//cpu:wasm32"],
14+
toolchain = ":dummy_cc_wasm_toolchain_cc",
15+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
16+
)
17+
18+
toolchain(
19+
name = "dummy_cc_wasm64_toolchain",
20+
target_compatible_with = ["@platforms//cpu:wasm64"],
21+
toolchain = ":dummy_cc_wasm_toolchain_cc",
1322
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
1423
)
1524

1625
cc_toolchain(
17-
name = "dummy_cc_wasm32_toolchain_cc",
26+
name = "dummy_cc_wasm_toolchain_cc",
1827
all_files = ":empty",
1928
compiler_files = ":empty",
2029
dwp_files = ":empty",
@@ -23,7 +32,7 @@ cc_toolchain(
2332
strip_files = ":empty",
2433
supports_param_files = 0,
2534
toolchain_config = ":cc_toolchain_config",
26-
toolchain_identifier = "dummy_wasm32_cc",
35+
toolchain_identifier = "dummy_wasm_cc",
2736
)
2837

2938
dummy_cc_config(

rust/private/dummy_cc_toolchain/dummy_cc_toolchain.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dummy_cc_toolchain = rule(
1313
def _config_impl(ctx):
1414
return cc_common.create_cc_toolchain_config_info(
1515
ctx = ctx,
16-
toolchain_identifier = "dummy-wasm32-cc-toolchain",
16+
toolchain_identifier = "dummy-wasm-cc-toolchain",
1717
host_system_name = "unknown",
1818
target_system_name = "unknown",
1919
target_cpu = "unknown",

rust/private/rustc.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ def construct_arguments(
10101010
# Rust's built-in linker can handle linking wasm files. We don't want to attempt to use the cc
10111011
# linker since it won't understand.
10121012
compilation_mode = ctx.var["COMPILATION_MODE"]
1013-
if toolchain.target_arch != "wasm32":
1013+
if toolchain.target_arch not in ("wasm32", "wasm64"):
10141014
if output_dir:
10151015
use_pic = _should_use_pic(cc_toolchain, feature_configuration, crate_info.type, compilation_mode)
10161016
rpaths = _compute_rpaths(toolchain, output_dir, dep_info, use_pic)

rust/private/utils.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ def determine_lib_name(name, crate_type, toolchain, lib_hash = None):
788788
prefix = "lib"
789789
if toolchain.target_triple and toolchain.target_os == "windows" and crate_type not in ("lib", "rlib"):
790790
prefix = ""
791-
if toolchain.target_arch == "wasm32" and crate_type == "cdylib":
791+
if toolchain.target_arch in ("wasm32", "wasm64") and crate_type == "cdylib":
792792
prefix = ""
793793

794794
return "{prefix}{name}{lib_hash}{extension}".format(

rust/repositories.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,3 +1069,4 @@ def rust_repository_set(
10691069
if register_toolchain:
10701070
native.register_toolchains(*all_toolchain_names)
10711071
native.register_toolchains(str(Label("//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain")))
1072+
native.register_toolchains(str(Label("//rust/private/dummy_cc_toolchain:dummy_cc_wasm64_toolchain")))

util/fetch_shas/fetch_shas.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def main() -> None:
273273

274274
file_key_to_sha = {}
275275

276-
retries = []
276+
artifacts = []
277277

278278
logging.info("Parsing artifacts...")
279279
for channel, versioned_info in manifest_data.items():
@@ -340,7 +340,7 @@ def main() -> None:
340340
template = tool_template.replace("{target}", target)
341341

342342
# See if we can download the file directly.
343-
retries.extend(
343+
artifacts.extend(
344344
[
345345
template.format(pkg=pkg_name, ext="tar.gz"),
346346
template.format(pkg=pkg_name, ext="tar.xz"),
@@ -352,7 +352,7 @@ def main() -> None:
352352
# Do a brute force check to find additional sha256 values.
353353
file_key_to_sha.update(
354354
download_direct_sha256s(
355-
artifacts=sorted(set(retries)),
355+
artifacts=sorted(set(artifacts)),
356356
output_dir=tmp_dir / "retries",
357357
)
358358
)

util/fetch_shas/fetch_shas_TARGETS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ thumbv8m.main-none-eabihf
7171
wasm32-unknown-emscripten
7272
wasm32-unknown-unknown
7373
wasm32-wasi
74+
wasm64-unknown-unknown
7475
x86_64-apple-darwin
7576
x86_64-apple-ios
7677
x86_64-fortanix-unknown-sgx

wasm_bindgen/private/transitions.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
"""Transition implementations for wasm-bindgen rust Rules"""
22

3-
def _wasm_bindgen_transition(_settings, _attr):
3+
def _wasm_bindgen_transition(_settings, attr):
44
"""The implementation of the `wasm_bindgen_transition` transition
55
66
Args:
77
_settings (dict): A dict {String:Object} of all settings declared
88
in the inputs parameter to `transition()`
9-
_attr (dict): A dict of attributes and values of the rule to which
9+
attr (dict): A dict of attributes and values of the rule to which
1010
the transition is attached
1111
1212
Returns:
1313
dict: A dict of new build settings values to apply
1414
"""
15-
return {"//command_line_option:platforms": str(Label("//rust/platform:wasm"))}
15+
return {"//command_line_option:platforms": str(Label("//rust/platform:{}".format(attr.target_arch)))}
1616

1717
wasm_bindgen_transition = transition(
1818
implementation = _wasm_bindgen_transition,

wasm_bindgen/private/wasm_bindgen.bzl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ WASM_BINDGEN_ATTR = {
113113
default = "bundler",
114114
values = ["web", "bundler", "nodejs", "no-modules", "deno"],
115115
),
116+
"target_arch": attr.string(
117+
doc = "The target architecture to use for the wasm-bindgen command line option.",
118+
default = "wasm32",
119+
values = ["wasm32", "wasm64"],
120+
),
116121
"wasm_file": attr.label(
117122
doc = "The `.wasm` file or crate to generate bindings for.",
118123
allow_single_file = True,
@@ -142,6 +147,11 @@ An example of this rule in use can be seen at [@rules_rust//examples/wasm](../ex
142147
default = "bundler",
143148
values = ["web", "bundler", "nodejs", "no-modules", "deno"],
144149
),
150+
"target_arch": attr.string(
151+
doc = "The target architecture to use for the wasm-bindgen command line option.",
152+
default = "wasm32",
153+
values = ["wasm32", "wasm64"],
154+
),
145155
"wasm_file": attr.label(
146156
doc = "The `.wasm` file or crate to generate bindings for.",
147157
allow_single_file = True,

0 commit comments

Comments
 (0)