Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions pyodide_build/build_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import functools
import os
import re
import shutil
import subprocess
import sys
from collections.abc import Iterator, Sequence
Expand All @@ -14,7 +15,12 @@
from packaging.tags import Tag, compatible_tags, cpython_tags

from pyodide_build import __version__
from pyodide_build.common import default_xbuildenv_path, search_pyproject_toml, to_bool
from pyodide_build.common import (
IS_WIN,
default_xbuildenv_path,
search_pyproject_toml,
to_bool,
)

RUST_BUILD_PRELUDE = """
rustup default ${RUST_TOOLCHAIN}
Expand Down Expand Up @@ -292,8 +298,11 @@ def emscripten_version() -> str:

def get_emscripten_version_info() -> str:
"""Extracted for testing purposes."""
emcc = shutil.which("emcc")
if not emcc:
raise FileNotFoundError
return subprocess.run(
["emcc", "-v"], capture_output=True, encoding="utf8", check=True
[emcc, "-v"], capture_output=True, encoding="utf8", check=True
).stderr


Expand Down Expand Up @@ -321,23 +330,38 @@ def activate_emscripten_env(emsdk_dir: Path) -> dict[str, str]:
dict[str, str]
Dictionary of environment variables set by emsdk_env.sh
"""
emsdk_env_script = emsdk_dir / "emsdk_env.sh"
emsdk_env_script_filename = "emsdk_env.bat" if IS_WIN else "emsdk_env.sh"
emsdk_env_script = emsdk_dir / emsdk_env_script_filename
if not emsdk_env_script.exists():
raise FileNotFoundError(f"emsdk_env.sh not found at {emsdk_env_script}")
raise FileNotFoundError(
f"{emsdk_env_script_filename} not found at {emsdk_env_script}"
)

# Source emsdk_env.sh and capture the resulting environment
result = subprocess.run(
["bash", "-c", f"source {emsdk_env_script} > /dev/null 2>&1 && env"],
capture_output=True,
encoding="utf8",
check=True,
)
if IS_WIN:
# Passing args as a string, as otherwise shell is misinterpreting the command with quotes,
# resulting in no output.
result = subprocess.run(
f'cmd /c call "{emsdk_env_script}" > nul 2>&1 && set',
capture_output=True,
encoding="utf8",
check=True,
)
else:
result = subprocess.run(
["bash", "-c", f'source "{emsdk_env_script}" > /dev/null 2>&1 && env'],
capture_output=True,
encoding="utf8",
check=True,
)

# Parse the environment variables from output
env_vars: dict[str, str] = {}
for line in result.stdout.splitlines():
if "=" in line:
key, _, value = line.partition("=")
# On Windows it's 'Path'.
key = key.upper()
if key in EMSDK_ENV_VARS:
env_vars[key] = value

Expand Down
17 changes: 9 additions & 8 deletions pyodide_build/xbuildenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,20 +540,21 @@ def install_emscripten(
self._clone_emscripten()

# Install the specified Emscripten version
emsdk = shutil.which("emsdk", path=emsdk_dir)
assert emsdk
subprocess.run(
["./emsdk", "install", "--build=Release", emscripten_version],
[emsdk, "install", "--build=Release", emscripten_version],
cwd=emsdk_dir,
check=True,
)

# Apply patches from xbuildenv/emsdk/patches directory to upstream/emscripten
try:
subprocess.run(
f"cat {patches_dir}/*.patch | patch -p1 --verbose",
check=True,
shell=True,
cwd=emscripten_root,
)
for patch_file in patches_dir.glob("*.patch"):
subprocess.check_call(
["git", "apply", "--verbose", str(patch_file)],
cwd=emscripten_root,
)
except subprocess.CalledProcessError as e:
raise RuntimeError(
f"Failed to apply Emscripten patches. This may occur if the Emscripten version "
Expand All @@ -565,7 +566,7 @@ def install_emscripten(
# Activate the specified Emscripten version
subprocess.run(
[
"./emsdk",
emsdk,
"activate",
"--embedded",
"--build=Release",
Expand Down
Loading