Skip to content

Commit 16a7285

Browse files
committed
make toolchain hermetic in case when secondary cache is used
1 parent 89a8f95 commit 16a7285

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

bazel/emscripten_build_file.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ filegroup(
1818
]),
1919
)
2020
21+
filegroup(
22+
name = "builtin_cache",
23+
srcs = glob([
24+
"emscripten/cache/**",
25+
]),
26+
)
27+
2128
filegroup(
2229
name = "emcc_common",
2330
srcs = [

bazel/emscripten_cache.bzl

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

6+
BUILD_FILE_USE_BUILTIN_CACHE = """
7+
alias(
8+
name = "emscripten_cache",
9+
actual = "{}//:builtin_cache",
10+
)
11+
"""
12+
13+
BUILD_FILE_USE_SECONDARY_CACHE = """
14+
filegroup(
15+
name = "emscripten_cache",
16+
srcs = glob(["cache/**"]),
17+
visibility = ["//visibility:public"],
18+
)
19+
"""
20+
621
EMBUILDER_CONFIG_TEMPLATE = """
722
CACHE = '{cache}'
823
BINARYEN_ROOT = '{binaryen_root}'
@@ -29,6 +44,26 @@ def get_root_and_script_ext(repository_ctx):
2944
else:
3045
fail("Unsupported operating system")
3146

47+
def get_bin_deps_repo_name(repository_ctx):
48+
if repository_ctx.os.name.startswith("linux"):
49+
if "amd64" in repository_ctx.os.arch or "x86_64" in repository_ctx.os.arch:
50+
return "@emscripten_bin_linux"
51+
elif "aarch64" in repository_ctx.os.arch:
52+
return "@emscripten_bin_linux_arm64"
53+
else:
54+
fail("Unsupported architecture for Linux")
55+
elif repository_ctx.os.name.startswith("mac"):
56+
if "amd64" in repository_ctx.os.arch or "x86_64" in repository_ctx.os.arch:
57+
return "@emscripten_bin_mac"
58+
elif "aarch64" in repository_ctx.os.arch:
59+
return "@emscripten_bin_mac_arm64"
60+
else:
61+
fail("Unsupported architecture for MacOS")
62+
elif repository_ctx.os.name.startswith("windows"):
63+
return "@emscripten_bin_win"
64+
else:
65+
fail("Unsupported operating system")
66+
3267
def _emscripten_cache_repository_impl(repository_ctx):
3368
# Read the default emscripten configuration file
3469
default_config = repository_ctx.read(
@@ -38,6 +73,8 @@ def _emscripten_cache_repository_impl(repository_ctx):
3873
)
3974

4075
repo_metadata = None
76+
build_file_content = BUILD_FILE_CONTENT_TEMPLATE
77+
use_builtin_cache = True
4178

4279
if repository_ctx.attr.prebuilt_cache_url:
4380
repository_ctx.download_and_extract(
@@ -48,8 +85,7 @@ def _emscripten_cache_repository_impl(repository_ctx):
4885
)
4986

5087
# Use the prebuilt cache
51-
prebuilt_cache_path = repository_ctx.path("cache")
52-
default_config += "CACHE = '{}'\n".format(prebuilt_cache_path)
88+
use_builtin_cache = False
5389

5490
repo_metadata = repository_ctx.repo_metadata(reproducible = True)
5591

@@ -90,12 +126,17 @@ def _emscripten_cache_repository_impl(repository_ctx):
90126
if result.return_code != 0:
91127
fail("Embuilder exited with a non-zero return code")
92128

93-
# Override Emscripten's cache with the secondary cache
94-
default_config += "CACHE = '{}'\n".format(cache)
129+
use_builtin_cache = False
130+
131+
if use_builtin_cache:
132+
build_file_content += BUILD_FILE_USE_BUILTIN_CACHE.format(get_bin_deps_repo_name(repository_ctx))
133+
else:
134+
default_config += 'CACHE = os.path.join(os.path.dirname(os.environ["EM_CONFIG_PATH"]), "cache")\n'
135+
build_file_content += BUILD_FILE_USE_SECONDARY_CACHE
95136

96137
# Create the configuration file for the toolchain and export
97138
repository_ctx.file("emscripten_config", default_config)
98-
repository_ctx.file("BUILD.bazel", BUILD_FILE_CONTENT_TEMPLATE)
139+
repository_ctx.file("BUILD.bazel", build_file_content)
99140

100141
return repo_metadata
101142

bazel/remote_emscripten_repository.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def create_toolchains(name, repo_name, exec_compatible_with):
5959
name = common_files_name,
6060
srcs = [
6161
"@emscripten_cache//:emscripten_config",
62+
"@emscripten_cache//:emscripten_cache",
6263
"@emsdk//emscripten_toolchain:env.sh",
6364
"@emsdk//emscripten_toolchain:env.bat",
6465
"@nodejs//:node_files",

0 commit comments

Comments
 (0)