Skip to content

Commit

Permalink
Support WebAssembly target platforms wasm{32,64}-unknown-unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
jmillikin committed Oct 17, 2024
1 parent 672a6a1 commit 879fe7e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
16 changes: 16 additions & 0 deletions platforms/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,19 @@ platform(
"@platforms//cpu:aarch64",
],
)

platform(
name = "wasm32",
constraint_values = [
"@platforms//os:none",
"@platforms//cpu:wasm32",
],
)

platform(
name = "wasm64",
constraint_values = [
"@platforms//os:none",
"@platforms//cpu:wasm64",
],
)
1 change: 1 addition & 0 deletions toolchain/BUILD.llvm_repo
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ filegroup(
srcs = [
"bin/ld.lld",
"bin/ld64.lld",
"bin/wasm-ld",
],
)

Expand Down
20 changes: 20 additions & 0 deletions toolchain/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ def cc_toolchain_config(
"clang",
"glibc_unknown",
),
"wasm32": (
"clang-wasm32",
"wasm32",
"unknown",
"clang",
"unknown",
"unknown",
),
"wasm64": (
"clang-wasm64",
"wasm64",
"unknown",
"clang",
"unknown",
"unknown",
),
}[target_os_arch_key]

# Unfiltered compiler flags; these are placed at the end of the command
Expand Down Expand Up @@ -171,6 +187,10 @@ def cc_toolchain_config(
archive_flags.extend([
"-static",
])
elif target_arch in ["wasm32", "wasm64"]:
# lld is invoked as wasm-ld for WebAssembly targets.
use_lld = True
use_libtool = False
else:
# Note that for xcompiling from darwin to linux, the native ld64 is
# not an option because it is not a cross-linker, so lld is the
Expand Down
16 changes: 14 additions & 2 deletions toolchain/internal/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

SUPPORTED_TARGETS = [("linux", "x86_64"), ("linux", "aarch64"), ("darwin", "x86_64"), ("darwin", "aarch64")]
SUPPORTED_TARGETS = [
("linux", "x86_64"),
("linux", "aarch64"),
("darwin", "x86_64"),
("darwin", "aarch64"),
("none", "wasm32"),
("none", "wasm64"),
]

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

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

def arch(rctx):
arch = rctx.attr.exec_arch
Expand All @@ -139,7 +146,12 @@ def arch(rctx):
return "x86_64"
return arch

def is_standalone_arch(os, arch):
return os == "none" and arch in ["wasm32", "wasm64"]

def os_arch_pair(os, arch):
if is_standalone_arch(os, arch):
return arch
return "{}-{}".format(os, arch)

_supported_os_arch = [os_arch_pair(os, arch) for (os, arch) in SUPPORTED_TARGETS]
Expand Down
13 changes: 12 additions & 1 deletion toolchain/internal/configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ load(
_check_os_arch_keys = "check_os_arch_keys",
_exec_os_arch_dict_value = "exec_os_arch_dict_value",
_is_absolute_path = "is_absolute_path",
_is_standalone_arch = "is_standalone_arch",
_list_to_string = "list_to_string",
_os = "os",
_os_arch_pair = "os_arch_pair",
Expand Down Expand Up @@ -243,7 +244,10 @@ def _cc_toolchains_str(
cc_toolchains_str = ""
toolchain_names = []
for (target_os, target_arch) in _supported_targets:
suffix = "{}-{}".format(target_arch, target_os)
if _is_standalone_arch(target_os, target_arch):
suffix = target_arch
else:
suffix = "{}-{}".format(target_arch, target_os)
cc_toolchain_str = _cc_toolchain_str(
rctx,
suffix,
Expand Down Expand Up @@ -315,6 +319,8 @@ def _cc_toolchain_str(
"darwin-aarch64": "aarch64-apple-macosx",
"linux-aarch64": "aarch64-unknown-linux-gnu",
"linux-x86_64": "x86_64-unknown-linux-gnu",
"wasm32": "wasm32-unknown-unknown",
"wasm64": "wasm64-unknown-unknown",
}[target_pair]
cxx_builtin_include_directories = [
toolchain_path_prefix + "include/c++/v1",
Expand All @@ -341,6 +347,11 @@ def _cc_toolchain_str(
_join(sysroot_prefix, "/usr/include"),
_join(sysroot_prefix, "/System/Library/Frameworks"),
])
elif target_os == "none":
if sysroot_prefix:
cxx_builtin_include_directories.extend([
_join(sysroot_prefix, "/include"),
])
else:
fail("Unreachable")

Expand Down

0 comments on commit 879fe7e

Please sign in to comment.