blob: 4e7b1baadd4274756875330fb8048a28c7556f85 [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.
5
6"""Redirects to the version of clang-format checked into the Chrome tree.
7
8clang-format binaries are pulled down from Google Cloud Storage whenever you
9sync Chrome, to platform-specific locations. This script knows how to locate
10those tools, assuming the script is invoked from inside a Chromium checkout."""
11
Raul Tambre80ee78e2019-05-06 22:41:05 +000012from __future__ import print_function
13
Nico Weber09e0b382019-03-11 16:54:07 +000014import gclient_paths
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000015import os
16import subprocess
17import sys
18
19
20class NotFoundError(Exception):
21 """A file could not be found."""
22 def __init__(self, e):
23 Exception.__init__(self,
24 'Problem while looking for clang-format in Chromium source tree:\n'
Sam Clegg54985b62016-12-14 15:03:42 -080025 '%s' % e)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000026
27
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000028def FindClangFormatToolInChromiumTree():
29 """Return a path to the clang-format executable, or die trying."""
Primiano Tuccia964ca12022-12-09 15:41:59 +000030 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
37
Nico Weber09e0b382019-03-11 16:54:07 +000038 bin_path = gclient_paths.GetBuildtoolsPlatformBinaryPath()
jochen@chromium.orgaaee92f2014-07-02 07:35:31 +000039 if not bin_path:
40 raise NotFoundError(
Sam Clegg54985b62016-12-14 15:03:42 -080041 'Could not find checkout in any parent of the current path.\n'
42 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium checkout.')
jochen@chromium.orgaaee92f2014-07-02 07:35:31 +000043
44 tool_path = os.path.join(bin_path,
Nico Weber09e0b382019-03-11 16:54:07 +000045 'clang-format' + gclient_paths.GetExeSuffix())
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000046 if not os.path.exists(tool_path):
nick@chromium.org8ca1aa32014-02-25 23:57:03 +000047 raise NotFoundError('File does not exist: %s' % tool_path)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000048 return tool_path
49
50
51def FindClangFormatScriptInChromiumTree(script_name):
52 """Return a path to a clang-format helper script, or die trying."""
Primiano Tuccia964ca12022-12-09 15:41:59 +000053 primary_solution_path = gclient_paths.GetPrimarySolutionPath()
54 if primary_solution_path:
55 script_path = os.path.join(primary_solution_path, 'third_party',
56 'clang-format', 'script', script_name)
57 if os.path.exists(script_path):
58 return script_path
59
Nico Weber09e0b382019-03-11 16:54:07 +000060 tools_path = gclient_paths.GetBuildtoolsPath()
jochen@chromium.orgaaee92f2014-07-02 07:35:31 +000061 if not tools_path:
62 raise NotFoundError(
Sam Clegg54985b62016-12-14 15:03:42 -080063 'Could not find checkout in any parent of the current path.\n',
64 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium checkout.')
65
jochen@chromium.orgaaee92f2014-07-02 07:35:31 +000066 script_path = os.path.join(tools_path, 'clang_format', 'script', script_name)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000067 if not os.path.exists(script_path):
nick@chromium.org8ca1aa32014-02-25 23:57:03 +000068 raise NotFoundError('File does not exist: %s' % script_path)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000069 return script_path
70
71
72def main(args):
73 try:
74 tool = FindClangFormatToolInChromiumTree()
Sam Clegg54985b62016-12-14 15:03:42 -080075 except NotFoundError as e:
76 sys.stderr.write("%s\n" % str(e))
77 return 1
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000078
79 # Add some visibility to --help showing where the tool lives, since this
80 # redirection can be a little opaque.
81 help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list')
82 if any(match in args for match in help_syntax):
Raul Tambre80ee78e2019-05-06 22:41:05 +000083 print(
84 '\nDepot tools redirects you to the clang-format at:\n %s\n' % tool)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000085
sbc@chromium.org013731e2015-02-26 18:28:43 +000086 return subprocess.call([tool] + args)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000087
88
89if __name__ == '__main__':
sbc@chromium.org013731e2015-02-26 18:28:43 +000090 try:
tfarina@chromium.orgc432a7f2015-02-28 19:20:22 +000091 sys.exit(main(sys.argv[1:]))
sbc@chromium.org013731e2015-02-26 18:28:43 +000092 except KeyboardInterrupt:
93 sys.stderr.write('interrupted\n')
94 sys.exit(1)