extern crate gcc;
extern crate semver;
use semver::{Version, VersionReq};
use semver::ParseError::IncorrectParse;
use std::process::Command;
fn llvm_config(arg: &str) -> String {
let stdout = Command::new("llvm-config")
.arg(arg)
.output()
.unwrap_or_else(|e| panic!("Couldn't execute llvm-config. Error: {}", e))
.stdout;
String::from_utf8(stdout).ok().expect("llvm-config output was not UTF-8.")
}
fn llvm_version() -> Version {
match Version::parse(&llvm_config("--version")) {
Err(IncorrectParse(v, _)) | Ok(v) => v,
_ => panic!("Could not determine LLVM version from llvm-config."),
}
}
fn main() {
let minimum_llvm_version = VersionReq::parse(">=3.6").unwrap();
let version = llvm_version();
if minimum_llvm_version.matches(&version) {
println!("Found LLVM version {}", version);
} else {
panic!("LLVM version 3.6 or higher is required. (Found {})", version);
};
for arg in llvm_config("--ldflags").split_whitespace() {
if arg.starts_with("-L") {
println!("cargo:rustc-link-search=native={}", &arg[2..]);
}
}
for arg in llvm_config("--libs").split_whitespace() {
if arg.starts_with("-l") {
println!("cargo:rustc-link-lib={}", &arg[2..]);
}
}
for arg in llvm_config("--system-libs").split_whitespace() {
if arg.starts_with("-l") {
println!("cargo:rustc-link-lib=dylib={}", &arg[2..]);
}
}
if !cfg!(windows) {
let cxxflags = llvm_config("--cxxflags");
let libcpp = if cxxflags.contains("stdlib=libc++") { "c++" } else { "stdc++" };
println!("cargo:rustc-link-lib={}", libcpp);
}
std::env::set_var("CFLAGS", llvm_config("--cflags").trim());
gcc::compile_library("libtargetwrappers.a", &["wrappers/target.c"]);
}