forked from ubc-systopia/gosdt-guesses
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.py
104 lines (87 loc) · 3.83 KB
/
build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import pathlib
import platform
import distro
import subprocess
import sys
import os
import shutil
def setup(args) -> None:
"""
Interface of the cross-platform scikit-build script
:param args: A list of arguments passed to `setup.py`
"""
subprocess.run([sys.executable, "setup.py"] + args).check_returncode()
def delocate_wheel(args) -> None:
"""
Interface of `delocate-wheel` that repairs wheel files on macOS
:param args: A list of arguments passed to `delocate-wheel`
"""
subprocess.run(["delocate-wheel"] + args).check_returncode()
def auditwheel(args) -> None:
"""
Interface of `auditwheel` that repairs wheel files on Ubuntu
:param args: A list of arguments passed to "auditwheel"
"""
subprocess.run(["auditwheel"] + args).check_returncode()
def delvewheel(args) -> None:
"""
Interface of `delvewheel` that repairs wheel files on Windows
:param args: A list of arguments passed to `delvewheel`
"""
subprocess.run([sys.executable, "-m", "delvewheel"] + args).check_returncode()
def repair_wheel(wheel) -> None:
"""
Repair the generated wheel file by copying all required dynamic libraries to it
:param wheel: Path to the generated wheel file
"""
# Fetch the current operating system
system = platform.system()
# Repair the wheel
if system == "Darwin":
delocate_wheel(["-w", "dist", "-v", wheel])
elif system == "Linux":
distribution = distro.id()
if distribution == "ubuntu":
auditwheel(["repair", "-w", "dist", "--plat", "linux_x86_64", wheel])
elif distribution == "centos":
auditwheel(["repair", "-w", "dist", "--plat", "manylinux_2_17_x86_64", wheel])
# Remove the original wheel
# The fixed wheel has a difference file name
os.remove(wheel)
else:
print("Linux distribution {} is not supported by this script.".format(distribution))
raise EnvironmentError
elif system == "Windows":
search_path = str(pathlib.Path(os.getenv("VCPKG_INSTALLATION_ROOT")) / "installed\\x64-windows\\bin")
delvewheel(["repair", "--no-mangle-all", "--add-path", search_path, wheel, "-w", "dist"])
else:
print("{} is not supported.".format(system))
raise EnvironmentError
def remove_dir_if_exists(str) -> None:
if os.path.exists(str):
shutil.rmtree(str)
if __name__ == '__main__':
try:
print(">> Cleaning the garbage...")
remove_dir_if_exists("dist")
remove_dir_if_exists("gosdt.egg-info")
setup(["clean"])
print(">> Rebuilding the project from scratch...")
# `--py-limited-api=cp37` is needed otherwise installing the wheel file produced by the CentOS 7 docker
# on other Linux distributions, such as Ubuntu, leads to an error `Not a supported wheel on this platform`.
# On Windows, the value must match the version of the Python that builds the wheel file,
# otherwise, wheel files will have the same name, despite being generated by different Python installations.
api_version = "cp{}{}".format(sys.version_info.major, sys.version_info.minor)
print("Using the CPython API {}.".format(api_version))
setup(["bdist_wheel", "--py-limited-api={}".format(api_version), "--build-type=Release",
"-G", "Ninja", "--", "--", "-j{}".format(os.cpu_count())])
print(">> Adding required dynamic libraries to the wheel file...")
wheels = os.listdir("dist")
assert len(wheels) == 1, "The number of generated wheels is not 1. All wheels: {}.".format(wheels)
wheel = "dist/{}".format(wheels[0])
print("Wheel file to be fixed: {}.".format(wheel))
repair_wheel(wheel)
print("All done.")
exit(0)
except (EnvironmentError, subprocess.CalledProcessError):
exit(1)