|
| 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 | + |
0 commit comments