clang_format.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python
  2. # Copyright 2014 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. """Redirects to the version of clang-format checked into the Chrome tree.
  6. clang-format binaries are pulled down from Google Cloud Storage whenever you
  7. sync Chrome, to platform-specific locations. This script knows how to locate
  8. those tools, assuming the script is invoked from inside a Chromium checkout."""
  9. from __future__ import print_function
  10. import gclient_paths
  11. import os
  12. import subprocess
  13. import sys
  14. class NotFoundError(Exception):
  15. """A file could not be found."""
  16. def __init__(self, e):
  17. Exception.__init__(self,
  18. 'Problem while looking for clang-format in Chromium source tree:\n'
  19. '%s' % e)
  20. def FindClangFormatToolInChromiumTree():
  21. """Return a path to the clang-format executable, or die trying."""
  22. bin_path = gclient_paths.GetBuildtoolsPlatformBinaryPath()
  23. if not bin_path:
  24. raise NotFoundError(
  25. 'Could not find checkout in any parent of the current path.\n'
  26. 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium checkout.')
  27. tool_path = os.path.join(bin_path,
  28. 'clang-format' + gclient_paths.GetExeSuffix())
  29. if not os.path.exists(tool_path):
  30. raise NotFoundError('File does not exist: %s' % tool_path)
  31. return tool_path
  32. def FindClangFormatScriptInChromiumTree(script_name):
  33. """Return a path to a clang-format helper script, or die trying."""
  34. tools_path = gclient_paths.GetBuildtoolsPath()
  35. if not tools_path:
  36. raise NotFoundError(
  37. 'Could not find checkout in any parent of the current path.\n',
  38. 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium checkout.')
  39. script_path = os.path.join(tools_path, 'clang_format', 'script', script_name)
  40. if not os.path.exists(script_path):
  41. raise NotFoundError('File does not exist: %s' % script_path)
  42. return script_path
  43. def main(args):
  44. try:
  45. tool = FindClangFormatToolInChromiumTree()
  46. except NotFoundError as e:
  47. sys.stderr.write("%s\n" % str(e))
  48. return 1
  49. # Add some visibility to --help showing where the tool lives, since this
  50. # redirection can be a little opaque.
  51. help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list')
  52. if any(match in args for match in help_syntax):
  53. print(
  54. '\nDepot tools redirects you to the clang-format at:\n %s\n' % tool)
  55. return subprocess.call([tool] + args)
  56. if __name__ == '__main__':
  57. try:
  58. sys.exit(main(sys.argv[1:]))
  59. except KeyboardInterrupt:
  60. sys.stderr.write('interrupted\n')
  61. sys.exit(1)