aya-build: guess `bpf_target_arch` from `HOST`

Remove the use of `CARGO_CFG_TARGET_ARCH` in ebpf crate build scripts,
moving it back only to `aya_build::build_ebpf` where it refers to the
userspace crate's target. In the ebpf crates restore the use of `HOST`
as the default compilation target when neither `--cfg bpf_target_arch`
nor `AYA_BPF_TARGET_ARCH` are provided.
reviewable/pr1380/r12
Tamir Duberstein 7 days ago
parent fe3f5c4e7d
commit 948b8553ee
No known key found for this signature in database

@ -16,16 +16,11 @@ pub struct Package<'a> {
pub root_dir: &'a str, pub root_dir: &'a str,
} }
fn target_arch() -> Cow<'static, str> { fn target_arch_fixup(target_arch: Cow<'_, str>) -> Cow<'_, 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_string()
.unwrap_or_else(|err| panic!("OsString::into_string({TARGET_ARCH}): {err:?}"));
if target_arch.starts_with("riscv64") { if target_arch.starts_with("riscv64") {
"riscv64".into() "riscv64".into()
} else { } else {
target_arch.into() target_arch
} }
} }
@ -58,7 +53,13 @@ pub fn build_ebpf<'a>(
return Err(anyhow!("unsupported endian={endian:?}")); return Err(anyhow!("unsupported endian={endian:?}"));
}; };
let bpf_target_arch = target_arch(); const TARGET_ARCH: &str = "CARGO_CFG_TARGET_ARCH";
let bpf_target_arch =
env::var_os(TARGET_ARCH).unwrap_or_else(|| panic!("{TARGET_ARCH} not set"));
let bpf_target_arch = bpf_target_arch
.into_string()
.unwrap_or_else(|err| panic!("OsString::into_string({TARGET_ARCH}): {err:?}"));
let bpf_target_arch = target_arch_fixup(bpf_target_arch.into());
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 {
@ -216,7 +217,16 @@ pub fn emit_bpf_target_arch_cfg() {
const AYA_BPF_TARGET_ARCH: &str = "AYA_BPF_TARGET_ARCH"; const AYA_BPF_TARGET_ARCH: &str = "AYA_BPF_TARGET_ARCH";
println!("cargo:rerun-if-env-changed={AYA_BPF_TARGET_ARCH}"); println!("cargo:rerun-if-env-changed={AYA_BPF_TARGET_ARCH}");
const HOST: &str = "HOST";
println!("cargo:rerun-if-env-changed={HOST}");
if std::env::var_os(BPF_TARGET_ARCH).is_none() { if std::env::var_os(BPF_TARGET_ARCH).is_none() {
let host = std::env::var_os(HOST).unwrap_or_else(|| panic!("{HOST} not set"));
let host = host
.into_string()
.unwrap_or_else(|err| panic!("OsString::into_string({HOST}): {err:?}"));
let host = host.as_str();
let bpf_target_arch = if let Some(bpf_target_arch) = std::env::var_os(AYA_BPF_TARGET_ARCH) { let bpf_target_arch = if let Some(bpf_target_arch) = std::env::var_os(AYA_BPF_TARGET_ARCH) {
bpf_target_arch bpf_target_arch
.into_string() .into_string()
@ -225,7 +235,11 @@ pub fn emit_bpf_target_arch_cfg() {
}) })
.into() .into()
} else { } else {
target_arch() target_arch_fixup(
host.split_once('-')
.map_or(host, |(arch, _rest)| arch)
.into(),
)
}; };
println!("cargo:rustc-cfg=bpf_target_arch=\"{bpf_target_arch}\""); println!("cargo:rustc-cfg=bpf_target_arch=\"{bpf_target_arch}\"");
} }

Loading…
Cancel
Save