@@ -3,24 +3,21 @@ package(default_visibility = ['//visibility:public'])
3
3
exports_files(['emscripten_config'])
4
4
"""
5
5
6
- def get_root_and_binext (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}'
9
11
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
14
13
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.
14
+ system = platform.system()
15
+ machine = "arm64" if platform.machine() in ('arm64', 'aarch64') else "amd64"
16
+ nodejs_binary = "bin/nodejs/node.exe" if(system =="Windows") else "bin/node"
17
+ NODE_JS = '{external_root}/nodejs_{{}}_{{}}/{{}}'.format(system.lower(), machine, nodejs_binary)
18
+ """
22
19
23
- """
20
+ def get_root_and_script_ext ( repository_ctx ):
24
21
if repository_ctx .os .name .startswith ('linux' ):
25
22
if 'amd64' in repository_ctx .os .arch or 'x86_64' in repository_ctx .os .arch :
26
23
return (repository_ctx .path (Label ("@emscripten_bin_linux//:BUILD.bazel" )).dirname , '' )
@@ -36,7 +33,8 @@ def get_root_and_binext(repository_ctx):
36
33
else :
37
34
fail ('Unsupported architecture for MacOS' )
38
35
elif repository_ctx .os .name .startswith ('windows' ):
39
- return (repository_ctx .path (Label ("@emscripten_bin_win//:BUILD.bazel" )).dirname , '.exe' )
36
+ fail ('Using a secondary cache is not supported on Windows' )
37
+ #return (repository_ctx.path(Label("@emscripten_bin_win//:BUILD.bazel")).dirname, '.bat')
40
38
else :
41
39
fail ('Unsupported operating system' )
42
40
@@ -49,35 +47,40 @@ def _emscripten_cache_impl(repository_ctx):
49
47
)
50
48
51
49
if repository_ctx .attr .libraries or repository_ctx .attr .flags :
52
- root , binext = get_root_and_binext (repository_ctx )
50
+ root , script_ext = get_root_and_script_ext (repository_ctx )
53
51
llvm_root = root .get_child ("bin" )
54
52
emscripten_root = root .get_child ("emscripten" )
55
- embuilder_path = emscripten_root . get_child ( "embuilder " )
56
- cache_path = repository_ctx . path ( 'cache' )
57
- nodejs = repository_ctx .path (Label ("@nodejs//:BUILD.bazel" )).dirname .get_child ( "bin" ). get_child ( "node" )
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
58
56
# Create configuration file
59
- embuilder_config_content = "NODE_JS = '{}{}'\n " .format (nodejs , binext )
60
- embuilder_config_content += "LLVM_ROOT = '{}'\n " .format (llvm_root )
61
- embuilder_config_content += "BINARYEN_ROOT = '{}'\n " .format (root )
62
- embuilder_config_content += "EMSCRIPTEN_ROOT = '{}'\n " .format (emscripten_root )
63
- 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
+ )
64
64
repository_ctx .file ("embuilder_config" , embuilder_config_content )
65
65
embuilder_config_path = repository_ctx .path ("embuilder_config" )
66
+ embuilder_path = "{}{}" .format (emscripten_root .get_child ("embuilder" ), script_ext )
66
67
# Prepare the command line
67
68
if repository_ctx .attr .libraries :
68
69
libraries = repository_ctx .attr .libraries
69
70
else :
70
- # if no libraries are requested, build everything
71
+ # If no libraries are requested, build everything
71
72
libraries = ["ALL" ]
72
73
flags = ["--em-config" , embuilder_config_path ] + repository_ctx .attr .flags
73
74
embuilder_args = [embuilder_path ] + flags + ["build" ] + libraries
74
75
# Run embuilder
75
76
repository_ctx .report_progress ("Building secondary cache" )
76
- result = repository_ctx .execute (embuilder_args , quiet = False )
77
+ result = repository_ctx .execute (embuilder_args , quiet = True )
77
78
if result .return_code != 0 :
79
+ # Windows fails here because external/nodejs_windows_amd64/bin/nodejs/node.exe
80
+ # does not exist at this point (while the equivalent on Linux and MacOS does)
78
81
fail ("Embuilder exited with a non-zero return code" )
79
82
# Override Emscripten's cache with the secondary cache
80
- default_config += "CACHE = '{}'\n " .format (cache_path )
83
+ default_config += "CACHE = '{}'\n " .format (cache )
81
84
82
85
# Create the configuration file for the toolchain and export
83
86
repository_ctx .file ('emscripten_config' , default_config )
0 commit comments