build-all.py - access pyodide for necessary variables, simplify debug build env

This commit is contained in:
Andrej730
2025-10-28 12:53:59 +05:00
parent d1ffb329c8
commit 89a6eac535
3 changed files with 19 additions and 35 deletions
+19 -24
View File
@@ -210,31 +210,26 @@ assert platform.system() == "Darwin" or not MAC_CROSS_COMPILE_INTEL
WASM = "wasm" in flags WASM = "wasm" in flags
"""Build WASM outside pyodide build environment.""" """Build WASM outside pyodide build environment."""
WASM_DEBUG = False
WASM_CMAKE_IS_USING_INIT_VARS = False WASM_CMAKE_IS_USING_INIT_VARS = False
if WASM: if WASM:
if "WASM_PYTHON_PATH" in os.environ:
wasm_python_path = os.environ["WASM_PYTHON_PATH"] def get_pyodide_config_var(var_name: str) -> str:
# Deduce version from path, assuming format .../python-X.Y.Z output = sp.check_output(["pyodide", "config", "get", var_name], encoding="utf-8").strip()
version_match = re.search(r"python-(\d+)\.(\d+)\.(\d+)", wasm_python_path) return output
assert version_match, f"Could not deduce python version from '{wasm_python_path}'"
python_version = version_match.group(1) if "PYODIDE_ROOT" not in os.environ:
os.environ["PYVERSION"] = python_version cecho("WARNING. Couldn't find 'PYODIDE_ROOT' in environment variables.", YELLOW)
os.environ["PYTHONINCLUDE"] = f"{wasm_python_path}/include/python{python_version.rpartition('.')[0]}" cecho("Assuming building wasm outside pyodide build environment and resetting necessary variables.", YELLOW)
os.environ["SIDE_MODULE_CFLAGS"] = "" os.environ["SIDE_MODULE_CFLAGS"] = get_pyodide_config_var("cflags")
# Required, otherwise library will compile as .a, not .so. os.environ["SIDE_MODULE_LDFLAGS"] = get_pyodide_config_var("ldflags")
os.environ["SIDE_MODULE_LDFLAGS"] = "-s SIDE_MODULE=1" # Override cmake toolchain for all `emcmake` calls,
# We're outside pyodide build environment, so need to provide toolchain file ourselves. # needed for shared libraries (resulting .so wrapper)
assert "WASM_TOOLCHAIN_FILE" in os.environ, "WASM_TOOLCHAIN_FILE must be set when WASM_PYTHON_PATH is provided" # and to ensure compilation is pyodide compatible (e.g. `-fwasm-exceptions` is used in compilation flags).
WASM_DEBUG = True os.environ["CMAKE_TOOLCHAIN_FILE"] = get_pyodide_config_var("cmake_toolchain_file")
required_vars = ( required_vars = (
# E.g. '3.13.2'.
"PYVERSION",
# 'include' folder in WASM-Python installation.
# e.g. '/pyodide/cpython/installs/python-3.13.2/include/python3.13'
"PYTHONINCLUDE",
"SIDE_MODULE_CFLAGS", "SIDE_MODULE_CFLAGS",
"SIDE_MODULE_LDFLAGS", "SIDE_MODULE_LDFLAGS",
"CMAKE_TOOLCHAIN_FILE",
) )
missing_vars = [v for v in required_vars if v not in os.environ] missing_vars = [v for v in required_vars if v not in os.environ]
assert not missing_vars, f"Some variables required for WASM compilation are missing: {', '.join(missing_vars)}" assert not missing_vars, f"Some variables required for WASM compilation are missing: {', '.join(missing_vars)}"
@@ -1459,9 +1454,6 @@ if "IfcOpenShell-Python" in targets:
if "wasm" in flags: if "wasm" in flags:
ADDITIONAL_ARGS = "-Wl,-undefined,suppress" ADDITIONAL_ARGS = "-Wl,-undefined,suppress"
# Override CMAKE_TOOLCHAIN_FILE because by default emscripten doesn't support building shared binaries.
if WASM_DEBUG:
os.environ["CMAKE_TOOLCHAIN_FILE"] = os.environ["WASM_TOOLCHAIN_FILE"]
# NOTE: We don't use `CXXFLAGS` for wrappers, so wrapper is compiled with different flags # NOTE: We don't use `CXXFLAGS` for wrappers, so wrapper is compiled with different flags
# (e.g. ` -fdata-sections` is missing, which is set by default for executables) # (e.g. ` -fdata-sections` is missing, which is set by default for executables)
@@ -1552,7 +1544,10 @@ if "IfcOpenShell-Python" in targets:
return module_dir return module_dir
if "wasm" in flags: if "wasm" in flags:
compile_python_wrapper(os.environ["PYVERSION"], os.environ["PYTHONINCLUDE"]) compile_python_wrapper(
run(["pyodide", "config", "get", "python_version"]),
run(["pyodide", "config", "get", "python_include_dir"]),
)
# Copy setup.py where pyodide build system expects it. # Copy setup.py where pyodide build system expects it.
shutil.copy(REPO_PATH / "pyodide" / "setup.py", REPO_PATH) shutil.copy(REPO_PATH / "pyodide" / "setup.py", REPO_PATH)
-1
View File
@@ -26,7 +26,6 @@ since it's pure cmake without any additional moving parts.
- setup pyodide environment, see above - setup pyodide environment, see above
- clone IfcOpenShell repo next to it to `IfcOpenShell` folder - clone IfcOpenShell repo next to it to `IfcOpenShell` folder
- setup debug build environment using `source pyodide/debug_build_env.sh /path/to/pyodide_root`
- run `python nix/build-all.py -wasm -py-313` in `IfcOpenShell` - run `python nix/build-all.py -wasm -py-313` in `IfcOpenShell`
- it will produce Python package in `IfcOpenShell/ifcopenshell` - it will produce Python package in `IfcOpenShell/ifcopenshell`
- run `pyodide build` - run `pyodide build`
-10
View File
@@ -1,10 +0,0 @@
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 <pyodide_root>"
exit 1
fi
export PYODIDE_ROOT=$(readlink -f "$1")
export PATH="$PYODIDE_ROOT/emsdk/emsdk:$PYODIDE_ROOT/emsdk/emsdk/node/22.16.0_64bit/bin:$PYODIDE_ROOT/emsdk/emsdk/upstream/emscripten:$PATH"
# build-all.py vars.
export WASM_PYTHON_PATH="$PYODIDE_ROOT/cpython/installs/python-3.13.2"
export WASM_TOOLCHAIN_FILE="$PYODIDE_ROOT/pyodide-build/pyodide_build/tools/cmake/Modules/Platform/Emscripten.cmake"