compiler_version.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. """Compiler version checking tool for gcc
  6. Print gcc version as XY if you are running gcc X.Y.*.
  7. This is used to tweak build flags for gcc 4.4.
  8. """
  9. import os
  10. import re
  11. import subprocess
  12. import sys
  13. def GetVersion(compiler):
  14. try:
  15. # Note that compiler could be something tricky like "distcc g++".
  16. compiler = compiler + " -dumpversion"
  17. pipe = subprocess.Popen(compiler, shell=True,
  18. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  19. gcc_output, gcc_error = pipe.communicate()
  20. if pipe.returncode:
  21. raise subprocess.CalledProcessError(pipe.returncode, compiler)
  22. result = re.match(r"(\d+)\.(\d+)", gcc_output)
  23. return result.group(1) + result.group(2)
  24. except Exception, e:
  25. if gcc_error:
  26. sys.stderr.write(gcc_error)
  27. print >> sys.stderr, "compiler_version.py failed to execute:", compiler
  28. print >> sys.stderr, e
  29. return ""
  30. def GetVersionFromEnvironment(compiler_env):
  31. """ Returns the version of compiler
  32. If the compiler was set by the given environment variable and exists,
  33. return its version, otherwise None is returned.
  34. """
  35. cxx = os.getenv(compiler_env, None)
  36. if cxx:
  37. cxx_version = GetVersion(cxx)
  38. if cxx_version != "":
  39. return cxx_version
  40. return None
  41. def main():
  42. # Check if CXX_target or CXX environment variable exists an if it does use
  43. # that compiler.
  44. # TODO: Fix ninja (see http://crbug.com/140900) instead and remove this code
  45. # In ninja's cross compile mode, the CXX_target is target compiler, while
  46. # the CXX is host. The CXX_target needs be checked first, though the target
  47. # and host compiler have different version, there seems no issue to use the
  48. # target compiler's version number as gcc_version in Android.
  49. cxx_version = GetVersionFromEnvironment("CXX_target")
  50. if cxx_version:
  51. print cxx_version
  52. return 0
  53. cxx_version = GetVersionFromEnvironment("CXX")
  54. if cxx_version:
  55. print cxx_version
  56. return 0
  57. # Otherwise we check the g++ version.
  58. gccversion = GetVersion("g++")
  59. if gccversion != "":
  60. print gccversion
  61. return 0
  62. return 1
  63. if __name__ == "__main__":
  64. sys.exit(main())