From 0d1da4174276a06c2180c0aea7e0ba5f9a3decbf Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 14 Apr 2022 14:40:33 -0700 Subject: [PATCH 1/4] Switch to platforms-based toolchain resolution Fixes #984. However, this requires `--incompatible_enable_cc_toolchain_resolution` and no longer supports `--crosstool_top=...`. I'm not sure how to support both at the same time. --- bazel/.bazelrc | 1 + bazel/BUILD | 7 +++++++ bazel/README.md | 18 +++--------------- bazel/WORKSPACE | 4 ++++ bazel/bazelrc | 5 ----- bazel/emscripten_toolchain/BUILD.bazel | 9 ++++++++- .../{crosstool.bzl => toolchain.bzl} | 2 +- bazel/emscripten_toolchain/wasm_cc_binary.bzl | 8 +------- bazel/test_external/.bazelrc | 1 + bazel/test_external/WORKSPACE | 4 ++++ bazel/toolchains.bzl | 2 ++ 11 files changed, 32 insertions(+), 29 deletions(-) create mode 100644 bazel/.bazelrc delete mode 100644 bazel/bazelrc rename bazel/emscripten_toolchain/{crosstool.bzl => toolchain.bzl} (99%) create mode 100644 bazel/test_external/.bazelrc create mode 100644 bazel/toolchains.bzl diff --git a/bazel/.bazelrc b/bazel/.bazelrc new file mode 100644 index 0000000000..fbd75a7ea7 --- /dev/null +++ b/bazel/.bazelrc @@ -0,0 +1 @@ +build --incompatible_enable_cc_toolchain_resolution diff --git a/bazel/BUILD b/bazel/BUILD index 87fe18133b..7a13c92db7 100644 --- a/bazel/BUILD +++ b/bazel/BUILD @@ -66,3 +66,10 @@ alias( "//conditions:default": ":empty", }), ) + +platform( + name = "platform_wasm", + constraint_values = [ + "@platforms//cpu:wasm32", + ], +) diff --git a/bazel/README.md b/bazel/README.md index 88654757e3..5e854dcbcb 100644 --- a/bazel/README.md +++ b/bazel/README.md @@ -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", "register_emscripten_toolchains") +register_emscripten_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 @@ -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`. ``` @@ -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 - -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 diff --git a/bazel/WORKSPACE b/bazel/WORKSPACE index a33ee12abd..bd5cce5b0e 100644 --- a/bazel/WORKSPACE +++ b/bazel/WORKSPACE @@ -7,3 +7,7 @@ deps() load(":emscripten_deps.bzl", "emscripten_deps") emscripten_deps() + +load(":toolchains.bzl", "register_emscripten_toolchains") + +register_emscripten_toolchains() diff --git a/bazel/bazelrc b/bazel/bazelrc deleted file mode 100644 index 85801e8381..0000000000 --- a/bazel/bazelrc +++ /dev/null @@ -1,5 +0,0 @@ -build:wasm --crosstool_top=//emscripten_toolchain:everything - -build:wasm --cpu=wasm - -build:wasm --host_crosstool_top=@bazel_tools//tools/cpp:toolchain diff --git a/bazel/emscripten_toolchain/BUILD.bazel b/bazel/emscripten_toolchain/BUILD.bazel index d101c67061..4486220b56 100644 --- a/bazel/emscripten_toolchain/BUILD.bazel +++ b/bazel/emscripten_toolchain/BUILD.bazel @@ -1,4 +1,4 @@ -load(":crosstool.bzl", "emscripten_cc_toolchain_config_rule") +load(":toolchain.bzl", "emscripten_cc_toolchain_config_rule") package(default_visibility = ["//visibility:public"]) @@ -86,6 +86,13 @@ cc_toolchain_suite( }, ) +toolchain( + name = "cc-toolchain-wasm", + target_compatible_with = ["@platforms//cpu:wasm32"], + toolchain = ":cc-compiler-wasm", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) + py_binary( name = "wasm_binary", srcs = ["wasm_binary.py"], diff --git a/bazel/emscripten_toolchain/crosstool.bzl b/bazel/emscripten_toolchain/toolchain.bzl similarity index 99% rename from bazel/emscripten_toolchain/crosstool.bzl rename to bazel/emscripten_toolchain/toolchain.bzl index a15c200da9..130405bd52 100644 --- a/bazel/emscripten_toolchain/crosstool.bzl +++ b/bazel/emscripten_toolchain/toolchain.bzl @@ -1105,7 +1105,7 @@ emscripten_cc_toolchain_config_rule = rule( attrs = { "cpu": attr.string(mandatory = True, values = ["asmjs", "wasm"]), "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], diff --git a/bazel/emscripten_toolchain/wasm_cc_binary.bzl b/bazel/emscripten_toolchain/wasm_cc_binary.bzl index 9cf6328aff..93ad0def1c 100644 --- a/bazel/emscripten_toolchain/wasm_cc_binary.bzl +++ b/bazel/emscripten_toolchain/wasm_cc_binary.bzl @@ -26,13 +26,10 @@ def _wasm_transition_impl(settings, attr): features.append("wasm_simd") return { - "//command_line_option:compiler": "emscripten", - "//command_line_option:crosstool_top": "@emsdk//emscripten_toolchain:everything", - "//command_line_option:cpu": "wasm", "//command_line_option:features": features, "//command_line_option:dynamic_mode": "off", "//command_line_option:linkopt": linkopts, - "//command_line_option:platforms": [], + "//command_line_option:platforms": ["@emsdk//:platform_wasm"], "//command_line_option:custom_malloc": "@emsdk//emscripten_toolchain:malloc", } @@ -43,9 +40,6 @@ _wasm_transition = transition( "//command_line_option:linkopt", ], outputs = [ - "//command_line_option:compiler", - "//command_line_option:cpu", - "//command_line_option:crosstool_top", "//command_line_option:features", "//command_line_option:dynamic_mode", "//command_line_option:linkopt", diff --git a/bazel/test_external/.bazelrc b/bazel/test_external/.bazelrc new file mode 100644 index 0000000000..fbd75a7ea7 --- /dev/null +++ b/bazel/test_external/.bazelrc @@ -0,0 +1 @@ +build --incompatible_enable_cc_toolchain_resolution diff --git a/bazel/test_external/WORKSPACE b/bazel/test_external/WORKSPACE index 4fe5772240..03f07f5070 100644 --- a/bazel/test_external/WORKSPACE +++ b/bazel/test_external/WORKSPACE @@ -10,3 +10,7 @@ deps() load("@emsdk//:emscripten_deps.bzl", "emscripten_deps") emscripten_deps() + +load("@emsdk//:toolchains.bzl", "register_emscripten_toolchains") + +register_emscripten_toolchains() diff --git a/bazel/toolchains.bzl b/bazel/toolchains.bzl new file mode 100644 index 0000000000..6d50b3d3d5 --- /dev/null +++ b/bazel/toolchains.bzl @@ -0,0 +1,2 @@ +def register_emscripten_toolchains(): + native.register_toolchains(str(Label("//emscripten_toolchain:cc-toolchain-wasm"))) From 6b1a03eacc3f62d543b7f88a70f23c9bb90481bb Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 1 Jun 2022 08:30:11 -0700 Subject: [PATCH 2/4] Note about `--incompatible_enable_cc_toolchain_resolution` in README --- bazel/README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bazel/README.md b/bazel/README.md index 5e854dcbcb..877f2e073d 100644 --- a/bazel/README.md +++ b/bazel/README.md @@ -29,7 +29,13 @@ parameter of `emsdk_emscripten_deps()`. Supported versions are listed in `revisi ## Building -First, write a new rule wrapping your `cc_binary`. +Put the following line into your `.bazelrc`: + +``` +build --incompatible_enable_cc_toolchain_resolution +``` + +Then write a new rule wrapping your `cc_binary`. ``` load("@rules_cc//cc:defs.bzl", "cc_binary") From 3800b0fdeb8e987fe2bc99a927ddeec9e5971123 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 1 Jun 2022 08:32:39 -0700 Subject: [PATCH 3/4] PR feedback --- bazel/bazelrc | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 bazel/bazelrc diff --git a/bazel/bazelrc b/bazel/bazelrc new file mode 100644 index 0000000000..d1c8aef229 --- /dev/null +++ b/bazel/bazelrc @@ -0,0 +1,2 @@ +build:wasm --incompatible_enable_cc_toolchain_resolution +build:wasm --platforms=@emsdk//:platform_wasm From 6599469c7c4bf4347c196d7dcdf8bbb938189ec4 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 10 Jun 2022 17:50:04 -0700 Subject: [PATCH 4/4] Add back in compiler, crosstool_top, cpu options --- bazel/emscripten_toolchain/wasm_cc_binary.bzl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bazel/emscripten_toolchain/wasm_cc_binary.bzl b/bazel/emscripten_toolchain/wasm_cc_binary.bzl index 93ad0def1c..8e4b6fb6b9 100644 --- a/bazel/emscripten_toolchain/wasm_cc_binary.bzl +++ b/bazel/emscripten_toolchain/wasm_cc_binary.bzl @@ -26,6 +26,9 @@ def _wasm_transition_impl(settings, attr): features.append("wasm_simd") return { + "//command_line_option:compiler": "emscripten", + "//command_line_option:crosstool_top": "@emsdk//emscripten_toolchain:everything", + "//command_line_option:cpu": "wasm", "//command_line_option:features": features, "//command_line_option:dynamic_mode": "off", "//command_line_option:linkopt": linkopts, @@ -40,6 +43,9 @@ _wasm_transition = transition( "//command_line_option:linkopt", ], outputs = [ + "//command_line_option:compiler", + "//command_line_option:cpu", + "//command_line_option:crosstool_top", "//command_line_option:features", "//command_line_option:dynamic_mode", "//command_line_option:linkopt",