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