]> git.proxmox.com Git - rustc.git/blame - src/bootstrap/rustc.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / bootstrap / rustc.rs
CommitLineData
7453a54e
SL
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.
54a0048b
SL
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.
7453a54e
SL
27
28extern crate bootstrap;
29
30use std::env;
31use std::ffi::OsString;
32use std::path::PathBuf;
33use std::process::Command;
34
35fn 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 is_build_script = args.iter()
40 .position(|i| i.to_str() == Some("--target"))
41 .is_none();
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 let rustc = if is_build_script {
49 env::var_os("RUSTC_SNAPSHOT").unwrap()
50 } else {
51 env::var_os("RUSTC_REAL").unwrap()
52 };
53
54 let mut cmd = Command::new(rustc);
55 cmd.args(&args)
56 .arg("--cfg").arg(format!("stage{}", env::var("RUSTC_STAGE").unwrap()));
57
58 if is_build_script {
59 // Build scripts are always built with the snapshot compiler, so we need
60 // to be sure to set up the right path information for the OS dynamic
61 // linker to find the libraries in question.
62 if let Some(p) = env::var_os("RUSTC_SNAPSHOT_LIBDIR") {
63 let mut path = bootstrap::dylib_path();
64 path.insert(0, PathBuf::from(p));
65 cmd.env(bootstrap::dylib_path_var(), env::join_paths(path).unwrap());
66 }
67 } else {
68 cmd.arg("--sysroot").arg(env::var_os("RUSTC_SYSROOT").unwrap());
69
70 // When we build Rust dylibs they're all intended for intermediate
71 // usage, so make sure we pass the -Cprefer-dynamic flag instead of
72 // linking all deps statically into the dylib.
73 cmd.arg("-Cprefer-dynamic");
74
75 if let Some(s) = env::var_os("MUSL_ROOT") {
76 let mut root = OsString::from("native=");
77 root.push(&s);
78 root.push("/lib");
79 cmd.arg("-L").arg(&root);
80 }
81 if let Ok(s) = env::var("RUSTC_FLAGS") {
82 cmd.args(&s.split(" ").filter(|s| !s.is_empty()).collect::<Vec<_>>());
83 }
84 }
85
86 // Set various options from config.toml to configure how we're building
87 // code.
88 if env::var("RUSTC_DEBUGINFO") == Ok("true".to_string()) {
89 cmd.arg("-g");
90 }
91 if env::var("RUSTC_RPATH") == Ok("true".to_string()) {
92 cmd.arg("-Crpath");
93 }
94 let debug_assertions = match env::var("RUSTC_DEBUG_ASSERTIONS") {
95 Ok(s) => if s == "true" {"y"} else {"n"},
96 Err(..) => "n",
97 };
98 cmd.arg("-C").arg(format!("debug-assertions={}", debug_assertions));
99 if let Ok(s) = env::var("RUSTC_CODEGEN_UNITS") {
100 cmd.arg("-C").arg(format!("codegen-units={}", s));
101 }
102
103 // Actually run the compiler!
104 std::process::exit(match cmd.status() {
105 Ok(s) => s.code().unwrap_or(1),
106 Err(e) => panic!("\n\nfailed to run {:?}: {}\n\n", cmd, e),
107 })
108}