Skip to content

Commit 0a9c2e8

Browse files
scripts/test.py: cleanup of pyodide_setup().
Use latest available Python package pyodide-build. Run `git pull` in emsdk checkout to get latest version. Improved diagnostics in build command. Removed old code that patches emsdk/upstream/bin/wasm-opt, as no longer required.
1 parent 29330c0 commit 0a9c2e8

File tree

1 file changed

+36
-90
lines changed

1 file changed

+36
-90
lines changed

scripts/test.py

Lines changed: 36 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,10 @@ def build_pyodide_wheel(pyodide_build_version=None):
474474
#
475475
env_extra['PYMUPDF_SETUP_MUPDF_TESSERACT'] = '0'
476476
setup = pyodide_setup(pymupdf_dir, pyodide_build_version=pyodide_build_version)
477-
command = f'{setup} && pyodide build --exports whole_archive'
477+
command = f'{setup} && echo "### Running pyodide build" && pyodide build --exports whole_archive'
478+
479+
command = command.replace(' && ', '\n && ')
480+
478481
run(command, env_extra=env_extra)
479482

480483
# Copy wheel into `wheelhouse/` so it is picked up as a workflow
@@ -499,23 +502,32 @@ def pyodide_setup(
499502
clean:
500503
If true we create an entirely new environment. Otherwise
501504
we reuse any existing emsdk repository and venv.
505+
pyodide_build_version:
506+
Version of Python package pyodide-build; if None we use latest
507+
available version.
508+
2025-02-13: pyodide_build_version='0.29.3' works.
509+
510+
The returned command does the following:
502511
503-
* Clone emsdk repository to `pipcl_emsdk` if not already present.
504-
* Create and activate a venv `pipcl_venv_pyodide` if not already present.
512+
* Checkout latest emsdk from https://github.com/emscripten-core/emsdk.git:
513+
* Clone emsdk repository to `emsdk` if not already present.
514+
* Run `git pull -r` inside emsdk checkout.
515+
* Create venv `venv_pyodide_<python_version>` if not already present.
516+
* Activate venv `venv_pyodide_<python_version>`.
505517
* Install/upgrade package `pyodide-build`.
506518
* Run emsdk install scripts and enter emsdk environment.
507-
* Replace emsdk/upstream/bin/wasm-opt
508-
(https://github.com/pyodide/pyodide/issues/4048).
509519
510520
Example usage in a build function:
511521
512-
command = pipcl_wasm.pyodide_setup()
522+
command = pyodide_setup()
513523
command += ' && pyodide build --exports pyinit'
514524
subprocess.run(command, shell=1, check=1)
515525
'''
516526
command = f'cd {directory}'
517527

518-
# Clone emsdk.
528+
# Clone/update emsdk. We always use the latest emsdk with `git pull`.
529+
#
530+
# 2025-02-13: this works: 2514ec738de72cebbba7f4fdba0cf2fabcb779a5
519531
#
520532
dir_emsdk = 'emsdk'
521533
if clean:
@@ -524,106 +536,40 @@ def pyodide_setup(
524536
# important to remove it here.
525537
shutil.rmtree('.pyodide-xbuildenv', ignore_errors=1)
526538
if not os.path.exists(f'{directory}/{dir_emsdk}'):
527-
command += f' && echo "### cloning emsdk.git"'
539+
command += f' && echo "### Cloning emsdk.git"'
528540
command += f' && git clone https://github.com/emscripten-core/emsdk.git {dir_emsdk}'
541+
command += f' && echo "### Updating checkout {dir_emsdk}"'
542+
command += f' && (cd {dir_emsdk} && git pull -r)'
543+
command += f' && echo "### Checkout {dir_emsdk} is:"'
544+
command += f' && (cd {dir_emsdk} && git show -s --oneline)'
529545

530546
# Create and enter Python venv.
531547
#
532-
# 2024-10-11: we only work with python-3.11; later versions fail with
533-
# pyodide-build==0.23.4 because `distutils` not available.
534-
if pyodide_build_version:
535-
python = sys.executable
536-
a, b = sys.version_info[:2]
537-
venv_pyodide = f'venv_pyodide_{a}.{b}'
538-
else:
539-
pyodide_build_version = '0.29.3'
540-
venv_pyodide = 'venv_pyodide_3.12'
541-
python = sys.executable
542-
if sys.version_info[:2] != (3, 12):
543-
log(f'Forcing use of python-3.12 because {sys.version=} is not 3.12.')
544-
python = 'python3.12'
548+
python = sys.executable
549+
venv_pyodide = f'venv_pyodide_{sys.version_info[0]}.{sys.version_info[1]}'
550+
545551
if not os.path.exists( f'{directory}/{venv_pyodide}'):
546-
command += f' && echo "### creating venv {venv_pyodide}"'
552+
command += f' && echo "### Creating venv {venv_pyodide}"'
547553
command += f' && {python} -m venv {venv_pyodide}'
548554
command += f' && . {venv_pyodide}/bin/activate'
549-
command += f' && echo "### running pip install ..."'
550-
command += f' && python -m pip install --upgrade pip wheel pyodide-build=={pyodide_build_version}'
551-
#command += f' && python -m pip install --upgrade pip wheel pyodide-build'
555+
command += f' && echo "### Installing Python packages."'
556+
command += f' && python -m pip install --upgrade pip wheel pyodide-build'
557+
if pyodide_build_version:
558+
command += f'=={pyodide_build_version}'
552559

553560
# Run emsdk install scripts and enter emsdk environment.
554561
#
555562
command += f' && cd {dir_emsdk}'
556563
command += ' && PYODIDE_EMSCRIPTEN_VERSION=$(pyodide config get emscripten_version)'
557-
command += ' && echo "### running ./emsdk install"'
564+
command += ' && echo "### PYODIDE_EMSCRIPTEN_VERSION is: $PYODIDE_EMSCRIPTEN_VERSION"'
565+
command += ' && echo "### Running ./emsdk install"'
558566
command += ' && ./emsdk install ${PYODIDE_EMSCRIPTEN_VERSION}'
559-
command += ' && echo "### running ./emsdk activate"'
567+
command += ' && echo "### Running ./emsdk activate"'
560568
command += ' && ./emsdk activate ${PYODIDE_EMSCRIPTEN_VERSION}'
561-
command += ' && echo "### running ./emsdk_env.sh"'
569+
command += ' && echo "### Running ./emsdk_env.sh"'
562570
command += ' && . ./emsdk_env.sh' # Need leading `./` otherwise weird 'Not found' error.
563571

564-
if pyodide_build_version:
565-
command += ' && echo "### Not patching emsdk"'
566-
else:
567-
# Make our returned command replace emsdk/upstream/bin/wasm-opt
568-
# with a script that does nothing, otherwise the linker
569-
# command fails after it has created the output file. See:
570-
# https://github.com/pyodide/pyodide/issues/4048
571-
#
572-
573-
def write( text, path):
574-
with open( path, 'w') as f:
575-
f.write( text)
576-
os.chmod( path, 0o755)
577-
578-
# Create a script that our command runs, that overwrites
579-
# `emsdk/upstream/bin/wasm-opt`, hopefully in a way that is
580-
# idempotent.
581-
#
582-
# The script moves the original wasm-opt to wasm-opt-0.
583-
#
584-
write(
585-
textwrap.dedent('''
586-
#! /usr/bin/env python3
587-
import os
588-
p = 'upstream/bin/wasm-opt'
589-
p0 = 'upstream/bin/wasm-opt-0'
590-
p1 = '../wasm-opt-1'
591-
if os.path.exists( p0):
592-
print(f'### {__file__}: {p0!r} already exists so not overwriting from {p!r}.')
593-
else:
594-
s = os.stat( p)
595-
assert s.st_size > 15000000, f'File smaller ({s.st_size}) than expected: {p!r}'
596-
print(f'### {__file__}: Moving {p!r} -> {p0!r}.')
597-
os.rename( p, p0)
598-
print(f'### {__file__}: Moving {p1!r} -> {p!r}.')
599-
os.rename( p1, p)
600-
'''
601-
).strip(),
602-
f'{directory}/wasm-opt-replace.py',
603-
)
604-
605-
# Create a wasm-opt script that basically does nothing, except
606-
# defers to the original script when run with `--version`.
607-
#
608-
write(
609-
textwrap.dedent('''
610-
#!/usr/bin/env python3
611-
import os
612-
import sys
613-
import subprocess
614-
if sys.argv[1:] == ['--version']:
615-
root = os.path.dirname(__file__)
616-
subprocess.run(f'{root}/wasm-opt-0 --version', shell=1, check=1)
617-
else:
618-
print(f'{__file__}: Doing nothing. {sys.argv=}')
619-
'''
620-
).strip(),
621-
f'{directory}/wasm-opt-1',
622-
)
623-
command += ' && ../wasm-opt-replace.py'
624-
625572
command += ' && cd ..'
626-
627573
return command
628574

629575

0 commit comments

Comments
 (0)