Index: build/compiler_version.py |
=================================================================== |
--- build/compiler_version.py (revision 265327) |
+++ build/compiler_version.py (working copy) |
@@ -18,6 +18,33 @@ |
compiler_version_cache = {} # Map from (compiler, tool) -> version. |
+def Usage(program_name): |
+ print '%s MODE TOOL' % os.path.basename(program_name) |
+ print 'MODE: host or target.' |
+ print 'TOOL: assembler or compiler or linker.' |
+ return 1 |
+ |
+ |
+def ParseArgs(args): |
+ if len(args) != 2: |
+ return (None, None) |
+ mode = args[0] |
+ tool = args[1] |
+ if (mode not in ('host', 'target') or |
+ 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.
|
+ return (None, None) |
+ return mode, tool |
+ |
+ |
+def GetEnvironFallback(var_list, default): |
+ """Look up a key in the environment, with fallback to secondary keys |
+ 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
|
+ for var in var_list: |
+ if var in os.environ: |
+ return os.environ[var] |
+ return default |
+ |
+ |
def GetVersion(compiler, tool): |
tool_output = tool_error = None |
cache_key = (compiler, tool) |
@@ -73,7 +100,10 @@ |
def main(args): |
- ret_code, result = ExtractVersion(args) |
+ (mode, tool) = ParseArgs(args[1:]) |
+ if not mode or not tool: |
+ return Usage(args[0]) |
+ ret_code, result = ExtractVersion(mode, tool) |
if ret_code == 0: |
print result |
return ret_code |
@@ -82,29 +112,31 @@ |
def DoMain(args): |
"""Hook to be called from gyp without starting a separate python |
interpreter.""" |
- ret_code, result = ExtractVersion(args) |
+ (mode, tool) = ParseArgs(args) |
+ ret_code, result = ExtractVersion(mode, tool) |
if ret_code == 0: |
return result |
raise Exception("Failed to extract compiler version for args: %s" % args) |
-def ExtractVersion(args): |
- tool = "compiler" |
- if len(args) == 1: |
- tool = args[0] |
- elif len(args) > 1: |
- print "Unknown arguments!" |
+def ExtractVersion(mode, tool): |
+ # Check if various CXX environment variables exist and use them if they |
+ # 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.
|
+ # GenerateOutputForConfig() in GYP's ninja generator. |
+ # The main difference being not supporting GYP's make_global_settings. |
+ environments = ['CXX_target', 'CXX'] |
+ if mode == 'host': |
+ new_environments = ['CXX_host'] |
+ new_environments.extend(environments) |
+ environments = new_environments |
mithro-old
2014/04/23 02:19:17
environments = ['CXX_host'] + environments
Lei Zhang
2014/04/23 02:33:29
Done.
|
+ compiler = GetEnvironFallback(environments, 'c++') |
- # Check if CXX environment variable exists and if it does use that |
- # compiler, otherwise check g++. |
- compiler = os.getenv("CXX", "g++") |
if compiler: |
compiler_version = GetVersion(compiler, tool) |
if compiler_version != "": |
return (0, compiler_version) |
- |
return (1, None) |
if __name__ == "__main__": |
- sys.exit(main(sys.argv[1:])) |
+ sys.exit(main(sys.argv)) |