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