use clap::Parser;
use sbpf_linker::{SbpfLinkerError, link_program};
use std::fs;
use std::path::{Path, PathBuf};
#[derive(Debug, Parser)]
#[command(
name = "sbpf-link",
version,
about = "Simple SBPF linker that processes object files directly"
)]
struct Args {
#[clap(value_name = "INPUT")]
input: PathBuf,
}
fn main() -> Result<(), SbpfLinkerError> {
let args = Args::parse();
println!("Linking: {}", args.input.display());
let linked_bytecode = link_object_file(&args.input)?;
let parent = args.input.parent().unwrap_or_else(|| Path::new("."));
let stem =
args.input.file_stem().and_then(|s| s.to_str()).unwrap_or("output");
let output = parent.join(format!("{stem}.so"));
println!("Writing output to: {}", output.display());
std::fs::write(&output, &linked_bytecode)?;
println!("Successfully linked {} bytes", linked_bytecode.len());
Ok(())
}
fn link_object_file<P: AsRef<Path>>(
path: P,
) -> Result<Vec<u8>, SbpfLinkerError> {
let bytes = fs::read(path.as_ref())?;
link_program(&bytes)
}