z3_sys/lib.rs
1//! # Z3
2//!
3//! Z3 is a theorem prover [from Microsoft Research](https://github.com/Z3Prover/z3/).
4//!
5//! This crate provides low-level bindings that provide no real
6//! affordances for usage from Rust and that mirror the C API.
7//!
8//! For bindings that are easier to use from Rust, see the higher level
9//! bindings in the [Z3](https://crates.io/crates/z3/) crate.
10//!
11//! # Build configuration
12//!
13//! This crate needs to locate a Z3 library at build time. The method is
14//! controlled by Cargo features and environment variables.
15//!
16//! ## Features
17//!
18//! | Feature | Behaviour |
19//! |---------|-----------|
20//! | *(default)* | Link against a system-installed Z3 (`brew install z3`, etc.) |
21//! | `vendored` | Build Z3 from source using `cmake` and statically link it |
22//! | `bundled` | **Deprecated.** Alias for `vendored`; will be removed in a future release |
23//! | `gh-release` | Build against a pre-compiled Z3 static library from GitHub Releases |
24//! | `vcpkg` | Use a Z3 installed via vcpkg |
25//! | `bindgen` | Regenerate `functions.rs` from local Z3 headers at build time |
26//!
27//! ## Environment variables
28//!
29//! ### `vendored` feature
30//!
31//! The `vendored` feature builds Z3 from source using `cmake` and statically
32//! links it. The Z3 source tree is shipped inside the [`z3-src`] crate and
33//! compiled locally — no network access is required at build time.
34//!
35//! By default the Z3 version built is whatever version `z3-src` bundles. To use
36//! a specific version without managing source yourself, `gh-release` with
37//! `Z3_SYS_Z3_VERSION` is a simpler alternative.
38//!
39//! **Using your own Z3 checkout:** Set `Z3_SRC_SOURCE_DIR` to the **absolute
40//! path** of a Z3 source tree:
41//!
42//! ```text
43//! Z3_SRC_SOURCE_DIR=/absolute/path/to/z3 cargo build
44//! ```
45//!
46//! To use a **project-relative** path, add the following to your project's
47//! `.cargo/config.toml`. The `relative = true` key tells Cargo to resolve
48//! the path relative to the config file's location:
49//!
50//! ```toml
51//! [env]
52//! Z3_SRC_SOURCE_DIR = { value = "path/to/z3", relative = true }
53//! ```
54//!
55//! > **Note:** A `z3` directory in your own project or workspace is **not**
56//! > picked up automatically — you must set `Z3_SRC_SOURCE_DIR` explicitly,
57//! > even if you have a Z3 git submodule in your repo.
58//!
59//! [`z3-src`]: https://crates.io/crates/z3-src
60//!
61//! ### Other variables
62//!
63//! | Variable | Feature | Description |
64//! |----------|---------|-------------|
65//! | `Z3_SYS_Z3_HEADER` | `bindgen` | Path to `z3.h`; defaults to `z3-src/z3/src/api/z3.h` |
66//! | `Z3_SYS_UPDATE_GENERATED` | `bindgen` | Set to `1` to also write `src/generated/functions.rs` and `src/generated/enums.rs` |
67//! | `Z3_LIBRARY_PATH_OVERRIDE` | default | Add an extra library search path for the linker |
68//! | `Z3_SYS_Z3_VERSION` | `gh-release` | Z3 version to download (e.g. `4.13.0`) |
69//! | `READ_ONLY_GITHUB_TOKEN` | `gh-release` | GitHub PAT to avoid API rate limits in CI |
70//! | `CXXSTDLIB` | any | Override which C++ standard library to link (e.g. `c++`, `stdc++`) |
71//!
72//! # Example
73//!
74//! ```
75//! use z3_sys::*;
76//!
77//! unsafe {
78//! let cfg = Z3_mk_config().unwrap();
79//! let ctx = Z3_mk_context(cfg).unwrap();
80//!
81//! let a = Z3_mk_not(ctx, Z3_mk_eq(ctx, Z3_mk_false(ctx).unwrap(), Z3_mk_true(ctx).unwrap()).unwrap()).unwrap();
82//! let b = Z3_mk_not(ctx, Z3_mk_iff(ctx, Z3_mk_false(ctx).unwrap(), Z3_mk_true(ctx).unwrap()).unwrap()).unwrap();
83//! assert_eq!(Z3_mk_true(ctx), Z3_simplify(ctx, a));
84//! assert_eq!(Z3_mk_true(ctx), Z3_simplify(ctx, b));
85//!
86//! Z3_del_config(cfg);
87//! Z3_del_context(ctx);
88//! }
89//! ```
90
91#![allow(non_camel_case_types)]
92#![allow(clippy::unreadable_literal)]
93// Generated C API docs don't conform to Rust doc formatting conventions.
94#![allow(clippy::doc_markdown)]
95#![allow(clippy::doc_lazy_continuation)]
96#![no_std]
97
98mod types;
99
100pub use types::*;
101
102#[cfg(not(feature = "bindgen"))]
103include!("generated/functions.rs");
104
105#[cfg(not(feature = "bindgen"))]
106include!("generated/enums.rs");
107
108#[cfg(feature = "bindgen")]
109include!(concat!(env!("OUT_DIR"), "/functions.rs"));
110
111#[cfg(feature = "bindgen")]
112include!(concat!(env!("OUT_DIR"), "/enums.rs"));
113
114include!("functions_patched.rs");
115
116#[cfg(not(windows))]
117#[link(name = "z3")]
118unsafe extern "C" {}
119
120#[cfg(windows)]
121#[link(name = "libz3")]
122unsafe extern "C" {}