Skip to content

Commit 4513656

Browse files
committed
gh-145177: Support multiple Emscripten versions for Emscripten buildbot
This adds an `--emsdk-cache` argument to the Emscripten build script and an emscripten_version.txt file. If the `--emsdk-cache` argument is passed, the build script will look in emscripten_version.txt to get the expected emsdk version is installed in a folder called e.g., 4.0.12 in the directory indicated by the `--emsdk-cache` argument. Otherwise, it will exit with an error.
1 parent 175ab31 commit 4513656

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

Tools/wasm/emscripten/__main__.py

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
EMSCRIPTEN_DIR = Path(__file__).parent
2424
CHECKOUT = EMSCRIPTEN_DIR.parent.parent.parent
25+
EMSCRIPTEN_VERSION_FILE = EMSCRIPTEN_DIR / "emscripten_version.txt"
2526

2627
CROSS_BUILD_DIR = CHECKOUT / "cross-build"
2728
NATIVE_BUILD_DIR = CROSS_BUILD_DIR / "build"
@@ -36,7 +37,32 @@
3637
LOCAL_SETUP_MARKER = b"# Generated by Tools/wasm/emscripten.py\n"
3738

3839

39-
def updated_env(updates={}):
40+
@functools.cache
41+
def get_required_emscripten_version():
42+
"""Read the required emscripten version from emscripten_version.txt."""
43+
return EMSCRIPTEN_VERSION_FILE.read_text().strip()
44+
45+
46+
@functools.cache
47+
def get_emsdk_activate_path(emsdk_cache):
48+
required_version = get_required_emscripten_version()
49+
return Path(emsdk_cache) / required_version / "emsdk_env.sh"
50+
51+
52+
def validate_emsdk_version(emsdk_cache):
53+
"""Validate that the emsdk cache contains the required emscripten version."""
54+
required_version = get_required_emscripten_version()
55+
emsdk_env = get_emsdk_activate_path(emsdk_cache)
56+
if not emsdk_env.is_file():
57+
print(
58+
f"Required emscripten version {required_version} not found in {emsdk_cache}",
59+
file=sys.stderr,
60+
)
61+
sys.exit(1)
62+
print(f"✅ Emscripten version {required_version} found in {emsdk_cache}")
63+
64+
65+
def updated_env(updates, emsdk_cache):
4066
"""Create a new dict representing the environment to use.
4167
4268
The changes made to the execution environment are printed out.
@@ -53,6 +79,15 @@ def updated_env(updates={}):
5379
pass # Might be building from a tarball.
5480
# This layering lets SOURCE_DATE_EPOCH from os.environ takes precedence.
5581
environment = env_defaults | os.environ | updates
82+
if emsdk_cache:
83+
env = os.environ.copy()
84+
call(
85+
["bash", "-c", f"source {get_emsdk_activate_path(emsdk_cache)}"],
86+
env=env,
87+
quiet=False,
88+
)
89+
for key in ["PATH", "EMSDK", "EMSDK_NODE"]:
90+
environment[key] = env[key]
5691

5792
env_diff = {}
5893
for key, value in environment.items():
@@ -204,7 +239,7 @@ def make_emscripten_libffi(context, working_dir):
204239
)
205240
call(
206241
[EMSCRIPTEN_DIR / "make_libffi.sh"],
207-
env=updated_env({"PREFIX": PREFIX_DIR}),
242+
env=updated_env({"PREFIX": PREFIX_DIR}, context.emsdk_cache),
208243
cwd=libffi_dir,
209244
quiet=context.quiet,
210245
)
@@ -231,6 +266,7 @@ def make_mpdec(context, working_dir):
231266
],
232267
cwd=mpdec_dir,
233268
quiet=context.quiet,
269+
env=updated_env({}, context.emsdk_cache),
234270
)
235271
call(
236272
["make", "install"],
@@ -300,7 +336,7 @@ def configure_emscripten_python(context, working_dir):
300336
configure.extend(context.args)
301337
call(
302338
configure,
303-
env=updated_env(env_additions),
339+
env=updated_env(env_additions, context.emsdk_cache),
304340
quiet=context.quiet,
305341
)
306342

@@ -358,7 +394,7 @@ def make_emscripten_python(context, working_dir):
358394
"""Run `make` for the emscripten/host build."""
359395
call(
360396
["make", "--jobs", str(cpu_count()), "all"],
361-
env=updated_env(),
397+
env=updated_env({}, context.emsdk_cache),
362398
quiet=context.quiet,
363399
)
364400

@@ -439,6 +475,14 @@ def main():
439475
dest="quiet",
440476
help="Redirect output from subprocesses to a log file",
441477
)
478+
subcommand.add_argument(
479+
"--emsdk-cache",
480+
action="store",
481+
default=None,
482+
dest="emsdk_cache",
483+
help="Path to emsdk cache directory. If provided, validates that "
484+
"the required emscripten version is installed.",
485+
)
442486
for subcommand in configure_build, configure_host:
443487
subcommand.add_argument(
444488
"--clean",
@@ -463,6 +507,10 @@ def main():
463507

464508
context = parser.parse_args()
465509

510+
if context.emsdk_cache:
511+
validate_emsdk_version(context.emsdk_cache)
512+
context.emsdk_cache = Path(context.emsdk_cache).absolute()
513+
466514
dispatch = {
467515
"make-libffi": make_emscripten_libffi,
468516
"make-mpdec": make_mpdec,

0 commit comments

Comments
 (0)