]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/build_system/mod.rs
Auto merge of #96906 - tbu-:pr_stabilize_to_ipv4_mapped, r=dtolnay
[rust.git] / compiler / rustc_codegen_cranelift / build_system / mod.rs
1 use std::env;
2 use std::path::PathBuf;
3 use std::process;
4
5 mod build_backend;
6 mod build_sysroot;
7 mod config;
8 mod prepare;
9 mod rustc_info;
10 mod utils;
11
12 fn usage() {
13     eprintln!("Usage:");
14     eprintln!("  ./y.rs prepare");
15     eprintln!(
16         "  ./y.rs build [--debug] [--sysroot none|clif|llvm] [--target-dir DIR] [--no-unstable-features]"
17     );
18 }
19
20 macro_rules! arg_error {
21     ($($err:tt)*) => {{
22         eprintln!($($err)*);
23         usage();
24         std::process::exit(1);
25     }};
26 }
27
28 enum Command {
29     Build,
30 }
31
32 #[derive(Copy, Clone)]
33 pub(crate) enum SysrootKind {
34     None,
35     Clif,
36     Llvm,
37 }
38
39 pub fn main() {
40     env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1");
41     env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1");
42     // The target dir is expected in the default location. Guard against the user changing it.
43     env::set_var("CARGO_TARGET_DIR", "target");
44
45     let mut args = env::args().skip(1);
46     let command = match args.next().as_deref() {
47         Some("prepare") => {
48             if args.next().is_some() {
49                 arg_error!("./x.rs prepare doesn't expect arguments");
50             }
51             prepare::prepare();
52             process::exit(0);
53         }
54         Some("build") => Command::Build,
55         Some(flag) if flag.starts_with('-') => arg_error!("Expected command found flag {}", flag),
56         Some(command) => arg_error!("Unknown command {}", command),
57         None => {
58             usage();
59             process::exit(0);
60         }
61     };
62
63     let mut target_dir = PathBuf::from("build");
64     let mut channel = "release";
65     let mut sysroot_kind = SysrootKind::Clif;
66     let mut use_unstable_features = true;
67     while let Some(arg) = args.next().as_deref() {
68         match arg {
69             "--target-dir" => {
70                 target_dir = PathBuf::from(args.next().unwrap_or_else(|| {
71                     arg_error!("--target-dir requires argument");
72                 }))
73             }
74             "--debug" => channel = "debug",
75             "--sysroot" => {
76                 sysroot_kind = match args.next().as_deref() {
77                     Some("none") => SysrootKind::None,
78                     Some("clif") => SysrootKind::Clif,
79                     Some("llvm") => SysrootKind::Llvm,
80                     Some(arg) => arg_error!("Unknown sysroot kind {}", arg),
81                     None => arg_error!("--sysroot requires argument"),
82                 }
83             }
84             "--no-unstable-features" => use_unstable_features = false,
85             flag if flag.starts_with("-") => arg_error!("Unknown flag {}", flag),
86             arg => arg_error!("Unexpected argument {}", arg),
87         }
88     }
89     target_dir = std::env::current_dir().unwrap().join(target_dir);
90
91     let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") {
92         host_triple
93     } else if let Some(host_triple) = config::get_value("host") {
94         host_triple
95     } else {
96         rustc_info::get_host_triple()
97     };
98     let target_triple = if let Ok(target_triple) = std::env::var("TARGET_TRIPLE") {
99         if target_triple != "" {
100             target_triple
101         } else {
102             host_triple.clone() // Empty target triple can happen on GHA
103         }
104     } else if let Some(target_triple) = config::get_value("target") {
105         target_triple
106     } else {
107         host_triple.clone()
108     };
109
110     if target_triple.ends_with("-msvc") {
111         eprintln!("The MSVC toolchain is not yet supported by rustc_codegen_cranelift.");
112         eprintln!("Switch to the MinGW toolchain for Windows support.");
113         eprintln!("Hint: You can use `rustup set default-host x86_64-pc-windows-gnu` to");
114         eprintln!("set the global default target to MinGW");
115         process::exit(1);
116     }
117
118     let cg_clif_build_dir =
119         build_backend::build_backend(channel, &host_triple, use_unstable_features);
120     build_sysroot::build_sysroot(
121         channel,
122         sysroot_kind,
123         &target_dir,
124         cg_clif_build_dir,
125         &host_triple,
126         &target_triple,
127     );
128 }