]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/bin/rustc.rs
Auto merge of #35856 - phimuemue:master, r=brson
[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 extern crate bootstrap;
29
30 use std::env;
31 use std::ffi::OsString;
32 use std::path::PathBuf;
33 use std::process::Command;
34
35 fn main() {
36     let args = env::args_os().skip(1).collect::<Vec<_>>();
37     // Detect whether or not we're a build script depending on whether --target
38     // is passed (a bit janky...)
39     let target = args.windows(2).find(|w| &*w[0] == "--target")
40                                 .and_then(|w| w[1].to_str());
41     let version = args.iter().find(|w| &**w == "-vV");
42
43     // Build scripts always use the snapshot compiler which is guaranteed to be
44     // able to produce an executable, whereas intermediate compilers may not
45     // have the standard library built yet and may not be able to produce an
46     // executable. Otherwise we just use the standard compiler we're
47     // bootstrapping with.
48     //
49     // Also note that cargo will detect the version of the compiler to trigger
50     // a rebuild when the compiler changes. If this happens, we want to make
51     // sure to use the actual compiler instead of the snapshot compiler becase
52     // that's the one that's actually changing.
53     let (rustc, libdir) = if target.is_none() && version.is_none() {
54         ("RUSTC_SNAPSHOT", "RUSTC_SNAPSHOT_LIBDIR")
55     } else {
56         ("RUSTC_REAL", "RUSTC_LIBDIR")
57     };
58     let stage = env::var("RUSTC_STAGE").unwrap();
59
60     let rustc = env::var_os(rustc).unwrap();
61     let libdir = env::var_os(libdir).unwrap();
62     let mut dylib_path = bootstrap::util::dylib_path();
63     dylib_path.insert(0, PathBuf::from(libdir));
64
65     let mut cmd = Command::new(rustc);
66     cmd.args(&args)
67        .arg("--cfg").arg(format!("stage{}", stage))
68        .env(bootstrap::util::dylib_path_var(),
69             env::join_paths(&dylib_path).unwrap());
70
71     if let Some(target) = target {
72         // The stage0 compiler has a special sysroot distinct from what we
73         // actually downloaded, so we just always pass the `--sysroot` option.
74         cmd.arg("--sysroot").arg(env::var_os("RUSTC_SYSROOT").unwrap());
75
76         // When we build Rust dylibs they're all intended for intermediate
77         // usage, so make sure we pass the -Cprefer-dynamic flag instead of
78         // linking all deps statically into the dylib.
79         cmd.arg("-Cprefer-dynamic");
80
81         // Help the libc crate compile by assisting it in finding the MUSL
82         // native libraries.
83         if let Some(s) = env::var_os("MUSL_ROOT") {
84             let mut root = OsString::from("native=");
85             root.push(&s);
86             root.push("/lib");
87             cmd.arg("-L").arg(&root);
88         }
89
90         // Pass down extra flags, commonly used to configure `-Clinker` when
91         // cross compiling.
92         if let Ok(s) = env::var("RUSTC_FLAGS") {
93             cmd.args(&s.split(" ").filter(|s| !s.is_empty()).collect::<Vec<_>>());
94         }
95
96         // If we're compiling specifically the `panic_abort` crate then we pass
97         // the `-C panic=abort` option. Note that we do not do this for any
98         // other crate intentionally as this is the only crate for now that we
99         // ship with panic=abort.
100         //
101         // This... is a bit of a hack how we detect this. Ideally this
102         // information should be encoded in the crate I guess? Would likely
103         // require an RFC amendment to RFC 1513, however.
104         let is_panic_abort = args.windows(2).any(|a| {
105             &*a[0] == "--crate-name" && &*a[1] == "panic_abort"
106         });
107         // FIXME(stage0): remove this `stage != "0"` condition
108         if is_panic_abort && stage != "0" {
109             cmd.arg("-C").arg("panic=abort");
110         }
111
112         // Set various options from config.toml to configure how we're building
113         // code.
114         if env::var("RUSTC_DEBUGINFO") == Ok("true".to_string()) {
115             cmd.arg("-g");
116         }
117         let debug_assertions = match env::var("RUSTC_DEBUG_ASSERTIONS") {
118             Ok(s) => if s == "true" {"y"} else {"n"},
119             Err(..) => "n",
120         };
121         cmd.arg("-C").arg(format!("debug-assertions={}", debug_assertions));
122         if let Ok(s) = env::var("RUSTC_CODEGEN_UNITS") {
123             cmd.arg("-C").arg(format!("codegen-units={}", s));
124         }
125
126         // Dealing with rpath here is a little special, so let's go into some
127         // detail. First off, `-rpath` is a linker option on Unix platforms
128         // which adds to the runtime dynamic loader path when looking for
129         // dynamic libraries. We use this by default on Unix platforms to ensure
130         // that our nightlies behave the same on Windows, that is they work out
131         // of the box. This can be disabled, of course, but basically that's why
132         // we're gated on RUSTC_RPATH here.
133         //
134         // Ok, so the astute might be wondering "why isn't `-C rpath` used
135         // here?" and that is indeed a good question to task. This codegen
136         // option is the compiler's current interface to generating an rpath.
137         // Unfortunately it doesn't quite suffice for us. The flag currently
138         // takes no value as an argument, so the compiler calculates what it
139         // should pass to the linker as `-rpath`. This unfortunately is based on
140         // the **compile time** directory structure which when building with
141         // Cargo will be very different than the runtime directory structure.
142         //
143         // All that's a really long winded way of saying that if we use
144         // `-Crpath` then the executables generated have the wrong rpath of
145         // something like `$ORIGIN/deps` when in fact the way we distribute
146         // rustc requires the rpath to be `$ORIGIN/../lib`.
147         //
148         // So, all in all, to set up the correct rpath we pass the linker
149         // argument manually via `-C link-args=-Wl,-rpath,...`. Plus isn't it
150         // fun to pass a flag to a tool to pass a flag to pass a flag to a tool
151         // to change a flag in a binary?
152         if env::var("RUSTC_RPATH") == Ok("true".to_string()) {
153             let rpath = if target.contains("apple") {
154                 Some("-Wl,-rpath,@loader_path/../lib")
155             } else if !target.contains("windows") {
156                 Some("-Wl,-rpath,$ORIGIN/../lib")
157             } else {
158                 None
159             };
160             if let Some(rpath) = rpath {
161                 cmd.arg("-C").arg(format!("link-args={}", rpath));
162             }
163         }
164     }
165
166     // Actually run the compiler!
167     std::process::exit(match cmd.status() {
168         Ok(s) => s.code().unwrap_or(1),
169         Err(e) => panic!("\n\nfailed to run {:?}: {}\n\n", cmd, e),
170     })
171 }