Skip to content

Commit 971b1ba

Browse files
committed
Record build and test result of extended tools into toolstates.json.
1 parent 128199e commit 971b1ba

File tree

7 files changed

+68
-12
lines changed

7 files changed

+68
-12
lines changed

config.toml.example

+4
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@
301301
# As a side-effect also generates MIR for all libraries.
302302
#test-miri = false
303303

304+
# After building or testing extended tools (e.g. clippy and rustfmt), append the
305+
# result (broken, compiling, testing) into this JSON file.
306+
#save-toolstates = "/path/to/toolstates.json"
307+
304308
# =============================================================================
305309
# Options for specific targets
306310
#

src/bootstrap/check.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,21 @@ impl fmt::Display for TestKind {
6565
}
6666
}
6767

68-
fn try_run_expecting(build: &Build, cmd: &mut Command, expect: BuildExpectation) {
68+
fn try_run_expecting(build: &Build, cmd: &mut Command, expect: BuildExpectation) -> bool {
6969
if !build.fail_fast {
7070
if !build.try_run(cmd, expect) {
7171
let mut failures = build.delayed_failures.borrow_mut();
7272
failures.push(format!("{:?}", cmd));
73+
return false;
7374
}
7475
} else {
7576
build.run_expecting(cmd, expect);
7677
}
78+
true
7779
}
7880

7981
fn try_run(build: &Build, cmd: &mut Command) {
80-
try_run_expecting(build, cmd, BuildExpectation::None)
82+
try_run_expecting(build, cmd, BuildExpectation::None);
8183
}
8284

8385
fn try_run_quiet(build: &Build, cmd: &mut Command) {
@@ -257,11 +259,13 @@ impl Step for Rls {
257259

258260
builder.add_rustc_lib_path(compiler, &mut cargo);
259261

260-
try_run_expecting(
262+
if try_run_expecting(
261263
build,
262264
&mut cargo,
263265
builder.build.config.toolstate.rls.passes(ToolState::Testing),
264-
);
266+
) {
267+
build.save_toolstate("rls", ToolState::Testing);
268+
}
265269
}
266270
}
267271

@@ -305,11 +309,13 @@ impl Step for Rustfmt {
305309

306310
builder.add_rustc_lib_path(compiler, &mut cargo);
307311

308-
try_run_expecting(
312+
if try_run_expecting(
309313
build,
310314
&mut cargo,
311315
builder.build.config.toolstate.rustfmt.passes(ToolState::Testing),
312-
);
316+
) {
317+
build.save_toolstate("rustfmt", ToolState::Testing);
318+
}
313319
}
314320
}
315321

@@ -354,11 +360,13 @@ impl Step for Miri {
354360

355361
builder.add_rustc_lib_path(compiler, &mut cargo);
356362

357-
try_run_expecting(
363+
if try_run_expecting(
358364
build,
359365
&mut cargo,
360366
builder.build.config.toolstate.miri.passes(ToolState::Testing),
361-
);
367+
) {
368+
build.save_toolstate("miri", ToolState::Testing);
369+
}
362370
} else {
363371
eprintln!("failed to test miri: could not build");
364372
}
@@ -411,11 +419,13 @@ impl Step for Clippy {
411419

412420
builder.add_rustc_lib_path(compiler, &mut cargo);
413421

414-
try_run_expecting(
422+
if try_run_expecting(
415423
build,
416424
&mut cargo,
417425
builder.build.config.toolstate.clippy.passes(ToolState::Testing),
418-
);
426+
) {
427+
build.save_toolstate("clippy-driver", ToolState::Testing);
428+
}
419429
} else {
420430
eprintln!("failed to test clippy: could not build");
421431
}

src/bootstrap/config.rs

+4
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ pub struct Config {
112112
pub channel: String,
113113
pub quiet_tests: bool,
114114
pub test_miri: bool,
115+
pub save_toolstates: Option<PathBuf>,
116+
115117
// Fallback musl-root for all targets
116118
pub musl_root: Option<PathBuf>,
117119
pub prefix: Option<PathBuf>,
@@ -279,6 +281,7 @@ struct Rust {
279281
dist_src: Option<bool>,
280282
quiet_tests: Option<bool>,
281283
test_miri: Option<bool>,
284+
save_toolstates: Option<String>,
282285
}
283286

284287
/// TOML representation of how each build target is configured.
@@ -473,6 +476,7 @@ impl Config {
473476
set(&mut config.test_miri, rust.test_miri);
474477
config.rustc_default_linker = rust.default_linker.clone();
475478
config.musl_root = rust.musl_root.clone().map(PathBuf::from);
479+
config.save_toolstates = rust.save_toolstates.clone().map(PathBuf::from);
476480

477481
match rust.codegen_units {
478482
Some(0) => config.rust_codegen_units = Some(num_cpus::get() as u32),

src/bootstrap/configure.py

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def v(*args):
7777
o("debuginfo-lines", "rust.debuginfo-lines", "build with line number debugger metadata")
7878
o("debuginfo-only-std", "rust.debuginfo-only-std", "build only libstd with debugging information")
7979
o("debug-jemalloc", "rust.debug-jemalloc", "build jemalloc with --enable-debug --enable-fill")
80+
v("save-toolstates", "rust.save-toolstates", "save build and test status of external tools into this file")
8081

8182
v("prefix", "install.prefix", "set installation prefix")
8283
v("localstatedir", "install.localstatedir", "local state directory")

src/bootstrap/lib.rs

+25
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ mod job {
190190
pub use config::Config;
191191
use flags::Subcommand;
192192
use cache::{Interned, INTERNER};
193+
use toolstate::ToolState;
193194

194195
/// A structure representing a Rust compiler.
195196
///
@@ -874,6 +875,30 @@ impl Build {
874875
}
875876
}
876877

878+
/// Updates the actual toolstate of a tool.
879+
///
880+
/// The toolstates are saved to the file specified by the key
881+
/// `rust.save-toolstates` in `config.toml`. If unspecified, nothing will be
882+
/// done. The file is updated immediately after this function completes.
883+
pub fn save_toolstate(&self, tool: &str, state: ToolState) {
884+
use std::io::{Seek, SeekFrom};
885+
886+
if let Some(ref path) = self.config.save_toolstates {
887+
let mut file = t!(fs::OpenOptions::new()
888+
.create(true)
889+
.read(true)
890+
.write(true)
891+
.open(path));
892+
893+
let mut current_toolstates: HashMap<Box<str>, ToolState> =
894+
serde_json::from_reader(&mut file).unwrap_or_default();
895+
current_toolstates.insert(tool.into(), state);
896+
t!(file.seek(SeekFrom::Start(0)));
897+
t!(file.set_len(0));
898+
t!(serde_json::to_writer(file, &current_toolstates));
899+
}
900+
}
901+
877902
/// Get a list of crates from a root crate.
878903
///
879904
/// Returns Vec<(crate, path to crate, is_root_crate)>

src/bootstrap/tool.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,19 @@ impl Step for ToolBuild {
115115
println!("Building stage{} tool {} ({})", compiler.stage, tool, target);
116116

117117
let mut cargo = prepare_tool_cargo(builder, compiler, target, "build", path);
118-
if !build.try_run(&mut cargo, expectation) {
118+
let is_expected = build.try_run(&mut cargo, expectation);
119+
// If the expectation is "Failing", `try_run` returning true actually
120+
// means a build-failure is successfully observed, i.e. the tool is
121+
// broken. Thus the XOR here.
122+
// Sorry for the complicated logic, but we can remove this expectation
123+
// logic after #45861 is fully fixed.
124+
build.save_toolstate(tool, if is_expected ^ (expectation == BuildExpectation::Failing) {
125+
ToolState::Compiling
126+
} else {
127+
ToolState::Broken
128+
});
129+
130+
if !is_expected {
119131
if expectation == BuildExpectation::None {
120132
exit(1);
121133
} else {

src/bootstrap/toolstate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use build_helper::BuildExpectation;
1212

13-
#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Eq)]
13+
#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
1414
/// Whether a tool can be compiled, tested or neither
1515
pub enum ToolState {
1616
/// The tool compiles successfully, but the test suite fails

0 commit comments

Comments
 (0)