-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
180 lines (155 loc) · 5.06 KB
/
setup.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from setuptools import setup
import os, platform, subprocess
import shutil
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
class genericpy_bdist_wheel(_bdist_wheel):
def finalize_options(self):
_bdist_wheel.finalize_options(self)
self.root_is_pure = False
def get_tag(self):
python, abi, plat = _bdist_wheel.get_tag(self)
python, abi = "py3", "none"
if os.environ.get("CIBUILDWHEEL", "0") == "1":
# pypi does not allow linux_x86_64 wheels to be uploaded
if plat == "linux_x86_64":
plat = "manylinux2014_x86_64"
elif plat == "linux_aarch64":
plat = "manylinux2014_aarch64"
return python, abi, plat
cmdclass = {"bdist_wheel": genericpy_bdist_wheel}
# cd to `HiGHS` directory and build highs
# run:
# mkdir build
# cmake -S. -Bbuild -DFAST_BUILD=ON -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=highs_dist
# cmake --build build --config Release --parallel 6
# cmake --install build
# then package highs_dist directory
def build_highs():
this_directory = os.path.abspath(os.path.dirname(__file__))
highs_dir = os.path.join(this_directory, "HiGHS")
# if highs dir does not exist, clone it
if not os.path.exists(highs_dir):
subprocess.run(
[
"git",
"clone",
"--depth",
"1",
"--branch",
"v1.10.0",
"https://github.com/ERGO-Code/HiGHS.git",
],
cwd=this_directory,
check=True,
)
# build highs
build_dir = os.path.join(highs_dir, "build")
if not os.path.exists(build_dir):
os.makedirs(build_dir)
subprocess.run(
[
"cmake",
"-S.",
"-Bbuild",
"-DFAST_BUILD=ON",
"-DBUILD_SHARED_LIBS=ON",
"-DCMAKE_BUILD_TYPE=Release",
"-DCMAKE_INSTALL_PREFIX=highs_dist",
],
cwd=highs_dir,
check=True,
)
subprocess.run(
["cmake", "--build", "build", "--config", "Release", "--parallel", "6"],
cwd=highs_dir,
check=True,
)
subprocess.run(
["cmake", "--install", "build"],
cwd=highs_dir,
check=True,
)
build_highs()
this_directory = os.path.abspath(os.path.dirname(__file__))
long_description = """
highsbox is a python package that packs binary of [HiGHS](https://github.com/ERGO-Code/HiGHS)
"""
from tempfile import TemporaryDirectory
def patch_rpath(executable_path: str):
if platform.system() == "Darwin":
subprocess.run(
[
"install_name_tool",
"-add_rpath",
"@loader_path/../lib",
executable_path,
],
check=True,
)
# show the altered rpath
p = subprocess.run(
["otool", "-l", executable_path],
check=True,
capture_output=True,
text=True,
)
print(f"Output of otool:\n{p.stdout}")
elif platform.system() == "Linux":
subprocess.run(
[
"patchelf",
"--set-rpath",
"$ORIGIN/../lib",
executable_path,
],
check=True,
)
# show the altered rpath
p = subprocess.run(
["patchelf", "--print-rpath", executable_path],
check=True,
capture_output=True,
text=True,
)
print(f"Output of patchelf:\n{p.stdout}")
with TemporaryDirectory() as temp_dir:
base_dir = os.path.abspath(os.path.dirname(__file__))
highs_dir = os.path.join(this_directory, "HiGHS")
dist_name = "highs_dist"
shutil.copytree(
os.path.join(highs_dir, dist_name),
os.path.join(temp_dir, dist_name),
dirs_exist_ok=True,
)
# In manylinux container, the lib directory will be set as lib64, we need to change it to lib
lib64dir = os.path.join(temp_dir, dist_name, "lib64")
if os.path.exists(lib64dir):
shutil.move(lib64dir, os.path.join(temp_dir, dist_name, "lib"))
# patch rpath
# if os.name != "nt":
# executable_path = os.path.join(temp_dir, dist_name, "bin", "highs")
# patch_rpath(executable_path)
for fname in ["__init__.py", "__main__.py"]:
shutil.copy2(
os.path.join(base_dir, "src", fname), os.path.join(temp_dir, fname)
)
setup(
name="highsbox",
version="1.10.0",
cmdclass=cmdclass,
author="Yue Yang",
author_email="[email protected]",
url="https://github.com/metab0t/highsbox",
description="highsbox: binary distribution of HiGHS optimizer",
long_description=long_description,
long_description_content_type="text/markdown",
license="MIT",
packages=["highsbox"],
zip_safe=False,
package_dir={"highsbox": temp_dir},
package_data={
"highsbox": [
"highs_dist/**",
]
},
)