use super::compiler::RustcInvocation;
use super::compiler_resolve::find_workspace_root;
use std::path::Path;
use std::process::Command;
pub(crate) const KEY_FLAG: &str = "REMAP_ALIASES=canonical-v2";
fn append_aliases(cmd: &mut Command, path: &Path, replacement: &str) {
append_prefix(cmd, path, replacement);
if let Ok(canonical) = path.canonicalize() {
if canonical != path {
append_prefix(cmd, &canonical, replacement);
}
}
}
fn append_prefix(cmd: &mut Command, path: &Path, replacement: &str) {
let path = path.to_string_lossy();
let prefix = path.trim_end_matches('/');
let with_separator = if replacement.is_empty() {
String::new()
} else {
format!("{replacement}/")
};
cmd.arg(format!("--remap-path-prefix={prefix}/={with_separator}"));
if !prefix.is_empty() {
cmd.arg(format!("--remap-path-prefix={prefix}={replacement}"));
}
}
fn append_darwin_oso_prefix(inv: &RustcInvocation<'_>, cmd: &mut Command) {
if !cfg!(target_os = "macos") {
return;
}
let ws_root = find_workspace_root(&inv.unit.manifest_path);
let canonical = ws_root.canonicalize().unwrap_or_else(|_| ws_root.clone());
let root = match inv.deps_dir {
Some(deps) if deps.starts_with(&canonical) => canonical,
Some(deps) if deps.starts_with(&ws_root) => ws_root,
_ => return,
};
let prefix = format!("{}/", root.to_string_lossy().trim_end_matches('/'));
cmd.args(["-C", &format!("link-arg=-Wl,-oso_prefix,{prefix}")]);
}
pub(crate) fn append_path_remaps(
inv: &RustcInvocation<'_>,
clean_crate: &str,
cmd: &mut Command,
pkg_dir: &Path,
) {
if !super::inputs::is_immutable_package_dir(pkg_dir, inv.unit.is_workspace) {
let ws_root = find_workspace_root(&inv.unit.manifest_path);
if pkg_dir.starts_with(&ws_root) {
append_aliases(cmd, &ws_root, "");
}
}
append_darwin_oso_prefix(inv, cmd);
let stable_out = format!("/mitts/out/{clean_crate}");
for out in std::iter::once(inv.out_dir).chain(
inv.directives
.and_then(|directives| directives.out_dir.as_deref()),
) {
append_aliases(cmd, out, &stable_out);
}
}