-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
71 lines (60 loc) · 1.93 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
use std::env;
use std::io;
use std::path::PathBuf;
use lazy_static::lazy_static;
use tonic_build;
#[cfg(any(feature = "seccomp", feature = "cap-ng"))]
use bindgen;
lazy_static! {
static ref OUT_DIR: PathBuf = {
let out_dir = env::var("OUT_DIR").unwrap();
PathBuf::from(out_dir)
};
static ref ROOT_DIR: PathBuf = {
let root_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
PathBuf::from(root_dir)
};
}
fn main() -> io::Result<()> {
println!("cargo:rerun-if-changed=Cargo.toml");
println!("cargo:rerun-if-changed=build.rs",);
build_proto()?;
#[cfg(feature = "seccomp")]
generate_libseccomp_binding()?;
#[cfg(feature = "cap-ng")]
generate_libcap_ng_binding()?;
Ok(())
}
fn build_proto() -> io::Result<()> {
const RPC_FILE: &str = "./rpc.proto";
println!("cargo:rerun-if-changed={}", RPC_FILE);
tonic_build::configure()
.build_client(false)
.build_server(true)
.compile(&[RPC_FILE], &["."])?;
Ok(())
}
#[cfg(feature = "seccomp")]
fn generate_libseccomp_binding() -> io::Result<()> {
const SECCOMP_HEADER: &str = "/usr/include/seccomp.h";
println!("cargo:rerun-if-changed={}", SECCOMP_HEADER);
println!("cargo:rustc-link-lib=dylib=seccomp");
bindgen::builder()
.header_contents("seccomp.h", "#include<seccomp.h>")
.generate()
.expect("Failed to generate libseccomp bindings")
.write_to_file(OUT_DIR.join("libseccomp.rs"))?;
Ok(())
}
#[cfg(feature = "cap-ng")]
fn generate_libcap_ng_binding() -> io::Result<()> {
const CAPNG_HEADER: &str = "/usr/include/cap-ng.h";
println!("cargo:rerun-if-changed={}", CAPNG_HEADER);
println!("cargo:rustc-link-lib=dylib=cap-ng");
bindgen::builder()
.header_contents("cap-ng.h", "#include<cap-ng.h>")
.generate()
.expect("Failed to generate libcap-ng bindings")
.write_to_file(OUT_DIR.join("libcapng.rs"))?;
Ok(())
}