1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::env;
use std::path::Path;
fn main() {
// Get the target OS
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
// Platform-specific configuration
match target_os.as_str() {
"macos" => {
// Find LLVM libraries
let llvm_paths = vec![
"/opt/homebrew/Cellar/llvm/19.1.7/lib",
"/opt/homebrew/lib",
"/usr/local/lib",
];
for path in &llvm_paths {
if Path::new(path).exists() {
println!("cargo:rustc-link-search=native={}", path);
// Use @rpath for macOS to make binaries relocatable
println!("cargo:rustc-link-arg=-Wl,-rpath,@loader_path");
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", path);
break;
}
}
// Link against libclang dynamically with fallback paths
println!("cargo:rustc-link-lib=dylib=clang");
}
"linux" => {
// Common Linux LLVM paths
let llvm_paths = vec![
"/usr/lib/llvm-14/lib",
"/usr/lib/llvm-13/lib",
"/usr/lib/llvm-12/lib",
"/usr/lib/x86_64-linux-gnu",
"/usr/local/lib",
];
for path in &llvm_paths {
if Path::new(path).exists() {
println!("cargo:rustc-link-search=native={}", path);
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", path);
break;
}
}
}
"windows" => {
// Windows typically uses PATH for DLL resolution
// Add common LLVM installation paths
if let Ok(llvm_path) = env::var("LLVM_PATH") {
println!("cargo:rustc-link-search=native={}/lib", llvm_path);
}
}
_ => {}
}
// Rerun if environment changes
println!("cargo:rerun-if-env-changed=LLVM_PATH");
println!("cargo:rerun-if-env-changed=LIBCLANG_PATH");
}