mod cpp;
mod icpp;
mod markdown;
mod python;
mod tera;
use anyhow::Result;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::process::Command;
use crate::deps_cache::DepsCache;
use crate::file_index::FileIndex;
use crate::graph::BuildGraph;
use crate::processors::{format_command, run_command_capture};
use crate::progress;
pub trait DepAnalyzer: Sync + Send {
fn description(&self) -> &str;
fn auto_detect(&self, file_index: &FileIndex) -> bool;
fn analyze(&self, graph: &mut BuildGraph, deps_cache: &mut DepsCache, file_index: &FileIndex, verbose: bool) -> Result<()>;
}
pub fn query_pkg_config_include_paths(tag: &str, packages: &[String], verbose: bool) -> Vec<PathBuf> {
if packages.is_empty() {
return Vec::new();
}
let mut cmd = Command::new("pkg-config");
cmd.arg("--cflags-only-I");
cmd.args(packages);
if verbose {
eprintln!("[{}] Querying pkg-config: {}", tag, format_command(&cmd));
}
let output = match run_command_capture(&mut cmd) {
Ok(o) => o,
Err(e) => {
eprintln!("[{}] Failed to query pkg-config: {}", tag, e);
return Vec::new();
}
};
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
eprintln!("[{}] pkg-config failed: {}", tag, stderr.trim());
return Vec::new();
}
let paths: Vec<PathBuf> = String::from_utf8_lossy(&output.stdout)
.split_whitespace()
.filter_map(|flag| flag.strip_prefix("-I").map(PathBuf::from))
.collect();
if verbose && !paths.is_empty() {
eprintln!("[{}] Found {} include paths from pkg-config", tag, paths.len());
}
paths
}
pub fn run_include_path_commands(tag: &str, commands: &[String], verbose: bool) -> Vec<PathBuf> {
if commands.is_empty() {
return Vec::new();
}
let mut paths = Vec::new();
for cmd_str in commands {
if cmd_str.trim().is_empty() {
continue;
}
let mut cmd = Command::new("sh");
cmd.arg("-c");
cmd.arg(cmd_str);
if verbose {
eprintln!("[{}] Running include path command: sh -c '{}'", tag, cmd_str);
}
let output = match run_command_capture(&mut cmd) {
Ok(o) => o,
Err(e) => {
eprintln!("[{}] Failed to run '{}': {}", tag, cmd_str, e);
continue;
}
};
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
eprintln!("[{}] Command '{}' failed: {}", tag, cmd_str, stderr.trim());
continue;
}
let path_str = String::from_utf8_lossy(&output.stdout).trim().to_string();
if path_str.is_empty() {
continue;
}
let path = PathBuf::from(&path_str);
if path.is_dir() {
if verbose {
eprintln!("[{}] Added include path from command: {}", tag, path.display());
}
paths.push(path);
} else if verbose {
eprintln!("[{}] Command output is not a directory: {}", tag, path_str);
}
}
if verbose && !paths.is_empty() {
eprintln!("[{}] Found {} include paths from commands", tag, paths.len());
}
paths
}
pub fn analyze_with_scanner<F, G>(
graph: &mut BuildGraph,
deps_cache: &mut DepsCache,
analyzer_name: &str,
match_product: F,
scan_deps: G,
verbose: bool,
) -> Result<()>
where
F: Fn(&crate::graph::Product) -> Option<PathBuf>,
G: Fn(&Path) -> Result<Vec<PathBuf>>,
{
let products: Vec<(usize, PathBuf)> = graph.products()
.iter()
.filter_map(|p| {
match_product(p).map(|source| (p.id, source))
})
.collect();
if products.is_empty() {
return Ok(());
}
let pb = progress::create_bar(
products.len() as u64,
verbose || crate::json_output::is_json_mode(),
);
for (id, source) in &products {
pb.set_message(format!("[{}] {}", analyzer_name, source.display()));
let deps = if let Some(cached) = deps_cache.get(source) {
cached
} else {
let scanned = scan_deps(source)?;
if let Err(e) = deps_cache.set(source, &scanned, analyzer_name) {
eprintln!("Warning: failed to cache dependencies for {}: {}", source.display(), e);
}
scanned
};
if !deps.is_empty()
&& let Some(product) = graph.get_product_mut(*id) {
let existing: HashSet<&PathBuf> = product.inputs.iter().collect();
let new_deps: Vec<PathBuf> = deps.into_iter()
.filter(|dep| !existing.contains(dep))
.collect();
product.inputs.extend(new_deps);
}
pb.inc(1);
}
pb.finish_and_clear();
if verbose {
let stats = deps_cache.stats();
if stats.hits > 0 || stats.misses > 0 {
eprintln!("[{}] Dependency cache: {} hits, {} recalculated",
analyzer_name, stats.hits, stats.misses);
}
}
Ok(())
}