Skip to content

Commit 2f2c34e

Browse files
committed
Use libLLVM instead of librustc_codegen_llvm
1 parent 87daab0 commit 2f2c34e

File tree

6 files changed

+36
-146
lines changed

6 files changed

+36
-146
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ matrix:
66
- os: linux
77
rust: nightly
88

9+
- os: linux
10+
rust: nightly-2019-04-20
11+
912
- os: linux
1013
rust: stable
1114

1215
- os: osx
1316
rust: nightly
1417

18+
- os: osx
19+
rust: nightly-2019-04-20
20+
1521
- os: osx
1622
rust: stable
1723

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.9"
3+
version = "0.2.0"
44
authors = ["Denys Zariaiev <[email protected]>"]
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/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![deny(warnings)]
2-
#![allow(non_snake_case, unused_imports, unused_macros)]
2+
#![allow(non_snake_case, unused_imports, unused_macros, unused_attributes)]
33

44
//! Dynamically proxy LLVM calls into Rust own shared library! 🎉
55
//!
@@ -76,9 +76,9 @@ pub mod proxy {
7676

7777
use llvm_sys::analysis::*;
7878
use llvm_sys::debuginfo::*;
79-
use llvm_sys::execution_engine::*;
8079
use llvm_sys::disassembler::*;
8180
use llvm_sys::error_handling::*;
81+
use llvm_sys::execution_engine::*;
8282
use llvm_sys::link_time_optimizer::*;
8383
use llvm_sys::lto::*;
8484
use llvm_sys::object::*;

src/path.rs

Lines changed: 23 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,181 +1,62 @@
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![];
4033

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-
}
66-
6734
if let Ok(lib_paths) = env::var("LD_LIBRARY_PATH") {
6835
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+
paths.push(PathBuf::from(item));
8337
}
8438
}
8539

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 = env::var("HOST").unwrap_or_else(|_| extract_arch(toolchain.to_str().unwrap()));
93-
94-
paths.push(
95-
cargo_path
96-
.join("lib")
97-
.join("rustlib")
98-
.join(arch)
99-
.join("codegen-backends"),
100-
);
101-
}
102-
}
103-
104-
if let Some(rustc) = find_rustc() {
105-
if let Ok(output) = Command::new(&rustc).args(&["--print", "sysroot"]).output() {
106-
let mut sysroot = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
107-
if let Some(arch) = find_arch(&rustc, &sysroot) {
108-
paths.push(
109-
sysroot
110-
.join("lib")
111-
.join("rustlib")
112-
.join(arch)
113-
.join("codegen-backends"),
114-
);
115-
}
116-
}
117-
}
118-
119-
if let Ok(output) = Command::new("rustup").args(&["which", "rustc"]).output() {
120-
let mut rustc_path = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
121-
rustc_path.pop();
122-
rustc_path.pop();
123-
124-
if let Some(toolchain) = rustc_path.file_name() {
125-
let arch = extract_arch(toolchain.to_str().unwrap());
40+
if let Ok(bin_paths) = env::var("PATH") {
41+
for item in bin_paths.split(':') {
42+
let mut possible_path = PathBuf::from(item);
12643

127-
paths.push(
128-
rustc_path
129-
.join("lib")
130-
.join("rustlib")
131-
.join(arch)
132-
.join("codegen-backends"),
133-
);
44+
possible_path.pop();
45+
possible_path.push("lib");
46+
paths.push(possible_path);
13447
}
13548
}
13649

137-
Ok(paths)
50+
paths
13851
}
13952

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

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

0 commit comments

Comments
 (0)