-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
build.rs
163 lines (135 loc) · 4.14 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::env;
use std::path::PathBuf;
const SRC: &str = "src/bpf/einat.bpf.c";
const SRC_DIR: &str = "src/bpf/";
fn out_path(file: &str) -> PathBuf {
let mut out =
PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script"));
out.push(file);
out
}
fn c_args() -> Vec<String> {
let mut c_args: Vec<String> = [
"-Wall",
"-mcpu=v3",
"-fno-stack-protector",
// error out if loop is not unrolled
"-Werror=pass-failed",
// "-Werror"
]
.iter()
.map(|s| s.to_string())
.collect();
if cfg!(feature = "ipv6") {
c_args.push("-DFEAT_IPV6".to_string());
}
c_args
}
#[cfg(any(feature = "aya", feature = "libbpf"))]
fn einat_obj_build() {
use std::ffi::OsStr;
use std::process::Command;
let bpf_obj_tmp = &out_path("einat.bpf.tmp.o");
let bpf_obj = &out_path("einat.bpf.o");
// compile BPF C code
let mut cmd = Command::new("clang");
cmd.args(c_args());
if let Some(cflags) = option_env!("EINAT_BPF_CFLAGS") {
cmd.args(cflags.split_ascii_whitespace());
}
// Specify environment variable LIBBPF_NO_PKG_CONFIG=1 to disable pkg-config lookup.
// Or just disable the "pkg-config" feature.
#[cfg(feature = "pkg-config")]
match pkg_config::Config::new()
.cargo_metadata(false)
.probe("libbpf")
{
Ok(libbpf) => {
let includes = libbpf
.include_paths
.into_iter()
.map(|i| format!("-I{}", i.to_string_lossy()));
cmd.args(includes);
}
Err(e) => {
eprintln!("Can not locate libbpf with pkg-config: {}", e)
}
}
let target = if env::var("CARGO_CFG_TARGET_ENDIAN").unwrap() == "little" {
"bpfel"
} else {
"bpfeb"
};
let res = cmd
.arg("-target")
.arg(target)
.arg("-g")
.arg("-O2")
.arg("-c")
.arg(SRC)
.arg("-o")
.arg(bpf_obj_tmp)
.status()
.expect("compile BPF object failed");
if !res.success() {
panic!("{}", res);
}
fn strip_obj<S: AsRef<OsStr>>(strip_cmd: &str, target: S, source: S) -> Result<(), String> {
let mut args = strip_cmd.split_ascii_whitespace();
let cmd = args.next().unwrap();
let res = Command::new(cmd)
.args(args)
.arg(target)
.arg(source)
.status();
match res {
Ok(res) => {
if res.success() {
return Ok(());
}
Err(format!("{}: {}", strip_cmd, res))
}
Err(err) => Err(format!("{}: {}", strip_cmd, err)),
}
}
// strip the DWARF debug information
let strip_bpf_obj = || -> Result<(), String> {
if let Some(strip_cmd) = option_env!("EINAT_BPF_STRIP_CMD") {
return strip_obj(strip_cmd, bpf_obj, bpf_obj_tmp);
}
let res = strip_obj("bpftool gen object", bpf_obj, bpf_obj_tmp);
if res.is_ok() {
return res;
}
eprintln!("strip with bpftool failed, fallback to llvm-strip");
let res = strip_obj("llvm-strip -g -o", bpf_obj, bpf_obj_tmp);
if res.is_ok() {
return res;
}
eprintln!("strip with llvm-strip failed, skip stripping");
std::fs::rename(bpf_obj_tmp, bpf_obj).unwrap();
Ok(())
};
strip_bpf_obj().expect("strip BPF object file failed");
println!("cargo:rerun-if-env-changed=EINAT_BPF_CFLAGS");
println!("cargo:rerun-if-env-changed=EINAT_BPF_STRIP_CMD");
}
#[cfg(feature = "libbpf-skel")]
fn libbpf_skel_build() {
use libbpf_cargo::SkeletonBuilder;
let out = out_path("einat.skel.rs");
SkeletonBuilder::new()
.source(SRC)
.clang_args(c_args())
.debug(true)
.build_and_generate(&out)
.unwrap();
}
fn main() {
#[cfg(any(feature = "aya", feature = "libbpf"))]
einat_obj_build();
#[cfg(feature = "libbpf-skel")]
libbpf_skel_build();
println!("cargo:rerun-if-changed={}", SRC_DIR);
println!("cargo:rerun-if-changed=build.rs");
}