From d6e129d82dc78e5bb723175bb6557f1c8f07310f Mon Sep 17 00:00:00 2001 From: "Daniel A. Wozniak" Date: Sat, 21 Nov 2026 01:40:18 -0700 Subject: [PATCH] Route get_toolchain() through ppbt.archive_for(triplet) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pre-0.2.0 ppbt only exposed `ARCHIVE` which pointed at the host native toolchain, so a cross-arch request on an x86_64 host silently extracted the x86_64 tarball into `/toolchain/aarch64-linux-gnu/` and the downstream build used x86_64 gcc with an aarch64 target triplet. ppbt 0.2.0 adds `archive_for(triplet)` which resolves cross triplets through the `ppbt.toolchains` entry point to the matching `ppbt-cross-` sibling package. - Use `archive_for(triplet)` when the ppbt in use exposes it; fall back to `ARCHIVE` for compat with 0.1.x installs. - If the resolved archive doesn't exist (user hasn't installed the `ppbt[cross-]` extra), surface a clear error instead of silently extracting the host tarball — unless the target toolchain dir already exists from a prior run, in which case we return it. - Expose matching extras in pyproject.toml so `pip install relenv[toolchain-cross-aarch64-linux-gnu]` pulls in the sibling package. - Extend _mock_ppbt_module to simulate 0.2.0+ shape and cover three new cases: cross-triplet routed via archive_for, missing cross archive raises, missing archive + existing toolchain returns dir. --- pyproject.toml | 5 +- relenv/common.py | 34 +++++++++-- tests/test_common.py | 142 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 173 insertions(+), 8 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index d20c4011..2e619049 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,10 @@ Changelog = "https://relenv.readthedocs.io/en/latest/changelog.html" relenv = "relenv.__main__:main" [project.optional-dependencies] -toolchain = ["ppbt"] +toolchain = ["ppbt>=0.2.0"] +toolchain-cross-aarch64-linux-gnu = ["ppbt[cross-aarch64-linux-gnu]>=0.2.0"] +toolchain-cross-x86_64-linux-gnu = ["ppbt[cross-x86_64-linux-gnu]>=0.2.0"] +toolchain-cross-all = ["ppbt[cross-all]>=0.2.0"] [tool.setuptools] include-package-data = true diff --git a/relenv/common.py b/relenv/common.py index 62f32d5a..53369953 100644 --- a/relenv/common.py +++ b/relenv/common.py @@ -447,6 +447,12 @@ def get_toolchain( patch sys.platform to "linux"). This allows using existing toolchains without requiring ppbt to be installed. + When ``arch`` differs from the host, the cross toolchain is looked + up via ``ppbt.common.archive_for(triplet)`` (ppbt 0.2.0+). The + matching cross archive ships in a sibling ``ppbt-cross-`` + PyPI package and is discovered through the ``ppbt.toolchains`` entry + point. Install it via ``pip install ppbt[cross-]``. + :param arch: The architecture to get the toolchain for :type arch: str :param root: The root of the relenv working directories to search in @@ -475,13 +481,33 @@ def get_toolchain( except ImportError: # If toolchain already exists, use it even without ppbt return toolchain_path if toolchain_path.exists() else None - archive_attr = getattr(ppbt_common, "ARCHIVE", None) extract = getattr(ppbt_common, "extract_archive", None) - if archive_attr is None or not callable(extract): - raise RelenvException("ppbt.common missing ARCHIVE or extract_archive") + if not callable(extract): + raise RelenvException("ppbt.common missing extract_archive") + + archive_for = getattr(ppbt_common, "archive_for", None) + if callable(archive_for): + # ppbt 0.2.0+: resolve by target triplet so a cross-arch request + # pulls the archive out of the matching ppbt-cross-* sibling. + archive_path = pathlib.Path(archive_for(triplet)) + else: + # Pre-0.2.0 ppbt only shipped the host native archive. + archive_attr = getattr(ppbt_common, "ARCHIVE", None) + if archive_attr is None: + raise RelenvException("ppbt.common missing ARCHIVE or archive_for") + archive_path = pathlib.Path(archive_attr) + + if not archive_path.exists(): + # Archive isn't staged. For cross targets this means the + # ppbt-cross- extra isn't installed. Leave existing + # extracted toolchains usable rather than erroring. + if toolchain_path.exists(): + return toolchain_path + raise RelenvException( + f"Toolchain archive {archive_path} not available; pip install ppbt[cross-{triplet}] for cross-arch builds" + ) toolchain_root.mkdir(parents=True, exist_ok=True) - archive_path = pathlib.Path(archive_attr) archive_meta = _archive_metadata(archive_path) if toolchain_path.exists() and metadata and _manifest_matches(metadata, archive_meta): diff --git a/tests/test_common.py b/tests/test_common.py index dbfae985..b0c8b663 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -43,12 +43,22 @@ from tests._pytest_typing import mark_skipif, parametrize if TYPE_CHECKING: - from collections.abc import Callable + from collections.abc import Callable, Mapping -def _mock_ppbt_module(monkeypatch: pytest.MonkeyPatch, triplet: str, archive_path: pathlib.Path) -> None: +def _mock_ppbt_module( + monkeypatch: pytest.MonkeyPatch, + triplet: str, + archive_path: pathlib.Path, + *, + archives: Mapping[str, pathlib.Path] | None = None, +) -> None: """ Provide a lightweight ppbt.common stub so get_toolchain() skips the real extraction. + + By default emulates pre-0.2.0 ppbt (only ``ARCHIVE`` is exposed). + Pass ``archives={triplet: path, ...}`` to also expose the 0.2.0+ + ``archive_for`` API so the cross-triplet lookup path is exercised. """ stub_package = ModuleType("ppbt") stub_common = ModuleType("ppbt.common") @@ -60,10 +70,26 @@ def _mock_ppbt_module(monkeypatch: pytest.MonkeyPatch, triplet: str, archive_pat setattr(stub_common, "ARCHIVE", archive_path) + if archives is not None: + + def archive_for(t: str) -> pathlib.Path: + return archives.get(t, archive_path.parent / f"{t}.tar.xz") + + setattr(stub_common, "archive_for", archive_for) + def fake_extract_archive(dest: str, archive: str) -> None: + # When archive_for is wired up (0.2.0+ emulation) derive the + # extracted dir from the archive filename so cross-triplet + # requests produce the right subdir. Otherwise fall back to + # the fixture's host triplet, matching pre-0.2.0 behaviour. + archive_name = pathlib.Path(archive).name + if archives is not None and archive_name.endswith(".tar.xz"): + extracted_triplet = archive_name[: -len(".tar.xz")] + else: + extracted_triplet = triplet dest_path = pathlib.Path(dest) dest_path.mkdir(parents=True, exist_ok=True) - (dest_path / triplet).mkdir(parents=True, exist_ok=True) + (dest_path / extracted_triplet).mkdir(parents=True, exist_ok=True) setattr(stub_common, "extract_archive", fake_extract_archive) @@ -203,6 +229,116 @@ def test_get_toolchain_no_arch(tmp_path: pathlib.Path, monkeypatch: pytest.Monke assert ret == data_dir / "toolchain" / triplet +def test_get_toolchain_cross_triplet_uses_archive_for(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None: + """ + A cross-arch request routes through ``ppbt.common.archive_for(triplet)``. + + Simulates ppbt 0.2.0+ with a sibling ``ppbt-cross-aarch64-linux-gnu`` + installed: the host archive is the x86_64 tarball, but asking for + ``arch="aarch64"`` picks the aarch64 tarball instead. + """ + data_dir = tmp_path / "data" + host_archive = tmp_path / "x86_64-linux-gnu.tar.xz" + host_archive.write_bytes(b"") + cross_archive = tmp_path / "aarch64-linux-gnu.tar.xz" + cross_archive.write_bytes(b"") + + monkeypatch.setattr(relenv.common, "DATA_DIR", data_dir, raising=False) + monkeypatch.setattr(sys, "platform", "linux") + monkeypatch.setattr( + relenv.common, + "get_triplet", + lambda machine=None, plat=None: f"{machine or 'x86_64'}-linux-gnu", + ) + monkeypatch.setenv("RELENV_TOOLCHAIN_CACHE", str(data_dir / "toolchain")) + + _mock_ppbt_module( + monkeypatch, + "x86_64-linux-gnu", + host_archive, + archives={ + "x86_64-linux-gnu": host_archive, + "aarch64-linux-gnu": cross_archive, + }, + ) + + ret = get_toolchain(arch="aarch64") + assert ret == data_dir / "toolchain" / "aarch64-linux-gnu" + # The native toolchain dir must NOT have been created as a side effect. + assert not (data_dir / "toolchain" / "x86_64-linux-gnu").exists() + + +def test_get_toolchain_missing_cross_archive_raises(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None: + """ + Requesting a cross triplet without the ppbt-cross-* extra installed raises. + + The 0.2.0 ``archive_for`` helper returns a path pointing into + ``ppbt-cross-``'s ``_toolchain/`` dir; if that package + isn't installed, the path doesn't exist and we surface a helpful + error instead of silently falling back to the host tarball. + """ + data_dir = tmp_path / "data" + host_archive = tmp_path / "x86_64-linux-gnu.tar.xz" + host_archive.write_bytes(b"") + + monkeypatch.setattr(relenv.common, "DATA_DIR", data_dir, raising=False) + monkeypatch.setattr(sys, "platform", "linux") + monkeypatch.setattr( + relenv.common, + "get_triplet", + lambda machine=None, plat=None: f"{machine or 'x86_64'}-linux-gnu", + ) + monkeypatch.setenv("RELENV_TOOLCHAIN_CACHE", str(data_dir / "toolchain")) + + # archive_for maps the cross triplet to a path that deliberately + # doesn't exist, mimicking "ppbt-cross-aarch64-linux-gnu not + # installed". + _mock_ppbt_module( + monkeypatch, + "x86_64-linux-gnu", + host_archive, + archives={"x86_64-linux-gnu": host_archive}, + ) + + with pytest.raises(RelenvException) as exc: + get_toolchain(arch="aarch64") + assert "aarch64-linux-gnu" in str(exc.value) + assert "ppbt[cross-aarch64-linux-gnu]" in str(exc.value) + + +def test_get_toolchain_missing_archive_but_existing_dir( + tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """ + Missing archive + previously-extracted toolchain → return the dir, no error. + + Lets users keep running against a toolchain they already extracted + even if they later uninstall the ppbt extra that supplied it. + """ + data_dir = tmp_path / "data" + triplet = "aarch64-linux-gnu" + existing = data_dir / "toolchain" / triplet + existing.mkdir(parents=True) + + host_archive = tmp_path / "x86_64-linux-gnu.tar.xz" + host_archive.write_bytes(b"") + + monkeypatch.setattr(relenv.common, "DATA_DIR", data_dir, raising=False) + monkeypatch.setattr(sys, "platform", "linux") + monkeypatch.setattr(relenv.common, "get_triplet", lambda machine=None, plat=None: triplet) + monkeypatch.setenv("RELENV_TOOLCHAIN_CACHE", str(data_dir / "toolchain")) + + _mock_ppbt_module( + monkeypatch, + "x86_64-linux-gnu", + host_archive, + archives={"x86_64-linux-gnu": host_archive}, # no aarch64 archive + ) + + ret = get_toolchain(arch="aarch64") + assert ret == existing + + WriteMode = Literal["w:gz", "w:xz", "w:bz2", "w"]