Skip to content

Commit 10eda7f

Browse files
committed
gh-145805: Add python Platforms/emscripten run subcommand
This should allow the build bot to be more agnostic to paths. I also added environment variables to set --quiet, --cross-build-dir, and --emsdk-cache
1 parent 4f5e798 commit 10eda7f

File tree

1 file changed

+61
-21
lines changed

1 file changed

+61
-21
lines changed

Platforms/emscripten/__main__.py

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ def get_build_paths(cross_build_dir=None, emsdk_cache=None):
7777
LOCAL_SETUP_MARKER = b"# Generated by Platforms/wasm/emscripten.py\n"
7878

7979

80+
@functools.cache
8081
def validate_emsdk_version(emsdk_cache):
8182
"""Validate that the emsdk cache contains the required emscripten version."""
8283
if emsdk_cache is None:
84+
print("Build using emcc from current environment.")
8385
return
8486
required_version = required_emscripten_version()
8587
emsdk_env = emsdk_activate_path(emsdk_cache)
@@ -530,7 +532,6 @@ def configure_emscripten_python(context, working_dir):
530532
@subdir("host_dir")
531533
def make_emscripten_python(context, working_dir):
532534
"""Run `make` for the emscripten/host build."""
533-
validate_emsdk_version(context.emsdk_cache)
534535
call(
535536
["make", "--jobs", str(cpu_count()), "all"],
536537
env=updated_env({}, context.emsdk_cache),
@@ -541,6 +542,26 @@ def make_emscripten_python(context, working_dir):
541542
subprocess.check_call([exec_script, "--version"])
542543

543544

545+
def run_emscripten_python(context):
546+
"""Run the built emscripten Python."""
547+
host_dir = context.build_paths["host_dir"]
548+
exec_script = host_dir / "python.sh"
549+
if not exec_script.is_file():
550+
print(
551+
f"Cannot find {exec_script}; "
552+
"has the emscripten Python been built?",
553+
file=sys.stderr,
554+
)
555+
sys.exit(1)
556+
557+
args = context.args
558+
# Strip the "--" separator if present
559+
if args and args[0] == "--":
560+
args = args[1:]
561+
562+
os.execv(str(exec_script), [str(exec_script)] + args)
563+
564+
544565
def build_target(context):
545566
"""Build one or more targets."""
546567
steps = []
@@ -550,12 +571,14 @@ def build_target(context):
550571
make_build_python,
551572
])
552573
if context.target in {"host", "all"}:
553-
steps.extend([
554-
make_emscripten_libffi,
555-
make_mpdec,
556-
configure_emscripten_python,
557-
make_emscripten_python,
558-
])
574+
steps.extend(
575+
[
576+
make_emscripten_libffi,
577+
make_mpdec,
578+
configure_emscripten_python,
579+
make_emscripten_python,
580+
]
581+
)
559582

560583
for step in steps:
561584
step(context)
@@ -581,6 +604,18 @@ def clean_contents(context):
581604
print(f"🧹 Deleting generated {LOCAL_SETUP} ...")
582605

583606

607+
def add_cross_build_dir_option(subcommand):
608+
subcommand.add_argument(
609+
"--cross-build-dir",
610+
action="store",
611+
default=os.environ.get("CROSS_BUILD_DIR"),
612+
dest="cross_build_dir",
613+
help="Path to the cross-build directory "
614+
f"(default: {DEFAULT_CROSS_BUILD_DIR}). "
615+
"Can also be set with the CROSS_BUILD_DIR environment variable.",
616+
)
617+
618+
584619
def main():
585620
default_host_runner = "node"
586621

@@ -623,6 +658,17 @@ def main():
623658
make_host = subcommands.add_parser(
624659
"make-host", help="Run `make` for the host/emscripten"
625660
)
661+
run = subcommands.add_parser(
662+
"run",
663+
help="Run the built emscripten Python",
664+
)
665+
run.add_argument(
666+
"args",
667+
nargs=argparse.REMAINDER,
668+
help="Arguments to pass to the emscripten Python "
669+
"(use '--' to separate from run options)",
670+
)
671+
add_cross_build_dir_option(run)
626672
clean = subcommands.add_parser(
627673
"clean", help="Delete files and directories created by this script"
628674
)
@@ -651,25 +697,20 @@ def main():
651697
subcommand.add_argument(
652698
"--quiet",
653699
action="store_true",
654-
default=False,
700+
default="QUIET" in os.environ,
655701
dest="quiet",
656-
help="Redirect output from subprocesses to a log file",
657-
)
658-
subcommand.add_argument(
659-
"--cross-build-dir",
660-
action="store",
661-
default=None,
662-
dest="cross_build_dir",
663-
help="Path to the cross-build directory "
664-
f"(default: {DEFAULT_CROSS_BUILD_DIR})",
702+
help="Redirect output from subprocesses to a log file. "
703+
"Can also be set with the QUIET environment variable.",
665704
)
705+
add_cross_build_dir_option(subcommand)
666706
subcommand.add_argument(
667707
"--emsdk-cache",
668708
action="store",
669-
default=None,
709+
default=os.environ.get("EMSDK_CACHE"),
670710
dest="emsdk_cache",
671711
help="Path to emsdk cache directory. If provided, validates that "
672-
"the required emscripten version is installed.",
712+
"the required emscripten version is installed. "
713+
"Can also be set with the EMSDK_CACHE environment variable.",
673714
)
674715
for subcommand in configure_build, configure_host:
675716
subcommand.add_argument(
@@ -699,8 +740,6 @@ def main():
699740

700741
if context.emsdk_cache:
701742
context.emsdk_cache = Path(context.emsdk_cache).absolute()
702-
else:
703-
print("Build will use EMSDK from current environment.")
704743

705744
context.build_paths = get_build_paths(
706745
context.cross_build_dir, context.emsdk_cache
@@ -715,6 +754,7 @@ def main():
715754
"configure-host": configure_emscripten_python,
716755
"make-host": make_emscripten_python,
717756
"build": build_target,
757+
"run": run_emscripten_python,
718758
"clean": clean_contents,
719759
}
720760

0 commit comments

Comments
 (0)