Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 30 additions & 4 deletions relenv/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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-<triplet>``
PyPI package and is discovered through the ``ppbt.toolchains`` entry
point. Install it via ``pip install ppbt[cross-<triplet>]``.
:param arch: The architecture to get the toolchain for
:type arch: str
:param root: The root of the relenv working directories to search in
Expand Down Expand Up @@ -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-<triplet> 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):
Expand Down
142 changes: 139 additions & 3 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)

Expand Down Expand Up @@ -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-<triplet>``'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"]


Expand Down
Loading