-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild.rs
More file actions
101 lines (88 loc) · 2.71 KB
/
Copy pathbuild.rs
File metadata and controls
101 lines (88 loc) · 2.71 KB
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
100
101
extern crate bindgen;
use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;
fn main() {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
println!("cargo:rustc-link-lib=static=pg_query");
let system_path = env::var("LIBPG_QUERY_PATH").map(PathBuf::from);
let header_path = if let Ok(system_path) = system_path {
build_from_system(&system_path);
system_path.join("include/pg_query.h").display().to_string()
} else {
build_from_source(&out_path);
out_path
.join("libpg_query/pg_query.h")
.display()
.to_string()
};
let bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header(header_path)
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
fn build_from_system(system_path: &Path) {
println!(
"cargo:rustc-link-search=native={}",
system_path.join("lib").display()
);
}
fn build_from_source(out_path: &Path) {
run_command(
"cp",
&[
"-r",
"./c_libs/libpg_query",
&out_path.display().to_string(),
],
None,
);
let make_dir = format!("{}/libpg_query", out_path.display());
run_command("make", &[], Some(make_dir));
println!(
"cargo:rustc-link-search=native={}",
out_path.join("libpg_query").display()
);
}
fn run_command(exe: &str, args: &[&str], dir: Option<String>) {
let mut c = Command::new(exe);
c.args(args);
if let Some(dir) = dir {
c.current_dir(dir);
}
let output = c.output().expect(&format!(
"failed to run command: {}",
command_str(exe, args),
));
let code = output.status.code().unwrap_or(-1);
if code != 0 {
let mut msg = format!(
"Failed to run {} with exit code of {}",
command_str(exe, args),
code
);
if !output.stdout.is_empty() {
if let Ok(out) = String::from_utf8(output.stdout) {
msg.push('\n');
msg.push_str(&format!("stdout =\n{out}"));
}
}
if !output.stderr.is_empty() {
if let Ok(out) = String::from_utf8(output.stderr) {
msg.push('\n');
msg.push_str(&format!("stderr =\n{out}"));
}
}
panic!("{}", msg);
}
}
fn command_str(exe: &str, args: &[&str]) -> String {
format!("{exe} {}", args.join(" "))
}