| 1 |
import os |
| 2 |
import subprocess |
| 3 |
from pathlib import Path |
| 4 |
from zipfile import ZipFile |
| 5 |
|
| 6 |
import pytest |
| 7 |
from elftools.elf.elffile import ELFFile |
| 8 |
|
| 9 |
from auditwheel.architecture import Architecture |
| 10 |
from auditwheel.elfutils import elf_read_dt_needed |
| 11 |
from auditwheel.libc import Libc |
| 12 |
from auditwheel.policy import ExternalReference |
| 13 |
from auditwheel.wheel_abi import analyze_wheel_abi |
| 14 |
|
| 15 |
HERE = Path(__file__).parent.resolve() |
| 16 |
android_dir = HERE / "android" |
| 17 |
|
| 18 |
# This wheel was generated from cibuildwheel's test_android.py::test_libcxx. It contains an |
| 19 |
# external reference to libc++_shared.so. |
| 20 |
libcxx_wheel = android_dir / "spam-0.1.0-cp313-cp313-android_24_arm64_v8a.whl" |
| 21 |
libcxx_module = "spam.cpython-313-aarch64-linux-android.so" |
| 22 |
|
| 23 |
|
| 24 |
def elf_read_tag(fn: Path, tag: str) -> str | None: |
| 25 |
with fn.open("rb") as f: |
| 26 |
elf = ELFFile(f) |
| 27 |
section = elf.get_section_by_name(".dynamic") |
| 28 |
if section: |
| 29 |
for t in section.iter_tags(): |
| 30 |
if t.entry.d_tag == f"DT_{tag.upper()}": |
| 31 |
return str(getattr(t, tag)) |
| 32 |
return None |
| 33 |
|
| 34 |
|
| 35 |
@pytest.mark.parametrize("ldpaths_methods", [["env"], ["arg"], ["env", "arg"]]) |
| 36 |
def test_libcxx(ldpaths_methods, tmp_path): |
| 37 |
wheelhouse = tmp_path / "wheelhouse" |
| 38 |
wheelhouse.mkdir() |
| 39 |
|
| 40 |
# The dummy libc++_shared.so files were generated using this command, where SUBDIR is either |
| 41 |
# "env" or "arg": |
| 42 |
# echo "void SUBDIR() {}" |
| 43 |
# | aarch64-linux-android24-clang -x c - -shared -o SUBDIR/libc++_shared.so |
| 44 |
ldpaths_args: list[str | Path] = [] |
| 45 |
if "arg" in ldpaths_methods: |
| 46 |
libcxx_hash = "1be9716c" |
| 47 |
ldpaths_args = ["--ldpaths", android_dir / "ldpaths/arg"] |
| 48 |
|
| 49 |
env = os.environ.copy() |
| 50 |
if "env" in ldpaths_methods: |
| 51 |
# AUDITWHEEL_LD_LIBRARY_PATH is searched before --ldpaths, so we overwrite the hash. |
| 52 |
libcxx_hash = "18b6a03d" |
| 53 |
env["AUDITWHEEL_LD_LIBRARY_PATH"] = str(android_dir / "ldpaths/env") |
| 54 |
|
| 55 |
subprocess.run( |
| 56 |
["auditwheel", "repair", "-w", wheelhouse, *ldpaths_args, libcxx_wheel], |
| 57 |
env=env, |
| 58 |
text=True, |
| 59 |
check=True, |
| 60 |
) |
| 61 |
output_wheels = list(wheelhouse.iterdir()) |
| 62 |
assert len(output_wheels) == 1 |
| 63 |
assert output_wheels[0].name == libcxx_wheel.name |
| 64 |
|
| 65 |
output_dir = tmp_path / "output" |
| 66 |
output_dir.mkdir() |
| 67 |
with ZipFile(output_wheels[0]) as zf: |
| 68 |
zf.extractall(output_dir) |
| 69 |
|
| 70 |
libs_dir = output_dir / "spam.libs" |
| 71 |
libcxx_path = libs_dir / f"libc++_shared-{libcxx_hash}.so" |
| 72 |
assert elf_read_tag(libcxx_path, "soname") == libcxx_path.name |
| 73 |
assert elf_read_tag(libcxx_path, "rpath") is None |
| 74 |
assert elf_read_tag(libcxx_path, "runpath") is None |
| 75 |
|
| 76 |
spam_path = output_dir / libcxx_module |
| 77 |
assert set(elf_read_dt_needed(spam_path)) == { |
| 78 |
# Included in the policy |
| 79 |
"libc.so", |
| 80 |
"libdl.so", |
| 81 |
"libm.so", |
| 82 |
# libpython dependency is normal on Android, so it should be left alone |
| 83 |
"libpython3.13.so", |
| 84 |
# Grafted library |
| 85 |
libcxx_path.name, |
| 86 |
} |
| 87 |
assert elf_read_tag(spam_path, "rpath") is None |
| 88 |
assert elf_read_tag(spam_path, "runpath") == f"$ORIGIN/{libs_dir.name}" |
| 89 |
|
| 90 |
|
| 91 |
def test_analyze_wheel_abi(): |
| 92 |
winfo = analyze_wheel_abi( |
| 93 |
Libc.ANDROID, |
| 94 |
Architecture.aarch64, |
| 95 |
libcxx_wheel, |
| 96 |
exclude=frozenset(), |
| 97 |
disable_isa_ext_check=False, |
| 98 |
allow_graft=True, |
| 99 |
) |
| 100 |
policy_name = "android_24_arm64_v8a" |
| 101 |
assert winfo.overall_policy.name == policy_name |
| 102 |
external_ref = ExternalReference( |
| 103 |
libs={"libc++_shared.so": None}, |
| 104 |
blacklist={}, |
| 105 |
policy=winfo.overall_policy, |
| 106 |
) |
| 107 |
assert winfo.external_refs[policy_name] == external_ref |
| 108 |
assert winfo.full_external_refs[Path(libcxx_module)] == winfo.external_refs |