|
41 | 41 |
|
42 | 42 | use libloading::Library;
|
43 | 43 |
|
44 |
| -mod path; |
45 |
| -use path::find_lib_path; |
46 |
| - |
47 | 44 | pub mod init;
|
48 | 45 |
|
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; |
58 | 54 | };
|
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 | + } |
67 | 98 | }
|
68 | 99 | }
|
69 | 100 | }
|
70 |
| - }; |
71 |
| -} |
| 101 | + } |
| 102 | + panic!("unable to find LLVM shared lib") |
| 103 | +}); |
72 | 104 |
|
73 | 105 | /// LLVM C-API symbols with dynamic resolving.
|
74 | 106 | pub mod proxy {
|
|
0 commit comments