Home | History | Annotate | Download | only in build
      1 #!/usr/bin/env python
      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
      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 
     17 
     18 compiler_version_cache = {}  # Map from (compiler, tool) -> version.
     19 
     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',):
     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 
     48 def GetVersion(compiler, tool):
     49   tool_output = tool_error = None
     50   cache_key = (compiler, tool)
     51   cached_version = compiler_version_cache.get(cache_key)
     52   if cached_version:
     53     return cached_version
     54   try:
     55     # Note that compiler could be something tricky like "distcc g++".
     56     if tool == "assembler":
     57       compiler = compiler + " -Xassembler --version -x assembler -c /dev/null"
     58       # Unmodified: GNU assembler (GNU Binutils) 2.24
     59       # Ubuntu: GNU assembler (GNU Binutils for Ubuntu) 2.22
     60       # Fedora: GNU assembler version 2.23.2
     61       version_re = re.compile(r"^GNU [^ ]+ .* (\d+).(\d+).*?$", re.M)
     62     else:
     63       raise Exception("Unknown tool %s" % tool)
     64 
     65     # Force the locale to C otherwise the version string could be localized
     66     # making regex matching fail.
     67     env = os.environ.copy()
     68     env["LC_ALL"] = "C"
     69     pipe = subprocess.Popen(compiler, shell=True, env=env,
     70                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
     71     tool_output, tool_error = pipe.communicate()
     72     if pipe.returncode:
     73       raise subprocess.CalledProcessError(pipe.returncode, compiler)
     74 
     75     parsed_output = version_re.match(tool_output)
     76     result = parsed_output.group(1) + parsed_output.group(2)
     77     compiler_version_cache[cache_key] = result
     78     return result
     79   except Exception, e:
     80     if tool_error:
     81       sys.stderr.write(tool_error)
     82     print >> sys.stderr, "compiler_version.py failed to execute:", compiler
     83     print >> sys.stderr, e
     84     return ""
     85 
     86 
     87 def main(args):
     88   try:
     89     (mode, tool) = ParseArgs(args[1:])
     90   except Exception, e:
     91     sys.stderr.write(e.message + '\n\n')
     92     return Usage(args[0])
     93 
     94   ret_code, result = ExtractVersion(mode, tool)
     95   if ret_code == 0:
     96     print result
     97   return ret_code
     98 
     99 
    100 def DoMain(args):
    101   """Hook to be called from gyp without starting a separate python
    102   interpreter."""
    103   (mode, tool) = ParseArgs(args)
    104   ret_code, result = ExtractVersion(mode, tool)
    105   if ret_code == 0:
    106     return result
    107   raise Exception("Failed to extract compiler version for args: %s" % args)
    108 
    109 
    110 def ExtractVersion(mode, tool):
    111   # Check if various CXX environment variables exist and use them if they
    112   # exist. The preferences and fallback order is a close approximation of
    113   # GenerateOutputForConfig() in GYP's ninja generator.
    114   # The main difference being not supporting GYP's make_global_settings.
    115   environments = ['CXX_target', 'CXX']
    116   if mode == 'host':
    117     environments = ['CXX_host'] + environments;
    118   compiler = GetEnvironFallback(environments, 'c++')
    119 
    120   if compiler:
    121     compiler_version = GetVersion(compiler, tool)
    122     if compiler_version != "":
    123       return (0, compiler_version)
    124   return (1, None)
    125 
    126 
    127 if __name__ == "__main__":
    128   sys.exit(main(sys.argv))
    129