-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
99 lines (90 loc) · 2.63 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#![allow(dead_code)]
extern crate bindgen;
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
/////
///// MLIR backend
/////
fn llvmconfigshim(arg: &str) -> String {
let call = format!("llvm-config {}", arg);
let tg = if cfg!(target_os = "windows") {
Command::new("cmd")
.args(["/C", &call[..]])
.output()
.expect("failed to execute process")
} else {
Command::new("sh")
.arg("-c")
.arg(&call[..])
.output()
.expect("failed to execute process")
}
.stdout;
let mut s = String::from_utf8_lossy(&tg);
s.to_mut().pop();
s.to_string()
}
fn get_system_libcpp() -> Option<&'static str> {
if cfg!(target_env = "msvc") {
None
} else if cfg!(target_os = "macos") || cfg!(target_os = "freebsd") {
Some("c++")
} else {
Some("stdc++")
}
}
fn llvm_libs() -> Vec<String> {
let libdir = llvmconfigshim("--libdir");
let paths = fs::read_dir(libdir).unwrap();
paths
.filter_map(|entry| {
entry.ok().and_then(|e| {
e.path()
.file_name()
.and_then(|n| n.to_str().map(String::from))
})
})
.collect::<Vec<String>>()
}
fn mlir() {
// Build bindings to MLIR C API.
println!("cargo:rerun-if-changed=wrapper.h");
println!("cargo:rerun-if-changed=build.rs");
let includedir = llvmconfigshim("--includedir");
let libdir = llvmconfigshim("--libdir");
println!("cargo:libdir={}", libdir);
println!("cargo:rustc-link-search=all={}", libdir);
println!(
"cargo:rustc-link-lib=dylib={}",
get_system_libcpp().unwrap()
);
for l in llvm_libs().iter() {
if l.contains("libMLIR") && l.contains(".a") {
let len = l.len();
println!("cargo:rustc-link-lib={}", l[3..(len - 2)].to_string());
}
}
println!("cargo:rustc-link-lib=LLVM");
println!("cargo:rustc-link-lib=MLIR");
println!("cargo:rustc-link-lib=LLVMExecutionEngine");
println!("cargo:rustc-link-lib=MLIRExecutionEngine");
let bindings = bindgen::builder()
.header("wrapper.h")
.clang_arg(format!("-I/{}", includedir))
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Unable to generate bindings.");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
/////
///// Feature selection.
/////
fn main() {
#[cfg(feature = "mlir")]
mlir();
}