Skip to content

[bazel] Fix and update emscripten_toolchain #1060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
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: 2 additions & 0 deletions bazel/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build:wasm --incompatible_enable_cc_toolchain_resolution
build:wasm --platforms='@emsdk//:cpu_wasm32'
54 changes: 18 additions & 36 deletions bazel/BUILD
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
package(default_visibility = ["//visibility:public"])

config_setting(
name = "linux",
platform(
name = "cpu_wasm32",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
"@platforms//cpu:wasm32",
],
)

config_setting(
name = "macos",
platform(
name = "cpu_wasm64",
constraint_values = [
"@platforms//os:macos",
"@platforms//cpu:x86_64",
],
)

config_setting(
name = "macos_arm64",
constraint_values = [
"@platforms//os:macos",
"@platforms//cpu:arm64",
],
)

config_setting(
name = "windows",
constraint_values = [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
"@platforms//cpu:wasm64",
],
)

Expand All @@ -37,32 +19,32 @@ filegroup(name = "empty")
alias(
name = "compiler_files",
actual = select({
":linux": "@emscripten_bin_linux//:compiler_files",
":macos": "@emscripten_bin_mac//:compiler_files",
":macos_arm64": "@emscripten_bin_mac_arm64//:compiler_files",
":windows": "@emscripten_bin_win//:compiler_files",
"@bazel_tools//src/conditions:linux_x86_64": "@emscripten_bin_linux//:compiler_files",
"@bazel_tools//src/conditions:darwin_x86_64": "@emscripten_bin_mac//:compiler_files",
"@bazel_tools//src/conditions:darwin_arm64": "@emscripten_bin_mac_arm64//:compiler_files",
"@bazel_tools//src/conditions:windows": "@emscripten_bin_win//:compiler_files",
"//conditions:default": ":empty",
}),
)

alias(
name = "linker_files",
actual = select({
":linux": "@emscripten_bin_linux//:linker_files",
":macos": "@emscripten_bin_mac//:linker_files",
":macos_arm64": "@emscripten_bin_mac_arm64//:linker_files",
":windows": "@emscripten_bin_win//:linker_files",
"@bazel_tools//src/conditions:linux_x86_64": "@emscripten_bin_linux//:linker_files",
"@bazel_tools//src/conditions:darwin_x86_64": "@emscripten_bin_mac//:linker_files",
"@bazel_tools//src/conditions:darwin_arm64": "@emscripten_bin_mac_arm64//:linker_files",
"@bazel_tools//src/conditions:windows": "@emscripten_bin_win//:linker_files",
"//conditions:default": ":empty",
}),
)

alias(
name = "ar_files",
actual = select({
":linux": "@emscripten_bin_linux//:ar_files",
":macos": "@emscripten_bin_mac//:ar_files",
":macos_arm64": "@emscripten_bin_mac_arm64//:ar_files",
":windows": "@emscripten_bin_win//:ar_files",
"@bazel_tools//src/conditions:linux_x86_64": "@emscripten_bin_linux//:ar_files",
"@bazel_tools//src/conditions:darwin_x86_64": "@emscripten_bin_mac//:ar_files",
"@bazel_tools//src/conditions:darwin_arm64": "@emscripten_bin_mac_arm64//:ar_files",
"@bazel_tools//src/conditions:windows": "@emscripten_bin_win//:ar_files",
"//conditions:default": ":empty",
}),
)
18 changes: 3 additions & 15 deletions bazel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ emsdk_deps()

load("@emsdk//:emscripten_deps.bzl", emsdk_emscripten_deps = "emscripten_deps")
emsdk_emscripten_deps(emscripten_version = "2.0.31")

load("@emsdk//:toolchains.bzl", "emsdk_register_toolchains")
emsdk_register_toolchains()
```
The SHA1 hash in the above `strip_prefix` and `url` parameters correspond to the git revision of
[emsdk 2.0.31](https://github.com/emscripten-core/emsdk/releases/tag/2.0.31). To get access to
Expand All @@ -26,7 +29,6 @@ parameter of `emsdk_emscripten_deps()`. Supported versions are listed in `revisi

## Building

### Using wasm_cc_binary (preferred)
First, write a new rule wrapping your `cc_binary`.

```
Expand Down Expand Up @@ -54,17 +56,3 @@ and all of its dependencies, and does not require amending `.bazelrc`. This
is the preferred way, since it also unpacks the resulting tarball.

See `test_external/` for an example using [embind](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html).

### Using --config=wasm
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does --config=wasm no longer work?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In an ideal world, --config=wasm32 and --config=wasm64 are both allowed, and --config=wasm just points to --config=wasm32 for backwards compatibility.

Copy link
Author

@jrandolf jrandolf Jul 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean by "backwards-compatibility". If we update the config here, it doesn't imply it propagates to the user. In particular, this only affects developers of this repo, not external users.


Put the following lines into your `.bazelrc`:
```
build:wasm --crosstool_top=@emsdk//emscripten_toolchain:everything
build:wasm --cpu=wasm
build:wasm --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
```

Simply pass `--config=wasm` when building a normal `cc_binary`. The result of
this build will be a tar archive containing any files produced by emscripten.
See the [Bazel documentation](https://docs.bazel.build/versions/main/tutorial/cc-toolchain-config.html)
for more details
4 changes: 4 additions & 0 deletions bazel/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ deps()
load(":emscripten_deps.bzl", "emscripten_deps")

emscripten_deps()

load(":toolchains.bzl", "emsdk_register_toolchains")

emsdk_register_toolchains()
5 changes: 0 additions & 5 deletions bazel/bazelrc

This file was deleted.

84 changes: 63 additions & 21 deletions bazel/emscripten_toolchain/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load(":crosstool.bzl", "emscripten_cc_toolchain_config_rule")
load(":crosstool.bzl", "emscripten_cc_toolchain_config")

package(default_visibility = ["//visibility:public"])

Expand All @@ -7,44 +7,53 @@ package(default_visibility = ["//visibility:public"])
# https://github.com/bazelbuild/rules_nodejs/issues/3375
node_files = "@nodejs_host//:node_files" if existing_rule("@nodejs_host//:node_files") else "@nodejs//:node_files"

filegroup(
name = "all_files",
srcs = [
":ar_files",
":compiler_files",
":linker_files",
],
)

filegroup(
name = "common_files",
srcs = [
"emscripten_config",
"env.sh",
"env.bat",
"env.sh",
node_files,
],
)

filegroup(
name = "compiler_files",
srcs = [
"emcc.sh",
"emcc.bat",
"@emsdk//:compiler_files",
"emcc.sh",
":common_files",
"@emsdk//:compiler_files",
],
)

filegroup(
name = "linker_files",
srcs = [
"emcc_link.sh",
"emcc_link.bat",
"emcc_link.sh",
"link_wrapper.py",
"@emsdk//:linker_files",
":common_files",
"@emsdk//:linker_files",
],
)

filegroup(
name = "ar_files",
srcs = [
"emar.sh",
"emar.bat",
"@emsdk//:ar_files",
"emar.sh",
":common_files",
"@emsdk//:ar_files",
],
)

Expand All @@ -53,9 +62,9 @@ filegroup(name = "empty")
# dlmalloc.bc is implictly added by the emscripten toolchain
cc_library(name = "malloc")

emscripten_cc_toolchain_config_rule(
name = "wasm",
cpu = "wasm",
emscripten_cc_toolchain_config(
name = "wasm32_cc_toolchain_config",
cpu = "wasm32",
em_config = "emscripten_config",
emscripten_binaries = "@emsdk//:compiler_files",
script_extension = select({
Expand All @@ -64,26 +73,59 @@ emscripten_cc_toolchain_config_rule(
}),
)

emscripten_cc_toolchain_config(
name = "wasm64_cc_toolchain_config",
cpu = "wasm64",
em_config = "emscripten_config",
emscripten_binaries = "@emsdk//:compiler_files",
script_extension = select({
"@bazel_tools//src/conditions:host_windows": "bat",
"//conditions:default": "sh",
}),
)

cc_toolchain(
name = "wasm32_cc_toolchain",
all_files = ":all_files",
ar_files = ":ar_files",
compiler_files = ":compiler_files",
dwp_files = ":empty",
linker_files = ":linker_files",
objcopy_files = ":empty",
strip_files = ":empty",
toolchain_config = ":wasm32_cc_toolchain_config",
toolchain_identifier = "emscripten-wasm",
)

cc_toolchain(
name = "cc-compiler-wasm",
all_files = ":empty",
name = "wasm64_cc_toolchain",
all_files = ":all_files",
ar_files = ":ar_files",
as_files = ":empty",
compiler_files = ":compiler_files",
dwp_files = ":empty",
linker_files = ":linker_files",
objcopy_files = ":empty",
strip_files = ":empty",
toolchain_config = "wasm",
toolchain_config = ":wasm64_cc_toolchain_config",
toolchain_identifier = "emscripten-wasm",
)

cc_toolchain_suite(
name = "everything",
toolchains = {
"wasm": ":cc-compiler-wasm",
"wasm|emscripten": ":cc-compiler-wasm",
},
toolchain(
name = "cc_wasm32_toolchain",
target_compatible_with = [
"@platforms//cpu:wasm32",
],
toolchain = ":wasm32_cc_toolchain",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

toolchain(
name = "cc_wasm64_toolchain",
target_compatible_with = [
"@platforms//cpu:wasm64",
],
toolchain = ":wasm64_cc_toolchain",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

py_binary(
Expand Down
28 changes: 11 additions & 17 deletions bazel/emscripten_toolchain/crosstool.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,18 @@ CROSSTOOL_DEFAULT_WARNINGS = [
"-Wall",
]

def _impl(ctx):
def _emscripten_cc_toolchain_config(ctx):
target_cpu = ctx.attr.cpu
toolchain_identifier = "emscripten-" + target_cpu
target_system_name = target_cpu + "-unknown-emscripten"

host_system_name = "i686-unknown-linux-gnu"

target_libc = "musl/js"

abi_version = "emscripten_syscalls"

compiler = "emscripten"
abi_libc_version = "default"

cc_target_os = "emscripten"

emscripten_dir = ctx.attr.emscripten_binaries.label.workspace_root

builtin_sysroot = emscripten_dir + "/emscripten/cache/sysroot"
Expand Down Expand Up @@ -150,8 +146,9 @@ def _impl(ctx):
flag_sets = [
flag_set(
flag_groups = [
flag_group(flags = ["rcsD"]),
flag_group(
flags = ["rcsD", "%{output_execpath}"],
flags = ["%{output_execpath}"],
expand_if_available = "output_execpath",
),
],
Expand Down Expand Up @@ -323,11 +320,11 @@ def _impl(ctx):
implies = ["crosstool_cpu_" + target_cpu],
),
feature(
name = "crosstool_cpu_asmjs",
name = "crosstool_cpu_wasm64",
provides = ["variant:crosstool_cpu"],
),
feature(
name = "crosstool_cpu_wasm",
name = "crosstool_cpu_wasm32",
provides = ["variant:crosstool_cpu"],
),

Expand Down Expand Up @@ -409,7 +406,6 @@ def _impl(ctx):
feature(name = "emcc_debug_link"),
feature(
name = "llvm_backend",
requires = [feature_set(features = ["crosstool_cpu_wasm"])],
enabled = True,
),

Expand Down Expand Up @@ -523,8 +519,8 @@ def _impl(ctx):
# Emscripten-specific settings:
flag_set(
actions = all_compile_actions + all_link_actions,
flags = ["-s", "WASM=0"],
features = ["crosstool_cpu_asmjs"],
flags = ["-s", "MEMORY64=1"],
features = ["crosstool_cpu_wasm64"],
),
flag_set(
actions = all_compile_actions +
Expand Down Expand Up @@ -1087,7 +1083,6 @@ def _impl(ctx):
artifact_name_patterns = artifact_name_patterns,
cxx_builtin_include_directories = cxx_builtin_include_directories,
toolchain_identifier = toolchain_identifier,
host_system_name = host_system_name,
target_system_name = target_system_name,
target_cpu = target_cpu,
target_libc = target_libc,
Expand All @@ -1097,15 +1092,14 @@ def _impl(ctx):
tool_paths = tool_paths,
make_variables = make_variables,
builtin_sysroot = builtin_sysroot,
cc_target_os = cc_target_os,
)

emscripten_cc_toolchain_config_rule = rule(
implementation = _impl,
emscripten_cc_toolchain_config = rule(
implementation = _emscripten_cc_toolchain_config,
attrs = {
"cpu": attr.string(mandatory = True, values = ["asmjs", "wasm"]),
"cpu": attr.string(mandatory = True, values = ["wasm32", "wasm64"]),
"em_config": attr.label(mandatory = True, allow_single_file = True),
"emscripten_binaries": attr.label(mandatory = True),
"emscripten_binaries": attr.label(mandatory = True, cfg = "exec"),
"script_extension": attr.string(mandatory = True, values = ["sh", "bat"]),
},
provides = [CcToolchainConfigInfo],
Expand Down
Loading