@@ -77,9 +77,11 @@ def get_build_paths(cross_build_dir=None, emsdk_cache=None):
7777LOCAL_SETUP_MARKER = b"# Generated by Platforms/wasm/emscripten.py\n "
7878
7979
80+ @functools .cache
8081def 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 will use EMSDK 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" )
531533def 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,22 @@ 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 ("Emscripten not built" , file = sys .stderr )
551+ sys .exit (1 )
552+
553+ args = context .args
554+ # Strip the "--" separator if present
555+ if args and args [0 ] == "--" :
556+ args = args [1 :]
557+
558+ os .execv (str (exec_script ), [str (exec_script )] + args )
559+
560+
544561def build_target (context ):
545562 """Build one or more targets."""
546563 steps = []
@@ -581,15 +598,31 @@ def clean_contents(context):
581598 print (f"🧹 Deleting generated { LOCAL_SETUP } ..." )
582599
583600
601+ def add_cross_build_dir_option (subcommand ):
602+ subcommand .add_argument (
603+ "--cross-build-dir" ,
604+ action = "store" ,
605+ default = os .environ .get ("CROSS_BUILD_DIR" ),
606+ dest = "cross_build_dir" ,
607+ help = (
608+ "Path to the cross-build directory "
609+ f"(default: { DEFAULT_CROSS_BUILD_DIR } ). "
610+ "Can also be set with the CROSS_BUILD_DIR environment variable." ,
611+ ),
612+ )
613+
614+
584615def main ():
585616 default_host_runner = "node"
586617
587618 parser = argparse .ArgumentParser ()
588619 subcommands = parser .add_subparsers (dest = "subcommand" )
620+
589621 install_emscripten_cmd = subcommands .add_parser (
590622 "install-emscripten" ,
591623 help = "Install the appropriate version of Emscripten" ,
592624 )
625+
593626 build = subcommands .add_parser ("build" , help = "Build everything" )
594627 build .add_argument (
595628 "target" ,
@@ -605,24 +638,46 @@ def main():
605638 configure_build = subcommands .add_parser (
606639 "configure-build-python" , help = "Run `configure` for the build Python"
607640 )
641+
608642 make_mpdec_cmd = subcommands .add_parser (
609643 "make-mpdec" ,
610644 help = "Clone mpdec repo, configure and build it for emscripten" ,
611645 )
646+
612647 make_libffi_cmd = subcommands .add_parser (
613648 "make-libffi" ,
614649 help = "Clone libffi repo, configure and build it for emscripten" ,
615650 )
651+
616652 make_build = subcommands .add_parser (
617653 "make-build-python" , help = "Run `make` for the build Python"
618654 )
655+
619656 configure_host = subcommands .add_parser (
620657 "configure-host" ,
621- help = "Run `configure` for the host/emscripten (pydebug builds are inferred from the build Python)" ,
658+ help = (
659+ "Run `configure` for the host/emscripten "
660+ "(pydebug builds are inferred from the build Python)"
661+ ),
622662 )
663+
623664 make_host = subcommands .add_parser (
624665 "make-host" , help = "Run `make` for the host/emscripten"
625666 )
667+
668+ run = subcommands .add_parser (
669+ "run" ,
670+ help = "Run the built emscripten Python" ,
671+ )
672+ run .add_argument (
673+ "args" ,
674+ nargs = argparse .REMAINDER ,
675+ help = (
676+ "Arguments to pass to the emscripten Python "
677+ "(use '--' to separate from run options)" ,
678+ )
679+ )
680+ add_cross_build_dir_option (run )
626681 clean = subcommands .add_parser (
627682 "clean" , help = "Delete files and directories created by this script"
628683 )
@@ -651,26 +706,26 @@ def main():
651706 subcommand .add_argument (
652707 "--quiet" ,
653708 action = "store_true" ,
654- default = False ,
709+ default = "QUIET" in os . environ ,
655710 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 } )" ,
711+ help = (
712+ "Redirect output from subprocesses to a log file. "
713+ "Can also be set with the QUIET environment variable."
714+ ),
665715 )
716+ add_cross_build_dir_option (subcommand )
666717 subcommand .add_argument (
667718 "--emsdk-cache" ,
668719 action = "store" ,
669- default = None ,
720+ default = os . environ . get ( "EMSDK_CACHE" ) ,
670721 dest = "emsdk_cache" ,
671- help = "Path to emsdk cache directory. If provided, validates that "
672- "the required emscripten version is installed." ,
722+ help = (
723+ "Path to emsdk cache directory. If provided, validates that "
724+ "the required emscripten version is installed. "
725+ "Can also be set with the EMSDK_CACHE environment variable."
726+ ),
673727 )
728+
674729 for subcommand in configure_build , configure_host :
675730 subcommand .add_argument (
676731 "--clean" ,
@@ -679,10 +734,12 @@ def main():
679734 dest = "clean" ,
680735 help = "Delete any relevant directories before building" ,
681736 )
737+
682738 for subcommand in build , configure_build , configure_host :
683739 subcommand .add_argument (
684740 "args" , nargs = "*" , help = "Extra arguments to pass to `configure`"
685741 )
742+
686743 for subcommand in build , configure_host :
687744 subcommand .add_argument (
688745 "--host-runner" ,
@@ -699,8 +756,6 @@ def main():
699756
700757 if context .emsdk_cache :
701758 context .emsdk_cache = Path (context .emsdk_cache ).absolute ()
702- else :
703- print ("Build will use EMSDK from current environment." )
704759
705760 context .build_paths = get_build_paths (
706761 context .cross_build_dir , context .emsdk_cache
@@ -715,6 +770,7 @@ def main():
715770 "configure-host" : configure_emscripten_python ,
716771 "make-host" : make_emscripten_python ,
717772 "build" : build_target ,
773+ "run" : run_emscripten_python ,
718774 "clean" : clean_contents ,
719775 }
720776
0 commit comments