Skip to content

Commit c55e2bd

Browse files
committed
rewrite some more code
1 parent 95d4071 commit c55e2bd

File tree

14 files changed

+149
-91
lines changed

14 files changed

+149
-91
lines changed

src/bootstrap/builder.rs

Lines changed: 109 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::any::Any;
2+
use std::borrow::Cow;
23
use std::cell::{Cell, RefCell};
34
use std::collections::BTreeSet;
45
use std::env;
@@ -50,7 +51,7 @@ impl<'a> Deref for Builder<'a> {
5051
}
5152
}
5253

53-
pub trait Step: 'static + Clone + Debug + PartialEq + Eq + Hash {
54+
pub(crate) trait Step: 'static + Clone + Debug + PartialEq + Eq + Hash {
5455
/// `PathBuf` when directories are created or to return a `Compiler` once
5556
/// it's been assembled.
5657
type Output: Clone;
@@ -67,15 +68,7 @@ pub trait Step: 'static + Clone + Debug + PartialEq + Eq + Hash {
6768
std::any::type_name::<Self>()
6869
}
6970

70-
// fn kind(&self) -> Kind;
71-
72-
fn compiler(&self) -> Option<&Compiler> {
73-
None
74-
}
75-
76-
fn target(&self) -> Option<&TargetSelection> {
77-
None
78-
}
71+
fn info(_step_info: &mut StepInfo<'_, '_, Self>) {}
7972

8073
/// The path that should be used on the command line to run this step.
8174
fn path(&self, builder: &Builder<'_>) -> PathBuf {
@@ -656,37 +649,10 @@ impl<'a> Builder<'a> {
656649
StepDescription::run(v, self, paths);
657650
}
658651

659-
/// Print a command that will run the current step.
660-
///
661-
/// This serves two purposes:
662-
/// 1. Describe what step is currently being run.
663-
/// 2. Describe how to run only this step in case it fails.
664652
pub(crate) fn step_info(&self, step: &impl Step) {
665-
if self.config.dry_run {
666-
return;
667-
}
668-
let (stage, host) = if let Some(compiler) = step.compiler() {
669-
(compiler.stage, Some(compiler.host))
670-
} else {
671-
(self.top_stage, None)
672-
};
673-
print!(
674-
"{} {} --stage {}",
675-
// TODO: this is wrong, e.g. `check --stage 1` runs build commands first
676-
self.kind,
677-
step.path(self).display(),
678-
stage,
679-
);
680-
if let Some(host) = host {
681-
print!(" --host {}", host);
682-
}
683-
if let Some(target) = step.target() {
684-
print!(" --target {}", target);
685-
}
686-
for arg in self.config.cmd.test_args() {
687-
print!(" --test-args \"{}\"", arg);
688-
}
689-
println!();
653+
let mut info = StepInfo::new(self, step);
654+
Step::info(&mut info);
655+
info.print();
690656
}
691657

692658
/// Obtain a compiler at a given stage and for a given host. Explicitly does
@@ -1611,7 +1577,7 @@ impl<'a> Builder<'a> {
16111577
/// Ensure that a given step is built, returning its output. This will
16121578
/// cache the step, so it is safe (and good!) to call this as often as
16131579
/// needed to ensure that all dependencies are built.
1614-
pub fn ensure<S: Step>(&'a self, step: S) -> S::Output {
1580+
pub(crate) fn ensure<S: Step>(&'a self, step: S) -> S::Output {
16151581
{
16161582
let mut stack = self.stack.borrow_mut();
16171583
for stack_step in stack.iter() {
@@ -1772,3 +1738,105 @@ impl From<Cargo> for Command {
17721738
cargo.command
17731739
}
17741740
}
1741+
1742+
pub(crate) struct StepInfo<'a, 'b, S> {
1743+
pub(crate) builder: &'a Builder<'b>,
1744+
pub(crate) step: &'a S,
1745+
compiler: Option<Cow<'a, Compiler>>,
1746+
stage: Option<u32>,
1747+
host: Option<TargetSelection>,
1748+
target: Option<TargetSelection>,
1749+
cmd: Option<Kind>,
1750+
}
1751+
1752+
impl<'a> From<Compiler> for Cow<'a, Compiler> {
1753+
fn from(val: Compiler) -> Self {
1754+
Self::Owned(val)
1755+
}
1756+
}
1757+
1758+
impl<'a> From<&'a Compiler> for Cow<'a, Compiler> {
1759+
fn from(val: &'a Compiler) -> Self {
1760+
Self::Borrowed(val)
1761+
}
1762+
}
1763+
1764+
impl<'a, 'b, S> StepInfo<'a, 'b, S> {
1765+
pub(crate) fn new(builder: &'a Builder<'b>, step: &'a S) -> Self {
1766+
Self { builder, step, compiler: None, stage: None, host: None, target: None, cmd: None }
1767+
}
1768+
1769+
pub(crate) fn compiler(&mut self, val: impl Into<Cow<'a, Compiler>>) -> &mut Self {
1770+
if self.compiler.is_some() {
1771+
panic!("cannot overwrite compiler");
1772+
}
1773+
let val = val.into();
1774+
self.stage(val.stage).host(val.host);
1775+
self.compiler = Some(val);
1776+
self
1777+
}
1778+
1779+
pub(crate) fn stage(&mut self, stage: u32) -> &mut Self {
1780+
if self.stage.is_some() {
1781+
panic!("cannot overwrite stage");
1782+
}
1783+
self.stage = Some(stage);
1784+
self
1785+
}
1786+
1787+
pub(crate) fn host(&mut self, host: TargetSelection) -> &mut Self {
1788+
if self.host.is_some() {
1789+
panic!("cannot overwrite host");
1790+
}
1791+
self.host = Some(host);
1792+
self
1793+
}
1794+
1795+
pub(crate) fn target(&mut self, target: TargetSelection) -> &mut Self {
1796+
if self.target.is_some() {
1797+
panic!("cannot overwrite target");
1798+
}
1799+
self.target = Some(target);
1800+
self
1801+
}
1802+
1803+
pub(crate) fn cmd(&mut self, val: Kind) -> &mut Self {
1804+
if self.cmd.is_some() {
1805+
panic!("cannot overwrite cmd");
1806+
}
1807+
self.cmd = Some(val);
1808+
self
1809+
}
1810+
1811+
/// Print a command that will run the current step.
1812+
///
1813+
/// This serves two purposes:
1814+
/// 1. Describe what step is currently being run.
1815+
/// 2. Describe how to run only this step in case it fails.
1816+
pub(crate) fn print(&self)
1817+
where
1818+
S: Step,
1819+
{
1820+
if self.builder.config.dry_run {
1821+
return;
1822+
}
1823+
// let stage = self.stage.unwrap_or(self.builder.top_stage);
1824+
let stage = self.stage.expect("missing stage");
1825+
let host = self.host;
1826+
let kind = self.cmd.unwrap_or(self.builder.kind);
1827+
let target = self.target;
1828+
print!("{} {} --stage {}", kind, self.step.path(self.builder).display(), stage,);
1829+
if let Some(host) = host {
1830+
print!(" --host {}", host);
1831+
}
1832+
if let Some(target) = target {
1833+
print!(" --target {}", target);
1834+
}
1835+
if kind == Kind::Test {
1836+
for arg in self.builder.config.cmd.test_args() {
1837+
print!(" --test-args \"{}\"", arg);
1838+
}
1839+
}
1840+
println!();
1841+
}
1842+
}

src/bootstrap/cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl Cache {
245245
Cache(RefCell::new(HashMap::new()))
246246
}
247247

248-
pub fn put<S: Step>(&self, step: S, value: S::Output) {
248+
pub(crate) fn put<S: Step>(&self, step: S, value: S::Output) {
249249
let mut cache = self.0.borrow_mut();
250250
let type_id = TypeId::of::<S>();
251251
let stepcache = cache
@@ -257,7 +257,7 @@ impl Cache {
257257
stepcache.insert(step, value);
258258
}
259259

260-
pub fn get<S: Step>(&self, step: &S) -> Option<S::Output> {
260+
pub(crate) fn get<S: Step>(&self, step: &S) -> Option<S::Output> {
261261
let mut cache = self.0.borrow_mut();
262262
let type_id = TypeId::of::<S>();
263263
let stepcache = cache

src/bootstrap/check.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Implementation of compiling the compiler and standard library, in "check"-based modes.
22
3-
use crate::builder::{Builder, Kind, RunConfig, ShouldRun, Step};
3+
use crate::builder::{Builder, Kind, RunConfig, ShouldRun, Step, StepInfo};
44
use crate::cache::Interned;
55
use crate::compile::{add_to_sysroot, run_cargo, rustc_cargo, rustc_cargo_env, std_cargo};
66
use crate::config::TargetSelection;
@@ -71,6 +71,13 @@ impl Step for Std {
7171
run.builder.ensure(Std { target: run.target });
7272
}
7373

74+
fn info(step_info: &mut StepInfo<'_, '_, Self>) {
75+
let step = step_info.step;
76+
let builder = step_info.builder;
77+
let compiler = builder.compiler(builder.top_stage, builder.config.build);
78+
step_info.compiler(compiler).target(step.target).cmd(Kind::Check);
79+
}
80+
7481
fn run(self, builder: &Builder<'_>) {
7582
builder.update_submodule(&Path::new("library").join("stdarch"));
7683

@@ -167,6 +174,13 @@ impl Step for Rustc {
167174
run.builder.ensure(Rustc { target: run.target });
168175
}
169176

177+
fn info(step_info: &mut StepInfo<'_, '_, Self>) {
178+
let step = step_info.step;
179+
let builder = step_info.builder;
180+
let compiler = builder.compiler(builder.top_stage, builder.config.build);
181+
step_info.compiler(compiler).target(step.target).cmd(Kind::Check);
182+
}
183+
170184
/// Builds the compiler.
171185
///
172186
/// This will build the compiler for a particular stage of the build using

src/bootstrap/compile.rs

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

2323
use crate::builder::Cargo;
24-
use crate::builder::{Builder, Kind, RunConfig, ShouldRun, Step};
24+
use crate::builder::{Builder, Kind, RunConfig, ShouldRun, Step, StepInfo};
2525
use crate::cache::{Interned, INTERNER};
2626
use crate::config::TargetSelection;
2727
use crate::dist;
@@ -54,12 +54,9 @@ impl Step for Std {
5454
});
5555
}
5656

57-
fn compiler(&self) -> Option<&Compiler> {
58-
Some(&self.compiler)
59-
}
60-
61-
fn target(&self) -> Option<&TargetSelection> {
62-
Some(&self.target)
57+
fn info(step_info: &mut StepInfo<'_, '_, Self>) {
58+
let step = step_info.step;
59+
step_info.compiler(&step.compiler).target(step.target).cmd(Kind::Build);
6360
}
6461

6562
/// Builds the standard library.
@@ -527,14 +524,11 @@ impl Step for Rustc {
527524
});
528525
}
529526

530-
fn compiler(&self) -> Option<&Compiler> {
531-
Some(&self.compiler)
527+
fn info(step_info: &mut StepInfo<'_, '_, Self>) {
528+
let step = step_info.step;
529+
step_info.compiler(&step.compiler).target(step.target).cmd(Kind::Build);
532530
}
533531

534-
// fn stage(&self, _builder: &Builder<'_>) -> u32 {
535-
// self.compiler.stage
536-
// }
537-
538532
/// Builds the compiler.
539533
///
540534
/// This will build the compiler for a particular stage of the build using

src/bootstrap/tool.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::process::{exit, Command};
66

77
use build_helper::t;
88

9-
use crate::builder::{Builder, Cargo as CargoCommand, RunConfig, ShouldRun, Step};
9+
use crate::builder::{Builder, Cargo as CargoCommand, Kind, RunConfig, ShouldRun, Step, StepInfo};
1010
use crate::channel::GitInfo;
1111
use crate::compile;
1212
use crate::config::TargetSelection;
@@ -319,16 +319,9 @@ macro_rules! bootstrap_tool {
319319
impl Step for $name {
320320
type Output = PathBuf;
321321

322-
// fn stage(&self, _builder: &Builder<'_>) -> u32 {
323-
// self.compiler.stage
324-
// }
325-
326-
fn compiler(&self) -> Option<&Compiler> {
327-
Some(&self.compiler)
328-
}
329-
330-
fn target(&self) -> Option<&TargetSelection> {
331-
Some(&self.target)
322+
fn info(step_info: &mut StepInfo<'_, '_, Self>) {
323+
let step = step_info.step;
324+
step_info.compiler(&step.compiler).target(step.target).cmd(Kind::Build);
332325
}
333326

334327
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
@@ -607,16 +600,9 @@ impl Step for Cargo {
607600
const DEFAULT: bool = true;
608601
const ONLY_HOSTS: bool = true;
609602

610-
// fn stage(&self, _builder: &Builder<'_>) -> u32 {
611-
// self.compiler.stage
612-
// }
613-
614-
fn compiler(&self) -> Option<&Compiler> {
615-
Some(&self.compiler)
616-
}
617-
618-
fn target(&self) -> Option<&TargetSelection> {
619-
Some(&self.target)
603+
fn info(step_info: &mut StepInfo<'_, '_, Self>) {
604+
let step = step_info.step;
605+
step_info.compiler(&step.compiler).target(step.target).cmd(Kind::Build);
620606
}
621607

622608
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {

src/bootstrap/toolstate.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::builder::{Builder, Kind, RunConfig, ShouldRun, Step};
1+
use crate::builder::{Builder, RunConfig, ShouldRun, Step};
22
use build_helper::t;
33
use serde::{Deserialize, Serialize};
44
use std::collections::HashMap;
@@ -141,10 +141,6 @@ pub struct ToolStateCheck;
141141
impl Step for ToolStateCheck {
142142
type Output = ();
143143

144-
// fn kind(&self) -> Kind {
145-
// Kind::Test
146-
// }
147-
148144
/// Checks tool state status.
149145
///
150146
/// This is intended to be used in the `checktools.sh` script. To use

src/doc/book

Submodule book updated 49 files

src/tools/rust-analyzer

0 commit comments

Comments
 (0)