1#![cfg_attr(docsrs, feature(doc_cfg))]
2pub 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
35pub 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
79pub 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
104fn get() -> Result<ResolvedLeanConfig, ResolutionError> {
112 impl_::resolve_lean_config()
113}
114
115pub fn resolve_lean_config() -> Result<LeanConfig, ResolutionError> {
117 get_lean_config_with_source().map(|resolved| resolved.config)
118}
119
120pub fn get_lean_config() -> Result<LeanConfig, ResolutionError> {
122 resolve_lean_config()
123}
124
125#[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
134pub 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 }
159}