Skip to content

Commit

Permalink
Make cargo build-dev produce a proper exit code (model-checking#2398)
Browse files Browse the repository at this point in the history
  • Loading branch information
tautschnig authored Apr 21, 2023
1 parent d9cf03a commit e1bc7bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
16 changes: 6 additions & 10 deletions tools/build-kani/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ fn main() -> Result<()> {
let args = parser::ArgParser::parse();

match args.subcommand {
parser::Commands::BuildDev(build_parser) => {
build_lib();
build_bin(&build_parser.args);
}
parser::Commands::BuildDev(build_parser) => build_lib().and(build_bin(&build_parser.args)),
parser::Commands::Bundle(bundle_parser) => {
let version_string = bundle_parser.version;
let kani_string = format!("kani-{version_string}");
Expand All @@ -45,10 +42,10 @@ fn main() -> Result<()> {
std::fs::remove_dir_all(dir)?;

println!("\nSuccessfully built release bundle: {bundle_name}");

Ok(())
}
}

Ok(())
}

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

// Before we begin, ensure Kani is built successfully in release mode.
build_bin(&["--release"]);
// And that libraries have been built too.
build_lib();
Ok(())
build_bin(&["--release"])
// And that libraries have been built too.
.and(build_lib())
}

/// Copy Kani files into `dir`
Expand Down
16 changes: 12 additions & 4 deletions tools/build-kani/src/sysroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//! Note: We don't cross-compile. Target is the same as the host.
use crate::{cp, AutoRun};
use anyhow::{bail, format_err, Result};
use cargo_metadata::{Artifact, Message};
use std::ffi::OsStr;
use std::fs;
Expand Down Expand Up @@ -57,7 +58,7 @@ pub fn kani_sysroot_bin() -> PathBuf {
/// Build the `lib/` folder for the new sysroot.
/// This will include Kani's libraries as well as the standard libraries compiled with --emit-mir.
/// TODO: Don't copy Kani's libstd.
pub fn build_lib() {
pub fn build_lib() -> Result<()> {
// Run cargo build with -Z build-std
let target = env!("TARGET");
let target_dir = env!("KANI_BUILD_LIBS");
Expand Down Expand Up @@ -106,7 +107,12 @@ pub fn build_lib() {

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

// Create sysroot folder hierarchy.
let sysroot_lib = kani_sysroot_lib();
Expand All @@ -118,6 +124,8 @@ pub fn build_lib() {
copy_libs(&artifacts, &sysroot_lib, &is_kani_lib);
// Copy standard libraries into rustlib/<target>/lib/ folder.
copy_libs(&artifacts, &std_path, &is_std_lib);

Ok(())
}

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

0 comments on commit e1bc7bf

Please sign in to comment.