Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove files including test only from generated filelist #1064

Merged
merged 1 commit into from
Nov 12, 2024
Merged
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: 1 addition & 3 deletions crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ mod filelist {
let _ = analyzer.analyze_pass3(&path.prj, input, &path.src, &parser.veryl);
}

let paths = veryl::cmd_build::CmdBuild::sort_filelist(&metadata, &paths);
let paths = veryl::cmd_build::CmdBuild::sort_filelist(&metadata, &paths, false);
let paths: Vec<_> = paths
.into_iter()
.map(|x| x.src.file_name().unwrap().to_string_lossy().into_owned())
Expand All @@ -329,8 +329,6 @@ mod filelist {
"fifo_controller.veryl",
"fifo.veryl",
"ram.veryl",
// This should be removed by #1064
"test_linear_sec.veryl",
];
check_list(&paths, all);

Expand Down
53 changes: 36 additions & 17 deletions crates/veryl/src/cmd_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::path::{Path, PathBuf};
use tempfile::TempDir;
use veryl_analyzer::namespace::Namespace;
use veryl_analyzer::symbol::SymbolKind;
use veryl_analyzer::{type_dag, Analyzer};
use veryl_analyzer::{symbol_table, type_dag, Analyzer};
use veryl_emitter::Emitter;
use veryl_metadata::{FilelistType, Metadata, SourceMapTarget, Target};
use veryl_parser::{resource_table, veryl_token::TokenSource, Parser};
Expand All @@ -25,7 +25,7 @@ impl CmdBuild {
Self { opt }
}

pub fn exec(&self, metadata: &mut Metadata) -> Result<bool> {
pub fn exec(&self, metadata: &mut Metadata, include_tests: bool) -> Result<bool> {
let paths = metadata.paths(&self.opt.files, true)?;

let mut check_error = CheckError::default();
Expand Down Expand Up @@ -124,7 +124,7 @@ impl CmdBuild {
}
}

self.gen_filelist(metadata, &paths, temp_dir)?;
self.gen_filelist(metadata, &paths, temp_dir, include_tests)?;

let _ = check_error.check_all()?;
Ok(true)
Expand All @@ -148,11 +148,12 @@ impl CmdBuild {
metadata: &Metadata,
paths: &[PathSet],
temp_dir: Option<TempDir>,
include_tests: bool,
) -> Result<()> {
let filelist_path = metadata.filelist_path();
let base_path = metadata.project_path();

let paths = Self::sort_filelist(metadata, paths);
let paths = Self::sort_filelist(metadata, paths, include_tests);

let text = if let Target::Bundle { path } = &metadata.build.target {
let temp_dir = temp_dir.unwrap();
Expand Down Expand Up @@ -200,24 +201,42 @@ impl CmdBuild {
Ok(())
}

pub fn sort_filelist(metadata: &Metadata, paths: &[PathSet]) -> Vec<PathSet> {
pub fn sort_filelist(
metadata: &Metadata,
paths: &[PathSet],
include_tests: bool,
) -> Vec<PathSet> {
let mut table = HashMap::new();
for path in paths {
table.insert(path.src.clone(), path);
}

// Remove files which are not connected from project
let connected_components = type_dag::connected_components();
// Collect files connected from project
let mut prj_namespace = Namespace::new();
prj_namespace.push(resource_table::insert_str(&metadata.project.name));
for symbols in &connected_components {
let used = symbols.iter().any(|x| x.namespace.included(&prj_namespace));
if !used {
for symbol in symbols {
if let TokenSource::File(x) = symbol.token.source {
let path = PathBuf::from(format!("{}", x));
table.remove(&path);
}

let mut candidate_symbols: Vec<_> = type_dag::connected_components()
.into_iter()
.filter(|symbols| {
symbols
.iter()
.any(|symbol| symbol.namespace.included(&prj_namespace))
})
.flatten()
.collect();
if include_tests {
candidate_symbols.extend(symbol_table::get_all().into_iter().filter(|symbol| {
matches!(symbol.kind, SymbolKind::Test(_))
&& symbol.namespace.included(&prj_namespace)
}));
}

let mut used_paths = HashMap::new();
for symbol in &candidate_symbols {
if let TokenSource::File(x) = symbol.token.source {
let path = PathBuf::from(format!("{}", x));
if let Some(x) = table.remove(&path) {
used_paths.insert(path, x);
}
}
}
Expand All @@ -231,14 +250,14 @@ impl CmdBuild {
) {
if let TokenSource::File(x) = symbol.token.source {
let path = PathBuf::from(format!("{}", x));
if let Some(x) = table.remove(&path) {
if let Some(x) = used_paths.remove(&path) {
ret.push(x.clone());
}
}
}
}

for path in table.into_values() {
for path in used_paths.into_values() {
ret.push(path.clone());
}

Expand Down
2 changes: 1 addition & 1 deletion crates/veryl/src/cmd_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl CmdTest {
let build = CmdBuild::new(OptBuild {
files: self.opt.files.clone(),
});
build.exec(metadata)?;
build.exec(metadata, true)?;

let tests: Vec<_> = symbol_table::get_all()
.into_iter()
Expand Down
2 changes: 1 addition & 1 deletion crates/veryl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn main() -> Result<ExitCode> {
Commands::Init(x) => cmd_init::CmdInit::new(x).exec()?,
Commands::Fmt(x) => cmd_fmt::CmdFmt::new(x).exec(&mut metadata)?,
Commands::Check(x) => cmd_check::CmdCheck::new(x).exec(&mut metadata)?,
Commands::Build(x) => cmd_build::CmdBuild::new(x).exec(&mut metadata)?,
Commands::Build(x) => cmd_build::CmdBuild::new(x).exec(&mut metadata, false)?,
Commands::Clean(x) => cmd_clean::CmdClean::new(x).exec(&mut metadata)?,
Commands::Update(x) => cmd_update::CmdUpdate::new(x).exec(&mut metadata)?,
Commands::Publish(x) => cmd_publish::CmdPublish::new(x).exec(&mut metadata)?,
Expand Down