-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.rs
98 lines (90 loc) · 3.43 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
extern crate bindgen;
use std::env;
use std::path::PathBuf;
use std::process::Command;
use which::which;
fn get_path_from_llvm(p: &str, s: &[&str]) -> PathBuf {
PathBuf::from(
String::from_utf8(
Command::new(p)
.args(s)
.output()
.expect("llvm-config --libdir failed")
.stdout,
)
.expect("response from llvm-config is not valid utf8")
.trim(),
)
}
fn main() {
// Tell cargo to tell rustc to link the system flux-core
// shared library.
println!("cargo:rustc-link-lib=flux-core");
if let Ok(libclang_path) = env::var("LIBCLANG_PATH") {
println!(
"LIBCLANG_PATH already set to {}, not overriding",
libclang_path
);
} else if let Ok(llvm_config_path) = which("llvm-config") {
let lc_str = llvm_config_path
.to_str()
.expect("llvm config path is not valid utf8");
let libdir = get_path_from_llvm(lc_str, &["--libdir"]);
let mut clang_path = get_path_from_llvm(lc_str, &["--bindir"]);
clang_path.push("clang");
println!("LIBCLANG_PATH={:?}", libdir);
env::set_var("LIBCLANG_PATH", libdir);
println!("CLANG_PATH={:?}", clang_path);
env::set_var("CLANG_PATH", clang_path);
} else if let Ok(mut clang_path) = which("clang") {
println!("{:?}", clang_path);
if let Err(_) = env::var("CLANG_PATH") {
env::set_var("CLANG_PATH", &clang_path);
}
if let Err(_) = env::var("LIBCLANG_PATH") {
// TODO: deal with 32 bit?
clang_path.pop(); // remove clang
clang_path.pop(); // remove bin
clang_path.push("lib64");
env::set_var("LIBCLANG_PATH", clang_path);
}
}
let flux_path = match env::var("FLUX_PATH") {
Ok(p) => p,
Err(_) => "/usr/local".to_string(),
};
let include_arg = "-I".to_string() + &flux_path + "/include";
println!("cargo:rustc-link-search=native={}/lib", flux_path);
// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
let bindings = bindgen::Builder::default()
.clang_arg(include_arg)
// The input header we would like to generate
// bindings for.
.header("wrapper.h")
// Only take flux functions, etc.
.allowlist_function("flux_.*")
.allowlist_var("FLUX_.*")
.allowlist_var("flux_.*")
.allowlist_type("flux_.*")
.allowlist_type("kvs_.*")
.constified_enum_module("flux_flag")
.constified_enum_module("kvs_op")
// .bitfield_enum("FLUX_.*")
// .rustified_enum("FLUX_.*") //can't, enums need names and functions need to take the enum
// type...
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect(
"Unable to generate bindings. If this is an include directory or clang argument
issue, use BINDGEN_EXTRA_CLANG_ARGS='args' to pass in necessary include paths and FLUX_PATH
to set the base path of the flux install",
);
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}