Skip to content

Commit 3a11ab0

Browse files
committed
Improve error reporting in build failures because of missing environment variables
1 parent 062afe8 commit 3a11ab0

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/libcprover-rust/build.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ fn get_current_working_dir() -> std::io::Result<PathBuf> {
88
env::current_dir()
99
}
1010

11-
fn get_lib_directory() -> Result<String, VarError> {
12-
env::var("CBMC_LIB_DIR")
13-
}
14-
1511
// Passed by the top-level CMakeLists.txt to control which version of the
1612
// static library of CBMC we're linking against. A user can also change the
1713
// environment variable to link against different versions of CBMC.
1814
fn get_cbmc_version() -> Result<String, VarError> {
1915
env::var("CBMC_VERSION")
2016
}
2117

18+
// Passed by the top-level CMakeLists.txt to control where the static library we
19+
// link against is located. A user can also change the location of the library
20+
// on their system by supplying the environment variable themselves.
21+
fn get_lib_directory() -> Result<String, VarError> {
22+
env::var("CBMC_LIB_DIR")
23+
}
24+
2225
fn get_library_build_dir() -> std::io::Result<PathBuf> {
2326
let env_var_fetch_result = get_lib_directory();
2427
if let Ok(build_dir) = env_var_fetch_result {
@@ -28,7 +31,7 @@ fn get_library_build_dir() -> std::io::Result<PathBuf> {
2831
}
2932
Err(Error::new(
3033
ErrorKind::Other,
31-
"Please set the environment variable CBMC_LIB_DIR with the path that contains the libcprover.x.y.z.a library on your system",
34+
"Environment variable `CBMC_LIB_DIR' not set",
3235
))
3336
}
3437

@@ -48,7 +51,14 @@ fn main() {
4851

4952
let libraries_path = match get_library_build_dir() {
5053
Ok(path) => path,
51-
Err(err) => panic!("Error: {}", err),
54+
Err(err) => {
55+
let error_message = &format!(
56+
"Error: {}.\n Advice: {}.",
57+
err,
58+
"Please set the environment variable `CBMC_LIB_DIR' with the path that contains libcprover.x.y.z.a on your system"
59+
);
60+
panic!("{}", error_message);
61+
}
5262
};
5363

5464
println!(
@@ -58,7 +68,14 @@ fn main() {
5868

5969
let cprover_static_libname = match get_cbmc_version() {
6070
Ok(version) => String::from("cprover.") + &version,
61-
Err(err) => panic!("Error: {}", err),
71+
Err(_) => {
72+
let error_message = &format!(
73+
"Error: {}.\n Advice: {}.",
74+
"Environment variable `CBMC_VERSION' not set",
75+
"Please set the environment variable `CBMC_VERSION' with the version of CBMC you want to link against (e.g. 5.78.0)"
76+
);
77+
panic!("{}", error_message);
78+
}
6279
};
6380

6481
println!("cargo:rustc-link-lib=static={}", cprover_static_libname);

0 commit comments

Comments
 (0)