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