|
| 1 | +use crate::config::ConfigInfo; |
| 2 | +use crate::utils::{ |
| 3 | + get_toolchain, run_command_with_output_and_env, rustc_toolchain_version_info, |
| 4 | + rustc_version_info, |
| 5 | +}; |
| 6 | + |
| 7 | +use std::collections::HashMap; |
| 8 | +use std::ffi::OsStr; |
| 9 | + |
| 10 | +fn args() -> Result<Option<Vec<String>>, String> { |
| 11 | + // We skip the binary and the "cargo" option. |
| 12 | + if let Some("--help") = std::env::args().skip(2).next().as_deref() { |
| 13 | + usage(); |
| 14 | + return Ok(None); |
| 15 | + } |
| 16 | + let args = std::env::args().skip(2).collect::<Vec<_>>(); |
| 17 | + if args.is_empty() { |
| 18 | + return Err( |
| 19 | + "Expected at least one argument for `cargo` subcommand, found none".to_string(), |
| 20 | + ); |
| 21 | + } |
| 22 | + Ok(Some(args)) |
| 23 | +} |
| 24 | + |
| 25 | +fn usage() { |
| 26 | + println!( |
| 27 | + r#" |
| 28 | +`cargo` command help: |
| 29 | +
|
| 30 | + [args] : Arguments to be passed to the cargo command |
| 31 | + --help : Show this help |
| 32 | +"# |
| 33 | + ) |
| 34 | +} |
| 35 | + |
| 36 | +pub fn run() -> Result<(), String> { |
| 37 | + let args = match args()? { |
| 38 | + Some(a) => a, |
| 39 | + None => return Ok(()), |
| 40 | + }; |
| 41 | + |
| 42 | + // We first need to go to the original location to ensure that the config setup will go as |
| 43 | + // expected. |
| 44 | + let current_dir = std::env::current_dir() |
| 45 | + .map_err(|error| format!("Failed to get current directory path: {:?}", error))?; |
| 46 | + let current_exe = std::env::current_exe() |
| 47 | + .map_err(|error| format!("Failed to get current exe path: {:?}", error))?; |
| 48 | + let parent_dir = match current_exe.parent() { |
| 49 | + Some(parent) => parent, |
| 50 | + None => { |
| 51 | + return Err(format!( |
| 52 | + "Cannot get parent of current executable path `{}`", |
| 53 | + current_exe.display() |
| 54 | + )); |
| 55 | + } |
| 56 | + }; |
| 57 | + std::env::set_current_dir(&parent_dir).map_err(|error| { |
| 58 | + format!( |
| 59 | + "Failed to go to `{}` folder: {:?}", |
| 60 | + parent_dir.display(), |
| 61 | + error |
| 62 | + ) |
| 63 | + })?; |
| 64 | + |
| 65 | + let mut env: HashMap<String, String> = std::env::vars().collect(); |
| 66 | + ConfigInfo::default().setup(&mut env, None)?; |
| 67 | + let toolchain = get_toolchain()?; |
| 68 | + |
| 69 | + let toolchain_version = rustc_toolchain_version_info(&toolchain)?; |
| 70 | + let default_version = rustc_version_info(None)?; |
| 71 | + if toolchain_version != default_version { |
| 72 | + println!( |
| 73 | + "rustc_codegen_gcc is built for {} but the default rustc version is {}.", |
| 74 | + toolchain_version.short, default_version.short, |
| 75 | + ); |
| 76 | + println!("Using {}.", toolchain_version.short); |
| 77 | + } |
| 78 | + |
| 79 | + // We go back to the original folder since we now have set up everything we needed. |
| 80 | + std::env::set_current_dir(¤t_dir).map_err(|error| { |
| 81 | + format!( |
| 82 | + "Failed to go back to `{}` folder: {:?}", |
| 83 | + current_dir.display(), |
| 84 | + error |
| 85 | + ) |
| 86 | + })?; |
| 87 | + |
| 88 | + let rustflags = env.get("RUSTFLAGS").cloned().unwrap_or_default(); |
| 89 | + env.insert("RUSTDOCFLAGS".to_string(), rustflags); |
| 90 | + let toolchain = format!("+{}", toolchain); |
| 91 | + let mut command: Vec<&dyn AsRef<OsStr>> = vec![&"cargo", &toolchain]; |
| 92 | + for arg in &args { |
| 93 | + command.push(arg); |
| 94 | + } |
| 95 | + run_command_with_output_and_env(&command, None, Some(&env))?; |
| 96 | + |
| 97 | + Ok(()) |
| 98 | +} |
0 commit comments