2015-11-19 15:20:12 -08:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2016-03-07 23:11:05 -08:00
|
|
|
|
|
|
|
//! Shim which is passed to Cargo as "rustc" when running the bootstrap.
|
|
|
|
//!
|
|
|
|
//! This shim will take care of some various tasks that our build process
|
|
|
|
//! requires that Cargo can't quite do through normal configuration:
|
|
|
|
//!
|
|
|
|
//! 1. When compiling build scripts and build dependencies, we need a guaranteed
|
|
|
|
//! full standard library available. The only compiler which actually has
|
|
|
|
//! this is the snapshot, so we detect this situation and always compile with
|
|
|
|
//! the snapshot compiler.
|
|
|
|
//! 2. We pass a bunch of `--cfg` and other flags based on what we're compiling
|
|
|
|
//! (and this slightly differs based on a whether we're using a snapshot or
|
|
|
|
//! not), so we do that all here.
|
|
|
|
//!
|
|
|
|
//! This may one day be replaced by RUSTFLAGS, but the dynamic nature of
|
|
|
|
//! switching compilers for the bootstrap and for build scripts will probably
|
|
|
|
//! never get replaced.
|
2015-11-19 15:20:12 -08:00
|
|
|
|
2016-12-20 09:38:13 -08:00
|
|
|
#![deny(warnings)]
|
|
|
|
|
2015-11-19 15:20:12 -08:00
|
|
|
extern crate bootstrap;
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::ffi::OsString;
|
2016-11-16 18:02:56 -05:00
|
|
|
use std::io;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::str::FromStr;
|
2016-04-29 14:23:15 -07:00
|
|
|
use std::path::PathBuf;
|
2016-12-18 23:45:39 +02:00
|
|
|
use std::process::{Command, ExitStatus};
|
2015-11-19 15:20:12 -08:00
|
|
|
|
|
|
|
fn main() {
|
2017-05-12 19:24:04 +09:00
|
|
|
let mut args = env::args_os().skip(1).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// Append metadata suffix for internal crates. See the corresponding entry
|
|
|
|
// in bootstrap/lib.rs for details.
|
|
|
|
if let Ok(s) = env::var("RUSTC_METADATA_SUFFIX") {
|
|
|
|
for i in 1..args.len() {
|
|
|
|
// Dirty code for borrowing issues
|
|
|
|
let mut new = None;
|
|
|
|
if let Some(current_as_str) = args[i].to_str() {
|
|
|
|
if (&*args[i - 1] == "-C" && current_as_str.starts_with("metadata")) ||
|
|
|
|
current_as_str.starts_with("-Cmetadata") {
|
|
|
|
new = Some(format!("{}-{}", current_as_str, s));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(new) = new { args[i] = new.into(); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 10:19:43 -07:00
|
|
|
// Drop `--error-format json` because despite our desire for json messages
|
|
|
|
// from Cargo we don't want any from rustc itself.
|
|
|
|
if let Some(n) = args.iter().position(|n| n == "--error-format") {
|
|
|
|
args.remove(n);
|
|
|
|
args.remove(n);
|
|
|
|
}
|
|
|
|
|
2015-11-19 15:20:12 -08:00
|
|
|
// Detect whether or not we're a build script depending on whether --target
|
|
|
|
// is passed (a bit janky...)
|
2016-10-16 14:57:25 +05:30
|
|
|
let target = args.windows(2)
|
|
|
|
.find(|w| &*w[0] == "--target")
|
|
|
|
.and_then(|w| w[1].to_str());
|
2016-08-18 15:22:23 -07:00
|
|
|
let version = args.iter().find(|w| &**w == "-vV");
|
2015-11-19 15:20:12 -08:00
|
|
|
|
2016-11-16 18:02:56 -05:00
|
|
|
let verbose = match env::var("RUSTC_VERBOSE") {
|
|
|
|
Ok(s) => usize::from_str(&s).expect("RUSTC_VERBOSE should be an integer"),
|
|
|
|
Err(_) => 0,
|
|
|
|
};
|
|
|
|
|
2017-06-27 13:24:37 -06:00
|
|
|
// Use a different compiler for build scripts, since there may not yet be a
|
|
|
|
// libstd for the real compiler to use. However, if Cargo is attempting to
|
|
|
|
// determine the version of the compiler, the real compiler needs to be
|
|
|
|
// used. Currently, these two states are differentiated based on whether
|
|
|
|
// --target and -vV is/isn't passed.
|
2016-08-18 15:22:23 -07:00
|
|
|
let (rustc, libdir) = if target.is_none() && version.is_none() {
|
2016-04-29 14:23:15 -07:00
|
|
|
("RUSTC_SNAPSHOT", "RUSTC_SNAPSHOT_LIBDIR")
|
2015-11-19 15:20:12 -08:00
|
|
|
} else {
|
2016-04-29 14:23:15 -07:00
|
|
|
("RUSTC_REAL", "RUSTC_LIBDIR")
|
2015-11-19 15:20:12 -08:00
|
|
|
};
|
2016-09-25 11:59:12 -04:00
|
|
|
let stage = env::var("RUSTC_STAGE").expect("RUSTC_STAGE was not set");
|
2016-12-24 16:04:48 +00:00
|
|
|
let sysroot = env::var_os("RUSTC_SYSROOT").expect("RUSTC_SYSROOT was not set");
|
2017-02-16 20:52:56 +02:00
|
|
|
let mut on_fail = env::var_os("RUSTC_ON_FAIL").map(|of| Command::new(of));
|
2015-11-19 15:20:12 -08:00
|
|
|
|
2016-09-25 11:59:12 -04:00
|
|
|
let rustc = env::var_os(rustc).unwrap_or_else(|| panic!("{:?} was not set", rustc));
|
|
|
|
let libdir = env::var_os(libdir).unwrap_or_else(|| panic!("{:?} was not set", libdir));
|
2016-07-05 21:58:20 -07:00
|
|
|
let mut dylib_path = bootstrap::util::dylib_path();
|
2016-04-29 14:23:15 -07:00
|
|
|
dylib_path.insert(0, PathBuf::from(libdir));
|
|
|
|
|
2015-11-19 15:20:12 -08:00
|
|
|
let mut cmd = Command::new(rustc);
|
|
|
|
cmd.args(&args)
|
2016-10-16 14:57:25 +05:30
|
|
|
.arg("--cfg")
|
|
|
|
.arg(format!("stage{}", stage))
|
|
|
|
.env(bootstrap::util::dylib_path_var(),
|
|
|
|
env::join_paths(&dylib_path).unwrap());
|
2015-11-19 15:20:12 -08:00
|
|
|
|
2016-05-02 15:16:15 -07:00
|
|
|
if let Some(target) = target {
|
|
|
|
// The stage0 compiler has a special sysroot distinct from what we
|
|
|
|
// actually downloaded, so we just always pass the `--sysroot` option.
|
2016-12-24 16:04:48 +00:00
|
|
|
cmd.arg("--sysroot").arg(sysroot);
|
2015-11-19 15:20:12 -08:00
|
|
|
|
|
|
|
// When we build Rust dylibs they're all intended for intermediate
|
|
|
|
// usage, so make sure we pass the -Cprefer-dynamic flag instead of
|
|
|
|
// linking all deps statically into the dylib.
|
2016-12-28 15:01:21 -08:00
|
|
|
if env::var_os("RUSTC_NO_PREFER_DYNAMIC").is_none() {
|
|
|
|
cmd.arg("-Cprefer-dynamic");
|
|
|
|
}
|
2015-11-19 15:20:12 -08:00
|
|
|
|
2016-05-02 15:16:15 -07:00
|
|
|
// Help the libc crate compile by assisting it in finding the MUSL
|
|
|
|
// native libraries.
|
2015-11-19 15:20:12 -08:00
|
|
|
if let Some(s) = env::var_os("MUSL_ROOT") {
|
|
|
|
let mut root = OsString::from("native=");
|
|
|
|
root.push(&s);
|
|
|
|
root.push("/lib");
|
|
|
|
cmd.arg("-L").arg(&root);
|
|
|
|
}
|
2016-05-02 15:16:15 -07:00
|
|
|
|
|
|
|
// Pass down extra flags, commonly used to configure `-Clinker` when
|
|
|
|
// cross compiling.
|
2016-02-24 14:16:54 -08:00
|
|
|
if let Ok(s) = env::var("RUSTC_FLAGS") {
|
|
|
|
cmd.args(&s.split(" ").filter(|s| !s.is_empty()).collect::<Vec<_>>());
|
|
|
|
}
|
2015-11-19 15:20:12 -08:00
|
|
|
|
2016-11-16 18:02:56 -05:00
|
|
|
// Pass down incremental directory, if any.
|
|
|
|
if let Ok(dir) = env::var("RUSTC_INCREMENTAL") {
|
|
|
|
cmd.arg(format!("-Zincremental={}", dir));
|
|
|
|
|
|
|
|
if verbose > 0 {
|
|
|
|
cmd.arg("-Zincremental-info");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-22 13:14:00 -07:00
|
|
|
let crate_name = args.windows(2)
|
|
|
|
.find(|a| &*a[0] == "--crate-name")
|
|
|
|
.unwrap();
|
|
|
|
let crate_name = &*crate_name[1];
|
|
|
|
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
// If we're compiling specifically the `panic_abort` crate then we pass
|
|
|
|
// the `-C panic=abort` option. Note that we do not do this for any
|
|
|
|
// other crate intentionally as this is the only crate for now that we
|
|
|
|
// ship with panic=abort.
|
|
|
|
//
|
|
|
|
// This... is a bit of a hack how we detect this. Ideally this
|
|
|
|
// information should be encoded in the crate I guess? Would likely
|
|
|
|
// require an RFC amendment to RFC 1513, however.
|
2017-07-16 02:02:34 +03:00
|
|
|
//
|
|
|
|
// `compiler_builtins` are unconditionally compiled with panic=abort to
|
|
|
|
// workaround undefined references to `rust_eh_unwind_resume` generated
|
|
|
|
// otherwise, see issue https://github.com/rust-lang/rust/issues/43095.
|
|
|
|
if crate_name == "panic_abort" ||
|
|
|
|
crate_name == "compiler_builtins" && stage != "0" {
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
cmd.arg("-C").arg("panic=abort");
|
|
|
|
}
|
|
|
|
|
2016-05-02 15:16:15 -07:00
|
|
|
// Set various options from config.toml to configure how we're building
|
|
|
|
// code.
|
2016-04-11 22:54:10 -07:00
|
|
|
if env::var("RUSTC_DEBUGINFO") == Ok("true".to_string()) {
|
|
|
|
cmd.arg("-g");
|
2016-10-19 09:48:46 -07:00
|
|
|
} else if env::var("RUSTC_DEBUGINFO_LINES") == Ok("true".to_string()) {
|
|
|
|
cmd.arg("-Cdebuginfo=1");
|
2016-04-11 22:54:10 -07:00
|
|
|
}
|
|
|
|
let debug_assertions = match env::var("RUSTC_DEBUG_ASSERTIONS") {
|
2016-10-16 14:57:25 +05:30
|
|
|
Ok(s) => if s == "true" { "y" } else { "n" },
|
2016-04-11 22:54:10 -07:00
|
|
|
Err(..) => "n",
|
|
|
|
};
|
2017-06-22 13:14:00 -07:00
|
|
|
|
|
|
|
// The compiler builtins are pretty sensitive to symbols referenced in
|
|
|
|
// libcore and such, so we never compile them with debug assertions.
|
|
|
|
if crate_name == "compiler_builtins" {
|
|
|
|
cmd.arg("-C").arg("debug-assertions=no");
|
|
|
|
} else {
|
|
|
|
cmd.arg("-C").arg(format!("debug-assertions={}", debug_assertions));
|
|
|
|
}
|
|
|
|
|
2016-04-11 22:54:10 -07:00
|
|
|
if let Ok(s) = env::var("RUSTC_CODEGEN_UNITS") {
|
|
|
|
cmd.arg("-C").arg(format!("codegen-units={}", s));
|
|
|
|
}
|
|
|
|
|
2016-10-27 11:41:56 +13:00
|
|
|
// Emit save-analysis info.
|
|
|
|
if env::var("RUSTC_SAVE_ANALYSIS") == Ok("api".to_string()) {
|
2017-07-22 10:33:12 +12:00
|
|
|
cmd.arg("-Zsave-analysis");
|
|
|
|
cmd.env("RUST_SAVE_ANALYSIS_CONFIG",
|
|
|
|
"{\"output_file\": null,\"full_docs\": false,\"pub_only\": true,\
|
2017-08-03 10:20:01 +12:00
|
|
|
\"distro_crate\": true,\"signatures\": false,\"borrow_data\": false}");
|
2016-10-27 11:41:56 +13:00
|
|
|
}
|
|
|
|
|
2016-04-11 22:54:10 -07:00
|
|
|
// Dealing with rpath here is a little special, so let's go into some
|
|
|
|
// detail. First off, `-rpath` is a linker option on Unix platforms
|
|
|
|
// which adds to the runtime dynamic loader path when looking for
|
|
|
|
// dynamic libraries. We use this by default on Unix platforms to ensure
|
|
|
|
// that our nightlies behave the same on Windows, that is they work out
|
|
|
|
// of the box. This can be disabled, of course, but basically that's why
|
|
|
|
// we're gated on RUSTC_RPATH here.
|
|
|
|
//
|
|
|
|
// Ok, so the astute might be wondering "why isn't `-C rpath` used
|
|
|
|
// here?" and that is indeed a good question to task. This codegen
|
|
|
|
// option is the compiler's current interface to generating an rpath.
|
|
|
|
// Unfortunately it doesn't quite suffice for us. The flag currently
|
|
|
|
// takes no value as an argument, so the compiler calculates what it
|
|
|
|
// should pass to the linker as `-rpath`. This unfortunately is based on
|
|
|
|
// the **compile time** directory structure which when building with
|
|
|
|
// Cargo will be very different than the runtime directory structure.
|
|
|
|
//
|
|
|
|
// All that's a really long winded way of saying that if we use
|
|
|
|
// `-Crpath` then the executables generated have the wrong rpath of
|
|
|
|
// something like `$ORIGIN/deps` when in fact the way we distribute
|
|
|
|
// rustc requires the rpath to be `$ORIGIN/../lib`.
|
|
|
|
//
|
|
|
|
// So, all in all, to set up the correct rpath we pass the linker
|
|
|
|
// argument manually via `-C link-args=-Wl,-rpath,...`. Plus isn't it
|
|
|
|
// fun to pass a flag to a tool to pass a flag to pass a flag to a tool
|
|
|
|
// to change a flag in a binary?
|
|
|
|
if env::var("RUSTC_RPATH") == Ok("true".to_string()) {
|
|
|
|
let rpath = if target.contains("apple") {
|
2016-12-17 14:11:02 -08:00
|
|
|
|
2017-03-12 14:13:35 -04:00
|
|
|
// Note that we need to take one extra step on macOS to also pass
|
2016-12-17 14:11:02 -08:00
|
|
|
// `-Wl,-instal_name,@rpath/...` to get things to work right. To
|
|
|
|
// do that we pass a weird flag to the compiler to get it to do
|
|
|
|
// so. Note that this is definitely a hack, and we should likely
|
|
|
|
// flesh out rpath support more fully in the future.
|
2017-06-06 19:32:43 -07:00
|
|
|
cmd.arg("-Z").arg("osx-rpath-install-name");
|
2016-04-11 22:54:10 -07:00
|
|
|
Some("-Wl,-rpath,@loader_path/../lib")
|
|
|
|
} else if !target.contains("windows") {
|
|
|
|
Some("-Wl,-rpath,$ORIGIN/../lib")
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
if let Some(rpath) = rpath {
|
|
|
|
cmd.arg("-C").arg(format!("link-args={}", rpath));
|
|
|
|
}
|
|
|
|
}
|
2017-02-14 14:30:39 -08:00
|
|
|
|
2017-08-22 16:24:29 -05:00
|
|
|
if let Ok(s) = env::var("RUSTC_CRT_STATIC") {
|
|
|
|
if s == "true" {
|
|
|
|
cmd.arg("-C").arg("target-feature=+crt-static");
|
|
|
|
}
|
|
|
|
if s == "false" {
|
|
|
|
cmd.arg("-C").arg("target-feature=-crt-static");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-08 14:36:33 -07:00
|
|
|
// Force all crates compiled by this compiler to (a) be unstable and (b)
|
|
|
|
// allow the `rustc_private` feature to link to other unstable crates
|
|
|
|
// also in the sysroot.
|
|
|
|
if env::var_os("RUSTC_FORCE_UNSTABLE").is_some() {
|
2017-06-06 19:32:43 -07:00
|
|
|
cmd.arg("-Z").arg("force-unstable-if-unmarked");
|
2017-05-08 14:36:33 -07:00
|
|
|
}
|
2015-11-19 15:20:12 -08:00
|
|
|
}
|
|
|
|
|
2017-06-21 10:04:21 -06:00
|
|
|
let color = match env::var("RUSTC_COLOR") {
|
|
|
|
Ok(s) => usize::from_str(&s).expect("RUSTC_COLOR should be an integer"),
|
|
|
|
Err(_) => 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
if color != 0 {
|
|
|
|
cmd.arg("--color=always");
|
|
|
|
}
|
|
|
|
|
2016-11-16 18:02:56 -05:00
|
|
|
if verbose > 1 {
|
|
|
|
writeln!(&mut io::stderr(), "rustc command: {:?}", cmd).unwrap();
|
|
|
|
}
|
|
|
|
|
2015-11-19 15:20:12 -08:00
|
|
|
// Actually run the compiler!
|
2017-02-16 20:52:56 +02:00
|
|
|
std::process::exit(if let Some(ref mut on_fail) = on_fail {
|
|
|
|
match cmd.status() {
|
|
|
|
Ok(s) if s.success() => 0,
|
|
|
|
_ => {
|
|
|
|
println!("\nDid not run successfully:\n{:?}\n-------------", cmd);
|
|
|
|
exec_cmd(on_fail).expect("could not run the backup command");
|
|
|
|
1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std::process::exit(match exec_cmd(&mut cmd) {
|
|
|
|
Ok(s) => s.code().unwrap_or(0xfe),
|
|
|
|
Err(e) => panic!("\n\nfailed to run {:?}: {}\n\n", cmd, e),
|
|
|
|
})
|
2015-11-19 15:20:12 -08:00
|
|
|
})
|
|
|
|
}
|
2016-12-18 23:45:39 +02:00
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn exec_cmd(cmd: &mut Command) -> ::std::io::Result<ExitStatus> {
|
|
|
|
use std::os::unix::process::CommandExt;
|
|
|
|
Err(cmd.exec())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
fn exec_cmd(cmd: &mut Command) -> ::std::io::Result<ExitStatus> {
|
|
|
|
cmd.status()
|
|
|
|
}
|