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 | |
Lukasz Anforowicz | 8646804 | 2024-03-20 16:44:16 | [diff] [blame] | 18 | sys.path.append( |
| 19 | os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'clang', |
| 20 | 'scripts')) |
| 21 | from build import (RunCommand) |
| 22 | |
danakj | 9535a9c0 | 2024-01-12 20:14:59 | [diff] [blame] | 23 | DEFAULT_SYSROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', |
| 24 | '..', 'third_party', 'rust-toolchain') |
| 25 | |
| 26 | |
| 27 | def RunCargo(rust_sysroot, home_dir, cargo_args): |
| 28 | if not os.path.exists(rust_sysroot): |
| 29 | print(f'WARNING: Rust sysroot missing at "{rust_sysroot}"') |
| 30 | |
| 31 | abs_rust_sysroot = os.path.abspath(rust_sysroot) |
| 32 | bin_dir = os.path.join(abs_rust_sysroot, 'bin') |
| 33 | |
| 34 | cargo_env = dict(os.environ) |
| 35 | if home_dir: |
| 36 | cargo_env['CARGO_HOME'] = home_dir |
| 37 | cargo_env['PATH'] = (f'{bin_dir}{os.pathsep}{cargo_env["PATH"]}' |
| 38 | if cargo_env["PATH"] else f'{bin_dir}') |
| 39 | |
Lukasz Anforowicz | 3f6638cc | 2024-08-27 17:17:48 | [diff] [blame] | 40 | return RunCommand(['cargo'] + cargo_args, env=cargo_env, fail_hard=False) |
danakj | 9535a9c0 | 2024-01-12 20:14:59 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def main(): |
| 44 | parser = argparse.ArgumentParser(description='run cargo') |
| 45 | parser.add_argument('--rust-sysroot', |
| 46 | default=DEFAULT_SYSROOT, |
| 47 | help='use cargo and rustc from here') |
| 48 | (args, cargo_args) = parser.parse_known_args() |
Lukasz Anforowicz | 8646804 | 2024-03-20 16:44:16 | [diff] [blame] | 49 | success = RunCargo(args.rust_sysroot, None, cargo_args) |
| 50 | return 0 if success else 1 |
danakj | 9535a9c0 | 2024-01-12 20:14:59 | [diff] [blame] | 51 | |
| 52 | |
| 53 | if __name__ == '__main__': |
| 54 | sys.exit(main()) |