diff --git a/aya-build/src/lib.rs b/aya-build/src/lib.rs index 89ba9d74..5ae01033 100644 --- a/aya-build/src/lib.rs +++ b/aya-build/src/lib.rs @@ -6,9 +6,12 @@ use std::{ }; use anyhow::{Context as _, Result, anyhow}; -// Re-export `cargo_metadata` to having to encode the version downstream and risk mismatches. -pub use cargo_metadata; -use cargo_metadata::{Artifact, CompilerMessage, Message, Package, Target}; +use cargo_metadata::{Artifact, CompilerMessage, Message, Target}; + +pub struct Package<'a> { + pub name: &'a str, + pub root_dir: &'a str, +} /// Build binary artifacts produced by `packages`. /// @@ -22,9 +25,9 @@ use cargo_metadata::{Artifact, CompilerMessage, Message, Package, Target}; /// prevent their use for the time being. /// /// [bindeps]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html?highlight=feature#artifact-dependencies -pub fn build_ebpf( - packages: impl IntoIterator, - toolchain: Toolchain<'_>, +pub fn build_ebpf<'a>( + packages: impl IntoIterator>, + toolchain: Toolchain<'a>, ) -> Result<()> { let out_dir = env::var_os("OUT_DIR").ok_or(anyhow!("OUT_DIR not set"))?; let out_dir = PathBuf::from(out_dir); @@ -44,21 +47,12 @@ pub fn build_ebpf( let target = format!("{target}-unknown-none"); - for Package { - name, - manifest_path, - .. - } in packages - { - let dir = manifest_path - .parent() - .ok_or(anyhow!("no parent for {manifest_path}"))?; - + for Package { name, root_dir } in packages { // 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* // targets, that only gets us half of the way. This stanza ensures cargo will rebuild us on // changes to the binaries too, which gets us the rest of the way. - println!("cargo:rerun-if-changed={dir}"); + println!("cargo:rerun-if-changed={root_dir}"); let mut cmd = Command::new("rustup"); cmd.args([ @@ -67,7 +61,7 @@ pub fn build_ebpf( "cargo", "build", "--package", - &name, + name, "-Z", "build-std=core", "--bins", @@ -98,7 +92,7 @@ pub fn build_ebpf( } // Workaround for https://github.com/rust-lang/cargo/issues/6412 where cargo flocks itself. - let target_dir = out_dir.join(name.as_str()); + let target_dir = out_dir.join(name); cmd.arg("--target-dir").arg(&target_dir); let mut child = cmd diff --git a/test/integration-test/Cargo.toml b/test/integration-test/Cargo.toml index d18e6fe9..e443af2f 100644 --- a/test/integration-test/Cargo.toml +++ b/test/integration-test/Cargo.toml @@ -45,6 +45,7 @@ xdpilone = { workspace = true } [build-dependencies] anyhow = { workspace = true } aya-build = { path = "../../aya-build" } +cargo_metadata = { workspace = true } # TODO(https://github.com/rust-lang/cargo/issues/12375): this should be an artifact dependency, but # it's not possible to tell cargo to use `-Z build-std` to build it. We cargo-in-cargo in the build # script to build this, but we want to teach cargo about the dependecy so that cache invalidation diff --git a/test/integration-test/build.rs b/test/integration-test/build.rs index 47c3b9da..44a4783a 100644 --- a/test/integration-test/build.rs +++ b/test/integration-test/build.rs @@ -12,7 +12,7 @@ use std::{ }; use anyhow::{Context as _, Ok, Result, anyhow}; -use aya_build::cargo_metadata::{Metadata, MetadataCommand, Package, Target, TargetKind}; +use cargo_metadata::{Metadata, MetadataCommand, Package, Target, TargetKind}; use xtask::{AYA_BUILD_INTEGRATION_BPF, LIBBPF_DIR, exec, install_libbpf_headers_cmd}; /// This file, along with the xtask crate, allows analysis tools such as `cargo check`, `cargo @@ -208,6 +208,18 @@ fn main() -> Result<()> { } } + let Package { + name, + manifest_path, + .. + } = integration_ebpf_package; + let integration_ebpf_package = aya_build::Package { + name: name.as_str(), + root_dir: manifest_path + .parent() + .ok_or_else(|| anyhow!("no parent for {manifest_path}"))? + .as_str(), + }; aya_build::build_ebpf([integration_ebpf_package], aya_build::Toolchain::default())?; } else { for (src, build_btf) in C_BPF { diff --git a/xtask/public-api/aya-build.txt b/xtask/public-api/aya-build.txt index bf7a479b..a9fe663c 100644 --- a/xtask/public-api/aya-build.txt +++ b/xtask/public-api/aya-build.txt @@ -1,5 +1,4 @@ pub mod aya_build -pub use aya_build::cargo_metadata pub enum aya_build::Toolchain<'a> pub aya_build::Toolchain::Custom(&'a str) pub aya_build::Toolchain::Nightly @@ -27,5 +26,30 @@ impl core::borrow::BorrowMut for aya_build::Toolchain<'a> where T: ?core:: pub fn aya_build::Toolchain<'a>::borrow_mut(&mut self) -> &mut T impl core::convert::From for aya_build::Toolchain<'a> pub fn aya_build::Toolchain<'a>::from(t: T) -> T -pub fn aya_build::build_ebpf(packages: impl core::iter::traits::collect::IntoIterator, toolchain: aya_build::Toolchain<'_>) -> anyhow::Result<()> +pub struct aya_build::Package<'a> +pub aya_build::Package::name: &'a str +pub aya_build::Package::root_dir: &'a str +impl<'a> core::marker::Freeze 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::Unpin for aya_build::Package<'a> +impl<'a> core::panic::unwind_safe::RefUnwindSafe for aya_build::Package<'a> +impl<'a> core::panic::unwind_safe::UnwindSafe for aya_build::Package<'a> +impl core::convert::Into for aya_build::Package<'a> where U: core::convert::From +pub fn aya_build::Package<'a>::into(self) -> U +impl core::convert::TryFrom for aya_build::Package<'a> where U: core::convert::Into +pub type aya_build::Package<'a>::Error = core::convert::Infallible +pub fn aya_build::Package<'a>::try_from(value: U) -> core::result::Result>::Error> +impl core::convert::TryInto for aya_build::Package<'a> where U: core::convert::TryFrom +pub type aya_build::Package<'a>::Error = >::Error +pub fn aya_build::Package<'a>::try_into(self) -> core::result::Result>::Error> +impl core::any::Any for aya_build::Package<'a> where T: 'static + ?core::marker::Sized +pub fn aya_build::Package<'a>::type_id(&self) -> core::any::TypeId +impl core::borrow::Borrow for aya_build::Package<'a> where T: ?core::marker::Sized +pub fn aya_build::Package<'a>::borrow(&self) -> &T +impl core::borrow::BorrowMut for aya_build::Package<'a> where T: ?core::marker::Sized +pub fn aya_build::Package<'a>::borrow_mut(&mut self) -> &mut T +impl core::convert::From for aya_build::Package<'a> +pub fn aya_build::Package<'a>::from(t: T) -> T +pub fn aya_build::build_ebpf<'a>(packages: impl core::iter::traits::collect::IntoIterator>, toolchain: aya_build::Toolchain<'a>) -> anyhow::Result<()> pub fn aya_build::emit_bpf_target_arch_cfg()