Debian Codegraph @

src/auditwheel/patcher.py

raw
1 from __future__ import annotations
2
3 import re
4 from itertools import chain
5 from shutil import which
6 from subprocess import CalledProcessError, check_call, check_output
7 from typing import TYPE_CHECKING
8
9 from auditwheel.wheeltools import android_api_level
10
11 if TYPE_CHECKING:
12 from pathlib import Path
13
14
15 class ElfPatcher:
16 def __init__(self, platform: str) -> None:
17 self.platform = platform
18
19 def replace_needed(self, file_name: Path, *old_new_pairs: tuple[str, str]) -> None:
20 raise NotImplementedError
21
22 def remove_needed(self, file_name: Path, *sonames: str) -> None:
23 raise NotImplementedError
24
25 def set_soname(self, file_name: Path, new_so_name: str) -> None:
26 raise NotImplementedError
27
28 def check_rpath_support(self) -> None:
29 # https://android.googlesource.com/platform/bionic/+/refs/heads/main/android-changes-for-ndk-developers.md
30 if self.platform.startswith("android") and android_api_level(self.platform) < 24:
31 msg = "Grafting libraries with RUNPATH requires API level 24 or higher"
32 raise ValueError(msg)
33
34 def set_rpath(self, file_name: Path, rpath: str) -> None:
35 raise NotImplementedError
36
37 def get_rpath(self, file_name: Path) -> str:
38 raise NotImplementedError
39
40 def clear_rpath(self, file_name: Path) -> None:
41 raise NotImplementedError
42
43
44 def _verify_patchelf() -> None:
45 """This function looks for the ``patchelf`` external binary in the PATH,
46 checks for the required version, and throws an exception if a proper
47 version can't be found. Otherwise, silence is golden
48 """
49 if not which("patchelf"):
50 msg = "Cannot find required utility `patchelf` in PATH"
51 raise ValueError(msg)
52 try:
53 version = check_output(["patchelf", "--version"]).decode("utf-8")
54 except CalledProcessError:
55 msg = "Could not call `patchelf` binary"
56 raise ValueError(msg) from None
57
58 m = re.match(r"patchelf\s+(\d+(.\d+)?)", version)
59 if m and tuple(int(x) for x in m.group(1).split(".")) >= (0, 14):
60 return
61 msg = f"patchelf {version} found. auditwheel repair requires patchelf >= 0.14."
62 raise ValueError(msg)
63
64
65 class Patchelf(ElfPatcher):
66 def __init__(self, platform: str = "") -> None:
67 super().__init__(platform)
68 _verify_patchelf()
69
70 def replace_needed(self, file_name: Path, *old_new_pairs: tuple[str, str]) -> None:
71 check_call(
72 [
73 "patchelf",
74 *chain.from_iterable(("--replace-needed", *pair) for pair in old_new_pairs),
75 file_name,
76 ],
77 )
78
79 def remove_needed(self, file_name: Path, *sonames: str) -> None:
80 check_call(
81 [
82 "patchelf",
83 *chain.from_iterable(("--remove-needed", soname) for soname in sonames),
84 file_name,
85 ],
86 )
87
88 def set_soname(self, file_name: Path, new_so_name: str) -> None:
89 check_call(["patchelf", "--set-soname", new_so_name, file_name])
90
91 def set_rpath(self, file_name: Path, rpath: str) -> None:
92 self.check_rpath_support()
93
94 set_args: list[str | Path] = ["patchelf", "--force-rpath", "--set-rpath", rpath, file_name]
95 if self.platform.startswith("android"):
96 # Android supports only RUNPATH, not RPATH.
97 set_args.remove("--force-rpath")
98
99 # we only want an RPATH, remove RUNPATH/RPATH altogether in a 1st pass
100 check_call(["patchelf", "--remove-rpath", file_name])
101 check_call(set_args)
102
103 def get_rpath(self, file_name: Path) -> str:
104 return check_output(["patchelf", "--print-rpath", file_name]).decode("utf-8").strip()
105
106 def clear_rpath(self, file_name: Path) -> None:
107 check_call(["patchelf", "--remove-rpath", file_name])