xtask: Move libbpf header installation logic to a helper function

Before that, the same code was repeated twice in different places.
pull/1238/head
Michal Rostecki 2 weeks ago
parent 6d36fe13d3
commit e229231d88

@ -8,7 +8,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 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 /// 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 /// 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}"); println!("cargo:rerun-if-changed={libbpf_dir}");
let libbpf_headers_dir = out_dir.join("libbpf_headers"); let libbpf_headers_dir = out_dir.join("libbpf_headers");
install_libbpf_headers(&libbpf_dir, &libbpf_headers_dir)?;
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"),
)?;
let bpf_dir = manifest_dir.join("bpf"); let bpf_dir = manifest_dir.join("bpf");

@ -1,8 +1,6 @@
use std::{ use std::{
ffi::OsString,
fs::{File, create_dir_all}, fs::{File, create_dir_all},
path::{Path, PathBuf}, path::{Path, PathBuf},
process::Command,
}; };
use anyhow::{Context as _, Result}; use anyhow::{Context as _, Result};
@ -10,13 +8,11 @@ use aya_tool::bindgen;
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use quote::ToTokens as _; use quote::ToTokens as _;
use syn::{Item, parse_str}; use syn::{Item, parse_str};
use xtask::install_libbpf_headers;
use crate::{ use crate::codegen::{
codegen::{ Architecture, SysrootOptions,
Architecture, SysrootOptions, helpers::{expand_helpers, extract_helpers},
helpers::{expand_helpers, extract_helpers},
},
exec,
}; };
pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<()> { 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 tmp_dir = tempfile::tempdir().context("tempdir failed")?;
let libbpf_headers_dir = tmp_dir.path().join("libbpf_headers"); let libbpf_headers_dir = tmp_dir.path().join("libbpf_headers");
let mut includedir = OsString::new(); install_libbpf_headers(libbpf_dir, &libbpf_headers_dir)?;
includedir.push("INCLUDEDIR=");
includedir.push(&libbpf_headers_dir);
exec(
Command::new("make")
.arg("-C")
.arg(libbpf_dir.join("src"))
.arg(includedir)
.arg("install_headers"),
)?;
let dir = PathBuf::from("ebpf/aya-ebpf-bindings"); let dir = PathBuf::from("ebpf/aya-ebpf-bindings");

@ -1,4 +1,4 @@
use std::process::Command; use std::{ffi::OsString, path::Path, process::Command};
use anyhow::{Context as _, Result, bail}; use anyhow::{Context as _, Result, bail};
@ -15,6 +15,27 @@ pub fn exec(cmd: &mut Command) -> Result<()> {
Ok(()) Ok(())
} }
/// Installs the libbpf headers files from the `source_dir` to the
/// `headers_dir`.
pub fn install_libbpf_headers(
source_dir: impl AsRef<Path>,
headers_dir: impl AsRef<Path>,
) -> 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)] #[derive(Debug)]
pub struct Errors<E>(Vec<E>); pub struct Errors<E>(Vec<E>);

Loading…
Cancel
Save