rustbuild: Refactor stage arguments away

The facet of a stage is rarely relevant when running a tool or building
something, it's all a question of what stage the *compiler* is built in. We've
already got a nice handy `Compiler` structure to carry this information, so
let's use it!

This refactors the signature of the `Build::cargo` function two ways:

1. The `stage` argument is removed, this was just duplicated with the `compiler`
   argument's stage field.
2. The `target` argument is now required. This was a bug where if the `--target`
   flag isn't passed then the snapshot stage0 compiler is always used, so we
   won't pick up any changes.

Much of the other changes in this commit are just propagating these decisions
outwards. For example many of the `Step` variants no longer have a stage
argument as they're baked into the compiler.
This commit is contained in:
Alex Crichton 2016-03-11 17:30:16 -08:00
parent 4a917e050d
commit 8cd1c17d90
6 changed files with 109 additions and 135 deletions

View File

@ -23,15 +23,13 @@ use build::{Build, Compiler, Mode};
/// This will build the standard library for a particular stage of the build
/// using the `compiler` targeting the `target` architecture. The artifacts
/// created will also be linked into the sysroot directory.
pub fn std<'a>(build: &'a Build, stage: u32, target: &str,
compiler: &Compiler<'a>) {
let host = compiler.host;
println!("Building stage{} std artifacts ({} -> {})", stage,
host, target);
pub fn std<'a>(build: &'a Build, target: &str, compiler: &Compiler<'a>) {
println!("Building stage{} std artifacts ({} -> {})", compiler.stage,
compiler.host, target);
// Move compiler-rt into place as it'll be required by the compiler when
// building the standard library to link the dylib of libstd
let libdir = build.sysroot_libdir(stage, &host, target);
let libdir = build.sysroot_libdir(compiler, target);
let _ = fs::remove_dir_all(&libdir);
t!(fs::create_dir_all(&libdir));
t!(fs::hard_link(&build.compiler_rt_built.borrow()[target],
@ -39,10 +37,9 @@ pub fn std<'a>(build: &'a Build, stage: u32, target: &str,
build_startup_objects(build, target, &libdir);
let out_dir = build.cargo_out(stage, &host, Mode::Libstd, target);
let out_dir = build.cargo_out(compiler, Mode::Libstd, target);
build.clear_if_dirty(&out_dir, &build.compiler_path(compiler));
let mut cargo = build.cargo(stage, compiler, Mode::Libstd, Some(target),
"build");
let mut cargo = build.cargo(compiler, Mode::Libstd, target, "build");
cargo.arg("--features").arg(build.std_features())
.arg("--manifest-path")
.arg(build.src.join("src/rustc/std_shim/Cargo.toml"));
@ -59,7 +56,7 @@ pub fn std<'a>(build: &'a Build, stage: u32, target: &str,
}
build.run(&mut cargo);
std_link(build, stage, target, compiler, host);
std_link(build, target, compiler, compiler.host);
}
/// Link all libstd rlibs/dylibs into the sysroot location.
@ -67,12 +64,12 @@ pub fn std<'a>(build: &'a Build, stage: u32, target: &str,
/// Links those artifacts generated in the given `stage` for `target` produced
/// by `compiler` into `host`'s sysroot.
pub fn std_link(build: &Build,
stage: u32,
target: &str,
compiler: &Compiler,
host: &str) {
let libdir = build.sysroot_libdir(stage, host, target);
let out_dir = build.cargo_out(stage, compiler.host, Mode::Libstd, target);
let target_compiler = Compiler::new(compiler.stage, host);
let libdir = build.sysroot_libdir(&target_compiler, target);
let out_dir = build.cargo_out(compiler, Mode::Libstd, target);
// If we're linking one compiler host's output into another, then we weren't
// called from the `std` method above. In that case we clean out what's
@ -85,7 +82,8 @@ pub fn std_link(build: &Build,
}
add_to_sysroot(&out_dir, &libdir);
if target.contains("musl") && (target.contains("x86_64") || target.contains("i686")) {
if target.contains("musl") &&
(target.contains("x86_64") || target.contains("i686")) {
copy_third_party_objects(build, target, &libdir);
}
}
@ -130,17 +128,14 @@ fn build_startup_objects(build: &Build, target: &str, into: &Path) {
/// This will build the compiler for a particular stage of the build using
/// the `compiler` targeting the `target` architecture. The artifacts
/// created will also be linked into the sysroot directory.
pub fn rustc<'a>(build: &'a Build, stage: u32, target: &str,
compiler: &Compiler<'a>) {
let host = compiler.host;
println!("Building stage{} compiler artifacts ({} -> {})", stage,
host, target);
pub fn rustc<'a>(build: &'a Build, target: &str, compiler: &Compiler<'a>) {
println!("Building stage{} compiler artifacts ({} -> {})",
compiler.stage, compiler.host, target);
let out_dir = build.cargo_out(stage, &host, Mode::Librustc, target);
build.clear_if_dirty(&out_dir, &libstd_shim(build, stage, &host, target));
let out_dir = build.cargo_out(compiler, Mode::Librustc, target);
build.clear_if_dirty(&out_dir, &libstd_shim(build, compiler, target));
let mut cargo = build.cargo(stage, compiler, Mode::Librustc, Some(target),
"build");
let mut cargo = build.cargo(compiler, Mode::Librustc, target, "build");
cargo.arg("--features").arg(build.rustc_features())
.arg("--manifest-path")
.arg(build.src.join("src/rustc/Cargo.toml"));
@ -184,7 +179,7 @@ pub fn rustc<'a>(build: &'a Build, stage: u32, target: &str,
}
build.run(&mut cargo);
rustc_link(build, stage, target, compiler, compiler.host);
rustc_link(build, target, compiler, compiler.host);
}
/// Link all librustc rlibs/dylibs into the sysroot location.
@ -192,19 +187,19 @@ pub fn rustc<'a>(build: &'a Build, stage: u32, target: &str,
/// Links those artifacts generated in the given `stage` for `target` produced
/// by `compiler` into `host`'s sysroot.
pub fn rustc_link(build: &Build,
stage: u32,
target: &str,
compiler: &Compiler,
host: &str) {
let libdir = build.sysroot_libdir(stage, host, target);
let out_dir = build.cargo_out(stage, compiler.host, Mode::Librustc, target);
let target_compiler = Compiler::new(compiler.stage, host);
let libdir = build.sysroot_libdir(&target_compiler, target);
let out_dir = build.cargo_out(compiler, Mode::Librustc, target);
add_to_sysroot(&out_dir, &libdir);
}
/// Cargo's output path for the standard library in a given stage, compiled
/// by a particular compiler for the specified target.
fn libstd_shim(build: &Build, stage: u32, host: &str, target: &str) -> PathBuf {
build.cargo_out(stage, host, Mode::Libstd, target).join("libstd_shim.rlib")
fn libstd_shim(build: &Build, compiler: &Compiler, target: &str) -> PathBuf {
build.cargo_out(compiler, Mode::Libstd, target).join("libstd_shim.rlib")
}
fn compiler_file(compiler: &Path, file: &str) -> String {
@ -219,16 +214,21 @@ fn compiler_file(compiler: &Path, file: &str) -> String {
/// compiler.
pub fn assemble_rustc(build: &Build, stage: u32, host: &str) {
assert!(stage > 0, "the stage0 compiler isn't assembled, it's downloaded");
// The compiler that we're assembling
let target_compiler = Compiler::new(stage, host);
// The compiler that compiled the compiler we're assembling
let build_compiler = Compiler::new(stage - 1, &build.config.build);
// Clear out old files
let sysroot = build.sysroot(stage, host);
let sysroot = build.sysroot(&target_compiler);
let _ = fs::remove_dir_all(&sysroot);
t!(fs::create_dir_all(&sysroot));
// Link in all dylibs to the libdir
let sysroot_libdir = sysroot.join(libdir(host));
t!(fs::create_dir_all(&sysroot_libdir));
let src_libdir = build.sysroot_libdir(stage - 1, &build.config.build, host);
let src_libdir = build.sysroot_libdir(&build_compiler, host);
for f in t!(fs::read_dir(&src_libdir)).map(|f| t!(f)) {
let filename = f.file_name().into_string().unwrap();
if is_dylib(&filename) {
@ -236,8 +236,7 @@ pub fn assemble_rustc(build: &Build, stage: u32, host: &str) {
}
}
let out_dir = build.cargo_out(stage - 1, &build.config.build,
Mode::Librustc, host);
let out_dir = build.cargo_out(&build_compiler, Mode::Librustc, host);
// Link the compiler binary itself into place
let rustc = out_dir.join(exe("rustc", host));
@ -315,7 +314,7 @@ pub fn tool(build: &Build, stage: u32, host: &str, tool: &str) {
// let out_dir = build.cargo_out(stage, &host, Mode::Librustc, target);
// build.clear_if_dirty(&out_dir, &libstd_shim(build, stage, &host, target));
let mut cargo = build.cargo(stage, &compiler, Mode::Tool, None, "build");
let mut cargo = build.cargo(&compiler, Mode::Tool, host, "build");
cargo.arg("--manifest-path")
.arg(build.src.join(format!("src/tools/{}/Cargo.toml", tool)));
build.run(&mut cargo);

View File

@ -81,6 +81,7 @@ pub fn standalone(build: &Build, stage: u32, host: &str, out: &Path) {
}
let mut cmd = Command::new(&rustdoc);
build.add_rustc_lib_path(&compiler, &mut cmd);
cmd.arg("--html-after-content").arg(&footer)
.arg("--html-before-content").arg(&version_info)
.arg("--html-in-header").arg(&favicon)
@ -107,14 +108,13 @@ pub fn standalone(build: &Build, stage: u32, host: &str, out: &Path) {
pub fn std(build: &Build, stage: u32, host: &str, out: &Path) {
println!("Documenting stage{} std ({})", stage, host);
let compiler = Compiler::new(stage, host);
let out_dir = build.stage_out(stage, host, Mode::Libstd)
let out_dir = build.stage_out(&compiler, Mode::Libstd)
.join(host).join("doc");
let rustdoc = build.rustdoc(&compiler);
build.clear_if_dirty(&out_dir, &rustdoc);
let mut cargo = build.cargo(stage, &compiler, Mode::Libstd, Some(host),
"doc");
let mut cargo = build.cargo(&compiler, Mode::Libstd, host, "doc");
cargo.arg("--manifest-path")
.arg(build.src.join("src/rustc/std_shim/Cargo.toml"))
.arg("--features").arg(build.std_features());
@ -125,14 +125,13 @@ pub fn std(build: &Build, stage: u32, host: &str, out: &Path) {
pub fn rustc(build: &Build, stage: u32, host: &str, out: &Path) {
println!("Documenting stage{} compiler ({})", stage, host);
let compiler = Compiler::new(stage, host);
let out_dir = build.stage_out(stage, host, Mode::Librustc)
let out_dir = build.stage_out(&compiler, Mode::Librustc)
.join(host).join("doc");
let rustdoc = build.rustdoc(&compiler);
if !up_to_date(&rustdoc, &out_dir.join("rustc/index.html")) {
t!(fs::remove_dir_all(&out_dir));
}
let mut cargo = build.cargo(stage, &compiler, Mode::Librustc, Some(host),
"doc");
let mut cargo = build.cargo(&compiler, Mode::Librustc, host, "doc");
cargo.arg("--manifest-path")
.arg(build.src.join("src/rustc/Cargo.toml"))
.arg("--features").arg(build.rustc_features());

View File

@ -152,19 +152,17 @@ impl Build {
CompilerRt { _dummy } => {
native::compiler_rt(self, target.target);
}
Libstd { stage, compiler } => {
compile::std(self, stage, target.target, &compiler);
Libstd { compiler } => {
compile::std(self, target.target, &compiler);
}
Librustc { stage, compiler } => {
compile::rustc(self, stage, target.target, &compiler);
Librustc { compiler } => {
compile::rustc(self, target.target, &compiler);
}
LibstdLink { stage, compiler, host } => {
compile::std_link(self, stage, target.target,
&compiler, host);
LibstdLink { compiler, host } => {
compile::std_link(self, target.target, &compiler, host);
}
LibrustcLink { stage, compiler, host } => {
compile::rustc_link(self, stage, target.target,
&compiler, host);
LibrustcLink { compiler, host } => {
compile::rustc_link(self, target.target, &compiler, host);
}
Rustc { stage: 0 } => {
// nothing to do...
@ -261,58 +259,52 @@ impl Build {
/// This will create a `Command` that represents a pending execution of
/// Cargo for the specified stage, whether or not the standard library is
/// being built, and using the specified compiler targeting `target`.
// FIXME: aren't stage/compiler duplicated?
fn cargo(&self,
stage: u32,
compiler: &Compiler,
mode: Mode,
target: Option<&str>,
target: &str,
cmd: &str) -> Command {
let mut cargo = Command::new(&self.cargo);
let host = compiler.host;
let out_dir = self.stage_out(stage, host, mode);
let out_dir = self.stage_out(compiler, mode);
cargo.env("CARGO_TARGET_DIR", out_dir)
.arg(cmd)
.arg("-j").arg(self.jobs().to_string());
.arg("-j").arg(self.jobs().to_string())
.arg("--target").arg(target);
// Customize the compiler we're running. Specify the compiler to cargo
// as our shim and then pass it some various options used to configure
// how the actual compiler itself is called.
cargo.env("RUSTC", self.out.join("bootstrap/debug/rustc"))
.env("RUSTC_REAL", self.compiler_path(compiler))
.env("RUSTC_STAGE", self.stage_arg(stage, compiler).to_string())
.env("RUSTC_STAGE", compiler.stage.to_string())
.env("RUSTC_DEBUGINFO", self.config.rust_debuginfo.to_string())
.env("RUSTC_CODEGEN_UNITS",
self.config.rust_codegen_units.to_string())
.env("RUSTC_DEBUG_ASSERTIONS",
self.config.rust_debug_assertions.to_string())
.env("RUSTC_SNAPSHOT", &self.rustc)
.env("RUSTC_SYSROOT", self.sysroot(stage, host))
.env("RUSTC_SYSROOT", self.sysroot(compiler))
.env("RUSTC_SNAPSHOT_LIBDIR", self.rustc_snapshot_libdir())
.env("RUSTC_RPATH", self.config.rust_rpath.to_string())
.env("RUSTDOC", self.out.join("bootstrap/debug/rustdoc"))
.env("RUSTDOC_REAL", self.rustdoc(compiler));
.env("RUSTDOC_REAL", self.rustdoc(compiler))
.env("RUSTC_FLAGS", self.rustc_flags(target).join(" "));
if let Some(target) = target {
cargo.env("RUSTC_FLAGS", self.rustc_flags(target).join(" "));
cargo.arg("--target").arg(target);
// Specify some various options for build scripts used throughout
// the build.
//
// FIXME: the guard against msvc shouldn't need to be here
if !target.contains("msvc") {
cargo.env(format!("CC_{}", target), self.cc(target))
.env(format!("AR_{}", target), self.ar(target))
.env(format!("CFLAGS_{}", target), self.cflags(target));
}
// Environment variables *required* needed throughout the build
//
// FIXME: should update code to not require this env vars
cargo.env("CFG_COMPILER_HOST_TRIPLE", target);
// Specify some various options for build scripts used throughout
// the build.
//
// FIXME: the guard against msvc shouldn't need to be here
if !target.contains("msvc") {
cargo.env(format!("CC_{}", target), self.cc(target))
.env(format!("AR_{}", target), self.ar(target))
.env(format!("CFLAGS_{}", target), self.cflags(target));
}
// Environment variables *required* needed throughout the build
//
// FIXME: should update code to not require this env vars
cargo.env("CFG_COMPILER_HOST_TRIPLE", target);
if self.config.verbose || self.flags.verbose {
cargo.arg("-v");
}
@ -328,50 +320,37 @@ impl Build {
if compiler.is_snapshot(self) {
self.rustc.clone()
} else {
self.sysroot(compiler.stage, compiler.host).join("bin")
.join(exe("rustc", compiler.host))
self.sysroot(compiler).join("bin").join(exe("rustc", compiler.host))
}
}
/// Get the specified tool built by the specified compiler
fn tool(&self, compiler: &Compiler, tool: &str) -> PathBuf {
self.stage_out(compiler.stage, compiler.host, Mode::Tool)
.join(self.cargo_dir())
self.cargo_out(compiler, Mode::Tool, compiler.host)
.join(exe(tool, compiler.host))
}
/// Get the `rustdoc` executable next to the specified compiler
fn rustdoc(&self, compiler: &Compiler) -> PathBuf {
let root = if compiler.is_snapshot(self) {
let mut rustdoc = self.rustc.clone();
rustdoc.pop();
rustdoc
} else {
let (stage, host) = (compiler.stage, compiler.host);
self.cargo_out(stage - 1, host, Mode::Librustc, host)
};
root.join(exe("rustdoc", compiler.host))
let mut rustdoc = self.compiler_path(compiler);
rustdoc.pop();
rustdoc.push(exe("rustdoc", compiler.host));
return rustdoc
}
/// Get a `Command` which is ready to run `tool` in `stage` built for
/// `host`.
#[allow(dead_code)] // this will be used soon
fn tool_cmd(&self, compiler: &Compiler, tool: &str) -> Command {
let mut cmd = Command::new(self.tool(&compiler, tool));
let host = compiler.host;
let stage = compiler.stage;
let paths = vec![
self.cargo_out(stage, host, Mode::Libstd, host).join("deps"),
self.cargo_out(stage, host, Mode::Librustc, host).join("deps"),
self.cargo_out(compiler, Mode::Libstd, host).join("deps"),
self.cargo_out(compiler, Mode::Librustc, host).join("deps"),
];
add_lib_path(paths, &mut cmd);
return cmd
}
fn stage_arg(&self, stage: u32, compiler: &Compiler) -> u32 {
if stage == 0 && compiler.host != self.config.build {1} else {stage}
}
/// Get the space-separated set of activated features for the standard
/// library.
fn std_features(&self) -> String {
@ -400,16 +379,16 @@ impl Build {
if self.config.rust_optimize {"release"} else {"debug"}
}
fn sysroot(&self, stage: u32, host: &str) -> PathBuf {
if stage == 0 {
self.stage_out(stage, host, Mode::Librustc)
fn sysroot(&self, compiler: &Compiler) -> PathBuf {
if compiler.stage == 0 {
self.out.join(compiler.host).join("stage0-sysroot")
} else {
self.out.join(host).join(format!("stage{}", stage))
self.out.join(compiler.host).join(format!("stage{}", compiler.stage))
}
}
fn sysroot_libdir(&self, stage: u32, host: &str, target: &str) -> PathBuf {
self.sysroot(stage, host).join("lib").join("rustlib")
fn sysroot_libdir(&self, compiler: &Compiler, target: &str) -> PathBuf {
self.sysroot(compiler).join("lib").join("rustlib")
.join(target).join("lib")
}
@ -417,20 +396,23 @@ impl Build {
/// stage when running with a particular host compiler.
///
/// The mode indicates what the root directory is for.
fn stage_out(&self, stage: u32, host: &str, mode: Mode) -> PathBuf {
fn stage_out(&self, compiler: &Compiler, mode: Mode) -> PathBuf {
let suffix = match mode {
Mode::Libstd => "-std",
_ => "-rustc",
};
self.out.join(host).join(format!("stage{}{}", stage, suffix))
self.out.join(compiler.host)
.join(format!("stage{}{}", compiler.stage, suffix))
}
/// Returns the root output directory for all Cargo output in a given stage,
/// running a particular comipler, wehther or not we're building the
/// standard library, and targeting the specified architecture.
fn cargo_out(&self, stage: u32, host: &str, mode: Mode,
fn cargo_out(&self,
compiler: &Compiler,
mode: Mode,
target: &str) -> PathBuf {
self.stage_out(stage, host, mode).join(target).join(self.cargo_dir())
self.stage_out(compiler, mode).join(target).join(self.cargo_dir())
}
/// Root output directory for LLVM compiled for `target`
@ -456,8 +438,7 @@ impl Build {
if compiler.is_snapshot(self) {
self.rustc_snapshot_libdir()
} else {
self.sysroot(compiler.stage, compiler.host)
.join(libdir(compiler.host))
self.sysroot(compiler).join(libdir(compiler.host))
}
}

View File

@ -29,18 +29,16 @@ macro_rules! targets {
// and one for the compiler itself. These are parameterized over the
// stage output they're going to be placed in along with the
// compiler which is producing the copy of libstd or librustc
(libstd, Libstd { stage: u32, compiler: Compiler<'a> }),
(librustc, Librustc { stage: u32, compiler: Compiler<'a> }),
(libstd, Libstd { compiler: Compiler<'a> }),
(librustc, Librustc { compiler: Compiler<'a> }),
// Links the standard library/librustc produced by the compiler
// provided into the host's directory also provided.
(libstd_link, LibstdLink {
stage: u32,
compiler: Compiler<'a>,
host: &'a str
}),
(librustc_link, LibrustcLink {
stage: u32,
compiler: Compiler<'a>,
host: &'a str
}),
@ -144,10 +142,9 @@ fn top_level(build: &Build) -> Vec<Step> {
}
let host = t.target(host);
if host.target == build.config.build {
targets.push(host.librustc(stage, host.compiler(stage)));
targets.push(host.librustc(host.compiler(stage)));
} else {
targets.push(host.librustc_link(stage, t.compiler(stage),
host.target));
targets.push(host.librustc_link(t.compiler(stage), host.target));
}
for target in build.config.target.iter() {
if !build.flags.target.contains(target) {
@ -156,11 +153,10 @@ fn top_level(build: &Build) -> Vec<Step> {
if host.target == build.config.build {
targets.push(host.target(target)
.libstd(stage, host.compiler(stage)));
.libstd(host.compiler(stage)));
} else {
targets.push(host.target(target)
.libstd_link(stage, t.compiler(stage),
host.target));
.libstd_link(t.compiler(stage), host.target));
}
}
}
@ -238,29 +234,29 @@ impl<'a> Step<'a> {
}
Source::Rustc { stage } => {
let compiler = Compiler::new(stage - 1, &build.config.build);
vec![self.librustc(stage - 1, compiler)]
vec![self.librustc(compiler)]
}
Source::Librustc { stage, compiler } => {
vec![self.libstd(stage, compiler), self.llvm(())]
Source::Librustc { compiler } => {
vec![self.libstd(compiler), self.llvm(())]
}
Source::Libstd { stage: _, compiler } => {
Source::Libstd { compiler } => {
vec![self.compiler_rt(()),
self.rustc(compiler.stage).target(compiler.host)]
}
Source::LibrustcLink { stage, compiler, host } => {
vec![self.librustc(stage, compiler),
self.libstd_link(stage, compiler, host)]
Source::LibrustcLink { compiler, host } => {
vec![self.librustc(compiler),
self.libstd_link(compiler, host)]
}
Source::LibstdLink { stage, compiler, host } => {
vec![self.libstd(stage, compiler),
self.target(host).rustc(stage)]
Source::LibstdLink { compiler, host } => {
vec![self.libstd(compiler),
self.target(host).rustc(compiler.stage)]
}
Source::CompilerRt { _dummy } => {
vec![self.llvm(()).target(&build.config.build)]
}
Source::Llvm { _dummy } => Vec::new(),
Source::DocStd { stage } => {
vec![self.libstd(stage, self.compiler(stage))]
vec![self.libstd(self.compiler(stage))]
}
Source::DocBook { stage } |
Source::DocNomicon { stage } |
@ -290,11 +286,11 @@ impl<'a> Step<'a> {
}
Source::ToolLinkchecker { stage } => {
vec![self.libstd(stage, self.compiler(stage))]
vec![self.libstd(self.compiler(stage))]
}
Source::ToolErrorIndex { stage } |
Source::ToolRustbook { stage } => {
vec![self.librustc(stage, self.compiler(stage))]
vec![self.librustc(self.compiler(stage))]
}
}
}

View File

@ -1,2 +1,2 @@
rustc: 2016-02-17
cargo: 2016-01-21
cargo: 2016-03-11

1
src/rustc/Cargo.lock generated
View File

@ -349,7 +349,6 @@ name = "test"
version = "0.0.0"
dependencies = [
"getopts 0.0.0",
"serialize 0.0.0",
"term 0.0.0",
]