blob: 22785e5ae2ee9dd8498698e31943b9e5ad1b1c31 [file] [log] [blame]
[email protected]3f09d182011-11-23 19:13:441#!/usr/bin/env python
[email protected]fad554f42012-05-07 17:24:492# Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]242a9e62009-11-03 17:32:103# 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
8Print gcc version as XY if you are running gcc X.Y.*.
9This is used to tweak build flags for gcc 4.4.
10"""
11
12import os
13import re
14import subprocess
15import sys
16
[email protected]1c26c6a2014-03-17 14:11:2117
18def GetVersion(compiler, tool):
19 tool_output = tool_error = None
[email protected]242a9e62009-11-03 17:32:1020 try:
21 # Note that compiler could be something tricky like "distcc g++".
[email protected]1c26c6a2014-03-17 14:11:2122 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]2ea79a22014-03-18 19:29:2828 # 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]91660902014-03-21 14:15:5231 version_re = re.compile(r"^GNU [^ ]+ .* (\d+)\.(\d+)\.*?$", re.M)
[email protected]1c26c6a2014-03-17 14:11:2132 elif tool == "linker":
33 compiler = compiler + " -Xlinker --version"
[email protected]2ea79a22014-03-18 19:29:2834 # 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]91660902014-03-21 14:15:5242 version_re = re.compile(r"^GNU [^ ]+ .* (\d+)\.(\d+)\.*?$", re.M)
[email protected]1c26c6a2014-03-17 14:11:2143 else:
44 raise Exception("Unknown tool %s" % tool)
45
[email protected]fad554f42012-05-07 17:24:4946 pipe = subprocess.Popen(compiler, shell=True,
47 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
[email protected]1c26c6a2014-03-17 14:11:2148 tool_output, tool_error = pipe.communicate()
[email protected]fad554f42012-05-07 17:24:4949 if pipe.returncode:
50 raise subprocess.CalledProcessError(pipe.returncode, compiler)
51
[email protected]1c26c6a2014-03-17 14:11:2152 result = version_re.match(tool_output)
[email protected]242a9e62009-11-03 17:32:1053 return result.group(1) + result.group(2)
54 except Exception, e:
[email protected]1c26c6a2014-03-17 14:11:2155 if tool_error:
56 sys.stderr.write(tool_error)
[email protected]242a9e62009-11-03 17:32:1057 print >> sys.stderr, "compiler_version.py failed to execute:", compiler
58 print >> sys.stderr, e
59 return ""
60
[email protected]1c26c6a2014-03-17 14:11:2161
62def 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]d0245a12012-10-18 22:08:0669 # 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]1c26c6a2014-03-17 14:11:2173 cxxversion = GetVersion(cxx, tool)
[email protected]d0245a12012-10-18 22:08:0674 if cxxversion != "":
75 print cxxversion
76 return 0
77 else:
78 # Otherwise we check the g++ version.
[email protected]1c26c6a2014-03-17 14:11:2179 gccversion = GetVersion("g++", tool)
[email protected]d0245a12012-10-18 22:08:0680 if gccversion != "":
81 print gccversion
82 return 0
[email protected]242a9e62009-11-03 17:32:1083
84 return 1
85
[email protected]1c26c6a2014-03-17 14:11:2186
[email protected]242a9e62009-11-03 17:32:1087if __name__ == "__main__":
[email protected]1c26c6a2014-03-17 14:11:2188 sys.exit(main(sys.argv[1:]))