From e1bc7bf6a14520c8847fed88e014a94b34915313 Mon Sep 17 00:00:00 2001 From: Michael Tautschnig Date: Fri, 21 Apr 2023 11:33:21 +0200 Subject: [PATCH] Make cargo build-dev produce a proper exit code (#2398) --- tools/build-kani/src/main.rs | 16 ++++++---------- tools/build-kani/src/sysroot.rs | 16 ++++++++++++---- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/tools/build-kani/src/main.rs b/tools/build-kani/src/main.rs index 0f95fb23819c..74958879679f 100644 --- a/tools/build-kani/src/main.rs +++ b/tools/build-kani/src/main.rs @@ -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}"); @@ -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. @@ -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` diff --git a/tools/build-kani/src/sysroot.rs b/tools/build-kani/src/sysroot.rs index 7f7c2e530146..b53202ac4478 100644 --- a/tools/build-kani/src/sysroot.rs +++ b/tools/build-kani/src/sysroot.rs @@ -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; @@ -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"); @@ -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(); @@ -118,6 +124,8 @@ pub fn build_lib() { copy_libs(&artifacts, &sysroot_lib, &is_kani_lib); // Copy standard libraries into rustlib//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. @@ -195,7 +203,7 @@ fn build_artifacts(cargo_cmd: &mut Child) -> Vec { /// ```bash /// cargo build --bins -Z unstable-options --out-dir $KANI_SYSROOT/bin/ /// ``` -pub fn build_bin>(extra_args: &[T]) { +pub fn build_bin>(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") @@ -203,5 +211,5 @@ pub fn build_bin>(extra_args: &[T]) { .args(args) .args(extra_args) .run() - .expect("Failed to build binaries."); + .or(Err(format_err!("Failed to build binaries."))) }