blob: bfe3b4b47e1ef5d0d282b4b2e68b12fda8f9e4e7 [file] [log] [blame]
Josip Sokcevic39120912021-07-20 18:35:42 +00001#!/usr/bin/env python3
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +00002# 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.
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +00005"""Redirects to the version of clang-format checked into the Chrome tree.
6
7clang-format binaries are pulled down from Google Cloud Storage whenever you
8sync Chrome, to platform-specific locations. This script knows how to locate
9those tools, assuming the script is invoked from inside a Chromium checkout."""
10
Raul Tambre80ee78e2019-05-06 22:41:05 +000011from __future__ import print_function
12
Nico Weber09e0b382019-03-11 16:54:07 +000013import gclient_paths
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000014import os
15import subprocess
16import sys
17
18
19class NotFoundError(Exception):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000020 """A file could not be found."""
21 def __init__(self, e):
22 Exception.__init__(
23 self,
24 'Problem while looking for clang-format in Chromium source tree:\n'
25 '%s' % e)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000026
27
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000028def FindClangFormatToolInChromiumTree():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000029 """Return a path to the clang-format executable, or die trying."""
30 primary_solution_path = gclient_paths.GetPrimarySolutionPath()
31 if primary_solution_path:
32 bin_path = os.path.join(primary_solution_path, 'third_party',
33 'clang-format',
34 'clang-format' + gclient_paths.GetExeSuffix())
35 if os.path.exists(bin_path):
36 return bin_path
Primiano Tuccia964ca12022-12-09 15:41:59 +000037
Mike Frysinger124bb8e2023-09-06 05:48:55 +000038 bin_path = gclient_paths.GetBuildtoolsPlatformBinaryPath()
39 if not bin_path:
40 raise NotFoundError(
41 'Could not find checkout in any parent of the current path.\n'
42 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium '
43 'checkout.')
jochen@chromium.orgaaee92f2014-07-02 07:35:31 +000044
Mike Frysinger124bb8e2023-09-06 05:48:55 +000045 tool_path = os.path.join(bin_path,
46 'clang-format' + gclient_paths.GetExeSuffix())
47 if not os.path.exists(tool_path):
48 raise NotFoundError('File does not exist: %s' % tool_path)
49 return tool_path
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000050
51
52def FindClangFormatScriptInChromiumTree(script_name):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000053 """Return a path to a clang-format helper script, or die trying."""
54 primary_solution_path = gclient_paths.GetPrimarySolutionPath()
55 if primary_solution_path:
56 script_path = os.path.join(primary_solution_path, 'third_party',
57 'clang-format', 'script', script_name)
58 if os.path.exists(script_path):
59 return script_path
Primiano Tuccia964ca12022-12-09 15:41:59 +000060
Mike Frysinger124bb8e2023-09-06 05:48:55 +000061 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.')
Sam Clegg54985b62016-12-14 15:03:42 -080067
Mike Frysinger124bb8e2023-09-06 05:48:55 +000068 script_path = os.path.join(tools_path, 'clang_format', 'script',
69 script_name)
70 if not os.path.exists(script_path):
71 raise NotFoundError('File does not exist: %s' % script_path)
72 return script_path
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000073
74
75def main(args):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000076 try:
77 tool = FindClangFormatToolInChromiumTree()
78 except NotFoundError as e:
79 sys.stderr.write("%s\n" % str(e))
80 return 1
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000081
Mike Frysinger124bb8e2023-09-06 05:48:55 +000082 # Add some visibility to --help showing where the tool lives, since this
83 # redirection can be a little opaque.
84 help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list')
85 if any(match in args for match in help_syntax):
86 print('\nDepot tools redirects you to the clang-format at:\n %s\n' %
87 tool)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000088
Mike Frysinger124bb8e2023-09-06 05:48:55 +000089 return subprocess.call([tool] + args)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000090
91
92if __name__ == '__main__':
Mike Frysinger124bb8e2023-09-06 05:48:55 +000093 try:
94 sys.exit(main(sys.argv[1:]))
95 except KeyboardInterrupt:
96 sys.stderr.write('interrupted\n')
97 sys.exit(1)