1 //! Shim which is passed to Cargo as "rustc" when running the bootstrap.
3 //! This shim will take care of some various tasks that our build process
4 //! requires that Cargo can't quite do through normal configuration:
6 //! 1. When compiling build scripts and build dependencies, we need a guaranteed
7 //! full standard library available. The only compiler which actually has
8 //! this is the snapshot, so we detect this situation and always compile with
9 //! the snapshot compiler.
10 //! 2. We pass a bunch of `--cfg` and other flags based on what we're compiling
11 //! (and this slightly differs based on a whether we're using a snapshot or
12 //! not), so we do that all here.
14 //! This may one day be replaced by RUSTFLAGS, but the dynamic nature of
15 //! switching compilers for the bootstrap and for build scripts will probably
16 //! never get replaced.
19 use std::path::PathBuf;
20 use std::process::Command;
21 use std::str::FromStr;
22 use std::time::Instant;
25 let args = env::args_os().skip(1).collect::<Vec<_>>();
27 // Detect whether or not we're a build script depending on whether --target
28 // is passed (a bit janky...)
29 let target = args.windows(2).find(|w| &*w[0] == "--target").and_then(|w| w[1].to_str());
30 let version = args.iter().find(|w| &**w == "-vV");
32 let verbose = match env::var("RUSTC_VERBOSE") {
33 Ok(s) => usize::from_str(&s).expect("RUSTC_VERBOSE should be an integer"),
37 // Use a different compiler for build scripts, since there may not yet be a
38 // libstd for the real compiler to use. However, if Cargo is attempting to
39 // determine the version of the compiler, the real compiler needs to be
40 // used. Currently, these two states are differentiated based on whether
41 // --target and -vV is/isn't passed.
42 let (rustc, libdir) = if target.is_none() && version.is_none() {
43 ("RUSTC_SNAPSHOT", "RUSTC_SNAPSHOT_LIBDIR")
45 ("RUSTC_REAL", "RUSTC_LIBDIR")
47 let stage = env::var("RUSTC_STAGE").expect("RUSTC_STAGE was not set");
48 let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set");
49 let on_fail = env::var_os("RUSTC_ON_FAIL").map(Command::new);
51 let rustc = env::var_os(rustc).unwrap_or_else(|| panic!("{:?} was not set", rustc));
52 let libdir = env::var_os(libdir).unwrap_or_else(|| panic!("{:?} was not set", libdir));
53 let mut dylib_path = bootstrap::util::dylib_path();
54 dylib_path.insert(0, PathBuf::from(&libdir));
56 let mut cmd = Command::new(rustc);
57 cmd.args(&args).env(bootstrap::util::dylib_path_var(), env::join_paths(&dylib_path).unwrap());
59 // Get the name of the crate we're compiling, if any.
61 args.windows(2).find(|args| args[0] == "--crate-name").and_then(|args| args[1].to_str());
63 if let Some(crate_name) = crate_name {
64 if let Some(target) = env::var_os("RUSTC_TIME") {
66 || target.into_string().unwrap().split(',').any(|c| c.trim() == crate_name)
73 // Print backtrace in case of ICE
74 if env::var("RUSTC_BACKTRACE_ON_ICE").is_ok() && env::var("RUST_BACKTRACE").is_err() {
75 cmd.env("RUST_BACKTRACE", "1");
78 if let Ok(lint_flags) = env::var("RUSTC_LINT_FLAGS") {
79 cmd.args(lint_flags.split_whitespace());
83 // The stage0 compiler has a special sysroot distinct from what we
84 // actually downloaded, so we just always pass the `--sysroot` option,
85 // unless one is already set.
86 if !args.iter().any(|arg| arg == "--sysroot") {
87 cmd.arg("--sysroot").arg(&sysroot);
90 // If we're compiling specifically the `panic_abort` crate then we pass
91 // the `-C panic=abort` option. Note that we do not do this for any
92 // other crate intentionally as this is the only crate for now that we
93 // ship with panic=abort.
95 // This... is a bit of a hack how we detect this. Ideally this
96 // information should be encoded in the crate I guess? Would likely
97 // require an RFC amendment to RFC 1513, however.
99 // `compiler_builtins` are unconditionally compiled with panic=abort to
100 // workaround undefined references to `rust_eh_unwind_resume` generated
101 // otherwise, see issue https://github.com/rust-lang/rust/issues/43095.
102 if crate_name == Some("panic_abort")
103 || crate_name == Some("compiler_builtins") && stage != "0"
105 cmd.arg("-C").arg("panic=abort");
108 // FIXME(rust-lang/cargo#5754) we shouldn't be using special env vars
109 // here, but rather Cargo should know what flags to pass rustc itself.
111 // Override linker if necessary.
112 if let Ok(host_linker) = env::var("RUSTC_HOST_LINKER") {
113 cmd.arg(format!("-Clinker={}", host_linker));
115 if env::var_os("RUSTC_HOST_FUSE_LD_LLD").is_some() {
116 cmd.arg("-Clink-args=-fuse-ld=lld");
119 if let Ok(s) = env::var("RUSTC_HOST_CRT_STATIC") {
121 cmd.arg("-C").arg("target-feature=+crt-static");
124 cmd.arg("-C").arg("target-feature=-crt-static");
129 if let Ok(map) = env::var("RUSTC_DEBUGINFO_MAP") {
130 cmd.arg("--remap-path-prefix").arg(&map);
133 // Force all crates compiled by this compiler to (a) be unstable and (b)
134 // allow the `rustc_private` feature to link to other unstable crates
135 // also in the sysroot. We also do this for host crates, since those
136 // may be proc macros, in which case we might ship them.
137 if env::var_os("RUSTC_FORCE_UNSTABLE").is_some() && (stage != "0" || target.is_some()) {
138 cmd.arg("-Z").arg("force-unstable-if-unmarked");
143 "rustc command: {:?}={:?} {:?}",
144 bootstrap::util::dylib_path_var(),
145 env::join_paths(&dylib_path).unwrap(),
148 eprintln!("sysroot: {:?}", sysroot);
149 eprintln!("libdir: {:?}", libdir);
152 let start = Instant::now();
154 let errmsg = format!("\nFailed to run:\n{:?}\n-------------", cmd);
155 cmd.status().expect(&errmsg)
158 if env::var_os("RUSTC_PRINT_STEP_TIMINGS").is_some() {
159 if let Some(crate_name) = crate_name {
160 let dur = start.elapsed();
161 let is_test = args.iter().any(|a| a == "--test");
163 "[RUSTC-TIMING] {} test:{} {}.{:03}",
172 if status.success() {
173 std::process::exit(0);
174 // note: everything below here is unreachable. do not put code that
175 // should run on success, after this block.
178 println!("\nDid not run successfully: {}\n{:?}\n-------------", status, cmd);
181 if let Some(mut on_fail) = on_fail {
182 on_fail.status().expect("Could not run the on_fail command");
185 // Preserve the exit code. In case of signal, exit with 0xfe since it's
186 // awkward to preserve this status in a cross-platform way.
187 match status.code() {
188 Some(i) => std::process::exit(i),
190 eprintln!("rustc exited with {}", status);
191 std::process::exit(0xfe);