xref: /aosp_15_r20/external/bazelbuild-rules_rust/cargo/bootstrap/bootstrap_installer.rs (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan //! A tool for installing bootstrapped Rust binaries into the requested paths.
2*d4726bddSHONG Yifan 
3*d4726bddSHONG Yifan use std::{
4*d4726bddSHONG Yifan     env,
5*d4726bddSHONG Yifan     fs::{copy, create_dir_all},
6*d4726bddSHONG Yifan     path::PathBuf,
7*d4726bddSHONG Yifan };
8*d4726bddSHONG Yifan 
install() -> std::io::Result<u64>9*d4726bddSHONG Yifan fn install() -> std::io::Result<u64> {
10*d4726bddSHONG Yifan     let binary = PathBuf::from(env!("RULES_RUST_CARGO_BOOTSTRAP_BINARY"));
11*d4726bddSHONG Yifan 
12*d4726bddSHONG Yifan     // Consume only the first argument as the destination
13*d4726bddSHONG Yifan     let dest = PathBuf::from(
14*d4726bddSHONG Yifan         env::args()
15*d4726bddSHONG Yifan             .nth(1)
16*d4726bddSHONG Yifan             .expect("No destination argument provided"),
17*d4726bddSHONG Yifan     );
18*d4726bddSHONG Yifan 
19*d4726bddSHONG Yifan     // Create the parent directory structure if it doesn't exist
20*d4726bddSHONG Yifan     if let Some(parent) = dest.parent() {
21*d4726bddSHONG Yifan         if !parent.exists() {
22*d4726bddSHONG Yifan             create_dir_all(parent)?;
23*d4726bddSHONG Yifan         }
24*d4726bddSHONG Yifan     }
25*d4726bddSHONG Yifan 
26*d4726bddSHONG Yifan     // Copy the file to the requested destination
27*d4726bddSHONG Yifan     copy(binary, dest)
28*d4726bddSHONG Yifan }
29*d4726bddSHONG Yifan 
main()30*d4726bddSHONG Yifan fn main() {
31*d4726bddSHONG Yifan     if let Err(err) = install() {
32*d4726bddSHONG Yifan         eprintln!("{err:?}");
33*d4726bddSHONG Yifan         std::process::exit(1);
34*d4726bddSHONG Yifan     };
35*d4726bddSHONG Yifan }
36