add a build command (#108)

Add a new command to build ebpf and userspace programs without running.

```
cargo xtask build
```

Co-authored-by: Michal Rostecki <vadorovsky@protonmail.com>
pull/104/head
wanjunlei 4 months ago committed by GitHub
parent f642921fee
commit 7c143de8af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -19,6 +19,12 @@ You may also change the target architecture with the `--target` flag.
cargo build
```
## Build eBPF and Userspace
```bash
cargo xtask build
```
## Run
```bash

@ -0,0 +1,42 @@
use std::process::Command;
use anyhow::Context as _;
use clap::Parser;
use crate::build_ebpf::{build_ebpf, Architecture, Options as BuildOptions};
#[derive(Debug, Parser)]
pub struct Options {
/// Set the endianness of the BPF target
#[clap(default_value = "bpfel-unknown-none", long)]
pub bpf_target: Architecture,
/// Build and run the release target
#[clap(long)]
pub release: bool,
}
/// Build the project
fn build_project(opts: &Options) -> Result<(), anyhow::Error> {
let mut args = vec!["build"];
if opts.release {
args.push("--release")
}
let status = Command::new("cargo")
.args(&args)
.status()
.expect("failed to build userspace");
assert!(status.success());
Ok(())
}
/// Build our ebpf program and the project
pub fn build(opts: Options) -> Result<(), anyhow::Error> {
// build our ebpf program followed by our application
build_ebpf(BuildOptions {
target: opts.bpf_target,
release: opts.release,
})
.context("Error while building eBPF program")?;
build_project(&opts).context("Error while building userspace application")?;
Ok(())
}

@ -1,4 +1,5 @@
mod build_ebpf;
mod build;
mod run;
use std::process::exit;
@ -14,6 +15,7 @@ pub struct Options {
#[derive(Debug, Parser)]
enum Command {
BuildEbpf(build_ebpf::Options),
Build(build::Options),
Run(run::Options),
}
@ -24,6 +26,7 @@ fn main() {
let ret = match opts.command {
BuildEbpf(opts) => build_ebpf::build_ebpf(opts),
Run(opts) => run::run(opts),
Build(opts) => build::build(opts),
};
if let Err(e) = ret {

@ -3,7 +3,7 @@ use std::process::Command;
use anyhow::Context as _;
use clap::Parser;
use crate::build_ebpf::{build_ebpf, Architecture, Options as BuildOptions};
use crate::{build::{build, Options as BuildOptions}, build_ebpf::Architecture};
#[derive(Debug, Parser)]
pub struct Options {
@ -21,30 +21,15 @@ pub struct Options {
pub run_args: Vec<String>,
}
/// Build the project
fn build(opts: &Options) -> Result<(), anyhow::Error> {
let mut args = vec!["build"];
if opts.release {
args.push("--release")
}
let status = Command::new("cargo")
.args(&args)
.status()
.expect("failed to build userspace");
assert!(status.success());
Ok(())
}
/// Build and run the project
pub fn run(opts: Options) -> Result<(), anyhow::Error> {
// build our ebpf program followed by our application
build_ebpf(BuildOptions {
target: opts.bpf_target,
// Build our ebpf program and the project
build(BuildOptions{
bpf_target: opts.bpf_target,
release: opts.release,
})
.context("Error while building eBPF program")?;
build(&opts).context("Error while building userspace application")?;
}).context("Error while building project")?;
// profile we are building (release or debug)
let profile = if opts.release { "release" } else { "debug" };
let bin_path = format!("target/{profile}/{{project-name}}");

Loading…
Cancel
Save