Skip to content

Commit 75d141c

Browse files
committed
Create newtype for platform triple in bootstrap.
As I was reading through the bootstrap code, I noticed myself getting lost at times trying to find out what the value of each `&str` is. I also noticed there's quite a few platform-checking conditionals checking the contents of these strings. In an attempt to clarify and cleanup these cases, I created a 'newtype' `Triple` that represents a platform triple and moved the platform-checking logic to methods on the new struct.
1 parent 95abee1 commit 75d141c

File tree

13 files changed

+297
-151
lines changed

13 files changed

+297
-151
lines changed

src/bootstrap/cc.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use std::process::Command;
3636
use build_helper::{cc2ar, output};
3737
use gcc;
3838

39-
use Build;
39+
use {Build, Triple};
4040
use config::Target;
4141

4242
pub fn find(build: &mut Build) {
@@ -45,7 +45,7 @@ pub fn find(build: &mut Build) {
4545
for target in build.config.target.iter() {
4646
let mut cfg = gcc::Config::new();
4747
cfg.cargo_metadata(false).opt_level(0).debug(false)
48-
.target(target).host(&build.config.build);
48+
.target(&target.0).host(&build.config.build.0);
4949

5050
let config = build.config.target_config.get(target);
5151
if let Some(cc) = config.and_then(|c| c.cc.as_ref()) {
@@ -55,19 +55,19 @@ pub fn find(build: &mut Build) {
5555
}
5656

5757
let compiler = cfg.get_compiler();
58-
let ar = cc2ar(compiler.path(), target);
58+
let ar = cc2ar(compiler.path(), &target.0);
5959
build.verbose(&format!("CC_{} = {:?}", target, compiler.path()));
6060
if let Some(ref ar) = ar {
6161
build.verbose(&format!("AR_{} = {:?}", target, ar));
6262
}
63-
build.cc.insert(target.to_string(), (compiler, ar));
63+
build.cc.insert(target.clone(), (compiler, ar));
6464
}
6565

6666
// For all host triples we need to find a C++ compiler as well
6767
for host in build.config.host.iter() {
6868
let mut cfg = gcc::Config::new();
6969
cfg.cargo_metadata(false).opt_level(0).debug(false).cpp(true)
70-
.target(host).host(&build.config.build);
70+
.target(&host.0).host(&build.config.build.0);
7171
let config = build.config.target_config.get(host);
7272
if let Some(cxx) = config.and_then(|c| c.cxx.as_ref()) {
7373
cfg.compiler(cxx);
@@ -76,29 +76,29 @@ pub fn find(build: &mut Build) {
7676
}
7777
let compiler = cfg.get_compiler();
7878
build.verbose(&format!("CXX_{} = {:?}", host, compiler.path()));
79-
build.cxx.insert(host.to_string(), compiler);
79+
build.cxx.insert(host.clone(), compiler);
8080
}
8181
}
8282

8383
fn set_compiler(cfg: &mut gcc::Config,
8484
gnu_compiler: &str,
85-
target: &str,
85+
target: &Triple,
8686
config: Option<&Target>) {
8787
match target {
8888
// When compiling for android we may have the NDK configured in the
8989
// config.toml in which case we look there. Otherwise the default
9090
// compiler already takes into account the triple in question.
91-
t if t.contains("android") => {
91+
t if t.is_android() => {
9292
if let Some(ndk) = config.and_then(|c| c.ndk.as_ref()) {
93-
let target = target.replace("armv7", "arm");
93+
let target = target.0.replace("armv7", "arm");
9494
let compiler = format!("{}-{}", target, gnu_compiler);
9595
cfg.compiler(ndk.join("bin").join(compiler));
9696
}
9797
}
9898

9999
// The default gcc version from OpenBSD may be too old, try using egcc,
100100
// which is a gcc version from ports, if this is the case.
101-
t if t.contains("openbsd") => {
101+
t if t.is_openbsd() => {
102102
let c = cfg.get_compiler();
103103
if !c.path().ends_with(gnu_compiler) {
104104
return

src/bootstrap/check.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::process::Command;
2121

2222
use build_helper::output;
2323

24-
use {Build, Compiler, Mode};
24+
use {Build, Compiler, Mode, Triple};
2525
use util::{self, dylib_path, dylib_path_var};
2626

2727
const ADB_TEST_DIR: &'static str = "/data/tmp";
@@ -30,7 +30,7 @@ const ADB_TEST_DIR: &'static str = "/data/tmp";
3030
///
3131
/// This tool in `src/tools` will verify the validity of all our links in the
3232
/// documentation to ensure we don't have a bunch of dead ones.
33-
pub fn linkcheck(build: &Build, stage: u32, host: &str) {
33+
pub fn linkcheck(build: &Build, stage: u32, host: &Triple) {
3434
println!("Linkcheck stage{} ({})", stage, host);
3535
let compiler = Compiler::new(stage, host);
3636
build.run(build.tool_cmd(&compiler, "linkchecker")
@@ -41,7 +41,7 @@ pub fn linkcheck(build: &Build, stage: u32, host: &str) {
4141
///
4242
/// This tool in `src/tools` will check out a few Rust projects and run `cargo
4343
/// test` to ensure that we don't regress the test suites there.
44-
pub fn cargotest(build: &Build, stage: u32, host: &str) {
44+
pub fn cargotest(build: &Build, stage: u32, host: &Triple) {
4545
let ref compiler = Compiler::new(stage, host);
4646

4747
// Configure PATH to find the right rustc. NB. we have to use PATH
@@ -69,14 +69,14 @@ pub fn cargotest(build: &Build, stage: u32, host: &str) {
6969
/// This tool in `src/tools` checks up on various bits and pieces of style and
7070
/// otherwise just implements a few lint-like checks that are specific to the
7171
/// compiler itself.
72-
pub fn tidy(build: &Build, stage: u32, host: &str) {
72+
pub fn tidy(build: &Build, stage: u32, host: &Triple) {
7373
println!("tidy check stage{} ({})", stage, host);
7474
let compiler = Compiler::new(stage, host);
7575
build.run(build.tool_cmd(&compiler, "tidy")
7676
.arg(build.src.join("src")));
7777
}
7878

79-
fn testdir(build: &Build, host: &str) -> PathBuf {
79+
fn testdir(build: &Build, host: &Triple) -> PathBuf {
8080
build.out.join(host).join("test")
8181
}
8282

@@ -87,7 +87,7 @@ fn testdir(build: &Build, host: &str) -> PathBuf {
8787
/// "run-pass" or `suite` can be something like `debuginfo`.
8888
pub fn compiletest(build: &Build,
8989
compiler: &Compiler,
90-
target: &str,
90+
target: &Triple,
9191
mode: &str,
9292
suite: &str) {
9393
println!("Check compiletest {} ({} -> {})", suite, compiler.host, target);
@@ -130,7 +130,7 @@ pub fn compiletest(build: &Build,
130130
let python_default = "python";
131131
cmd.arg("--docck-python").arg(python_default);
132132

133-
if build.config.build.ends_with("apple-darwin") {
133+
if build.config.build.is_apple_darwin() {
134134
// Force /usr/bin/python on OSX for LLDB tests because we're loading the
135135
// LLDB plugin's compiled module which only works with the system python
136136
// (namely not Homebrew-installed python)
@@ -178,7 +178,7 @@ pub fn compiletest(build: &Build,
178178

179179
// Running a C compiler on MSVC requires a few env vars to be set, to be
180180
// sure to set them here.
181-
if target.contains("msvc") {
181+
if target.is_msvc() {
182182
for &(ref k, ref v) in build.cc[target].0.env() {
183183
if k != "PATH" {
184184
cmd.env(k, v);
@@ -189,7 +189,7 @@ pub fn compiletest(build: &Build,
189189

190190
cmd.arg("--adb-path").arg("adb");
191191
cmd.arg("--adb-test-dir").arg(ADB_TEST_DIR);
192-
if target.contains("android") {
192+
if target.is_android() {
193193
// Assume that cc for this target comes from the android sysroot
194194
cmd.arg("--android-cross-path")
195195
.arg(build.cc(target).parent().unwrap().parent().unwrap());
@@ -262,7 +262,7 @@ fn markdown_test(build: &Build, compiler: &Compiler, markdown: &Path) {
262262
/// arguments, and those arguments are discovered from `Cargo.lock`.
263263
pub fn krate(build: &Build,
264264
compiler: &Compiler,
265-
target: &str,
265+
target: &Triple,
266266
mode: Mode) {
267267
let (name, path, features) = match mode {
268268
Mode::Libstd => ("libstd", "src/rustc/std_shim", build.std_features()),
@@ -320,7 +320,7 @@ pub fn krate(build: &Build,
320320
dylib_path.insert(0, build.sysroot_libdir(compiler, target));
321321
cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
322322

323-
if target.contains("android") {
323+
if target.is_android() {
324324
build.run(cargo.arg("--no-run"));
325325
krate_android(build, compiler, target, mode);
326326
} else {
@@ -331,7 +331,7 @@ pub fn krate(build: &Build,
331331

332332
fn krate_android(build: &Build,
333333
compiler: &Compiler,
334-
target: &str,
334+
target: &Triple,
335335
mode: Mode) {
336336
let mut tests = Vec::new();
337337
let out_dir = build.cargo_out(compiler, mode, target);
@@ -372,24 +372,24 @@ fn krate_android(build: &Build,
372372
}
373373

374374
fn find_tests(dir: &Path,
375-
target: &str,
375+
target: &Triple,
376376
dst: &mut Vec<PathBuf>) {
377377
for e in t!(dir.read_dir()).map(|e| t!(e)) {
378378
let file_type = t!(e.file_type());
379379
if !file_type.is_file() {
380380
continue
381381
}
382382
let filename = e.file_name().into_string().unwrap();
383-
if (target.contains("windows") && filename.ends_with(".exe")) ||
384-
(!target.contains("windows") && !filename.contains(".")) {
383+
if (target.is_windows() && filename.ends_with(".exe")) ||
384+
(!target.is_windows() && !filename.contains(".")) {
385385
dst.push(e.path());
386386
}
387387
}
388388
}
389389

390390
pub fn android_copy_libs(build: &Build,
391391
compiler: &Compiler,
392-
target: &str) {
392+
target: &Triple) {
393393
println!("Android copy libs to emulator ({})", target);
394394
build.run(Command::new("adb").arg("remount"));
395395
build.run(Command::new("adb").args(&["shell", "rm", "-r", ADB_TEST_DIR]));

src/bootstrap/compile.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ use build_helper::output;
2626
use filetime::FileTime;
2727

2828
use util::{exe, staticlib, libdir, mtime, is_dylib, copy};
29-
use {Build, Compiler, Mode};
29+
use {Build, Compiler, Mode, Triple};
3030

3131
/// Build the standard library.
3232
///
3333
/// This will build the standard library for a particular stage of the build
3434
/// using the `compiler` targeting the `target` architecture. The artifacts
3535
/// created will also be linked into the sysroot directory.
36-
pub fn std<'a>(build: &'a Build, target: &str, compiler: &Compiler<'a>) {
36+
pub fn std<'a>(build: &'a Build, target: &Triple, compiler: &Compiler<'a>) {
3737
println!("Building stage{} std artifacts ({} -> {})", compiler.stage,
3838
compiler.host, target);
3939

@@ -71,7 +71,7 @@ pub fn std<'a>(build: &'a Build, target: &str, compiler: &Compiler<'a>) {
7171
cargo.env("JEMALLOC_OVERRIDE", jemalloc);
7272
}
7373
}
74-
if target.contains("musl") {
74+
if target.is_musl() {
7575
if let Some(p) = build.musl_root(target) {
7676
cargo.env("MUSL_ROOT", p);
7777
}
@@ -87,9 +87,9 @@ pub fn std<'a>(build: &'a Build, target: &str, compiler: &Compiler<'a>) {
8787
/// Links those artifacts generated in the given `stage` for `target` produced
8888
/// by `compiler` into `host`'s sysroot.
8989
pub fn std_link(build: &Build,
90-
target: &str,
90+
target: &Triple,
9191
compiler: &Compiler,
92-
host: &str) {
92+
host: &Triple) {
9393
let target_compiler = Compiler::new(compiler.stage, host);
9494
let libdir = build.sysroot_libdir(&target_compiler, target);
9595
let out_dir = build.cargo_out(compiler, Mode::Libstd, target);
@@ -103,7 +103,7 @@ pub fn std_link(build: &Build,
103103
}
104104
add_to_sysroot(&out_dir, &libdir);
105105

106-
if target.contains("musl") && !target.contains("mips") {
106+
if target.is_musl() && !target.is_mips() {
107107
copy_musl_third_party_objects(build, &libdir);
108108
}
109109
}
@@ -123,8 +123,8 @@ fn copy_musl_third_party_objects(build: &Build, into: &Path) {
123123
/// They don't require any library support as they're just plain old object
124124
/// files, so we just use the nightly snapshot compiler to always build them (as
125125
/// no other compilers are guaranteed to be available).
126-
fn build_startup_objects(build: &Build, target: &str, into: &Path) {
127-
if !target.contains("pc-windows-gnu") {
126+
fn build_startup_objects(build: &Build, target: &Triple, into: &Path) {
127+
if !target.is_pc_windows_gnu() {
128128
return
129129
}
130130
let compiler = Compiler::new(0, &build.config.build);
@@ -150,7 +150,8 @@ fn build_startup_objects(build: &Build, target: &str, into: &Path) {
150150
/// This will build libtest and supporting libraries for a particular stage of
151151
/// the build using the `compiler` targeting the `target` architecture. The
152152
/// artifacts created will also be linked into the sysroot directory.
153-
pub fn test<'a>(build: &'a Build, target: &str, compiler: &Compiler<'a>) {
153+
pub fn test<'a>(build: &'a Build, target: &Triple,
154+
compiler: &Compiler<'a>) {
154155
println!("Building stage{} test artifacts ({} -> {})", compiler.stage,
155156
compiler.host, target);
156157
let out_dir = build.cargo_out(compiler, Mode::Libtest, target);
@@ -168,9 +169,9 @@ pub fn test<'a>(build: &'a Build, target: &str, compiler: &Compiler<'a>) {
168169
/// Links those artifacts generated in the given `stage` for `target` produced
169170
/// by `compiler` into `host`'s sysroot.
170171
pub fn test_link(build: &Build,
171-
target: &str,
172+
target: &Triple,
172173
compiler: &Compiler,
173-
host: &str) {
174+
host: &Triple) {
174175
let target_compiler = Compiler::new(compiler.stage, host);
175176
let libdir = build.sysroot_libdir(&target_compiler, target);
176177
let out_dir = build.cargo_out(compiler, Mode::Libtest, target);
@@ -182,7 +183,7 @@ pub fn test_link(build: &Build,
182183
/// This will build the compiler for a particular stage of the build using
183184
/// the `compiler` targeting the `target` architecture. The artifacts
184185
/// created will also be linked into the sysroot directory.
185-
pub fn rustc<'a>(build: &'a Build, target: &str, compiler: &Compiler<'a>) {
186+
pub fn rustc<'a>(build: &'a Build, target: &Triple, compiler: &Compiler<'a>) {
186187
println!("Building stage{} compiler artifacts ({} -> {})",
187188
compiler.stage, compiler.host, target);
188189

@@ -241,9 +242,9 @@ pub fn rustc<'a>(build: &'a Build, target: &str, compiler: &Compiler<'a>) {
241242
/// Links those artifacts generated in the given `stage` for `target` produced
242243
/// by `compiler` into `host`'s sysroot.
243244
pub fn rustc_link(build: &Build,
244-
target: &str,
245+
target: &Triple,
245246
compiler: &Compiler,
246-
host: &str) {
247+
host: &Triple) {
247248
let target_compiler = Compiler::new(compiler.stage, host);
248249
let libdir = build.sysroot_libdir(&target_compiler, target);
249250
let out_dir = build.cargo_out(compiler, Mode::Librustc, target);
@@ -252,13 +253,15 @@ pub fn rustc_link(build: &Build,
252253

253254
/// Cargo's output path for the standard library in a given stage, compiled
254255
/// by a particular compiler for the specified target.
255-
fn libstd_stamp(build: &Build, compiler: &Compiler, target: &str) -> PathBuf {
256+
fn libstd_stamp(build: &Build, compiler: &Compiler,
257+
target: &Triple) -> PathBuf {
256258
build.cargo_out(compiler, Mode::Libstd, target).join(".libstd.stamp")
257259
}
258260

259261
/// Cargo's output path for libtest in a given stage, compiled by a particular
260262
/// compiler for the specified target.
261-
fn libtest_stamp(build: &Build, compiler: &Compiler, target: &str) -> PathBuf {
263+
fn libtest_stamp(build: &Build, compiler: &Compiler,
264+
target: &Triple) -> PathBuf {
262265
build.cargo_out(compiler, Mode::Libtest, target).join(".libtest.stamp")
263266
}
264267

@@ -273,7 +276,7 @@ fn compiler_file(compiler: &Path, file: &str) -> PathBuf {
273276
/// This will assemble a compiler in `build/$host/stage$stage`. The compiler
274277
/// must have been previously produced by the `stage - 1` build.config.build
275278
/// compiler.
276-
pub fn assemble_rustc(build: &Build, stage: u32, host: &str) {
279+
pub fn assemble_rustc(build: &Build, stage: u32, host: &Triple) {
277280
assert!(stage > 0, "the stage0 compiler isn't assembled, it's downloaded");
278281
// The compiler that we're assembling
279282
let target_compiler = Compiler::new(stage, host);
@@ -360,7 +363,7 @@ fn add_to_sysroot(out_dir: &Path, sysroot_dst: &Path) {
360363
///
361364
/// This will build the specified tool with the specified `host` compiler in
362365
/// `stage` into the normal cargo output directory.
363-
pub fn tool(build: &Build, stage: u32, host: &str, tool: &str) {
366+
pub fn tool(build: &Build, stage: u32, host: &Triple, tool: &str) {
364367
println!("Building stage{} tool {} ({})", stage, tool, host);
365368

366369
let compiler = Compiler::new(stage, host);

0 commit comments

Comments
 (0)