Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cmd/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ fn emit_template(
all_defines.extend(
src.defines
.iter()
.map(|(k, &v)| (k.to_string(), v.map(String::from))),
.map(|(k, (_, v))| (k.to_string(), v.map(String::from))),
);
all_incdirs.extend(src.clone().get_incdirs());

Expand Down Expand Up @@ -569,7 +569,7 @@ fn emit_template(
local_defines.extend(
src.defines
.iter()
.map(|(k, &v)| (k.to_string(), v.map(String::from))),
.map(|(k, (_, v))| (k.to_string(), v.map(String::from))),
);

add_defines(&mut local_defines, &args.define);
Expand Down
64 changes: 59 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ pub struct Sources {
pub target: TargetSpec,
/// The directories to search for include files, with their target specs.
pub include_dirs: Vec<(TargetSpec, PathBuf)>,
/// The preprocessor definitions.
pub defines: IndexMap<String, Option<String>>,
/// The preprocessor definitions, with their target specs.
pub defines: IndexMap<String, (TargetSpec, Option<String>)>,
/// The source files.
pub files: Vec<SourceFile>,
/// The files in this source will override other files.
Expand Down Expand Up @@ -918,6 +918,46 @@ impl Validate for PartialIncludeDir {
}
}

/// A partial filtered preprocessor define
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct PartialDefine {
/// The target spec for which the define applies
pub target: Option<TargetSpec>,
/// The define value (None for valueless defines)
pub value: Option<String>,
/// Unknown extra fields
#[serde(flatten)]
pub extra: HashMap<String, Value>,
}

impl FromStr for PartialDefine {
type Err = Void;
fn from_str(s: &str) -> std::result::Result<Self, Void> {
Ok(PartialDefine {
target: None,
value: Some(s.into()),
extra: HashMap::new(),
})
}
}

impl Validate for PartialDefine {
type Output = (TargetSpec, Option<String>);
type Error = Error;
fn validate(self, vctx: &ValidationContext) -> Result<(TargetSpec, Option<String>)> {
if !vctx.pre_output {
self.extra.iter().for_each(|(k, _)| {
Warnings::IgnoreUnknownField {
field: k.clone(),
pkg: vctx.package_name.to_string(),
}
.emit();
});
}
Ok((self.target.unwrap_or(TargetSpec::Wildcard), self.value))
}
}

/// A partial group of source files.
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct PartialSources {
Expand All @@ -926,7 +966,7 @@ pub struct PartialSources {
/// The directories to search for include files.
pub include_dirs: Option<Vec<StringOrStruct<PartialIncludeDir>>>,
/// The preprocessor definitions.
pub defines: Option<IndexMap<String, Option<String>>>,
pub defines: Option<IndexMap<String, Option<StringOrStruct<PartialDefine>>>>,
/// The source file paths.
pub files: Option<Vec<PartialSourceFile>>,
/// A sv file.
Expand Down Expand Up @@ -1139,7 +1179,10 @@ impl Validate for PartialSources {
if let Some(eq_idx) = file.find("=") {
(
file[..eq_idx].to_string(),
Some(file[eq_idx + 1..].to_string()),
Some(StringOrStruct(PartialDefine {
value: Some(file[eq_idx + 1..].to_string()),
..Default::default()
})),
)
} else {
(file.to_string(), None)
Expand Down Expand Up @@ -1251,7 +1294,18 @@ impl Validate for PartialSources {
})
.collect::<Result<Vec<_>>>()?;

let defines = defines.unwrap_or_default();
let defines: IndexMap<String, (TargetSpec, Option<String>)> = defines
.unwrap_or_default()
.into_iter()
.map(|(name, partial)| {
let (trgt, value) = if let Some(partial) = partial {
partial.validate(vctx)?
} else {
(TargetSpec::Wildcard, None)
};
Ok((name, (trgt, value)))
})
.collect::<Result<_>>()?;
let files: Result<Vec<_>> = post_glob_files
.into_iter()
.map(|f| f.validate(vctx))
Expand Down
7 changes: 6 additions & 1 deletion src/sess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,12 @@ impl<'ctx> Session<'ctx> {
let defines = sources
.defines
.iter()
.map(|(k, v)| (k.clone(), v.as_ref().map(|v| self.intern_string(v))))
.map(|(k, (trgt, v))| {
(
k.clone(),
(trgt.clone(), v.as_ref().map(|v| self.intern_string(v))),
)
})
.collect();
let files = sources
.files
Expand Down
19 changes: 14 additions & 5 deletions src/src.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub struct SourceGroup<'ctx> {
pub include_dirs: Vec<(TargetSpec, &'ctx Path)>,
/// The directories exported by dependent package for include files, with their target specs.
pub export_incdirs: IndexMap<String, Vec<(TargetSpec, &'ctx Path)>>,
/// The preprocessor definitions.
pub defines: IndexMap<String, Option<&'ctx str>>,
/// The preprocessor definitions, with their target specs.
pub defines: IndexMap<String, (TargetSpec, Option<&'ctx str>)>,
/// The files in this group.
pub files: Vec<SourceFile<'ctx>>,
/// Package dependencies of this source group
Expand Down Expand Up @@ -148,14 +148,23 @@ impl<'ctx> SourceGroup<'ctx> {
.cloned()
.collect();

let mut defines: IndexMap<String, (TargetSpec, Option<&str>)> = self
.defines
.iter()
.filter(|(_, (trgt, _))| trgt.matches(&all_targets))
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
let mut target_defs = all_targets
.into_iter()
.map(|t| "TARGET_".to_owned() + &t.to_uppercase())
.collect::<IndexSet<_>>();
target_defs.sort();
let mut defines: IndexMap<String, Option<&str>> = self.defines.clone();
if self.package.is_some() {
defines.extend(target_defs.into_iter().map(|t| (t, None)));
defines.extend(
target_defs
.into_iter()
.map(|t| (t, (TargetSpec::Wildcard, None))),
);
}

let export_incdirs = self
Expand Down Expand Up @@ -356,7 +365,7 @@ impl<'ctx> SourceGroup<'ctx> {
grp.defines = self
.defines
.iter()
.map(|(k, v)| (k.clone(), *v))
.map(|(k, v)| (k.clone(), v.clone()))
.chain(grp.defines.into_iter())
.collect();
grp.flatten_into(into);
Expand Down
28 changes: 28 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,34 @@ where
Ok(T::from_str(value).unwrap())
}

fn visit_bool<E>(self, value: bool) -> std::result::Result<T, E>
where
E: de::Error,
{
self.visit_str(if value { "true" } else { "false" })
}

fn visit_i64<E>(self, value: i64) -> std::result::Result<T, E>
where
E: de::Error,
{
self.visit_str(&value.to_string())
}

fn visit_u64<E>(self, value: u64) -> std::result::Result<T, E>
where
E: de::Error,
{
self.visit_str(&value.to_string())
}

fn visit_f64<E>(self, value: f64) -> std::result::Result<T, E>
where
E: de::Error,
{
self.visit_str(&value.to_string())
}

Comment on lines +110 to +137
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to write this as a generic function? E.g. something like:

            fn visit_bool<E>(self, value: T) -> std::result::Result<T, E>
            where
                T: ToString
                E: de::Error,
            {
                self.visit_str(&value.to_string())
            }       

fn visit_map<M>(self, visitor: M) -> std::result::Result<T, M::Error>
where
M: de::MapAccess<'de>,
Expand Down