blob: aae33a12d8b18783ef043511e915fe0369e8322a [file] [log] [blame]
Lukasz Anforowiczb4d39542021-09-30 23:39:25 +00001#!/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
7Rust binaries are pulled down from Google Cloud Storage whenever you sync
8Chrome. This script knows how to locate those tools, assuming the script is
9invoked from inside a Chromium checkout."""
10
11import gclient_paths
12import os
13import subprocess
14import sys
15
16
17class 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
26def 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 Khoroshilov6e33ba02022-12-06 18:46:29 +000034 tool_path = os.path.join(chromium_src_path, 'third_party', 'rust-toolchain',
35 'bin', 'rustfmt' + gclient_paths.GetExeSuffix())
Lukasz Anforowiczb4d39542021-09-30 23:39:25 +000036 if not os.path.exists(tool_path):
37 raise NotFoundError('File does not exist: %s' % tool_path)
38 return tool_path
39
40
41def IsRustfmtSupported():
42 try:
43 FindRustfmtToolInChromiumTree()
44 return True
45 except NotFoundError:
46 return False
47
48
49def 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
65if __name__ == '__main__':
66 try:
67 sys.exit(main(sys.argv[1:]))
68 except KeyboardInterrupt:
69 sys.stderr.write('interrupted\n')
70 sys.exit(1)