-
Notifications
You must be signed in to change notification settings - Fork 15
/
build.rs
42 lines (34 loc) · 1.34 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
//! This build script will run cbindgen to generate libmicrovmi C header file
use std::env;
use std::fs;
use std::path::PathBuf;
use cbindgen::Config;
fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
// a little hack to write outside of OUT_DIR
// https://github.com/mmstick/cargo-deb/blob/e43018a46b8dc922cfdf6cdde12f7ed92fcc41aa/example/build.rs
// libmicrovmi issue: https://github.com/Wenzel/libmicrovmi/issues/177
let out_dir = env::var("OUT_DIR").unwrap();
let mut out_path = PathBuf::from(&out_dir)
.ancestors() // .../target/<debug|release>/build/example-<SHA>/out
.nth(3) // .../target/<debug|release>
.unwrap()
.to_owned();
out_path.push("capi");
// .../target/<debug|release>/capi/
if !out_path.exists() {
fs::create_dir(&out_path).expect("Could not create capi dir");
}
// .../target/<debug|release>/capi/libmicrovmi.h
out_path.push("libmicrovmi.h");
let config = Config::from_root_or_default(&crate_dir);
cbindgen::Builder::new()
.with_crate(crate_dir)
.with_config(config)
.generate()
.expect("Unable to generate bindings")
.write_to_file(&out_path);
println!("cargo:rerun-if-changed=src/capi.rs");
// if it has been removed
println!("cargo:rerun-if-changed={}", &out_path.display());
}