diff --git a/test/integration-test/build.rs b/test/integration-test/build.rs index 29e41dda..47bfce48 100644 --- a/test/integration-test/build.rs +++ b/test/integration-test/build.rs @@ -8,7 +8,7 @@ use std::{ use anyhow::{Context as _, Ok, Result, anyhow}; use aya_build::cargo_metadata::{Metadata, MetadataCommand, Package, Target, TargetKind}; -use xtask::{AYA_BUILD_INTEGRATION_BPF, LIBBPF_DIR, exec}; +use xtask::{AYA_BUILD_INTEGRATION_BPF, LIBBPF_DIR, exec, install_libbpf_headers}; /// This file, along with the xtask crate, allows analysis tools such as `cargo check`, `cargo /// clippy`, and even `cargo build` to work as users expect. Prior to this file's existence, this @@ -86,18 +86,7 @@ fn main() -> Result<()> { println!("cargo:rerun-if-changed={libbpf_dir}"); let libbpf_headers_dir = out_dir.join("libbpf_headers"); - - let mut includedir = OsString::new(); - includedir.push("INCLUDEDIR="); - includedir.push(&libbpf_headers_dir); - - exec( - Command::new("make") - .arg("-C") - .arg(libbpf_dir.join("src")) - .arg(includedir) - .arg("install_headers"), - )?; + install_libbpf_headers(&libbpf_dir, &libbpf_headers_dir)?; let bpf_dir = manifest_dir.join("bpf"); diff --git a/xtask/src/codegen/aya_ebpf_bindings.rs b/xtask/src/codegen/aya_ebpf_bindings.rs index d0285387..702761b3 100644 --- a/xtask/src/codegen/aya_ebpf_bindings.rs +++ b/xtask/src/codegen/aya_ebpf_bindings.rs @@ -1,8 +1,6 @@ use std::{ - ffi::OsString, fs::{File, create_dir_all}, path::{Path, PathBuf}, - process::Command, }; use anyhow::{Context as _, Result}; @@ -10,13 +8,11 @@ use aya_tool::bindgen; use proc_macro2::TokenStream; use quote::ToTokens as _; use syn::{Item, parse_str}; +use xtask::install_libbpf_headers; -use crate::{ - codegen::{ - Architecture, SysrootOptions, - helpers::{expand_helpers, extract_helpers}, - }, - exec, +use crate::codegen::{ + Architecture, SysrootOptions, + helpers::{expand_helpers, extract_helpers}, }; pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<()> { @@ -34,17 +30,7 @@ pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<()> { let tmp_dir = tempfile::tempdir().context("tempdir failed")?; let libbpf_headers_dir = tmp_dir.path().join("libbpf_headers"); - let mut includedir = OsString::new(); - includedir.push("INCLUDEDIR="); - includedir.push(&libbpf_headers_dir); - - exec( - Command::new("make") - .arg("-C") - .arg(libbpf_dir.join("src")) - .arg(includedir) - .arg("install_headers"), - )?; + install_libbpf_headers(libbpf_dir, &libbpf_headers_dir)?; let dir = PathBuf::from("ebpf/aya-ebpf-bindings"); diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index cd7abe3a..9a9f56aa 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -1,4 +1,4 @@ -use std::process::Command; +use std::{ffi::OsString, path::Path, process::Command}; use anyhow::{Context as _, Result, bail}; @@ -15,6 +15,27 @@ pub fn exec(cmd: &mut Command) -> Result<()> { Ok(()) } +/// Installs the libbpf headers files from the `source_dir` to the +/// `headers_dir`. +pub fn install_libbpf_headers( + source_dir: impl AsRef, + headers_dir: impl AsRef, +) -> Result<()> { + let mut includedir = OsString::new(); + includedir.push("INCLUDEDIR="); + includedir.push(headers_dir.as_ref().as_os_str()); + + exec( + Command::new("make") + .arg("-C") + .arg(source_dir.as_ref().join("src")) + .arg(includedir) + .arg("install_headers"), + )?; + + Ok(()) +} + #[derive(Debug)] pub struct Errors(Vec);