aya-build: remove cargo_metadata from public API

reviewable/pr1376/r12
Tamir Duberstein 3 days ago
parent a18e283e2a
commit d9704be77d
No known key found for this signature in database

@ -6,9 +6,12 @@ use std::{
}; };
use anyhow::{Context as _, Result, anyhow}; use anyhow::{Context as _, Result, anyhow};
// Re-export `cargo_metadata` to having to encode the version downstream and risk mismatches. use cargo_metadata::{Artifact, CompilerMessage, Message, Target};
pub use cargo_metadata;
use cargo_metadata::{Artifact, CompilerMessage, Message, Package, Target}; pub struct Package<'a> {
pub name: &'a str,
pub root_dir: &'a str,
}
/// Build binary artifacts produced by `packages`. /// 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. /// prevent their use for the time being.
/// ///
/// [bindeps]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html?highlight=feature#artifact-dependencies /// [bindeps]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html?highlight=feature#artifact-dependencies
pub fn build_ebpf( pub fn build_ebpf<'a>(
packages: impl IntoIterator<Item = Package>, packages: impl IntoIterator<Item = Package<'a>>,
toolchain: Toolchain<'_>, toolchain: Toolchain<'a>,
) -> Result<()> { ) -> Result<()> {
let out_dir = env::var_os("OUT_DIR").ok_or(anyhow!("OUT_DIR not set"))?; let out_dir = env::var_os("OUT_DIR").ok_or(anyhow!("OUT_DIR not set"))?;
let out_dir = PathBuf::from(out_dir); let out_dir = PathBuf::from(out_dir);
@ -44,21 +47,12 @@ pub fn build_ebpf(
let target = format!("{target}-unknown-none"); let target = format!("{target}-unknown-none");
for Package { for Package { name, root_dir } in packages {
name,
manifest_path,
..
} in packages
{
let dir = manifest_path
.parent()
.ok_or(anyhow!("no parent for {manifest_path}"))?;
// 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
// changes to the binaries too, which gets us the rest of the way. // 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"); let mut cmd = Command::new("rustup");
cmd.args([ cmd.args([
@ -67,7 +61,7 @@ pub fn build_ebpf(
"cargo", "cargo",
"build", "build",
"--package", "--package",
&name, name,
"-Z", "-Z",
"build-std=core", "build-std=core",
"--bins", "--bins",
@ -98,7 +92,7 @@ pub fn build_ebpf(
} }
// Workaround for https://github.com/rust-lang/cargo/issues/6412 where cargo flocks itself. // 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); cmd.arg("--target-dir").arg(&target_dir);
let mut child = cmd let mut child = cmd

@ -45,6 +45,7 @@ xdpilone = { workspace = true }
[build-dependencies] [build-dependencies]
anyhow = { workspace = true } anyhow = { workspace = true }
aya-build = { path = "../../aya-build" } 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 # 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 # 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 # script to build this, but we want to teach cargo about the dependecy so that cache invalidation

@ -12,7 +12,7 @@ use std::{
}; };
use anyhow::{Context as _, Ok, Result, anyhow}; 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}; 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 /// 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())?; aya_build::build_ebpf([integration_ebpf_package], aya_build::Toolchain::default())?;
} else { } else {
for (src, build_btf) in C_BPF { for (src, build_btf) in C_BPF {

@ -1,5 +1,4 @@
pub mod aya_build pub mod aya_build
pub use aya_build::cargo_metadata
pub enum aya_build::Toolchain<'a> pub enum aya_build::Toolchain<'a>
pub aya_build::Toolchain::Custom(&'a str) pub aya_build::Toolchain::Custom(&'a str)
pub aya_build::Toolchain::Nightly pub aya_build::Toolchain::Nightly
@ -27,5 +26,30 @@ impl<T> core::borrow::BorrowMut<T> for aya_build::Toolchain<'a> where T: ?core::
pub fn aya_build::Toolchain<'a>::borrow_mut(&mut self) -> &mut T 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 fn aya_build::build_ebpf(packages: impl core::iter::traits::collect::IntoIterator<Item = cargo_metadata::Package>, 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<T, U> core::convert::Into<U> for aya_build::Package<'a> where U: core::convert::From<T>
pub fn aya_build::Package<'a>::into(self) -> U
impl<T, U> core::convert::TryFrom<U> for aya_build::Package<'a> where U: core::convert::Into<T>
pub type aya_build::Package<'a>::Error = core::convert::Infallible
pub fn aya_build::Package<'a>::try_from(value: U) -> core::result::Result<T, <T as core::convert::TryFrom<U>>::Error>
impl<T, U> core::convert::TryInto<U> for aya_build::Package<'a> where U: core::convert::TryFrom<T>
pub type aya_build::Package<'a>::Error = <U as core::convert::TryFrom<T>>::Error
pub fn aya_build::Package<'a>::try_into(self) -> core::result::Result<U, <U as core::convert::TryFrom<T>>::Error>
impl<T> 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<T> core::borrow::Borrow<T> for aya_build::Package<'a> where T: ?core::marker::Sized
pub fn aya_build::Package<'a>::borrow(&self) -> &T
impl<T> core::borrow::BorrowMut<T> for aya_build::Package<'a> where T: ?core::marker::Sized
pub fn aya_build::Package<'a>::borrow_mut(&mut self) -> &mut T
impl<T> core::convert::From<T> 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<Item = aya_build::Package<'a>>, toolchain: aya_build::Toolchain<'a>) -> anyhow::Result<()>
pub fn aya_build::emit_bpf_target_arch_cfg() pub fn aya_build::emit_bpf_target_arch_cfg()

Loading…
Cancel
Save