@@ -3,40 +3,38 @@ package(default_visibility = ['//visibility:public'])
3
3
exports_files(['emscripten_config'])
4
4
"""
5
5
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}'
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
13
+ import os
14
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.
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
+ """
22
20
23
- """
21
+ def get_root_and_script_ext ( repository_ctx ):
24
22
if repository_ctx .os .name .startswith ('linux' ):
25
23
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 , '' )
27
25
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 , '' )
29
27
else :
30
28
fail ('Unsupported architecture for Linux' )
31
29
elif repository_ctx .os .name .startswith ('mac' ):
32
30
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 , '' )
34
32
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 , '' )
36
34
else :
37
35
fail ('Unsupported architecture for MacOS' )
38
36
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' )
40
38
else :
41
39
fail ('Unsupported operating system' )
42
40
@@ -49,69 +47,38 @@ def _emscripten_cache_impl(repository_ctx):
49
47
)
50
48
51
49
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 )
53
51
llvm_root = root .get_child ("bin" )
54
52
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
59
56
# 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
+ )
65
64
repository_ctx .file ("embuilder_config" , embuilder_config_content )
66
65
embuilder_config_path = repository_ctx .path ("embuilder_config" )
66
+ embuilder_path = "{}{}" .format (emscripten_root .get_child ("embuilder" ), script_ext )
67
67
# Prepare the command line
68
68
if repository_ctx .attr .libraries :
69
69
libraries = repository_ctx .attr .libraries
70
70
else :
71
- # if no libraries are requested, build everything
71
+ # If no libraries are requested, build everything
72
72
libraries = ["ALL" ]
73
73
flags = ["--em-config" , embuilder_config_path ] + repository_ctx .attr .flags
74
74
embuilder_args = [embuilder_path ] + flags + ["build" ] + libraries
75
75
# Run embuilder
76
76
repository_ctx .report_progress ("Building secondary cache" )
77
77
result = repository_ctx .execute (embuilder_args , quiet = False )
78
78
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
112
79
fail ("Embuilder exited with a non-zero return code" )
113
80
# Override Emscripten's cache with the secondary cache
114
- default_config += "CACHE = '{}'\n " .format (cache_path )
81
+ default_config += "CACHE = '{}'\n " .format (cache )
115
82
116
83
# Create the configuration file for the toolchain and export
117
84
repository_ctx .file ('emscripten_config' , default_config )
0 commit comments