Cross-compile C binaries too

This commit is contained in:
ppom 2025-06-06 12:00:00 +02:00
commit ccd9f53758
No known key found for this signature in database

View file

@ -1,5 +1,5 @@
use std::{
env::var_os,
env::{var, var_os},
io::{self, ErrorKind},
path::Path,
process,
@ -10,8 +10,22 @@ use clap_complete::shells;
// SubCommand defined here
include!("src/cli.rs");
fn compile_helper(name: &str, out_dir: &Path) -> io::Result<()> {
process::Command::new("gcc")
fn gcc() -> String {
// TARGET looks like aarch64-unknown-linux-musl
match var("TARGET") {
Ok(target) => {
// We're looking for an environment variable looking like
// CC_aarch64_unknown_linux_musl
let target = target.replace("-", "_");
var(format!("CC_{}", target.replace("-", "_"))).ok()
}
Err(_) => None,
}
.unwrap_or("gcc".into())
}
fn compile_helper(cc: &str, name: &str, out_dir: &Path) -> io::Result<()> {
process::Command::new(cc)
.args([
&format!("helpers_c/{name}.c"),
"-o",
@ -26,8 +40,10 @@ fn main() -> io::Result<()> {
let out_dir = PathBuf::from(var_os("OUT_DIR").ok_or(ErrorKind::NotFound)?).join("../../..");
// Compile C helpers
compile_helper("ip46tables", &out_dir)?;
compile_helper("nft46", &out_dir)?;
let cc = gcc();
println!("CC is: {}", cc);
compile_helper(&cc, "ip46tables", &out_dir)?;
compile_helper(&cc, "nft46", &out_dir)?;
// Build CLI
let cli = clap::Command::new("reaction");