Fixing an off by one error in screenshot manager code.
[chromium-blink-merge.git] / build / compiler_version.py
blobfd23d89bd63b21c2304b80e22c12df7450120b42
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.
6 """Compiler version checking tool for gcc
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 """
12 import os
13 import re
14 import subprocess
15 import sys
18 def GetVersion(compiler, tool):
19 tool_output = tool_error = None
20 try:
21 # Note that compiler could be something tricky like "distcc g++".
22 if tool == "compiler":
23 compiler = compiler + " -dumpversion"
24 # 4.6
25 version_re = re.compile(r"(\d+)\.(\d+)")
26 elif tool == "assembler":
27 compiler = compiler + " -Xassembler --version -x assembler -c /dev/null"
28 # Unmodified: GNU assembler (GNU Binutils) 2.24
29 # Ubuntu: GNU assembler (GNU Binutils for Ubuntu) 2.22
30 # Fedora: GNU assembler version 2.23.2
31 version_re = re.compile(r"^GNU [^ ]+ .* (\d+).(\d+).*?$", re.M)
32 elif tool == "linker":
33 compiler = compiler + " -Xlinker --version"
34 # Using BFD linker
35 # Unmodified: GNU ld (GNU Binutils) 2.24
36 # Ubuntu: GNU ld (GNU Binutils for Ubuntu) 2.22
37 # Fedora: GNU ld version 2.23.2
38 # Using Gold linker
39 # Unmodified: GNU gold (GNU Binutils 2.24) 1.11
40 # Ubuntu: GNU gold (GNU Binutils for Ubuntu 2.22) 1.11
41 # Fedora: GNU gold (version 2.23.2) 1.11
42 version_re = re.compile(r"^GNU [^ ]+ .* (\d+).(\d+).*?$", re.M)
43 else:
44 raise Exception("Unknown tool %s" % tool)
46 pipe = subprocess.Popen(compiler, shell=True,
47 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
48 tool_output, tool_error = pipe.communicate()
49 if pipe.returncode:
50 raise subprocess.CalledProcessError(pipe.returncode, compiler)
52 result = version_re.match(tool_output)
53 return result.group(1) + result.group(2)
54 except Exception, e:
55 if tool_error:
56 sys.stderr.write(tool_error)
57 print >> sys.stderr, "compiler_version.py failed to execute:", compiler
58 print >> sys.stderr, e
59 return ""
62 def main(args):
63 # Force the locale to C otherwise the version string could be localized
64 # making regex matching fail.
65 os.environ["LC_ALL"] = "C"
67 tool = "compiler"
68 if len(args) == 1:
69 tool = args[0]
70 elif len(args) > 1:
71 print "Unknown arguments!"
73 # Check if CXX environment variable exists and
74 # if it does use that compiler.
75 cxx = os.getenv("CXX", None)
76 if cxx:
77 cxxversion = GetVersion(cxx, tool)
78 if cxxversion != "":
79 print cxxversion
80 return 0
81 else:
82 # Otherwise we check the g++ version.
83 gccversion = GetVersion("g++", tool)
84 if gccversion != "":
85 print gccversion
86 return 0
88 return 1
91 if __name__ == "__main__":
92 sys.exit(main(sys.argv[1:]))