mod build;
mod clean;
mod config_cmd;
mod deps;
mod doctor;
mod graph;
pub(crate) mod processors;
pub(crate) mod sloc;
pub(crate) mod smart;
pub(crate) mod tools;
use std::borrow::Cow;
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::{Duration, Instant};
use anyhow::Result;
use crate::analyzers::{CppDepAnalyzer, DepAnalyzer, PythonDepAnalyzer};
use crate::cli::BuildPhase;
use crate::color;
#[allow(unused_imports)]
use crate::config::*;
use crate::deps_cache::DepsCache;
use crate::errors;
use crate::file_index::FileIndex;
use crate::graph::BuildGraph;
use crate::object_store::{ObjectStore, ObjectStoreOptions};
use crate::processors as proc_mod;
use crate::processors::{LuaProcessor, ProcessorMap, ProductDiscovery, names as proc_names};
use crate::remote_cache;
use crate::tool_lock;
pub(crate) type PhaseTimings = Vec<(String, Duration)>;
pub enum ValidationSeverity {
Error,
Warning,
}
pub struct ValidationIssue {
pub severity: ValidationSeverity,
pub message: String,
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum GraphBuildMode {
Normal,
ForClean,
}
fn sorted_keys<V>(map: &HashMap<String, V>) -> Vec<&String> {
let mut keys: Vec<&String> = map.keys().collect();
keys.sort();
keys
}
fn phases_debug() -> bool {
crate::runtime_flags::phases_debug()
}
struct ProductStatusLabels<'a> {
current: (Cow<'a, str>, &'static str),
restorable: (Cow<'a, str>, &'static str),
stale: (Cow<'a, str>, &'static str),
}
macro_rules! gen_processor_dispatch {
( $( $const_name:ident, $field:ident, $config_type:ty, $proc_type:ident,
($scan_dir:expr, $exts:expr, $excl:expr); )* ) => {
pub(crate) fn create_processor_for_instance(
type_name: &str,
config_toml: &toml::Value,
) -> anyhow::Result<Option<Box<dyn ProductDiscovery>>> {
match type_name {
$(
stringify!($field) => {
let mut cfg: $config_type = toml::from_str(&toml::to_string(config_toml)?)?;
cfg.scan.resolve($scan_dir, $exts, $excl);
gen_processor_dispatch!(@construct $proc_type, cfg)
}
)*
_ => Ok(None), }
}
pub(crate) fn create_all_default_processors() -> ProcessorMap {
let mut processors: ProcessorMap = HashMap::new();
$(
gen_processor_dispatch!(@register_default processors, $const_name, $field, $config_type, $proc_type,
$scan_dir, $exts, $excl);
)*
processors
}
};
(@construct $proc_type:ident, $cfg:ident) => {
Ok(Some(Box::new(proc_mod::$proc_type::new($cfg))))
};
(@register_default $processors:ident, $const_name:ident, $field:ident, $config_type:ty, $proc_type:ident,
$scan_dir:expr, $exts:expr, $excl:expr) => {
{
let mut cfg = <$config_type>::default();
cfg.scan.resolve($scan_dir, $exts, $excl);
Builder::register(
&mut $processors,
proc_names::$const_name,
proc_mod::$proc_type::new(cfg),
);
}
};
}
for_each_processor!(gen_processor_dispatch);
pub struct Builder {
object_store: ObjectStore,
config: Config,
file_index: FileIndex,
}
impl Builder {
pub fn new() -> Result<Self> {
Config::require_config()?;
let config = Config::load()?;
if config.cache.compression && config.cache.restore_method == crate::config::RestoreMethod::Hardlink {
anyhow::bail!("Cannot use cache compression with hardlink restore method. \
Set restore_method = \"copy\" or disable compression.");
}
let remote_backend = match &config.cache.remote {
Some(url) => Some(remote_cache::create_backend(url)?),
None => None,
};
let object_store = ObjectStore::new(ObjectStoreOptions {
restore_method: config.cache.restore_method,
compression: config.cache.compression,
remote: remote_backend,
remote_push: config.cache.remote_push,
remote_pull: config.cache.remote_pull,
mtime_check: config.cache.mtime_check,
})?;
let file_index = FileIndex::build()?;
Ok(Self {
object_store,
config,
file_index,
})
}
fn register(processors: &mut ProcessorMap, name: &str, proc: impl ProductDiscovery + 'static) {
processors.insert(name.to_string(), Box::new(proc));
}
pub fn create_processors(&self) -> Result<ProcessorMap> {
let cfg = &self.config.processor;
let mut processors: ProcessorMap = HashMap::new();
for inst in &cfg.instances {
match create_processor_for_instance(&inst.type_name, &inst.config_toml) {
Ok(Some(proc)) => {
processors.insert(inst.instance_name.clone(), proc);
}
Ok(None) => {
anyhow::bail!("Unknown processor type: '{}'", inst.type_name);
}
Err(e) => {
return Err(e.context(format!(
"Failed to create processor instance '{}'", inst.instance_name
)));
}
}
}
let lua_plugins = LuaProcessor::discover_plugins(
&self.config.plugins.dir,
&cfg.extra,
)?;
for (name, proc) in lua_plugins {
if processors.contains_key(&name) {
anyhow::bail!("Lua plugin '{}' conflicts with processor instance", name);
}
processors.insert(name, Box::new(proc));
}
Ok(processors)
}
fn detect_config_changes(&self, processors: &ProcessorMap) {
if crate::json_output::is_json_mode() || crate::runtime_flags::quiet() {
return;
}
for name in sorted_keys(processors) {
let processor = processors.get(name).expect(errors::PROCESSOR_NOT_IN_MAP);
let config_json = match processor.config_json() {
Some(json) => json,
None => continue,
};
if let Ok(Some(old_json)) = self.object_store.store_processor_config(name, &config_json) {
if let Some(diff) = ObjectStore::diff_configs(&old_json, &config_json) {
println!("{}",
color::yellow(&format!("Config changed for [{}]:", name)));
println!("{}", diff);
}
}
}
}
fn create_analyzers(&self, verbose: bool) -> HashMap<String, Box<dyn DepAnalyzer>> {
let mut analyzers: HashMap<String, Box<dyn DepAnalyzer>> = HashMap::new();
let cpp_analyzer = CppDepAnalyzer::new(
self.config.analyzer.cpp.clone(),
verbose,
);
analyzers.insert("cpp".to_string(), Box::new(cpp_analyzer));
let python_analyzer = PythonDepAnalyzer::new();
analyzers.insert("python".to_string(), Box::new(python_analyzer));
analyzers
}
fn run_analyzers(&self, graph: &mut BuildGraph, verbose: bool) -> Result<()> {
let analyzers = self.create_analyzers(verbose);
let mut deps_cache = DepsCache::open()?;
let active_analyzers: Vec<&String> = sorted_keys(&analyzers).into_iter()
.filter(|name| {
let in_enabled_list = self.config.analyzer.is_enabled(name);
if !in_enabled_list {
return false;
}
!self.config.analyzer.auto_detect || analyzers[*name].auto_detect(&self.file_index)
})
.collect();
for name in &active_analyzers {
analyzers[*name].analyze(graph, &mut deps_cache, &self.file_index, verbose)?;
}
Ok(())
}
fn build_graph_with_processors(&self, processors: &ProcessorMap) -> Result<BuildGraph> {
let (graph, _) = self.build_graph_with_processors_impl(processors, GraphBuildMode::Normal, BuildPhase::Build, None, false)?;
Ok(graph)
}
fn build_graph_with_processors_and_phase(&self, processors: &ProcessorMap, stop_after: BuildPhase, processor_filter: Option<&[String]>, verbose: bool) -> Result<(BuildGraph, PhaseTimings)> {
self.build_graph_with_processors_impl(processors, GraphBuildMode::Normal, stop_after, processor_filter, verbose)
}
fn build_graph_for_clean_with_processors(&self, processors: &ProcessorMap) -> Result<BuildGraph> {
let (graph, _) = self.build_graph_with_processors_impl(processors, GraphBuildMode::ForClean, BuildPhase::Build, None, false)?;
Ok(graph)
}
pub fn detected_processors(&self) -> Result<std::collections::HashSet<String>> {
let processors = create_all_default_processors();
let mut detected = std::collections::HashSet::new();
for (name, proc) in &processors {
if proc.auto_detect(&self.file_index) {
detected.insert(name.clone());
}
}
Ok(detected)
}
pub fn detected_and_available_processors(&self) -> Result<std::collections::HashSet<String>> {
let processors = create_all_default_processors();
let mut available = std::collections::HashSet::new();
for (name, proc) in &processors {
if !proc.auto_detect(&self.file_index) {
continue;
}
let tools = proc.required_tools();
if tools.iter().all(|t| which::which(t).is_ok()) {
available.insert(name.clone());
}
}
Ok(available)
}
fn is_processor_active(&self, _name: &str, _processor: &dyn ProductDiscovery) -> bool {
true
}
fn build_graph_with_processors_impl(&self, processors: &ProcessorMap, mode: GraphBuildMode, stop_after: BuildPhase, processor_filter: Option<&[String]>, verbose: bool) -> Result<(BuildGraph, PhaseTimings)> {
if phases_debug() {
eprintln!("{}", color::bold("Phase: Building dependency graph..."));
}
let mut graph = BuildGraph::new();
let mut phase_timings = PhaseTimings::new();
let active_processors: Vec<&String> = sorted_keys(processors).into_iter()
.filter(|name| {
if let Some(filter) = processor_filter
&& !filter.iter().any(|f| f == *name) {
return false;
}
self.is_processor_active(name, processors[*name].as_ref())
})
.collect();
if phases_debug() {
eprintln!("{}", color::dim(" Phase: discover"));
}
let t = Instant::now();
for name in &active_processors {
if mode == GraphBuildMode::ForClean {
processors[*name].discover_for_clean(&mut graph, &self.file_index)?;
} else {
processors[*name].discover(&mut graph, &self.file_index)?;
}
}
phase_timings.push(("discover".to_string(), t.elapsed()));
if stop_after == BuildPhase::Discover {
return Ok((graph, phase_timings));
}
if mode == GraphBuildMode::Normal {
if phases_debug() {
eprintln!("{}", color::dim(" Phase: add_dependencies"));
}
let t = Instant::now();
self.run_analyzers(&mut graph, verbose)?;
phase_timings.push(("add_dependencies".to_string(), t.elapsed()));
}
if stop_after == BuildPhase::AddDependencies {
return Ok((graph, phase_timings));
}
if phases_debug() {
eprintln!("{}", color::dim(" Phase: apply_tool_version_hashes"));
}
let t = Instant::now();
let tool_hashes = tool_lock::processor_tool_hashes(
processors,
&|_name| true, )?;
if !tool_hashes.is_empty() {
graph.apply_tool_version_hashes(&tool_hashes);
}
phase_timings.push(("tool_version_hashes".to_string(), t.elapsed()));
if phases_debug() {
eprintln!("{}", color::dim(" Phase: resolve_dependencies"));
}
let t = Instant::now();
graph.resolve_dependencies();
phase_timings.push(("resolve".to_string(), t.elapsed()));
Ok((graph, phase_timings))
}
pub fn build_graph_filtered(
&self,
filter_name: Option<&str>,
include_all: bool,
) -> Result<BuildGraph> {
let processors = self.create_processors()?;
let mut graph = BuildGraph::new();
let active_processors: Vec<&String> = sorted_keys(&processors).into_iter()
.filter(|name| {
if let Some(filter) = filter_name
&& name.as_str() != filter {
return false;
}
include_all || self.is_processor_active(name, processors[*name].as_ref())
})
.collect();
for name in &active_processors {
processors[*name].discover(&mut graph, &self.file_index)?;
}
self.run_analyzers(&mut graph, false)?;
graph.resolve_dependencies();
Ok(graph)
}
fn build_graph(&self) -> Result<BuildGraph> {
let processors = self.create_processors()?;
self.build_graph_with_processors(&processors)
}
pub fn build_graph_for_cache(&self) -> Result<BuildGraph> {
self.build_graph()
}
pub fn valid_cache_keys(&self) -> Result<std::collections::HashSet<String>> {
let graph = self.build_graph_for_cache()?;
Ok(graph.products().iter().map(|p| p.cache_key()).collect())
}
pub fn object_store(&self) -> &ObjectStore {
&self.object_store
}
pub fn watch_paths(&self) -> Vec<PathBuf> {
let mut paths: Vec<PathBuf> = Vec::new();
let config_path = PathBuf::from("rsconstruct.toml");
if config_path.exists() {
paths.push(config_path);
}
for dir in self.config.processor.scan_dirs() {
let full = PathBuf::from(&dir);
if full.exists() {
paths.push(full);
}
}
for name in &["pyproject.toml", "config"] {
let p = PathBuf::from(name);
if p.exists() {
paths.push(p);
}
}
let plugins = PathBuf::from(&self.config.plugins.dir);
if plugins.exists() {
paths.push(plugins);
}
paths.sort();
paths.dedup();
paths
}
}