diff --git a/cranelift/src/run.rs b/cranelift/src/run.rs index 45c9d00c22..e54d56ed2b 100644 --- a/cranelift/src/run.rs +++ b/cranelift/src/run.rs @@ -1,13 +1,12 @@ //! CLI tool to compile Cranelift IR files to native code in memory and execute them. -use crate::utils::read_to_string; +use crate::utils::{iterate_files, read_to_string}; use cranelift_codegen::isa::{CallConv, TargetIsa}; use cranelift_filetests::SingleFunctionCompiler; use cranelift_native::builder as host_isa_builder; use cranelift_reader::{parse_run_command, parse_test, Details, IsaSpec, ParseOptions}; use std::path::PathBuf; use target_lexicon::Triple; -use walkdir::WalkDir; pub fn run(files: Vec, flag_print: bool) -> Result<(), String> { let stdin_exist = files.iter().find(|file| *file == "-").is_some(); @@ -54,29 +53,6 @@ pub fn run(files: Vec, flag_print: bool) -> Result<(), String> { } } -/// Iterate over all of the files passed as arguments, recursively iterating through directories -fn iterate_files(files: Vec) -> impl Iterator { - files - .into_iter() - .flat_map(WalkDir::new) - .filter(|f| match f { - Ok(d) => { - // filter out hidden files (starting with .) - !d.file_name().to_str().map_or(false, |s| s.starts_with('.')) - // filter out directories - && !d.file_type().is_dir() - } - Err(e) => { - println!("Unable to read file: {}", e); - false - } - }) - .map(|f| { - f.expect("This should not happen: we have already filtered out the errors") - .into_path() - }) -} - /// Run all functions in a file that are succeeded by "run:" comments fn run_single_file(path: &PathBuf) -> Result<(), String> { let file_contents = read_to_string(&path).map_err(|e| e.to_string())?; diff --git a/cranelift/src/utils.rs b/cranelift/src/utils.rs index 2bae5f9aa6..7771949536 100644 --- a/cranelift/src/utils.rs +++ b/cranelift/src/utils.rs @@ -6,9 +6,10 @@ use cranelift_codegen::settings::{self, FlagsOrIsa}; use cranelift_reader::{parse_options, Location, ParseError, ParseOptionError}; use std::fs::File; use std::io::{self, Read}; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::str::FromStr; use target_lexicon::Triple; +use walkdir::WalkDir; /// Read an entire file into a string. pub fn read_to_string>(path: P) -> io::Result { @@ -105,3 +106,26 @@ pub fn parse_sets_and_triple( Ok(OwnedFlagsOrIsa::Flags(settings::Flags::new(flag_builder))) } } + +/// Iterate over all of the files passed as arguments, recursively iterating through directories. +pub fn iterate_files(files: Vec) -> impl Iterator { + files + .into_iter() + .flat_map(WalkDir::new) + .filter(|f| match f { + Ok(d) => { + // Filter out hidden files (starting with .). + !d.file_name().to_str().map_or(false, |s| s.starts_with('.')) + // Filter out directories. + && !d.file_type().is_dir() + } + Err(e) => { + println!("Unable to read file: {}", e); + false + } + }) + .map(|f| { + f.expect("this should not happen: we have already filtered out the errors") + .into_path() + }) +}