-
Notifications
You must be signed in to change notification settings - Fork 17
/
configure
executable file
·428 lines (340 loc) · 10.5 KB
/
configure
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env python3
import sys
import os
import platform
import getopt
import subprocess
import errno
import shlex
LOG = None
VERBOSE = False
REQUIRE_ALL = False
class CheckCallException(Exception):
pass
# standard line print
def lprint(x):
print(x)
LOG.write(x + "\n")
# verbose print
def vprint(x=""):
if VERBOSE:
print(x)
LOG.write(x + "\n")
# immediate (no newline) print
def iprint(x):
sys.stdout.write(x)
sys.stdout.flush()
LOG.write(x)
class IniParser:
def __init__(self, f):
self.__d = {}
sectiondata = None
for x in f.readlines():
x = x.replace("\r\n", "").replace("\n", "")
xs = x.strip()
if xs == "" or xs.startswith("#"):
continue
if x.startswith("[") and x.endswith("]"):
sectiondata = {}
keydata = []
self.__d[x[1:-1]] = (sectiondata, keydata)
continue
key, value = x.split("=", 1)
sectiondata[key] = value
keydata.append((key, value))
def has_key(self, key):
return key in self.__d
def get(self, key, value=None):
if key not in self.__d:
return value
return self[key]
def __getitem__(self, key):
return self.__d[key][0]
def keys(self, key):
return self.__d[key][1]
class ConfigureIniParser(IniParser):
def __init__(self, f):
super().__init__(f)
self.modules = {}
self.buildorder = []
self.updatemodules(self.keys("modules"))
self.selectlibs = {}
for k, v in self.get("selectlibs", {}).items():
self.selectlibs[k] = v.split()
self.libs = {}
for x in self["core"]["libs"].split():
section = f"lib{x}"
if "run" in self[section]:
subsections = self[section]["run"].split(" ")
else:
subsections = [section]
self.libs[x] = []
for subsection in subsections:
self.libs[x].append(dict(self[subsection]))
self.osflags = {}
if self.has_key("osvars"):
for k, v in self.keys("osvars"):
self.osflags.setdefault(k, []).append(v)
self.options = self["options"]
def configprint(self):
vprint("--- config --------------------------------------------")
for x in dir(self):
if x.startswith("_"):
continue
v = getattr(self, x)
if not isinstance(v, list) and not isinstance(v, dict):
continue
vprint(f"{x!r:50}: {v!r}")
vprint("--- config --------------------------------------------")
def updatemodules(self, x, workspace = None):
for k, v in x:
if workspace and workspace != ".":
name = workspace + "/" + k
else:
name = k
self.buildorder.append(name)
self.modules[name] = v.split()
class MultiConfigureIniParser(ConfigureIniParser):
def __init__(self, files):
super().__init__(files[0][1])
for workspace, file in files[1:]:
c2 = IniParser(file)
if c2.has_key("modules"):
self.updatemodules(c2.keys("modules"), workspace)
if c2.has_key("options"):
self.options.update(c2["options"])
if c2.has_key("osvars"):
for k, v in c2.keys("osvars"):
self.osflags.setdefault(k, []).append(v)
def check_call(args):
vprint(f"invoking: {args!r}")
p = subprocess.Popen(args, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="latin-1")
stdout, stderr = p.communicate()
if p.returncode != 0:
raise CheckCallException(f"bad return code: {p.returncode}, stdout: {stdout!r} stderr: {stderr!r}")
vprint(f"return code: {p.returncode}, stdout: {stdout!r} stderr: {stderr!r}")
return stdout.split("\n")[0]
def pkgconfig(args):
return check_call(["pkg-config"] + args)
def check_pkgconfig(d):
package = d.get("pkgconfig")
if not package:
return
try:
pkgconfig(["--exists", package])
except CheckCallException:
return
libpath = pkgconfig(["--libs", package])
incpath = pkgconfig(["--cflags", package])
return libpath, incpath
def check_exec(d):
if "libexec" not in d or "incexec" not in d:
return
try:
libpath = check_call(shlex.split(d["libexec"]))
incpath = check_call(shlex.split(d["incexec"]))
except FileNotFoundError:
# already logged
return
libspec = d.get("libspec", "{}")
lib = libspec.replace("{}", libpath)
incspec = d.get("incspec", "{}")
inc = incspec.replace("{}", incpath)
return lib, inc
def librarycheck(libraries):
def findlibrarypaths(library, defn):
for x in defn:
if x.get("alwayspresent"):
return True
v = check_pkgconfig(x)
if v is not None:
return v
v = check_exec(x)
if v is not None:
return v
libsfound = []
output = []
for k in libraries:
iprint(f"checking for {k}... ")
ret = findlibrarypaths(k, libraries[k])
if not ret:
lprint("failed")
continue
libsfound.append(k)
if ret is not True:
lib, inc = ret
libline = f"LIB{k.upper()}={lib}"
incline = f"INC{k.upper()}={inc}"
output.append(libline)
output.append(incline)
lprint("ok")
if ret is not True:
vprint(f"library path: {libline}")
vprint(f"include path: {incline}")
return output, libsfound
def systemcheck(osflags):
output = []
iprint("checking for system... ")
system = platform.system()
lprint(system)
iprint("checking for pkg-config... ")
try:
pkgconfig(["--version"])
except FileNotFoundError:
lprint("not found")
lprint("pkg-config is required to continue, aborting!")
sys.exit(1)
lprint("ok")
for v in osflags.get(system, []):
output.append(v)
return output,
def modulecheck(modules, libsfound, buildorder, selectlibs, overrides):
defaultselections = {}
for k, v in selectlibs.items():
if k in overrides:
assert overrides[k] in libsfound
libsfound.append(k)
defaultselections[k] = overrides[k]
else:
for x in v:
if x in libsfound:
libsfound.append(k)
defaultselections[k] = x
break
building = set()
for k, v in modules.items():
for x in v:
if x not in libsfound:
break
else:
building.add(k)
notfound = set(filter(lambda x: not os.path.exists(x), building))
cantbuild = set(modules) - building
building = building - notfound
orderedbuild = []
for x in buildorder:
if x in building:
orderedbuild.append(x)
build = [f"DIRS={' '.join(orderedbuild)}"]
return build, orderedbuild, notfound, cantbuild, defaultselections
def writemakefile(inc, out, appendstart=None, appendend=None, silent=False):
with open(out, "w") as p:
p.write(f"## AUTOMATICALLY GENERATED -- EDIT {inc} INSTEAD\n\n")
if appendstart:
p.write("\n".join(appendstart))
p.write("\n")
with open(inc, "r") as f:
for l in f.readlines():
p.write(l)
if appendend:
p.write("\n".join(appendend))
p.write("\n")
if not silent:
lprint(f"configure: wrote {out}")
def writeconfigh(filename, modules, defaultselections):
with open(filename, "w") as f:
f.write("/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n")
for x in modules:
f.write(f"#define HAVE_{x.upper()}\n")
for k, v in defaultselections.items():
f.write(f"#define USE_{k.upper()}_{v.upper()}\n")
lprint(f"configure: wrote {filename}")
def configure(config, selectoverrides, workspaces):
# figure out any custom OS flags
flags, = systemcheck(config.osflags)
# find the libraries/includes we have and their paths
f, libsfound = librarycheck(config.libs)
for k, v in selectoverrides.items():
if not v in libsfound:
lprint(f"configure: can't set {k} to {v} as {v} was not found.")
return
flags = flags + f
# see which modules we can build
buildlines, building, notfound, cantbuild, defaultselections = modulecheck(config.modules, libsfound, config.buildorder, config.selectlibs, selectoverrides)
for k, v in defaultselections.items():
lprint(f"configure: selected {v} as {k}")
flags.append(f"LIB{k.upper()}=$(LIB{v.upper()})")
flags.append(f"INC{k.upper()}=$(INC{v.upper()})")
writemakefile("build.mk.in", "build.mk", appendend=flags + ["=".join(x) for x in config.options.items()] + ["DIRS=" + " ".join(building), "WORKSPACES=" + " ".join(workspaces)])
writeconfigh("config.h", libsfound, defaultselections)
lprint(f"configure: selected: {' '.join(building)}")
if len(notfound) > 0:
lprint(f"configure: couldn't find: {' '.join(notfound)}")
check_require_all()
if len(cantbuild) > 0:
lprint(f"configure: can't select: {' '.join(cantbuild)}")
check_require_all()
def check_require_all():
if REQUIRE_ALL:
lprint("configure: require-all selected, so failing")
sys.exit(1)
validopts = {}
def usage():
print()
print(f" Usage: {sys.argv[0]} [-h] [-v] [options]")
print()
print(" Additional options are:")
for k, v in validopts.items():
print(f" --with-{v[0]}=[{'|'.join(v[1])}]")
print(" -L [additional lib dir]")
print(" -I [additional include dir]")
print(" -m [additional module]")
print(" -R: require everything")
print(" -v: verbose")
def main():
global LOG, VERBOSE, REQUIRE_ALL
files, workspaces = [], []
for root, _, file_list in os.walk("."):
if "configure.ini" not in file_list:
continue
print(f"found workspace: {root}")
workspaces.append(root)
path = os.path.join(root, "configure.ini")
files.append( (root, open(path, "r")) )
local_path = os.path.join(root, "configure.ini.local")
if os.path.exists(local_path):
files.append( (root, open(local_path, "r")) )
config = MultiConfigureIniParser(files)
mopts = []
for k, v in config.selectlibs.items():
mopts.append(f"with-{k}=")
validopts[f"--with-{k}"] = (k, v)
try:
opts, args = getopt.getopt(sys.argv[1:], "hvcI:L:m:R", mopts)
except getopt.GetoptError as err:
print(str(err))
usage()
sys.exit(1)
overrides = {}
libs = []
includes = []
modules = []
for o, a in opts:
if o in validopts:
v = validopts[o]
if not a in v[1]:
usage()
return
overrides[v[0]] = a
elif o == "-h":
usage()
return
elif o == "-v":
VERBOSE = True
elif o == "-R":
REQUIRE_ALL = True
elif o == "-L":
libs.append(a)
elif o == "-I":
includes.append(a)
elif o == "-m":
modules.append(a)
else:
raise Exception(f"unknown option: {o!r}")
with open(".configure.log", "w") as LOG:
vprint(f"invoked as: {sys.argv!r}")
config.updatemodules([(x, "") for x in modules])
config.configprint()
configure(config, overrides, workspaces)
if __name__ == "__main__":
main()