Lukasz Anforowicz | b4d3954 | 2021-09-30 23:39:25 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2021 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 | """Redirects to the version of rustfmt present in the Chrome tree. |
| 6 | |
| 7 | Rust binaries are pulled down from Google Cloud Storage whenever you sync |
| 8 | Chrome. This script knows how to locate those tools, assuming the script is |
| 9 | invoked from inside a Chromium checkout.""" |
| 10 | |
| 11 | import gclient_paths |
| 12 | import os |
| 13 | import subprocess |
| 14 | import sys |
| 15 | |
| 16 | |
| 17 | class NotFoundError(Exception): |
| 18 | """A file could not be found.""" |
| 19 | |
| 20 | def __init__(self, e): |
| 21 | Exception.__init__( |
| 22 | self, 'Problem while looking for rustfmt in Chromium source tree:\n' |
| 23 | '%s' % e) |
| 24 | |
| 25 | |
| 26 | def FindRustfmtToolInChromiumTree(): |
| 27 | """Return a path to the rustfmt executable, or die trying.""" |
| 28 | chromium_src_path = gclient_paths.GetPrimarySolutionPath() |
| 29 | if not chromium_src_path: |
| 30 | raise NotFoundError( |
| 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.') |
| 33 | |
Aleksey Khoroshilov | 6e33ba0 | 2022-12-06 18:46:29 +0000 | [diff] [blame] | 34 | tool_path = os.path.join(chromium_src_path, 'third_party', 'rust-toolchain', |
| 35 | 'bin', 'rustfmt' + gclient_paths.GetExeSuffix()) |
Lukasz Anforowicz | b4d3954 | 2021-09-30 23:39:25 +0000 | [diff] [blame] | 36 | if not os.path.exists(tool_path): |
| 37 | raise NotFoundError('File does not exist: %s' % tool_path) |
| 38 | return tool_path |
| 39 | |
| 40 | |
| 41 | def IsRustfmtSupported(): |
| 42 | try: |
| 43 | FindRustfmtToolInChromiumTree() |
| 44 | return True |
| 45 | except NotFoundError: |
| 46 | return False |
| 47 | |
| 48 | |
| 49 | def main(args): |
| 50 | try: |
| 51 | tool = FindRustfmtToolInChromiumTree() |
| 52 | except NotFoundError as e: |
| 53 | sys.stderr.write("%s\n" % str(e)) |
| 54 | return 1 |
| 55 | |
| 56 | # Add some visibility to --help showing where the tool lives, since this |
| 57 | # redirection can be a little opaque. |
| 58 | help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list') |
| 59 | if any(match in args for match in help_syntax): |
| 60 | print('\nDepot tools redirects you to the rustfmt at:\n %s\n' % tool) |
| 61 | |
| 62 | return subprocess.call([tool] + args) |
| 63 | |
| 64 | |
| 65 | if __name__ == '__main__': |
| 66 | try: |
| 67 | sys.exit(main(sys.argv[1:])) |
| 68 | except KeyboardInterrupt: |
| 69 | sys.stderr.write('interrupted\n') |
| 70 | sys.exit(1) |