[email protected] | 3f09d18 | 2011-11-23 19:13:44 | [diff] [blame] | 1 | #!/usr/bin/env python |
[email protected] | fad554f4 | 2012-05-07 17:24:49 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 242a9e6 | 2009-11-03 17:32:10 | [diff] [blame] | 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 | |
[email protected] | 1c26c6a | 2014-03-17 14:11:21 | [diff] [blame] | 17 | |
| 18 | def GetVersion(compiler, tool): |
| 19 | tool_output = tool_error = None |
[email protected] | 242a9e6 | 2009-11-03 17:32:10 | [diff] [blame] | 20 | try: |
| 21 | # Note that compiler could be something tricky like "distcc g++". |
[email protected] | 1c26c6a | 2014-03-17 14:11:21 | [diff] [blame] | 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" |
[email protected] | 2ea79a2 | 2014-03-18 19:29:28 | [diff] [blame] | 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 |
[email protected] | 9166090 | 2014-03-21 14:15:52 | [diff] [blame] | 31 | version_re = re.compile(r"^GNU [^ ]+ .* (\d+)\.(\d+)\.*?$", re.M) |
[email protected] | 1c26c6a | 2014-03-17 14:11:21 | [diff] [blame] | 32 | elif tool == "linker": |
| 33 | compiler = compiler + " -Xlinker --version" |
[email protected] | 2ea79a2 | 2014-03-18 19:29:28 | [diff] [blame] | 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 |
[email protected] | 9166090 | 2014-03-21 14:15:52 | [diff] [blame] | 42 | version_re = re.compile(r"^GNU [^ ]+ .* (\d+)\.(\d+)\.*?$", re.M) |
[email protected] | 1c26c6a | 2014-03-17 14:11:21 | [diff] [blame] | 43 | else: |
| 44 | raise Exception("Unknown tool %s" % tool) |
| 45 | |
[email protected] | fad554f4 | 2012-05-07 17:24:49 | [diff] [blame] | 46 | pipe = subprocess.Popen(compiler, shell=True, |
| 47 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
[email protected] | 1c26c6a | 2014-03-17 14:11:21 | [diff] [blame] | 48 | tool_output, tool_error = pipe.communicate() |
[email protected] | fad554f4 | 2012-05-07 17:24:49 | [diff] [blame] | 49 | if pipe.returncode: |
| 50 | raise subprocess.CalledProcessError(pipe.returncode, compiler) |
| 51 | |
[email protected] | 1c26c6a | 2014-03-17 14:11:21 | [diff] [blame] | 52 | result = version_re.match(tool_output) |
[email protected] | 242a9e6 | 2009-11-03 17:32:10 | [diff] [blame] | 53 | return result.group(1) + result.group(2) |
| 54 | except Exception, e: |
[email protected] | 1c26c6a | 2014-03-17 14:11:21 | [diff] [blame] | 55 | if tool_error: |
| 56 | sys.stderr.write(tool_error) |
[email protected] | 242a9e6 | 2009-11-03 17:32:10 | [diff] [blame] | 57 | print >> sys.stderr, "compiler_version.py failed to execute:", compiler |
| 58 | print >> sys.stderr, e |
| 59 | return "" |
| 60 | |
[email protected] | 1c26c6a | 2014-03-17 14:11:21 | [diff] [blame] | 61 | |
| 62 | def main(args): |
| 63 | tool = "compiler" |
| 64 | if len(args) == 1: |
| 65 | tool = args[0] |
| 66 | elif len(args) > 1: |
| 67 | print "Unknown arguments!" |
| 68 | |
[email protected] | d0245a1 | 2012-10-18 22:08:06 | [diff] [blame] | 69 | # Check if CXX environment variable exists and |
| 70 | # if it does use that compiler. |
| 71 | cxx = os.getenv("CXX", None) |
| 72 | if cxx: |
[email protected] | 1c26c6a | 2014-03-17 14:11:21 | [diff] [blame] | 73 | cxxversion = GetVersion(cxx, tool) |
[email protected] | d0245a1 | 2012-10-18 22:08:06 | [diff] [blame] | 74 | if cxxversion != "": |
| 75 | print cxxversion |
| 76 | return 0 |
| 77 | else: |
| 78 | # Otherwise we check the g++ version. |
[email protected] | 1c26c6a | 2014-03-17 14:11:21 | [diff] [blame] | 79 | gccversion = GetVersion("g++", tool) |
[email protected] | d0245a1 | 2012-10-18 22:08:06 | [diff] [blame] | 80 | if gccversion != "": |
| 81 | print gccversion |
| 82 | return 0 |
[email protected] | 242a9e6 | 2009-11-03 17:32:10 | [diff] [blame] | 83 | |
| 84 | return 1 |
| 85 | |
[email protected] | 1c26c6a | 2014-03-17 14:11:21 | [diff] [blame] | 86 | |
[email protected] | 242a9e6 | 2009-11-03 17:32:10 | [diff] [blame] | 87 | if __name__ == "__main__": |
[email protected] | 1c26c6a | 2014-03-17 14:11:21 | [diff] [blame] | 88 | sys.exit(main(sys.argv[1:])) |