-
Notifications
You must be signed in to change notification settings - Fork 5
/
compile
executable file
·206 lines (162 loc) · 4.89 KB
/
compile
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python3
import argparse
import os
from os.path import dirname, join, isdir, abspath
import subprocess as sp
import shutil
import sys
import base64
import uuid
import signal
import threading
import time
DEFAULT_INSTALL_DIR = "install"
def get_cli_args(btllib_dir):
# Create parser
parser = argparse.ArgumentParser(description="Compile btllib.")
# Add "prefix" argument
default_install_dir = join(btllib_dir, DEFAULT_INSTALL_DIR)
parser.add_argument(
"--prefix",
"-p",
type=str,
default=default_install_dir,
help="Prefix of the installation path.",
)
return parser.parse_args()
def program_exists(program):
return sp.run([f"command -v {program}"], shell=True).returncode == 0
def install_temp_deps(venv_path):
deps = ["meson", "ninja", "cmake"]
missing_deps = []
for dep in deps:
if not program_exists(dep):
missing_deps.append(dep)
missing_deps = " ".join(missing_deps)
if len(missing_deps) > 0:
print(
f"Building virtual environment for missing dependencies: {missing_deps}",
flush=True,
)
os.makedirs(f"{os.path.dirname(venv_path)}", exist_ok=True)
# Create virtual environment
if isdir(venv_path):
print(
f"Using already existing virtual environment: {venv_path}", flush=True
)
else:
sp.run([f"python3 -m venv {venv_path}"], shell=True)
sp.run(
[
f"""
. {venv_path}/bin/activate &&
pip3 install {missing_deps} &&
deactivate;
"""
],
shell=True,
check=True,
)
else:
print("All build dependencies are present.", flush=True)
def export_python_flags():
python3_config = join(sys.exec_prefix, "bin", "python3-config")
# Get python flags
cflags = sp.run(
[f"{python3_config} --cflags"],
shell=True,
capture_output=True,
check=True,
text=True,
).stdout.strip()
ldflags = sp.run(
[f"{python3_config} --ldflags"],
shell=True,
capture_output=True,
check=True,
text=True,
).stdout.strip()
# Print flags
print(f"Passing the following CFLAGS to Python: {cflags}", flush=True)
print(f"Passing the following LDFLAGS to Python: {ldflags}", flush=True)
# Export flags
os.environ["BTLLIB_PYTHON_CFLAGS"] = cflags
os.environ["BTLLIB_PYTHON_LDFLAGS"] = ldflags
def process_terminate():
"""SIGTERM the calling process."""
os.kill(os.getpid(), signal.SIGTERM)
def watch_process(process):
"""SIGTERM the calling process if the observed process dies with an error."""
def _watch_process(process):
process.wait()
if process.returncode != 0:
print(f"{process.args} failed!")
process_terminate()
threading.Thread(target=_watch_process, args=(process,), daemon=True).start()
def autoclean(to_clean, btllib_dir):
process = sp.Popen([join(btllib_dir, "auto-cleanup"), to_clean])
watch_process(process)
def get_random_name():
return (
base64.urlsafe_b64encode(uuid.uuid4().bytes)
.rstrip(b"=")
.replace(b"-", b"")
.decode("ascii")
)
def get_build_dir(btllib_dir):
build_dir_name = f"btllib-build-{get_random_name()}"
if isdir("/tmp"):
prefix = "/tmp"
elif "TMPDIR" in os.environ:
prefix = os.environ["TMPDIR"]
else:
prefix = btllib_dir
return join(prefix, build_dir_name)
if __name__ == "__main__":
btllib_dir = abspath(dirname(__file__))
args = get_cli_args(btllib_dir)
os.chdir(btllib_dir)
build_dir = get_build_dir(btllib_dir)
autoclean(build_dir, btllib_dir)
# Print build dir
print(f"Building btllib at: {build_dir}", flush=True)
# Check if previous build dir exists and remove it
if isdir(build_dir):
print(f"Removing previous build dir: {build_dir}", flush=True)
shutil.rmtree(build_dir, ignore_errors=True)
venv_path = join(build_dir, "venv")
install_temp_deps(venv_path)
export_python_flags()
time.sleep(3)
ar = ''
lto = ''
if program_exists('gcc-ar'):
ar = 'export AR=gcc-ar &&'
elif program_exists('llvm-ar'):
ar = 'export AR=llvm-ar &&'
if len(ar) > 0:
lto = '-Db_lto=true'
sp.run(
[
f"""
if [ -f {venv_path}/bin/activate ]; then
. {venv_path}/bin/activate;
fi &&
{ar}
meson setup --buildtype release {lto} -Db_ndebug=true -Db_coverage=false --prefix={args.prefix} {build_dir} &&
cd {build_dir} &&
ninja install &&
cd .. &&
if [ -f {venv_path}/bin/activate ]; then
deactivate;
fi
"""
],
shell=True,
check=True,
)
print(
f"""
Build finished successfully!
btllib installation can be found at: {args.prefix}"""
)