Skip to content

Commit 85390ce

Browse files
authored
Bazel: Pass nodejs binary path as an environment variable. (#1518)
The current way to derive the binary path relies on a specific name for the nodejs repository. This blocks migrating to bzlmod, as bzlmod prefixes repository directories with the module name that created them. By asking bazel for the path instead we always get the correct path, so we can work with both bzlmod and WORKSPACE based dependencies at the same time. The repository @nodejs, used in the build label, refers to nodejs for the host platform and is generated by the following macro (wasn't too obvious to me): https://github.com/bazel-contrib/rules_nodejs/blob/d19d695275c5fd0d5224ddc7826d9f7f4b8186df/nodejs/repositories.bzl#L452 This is some work towards solving #1509.
1 parent 580895a commit 85390ce

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

bazel/emscripten_toolchain/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ emscripten_cc_toolchain_config_rule(
6262
cpu = "wasm",
6363
em_config = "@emscripten_cache//:emscripten_config",
6464
emscripten_binaries = "@emsdk//:compiler_files",
65+
nodejs_bin = "@nodejs//:node",
6566
script_extension = select({
6667
"@bazel_tools//src/conditions:host_windows": "bat",
6768
"//conditions:default": "sh",

bazel/emscripten_toolchain/default_config

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,12 @@ ROOT_DIR = os.environ["ROOT_DIR"]
55
EMSCRIPTEN_ROOT = os.environ["EMSCRIPTEN"]
66
BINARYEN_ROOT = os.path.join(ROOT_DIR, os.environ["EM_BIN_PATH"])
77
LLVM_ROOT = os.path.join(BINARYEN_ROOT, "bin")
8+
NODE_JS = os.path.join(ROOT_DIR, os.environ["NODE_JS_PATH"])
89
FROZEN_CACHE = True
910

10-
system = platform.system()
11-
12-
machine = "arm64" if platform.machine() in ('arm64', 'aarch64') else "amd64"
13-
nodejs_binary = "bin/nodejs/node.exe" if(system =="Windows") else "bin/node"
14-
NODE_JS = ROOT_DIR + "/external/nodejs_{}_{}/{}".format(system.lower(), machine, nodejs_binary)
15-
16-
1711
# This works around an issue with Bazel RBE where the symlinks in node_modules/.bin
1812
# are uploaded as the linked files, which means the cli.js cannot load its
1913
# dependencies from the expected locations.
2014
# See https://github.com/emscripten-core/emscripten/pull/16640 for more
21-
if system != "Windows":
22-
CLOSURE_COMPILER = [NODE_JS, os.path.join(EMSCRIPTEN_ROOT, "node_modules",
23-
"google-closure-compiler", "cli.js")]
15+
CLOSURE_COMPILER = [NODE_JS, os.path.join(EMSCRIPTEN_ROOT, "node_modules",
16+
"google-closure-compiler", "cli.js")]

bazel/emscripten_toolchain/toolchain.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def _impl(ctx):
7171
cc_target_os = "emscripten"
7272

7373
emscripten_dir = ctx.attr.emscripten_binaries.label.workspace_root
74+
nodejs_path = ctx.file.nodejs_bin.path
7475

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

@@ -1060,6 +1061,10 @@ def _impl(ctx):
10601061
key = "EM_CONFIG_PATH",
10611062
value = ctx.file.em_config.path,
10621063
),
1064+
env_entry(
1065+
key = "NODE_JS_PATH",
1066+
value = nodejs_path,
1067+
),
10631068
],
10641069
),
10651070
# Use llvm backend. Off by default, enabled via --features=llvm_backend
@@ -1134,6 +1139,7 @@ emscripten_cc_toolchain_config_rule = rule(
11341139
"cpu": attr.string(mandatory = True, values = ["asmjs", "wasm"]),
11351140
"em_config": attr.label(mandatory = True, allow_single_file = True),
11361141
"emscripten_binaries": attr.label(mandatory = True, cfg = "exec"),
1142+
"nodejs_bin": attr.label(mandatory = True, allow_single_file = True),
11371143
"script_extension": attr.string(mandatory = True, values = ["sh", "bat"]),
11381144
},
11391145
provides = [CcToolchainConfigInfo],

0 commit comments

Comments
 (0)