Skip to content

Commit 6833b94

Browse files
committed
A simple sanity checker to spot obvious configuration errors.
1 parent 9488e55 commit 6833b94

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed

sanity/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

sanity/checker.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import os
2+
import subprocess as sp
3+
4+
explain_LLVM_COMPILER = """
5+
The environment variable 'LLVM_COMPILER' is a switch. It should either
6+
be set to 'clang' or 'dragonegg'. Anything else will cause an error.
7+
"""
8+
9+
explain_LLVM_DRAGONEGG_PLUGIN = """
10+
You need to set the environment variable LLVM_DRAGONEGG_PLUGIN to the full path
11+
to your dragonegg plugin. Thanks.
12+
"""
13+
14+
class Checker(object):
15+
def __init__(self):
16+
path = os.getenv('LLVM_COMPILER_PATH')
17+
18+
if path and path[-1] != os.path.sep:
19+
path = path + os.path.sep
20+
21+
self.path = path if path else ''
22+
23+
24+
def checkSwitch(self):
25+
compiler_type = os.getenv('LLVM_COMPILER')
26+
if compiler_type == 'clang':
27+
return (1, 'Good, we are using clang.\n')
28+
elif compiler_type == 'dragonegg':
29+
return (2, 'OK, we are using dragonegg.\n')
30+
else:
31+
return (0, explain_LLVM_COMPILER)
32+
33+
34+
def checkClang(self):
35+
36+
cc_name = os.getenv('LLVM_CC_NAME')
37+
cxx_name = os.getenv('LLVM_CXX_NAME')
38+
39+
cc = '{0}{1}'.format(self.path, cc_name if cc_name else 'clang')
40+
cxx = '{0}{1}'.format(self.path, cxx_name if cxx_name else 'clang++')
41+
42+
return self.checkCompilers(cc, cxx)
43+
44+
45+
def checkDragonegg(self):
46+
47+
if not self.checkDragoneggPlugin():
48+
return False
49+
50+
pfx = ''
51+
if os.getenv('LLVM_GCC_PREFIX') is not None:
52+
pfx = os.getenv('LLVM_GCC_PREFIX')
53+
54+
cc = '{0}{1}gcc'.format(self.path, pfx)
55+
cxx = '{0}{1}g++'.format(self.path, pfx)
56+
57+
(ccOk, ccVersion) = self.checkExecutable(cc)
58+
(cxxOk, cxxVersion) = self.checkExecutable(cxx)
59+
60+
return self.checkCompilers(cc, cxx)
61+
62+
63+
def checkDragoneggPlugin(self):
64+
plugin = os.getenv('LLVM_DRAGONEGG_PLUGIN')
65+
66+
if not plugin:
67+
print explain_LLVM_DRAGONEGG_PLUGIN
68+
return False
69+
70+
if os.path.isfile(plugin):
71+
try:
72+
open(plugin)
73+
pass
74+
except IOError as e:
75+
print "Unable to open {0}".format(plugin)
76+
else:
77+
return True
78+
else:
79+
print "Could not find {0}".format(plugin)
80+
return False
81+
82+
83+
def checkCompiler(self):
84+
(code, comment) = self.checkSwitch()
85+
86+
if code == 0:
87+
print comment
88+
return False
89+
elif code == 1:
90+
print comment
91+
return self.checkClang()
92+
elif code == 2:
93+
print comment
94+
return self.checkDragonegg()
95+
else:
96+
print 'Insane\n'
97+
return False
98+
99+
100+
def checkCompilers(self, cc, cxx):
101+
102+
(ccOk, ccVersion) = self.checkExecutable(cc)
103+
(cxxOk, cxxVersion) = self.checkExecutable(cxx)
104+
105+
if not ccOk:
106+
print 'The C compiler {0} was not found or not executable.\nBetter not try using wllvm!\n'.format(cc)
107+
else:
108+
print 'The C compiler {0} is:\n{1}\n'.format(cc, ccVersion)
109+
110+
if not cxxOk:
111+
print 'The CXX compiler {0} was not found or not executable.\nBetter not try using wllvm++!\n'.format(cxx)
112+
else:
113+
print 'The C++ compiler {0} is:\n{1}\n'.format(cxx, cxxVersion)
114+
115+
return ccOk or cxxOk
116+
117+
118+
def checkExecutable(self, exe, version_switch='-v'):
119+
cmd = [exe, version_switch]
120+
try:
121+
compiler = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE)
122+
output = compiler.communicate()
123+
compilerOutput = '{0}{1}'.format(output[0], output[1])
124+
except OSError as e:
125+
return (False, '{0} not found or not executable'.format(exe))
126+
else:
127+
return (True, compilerOutput)
128+
129+
130+
131+
def checkAuxiliaries(self):
132+
link = '{0}llvm-link'.format(self.path) if self.path else 'llvm-link' #LLVM_LINKER_NAME
133+
ar = '{0}llvm-ar'.format(self.path) if self.path else 'llvm-ar' #LLVM_ARCHIVER_NAME
134+
135+
(linkOk, linkVersion) = self.checkExecutable(link, '-version')
136+
137+
(arOk, arVersion) = self.checkExecutable(ar, '-version')
138+
139+
if not linkOk:
140+
print 'The bitcode linker {0} was not found or not executable.\nBetter not try using extract-bc!\n'.format(link)
141+
else:
142+
print 'The bitcode linker {0} is:\n{1}\n'.format(link, linkVersion)
143+
144+
if not arOk:
145+
print 'The bitcode archiver {0} was not found or not executable.\nBetter not try using extract-bc!\n'.format(ar)
146+
else:
147+
print 'The bitcode archiver {0} is:\n{1}\n'.format(ar, arVersion)
148+
149+

wllvm-sanity-checker

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python
2+
#
3+
# This does some simple sanity checks on the configuration, and tries to print informative
4+
# results of that check. Hopefully never dumping a python stack trace.
5+
#
6+
7+
import sys
8+
import os
9+
from sanity.checker import *
10+
11+
def main(args):
12+
13+
14+
if not (sys.platform.startswith('freebsd') or
15+
sys.platform.startswith('linux') or
16+
sys.platform.startswith('darwin')):
17+
print 'I do not think we support your OS. Sorry.'
18+
return 1
19+
20+
checker = Checker()
21+
22+
23+
success = checker.checkCompiler()
24+
25+
if success:
26+
checker.checkAuxiliaries()
27+
28+
return 0 if success else 1
29+
30+
31+
if __name__ == '__main__':
32+
sys.exit(main(sys.argv))
33+
34+
35+

0 commit comments

Comments
 (0)