use serde::Deserialize;
use serde::Serialize;
use super::BuildOutput;
use crate::compiler::unit_dependencies::IsArtifact;
use crate::compiler::{CompileKind, CompileMode, CompileTarget, CrateType};
use crate::util::GlobalContext;
use crate::util::data_structures::HashSet;
use crate::util::interning::InternedString;
use crate::workspace::Package;
use crate::workspace::manifest::{Target, TargetKind};
use crate::workspace::profiles::Profile;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::rc::Rc;
#[derive(
Serialize, Deserialize, Debug, Default, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord,
)]
#[serde(transparent)]
pub struct UnitIndex(pub u64);
impl fmt::Display for UnitIndex {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Clone, PartialOrd, Ord)]
pub struct Unit {
inner: Rc<UnitInner>,
}
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct UnitInner {
pub pkg: Package,
pub target: Target,
pub profile: Profile,
pub kind: CompileKind,
pub mode: CompileMode,
pub features: Vec<InternedString>,
pub rustflags: Rc<[String]>,
pub rustdocflags: Rc<[String]>,
pub links_overrides: Rc<BTreeMap<String, BuildOutput>>,
pub artifact: IsArtifact,
pub is_std: bool,
pub dep_hash: u64,
pub artifact_target_for_features: Option<CompileTarget>,
pub skip_non_compile_time_dep: bool,
}
impl UnitInner {
pub fn requires_upstream_objects(&self) -> bool {
self.mode.is_any_test() || self.target.kind().requires_upstream_objects()
}
pub fn benefits_from_no_embed_metadata(&self) -> bool {
matches!(self.mode, CompileMode::Build)
&& self.target.kind().benefits_from_no_embed_metadata()
}
pub fn is_local(&self) -> bool {
self.pkg.package_id().source_id().is_path() && !self.is_std
}
pub fn show_warnings(&self, gctx: &GlobalContext) -> bool {
self.is_local() || gctx.extra_verbose()
}
}
impl Hash for Unit {
fn hash<H: Hasher>(&self, hasher: &mut H) {
std::ptr::hash(&*self.inner, hasher)
}
}
impl PartialEq for Unit {
fn eq(&self, other: &Unit) -> bool {
std::ptr::eq(&*self.inner, &*other.inner)
}
}
impl Eq for Unit {}
impl Deref for Unit {
type Target = UnitInner;
fn deref(&self) -> &UnitInner {
&*self.inner
}
}
impl fmt::Debug for Unit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Unit")
.field("pkg", &self.pkg)
.field("target", &self.target)
.field("profile", &self.profile)
.field("kind", &self.kind)
.field("mode", &self.mode)
.field("features", &self.features)
.field("rustflags", &self.rustflags)
.field("rustdocflags", &self.rustdocflags)
.field("links_overrides", &self.links_overrides)
.field("artifact", &self.artifact.is_true())
.field(
"artifact_target_for_features",
&self.artifact_target_for_features,
)
.field("is_std", &self.is_std)
.field("dep_hash", &self.dep_hash)
.finish()
}
}
pub struct UnitInterner {
state: RefCell<InternerState>,
}
struct InternerState {
cache: HashSet<Rc<UnitInner>>,
}
impl UnitInterner {
pub fn new() -> UnitInterner {
UnitInterner {
state: RefCell::new(InternerState {
cache: HashSet::default(),
}),
}
}
pub fn intern(
&self,
pkg: &Package,
target: &Target,
profile: Profile,
kind: CompileKind,
mode: CompileMode,
features: Vec<InternedString>,
rustflags: Rc<[String]>,
rustdocflags: Rc<[String]>,
links_overrides: Rc<BTreeMap<String, BuildOutput>>,
is_std: bool,
dep_hash: u64,
artifact: IsArtifact,
artifact_target_for_features: Option<CompileTarget>,
skip_non_compile_time_dep: bool,
) -> Unit {
let target = match (is_std, target.kind()) {
(true, TargetKind::Lib(crate_types)) if crate_types.contains(&CrateType::Dylib) => {
let mut new_target = Target::clone(target);
new_target.set_kind(TargetKind::Lib(vec![CrateType::Rlib]));
new_target
}
_ => target.clone(),
};
let inner = self.intern_inner(&UnitInner {
pkg: pkg.clone(),
target,
profile,
kind,
mode,
features,
rustflags,
rustdocflags,
links_overrides,
is_std,
dep_hash,
artifact,
artifact_target_for_features,
skip_non_compile_time_dep,
});
Unit { inner }
}
fn intern_inner(&self, item: &UnitInner) -> Rc<UnitInner> {
let mut me = self.state.borrow_mut();
if let Some(item) = me.cache.get(item) {
return item.clone();
}
let item = Rc::new(item.clone());
me.cache.insert(item.clone());
item
}
}