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 | 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 return (None, None) | |
| 31 mode = args[0] | |
| 32 tool = args[1] | |
| 33 if (mode not in ('host', 'target') or | |
| 34 tool not in ('assembler', 'compiler', 'linker')): | |
|
mithro-old
2014/04/23 02:19:17
Could just raise an exception here...
Lei Zhang
2014/04/23 02:33:29
I want main() to be able to run Usage() instead.
| |
| 35 return (None, None) | |
| 36 return mode, tool | |
| 37 | |
| 38 | |
| 39 def GetEnvironFallback(var_list, default): | |
| 40 """Look up a key in the environment, with fallback to secondary keys | |
| 41 and finally falling back to a default value.""" | |
|
mithro-old
2014/04/23 02:19:17
Technically the first line description should fit
Lei Zhang
2014/04/23 02:33:29
I copied this out of gyp. Shortened the comment a
| |
| 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 (mode, tool) = ParseArgs(args[1:]) |
| 104 if not mode or not tool: | |
| 105 return Usage(args[0]) | |
| 106 ret_code, result = ExtractVersion(mode, tool) | |
| 77 if ret_code == 0: | 107 if ret_code == 0: |
| 78 print result | 108 print result |
| 79 return ret_code | 109 return ret_code |
| 80 | 110 |
| 81 | 111 |
| 82 def DoMain(args): | 112 def DoMain(args): |
| 83 """Hook to be called from gyp without starting a separate python | 113 """Hook to be called from gyp without starting a separate python |
| 84 interpreter.""" | 114 interpreter.""" |
| 85 ret_code, result = ExtractVersion(args) | 115 (mode, tool) = ParseArgs(args) |
| 116 ret_code, result = ExtractVersion(mode, tool) | |
| 86 if ret_code == 0: | 117 if ret_code == 0: |
| 87 return result | 118 return result |
| 88 raise Exception("Failed to extract compiler version for args: %s" % args) | 119 raise Exception("Failed to extract compiler version for args: %s" % args) |
| 89 | 120 |
| 90 | 121 |
| 91 def ExtractVersion(args): | 122 def ExtractVersion(mode, tool): |
| 92 tool = "compiler" | 123 # Check if various CXX environment variables exist and use them if they |
| 93 if len(args) == 1: | 124 # exit. The preferences and fallback order is a close approximation of |
|
mithro-old
2014/04/23 02:19:17
s/exit/exist/
Lei Zhang
2014/04/23 02:33:29
Done.
| |
| 94 tool = args[0] | 125 # GenerateOutputForConfig() in GYP's ninja generator. |
| 95 elif len(args) > 1: | 126 # The main difference being not supporting GYP's make_global_settings. |
| 96 print "Unknown arguments!" | 127 environments = ['CXX_target', 'CXX'] |
| 128 if mode == 'host': | |
| 129 new_environments = ['CXX_host'] | |
| 130 new_environments.extend(environments) | |
| 131 environments = new_environments | |
|
mithro-old
2014/04/23 02:19:17
environments = ['CXX_host'] + environments
Lei Zhang
2014/04/23 02:33:29
Done.
| |
| 132 compiler = GetEnvironFallback(environments, 'c++') | |
| 97 | 133 |
| 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: | 134 if compiler: |
| 102 compiler_version = GetVersion(compiler, tool) | 135 compiler_version = GetVersion(compiler, tool) |
| 103 if compiler_version != "": | 136 if compiler_version != "": |
| 104 return (0, compiler_version) | 137 return (0, compiler_version) |
| 105 | |
| 106 return (1, None) | 138 return (1, None) |
| 107 | 139 |
| 108 | 140 |
| 109 if __name__ == "__main__": | 141 if __name__ == "__main__": |
| 110 sys.exit(main(sys.argv[1:])) | 142 sys.exit(main(sys.argv)) |
| OLD | NEW |