Skip to content

Commit 8800a0f

Browse files
committed
Support WebAssembly target platforms wasm{32,64}-unknown-unknown
1 parent 9bc2e80 commit 8800a0f

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed

platforms/BUILD.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,19 @@ platform(
4545
"@platforms//cpu:aarch64",
4646
],
4747
)
48+
49+
platform(
50+
name = "wasm32",
51+
constraint_values = [
52+
"@platforms//os:none",
53+
"@platforms//cpu:wasm32",
54+
],
55+
)
56+
57+
platform(
58+
name = "wasm64",
59+
constraint_values = [
60+
"@platforms//os:none",
61+
"@platforms//cpu:wasm64",
62+
],
63+
)

toolchain/BUILD.llvm_repo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ filegroup(
4141
srcs = [
4242
"bin/ld.lld",
4343
"bin/ld64.lld",
44+
"bin/wasm-ld",
4445
],
4546
)
4647

toolchain/cc_toolchain_config.bzl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ def cc_toolchain_config(
8989
"clang",
9090
"glibc_unknown",
9191
),
92+
"wasm32": (
93+
"clang-wasm32",
94+
"wasm32",
95+
"unknown",
96+
"clang",
97+
"unknown",
98+
"unknown",
99+
),
100+
"wasm64": (
101+
"clang-wasm64",
102+
"wasm64",
103+
"unknown",
104+
"clang",
105+
"unknown",
106+
"unknown",
107+
),
92108
}[target_os_arch_key]
93109

94110
# Unfiltered compiler flags; these are placed at the end of the command
@@ -171,6 +187,10 @@ def cc_toolchain_config(
171187
archive_flags.extend([
172188
"-static",
173189
])
190+
elif target_arch in ["wasm32", "wasm64"]:
191+
# lld is invoked as wasm-ld for WebAssembly targets.
192+
use_lld = True
193+
use_libtool = False
174194
else:
175195
# Note that for xcompiling from darwin to linux, the native ld64 is
176196
# not an option because it is not a cross-linker, so lld is the

toolchain/internal/common.bzl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
SUPPORTED_TARGETS = [("linux", "x86_64"), ("linux", "aarch64"), ("darwin", "x86_64"), ("darwin", "aarch64")]
15+
SUPPORTED_TARGETS = [
16+
("linux", "x86_64"),
17+
("linux", "aarch64"),
18+
("darwin", "x86_64"),
19+
("darwin", "aarch64"),
20+
("none", "wasm32"),
21+
("none", "wasm64"),
22+
]
1623

1724
# Map of tool name to its symlinked name in the tools directory.
1825
# See tool_paths in toolchain/cc_toolchain_config.bzl.
@@ -120,7 +127,7 @@ def os(rctx):
120127

121128
def os_bzl(os):
122129
# Return the OS string as used in bazel platform constraints.
123-
return {"darwin": "osx", "linux": "linux"}[os]
130+
return {"darwin": "osx", "linux": "linux", "none": "none"}[os]
124131

125132
def arch(rctx):
126133
arch = rctx.attr.exec_arch
@@ -139,7 +146,12 @@ def arch(rctx):
139146
return "x86_64"
140147
return arch
141148

149+
def is_standalone_arch(os, arch):
150+
return os == "none" and arch in ["wasm32", "wasm64"]
151+
142152
def os_arch_pair(os, arch):
153+
if is_standalone_arch(os, arch):
154+
return arch
143155
return "{}-{}".format(os, arch)
144156

145157
_supported_os_arch = [os_arch_pair(os, arch) for (os, arch) in SUPPORTED_TARGETS]

toolchain/internal/configure.bzl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ load(
2525
_check_os_arch_keys = "check_os_arch_keys",
2626
_exec_os_arch_dict_value = "exec_os_arch_dict_value",
2727
_is_absolute_path = "is_absolute_path",
28+
_is_standalone_arch = "is_standalone_arch",
2829
_list_to_string = "list_to_string",
2930
_os = "os",
3031
_os_arch_pair = "os_arch_pair",
@@ -243,7 +244,10 @@ def _cc_toolchains_str(
243244
cc_toolchains_str = ""
244245
toolchain_names = []
245246
for (target_os, target_arch) in _supported_targets:
246-
suffix = "{}-{}".format(target_arch, target_os)
247+
if _is_standalone_arch(target_os, target_arch):
248+
suffix = target_arch
249+
else:
250+
suffix = "{}-{}".format(target_arch, target_os)
247251
cc_toolchain_str = _cc_toolchain_str(
248252
rctx,
249253
suffix,
@@ -315,6 +319,8 @@ def _cc_toolchain_str(
315319
"darwin-aarch64": "aarch64-apple-macosx",
316320
"linux-aarch64": "aarch64-unknown-linux-gnu",
317321
"linux-x86_64": "x86_64-unknown-linux-gnu",
322+
"wasm32": "wasm32-unknown-unknown",
323+
"wasm64": "wasm64-unknown-unknown",
318324
}[target_pair]
319325
cxx_builtin_include_directories = [
320326
toolchain_path_prefix + "include/c++/v1",
@@ -341,6 +347,11 @@ def _cc_toolchain_str(
341347
_join(sysroot_prefix, "/usr/include"),
342348
_join(sysroot_prefix, "/System/Library/Frameworks"),
343349
])
350+
elif target_os == "none":
351+
if sysroot_prefix:
352+
cxx_builtin_include_directories.extend([
353+
_join(sysroot_prefix, "/include"),
354+
])
344355
else:
345356
fail("Unreachable")
346357

0 commit comments

Comments
 (0)