@ -2,20 +2,55 @@ use std::{
env ,
env ,
ffi ::OsString ,
ffi ::OsString ,
fmt ::Write as _ ,
fmt ::Write as _ ,
fs ::copy ,
fs ,
io ::BufReader ,
io ::BufReader ,
path ::PathBuf ,
path ::PathBuf ,
process ::{ Child , Command , Stdio } ,
process ::{ Child , Command , Stdio } ,
} ;
} ;
use cargo_metadata ::{ Artifact , CompilerMessage , Message , Target } ;
use cargo_metadata ::{
Artifact , CompilerMessage , Message , Metadata , MetadataCommand , Package , Target ,
} ;
fn main ( ) {
fn main ( ) {
const AYA_BUILD_INTEGRATION_BPF : & str = "AYA_BUILD_INTEGRATION_BPF" ;
println! ( "cargo:rerun-if-env-changed={}" , AYA_BUILD_INTEGRATION_BPF ) ;
let build_integration_bpf = match env ::var_os ( AYA_BUILD_INTEGRATION_BPF ) {
None = > false ,
Some ( s ) = > {
let s = s . to_str ( ) . unwrap ( ) ;
s . parse ::< bool > ( ) . unwrap ( )
}
} ;
let manifest_dir = env ::var_os ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) ;
let manifest_dir = env ::var_os ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) ;
let manifest_dir = PathBuf ::from ( manifest_dir ) ;
let manifest_dir = PathBuf ::from ( manifest_dir ) ;
let out_dir = env ::var_os ( "OUT_DIR" ) . unwrap ( ) ;
let out_dir = env ::var_os ( "OUT_DIR" ) . unwrap ( ) ;
let out_dir = PathBuf ::from ( out_dir ) ;
let out_dir = PathBuf ::from ( out_dir ) ;
let endian = env ::var_os ( "CARGO_CFG_TARGET_ENDIAN" ) . unwrap ( ) ;
let target = if endian = = "big" {
"bpfeb"
} else if endian = = "little" {
"bpfel"
} else {
panic! ( "unsupported endian={:?}" , endian )
} ;
const C_BPF_PROBES : & [ ( & str , & str ) ] = & [
( "ext.bpf.c" , "ext.bpf.o" ) ,
( "main.bpf.c" , "main.bpf.o" ) ,
( "multimap-btf.bpf.c" , "multimap-btf.bpf.o" ) ,
( "text_64_64_reloc.c" , "text_64_64_reloc.o" ) ,
] ;
let c_bpf_probes = C_BPF_PROBES
. iter ( )
. map ( | ( src , dst ) | ( src , out_dir . join ( dst ) ) ) ;
if build_integration_bpf {
let libbpf_dir = manifest_dir
let libbpf_dir = manifest_dir
. parent ( )
. parent ( )
. unwrap ( )
. unwrap ( )
@ -47,15 +82,6 @@ fn main() {
let bpf_dir = manifest_dir . join ( "bpf" ) ;
let bpf_dir = manifest_dir . join ( "bpf" ) ;
let endian = env ::var_os ( "CARGO_CFG_TARGET_ENDIAN" ) . unwrap ( ) ;
let target = if endian = = "big" {
"bpfeb"
} else if endian = = "little" {
"bpfel"
} else {
panic! ( "unsupported endian={:?}" , endian )
} ;
let mut target_arch = OsString ::new ( ) ;
let mut target_arch = OsString ::new ( ) ;
target_arch . push ( "-D__TARGET_ARCH_" ) ;
target_arch . push ( "-D__TARGET_ARCH_" ) ;
@ -68,14 +94,8 @@ fn main() {
target_arch . push ( arch ) ;
target_arch . push ( arch ) ;
} ;
} ;
for ( src , dst ) in [
for ( src , dst ) in c_bpf_probes {
( "ext.bpf.c" , "ext.bpf.o" ) ,
( "main.bpf.c" , "main.bpf.o" ) ,
( "multimap-btf.bpf.c" , "multimap-btf.bpf.o" ) ,
( "text_64_64_reloc.c" , "text_64_64_reloc.o" ) ,
] {
let src = bpf_dir . join ( src ) ;
let src = bpf_dir . join ( src ) ;
let out = out_dir . join ( dst ) ;
let mut cmd = Command ::new ( "clang" ) ;
let mut cmd = Command ::new ( "clang" ) ;
cmd . arg ( "-I" )
cmd . arg ( "-I" )
. arg ( & libbpf_headers_dir )
. arg ( & libbpf_headers_dir )
@ -83,7 +103,7 @@ fn main() {
. arg ( & target_arch )
. arg ( & target_arch )
. arg ( src )
. arg ( src )
. arg ( "-o" )
. arg ( "-o" )
. arg ( ou t) ;
. arg ( ds t) ;
let status = cmd
let status = cmd
. status ( )
. status ( )
. unwrap_or_else ( | err | panic! ( "failed to run {cmd:?}: {err}" ) ) ;
. unwrap_or_else ( | err | panic! ( "failed to run {cmd:?}: {err}" ) ) ;
@ -151,7 +171,26 @@ fn main() {
for ( name , binary ) in executables {
for ( name , binary ) in executables {
let dst = out_dir . join ( name ) ;
let dst = out_dir . join ( name ) ;
let _ : u64 = copy ( & binary , & dst )
let _ : u64 = fs ::copy ( & binary , & dst )
. unwrap_or_else ( | err | panic! ( "failed to copy {binary:?} to {dst:?}: {err}" ) ) ;
. unwrap_or_else ( | err | panic! ( "failed to copy {binary:?} to {dst:?}: {err}" ) ) ;
}
}
} else {
for ( _src , dst ) in c_bpf_probes {
fs ::write ( & dst , [ ] ) . unwrap_or_else ( | err | panic! ( "failed to create {dst:?}: {err}" ) ) ;
}
let Metadata { packages , .. } = MetadataCommand ::new ( ) . no_deps ( ) . exec ( ) . unwrap ( ) ;
for Package { name , targets , .. } in packages {
if name ! = "integration-ebpf" {
continue ;
}
for Target { name , kind , .. } in targets {
if kind ! = [ "bin" ] {
continue ;
}
let dst = out_dir . join ( name ) ;
fs ::write ( & dst , [ ] ) . unwrap_or_else ( | err | panic! ( "failed to create {dst:?}: {err}" ) ) ;
}
}
}
}
}