blob: 65246aef57c543126748f1db530522eb5fa2eed6 [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):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000018 """A file could not be found."""
19 def __init__(self, e):
20 Exception.__init__(
21 self, 'Problem while looking for rustfmt in Chromium source tree:\n'
22 '%s' % e)
Lukasz Anforowiczb4d39542021-09-30 23:39:25 +000023
24
25def FindRustfmtToolInChromiumTree():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000026 """Return a path to the rustfmt executable, or die trying."""
27 chromium_src_path = gclient_paths.GetPrimarySolutionPath()
28 if not chromium_src_path:
29 raise NotFoundError(
30 'Could not find checkout in any parent of the current path.\n'
31 'Set CHROMIUM_BUILDTOOLS_PATH to use outside of a chromium '
32 'checkout.')
Lukasz Anforowiczb4d39542021-09-30 23:39:25 +000033
Mike Frysinger124bb8e2023-09-06 05:48:55 +000034 tool_path = os.path.join(chromium_src_path, 'third_party', 'rust-toolchain',
35 'bin', 'rustfmt' + gclient_paths.GetExeSuffix())
36 if not os.path.exists(tool_path):
37 raise NotFoundError('File does not exist: %s' % tool_path)
38 return tool_path
Lukasz Anforowiczb4d39542021-09-30 23:39:25 +000039
40
41def IsRustfmtSupported():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000042 try:
43 FindRustfmtToolInChromiumTree()
44 return True
45 except NotFoundError:
46 return False
Lukasz Anforowiczb4d39542021-09-30 23:39:25 +000047
48
49def main(args):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000050 try:
51 tool = FindRustfmtToolInChromiumTree()
52 except NotFoundError as e:
53 sys.stderr.write("%s\n" % str(e))
54 return 1
Lukasz Anforowiczb4d39542021-09-30 23:39:25 +000055
Mike Frysinger124bb8e2023-09-06 05:48:55 +000056 # 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)
Lukasz Anforowiczb4d39542021-09-30 23:39:25 +000061
Mike Frysinger124bb8e2023-09-06 05:48:55 +000062 return subprocess.call([tool] + args)
Lukasz Anforowiczb4d39542021-09-30 23:39:25 +000063
64
65if __name__ == '__main__':
Mike Frysinger124bb8e2023-09-06 05:48:55 +000066 try:
67 sys.exit(main(sys.argv[1:]))
68 except KeyboardInterrupt:
69 sys.stderr.write('interrupted\n')
70 sys.exit(1)