Skip to content

Commit 71190da

Browse files
authored
Merge pull request #25 from aya-rs/avoid-static-libllvm
Search all paths rather than just one
2 parents af265ec + 4fb07a1 commit 71190da

File tree

3 files changed

+55
-92
lines changed

3 files changed

+55
-92
lines changed

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ readme = "README.md"
1111
edition = "2021"
1212

1313
[dependencies]
14-
anyhow = "1.0.72"
15-
lazy_static = "1.0"
14+
once_cell = "1.18.0"
1615
libc = "0.2"
1716
libloading = "0.8.0"
1817
llvm-sys = { version = "170", features = [

src/lib.rs

+54-22
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,66 @@
4141
4242
use libloading::Library;
4343

44-
mod path;
45-
use path::find_lib_path;
46-
4744
pub mod init;
4845

49-
lazy_static::lazy_static! {
50-
static ref SHARED_LIB: Library = {
51-
let lib_path = match find_lib_path() {
52-
Ok(path) => path,
53-
54-
Err(error) => {
55-
eprintln!("{}", error);
56-
panic!();
57-
}
46+
static SHARED_LIB: once_cell::sync::Lazy<Library> = once_cell::sync::Lazy::new(|| {
47+
for (var, is_bin) in [
48+
("LD_LIBRARY_PATH", false),
49+
("DYLD_FALLBACK_LIBRARY_PATH", false),
50+
("PATH", true),
51+
] {
52+
let Some(unparsed) = std::env::var_os(var) else {
53+
continue;
5854
};
59-
60-
unsafe {
61-
match Library::new(lib_path) {
62-
Ok(path) => path,
63-
64-
Err(error) => {
65-
eprintln!("Unable to open LLVM shared lib: {}", error);
66-
panic!();
55+
let paths = std::env::split_paths(&unparsed);
56+
for mut path in paths {
57+
if is_bin {
58+
path.pop();
59+
path.push("lib");
60+
}
61+
let files = match path.read_dir() {
62+
Ok(files) => files,
63+
Err(err) => {
64+
eprintln!("unable to read dir {}: {}", path.display(), err);
65+
continue;
66+
}
67+
};
68+
for (i, file) in files.enumerate() {
69+
let file = match file {
70+
Ok(file) => file,
71+
Err(err) => {
72+
eprintln!(
73+
"unable to read dir entry {} in {}: {}",
74+
i,
75+
path.display(),
76+
err
77+
);
78+
continue;
79+
}
80+
};
81+
let path = file.path();
82+
let Some(stem) = path.file_stem() else {
83+
continue;
84+
};
85+
let Some(stem) = stem.to_str() else { continue };
86+
if stem.starts_with("libLLVM") {
87+
match unsafe { Library::new(&path) } {
88+
Ok(library) => return library,
89+
Err(error) => {
90+
eprintln!(
91+
"unable to open LLVM shared lib {}: {}",
92+
path.display(),
93+
error
94+
);
95+
continue;
96+
}
97+
}
6798
}
6899
}
69100
}
70-
};
71-
}
101+
}
102+
panic!("unable to find LLVM shared lib")
103+
});
72104

73105
/// LLVM C-API symbols with dynamic resolving.
74106
pub mod proxy {

src/path.rs

-68
This file was deleted.

0 commit comments

Comments
 (0)