danakj | 9535a9c0 | 2024-01-12 20:14:59 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2024 The Chromium Authors |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | '''Run cargo from the chromium Rust toolchain. |
| 6 | |
| 7 | Arguments are passed through to cargo. |
| 8 | |
| 9 | Should be run from the checkout root (i.e. as `tools/crates/run_cargo.py ...`) |
| 10 | ''' |
| 11 | |
| 12 | import argparse |
| 13 | import os |
| 14 | import platform |
| 15 | import subprocess |
| 16 | import sys |
| 17 | |
| 18 | DEFAULT_SYSROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', |
| 19 | '..', 'third_party', 'rust-toolchain') |
| 20 | |
| 21 | |
| 22 | def RunCargo(rust_sysroot, home_dir, cargo_args): |
| 23 | if not os.path.exists(rust_sysroot): |
| 24 | print(f'WARNING: Rust sysroot missing at "{rust_sysroot}"') |
| 25 | |
| 26 | abs_rust_sysroot = os.path.abspath(rust_sysroot) |
| 27 | bin_dir = os.path.join(abs_rust_sysroot, 'bin') |
| 28 | |
| 29 | cargo_env = dict(os.environ) |
| 30 | if home_dir: |
| 31 | cargo_env['CARGO_HOME'] = home_dir |
| 32 | cargo_env['PATH'] = (f'{bin_dir}{os.pathsep}{cargo_env["PATH"]}' |
| 33 | if cargo_env["PATH"] else f'{bin_dir}') |
| 34 | |
| 35 | return subprocess.run([ |
| 36 | 'cargo', |
| 37 | ] + cargo_args, env=cargo_env).returncode |
| 38 | |
| 39 | |
| 40 | def main(): |
| 41 | parser = argparse.ArgumentParser(description='run cargo') |
| 42 | parser.add_argument('--rust-sysroot', |
| 43 | default=DEFAULT_SYSROOT, |
| 44 | help='use cargo and rustc from here') |
| 45 | (args, cargo_args) = parser.parse_known_args() |
| 46 | |
| 47 | if sys.platform == 'darwin' and platform.machine() == 'arm64': |
| 48 | if args.rust_sysroot == 'third_party/rust-toolchain': |
| 49 | args.rust_sysroot = os.path.expanduser( |
| 50 | '~/.rustup/toolchains/nightly-aarch64-apple-darwin') |
| 51 | print('No "cargo" provided in the Chromium toolchain on Mac-ARM. ' |
| 52 | 'Install cargo nightly to ~/.rustup or use --rust-sysroot:') |
| 53 | print("== To install: `rustup install nightly`") |
| 54 | |
| 55 | return RunCargo(args.rust_sysroot, None, cargo_args) |
| 56 | |
| 57 | |
| 58 | if __name__ == '__main__': |
| 59 | sys.exit(main()) |