Skip to content

Commit ea4386a

Browse files
committed
Remove hard-coded @emscripten_bin_linux// reference
1 parent 592171e commit ea4386a

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

bazel/emscripten_cache.bzl

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,44 @@ package(default_visibility = ['//visibility:public'])
33
exports_files(['emscripten_config'])
44
"""
55

6+
def get_binaryen_root(repository_ctx):
7+
"""
8+
Retrieve the path to the Emscripten binary directory
9+
10+
This function determines the correct Emscripten binary directory path by
11+
examining the operating system (OS) and architecture (arch) of the
12+
environment. It supports Linux, macOS, and Windows operating systems with
13+
specific architectures.
14+
15+
Args:
16+
repository_ctx: The repository context object which provides information
17+
about the OS and architecture, and methods to obtain paths and labels.
18+
19+
Returns:
20+
str: The directory path to the Emscripten binaries for the detected OS
21+
and architecture.
22+
23+
"""
24+
if repository_ctx.os.name.startswith('linux'):
25+
if 'amd64' in repository_ctx.os.arch or 'x86_64' in repository_ctx.os.arch:
26+
return repository_ctx.path(Label("@emscripten_bin_linux//:all")).dirname
27+
elif 'aarch64' in repository_ctx.os.arch:
28+
return repository_ctx.path(Label("@emscripten_bin_linux_arm64//:all")).dirname
29+
else:
30+
repository_ctx.fail('Unsupported architecture for Linux')
31+
elif repository_ctx.os.name.startswith('mac'):
32+
if 'amd64' in repository_ctx.os.arch or 'x86_64' in repository_ctx.os.arch:
33+
return repository_ctx.path(Label("@emscripten_bin_mac//:all")).dirname
34+
elif 'aarch64' in repository_ctx.os.arch:
35+
return repository_ctx.path(Label("@emscripten_bin_mac_arm64//:all")).dirname
36+
else:
37+
repository_ctx.fail('Unsupported architecture for MacOS')
38+
elif repository_ctx.os.name.startswith('windows'):
39+
return repository_ctx.path(Label("@emscripten_bin_win//:all")).dirname
40+
else:
41+
repository_ctx.fail('Unsupported operating system')
42+
return ''
43+
644
def _emscripten_cache_impl(repository_ctx):
745
# Read the default emscripten configuration file
846
default_config = repository_ctx.read(
@@ -11,11 +49,9 @@ def _emscripten_cache_impl(repository_ctx):
1149
)
1250
)
1351

14-
# TODO I need a cross platform way to get embuilder and bin/node
1552
if repository_ctx.attr.libraries or repository_ctx.attr.flags:
16-
# Get paths to tools (toolchain is not yet setup, so we cannot use emscripten_config)
17-
embuilder_path = repository_ctx.path(Label("@emscripten_bin_linux//:emscripten/embuilder.py"))
18-
binaryen_root = embuilder_path.dirname.dirname
53+
binaryen_root = get_binaryen_root(repository_ctx)
54+
embuilder_path = binaryen_root.get_child('emscripten/embuilder')
1955
llvm_root = binaryen_root.get_child("bin")
2056
nodejs = repository_ctx.path(Label("@nodejs//:node_files")).dirname.get_child('bin/node')
2157
# Create configuration file
@@ -38,6 +74,7 @@ def _emscripten_cache_impl(repository_ctx):
3874
repository_ctx.execute(embuilder_args, quiet=False)
3975
# Override Emscripten's cache with the secondary cache
4076
default_config += "CACHE = '{}'\n".format(repository_ctx.path('cache'))
77+
4178
# Create the configuration file for the toolchain and export
4279
repository_ctx.file('emscripten_config', default_config)
4380
repository_ctx.file('BUILD.bazel', BUILD_FILE_CONTENT_TEMPLATE)

0 commit comments

Comments
 (0)