Skip to content

Commit c246d38

Browse files
committed
Test
1 parent fdab57f commit c246d38

File tree

1 file changed

+32
-65
lines changed

1 file changed

+32
-65
lines changed

bazel/emscripten_cache.bzl

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

6-
def get_root_and_extensions(repository_ctx):
7-
"""
8-
Retrieve the path to the Emscripten binary directory
6+
EMBUILDER_FILE_CONTENT_TEMPLATE = """
7+
CACHE = '{cache}'
8+
EMSCRIPTEN_ROOT = '{emscripten_root}'
9+
BINARYEN_ROOT = '{binaryen_root}'
10+
LLVM_ROOT = '{llvm_root}'
911
10-
This function determines the correct Emscripten binary directory path by
11-
examining the operating system and architecture of the environment. It
12-
supports Linux, macOS, and Windows operating systems with specific
13-
architectures.
12+
import platform
13+
import os
1414
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.
15+
system = platform.system()
16+
machine = "arm64" if platform.machine() in ('arm64', 'aarch64') else "amd64"
17+
nodejs_binary = "bin/nodejs/node.exe" if(system =="Windows") else "bin/node"
18+
NODE_JS = os.path.join('{external_root}', 'nodejs_{{}}_{{}}/{{}}'.format(system.lower(), machine, nodejs_binary))
19+
"""
2220

23-
"""
21+
def get_root_and_script_ext(repository_ctx):
2422
if repository_ctx.os.name.startswith('linux'):
2523
if 'amd64' in repository_ctx.os.arch or 'x86_64' in repository_ctx.os.arch:
26-
return (repository_ctx.path(Label("@emscripten_bin_linux//:BUILD.bazel")).dirname, '', '')
24+
return (repository_ctx.path(Label("@emscripten_bin_linux//:BUILD.bazel")).dirname, '')
2725
elif 'aarch64' in repository_ctx.os.arch:
28-
return (repository_ctx.path(Label("@emscripten_bin_linux_arm64//:BUILD.bazel")).dirname, '', '')
26+
return (repository_ctx.path(Label("@emscripten_bin_linux_arm64//:BUILD.bazel")).dirname, '')
2927
else:
3028
fail('Unsupported architecture for Linux')
3129
elif repository_ctx.os.name.startswith('mac'):
3230
if 'amd64' in repository_ctx.os.arch or 'x86_64' in repository_ctx.os.arch:
33-
return (repository_ctx.path(Label("@emscripten_bin_mac//:BUILD.bazel")).dirname, '', '')
31+
return (repository_ctx.path(Label("@emscripten_bin_mac//:BUILD.bazel")).dirname, '')
3432
elif 'aarch64' in repository_ctx.os.arch:
35-
return (repository_ctx.path(Label("@emscripten_bin_mac_arm64//:BUILD.bazel")).dirname, '', '')
33+
return (repository_ctx.path(Label("@emscripten_bin_mac_arm64//:BUILD.bazel")).dirname, '')
3634
else:
3735
fail('Unsupported architecture for MacOS')
3836
elif repository_ctx.os.name.startswith('windows'):
39-
return (repository_ctx.path(Label("@emscripten_bin_win//:BUILD.bazel")).dirname, '.exe', '.bat')
37+
return (repository_ctx.path(Label("@emscripten_bin_win//:BUILD.bazel")).dirname, '.bat')
4038
else:
4139
fail('Unsupported operating system')
4240

@@ -49,69 +47,38 @@ def _emscripten_cache_impl(repository_ctx):
4947
)
5048

5149
if repository_ctx.attr.libraries or repository_ctx.attr.flags:
52-
root, binary_ext, script_ext = get_root_and_extensions(repository_ctx)
50+
root, script_ext = get_root_and_script_ext(repository_ctx)
5351
llvm_root = root.get_child("bin")
5452
emscripten_root = root.get_child("emscripten")
55-
nodejs_root = repository_ctx.path(Label("@nodejs//:BUILD.bazel")).dirname
56-
cache_path = repository_ctx.path("cache")
57-
embuilder_path = "{}{}".format(emscripten_root.get_child("embuilder"), script_ext)
58-
nodejs_path = "{}{}".format(nodejs_root.get_child("bin").get_child("node"), binary_ext)
53+
cache = repository_ctx.path("cache")
54+
# Ugly hack to get the "external" directory (needed for Windows/Node.js)
55+
external_root = repository_ctx.path(Label("@nodejs//:BUILD.bazel")).dirname.dirname
5956
# Create configuration file
60-
embuilder_config_content = "NODE_JS = '{}'\n".format(nodejs_path)
61-
embuilder_config_content += "LLVM_ROOT = '{}'\n".format(llvm_root)
62-
embuilder_config_content += "BINARYEN_ROOT = '{}'\n".format(root)
63-
embuilder_config_content += "EMSCRIPTEN_ROOT = '{}'\n".format(emscripten_root)
64-
embuilder_config_content += "CACHE = '{}'\n".format(cache_path)
57+
embuilder_config_content = EMBUILDER_FILE_CONTENT_TEMPLATE.format(
58+
cache=cache,
59+
emscripten_root=emscripten_root,
60+
binaryen_root=root,
61+
llvm_root=llvm_root,
62+
external_root=external_root,
63+
)
6564
repository_ctx.file("embuilder_config", embuilder_config_content)
6665
embuilder_config_path = repository_ctx.path("embuilder_config")
66+
embuilder_path = "{}{}".format(emscripten_root.get_child("embuilder"), script_ext)
6767
# Prepare the command line
6868
if repository_ctx.attr.libraries:
6969
libraries = repository_ctx.attr.libraries
7070
else:
71-
# if no libraries are requested, build everything
71+
# If no libraries are requested, build everything
7272
libraries = ["ALL"]
7373
flags = ["--em-config", embuilder_config_path] + repository_ctx.attr.flags
7474
embuilder_args = [embuilder_path] + flags + ["build"] + libraries
7575
# Run embuilder
7676
repository_ctx.report_progress("Building secondary cache")
7777
result = repository_ctx.execute(embuilder_args, quiet=False)
7878
if result.return_code != 0:
79-
### TESTING
80-
print('stderr = ' + result.stderr)
81-
print('stdout = ' + result.stdout)
82-
print('---------------------------------------')
83-
84-
nodejs_bindir = nodejs_root.get_child("bin")
85-
dir_result = repository_ctx.execute(['dir'], quiet=False, working_directory=str(nodejs_bindir))
86-
print('dir_result(nodejs_bindir).return_code = {}\n\n'.format(dir_result.return_code))
87-
print('dir_result(nodejs_bindir).stderr = {}\n\n'.format(dir_result.stderr))
88-
print('dir_result(nodejs_bindir).stdout = {}\n\n'.format(dir_result.stdout))
89-
90-
acl_result = repository_ctx.execute(["powershell.exe", "Get-Acl", "-Path", str(nodejs_bindir)], quiet=False)
91-
print('acl_result(nodejs_bindir).return_code = {}\n\n'.format(acl_result.return_code))
92-
print('acl_result(nodejs_bindir).stderr = {}\n\n'.format(acl_result.stderr))
93-
print('acl_result(nodejs_bindir).stdout = {}\n\n'.format(acl_result.stdout))
94-
95-
print('---------------------------------------')
96-
97-
acl_result = repository_ctx.execute(["powershell.exe", "Get-Acl", "-Path", str(nodejs_path)], quiet=False)
98-
print('acl_result(nodejs_path).return_code = {}\n\n'.format(acl_result.return_code))
99-
print('acl_result(nodejs_path).stderr = {}\n\n'.format(acl_result.stderr))
100-
print('acl_result(nodejs_path).stdout = {}\n\n'.format(acl_result.stdout))
101-
102-
print('---------------------------------------')
103-
104-
### Run node version under windows
105-
nodejs_result = repository_ctx.execute([nodejs_path, "-v"], quiet=False)
106-
print('nodejs_result.return_code = {}\n\n'.format(nodejs_result.return_code))
107-
print('nodejs_result.stderr = {}\n\n'.format(nodejs_result.stderr))
108-
print('nodejs_result.stdout = {}\n\n'.format(nodejs_result.stdout))
109-
110-
111-
### TESTING
11279
fail("Embuilder exited with a non-zero return code")
11380
# Override Emscripten's cache with the secondary cache
114-
default_config += "CACHE = '{}'\n".format(cache_path)
81+
default_config += "CACHE = '{}'\n".format(cache)
11582

11683
# Create the configuration file for the toolchain and export
11784
repository_ctx.file('emscripten_config', default_config)

0 commit comments

Comments
 (0)