xref: /DragonOS/user/apps/test_ebpf/syscall_ebpf/xtask/src/build.rs (revision fae6e9ade46a52976ad5d099643d51cc20876448)
1*fae6e9adSlinfeng use std::process::Command;
2*fae6e9adSlinfeng 
3*fae6e9adSlinfeng use anyhow::Context as _;
4*fae6e9adSlinfeng use clap::Parser;
5*fae6e9adSlinfeng 
6*fae6e9adSlinfeng use crate::build_ebpf::{build_ebpf, Architecture, Options as BuildOptions};
7*fae6e9adSlinfeng 
8*fae6e9adSlinfeng #[derive(Debug, Parser)]
9*fae6e9adSlinfeng pub struct Options {
10*fae6e9adSlinfeng     /// Set the endianness of the BPF target
11*fae6e9adSlinfeng     #[clap(default_value = "bpfel-unknown-none", long)]
12*fae6e9adSlinfeng     pub bpf_target: Architecture,
13*fae6e9adSlinfeng     /// Build and run the release target
14*fae6e9adSlinfeng     #[clap(long)]
15*fae6e9adSlinfeng     pub release: bool,
16*fae6e9adSlinfeng }
17*fae6e9adSlinfeng 
18*fae6e9adSlinfeng /// Build the project
build_project(opts: &Options) -> Result<(), anyhow::Error>19*fae6e9adSlinfeng fn build_project(opts: &Options) -> Result<(), anyhow::Error> {
20*fae6e9adSlinfeng     let mut args = vec!["build"];
21*fae6e9adSlinfeng     if opts.release {
22*fae6e9adSlinfeng         args.push("--release")
23*fae6e9adSlinfeng     }
24*fae6e9adSlinfeng     let status = Command::new("cargo")
25*fae6e9adSlinfeng         .args(&args)
26*fae6e9adSlinfeng         .status()
27*fae6e9adSlinfeng         .expect("failed to build userspace");
28*fae6e9adSlinfeng     assert!(status.success());
29*fae6e9adSlinfeng     Ok(())
30*fae6e9adSlinfeng }
31*fae6e9adSlinfeng 
32*fae6e9adSlinfeng /// Build our ebpf program and the project
build(opts: Options) -> Result<(), anyhow::Error>33*fae6e9adSlinfeng pub fn build(opts: Options) -> Result<(), anyhow::Error> {
34*fae6e9adSlinfeng     // build our ebpf program followed by our application
35*fae6e9adSlinfeng     build_ebpf(BuildOptions {
36*fae6e9adSlinfeng         target: opts.bpf_target,
37*fae6e9adSlinfeng         release: opts.release,
38*fae6e9adSlinfeng     })
39*fae6e9adSlinfeng     .context("Error while building eBPF program")?;
40*fae6e9adSlinfeng     build_project(&opts).context("Error while building userspace application")?;
41*fae6e9adSlinfeng     Ok(())
42*fae6e9adSlinfeng }