Skip to content

Commit 1c16f95

Browse files
authored
Merge pull request #7 from denzp/libllvm
Use `libLLVM` instead of `librustc_codegen_llvm`
2 parents 03b5be4 + 27243b3 commit 1c16f95

File tree

4 files changed

+33
-143
lines changed

4 files changed

+33
-143
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
[package]
22
name = "rustc-llvm-proxy"
3-
version = "0.1.11"
3+
version = "0.2.0"
44
authors = ["Denys Zariaiev <denys.zariaiev@gmail.com>"]
55
license = "MIT"
6+
edition = "2018"
67

78
readme = "README.md"
89
description = "Dynamically proxy LLVM calls into Rust own shared library"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Then all you need to do is to include the crate into your project:
2828

2929
``` toml
3030
[dependencies]
31-
rustc-llvm-proxy = "0.1"
31+
rustc-llvm-proxy = "0.2"
3232
```
3333

3434
``` rust

appveyor.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ environment:
44
matrix:
55
- TARGET: x86_64-pc-windows-gnu
66
- TARGET: i686-pc-windows-gnu
7+
- TARGET: x86_64-pc-windows-msvc
8+
- TARGET: i686-pc-windows-msvc
79

810
install:
911
- appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe

src/path.rs

Lines changed: 28 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,182 +1,69 @@
11
use std::env;
2+
use std::fs::read_dir;
23
use std::path::{Path, PathBuf};
34
use std::process::Command;
45

56
use failure::Error;
67

78
pub fn find_lib_path() -> Result<PathBuf, Error> {
8-
let paths = collect_possible_paths()?;
9+
let directories = collect_possible_directories();
910

10-
if paths.is_empty() {
11+
if directories.is_empty() {
1112
bail!("Unable to find possible LLVM shared lib locations.");
1213
}
1314

14-
for path in &paths {
15-
if path.join("librustc_codegen_llvm-llvm.so").exists() {
16-
return Ok(path.join("librustc_codegen_llvm-llvm.so"));
17-
}
18-
19-
if path.join("librustc_codegen_llvm-llvm.dylib").exists() {
20-
return Ok(path.join("librustc_codegen_llvm-llvm.dylib"));
21-
}
22-
23-
if path.join("rustc_codegen_llvm-llvm.dll").exists() {
24-
return Ok(path.join("rustc_codegen_llvm-llvm.dll"));
15+
for directory in &directories {
16+
if let Some(library) = find_library_in_directory(&directory) {
17+
return Ok(library);
2518
}
2619
}
2720

2821
bail!(
2922
"Unable to find LLVM shared lib in possible locations:\n- {}",
30-
paths
23+
directories
3124
.into_iter()
3225
.map(|item| item.to_str().unwrap().to_owned())
3326
.collect::<Vec<_>>()
3427
.join("\n- ")
3528
);
3629
}
3730

38-
fn collect_possible_paths() -> Result<Vec<PathBuf>, Error> {
31+
fn collect_possible_directories() -> Vec<PathBuf> {
3932
let mut paths = vec![];
40-
41-
// Special case: find the location for Rust built from sources.
42-
if let Ok(env_path) = env::var("PATH") {
43-
for item in env_path.split(':') {
44-
let mut rustc_path = PathBuf::from(item);
45-
46-
rustc_path.pop();
47-
paths.push(rustc_path.join("codegen-backends"));
48-
}
49-
}
50-
51-
if let Ok(rustup_home) = env::var("RUSTUP_HOME") {
52-
let rustup_home = PathBuf::from(rustup_home);
53-
let rustup_toolchain = env::var("RUSTUP_TOOLCHAIN")?;
54-
let rustup_arch = extract_arch(&rustup_toolchain);
55-
56-
paths.push(
57-
rustup_home
58-
.join("toolchains")
59-
.join(&rustup_toolchain)
60-
.join("lib")
61-
.join("rustlib")
62-
.join(rustup_arch)
63-
.join("codegen-backends"),
64-
);
65-
}
33+
let separator = if cfg!(windows) { ';' } else { ':' };
6634

6735
if let Ok(lib_paths) = env::var("LD_LIBRARY_PATH") {
68-
for item in lib_paths.split(':') {
69-
let mut possible_path = PathBuf::from(item);
70-
possible_path.pop();
71-
72-
if let Some(possible_toolchain) = possible_path.file_name() {
73-
let possible_arch = extract_arch(possible_toolchain.to_str().unwrap());
74-
75-
paths.push(
76-
possible_path
77-
.join("lib")
78-
.join("rustlib")
79-
.join(possible_arch)
80-
.join("codegen-backends"),
81-
);
82-
}
36+
for item in lib_paths.split(separator) {
37+
paths.push(PathBuf::from(item));
8338
}
8439
}
8540

86-
if let Ok(cargo) = env::var("CARGO") {
87-
let mut cargo_path = PathBuf::from(cargo);
88-
cargo_path.pop();
89-
cargo_path.pop();
90-
91-
if let Some(toolchain) = cargo_path.file_name() {
92-
let arch =
93-
env::var("HOST").unwrap_or_else(|_| extract_arch(toolchain.to_str().unwrap()));
94-
95-
paths.push(
96-
cargo_path
97-
.join("lib")
98-
.join("rustlib")
99-
.join(arch)
100-
.join("codegen-backends"),
101-
);
41+
if let Ok(lib_paths) = env::var("DYLD_FALLBACK_LIBRARY_PATH") {
42+
for item in lib_paths.split(separator) {
43+
paths.push(PathBuf::from(item));
10244
}
10345
}
10446

105-
if let Some(rustc) = find_rustc() {
106-
if let Ok(output) = Command::new(&rustc).args(&["--print", "sysroot"]).output() {
107-
let sysroot = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
108-
if let Some(arch) = find_arch(&rustc, &sysroot) {
109-
paths.push(
110-
sysroot
111-
.join("lib")
112-
.join("rustlib")
113-
.join(arch)
114-
.join("codegen-backends"),
115-
);
116-
}
117-
}
118-
}
119-
120-
if let Ok(output) = Command::new("rustup").args(&["which", "rustc"]).output() {
121-
let mut rustc_path = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
122-
rustc_path.pop();
123-
rustc_path.pop();
124-
125-
if let Some(toolchain) = rustc_path.file_name() {
126-
let arch = extract_arch(toolchain.to_str().unwrap());
47+
if let Ok(bin_paths) = env::var("PATH") {
48+
for item in bin_paths.split(separator) {
49+
let mut possible_path = PathBuf::from(item);
12750

128-
paths.push(
129-
rustc_path
130-
.join("lib")
131-
.join("rustlib")
132-
.join(arch)
133-
.join("codegen-backends"),
134-
);
51+
possible_path.pop();
52+
possible_path.push("lib");
53+
paths.push(possible_path);
13554
}
13655
}
13756

138-
Ok(paths)
57+
paths
13958
}
14059

141-
// Fails if using nightly build from a specific date
142-
// e.g. nightly-2018-11-30-x86_64-unknown-linux-gnu
143-
fn extract_arch(toolchain: &str) -> String {
144-
toolchain
145-
.split('-')
146-
// Skip `nightly` rust version prefix.
147-
.skip(1)
148-
// Also skip rust version specification if exists.
149-
.skip_while(|item| match item.chars().next() {
150-
None | Some('0'..='9') => true,
151-
_ => false,
152-
})
153-
.collect::<Vec<_>>()
154-
.join("-")
155-
}
60+
fn find_library_in_directory(directory: &Path) -> Option<PathBuf> {
61+
match read_dir(directory) {
62+
Ok(files) => files
63+
.filter_map(Result::ok)
64+
.find(|file| file.file_name().to_string_lossy().starts_with("libLLVM"))
65+
.map(|file| file.path()),
15666

157-
fn find_rustc() -> Option<PathBuf> {
158-
if let Some(path) = env::var_os("RUSTC") {
159-
Some(path.into())
160-
} else if let Ok(output) = Command::new("rustup").args(&["which", "rustc"]).output() {
161-
Some(String::from_utf8_lossy(&output.stdout).trim().into())
162-
} else {
163-
None
164-
}
165-
}
166-
167-
fn find_arch(rustc: &Path, sysroot: &Path) -> Option<String> {
168-
if let Ok(path) = env::var("HOST") {
169-
Some(path)
170-
} else if let Ok(output) = Command::new(&rustc).args(&["-vV"]).output() {
171-
for line in String::from_utf8_lossy(&output.stdout).lines() {
172-
if line.starts_with("host") {
173-
return Some(line.trim_start_matches("host:").trim().to_string());
174-
}
175-
}
176-
None
177-
} else if let Some(toolchain) = sysroot.file_name() {
178-
Some(extract_arch(toolchain.to_str().unwrap()))
179-
} else {
180-
None
67+
Err(_) => None,
18168
}
18269
}

0 commit comments

Comments
 (0)