blob: c32995e4246e2a62425018b26c7c4d23574aa7dd [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
Nico Weber09e0b382019-03-11 16:54:07 +000011import gclient_paths
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000012import os
13import subprocess
14import sys
15
16
17class NotFoundError(Exception):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000018 """A file could not be found."""
19 def __init__(self, e):
20 Exception.__init__(
21 self,
22 'Problem while looking for clang-format in Chromium source tree:\n'
23 '%s' % e)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000024
25
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000026def FindClangFormatToolInChromiumTree():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000027 """Return a path to the clang-format executable, or die trying."""
28 primary_solution_path = gclient_paths.GetPrimarySolutionPath()
29 if primary_solution_path:
30 bin_path = os.path.join(primary_solution_path, 'third_party',
31 'clang-format',
32 'clang-format' + gclient_paths.GetExeSuffix())
33 if os.path.exists(bin_path):
34 return bin_path
Primiano Tuccia964ca12022-12-09 15:41:59 +000035
Mike Frysinger124bb8e2023-09-06 05:48:55 +000036 bin_path = gclient_paths.GetBuildtoolsPlatformBinaryPath()
37 if not bin_path:
38 raise NotFoundError(
39 'Could not find checkout in any parent of the current path.\n'
40 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium '
41 'checkout.')
jochen@chromium.orgaaee92f2014-07-02 07:35:31 +000042
Mike Frysinger124bb8e2023-09-06 05:48:55 +000043 tool_path = os.path.join(bin_path,
44 'clang-format' + gclient_paths.GetExeSuffix())
45 if not os.path.exists(tool_path):
46 raise NotFoundError('File does not exist: %s' % tool_path)
47 return tool_path
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000048
49
50def FindClangFormatScriptInChromiumTree(script_name):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000051 """Return a path to a clang-format helper script, or die trying."""
52 primary_solution_path = gclient_paths.GetPrimarySolutionPath()
53 if primary_solution_path:
54 script_path = os.path.join(primary_solution_path, 'third_party',
55 'clang-format', 'script', script_name)
56 if os.path.exists(script_path):
57 return script_path
Primiano Tuccia964ca12022-12-09 15:41:59 +000058
Mike Frysinger124bb8e2023-09-06 05:48:55 +000059 tools_path = gclient_paths.GetBuildtoolsPath()
60 if not tools_path:
61 raise NotFoundError(
62 'Could not find checkout in any parent of the current path.\n',
63 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium '
64 'checkout.')
Sam Clegg54985b62016-12-14 15:03:42 -080065
Mike Frysinger124bb8e2023-09-06 05:48:55 +000066 script_path = os.path.join(tools_path, 'clang_format', 'script',
67 script_name)
68 if not os.path.exists(script_path):
69 raise NotFoundError('File does not exist: %s' % script_path)
70 return script_path
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000071
72
73def main(args):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000074 try:
75 tool = FindClangFormatToolInChromiumTree()
76 except NotFoundError as e:
77 sys.stderr.write("%s\n" % str(e))
78 return 1
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000079
Mike Frysinger124bb8e2023-09-06 05:48:55 +000080 # Add some visibility to --help showing where the tool lives, since this
81 # redirection can be a little opaque.
82 help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list')
83 if any(match in args for match in help_syntax):
84 print('\nDepot tools redirects you to the clang-format at:\n %s\n' %
85 tool)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000086
Mike Frysinger124bb8e2023-09-06 05:48:55 +000087 return subprocess.call([tool] + args)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000088
89
90if __name__ == '__main__':
Mike Frysinger124bb8e2023-09-06 05:48:55 +000091 try:
92 sys.exit(main(sys.argv[1:]))
93 except KeyboardInterrupt:
94 sys.stderr.write('interrupted\n')
95 sys.exit(1)