Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly propagate llvm-config errors #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 25 additions & 26 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ fn main() {
}

fn run() -> Result<(), Box<dyn Error>> {
let version = llvm_config("--version")?;
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=wrapper.h");

let version = llvm_config("--version")?;
if !version.starts_with(&format!("{}.", LLVM_MAJOR_VERSION)) {
return Err(format!(
"failed to find correct version ({}.x.x) of llvm-config (found {})",
Expand All @@ -27,10 +29,9 @@ fn run() -> Result<(), Box<dyn Error>> {
.into());
}

println!("cargo:rerun-if-changed=wrapper.h");
println!("cargo:rustc-link-search={}", llvm_config("--libdir")?);

for name in fs::read_dir(llvm_config("--libdir")?)?
let lib_dir = llvm_config("--libdir")?;
println!("cargo:rustc-link-search={lib_dir}");
for name in fs::read_dir(lib_dir)?
.map(|entry| {
Ok(if let Some(name) = entry?.path().file_name() {
name.to_str().map(String::from)
Expand All @@ -53,13 +54,13 @@ fn run() -> Result<(), Box<dyn Error>> {
}
}

for name in llvm_config("--libnames")?.trim().split(' ') {
for name in llvm_config("--libnames")?.split(' ') {
if let Some(name) = trim_library_name(name) {
println!("cargo:rustc-link-lib={}", name);
}
}

for flag in llvm_config("--system-libs")?.trim().split(' ') {
for flag in llvm_config("--system-libs")?.split(' ') {
let flag = flag.trim_start_matches("-l");

if flag.starts_with('/') {
Expand Down Expand Up @@ -102,35 +103,33 @@ fn run() -> Result<(), Box<dyn Error>> {
}

fn get_system_libcpp() -> Option<&'static str> {
if cfg!(target_env = "msvc") {
if env::var("CARGO_CFG_TARGET_ENV").ok()? == "msvc" {
None
} else if cfg!(target_os = "macos") {
} else if env::var("CARGO_CFG_TARGET_VENDOR").ok()? == "apple" {
Some("c++")
} else {
Some("stdc++")
}
}

fn llvm_config(argument: &str) -> Result<String, Box<dyn Error>> {
let prefix = env::var(format!("MLIR_SYS_{}0_PREFIX", LLVM_MAJOR_VERSION))
let prefix = env::var_os(format!("MLIR_SYS_{}0_PREFIX", LLVM_MAJOR_VERSION))
.map(|path| Path::new(&path).join("bin"))
.unwrap_or_default();
let call = format!(
"{} --link-static {}",
prefix.join("llvm-config").display(),
argument
);

Ok(str::from_utf8(
&if cfg!(target_os = "windows") {
Command::new("cmd").args(["/C", &call]).output()?
} else {
Command::new("sh").arg("-c").arg(&call).output()?
}
.stdout,
)?
.trim()
.to_string())
let mut cmd = Command::new(prefix.join("llvm-config"));
cmd.arg("--link-static").arg(argument);
let output = cmd
.output()
.map_err(|e| format!("failed to run `{cmd:?}`: {e}"))?;
if !output.status.success() {
return Err(format!(
"failed to run `{cmd:?}`: {}; stderr:\n{}",
output.status,
String::from_utf8_lossy(&output.stderr),
)
.into());
}
Ok(str::from_utf8(&output.stdout)?.trim().to_string())
}

fn trim_library_name(name: &str) -> Option<&str> {
Expand Down