-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder.rs
91 lines (80 loc) · 3.13 KB
/
builder.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
use std::{
env,
error::Error,
fmt::{Display, Formatter},
path::{Path, PathBuf},
process::{Command, ExitStatus}
};
/// Custom error for compilation of the C library
#[derive(Debug)]
struct CompileError<'a> {
command: &'a str,
exit_code: Option<i32>
}
impl<'a> Display for CompileError<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let end_text = if let Some(code) = self.exit_code {
format!("with exit code {}", code)
} else {
"without exit code".to_string()
};
let text = format!("Command with name `{}` failed {}", self.command, end_text);
write!(f, "{}", text)
}
}
impl<'a> Error for CompileError<'a> {}
/// Handles the exit statuses of the executed bash commands
///
/// # Arguments
/// * `name` - Name of the executed bash command
/// * `exit_states` - The exit status of the executed bash command
///
/// # Returns
///
/// Returns () if the exit status was success
///
/// # Errors
///
/// Returns a CompilationError if the command failed
fn exit_status_to_result(name: &str, exit_status: ExitStatus) -> Result<(), CompileError> {
match exit_status.success() {
true => Ok(()),
false => Err(CompileError { command: name, exit_code: exit_status.code() })
}
}
fn main() -> Result<(), Box<dyn Error>> {
// remove the old libsais folder
Command::new("rm").args(["-rf", "libsais"]).status().unwrap_or_default(); // if removing fails, it is since the folder did not exist, we just can ignore it
// clone the c library
Command::new("git")
.args(["clone", "https://github.com/IlyaGrebnov/libsais.git", "--depth=1"])
.status()
.expect("Failed to clone the libsais repository");
// compile the c library
Command::new("rm").args(["libsais/CMakeCache.txt"]).status().unwrap_or_default(); // if removing fails, it is since the cmake cache did not exist, we just can ignore it
exit_status_to_result(
"cmake",
Command::new("cmake").args(["-DCMAKE_BUILD_TYPE=\"Release\"", "libsais", "-Blibsais"]).status()?
)?;
exit_status_to_result("make", Command::new("make").args(["-C", "libsais"]).status()?)?;
// link the c libsais library to rust
let dir = env::var("CARGO_MANIFEST_DIR").unwrap();
println!("cargo:rustc-link-search=native={}", Path::new(&dir).join("libsais").display());
println!("cargo:rustc-link-lib=static=libsais");
// 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()
// The input header we would like to generate
// bindings for.
.header("libsais-wrapper.h")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
// Finish the builder and generate the bindings.
.generate()?;
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR")?);
bindings.write_to_file(out_path.join("bindings.rs"))?;
Ok(())
}