use std::path::PathBuf;
use anyhow::{bail, Context, Result};
use super::{find_executable, CompilerInfo, CompilerType, Toolchain};
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OpenwrtArch {
Mipsel24kc,
Mips24kc,
ArmCortexA7,
Aarch64,
}
impl OpenwrtArch {
pub fn triple_prefix(self) -> &'static str {
match self {
OpenwrtArch::Mipsel24kc => "mipsel-linux-musl",
OpenwrtArch::Mips24kc => "mips-linux-musl",
OpenwrtArch::ArmCortexA7 => "arm-linux-musleabihf",
OpenwrtArch::Aarch64 => "aarch64-linux-musl",
}
}
pub fn arch_string(self) -> &'static str {
match self {
OpenwrtArch::Mipsel24kc => "mipsel_24kc",
OpenwrtArch::Mips24kc => "mips_24kc",
OpenwrtArch::ArmCortexA7 => "arm_cortex_a7",
OpenwrtArch::Aarch64 => "aarch64",
}
}
pub fn cmake_system_processor(self) -> &'static str {
match self {
OpenwrtArch::Mipsel24kc => "mipsel",
OpenwrtArch::Mips24kc => "mips",
OpenwrtArch::ArmCortexA7 => "arm",
OpenwrtArch::Aarch64 => "aarch64",
}
}
pub fn cpu_cflags(self) -> &'static str {
match self {
OpenwrtArch::Mipsel24kc => "-march=mips32r2 -mtune=24kc",
OpenwrtArch::Mips24kc => "-march=mips32r2 -mtune=24kc",
OpenwrtArch::ArmCortexA7 => {
"-march=armv7-a -mcpu=cortex-a7 -mfpu=neon-vfpv4 -mfloat-abi=hard"
}
OpenwrtArch::Aarch64 => "",
}
}
pub fn parse(s: &str) -> Result<Self> {
match s.to_lowercase().as_str() {
"mipsel_24kc" | "mipsel" => Ok(OpenwrtArch::Mipsel24kc),
"mips_24kc" | "mips" => Ok(OpenwrtArch::Mips24kc),
"arm_cortex_a7" | "arm_cortex-a7" | "arm" => Ok(OpenwrtArch::ArmCortexA7),
"aarch64" | "arm64" => Ok(OpenwrtArch::Aarch64),
other => bail!(
"Unknown OpenWrt architecture '{}'. Supported: mipsel_24kc, mips_24kc, arm_cortex_a7, aarch64",
other
),
}
}
}
pub struct OpenwrtToolchain {
compiler: CompilerInfo,
arch: OpenwrtArch,
}
impl OpenwrtToolchain {
pub fn detect_for_arch(arch: OpenwrtArch) -> Result<Self> {
let prefix = arch.triple_prefix();
let cc_name = format!("{}-gcc", prefix);
let cxx_name = format!("{}-g++", prefix);
let cc = find_executable(&cc_name).ok_or_else(|| {
anyhow::anyhow!(
"OpenWrt cross-compiler not found: {}\n\
Use Docker to build OpenWrt targets: ccgo build openwrt --docker",
cc_name
)
})?;
let cxx = find_executable(&cxx_name).with_context(|| {
format!("OpenWrt C++ cross-compiler not found: {}", cxx_name)
})?;
let version = super::get_compiler_version(&cc).unwrap_or_else(|| "unknown".to_string());
Ok(Self {
compiler: CompilerInfo { compiler_type: CompilerType::Gcc, cc, cxx, version },
arch,
})
}
pub fn arch(&self) -> OpenwrtArch {
self.arch
}
pub fn ar_path(&self) -> PathBuf {
let ar_name = format!("{}-ar", self.arch.triple_prefix());
find_executable(&ar_name).unwrap_or_else(|| PathBuf::from("ar"))
}
pub fn cross_cmake_variables(&self) -> Vec<(String, String)> {
let prefix = self.arch.triple_prefix();
let sysroot = format!("/opt/cross/{}-cross/{}", prefix, prefix);
let mut vars = vec![
("CMAKE_SYSTEM_NAME".to_string(), "Linux".to_string()),
(
"CMAKE_SYSTEM_PROCESSOR".to_string(),
self.arch.cmake_system_processor().to_string(),
),
("CMAKE_C_COMPILER".to_string(), self.compiler.cc.display().to_string()),
("CMAKE_CXX_COMPILER".to_string(), self.compiler.cxx.display().to_string()),
("CMAKE_FIND_ROOT_PATH".to_string(), sysroot),
("CMAKE_FIND_ROOT_PATH_MODE_PROGRAM".to_string(), "NEVER".to_string()),
("CMAKE_FIND_ROOT_PATH_MODE_LIBRARY".to_string(), "ONLY".to_string()),
("CMAKE_FIND_ROOT_PATH_MODE_INCLUDE".to_string(), "ONLY".to_string()),
];
let cflags = self.arch.cpu_cflags();
if !cflags.is_empty() {
vars.push(("CMAKE_C_FLAGS_INIT".to_string(), cflags.to_string()));
vars.push(("CMAKE_CXX_FLAGS_INIT".to_string(), cflags.to_string()));
}
vars
}
pub fn merge_static_libs(&self, src_libs: &[PathBuf], dst_lib: &PathBuf) -> Result<()> {
if src_libs.is_empty() {
bail!("No source libraries to merge");
}
if let Some(parent) = dst_lib.parent() {
std::fs::create_dir_all(parent)?;
}
let temp_dir = std::env::temp_dir().join(format!(
"ccgo-openwrt-merge-{}",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs()
));
std::fs::create_dir_all(&temp_dir)?;
let ar_cmd = self.ar_path();
for (idx, lib) in src_libs.iter().enumerate() {
let extract_dir = temp_dir.join(format!("lib{}", idx));
std::fs::create_dir_all(&extract_dir)?;
let output = std::process::Command::new(&ar_cmd)
.arg("x")
.arg(lib)
.current_dir(&extract_dir)
.output()
.context("Failed to run ar for extraction")?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
std::fs::remove_dir_all(&temp_dir).ok();
bail!("ar extraction failed for {}: {}", lib.display(), stderr);
}
}
let mut object_files: Vec<PathBuf> = Vec::new();
for entry in walkdir::WalkDir::new(&temp_dir) {
let entry = entry?;
let ext = entry.path().extension().and_then(|e| e.to_str());
if matches!(ext, Some("o") | Some("obj")) {
object_files.push(entry.path().to_path_buf());
}
}
if object_files.is_empty() {
std::fs::remove_dir_all(&temp_dir).ok();
bail!("No object files found in source libraries");
}
if dst_lib.exists() {
std::fs::remove_file(dst_lib)?;
}
let mut cmd = std::process::Command::new(&ar_cmd);
cmd.arg("rcs").arg(dst_lib);
for obj in &object_files {
cmd.arg(obj);
}
let output = cmd.output().context("Failed to run ar for merging")?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
std::fs::remove_dir_all(&temp_dir).ok();
bail!("ar merge failed: {}", stderr);
}
std::fs::remove_dir_all(&temp_dir).ok();
Ok(())
}
}
impl Toolchain for OpenwrtToolchain {
fn name(&self) -> &str {
"musl-gcc"
}
fn is_available(&self) -> bool {
self.compiler.cc.exists() && self.compiler.cxx.exists()
}
fn path(&self) -> Option<PathBuf> {
self.compiler.cc.parent().map(|p| p.to_path_buf())
}
fn cmake_variables(&self) -> Vec<(String, String)> {
self.cross_cmake_variables()
}
fn validate(&self) -> Result<()> {
if !self.compiler.cc.exists() {
bail!("C cross-compiler not found at: {}", self.compiler.cc.display());
}
if !self.compiler.cxx.exists() {
bail!("C++ cross-compiler not found at: {}", self.compiler.cxx.display());
}
Ok(())
}
}