maruel@chromium.org | 1f144a3 | 2011-11-24 04:13:44 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python |
cwolfe@chromium.org | ba48fa6 | 2012-05-08 02:24:49 +0900 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
craig.schlenter@chromium.org | d9110cb | 2009-11-04 02:32:10 +0900 | [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 | |
mithro@mithis.com | 254c5c1 | 2014-03-17 23:11:21 +0900 | [diff] [blame] | 17 | |
bratell@opera.com | f6e4285 | 2014-04-17 05:59:15 +0900 | [diff] [blame] | 18 | compiler_version_cache = {} # Map from (compiler, tool) -> version. |
| 19 | |
| 20 | |
thestig@chromium.org | 1d3881e | 2014-04-23 17:20:16 +0900 | [diff] [blame] | 21 | def Usage(program_name): |
| 22 | print '%s MODE TOOL' % os.path.basename(program_name) |
| 23 | print 'MODE: host or target.' |
| 24 | print 'TOOL: assembler or compiler or linker.' |
| 25 | return 1 |
| 26 | |
| 27 | |
| 28 | def ParseArgs(args): |
| 29 | if len(args) != 2: |
| 30 | raise Exception('Invalid number of arguments') |
| 31 | mode = args[0] |
| 32 | tool = args[1] |
| 33 | if mode not in ('host', 'target'): |
| 34 | raise Exception('Invalid mode: %s' % mode) |
| 35 | if tool not in ('assembler', 'compiler', 'linker'): |
| 36 | raise Exception('Invalid tool: %s' % tool) |
| 37 | return mode, tool |
| 38 | |
| 39 | |
| 40 | def GetEnvironFallback(var_list, default): |
| 41 | """Look up an environment variable from a possible list of variable names.""" |
| 42 | for var in var_list: |
| 43 | if var in os.environ: |
| 44 | return os.environ[var] |
| 45 | return default |
| 46 | |
| 47 | |
mithro@mithis.com | 254c5c1 | 2014-03-17 23:11:21 +0900 | [diff] [blame] | 48 | def GetVersion(compiler, tool): |
| 49 | tool_output = tool_error = None |
bratell@opera.com | f6e4285 | 2014-04-17 05:59:15 +0900 | [diff] [blame] | 50 | cache_key = (compiler, tool) |
| 51 | cached_version = compiler_version_cache.get(cache_key) |
| 52 | if cached_version: |
| 53 | return cached_version |
craig.schlenter@chromium.org | d9110cb | 2009-11-04 02:32:10 +0900 | [diff] [blame] | 54 | try: |
| 55 | # Note that compiler could be something tricky like "distcc g++". |
mithro@mithis.com | 254c5c1 | 2014-03-17 23:11:21 +0900 | [diff] [blame] | 56 | if tool == "compiler": |
| 57 | compiler = compiler + " -dumpversion" |
| 58 | # 4.6 |
| 59 | version_re = re.compile(r"(\d+)\.(\d+)") |
| 60 | elif tool == "assembler": |
| 61 | compiler = compiler + " -Xassembler --version -x assembler -c /dev/null" |
mithro@mithis.com | b4ca374 | 2014-03-19 04:29:28 +0900 | [diff] [blame] | 62 | # Unmodified: GNU assembler (GNU Binutils) 2.24 |
| 63 | # Ubuntu: GNU assembler (GNU Binutils for Ubuntu) 2.22 |
| 64 | # Fedora: GNU assembler version 2.23.2 |
mithro@mithis.com | c3b6cfc | 2014-03-21 23:48:38 +0900 | [diff] [blame] | 65 | version_re = re.compile(r"^GNU [^ ]+ .* (\d+).(\d+).*?$", re.M) |
mithro@mithis.com | 254c5c1 | 2014-03-17 23:11:21 +0900 | [diff] [blame] | 66 | elif tool == "linker": |
| 67 | compiler = compiler + " -Xlinker --version" |
mithro@mithis.com | b4ca374 | 2014-03-19 04:29:28 +0900 | [diff] [blame] | 68 | # Using BFD linker |
| 69 | # Unmodified: GNU ld (GNU Binutils) 2.24 |
| 70 | # Ubuntu: GNU ld (GNU Binutils for Ubuntu) 2.22 |
| 71 | # Fedora: GNU ld version 2.23.2 |
| 72 | # Using Gold linker |
| 73 | # Unmodified: GNU gold (GNU Binutils 2.24) 1.11 |
| 74 | # Ubuntu: GNU gold (GNU Binutils for Ubuntu 2.22) 1.11 |
| 75 | # Fedora: GNU gold (version 2.23.2) 1.11 |
mithro@mithis.com | c3b6cfc | 2014-03-21 23:48:38 +0900 | [diff] [blame] | 76 | version_re = re.compile(r"^GNU [^ ]+ .* (\d+).(\d+).*?$", re.M) |
mithro@mithis.com | 254c5c1 | 2014-03-17 23:11:21 +0900 | [diff] [blame] | 77 | else: |
| 78 | raise Exception("Unknown tool %s" % tool) |
| 79 | |
bratell@opera.com | f6e4285 | 2014-04-17 05:59:15 +0900 | [diff] [blame] | 80 | # Force the locale to C otherwise the version string could be localized |
| 81 | # making regex matching fail. |
| 82 | env = os.environ.copy() |
| 83 | env["LC_ALL"] = "C" |
| 84 | pipe = subprocess.Popen(compiler, shell=True, env=env, |
cwolfe@chromium.org | ba48fa6 | 2012-05-08 02:24:49 +0900 | [diff] [blame] | 85 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
mithro@mithis.com | 254c5c1 | 2014-03-17 23:11:21 +0900 | [diff] [blame] | 86 | tool_output, tool_error = pipe.communicate() |
cwolfe@chromium.org | ba48fa6 | 2012-05-08 02:24:49 +0900 | [diff] [blame] | 87 | if pipe.returncode: |
| 88 | raise subprocess.CalledProcessError(pipe.returncode, compiler) |
| 89 | |
bratell@opera.com | f6e4285 | 2014-04-17 05:59:15 +0900 | [diff] [blame] | 90 | parsed_output = version_re.match(tool_output) |
| 91 | result = parsed_output.group(1) + parsed_output.group(2) |
| 92 | compiler_version_cache[cache_key] = result |
| 93 | return result |
craig.schlenter@chromium.org | d9110cb | 2009-11-04 02:32:10 +0900 | [diff] [blame] | 94 | except Exception, e: |
mithro@mithis.com | 254c5c1 | 2014-03-17 23:11:21 +0900 | [diff] [blame] | 95 | if tool_error: |
| 96 | sys.stderr.write(tool_error) |
craig.schlenter@chromium.org | d9110cb | 2009-11-04 02:32:10 +0900 | [diff] [blame] | 97 | print >> sys.stderr, "compiler_version.py failed to execute:", compiler |
| 98 | print >> sys.stderr, e |
| 99 | return "" |
| 100 | |
mithro@mithis.com | 254c5c1 | 2014-03-17 23:11:21 +0900 | [diff] [blame] | 101 | |
| 102 | def main(args): |
thestig@chromium.org | 1d3881e | 2014-04-23 17:20:16 +0900 | [diff] [blame] | 103 | try: |
| 104 | (mode, tool) = ParseArgs(args[1:]) |
| 105 | except Exception, e: |
| 106 | sys.stderr.write(e.message + '\n\n') |
| 107 | return Usage(args[0]) |
| 108 | |
| 109 | ret_code, result = ExtractVersion(mode, tool) |
bratell@opera.com | f6e4285 | 2014-04-17 05:59:15 +0900 | [diff] [blame] | 110 | if ret_code == 0: |
| 111 | print result |
| 112 | return ret_code |
mithro@mithis.com | 5b69806 | 2014-03-24 12:40:01 +0900 | [diff] [blame] | 113 | |
bratell@opera.com | f6e4285 | 2014-04-17 05:59:15 +0900 | [diff] [blame] | 114 | |
| 115 | def DoMain(args): |
| 116 | """Hook to be called from gyp without starting a separate python |
| 117 | interpreter.""" |
thestig@chromium.org | 1d3881e | 2014-04-23 17:20:16 +0900 | [diff] [blame] | 118 | (mode, tool) = ParseArgs(args) |
| 119 | ret_code, result = ExtractVersion(mode, tool) |
bratell@opera.com | f6e4285 | 2014-04-17 05:59:15 +0900 | [diff] [blame] | 120 | if ret_code == 0: |
| 121 | return result |
| 122 | raise Exception("Failed to extract compiler version for args: %s" % args) |
| 123 | |
| 124 | |
thestig@chromium.org | 1d3881e | 2014-04-23 17:20:16 +0900 | [diff] [blame] | 125 | def ExtractVersion(mode, tool): |
| 126 | # Check if various CXX environment variables exist and use them if they |
| 127 | # exist. The preferences and fallback order is a close approximation of |
| 128 | # GenerateOutputForConfig() in GYP's ninja generator. |
| 129 | # The main difference being not supporting GYP's make_global_settings. |
| 130 | environments = ['CXX_target', 'CXX'] |
| 131 | if mode == 'host': |
| 132 | environments = ['CXX_host'] + environments; |
| 133 | compiler = GetEnvironFallback(environments, 'c++') |
mithro@mithis.com | 254c5c1 | 2014-03-17 23:11:21 +0900 | [diff] [blame] | 134 | |
bratell@opera.com | f6e4285 | 2014-04-17 05:59:15 +0900 | [diff] [blame] | 135 | if compiler: |
| 136 | compiler_version = GetVersion(compiler, tool) |
| 137 | if compiler_version != "": |
| 138 | return (0, compiler_version) |
bratell@opera.com | f6e4285 | 2014-04-17 05:59:15 +0900 | [diff] [blame] | 139 | return (1, None) |
craig.schlenter@chromium.org | d9110cb | 2009-11-04 02:32:10 +0900 | [diff] [blame] | 140 | |
mithro@mithis.com | 254c5c1 | 2014-03-17 23:11:21 +0900 | [diff] [blame] | 141 | |
craig.schlenter@chromium.org | d9110cb | 2009-11-04 02:32:10 +0900 | [diff] [blame] | 142 | if __name__ == "__main__": |
thestig@chromium.org | 1d3881e | 2014-04-23 17:20:16 +0900 | [diff] [blame] | 143 | sys.exit(main(sys.argv)) |