Skip to content

Commit

Permalink
refactor use of Cargo and configure_linker in bootstrap
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <work@onurozkan.dev>
  • Loading branch information
onur-ozkan committed Feb 10, 2024
1 parent 993c72f commit 3c019de
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 187 deletions.
23 changes: 14 additions & 9 deletions src/bootstrap/src/core/build_steps/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use crate::core::build_steps::compile::{
add_to_sysroot, run_cargo, rustc_cargo, rustc_cargo_env, std_cargo,
};
use crate::core::build_steps::tool::{prepare_tool_cargo, SourceType};
use crate::core::builder::{crate_description, Alias, Builder, Kind, RunConfig, ShouldRun, Step};
use crate::core::builder::{
self, crate_description, Alias, Builder, Kind, RunConfig, ShouldRun, Step,
};
use crate::core::config::TargetSelection;
use crate::utils::cache::Interned;
use crate::INTERNER;
Expand Down Expand Up @@ -110,14 +112,15 @@ impl Step for Std {
let target = self.target;
let compiler = builder.compiler(builder.top_stage, builder.config.build);

let mut cargo = builder.cargo(
let mut cargo = builder::Cargo::new(
builder,
compiler,
Mode::Std,
SourceType::InTree,
target,
cargo_subcommand(builder.kind),
false,
);

std_cargo(builder, target, compiler.stage, &mut cargo);
if matches!(builder.config.cmd, Subcommand::Fix { .. }) {
// By default, cargo tries to fix all targets. Tell it not to fix tests until we've added `test` to the sysroot.
Expand Down Expand Up @@ -163,13 +166,13 @@ impl Step for Std {
// since we initialize with an empty sysroot.
//
// Currently only the "libtest" tree of crates does this.
let mut cargo = builder.cargo(
let mut cargo = builder::Cargo::new(
builder,
compiler,
Mode::Std,
SourceType::InTree,
target,
cargo_subcommand(builder.kind),
false,
);

// If we're not in stage 0, tests and examples will fail to compile
Expand Down Expand Up @@ -258,14 +261,15 @@ impl Step for Rustc {
builder.ensure(Std::new(target));
}

let mut cargo = builder.cargo(
let mut cargo = builder::Cargo::new(
builder,
compiler,
Mode::Rustc,
SourceType::InTree,
target,
cargo_subcommand(builder.kind),
false,
);

rustc_cargo(builder, &mut cargo, target, compiler.stage);

// For ./x.py clippy, don't run with --all-targets because
Expand Down Expand Up @@ -335,14 +339,15 @@ impl Step for CodegenBackend {

builder.ensure(Rustc::new(target, builder));

let mut cargo = builder.cargo(
let mut cargo = builder::Cargo::new(
builder,
compiler,
Mode::Codegen,
SourceType::InTree,
target,
cargo_subcommand(builder.kind),
false,
);

cargo
.arg("--manifest-path")
.arg(builder.src.join(format!("compiler/rustc_codegen_{backend}/Cargo.toml")));
Expand Down
50 changes: 37 additions & 13 deletions src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use serde_derive::Deserialize;
use crate::core::build_steps::dist;
use crate::core::build_steps::llvm;
use crate::core::build_steps::tool::SourceType;
use crate::core::builder;
use crate::core::builder::crate_description;
use crate::core::builder::Cargo;
use crate::core::builder::{Builder, Kind, PathSet, RunConfig, ShouldRun, Step, TaskPath};
Expand Down Expand Up @@ -233,25 +234,35 @@ impl Step for Std {
}
}

let mut cargo = builder.cargo(
compiler,
Mode::Std,
SourceType::InTree,
target,
if self.is_for_mir_opt_tests { "check" } else { "build" },
self.is_for_mir_opt_tests,
);
// We build a sysroot for mir-opt tests using the same trick that Miri does: A check build
// with -Zalways-encode-mir. This frees us from the need to have a target linker, and the
// fact that this is a check build integrates nicely with run_cargo.
if self.is_for_mir_opt_tests {
let mut cargo = if self.is_for_mir_opt_tests {
let mut cargo = builder::Cargo::new_for_mir_opt_tests(
builder,
compiler,
Mode::Std,
SourceType::InTree,
target,
"check",
);
cargo.rustflag("-Zalways-encode-mir");
cargo.arg("--manifest-path").arg(builder.src.join("library/sysroot/Cargo.toml"));
cargo
} else {
let mut cargo = builder::Cargo::new_for_mir_opt_tests(
builder,
compiler,
Mode::Std,
SourceType::InTree,
target,
"build",
);
std_cargo(builder, target, compiler.stage, &mut cargo);
for krate in &*self.crates {
cargo.arg("-p").arg(krate);
}
cargo
};

// See src/bootstrap/synthetic_targets.rs
Expand Down Expand Up @@ -915,8 +926,15 @@ impl Step for Rustc {
builder.config.build,
));

let mut cargo =
builder.cargo(compiler, Mode::Rustc, SourceType::InTree, target, "build", false);
let mut cargo = builder::Cargo::new(
builder,
compiler,
Mode::Rustc,
SourceType::InTree,
target,
"build",
);

rustc_cargo(builder, &mut cargo, target, compiler.stage);

if builder.config.rust_profile_use.is_some()
Expand Down Expand Up @@ -1336,8 +1354,14 @@ impl Step for CodegenBackend {

let out_dir = builder.cargo_out(compiler, Mode::Codegen, target);

let mut cargo =
builder.cargo(compiler, Mode::Codegen, SourceType::InTree, target, "build", false);
let mut cargo = builder::Cargo::new(
builder,
compiler,
Mode::Codegen,
SourceType::InTree,
target,
"build",
);
cargo
.arg("--manifest-path")
.arg(builder.src.join(format!("compiler/rustc_codegen_{backend}/Cargo.toml")));
Expand Down
9 changes: 6 additions & 3 deletions src/bootstrap/src/core/build_steps/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{fs, mem};

use crate::core::build_steps::compile;
use crate::core::build_steps::tool::{self, prepare_tool_cargo, SourceType, Tool};
use crate::core::builder::crate_description;
use crate::core::builder::{self, crate_description};
use crate::core::builder::{Alias, Builder, Compiler, Kind, RunConfig, ShouldRun, Step};
use crate::core::config::{Config, TargetSelection};
use crate::utils::cache::{Interned, INTERNER};
Expand Down Expand Up @@ -684,7 +684,9 @@ fn doc_std(
// as a function parameter.
let out_dir = target_dir.join(target.triple).join("doc");

let mut cargo = builder.cargo(compiler, Mode::Std, SourceType::InTree, target, "doc", false);
let mut cargo =
builder::Cargo::new(builder, compiler, Mode::Std, SourceType::InTree, target, "doc");

compile::std_cargo(builder, target, compiler.stage, &mut cargo);
cargo
.arg("--no-deps")
Expand Down Expand Up @@ -786,7 +788,8 @@ impl Step for Rustc {

// Build cargo command.
let mut cargo =
builder.cargo(compiler, Mode::Rustc, SourceType::InTree, target, "doc", false);
builder::Cargo::new(builder, compiler, Mode::Rustc, SourceType::InTree, target, "doc");

cargo.rustdocflag("--document-private-items");
// Since we always pass --document-private-items, there's no need to warn about linking to private items.
cargo.rustdocflag("-Arustdoc::private-intra-doc-links");
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/build_steps/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl Step for Miri {
SourceType::InTree,
&[],
);
miri.add_rustc_lib_path(builder, compiler);
miri.add_rustc_lib_path(builder);
// Forward arguments.
miri.arg("--").arg("--target").arg(target.rustc_target_arg());
miri.args(builder.config.args());
Expand Down
36 changes: 23 additions & 13 deletions src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::core::build_steps::llvm;
use crate::core::build_steps::synthetic_targets::MirOptPanicAbortSyntheticTarget;
use crate::core::build_steps::tool::{self, SourceType, Tool};
use crate::core::build_steps::toolstate::ToolState;
use crate::core::builder;
use crate::core::builder::crate_description;
use crate::core::builder::{Builder, Compiler, Kind, RunConfig, ShouldRun, Step};
use crate::core::config::flags::get_completion;
Expand Down Expand Up @@ -380,7 +381,7 @@ impl Step for RustAnalyzer {
// work in Rust CI
cargo.env("SKIP_SLOW_TESTS", "1");

cargo.add_rustc_lib_path(builder, compiler);
cargo.add_rustc_lib_path(builder);
run_cargo_test(cargo, &[], &[], "rust-analyzer", "rust-analyzer", compiler, host, builder);
}
}
Expand Down Expand Up @@ -426,7 +427,7 @@ impl Step for Rustfmt {
t!(fs::create_dir_all(&dir));
cargo.env("RUSTFMT_TEST_DIR", dir);

cargo.add_rustc_lib_path(builder, compiler);
cargo.add_rustc_lib_path(builder);

run_cargo_test(cargo, &[], &[], "rustfmt", "rustfmt", compiler, host, builder);
}
Expand Down Expand Up @@ -476,7 +477,7 @@ impl Step for RustDemangler {
t!(fs::create_dir_all(&dir));

cargo.env("RUST_DEMANGLER_DRIVER_PATH", rust_demangler);
cargo.add_rustc_lib_path(builder, compiler);
cargo.add_rustc_lib_path(builder);

run_cargo_test(
cargo,
Expand Down Expand Up @@ -517,7 +518,7 @@ impl Miri {
SourceType::InTree,
&[],
);
cargo.add_rustc_lib_path(builder, compiler);
cargo.add_rustc_lib_path(builder);
cargo.arg("--").arg("miri").arg("setup");
cargo.arg("--target").arg(target.rustc_target_arg());

Expand Down Expand Up @@ -618,7 +619,7 @@ impl Step for Miri {
);
let _guard = builder.msg_sysroot_tool(Kind::Test, compiler.stage, "miri", host, target);

cargo.add_rustc_lib_path(builder, compiler);
cargo.add_rustc_lib_path(builder);

// miri tests need to know about the stage sysroot
cargo.env("MIRI_SYSROOT", &miri_sysroot);
Expand Down Expand Up @@ -671,7 +672,7 @@ impl Step for Miri {
SourceType::Submodule,
&[],
);
cargo.add_rustc_lib_path(builder, compiler);
cargo.add_rustc_lib_path(builder);
cargo.arg("--").arg("miri").arg("test");
if builder.config.locked_deps {
cargo.arg("--locked");
Expand Down Expand Up @@ -788,7 +789,7 @@ impl Step for Clippy {
let host_libs = builder.stage_out(compiler, Mode::ToolRustc).join(builder.cargo_dir());
cargo.env("HOST_LIBS", host_libs);

cargo.add_rustc_lib_path(builder, compiler);
cargo.add_rustc_lib_path(builder);
let mut cargo = prepare_cargo_test(cargo, &[], &[], "clippy", compiler, host, builder);

let _guard = builder.msg_sysroot_tool(Kind::Test, compiler.stage, "clippy", host, host);
Expand Down Expand Up @@ -2499,8 +2500,15 @@ impl Step for Crate {
// we're working with automatically.
let compiler = builder.compiler_for(compiler.stage, compiler.host, target);

let mut cargo =
builder.cargo(compiler, mode, SourceType::InTree, target, builder.kind.as_str(), false);
let mut cargo = builder::Cargo::new(
builder,
compiler,
mode,
SourceType::InTree,
target,
builder.kind.as_str(),
);

match mode {
Mode::Std => {
compile::std_cargo(builder, target, compiler.stage, &mut cargo);
Expand Down Expand Up @@ -3134,14 +3142,15 @@ impl Step for CodegenCranelift {
let compiler = builder.compiler_for(compiler.stage, compiler.host, target);

let build_cargo = || {
let mut cargo = builder.cargo(
let mut cargo = builder::Cargo::new(
builder,
compiler,
Mode::Codegen, // Must be codegen to ensure dlopen on compiled dylibs works
SourceType::InTree,
target,
"run",
false,
);

cargo.current_dir(&builder.src.join("compiler/rustc_codegen_cranelift"));
cargo
.arg("--manifest-path")
Expand Down Expand Up @@ -3261,14 +3270,15 @@ impl Step for CodegenGCC {
let compiler = builder.compiler_for(compiler.stage, compiler.host, target);

let build_cargo = || {
let mut cargo = builder.cargo(
let mut cargo = builder::Cargo::new(
builder,
compiler,
Mode::Codegen, // Must be codegen to ensure dlopen on compiled dylibs works
SourceType::InTree,
target,
"run",
false,
);

cargo.current_dir(&builder.src.join("compiler/rustc_codegen_gcc"));
cargo
.arg("--manifest-path")
Expand Down
4 changes: 3 additions & 1 deletion src/bootstrap/src/core/build_steps/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::process::Command;

use crate::core::build_steps::compile;
use crate::core::build_steps::toolstate::ToolState;
use crate::core::builder;
use crate::core::builder::{Builder, Cargo as CargoCommand, RunConfig, ShouldRun, Step};
use crate::core::config::TargetSelection;
use crate::utils::channel::GitInfo;
Expand Down Expand Up @@ -142,7 +143,8 @@ pub fn prepare_tool_cargo(
source_type: SourceType,
extra_features: &[String],
) -> CargoCommand {
let mut cargo = builder.cargo(compiler, mode, source_type, target, command, false);
let mut cargo = builder::Cargo::new(builder, compiler, mode, source_type, target, command);

let dir = builder.src.join(path);
cargo.arg("--manifest-path").arg(dir.join("Cargo.toml"));

Expand Down
Loading

0 comments on commit 3c019de

Please sign in to comment.