-
Notifications
You must be signed in to change notification settings - Fork 22
/
compile_utils.py
257 lines (214 loc) · 6.73 KB
/
compile_utils.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
from __future__ import print_function
import os
import sys
from subprocess import check_output, CalledProcessError
def sys_exec(cmd):
print(" ".join(cmd))
r = os.system(" ".join(cmd))
if r != 0:
sys.exit(r)
def is_uptodate(outfile, depfiles=None):
try:
outmtime = os.path.getmtime(outfile)
except OSError:
return False
if depfiles is None:
try:
depfiles = get_depfiles(outfile)
except IOError:
return False
for depfn in depfiles:
try:
if os.path.getmtime(depfn) > outmtime:
return False
except OSError:
return False
return True
def get_cc_outfilename(infile):
# Kind of custom, not totally bullet-proof, but it's ok
# for our most common input, like "../musicplayer.cpp".
fn = os.path.splitext(infile)[0]
if fn.startswith("../"):
fn = fn[3:]
else:
fn = "++" + fn
fn = fn.replace("/", "+_")
fn = fn.replace("..", "+.")
return fn + ".o"
def get_depfilename(outfile):
return outfile + ".deps"
def get_depfiles(outfile):
depfile = get_depfilename(outfile)
first_line = True
lastLine = False
fileList = []
for line in open(depfile):
line = line.strip()
if not line: continue
assert not lastLine
if first_line:
assert line.startswith(outfile + ": ")
line = line[len(outfile) + 2:]
first_line = False
if line[-2:] == " \\":
line = line[:-2]
else:
lastLine = True
fileList += line.split()
assert lastLine
return fileList
def get_mtime(filename):
return os.path.getmtime(filename)
LDFLAGS = os.environ.get("LDFLAGS", "").split()
def link(outfile, infiles, options):
if "--weak-linking" in options:
idx = options.index("--weak-linking")
if sys.platform == "darwin":
options[idx:idx + 1] = ["-undefined", "dynamic_lookup"]
else:
options[idx:idx + 1] = []
if is_uptodate(outfile, depfiles=infiles):
print("up-to-date:", outfile)
return
if sys.platform == "darwin":
sys_exec(
#["libtool", "-dynamic", "-o", outfile] +
["cc"] +
infiles +
options +
LDFLAGS +
["-lc"] +
["-o", outfile]
)
else:
sys_exec(
["cc"] + # use "cc" instead of "ld", to be sure that we have the right options
["-L/usr/local/lib"] +
infiles +
options +
LDFLAGS +
["-lc"] +
["-shared", "-o", outfile]
)
def link_exec(outfile, infiles, options):
options += ["-lc", "-lc++"]
if is_uptodate(outfile, depfiles=infiles):
print("up-to-date: %s" % outfile)
return
sys_exec(
["cc"] +
infiles +
options +
LDFLAGS +
["-o", outfile]
)
CFLAGS = os.environ.get("CFLAGS", "").split()
CFLAGS += ["-fpic"]
def cc_single(infile, options):
options = list(options)
ext = os.path.splitext(infile)[1]
if ext in [".cpp", ".mm"]:
options += ["-std=c++11"]
outfilename = get_cc_outfilename(infile)
depfilename = get_depfilename(outfilename)
if is_uptodate(outfilename):
print("up-to-date: %s" % outfilename)
return
sys_exec(
["cc"] + options + CFLAGS +
["-c", infile, "-o", outfilename, "-MMD", "-MF", depfilename]
)
def cc(files, options):
for f in files:
cc_single(f, options)
LinkPython = False
UsePyPy = False
Python3 = True
ffmpeg_packages = ['libavutil', 'libavformat', 'libavcodec', 'libswresample']
def find_exec_in_path(exec_name):
"""
:param str exec_name:
:return: yields full paths
:rtype: list[str]
"""
if "PATH" not in os.environ:
return
for p in os.environ["PATH"].split(":"):
pp = "%s/%s" % (p, exec_name)
if os.path.exists(pp):
yield pp
def get_pkg_config(pkg_config_args, *packages):
"""
:param str|list[str] pkg_config_args: e.g. "--cflags"
:param str packages: e.g. "python3"
:rtype: list[str]|None
"""
if not isinstance(pkg_config_args, (tuple, list)):
pkg_config_args = [pkg_config_args]
# Maybe we have multiple pkg-config, and maybe some of them finds it.
for pp in find_exec_in_path("pkg-config"):
try:
cmd = [pp] + list(pkg_config_args) + list(packages)
#print(" ".join(cmd))
out = check_output(cmd, stderr=open(os.devnull, "wb"))
return out.strip().decode("utf8").split()
except CalledProcessError:
pass
return None
def get_python_linkopts():
flags = []
if LinkPython:
# TODO: also python-config, like in ..._ccopts
link_opts = get_pkg_config("--libs", "python3" if Python3 else "python")
assert link_opts is not None, "pkg-config failed"
flags += link_opts
else:
flags += ["--weak-linking"]
pkg_flags = get_pkg_config("--libs", *ffmpeg_packages)
if pkg_flags is not None:
flags += pkg_flags
else:
flags += [
"-lavutil",
"-lavformat",
"-lavcodec",
"-lswresample",
"-lportaudio"]
pkg_flags = get_pkg_config("--libs", "portaudio")
if pkg_flags is not None:
flags += pkg_flags
else:
flags += ["-lportaudio"]
return flags
def get_python_ccopts():
flags = []
if UsePyPy:
# TODO use pkg-config or so
flags += ["-I", "/usr/local/Cellar/pypy/1.9/include"]
else:
try:
out = check_output(["python3-config" if Python3 else "python-config", "--cflags"])
flags += out.strip().decode("utf8").split()
except (CalledProcessError, OSError):
pkg_flags = get_pkg_config("--cflags", "python3" if Python3 else "python")
if pkg_flags is not None:
flags += pkg_flags
else:
# fallback to some defaults
if Python3:
flags += [
"-I", "/usr/local/opt/python3/Frameworks/Python.framework/Versions/3.6/Headers", # mac
"-I", "/usr/include/python3.6"
]
else:
flags += [
"-I", "/System/Library/Frameworks/Python.framework/Headers", # mac
"-I", "/usr/include/python2.7", # common linux/unix
]
pkg_flags = get_pkg_config("--cflags", *ffmpeg_packages)
if pkg_flags is not None:
flags += pkg_flags
else:
# fallback to some defaults
flags += ["-I", "/usr/local/opt/ffmpeg/include"]
return flags