xtask: ensure libbpf submodule is initialized

For xtask tasks which need libbpf, make sure it's initialized automatically.
This eases the user-experience so that users don't need to think about the submodule,
while retaining all the benefits of using a submodule vs forcing the user to manually
check out libbpf and stick it in some pre-defined place.
reviewable/pr664/r1
Andrew Werner 2 years ago
parent af54a819a3
commit 7f24e25f44

@ -3,7 +3,10 @@ use std::path::PathBuf;
use aya_tool::{bindgen, write_to_file};
use crate::codegen::{Architecture, SysrootOptions};
use crate::{
codegen::{Architecture, SysrootOptions},
libbpf,
};
pub fn codegen(opts: &SysrootOptions) -> Result<(), anyhow::Error> {
codegen_internal_btf_bindings()?;
@ -13,7 +16,7 @@ pub fn codegen(opts: &SysrootOptions) -> Result<(), anyhow::Error> {
fn codegen_internal_btf_bindings() -> Result<(), anyhow::Error> {
let dir = PathBuf::from("aya-obj");
let generated = dir.join("src/generated");
let libbpf_dir = PathBuf::from("libbpf");
let libbpf_dir = libbpf::ensure_initialized()?;
let mut bindgen = bindgen::user_builder()
.clang_arg(format!(
@ -162,7 +165,7 @@ fn codegen_bindings(opts: &SysrootOptions) -> Result<(), anyhow::Error> {
let dir = PathBuf::from("aya-obj");
let generated = dir.join("src/generated");
let libbpf_dir = PathBuf::from("libbpf");
let libbpf_dir = libbpf::ensure_initialized()?;
let builder = || {
bindgen::user_builder()

@ -6,9 +6,12 @@ use std::path::PathBuf;
use aya_tool::{bindgen, write_to_file_fmt};
use syn::{parse_str, Item};
use crate::codegen::{
helpers::{expand_helpers, extract_helpers},
Architecture, SysrootOptions,
use crate::{
codegen::{
helpers::{expand_helpers, extract_helpers},
Architecture, SysrootOptions,
},
libbpf,
};
pub fn codegen(opts: &SysrootOptions) -> Result<(), anyhow::Error> {
@ -20,7 +23,7 @@ pub fn codegen(opts: &SysrootOptions) -> Result<(), anyhow::Error> {
} = opts;
let dir = PathBuf::from("bpf/aya-bpf-bindings");
let libbpf_dir = PathBuf::from("libbpf");
let libbpf_dir = libbpf::ensure_initialized()?;
let builder = || {
let mut bindgen = bindgen::bpf_builder()

@ -0,0 +1,23 @@
/// Functionality for ensuring that the submodule is properly set up.
use std::{path::PathBuf, process::Command};
use anyhow::Context;
/// Ensures that the submodule is initialized and returns the path to the submodule.
pub(crate) fn ensure_initialized() -> Result<PathBuf, anyhow::Error> {
// It's okay to hard-code this path because the submodule is checked in.
let libbpf_dir = PathBuf::from("libbpf");
// Exec `git submodule update --init` to ensure that the submodule is initialized.
let status = Command::new("git")
.args(["submodule", "update", "--init", "--"])
.arg(&libbpf_dir)
.status()
.context("failed to initialized submodules")?;
if status.success() {
Ok(libbpf_dir)
} else {
Err(anyhow::anyhow!(
"failed to initialize submodules: {status:?}"
))
}
}

@ -1,5 +1,6 @@
mod codegen;
mod docs;
mod libbpf;
mod run;
use clap::Parser;

@ -5,6 +5,7 @@ use std::{
process::{Command, Stdio},
};
use crate::libbpf;
use anyhow::{Context as _, Result};
use cargo_metadata::{Artifact, CompilerMessage, Message, Target};
use clap::Parser;
@ -33,6 +34,7 @@ pub struct Options {
/// Build the project
pub fn build(opts: BuildOptions) -> Result<Vec<(String, PathBuf)>> {
let _libbpf_dir = libbpf::ensure_initialized()?;
let BuildOptions { release, target } = opts;
let mut cmd = Command::new("cargo");
cmd.env("AYA_BUILD_INTEGRATION_BPF", "true").args([

Loading…
Cancel
Save