@ -1,5 +1,8 @@
use std ::{
use std ::{
env , fs ,
borrow ::Cow ,
env ,
ffi ::OsString ,
fs ,
io ::{ BufRead as _ , BufReader } ,
io ::{ BufRead as _ , BufReader } ,
path ::PathBuf ,
path ::PathBuf ,
process ::{ Child , Command , Stdio } ,
process ::{ Child , Command , Stdio } ,
@ -13,6 +16,19 @@ pub struct Package<'a> {
pub root_dir : & ' a str ,
pub root_dir : & ' a str ,
}
}
fn target_arch ( ) -> Cow < ' static , str > {
const TARGET_ARCH : & str = "CARGO_CFG_TARGET_ARCH" ;
let target_arch = env ::var_os ( TARGET_ARCH ) . unwrap_or_else ( | | panic! ( "{TARGET_ARCH} not set" ) ) ;
let target_arch = target_arch . into_encoded_bytes ( ) ;
let target_arch = String ::from_utf8 ( target_arch )
. unwrap_or_else ( | err | panic! ( "String::from_utf8({TARGET_ARCH}): {err:?}" ) ) ;
if target_arch . starts_with ( "riscv64" ) {
"riscv64" . into ( )
} else {
target_arch . into ( )
}
}
/// Build binary artifacts produced by `packages`.
/// Build binary artifacts produced by `packages`.
///
///
/// This would be better expressed as one or more [artifact-dependencies][bindeps] but issues such
/// This would be better expressed as one or more [artifact-dependencies][bindeps] but issues such
@ -42,9 +58,7 @@ pub fn build_ebpf<'a>(
return Err ( anyhow ! ( "unsupported endian={endian:?}" ) ) ;
return Err ( anyhow ! ( "unsupported endian={endian:?}" ) ) ;
} ;
} ;
let arch =
let arch = target_arch ( ) ;
env ::var_os ( "CARGO_CFG_TARGET_ARCH" ) . ok_or ( anyhow ! ( "CARGO_CFG_TARGET_ARCH not set" ) ) ? ;
let target = format! ( "{target}-unknown-none" ) ;
let target = format! ( "{target}-unknown-none" ) ;
for Package { name , root_dir } in packages {
for Package { name , root_dir } in packages {
@ -71,20 +85,25 @@ pub fn build_ebpf<'a>(
& target ,
& target ,
] ) ;
] ) ;
cmd . env ( "CARGO_CFG_BPF_TARGET_ARCH" , & arch ) ;
{
cmd . env (
const SEPARATOR : & str = "\x1f" ;
"CARGO_ENCODED_RUSTFLAGS" ,
[ "debuginfo=2" , "link-arg=--btf" ]
let mut rustflags = OsString ::new ( ) ;
. into_iter ( )
. flat_map ( | flag | [ "-C" , flag ] )
for s in [
. fold ( String ::new ( ) , | mut acc , flag | {
"--cfg=bpf_target_arch=\"" ,
if ! acc . is_empty ( ) {
& arch ,
acc . push ( '\x1f' ) ;
"\"" ,
}
SEPARATOR ,
acc . push_str ( flag ) ;
"-Cdebuginfo=2" ,
acc
SEPARATOR ,
} ) ,
"-Clink-arg=--btf" ,
) ;
] {
rustflags . push ( s ) ;
}
cmd . env ( "CARGO_ENCODED_RUSTFLAGS" , rustflags ) ;
}
// Workaround to make sure that the correct toolchain is used.
// Workaround to make sure that the correct toolchain is used.
for key in [ "RUSTC" , "RUSTC_WORKSPACE_WRAPPER" ] {
for key in [ "RUSTC" , "RUSTC_WORKSPACE_WRAPPER" ] {
@ -183,20 +202,17 @@ impl<'a> Toolchain<'a> {
/// Emit cfg flags that describe the desired BPF target architecture.
/// Emit cfg flags that describe the desired BPF target architecture.
pub fn emit_bpf_target_arch_cfg ( ) {
pub fn emit_bpf_target_arch_cfg ( ) {
println! ( "cargo:rerun-if-env-changed=CARGO_CFG_BPF_TARGET_ARCH" ) ;
const RUSTFLAGS : & str = "CARGO_ENCODED_RUSTFLAGS" ;
let bpf_target_arch = env ::var_os ( "CARGO_CFG_BPF_TARGET_ARCH" ) ;
let target_arch = env ::var_os ( "CARGO_CFG_TARGET_ARCH" ) ;
println! ( "cargo:rerun-if-env-changed={RUSTFLAGS}" ) ;
let arch = if let Some ( bpf_target_arch ) = bpf_target_arch . as_ref ( ) {
let rustc_cfgs = std ::env ::var_os ( RUSTFLAGS ) . unwrap_or_else ( | | panic! ( "{RUSTFLAGS} not set" ) ) ;
bpf_target_arch . to_str ( ) . unwrap ( )
let rustc_cfgs = rustc_cfgs
} else {
. to_str ( )
let target_arch = target_arch . as_ref ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
. unwrap_or_else ( | | panic! ( "{RUSTFLAGS}={rustc_cfgs:?} not unicode" ) ) ;
if target_arch . starts_with ( "riscv64" ) {
if ! rustc_cfgs . contains ( "bpf_target_arch" ) {
"riscv64"
let arch = target_arch ( ) ;
} else {
println! ( "cargo:rustc-cfg=bpf_target_arch=\"{arch}\"" ) ;
target_arch
}
}
} ;
println! ( "cargo:rustc-cfg=bpf_target_arch=\"{arch}\"" ) ;
print! ( "cargo::rustc-check-cfg=cfg(bpf_target_arch, values(" ) ;
print! ( "cargo::rustc-check-cfg=cfg(bpf_target_arch, values(" ) ;
for value in [
for value in [