about summary refs log tree commit diff
path: root/compiler/rustc_codegen_gcc/build_system/src/clone_gcc.rs
blob: ee683df419c227f369c96c442eaa49ccfa88fe4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use std::path::{Path, PathBuf};

use crate::config::ConfigInfo;
use crate::utils::{git_clone, run_command_with_output};

fn show_usage() {
    println!(
        r#"
`clone-gcc` command help:

    --out-path         : Location where the GCC repository will be cloned (default: `./gcc`)"#
    );
    ConfigInfo::show_usage();
    println!("    --help                 : Show this help");
}

#[derive(Default)]
struct Args {
    out_path: PathBuf,
    config_info: ConfigInfo,
}

impl Args {
    fn new() -> Result<Option<Self>, String> {
        let mut command_args = Self::default();

        let mut out_path = None;

        // We skip binary name and the `clone-gcc` command.
        let mut args = std::env::args().skip(2);

        while let Some(arg) = args.next() {
            match arg.as_str() {
                "--out-path" => match args.next() {
                    Some(path) if !path.is_empty() => out_path = Some(path),
                    _ => {
                        return Err("Expected an argument after `--out-path`, found nothing".into());
                    }
                },
                "--help" => {
                    show_usage();
                    return Ok(None);
                }
                arg => {
                    if !command_args.config_info.parse_argument(arg, &mut args)? {
                        return Err(format!("Unknown option {arg}"));
                    }
                }
            }
        }
        command_args.out_path = match out_path {
            Some(p) => p.into(),
            None => PathBuf::from("./gcc"),
        };
        Ok(Some(command_args))
    }
}

pub fn run() -> Result<(), String> {
    let Some(args) = Args::new()? else {
        return Ok(());
    };

    let result = git_clone("https://github.com/rust-lang/gcc", Some(&args.out_path), false)?;
    if result.ran_clone {
        let gcc_commit = args.config_info.get_gcc_commit()?;
        println!("Checking out GCC commit `{gcc_commit}`...");
        run_command_with_output(
            &[&"git", &"checkout", &gcc_commit],
            Some(Path::new(&result.repo_dir)),
        )?;
    } else {
        println!(
            "There is already a GCC folder in `{}`, leaving things as is...",
            args.out_path.display()
        );
    }
    Ok(())
}