Chromium Code Reviews| Index: build/compiler_version.py |
| diff --git a/build/compiler_version.py b/build/compiler_version.py |
| index fd23d89bd63b21c2304b80e22c12df7450120b42..82d4c336e9f009f972bd126166adde4318a45564 100755 |
| --- a/build/compiler_version.py |
| +++ b/build/compiler_version.py |
| @@ -14,9 +14,14 @@ import re |
| import subprocess |
| import sys |
| +compiler_version_cache = {} # Map from (compiler, tool) -> version. |
|
scottmg
2014/04/15 18:29:02
nit; two blank lines between top level things in p
scottmg
2014/04/15 18:29:02
nit; two spaces after code before #
|
| def GetVersion(compiler, tool): |
| tool_output = tool_error = None |
| + cache_key = (compiler, tool) |
| + cached_version = compiler_version_cache.get(cache_key) |
|
scottmg
2014/04/15 18:29:02
is it the cache (not-invoking-compiler) or the not
Daniel Bratell
2014/04/15 19:37:41
A bit hard to say because the python profiler does
|
| + if cached_version: |
| + return cached_version |
| try: |
| # Note that compiler could be something tricky like "distcc g++". |
| if tool == "compiler": |
| @@ -43,14 +48,20 @@ def GetVersion(compiler, tool): |
| else: |
| raise Exception("Unknown tool %s" % tool) |
| - pipe = subprocess.Popen(compiler, shell=True, |
| + # Force the locale to C otherwise the version string could be localized |
| + # making regex matching fail. |
| + env = os.environ.copy() |
| + env["LC_ALL"] = "C" |
| + pipe = subprocess.Popen(compiler, shell=True, env=env, |
| stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| tool_output, tool_error = pipe.communicate() |
| if pipe.returncode: |
| raise subprocess.CalledProcessError(pipe.returncode, compiler) |
| - result = version_re.match(tool_output) |
| - return result.group(1) + result.group(2) |
| + parsed_output = version_re.match(tool_output) |
| + result = parsed_output.group(1) + parsed_output.group(2) |
| + compiler_version_cache[cache_key] = result |
| + return result |
| except Exception, e: |
| if tool_error: |
| sys.stderr.write(tool_error) |
| @@ -60,10 +71,20 @@ def GetVersion(compiler, tool): |
| def main(args): |
| - # Force the locale to C otherwise the version string could be localized |
| - # making regex matching fail. |
| - os.environ["LC_ALL"] = "C" |
| + (ret_code, result) = ExtractVersion(args) |
|
scottmg
2014/04/15 18:29:02
no parens around return value
|
| + if ret_code == 0: |
| + print(result) |
|
scottmg
2014/04/15 18:29:02
no parens around print (as this file doesn't seem
|
| + return ret_code |
| +def DoMain(args): |
| + """Hook to be called from gyp withing starting a separate python |
|
scottmg
2014/04/15 18:29:02
withing -> without
|
| + interpreter.""" |
| + (ret_code, result) = ExtractVersion(args) |
|
scottmg
2014/04/15 18:29:02
no parens on return
|
| + 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] |
| @@ -76,16 +97,14 @@ def main(args): |
| if cxx: |
| cxxversion = GetVersion(cxx, tool) |
| if cxxversion != "": |
| - print cxxversion |
| - return 0 |
| + return (0, cxxversion) |
| else: |
| # Otherwise we check the g++ version. |
| gccversion = GetVersion("g++", tool) |
| if gccversion != "": |
| - print gccversion |
| - return 0 |
| + return (0, gccversion) |
| - return 1 |
| + return (1, None) |
| if __name__ == "__main__": |