clang_format.py 3.4 KB

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