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. |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 5 | """Redirects to the version of clang-format checked into the Chrome tree. |
| 6 | |
| 7 | clang-format binaries are pulled down from Google Cloud Storage whenever you |
| 8 | sync Chrome, to platform-specific locations. This script knows how to locate |
| 9 | those tools, assuming the script is invoked from inside a Chromium checkout.""" |
| 10 | |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 11 | import gclient_paths |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 12 | import os |
| 13 | import subprocess |
| 14 | import sys |
| 15 | |
| 16 | |
| 17 | class NotFoundError(Exception): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 18 | """A file could not be found.""" |
| 19 | def __init__(self, e): |
| 20 | Exception.__init__( |
| 21 | self, |
| 22 | 'Problem while looking for clang-format in Chromium source tree:\n' |
| 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(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 27 | """Return a path to the clang-format executable, or die trying.""" |
| 28 | primary_solution_path = gclient_paths.GetPrimarySolutionPath() |
| 29 | if primary_solution_path: |
| 30 | bin_path = os.path.join(primary_solution_path, 'third_party', |
| 31 | 'clang-format', |
| 32 | 'clang-format' + gclient_paths.GetExeSuffix()) |
| 33 | if os.path.exists(bin_path): |
| 34 | return bin_path |
Primiano Tucci | a964ca1 | 2022-12-09 15:41:59 +0000 | [diff] [blame] | 35 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 36 | bin_path = gclient_paths.GetBuildtoolsPlatformBinaryPath() |
| 37 | if not bin_path: |
| 38 | raise NotFoundError( |
| 39 | 'Could not find checkout in any parent of the current path.\n' |
| 40 | 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium ' |
| 41 | 'checkout.') |
jochen@chromium.org | aaee92f | 2014-07-02 07:35:31 +0000 | [diff] [blame] | 42 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 43 | tool_path = os.path.join(bin_path, |
| 44 | 'clang-format' + gclient_paths.GetExeSuffix()) |
| 45 | if not os.path.exists(tool_path): |
| 46 | raise NotFoundError('File does not exist: %s' % tool_path) |
| 47 | return tool_path |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | def FindClangFormatScriptInChromiumTree(script_name): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 51 | """Return a path to a clang-format helper script, or die trying.""" |
| 52 | primary_solution_path = gclient_paths.GetPrimarySolutionPath() |
| 53 | if primary_solution_path: |
| 54 | script_path = os.path.join(primary_solution_path, 'third_party', |
| 55 | 'clang-format', 'script', script_name) |
| 56 | if os.path.exists(script_path): |
| 57 | return script_path |
Primiano Tucci | a964ca1 | 2022-12-09 15:41:59 +0000 | [diff] [blame] | 58 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 59 | tools_path = gclient_paths.GetBuildtoolsPath() |
| 60 | if not tools_path: |
| 61 | raise NotFoundError( |
| 62 | 'Could not find checkout in any parent of the current path.\n', |
| 63 | 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium ' |
| 64 | 'checkout.') |
Sam Clegg | 54985b6 | 2016-12-14 15:03:42 -0800 | [diff] [blame] | 65 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 66 | script_path = os.path.join(tools_path, 'clang_format', 'script', |
| 67 | script_name) |
| 68 | if not os.path.exists(script_path): |
| 69 | raise NotFoundError('File does not exist: %s' % script_path) |
| 70 | return script_path |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 71 | |
| 72 | |
| 73 | def main(args): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 74 | try: |
| 75 | tool = FindClangFormatToolInChromiumTree() |
| 76 | except NotFoundError as e: |
| 77 | sys.stderr.write("%s\n" % str(e)) |
| 78 | return 1 |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 79 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 80 | # Add some visibility to --help showing where the tool lives, since this |
| 81 | # redirection can be a little opaque. |
| 82 | help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list') |
| 83 | if any(match in args for match in help_syntax): |
| 84 | print('\nDepot tools redirects you to the clang-format at:\n %s\n' % |
| 85 | tool) |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 86 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 87 | return subprocess.call([tool] + args) |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 88 | |
| 89 | |
| 90 | if __name__ == '__main__': |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 91 | try: |
| 92 | sys.exit(main(sys.argv[1:])) |
| 93 | except KeyboardInterrupt: |
| 94 | sys.stderr.write('interrupted\n') |
| 95 | sys.exit(1) |