1import os 2import subprocess 3import re 4import sys 5 6def which(cmd): 7 for p in os.environ["PATH"].split(os.pathsep): 8 clang_path = os.path.join(p, cmd) 9 if os.path.exists(clang_path): 10 return clang_path 11 return None 12 13CLANG_VERSION_REGEX=".*version\s*([0-9]*\.[0-9]*)\.*" 14clang_path = which("clang++") 15clang_version_major = 0 16clang_version_minor = 0 17 18if clang_path: 19 clang_version_out = subprocess.Popen([clang_path, "--version"], 20 stdout=subprocess.PIPE).communicate()[0] 21 clang_version_match = re.search(CLANG_VERSION_REGEX, clang_version_out) 22 clang_version_str = clang_version_match.group(1) 23 clang_version_array = clang_version_str.split('.') 24 clang_version_major = int(clang_version_array[0]) 25 clang_version_minor = int(clang_version_array[1]) 26 27if clang_version_major >= 3 and clang_version_minor >= 5: 28 print "" 29else: 30 # Loop in support clang version only 31 clang_version_major = 3 32 clang_version_minor = 9 33 while clang_version_major >= 3 and clang_version_minor >= 5: 34 clang_version_str = "%d.%d" % (clang_version_major, clang_version_minor) 35 clang_path = which("clang++-" + clang_version_str) 36 if clang_path: 37 print clang_version_str 38 sys.exit(0) 39 clang_version_minor -= 1 40 if clang_version_minor < 0: 41 clang_version_minor = 9 42 clang_version_major -= 1 43 print "None" 44