/
configure.py
executable file
·495 lines (402 loc) · 16.5 KB
/
configure.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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#!/usr/bin/env python
from __future__ import print_function, absolute_import, division, generators, nested_scopes, with_statement
import os, sys, functools, argparse, re, copy
from os.path import join, getsize, relpath
import json
import subprocess
import platform
try:
from subprocess import DEVNULL # py3k
except ImportError:
import os
DEVNULL = open(os.devnull, 'wb')
## Output functions
verbose=1
# Strip directory, .cpp and add .o
def objname(arg):
return objdir + os.path.basename(arg)[:-4] + ".o"
def fatal_error(*args):
print(*args, file=sys.stderr)
sys.exit(1)
def verbose_print(level, *args):
if level <= verbose:
print(*args)
## Capture Git options
def progout(prog):
process = subprocess.Popen(prog, stdout=subprocess.PIPE,
stderr=DEVNULL)
(output, err) = process.communicate()
exit_code = process.wait()
return (output, err, exit_code)
#Check if prog can be run
def progexists(prog):
try:
subprocess.call([prog], stdout=DEVNULL,
stderr=DEVNULL)
except OSError as e:
return False
return True
def getGitVersion():
try:
(out, err, code) = progout(["git", "log", "-1", '--pretty=format:"%h (%ai)"'])
if code != 0 and err != "":
return "\"<unknown>\""
else:
return out.decode("utf-8")
except Exception:
return "\"<unknown>\""
# Reads JSON from string 'instr', with a more helpful error message
# than json.loads. 'outname' should give filename the JSON came from
def readjson(instr, outname):
try:
data = json.loads(instr)
return data
except ValueError as err:
print(err)
m = re.search("line (\d+) column (\d+)", str(err))
print("Invalid JSON in %s:\nError: '%s'\n"%(outname, err), file=sys.stderr)
if (m == None) or (m.group(2) == None) or (m.group(1) == None):
print(instr, file=sys.stderr)
else:
line = 0
for s in str.splitlines(instr):
print(s, file=sys.stderr)
if line == int(m.group(1)) - 1:
print("-"*(int(m.group(2)) - 1) + "^", file=sys.stderr)
line = line + 1
sys.exit(1)
# Get list of all files in a directory with .h or .hpp extensions
def getfilelist(dir):
filelist=[]
for root, dirs, files in os.walk(dir):
f=list(filter(lambda a:(a[-4:]==".hpp" or a[-2:]==".h"), files))
for i in range(len(f)):
f[i]=os.path.join(root, f[i])
filelist=filelist+f
return filelist
# Grab all JSON in a file which are wrapped in '/* JSON ... */'
def grabjsonfromfile(fname):
allfile=open(fname,"r").read()
jsonblocks = []
idx=allfile.find("/* JSON")
while idx>-1:
idxend=allfile[idx:].find("*/")+idx
if idxend==-1:
fatal_error("WARNING: found beginning of JSON comment but not end in "+fname)
gsp=str.expandtabs(allfile[idx+7:idxend].strip(), 4)
jsonblocks.append(readjson(gsp, fname))
allfile=allfile[idxend:]
idx=allfile.find("/* JSON")
return jsonblocks
##########################################################
##########################################################
##########################################################
parser = argparse.ArgumentParser(description="Minion Builder")
parser.add_argument('--domains64', action='store_const', const=["-DDOMAINS64"],
help='Enable 64-bit domains')
parser.add_argument('--wdeg', help='Enable wdeg heuristics (yes/no, default = yes)')
parser.add_argument('--quick', action='store_const', const=['-DQUICK_COMPILE'],
help='Quick build')
parser.add_argument('--static', action='store_const', const=["-static"],
help='Enable static build')
parser.add_argument('--compiler', help="Set compiler")
parser.add_argument('--constraints', help="Comma seperated list of constraints to use")
parser.add_argument('--assertions', action='store_const',
const=['-D_GLIBCXX_DEBUG', '-DMINION_DEBUG', '-DMORE_SEARCH_INFO'],
help='Enable assertions')
parser.add_argument('--debug', action='store_const',
const=['-D_GLIBCXX_DEBUG', '-DMINION_DEBUG', '-DMORE_SEARCH_INFO', '-g'],
help='Enable debugging (also adds --assertions)')
parser.add_argument('--print', action='store_const', const=['-DMINION_DEBUG_PRINT'],
help='Enable verbose debug printing')
parser.add_argument('--info', action='store_const', const=['-DMORE_SEARCH_INFO'],
help="Print info..")
parser.add_argument('--library', action='store_true',
help="Create libminion (requires buildsystem is 'make')")
parser.add_argument('--unoptimised', action='store_true',
help="Disable optimisation")
parser.add_argument('--basicopt', action='store_true',
help="Enable basic optimisation")
parser.add_argument('--profile', action='store_const', const=['-fno-inline', '-fno-inline-functions','-g'],
help='Enable compiler flags for profiling')
parser.add_argument('--extraflags', help="Add extra compiler options")
parser.add_argument('--setflags', help="Override all other compiler flags (experts only!)")
parser.add_argument('--buildsystem', help="Set build system. Current options: make (default), sh, tup, bat", default="make")
parser.add_argument('--sanitize', help="Run clang sanitizers (currently leak and address)", action='store_true')
arg = parser.parse_args()
currentdir = os.getcwd()
print(currentdir)
print(os.path.dirname(os.path.realpath(__file__)))
scriptdir = os.path.relpath(os.path.dirname(os.path.realpath(__file__)), currentdir)
outsrcdir = "src/"
objdir = "bin/"
if os.path.realpath(scriptdir) == os.path.realpath(currentdir):
fatal_error("ERROR: Can't build Minion in it's source directory\n",
"Run this script from another directory, for example:\n",
"mkdir bin; cd bin; ../configure.py")
if not os.path.exists(outsrcdir):
os.mkdir(outsrcdir)
if not os.path.exists(outsrcdir):
fatal_error("ERROR: Can't create dir ", outsrcdir)
if not os.path.exists(objdir):
os.mkdir(objdir)
if not os.path.exists(objdir):
fatal_error("ERROR: Can't create dir ", objdir)
verbose_print(1, "Current dir: " + currentdir)
verbose_print(1, "Minion base dir: " + scriptdir)
commandargs = ["-std=gnu++14", "-Wall", "-Wextra", "-Wno-unused-parameter", "-Wno-sign-compare",
"-I", scriptdir + "/minion", "-I", outsrcdir]
for c in ['domains64', 'quick', 'debug', 'assertions', 'print', 'info', 'profile', 'static']:
if getattr(arg, c) != None:
commandargs = commandargs + getattr(arg, c)
if getattr(arg, 'wdeg'):
if arg.wdeg == 'yes':
commandargs = commandargs + ["-DWDEG"]
elif arg.wdeg != 'no':
fatal_error("Invalid argument for --wdeg:" + arg.wdeg)
else:
commandargs = commandargs + ["-DWDEG"]
if arg.extraflags:
commandargs = commandargs + arg.extraflags.split()
if arg.setflags:
commandargs = arg.setflags.split()
if not arg.unoptimised:
if arg.profile:
commandargs = commandargs + ["-O"]
elif arg.basicopt:
commandargs = commandargs + ["-O"]
else:
if platform.system() == 'Darwin' and ("-static" not in commandargs):
commandargs = commandargs + ["-O3", "-mdynamic-no-pic", "-fomit-frame-pointer"]
else:
commandargs = commandargs + ["-O3", "-fomit-frame-pointer"]
if arg.library:
commandargs = commandargs + ["-fPIC"]
commandargs = commandargs + ["-DLIBMINION"]
if arg.sanitize:
compiler = "clang++"
commandargs = commandargs + ["-fsanitize=address"]
else:
if arg.compiler:
compiler = arg.compiler
else:
compiler = ""
if progexists('sccache'):
compiler = "sccache " + compiler
elif progexists('ccache'):
compiler = "ccache " + compiler
if progexists('c++'):
compiler = compiler + ' c++'
elif progexists('g++'):
compiler = compiler + ' g++'
elif progexists('clang++'):
compiler = compiler + ' clang++'
else:
fatal_error("Unable to find working C++ compiler.. please use --compiler")
if progexists('gold'):
commandargs = commandargs + ["-fuse-ld=gold","-Wl,--icf=all","-Wl,--gc-sections","-ffunction-sections"]
verbose_print(1, "Compiler flags" + str(commandargs))
constraints=[]
filelist = getfilelist(scriptdir+"/minion")
verbose_print(1, "Checking " + str(len(filelist)) + " files")
for fname in getfilelist(scriptdir+"/minion"):
verbose_print(2, fname)
data = grabjsonfromfile(fname)
verbose_print(2, data)
for d in data:
d["filename"] = fname
if d["type"] == "constraint":
constraints.append(d)
else:
fatal_error("Bad 'type' in JSON: "+str(d)+" in "+fname)
## Check for validity of JSON, collisions of internal_name
def validate_names(constraints):
internal_names = set()
names = set()
for c in constraints:
if c["internal_name"] in internal_names:
fatal_error("Duplicate internal_name: " + c["internal_name"])
internal_names.add(c["internal_name"])
if c["name"] in names:
fatal_error("Duplicate name: " + c["name"])
names.add(c["name"])
validate_names(constraints)
if verbose >= 1:
print("Found the following constraints:")
print([c["name"] for c in constraints])
## Filter list, if variable exists
if arg.constraints:
newcon = []
for c in list(set(arg.constraints.split(",") + ['true','false','pow','product'])):
found = False
for con in constraints:
if con["name"] == c or con["internal_name"] == c:
found = True
newcon = newcon + [con]
if not found:
fatal_error("Did not find constraint '"+c+"'")
constraints = newcon
constraints.sort(key=lambda d: sorted(d.items()))
constraintsrclist = []
constraintsrclistshort = []
constraint_chunk = 0
# Force first loop to create a file
constraint_current_chunk = 4
for c in constraints:
constraint_current_chunk += 1
if constraint_current_chunk >= 5:
constraint_chunk += 1
constraint_current_chunk = 0
srcname = "build_constraint_" + str(constraint_chunk) + ".cpp"
constraintsrclistshort.append("src/"+srcname)
srcname = outsrcdir+srcname
constraintsrclist.append(srcname)
with open(srcname, "w") as conout:
conout.write("// Minion constraint file\n")
varcount = sum([ m in ["read_list", "read_var", "read_2_vars"] for m in c["args"]])
with open(srcname, "a") as conout:
conout.write('#include "minion.h"\n')
conout.write('#include "../' + c["filename"] + '"\n\n')
conout.write("BUILD_CT("+c["internal_name"] + ", "+str(varcount)+")\n")
# Output buildstart
with open(outsrcdir+"BuildStaticStart.cpp", "w") as bss:
constraintsrclist.append(outsrcdir+"BuildStaticStart.cpp")
bss.write('#include "minion.h"\n')
for c in constraints:
bss.write("AbstractConstraint* build_constraint_" + c["internal_name"] + "(ConstraintBlob&);\n")
bss.write("AbstractConstraint* build_constraint(ConstraintBlob& b) {\n")
bss.write(" switch(b.constraint->type) {\n")
for c in constraints:
bss.write(" case " + c["internal_name"]+ ":\n")
bss.write(" return build_constraint_" + c["internal_name"] + "(b);\n")
bss.write(" default: abort();\n")
bss.write(" }\n}\n")
with open(outsrcdir+"constraint_defs.h", "w") as defs:
defs.write("ConstraintDef constraint_list[] = {\n")
for c in constraints:
args = "{" + ",".join(c["args"]) + "}"
defs.write('{{ "{name}", {inname}, {count}, {{ {args} }} }},\n'
.format(name=c["name"], inname=c["internal_name"],
count=len(c["args"]), args=args))
defs.write("};\n")
with open(outsrcdir+"ConstraintEnum.h", "w") as enum:
enum.write("#ifndef CONSTRAINT_ENUM_QWE\n")
enum.write("#define CONSTRAINT_ENUM_QWE\n")
enum.write("enum ConstraintType {\n")
for c in constraints:
enum.write(c["internal_name"]+",\n")
enum.write("};\n")
enum.write("#endif\n")
with open(outsrcdir+"BuildDefines.h", "w") as defs:
defs.write('#define GIT_VER ' + getGitVersion() + '\n')
minionlibsrclist = ['minion/BuildVariables.cpp',
'minion/BuildCSP.cpp',
'minion/buildConstraints.cpp',
'minion/commandline_parse.cpp',
'minion/debug_functions.cpp',
'minion/get_info.cpp',
'minion/info_dumps.cpp',
'minion/minion.cpp',
'minion/globals.cpp',
'minion/preprocess.cpp',
'minion/system/trigger_timer.cpp',
'minion/system/sha1.cpp',
'minion/help/help.cpp',
'minion/inputfile_parse/inputfile_parse.cpp',
'minion/dump_state.cpp',
'minion/parallel.cpp',
'minion/search_dump.cpp',
'minion/search_dump_sql.cpp',
'minion/command_search.cpp',
'minion/libwrapper.cpp'
]
minionbinsrclist = minionlibsrclist + ['minion/main.cpp']
if arg.buildsystem == "make":
outname = currentdir + "/Makefile"
def varsub(x):
return ' $(' + x + ') '
def vardef(x,value):
return x + "=" + value
elif arg.buildsystem == "sh":
outname = currentdir + "/build.sh"
def varsub(x):
return ' ${' + x + '} '
def vardef(x,value):
return x + '="' + value + '"'
elif arg.buildsystem == "tup":
outname = currentdir + "/Tupfile"
qw = ''
def varsub(x):
return ' $(' + x + ') '
def vardef(x,value):
return x + "=" + value
elif arg.buildsystem == "bat":
outname = currentdir + "/build.bat"
qw='"'
def varsub(x):
return ' %' + x + '% '
def vardef(x,value):
return 'set "' + x + '=' + value + '"'
else:
print("Build system not supported")
sys.exit(1)
constraintobjlist = [objname(x) for x in constraintsrclist]
minionlibobjlist = [objname(x) for x in minionlibsrclist]
minionbinobjlist = [objname(x) for x in minionbinsrclist]
print(minionbinsrclist)
print(minionbinobjlist)
if 'Windows' in platform.system():
execname = "minion.exe"
thread = ""
else:
thread = "-pthread"
execname = "minion"
with open("Makeflags.mak", "w") as out:
out.write('FLAGS=' + ' '.join(commandargs)+ '\n')
with open(outname, "w") as out:
if arg.buildsystem == "sh":
out.write("#!/usr/bin/env bash\n")
if arg.buildsystem == "make":
commandargs = commandargs + ["-MD", "-MP"]
out.write(vardef('FLAGS', ' '.join(commandargs))+'\n')
if arg.buildsystem != "tup":
out.write(vardef('CONSRCS', ' '.join(constraintsrclist)) +'\n')
out.write(vardef('CONOBJS', ' '.join(constraintobjlist)) +'\n')
out.write(vardef('MINLIBOBJS', ' '.join(minionlibobjlist)) +'\n')
out.write(vardef('MINBINOBJS', ' '.join(minionbinobjlist)) +'\n')
if arg.buildsystem == "tup":
out.write(": foreach ")
out.write(" ".join(constraintsrclistshort + ["src/BuildStaticStart.cpp"] + [ "../"+i for i in minionbinsrclist]))
out.write(" |> ")
out.write(compiler + ' ' + varsub('FLAGS') + ' -c %f -o %o |> bin/%B.o\n')
out.write(': bin/*.o |> ' + compiler + ' ' + varsub('FLAGS') + '%f -o %o |> minion\n')
sys.exit(0)
if arg.buildsystem == "make":
if arg.library:
out.write('all: ' + execname + ' libminion.a\n')
else:
out.write('all: ' + execname + '\n')
if arg.buildsystem == "make":
out.write("-include $(CONOBJS:.o=.d)\n")
out.write("-include $(MINOBJS:.o=.d)\n")
for i in constraintsrclist:
if arg.buildsystem == "make":
# out.write(objname(i) + ": " + i + "\n")
out.write(objname(i) + ": Makeflags.mak "+i+'\n')
out.write('\t'+compiler+' '+varsub('FLAGS') + ' -c -o ' +
objname(i) + " " + i +'\n')
for i in minionbinsrclist:
if arg.buildsystem == "make":
# out.write(objname(i)+ " : \n")
out.write(objname(i)+ " : Makeflags.mak " + scriptdir + "/" + i +'\n')
out.write('\t'+compiler+' ' + varsub('FLAGS') + ' -c -o ' +
objname(i) + " " + scriptdir + "/" + i +'\n')
if arg.buildsystem == "make":
out.write(execname + ': $(CONOBJS) $(MINBINOBJS)\n')
out.write('\t' + compiler + ' ' + varsub('FLAGS') + varsub('CONOBJS') +
varsub('MINBINOBJS') + thread + ' -o ' + execname + '\n')
if arg.library:
if arg.buildsystem == "make":
out.write('libminion.a: $(CONOBJS) $(MINLIBOBJS)\n')
out.write('\t' + 'ar rcs libminion.a' +
varsub('MINLIBOBJS') + varsub('CONOBJS') + '\n')