Skip to content

Commit

Permalink
feat(cadical): include source for newest version
Browse files Browse the repository at this point in the history
  • Loading branch information
chrjabs committed Feb 21, 2025
1 parent 6dcec86 commit b166879
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 132 deletions.
4 changes: 4 additions & 0 deletions cadical/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ include = [
"/patches/",
"/examples/",
"/cpp-extension/",
"/cppsrc/src/",
"/cppsrc/README.md",
"/cppsrc/LICENSE",
"/cppsrc/VERSION",
]

build = "build.rs"
Expand Down
12 changes: 10 additions & 2 deletions cadical/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ Armin Biere's SAT solver [CaDiCaL](https://github.com/arminbiere/cadical) to be
## CaDiCaL Versions

CaDiCaL versions can be selected via cargo crate features.
All CaDiCaL versions up to [Version 2.1.3](https://github.com/arminbiere/cadical/releases/tag/rel-2.1.3) are available.
For the full list of versions and the changelog see [the CaDiCaL releases](https://github.com/arminbiere/cadical/releases).
All CaDiCaL versions from
[Version 1.5.0](https://github.com/arminbiere/cadical/releases/tag/rel-1.5.0)
up to
[Version 2.1.3](https://github.com/arminbiere/cadical/releases/tag/rel-2.1.3)
are available. For the full list of versions and the changelog see
[the CaDiCaL releases](https://github.com/arminbiere/cadical/releases).

Without any features selected, the newest version will be used.
If conflicting CaDiCaL versions are requested, the newest requested version will be selected.

If the determined version is _not_ the newest available, and no custom source directory is
specified (see customization below), the CaDiCaL source code is downloaded at compile time,
which requires network access.

## Customization

In order to build a custom version of CaDiCaL, this crate supports two environment variables to
Expand Down
91 changes: 64 additions & 27 deletions cadical/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,40 @@ enum Version {
// Don't forget to update the crate documentation when adding a newer version
}

/// Checks if the version was set manually via a feature
macro_rules! version_set_manually {
() => {
cfg!(any(
feature = "v2-1-3",
feature = "v2-1-2",
feature = "v2-1-1",
feature = "v2-1-0",
feature = "v2-0-0",
feature = "v1-9-5",
feature = "v1-9-4",
feature = "v1-9-3",
feature = "v1-9-2",
feature = "v1-9-1",
feature = "v1-9-0",
feature = "v1-8-0",
feature = "v1-7-5",
feature = "v1-7-4",
feature = "v1-7-3",
feature = "v1-7-2",
feature = "v1-7-1",
feature = "v1-7-0",
feature = "v1-6-0",
feature = "v1-5-6",
feature = "v1-5-5",
feature = "v1-5-4",
feature = "v1-5-3",
feature = "v1-5-2",
feature = "v1-5-1",
feature = "v1-5-0",
))
};
}

impl Version {
fn determine() -> Self {
if cfg!(feature = "v2-1-3") {
Expand Down Expand Up @@ -181,13 +215,6 @@ fn main() {

let version = Version::determine();

if std::env::var("DOCS_RS").is_ok() {
// don't build c++ library on docs.rs due to network restrictions
// instead, only generate bindings from included header file
generate_bindings("cpp-extension/dummy-ccadical.h", version, &out_dir);
return;
}

#[cfg(all(feature = "quiet", feature = "logging"))]
compile_error!("cannot combine cadical features quiet and logging");

Expand Down Expand Up @@ -216,7 +243,7 @@ fn main() {
}
}

let cadical_dir = get_cadical_dir(None);
let cadical_dir = get_cadical_dir(version, None);

generate_bindings(&format!("{cadical_dir}/src/ccadical.h"), version, &out_dir);

Expand Down Expand Up @@ -269,26 +296,36 @@ fn generate_bindings(header_path: &str, version: Version, out_dir: &str) {
.expect("Could not write ffi bindings");
}

fn get_cadical_dir(remote: Option<(&str, &str, Version)>) -> String {
std::env::var("CADICAL_SRC_DIR").unwrap_or_else(|_| {
let out_dir = env::var("OUT_DIR").unwrap();
let mut tmp = out_dir.clone();
tmp.push_str("/cadical");
if let Some((repo, branch, version)) = remote {
update_repo(
Path::new(&tmp),
repo,
branch,
version.reference(),
Path::new("patches").join(version.patch()),
);
fn get_cadical_dir(version: Version, remote: Option<(&str, &str)>) -> String {
if let Ok(src_dir) = std::env::var("CADICAL_SRC_DIR") {
if version_set_manually!() {
println!("cargo:warning=Both version feature and CADICAL_SRC_DIR. It is your responsibility to ensure that they make sense together.");
}
tmp
})
return src_dir;
}

if version == Version::default() {
// the sources for the default version are included with the crate and do not need to be
// cloned
return String::from("cppsrc");
}

let mut src_dir = env::var("OUT_DIR").unwrap();
src_dir.push_str("/cadical");
if let Some((repo, branch)) = remote {
update_repo(
Path::new(&src_dir),
repo,
branch,
version.reference(),
Path::new("patches").join(version.patch()),
);
}
src_dir
}

fn build(repo: &str, branch: &str, version: Version) {
let cadical_dir_str = get_cadical_dir(Some((repo, branch, version)));
let cadical_dir_str = get_cadical_dir(version, Some((repo, branch)));
let cadical_dir = Path::new(&cadical_dir_str);
// We specify the build manually here instead of calling make for better portability
let src_files = glob(&format!("{cadical_dir_str}/src/*.cpp"))
Expand All @@ -313,13 +350,13 @@ fn build(repo: &str, branch: &str, version: Version) {
.opt_level(0)
.define("DEBUG", None)
.warnings(true)
.debug(true)
.define("NCONTRACTS", None) // --no-contracts
.define("NTRACING", None); // --no-tracing
.debug(true);
} else {
cadical_build
.opt_level(3)
.define("NDEBUG", None)
.define("NCONTRACTS", None) // --no-contracts
.define("NTRACING", None) // --no-tracing
.warnings(false);
}
#[cfg(feature = "quiet")]
Expand Down
72 changes: 0 additions & 72 deletions cadical/cpp-extension/dummy-ccadical.h

This file was deleted.

12 changes: 10 additions & 2 deletions cadical/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@
//! ## CaDiCaL Versions
//!
//! CaDiCaL versions can be selected via cargo crate features.
//! All CaDiCaL versions up to [Version 2.1.3](https://github.com/arminbiere/cadical/releases/tag/rel-2.1.3) are available.
//! For the full list of versions and the changelog see [the CaDiCaL releases](https://github.com/arminbiere/cadical/releases).
//! All CaDiCaL versions from
//! [Version 1.5.0](https://github.com/arminbiere/cadical/releases/tag/rel-1.5.0)
//! up to
//! [Version 2.1.3](https://github.com/arminbiere/cadical/releases/tag/rel-2.1.3)
//! are available. For the full list of versions and the changelog see
//! [the CaDiCaL releases](https://github.com/arminbiere/cadical/releases).
//!
//! Without any features selected, the newest version will be used.
//! If conflicting CaDiCaL versions are requested, the newest requested version will be selected.
//!
//! If the determined version is _not_ the newest available, and no custom source directory is
//! specified (see customization below), the CaDiCaL source code is downloaded at compile time,
//! which requires network access.
//!
//! ## Customization
//!
//! In order to build a custom version of CaDiCaL, this crate supports two environment variables to
Expand Down
Loading

0 comments on commit b166879

Please sign in to comment.