diff --git a/cpython/Makefile b/cpython/Makefile index 8f6f11bd8..be5330b12 100644 --- a/cpython/Makefile +++ b/cpython/Makefile @@ -30,7 +30,7 @@ sysconfigdata: # the subfolder is written to pybuilddir.txt. cd $(PYBUILD) && _PYTHON_SYSCONFIGDATA_NAME=$(SYSCONFIG_NAME) _PYTHON_PROJECT_BASE=$(PYBUILD) $(HOSTPYTHON) -m sysconfig --generate-posix-vars $(eval PYBUILDDIR=$(PYBUILD)/`cat $(PYBUILD)/pybuilddir.txt`) - PYTHONPATH=$(PYBUILDDIR) python$(PYMAJOR).$(PYMINOR) adjust_sysconfig.py + PYODIDE_ROOT=$(PYODIDE_ROOT) PYTHONPATH=$(PYBUILDDIR) python$(PYMAJOR).$(PYMINOR) adjust_sysconfig.py mkdir -p $(PYINSTALL)/lib/python$(PYMAJOR).$(PYMINOR)/ mkdir -p $(SYSCONFIGDATA_DIR) cp $(PYBUILDDIR)/$(SYSCONFIG_NAME).py $(PYINSTALL)/lib/python$(PYMAJOR).$(PYMINOR)/ @@ -73,13 +73,13 @@ $(PYTARBALL): @GOT_SHASUM=`shasum --algorithm 256 $(PYTARBALL) | cut -f1 -d' '` \ && (echo $$GOT_SHASUM | grep -q $(PYTHON_ARCHIVE_SHA256)) \ || (\ - echo "Got unexpected shasum $$GOT_SHASUM" \ + rm $@ \ + && echo "Got unexpected shasum $$GOT_SHASUM" \ && echo "If you are updating the Python version, set PYTHON_ARCHIVE_SHA256 in Makefile.envs to this." \ && exit 1 \ ) - prepare-source: $(PYBUILD)/.patched $(PYBUILD)/.patched: $(PYTARBALL) diff --git a/cpython/adjust_sysconfig.py b/cpython/adjust_sysconfig.py index 903258539..7bc127650 100644 --- a/cpython/adjust_sysconfig.py +++ b/cpython/adjust_sysconfig.py @@ -1,7 +1,8 @@ import os -import pprint from textwrap import dedent +PYODIDE_ROOT = os.environ["PYODIDE_ROOT"] + def load_sysconfig(sysconfig_name: str): _temp = __import__(sysconfig_name, globals(), locals(), ["build_time_vars"], 0) @@ -9,11 +10,14 @@ def load_sysconfig(sysconfig_name: str): return config_vars, _temp.__file__ -def write_sysconfig(destfile: str, config_vars: dict[str, str]): +def write_sysconfig(destfile: str, fmted_config_vars: dict[str, str]): with open(destfile, "w", encoding="utf8") as f: f.write("# system configuration generated and used by the sysconfig module\n") + # Set PYODIDE_ROOT + f.write("import os\n") + f.write(f'PYODIDE_ROOT = os.environ.get("PYODIDE_ROOT", "{PYODIDE_ROOT}")\n') f.write("build_time_vars = ") - pprint.pprint(config_vars, stream=f) + f.write(fmted_config_vars) # at build time, packages that are looking for the Python includes and # libraries can get deceived by the values of platbase and # installed_base (and possibly others, but we haven't run into trouble @@ -44,10 +48,31 @@ def adjust_sysconfig(config_vars: dict[str, str]): CXX="c++", LDCXXSHARED="c++", ) + for [key, val] in config_vars.items(): + if not isinstance(val, str): + continue + # Make sysconfigdata relocatable. + # Replace all instances of "/path/to/pyodide" with "{PYODIDE_ROOT}" + val = val.replace(f"{PYODIDE_ROOT}", "{PYODIDE_ROOT}") + # If we made a replacement, then prefix the string with `--tofstring--` + # so we can convert it to an fstring below + if "PYODIDE_ROOT" in val: + val = "--tofstring--" + val + config_vars[key] = val + + +def format_sysconfig(config_vars: dict[str, str]) -> str: + fmted_config_vars = repr(config_vars) + # Make any string that begins with `--tofstring--` into an fstring and + # remove the prefix. + fmted_config_vars = fmted_config_vars.replace("'--tofstring--", "f'") + fmted_config_vars = fmted_config_vars.replace('"--tofstring--', 'f"') + return fmted_config_vars if __name__ == "__main__": sysconfig_name = os.environ["SYSCONFIG_NAME"] config_vars, file = load_sysconfig(sysconfig_name) adjust_sysconfig(config_vars) - write_sysconfig(file, config_vars) + fmted_config_vars = format_sysconfig(config_vars) + write_sysconfig(file, fmted_config_vars)