michael@0: #!/usr/bin/env python michael@0: # Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: # Use of this source code is governed by a BSD-style license that can be michael@0: # found in the LICENSE file. michael@0: michael@0: """Compiler version checking tool for gcc michael@0: michael@0: Print gcc version as XY if you are running gcc X.Y.*. michael@0: This is used to tweak build flags for gcc 4.4. michael@0: """ michael@0: michael@0: import os michael@0: import re michael@0: import subprocess michael@0: import sys michael@0: michael@0: def GetVersion(compiler): michael@0: try: michael@0: # Note that compiler could be something tricky like "distcc g++". michael@0: compiler = compiler + " -dumpversion" michael@0: pipe = subprocess.Popen(compiler, shell=True, michael@0: stdout=subprocess.PIPE, stderr=subprocess.PIPE) michael@0: gcc_output, gcc_error = pipe.communicate() michael@0: if pipe.returncode: michael@0: raise subprocess.CalledProcessError(pipe.returncode, compiler) michael@0: michael@0: result = re.match(r"(\d+)\.(\d+)", gcc_output) michael@0: return result.group(1) + result.group(2) michael@0: except Exception, e: michael@0: if gcc_error: michael@0: sys.stderr.write(gcc_error) michael@0: print >> sys.stderr, "compiler_version.py failed to execute:", compiler michael@0: print >> sys.stderr, e michael@0: return "" michael@0: michael@0: def GetVersionFromEnvironment(compiler_env): michael@0: """ Returns the version of compiler michael@0: michael@0: If the compiler was set by the given environment variable and exists, michael@0: return its version, otherwise None is returned. michael@0: """ michael@0: cxx = os.getenv(compiler_env, None) michael@0: if cxx: michael@0: cxx_version = GetVersion(cxx) michael@0: if cxx_version != "": michael@0: return cxx_version michael@0: return None michael@0: michael@0: def main(): michael@0: # Check if CXX_target or CXX environment variable exists an if it does use michael@0: # that compiler. michael@0: # TODO: Fix ninja (see http://crbug.com/140900) instead and remove this code michael@0: # In ninja's cross compile mode, the CXX_target is target compiler, while michael@0: # the CXX is host. The CXX_target needs be checked first, though the target michael@0: # and host compiler have different version, there seems no issue to use the michael@0: # target compiler's version number as gcc_version in Android. michael@0: cxx_version = GetVersionFromEnvironment("CXX_target") michael@0: if cxx_version: michael@0: print cxx_version michael@0: return 0 michael@0: michael@0: cxx_version = GetVersionFromEnvironment("CXX") michael@0: if cxx_version: michael@0: print cxx_version michael@0: return 0 michael@0: michael@0: # Otherwise we check the g++ version. michael@0: gccversion = GetVersion("g++") michael@0: if gccversion != "": michael@0: print gccversion michael@0: return 0 michael@0: michael@0: return 1 michael@0: michael@0: if __name__ == "__main__": michael@0: sys.exit(main())