clang_format.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python3
  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__(
  18. self,
  19. 'Problem while looking for clang-format in Chromium source tree:\n'
  20. '%s' % e)
  21. def FindClangFormatToolInChromiumTree():
  22. """Return a path to the clang-format executable, or die trying."""
  23. primary_solution_path = gclient_paths.GetPrimarySolutionPath()
  24. if primary_solution_path:
  25. bin_path = os.path.join(primary_solution_path, 'third_party',
  26. 'clang-format',
  27. 'clang-format' + gclient_paths.GetExeSuffix())
  28. if os.path.exists(bin_path):
  29. return bin_path
  30. bin_path = gclient_paths.GetBuildtoolsPlatformBinaryPath()
  31. if not bin_path:
  32. raise NotFoundError(
  33. 'Could not find checkout in any parent of the current path.\n'
  34. 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium '
  35. 'checkout.')
  36. tool_path = os.path.join(bin_path,
  37. 'clang-format' + gclient_paths.GetExeSuffix())
  38. if not os.path.exists(tool_path):
  39. raise NotFoundError('File does not exist: %s' % tool_path)
  40. return tool_path
  41. def FindClangFormatScriptInChromiumTree(script_name):
  42. """Return a path to a clang-format helper script, or die trying."""
  43. primary_solution_path = gclient_paths.GetPrimarySolutionPath()
  44. if primary_solution_path:
  45. script_path = os.path.join(primary_solution_path, 'third_party',
  46. 'clang-format', 'script', script_name)
  47. if os.path.exists(script_path):
  48. return script_path
  49. tools_path = gclient_paths.GetBuildtoolsPath()
  50. if not tools_path:
  51. raise NotFoundError(
  52. 'Could not find checkout in any parent of the current path.\n',
  53. 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium '
  54. 'checkout.')
  55. script_path = os.path.join(tools_path, 'clang_format', 'script',
  56. script_name)
  57. if not os.path.exists(script_path):
  58. raise NotFoundError('File does not exist: %s' % script_path)
  59. return script_path
  60. def main(args):
  61. try:
  62. tool = FindClangFormatToolInChromiumTree()
  63. except NotFoundError as e:
  64. sys.stderr.write("%s\n" % str(e))
  65. return 1
  66. # Add some visibility to --help showing where the tool lives, since this
  67. # redirection can be a little opaque.
  68. help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list')
  69. if any(match in args for match in help_syntax):
  70. print('\nDepot tools redirects you to the clang-format at:\n %s\n' %
  71. tool)
  72. return subprocess.call([tool] + args)
  73. if __name__ == '__main__':
  74. try:
  75. sys.exit(main(sys.argv[1:]))
  76. except KeyboardInterrupt:
  77. sys.stderr.write('interrupted\n')
  78. sys.exit(1)