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.
18 def GetVersion(compiler
, tool
):
19 tool_output
= tool_error
= None
21 # Note that compiler could be something tricky like "distcc g++".
22 if tool
== "compiler":
23 compiler
= compiler
+ " -dumpversion"
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"
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
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
)
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()
50 raise subprocess
.CalledProcessError(pipe
.returncode
, compiler
)
52 result
= version_re
.match(tool_output
)
53 return result
.group(1) + result
.group(2)
56 sys
.stderr
.write(tool_error
)
57 print >> sys
.stderr
, "compiler_version.py failed to execute:", compiler
58 print >> sys
.stderr
, e
63 # Force the locale to C otherwise the version string could be localized
64 # making regex matching fail.
65 os
.environ
["LC_ALL"] = "C"
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)
77 cxxversion
= GetVersion(cxx
, tool
)
82 # Otherwise we check the g++ version.
83 gccversion
= GetVersion("g++", tool
)
91 if __name__
== "__main__":
92 sys
.exit(main(sys
.argv
[1:]))