• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![warn(rust_2018_idioms)]
2 #![warn(unused_lifetimes)]
3 #![warn(unreachable_pub)]
4 
5 use std::env;
6 use std::path::PathBuf;
7 use std::process;
8 
9 use self::utils::{is_ci, is_ci_opt, Compiler};
10 
11 mod abi_cafe;
12 mod bench;
13 mod build_backend;
14 mod build_sysroot;
15 mod config;
16 mod path;
17 mod prepare;
18 mod rustc_info;
19 mod tests;
20 mod utils;
21 
usage()22 fn usage() {
23     eprintln!("{}", include_str!("usage.txt"));
24 }
25 
26 macro_rules! arg_error {
27     ($($err:tt)*) => {{
28         eprintln!($($err)*);
29         usage();
30         std::process::exit(1);
31     }};
32 }
33 
34 #[derive(PartialEq, Debug)]
35 enum Command {
36     Prepare,
37     Build,
38     Test,
39     AbiCafe,
40     Bench,
41 }
42 
43 #[derive(Copy, Clone, Debug)]
44 enum SysrootKind {
45     None,
46     Clif,
47     Llvm,
48 }
49 
50 #[derive(Clone, Debug)]
51 enum CodegenBackend {
52     Local(PathBuf),
53     Builtin(String),
54 }
55 
main()56 fn main() {
57     if env::var("RUST_BACKTRACE").is_err() {
58         env::set_var("RUST_BACKTRACE", "1");
59     }
60     env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1");
61 
62     if is_ci() {
63         // Disabling incr comp reduces cache size and incr comp doesn't save as much on CI anyway
64         env::set_var("CARGO_BUILD_INCREMENTAL", "false");
65 
66         if !is_ci_opt() {
67             // Enable the Cranelift verifier
68             env::set_var("CG_CLIF_ENABLE_VERIFIER", "1");
69         }
70     }
71 
72     let mut args = env::args().skip(1);
73     let command = match args.next().as_deref() {
74         Some("prepare") => Command::Prepare,
75         Some("build") => Command::Build,
76         Some("test") => Command::Test,
77         Some("abi-cafe") => Command::AbiCafe,
78         Some("bench") => Command::Bench,
79         Some(flag) if flag.starts_with('-') => arg_error!("Expected command found flag {}", flag),
80         Some(command) => arg_error!("Unknown command {}", command),
81         None => {
82             usage();
83             process::exit(0);
84         }
85     };
86 
87     let mut out_dir = PathBuf::from(".");
88     let mut download_dir = None;
89     let mut channel = "release";
90     let mut sysroot_kind = SysrootKind::Clif;
91     let mut use_unstable_features = true;
92     let mut frozen = false;
93     let mut skip_tests = vec![];
94     let mut use_backend = None;
95     while let Some(arg) = args.next().as_deref() {
96         match arg {
97             "--out-dir" => {
98                 out_dir = PathBuf::from(args.next().unwrap_or_else(|| {
99                     arg_error!("--out-dir requires argument");
100                 }));
101             }
102             "--download-dir" => {
103                 download_dir = Some(PathBuf::from(args.next().unwrap_or_else(|| {
104                     arg_error!("--download-dir requires argument");
105                 })));
106             }
107             "--debug" => channel = "debug",
108             "--sysroot" => {
109                 sysroot_kind = match args.next().as_deref() {
110                     Some("none") => SysrootKind::None,
111                     Some("clif") => SysrootKind::Clif,
112                     Some("llvm") => SysrootKind::Llvm,
113                     Some(arg) => arg_error!("Unknown sysroot kind {}", arg),
114                     None => arg_error!("--sysroot requires argument"),
115                 }
116             }
117             "--no-unstable-features" => use_unstable_features = false,
118             "--frozen" => frozen = true,
119             "--skip-test" => {
120                 // FIXME check that all passed in tests actually exist
121                 skip_tests.push(args.next().unwrap_or_else(|| {
122                     arg_error!("--skip-test requires argument");
123                 }));
124             }
125             "--use-backend" => {
126                 use_backend = Some(match args.next() {
127                     Some(name) => name,
128                     None => arg_error!("--use-backend requires argument"),
129                 });
130             }
131             flag if flag.starts_with("-") => arg_error!("Unknown flag {}", flag),
132             arg => arg_error!("Unexpected argument {}", arg),
133         }
134     }
135 
136     let current_dir = std::env::current_dir().unwrap();
137     out_dir = current_dir.join(out_dir);
138 
139     if command == Command::Prepare {
140         prepare::prepare(&path::Dirs {
141             source_dir: current_dir.clone(),
142             download_dir: download_dir
143                 .map(|dir| current_dir.join(dir))
144                 .unwrap_or_else(|| out_dir.join("download")),
145             build_dir: PathBuf::from("dummy_do_not_use"),
146             dist_dir: PathBuf::from("dummy_do_not_use"),
147             frozen,
148         });
149         process::exit(0);
150     }
151 
152     let rustup_toolchain_name = match (env::var("CARGO"), env::var("RUSTC"), env::var("RUSTDOC")) {
153         (Ok(_), Ok(_), Ok(_)) => None,
154         (Err(_), Err(_), Err(_)) => Some(rustc_info::get_toolchain_name()),
155         _ => {
156             eprintln!("All of CARGO, RUSTC and RUSTDOC need to be set or none must be set");
157             process::exit(1);
158         }
159     };
160     let bootstrap_host_compiler = {
161         let cargo = rustc_info::get_cargo_path();
162         let rustc = rustc_info::get_rustc_path();
163         let rustdoc = rustc_info::get_rustdoc_path();
164         let triple = std::env::var("HOST_TRIPLE")
165             .ok()
166             .or_else(|| config::get_value("host"))
167             .unwrap_or_else(|| rustc_info::get_host_triple(&rustc));
168         Compiler {
169             cargo,
170             rustc,
171             rustdoc,
172             rustflags: String::new(),
173             rustdocflags: String::new(),
174             triple,
175             runner: vec![],
176         }
177     };
178     let target_triple = std::env::var("TARGET_TRIPLE")
179         .ok()
180         .or_else(|| config::get_value("target"))
181         .unwrap_or_else(|| bootstrap_host_compiler.triple.clone());
182 
183     let dirs = path::Dirs {
184         source_dir: current_dir.clone(),
185         download_dir: download_dir
186             .map(|dir| current_dir.join(dir))
187             .unwrap_or_else(|| out_dir.join("download")),
188         build_dir: out_dir.join("build"),
189         dist_dir: out_dir.join("dist"),
190         frozen,
191     };
192 
193     path::RelPath::BUILD.ensure_exists(&dirs);
194 
195     {
196         // Make sure we always explicitly specify the target dir
197         let target =
198             path::RelPath::BUILD.join("target_dir_should_be_set_explicitly").to_path(&dirs);
199         env::set_var("CARGO_TARGET_DIR", &target);
200         let _ = std::fs::remove_file(&target);
201         std::fs::File::create(target).unwrap();
202     }
203 
204     env::set_var("RUSTC", "rustc_should_be_set_explicitly");
205     env::set_var("RUSTDOC", "rustdoc_should_be_set_explicitly");
206 
207     let cg_clif_dylib = if let Some(name) = use_backend {
208         CodegenBackend::Builtin(name)
209     } else {
210         CodegenBackend::Local(build_backend::build_backend(
211             &dirs,
212             channel,
213             &bootstrap_host_compiler,
214             use_unstable_features,
215         ))
216     };
217     match command {
218         Command::Prepare => {
219             // Handled above
220         }
221         Command::Test => {
222             tests::run_tests(
223                 &dirs,
224                 channel,
225                 sysroot_kind,
226                 use_unstable_features,
227                 &skip_tests.iter().map(|test| &**test).collect::<Vec<_>>(),
228                 &cg_clif_dylib,
229                 &bootstrap_host_compiler,
230                 rustup_toolchain_name.as_deref(),
231                 target_triple.clone(),
232             );
233         }
234         Command::AbiCafe => {
235             if bootstrap_host_compiler.triple != target_triple {
236                 eprintln!("Abi-cafe doesn't support cross-compilation");
237                 process::exit(1);
238             }
239             abi_cafe::run(
240                 channel,
241                 sysroot_kind,
242                 &dirs,
243                 &cg_clif_dylib,
244                 rustup_toolchain_name.as_deref(),
245                 &bootstrap_host_compiler,
246             );
247         }
248         Command::Build => {
249             build_sysroot::build_sysroot(
250                 &dirs,
251                 channel,
252                 sysroot_kind,
253                 &cg_clif_dylib,
254                 &bootstrap_host_compiler,
255                 rustup_toolchain_name.as_deref(),
256                 target_triple,
257             );
258         }
259         Command::Bench => {
260             build_sysroot::build_sysroot(
261                 &dirs,
262                 channel,
263                 sysroot_kind,
264                 &cg_clif_dylib,
265                 &bootstrap_host_compiler,
266                 rustup_toolchain_name.as_deref(),
267                 target_triple,
268             );
269             bench::benchmark(&dirs, &bootstrap_host_compiler);
270         }
271     }
272 }
273