Skip to content

Commit c2cba5b

Browse files
committed
Add support for building with cadical for the Rust API
1 parent b566c4f commit c2cba5b

File tree

3 files changed

+72
-40
lines changed

3 files changed

+72
-40
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ else()
273273
FetchContent_MakeAvailable(Corrosion)
274274

275275
corrosion_import_crate(MANIFEST_PATH src/libcprover-rust/Cargo.toml)
276-
corrosion_set_env_vars(cprover-api-rust CBMC_LIB_DIR=${CMAKE_LIBRARY_OUTPUT_DIRECTORY} SAT_IMPL=${sat_impl})
276+
corrosion_set_env_vars(cprover-api-rust CBMC_BUILD_DIR=${CMAKE_BINARY_DIR} SAT_IMPL=${sat_impl})
277277
corrosion_link_libraries(cprover-api-rust cprover-api-cpp)
278278
install(TARGETS cprover-api-rust RUNTIME DESTINATION lib)
279279
# NOTE: We want to rename to a name consistent with the name of the

src/libcprover-rust/Cargo.lock

Lines changed: 26 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/libcprover-rust/build.rs

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::env;
2+
use std::env::VarError;
23
use std::io::{Error, ErrorKind};
34
use std::path::Path;
45
use std::path::PathBuf;
@@ -7,30 +8,45 @@ fn get_current_working_dir() -> std::io::Result<PathBuf> {
78
env::current_dir()
89
}
910

11+
fn get_build_directory() -> Result<String, VarError> {
12+
env::var("CBMC_BUILD_DIR")
13+
}
14+
1015
fn get_library_build_dir() -> std::io::Result<PathBuf> {
11-
let env_var_name = "CBMC_LIB_DIR";
12-
let env_var_fetch_result = env::var(env_var_name);
13-
if let Ok(lib_dir) = env_var_fetch_result {
16+
let env_var_fetch_result = get_build_directory();
17+
if let Ok(build_dir) = env_var_fetch_result {
1418
let mut path = PathBuf::new();
15-
path.push(lib_dir);
19+
path.push(build_dir);
20+
path.push("lib/");
1621
return Ok(path);
1722
}
18-
Err(Error::new(ErrorKind::Other, "failed to get library output directory"))
23+
Err(Error::new(ErrorKind::Other, "failed to get build output directory"))
1924
}
2025

21-
fn get_sat_library() -> &'static str {
26+
fn get_cadical_build_dir() -> std::io::Result<PathBuf> {
27+
let env_var_fetch_result = get_build_directory();
28+
if let Ok(build_dir) = env_var_fetch_result {
29+
let mut path = PathBuf::new();
30+
path.push(build_dir);
31+
path.push("cadical-src/build/");
32+
return Ok(path);
33+
}
34+
Err(Error::new(ErrorKind::Other, "failed to get build output directory"))
35+
}
36+
37+
fn get_sat_library() -> std::io::Result<&'static str> {
2238
let env_var_name = "SAT_IMPL";
2339
let env_var_fetch_result = env::var(env_var_name);
2440
if let Ok(sat_impl) = env_var_fetch_result {
2541
let solver_lib = match sat_impl.as_str() {
26-
"minisat2" => "minisat2-condensed",
27-
"glucose" => "libglucose-condensed",
28-
"cadical" => "cadical",
29-
_ => "Error: no identifiable solver detected"
42+
"minisat2" => Ok("minisat2-condensed"),
43+
"glucose" => Ok("glucose-condensed"),
44+
"cadical" => Ok("cadical"),
45+
_ => Err(Error::new(ErrorKind::Other, "no identifiable solver detected"))
3046
};
3147
return solver_lib;
3248
}
33-
"Error: no identifiable solver detected"
49+
Err(Error::new(ErrorKind::Other, "SAT_IMPL environment variable not set"))
3450
}
3551

3652
fn main() {
@@ -49,9 +65,25 @@ fn main() {
4965

5066
let libraries_path = match get_library_build_dir() {
5167
Ok(path) => path,
52-
Err(err) => panic!("{}", err)
68+
Err(err) => panic!("Error: {}", err)
69+
};
70+
71+
let solver_lib = match get_sat_library() {
72+
Ok(solver) => solver,
73+
Err(err) => panic!("Error: {}", err)
5374
};
5475

76+
// Cadical is being built in its own directory, with the resultant artefacts being
77+
// present only there. Hence, we need to instruct cargo to look for them in cadical's
78+
// build directory, otherwise we're going to get build errors.
79+
if solver_lib == "cadical" {
80+
let cadical_build_dir = match get_cadical_build_dir() {
81+
Ok(cadical_directory) => cadical_directory,
82+
Err(err) => panic!("Error: {}", err)
83+
};
84+
println!("cargo:rustc-link-search=native={}", cadical_build_dir.display());
85+
}
86+
5587
println!("cargo:rustc-link-search=native={}", libraries_path.display());
5688
println!("cargo:rustc-link-lib=static=goto-programs");
5789
println!("cargo:rustc-link-lib=static=util");
@@ -72,7 +104,7 @@ fn main() {
72104
println!("cargo:rustc-link-lib=static=statement-list");
73105
println!("cargo:rustc-link-lib=static=goto-symex");
74106
println!("cargo:rustc-link-lib=static=pointer-analysis");
75-
println!("cargo:rustc-link-lib=static={}", get_sat_library());
107+
println!("cargo:rustc-link-lib=static={}", solver_lib);
76108
println!("cargo:rustc-link-lib=static=cbmc-lib");
77109
println!("cargo:rustc-link-lib=static=cprover-api-cpp");
78110
}

0 commit comments

Comments
 (0)