blob: 4da2cc9079b8b6380439581e540317c47840570d [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
34 # TODO(lukasza): Deduplicate the is-Rust-supported and find-Rust-binaries code
35 # by somehow sharing the `rust_prefix` variable from //build/config/rust.gni
36 tool_path = os.path.join(chromium_src_path, 'third_party',
37 'android_rust_toolchain', 'toolchain', 'bin',
38 'rustfmt' + gclient_paths.GetExeSuffix())
39 if not os.path.exists(tool_path):
40 raise NotFoundError('File does not exist: %s' % tool_path)
41 return tool_path
42
43
44def IsRustfmtSupported():
45 try:
46 FindRustfmtToolInChromiumTree()
47 return True
48 except NotFoundError:
49 return False
50
51
52def main(args):
53 try:
54 tool = FindRustfmtToolInChromiumTree()
55 except NotFoundError as e:
56 sys.stderr.write("%s\n" % str(e))
57 return 1
58
59 # Add some visibility to --help showing where the tool lives, since this
60 # redirection can be a little opaque.
61 help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list')
62 if any(match in args for match in help_syntax):
63 print('\nDepot tools redirects you to the rustfmt at:\n %s\n' % tool)
64
65 return subprocess.call([tool] + args)
66
67
68if __name__ == '__main__':
69 try:
70 sys.exit(main(sys.argv[1:]))
71 except KeyboardInterrupt:
72 sys.stderr.write('interrupted\n')
73 sys.exit(1)