Skip to content

Commit 0b6766d

Browse files
committed
Allow adding RUSTFLAGS after Builder::cargo
This commit changes the return type of `Builder::cargo` to return a builder that allows dynamically adding more `RUSTFLAGS` values after-the-fact. While not used yet, this will later be used to delete more of `rustc.rs`
1 parent 5cc6eb4 commit 0b6766d

File tree

6 files changed

+95
-59
lines changed

6 files changed

+95
-59
lines changed

src/bootstrap/builder.rs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::cell::{Cell, RefCell};
33
use std::collections::BTreeSet;
44
use std::collections::HashMap;
55
use std::env;
6+
use std::ffi::OsStr;
67
use std::fmt::Debug;
78
use std::fs;
89
use std::hash::Hash;
@@ -682,15 +683,15 @@ impl<'a> Builder<'a> {
682683

683684
/// Adds the compiler's directory of dynamic libraries to `cmd`'s dynamic
684685
/// library lookup path.
685-
pub fn add_rustc_lib_path(&self, compiler: Compiler, cmd: &mut Command) {
686+
pub fn add_rustc_lib_path(&self, compiler: Compiler, cmd: &mut Cargo) {
686687
// Windows doesn't need dylib path munging because the dlls for the
687688
// compiler live next to the compiler and the system will find them
688689
// automatically.
689690
if cfg!(windows) {
690691
return;
691692
}
692693

693-
add_lib_path(vec![self.rustc_libdir(compiler)], cmd);
694+
add_lib_path(vec![self.rustc_libdir(compiler)], &mut cmd.command);
694695
}
695696

696697
/// Gets a path to the compiler specified.
@@ -752,7 +753,7 @@ impl<'a> Builder<'a> {
752753
mode: Mode,
753754
target: Interned<String>,
754755
cmd: &str,
755-
) -> Command {
756+
) -> Cargo {
756757
let mut cargo = Command::new(&self.initial_cargo);
757758
let out_dir = self.stage_out(compiler, mode);
758759

@@ -1225,9 +1226,10 @@ impl<'a> Builder<'a> {
12251226

12261227
self.ci_env.force_coloring_in_ci(&mut cargo);
12271228

1228-
cargo.env("RUSTFLAGS", &rustflags.0);
1229-
1230-
cargo
1229+
Cargo {
1230+
command: cargo,
1231+
rustflags,
1232+
}
12311233
}
12321234

12331235
/// Ensure that a given step is built, returning its output. This will
@@ -1328,6 +1330,7 @@ impl<'a> Builder<'a> {
13281330
#[cfg(test)]
13291331
mod tests;
13301332

1333+
#[derive(Debug)]
13311334
struct Rustflags(String);
13321335

13331336
impl Rustflags {
@@ -1364,3 +1367,37 @@ impl Rustflags {
13641367
self
13651368
}
13661369
}
1370+
1371+
#[derive(Debug)]
1372+
pub struct Cargo {
1373+
command: Command,
1374+
rustflags: Rustflags,
1375+
}
1376+
1377+
impl Cargo {
1378+
pub fn arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Cargo {
1379+
self.command.arg(arg.as_ref());
1380+
self
1381+
}
1382+
1383+
pub fn args<I, S>(&mut self, args: I) -> &mut Cargo
1384+
where I: IntoIterator<Item=S>, S: AsRef<OsStr>
1385+
{
1386+
for arg in args {
1387+
self.arg(arg.as_ref());
1388+
}
1389+
self
1390+
}
1391+
1392+
pub fn env(&mut self, key: impl AsRef<OsStr>, value: impl AsRef<OsStr>) -> &mut Cargo {
1393+
self.command.env(key.as_ref(), value.as_ref());
1394+
self
1395+
}
1396+
}
1397+
1398+
impl From<Cargo> for Command {
1399+
fn from(mut cargo: Cargo) -> Command {
1400+
cargo.command.env("RUSTFLAGS", &cargo.rustflags.0);
1401+
cargo.command
1402+
}
1403+
}

src/bootstrap/check.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl Step for Std {
5252

5353
builder.info(&format!("Checking std artifacts ({} -> {})", &compiler.host, target));
5454
run_cargo(builder,
55-
&mut cargo,
55+
cargo,
5656
args(builder.kind),
5757
&libstd_stamp(builder, compiler, target),
5858
true);
@@ -100,7 +100,7 @@ impl Step for Rustc {
100100

101101
builder.info(&format!("Checking compiler artifacts ({} -> {})", &compiler.host, target));
102102
run_cargo(builder,
103-
&mut cargo,
103+
cargo,
104104
args(builder.kind),
105105
&librustc_stamp(builder, compiler, target),
106106
true);
@@ -152,7 +152,7 @@ impl Step for CodegenBackend {
152152
// We won't build LLVM if it's not available, as it shouldn't affect `check`.
153153

154154
run_cargo(builder,
155-
&mut cargo,
155+
cargo,
156156
args(builder.kind),
157157
&codegen_backend_stamp(builder, compiler, target, backend),
158158
true);
@@ -185,18 +185,18 @@ impl Step for Rustdoc {
185185

186186
builder.ensure(Rustc { target });
187187

188-
let mut cargo = prepare_tool_cargo(builder,
189-
compiler,
190-
Mode::ToolRustc,
191-
target,
192-
cargo_subcommand(builder.kind),
193-
"src/tools/rustdoc",
194-
SourceType::InTree,
195-
&[]);
188+
let cargo = prepare_tool_cargo(builder,
189+
compiler,
190+
Mode::ToolRustc,
191+
target,
192+
cargo_subcommand(builder.kind),
193+
"src/tools/rustdoc",
194+
SourceType::InTree,
195+
&[]);
196196

197197
println!("Checking rustdoc artifacts ({} -> {})", &compiler.host, target);
198198
run_cargo(builder,
199-
&mut cargo,
199+
cargo,
200200
args(builder.kind),
201201
&rustdoc_stamp(builder, compiler, target),
202202
true);

src/bootstrap/compile.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use serde::Deserialize;
2121
use serde_json;
2222

2323
use crate::dist;
24+
use crate::builder::Cargo;
2425
use crate::util::{exe, is_dylib};
2526
use crate::{Compiler, Mode, GitRepo};
2627
use crate::native;
@@ -98,7 +99,7 @@ impl Step for Std {
9899
builder.info(&format!("Building stage{} std artifacts ({} -> {})", compiler.stage,
99100
&compiler.host, target));
100101
run_cargo(builder,
101-
&mut cargo,
102+
cargo,
102103
vec![],
103104
&libstd_stamp(builder, compiler, target),
104105
false);
@@ -156,7 +157,7 @@ fn copy_third_party_objects(builder: &Builder<'_>, compiler: &Compiler, target:
156157
pub fn std_cargo(builder: &Builder<'_>,
157158
compiler: &Compiler,
158159
target: Interned<String>,
159-
cargo: &mut Command) {
160+
cargo: &mut Cargo) {
160161
if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") {
161162
cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
162163
}
@@ -430,7 +431,7 @@ impl Step for Rustc {
430431
builder.info(&format!("Building stage{} compiler artifacts ({} -> {})",
431432
compiler.stage, &compiler.host, target));
432433
run_cargo(builder,
433-
&mut cargo,
434+
cargo,
434435
vec![],
435436
&librustc_stamp(builder, compiler, target),
436437
false);
@@ -443,14 +444,14 @@ impl Step for Rustc {
443444
}
444445
}
445446

446-
pub fn rustc_cargo(builder: &Builder<'_>, cargo: &mut Command) {
447+
pub fn rustc_cargo(builder: &Builder<'_>, cargo: &mut Cargo) {
447448
cargo.arg("--features").arg(builder.rustc_features())
448449
.arg("--manifest-path")
449450
.arg(builder.src.join("src/rustc/Cargo.toml"));
450451
rustc_cargo_env(builder, cargo);
451452
}
452453

453-
pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Command) {
454+
pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo) {
454455
// Set some configuration variables picked up by build scripts and
455456
// the compiler alike
456457
cargo.env("CFG_RELEASE", builder.rust_release())
@@ -577,14 +578,11 @@ impl Step for CodegenBackend {
577578
rustc_cargo_env(builder, &mut cargo);
578579

579580
let features = build_codegen_backend(&builder, &mut cargo, &compiler, target, backend);
581+
cargo.arg("--features").arg(features);
580582

581583
let tmp_stamp = out_dir.join(".tmp.stamp");
582584

583-
let files = run_cargo(builder,
584-
cargo.arg("--features").arg(features),
585-
vec![],
586-
&tmp_stamp,
587-
false);
585+
let files = run_cargo(builder, cargo, vec![], &tmp_stamp, false);
588586
if builder.config.dry_run {
589587
return;
590588
}
@@ -609,7 +607,7 @@ impl Step for CodegenBackend {
609607
}
610608

611609
pub fn build_codegen_backend(builder: &Builder<'_>,
612-
cargo: &mut Command,
610+
cargo: &mut Cargo,
613611
compiler: &Compiler,
614612
target: Interned<String>,
615613
backend: Interned<String>) -> String {
@@ -949,7 +947,7 @@ pub fn add_to_sysroot(
949947
}
950948

951949
pub fn run_cargo(builder: &Builder<'_>,
952-
cargo: &mut Command,
950+
cargo: Cargo,
953951
tail_args: Vec<String>,
954952
stamp: &Path,
955953
is_check: bool)
@@ -1081,10 +1079,11 @@ pub fn run_cargo(builder: &Builder<'_>,
10811079

10821080
pub fn stream_cargo(
10831081
builder: &Builder<'_>,
1084-
cargo: &mut Command,
1082+
cargo: Cargo,
10851083
tail_args: Vec<String>,
10861084
cb: &mut dyn FnMut(CargoMessage<'_>),
10871085
) -> bool {
1086+
let mut cargo = Command::from(cargo);
10881087
if builder.config.dry_run {
10891088
return true;
10901089
}

src/bootstrap/doc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ impl Step for Std {
475475
.arg("--resource-suffix").arg(crate::channel::CFG_RELEASE_NUM)
476476
.arg("--index-page").arg(&builder.src.join("src/doc/index.md"));
477477

478-
builder.run(&mut cargo);
478+
builder.run(&mut cargo.into());
479479
};
480480
for krate in &["alloc", "core", "std", "proc_macro", "test"] {
481481
run_cargo_rustdoc_for(krate);
@@ -561,7 +561,7 @@ impl Step for Rustc {
561561
cargo.arg("-p").arg(krate);
562562
}
563563

564-
builder.run(&mut cargo);
564+
builder.run(&mut cargo.into());
565565
}
566566
}
567567

@@ -656,7 +656,7 @@ impl Step for Rustdoc {
656656
cargo.arg("-p").arg("rustdoc");
657657

658658
cargo.env("RUSTDOCFLAGS", "--document-private-items");
659-
builder.run(&mut cargo);
659+
builder.run(&mut cargo.into());
660660
}
661661
}
662662

src/bootstrap/test.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,9 @@ impl Step for Cargo {
233233
// those features won't be able to land.
234234
cargo.env("CARGO_TEST_DISABLE_NIGHTLY", "1");
235235

236-
try_run(
237-
builder,
238-
cargo.env("PATH", &path_for_cargo(builder, compiler)),
239-
);
236+
cargo.env("PATH", &path_for_cargo(builder, compiler));
237+
238+
try_run(builder, &mut cargo.into());
240239
}
241240
}
242241

@@ -290,7 +289,7 @@ impl Step for Rls {
290289
cargo.arg("--")
291290
.args(builder.config.cmd.test_args());
292291

293-
if try_run(builder, &mut cargo) {
292+
if try_run(builder, &mut cargo.into()) {
294293
builder.save_toolstate("rls", ToolState::TestPass);
295294
}
296295
}
@@ -348,7 +347,7 @@ impl Step for Rustfmt {
348347

349348
builder.add_rustc_lib_path(compiler, &mut cargo);
350349

351-
if try_run(builder, &mut cargo) {
350+
if try_run(builder, &mut cargo.into()) {
352351
builder.save_toolstate("rustfmt", ToolState::TestPass);
353352
}
354353
}
@@ -418,6 +417,7 @@ impl Step for Miri {
418417
cargo.env("CARGO_INSTALL_ROOT", &builder.out); // cargo adds a `bin/`
419418
cargo.env("XARGO", builder.out.join("bin").join("xargo"));
420419

420+
let mut cargo = Command::from(cargo);
421421
if !try_run(builder, &mut cargo) {
422422
return;
423423
}
@@ -467,7 +467,7 @@ impl Step for Miri {
467467

468468
builder.add_rustc_lib_path(compiler, &mut cargo);
469469

470-
if !try_run(builder, &mut cargo) {
470+
if !try_run(builder, &mut cargo.into()) {
471471
return;
472472
}
473473

@@ -502,16 +502,16 @@ impl Step for CompiletestTest {
502502
let host = self.host;
503503
let compiler = builder.compiler(0, host);
504504

505-
let mut cargo = tool::prepare_tool_cargo(builder,
506-
compiler,
507-
Mode::ToolBootstrap,
508-
host,
509-
"test",
510-
"src/tools/compiletest",
511-
SourceType::InTree,
512-
&[]);
505+
let cargo = tool::prepare_tool_cargo(builder,
506+
compiler,
507+
Mode::ToolBootstrap,
508+
host,
509+
"test",
510+
"src/tools/compiletest",
511+
SourceType::InTree,
512+
&[]);
513513

514-
try_run(builder, &mut cargo);
514+
try_run(builder, &mut cargo.into());
515515
}
516516
}
517517

@@ -571,7 +571,7 @@ impl Step for Clippy {
571571

572572
builder.add_rustc_lib_path(compiler, &mut cargo);
573573

574-
if try_run(builder, &mut cargo) {
574+
if try_run(builder, &mut cargo.into()) {
575575
builder.save_toolstate("clippy-driver", ToolState::TestPass);
576576
}
577577
} else {
@@ -1841,7 +1841,7 @@ impl Step for Crate {
18411841
test_kind, krate, compiler.stage, &compiler.host, target
18421842
));
18431843
let _time = util::timeit(&builder);
1844-
try_run(builder, &mut cargo);
1844+
try_run(builder, &mut cargo.into());
18451845
}
18461846
}
18471847

@@ -1909,7 +1909,7 @@ impl Step for CrateRustdoc {
19091909
));
19101910
let _time = util::timeit(&builder);
19111911

1912-
try_run(builder, &mut cargo);
1912+
try_run(builder, &mut cargo.into());
19131913
}
19141914
}
19151915

0 commit comments

Comments
 (0)