use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
use super::super::args::UserDepFlags;
use crate::core::NormalizedPath;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DepfileStrategy {
Injected { path: NormalizedPath },
UserSpecified { path: NormalizedPath },
UserDefault { path: NormalizedPath },
ShowIncludes,
Unsupported,
}
#[must_use]
pub fn user_depfile_destination(
dep_flags: &UserDepFlags,
output_file: &Path,
) -> Option<NormalizedPath> {
if let Some(ref mf_path) = dep_flags.mf_path {
return Some(mf_path.clone());
}
if dep_flags.has_md {
return Some(output_file.with_extension("d").into());
}
None
}
pub fn prepare_depfile(
supports_depfile: bool,
dep_flags: &UserDepFlags,
output_file: &Path,
tmpdir: &Path,
) -> (Vec<String>, DepfileStrategy) {
if !supports_depfile {
return (Vec::new(), DepfileStrategy::Unsupported);
}
if let Some(ref mf_path) = dep_flags.mf_path {
return (
Vec::new(),
DepfileStrategy::UserSpecified {
path: mf_path.clone(),
},
);
}
if dep_flags.has_md {
let d_path = output_file.with_extension("d");
return (
Vec::new(),
DepfileStrategy::UserDefault {
path: d_path.into(),
},
);
}
if !tmpdir.exists() {
let _ = std::fs::create_dir_all(tmpdir);
}
static DEPFILE_COUNTER: AtomicU64 = AtomicU64::new(0);
let unique = DEPFILE_COUNTER.fetch_add(1, Ordering::Relaxed);
let stem = output_file
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("depfile");
let tmp_path = tmpdir.join(format!("{stem}_{}_{unique}.d", std::process::id()));
let tmp_path: NormalizedPath = tmp_path.into();
let extra_args = vec![
"-MD".to_string(),
"-MF".to_string(),
tmp_path.to_string_lossy().into_owned(),
];
(extra_args, DepfileStrategy::Injected { path: tmp_path })
}