Compare commits

..

8 Commits

Author SHA1 Message Date
Tim W 17573e0e47 aya-build: plumb features of ebpf crates
This allows callers to select features of the ebpf crate.
1 day ago
Tamir Duberstein 948b8553ee
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.
7 days ago
Tamir Duberstein fe3f5c4e7d
aya-build: read AYA_BPF_TARGET_ARCH
This allows users to set `bpf_target_arch` from the environment without
touching RUSTFLAGS.
7 days ago
Tamir Duberstein 4b0ddfc2b0
aya-build: simplify
Cargo sets `CARGO_CFG_BPF_TARGET_ARCH` so we don't have to inspect
`CARGO_ENCODED_RUSTFLAGS`.
7 days ago
Tamir Duberstein 0c7c8097b2
aya-build: clarify naming 7 days ago
Tamir Duberstein e2c50ac221
aya-build: use OsString::into_string 7 days ago
Tamir Duberstein 4dc4a6ce08
.github: exercise targets for real 7 days ago
Alessandro Decina 3a3c451009 aya: restore must_exist argument to set_global
In
03e8487177
we deprecated set_global but accidentally broke its API by deleting the
must_exist argument.
7 days ago

@ -178,9 +178,8 @@ jobs:
for target in bpfeb-unknown-none bpfel-unknown-none; do for target in bpfeb-unknown-none bpfel-unknown-none; do
echo "::group::Build and test for $arch / $target" echo "::group::Build and test for $arch / $target"
if ! ( if ! (
export CARGO_CFG_BPF_TARGET_ARCH="$arch"
cargo +nightly hack build \ RUSTFLAGS="--cfg bpf_target_arch=\"$arch\"" cargo +nightly hack build \
--target "$target" \ --target "$target" \
-Z build-std=core \ -Z build-std=core \
--package aya-ebpf \ --package aya-ebpf \

@ -11,21 +11,19 @@ use std::{
use anyhow::{Context as _, Result, anyhow}; use anyhow::{Context as _, Result, anyhow};
use cargo_metadata::{Artifact, CompilerMessage, Message, Target}; use cargo_metadata::{Artifact, CompilerMessage, Message, Target};
#[derive(Default)]
pub struct Package<'a> { pub struct Package<'a> {
pub name: &'a str, pub name: &'a str,
pub root_dir: &'a str, pub root_dir: &'a str,
pub no_default_features: bool,
pub features: &'a [&'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_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") { if target_arch.starts_with("riscv64") {
"riscv64".into() "riscv64".into()
} else { } else {
target_arch.into() target_arch
} }
} }
@ -58,10 +56,22 @@ pub fn build_ebpf<'a>(
return Err(anyhow!("unsupported endian={endian:?}")); return Err(anyhow!("unsupported endian={endian:?}"));
}; };
let 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,
no_default_features,
features,
} in packages
{
// We have a build-dependency on `name`, so cargo will automatically rebuild us if `name`'s // We have a build-dependency on `name`, so cargo will automatically rebuild us if `name`'s
// *library* target or any of its dependencies change. Since we depend on `name`'s *binary* // *library* target or any of its dependencies change. Since we depend on `name`'s *binary*
// targets, that only gets us half of the way. This stanza ensures cargo will rebuild us on // targets, that only gets us half of the way. This stanza ensures cargo will rebuild us on
@ -84,6 +94,10 @@ pub fn build_ebpf<'a>(
"--target", "--target",
&target, &target,
]); ]);
if no_default_features {
cmd.arg("--no-default-features");
}
cmd.args(["--features", &features.join(",")]);
{ {
const SEPARATOR: &str = "\x1f"; const SEPARATOR: &str = "\x1f";
@ -92,7 +106,7 @@ pub fn build_ebpf<'a>(
for s in [ for s in [
"--cfg=bpf_target_arch=\"", "--cfg=bpf_target_arch=\"",
&arch, &bpf_target_arch,
"\"", "\"",
SEPARATOR, SEPARATOR,
"-Cdebuginfo=2", "-Cdebuginfo=2",
@ -202,16 +216,45 @@ 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() {
const RUSTFLAGS: &str = "CARGO_ENCODED_RUSTFLAGS"; // The presence of this environment variable indicates that `--cfg
// bpf_target_arch="..."` was passed to the compiler, so we don't need to
println!("cargo:rerun-if-env-changed={RUSTFLAGS}"); // emit it again. Note that we cannot *set* this environment variable - it
let rustc_cfgs = std::env::var_os(RUSTFLAGS).unwrap_or_else(|| panic!("{RUSTFLAGS} not set")); // is set by cargo.
let rustc_cfgs = rustc_cfgs const BPF_TARGET_ARCH: &str = "CARGO_CFG_BPF_TARGET_ARCH";
.to_str() println!("cargo:rerun-if-env-changed={BPF_TARGET_ARCH}");
.unwrap_or_else(|| panic!("{RUSTFLAGS}={rustc_cfgs:?} not unicode"));
if !rustc_cfgs.contains("bpf_target_arch") { // Users may directly set this environment variable in situations where
let arch = target_arch(); // using RUSTFLAGS to set `--cfg bpf_target_arch="..."` is not possible or
println!("cargo:rustc-cfg=bpf_target_arch=\"{arch}\""); // not ergonomic. In contrast to RUSTFLAGS this mechanism reuses the target
// cache for all values, producing many more invalidations.
const AYA_BPF_TARGET_ARCH: &str = "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() {
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) {
bpf_target_arch
.into_string()
.unwrap_or_else(|err| {
panic!("OsString::into_string({AYA_BPF_TARGET_ARCH}): {err:?}")
})
.into()
} else {
target_arch_fixup(
host.split_once('-')
.map_or(host, |(arch, _rest)| arch)
.into(),
)
};
println!("cargo:rustc-cfg=bpf_target_arch=\"{bpf_target_arch}\"");
} }
print!("cargo::rustc-check-cfg=cfg(bpf_target_arch, values("); print!("cargo::rustc-check-cfg=cfg(bpf_target_arch, values(");

@ -293,8 +293,13 @@ impl<'a> EbpfLoader<'a> {
/// Override the value of a global variable. /// Override the value of a global variable.
#[deprecated(since = "0.13.2", note = "please use `override_global` instead")] #[deprecated(since = "0.13.2", note = "please use `override_global` instead")]
pub fn set_global<T: Into<GlobalData<'a>>>(&mut self, name: &'a str, value: T) -> &mut Self { pub fn set_global<T: Into<GlobalData<'a>>>(
self.override_global(name, value, false) &mut self,
name: &'a str,
value: T,
must_exist: bool,
) -> &mut Self {
self.override_global(name, value, must_exist)
} }
/// Set the max_entries for specified map. /// Set the max_entries for specified map.

@ -219,6 +219,7 @@ fn main() -> Result<()> {
.parent() .parent()
.ok_or_else(|| anyhow!("no parent for {manifest_path}"))? .ok_or_else(|| anyhow!("no parent for {manifest_path}"))?
.as_str(), .as_str(),
..Default::default()
}; };
aya_build::build_ebpf([integration_ebpf_package], aya_build::Toolchain::default())?; aya_build::build_ebpf([integration_ebpf_package], aya_build::Toolchain::default())?;
} else { } else {

@ -27,8 +27,12 @@ pub fn aya_build::Toolchain<'a>::borrow_mut(&mut self) -> &mut T
impl<T> core::convert::From<T> for aya_build::Toolchain<'a> impl<T> core::convert::From<T> for aya_build::Toolchain<'a>
pub fn aya_build::Toolchain<'a>::from(t: T) -> T pub fn aya_build::Toolchain<'a>::from(t: T) -> T
pub struct aya_build::Package<'a> pub struct aya_build::Package<'a>
pub aya_build::Package::features: &'a [&'a str]
pub aya_build::Package::name: &'a str pub aya_build::Package::name: &'a str
pub aya_build::Package::no_default_features: bool
pub aya_build::Package::root_dir: &'a str pub aya_build::Package::root_dir: &'a str
impl<'a> core::default::Default for aya_build::Package<'a>
pub fn aya_build::Package<'a>::default() -> aya_build::Package<'a>
impl<'a> core::marker::Freeze for aya_build::Package<'a> impl<'a> core::marker::Freeze for aya_build::Package<'a>
impl<'a> core::marker::Send for aya_build::Package<'a> impl<'a> core::marker::Send for aya_build::Package<'a>
impl<'a> core::marker::Sync for aya_build::Package<'a> impl<'a> core::marker::Sync for aya_build::Package<'a>

@ -10750,7 +10750,7 @@ pub fn aya::EbpfLoader<'a>::map_max_entries(&mut self, name: &'a str, size: u32)
pub fn aya::EbpfLoader<'a>::map_pin_path<P: core::convert::Into<alloc::borrow::Cow<'a, std::path::Path>>>(&mut self, name: &'a str, path: P) -> &mut Self pub fn aya::EbpfLoader<'a>::map_pin_path<P: core::convert::Into<alloc::borrow::Cow<'a, std::path::Path>>>(&mut self, name: &'a str, path: P) -> &mut Self
pub fn aya::EbpfLoader<'a>::new() -> Self pub fn aya::EbpfLoader<'a>::new() -> Self
pub fn aya::EbpfLoader<'a>::override_global<T: core::convert::Into<aya::GlobalData<'a>>>(&mut self, name: &'a str, value: T, must_exist: bool) -> &mut Self pub fn aya::EbpfLoader<'a>::override_global<T: core::convert::Into<aya::GlobalData<'a>>>(&mut self, name: &'a str, value: T, must_exist: bool) -> &mut Self
pub fn aya::EbpfLoader<'a>::set_global<T: core::convert::Into<aya::GlobalData<'a>>>(&mut self, name: &'a str, value: T) -> &mut Self pub fn aya::EbpfLoader<'a>::set_global<T: core::convert::Into<aya::GlobalData<'a>>>(&mut self, name: &'a str, value: T, must_exist: bool) -> &mut Self
pub fn aya::EbpfLoader<'a>::set_max_entries(&mut self, name: &'a str, size: u32) -> &mut Self pub fn aya::EbpfLoader<'a>::set_max_entries(&mut self, name: &'a str, size: u32) -> &mut Self
pub fn aya::EbpfLoader<'a>::verifier_log_level(&mut self, level: aya::VerifierLogLevel) -> &mut Self pub fn aya::EbpfLoader<'a>::verifier_log_level(&mut self, level: aya::VerifierLogLevel) -> &mut Self
impl core::default::Default for aya::EbpfLoader<'_> impl core::default::Default for aya::EbpfLoader<'_>

Loading…
Cancel
Save