Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Compiler version checking tool for gcc | 6 """Compiler version checking tool for gcc |
| 7 | 7 |
| 8 Print gcc version as XY if you are running gcc X.Y.*. | 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. | 9 This is used to tweak build flags for gcc 4.4. |
| 10 """ | 10 """ |
| 11 | 11 |
| 12 import os | 12 import os |
| 13 import re | 13 import re |
| 14 import subprocess | 14 import subprocess |
| 15 import sys | 15 import sys |
| 16 | 16 |
| 17 def GetVersion(compiler): | 17 |
| 18 def GetVersion(compiler, tool): | |
| 19 tool_output = tool_error = None | |
| 18 try: | 20 try: |
| 19 # Note that compiler could be something tricky like "distcc g++". | 21 # Note that compiler could be something tricky like "distcc g++". |
| 20 compiler = compiler + " -dumpversion" | 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 # GNU assembler (GNU Binutils for Ubuntu) 2.22 | |
| 29 version_re = re.compile(r"GNU [^ ]+ \(.*\) (\d+).(\d+)") | |
| 30 elif tool == "linker": | |
| 31 compiler = compiler + " -Xlinker --version" | |
| 32 # GNU gold (GNU Binutils for Ubuntu 2.22) 1.11 | |
| 33 version_re = re.compile(r"GNU [^ ]+ \(.*\) (\d+).(\d+)") | |
| 34 else: | |
| 35 raise Exception("Unknown tool %s" % tool) | |
| 36 | |
| 21 pipe = subprocess.Popen(compiler, shell=True, | 37 pipe = subprocess.Popen(compiler, shell=True, |
| 22 stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 38 stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 23 gcc_output, gcc_error = pipe.communicate() | 39 tool_output, tool_error = pipe.communicate() |
| 24 if pipe.returncode: | 40 if pipe.returncode: |
| 25 raise subprocess.CalledProcessError(pipe.returncode, compiler) | 41 raise subprocess.CalledProcessError(pipe.returncode, compiler) |
| 26 | 42 |
| 27 result = re.match(r"(\d+)\.(\d+)", gcc_output) | 43 result = version_re.match(tool_output) |
| 28 return result.group(1) + result.group(2) | 44 return result.group(1) + result.group(2) |
| 29 except Exception, e: | 45 except Exception, e: |
| 30 if gcc_error: | 46 if tool_error: |
| 31 sys.stderr.write(gcc_error) | 47 sys.stderr.write(tool_error) |
| 32 print >> sys.stderr, "compiler_version.py failed to execute:", compiler | 48 print >> sys.stderr, "compiler_version.py failed to execute:", compiler |
| 33 print >> sys.stderr, e | 49 print >> sys.stderr, e |
| 34 return "" | 50 return "" |
| 35 | 51 |
| 36 def main(): | 52 |
| 53 def main(args): | |
| 54 tool = "compiler" | |
| 55 if len(args) == 1: | |
| 56 tool = args[0] | |
| 57 elif len(args) > 1: | |
| 58 print "Unknown arguments!" | |
|
Lei Zhang
2014/03/17 18:43:54
Probably should return 1 in this case?
| |
| 59 | |
| 37 # Check if CXX environment variable exists and | 60 # Check if CXX environment variable exists and |
| 38 # if it does use that compiler. | 61 # if it does use that compiler. |
| 39 cxx = os.getenv("CXX", None) | 62 cxx = os.getenv("CXX", None) |
| 40 if cxx: | 63 if cxx: |
| 41 cxxversion = GetVersion(cxx) | 64 cxxversion = GetVersion(cxx, tool) |
| 42 if cxxversion != "": | 65 if cxxversion != "": |
| 43 print cxxversion | 66 print cxxversion |
| 44 return 0 | 67 return 0 |
| 45 else: | 68 else: |
| 46 # Otherwise we check the g++ version. | 69 # Otherwise we check the g++ version. |
| 47 gccversion = GetVersion("g++") | 70 gccversion = GetVersion("g++", tool) |
| 48 if gccversion != "": | 71 if gccversion != "": |
| 49 print gccversion | 72 print gccversion |
| 50 return 0 | 73 return 0 |
| 51 | 74 |
| 52 return 1 | 75 return 1 |
| 53 | 76 |
| 77 | |
| 54 if __name__ == "__main__": | 78 if __name__ == "__main__": |
| 55 sys.exit(main()) | 79 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |