| |
1 #!/usr/bin/env python |
| |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| |
3 # Use of this source code is governed by a BSD-style license that can be |
| |
4 # found in the LICENSE file. |
| |
5 |
| |
6 """Compiler version checking tool for gcc |
| |
7 |
| |
8 Print gcc version as XY if you are running gcc X.Y.*. |
| |
9 This is used to tweak build flags for gcc 4.4. |
| |
10 """ |
| |
11 |
| |
12 import os |
| |
13 import re |
| |
14 import subprocess |
| |
15 import sys |
| |
16 |
| |
17 def GetVersion(compiler): |
| |
18 try: |
| |
19 # Note that compiler could be something tricky like "distcc g++". |
| |
20 compiler = compiler + " -dumpversion" |
| |
21 pipe = subprocess.Popen(compiler, shell=True, |
| |
22 stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| |
23 gcc_output, gcc_error = pipe.communicate() |
| |
24 if pipe.returncode: |
| |
25 raise subprocess.CalledProcessError(pipe.returncode, compiler) |
| |
26 |
| |
27 result = re.match(r"(\d+)\.(\d+)", gcc_output) |
| |
28 return result.group(1) + result.group(2) |
| |
29 except Exception, e: |
| |
30 if gcc_error: |
| |
31 sys.stderr.write(gcc_error) |
| |
32 print >> sys.stderr, "compiler_version.py failed to execute:", compiler |
| |
33 print >> sys.stderr, e |
| |
34 return "" |
| |
35 |
| |
36 def GetVersionFromEnvironment(compiler_env): |
| |
37 """ Returns the version of compiler |
| |
38 |
| |
39 If the compiler was set by the given environment variable and exists, |
| |
40 return its version, otherwise None is returned. |
| |
41 """ |
| |
42 cxx = os.getenv(compiler_env, None) |
| |
43 if cxx: |
| |
44 cxx_version = GetVersion(cxx) |
| |
45 if cxx_version != "": |
| |
46 return cxx_version |
| |
47 return None |
| |
48 |
| |
49 def main(): |
| |
50 # Check if CXX_target or CXX environment variable exists an if it does use |
| |
51 # that compiler. |
| |
52 # TODO: Fix ninja (see http://crbug.com/140900) instead and remove this code |
| |
53 # In ninja's cross compile mode, the CXX_target is target compiler, while |
| |
54 # the CXX is host. The CXX_target needs be checked first, though the target |
| |
55 # and host compiler have different version, there seems no issue to use the |
| |
56 # target compiler's version number as gcc_version in Android. |
| |
57 cxx_version = GetVersionFromEnvironment("CXX_target") |
| |
58 if cxx_version: |
| |
59 print cxx_version |
| |
60 return 0 |
| |
61 |
| |
62 cxx_version = GetVersionFromEnvironment("CXX") |
| |
63 if cxx_version: |
| |
64 print cxx_version |
| |
65 return 0 |
| |
66 |
| |
67 # Otherwise we check the g++ version. |
| |
68 gccversion = GetVersion("g++") |
| |
69 if gccversion != "": |
| |
70 print gccversion |
| |
71 return 0 |
| |
72 |
| |
73 return 1 |
| |
74 |
| |
75 if __name__ == "__main__": |
| |
76 sys.exit(main()) |