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
9 changes: 7 additions & 2 deletions src/cmd/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tokio::runtime::Runtime;
use crate::config::{Dependency, Validate, ValidationContext};
use crate::error::*;
use crate::sess::{Session, SessionIo};
use crate::src::FilteredSourceGroup;
use crate::target::{TargetSet, TargetSpec};

/// Emit the source file manifest for the package
Expand Down Expand Up @@ -124,10 +125,14 @@ pub fn run(sess: &Session, args: &SourcesArgs) -> Result<()> {
let stdout = std::io::stdout();
let handle = stdout.lock();
if args.flatten {
let srcs = srcs.flatten();
let srcs = srcs
.flatten()
.into_iter()
.map(FilteredSourceGroup::from)
.collect::<Vec<_>>();
serde_json::to_writer_pretty(handle, &srcs)
} else {
serde_json::to_writer_pretty(handle, &srcs)
serde_json::to_writer_pretty(handle, &FilteredSourceGroup::from(srcs))
}
};
let _ = writeln!(std::io::stdout(),);
Expand Down
4 changes: 4 additions & 0 deletions src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,10 @@ pub enum Warnings {
#[error("Override files in {} does not support additional fields like include_dirs, defines, etc.", fmt_pkg!(.0))]
#[diagnostic(code(W33))]
OverrideFilesWithExtras(String),

#[error("Source group in package {} uses `override_files`, which is not supported in the simplified source output and will be ignored.", fmt_pkg!(.0))]
#[diagnostic(code(W34))]
OverrideFilesIgnored(String),
}

#[cfg(test)]
Expand Down
103 changes: 103 additions & 0 deletions src/src.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,109 @@ impl<'ctx> SourceGroup<'ctx> {
}
}

/// A source file group.
#[derive(Serialize, Clone, Debug, Default)]
pub struct FilteredSourceGroup<'ctx> {
Copy link
Contributor

Choose a reason for hiding this comment

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

A bit unfortunate that this requires another *SourceGroup type, but I also don't have a better solution.

/// The package which this source group represents.
pub package: Option<&'ctx str>,
/// Whether the source files in this group can be treated in parallel.
pub independent: bool,
/// The directories to search for include files.
pub include_dirs: Vec<&'ctx Path>,
/// The preprocessor definitions.
pub defines: IndexMap<String, Option<&'ctx str>>,
/// The files in this group.
pub files: Vec<FilteredSourceFile<'ctx>>,
/// Package dependencies of this source group
pub dependencies: IndexSet<String>,
/// Version information of the package
pub version: Option<semver::Version>,
}

impl<'ctx> From<SourceGroup<'ctx>> for FilteredSourceGroup<'ctx> {
fn from(group: SourceGroup<'ctx>) -> Self {
if group.override_files {
if let Some(pkg) = group.package {
Warnings::OverrideFilesIgnored(pkg.to_string()).emit();
}
}
let include_dirs = group
.include_dirs
.into_iter()
.map(|(_, path)| path)
.chain(
group
.export_incdirs
.into_iter()
.flat_map(|(_, dirs)| dirs.into_iter().map(|(_, path)| path)),
)
.collect();
FilteredSourceGroup {
package: group.package,
independent: group.independent,
include_dirs,
defines: group.defines,
files: group
.files
.into_iter()
.map(FilteredSourceFile::from)
.collect(),
dependencies: group.dependencies,
version: group.version,
}
}
}

/// A filtered source file.
///
/// This can either be an individual file, or a filtered subgroup of files.
#[derive(Clone)]
pub enum FilteredSourceFile<'ctx> {
/// A file.
File(&'ctx Path, &'ctx Option<SourceType>),
/// A group of files.
Group(Box<FilteredSourceGroup<'ctx>>),
}

impl<'ctx> From<SourceFile<'ctx>> for FilteredSourceFile<'ctx> {
fn from(file: SourceFile<'ctx>) -> Self {
match file {
SourceFile::File(path, ty) => FilteredSourceFile::File(path, ty),
SourceFile::Group(grp) => {
FilteredSourceFile::Group(Box::new(FilteredSourceGroup::from(*grp)))
}
}
}
}

impl<'ctx> fmt::Debug for FilteredSourceFile<'ctx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
FilteredSourceFile::File(path, ty) => {
fmt::Debug::fmt(path, f)?;
if let Some(t) = ty {
write!(f, " as {:?}", t)
} else {
write!(f, " as <unknown>")
}
}
FilteredSourceFile::Group(ref srcs) => fmt::Debug::fmt(srcs, f),
}
}
}

impl<'ctx> Serialize for FilteredSourceFile<'ctx> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match *self {
FilteredSourceFile::File(path, _) => path.serialize(serializer),
FilteredSourceFile::Group(ref group) => group.serialize(serializer),
}
}
}

/// File types for a source file.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum SourceType {
Expand Down