Skip to main content

leo3_build_config/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2//! Build-time configuration for Leo3 (Rust-Lean4 bindings).
3//!
4//! This crate provides functionality to detect Lean4 installations and
5//! configure build settings appropriately. It should be called from
6//! build scripts of crates that depend on leo3.
7
8pub mod errors;
9pub mod impl_;
10
11use errors::cargo_warn;
12use std::env;
13use std::sync::OnceLock;
14
15pub use impl_::{
16    LeanAllocator, LeanConfig, LeanConfigSource, LeanVersion, ResolutionAttempt, ResolutionError,
17    ResolvedLeanConfig,
18};
19
20static LEAN_CONFIG: OnceLock<ResolvedLeanConfig> = OnceLock::new();
21
22fn warn_resolution_error(error: &ResolutionError) {
23    for line in error.warning_lines() {
24        cargo_warn!("{}", line);
25    }
26}
27
28fn emit_resolved_config(config: &LeanConfig) {
29    impl_::emit_link_config(config);
30    impl_::emit_version_cfgs(config);
31    impl_::emit_allocator_cfgs(config);
32    println!("cargo:rerun-if-changed={}", config.lean_home.display());
33}
34
35/// Main entry point: adds all Leo3 configuration to the current compilation.
36///
37/// ## Environment Variables
38///
39/// - `LEO3_NO_LEAN=1` - Skip Lean4 detection and linking (for compile-only tests)
40/// - `LEAN_HOME` - Override Lean4 installation directory
41/// - `LEAN_LIB_DIR` - Override Lean4 library directory
42/// - `LEAN_INCLUDE_DIR` - Override Lean4 include directory
43/// - `LEO3_CONFIG_FILE` - Path to a pre-built config file
44/// - `LEO3_CROSS_LIB_DIR` - Cross-compile: Lean library directory
45/// - `LEO3_CROSS_LEAN_VERSION` - Cross-compile: Lean version string
46/// - `DEP_LEAN4_LEO3_CONFIG` - Upstream propagated config from `leo3-ffi`
47///
48/// Resolution order:
49/// 1. `DEP_LEAN4_LEO3_CONFIG`
50/// 2. `LEO3_CONFIG_FILE`
51/// 3. Host discovery (`LEO3_CROSS_*` → `LEAN_HOME` → `lake` → `elan` → `PATH`)
52///
53/// Explicit higher-priority inputs are authoritative: if they are present but
54/// invalid, Leo3 reports that configuration error instead of silently falling
55/// back to a lower-priority discovery source.
56pub fn use_leo3_cfgs() {
57    impl_::print_expected_cfgs();
58    impl_::print_rerun_if_env_changed();
59
60    if env::var("LEO3_NO_LEAN").is_ok() {
61        cargo_warn!("LEO3_NO_LEAN set: skipping Lean4 detection and linking");
62        cargo_warn!("Tests requiring Lean4 runtime will not run");
63        return;
64    }
65
66    match get_lean_config_with_source() {
67        Ok(resolved) => {
68            impl_::emit_resolved_config_rerun_if_changed(&resolved);
69            emit_resolved_config(&resolved.config);
70        }
71        Err(error) => {
72            warn_resolution_error(&error);
73            cargo_warn!("Leo3 will not function without Lean4 installed");
74            cargo_warn!("Set LEO3_NO_LEAN=1 to build without Lean4 (compile-only)");
75        }
76    }
77}
78
79/// Like [`use_leo3_cfgs`], but only accepts config propagated from `leo3-ffi`.
80///
81/// This keeps `leo3` aligned with the exact Lean cfg flags chosen by the
82/// upstream `links = "lean4"` crate during `cargo publish` verification.
83pub fn use_upstream_leo3_cfgs() {
84    impl_::print_expected_cfgs();
85    impl_::print_rerun_if_env_changed();
86
87    if env::var("LEO3_NO_LEAN").is_ok() {
88        cargo_warn!("LEO3_NO_LEAN set: skipping Lean4 configuration");
89        return;
90    }
91
92    match impl_::resolve_dep_lean_config() {
93        Ok(resolved) => {
94            impl_::emit_resolved_config_rerun_if_changed(&resolved);
95            emit_resolved_config(&resolved.config)
96        }
97        Err(error) => {
98            warn_resolution_error(&error);
99            cargo_warn!("leo3 reuses the Lean4 config chosen by leo3-ffi to avoid cfg mismatches");
100        }
101    }
102}
103
104/// Resolve a `LeanConfig` with the following priority:
105///
106/// 1. `DEP_LEAN4_LEO3_CONFIG` env var (hex-encoded, set by `leo3-ffi` via `links = "lean4"`)
107/// 2. `LEO3_CONFIG_FILE` env var (path to a key=value config file)
108/// 3. Host detection (`LEO3_CROSS_*` → `LEAN_HOME` → lake → elan → PATH)
109///
110/// Explicit higher-priority inputs are authoritative.
111fn get() -> Result<ResolvedLeanConfig, ResolutionError> {
112    impl_::resolve_lean_config()
113}
114
115/// Detect Lean4 configuration (cached) with a typed error.
116pub fn resolve_lean_config() -> Result<LeanConfig, ResolutionError> {
117    get_lean_config_with_source().map(|resolved| resolved.config)
118}
119
120/// Detect Lean4 configuration (cached).
121pub fn get_lean_config() -> Result<LeanConfig, ResolutionError> {
122    resolve_lean_config()
123}
124
125/// Detect Lean4 configuration (cached) and stringify any resolution error.
126///
127/// Deprecated: prefer [`get_lean_config`] or [`get_lean_config_with_source`]
128/// for structured [`ResolutionError`] diagnostics.
129#[deprecated(note = "use get_lean_config() or get_lean_config_with_source() for typed errors")]
130pub fn get_lean_config_string() -> Result<LeanConfig, String> {
131    get_lean_config().map_err(|error| error.to_string())
132}
133
134/// Detect Lean4 configuration (cached), returning the resolution source.
135pub fn get_lean_config_with_source() -> Result<ResolvedLeanConfig, ResolutionError> {
136    if let Some(config) = LEAN_CONFIG.get() {
137        return Ok(config.clone());
138    }
139
140    let config = get()?;
141    let _ = LEAN_CONFIG.set(config.clone());
142    Ok(config)
143}
144
145#[cfg(test)]
146mod tests {
147    use super::*;
148
149    #[test]
150    fn get_lean_config_has_typed_error() {
151        let _typed: fn() -> Result<LeanConfig, ResolutionError> = get_lean_config;
152    }
153
154    #[test]
155    fn test_version_parsing() {
156        // This test requires Lean to be installed
157        // In CI, this might need to be conditional
158    }
159}