-
Notifications
You must be signed in to change notification settings - Fork 26
/
build.rs
83 lines (70 loc) · 2.38 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
extern crate bindgen;
extern crate git2;
extern crate regex;
use git2::Repository;
use regex::Regex;
use std::env;
use std::fs::remove_dir_all;
use std::path::{Path, PathBuf};
use std::process::Command;
const LIBINJECTION_URL: &'static str = "https://github.com/client9/libinjection";
const BUILD_DIR_NAME: &'static str = "libinjection";
fn clone_libinjection(build_dir: &Path, version: &str) -> Option<()> {
let repo = Repository::clone(LIBINJECTION_URL, build_dir).ok()?;
let rev = repo.revparse_single(version).ok()?;
repo.set_head_detached(rev.id()).ok()
}
fn run_make(rule: &str, cwd: &Path) -> bool {
let output = Command::new("make")
.arg(rule)
.env("OUT_DIR", env::var("OUT_DIR").unwrap())
.current_dir(cwd)
.output()
.unwrap();
if output.status.success() {
true
} else {
panic!("make error: {}", String::from_utf8_lossy(&output.stderr));
}
}
fn fix_python_version() -> Option<()> {
if if let Ok(output) = Command::new("python").arg("-V").output() {
let python_version = String::from_utf8_lossy(&output.stdout).to_string();
!Regex::new("Python 2.*")
.ok()?
.is_match(python_version.as_str())
} else {
true
} {
let cwd = env::current_dir().ok()?;
if !run_make("fix-python", cwd.as_path()) {
return None;
}
}
Some(())
}
fn main() {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let mut build_parent_dir = out_path.join(BUILD_DIR_NAME);
let _ = remove_dir_all(build_parent_dir.as_path());
if clone_libinjection(build_parent_dir.as_path(), "v3.10.0").is_none() {
panic!("unable to clone libinjection");
}
if fix_python_version().is_none() {
panic!("unable to fix python version");
}
build_parent_dir.push("src");
if !run_make("all", build_parent_dir.as_path()) {
panic!("unable to make libinjection");
}
println!("cargo:rustc-link-lib=static=injection");
println!("cargo:rustc-link-search={}", build_parent_dir.display());
let h_path = build_parent_dir.join("libinjection.h");
let bindings = bindgen::Builder::default()
.header(h_path.to_str().unwrap())
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("unable to write bindings");
}