|
7 | 7 | //! Zig FFI layer (which delegates to Idris 2 compiled code). |
8 | 8 |
|
9 | 9 | fn main() { |
10 | | - // Development build path: relative to bindings/rust/ within the repo |
11 | | - println!("cargo:rustc-link-search=native=../../ffi/zig/zig-out/lib"); |
12 | | - |
13 | | - // System-wide installation paths |
14 | | - println!("cargo:rustc-link-search=native=/usr/local/lib"); |
15 | | - println!("cargo:rustc-link-search=native=/usr/lib"); |
| 10 | + // 1. Determine library search paths |
| 11 | + let mut search_paths = vec![ |
| 12 | + "../../ffi/zig/zig-out/lib".to_string(), |
| 13 | + "/usr/local/lib".to_string(), |
| 14 | + "/usr/lib".to_string(), |
| 15 | + ]; |
16 | 16 |
|
17 | 17 | // Allow override via PROVEN_LIB_DIR environment variable |
18 | | - if let Ok(lib_dir) = std::env::var("PROVEN_LIB_DIR") { |
19 | | - println!("cargo:rustc-link-search=native={}", lib_dir); |
| 18 | + if let Ok(env_path) = std::env::var("PROVEN_LIB_DIR") { |
| 19 | + search_paths.insert(0, env_path); |
| 20 | + } |
| 21 | + |
| 22 | + for path in &search_paths { |
| 23 | + println!("cargo:rustc-link-search=native={}", path); |
20 | 24 | } |
21 | 25 |
|
22 | | - // Link against libproven (the compiled Zig/Idris2 library). |
| 26 | + // 2. Link against libproven (the compiled Zig/Idris2 library). |
23 | 27 | // Prefer static linking if libproven.a is found (required for ClusterFuzzLite). |
24 | | - let is_static = std::path::Path::new(&lib_dir_path).join("libproven.a").exists(); |
| 28 | + let mut is_static = false; |
| 29 | + for path in &search_paths { |
| 30 | + if std::path::Path::new(path).join("libproven.a").exists() { |
| 31 | + is_static = true; |
| 32 | + break; |
| 33 | + } |
| 34 | + } |
| 35 | + |
25 | 36 | if is_static { |
26 | 37 | println!("cargo:rustc-link-lib=static=proven"); |
27 | 38 | } else { |
28 | 39 | println!("cargo:rustc-link-lib=dylib=proven"); |
29 | 40 | } |
30 | 41 |
|
31 | | - // Add RPATH so binaries can find libproven.so relative to themselves (e.g. in ./lib/ or ./) |
32 | | - // This is required for ClusterFuzzLite and standalone distributions. |
| 42 | + // 3. Add RPATH so binaries can find libproven.so relative to themselves (e.g. in ./lib/ or ./) |
| 43 | + // This is required for standalone distributions using dynamic linking. |
33 | 44 | if std::env::var("CARGO_CFG_TARGET_OS").ok() == Some("linux".to_string()) { |
34 | 45 | println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN/lib"); |
35 | 46 | println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN"); |
36 | 47 | } |
37 | 48 |
|
38 | | - // Re-run build script if the library changes |
| 49 | + // Re-run build script if relevant files change |
39 | 50 | println!("cargo:rerun-if-env-changed=PROVEN_LIB_DIR"); |
40 | 51 | println!("cargo:rerun-if-changed=../../ffi/zig/zig-out/lib/libproven.so"); |
41 | | - println!("cargo:rerun-if-changed=../../ffi/zig/zig-out/lib/libproven.dylib"); |
| 52 | + println!("cargo:rerun-if-changed=../../ffi/zig/zig-out/lib/libproven.a"); |
42 | 53 | } |
0 commit comments