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. | 18 compiler_version_cache = {} # Map from (compiler, tool) -> version. |
19 | 19 |
20 | 20 |
| 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 |
21 def GetVersion(compiler, tool): | 48 def GetVersion(compiler, tool): |
22 tool_output = tool_error = None | 49 tool_output = tool_error = None |
23 cache_key = (compiler, tool) | 50 cache_key = (compiler, tool) |
24 cached_version = compiler_version_cache.get(cache_key) | 51 cached_version = compiler_version_cache.get(cache_key) |
25 if cached_version: | 52 if cached_version: |
26 return cached_version | 53 return cached_version |
27 try: | 54 try: |
28 # Note that compiler could be something tricky like "distcc g++". | 55 # Note that compiler could be something tricky like "distcc g++". |
29 if tool == "compiler": | 56 if tool == "compiler": |
30 compiler = compiler + " -dumpversion" | 57 compiler = compiler + " -dumpversion" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 return result | 93 return result |
67 except Exception, e: | 94 except Exception, e: |
68 if tool_error: | 95 if tool_error: |
69 sys.stderr.write(tool_error) | 96 sys.stderr.write(tool_error) |
70 print >> sys.stderr, "compiler_version.py failed to execute:", compiler | 97 print >> sys.stderr, "compiler_version.py failed to execute:", compiler |
71 print >> sys.stderr, e | 98 print >> sys.stderr, e |
72 return "" | 99 return "" |
73 | 100 |
74 | 101 |
75 def main(args): | 102 def main(args): |
76 ret_code, result = ExtractVersion(args) | 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) |
77 if ret_code == 0: | 110 if ret_code == 0: |
78 print result | 111 print result |
79 return ret_code | 112 return ret_code |
80 | 113 |
81 | 114 |
82 def DoMain(args): | 115 def DoMain(args): |
83 """Hook to be called from gyp without starting a separate python | 116 """Hook to be called from gyp without starting a separate python |
84 interpreter.""" | 117 interpreter.""" |
85 ret_code, result = ExtractVersion(args) | 118 (mode, tool) = ParseArgs(args) |
| 119 ret_code, result = ExtractVersion(mode, tool) |
86 if ret_code == 0: | 120 if ret_code == 0: |
87 return result | 121 return result |
88 raise Exception("Failed to extract compiler version for args: %s" % args) | 122 raise Exception("Failed to extract compiler version for args: %s" % args) |
89 | 123 |
90 | 124 |
91 def ExtractVersion(args): | 125 def ExtractVersion(mode, tool): |
92 tool = "compiler" | 126 # Check if various CXX environment variables exist and use them if they |
93 if len(args) == 1: | 127 # exist. The preferences and fallback order is a close approximation of |
94 tool = args[0] | 128 # GenerateOutputForConfig() in GYP's ninja generator. |
95 elif len(args) > 1: | 129 # The main difference being not supporting GYP's make_global_settings. |
96 print "Unknown arguments!" | 130 environments = ['CXX_target', 'CXX'] |
| 131 if mode == 'host': |
| 132 environments = ['CXX_host'] + environments; |
| 133 compiler = GetEnvironFallback(environments, 'c++') |
97 | 134 |
98 # Check if CXX environment variable exists and if it does use that | |
99 # compiler, otherwise check g++. | |
100 compiler = os.getenv("CXX", "g++") | |
101 if compiler: | 135 if compiler: |
102 compiler_version = GetVersion(compiler, tool) | 136 compiler_version = GetVersion(compiler, tool) |
103 if compiler_version != "": | 137 if compiler_version != "": |
104 return (0, compiler_version) | 138 return (0, compiler_version) |
105 | |
106 return (1, None) | 139 return (1, None) |
107 | 140 |
108 | 141 |
109 if __name__ == "__main__": | 142 if __name__ == "__main__": |
110 sys.exit(main(sys.argv[1:])) | 143 sys.exit(main(sys.argv)) |
OLD | NEW |