|
1 | 1 | use std::env;
|
2 | 2 | use std::env::VarError;
|
3 | 3 | use std::io::{Error, ErrorKind};
|
4 |
| -use std::path::Path; |
5 | 4 | use std::path::PathBuf;
|
6 | 5 |
|
7 | 6 | fn get_current_working_dir() -> std::io::Result<PathBuf> {
|
@@ -37,12 +36,40 @@ fn get_library_build_dir() -> std::io::Result<PathBuf> {
|
37 | 36 | ))
|
38 | 37 | }
|
39 | 38 |
|
| 39 | +// This is needed so that cargo can find the include folder for the C++ |
| 40 | +// API headers at compile time. |
| 41 | +fn get_include_directory_envvar() -> Result<String, VarError> { |
| 42 | + env::var("CBMC_INCLUDE_DIR") |
| 43 | +} |
| 44 | + |
| 45 | +fn get_include_directory() -> std::io::Result<PathBuf> { |
| 46 | + let env_var_fetch_result = get_include_directory_envvar(); |
| 47 | + if let Ok(build_dir) = env_var_fetch_result { |
| 48 | + let mut path = PathBuf::new(); |
| 49 | + path.push(build_dir); |
| 50 | + return Ok(path); |
| 51 | + } |
| 52 | + Err(Error::new( |
| 53 | + ErrorKind::Other, |
| 54 | + "Environment variable `CBMC_INCLUDE_DIR' not set", |
| 55 | + )) |
| 56 | +} |
| 57 | + |
40 | 58 | fn main() {
|
41 |
| - let cbmc_source_path = Path::new(".."); |
42 |
| - let cpp_api_path = Path::new("../libcprover-cpp/"); |
| 59 | + let cpp_api_include_path = match get_include_directory() { |
| 60 | + Ok(path) => path, |
| 61 | + Err(err) => { |
| 62 | + let error_message = &format!( |
| 63 | + "Error: {}.\n Advice: {}.", |
| 64 | + err, |
| 65 | + "Please set the environment variable `CBMC_INCLUDE_DIR' with the path that contains cprover/api.h on your system" |
| 66 | + ); |
| 67 | + panic!("{}", error_message); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
43 | 71 | let _build = cxx_build::bridge("src/lib.rs")
|
44 |
| - .include(cbmc_source_path) |
45 |
| - .include(cpp_api_path) |
| 72 | + .include(cpp_api_include_path) |
46 | 73 | .include(get_current_working_dir().unwrap())
|
47 | 74 | .file("src/c_api.cc")
|
48 | 75 | .flag_if_supported("-std=c++11")
|
|
0 commit comments