Kaynağa Gözat

xtask: Move libbpf header installation logic to a helper function

Before that, the same code was repeated twice in different places.
Michal Rostecki 2 gün önce
ebeveyn
işleme
e229231d88

+ 2 - 13
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");
 

+ 5 - 19
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");
 

+ 22 - 1
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<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)]
 pub struct Errors<E>(Vec<E>);