Skip to content

Commit 45d1e79

Browse files
committed
Merge pull request #6 from jdtatz/ir_reader_impl
Robust method for finding the host arch & Expose the rest of the llvm c api
2 parents 20815b5 + 149ab5c commit 45d1e79

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

build.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ mod llvm {
5454
"linker.rs",
5555
"bit_reader.rs",
5656
"bit_writer.rs",
57+
"ir_reader.rs",
58+
"disassembler.rs",
59+
"error_handling.rs",
60+
"initialization.rs",
61+
"link_time_optimizer.rs",
62+
"lto.rs",
63+
"object.rs",
64+
"orc.rs",
65+
"support.rs",
5766
"target.rs",
5867
"target_machine.rs",
5968
"transforms/ipo.rs",

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ pub mod proxy {
7777
use llvm_sys::analysis::*;
7878
use llvm_sys::debuginfo::*;
7979
use llvm_sys::execution_engine::*;
80+
use llvm_sys::disassembler::*;
81+
use llvm_sys::error_handling::*;
82+
use llvm_sys::link_time_optimizer::*;
83+
use llvm_sys::lto::*;
84+
use llvm_sys::object::*;
85+
use llvm_sys::orc::*;
8086
use llvm_sys::prelude::*;
8187
use llvm_sys::target::*;
8288
use llvm_sys::target_machine::*;

src/path.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::env;
2-
use std::path::PathBuf;
2+
use std::path::{Path, PathBuf};
33
use std::process::Command;
44

55
use failure::Error;
@@ -89,7 +89,7 @@ fn collect_possible_paths() -> Result<Vec<PathBuf>, Error> {
8989
cargo_path.pop();
9090

9191
if let Some(toolchain) = cargo_path.file_name() {
92-
let arch = extract_arch(toolchain.to_str().unwrap());
92+
let arch = env::var("HOST").unwrap_or_else(|_| extract_arch(toolchain.to_str().unwrap()));
9393

9494
paths.push(
9595
cargo_path
@@ -101,6 +101,21 @@ fn collect_possible_paths() -> Result<Vec<PathBuf>, Error> {
101101
}
102102
}
103103

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+
104119
if let Ok(output) = Command::new("rustup").args(&["which", "rustc"]).output() {
105120
let mut rustc_path = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
106121
rustc_path.pop();
@@ -122,6 +137,8 @@ fn collect_possible_paths() -> Result<Vec<PathBuf>, Error> {
122137
Ok(paths)
123138
}
124139

140+
// Fails if using nightly build from a specific date
141+
// e.g. nightly-2018-11-30-x86_64-unknown-linux-gnu
125142
fn extract_arch(toolchain: &str) -> String {
126143
toolchain
127144
.split('-')
@@ -135,3 +152,30 @@ fn extract_arch(toolchain: &str) -> String {
135152
.collect::<Vec<_>>()
136153
.join("-")
137154
}
155+
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
180+
}
181+
}

0 commit comments

Comments
 (0)