Skip to content

Commit e1bc7bf

Browse files
authored
Make cargo build-dev produce a proper exit code (model-checking#2398)
1 parent d9cf03a commit e1bc7bf

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

tools/build-kani/src/main.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ fn main() -> Result<()> {
1919
let args = parser::ArgParser::parse();
2020

2121
match args.subcommand {
22-
parser::Commands::BuildDev(build_parser) => {
23-
build_lib();
24-
build_bin(&build_parser.args);
25-
}
22+
parser::Commands::BuildDev(build_parser) => build_lib().and(build_bin(&build_parser.args)),
2623
parser::Commands::Bundle(bundle_parser) => {
2724
let version_string = bundle_parser.version;
2825
let kani_string = format!("kani-{version_string}");
@@ -45,10 +42,10 @@ fn main() -> Result<()> {
4542
std::fs::remove_dir_all(dir)?;
4643

4744
println!("\nSuccessfully built release bundle: {bundle_name}");
45+
46+
Ok(())
4847
}
4948
}
50-
51-
Ok(())
5249
}
5350

5451
/// Ensures everything is good to go before we begin to build the release bundle.
@@ -70,10 +67,9 @@ fn prebundle(dir: &Path) -> Result<()> {
7067
}
7168

7269
// Before we begin, ensure Kani is built successfully in release mode.
73-
build_bin(&["--release"]);
74-
// And that libraries have been built too.
75-
build_lib();
76-
Ok(())
70+
build_bin(&["--release"])
71+
// And that libraries have been built too.
72+
.and(build_lib())
7773
}
7874

7975
/// Copy Kani files into `dir`

tools/build-kani/src/sysroot.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//! Note: We don't cross-compile. Target is the same as the host.
1313
1414
use crate::{cp, AutoRun};
15+
use anyhow::{bail, format_err, Result};
1516
use cargo_metadata::{Artifact, Message};
1617
use std::ffi::OsStr;
1718
use std::fs;
@@ -57,7 +58,7 @@ pub fn kani_sysroot_bin() -> PathBuf {
5758
/// Build the `lib/` folder for the new sysroot.
5859
/// This will include Kani's libraries as well as the standard libraries compiled with --emit-mir.
5960
/// TODO: Don't copy Kani's libstd.
60-
pub fn build_lib() {
61+
pub fn build_lib() -> Result<()> {
6162
// Run cargo build with -Z build-std
6263
let target = env!("TARGET");
6364
let target_dir = env!("KANI_BUILD_LIBS");
@@ -106,7 +107,12 @@ pub fn build_lib() {
106107

107108
// Collect the build artifacts.
108109
let artifacts = build_artifacts(&mut cmd);
109-
let _ = cmd.wait().expect("Couldn't get cargo's exit status");
110+
let exit_status = cmd.wait().expect("Couldn't get cargo's exit status");
111+
// `exit_ok` is an experimental API where we could do `.exit_ok().expect("...")` instead of the
112+
// below use of `panic`.
113+
if !exit_status.success() {
114+
bail!("Build failed: `cargo build-dev` didn't complete successfully");
115+
}
110116

111117
// Create sysroot folder hierarchy.
112118
let sysroot_lib = kani_sysroot_lib();
@@ -118,6 +124,8 @@ pub fn build_lib() {
118124
copy_libs(&artifacts, &sysroot_lib, &is_kani_lib);
119125
// Copy standard libraries into rustlib/<target>/lib/ folder.
120126
copy_libs(&artifacts, &std_path, &is_std_lib);
127+
128+
Ok(())
121129
}
122130

123131
/// Check if an artifact is a rust library that can be used by rustc on further crates compilations.
@@ -195,13 +203,13 @@ fn build_artifacts(cargo_cmd: &mut Child) -> Vec<Artifact> {
195203
/// ```bash
196204
/// cargo build --bins -Z unstable-options --out-dir $KANI_SYSROOT/bin/
197205
/// ```
198-
pub fn build_bin<T: AsRef<OsStr>>(extra_args: &[T]) {
206+
pub fn build_bin<T: AsRef<OsStr>>(extra_args: &[T]) -> Result<()> {
199207
let out_dir = kani_sysroot_bin();
200208
let args = ["--bins", "-Z", "unstable-options", "--out-dir", out_dir.to_str().unwrap()];
201209
Command::new("cargo")
202210
.arg("build")
203211
.args(args)
204212
.args(extra_args)
205213
.run()
206-
.expect("Failed to build binaries.");
214+
.or(Err(format_err!("Failed to build binaries.")))
207215
}

0 commit comments

Comments
 (0)