blob: b7b60a8f74c061e2e5a5ad2a51146a83fc919847 [file] [log] [blame]
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +00001#!/usr/bin/env python
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
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."""
Nico Weber09e0b382019-03-11 16:54:07 +000030 bin_path = gclient_paths.GetBuildtoolsPlatformBinaryPath()
jochen@chromium.orgaaee92f2014-07-02 07:35:31 +000031 if not bin_path:
32 raise NotFoundError(
Sam Clegg54985b62016-12-14 15:03:42 -080033 'Could not find checkout in any parent of the current path.\n'
34 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium checkout.')
jochen@chromium.orgaaee92f2014-07-02 07:35:31 +000035
36 tool_path = os.path.join(bin_path,
Nico Weber09e0b382019-03-11 16:54:07 +000037 'clang-format' + gclient_paths.GetExeSuffix())
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000038 if not os.path.exists(tool_path):
nick@chromium.org8ca1aa32014-02-25 23:57:03 +000039 raise NotFoundError('File does not exist: %s' % tool_path)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000040 return tool_path
41
42
43def FindClangFormatScriptInChromiumTree(script_name):
44 """Return a path to a clang-format helper script, or die trying."""
Nico Weber09e0b382019-03-11 16:54:07 +000045 tools_path = gclient_paths.GetBuildtoolsPath()
jochen@chromium.orgaaee92f2014-07-02 07:35:31 +000046 if not tools_path:
47 raise NotFoundError(
Sam Clegg54985b62016-12-14 15:03:42 -080048 'Could not find checkout in any parent of the current path.\n',
49 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium checkout.')
50
jochen@chromium.orgaaee92f2014-07-02 07:35:31 +000051 script_path = os.path.join(tools_path, 'clang_format', 'script', script_name)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000052 if not os.path.exists(script_path):
nick@chromium.org8ca1aa32014-02-25 23:57:03 +000053 raise NotFoundError('File does not exist: %s' % script_path)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000054 return script_path
55
56
57def main(args):
58 try:
59 tool = FindClangFormatToolInChromiumTree()
Sam Clegg54985b62016-12-14 15:03:42 -080060 except NotFoundError as e:
61 sys.stderr.write("%s\n" % str(e))
62 return 1
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000063
64 # Add some visibility to --help showing where the tool lives, since this
65 # redirection can be a little opaque.
66 help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list')
67 if any(match in args for match in help_syntax):
Raul Tambre80ee78e2019-05-06 22:41:05 +000068 print(
69 '\nDepot tools redirects you to the clang-format at:\n %s\n' % tool)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000070
sbc@chromium.org013731e2015-02-26 18:28:43 +000071 return subprocess.call([tool] + args)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000072
73
74if __name__ == '__main__':
sbc@chromium.org013731e2015-02-26 18:28:43 +000075 try:
tfarina@chromium.orgc432a7f2015-02-28 19:20:22 +000076 sys.exit(main(sys.argv[1:]))
sbc@chromium.org013731e2015-02-26 18:28:43 +000077 except KeyboardInterrupt:
78 sys.stderr.write('interrupted\n')
79 sys.exit(1)