diff --git a/platforms/BUILD.bazel b/platforms/BUILD.bazel index d9c7140d..d1cc84ed 100644 --- a/platforms/BUILD.bazel +++ b/platforms/BUILD.bazel @@ -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", + ], +) diff --git a/toolchain/BUILD.llvm_repo b/toolchain/BUILD.llvm_repo index 76cac99c..bbcbd1d5 100644 --- a/toolchain/BUILD.llvm_repo +++ b/toolchain/BUILD.llvm_repo @@ -41,6 +41,7 @@ filegroup( srcs = [ "bin/ld.lld", "bin/ld64.lld", + "bin/wasm-ld", ], ) diff --git a/toolchain/cc_toolchain_config.bzl b/toolchain/cc_toolchain_config.bzl index fb32d1ea..7bb55f35 100644 --- a/toolchain/cc_toolchain_config.bzl +++ b/toolchain/cc_toolchain_config.bzl @@ -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 @@ -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 diff --git a/toolchain/internal/common.bzl b/toolchain/internal/common.bzl index ff94bfb2..fe56b6a8 100644 --- a/toolchain/internal/common.bzl +++ b/toolchain/internal/common.bzl @@ -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. @@ -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 @@ -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] diff --git a/toolchain/internal/configure.bzl b/toolchain/internal/configure.bzl index 8731af75..37a0ab61 100644 --- a/toolchain/internal/configure.bzl +++ b/toolchain/internal/configure.bzl @@ -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", @@ -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, @@ -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", @@ -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")