| 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 | 17 |
| 18 compiler_version_cache = {} # Map from (compiler, tool) -> version. |
| 19 |
| 20 |
| 18 def GetVersion(compiler, tool): | 21 def GetVersion(compiler, tool): |
| 19 tool_output = tool_error = None | 22 tool_output = tool_error = None |
| 23 cache_key = (compiler, tool) |
| 24 cached_version = compiler_version_cache.get(cache_key) |
| 25 if cached_version: |
| 26 return cached_version |
| 20 try: | 27 try: |
| 21 # Note that compiler could be something tricky like "distcc g++". | 28 # Note that compiler could be something tricky like "distcc g++". |
| 22 if tool == "compiler": | 29 if tool == "compiler": |
| 23 compiler = compiler + " -dumpversion" | 30 compiler = compiler + " -dumpversion" |
| 24 # 4.6 | 31 # 4.6 |
| 25 version_re = re.compile(r"(\d+)\.(\d+)") | 32 version_re = re.compile(r"(\d+)\.(\d+)") |
| 26 elif tool == "assembler": | 33 elif tool == "assembler": |
| 27 compiler = compiler + " -Xassembler --version -x assembler -c /dev/null" | 34 compiler = compiler + " -Xassembler --version -x assembler -c /dev/null" |
| 28 # Unmodified: GNU assembler (GNU Binutils) 2.24 | 35 # Unmodified: GNU assembler (GNU Binutils) 2.24 |
| 29 # Ubuntu: GNU assembler (GNU Binutils for Ubuntu) 2.22 | 36 # Ubuntu: GNU assembler (GNU Binutils for Ubuntu) 2.22 |
| 30 # Fedora: GNU assembler version 2.23.2 | 37 # Fedora: GNU assembler version 2.23.2 |
| 31 version_re = re.compile(r"^GNU [^ ]+ .* (\d+).(\d+).*?$", re.M) | 38 version_re = re.compile(r"^GNU [^ ]+ .* (\d+).(\d+).*?$", re.M) |
| 32 elif tool == "linker": | 39 elif tool == "linker": |
| 33 compiler = compiler + " -Xlinker --version" | 40 compiler = compiler + " -Xlinker --version" |
| 34 # Using BFD linker | 41 # Using BFD linker |
| 35 # Unmodified: GNU ld (GNU Binutils) 2.24 | 42 # Unmodified: GNU ld (GNU Binutils) 2.24 |
| 36 # Ubuntu: GNU ld (GNU Binutils for Ubuntu) 2.22 | 43 # Ubuntu: GNU ld (GNU Binutils for Ubuntu) 2.22 |
| 37 # Fedora: GNU ld version 2.23.2 | 44 # Fedora: GNU ld version 2.23.2 |
| 38 # Using Gold linker | 45 # Using Gold linker |
| 39 # Unmodified: GNU gold (GNU Binutils 2.24) 1.11 | 46 # Unmodified: GNU gold (GNU Binutils 2.24) 1.11 |
| 40 # Ubuntu: GNU gold (GNU Binutils for Ubuntu 2.22) 1.11 | 47 # Ubuntu: GNU gold (GNU Binutils for Ubuntu 2.22) 1.11 |
| 41 # Fedora: GNU gold (version 2.23.2) 1.11 | 48 # Fedora: GNU gold (version 2.23.2) 1.11 |
| 42 version_re = re.compile(r"^GNU [^ ]+ .* (\d+).(\d+).*?$", re.M) | 49 version_re = re.compile(r"^GNU [^ ]+ .* (\d+).(\d+).*?$", re.M) |
| 43 else: | 50 else: |
| 44 raise Exception("Unknown tool %s" % tool) | 51 raise Exception("Unknown tool %s" % tool) |
| 45 | 52 |
| 46 pipe = subprocess.Popen(compiler, shell=True, | 53 # Force the locale to C otherwise the version string could be localized |
| 54 # making regex matching fail. |
| 55 env = os.environ.copy() |
| 56 env["LC_ALL"] = "C" |
| 57 pipe = subprocess.Popen(compiler, shell=True, env=env, |
| 47 stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 58 stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 48 tool_output, tool_error = pipe.communicate() | 59 tool_output, tool_error = pipe.communicate() |
| 49 if pipe.returncode: | 60 if pipe.returncode: |
| 50 raise subprocess.CalledProcessError(pipe.returncode, compiler) | 61 raise subprocess.CalledProcessError(pipe.returncode, compiler) |
| 51 | 62 |
| 52 result = version_re.match(tool_output) | 63 parsed_output = version_re.match(tool_output) |
| 53 return result.group(1) + result.group(2) | 64 result = parsed_output.group(1) + parsed_output.group(2) |
| 65 compiler_version_cache[cache_key] = result |
| 66 return result |
| 54 except Exception, e: | 67 except Exception, e: |
| 55 if tool_error: | 68 if tool_error: |
| 56 sys.stderr.write(tool_error) | 69 sys.stderr.write(tool_error) |
| 57 print >> sys.stderr, "compiler_version.py failed to execute:", compiler | 70 print >> sys.stderr, "compiler_version.py failed to execute:", compiler |
| 58 print >> sys.stderr, e | 71 print >> sys.stderr, e |
| 59 return "" | 72 return "" |
| 60 | 73 |
| 61 | 74 |
| 62 def main(args): | 75 def main(args): |
| 63 # Force the locale to C otherwise the version string could be localized | 76 ret_code, result = ExtractVersion(args) |
| 64 # making regex matching fail. | 77 if ret_code == 0: |
| 65 os.environ["LC_ALL"] = "C" | 78 print result |
| 79 return ret_code |
| 66 | 80 |
| 81 |
| 82 def DoMain(args): |
| 83 """Hook to be called from gyp without starting a separate python |
| 84 interpreter.""" |
| 85 ret_code, result = ExtractVersion(args) |
| 86 if ret_code == 0: |
| 87 return result |
| 88 raise Exception("Failed to extract compiler version for args: %s" % args) |
| 89 |
| 90 |
| 91 def ExtractVersion(args): |
| 67 tool = "compiler" | 92 tool = "compiler" |
| 68 if len(args) == 1: | 93 if len(args) == 1: |
| 69 tool = args[0] | 94 tool = args[0] |
| 70 elif len(args) > 1: | 95 elif len(args) > 1: |
| 71 print "Unknown arguments!" | 96 print "Unknown arguments!" |
| 72 | 97 |
| 73 # Check if CXX environment variable exists and | 98 # Check if CXX environment variable exists and if it does use that |
| 74 # if it does use that compiler. | 99 # compiler, otherwise check g++. |
| 75 cxx = os.getenv("CXX", None) | 100 compiler = os.getenv("CXX", "g++") |
| 76 if cxx: | 101 if compiler: |
| 77 cxxversion = GetVersion(cxx, tool) | 102 compiler_version = GetVersion(compiler, tool) |
| 78 if cxxversion != "": | 103 if compiler_version != "": |
| 79 print cxxversion | 104 return (0, compiler_version) |
| 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 | |
| 87 | 105 |
| 88 return 1 | 106 return (1, None) |
| 89 | 107 |
| 90 | 108 |
| 91 if __name__ == "__main__": | 109 if __name__ == "__main__": |
| 92 sys.exit(main(sys.argv[1:])) | 110 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |