1212//! Note: We don't cross-compile. Target is the same as the host.
1313
1414use crate :: { cp, AutoRun } ;
15+ use anyhow:: { bail, format_err, Result } ;
1516use cargo_metadata:: { Artifact , Message } ;
1617use std:: ffi:: OsStr ;
1718use 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