-
Notifications
You must be signed in to change notification settings - Fork 49
/
configure
executable file
·567 lines (446 loc) · 18.9 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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#!/usr/bin/env python3
"""
Configure the build of klee-uclibc. It can be built
as a native library or as an LLVM bitcode archive.
Information about configuration is written to console and
more verbose information is writen to a log file (usually
config.log)
You can use the CC environment variable (or flag --with-cc)
to force a particular C compiler. e.g.
$ CC=/usr/bin/clang ./configure
If the forced compiler is a tool name (e.g. CC=clang) then
the PATH is searched. Otherwise it is assumed to be a
relative or absolute path.
If building an LLVM bitcode archive and CC is not set
then this script will try and find a LLVM bitcode compiler
in your path in the following order
1. clang built inside `llvm-config --bindir`
2. clang in PATH
"""
import argparse
import fileinput
import logging
import os
import platform
import pprint
import shutil
import subprocess
import sys
uclibcRoot=os.path.dirname( os.path.abspath(__file__) )
templateTarget= os.path.join( uclibcRoot, 'Makefile.klee')
templateFile= os.path.join( uclibcRoot, 'Makefile.klee.in')
configLogFile= os.path.join( uclibcRoot, 'config.log')
def main(args):
# Log everything to file
logging.basicConfig(level=logging.DEBUG,
format='%(levelname)s:: %(funcName)s() at line %(lineno)d ::%(message)s',
filename=configLogFile,
filemode='w'
)
# Log the arguments used so that it is easier to reinvoke later.
logging.debug('Executing with command line:\n{} {}\n'.format( __file__, ' '.join(args)))
# Log to console too but by default be less verbose
consoleHandler = logging.StreamHandler()
consoleHandler.setLevel(logging.INFO)
consoleFormatter = logging.Formatter('%(levelname)s:%(message)s')
consoleHandler.setFormatter(consoleFormatter)
logging.getLogger().addHandler(consoleHandler) # Add extra handler to root logger
parser = argparse.ArgumentParser(description=__doc__)
# Two different modes
meo = parser.add_mutually_exclusive_group(required=True)
meo.add_argument('-n','--make-native', help='Produce native binary library.', action='store_true')
meo.add_argument('-l','--make-llvm-lib', help='Produce library compiled as LLVM bitcode.', action='store_true')
parser.add_argument('--with-llvm-config', help='Path to llvm-config executable to use. If not set then llvm-config in the PATH environment will be used.', type=str)
parser.add_argument('--with-cc', help='Force C compiler to be used. If this and CC environment variable is not set then auto-detection will be detected.', type=str)
parser.add_argument('--enable-assertions', default=False, action='store_true', help='Enable assertions. (Default %(default)s)')
parser.add_argument('--enable-release', default=False, action='store_true', help='Enable Release mode. (Default %(default)s)')
parser.add_argument('--disable-prebuilt-config', default=False, action='store_true',
help='Do not use pre-built config (+ any needed patches) to emulate legacy klee-uclibc. If you want to generate your own config use this option then run `make menuconfig`. (Default %(default)s)')
parser.add_argument('--log-level',type=str, default='info',choices=['debug','info','warning','error','critical'],
help='Set console logging level output (Default %(default)s). Logging to "{}" is unaffected.'.format(configLogFile))
pargs = parser.parse_args(args)
# Configure the level for console output but leave file output untouched
consoleHandler.setLevel(level=getattr(logging, pargs.log_level.upper(), None))
# Force compiler if requested
cc = pargs.with_cc
if not cc:
cc = os.getenv('CC')
if cc:
logging.info('Forcing C compiler to be...{}'.format(cc))
if os.sep in cc:
# Absolute or relative path
cc = os.path.abspath(cc)
else:
# Search for tool in PATH
ccAbs = shutil.which(cc)
if ccAbs is None:
logging.error('"{0}" is not in your path.'.format(cc))
sys.exit(1)
else:
cc = ccAbs
if not os.path.exists(cc):
logging.error('"{}" does not exist.'.format(cc))
sys.exit(1)
logging.info('Absolute path to compiler...{}'.format(cc))
if pargs.make_native:
handleNativeConfig(pargs, cc)
else:
handleLLVMConfig(pargs, cc)
uclibcConfigFile = os.path.join(uclibcRoot, '.config')
if pargs.disable_prebuilt_config:
logging.info('Not using pre-built config. You should run `make menuconfig` or `make config`')
if os.path.exists(uclibcConfigFile):
logging.warning('A pre-existing .config file was detected. You should probably remove it.')
else:
if os.path.exists(uclibcConfigFile):
logging.warning('Removing existing config file...{}'.format(uclibcConfigFile))
os.remove(uclibcConfigFile)
installPrebuiltConfig()
def installPrebuiltConfig():
""" This function installs pre-made .config files
and any necessary patches for a particular architecture.
FIXME: Remove this for upstream klee-uclibc. People should
just run `make menuconfig` themselves. It's not hard!
"""
p = platform.machine()
logging.info('Setting up pre-made configure for...{}'.format(p))
patchDir = os.path.join(uclibcRoot, 'klee-premade-configs', p)
if not os.path.exists(patchDir):
logging.error('"{}" does not exist. Cannot install pre-made .config file'.format(patchDir))
sys.exit(1)
configFile = os.path.join(patchDir, 'config')
if not os.path.exists(configFile):
logging.error('"{}" does not exist. Cannot install pre-made .config file'.format(patchDir))
sys.exit(1)
if p == 'x86_64' and platform.architecture()[0] == '64bit':
logging.info('Installing .config file')
shutil.copy(configFile, os.path.join(uclibcRoot, '.config'))
elif p == 'i686' and platform.architecture()[0] == '32bit':
logging.info('Installing .config file')
shutil.copy(configFile, os.path.join(uclibcRoot, '.config'))
else:
logging.error('Your architecture is not supported. You will need to run `make menuconfig` manually')
logging.info('Looking for kernel include path...')
path = findKernelIncludePath()
logging.info('Found "{}"'.format(path))
# Patch the .config file as necessary
for line in fileinput.input(os.path.join(uclibcRoot, '.config'),inplace=True):
if "KERNEL_HEADERS" in line:
line = 'KERNEL_HEADERS="' + path + '"\n'
logging.debug('Patching KERNEL_HEADERS with path "{}"'.format(path))
sys.stdout.write(line)
def findKernelIncludePath():
""" This function searches for Kernel include files
which are needed to build uclibc
"""
std_include = os.environ.get("UCLIBC_KERNEL_HEADERS", "/usr/include")
test_file = "asm/unistd.h"
p = platform.machine()
if p == 'x86_64':
if os.path.exists( os.path.join(std_include, "x86_64-linux-gnu", test_file) ):
return os.path.join(std_include, "x86_64-linux-gnu")
if p == 'i686':
if os.path.exists( os.path.join(std_include, "i386-linux-gnu", test_file) ):
return os.path.join(std_include, "i386-linux-gnu")
test_path = os.path.join(std_include, test_file)
if not os.path.exists( test_path ):
msg = ("Kernel header files not found at '%s': '%s' is absent."
"Export the UCLIBC_KERNEL_HEADERS environment variable to change the"
"default path ('/usr/include')")
logging.error(msg % (std_include, test_path))
return std_include
def runTool(cmd):
"""
return (returnCode, output) from tool
"""
retCode=0
output=""
try:
logging.debug('Executing {0}'.format(pprint.pformat(cmd)))
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
retCode = e.returncode
output = e.output
# Clean up output
output = output.decode()
logging.debug('Execution completed with return code:{}'.format(retCode))
logging.debug('Execution completed with output:"{}"'.format(output))
return (retCode, output)
def handleCommonOptions(pargs, subs):
if pargs.enable_assertions:
subs['ASSERTS'] = 'y'
logging.info('Enabling assertions')
else:
logging.info('Disabling assertions')
subs['ASSERTS'] = 'n'
if pargs.enable_release:
logging.info('Configuring for Release build')
subs['DEBUG'] = 'n'
else:
logging.info('Configuring for Debug build')
subs['DEBUG'] = 'y'
def handleNativeConfig(pargs, cc=None):
logging.info('Configuring for native archive')
subs = { 'EMIT_LLVM':'', # Building natively so we don't want LLVM Bitcode
'TOOLDIR':'' # Native compiler tools should be in PATH
}
handleCommonOptions(pargs, subs)
def searchPath(cc, lookFor):
if not cc:
logging.info('Looking for...{}'.format(lookFor))
ccNew = shutil.which(lookFor)
if ccNew:
logging.info('Found...{}'.format(ccNew))
else:
logging.info('Could not find {}'.format(lookFor))
return ccNew
return cc
# Detect compiler if not forced
if not cc:
cc=searchPath(cc, 'clang')
cc=searchPath(cc, 'cc')
# Test compiler
#FIXME
if cc is None:
logging.error('Could not find a C compiler')
sys.exit(1)
logging.info('Using CC...{}'.format(cc))
subs['CC'] = cc
subs['HOSTCC'] = cc
if not checkForNCurses(subs['HOSTCC']):
sys.exit(1)
# Check tools are present
tools = { 'LINKER':'ld',
'ARCHIVER':'ar',
'NM':'nm',
'OBJDUMP':'objdump'
}
for (name, executable) in tools.items():
if not shutil.which(executable):
logging.error('Could not find {} in PATH'.format(executable))
sys.exit(1)
subs[name] = executable
# Do substitution
logging.debug('Using template substitutions \n{} \nfor file "{}"'.format(
pprint.pformat(subs),
templateTarget)
)
doTemplate(subs, templateFile, templateTarget)
def handleLLVMConfig(pargs, cc=None):
# Substitutions
subs = { 'EMIT_LLVM':'-emit-llvm'}
handleCommonOptions(pargs, subs)
logging.info('Configuring for LLVM bitcode archive')
llvmConfigTool = pargs.with_llvm_config
if not llvmConfigTool:
llvmConfigTool = shutil.which('llvm-config')
else:
if os.sep not in llvmConfigTool:
llvmConfigTool = shutil.which(llvmConfigTool)
else:
llvmConfigTool = os.path.abspath(llvmConfigTool)
if not os.path.exists(llvmConfigTool):
logging.error('"{}" does not exist. Cannot use as llvm-config tool'.format(llvmConfigTool))
sys.exit(1)
logging.info('Using llvm-config at...{}'.format(llvmConfigTool))
if llvmConfigTool is None:
logging.error('llvm-config cannot be found')
sys.exit(1)
# Detect tool directory
(retCode, output) = runTool([llvmConfigTool, '--bindir'])
if retCode != 0:
logging.error('Failed to execute llvm-config:\n{}'.format(output))
sys.exit(1)
llvmToolDir = output.replace('\n','').lstrip().rstrip()
if not os.path.exists(llvmToolDir):
logging.error('The tool directory ({})reported by llvm-config does not exist.'.format(llvmToolDir))
sys.exit(1)
logging.info('Using llvm tool dir...{}'.format(llvmToolDir))
subs['TOOLDIR'] = llvmToolDir + os.sep # Trailing slash is needed in Makefile
# Check for the needed llvm tools
llvmDeps = {
'NM' : ['llvm-nm'],
'ARCHIVER' : ['llvm-ar'],
'LINKER': ['llvm-link', 'llvm-ld' ], # Two possible tools to use as linker
'OBJDUMP': ['llvm-objdump']
}
for (name,toolNames) in llvmDeps.items():
found=False
for t in toolNames:
absToolPath = os.path.join( llvmToolDir, t)
if os.path.exists(absToolPath):
logging.info('Found "{}".'.format(absToolPath))
found=True
subs[name] = t # Add substitution
break
if not found:
logging.error('Could not find needed tool. Tried {}'.format(llvmDeps))
sys.exit(1)
if not cc:
cc = findBitCodeCompiler(llvmToolDir)
if not cc:
logging.error('Failed to find a working LLVM bitcode compiler')
sys.exit(1)
logging.info('Using LLVM Bitcode Compiler...{}'.format(cc))
else:
logging.info('Using LLVM Bitcode Compiler specified by CC ...{}'.format(cc))
if not testBitCodeCompiler(cc, llvmToolDir):
logging.error('LLVM Bitcode compiler does not work')
sys.exit(1)
# Add compiler to substitutions
subs['CC'] = cc
subs['HOSTCC'] = cc
if not checkForNCurses(subs['HOSTCC']):
sys.exit(1)
logging.debug('Using template substitutions \n{} \nfor file "{}"'.format(
pprint.pformat(subs),
templateTarget)
)
doTemplate(subs, templateFile, templateTarget)
def doTemplate(subs, src, dest):
"""
Do a Template substitution using @KEY@ syntax.
subs : Dictionary of substitutions
src : File to apply template to
dest : The destination for templated file.
"""
# Remove old destination
if os.path.exists(dest):
logging.info('Removing template destination "{}"'.format(dest))
os.remove(dest)
if not os.path.exists(src):
logging.error('Template source "{}" does not exist'.format(src))
sys.exit(1)
# Read file into string and do replacements
with open(src,'r') as f:
srcString = f.read()
# Do replacements
for (oldString, replacement) in subs.items():
srcString = srcString.replace('@' + oldString + '@', replacement)
# Write templated string to file
logging.info('Writing templated file to "{}"'.format(dest))
with open(dest,'w') as f:
f.write(srcString)
def findBitCodeCompiler(llvmToolDir):
"""
Search for LLVM Bitcode compiler.
Returns absolute path to compiler. The compiler will
be tested against the tools in llvmToolDir
"""
logging.info('Searching for LLVM Bitcode compiler...')
# First try clang in LLVM Build directory
ccPath = os.path.join(llvmToolDir, 'clang')
if os.path.exists( ccPath):
logging.info('Found clang in LLVM Build dir...{}'.format(ccPath))
if not testBitCodeCompiler(ccPath, llvmToolDir):
ccPath=None
else:
ccPath=None
# If that failed try clang in PATH
if not ccPath:
clang = shutil.which('clang')
if clang:
logging.info('Found clang in PATH...{}'.format(clang))
if testBitCodeCompiler(clang, llvmToolDir):
ccPath = clang
return ccPath
def testBitCodeCompiler(cc, llvmToolDir):
"""
Returns true if cc is working LLVM bitcode
compiler.
This works by compiling a small program to LLVM bitcode using cc
then this is converted back to LLVM assembly using llvm-dis.
If there is an incompatibility between llvm-dis and the bitcode
compiler it will hopefully be detected here.
cc : Absolute path to bitcode compiler
"""
import tempfile
logging.info('Testing LLVM Bitcode compiler...{}'.format(cc))
# Get temporary file name for output
bitCodeFileName=None
cProgramFileName=None
llvmAsFileName=None
def cleanUp():
if bitCodeFileName and os.path.exists(bitCodeFileName): os.remove(bitCodeFileName)
if cProgramFileName and os.path.exists(cProgramFileName): os.remove(cProgramFileName)
if llvmAsFileName and os.path.exists(llvmAsFileName): os.remove(llvmAsFileName)
try:
with tempfile.NamedTemporaryFile(suffix='.bc', delete=False) as f:
bitCodeFileName = f.name
if not bitCodeFileName:
logging.error('Failed to generated temporary file name')
sys.exit(1)
with tempfile.NamedTemporaryFile(mode='w', suffix='.c', delete=False) as f:
# Write a simple C program to tempfile
f.write('int main() { return 0; }')
cProgramFileName = f.name
if not cProgramFileName:
msg='Failed to generated temporary file name'
logging.error(msg)
raise Exception(msg)
(retCode, ccOutput) = runTool([cc,
'-c',
'-g',
'-emit-llvm',
cProgramFileName,
'-o', bitCodeFileName]
)
if retCode != 0:
logging.info('Compiler failed with output:\n{}'.format(ccOutput))
cleanUp()
return False
logging.debug('{} succeeded'.format(cc))
llvmDis = os.path.join(llvmToolDir, 'llvm-dis')
if not os.path.exists(llvmDis):
logging.error('Cannot find "{}"'.format(llvmDis))
cleanUp()
return False
# Get temporary file name for LLVM assembly
with tempfile.NamedTemporaryFile(suffix='.ll', delete=False) as f:
llvmAsFileName = f.name
if not llvmAsFileName:
msg='Failed to generated temporary file name'
logging.error(msg)
raise Exception(msg)
(retCode, llvmDisOutput) = runTool([ llvmDis, '-o=' + llvmAsFileName, bitCodeFileName])
if retCode != 0:
logging.info('Conversion of LLVM Bitcode to LLVM Assembly failed with output:\n{}'.format(llvmDisOutput))
cleanUp()
return False
except Exception:
cleanUp()
raise
cleanUp()
logging.info('Compiler {} works'.format(cc))
return True
def checkForNCurses(cc):
"""
uClibc needs ncurses for make menuconfig
This function returns true if it is available.
Other wise it returns false.
cc is the compiler to use
"""
import tempfile
# Simple ncurses program
src="""
#include <ncurses.h>
int main()
{
initscr();
return 0;
}
"""
logging.info('Checking for ncurses...')
outputName='a.out'
with tempfile.NamedTemporaryFile(mode='w+', suffix='.c') as f:
f.write(src)
f.flush()
(returnCode, output) = runTool([cc, f.name, '-lncurses', '-o', outputName])
if returnCode == 0:
os.remove(outputName)
return True
logging.error('Failed to find ncurses. Compiler said:\n{}'.format(output))
logging.error('You should install the ncurses library and development headers.')
return False
if __name__ == '__main__':
main(sys.argv[1:])