Skip to content
Merged
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
3 changes: 1 addition & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,7 @@ workflows:
bazel_version:
- "7.x"
- "8.x"
# https://github.com/emscripten-core/emsdk/issues/1642
# - "9.x"
- "9.x"
Copy link
Contributor

Choose a reason for hiding this comment

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

@sbc100 ,
This is also closing 1642 issue? Shall Closes #xxx be added to the description and maybe several others ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, please update the PR description (we use Fixes: #xxx)

test-bazel-windows:
jobs:
- test-bazel-windows:
Expand Down
4 changes: 2 additions & 2 deletions bazel/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ bazel_dep(name = "bazel_skylib", version = "1.8.2")
bazel_dep(name = "aspect_rules_js", version = "2.9.2")
bazel_dep(name = "rules_nodejs", version = "6.7.3")
bazel_dep(name = "rules_cc", version = "0.2.16")
bazel_dep(name = "rules_python", version = "1.8.3")
bazel_dep(name = "rules_python", version = "1.8.4")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
python_version = "3.13",
python_version = "3.14",
)

node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node")
Expand Down
2 changes: 1 addition & 1 deletion bazel/emscripten_toolchain/emar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

source $(dirname $0)/env.sh

exec python3 $EMSCRIPTEN/emar.py "$@"
exec $EMSDK_PYTHON $EMSCRIPTEN/emar.py "$@"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if could now maybe remove these custom launcher scripts and just use the standard emcc launcher shell script (as a followup, no in this PR)

I guess it depends what else in env.sh is needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually tried that in one of my unsuccessful iterations. I replaced all those scripts with pure Python, presented them to Bazel via py_binary, and then gave those binaries to the cc_toolchain.

The first problem was that binaries were resolved in the "target context (WASM)" instead of the "exec" context, which then tried to run the "WASM version of Python", which doesn't exist. I circumvented that by creating a transitioned binary in exec context, and that almost worked, except final compilation attempts failed because execv("/path/to/python/wrapper") failed. I didn't investigate deeply enough to find out what was wrong there, and instead backtracked to pursuing adding Python interpreter runfiles to the toolchain context, so they end up in the sandbox.

My first working solution required a patch to rules_python, which I created a PR for, but the rules_python author then told me this wasn't how it is supposed to be used and instructed me to research the Python exec toolchain. I did that, and this solution now works, so I created this PR.

In the future, I'll probably revisit this approach that uses pure Python without any shell wrappers, but I still need to get better at Bazel before that (I'm still learning it).

2 changes: 1 addition & 1 deletion bazel/emscripten_toolchain/emcc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

source $(dirname $0)/env.sh

exec python3 $EMSCRIPTEN/emcc.py "$@"
exec $EMSDK_PYTHON $EMSCRIPTEN/emcc.py "$@"
2 changes: 1 addition & 1 deletion bazel/emscripten_toolchain/emcc_link.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

source $(dirname $0)/env.sh

exec python3 $(dirname $0)/link_wrapper.py "$@"
exec $EMSDK_PYTHON $(dirname $0)/link_wrapper.py "$@"
28 changes: 28 additions & 0 deletions bazel/emscripten_toolchain/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ def _impl(ctx):

nodejs_path = ctx.file.nodejs_bin.path

python_exec_runtime = (
ctx.toolchains["@rules_python//python:exec_tools_toolchain_type"].
exec_tools.exec_interpreter[platform_common.ToolchainInfo].py3_runtime
)

builtin_sysroot = emscripten_dir + "/emscripten/cache/sysroot"

emcc_script = "emcc.%s" % ctx.attr.script_extension
Expand Down Expand Up @@ -1078,6 +1083,10 @@ def _impl(ctx):
key = "NODE_JS_PATH",
value = nodejs_path,
),
env_entry(
key = "EMSDK_PYTHON",
value = python_exec_runtime.interpreter.path,
),
],
),
# Use llvm backend. Off by default, enabled via --features=llvm_backend
Expand Down Expand Up @@ -1156,4 +1165,23 @@ emscripten_cc_toolchain_config_rule = rule(
"script_extension": attr.string(mandatory = True, values = ["sh", "bat"]),
},
provides = [CcToolchainConfigInfo],
toolchains = [
"@rules_python//python:exec_tools_toolchain_type",
]
)

def _python_interpreter_files_impl(ctx):
python_exec_runtime = (
ctx.toolchains["@rules_python//python:exec_tools_toolchain_type"].
exec_tools.exec_interpreter[platform_common.ToolchainInfo].py3_runtime
)

return DefaultInfo(files = python_exec_runtime.files)


emscripten_python_interpreter_files = rule(
implementation = _python_interpreter_files_impl,
toolchains = [
"@rules_python//python:exec_tools_toolchain_type",
]
)
8 changes: 7 additions & 1 deletion bazel/remote_emscripten_repository.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@rules_cc//cc:defs.bzl", "cc_toolchain", "cc_toolchain_suite")
load("//emscripten_toolchain:toolchain.bzl", "emscripten_cc_toolchain_config_rule")
load("//emscripten_toolchain:toolchain.bzl", "emscripten_cc_toolchain_config_rule", "emscripten_python_interpreter_files")
load(":emscripten_build_file.bzl", "EMSCRIPTEN_BUILD_FILE_CONTENT_TEMPLATE")

def remote_emscripten_repository(
Expand Down Expand Up @@ -45,6 +45,7 @@ def create_toolchains(name, repo_name, exec_compatible_with):
ar_files_name, ar_files_target = _get_name_and_target("ar_files_" + name)
all_files_name, all_files_target = _get_name_and_target("all_files_" + name)
cc_wasm_name, cc_wasm_target = _get_name_and_target("cc-compiler-wasm-" + name)
python_interpreter_name, python_interpreter_target = _get_name_and_target("python_interpreter-" + name)

wasm_name = "wasm-" + name

Expand All @@ -54,13 +55,18 @@ def create_toolchains(name, repo_name, exec_compatible_with):
repo_linker_files_target = remote_repo + ":linker_files"
repo_ar_files_target = remote_repo + ":ar_files"

emscripten_python_interpreter_files(
name = python_interpreter_name,
)

native.filegroup(
name = common_files_name,
srcs = [
"@emscripten_cache//:emscripten_config",
"@emsdk//emscripten_toolchain:env.sh",
"@emsdk//emscripten_toolchain:env.bat",
"@nodejs//:node_files",
python_interpreter_target,
],
)

Expand Down