Skip to content

Commit

Permalink
z3-sys: Use pkg-config when using system libs.
Browse files Browse the repository at this point in the history
This helps find the lib and includes more accurately than not
using it.

This builds on previous work by @poscat0x04 and @wtdcode.
  • Loading branch information
waywardmonkeys committed Nov 25, 2023
1 parent 985bf03 commit 50331a4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions z3-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ repository = "https://github.com/prove-rs/z3.rs.git"
[build-dependencies]
bindgen = { version = "0.69", default-features = false, features = ["runtime"] }
cmake = { version = "0.1.49", optional = true }
pkg-config = "0.3.27"
vcpkg = { version = "0.2.15", optional = true }

[features]
Expand Down
24 changes: 19 additions & 5 deletions z3-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
use std::env;
use std::{env, path::PathBuf};

fn main() {
// Don't need to specify this for these build configurations.
#[cfg(any(feature = "bundled", feature = "vcpkg"))]
let search_paths = vec![];

#[cfg(feature = "bundled")]
build_bundled_z3();

#[cfg(not(any(feature = "bundled", feature = "vcpkg")))]
let search_paths = {
if let Ok(lib) = pkg_config::Config::new().probe("z3") {
lib.include_paths
} else {
vec![]
}
};

#[cfg(feature = "deprecated-static-link-z3")]
println!("cargo:warning=The 'static-link-z3' feature is deprecated. Please use the 'bundled' feature.");

Expand All @@ -17,7 +30,7 @@ fn main() {

link_against_cxx_stdlib();

generate_binding(&header);
generate_binding(&header, &search_paths);
}

fn link_against_cxx_stdlib() {
Expand Down Expand Up @@ -82,8 +95,8 @@ fn find_header_by_env() -> String {
header
}

fn generate_binding(header: &str) {
let out_path = std::path::PathBuf::from(env::var("OUT_DIR").unwrap());
fn generate_binding(header: &str, search_paths: &[PathBuf]) {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());

for x in &[
"ast_kind",
Expand All @@ -101,7 +114,8 @@ fn generate_binding(header: &str) {
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate_comments(false)
.rustified_enum(format!("Z3_{x}"))
.allowlist_type(format!("Z3_{x}"));
.allowlist_type(format!("Z3_{x}"))
.clang_args(search_paths.iter().map(|p| format!("-I{}", p.display())));
if env::var("TARGET").unwrap() == "wasm32-unknown-emscripten" {
enum_bindings = enum_bindings.clang_arg(format!(
"--sysroot={}/upstream/emscripten/cache/sysroot",
Expand Down

0 comments on commit 50331a4

Please sign in to comment.