-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
124 lines (104 loc) · 3.31 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
extern crate bindgen;
use std::process;
fn main() {
build_perf_event_bindings();
build_bpf_bindings();
}
const PERF_WHITELIST_TYPES: &'static [&'static str] =
&["perf_type_id", "perf_event_.*", "perf_sw_ids"];
const PERF_WHITELIST_VARS: &'static [&'static str] = &["PERF_FLAG_FD_CLOEXEC"];
const BPF_WHITELIST_TYPES: &'static [&'static str] = &["bpf_.*"];
const BPF_WHITELIST_VARS: &'static [&'static str] = &[
"LOG_BUF_SIZE",
"BPF_.*",
"MAX_BPF_REG",
"MAX_BPF_ATTACH_TYPE",
"__BPF_FUNC_MAPPER",
"__BPF_ENUM_FN",
];
fn build_perf_event_bindings() {
let mut bindings = bindgen::Builder::default().header("/usr/include/linux/perf_event.h");
for ty in PERF_WHITELIST_TYPES {
bindings = bindings.whitelist_type(ty);
}
for var in PERF_WHITELIST_VARS {
bindings = bindings.whitelist_var(var);
}
bindings = bindings
.derive_debug(true)
.impl_debug(true)
.derive_default(true)
.derive_partialeq(true)
.impl_partialeq(true)
.derive_eq(true)
.derive_partialord(true)
.derive_ord(true)
.derive_hash(true)
.rustfmt_bindings(true);
let builder = bindings
.generate()
.expect("Unable to generate perf_event.h bindings");
builder
.write_to_file("./src/perf_event_bindings.rs")
.expect("Couldn't write perf_event bindings!");
let have_working_rustfmt = process::Command::new("rustup")
.args(&["run", "nightly", "rustfmt", "--version"])
.stdout(process::Stdio::null())
.stderr(process::Stdio::null())
.status()
.ok()
.map_or(false, |status| status.success());
if !have_working_rustfmt {
println!(
"
The latest `rustfmt` is required to format the generated bindings. Install
`rustfmt` with:
$ rustup component add rustfmt-preview
$ rustup update
"
);
}
}
fn build_bpf_bindings() {
let mut bindings = bindgen::Builder::default().header("src/bpf_elf/bpf.h");
for ty in BPF_WHITELIST_TYPES {
bindings = bindings.whitelist_type(ty);
}
for var in BPF_WHITELIST_VARS {
bindings = bindings.whitelist_var(var);
}
bindings = bindings
.derive_debug(true)
.impl_debug(true)
.derive_default(true)
.derive_partialeq(true)
.impl_partialeq(true)
.derive_eq(true)
.derive_partialord(true)
.derive_ord(true)
.derive_hash(true)
.rustfmt_bindings(true);
let builder = bindings
.generate()
.expect("Unable to generate perf_event.h bindings");
builder
.write_to_file("./src/bpf_elf/bpf_bindings.rs")
.expect("Couldn't write bpf bindings!");
let have_working_rustfmt = process::Command::new("rustup")
.args(&["run", "nightly", "rustfmt", "--version"])
.stdout(process::Stdio::null())
.stderr(process::Stdio::null())
.status()
.ok()
.map_or(false, |status| status.success());
if !have_working_rustfmt {
println!(
"
The latest `rustfmt` is required to format the generated bindings. Install
`rustfmt` with:
$ rustup component add rustfmt-preview
$ rustup update
"
);
}
}