nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 1 | #!/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 | |
| 8 | clang-format binaries are pulled down from Google Cloud Storage whenever you |
| 9 | sync Chrome, to platform-specific locations. This script knows how to locate |
| 10 | those tools, assuming the script is invoked from inside a Chromium checkout.""" |
| 11 | |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 12 | import gclient_paths |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 13 | import os |
| 14 | import subprocess |
| 15 | import sys |
| 16 | |
| 17 | |
| 18 | class 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' |
Sam Clegg | 54985b6 | 2016-12-14 15:03:42 -0800 | [diff] [blame] | 23 | '%s' % e) |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 24 | |
| 25 | |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 26 | def FindClangFormatToolInChromiumTree(): |
| 27 | """Return a path to the clang-format executable, or die trying.""" |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 28 | bin_path = gclient_paths.GetBuildtoolsPlatformBinaryPath() |
jochen@chromium.org | aaee92f | 2014-07-02 07:35:31 +0000 | [diff] [blame] | 29 | if not bin_path: |
| 30 | raise NotFoundError( |
Sam Clegg | 54985b6 | 2016-12-14 15:03:42 -0800 | [diff] [blame] | 31 | 'Could not find checkout in any parent of the current path.\n' |
| 32 | 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium checkout.') |
jochen@chromium.org | aaee92f | 2014-07-02 07:35:31 +0000 | [diff] [blame] | 33 | |
| 34 | tool_path = os.path.join(bin_path, |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 35 | 'clang-format' + gclient_paths.GetExeSuffix()) |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 36 | if not os.path.exists(tool_path): |
nick@chromium.org | 8ca1aa3 | 2014-02-25 23:57:03 +0000 | [diff] [blame] | 37 | raise NotFoundError('File does not exist: %s' % tool_path) |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 38 | return tool_path |
| 39 | |
| 40 | |
| 41 | def FindClangFormatScriptInChromiumTree(script_name): |
| 42 | """Return a path to a clang-format helper script, or die trying.""" |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 43 | tools_path = gclient_paths.GetBuildtoolsPath() |
jochen@chromium.org | aaee92f | 2014-07-02 07:35:31 +0000 | [diff] [blame] | 44 | if not tools_path: |
| 45 | raise NotFoundError( |
Sam Clegg | 54985b6 | 2016-12-14 15:03:42 -0800 | [diff] [blame] | 46 | 'Could not find checkout in any parent of the current path.\n', |
| 47 | 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium checkout.') |
| 48 | |
jochen@chromium.org | aaee92f | 2014-07-02 07:35:31 +0000 | [diff] [blame] | 49 | script_path = os.path.join(tools_path, 'clang_format', 'script', script_name) |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 50 | if not os.path.exists(script_path): |
nick@chromium.org | 8ca1aa3 | 2014-02-25 23:57:03 +0000 | [diff] [blame] | 51 | raise NotFoundError('File does not exist: %s' % script_path) |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 52 | return script_path |
| 53 | |
| 54 | |
| 55 | def main(args): |
| 56 | try: |
| 57 | tool = FindClangFormatToolInChromiumTree() |
Sam Clegg | 54985b6 | 2016-12-14 15:03:42 -0800 | [diff] [blame] | 58 | except NotFoundError as e: |
| 59 | sys.stderr.write("%s\n" % str(e)) |
| 60 | return 1 |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 61 | |
| 62 | # Add some visibility to --help showing where the tool lives, since this |
| 63 | # redirection can be a little opaque. |
| 64 | help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list') |
| 65 | if any(match in args for match in help_syntax): |
| 66 | print '\nDepot tools redirects you to the clang-format at:\n %s\n' % tool |
| 67 | |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 68 | return subprocess.call([tool] + args) |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 69 | |
| 70 | |
| 71 | if __name__ == '__main__': |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 72 | try: |
tfarina@chromium.org | c432a7f | 2015-02-28 19:20:22 +0000 | [diff] [blame] | 73 | sys.exit(main(sys.argv[1:])) |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 74 | except KeyboardInterrupt: |
| 75 | sys.stderr.write('interrupted\n') |
| 76 | sys.exit(1) |