Skip to content

Commit 0788cd2

Browse files
committed
rustbuild: Use an enum to indicate destination
Instead of using a `is_std: bool`, instead use a more well-typed and self-documenting enum to indicate the mode in which Cargo is being invoked.
1 parent ee6df13 commit 0788cd2

File tree

3 files changed

+61
-43
lines changed

3 files changed

+61
-43
lines changed

src/bootstrap/build/compile.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::process::Command;
1616
use build_helper::output;
1717

1818
use build::util::{exe, staticlib, libdir, mtime, is_dylib};
19-
use build::{Build, Compiler};
19+
use build::{Build, Compiler, Mode};
2020

2121
/// Build the standard library.
2222
///
@@ -39,9 +39,10 @@ pub fn std<'a>(build: &'a Build, stage: u32, target: &str,
3939

4040
build_startup_objects(build, target, &libdir);
4141

42-
let out_dir = build.cargo_out(stage, &host, true, target);
42+
let out_dir = build.cargo_out(stage, &host, Mode::Libstd, target);
4343
build.clear_if_dirty(&out_dir, &build.compiler_path(compiler));
44-
let mut cargo = build.cargo(stage, compiler, true, target, "build");
44+
let mut cargo = build.cargo(stage, compiler, Mode::Libstd, Some(target),
45+
"build");
4546
cargo.arg("--features").arg(build.std_features())
4647
.arg("--manifest-path")
4748
.arg(build.src.join("src/rustc/std_shim/Cargo.toml"));
@@ -71,7 +72,7 @@ pub fn std_link(build: &Build,
7172
compiler: &Compiler,
7273
host: &str) {
7374
let libdir = build.sysroot_libdir(stage, host, target);
74-
let out_dir = build.cargo_out(stage, compiler.host, true, target);
75+
let out_dir = build.cargo_out(stage, compiler.host, Mode::Libstd, target);
7576

7677
// If we're linking one compiler host's output into another, then we weren't
7778
// called from the `std` method above. In that case we clean out what's
@@ -135,10 +136,11 @@ pub fn rustc<'a>(build: &'a Build, stage: u32, target: &str,
135136
println!("Building stage{} compiler artifacts ({} -> {})", stage,
136137
host, target);
137138

138-
let out_dir = build.cargo_out(stage, &host, false, target);
139+
let out_dir = build.cargo_out(stage, &host, Mode::Librustc, target);
139140
build.clear_if_dirty(&out_dir, &libstd_shim(build, stage, &host, target));
140141

141-
let mut cargo = build.cargo(stage, compiler, false, target, "build");
142+
let mut cargo = build.cargo(stage, compiler, Mode::Librustc, Some(target),
143+
"build");
142144
cargo.arg("--features").arg(build.rustc_features(stage))
143145
.arg("--manifest-path")
144146
.arg(build.src.join("src/rustc/Cargo.toml"));
@@ -200,14 +202,14 @@ pub fn rustc_link(build: &Build,
200202
compiler: &Compiler,
201203
host: &str) {
202204
let libdir = build.sysroot_libdir(stage, host, target);
203-
let out_dir = build.cargo_out(stage, compiler.host, false, target);
205+
let out_dir = build.cargo_out(stage, compiler.host, Mode::Librustc, target);
204206
add_to_sysroot(&out_dir, &libdir);
205207
}
206208

207209
/// Cargo's output path for the standard library in a given stage, compiled
208210
/// by a particular compiler for the specified target.
209211
fn libstd_shim(build: &Build, stage: u32, host: &str, target: &str) -> PathBuf {
210-
build.cargo_out(stage, host, true, target).join("libstd_shim.rlib")
212+
build.cargo_out(stage, host, Mode::Libstd, target).join("libstd_shim.rlib")
211213
}
212214

213215
fn compiler_file(compiler: &Path, file: &str) -> String {
@@ -239,7 +241,8 @@ pub fn assemble_rustc(build: &Build, stage: u32, host: &str) {
239241
}
240242
}
241243

242-
let out_dir = build.cargo_out(stage - 1, &build.config.build, false, host);
244+
let out_dir = build.cargo_out(stage - 1, &build.config.build,
245+
Mode::Librustc, host);
243246

244247
// Link the compiler binary itself into place
245248
let rustc = out_dir.join(exe("rustc", host));

src/bootstrap/build/doc.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::path::Path;
1212
use std::fs::{self, File};
1313
use std::io::prelude::*;
1414

15-
use build::{Build, Compiler};
15+
use build::{Build, Compiler, Mode};
1616
use build::util::{up_to_date, cp_r};
1717

1818
pub fn rustbook(build: &Build, stage: u32, host: &str, name: &str, out: &Path) {
@@ -106,14 +106,14 @@ pub fn standalone(build: &Build, stage: u32, host: &str, out: &Path) {
106106
pub fn std(build: &Build, stage: u32, host: &str, out: &Path) {
107107
println!("Documenting stage{} std ({})", stage, host);
108108
let compiler = Compiler::new(stage, host);
109-
let out_dir = build.stage_out(stage, host, true)
109+
let out_dir = build.stage_out(stage, host, Mode::Libstd)
110110
.join(host).join("doc");
111111
let rustdoc = build.tool(&compiler, "rustdoc");
112112
if !up_to_date(&rustdoc, &out_dir.join("std/index.html")) {
113113
t!(fs::remove_dir_all(&out_dir));
114114
}
115115

116-
let mut cargo = build.cargo(stage, &compiler, true, host,
116+
let mut cargo = build.cargo(stage, &compiler, Mode::Libstd, Some(host),
117117
"doc");
118118
cargo.arg("--manifest-path")
119119
.arg(build.src.join("src/rustc/std_shim/Cargo.toml"))
@@ -125,13 +125,13 @@ pub fn std(build: &Build, stage: u32, host: &str, out: &Path) {
125125
pub fn rustc(build: &Build, stage: u32, host: &str, out: &Path) {
126126
println!("Documenting stage{} compiler ({})", stage, host);
127127
let compiler = Compiler::new(stage, host);
128-
let out_dir = build.stage_out(stage, host, false)
128+
let out_dir = build.stage_out(stage, host, Mode::Librustc)
129129
.join(host).join("doc");
130130
let rustdoc = build.tool(&compiler, "rustdoc");
131131
if !up_to_date(&rustdoc, &out_dir.join("rustc/index.html")) {
132132
t!(fs::remove_dir_all(&out_dir));
133133
}
134-
let mut cargo = build.cargo(stage, &compiler, false, host,
134+
let mut cargo = build.cargo(stage, &compiler, Mode::Librustc, Some(host),
135135
"doc");
136136
cargo.arg("--manifest-path")
137137
.arg(build.src.join("src/rustc/Cargo.toml"))

src/bootstrap/build/mod.rs

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ pub struct Build {
8383
compiler_rt_built: RefCell<HashMap<String, PathBuf>>,
8484
}
8585

86+
pub enum Mode {
87+
Libstd,
88+
Librustc,
89+
Tool,
90+
}
91+
8692
impl Build {
8793
pub fn new(flags: Flags, config: Config) -> Build {
8894
let cwd = t!(env::current_dir());
@@ -241,14 +247,17 @@ impl Build {
241247
/// Cargo for the specified stage, whether or not the standard library is
242248
/// being built, and using the specified compiler targeting `target`.
243249
// FIXME: aren't stage/compiler duplicated?
244-
fn cargo(&self, stage: u32, compiler: &Compiler, is_std: bool,
245-
target: &str, cmd: &str) -> Command {
250+
fn cargo(&self,
251+
stage: u32,
252+
compiler: &Compiler,
253+
mode: Mode,
254+
target: Option<&str>,
255+
cmd: &str) -> Command {
246256
let mut cargo = Command::new(&self.cargo);
247257
let host = compiler.host;
248-
let out_dir = self.stage_out(stage, host, is_std);
258+
let out_dir = self.stage_out(stage, host, mode);
249259
cargo.env("CARGO_TARGET_DIR", out_dir)
250260
.arg(cmd)
251-
.arg("--target").arg(target)
252261
.arg("-j").arg(self.jobs().to_string());
253262

254263
// Customize the compiler we're running. Specify the compiler to cargo
@@ -265,24 +274,28 @@ impl Build {
265274
.env("RUSTC_SNAPSHOT", &self.rustc)
266275
.env("RUSTC_SYSROOT", self.sysroot(stage, host))
267276
.env("RUSTC_SNAPSHOT_LIBDIR", self.rustc_snapshot_libdir())
268-
.env("RUSTC_FLAGS", self.rustc_flags(target).join(" "))
269277
.env("RUSTC_RPATH", self.config.rust_rpath.to_string())
270278
.env("RUSTDOC", self.tool(compiler, "rustdoc"));
271279

272-
// Specify some variuos options for build scripts used throughout the
273-
// build.
274-
//
275-
// FIXME: the guard against msvc shouldn't need to be here
276-
if !target.contains("msvc") {
277-
cargo.env(format!("CC_{}", target), self.cc(target))
278-
.env(format!("AR_{}", target), self.ar(target))
279-
.env(format!("CFLAGS_{}", target), self.cflags(target));
280-
}
280+
if let Some(target) = target {
281+
cargo.env("RUSTC_FLAGS", self.rustc_flags(target).join(" "));
282+
cargo.arg("--target").arg(target);
283+
284+
// Specify some various options for build scripts used throughout
285+
// the build.
286+
//
287+
// FIXME: the guard against msvc shouldn't need to be here
288+
if !target.contains("msvc") {
289+
cargo.env(format!("CC_{}", target), self.cc(target))
290+
.env(format!("AR_{}", target), self.ar(target))
291+
.env(format!("CFLAGS_{}", target), self.cflags(target));
292+
}
281293

282-
// Environment variables *required* needed throughout the build
283-
//
284-
// FIXME: should update code to not require this env vars
285-
cargo.env("CFG_COMPILER_HOST_TRIPLE", target);
294+
// Environment variables *required* needed throughout the build
295+
//
296+
// FIXME: should update code to not require this env vars
297+
cargo.env("CFG_COMPILER_HOST_TRIPLE", target);
298+
}
286299

287300
if self.config.verbose || self.flags.verbose {
288301
cargo.arg("-v");
@@ -306,7 +319,7 @@ impl Build {
306319

307320
/// Get the specified tool next to the specified compiler
308321
fn tool(&self, compiler: &Compiler, tool: &str) -> PathBuf {
309-
self.stage_out(compiler.stage, compiler.host, false)
322+
self.stage_out(compiler.stage, compiler.host, Mode::Tool)
310323
.join(self.cargo_dir())
311324
.join(exe(tool, compiler.host))
312325
}
@@ -319,8 +332,8 @@ impl Build {
319332
let host = compiler.host;
320333
let stage = compiler.stage;
321334
let paths = vec![
322-
self.cargo_out(stage, host, true, host).join("deps"),
323-
self.cargo_out(stage, host, false, host).join("deps"),
335+
self.cargo_out(stage, host, Mode::Libstd, host).join("deps"),
336+
self.cargo_out(stage, host, Mode::Librustc, host).join("deps"),
324337
];
325338
add_lib_path(paths, &mut cmd);
326339
return cmd
@@ -363,7 +376,7 @@ impl Build {
363376

364377
fn sysroot(&self, stage: u32, host: &str) -> PathBuf {
365378
if stage == 0 {
366-
self.stage_out(stage, host, false)
379+
self.stage_out(stage, host, Mode::Librustc)
367380
} else {
368381
self.out.join(host).join(format!("stage{}", stage))
369382
}
@@ -377,19 +390,21 @@ impl Build {
377390
/// Returns the root directory for all output generated in a particular
378391
/// stage when running with a particular host compiler.
379392
///
380-
/// The `is_std` flag indicates whether the root directory is for the
381-
/// bootstrap of the standard library or for the compiler.
382-
fn stage_out(&self, stage: u32, host: &str, is_std: bool) -> PathBuf {
383-
self.out.join(host)
384-
.join(format!("stage{}{}", stage, if is_std {"-std"} else {"-rustc"}))
393+
/// The mode indicates what the root directory is for.
394+
fn stage_out(&self, stage: u32, host: &str, mode: Mode) -> PathBuf {
395+
let suffix = match mode {
396+
Mode::Libstd => "-std",
397+
_ => "-rustc",
398+
};
399+
self.out.join(host).join(format!("stage{}{}", stage, suffix))
385400
}
386401

387402
/// Returns the root output directory for all Cargo output in a given stage,
388403
/// running a particular comipler, wehther or not we're building the
389404
/// standard library, and targeting the specified architecture.
390-
fn cargo_out(&self, stage: u32, host: &str, is_std: bool,
405+
fn cargo_out(&self, stage: u32, host: &str, mode: Mode,
391406
target: &str) -> PathBuf {
392-
self.stage_out(stage, host, is_std).join(target).join(self.cargo_dir())
407+
self.stage_out(stage, host, mode).join(target).join(self.cargo_dir())
393408
}
394409

395410
/// Root output directory for LLVM compiled for `target`

0 commit comments

Comments
 (0)