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.
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.
11 //! Shim which is passed to Cargo as "rustc" when running the bootstrap.
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:
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.
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.
30 extern crate bootstrap
;
33 use std
::ffi
::OsString
;
34 use std
::str::FromStr
;
35 use std
::path
::PathBuf
;
36 use std
::process
::{Command, ExitStatus}
;
39 let mut args
= env
::args_os().skip(1).collect
::<Vec
<_
>>();
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
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
));
53 if let Some(new
) = new { args[i] = new.into(); }
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") {
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");
71 let verbose
= match env
::var("RUSTC_VERBOSE") {
72 Ok(s
) => usize::from_str(&s
).expect("RUSTC_VERBOSE should be an integer"),
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")
84 ("RUSTC_REAL", "RUSTC_LIBDIR")
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
));
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
));
95 let mut cmd
= Command
::new(rustc
);
98 .arg(format
!("stage{}", stage
))
99 .env(bootstrap
::util
::dylib_path_var(),
100 env
::join_paths(&dylib_path
).unwrap());
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
);
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");
114 // Help the libc crate compile by assisting it in finding the MUSL
116 if let Some(s
) = env
::var_os("MUSL_ROOT") {
117 let mut root
= OsString
::from("native=");
120 cmd
.arg("-L").arg(&root
);
123 // Override linker if necessary.
124 if let Ok(target_linker
) = env
::var("RUSTC_TARGET_LINKER") {
125 cmd
.arg(format
!("-Clinker={}", target_linker
));
128 // Pass down incremental directory, if any.
129 if let Ok(dir
) = env
::var("RUSTC_INCREMENTAL") {
130 cmd
.arg(format
!("-Zincremental={}", dir
));
133 let crate_name
= args
.windows(2)
134 .find(|a
| &*a
[0] == "--crate-name")
136 let crate_name
= &*crate_name
[1];
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.
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.
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");
155 // Set various options from config.toml to configure how we're building
157 if env
::var("RUSTC_DEBUGINFO") == Ok("true".to_string()) {
159 } else if env
::var("RUSTC_DEBUGINFO_LINES") == Ok("true".to_string()) {
160 cmd
.arg("-Cdebuginfo=1");
162 let debug_assertions
= match env
::var("RUSTC_DEBUG_ASSERTIONS") {
163 Ok(s
) => if s
== "true" { "y" }
else { "n" }
,
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");
172 cmd
.arg("-C").arg(format
!("debug-assertions={}", debug_assertions
));
175 if let Ok(s
) = env
::var("RUSTC_CODEGEN_UNITS") {
176 cmd
.arg("-C").arg(format
!("codegen-units={}", s
));
178 if stage
!= "0" && env
::var("RUSTC_THINLTO").is_ok() {
179 cmd
.arg("-Ccodegen-units=16").arg("-Zthinlto");
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,\"pub_only\": true,\
187 \"distro_crate\": true,\"signatures\": false,\"borrow_data\": false}");
190 // Dealing with rpath here is a little special, so let's go into some
191 // detail. First off, `-rpath` is a linker option on Unix platforms
192 // which adds to the runtime dynamic loader path when looking for
193 // dynamic libraries. We use this by default on Unix platforms to ensure
194 // that our nightlies behave the same on Windows, that is they work out
195 // of the box. This can be disabled, of course, but basically that's why
196 // we're gated on RUSTC_RPATH here.
198 // Ok, so the astute might be wondering "why isn't `-C rpath` used
199 // here?" and that is indeed a good question to task. This codegen
200 // option is the compiler's current interface to generating an rpath.
201 // Unfortunately it doesn't quite suffice for us. The flag currently
202 // takes no value as an argument, so the compiler calculates what it
203 // should pass to the linker as `-rpath`. This unfortunately is based on
204 // the **compile time** directory structure which when building with
205 // Cargo will be very different than the runtime directory structure.
207 // All that's a really long winded way of saying that if we use
208 // `-Crpath` then the executables generated have the wrong rpath of
209 // something like `$ORIGIN/deps` when in fact the way we distribute
210 // rustc requires the rpath to be `$ORIGIN/../lib`.
212 // So, all in all, to set up the correct rpath we pass the linker
213 // argument manually via `-C link-args=-Wl,-rpath,...`. Plus isn't it
214 // fun to pass a flag to a tool to pass a flag to pass a flag to a tool
215 // to change a flag in a binary?
216 if env
::var("RUSTC_RPATH") == Ok("true".to_string()) {
217 let rpath
= if target
.contains("apple") {
219 // Note that we need to take one extra step on macOS to also pass
220 // `-Wl,-instal_name,@rpath/...` to get things to work right. To
221 // do that we pass a weird flag to the compiler to get it to do
222 // so. Note that this is definitely a hack, and we should likely
223 // flesh out rpath support more fully in the future.
224 cmd
.arg("-Z").arg("osx-rpath-install-name");
225 Some("-Wl,-rpath,@loader_path/../lib")
226 } else if !target
.contains("windows") {
227 Some("-Wl,-rpath,$ORIGIN/../lib")
231 if let Some(rpath
) = rpath
{
232 cmd
.arg("-C").arg(format
!("link-args={}", rpath
));
236 if let Ok(s
) = env
::var("RUSTC_CRT_STATIC") {
238 cmd
.arg("-C").arg("target-feature=+crt-static");
241 cmd
.arg("-C").arg("target-feature=-crt-static");
245 // When running miri tests, we need to generate MIR for all libraries
246 if env
::var("TEST_MIRI").ok().map_or(false, |val
| val
== "true") {
247 cmd
.arg("-Zalways-encode-mir");
248 cmd
.arg("-Zmir-emit-validate=1");
251 // Force all crates compiled by this compiler to (a) be unstable and (b)
252 // allow the `rustc_private` feature to link to other unstable crates
253 // also in the sysroot.
254 if env
::var_os("RUSTC_FORCE_UNSTABLE").is_some() {
255 cmd
.arg("-Z").arg("force-unstable-if-unmarked");
258 // Override linker if necessary.
259 if let Ok(host_linker
) = env
::var("RUSTC_HOST_LINKER") {
260 cmd
.arg(format
!("-Clinker={}", host_linker
));
264 let color
= match env
::var("RUSTC_COLOR") {
265 Ok(s
) => usize::from_str(&s
).expect("RUSTC_COLOR should be an integer"),
270 cmd
.arg("--color=always");
274 eprintln
!("rustc command: {:?}", cmd
);
277 // Actually run the compiler!
278 std
::process
::exit(if let Some(ref mut on_fail
) = on_fail
{
280 Ok(s
) if s
.success() => 0,
282 println
!("\nDid not run successfully:\n{:?}\n-------------", cmd
);
283 exec_cmd(on_fail
).expect("could not run the backup command");
288 std
::process
::exit(match exec_cmd(&mut cmd
) {
289 Ok(s
) => s
.code().unwrap_or(0xfe),
290 Err(e
) => panic
!("\n\nfailed to run {:?}: {}\n\n", cmd
, e
),
296 fn exec_cmd(cmd
: &mut Command
) -> ::std
::io
::Result
<ExitStatus
> {
297 use std
::os
::unix
::process
::CommandExt
;
302 fn exec_cmd(cmd
: &mut Command
) -> ::std
::io
::Result
<ExitStatus
> {