Josip Sokcevic | 3912091 | 2021-07-20 18:35:42 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 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 | |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 12 | from __future__ import print_function |
| 13 | |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 14 | import gclient_paths |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 15 | import os |
| 16 | import subprocess |
| 17 | import sys |
| 18 | |
| 19 | |
| 20 | class 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 Clegg | 54985b6 | 2016-12-14 15:03:42 -0800 | [diff] [blame] | 25 | '%s' % e) |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 26 | |
| 27 | |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 28 | def FindClangFormatToolInChromiumTree(): |
| 29 | """Return a path to the clang-format executable, or die trying.""" |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 30 | bin_path = gclient_paths.GetBuildtoolsPlatformBinaryPath() |
jochen@chromium.org | aaee92f | 2014-07-02 07:35:31 +0000 | [diff] [blame] | 31 | if not bin_path: |
| 32 | raise NotFoundError( |
Sam Clegg | 54985b6 | 2016-12-14 15:03:42 -0800 | [diff] [blame] | 33 | '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.org | aaee92f | 2014-07-02 07:35:31 +0000 | [diff] [blame] | 35 | |
| 36 | tool_path = os.path.join(bin_path, |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 37 | 'clang-format' + gclient_paths.GetExeSuffix()) |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 38 | if not os.path.exists(tool_path): |
nick@chromium.org | 8ca1aa3 | 2014-02-25 23:57:03 +0000 | [diff] [blame] | 39 | raise NotFoundError('File does not exist: %s' % tool_path) |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 40 | return tool_path |
| 41 | |
| 42 | |
| 43 | def FindClangFormatScriptInChromiumTree(script_name): |
| 44 | """Return a path to a clang-format helper script, or die trying.""" |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 45 | tools_path = gclient_paths.GetBuildtoolsPath() |
jochen@chromium.org | aaee92f | 2014-07-02 07:35:31 +0000 | [diff] [blame] | 46 | if not tools_path: |
| 47 | raise NotFoundError( |
Sam Clegg | 54985b6 | 2016-12-14 15:03:42 -0800 | [diff] [blame] | 48 | '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.org | aaee92f | 2014-07-02 07:35:31 +0000 | [diff] [blame] | 51 | 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] | 52 | if not os.path.exists(script_path): |
nick@chromium.org | 8ca1aa3 | 2014-02-25 23:57:03 +0000 | [diff] [blame] | 53 | raise NotFoundError('File does not exist: %s' % script_path) |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 54 | return script_path |
| 55 | |
| 56 | |
| 57 | def main(args): |
| 58 | try: |
| 59 | tool = FindClangFormatToolInChromiumTree() |
Sam Clegg | 54985b6 | 2016-12-14 15:03:42 -0800 | [diff] [blame] | 60 | except NotFoundError as e: |
| 61 | sys.stderr.write("%s\n" % str(e)) |
| 62 | return 1 |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 63 | |
| 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 Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 68 | print( |
| 69 | '\nDepot tools redirects you to the clang-format at:\n %s\n' % tool) |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 70 | |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 71 | return subprocess.call([tool] + args) |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 72 | |
| 73 | |
| 74 | if __name__ == '__main__': |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 75 | try: |
tfarina@chromium.org | c432a7f | 2015-02-28 19:20:22 +0000 | [diff] [blame] | 76 | sys.exit(main(sys.argv[1:])) |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 77 | except KeyboardInterrupt: |
| 78 | sys.stderr.write('interrupted\n') |
| 79 | sys.exit(1) |