]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/bin/rustc.rs
Update bootstrap compiler
[rust.git] / src / bootstrap / bin / rustc.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Shim which is passed to Cargo as "rustc" when running the bootstrap.
12 //!
13 //! This shim will take care of some various tasks that our build process
14 //! requires that Cargo can't quite do through normal configuration:
15 //!
16 //! 1. When compiling build scripts and build dependencies, we need a guaranteed
17 //!    full standard library available. The only compiler which actually has
18 //!    this is the snapshot, so we detect this situation and always compile with
19 //!    the snapshot compiler.
20 //! 2. We pass a bunch of `--cfg` and other flags based on what we're compiling
21 //!    (and this slightly differs based on a whether we're using a snapshot or
22 //!    not), so we do that all here.
23 //!
24 //! This may one day be replaced by RUSTFLAGS, but the dynamic nature of
25 //! switching compilers for the bootstrap and for build scripts will probably
26 //! never get replaced.
27
28 #![deny(warnings)]
29
30 extern crate bootstrap;
31
32 use std::env;
33 use std::ffi::OsString;
34 use std::str::FromStr;
35 use std::path::PathBuf;
36 use std::process::{Command, ExitStatus};
37
38 fn main() {
39     let mut args = env::args_os().skip(1).collect::<Vec<_>>();
40
41     // Append metadata suffix for internal crates. See the corresponding entry
42     // in bootstrap/lib.rs for details.
43     if let Ok(s) = env::var("RUSTC_METADATA_SUFFIX") {
44         for i in 1..args.len() {
45             // Dirty code for borrowing issues
46             let mut new = None;
47             if let Some(current_as_str) = args[i].to_str() {
48                 if (&*args[i - 1] == "-C" && current_as_str.starts_with("metadata")) ||
49                    current_as_str.starts_with("-Cmetadata") {
50                     new = Some(format!("{}-{}", current_as_str, s));
51                 }
52             }
53             if let Some(new) = new { args[i] = new.into(); }
54         }
55     }
56
57     // Drop `--error-format json` because despite our desire for json messages
58     // from Cargo we don't want any from rustc itself.
59     if let Some(n) = args.iter().position(|n| n == "--error-format") {
60         args.remove(n);
61         args.remove(n);
62     }
63
64     // Detect whether or not we're a build script depending on whether --target
65     // is passed (a bit janky...)
66     let target = args.windows(2)
67         .find(|w| &*w[0] == "--target")
68         .and_then(|w| w[1].to_str());
69     let version = args.iter().find(|w| &**w == "-vV");
70
71     let verbose = match env::var("RUSTC_VERBOSE") {
72         Ok(s) => usize::from_str(&s).expect("RUSTC_VERBOSE should be an integer"),
73         Err(_) => 0,
74     };
75
76     // Use a different compiler for build scripts, since there may not yet be a
77     // libstd for the real compiler to use. However, if Cargo is attempting to
78     // determine the version of the compiler, the real compiler needs to be
79     // used. Currently, these two states are differentiated based on whether
80     // --target and -vV is/isn't passed.
81     let (rustc, libdir) = if target.is_none() && version.is_none() {
82         ("RUSTC_SNAPSHOT", "RUSTC_SNAPSHOT_LIBDIR")
83     } else {
84         ("RUSTC_REAL", "RUSTC_LIBDIR")
85     };
86     let stage = env::var("RUSTC_STAGE").expect("RUSTC_STAGE was not set");
87     let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set");
88     let mut on_fail = env::var_os("RUSTC_ON_FAIL").map(|of| Command::new(of));
89
90     let rustc = env::var_os(rustc).unwrap_or_else(|| panic!("{:?} was not set", rustc));
91     let libdir = env::var_os(libdir).unwrap_or_else(|| panic!("{:?} was not set", libdir));
92     let mut dylib_path = bootstrap::util::dylib_path();
93     dylib_path.insert(0, PathBuf::from(libdir));
94
95     let mut cmd = Command::new(rustc);
96     cmd.args(&args)
97         .arg("--cfg")
98         .arg(format!("stage{}", stage))
99         .env(bootstrap::util::dylib_path_var(),
100              env::join_paths(&dylib_path).unwrap());
101
102     if let Some(target) = target {
103         // The stage0 compiler has a special sysroot distinct from what we
104         // actually downloaded, so we just always pass the `--sysroot` option.
105         cmd.arg("--sysroot").arg(sysroot);
106
107         // When we build Rust dylibs they're all intended for intermediate
108         // usage, so make sure we pass the -Cprefer-dynamic flag instead of
109         // linking all deps statically into the dylib.
110         if env::var_os("RUSTC_NO_PREFER_DYNAMIC").is_none() {
111             cmd.arg("-Cprefer-dynamic");
112         }
113
114         // Help the libc crate compile by assisting it in finding the MUSL
115         // native libraries.
116         if let Some(s) = env::var_os("MUSL_ROOT") {
117             let mut root = OsString::from("native=");
118             root.push(&s);
119             root.push("/lib");
120             cmd.arg("-L").arg(&root);
121         }
122
123         // Override linker if necessary.
124         if let Ok(target_linker) = env::var("RUSTC_TARGET_LINKER") {
125             cmd.arg(format!("-Clinker={}", target_linker));
126         }
127
128         // Pass down incremental directory, if any.
129         if let Ok(dir) = env::var("RUSTC_INCREMENTAL") {
130             cmd.arg(format!("-Zincremental={}", dir));
131         }
132
133         let crate_name = args.windows(2)
134             .find(|a| &*a[0] == "--crate-name")
135             .unwrap();
136         let crate_name = &*crate_name[1];
137
138         // If we're compiling specifically the `panic_abort` crate then we pass
139         // the `-C panic=abort` option. Note that we do not do this for any
140         // other crate intentionally as this is the only crate for now that we
141         // ship with panic=abort.
142         //
143         // This... is a bit of a hack how we detect this. Ideally this
144         // information should be encoded in the crate I guess? Would likely
145         // require an RFC amendment to RFC 1513, however.
146         //
147         // `compiler_builtins` are unconditionally compiled with panic=abort to
148         // workaround undefined references to `rust_eh_unwind_resume` generated
149         // otherwise, see issue https://github.com/rust-lang/rust/issues/43095.
150         if crate_name == "panic_abort" ||
151            crate_name == "compiler_builtins" && stage != "0" {
152             cmd.arg("-C").arg("panic=abort");
153         }
154
155         // Set various options from config.toml to configure how we're building
156         // code.
157         if env::var("RUSTC_DEBUGINFO") == Ok("true".to_string()) {
158             cmd.arg("-g");
159         } else if env::var("RUSTC_DEBUGINFO_LINES") == Ok("true".to_string()) {
160             cmd.arg("-Cdebuginfo=1");
161         }
162         let debug_assertions = match env::var("RUSTC_DEBUG_ASSERTIONS") {
163             Ok(s) => if s == "true" { "y" } else { "n" },
164             Err(..) => "n",
165         };
166
167         // The compiler builtins are pretty sensitive to symbols referenced in
168         // libcore and such, so we never compile them with debug assertions.
169         if crate_name == "compiler_builtins" {
170             cmd.arg("-C").arg("debug-assertions=no");
171         } else {
172             cmd.arg("-C").arg(format!("debug-assertions={}", debug_assertions));
173         }
174
175         if let Ok(s) = env::var("RUSTC_CODEGEN_UNITS") {
176             cmd.arg("-C").arg(format!("codegen-units={}", s));
177         }
178         if env::var("RUSTC_THINLTO").is_ok() {
179             cmd.arg("-Ccodegen-units=16").arg("-Zthinlto");
180         }
181
182         // Emit save-analysis info.
183         if env::var("RUSTC_SAVE_ANALYSIS") == Ok("api".to_string()) {
184             cmd.arg("-Zsave-analysis");
185             cmd.env("RUST_SAVE_ANALYSIS_CONFIG",
186                     "{\"output_file\": null,\"full_docs\": false,\
187                      \"pub_only\": true,\"reachable_only\": false,\
188                      \"distro_crate\": true,\"signatures\": false,\"borrow_data\": false}");
189         }
190
191         // Dealing with rpath here is a little special, so let's go into some
192         // detail. First off, `-rpath` is a linker option on Unix platforms
193         // which adds to the runtime dynamic loader path when looking for
194         // dynamic libraries. We use this by default on Unix platforms to ensure
195         // that our nightlies behave the same on Windows, that is they work out
196         // of the box. This can be disabled, of course, but basically that's why
197         // we're gated on RUSTC_RPATH here.
198         //
199         // Ok, so the astute might be wondering "why isn't `-C rpath` used
200         // here?" and that is indeed a good question to task. This codegen
201         // option is the compiler's current interface to generating an rpath.
202         // Unfortunately it doesn't quite suffice for us. The flag currently
203         // takes no value as an argument, so the compiler calculates what it
204         // should pass to the linker as `-rpath`. This unfortunately is based on
205         // the **compile time** directory structure which when building with
206         // Cargo will be very different than the runtime directory structure.
207         //
208         // All that's a really long winded way of saying that if we use
209         // `-Crpath` then the executables generated have the wrong rpath of
210         // something like `$ORIGIN/deps` when in fact the way we distribute
211         // rustc requires the rpath to be `$ORIGIN/../lib`.
212         //
213         // So, all in all, to set up the correct rpath we pass the linker
214         // argument manually via `-C link-args=-Wl,-rpath,...`. Plus isn't it
215         // fun to pass a flag to a tool to pass a flag to pass a flag to a tool
216         // to change a flag in a binary?
217         if env::var("RUSTC_RPATH") == Ok("true".to_string()) {
218             let rpath = if target.contains("apple") {
219
220                 // Note that we need to take one extra step on macOS to also pass
221                 // `-Wl,-instal_name,@rpath/...` to get things to work right. To
222                 // do that we pass a weird flag to the compiler to get it to do
223                 // so. Note that this is definitely a hack, and we should likely
224                 // flesh out rpath support more fully in the future.
225                 cmd.arg("-Z").arg("osx-rpath-install-name");
226                 Some("-Wl,-rpath,@loader_path/../lib")
227             } else if !target.contains("windows") {
228                 Some("-Wl,-rpath,$ORIGIN/../lib")
229             } else {
230                 None
231             };
232             if let Some(rpath) = rpath {
233                 cmd.arg("-C").arg(format!("link-args={}", rpath));
234             }
235         }
236
237         if let Ok(s) = env::var("RUSTC_CRT_STATIC") {
238             if s == "true" {
239                 cmd.arg("-C").arg("target-feature=+crt-static");
240             }
241             if s == "false" {
242                 cmd.arg("-C").arg("target-feature=-crt-static");
243             }
244         }
245
246         // When running miri tests, we need to generate MIR for all libraries
247         if env::var("TEST_MIRI").ok().map_or(false, |val| val == "true") {
248             cmd.arg("-Zalways-encode-mir");
249             cmd.arg("-Zmir-emit-validate=1");
250         }
251
252         // Force all crates compiled by this compiler to (a) be unstable and (b)
253         // allow the `rustc_private` feature to link to other unstable crates
254         // also in the sysroot.
255         if env::var_os("RUSTC_FORCE_UNSTABLE").is_some() {
256             cmd.arg("-Z").arg("force-unstable-if-unmarked");
257         }
258     } else {
259         // Override linker if necessary.
260         if let Ok(host_linker) = env::var("RUSTC_HOST_LINKER") {
261             cmd.arg(format!("-Clinker={}", host_linker));
262         }
263     }
264
265     let color = match env::var("RUSTC_COLOR") {
266         Ok(s) => usize::from_str(&s).expect("RUSTC_COLOR should be an integer"),
267         Err(_) => 0,
268     };
269
270     if color != 0 {
271         cmd.arg("--color=always");
272     }
273
274     if verbose > 1 {
275         eprintln!("rustc command: {:?}", cmd);
276     }
277
278     // Actually run the compiler!
279     std::process::exit(if let Some(ref mut on_fail) = on_fail {
280         match cmd.status() {
281             Ok(s) if s.success() => 0,
282             _ => {
283                 println!("\nDid not run successfully:\n{:?}\n-------------", cmd);
284                 exec_cmd(on_fail).expect("could not run the backup command");
285                 1
286             }
287         }
288     } else {
289         std::process::exit(match exec_cmd(&mut cmd) {
290             Ok(s) => s.code().unwrap_or(0xfe),
291             Err(e) => panic!("\n\nfailed to run {:?}: {}\n\n", cmd, e),
292         })
293     })
294 }
295
296 #[cfg(unix)]
297 fn exec_cmd(cmd: &mut Command) -> ::std::io::Result<ExitStatus> {
298     use std::os::unix::process::CommandExt;
299     Err(cmd.exec())
300 }
301
302 #[cfg(not(unix))]
303 fn exec_cmd(cmd: &mut Command) -> ::std::io::Result<ExitStatus> {
304     cmd.status()
305 }