Skip to content
This repository was archived by the owner on Aug 15, 2025. It is now read-only.

Commit d8ece6e

Browse files
committed
1 parent 86bbfa6 commit d8ece6e

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

aarch64_linux/embed_library.py

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,49 @@
11
#!/usr/bin/env python3
22

3-
from auditwheel.patcher import Patchelf
4-
from auditwheel.wheeltools import InWheelCtx
5-
from auditwheel.elfutils import elf_file_filter
6-
from auditwheel.repair import copylib
7-
from auditwheel.lddtree import lddtree
8-
from subprocess import check_call
93
import os
104
import shutil
115
import sys
6+
from subprocess import check_call
127
from tempfile import TemporaryDirectory
138

9+
from auditwheel.elfutils import elf_file_filter
10+
from auditwheel.lddtree import lddtree
11+
from auditwheel.patcher import Patchelf
12+
from auditwheel.repair import copylib
13+
from auditwheel.wheeltools import InWheelCtx
14+
1415

1516
def replace_tag(filename):
16-
with open(filename) as f:
17-
lines = f.read().split("\\n")
18-
for i,line in enumerate(lines):
19-
if not line.startswith("Tag: "):
20-
continue
21-
lines[i] = line.replace("-linux_", "-manylinux2014_")
22-
print(f'Updated tag from {line} to {lines[i]}')
17+
with open(filename) as f:
18+
lines = f.read().split("\\n")
19+
for i, line in enumerate(lines):
20+
if not line.startswith("Tag: "):
21+
continue
22+
lines[i] = line.replace("-linux_", "-manylinux2014_")
23+
print(f"Updated tag from {line} to {lines[i]}")
2324

24-
with open(filename, 'w') as f:
25-
f.write("\\n".join(lines))
25+
with open(filename, "w") as f:
26+
f.write("\\n".join(lines))
2627

2728

2829
class AlignedPatchelf(Patchelf):
2930
def set_soname(self, file_name: str, new_soname: str) -> None:
30-
check_call(['patchelf', '--page-size', '65536', '--set-soname', new_soname, file_name])
31+
check_call(
32+
["patchelf", "--page-size", "65536", "--set-soname", new_soname, file_name]
33+
)
3134

3235
def replace_needed(self, file_name: str, soname: str, new_soname: str) -> None:
33-
check_call(['patchelf', '--page-size', '65536', '--replace-needed', soname, new_soname, file_name])
36+
check_call(
37+
[
38+
"patchelf",
39+
"--page-size",
40+
"65536",
41+
"--replace-needed",
42+
soname,
43+
new_soname,
44+
file_name,
45+
]
46+
)
3447

3548

3649
def embed_library(whl_path, lib_soname, update_tag=False):
@@ -39,16 +52,16 @@ def embed_library(whl_path, lib_soname, update_tag=False):
3952
whl_name = os.path.basename(whl_path)
4053
tmp_whl_name = os.path.join(out_dir.name, whl_name)
4154
with InWheelCtx(whl_path) as ctx:
42-
torchlib_path = os.path.join(ctx._tmpdir.name, 'torch', 'lib')
43-
ctx.out_wheel=tmp_whl_name
55+
torchlib_path = os.path.join(ctx._tmpdir.name, "torch", "lib")
56+
ctx.out_wheel = tmp_whl_name
4457
new_lib_path, new_lib_soname = None, None
4558
for filename, _ in elf_file_filter(ctx.iter_files()):
46-
if not filename.startswith('torch/lib'):
59+
if not filename.startswith("torch/lib"):
4760
continue
4861
libtree = lddtree(filename)
49-
if lib_soname not in libtree['needed']:
62+
if lib_soname not in libtree["needed"]:
5063
continue
51-
lib_path = libtree['libs'][lib_soname]['path']
64+
lib_path = libtree["libs"][lib_soname]["path"]
5265
if lib_path is None:
5366
print(f"Can't embed {lib_soname} as it could not be found")
5467
break
@@ -58,15 +71,17 @@ def embed_library(whl_path, lib_soname, update_tag=False):
5871
if new_lib_path is None:
5972
new_lib_soname, new_lib_path = copylib(lib_path, torchlib_path, patcher)
6073
patcher.replace_needed(filename, lib_soname, new_lib_soname)
61-
print(f'Replacing {lib_soname} with {new_lib_soname} for {filename}')
74+
print(f"Replacing {lib_soname} with {new_lib_soname} for {filename}")
6275
if update_tag:
6376
# Add manylinux2014 tag
6477
for filename in ctx.iter_files():
65-
if os.path.basename(filename) != 'WHEEL':
78+
if os.path.basename(filename) != "WHEEL":
6679
continue
6780
replace_tag(filename)
6881
shutil.move(tmp_whl_name, whl_path)
6982

7083

71-
if __name__ == '__main__':
72-
embed_library(sys.argv[1], 'libgomp.so.1', len(sys.argv) > 2 and sys.argv[2] == '--update-tag')
84+
if __name__ == "__main__":
85+
embed_library(
86+
sys.argv[1], "libgomp.so.1", len(sys.argv) > 2 and sys.argv[2] == "--update-tag"
87+
)

0 commit comments

Comments
 (0)