-
Notifications
You must be signed in to change notification settings - Fork 808
Expand file tree
/
Copy pathemscripten_cache.bzl
More file actions
193 lines (170 loc) · 7.18 KB
/
emscripten_cache.bzl
File metadata and controls
193 lines (170 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
BUILD_FILE_CONTENT_TEMPLATE = """
package(default_visibility = ['//visibility:public'])
exports_files(['emscripten_config'])
"""
BUILD_FILE_USE_BUILTIN_CACHE = """
alias(
name = "emscripten_cache",
actual = "{}//:builtin_cache",
)
"""
BUILD_FILE_USE_SECONDARY_CACHE = """
filegroup(
name = "emscripten_cache",
srcs = glob(["cache/**"]),
visibility = ["//visibility:public"],
)
"""
EMBUILDER_CONFIG_TEMPLATE = """
CACHE = '{cache}'
BINARYEN_ROOT = '{binaryen_root}'
LLVM_ROOT = '{llvm_root}'
"""
def get_root_and_script_ext(repository_ctx):
if repository_ctx.os.name.startswith("linux"):
if "amd64" in repository_ctx.os.arch or "x86_64" in repository_ctx.os.arch:
return (repository_ctx.path(Label("@emscripten_bin_linux//:BUILD.bazel")).dirname, "")
elif "aarch64" in repository_ctx.os.arch:
return (repository_ctx.path(Label("@emscripten_bin_linux_arm64//:BUILD.bazel")).dirname, "")
else:
fail("Unsupported architecture for Linux")
elif repository_ctx.os.name.startswith("mac"):
if "amd64" in repository_ctx.os.arch or "x86_64" in repository_ctx.os.arch:
return (repository_ctx.path(Label("@emscripten_bin_mac//:BUILD.bazel")).dirname, "")
elif "aarch64" in repository_ctx.os.arch:
return (repository_ctx.path(Label("@emscripten_bin_mac_arm64//:BUILD.bazel")).dirname, "")
else:
fail("Unsupported architecture for MacOS")
elif repository_ctx.os.name.startswith("windows"):
return (repository_ctx.path(Label("@emscripten_bin_win//:BUILD.bazel")).dirname, ".bat")
else:
fail("Unsupported operating system")
def get_bin_deps_repo_name(repository_ctx):
if repository_ctx.os.name.startswith("linux"):
if "amd64" in repository_ctx.os.arch or "x86_64" in repository_ctx.os.arch:
return "@emscripten_bin_linux"
elif "aarch64" in repository_ctx.os.arch:
return "@emscripten_bin_linux_arm64"
else:
fail("Unsupported architecture for Linux")
elif repository_ctx.os.name.startswith("mac"):
if "amd64" in repository_ctx.os.arch or "x86_64" in repository_ctx.os.arch:
return "@emscripten_bin_mac"
elif "aarch64" in repository_ctx.os.arch:
return "@emscripten_bin_mac_arm64"
else:
fail("Unsupported architecture for MacOS")
elif repository_ctx.os.name.startswith("windows"):
return "@emscripten_bin_win"
else:
fail("Unsupported operating system")
def _emscripten_cache_repository_impl(repository_ctx):
# Read the default emscripten configuration file
default_config = repository_ctx.read(
repository_ctx.path(
Label("@emsdk//emscripten_toolchain:default_config"),
),
)
repo_metadata = None
build_file_content = BUILD_FILE_CONTENT_TEMPLATE
use_builtin_cache = True
if repository_ctx.attr.prebuilt_cache_url:
repository_ctx.download_and_extract(
url = repository_ctx.attr.prebuilt_cache_url,
output = "cache",
sha256 = repository_ctx.attr.prebuilt_cache_sha256,
strip_prefix = repository_ctx.attr.prebuilt_cache_strip_prefix,
)
# Use the prebuilt cache
use_builtin_cache = False
repo_metadata = repository_ctx.repo_metadata(reproducible = True)
elif repository_ctx.attr.targets or repository_ctx.attr.configuration:
root, script_ext = get_root_and_script_ext(repository_ctx)
llvm_root = root.get_child("bin")
cache = repository_ctx.path("cache")
# Create configuration file
embuilder_config_content = EMBUILDER_CONFIG_TEMPLATE.format(
cache = cache,
binaryen_root = root,
llvm_root = llvm_root,
)
repository_ctx.file("embuilder_config", embuilder_config_content)
embuilder_config_path = repository_ctx.path("embuilder_config")
embuilder_path = "{}{}".format(root.get_child("emscripten").get_child("embuilder"), script_ext)
# Prepare the command line
if repository_ctx.attr.targets:
targets = repository_ctx.attr.targets
else:
# If no targets are requested, build everything
targets = ["ALL"]
flags = ["--em-config", embuilder_config_path] + repository_ctx.attr.configuration
embuilder_args = [embuilder_path] + flags + ["build"] + targets
# Run embuilder
repository_ctx.report_progress("Building secondary cache")
result = repository_ctx.execute(
embuilder_args,
quiet = True,
environment = {
"EM_IGNORE_SANITY": "1",
"EM_NODE_JS": "empty",
},
)
if result.return_code != 0:
fail("Embuilder exited with a non-zero return code")
use_builtin_cache = False
if use_builtin_cache:
build_file_content += BUILD_FILE_USE_BUILTIN_CACHE.format(get_bin_deps_repo_name(repository_ctx))
else:
default_config += 'CACHE = os.path.join(os.path.dirname(os.environ["EM_CONFIG_PATH"]), "cache")\n'
build_file_content += BUILD_FILE_USE_SECONDARY_CACHE
# Create the configuration file for the toolchain and export
repository_ctx.file("emscripten_config", default_config)
repository_ctx.file("BUILD.bazel", build_file_content)
return repo_metadata
_emscripten_cache_repository = repository_rule(
implementation = _emscripten_cache_repository_impl,
attrs = {
"configuration": attr.string_list(),
"targets": attr.string_list(),
"prebuilt_cache_url": attr.string(),
"prebuilt_cache_sha256": attr.string(),
"prebuilt_cache_strip_prefix": attr.string(),
},
)
def _emscripten_cache_impl(ctx):
all_configuration = []
all_targets = []
prebuilt_cache_url = None
prebuilt_cache_sha256 = None
prebuilt_cache_strip_prefix = None
for mod in ctx.modules:
for configuration in mod.tags.configuration:
all_configuration += configuration.flags
for targets in mod.tags.targets:
all_targets += targets.targets
for prebuilt_cache in mod.tags.prebuilt_cache:
if prebuilt_cache_url != None:
fail("Only one prebuilt_cache tag is allowed")
prebuilt_cache_url = prebuilt_cache.http_archive_url
prebuilt_cache_sha256 = prebuilt_cache.sha256
prebuilt_cache_strip_prefix = prebuilt_cache.strip_prefix
_emscripten_cache_repository(
name = "emscripten_cache",
configuration = all_configuration,
targets = all_targets,
prebuilt_cache_url = prebuilt_cache_url,
prebuilt_cache_sha256 = prebuilt_cache_sha256,
prebuilt_cache_strip_prefix = prebuilt_cache_strip_prefix,
)
emscripten_cache = module_extension(
tag_classes = {
"configuration": tag_class(attrs = {"flags": attr.string_list()}),
"targets": tag_class(attrs = {"targets": attr.string_list()}),
"prebuilt_cache": tag_class(attrs = {
"http_archive_url": attr.string(),
"sha256": attr.string(),
"strip_prefix": attr.string(),
}),
},
implementation = _emscripten_cache_impl,
)