|  | @@ -0,0 +1,80 @@
 | 
	
		
			
				|  |  | +use std::{fs, path::PathBuf, process::Command};
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +use anyhow::Context as _;
 | 
	
		
			
				|  |  | +use clap::Parser;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +use crate::build_ebpf::{self, build_ebpf, Architecture};
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +#[derive(Debug, Parser)]
 | 
	
		
			
				|  |  | +pub struct Options {
 | 
	
		
			
				|  |  | +    /// Set the endianness of the BPF target
 | 
	
		
			
				|  |  | +    #[clap(default_value = "bpfel-unknown-none", long)]
 | 
	
		
			
				|  |  | +    pub bpf_target: Architecture,
 | 
	
		
			
				|  |  | +    // Output folder
 | 
	
		
			
				|  |  | +    #[clap(short, long, default_value = "bin")]
 | 
	
		
			
				|  |  | +    pub output_folder: PathBuf,
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +fn build_user(_opts: &Options) -> Result<(), anyhow::Error> {
 | 
	
		
			
				|  |  | +    let status = Command::new("cargo")
 | 
	
		
			
				|  |  | +        .args(["build", "--release", "--features=default_artifact_build"])
 | 
	
		
			
				|  |  | +        .status()
 | 
	
		
			
				|  |  | +        .expect("failed to build responder userspace program");
 | 
	
		
			
				|  |  | +    if !status.success() {
 | 
	
		
			
				|  |  | +        return Err(anyhow::anyhow!(
 | 
	
		
			
				|  |  | +            "cargo build --release failed to run: {}",
 | 
	
		
			
				|  |  | +            status.to_string()
 | 
	
		
			
				|  |  | +        ));
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    Ok(())
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +pub fn build(opts: Options) -> Result<(), anyhow::Error> {
 | 
	
		
			
				|  |  | +    fs::create_dir_all(&opts.output_folder).context(format!(
 | 
	
		
			
				|  |  | +        "Error creating dirs for path: {}",
 | 
	
		
			
				|  |  | +        opts.output_folder
 | 
	
		
			
				|  |  | +            .to_str()
 | 
	
		
			
				|  |  | +            .unwrap_or("[path not valid unicode]")
 | 
	
		
			
				|  |  | +    ))?;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    build_ebpf(build_ebpf::Options {
 | 
	
		
			
				|  |  | +        target: opts.bpf_target,
 | 
	
		
			
				|  |  | +        release: true,
 | 
	
		
			
				|  |  | +    })
 | 
	
		
			
				|  |  | +    .context("Error while building eBPF program")?;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    for entry in fs::read_dir(
 | 
	
		
			
				|  |  | +        [r"target", opts.bpf_target.to_string().as_str(), r"release"]
 | 
	
		
			
				|  |  | +            .iter()
 | 
	
		
			
				|  |  | +            .collect::<PathBuf>(),
 | 
	
		
			
				|  |  | +    )? {
 | 
	
		
			
				|  |  | +        let ebpf_bin = opts.output_folder.join(r"ebpf");
 | 
	
		
			
				|  |  | +        fs::create_dir_all(&ebpf_bin)?;
 | 
	
		
			
				|  |  | +        if let Ok(entry) = entry {
 | 
	
		
			
				|  |  | +            let path = entry.path();
 | 
	
		
			
				|  |  | +            if path.is_file()
 | 
	
		
			
				|  |  | +                && !path
 | 
	
		
			
				|  |  | +                    .file_name()
 | 
	
		
			
				|  |  | +                    .unwrap()
 | 
	
		
			
				|  |  | +                    .to_str()
 | 
	
		
			
				|  |  | +                    .map(|s| s.contains("."))
 | 
	
		
			
				|  |  | +                    .unwrap_or(true)
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                let to = ebpf_bin.join(path.file_name().unwrap());
 | 
	
		
			
				|  |  | +                fs::copy(&path, &to).context(format!(
 | 
	
		
			
				|  |  | +                    "Error copying from {} to {}",
 | 
	
		
			
				|  |  | +                    path.to_str().unwrap_or("[path not valid unicode]"),
 | 
	
		
			
				|  |  | +                    to.to_str().unwrap_or("[path not valid unicode]")
 | 
	
		
			
				|  |  | +                ))?;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    build_user(&opts).context("Error while building userspace application")?;
 | 
	
		
			
				|  |  | +    fs::copy(
 | 
	
		
			
				|  |  | +        "target/release/responder",
 | 
	
		
			
				|  |  | +        opts.output_folder.join("responder"),
 | 
	
		
			
				|  |  | +    )?;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    Ok(())
 | 
	
		
			
				|  |  | +}
 |