clang_format.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 detect_host_arch
  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. # TODO(b/336843583): Remove old_tool_path when migrated over
  37. old_tool_path = os.path.join(bin_path,
  38. 'clang-format' + gclient_paths.GetExeSuffix())
  39. new_bin_path = bin_path
  40. arch = detect_host_arch.HostArch()
  41. if sys.platform == 'darwin' and arch == 'arm64':
  42. new_bin_path += '_arm64'
  43. old_new_tool_path = os.path.join(
  44. new_bin_path, 'format', 'clang-format' + gclient_paths.GetExeSuffix())
  45. latest_new_tool_path = os.path.join(
  46. f'{new_bin_path}-format', 'clang-format' + gclient_paths.GetExeSuffix())
  47. possible_paths = [latest_new_tool_path, old_new_tool_path, old_tool_path]
  48. for path in possible_paths:
  49. if os.path.exists(path):
  50. return path
  51. raise NotFoundError('File does not exist in either path: %s' %
  52. possible_paths)
  53. def FindClangFormatScriptInChromiumTree(script_name):
  54. """Return a path to a clang-format helper script, or die trying."""
  55. primary_solution_path = gclient_paths.GetPrimarySolutionPath()
  56. if primary_solution_path:
  57. script_path = os.path.join(primary_solution_path, 'third_party',
  58. 'clang-format', 'script', script_name)
  59. if os.path.exists(script_path):
  60. return script_path
  61. tools_path = gclient_paths.GetBuildtoolsPath()
  62. if not tools_path:
  63. raise NotFoundError(
  64. 'Could not find checkout in any parent of the current path.\n',
  65. 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium '
  66. 'checkout.')
  67. script_path = os.path.join(tools_path, 'clang_format', 'script',
  68. script_name)
  69. if not os.path.exists(script_path):
  70. raise NotFoundError('File does not exist: %s' % script_path)
  71. return script_path
  72. def main(args):
  73. try:
  74. tool = FindClangFormatToolInChromiumTree()
  75. except NotFoundError as e:
  76. sys.stderr.write("%s\n" % str(e))
  77. return 1
  78. # Add some visibility to --help showing where the tool lives, since this
  79. # redirection can be a little opaque.
  80. help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list')
  81. if any(match in args for match in help_syntax):
  82. print('\nDepot tools redirects you to the clang-format at:\n %s\n' %
  83. tool)
  84. return subprocess.call([tool] + args)
  85. if __name__ == '__main__':
  86. try:
  87. sys.exit(main(sys.argv[1:]))
  88. except KeyboardInterrupt:
  89. sys.stderr.write('interrupted\n')
  90. sys.exit(1)