blob: 8320e6d2a0dc449d4e07cd82dcf3ab11b19c01c5 [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
12import gclient_utils
13import os
14import subprocess
15import sys
16
17
18class NotFoundError(Exception):
19 """A file could not be found."""
20 def __init__(self, e):
21 Exception.__init__(self,
22 'Problem while looking for clang-format in Chromium source tree:\n'
23 ' %s' % e)
24
25
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000026def FindClangFormatToolInChromiumTree():
27 """Return a path to the clang-format executable, or die trying."""
jochen@chromium.orgaaee92f2014-07-02 07:35:31 +000028 bin_path = gclient_utils.GetBuildtoolsPlatformBinaryPath()
29 if not bin_path:
30 raise NotFoundError(
31 'Could not find checkout in any parent of the current path.')
32
33 tool_path = os.path.join(bin_path,
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000034 'clang-format' + gclient_utils.GetExeSuffix())
35 if not os.path.exists(tool_path):
nick@chromium.org8ca1aa32014-02-25 23:57:03 +000036 raise NotFoundError('File does not exist: %s' % tool_path)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000037 return tool_path
38
39
40def FindClangFormatScriptInChromiumTree(script_name):
41 """Return a path to a clang-format helper script, or die trying."""
jochen@chromium.orgaaee92f2014-07-02 07:35:31 +000042 tools_path = gclient_utils.GetBuildtoolsPath()
43 if not tools_path:
44 raise NotFoundError(
45 'Could not find checkout in any parent of the current path.')
46 script_path = os.path.join(tools_path, 'clang_format', 'script', script_name)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000047 if not os.path.exists(script_path):
nick@chromium.org8ca1aa32014-02-25 23:57:03 +000048 raise NotFoundError('File does not exist: %s' % script_path)
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000049 return script_path
50
51
52def main(args):
53 try:
54 tool = FindClangFormatToolInChromiumTree()
55 except NotFoundError, e:
56 print >> sys.stderr, e
57 sys.exit(1)
58
59 # Add some visibility to --help showing where the tool lives, since this
60 # redirection can be a little opaque.
61 help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list')
62 if any(match in args for match in help_syntax):
63 print '\nDepot tools redirects you to the clang-format at:\n %s\n' % tool
64
65 return subprocess.call([tool] + sys.argv[1:])
66
67
68if __name__ == '__main__':
69 sys.exit(main(sys.argv))