From 63b7024cb98717dd785ae888f419002b9835c6b1 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Fri, 14 Apr 2023 23:59:23 +0200 Subject: xtask: build usage examples and general improvements --- xtask/src/argument_parsing.rs | 53 ++++++++++++++++++++++++- xtask/src/cargo_commands.rs | 27 +++++++++++++ xtask/src/command.rs | 92 +++++++++++++++++++++++++++++++++++++------ xtask/src/main.rs | 23 ++++++++--- 4 files changed, 177 insertions(+), 18 deletions(-) (limited to 'xtask') diff --git a/xtask/src/argument_parsing.rs b/xtask/src/argument_parsing.rs index 3ee9e34..d74ba69 100644 --- a/xtask/src/argument_parsing.rs +++ b/xtask/src/argument_parsing.rs @@ -190,8 +190,8 @@ pub enum BuildOrCheck { #[derive(Parser, Clone)] pub struct Globals { - /// For which backend to build (defaults to thumbv7) - #[arg(value_enum, short, long, global = true)] + /// For which backend to build. + #[arg(value_enum, short, default_value = "thumbv7", long, global = true)] pub backend: Option, /// List of comma separated examples to include, all others are excluded @@ -300,6 +300,55 @@ pub enum Commands { /// Build books with mdbook Book(Arg), + + /// Check one or more usage examples. + /// + /// Usage examples are located in ./examples + UsageExamplesCheck(UsageExamples), + + /// Build one or more usage examples. + /// + /// Usage examples are located in ./examples + #[clap(alias = "./examples")] + UsageExampleBuild(UsageExamples), +} + +#[derive(Args, Clone, Debug)] +pub struct UsageExamples { + /// The usage examples to build. All usage examples are selected if this argument is not provided. + /// + /// Example: `rp2040_local_i2c_init,stm32f3_blinky`. + examples: Option, +} + +impl UsageExamples { + pub fn examples(&self) -> anyhow::Result> { + let usage_examples: Vec<_> = std::fs::read_dir("./examples")? + .filter_map(Result::ok) + .filter(|p| p.metadata().ok().map(|p| p.is_dir()).unwrap_or(false)) + .filter_map(|p| p.file_name().as_os_str().to_str().map(ToString::to_string)) + .collect(); + + let selected_examples: Option> = self + .examples + .clone() + .map(|s| s.split(",").map(ToString::to_string).collect()); + + if let Some(selected_examples) = selected_examples { + if let Some(unfound_example) = selected_examples + .iter() + .find(|e| !usage_examples.contains(e)) + { + Err(anyhow::anyhow!( + "Usage example {unfound_example} does not exist" + )) + } else { + Ok(selected_examples) + } + } else { + Ok(usage_examples) + } + } } #[derive(Args, Debug, Clone)] diff --git a/xtask/src/cargo_commands.rs b/xtask/src/cargo_commands.rs index 9cbdaef..9b07088 100644 --- a/xtask/src/cargo_commands.rs +++ b/xtask/src/cargo_commands.rs @@ -126,6 +126,33 @@ pub fn cargo<'c>( runner.run_and_coalesce() } +/// Cargo command to build a usage example. +/// +/// The usage examples are in examples/ +pub fn cargo_usage_example( + globals: &Globals, + operation: BuildOrCheck, + usage_examples: Vec, +) -> Vec> { + examples_iter(&usage_examples) + .map(|example| { + let path = format!("examples/{example}"); + + let command = match operation { + BuildOrCheck::Check => CargoCommand::CheckInDir { + mode: BuildMode::Release, + dir: path.into(), + }, + BuildOrCheck::Build => CargoCommand::BuildInDir { + mode: BuildMode::Release, + dir: path.into(), + }, + }; + (globals, command, false) + }) + .run_and_coalesce() +} + /// Cargo command to either build or check all examples /// /// The examples are in rtic/examples diff --git a/xtask/src/command.rs b/xtask/src/command.rs index b62724a..93de9cf 100644 --- a/xtask/src/command.rs +++ b/xtask/src/command.rs @@ -8,6 +8,7 @@ use core::fmt; use std::{ fs::File, io::Read, + path::PathBuf, process::{Command, Stdio}, }; @@ -111,6 +112,14 @@ pub enum CargoCommand<'a> { mode: BuildMode, arguments: Option, }, + CheckInDir { + mode: BuildMode, + dir: PathBuf, + }, + BuildInDir { + mode: BuildMode, + dir: PathBuf, + }, } impl core::fmt::Display for CargoCommand<'_> { @@ -211,6 +220,10 @@ impl core::fmt::Display for CargoCommand<'_> { details(target, mode, features, cargoarg) ) } + CargoCommand::BuildInDir { mode, dir } => { + let dir = dir.as_os_str().to_str().unwrap_or("Not displayable"); + write!(f, "Build {dir} ({mode})") + } CargoCommand::Check { cargoarg, package, @@ -225,6 +238,10 @@ impl core::fmt::Display for CargoCommand<'_> { details(target, mode, features, cargoarg) ) } + CargoCommand::CheckInDir { mode, dir } => { + let dir = dir.as_os_str().to_str().unwrap_or("Not displayable"); + write!(f, "Check {dir} ({mode})") + } CargoCommand::Clippy { cargoarg, package, @@ -316,11 +333,15 @@ impl<'a> CargoCommand<'a> { format!("{executable} {args}") } - fn command(&self) -> &str { + fn command(&self) -> &'static str { match self { CargoCommand::Run { .. } | CargoCommand::Qemu { .. } => "run", - CargoCommand::ExampleCheck { .. } | CargoCommand::Check { .. } => "check", - CargoCommand::ExampleBuild { .. } | CargoCommand::Build { .. } => "build", + CargoCommand::ExampleCheck { .. } + | CargoCommand::Check { .. } + | CargoCommand::CheckInDir { .. } => "check", + CargoCommand::ExampleBuild { .. } + | CargoCommand::Build { .. } + | CargoCommand::BuildInDir { .. } => "build", CargoCommand::ExampleSize { .. } => "size", CargoCommand::Clippy { .. } => "clippy", CargoCommand::Format { .. } => "fmt", @@ -329,7 +350,7 @@ impl<'a> CargoCommand<'a> { CargoCommand::Test { .. } => "test", } } - pub fn executable(&self) -> &str { + pub fn executable(&self) -> &'static str { match self { CargoCommand::Run { .. } | CargoCommand::Qemu { .. } @@ -341,7 +362,9 @@ impl<'a> CargoCommand<'a> { | CargoCommand::Clippy { .. } | CargoCommand::Format { .. } | CargoCommand::Test { .. } - | CargoCommand::Doc { .. } => "cargo", + | CargoCommand::Doc { .. } + | CargoCommand::CheckInDir { .. } + | CargoCommand::BuildInDir { .. } => "cargo", CargoCommand::Book { .. } => "mdbook", } } @@ -641,6 +664,34 @@ impl<'a> CargoCommand<'a> { } args } + CargoCommand::CheckInDir { mode, dir: _ } => { + let mut args = vec!["+nightly"]; + args.push(self.command()); + + if let Some(mode) = mode.to_flag() { + args.push(mode); + } + + args + } + CargoCommand::BuildInDir { mode, dir: _ } => { + let mut args = vec!["+nightly", self.command()]; + + if let Some(mode) = mode.to_flag() { + args.push(mode); + } + + args + } + } + } + + fn chdir(&self) -> Option<&PathBuf> { + match self { + CargoCommand::CheckInDir { dir, .. } | CargoCommand::BuildInDir { dir, .. } => { + Some(dir) + } + _ => None, } } } @@ -669,11 +720,18 @@ impl fmt::Display for BuildMode { pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::Result { log::info!("👟 {command}"); - let result = Command::new(command.executable()) + let mut process = Command::new(command.executable()); + + process .args(command.args()) .stdout(Stdio::piped()) - .stderr(stderr_mode) - .output()?; + .stderr(stderr_mode); + + if let Some(dir) = command.chdir() { + process.current_dir(dir); + } + + let result = process.output()?; let exit_status = result.status; let stderr = String::from_utf8(result.stderr).unwrap_or("Not displayable".into()); @@ -759,15 +817,27 @@ pub fn handle_results(globals: &Globals, results: Vec) -> anyhow errors.clone().for_each(log_stdout_stderr(Level::Error)); successes.for_each(|(cmd, _)| { + let path = if let Some(dir) = cmd.chdir() { + let path = dir.as_os_str().to_str().unwrap_or("Not displayable"); + format!(" (in {path}") + } else { + format!("") + }; + if globals.verbose > 0 { - info!("✅ Success: {cmd}\n {}", cmd.as_cmd_string()); + info!("✅ Success:{path} {cmd}\n {}", cmd.as_cmd_string()); } else { - info!("✅ Success: {cmd}"); + info!("✅ Success:{path} {cmd}"); } }); errors.clone().for_each(|(cmd, _)| { - error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string()); + if let Some(dir) = cmd.chdir() { + let path = dir.as_os_str().to_str().unwrap_or("Not displayable"); + error!("❌ Failed: (in {path}) {cmd}\n {}", cmd.as_cmd_string()); + } else { + error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string()); + } }); let ecount = errors.count(); diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 2bfe851..2b45f23 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -23,10 +23,7 @@ use log::{error, info, log_enabled, trace, Level}; use crate::{ argument_parsing::{Backends, BuildOrCheck, Cli, Commands}, build::init_build_dir, - cargo_commands::{ - build_and_check_size, cargo, cargo_book, cargo_clippy, cargo_doc, cargo_example, - cargo_format, cargo_test, run_test, - }, + cargo_commands::*, command::{handle_results, run_command, run_successful, CargoCommand}, }; @@ -152,6 +149,12 @@ fn main() -> anyhow::Result<()> { trace!("default logging level: {0}", globals.verbose); + log::debug!( + "Stderr of child processes is inherited: {}", + globals.stderr_inherited + ); + log::debug!("Partial features: {}", globals.partial); + let backend = if let Some(backend) = globals.backend { backend } else { @@ -285,6 +288,14 @@ fn main() -> anyhow::Result<()> { info!("Running mdbook"); cargo_book(globals, &args.arguments) } + Commands::UsageExamplesCheck(examples) => { + info!("Checking usage examples"); + cargo_usage_example(globals, BuildOrCheck::Check, examples.examples()?) + } + Commands::UsageExampleBuild(examples) => { + info!("Building usage examples"); + cargo_usage_example(globals, BuildOrCheck::Build, examples.examples()?) + } }; handle_results(globals, final_run_results) @@ -347,7 +358,9 @@ fn command_parser( | CargoCommand::Doc { .. } | CargoCommand::Test { .. } | CargoCommand::Book { .. } - | CargoCommand::ExampleSize { .. } => { + | CargoCommand::ExampleSize { .. } + | CargoCommand::BuildInDir { .. } + | CargoCommand::CheckInDir { .. } => { let cargo_result = run_command(command, output_mode)?; Ok(cargo_result) } -- cgit v1.2.3 From fa8af4cbcffbedec1504ed464bd02b6ee6e3fb4b Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 15 Apr 2023 20:19:37 +0200 Subject: Add the most important message --- xtask/src/command.rs | 6 ++++-- xtask/src/main.rs | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'xtask') diff --git a/xtask/src/command.rs b/xtask/src/command.rs index 93de9cf..186836b 100644 --- a/xtask/src/command.rs +++ b/xtask/src/command.rs @@ -771,7 +771,7 @@ pub fn run_successful(run: &RunResult, expected_output_file: &str) -> Result<(), } } -pub fn handle_results(globals: &Globals, results: Vec) -> anyhow::Result<()> { +pub fn handle_results(globals: &Globals, results: Vec) -> Result<(), ()> { let errors = results.iter().filter_map(|r| { if let FinalRunResult::Failed(c, r) = r { Some((c, r)) @@ -842,8 +842,10 @@ pub fn handle_results(globals: &Globals, results: Vec) -> anyhow let ecount = errors.count(); if ecount != 0 { - Err(anyhow::anyhow!("{ecount} commands failed.")) + log::error!("{ecount} commands failed."); + Err(()) } else { + info!("🚀🚀🚀 All tasks succeeded 🚀🚀🚀"); Ok(()) } } diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 2b45f23..1cc0185 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -298,7 +298,7 @@ fn main() -> anyhow::Result<()> { } }; - handle_results(globals, final_run_results) + handle_results(globals, final_run_results).map_err(|_| anyhow::anyhow!("Commands failed")) } // run example binary `example` -- cgit v1.2.3 From 6517a4bec2e909b40eb9974e063f95e44e4d9a31 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 15 Apr 2023 20:44:06 +0200 Subject: Also check for CommandErrors in error checking --- xtask/src/cargo_commands.rs | 14 ++++++++++---- xtask/src/command.rs | 27 +++++++++++++++++++-------- xtask/src/main.rs | 2 -- 3 files changed, 29 insertions(+), 14 deletions(-) (limited to 'xtask') diff --git a/xtask/src/cargo_commands.rs b/xtask/src/cargo_commands.rs index 9b07088..ead6eaa 100644 --- a/xtask/src/cargo_commands.rs +++ b/xtask/src/cargo_commands.rs @@ -10,10 +10,11 @@ use rayon::prelude::*; use iters::*; +#[derive(Debug)] pub enum FinalRunResult<'c> { Success(CargoCommand<'c>, RunResult), Failed(CargoCommand<'c>, RunResult), - CommandError(anyhow::Error), + CommandError(CargoCommand<'c>, anyhow::Error), } fn run_and_convert<'a>( @@ -21,7 +22,8 @@ fn run_and_convert<'a>( ) -> FinalRunResult<'a> { // Run the command let result = command_parser(global, &command, overwrite); - match result { + + let output = match result { // If running the command succeeded without looking at any of the results, // log the data and see if the actual execution was succesfull too. Ok(result) => { @@ -32,8 +34,12 @@ fn run_and_convert<'a>( } } // If it didn't and some IO error occured, just panic - Err(e) => FinalRunResult::CommandError(e), - } + Err(e) => FinalRunResult::CommandError(command, e), + }; + + log::trace!("Final result: {output:?}"); + + output } pub trait CoalescingRunner<'c> { diff --git a/xtask/src/command.rs b/xtask/src/command.rs index 186836b..93ea824 100644 --- a/xtask/src/command.rs +++ b/xtask/src/command.rs @@ -774,7 +774,7 @@ pub fn run_successful(run: &RunResult, expected_output_file: &str) -> Result<(), pub fn handle_results(globals: &Globals, results: Vec) -> Result<(), ()> { let errors = results.iter().filter_map(|r| { if let FinalRunResult::Failed(c, r) = r { - Some((c, r)) + Some((c, &r.stdout, &r.stderr)) } else { None } @@ -782,16 +782,22 @@ pub fn handle_results(globals: &Globals, results: Vec) -> Result let successes = results.iter().filter_map(|r| { if let FinalRunResult::Success(c, r) = r { - Some((c, r)) + Some((c, &r.stdout, &r.stderr)) + } else { + None + } + }); + + let command_errors = results.iter().filter_map(|r| { + if let FinalRunResult::CommandError(c, e) = r { + Some((c, e)) } else { None } }); let log_stdout_stderr = |level: Level| { - move |(command, result): (&CargoCommand, &RunResult)| { - let stdout = &result.stdout; - let stderr = &result.stderr; + move |(command, stdout, stderr): (&CargoCommand, &String, &String)| { if !stdout.is_empty() && !stderr.is_empty() { log::log!( level, @@ -816,7 +822,7 @@ pub fn handle_results(globals: &Globals, results: Vec) -> Result successes.clone().for_each(log_stdout_stderr(Level::Debug)); errors.clone().for_each(log_stdout_stderr(Level::Error)); - successes.for_each(|(cmd, _)| { + successes.for_each(|(cmd, _, _)| { let path = if let Some(dir) = cmd.chdir() { let path = dir.as_os_str().to_str().unwrap_or("Not displayable"); format!(" (in {path}") @@ -831,7 +837,7 @@ pub fn handle_results(globals: &Globals, results: Vec) -> Result } }); - errors.clone().for_each(|(cmd, _)| { + errors.clone().for_each(|(cmd, _, _)| { if let Some(dir) = cmd.chdir() { let path = dir.as_os_str().to_str().unwrap_or("Not displayable"); error!("❌ Failed: (in {path}) {cmd}\n {}", cmd.as_cmd_string()); @@ -840,8 +846,13 @@ pub fn handle_results(globals: &Globals, results: Vec) -> Result } }); + command_errors + .clone() + .for_each(|(cmd, error)| error!("❌ Failed: {cmd}\n {}\n{error}", cmd.as_cmd_string())); + let ecount = errors.count(); - if ecount != 0 { + let cecount = command_errors.count(); + if ecount != 0 || cecount != 0 { log::error!("{ecount} commands failed."); Err(()) } else { diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 1cc0185..4cf6db8 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -324,9 +324,7 @@ fn command_parser( .map_err(TestRunError::PathConversionError)?; // cargo run <..> - info!("Running example: {example}"); let cargo_run_result = run_command(command, output_mode)?; - info!("{}", cargo_run_result.stdout); // Create a file for the expected output if it does not exist or mismatches if overwrite { -- cgit v1.2.3 From 859cd418f063590a9928b3e43caeea0b53dc0823 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 15 Apr 2023 20:57:27 +0200 Subject: Rename some things --- xtask/src/argument_parsing.rs | 8 ++++---- xtask/src/command.rs | 6 +++--- xtask/src/main.rs | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) (limited to 'xtask') diff --git a/xtask/src/argument_parsing.rs b/xtask/src/argument_parsing.rs index d74ba69..6cae186 100644 --- a/xtask/src/argument_parsing.rs +++ b/xtask/src/argument_parsing.rs @@ -304,24 +304,24 @@ pub enum Commands { /// Check one or more usage examples. /// /// Usage examples are located in ./examples - UsageExamplesCheck(UsageExamples), + UsageExampleCheck(UsageExamplesOpt), /// Build one or more usage examples. /// /// Usage examples are located in ./examples #[clap(alias = "./examples")] - UsageExampleBuild(UsageExamples), + UsageExampleBuild(UsageExamplesOpt), } #[derive(Args, Clone, Debug)] -pub struct UsageExamples { +pub struct UsageExamplesOpt { /// The usage examples to build. All usage examples are selected if this argument is not provided. /// /// Example: `rp2040_local_i2c_init,stm32f3_blinky`. examples: Option, } -impl UsageExamples { +impl UsageExamplesOpt { pub fn examples(&self) -> anyhow::Result> { let usage_examples: Vec<_> = std::fs::read_dir("./examples")? .filter_map(Result::ok) diff --git a/xtask/src/command.rs b/xtask/src/command.rs index 93ea824..a1c4d25 100644 --- a/xtask/src/command.rs +++ b/xtask/src/command.rs @@ -831,16 +831,16 @@ pub fn handle_results(globals: &Globals, results: Vec) -> Result }; if globals.verbose > 0 { - info!("✅ Success:{path} {cmd}\n {}", cmd.as_cmd_string()); + info!("✅ Success: {cmd}{path}\n {}", cmd.as_cmd_string()); } else { - info!("✅ Success:{path} {cmd}"); + info!("✅ Success:{cmd}{path}"); } }); errors.clone().for_each(|(cmd, _, _)| { if let Some(dir) = cmd.chdir() { let path = dir.as_os_str().to_str().unwrap_or("Not displayable"); - error!("❌ Failed: (in {path}) {cmd}\n {}", cmd.as_cmd_string()); + error!("❌ Failed: {cmd} (in {path}) \n {}", cmd.as_cmd_string()); } else { error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string()); } diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 4cf6db8..10499c0 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -288,7 +288,7 @@ fn main() -> anyhow::Result<()> { info!("Running mdbook"); cargo_book(globals, &args.arguments) } - Commands::UsageExamplesCheck(examples) => { + Commands::UsageExampleCheck(examples) => { info!("Checking usage examples"); cargo_usage_example(globals, BuildOrCheck::Check, examples.examples()?) } -- cgit v1.2.3 From d838286de679a1ac35ea79999816418cd02b7259 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 15 Apr 2023 21:16:45 +0200 Subject: Fix config pickup behaviour so that both examples and usage-examples build correctly --- xtask/src/command.rs | 28 +++++++++++++++++++++++++--- xtask/src/main.rs | 4 ++-- 2 files changed, 27 insertions(+), 5 deletions(-) (limited to 'xtask') diff --git a/xtask/src/command.rs b/xtask/src/command.rs index a1c4d25..a45cb8a 100644 --- a/xtask/src/command.rs +++ b/xtask/src/command.rs @@ -383,6 +383,7 @@ impl<'a> CargoCommand<'a> { if let Some(cargoarg) = cargoarg { args.extend_from_slice(&[cargoarg]); } + args.extend_from_slice(&[ self.command(), "--example", @@ -407,9 +408,15 @@ impl<'a> CargoCommand<'a> { mode, } => { let mut args = vec!["+nightly"]; + if let Some(cargoarg) = cargoarg { args.extend_from_slice(&[cargoarg]); } + + // We need to be in the `rtic` directory to pick up + // the correct .cargo/config.toml file + args.extend_from_slice(&["-Z", "unstable-options", "-C", "rtic"]); + args.extend_from_slice(&[ self.command(), "--example", @@ -641,6 +648,11 @@ impl<'a> CargoCommand<'a> { if let Some(cargoarg) = cargoarg { args.extend_from_slice(&[cargoarg]); } + + // We need to be in the `rtic` directory to pick up + // the correct .cargo/config.toml file + args.extend_from_slice(&["-Z", "unstable-options", "-C", "rtic"]); + args.extend_from_slice(&[ self.command(), "--example", @@ -694,6 +706,13 @@ impl<'a> CargoCommand<'a> { _ => None, } } + + pub fn print_stdout_intermediate(&self) -> bool { + match self { + Self::ExampleSize { .. } => true, + _ => false, + } + } } impl BuildMode { @@ -737,6 +756,10 @@ pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::R let stderr = String::from_utf8(result.stderr).unwrap_or("Not displayable".into()); let stdout = String::from_utf8(result.stdout).unwrap_or("Not displayable".into()); + if command.print_stdout_intermediate() && exit_status.success() { + log::info!("\n{}", stdout); + } + Ok(RunResult { exit_status, stdout, @@ -850,9 +873,8 @@ pub fn handle_results(globals: &Globals, results: Vec) -> Result .clone() .for_each(|(cmd, error)| error!("❌ Failed: {cmd}\n {}\n{error}", cmd.as_cmd_string())); - let ecount = errors.count(); - let cecount = command_errors.count(); - if ecount != 0 || cecount != 0 { + let ecount = errors.count() + command_errors.count(); + if ecount != 0 { log::error!("{ecount} commands failed."); Err(()) } else { diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 10499c0..7077d55 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -100,8 +100,8 @@ impl fmt::Display for TestRunError { TestRunError::CommandError(e) => { write!( f, - "Command failed with exit status {}: {}", - e.exit_status, e.stdout + "Command failed with exit status {}: {} {}", + e.exit_status, e.stdout, e.stderr ) } TestRunError::PathConversionError(p) => { -- cgit v1.2.3 From 1c84ccf6e4169b4b45f0e22e709e4265a10324a5 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 15 Apr 2023 22:19:13 +0200 Subject: Fix running of tests --- xtask/src/cargo_commands.rs | 47 ++++++++++++++++++++++----------------------- xtask/src/command.rs | 7 ++++++- xtask/src/main.rs | 2 +- 3 files changed, 30 insertions(+), 26 deletions(-) (limited to 'xtask') diff --git a/xtask/src/cargo_commands.rs b/xtask/src/cargo_commands.rs index ead6eaa..42895d8 100644 --- a/xtask/src/cargo_commands.rs +++ b/xtask/src/cargo_commands.rs @@ -302,7 +302,7 @@ pub fn cargo_book<'c>( /// Run examples /// /// Supports updating the expected output via the overwrite argument -pub fn run_test<'c>( +pub fn qemu_run_examples<'c>( globals: &Globals, cargoarg: &'c Option<&'c str>, backend: Backends, @@ -312,31 +312,30 @@ pub fn run_test<'c>( let target = backend.to_target(); let features = Some(target.and_features(backend.to_rtic_feature())); - examples_iter(examples) - .map(|example| { - let cmd = CargoCommand::ExampleBuild { - cargoarg: &Some("--quiet"), - example, - target, - features: features.clone(), - mode: BuildMode::Release, - }; - - if let Err(err) = command_parser(globals, &cmd, false) { - error!("{err}"); - } + let build = examples_iter(examples).map(|example| { + let cmd_build = CargoCommand::ExampleBuild { + // We need to be in the correct + cargoarg: &None, + example, + target, + features: features.clone(), + mode: BuildMode::Release, + }; + (globals, cmd_build, overwrite) + }); - let cmd = CargoCommand::Qemu { - cargoarg, - example, - target, - features: features.clone(), - mode: BuildMode::Release, - }; + let run = examples_iter(examples).map(|example| { + let cmd_qemu = CargoCommand::Qemu { + cargoarg, + example, + target, + features: features.clone(), + mode: BuildMode::Release, + }; + (globals, cmd_qemu, overwrite) + }); - (globals, cmd, overwrite) - }) - .run_and_coalesce() + build.chain(run).run_and_coalesce() } /// Check the binary sizes of examples diff --git a/xtask/src/command.rs b/xtask/src/command.rs index a45cb8a..187a3dd 100644 --- a/xtask/src/command.rs +++ b/xtask/src/command.rs @@ -593,6 +593,11 @@ impl<'a> CargoCommand<'a> { if let Some(cargoarg) = cargoarg { args.extend_from_slice(&[cargoarg]); } + + // We need to be in the `rtic` directory to pick up + // the correct .cargo/config.toml file + args.extend_from_slice(&["-Z", "unstable-options", "-C", "rtic"]); + args.extend_from_slice(&[ self.command(), "--example", @@ -856,7 +861,7 @@ pub fn handle_results(globals: &Globals, results: Vec) -> Result if globals.verbose > 0 { info!("✅ Success: {cmd}{path}\n {}", cmd.as_cmd_string()); } else { - info!("✅ Success:{cmd}{path}"); + info!("✅ Success: {cmd}{path}"); } }); diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 7077d55..853dbe7 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -268,7 +268,7 @@ fn main() -> anyhow::Result<()> { Commands::Qemu(args) | Commands::Run(args) => { // x86_64 target not valid info!("Testing for backend: {backend:?}"); - run_test( + qemu_run_examples( globals, &cargologlevel, backend, -- cgit v1.2.3 From deeb3877f061fb71389ec7730c6c21e81e9e3050 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 15 Apr 2023 22:40:25 +0200 Subject: Improve locality of error messages & ExampleBuild + Qemu commands, and indicate failure earlier --- xtask/src/cargo_commands.rs | 50 +++++++++++++++++++++++++-------------------- xtask/src/command.rs | 34 +++++++++++++----------------- 2 files changed, 42 insertions(+), 42 deletions(-) (limited to 'xtask') diff --git a/xtask/src/cargo_commands.rs b/xtask/src/cargo_commands.rs index 42895d8..2e30997 100644 --- a/xtask/src/cargo_commands.rs +++ b/xtask/src/cargo_commands.rs @@ -312,30 +312,36 @@ pub fn qemu_run_examples<'c>( let target = backend.to_target(); let features = Some(target.and_features(backend.to_rtic_feature())); - let build = examples_iter(examples).map(|example| { - let cmd_build = CargoCommand::ExampleBuild { - // We need to be in the correct - cargoarg: &None, - example, - target, - features: features.clone(), - mode: BuildMode::Release, - }; - (globals, cmd_build, overwrite) - }); + examples_iter(examples) + .flat_map(|example| { + let cmd_build = CargoCommand::ExampleBuild { + cargoarg: &None, + example, + target, + features: features.clone(), + mode: BuildMode::Release, + }; - let run = examples_iter(examples).map(|example| { - let cmd_qemu = CargoCommand::Qemu { - cargoarg, - example, - target, - features: features.clone(), - mode: BuildMode::Release, - }; - (globals, cmd_qemu, overwrite) - }); + let cmd_qemu = CargoCommand::Qemu { + cargoarg, + example, + target, + features: features.clone(), + mode: BuildMode::Release, + }; + + #[cfg(not(feature = "rayon"))] + { + [cmd_build, cmd_qemu].into_iter() + } - build.chain(run).run_and_coalesce() + #[cfg(feature = "rayon")] + { + [cmd_build, cmd_qemu].into_par_iter() + } + }) + .map(|cmd| (globals, cmd, overwrite)) + .run_and_coalesce() } /// Check the binary sizes of examples diff --git a/xtask/src/command.rs b/xtask/src/command.rs index 187a3dd..3596897 100644 --- a/xtask/src/command.rs +++ b/xtask/src/command.rs @@ -765,6 +765,10 @@ pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::R log::info!("\n{}", stdout); } + if !exit_status.success() { + log::error!("❌ Command failed. Run to completion for the summary."); + } + Ok(RunResult { exit_status, stdout, @@ -825,32 +829,19 @@ pub fn handle_results(globals: &Globals, results: Vec) -> Result }); let log_stdout_stderr = |level: Level| { - move |(command, stdout, stderr): (&CargoCommand, &String, &String)| { + move |(cmd, stdout, stderr): (&CargoCommand, &String, &String)| { + let cmd = cmd.as_cmd_string(); if !stdout.is_empty() && !stderr.is_empty() { - log::log!( - level, - "Output for \"{command}\"\nStdout:\n{stdout}\nStderr:\n{stderr}" - ); + log::log!(level, "\n{cmd}\nStdout:\n{stdout}\nStderr:\n{stderr}"); } else if !stdout.is_empty() { - log::log!( - level, - "Output for \"{command}\":\nStdout:\n{}", - stdout.trim_end() - ); + log::log!(level, "\n{cmd}\nStdout:\n{}", stdout.trim_end()); } else if !stderr.is_empty() { - log::log!( - level, - "Output for \"{command}\"\nStderr:\n{}", - stderr.trim_end() - ); + log::log!(level, "\n{cmd}\nStderr:\n{}", stderr.trim_end()); } } }; - successes.clone().for_each(log_stdout_stderr(Level::Debug)); - errors.clone().for_each(log_stdout_stderr(Level::Error)); - - successes.for_each(|(cmd, _, _)| { + successes.for_each(|(cmd, stdout, stderr)| { let path = if let Some(dir) = cmd.chdir() { let path = dir.as_os_str().to_str().unwrap_or("Not displayable"); format!(" (in {path}") @@ -863,15 +854,18 @@ pub fn handle_results(globals: &Globals, results: Vec) -> Result } else { info!("✅ Success: {cmd}{path}"); } + + log_stdout_stderr(Level::Debug)((cmd, stdout, stderr)); }); - errors.clone().for_each(|(cmd, _, _)| { + errors.clone().for_each(|(cmd, stdout, stderr)| { if let Some(dir) = cmd.chdir() { let path = dir.as_os_str().to_str().unwrap_or("Not displayable"); error!("❌ Failed: {cmd} (in {path}) \n {}", cmd.as_cmd_string()); } else { error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string()); } + log_stdout_stderr(Level::Error)((cmd, stdout, stderr)); }); command_errors -- cgit v1.2.3 From 9dc9f492639daace5222562c124846fb0d3cb154 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 15 Apr 2023 23:22:00 +0200 Subject: Use chdir() instead of unstable option, also confirm whenver a command succeeds because why not --- xtask/src/argument_parsing.rs | 2 +- xtask/src/cargo_commands.rs | 7 ++++ xtask/src/command.rs | 89 ++++++++++++++++++++++++++----------------- 3 files changed, 63 insertions(+), 35 deletions(-) (limited to 'xtask') diff --git a/xtask/src/argument_parsing.rs b/xtask/src/argument_parsing.rs index 6cae186..69275eb 100644 --- a/xtask/src/argument_parsing.rs +++ b/xtask/src/argument_parsing.rs @@ -326,7 +326,7 @@ impl UsageExamplesOpt { let usage_examples: Vec<_> = std::fs::read_dir("./examples")? .filter_map(Result::ok) .filter(|p| p.metadata().ok().map(|p| p.is_dir()).unwrap_or(false)) - .filter_map(|p| p.file_name().as_os_str().to_str().map(ToString::to_string)) + .filter_map(|p| p.file_name().to_str().map(ToString::to_string)) .collect(); let selected_examples: Option> = self diff --git a/xtask/src/cargo_commands.rs b/xtask/src/cargo_commands.rs index 2e30997..ec91eae 100644 --- a/xtask/src/cargo_commands.rs +++ b/xtask/src/cargo_commands.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use crate::{ argument_parsing::{Backends, BuildOrCheck, ExtraArguments, Globals, PackageOpt, TestMetadata}, command::{BuildMode, CargoCommand}, @@ -186,6 +188,7 @@ pub fn cargo_example<'c>( target: backend.to_target(), features, mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), }, }; (globals, command, false) @@ -320,6 +323,7 @@ pub fn qemu_run_examples<'c>( target, features: features.clone(), mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), }; let cmd_qemu = CargoCommand::Qemu { @@ -328,6 +332,7 @@ pub fn qemu_run_examples<'c>( target, features: features.clone(), mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), }; #[cfg(not(feature = "rayon"))] @@ -363,6 +368,7 @@ pub fn build_and_check_size<'c>( target, features: features.clone(), mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), }; if let Err(err) = command_parser(globals, &cmd, false) { error!("{err}"); @@ -375,6 +381,7 @@ pub fn build_and_check_size<'c>( features: features.clone(), mode: BuildMode::Release, arguments: arguments.clone(), + dir: Some(PathBuf::from("./rtic")), }; (globals, cmd, false) }); diff --git a/xtask/src/command.rs b/xtask/src/command.rs index 3596897..e06c89e 100644 --- a/xtask/src/command.rs +++ b/xtask/src/command.rs @@ -51,6 +51,7 @@ pub enum CargoCommand<'a> { target: Target<'a>, features: Option, mode: BuildMode, + dir: Option, }, ExampleBuild { cargoarg: &'a Option<&'a str>, @@ -58,6 +59,7 @@ pub enum CargoCommand<'a> { target: Target<'a>, features: Option, mode: BuildMode, + dir: Option, }, ExampleCheck { cargoarg: &'a Option<&'a str>, @@ -111,6 +113,7 @@ pub enum CargoCommand<'a> { features: Option, mode: BuildMode, arguments: Option, + dir: Option, }, CheckInDir { mode: BuildMode, @@ -179,22 +182,32 @@ impl core::fmt::Display for CargoCommand<'_> { target, features, mode, - } => write!( - f, - "Run example {example} in QEMU {}", - details(target, mode, features, cargoarg) - ), + dir, + } => { + let details = details(target, mode, features, cargoarg); + if let Some(dir) = dir { + let dir = dir.to_str().unwrap_or("Not displayable"); + write!(f, "Run example {example} in QEMU from {dir} {details}",) + } else { + write!(f, "Run example {example} in QEMU {details}",) + } + } CargoCommand::ExampleBuild { cargoarg, example, target, features, mode, - } => write!( - f, - "Build example {example} {}", - details(target, mode, features, cargoarg) - ), + dir, + } => { + let details = details(target, mode, features, cargoarg); + if let Some(dir) = dir { + let dir = dir.to_str().unwrap_or("Not displayable"); + write!(f, "Build example {example} in {dir} {details}") + } else { + write!(f, "Build example {example} {details}",) + } + } CargoCommand::ExampleCheck { cargoarg, example, @@ -221,7 +234,7 @@ impl core::fmt::Display for CargoCommand<'_> { ) } CargoCommand::BuildInDir { mode, dir } => { - let dir = dir.as_os_str().to_str().unwrap_or("Not displayable"); + let dir = dir.to_str().unwrap_or("Not displayable"); write!(f, "Build {dir} ({mode})") } CargoCommand::Check { @@ -239,7 +252,7 @@ impl core::fmt::Display for CargoCommand<'_> { ) } CargoCommand::CheckInDir { mode, dir } => { - let dir = dir.as_os_str().to_str().unwrap_or("Not displayable"); + let dir = dir.to_str().unwrap_or("Not displayable"); write!(f, "Check {dir} ({mode})") } CargoCommand::Clippy { @@ -315,12 +328,15 @@ impl core::fmt::Display for CargoCommand<'_> { features, mode, arguments: _, + dir, } => { - write!( - f, - "Compute size of example {example} {}", - details(target, mode, features, cargoarg) - ) + let details = details(target, mode, features, cargoarg); + if let Some(dir) = dir { + let dir = dir.to_str().unwrap_or("Not displayable"); + write!(f, "Compute size of example {example} from {dir} {details}",) + } else { + write!(f, "Compute size of example {example} {details}") + } } } } @@ -328,9 +344,15 @@ impl core::fmt::Display for CargoCommand<'_> { impl<'a> CargoCommand<'a> { pub fn as_cmd_string(&self) -> String { + let cd = if let Some(Some(chdir)) = self.chdir().map(|p| p.to_str()) { + format!("cd {chdir} && ") + } else { + format!("") + }; + let executable = self.executable(); let args = self.args().join(" "); - format!("{executable} {args}") + format!("{cd}{executable} {args}") } fn command(&self) -> &'static str { @@ -406,6 +428,8 @@ impl<'a> CargoCommand<'a> { target, features, mode, + // Dir is exposed through chdir instead + dir: _, } => { let mut args = vec!["+nightly"]; @@ -413,10 +437,6 @@ impl<'a> CargoCommand<'a> { args.extend_from_slice(&[cargoarg]); } - // We need to be in the `rtic` directory to pick up - // the correct .cargo/config.toml file - args.extend_from_slice(&["-Z", "unstable-options", "-C", "rtic"]); - args.extend_from_slice(&[ self.command(), "--example", @@ -588,16 +608,14 @@ impl<'a> CargoCommand<'a> { target, features, mode, + // Dir is exposed through chdir instead + dir: _, } => { let mut args = vec!["+nightly"]; if let Some(cargoarg) = cargoarg { args.extend_from_slice(&[cargoarg]); } - // We need to be in the `rtic` directory to pick up - // the correct .cargo/config.toml file - args.extend_from_slice(&["-Z", "unstable-options", "-C", "rtic"]); - args.extend_from_slice(&[ self.command(), "--example", @@ -648,16 +666,14 @@ impl<'a> CargoCommand<'a> { features, mode, arguments, + // Dir is exposed through chdir instead + dir: _, } => { let mut args = vec!["+nightly"]; if let Some(cargoarg) = cargoarg { args.extend_from_slice(&[cargoarg]); } - // We need to be in the `rtic` directory to pick up - // the correct .cargo/config.toml file - args.extend_from_slice(&["-Z", "unstable-options", "-C", "rtic"]); - args.extend_from_slice(&[ self.command(), "--example", @@ -708,6 +724,9 @@ impl<'a> CargoCommand<'a> { CargoCommand::CheckInDir { dir, .. } | CargoCommand::BuildInDir { dir, .. } => { Some(dir) } + CargoCommand::Qemu { dir, .. } + | CargoCommand::ExampleBuild { dir, .. } + | CargoCommand::ExampleSize { dir, .. } => dir.as_ref(), _ => None, } } @@ -752,7 +771,7 @@ pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::R .stderr(stderr_mode); if let Some(dir) = command.chdir() { - process.current_dir(dir); + process.current_dir(dir.canonicalize()?); } let result = process.output()?; @@ -765,7 +784,9 @@ pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::R log::info!("\n{}", stdout); } - if !exit_status.success() { + if exit_status.success() { + log::info!("✅ Success.") + } else { log::error!("❌ Command failed. Run to completion for the summary."); } @@ -843,7 +864,7 @@ pub fn handle_results(globals: &Globals, results: Vec) -> Result successes.for_each(|(cmd, stdout, stderr)| { let path = if let Some(dir) = cmd.chdir() { - let path = dir.as_os_str().to_str().unwrap_or("Not displayable"); + let path = dir.to_str().unwrap_or("Not displayable"); format!(" (in {path}") } else { format!("") @@ -860,7 +881,7 @@ pub fn handle_results(globals: &Globals, results: Vec) -> Result errors.clone().for_each(|(cmd, stdout, stderr)| { if let Some(dir) = cmd.chdir() { - let path = dir.as_os_str().to_str().unwrap_or("Not displayable"); + let path = dir.to_str().unwrap_or("Not displayable"); error!("❌ Failed: {cmd} (in {path}) \n {}", cmd.as_cmd_string()); } else { error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string()); -- cgit v1.2.3 From 404867cdf92990cb0aba415dfbee97c7fef78b60 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 16 Apr 2023 09:44:30 +0200 Subject: CargoCommand can take any package --- xtask/src/argument_parsing.rs | 20 +++++++++++--------- xtask/src/cargo_commands.rs | 8 ++++---- xtask/src/command.rs | 26 +++++++++++++------------- xtask/src/main.rs | 2 +- 4 files changed, 29 insertions(+), 27 deletions(-) (limited to 'xtask') diff --git a/xtask/src/argument_parsing.rs b/xtask/src/argument_parsing.rs index 69275eb..738168e 100644 --- a/xtask/src/argument_parsing.rs +++ b/xtask/src/argument_parsing.rs @@ -19,15 +19,17 @@ impl fmt::Display for Package { } impl Package { - pub fn name(&self) -> &str { - match self { + pub fn name(&self) -> String { + let name = match self { Package::Rtic => "rtic", Package::RticCommon => "rtic-common", Package::RticMacros => "rtic-macros", Package::RticMonotonics => "rtic-monotonics", Package::RticSync => "rtic-sync", Package::RticTime => "rtic-time", - } + }; + + name.to_string() } pub fn all() -> Vec { @@ -102,33 +104,33 @@ impl TestMetadata { ); let features = Some(backend.to_target().and_features(&features)); CargoCommand::Test { - package: Some(package), + package: Some(package.name()), features, test: Some("ui".to_owned()), } } Package::RticMacros => CargoCommand::Test { - package: Some(package), + package: Some(package.name()), features: Some(backend.to_rtic_macros_feature().to_owned()), test: None, }, Package::RticSync => CargoCommand::Test { - package: Some(package), + package: Some(package.name()), features: Some("testing".to_owned()), test: None, }, Package::RticCommon => CargoCommand::Test { - package: Some(package), + package: Some(package.name()), features: Some("testing".to_owned()), test: None, }, Package::RticMonotonics => CargoCommand::Test { - package: Some(package), + package: Some(package.name()), features: None, test: None, }, Package::RticTime => CargoCommand::Test { - package: Some(package), + package: Some(package.name()), features: Some("critical-section/std".into()), test: None, }, diff --git a/xtask/src/cargo_commands.rs b/xtask/src/cargo_commands.rs index ec91eae..c290e95 100644 --- a/xtask/src/cargo_commands.rs +++ b/xtask/src/cargo_commands.rs @@ -114,14 +114,14 @@ pub fn cargo<'c>( let command = match operation { BuildOrCheck::Check => CargoCommand::Check { cargoarg, - package: Some(package), + package: Some(package.name()), target, features, mode: BuildMode::Release, }, BuildOrCheck::Build => CargoCommand::Build { cargoarg, - package: Some(package), + package: Some(package.name()), target, features, mode: BuildMode::Release, @@ -224,7 +224,7 @@ pub fn cargo_clippy<'c>( globals, CargoCommand::Clippy { cargoarg, - package: Some(package), + package: Some(package.name()), target, features, }, @@ -247,7 +247,7 @@ pub fn cargo_format<'c>( globals, CargoCommand::Format { cargoarg, - package: Some(p), + package: Some(p.name()), check_only, }, false, diff --git a/xtask/src/command.rs b/xtask/src/command.rs index e06c89e..da6d907 100644 --- a/xtask/src/command.rs +++ b/xtask/src/command.rs @@ -1,8 +1,8 @@ use log::{error, info, Level}; use crate::{ - argument_parsing::Globals, cargo_commands::FinalRunResult, ExtraArguments, Package, RunResult, - Target, TestRunError, + argument_parsing::Globals, cargo_commands::FinalRunResult, ExtraArguments, RunResult, Target, + TestRunError, }; use core::fmt; use std::{ @@ -70,27 +70,27 @@ pub enum CargoCommand<'a> { }, Build { cargoarg: &'a Option<&'a str>, - package: Option, + package: Option, target: Target<'a>, features: Option, mode: BuildMode, }, Check { cargoarg: &'a Option<&'a str>, - package: Option, + package: Option, target: Target<'a>, features: Option, mode: BuildMode, }, Clippy { cargoarg: &'a Option<&'a str>, - package: Option, + package: Option, target: Target<'a>, features: Option, }, Format { cargoarg: &'a Option<&'a str>, - package: Option, + package: Option, check_only: bool, }, Doc { @@ -99,7 +99,7 @@ pub enum CargoCommand<'a> { arguments: Option, }, Test { - package: Option, + package: Option, features: Option, test: Option, }, @@ -127,7 +127,7 @@ pub enum CargoCommand<'a> { impl core::fmt::Display for CargoCommand<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let p = |p: &Option| { + let p = |p: &Option| { if let Some(package) = p { format!("package {package}") } else { @@ -468,7 +468,7 @@ impl<'a> CargoCommand<'a> { args.extend_from_slice(&[self.command(), "--target", target.triple()]); if let Some(package) = package { - args.extend_from_slice(&["--package", package.name()]); + args.extend_from_slice(&["--package", package]); } if let Some(feature) = features { @@ -493,7 +493,7 @@ impl<'a> CargoCommand<'a> { args.extend_from_slice(&[self.command()]); if let Some(package) = package { - args.extend_from_slice(&["--package", package.name()]); + args.extend_from_slice(&["--package", package]); } if let Some(feature) = features { @@ -518,7 +518,7 @@ impl<'a> CargoCommand<'a> { args.extend_from_slice(&[self.command()]); if let Some(package) = package { - args.extend_from_slice(&["--package", package.name()]); + args.extend_from_slice(&["--package", package]); } if let Some(feature) = features { @@ -557,7 +557,7 @@ impl<'a> CargoCommand<'a> { args.extend_from_slice(&[self.command()]); if let Some(package) = package { - args.extend_from_slice(&["--package", package.name()]); + args.extend_from_slice(&["--package", package]); } if let Some(feature) = features { @@ -594,7 +594,7 @@ impl<'a> CargoCommand<'a> { } if let Some(package) = package { - args.extend_from_slice(&["--package", package.name()]); + args.extend_from_slice(&["--package", package]); } if *check_only { args.extend_from_slice(&["--check"]); diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 853dbe7..0043474 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -3,7 +3,7 @@ mod build; mod cargo_commands; mod command; -use argument_parsing::{ExtraArguments, Globals, Package}; +use argument_parsing::{ExtraArguments, Globals}; use clap::Parser; use command::OutputMode; use core::fmt; -- cgit v1.2.3 From b59bf686c1e10bb8068d89e43779d7777f553c48 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 16 Apr 2023 11:00:39 +0200 Subject: Redo command building so that we don't repeat as much, and to make it easier to add new ones --- xtask/src/cargo_commands.rs | 46 ++-- xtask/src/command.rs | 583 ++++++++++++++++++-------------------------- xtask/src/main.rs | 4 +- 3 files changed, 270 insertions(+), 363 deletions(-) (limited to 'xtask') diff --git a/xtask/src/cargo_commands.rs b/xtask/src/cargo_commands.rs index c290e95..e2982e4 100644 --- a/xtask/src/cargo_commands.rs +++ b/xtask/src/cargo_commands.rs @@ -111,6 +111,7 @@ pub fn cargo<'c>( } }) .map(move |(package, target, features)| { + let target = target.into(); let command = match operation { BuildOrCheck::Check => CargoCommand::Check { cargoarg, @@ -118,6 +119,7 @@ pub fn cargo<'c>( target, features, mode: BuildMode::Release, + dir: None, }, BuildOrCheck::Build => CargoCommand::Build { cargoarg, @@ -125,6 +127,7 @@ pub fn cargo<'c>( target, features, mode: BuildMode::Release, + dir: None, }, }; @@ -147,13 +150,21 @@ pub fn cargo_usage_example( let path = format!("examples/{example}"); let command = match operation { - BuildOrCheck::Check => CargoCommand::CheckInDir { + BuildOrCheck::Check => CargoCommand::Check { + cargoarg: &None, mode: BuildMode::Release, - dir: path.into(), + dir: Some(path.into()), + package: None, + target: None, + features: None, }, - BuildOrCheck::Build => CargoCommand::BuildInDir { + BuildOrCheck::Build => CargoCommand::Build { + cargoarg: &None, + package: None, + target: None, + features: None, mode: BuildMode::Release, - dir: path.into(), + dir: Some(path.into()), }, }; (globals, command, false) @@ -178,14 +189,14 @@ pub fn cargo_example<'c>( BuildOrCheck::Check => CargoCommand::ExampleCheck { cargoarg, example, - target: backend.to_target(), + target: Some(backend.to_target()), features, mode: BuildMode::Release, }, BuildOrCheck::Build => CargoCommand::ExampleBuild { cargoarg, example, - target: backend.to_target(), + target: Some(backend.to_target()), features, mode: BuildMode::Release, dir: Some(PathBuf::from("./rtic")), @@ -220,16 +231,14 @@ pub fn cargo_clippy<'c>( } }) .map(move |(package, target, features)| { - ( - globals, - CargoCommand::Clippy { - cargoarg, - package: Some(package.name()), - target, - features, - }, - false, - ) + let command = CargoCommand::Clippy { + cargoarg, + package: Some(package.name()), + target: target.into(), + features, + }; + + (globals, command, false) }); runner.run_and_coalesce() @@ -317,6 +326,7 @@ pub fn qemu_run_examples<'c>( examples_iter(examples) .flat_map(|example| { + let target = target.into(); let cmd_build = CargoCommand::ExampleBuild { cargoarg: &None, example, @@ -361,6 +371,8 @@ pub fn build_and_check_size<'c>( let features = Some(target.and_features(backend.to_rtic_feature())); let runner = examples_iter(examples).map(|example| { + let target = target.into(); + // Make sure the requested example(s) are built let cmd = CargoCommand::ExampleBuild { cargoarg: &Some("--quiet"), @@ -377,7 +389,7 @@ pub fn build_and_check_size<'c>( let cmd = CargoCommand::ExampleSize { cargoarg, example, - target: backend.to_target(), + target, features: features.clone(), mode: BuildMode::Release, arguments: arguments.clone(), diff --git a/xtask/src/command.rs b/xtask/src/command.rs index da6d907..33d0703 100644 --- a/xtask/src/command.rs +++ b/xtask/src/command.rs @@ -41,14 +41,15 @@ pub enum CargoCommand<'a> { Run { cargoarg: &'a Option<&'a str>, example: &'a str, - target: Target<'a>, + target: Option>, features: Option, mode: BuildMode, + dir: Option, }, Qemu { cargoarg: &'a Option<&'a str>, example: &'a str, - target: Target<'a>, + target: Option>, features: Option, mode: BuildMode, dir: Option, @@ -56,7 +57,7 @@ pub enum CargoCommand<'a> { ExampleBuild { cargoarg: &'a Option<&'a str>, example: &'a str, - target: Target<'a>, + target: Option>, features: Option, mode: BuildMode, dir: Option, @@ -64,28 +65,30 @@ pub enum CargoCommand<'a> { ExampleCheck { cargoarg: &'a Option<&'a str>, example: &'a str, - target: Target<'a>, + target: Option>, features: Option, mode: BuildMode, }, Build { cargoarg: &'a Option<&'a str>, package: Option, - target: Target<'a>, + target: Option>, features: Option, mode: BuildMode, + dir: Option, }, Check { cargoarg: &'a Option<&'a str>, package: Option, - target: Target<'a>, + target: Option>, features: Option, mode: BuildMode, + dir: Option, }, Clippy { cargoarg: &'a Option<&'a str>, package: Option, - target: Target<'a>, + target: Option>, features: Option, }, Format { @@ -109,60 +112,78 @@ pub enum CargoCommand<'a> { ExampleSize { cargoarg: &'a Option<&'a str>, example: &'a str, - target: Target<'a>, + target: Option>, features: Option, mode: BuildMode, arguments: Option, dir: Option, }, - CheckInDir { - mode: BuildMode, - dir: PathBuf, - }, - BuildInDir { - mode: BuildMode, - dir: PathBuf, - }, } impl core::fmt::Display for CargoCommand<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let p = |p: &Option| { + fn p(p: &Option) -> String { if let Some(package) = p { format!("package {package}") } else { format!("default package") } - }; + } - let feat = |f: &Option| { + fn feat(f: &Option) -> String { if let Some(features) = f { format!("\"{features}\"") } else { format!("no features") } - }; + } - let carg = |f: &&Option<&str>| { + fn carg(f: &&Option<&str>) -> String { if let Some(cargoarg) = f { format!("{cargoarg}") } else { format!("no cargo args") } - }; + } - let details = |target: &Target, - mode: &BuildMode, - features: &Option, - cargoarg: &&Option<&str>| { + fn details( + target: &Option, + mode: Option<&BuildMode>, + features: &Option, + cargoarg: &&Option<&str>, + path: Option<&PathBuf>, + ) -> String { let feat = feat(features); let carg = carg(cargoarg); - if cargoarg.is_some() { + let in_dir = if let Some(path) = path { + let path = path.to_str().unwrap_or(""); + format!("in {path}") + } else { + format!("") + }; + + let target = if let Some(target) = target { + format!("{target}") + } else { + format!("") + }; + + let mode = if let Some(mode) = mode { + format!("{mode}") + } else { + format!("debug") + }; + + if cargoarg.is_some() && path.is_some() { + format!("({target}, {mode}, {feat}, {carg}, {in_dir})") + } else if cargoarg.is_some() { format!("({target}, {mode}, {feat}, {carg})") + } else if path.is_some() { + format!("({target}, {mode}, {feat}, {in_dir})") } else { format!("({target}, {mode}, {feat})") } - }; + } match self { CargoCommand::Run { @@ -171,11 +192,14 @@ impl core::fmt::Display for CargoCommand<'_> { target, features, mode, - } => write!( - f, - "Run example {example} {}", - details(target, mode, features, cargoarg) - ), + dir, + } => { + write!( + f, + "Run example {example} {}", + details(target, Some(mode), features, cargoarg, dir.as_ref()) + ) + } CargoCommand::Qemu { cargoarg, example, @@ -184,13 +208,8 @@ impl core::fmt::Display for CargoCommand<'_> { mode, dir, } => { - let details = details(target, mode, features, cargoarg); - if let Some(dir) = dir { - let dir = dir.to_str().unwrap_or("Not displayable"); - write!(f, "Run example {example} in QEMU from {dir} {details}",) - } else { - write!(f, "Run example {example} in QEMU {details}",) - } + let details = details(target, Some(mode), features, cargoarg, dir.as_ref()); + write!(f, "Run example {example} in QEMU {details}",) } CargoCommand::ExampleBuild { cargoarg, @@ -200,13 +219,8 @@ impl core::fmt::Display for CargoCommand<'_> { mode, dir, } => { - let details = details(target, mode, features, cargoarg); - if let Some(dir) = dir { - let dir = dir.to_str().unwrap_or("Not displayable"); - write!(f, "Build example {example} in {dir} {details}") - } else { - write!(f, "Build example {example} {details}",) - } + let details = details(target, Some(mode), features, cargoarg, dir.as_ref()); + write!(f, "Build example {example} {details}",) } CargoCommand::ExampleCheck { cargoarg, @@ -217,7 +231,7 @@ impl core::fmt::Display for CargoCommand<'_> { } => write!( f, "Check example {example} {}", - details(target, mode, features, cargoarg) + details(target, Some(mode), features, cargoarg, None) ), CargoCommand::Build { cargoarg, @@ -225,50 +239,40 @@ impl core::fmt::Display for CargoCommand<'_> { target, features, mode, + dir, } => { let package = p(package); write!( f, "Build {package} {}", - details(target, mode, features, cargoarg) + details(target, Some(mode), features, cargoarg, dir.as_ref()) ) } - CargoCommand::BuildInDir { mode, dir } => { - let dir = dir.to_str().unwrap_or("Not displayable"); - write!(f, "Build {dir} ({mode})") - } + CargoCommand::Check { cargoarg, package, target, features, mode, + dir, } => { let package = p(package); write!( f, "Check {package} {}", - details(target, mode, features, cargoarg) + details(target, Some(mode), features, cargoarg, dir.as_ref()) ) } - CargoCommand::CheckInDir { mode, dir } => { - let dir = dir.to_str().unwrap_or("Not displayable"); - write!(f, "Check {dir} ({mode})") - } CargoCommand::Clippy { cargoarg, package, target, features, } => { + let details = details(target, None, features, cargoarg, None); let package = p(package); - let features = feat(features); - let carg = carg(cargoarg); - if cargoarg.is_some() { - write!(f, "Clippy {package} ({target}, {features}, {carg})") - } else { - write!(f, "Clippy {package} ({target}, {features})") - } + write!(f, "Clippy {package} {details}") } CargoCommand::Format { cargoarg, @@ -330,13 +334,8 @@ impl core::fmt::Display for CargoCommand<'_> { arguments: _, dir, } => { - let details = details(target, mode, features, cargoarg); - if let Some(dir) = dir { - let dir = dir.to_str().unwrap_or("Not displayable"); - write!(f, "Compute size of example {example} from {dir} {details}",) - } else { - write!(f, "Compute size of example {example} {details}") - } + let details = details(target, Some(mode), features, cargoarg, dir.as_ref()); + write!(f, "Compute size of example {example} {details}") } } } @@ -358,12 +357,8 @@ impl<'a> CargoCommand<'a> { fn command(&self) -> &'static str { match self { CargoCommand::Run { .. } | CargoCommand::Qemu { .. } => "run", - CargoCommand::ExampleCheck { .. } - | CargoCommand::Check { .. } - | CargoCommand::CheckInDir { .. } => "check", - CargoCommand::ExampleBuild { .. } - | CargoCommand::Build { .. } - | CargoCommand::BuildInDir { .. } => "build", + CargoCommand::ExampleCheck { .. } | CargoCommand::Check { .. } => "check", + CargoCommand::ExampleBuild { .. } | CargoCommand::Build { .. } => "build", CargoCommand::ExampleSize { .. } => "size", CargoCommand::Clippy { .. } => "clippy", CargoCommand::Format { .. } => "fmt", @@ -384,189 +379,159 @@ impl<'a> CargoCommand<'a> { | CargoCommand::Clippy { .. } | CargoCommand::Format { .. } | CargoCommand::Test { .. } - | CargoCommand::Doc { .. } - | CargoCommand::CheckInDir { .. } - | CargoCommand::BuildInDir { .. } => "cargo", + | CargoCommand::Doc { .. } => "cargo", CargoCommand::Book { .. } => "mdbook", } } + /// Build args using common arguments for all commands, and the + /// specific information provided + fn build_args<'i, T: Iterator>( + &'i self, + nightly: bool, + cargoarg: &'i Option<&'i str>, + features: &'i Option, + mode: Option<&'i BuildMode>, + extra: T, + ) -> Vec<&str> { + let mut args: Vec<&str> = Vec::new(); + + if nightly { + args.push("+nightly"); + } + + if let Some(cargoarg) = cargoarg.as_deref() { + args.push(cargoarg); + } + + args.push(self.command()); + + if let Some(target) = self.target() { + args.extend_from_slice(&["--target", target.triple()]) + } + + if let Some(features) = features.as_ref() { + args.extend_from_slice(&["--features", features]); + } + + if let Some(mode) = mode.map(|m| m.to_flag()).flatten() { + args.push(mode); + } + + args.extend(extra); + + args + } + + /// Turn the ExtraArguments into an interator that contains the separating dashes + /// and the rest of the arguments. + /// + /// NOTE: you _must_ chain this iterator at the _end_ of the extra arguments. + fn extra_args(args: Option<&ExtraArguments>) -> impl Iterator { + #[allow(irrefutable_let_patterns)] + let args = if let Some(ExtraArguments::Other(arguments)) = args { + // Extra arguments must be passed after "--" + ["--"] + .into_iter() + .chain(arguments.iter().map(String::as_str)) + .collect() + } else { + vec![] + }; + args.into_iter() + } + pub fn args(&self) -> Vec<&str> { + fn p(package: &Option) -> impl Iterator { + if let Some(package) = package { + vec!["--package", &package].into_iter() + } else { + vec![].into_iter() + } + } + match self { // For future embedded-ci, for now the same as Qemu CargoCommand::Run { cargoarg, example, - target, features, mode, - } => { - let mut args = vec!["+nightly"]; - if let Some(cargoarg) = cargoarg { - args.extend_from_slice(&[cargoarg]); - } - - args.extend_from_slice(&[ - self.command(), - "--example", - example, - "--target", - target.triple(), - ]); - - if let Some(feature) = features { - args.extend_from_slice(&["--features", feature]); - } - if let Some(flag) = mode.to_flag() { - args.push(flag); - } - args - } + // dir is exposed through `chdir` + dir: _, + // Target is added by build_args + target: _, + } => self.build_args( + true, + cargoarg, + features, + Some(mode), + ["--example", example].into_iter(), + ), CargoCommand::Qemu { cargoarg, example, - target, features, mode, - // Dir is exposed through chdir instead + // dir is exposed through `chdir` dir: _, - } => { - let mut args = vec!["+nightly"]; - - if let Some(cargoarg) = cargoarg { - args.extend_from_slice(&[cargoarg]); - } - - args.extend_from_slice(&[ - self.command(), - "--example", - example, - "--target", - target.triple(), - ]); - - if let Some(feature) = features { - args.extend_from_slice(&["--features", feature]); - } - if let Some(flag) = mode.to_flag() { - args.push(flag); - } - args - } + // Target is added by build_args + target: _, + } => self.build_args( + true, + cargoarg, + features, + Some(mode), + ["--example", example].into_iter(), + ), CargoCommand::Build { cargoarg, package, - target, features, mode, - } => { - let mut args = vec!["+nightly"]; - if let Some(cargoarg) = cargoarg { - args.extend_from_slice(&[cargoarg]); - } - - args.extend_from_slice(&[self.command(), "--target", target.triple()]); - - if let Some(package) = package { - args.extend_from_slice(&["--package", package]); - } - - if let Some(feature) = features { - args.extend_from_slice(&["--features", feature]); - } - if let Some(flag) = mode.to_flag() { - args.push(flag); - } - args - } + // Dir is exposed through `chdir` + dir: _, + // Target is added by build_args + target: _, + } => self.build_args(true, cargoarg, features, Some(mode), p(package)), CargoCommand::Check { cargoarg, package, - target: _, features, mode, - } => { - let mut args = vec!["+nightly"]; - if let Some(cargoarg) = cargoarg { - args.extend_from_slice(&[cargoarg]); - } - args.extend_from_slice(&[self.command()]); - - if let Some(package) = package { - args.extend_from_slice(&["--package", package]); - } - - if let Some(feature) = features { - args.extend_from_slice(&["--features", feature]); - } - if let Some(flag) = mode.to_flag() { - args.push(flag); - } - args - } + // Dir is exposed through `chdir` + dir: _, + // Target is added by build_args + target: _, + } => self.build_args(true, cargoarg, features, Some(mode), p(package)), CargoCommand::Clippy { cargoarg, package, - target: _, features, - } => { - let mut args = vec!["+nightly"]; - if let Some(cargoarg) = cargoarg { - args.extend_from_slice(&[cargoarg]); - } - - args.extend_from_slice(&[self.command()]); - - if let Some(package) = package { - args.extend_from_slice(&["--package", package]); - } - - if let Some(feature) = features { - args.extend_from_slice(&["--features", feature]); - } - args - } + // Target is added by build_args + target: _, + } => self.build_args(true, cargoarg, features, None, p(package)), CargoCommand::Doc { cargoarg, features, arguments, } => { - let mut args = vec!["+nightly"]; - if let Some(cargoarg) = cargoarg { - args.extend_from_slice(&[cargoarg]); - } - - args.extend_from_slice(&[self.command()]); - - if let Some(feature) = features { - args.extend_from_slice(&["--features", feature]); - } - if let Some(ExtraArguments::Other(arguments)) = arguments { - for arg in arguments { - args.extend_from_slice(&[arg.as_str()]); - } - } - args + let extra = Self::extra_args(arguments.as_ref()); + self.build_args(true, cargoarg, features, None, extra) } CargoCommand::Test { package, features, test, } => { - let mut args = vec!["+nightly"]; - args.extend_from_slice(&[self.command()]); - - if let Some(package) = package { - args.extend_from_slice(&["--package", package]); - } - - if let Some(feature) = features { - args.extend_from_slice(&["--features", feature]); - } - if let Some(test) = test { - args.extend_from_slice(&["--test", test]); - } - args + let extra = if let Some(test) = test { + vec!["--test", test] + } else { + vec![] + }; + let package = p(package); + let extra = extra.into_iter().chain(package); + self.build_args(true, &None, features, None, extra) } CargoCommand::Book { arguments } => { let mut args = vec![]; @@ -588,145 +553,89 @@ impl<'a> CargoCommand<'a> { package, check_only, } => { - let mut args = vec!["+nightly", self.command()]; - if let Some(cargoarg) = cargoarg { - args.extend_from_slice(&[cargoarg]); - } - - if let Some(package) = package { - args.extend_from_slice(&["--package", package]); - } - if *check_only { - args.extend_from_slice(&["--check"]); - } - - args + let extra = if *check_only { Some("--check") } else { None }; + let package = p(package); + self.build_args( + true, + cargoarg, + &None, + None, + extra.into_iter().chain(package), + ) } CargoCommand::ExampleBuild { cargoarg, example, - target, features, mode, - // Dir is exposed through chdir instead + // dir is exposed through `chdir` dir: _, - } => { - let mut args = vec!["+nightly"]; - if let Some(cargoarg) = cargoarg { - args.extend_from_slice(&[cargoarg]); - } - - args.extend_from_slice(&[ - self.command(), - "--example", - example, - "--target", - target.triple(), - ]); - - if let Some(feature) = features { - args.extend_from_slice(&["--features", feature]); - } - if let Some(flag) = mode.to_flag() { - args.push(flag); - } - args - } + // Target is added by build_args + target: _, + } => self.build_args( + true, + cargoarg, + features, + Some(mode), + ["--example", example].into_iter(), + ), CargoCommand::ExampleCheck { cargoarg, example, - target, features, mode, - } => { - let mut args = vec!["+nightly"]; - if let Some(cargoarg) = cargoarg { - args.extend_from_slice(&[cargoarg]); - } - args.extend_from_slice(&[ - self.command(), - "--example", - example, - "--target", - target.triple(), - ]); - - if let Some(feature) = features { - args.extend_from_slice(&["--features", feature]); - } - if let Some(flag) = mode.to_flag() { - args.push(flag); - } - args - } + // Target is added by build_args + target: _, + } => self.build_args( + true, + cargoarg, + features, + Some(mode), + ["--example", example].into_iter(), + ), CargoCommand::ExampleSize { cargoarg, example, - target, features, mode, arguments, - // Dir is exposed through chdir instead + // Target is added by build_args + target: _, + // dir is exposed through `chdir` dir: _, } => { - let mut args = vec!["+nightly"]; - if let Some(cargoarg) = cargoarg { - args.extend_from_slice(&[cargoarg]); - } - - args.extend_from_slice(&[ - self.command(), - "--example", - example, - "--target", - target.triple(), - ]); - - if let Some(feature_name) = features { - args.extend_from_slice(&["--features", feature_name]); - } - if let Some(flag) = mode.to_flag() { - args.push(flag); - } - if let Some(ExtraArguments::Other(arguments)) = arguments { - // Arguments to cargo size must be passed after "--" - args.extend_from_slice(&["--"]); - for arg in arguments { - args.extend_from_slice(&[arg.as_str()]); - } - } - args - } - CargoCommand::CheckInDir { mode, dir: _ } => { - let mut args = vec!["+nightly"]; - args.push(self.command()); + let extra = ["--example", example] + .into_iter() + .chain(Self::extra_args(arguments.as_ref())); - if let Some(mode) = mode.to_flag() { - args.push(mode); - } - - args - } - CargoCommand::BuildInDir { mode, dir: _ } => { - let mut args = vec!["+nightly", self.command()]; - - if let Some(mode) = mode.to_flag() { - args.push(mode); - } - - args + self.build_args(true, cargoarg, features, Some(mode), extra) } } } + /// TODO: integrate this into `args` once `-C` becomes stable. fn chdir(&self) -> Option<&PathBuf> { match self { - CargoCommand::CheckInDir { dir, .. } | CargoCommand::BuildInDir { dir, .. } => { - Some(dir) - } CargoCommand::Qemu { dir, .. } | CargoCommand::ExampleBuild { dir, .. } - | CargoCommand::ExampleSize { dir, .. } => dir.as_ref(), + | CargoCommand::ExampleSize { dir, .. } + | CargoCommand::Build { dir, .. } + | CargoCommand::Run { dir, .. } + | CargoCommand::Check { dir, .. } => dir.as_ref(), + _ => None, + } + } + + fn target(&self) -> Option<&Target> { + match self { + CargoCommand::Run { target, .. } + | CargoCommand::Qemu { target, .. } + | CargoCommand::ExampleBuild { target, .. } + | CargoCommand::ExampleCheck { target, .. } + | CargoCommand::Build { target, .. } + | CargoCommand::Check { target, .. } + | CargoCommand::Clippy { target, .. } + | CargoCommand::ExampleSize { target, .. } => target.as_ref(), _ => None, } } @@ -863,29 +772,17 @@ pub fn handle_results(globals: &Globals, results: Vec) -> Result }; successes.for_each(|(cmd, stdout, stderr)| { - let path = if let Some(dir) = cmd.chdir() { - let path = dir.to_str().unwrap_or("Not displayable"); - format!(" (in {path}") - } else { - format!("") - }; - if globals.verbose > 0 { - info!("✅ Success: {cmd}{path}\n {}", cmd.as_cmd_string()); + info!("✅ Success: {cmd}\n {}", cmd.as_cmd_string()); } else { - info!("✅ Success: {cmd}{path}"); + info!("✅ Success: {cmd}"); } log_stdout_stderr(Level::Debug)((cmd, stdout, stderr)); }); errors.clone().for_each(|(cmd, stdout, stderr)| { - if let Some(dir) = cmd.chdir() { - let path = dir.to_str().unwrap_or("Not displayable"); - error!("❌ Failed: {cmd} (in {path}) \n {}", cmd.as_cmd_string()); - } else { - error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string()); - } + error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string()); log_stdout_stderr(Level::Error)((cmd, stdout, stderr)); }); diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 0043474..2f35079 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -356,9 +356,7 @@ fn command_parser( | CargoCommand::Doc { .. } | CargoCommand::Test { .. } | CargoCommand::Book { .. } - | CargoCommand::ExampleSize { .. } - | CargoCommand::BuildInDir { .. } - | CargoCommand::CheckInDir { .. } => { + | CargoCommand::ExampleSize { .. } => { let cargo_result = run_command(command, output_mode)?; Ok(cargo_result) } -- cgit v1.2.3 From 66a3d02b4585a76615d750c33a37edd5e8fd30e6 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 16 Apr 2023 11:02:49 +0200 Subject: Rename cargo_commands -> run Rename command -> cargo_command --- xtask/src/argument_parsing.rs | 2 +- xtask/src/cargo_command.rs | 801 ++++++++++++++++++++++++++++++++++++++++++ xtask/src/cargo_commands.rs | 402 --------------------- xtask/src/command.rs | 801 ------------------------------------------ xtask/src/main.rs | 10 +- xtask/src/run.rs | 402 +++++++++++++++++++++ 6 files changed, 1209 insertions(+), 1209 deletions(-) create mode 100644 xtask/src/cargo_command.rs delete mode 100644 xtask/src/cargo_commands.rs delete mode 100644 xtask/src/command.rs create mode 100644 xtask/src/run.rs (limited to 'xtask') diff --git a/xtask/src/argument_parsing.rs b/xtask/src/argument_parsing.rs index 738168e..8c8f7d2 100644 --- a/xtask/src/argument_parsing.rs +++ b/xtask/src/argument_parsing.rs @@ -1,4 +1,4 @@ -use crate::{command::CargoCommand, Target, ARMV6M, ARMV7M, ARMV8MBASE, ARMV8MMAIN}; +use crate::{cargo_command::CargoCommand, Target, ARMV6M, ARMV7M, ARMV8MBASE, ARMV8MMAIN}; use clap::{Args, Parser, Subcommand}; use core::fmt; diff --git a/xtask/src/cargo_command.rs b/xtask/src/cargo_command.rs new file mode 100644 index 0000000..cd38566 --- /dev/null +++ b/xtask/src/cargo_command.rs @@ -0,0 +1,801 @@ +use log::{error, info, Level}; + +use crate::{ + argument_parsing::Globals, xtasks::FinalRunResult, ExtraArguments, RunResult, Target, + TestRunError, +}; +use core::fmt; +use std::{ + fs::File, + io::Read, + path::PathBuf, + process::{Command, Stdio}, +}; + +#[allow(dead_code)] +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum BuildMode { + Release, + Debug, +} + +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum OutputMode { + PipedAndCollected, + Inherited, +} + +impl From for Stdio { + fn from(value: OutputMode) -> Self { + match value { + OutputMode::PipedAndCollected => Stdio::piped(), + OutputMode::Inherited => Stdio::inherit(), + } + } +} + +#[derive(Debug)] +pub enum CargoCommand<'a> { + // For future embedded-ci + #[allow(dead_code)] + Run { + cargoarg: &'a Option<&'a str>, + example: &'a str, + target: Option>, + features: Option, + mode: BuildMode, + dir: Option, + }, + Qemu { + cargoarg: &'a Option<&'a str>, + example: &'a str, + target: Option>, + features: Option, + mode: BuildMode, + dir: Option, + }, + ExampleBuild { + cargoarg: &'a Option<&'a str>, + example: &'a str, + target: Option>, + features: Option, + mode: BuildMode, + dir: Option, + }, + ExampleCheck { + cargoarg: &'a Option<&'a str>, + example: &'a str, + target: Option>, + features: Option, + mode: BuildMode, + }, + Build { + cargoarg: &'a Option<&'a str>, + package: Option, + target: Option>, + features: Option, + mode: BuildMode, + dir: Option, + }, + Check { + cargoarg: &'a Option<&'a str>, + package: Option, + target: Option>, + features: Option, + mode: BuildMode, + dir: Option, + }, + Clippy { + cargoarg: &'a Option<&'a str>, + package: Option, + target: Option>, + features: Option, + }, + Format { + cargoarg: &'a Option<&'a str>, + package: Option, + check_only: bool, + }, + Doc { + cargoarg: &'a Option<&'a str>, + features: Option, + arguments: Option, + }, + Test { + package: Option, + features: Option, + test: Option, + }, + Book { + arguments: Option, + }, + ExampleSize { + cargoarg: &'a Option<&'a str>, + example: &'a str, + target: Option>, + features: Option, + mode: BuildMode, + arguments: Option, + dir: Option, + }, +} + +impl core::fmt::Display for CargoCommand<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fn p(p: &Option) -> String { + if let Some(package) = p { + format!("package {package}") + } else { + format!("default package") + } + } + + fn feat(f: &Option) -> String { + if let Some(features) = f { + format!("\"{features}\"") + } else { + format!("no features") + } + } + + fn carg(f: &&Option<&str>) -> String { + if let Some(cargoarg) = f { + format!("{cargoarg}") + } else { + format!("no cargo args") + } + } + + fn details( + target: &Option, + mode: Option<&BuildMode>, + features: &Option, + cargoarg: &&Option<&str>, + path: Option<&PathBuf>, + ) -> String { + let feat = feat(features); + let carg = carg(cargoarg); + let in_dir = if let Some(path) = path { + let path = path.to_str().unwrap_or(""); + format!("in {path}") + } else { + format!("") + }; + + let target = if let Some(target) = target { + format!("{target}") + } else { + format!("") + }; + + let mode = if let Some(mode) = mode { + format!("{mode}") + } else { + format!("debug") + }; + + if cargoarg.is_some() && path.is_some() { + format!("({target}, {mode}, {feat}, {carg}, {in_dir})") + } else if cargoarg.is_some() { + format!("({target}, {mode}, {feat}, {carg})") + } else if path.is_some() { + format!("({target}, {mode}, {feat}, {in_dir})") + } else { + format!("({target}, {mode}, {feat})") + } + } + + match self { + CargoCommand::Run { + cargoarg, + example, + target, + features, + mode, + dir, + } => { + write!( + f, + "Run example {example} {}", + details(target, Some(mode), features, cargoarg, dir.as_ref()) + ) + } + CargoCommand::Qemu { + cargoarg, + example, + target, + features, + mode, + dir, + } => { + let details = details(target, Some(mode), features, cargoarg, dir.as_ref()); + write!(f, "Run example {example} in QEMU {details}",) + } + CargoCommand::ExampleBuild { + cargoarg, + example, + target, + features, + mode, + dir, + } => { + let details = details(target, Some(mode), features, cargoarg, dir.as_ref()); + write!(f, "Build example {example} {details}",) + } + CargoCommand::ExampleCheck { + cargoarg, + example, + target, + features, + mode, + } => write!( + f, + "Check example {example} {}", + details(target, Some(mode), features, cargoarg, None) + ), + CargoCommand::Build { + cargoarg, + package, + target, + features, + mode, + dir, + } => { + let package = p(package); + write!( + f, + "Build {package} {}", + details(target, Some(mode), features, cargoarg, dir.as_ref()) + ) + } + + CargoCommand::Check { + cargoarg, + package, + target, + features, + mode, + dir, + } => { + let package = p(package); + write!( + f, + "Check {package} {}", + details(target, Some(mode), features, cargoarg, dir.as_ref()) + ) + } + CargoCommand::Clippy { + cargoarg, + package, + target, + features, + } => { + let details = details(target, None, features, cargoarg, None); + let package = p(package); + write!(f, "Clippy {package} {details}") + } + CargoCommand::Format { + cargoarg, + package, + check_only, + } => { + let package = p(package); + let carg = carg(cargoarg); + + let carg = if cargoarg.is_some() { + format!("(cargo args: {carg})") + } else { + format!("") + }; + + if *check_only { + write!(f, "Check format for {package} {carg}") + } else { + write!(f, "Format {package} {carg}") + } + } + CargoCommand::Doc { + cargoarg, + features, + arguments, + } => { + let feat = feat(features); + let carg = carg(cargoarg); + let arguments = arguments + .clone() + .map(|a| format!("{a}")) + .unwrap_or_else(|| "no extra arguments".into()); + if cargoarg.is_some() { + write!(f, "Document ({feat}, {carg}, {arguments})") + } else { + write!(f, "Document ({feat}, {arguments})") + } + } + CargoCommand::Test { + package, + features, + test, + } => { + let p = p(package); + let test = test + .clone() + .map(|t| format!("test {t}")) + .unwrap_or("all tests".into()); + let feat = feat(features); + write!(f, "Run {test} in {p} (features: {feat})") + } + CargoCommand::Book { arguments: _ } => write!(f, "Build the book"), + CargoCommand::ExampleSize { + cargoarg, + example, + target, + features, + mode, + arguments: _, + dir, + } => { + let details = details(target, Some(mode), features, cargoarg, dir.as_ref()); + write!(f, "Compute size of example {example} {details}") + } + } + } +} + +impl<'a> CargoCommand<'a> { + pub fn as_cmd_string(&self) -> String { + let cd = if let Some(Some(chdir)) = self.chdir().map(|p| p.to_str()) { + format!("cd {chdir} && ") + } else { + format!("") + }; + + let executable = self.executable(); + let args = self.args().join(" "); + format!("{cd}{executable} {args}") + } + + fn command(&self) -> &'static str { + match self { + CargoCommand::Run { .. } | CargoCommand::Qemu { .. } => "run", + CargoCommand::ExampleCheck { .. } | CargoCommand::Check { .. } => "check", + CargoCommand::ExampleBuild { .. } | CargoCommand::Build { .. } => "build", + CargoCommand::ExampleSize { .. } => "size", + CargoCommand::Clippy { .. } => "clippy", + CargoCommand::Format { .. } => "fmt", + CargoCommand::Doc { .. } => "doc", + CargoCommand::Book { .. } => "build", + CargoCommand::Test { .. } => "test", + } + } + pub fn executable(&self) -> &'static str { + match self { + CargoCommand::Run { .. } + | CargoCommand::Qemu { .. } + | CargoCommand::ExampleCheck { .. } + | CargoCommand::Check { .. } + | CargoCommand::ExampleBuild { .. } + | CargoCommand::Build { .. } + | CargoCommand::ExampleSize { .. } + | CargoCommand::Clippy { .. } + | CargoCommand::Format { .. } + | CargoCommand::Test { .. } + | CargoCommand::Doc { .. } => "cargo", + CargoCommand::Book { .. } => "mdbook", + } + } + + /// Build args using common arguments for all commands, and the + /// specific information provided + fn build_args<'i, T: Iterator>( + &'i self, + nightly: bool, + cargoarg: &'i Option<&'i str>, + features: &'i Option, + mode: Option<&'i BuildMode>, + extra: T, + ) -> Vec<&str> { + let mut args: Vec<&str> = Vec::new(); + + if nightly { + args.push("+nightly"); + } + + if let Some(cargoarg) = cargoarg.as_deref() { + args.push(cargoarg); + } + + args.push(self.command()); + + if let Some(target) = self.target() { + args.extend_from_slice(&["--target", target.triple()]) + } + + if let Some(features) = features.as_ref() { + args.extend_from_slice(&["--features", features]); + } + + if let Some(mode) = mode.map(|m| m.to_flag()).flatten() { + args.push(mode); + } + + args.extend(extra); + + args + } + + /// Turn the ExtraArguments into an interator that contains the separating dashes + /// and the rest of the arguments. + /// + /// NOTE: you _must_ chain this iterator at the _end_ of the extra arguments. + fn extra_args(args: Option<&ExtraArguments>) -> impl Iterator { + #[allow(irrefutable_let_patterns)] + let args = if let Some(ExtraArguments::Other(arguments)) = args { + // Extra arguments must be passed after "--" + ["--"] + .into_iter() + .chain(arguments.iter().map(String::as_str)) + .collect() + } else { + vec![] + }; + args.into_iter() + } + + pub fn args(&self) -> Vec<&str> { + fn p(package: &Option) -> impl Iterator { + if let Some(package) = package { + vec!["--package", &package].into_iter() + } else { + vec![].into_iter() + } + } + + match self { + // For future embedded-ci, for now the same as Qemu + CargoCommand::Run { + cargoarg, + example, + features, + mode, + // dir is exposed through `chdir` + dir: _, + // Target is added by build_args + target: _, + } => self.build_args( + true, + cargoarg, + features, + Some(mode), + ["--example", example].into_iter(), + ), + CargoCommand::Qemu { + cargoarg, + example, + features, + mode, + // dir is exposed through `chdir` + dir: _, + // Target is added by build_args + target: _, + } => self.build_args( + true, + cargoarg, + features, + Some(mode), + ["--example", example].into_iter(), + ), + CargoCommand::Build { + cargoarg, + package, + features, + mode, + // Dir is exposed through `chdir` + dir: _, + // Target is added by build_args + target: _, + } => self.build_args(true, cargoarg, features, Some(mode), p(package)), + CargoCommand::Check { + cargoarg, + package, + features, + mode, + // Dir is exposed through `chdir` + dir: _, + // Target is added by build_args + target: _, + } => self.build_args(true, cargoarg, features, Some(mode), p(package)), + CargoCommand::Clippy { + cargoarg, + package, + features, + // Target is added by build_args + target: _, + } => self.build_args(true, cargoarg, features, None, p(package)), + CargoCommand::Doc { + cargoarg, + features, + arguments, + } => { + let extra = Self::extra_args(arguments.as_ref()); + self.build_args(true, cargoarg, features, None, extra) + } + CargoCommand::Test { + package, + features, + test, + } => { + let extra = if let Some(test) = test { + vec!["--test", test] + } else { + vec![] + }; + let package = p(package); + let extra = extra.into_iter().chain(package); + self.build_args(true, &None, features, None, extra) + } + CargoCommand::Book { arguments } => { + let mut args = vec![]; + + if let Some(ExtraArguments::Other(arguments)) = arguments { + for arg in arguments { + args.extend_from_slice(&[arg.as_str()]); + } + } else { + // If no argument given, run mdbook build + // with default path to book + args.extend_from_slice(&[self.command()]); + args.extend_from_slice(&["book/en"]); + } + args + } + CargoCommand::Format { + cargoarg, + package, + check_only, + } => { + let extra = if *check_only { Some("--check") } else { None }; + let package = p(package); + self.build_args( + true, + cargoarg, + &None, + None, + extra.into_iter().chain(package), + ) + } + CargoCommand::ExampleBuild { + cargoarg, + example, + features, + mode, + // dir is exposed through `chdir` + dir: _, + // Target is added by build_args + target: _, + } => self.build_args( + true, + cargoarg, + features, + Some(mode), + ["--example", example].into_iter(), + ), + CargoCommand::ExampleCheck { + cargoarg, + example, + features, + mode, + // Target is added by build_args + target: _, + } => self.build_args( + true, + cargoarg, + features, + Some(mode), + ["--example", example].into_iter(), + ), + CargoCommand::ExampleSize { + cargoarg, + example, + features, + mode, + arguments, + // Target is added by build_args + target: _, + // dir is exposed through `chdir` + dir: _, + } => { + let extra = ["--example", example] + .into_iter() + .chain(Self::extra_args(arguments.as_ref())); + + self.build_args(true, cargoarg, features, Some(mode), extra) + } + } + } + + /// TODO: integrate this into `args` once `-C` becomes stable. + fn chdir(&self) -> Option<&PathBuf> { + match self { + CargoCommand::Qemu { dir, .. } + | CargoCommand::ExampleBuild { dir, .. } + | CargoCommand::ExampleSize { dir, .. } + | CargoCommand::Build { dir, .. } + | CargoCommand::Run { dir, .. } + | CargoCommand::Check { dir, .. } => dir.as_ref(), + _ => None, + } + } + + fn target(&self) -> Option<&Target> { + match self { + CargoCommand::Run { target, .. } + | CargoCommand::Qemu { target, .. } + | CargoCommand::ExampleBuild { target, .. } + | CargoCommand::ExampleCheck { target, .. } + | CargoCommand::Build { target, .. } + | CargoCommand::Check { target, .. } + | CargoCommand::Clippy { target, .. } + | CargoCommand::ExampleSize { target, .. } => target.as_ref(), + _ => None, + } + } + + pub fn print_stdout_intermediate(&self) -> bool { + match self { + Self::ExampleSize { .. } => true, + _ => false, + } + } +} + +impl BuildMode { + #[allow(clippy::wrong_self_convention)] + pub fn to_flag(&self) -> Option<&str> { + match self { + BuildMode::Release => Some("--release"), + BuildMode::Debug => None, + } + } +} + +impl fmt::Display for BuildMode { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let cmd = match self { + BuildMode::Release => "release", + BuildMode::Debug => "debug", + }; + + write!(f, "{cmd}") + } +} + +pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::Result { + log::info!("👟 {command}"); + + let mut process = Command::new(command.executable()); + + process + .args(command.args()) + .stdout(Stdio::piped()) + .stderr(stderr_mode); + + if let Some(dir) = command.chdir() { + process.current_dir(dir.canonicalize()?); + } + + let result = process.output()?; + + let exit_status = result.status; + let stderr = String::from_utf8(result.stderr).unwrap_or("Not displayable".into()); + let stdout = String::from_utf8(result.stdout).unwrap_or("Not displayable".into()); + + if command.print_stdout_intermediate() && exit_status.success() { + log::info!("\n{}", stdout); + } + + if exit_status.success() { + log::info!("✅ Success.") + } else { + log::error!("❌ Command failed. Run to completion for the summary."); + } + + Ok(RunResult { + exit_status, + stdout, + stderr, + }) +} + +/// Check if `run` was successful. +/// returns Ok in case the run went as expected, +/// Err otherwise +pub fn run_successful(run: &RunResult, expected_output_file: &str) -> Result<(), TestRunError> { + let mut file_handle = + File::open(expected_output_file).map_err(|_| TestRunError::FileError { + file: expected_output_file.to_owned(), + })?; + let mut expected_output = String::new(); + file_handle + .read_to_string(&mut expected_output) + .map_err(|_| TestRunError::FileError { + file: expected_output_file.to_owned(), + })?; + + if expected_output != run.stdout { + Err(TestRunError::FileCmpError { + expected: expected_output.clone(), + got: run.stdout.clone(), + }) + } else if !run.exit_status.success() { + Err(TestRunError::CommandError(run.clone())) + } else { + Ok(()) + } +} + +pub fn handle_results(globals: &Globals, results: Vec) -> Result<(), ()> { + let errors = results.iter().filter_map(|r| { + if let FinalRunResult::Failed(c, r) = r { + Some((c, &r.stdout, &r.stderr)) + } else { + None + } + }); + + let successes = results.iter().filter_map(|r| { + if let FinalRunResult::Success(c, r) = r { + Some((c, &r.stdout, &r.stderr)) + } else { + None + } + }); + + let command_errors = results.iter().filter_map(|r| { + if let FinalRunResult::CommandError(c, e) = r { + Some((c, e)) + } else { + None + } + }); + + let log_stdout_stderr = |level: Level| { + move |(cmd, stdout, stderr): (&CargoCommand, &String, &String)| { + let cmd = cmd.as_cmd_string(); + if !stdout.is_empty() && !stderr.is_empty() { + log::log!(level, "\n{cmd}\nStdout:\n{stdout}\nStderr:\n{stderr}"); + } else if !stdout.is_empty() { + log::log!(level, "\n{cmd}\nStdout:\n{}", stdout.trim_end()); + } else if !stderr.is_empty() { + log::log!(level, "\n{cmd}\nStderr:\n{}", stderr.trim_end()); + } + } + }; + + successes.for_each(|(cmd, stdout, stderr)| { + if globals.verbose > 0 { + info!("✅ Success: {cmd}\n {}", cmd.as_cmd_string()); + } else { + info!("✅ Success: {cmd}"); + } + + log_stdout_stderr(Level::Debug)((cmd, stdout, stderr)); + }); + + errors.clone().for_each(|(cmd, stdout, stderr)| { + error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string()); + log_stdout_stderr(Level::Error)((cmd, stdout, stderr)); + }); + + command_errors + .clone() + .for_each(|(cmd, error)| error!("❌ Failed: {cmd}\n {}\n{error}", cmd.as_cmd_string())); + + let ecount = errors.count() + command_errors.count(); + if ecount != 0 { + log::error!("{ecount} commands failed."); + Err(()) + } else { + info!("🚀🚀🚀 All tasks succeeded 🚀🚀🚀"); + Ok(()) + } +} diff --git a/xtask/src/cargo_commands.rs b/xtask/src/cargo_commands.rs deleted file mode 100644 index e2982e4..0000000 --- a/xtask/src/cargo_commands.rs +++ /dev/null @@ -1,402 +0,0 @@ -use std::path::PathBuf; - -use crate::{ - argument_parsing::{Backends, BuildOrCheck, ExtraArguments, Globals, PackageOpt, TestMetadata}, - command::{BuildMode, CargoCommand}, - command_parser, RunResult, -}; -use log::error; - -#[cfg(feature = "rayon")] -use rayon::prelude::*; - -use iters::*; - -#[derive(Debug)] -pub enum FinalRunResult<'c> { - Success(CargoCommand<'c>, RunResult), - Failed(CargoCommand<'c>, RunResult), - CommandError(CargoCommand<'c>, anyhow::Error), -} - -fn run_and_convert<'a>( - (global, command, overwrite): (&Globals, CargoCommand<'a>, bool), -) -> FinalRunResult<'a> { - // Run the command - let result = command_parser(global, &command, overwrite); - - let output = match result { - // If running the command succeeded without looking at any of the results, - // log the data and see if the actual execution was succesfull too. - Ok(result) => { - if result.exit_status.success() { - FinalRunResult::Success(command, result) - } else { - FinalRunResult::Failed(command, result) - } - } - // If it didn't and some IO error occured, just panic - Err(e) => FinalRunResult::CommandError(command, e), - }; - - log::trace!("Final result: {output:?}"); - - output -} - -pub trait CoalescingRunner<'c> { - /// Run all the commands in this iterator, and coalesce the results into - /// one error (if any individual commands failed) - fn run_and_coalesce(self) -> Vec>; -} - -#[cfg(not(feature = "rayon"))] -mod iters { - use super::*; - - pub fn examples_iter(examples: &[String]) -> impl Iterator { - examples.into_iter() - } - - impl<'g, 'c, I> CoalescingRunner<'c> for I - where - I: Iterator, bool)>, - { - fn run_and_coalesce(self) -> Vec> { - self.map(run_and_convert).collect() - } - } -} - -#[cfg(feature = "rayon")] -mod iters { - use super::*; - - pub fn examples_iter(examples: &[String]) -> impl ParallelIterator { - examples.into_par_iter() - } - - impl<'g, 'c, I> CoalescingRunner<'c> for I - where - I: ParallelIterator, bool)>, - { - fn run_and_coalesce(self) -> Vec> { - self.map(run_and_convert).collect() - } - } -} - -/// Cargo command to either build or check -pub fn cargo<'c>( - globals: &Globals, - operation: BuildOrCheck, - cargoarg: &'c Option<&'c str>, - package: &'c PackageOpt, - backend: Backends, -) -> Vec> { - let runner = package - .packages() - .flat_map(|package| { - let target = backend.to_target(); - let features = package.features(target, backend, globals.partial); - - #[cfg(feature = "rayon")] - { - features.into_par_iter().map(move |f| (package, target, f)) - } - - #[cfg(not(feature = "rayon"))] - { - features.into_iter().map(move |f| (package, target, f)) - } - }) - .map(move |(package, target, features)| { - let target = target.into(); - let command = match operation { - BuildOrCheck::Check => CargoCommand::Check { - cargoarg, - package: Some(package.name()), - target, - features, - mode: BuildMode::Release, - dir: None, - }, - BuildOrCheck::Build => CargoCommand::Build { - cargoarg, - package: Some(package.name()), - target, - features, - mode: BuildMode::Release, - dir: None, - }, - }; - - (globals, command, false) - }); - - runner.run_and_coalesce() -} - -/// Cargo command to build a usage example. -/// -/// The usage examples are in examples/ -pub fn cargo_usage_example( - globals: &Globals, - operation: BuildOrCheck, - usage_examples: Vec, -) -> Vec> { - examples_iter(&usage_examples) - .map(|example| { - let path = format!("examples/{example}"); - - let command = match operation { - BuildOrCheck::Check => CargoCommand::Check { - cargoarg: &None, - mode: BuildMode::Release, - dir: Some(path.into()), - package: None, - target: None, - features: None, - }, - BuildOrCheck::Build => CargoCommand::Build { - cargoarg: &None, - package: None, - target: None, - features: None, - mode: BuildMode::Release, - dir: Some(path.into()), - }, - }; - (globals, command, false) - }) - .run_and_coalesce() -} - -/// Cargo command to either build or check all examples -/// -/// The examples are in rtic/examples -pub fn cargo_example<'c>( - globals: &Globals, - operation: BuildOrCheck, - cargoarg: &'c Option<&'c str>, - backend: Backends, - examples: &'c [String], -) -> Vec> { - let runner = examples_iter(examples).map(|example| { - let features = Some(backend.to_target().and_features(backend.to_rtic_feature())); - - let command = match operation { - BuildOrCheck::Check => CargoCommand::ExampleCheck { - cargoarg, - example, - target: Some(backend.to_target()), - features, - mode: BuildMode::Release, - }, - BuildOrCheck::Build => CargoCommand::ExampleBuild { - cargoarg, - example, - target: Some(backend.to_target()), - features, - mode: BuildMode::Release, - dir: Some(PathBuf::from("./rtic")), - }, - }; - (globals, command, false) - }); - runner.run_and_coalesce() -} - -/// Run cargo clippy on selected package -pub fn cargo_clippy<'c>( - globals: &Globals, - cargoarg: &'c Option<&'c str>, - package: &'c PackageOpt, - backend: Backends, -) -> Vec> { - let runner = package - .packages() - .flat_map(|package| { - let target = backend.to_target(); - let features = package.features(target, backend, globals.partial); - - #[cfg(feature = "rayon")] - { - features.into_par_iter().map(move |f| (package, target, f)) - } - - #[cfg(not(feature = "rayon"))] - { - features.into_iter().map(move |f| (package, target, f)) - } - }) - .map(move |(package, target, features)| { - let command = CargoCommand::Clippy { - cargoarg, - package: Some(package.name()), - target: target.into(), - features, - }; - - (globals, command, false) - }); - - runner.run_and_coalesce() -} - -/// Run cargo fmt on selected package -pub fn cargo_format<'c>( - globals: &Globals, - cargoarg: &'c Option<&'c str>, - package: &'c PackageOpt, - check_only: bool, -) -> Vec> { - let runner = package.packages().map(|p| { - ( - globals, - CargoCommand::Format { - cargoarg, - package: Some(p.name()), - check_only, - }, - false, - ) - }); - runner.run_and_coalesce() -} - -/// Run cargo doc -pub fn cargo_doc<'c>( - globals: &Globals, - cargoarg: &'c Option<&'c str>, - backend: Backends, - arguments: &'c Option, -) -> Vec> { - let features = Some(backend.to_target().and_features(backend.to_rtic_feature())); - - let command = CargoCommand::Doc { - cargoarg, - features, - arguments: arguments.clone(), - }; - - vec![run_and_convert((globals, command, false))] -} - -/// Run cargo test on the selected package or all packages -/// -/// If no package is specified, loop through all packages -pub fn cargo_test<'c>( - globals: &Globals, - package: &'c PackageOpt, - backend: Backends, -) -> Vec> { - package - .packages() - .map(|p| (globals, TestMetadata::match_package(p, backend), false)) - .run_and_coalesce() -} - -/// Use mdbook to build the book -pub fn cargo_book<'c>( - globals: &Globals, - arguments: &'c Option, -) -> Vec> { - vec![run_and_convert(( - globals, - CargoCommand::Book { - arguments: arguments.clone(), - }, - false, - ))] -} - -/// Run examples -/// -/// Supports updating the expected output via the overwrite argument -pub fn qemu_run_examples<'c>( - globals: &Globals, - cargoarg: &'c Option<&'c str>, - backend: Backends, - examples: &'c [String], - overwrite: bool, -) -> Vec> { - let target = backend.to_target(); - let features = Some(target.and_features(backend.to_rtic_feature())); - - examples_iter(examples) - .flat_map(|example| { - let target = target.into(); - let cmd_build = CargoCommand::ExampleBuild { - cargoarg: &None, - example, - target, - features: features.clone(), - mode: BuildMode::Release, - dir: Some(PathBuf::from("./rtic")), - }; - - let cmd_qemu = CargoCommand::Qemu { - cargoarg, - example, - target, - features: features.clone(), - mode: BuildMode::Release, - dir: Some(PathBuf::from("./rtic")), - }; - - #[cfg(not(feature = "rayon"))] - { - [cmd_build, cmd_qemu].into_iter() - } - - #[cfg(feature = "rayon")] - { - [cmd_build, cmd_qemu].into_par_iter() - } - }) - .map(|cmd| (globals, cmd, overwrite)) - .run_and_coalesce() -} - -/// Check the binary sizes of examples -pub fn build_and_check_size<'c>( - globals: &Globals, - cargoarg: &'c Option<&'c str>, - backend: Backends, - examples: &'c [String], - arguments: &'c Option, -) -> Vec> { - let target = backend.to_target(); - let features = Some(target.and_features(backend.to_rtic_feature())); - - let runner = examples_iter(examples).map(|example| { - let target = target.into(); - - // Make sure the requested example(s) are built - let cmd = CargoCommand::ExampleBuild { - cargoarg: &Some("--quiet"), - example, - target, - features: features.clone(), - mode: BuildMode::Release, - dir: Some(PathBuf::from("./rtic")), - }; - if let Err(err) = command_parser(globals, &cmd, false) { - error!("{err}"); - } - - let cmd = CargoCommand::ExampleSize { - cargoarg, - example, - target, - features: features.clone(), - mode: BuildMode::Release, - arguments: arguments.clone(), - dir: Some(PathBuf::from("./rtic")), - }; - (globals, cmd, false) - }); - - runner.run_and_coalesce() -} diff --git a/xtask/src/command.rs b/xtask/src/command.rs deleted file mode 100644 index 33d0703..0000000 --- a/xtask/src/command.rs +++ /dev/null @@ -1,801 +0,0 @@ -use log::{error, info, Level}; - -use crate::{ - argument_parsing::Globals, cargo_commands::FinalRunResult, ExtraArguments, RunResult, Target, - TestRunError, -}; -use core::fmt; -use std::{ - fs::File, - io::Read, - path::PathBuf, - process::{Command, Stdio}, -}; - -#[allow(dead_code)] -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum BuildMode { - Release, - Debug, -} - -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum OutputMode { - PipedAndCollected, - Inherited, -} - -impl From for Stdio { - fn from(value: OutputMode) -> Self { - match value { - OutputMode::PipedAndCollected => Stdio::piped(), - OutputMode::Inherited => Stdio::inherit(), - } - } -} - -#[derive(Debug)] -pub enum CargoCommand<'a> { - // For future embedded-ci - #[allow(dead_code)] - Run { - cargoarg: &'a Option<&'a str>, - example: &'a str, - target: Option>, - features: Option, - mode: BuildMode, - dir: Option, - }, - Qemu { - cargoarg: &'a Option<&'a str>, - example: &'a str, - target: Option>, - features: Option, - mode: BuildMode, - dir: Option, - }, - ExampleBuild { - cargoarg: &'a Option<&'a str>, - example: &'a str, - target: Option>, - features: Option, - mode: BuildMode, - dir: Option, - }, - ExampleCheck { - cargoarg: &'a Option<&'a str>, - example: &'a str, - target: Option>, - features: Option, - mode: BuildMode, - }, - Build { - cargoarg: &'a Option<&'a str>, - package: Option, - target: Option>, - features: Option, - mode: BuildMode, - dir: Option, - }, - Check { - cargoarg: &'a Option<&'a str>, - package: Option, - target: Option>, - features: Option, - mode: BuildMode, - dir: Option, - }, - Clippy { - cargoarg: &'a Option<&'a str>, - package: Option, - target: Option>, - features: Option, - }, - Format { - cargoarg: &'a Option<&'a str>, - package: Option, - check_only: bool, - }, - Doc { - cargoarg: &'a Option<&'a str>, - features: Option, - arguments: Option, - }, - Test { - package: Option, - features: Option, - test: Option, - }, - Book { - arguments: Option, - }, - ExampleSize { - cargoarg: &'a Option<&'a str>, - example: &'a str, - target: Option>, - features: Option, - mode: BuildMode, - arguments: Option, - dir: Option, - }, -} - -impl core::fmt::Display for CargoCommand<'_> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fn p(p: &Option) -> String { - if let Some(package) = p { - format!("package {package}") - } else { - format!("default package") - } - } - - fn feat(f: &Option) -> String { - if let Some(features) = f { - format!("\"{features}\"") - } else { - format!("no features") - } - } - - fn carg(f: &&Option<&str>) -> String { - if let Some(cargoarg) = f { - format!("{cargoarg}") - } else { - format!("no cargo args") - } - } - - fn details( - target: &Option, - mode: Option<&BuildMode>, - features: &Option, - cargoarg: &&Option<&str>, - path: Option<&PathBuf>, - ) -> String { - let feat = feat(features); - let carg = carg(cargoarg); - let in_dir = if let Some(path) = path { - let path = path.to_str().unwrap_or(""); - format!("in {path}") - } else { - format!("") - }; - - let target = if let Some(target) = target { - format!("{target}") - } else { - format!("") - }; - - let mode = if let Some(mode) = mode { - format!("{mode}") - } else { - format!("debug") - }; - - if cargoarg.is_some() && path.is_some() { - format!("({target}, {mode}, {feat}, {carg}, {in_dir})") - } else if cargoarg.is_some() { - format!("({target}, {mode}, {feat}, {carg})") - } else if path.is_some() { - format!("({target}, {mode}, {feat}, {in_dir})") - } else { - format!("({target}, {mode}, {feat})") - } - } - - match self { - CargoCommand::Run { - cargoarg, - example, - target, - features, - mode, - dir, - } => { - write!( - f, - "Run example {example} {}", - details(target, Some(mode), features, cargoarg, dir.as_ref()) - ) - } - CargoCommand::Qemu { - cargoarg, - example, - target, - features, - mode, - dir, - } => { - let details = details(target, Some(mode), features, cargoarg, dir.as_ref()); - write!(f, "Run example {example} in QEMU {details}",) - } - CargoCommand::ExampleBuild { - cargoarg, - example, - target, - features, - mode, - dir, - } => { - let details = details(target, Some(mode), features, cargoarg, dir.as_ref()); - write!(f, "Build example {example} {details}",) - } - CargoCommand::ExampleCheck { - cargoarg, - example, - target, - features, - mode, - } => write!( - f, - "Check example {example} {}", - details(target, Some(mode), features, cargoarg, None) - ), - CargoCommand::Build { - cargoarg, - package, - target, - features, - mode, - dir, - } => { - let package = p(package); - write!( - f, - "Build {package} {}", - details(target, Some(mode), features, cargoarg, dir.as_ref()) - ) - } - - CargoCommand::Check { - cargoarg, - package, - target, - features, - mode, - dir, - } => { - let package = p(package); - write!( - f, - "Check {package} {}", - details(target, Some(mode), features, cargoarg, dir.as_ref()) - ) - } - CargoCommand::Clippy { - cargoarg, - package, - target, - features, - } => { - let details = details(target, None, features, cargoarg, None); - let package = p(package); - write!(f, "Clippy {package} {details}") - } - CargoCommand::Format { - cargoarg, - package, - check_only, - } => { - let package = p(package); - let carg = carg(cargoarg); - - let carg = if cargoarg.is_some() { - format!("(cargo args: {carg})") - } else { - format!("") - }; - - if *check_only { - write!(f, "Check format for {package} {carg}") - } else { - write!(f, "Format {package} {carg}") - } - } - CargoCommand::Doc { - cargoarg, - features, - arguments, - } => { - let feat = feat(features); - let carg = carg(cargoarg); - let arguments = arguments - .clone() - .map(|a| format!("{a}")) - .unwrap_or_else(|| "no extra arguments".into()); - if cargoarg.is_some() { - write!(f, "Document ({feat}, {carg}, {arguments})") - } else { - write!(f, "Document ({feat}, {arguments})") - } - } - CargoCommand::Test { - package, - features, - test, - } => { - let p = p(package); - let test = test - .clone() - .map(|t| format!("test {t}")) - .unwrap_or("all tests".into()); - let feat = feat(features); - write!(f, "Run {test} in {p} (features: {feat})") - } - CargoCommand::Book { arguments: _ } => write!(f, "Build the book"), - CargoCommand::ExampleSize { - cargoarg, - example, - target, - features, - mode, - arguments: _, - dir, - } => { - let details = details(target, Some(mode), features, cargoarg, dir.as_ref()); - write!(f, "Compute size of example {example} {details}") - } - } - } -} - -impl<'a> CargoCommand<'a> { - pub fn as_cmd_string(&self) -> String { - let cd = if let Some(Some(chdir)) = self.chdir().map(|p| p.to_str()) { - format!("cd {chdir} && ") - } else { - format!("") - }; - - let executable = self.executable(); - let args = self.args().join(" "); - format!("{cd}{executable} {args}") - } - - fn command(&self) -> &'static str { - match self { - CargoCommand::Run { .. } | CargoCommand::Qemu { .. } => "run", - CargoCommand::ExampleCheck { .. } | CargoCommand::Check { .. } => "check", - CargoCommand::ExampleBuild { .. } | CargoCommand::Build { .. } => "build", - CargoCommand::ExampleSize { .. } => "size", - CargoCommand::Clippy { .. } => "clippy", - CargoCommand::Format { .. } => "fmt", - CargoCommand::Doc { .. } => "doc", - CargoCommand::Book { .. } => "build", - CargoCommand::Test { .. } => "test", - } - } - pub fn executable(&self) -> &'static str { - match self { - CargoCommand::Run { .. } - | CargoCommand::Qemu { .. } - | CargoCommand::ExampleCheck { .. } - | CargoCommand::Check { .. } - | CargoCommand::ExampleBuild { .. } - | CargoCommand::Build { .. } - | CargoCommand::ExampleSize { .. } - | CargoCommand::Clippy { .. } - | CargoCommand::Format { .. } - | CargoCommand::Test { .. } - | CargoCommand::Doc { .. } => "cargo", - CargoCommand::Book { .. } => "mdbook", - } - } - - /// Build args using common arguments for all commands, and the - /// specific information provided - fn build_args<'i, T: Iterator>( - &'i self, - nightly: bool, - cargoarg: &'i Option<&'i str>, - features: &'i Option, - mode: Option<&'i BuildMode>, - extra: T, - ) -> Vec<&str> { - let mut args: Vec<&str> = Vec::new(); - - if nightly { - args.push("+nightly"); - } - - if let Some(cargoarg) = cargoarg.as_deref() { - args.push(cargoarg); - } - - args.push(self.command()); - - if let Some(target) = self.target() { - args.extend_from_slice(&["--target", target.triple()]) - } - - if let Some(features) = features.as_ref() { - args.extend_from_slice(&["--features", features]); - } - - if let Some(mode) = mode.map(|m| m.to_flag()).flatten() { - args.push(mode); - } - - args.extend(extra); - - args - } - - /// Turn the ExtraArguments into an interator that contains the separating dashes - /// and the rest of the arguments. - /// - /// NOTE: you _must_ chain this iterator at the _end_ of the extra arguments. - fn extra_args(args: Option<&ExtraArguments>) -> impl Iterator { - #[allow(irrefutable_let_patterns)] - let args = if let Some(ExtraArguments::Other(arguments)) = args { - // Extra arguments must be passed after "--" - ["--"] - .into_iter() - .chain(arguments.iter().map(String::as_str)) - .collect() - } else { - vec![] - }; - args.into_iter() - } - - pub fn args(&self) -> Vec<&str> { - fn p(package: &Option) -> impl Iterator { - if let Some(package) = package { - vec!["--package", &package].into_iter() - } else { - vec![].into_iter() - } - } - - match self { - // For future embedded-ci, for now the same as Qemu - CargoCommand::Run { - cargoarg, - example, - features, - mode, - // dir is exposed through `chdir` - dir: _, - // Target is added by build_args - target: _, - } => self.build_args( - true, - cargoarg, - features, - Some(mode), - ["--example", example].into_iter(), - ), - CargoCommand::Qemu { - cargoarg, - example, - features, - mode, - // dir is exposed through `chdir` - dir: _, - // Target is added by build_args - target: _, - } => self.build_args( - true, - cargoarg, - features, - Some(mode), - ["--example", example].into_iter(), - ), - CargoCommand::Build { - cargoarg, - package, - features, - mode, - // Dir is exposed through `chdir` - dir: _, - // Target is added by build_args - target: _, - } => self.build_args(true, cargoarg, features, Some(mode), p(package)), - CargoCommand::Check { - cargoarg, - package, - features, - mode, - // Dir is exposed through `chdir` - dir: _, - // Target is added by build_args - target: _, - } => self.build_args(true, cargoarg, features, Some(mode), p(package)), - CargoCommand::Clippy { - cargoarg, - package, - features, - // Target is added by build_args - target: _, - } => self.build_args(true, cargoarg, features, None, p(package)), - CargoCommand::Doc { - cargoarg, - features, - arguments, - } => { - let extra = Self::extra_args(arguments.as_ref()); - self.build_args(true, cargoarg, features, None, extra) - } - CargoCommand::Test { - package, - features, - test, - } => { - let extra = if let Some(test) = test { - vec!["--test", test] - } else { - vec![] - }; - let package = p(package); - let extra = extra.into_iter().chain(package); - self.build_args(true, &None, features, None, extra) - } - CargoCommand::Book { arguments } => { - let mut args = vec![]; - - if let Some(ExtraArguments::Other(arguments)) = arguments { - for arg in arguments { - args.extend_from_slice(&[arg.as_str()]); - } - } else { - // If no argument given, run mdbook build - // with default path to book - args.extend_from_slice(&[self.command()]); - args.extend_from_slice(&["book/en"]); - } - args - } - CargoCommand::Format { - cargoarg, - package, - check_only, - } => { - let extra = if *check_only { Some("--check") } else { None }; - let package = p(package); - self.build_args( - true, - cargoarg, - &None, - None, - extra.into_iter().chain(package), - ) - } - CargoCommand::ExampleBuild { - cargoarg, - example, - features, - mode, - // dir is exposed through `chdir` - dir: _, - // Target is added by build_args - target: _, - } => self.build_args( - true, - cargoarg, - features, - Some(mode), - ["--example", example].into_iter(), - ), - CargoCommand::ExampleCheck { - cargoarg, - example, - features, - mode, - // Target is added by build_args - target: _, - } => self.build_args( - true, - cargoarg, - features, - Some(mode), - ["--example", example].into_iter(), - ), - CargoCommand::ExampleSize { - cargoarg, - example, - features, - mode, - arguments, - // Target is added by build_args - target: _, - // dir is exposed through `chdir` - dir: _, - } => { - let extra = ["--example", example] - .into_iter() - .chain(Self::extra_args(arguments.as_ref())); - - self.build_args(true, cargoarg, features, Some(mode), extra) - } - } - } - - /// TODO: integrate this into `args` once `-C` becomes stable. - fn chdir(&self) -> Option<&PathBuf> { - match self { - CargoCommand::Qemu { dir, .. } - | CargoCommand::ExampleBuild { dir, .. } - | CargoCommand::ExampleSize { dir, .. } - | CargoCommand::Build { dir, .. } - | CargoCommand::Run { dir, .. } - | CargoCommand::Check { dir, .. } => dir.as_ref(), - _ => None, - } - } - - fn target(&self) -> Option<&Target> { - match self { - CargoCommand::Run { target, .. } - | CargoCommand::Qemu { target, .. } - | CargoCommand::ExampleBuild { target, .. } - | CargoCommand::ExampleCheck { target, .. } - | CargoCommand::Build { target, .. } - | CargoCommand::Check { target, .. } - | CargoCommand::Clippy { target, .. } - | CargoCommand::ExampleSize { target, .. } => target.as_ref(), - _ => None, - } - } - - pub fn print_stdout_intermediate(&self) -> bool { - match self { - Self::ExampleSize { .. } => true, - _ => false, - } - } -} - -impl BuildMode { - #[allow(clippy::wrong_self_convention)] - pub fn to_flag(&self) -> Option<&str> { - match self { - BuildMode::Release => Some("--release"), - BuildMode::Debug => None, - } - } -} - -impl fmt::Display for BuildMode { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let cmd = match self { - BuildMode::Release => "release", - BuildMode::Debug => "debug", - }; - - write!(f, "{cmd}") - } -} - -pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::Result { - log::info!("👟 {command}"); - - let mut process = Command::new(command.executable()); - - process - .args(command.args()) - .stdout(Stdio::piped()) - .stderr(stderr_mode); - - if let Some(dir) = command.chdir() { - process.current_dir(dir.canonicalize()?); - } - - let result = process.output()?; - - let exit_status = result.status; - let stderr = String::from_utf8(result.stderr).unwrap_or("Not displayable".into()); - let stdout = String::from_utf8(result.stdout).unwrap_or("Not displayable".into()); - - if command.print_stdout_intermediate() && exit_status.success() { - log::info!("\n{}", stdout); - } - - if exit_status.success() { - log::info!("✅ Success.") - } else { - log::error!("❌ Command failed. Run to completion for the summary."); - } - - Ok(RunResult { - exit_status, - stdout, - stderr, - }) -} - -/// Check if `run` was successful. -/// returns Ok in case the run went as expected, -/// Err otherwise -pub fn run_successful(run: &RunResult, expected_output_file: &str) -> Result<(), TestRunError> { - let mut file_handle = - File::open(expected_output_file).map_err(|_| TestRunError::FileError { - file: expected_output_file.to_owned(), - })?; - let mut expected_output = String::new(); - file_handle - .read_to_string(&mut expected_output) - .map_err(|_| TestRunError::FileError { - file: expected_output_file.to_owned(), - })?; - - if expected_output != run.stdout { - Err(TestRunError::FileCmpError { - expected: expected_output.clone(), - got: run.stdout.clone(), - }) - } else if !run.exit_status.success() { - Err(TestRunError::CommandError(run.clone())) - } else { - Ok(()) - } -} - -pub fn handle_results(globals: &Globals, results: Vec) -> Result<(), ()> { - let errors = results.iter().filter_map(|r| { - if let FinalRunResult::Failed(c, r) = r { - Some((c, &r.stdout, &r.stderr)) - } else { - None - } - }); - - let successes = results.iter().filter_map(|r| { - if let FinalRunResult::Success(c, r) = r { - Some((c, &r.stdout, &r.stderr)) - } else { - None - } - }); - - let command_errors = results.iter().filter_map(|r| { - if let FinalRunResult::CommandError(c, e) = r { - Some((c, e)) - } else { - None - } - }); - - let log_stdout_stderr = |level: Level| { - move |(cmd, stdout, stderr): (&CargoCommand, &String, &String)| { - let cmd = cmd.as_cmd_string(); - if !stdout.is_empty() && !stderr.is_empty() { - log::log!(level, "\n{cmd}\nStdout:\n{stdout}\nStderr:\n{stderr}"); - } else if !stdout.is_empty() { - log::log!(level, "\n{cmd}\nStdout:\n{}", stdout.trim_end()); - } else if !stderr.is_empty() { - log::log!(level, "\n{cmd}\nStderr:\n{}", stderr.trim_end()); - } - } - }; - - successes.for_each(|(cmd, stdout, stderr)| { - if globals.verbose > 0 { - info!("✅ Success: {cmd}\n {}", cmd.as_cmd_string()); - } else { - info!("✅ Success: {cmd}"); - } - - log_stdout_stderr(Level::Debug)((cmd, stdout, stderr)); - }); - - errors.clone().for_each(|(cmd, stdout, stderr)| { - error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string()); - log_stdout_stderr(Level::Error)((cmd, stdout, stderr)); - }); - - command_errors - .clone() - .for_each(|(cmd, error)| error!("❌ Failed: {cmd}\n {}\n{error}", cmd.as_cmd_string())); - - let ecount = errors.count() + command_errors.count(); - if ecount != 0 { - log::error!("{ecount} commands failed."); - Err(()) - } else { - info!("🚀🚀🚀 All tasks succeeded 🚀🚀🚀"); - Ok(()) - } -} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 2f35079..09bf8c7 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,11 +1,11 @@ mod argument_parsing; mod build; -mod cargo_commands; -mod command; +mod xtasks; +mod cargo_command; use argument_parsing::{ExtraArguments, Globals}; use clap::Parser; -use command::OutputMode; +use cargo_command::OutputMode; use core::fmt; use diffy::{create_patch, PatchFormatter}; use std::{ @@ -23,8 +23,8 @@ use log::{error, info, log_enabled, trace, Level}; use crate::{ argument_parsing::{Backends, BuildOrCheck, Cli, Commands}, build::init_build_dir, - cargo_commands::*, - command::{handle_results, run_command, run_successful, CargoCommand}, + xtasks::*, + cargo_command::{handle_results, run_command, run_successful, CargoCommand}, }; #[derive(Debug, Clone, Copy)] diff --git a/xtask/src/run.rs b/xtask/src/run.rs new file mode 100644 index 0000000..32fafc8 --- /dev/null +++ b/xtask/src/run.rs @@ -0,0 +1,402 @@ +use std::path::PathBuf; + +use crate::{ + argument_parsing::{Backends, BuildOrCheck, ExtraArguments, Globals, PackageOpt, TestMetadata}, + cargo_command::{BuildMode, CargoCommand}, + command_parser, RunResult, +}; +use log::error; + +#[cfg(feature = "rayon")] +use rayon::prelude::*; + +use iters::*; + +#[derive(Debug)] +pub enum FinalRunResult<'c> { + Success(CargoCommand<'c>, RunResult), + Failed(CargoCommand<'c>, RunResult), + CommandError(CargoCommand<'c>, anyhow::Error), +} + +fn run_and_convert<'a>( + (global, command, overwrite): (&Globals, CargoCommand<'a>, bool), +) -> FinalRunResult<'a> { + // Run the command + let result = command_parser(global, &command, overwrite); + + let output = match result { + // If running the command succeeded without looking at any of the results, + // log the data and see if the actual execution was succesfull too. + Ok(result) => { + if result.exit_status.success() { + FinalRunResult::Success(command, result) + } else { + FinalRunResult::Failed(command, result) + } + } + // If it didn't and some IO error occured, just panic + Err(e) => FinalRunResult::CommandError(command, e), + }; + + log::trace!("Final result: {output:?}"); + + output +} + +pub trait CoalescingRunner<'c> { + /// Run all the commands in this iterator, and coalesce the results into + /// one error (if any individual commands failed) + fn run_and_coalesce(self) -> Vec>; +} + +#[cfg(not(feature = "rayon"))] +mod iters { + use super::*; + + pub fn examples_iter(examples: &[String]) -> impl Iterator { + examples.into_iter() + } + + impl<'g, 'c, I> CoalescingRunner<'c> for I + where + I: Iterator, bool)>, + { + fn run_and_coalesce(self) -> Vec> { + self.map(run_and_convert).collect() + } + } +} + +#[cfg(feature = "rayon")] +mod iters { + use super::*; + + pub fn examples_iter(examples: &[String]) -> impl ParallelIterator { + examples.into_par_iter() + } + + impl<'g, 'c, I> CoalescingRunner<'c> for I + where + I: ParallelIterator, bool)>, + { + fn run_and_coalesce(self) -> Vec> { + self.map(run_and_convert).collect() + } + } +} + +/// Cargo command to either build or check +pub fn cargo<'c>( + globals: &Globals, + operation: BuildOrCheck, + cargoarg: &'c Option<&'c str>, + package: &'c PackageOpt, + backend: Backends, +) -> Vec> { + let runner = package + .packages() + .flat_map(|package| { + let target = backend.to_target(); + let features = package.features(target, backend, globals.partial); + + #[cfg(feature = "rayon")] + { + features.into_par_iter().map(move |f| (package, target, f)) + } + + #[cfg(not(feature = "rayon"))] + { + features.into_iter().map(move |f| (package, target, f)) + } + }) + .map(move |(package, target, features)| { + let target = target.into(); + let command = match operation { + BuildOrCheck::Check => CargoCommand::Check { + cargoarg, + package: Some(package.name()), + target, + features, + mode: BuildMode::Release, + dir: None, + }, + BuildOrCheck::Build => CargoCommand::Build { + cargoarg, + package: Some(package.name()), + target, + features, + mode: BuildMode::Release, + dir: None, + }, + }; + + (globals, command, false) + }); + + runner.run_and_coalesce() +} + +/// Cargo command to build a usage example. +/// +/// The usage examples are in examples/ +pub fn cargo_usage_example( + globals: &Globals, + operation: BuildOrCheck, + usage_examples: Vec, +) -> Vec> { + examples_iter(&usage_examples) + .map(|example| { + let path = format!("examples/{example}"); + + let command = match operation { + BuildOrCheck::Check => CargoCommand::Check { + cargoarg: &None, + mode: BuildMode::Release, + dir: Some(path.into()), + package: None, + target: None, + features: None, + }, + BuildOrCheck::Build => CargoCommand::Build { + cargoarg: &None, + package: None, + target: None, + features: None, + mode: BuildMode::Release, + dir: Some(path.into()), + }, + }; + (globals, command, false) + }) + .run_and_coalesce() +} + +/// Cargo command to either build or check all examples +/// +/// The examples are in rtic/examples +pub fn cargo_example<'c>( + globals: &Globals, + operation: BuildOrCheck, + cargoarg: &'c Option<&'c str>, + backend: Backends, + examples: &'c [String], +) -> Vec> { + let runner = examples_iter(examples).map(|example| { + let features = Some(backend.to_target().and_features(backend.to_rtic_feature())); + + let command = match operation { + BuildOrCheck::Check => CargoCommand::ExampleCheck { + cargoarg, + example, + target: Some(backend.to_target()), + features, + mode: BuildMode::Release, + }, + BuildOrCheck::Build => CargoCommand::ExampleBuild { + cargoarg, + example, + target: Some(backend.to_target()), + features, + mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), + }, + }; + (globals, command, false) + }); + runner.run_and_coalesce() +} + +/// Run cargo clippy on selected package +pub fn cargo_clippy<'c>( + globals: &Globals, + cargoarg: &'c Option<&'c str>, + package: &'c PackageOpt, + backend: Backends, +) -> Vec> { + let runner = package + .packages() + .flat_map(|package| { + let target = backend.to_target(); + let features = package.features(target, backend, globals.partial); + + #[cfg(feature = "rayon")] + { + features.into_par_iter().map(move |f| (package, target, f)) + } + + #[cfg(not(feature = "rayon"))] + { + features.into_iter().map(move |f| (package, target, f)) + } + }) + .map(move |(package, target, features)| { + let command = CargoCommand::Clippy { + cargoarg, + package: Some(package.name()), + target: target.into(), + features, + }; + + (globals, command, false) + }); + + runner.run_and_coalesce() +} + +/// Run cargo fmt on selected package +pub fn cargo_format<'c>( + globals: &Globals, + cargoarg: &'c Option<&'c str>, + package: &'c PackageOpt, + check_only: bool, +) -> Vec> { + let runner = package.packages().map(|p| { + ( + globals, + CargoCommand::Format { + cargoarg, + package: Some(p.name()), + check_only, + }, + false, + ) + }); + runner.run_and_coalesce() +} + +/// Run cargo doc +pub fn cargo_doc<'c>( + globals: &Globals, + cargoarg: &'c Option<&'c str>, + backend: Backends, + arguments: &'c Option, +) -> Vec> { + let features = Some(backend.to_target().and_features(backend.to_rtic_feature())); + + let command = CargoCommand::Doc { + cargoarg, + features, + arguments: arguments.clone(), + }; + + vec![run_and_convert((globals, command, false))] +} + +/// Run cargo test on the selected package or all packages +/// +/// If no package is specified, loop through all packages +pub fn cargo_test<'c>( + globals: &Globals, + package: &'c PackageOpt, + backend: Backends, +) -> Vec> { + package + .packages() + .map(|p| (globals, TestMetadata::match_package(p, backend), false)) + .run_and_coalesce() +} + +/// Use mdbook to build the book +pub fn cargo_book<'c>( + globals: &Globals, + arguments: &'c Option, +) -> Vec> { + vec![run_and_convert(( + globals, + CargoCommand::Book { + arguments: arguments.clone(), + }, + false, + ))] +} + +/// Run examples +/// +/// Supports updating the expected output via the overwrite argument +pub fn qemu_run_examples<'c>( + globals: &Globals, + cargoarg: &'c Option<&'c str>, + backend: Backends, + examples: &'c [String], + overwrite: bool, +) -> Vec> { + let target = backend.to_target(); + let features = Some(target.and_features(backend.to_rtic_feature())); + + examples_iter(examples) + .flat_map(|example| { + let target = target.into(); + let cmd_build = CargoCommand::ExampleBuild { + cargoarg: &None, + example, + target, + features: features.clone(), + mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), + }; + + let cmd_qemu = CargoCommand::Qemu { + cargoarg, + example, + target, + features: features.clone(), + mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), + }; + + #[cfg(not(feature = "rayon"))] + { + [cmd_build, cmd_qemu].into_iter() + } + + #[cfg(feature = "rayon")] + { + [cmd_build, cmd_qemu].into_par_iter() + } + }) + .map(|cmd| (globals, cmd, overwrite)) + .run_and_coalesce() +} + +/// Check the binary sizes of examples +pub fn build_and_check_size<'c>( + globals: &Globals, + cargoarg: &'c Option<&'c str>, + backend: Backends, + examples: &'c [String], + arguments: &'c Option, +) -> Vec> { + let target = backend.to_target(); + let features = Some(target.and_features(backend.to_rtic_feature())); + + let runner = examples_iter(examples).map(|example| { + let target = target.into(); + + // Make sure the requested example(s) are built + let cmd = CargoCommand::ExampleBuild { + cargoarg: &Some("--quiet"), + example, + target, + features: features.clone(), + mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), + }; + if let Err(err) = command_parser(globals, &cmd, false) { + error!("{err}"); + } + + let cmd = CargoCommand::ExampleSize { + cargoarg, + example, + target, + features: features.clone(), + mode: BuildMode::Release, + arguments: arguments.clone(), + dir: Some(PathBuf::from("./rtic")), + }; + (globals, cmd, false) + }); + + runner.run_and_coalesce() +} -- cgit v1.2.3 From c6b43800d2a368d7948639899b8dca50cc97712f Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 16 Apr 2023 11:05:41 +0200 Subject: Move all run-related stuff into `run` --- xtask/src/cargo_command.rs | 162 +-------------------------------------------- xtask/src/main.rs | 7 +- xtask/src/run.rs | 157 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 160 insertions(+), 166 deletions(-) (limited to 'xtask') diff --git a/xtask/src/cargo_command.rs b/xtask/src/cargo_command.rs index cd38566..f399b87 100644 --- a/xtask/src/cargo_command.rs +++ b/xtask/src/cargo_command.rs @@ -1,16 +1,6 @@ -use log::{error, info, Level}; - -use crate::{ - argument_parsing::Globals, xtasks::FinalRunResult, ExtraArguments, RunResult, Target, - TestRunError, -}; +use crate::{ExtraArguments, Target}; use core::fmt; -use std::{ - fs::File, - io::Read, - path::PathBuf, - process::{Command, Stdio}, -}; +use std::path::PathBuf; #[allow(dead_code)] #[derive(Debug, Clone, Copy, PartialEq)] @@ -19,21 +9,6 @@ pub enum BuildMode { Debug, } -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum OutputMode { - PipedAndCollected, - Inherited, -} - -impl From for Stdio { - fn from(value: OutputMode) -> Self { - match value { - OutputMode::PipedAndCollected => Stdio::piped(), - OutputMode::Inherited => Stdio::inherit(), - } - } -} - #[derive(Debug)] pub enum CargoCommand<'a> { // For future embedded-ci @@ -614,7 +589,7 @@ impl<'a> CargoCommand<'a> { } /// TODO: integrate this into `args` once `-C` becomes stable. - fn chdir(&self) -> Option<&PathBuf> { + pub fn chdir(&self) -> Option<&PathBuf> { match self { CargoCommand::Qemu { dir, .. } | CargoCommand::ExampleBuild { dir, .. } @@ -668,134 +643,3 @@ impl fmt::Display for BuildMode { write!(f, "{cmd}") } } - -pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::Result { - log::info!("👟 {command}"); - - let mut process = Command::new(command.executable()); - - process - .args(command.args()) - .stdout(Stdio::piped()) - .stderr(stderr_mode); - - if let Some(dir) = command.chdir() { - process.current_dir(dir.canonicalize()?); - } - - let result = process.output()?; - - let exit_status = result.status; - let stderr = String::from_utf8(result.stderr).unwrap_or("Not displayable".into()); - let stdout = String::from_utf8(result.stdout).unwrap_or("Not displayable".into()); - - if command.print_stdout_intermediate() && exit_status.success() { - log::info!("\n{}", stdout); - } - - if exit_status.success() { - log::info!("✅ Success.") - } else { - log::error!("❌ Command failed. Run to completion for the summary."); - } - - Ok(RunResult { - exit_status, - stdout, - stderr, - }) -} - -/// Check if `run` was successful. -/// returns Ok in case the run went as expected, -/// Err otherwise -pub fn run_successful(run: &RunResult, expected_output_file: &str) -> Result<(), TestRunError> { - let mut file_handle = - File::open(expected_output_file).map_err(|_| TestRunError::FileError { - file: expected_output_file.to_owned(), - })?; - let mut expected_output = String::new(); - file_handle - .read_to_string(&mut expected_output) - .map_err(|_| TestRunError::FileError { - file: expected_output_file.to_owned(), - })?; - - if expected_output != run.stdout { - Err(TestRunError::FileCmpError { - expected: expected_output.clone(), - got: run.stdout.clone(), - }) - } else if !run.exit_status.success() { - Err(TestRunError::CommandError(run.clone())) - } else { - Ok(()) - } -} - -pub fn handle_results(globals: &Globals, results: Vec) -> Result<(), ()> { - let errors = results.iter().filter_map(|r| { - if let FinalRunResult::Failed(c, r) = r { - Some((c, &r.stdout, &r.stderr)) - } else { - None - } - }); - - let successes = results.iter().filter_map(|r| { - if let FinalRunResult::Success(c, r) = r { - Some((c, &r.stdout, &r.stderr)) - } else { - None - } - }); - - let command_errors = results.iter().filter_map(|r| { - if let FinalRunResult::CommandError(c, e) = r { - Some((c, e)) - } else { - None - } - }); - - let log_stdout_stderr = |level: Level| { - move |(cmd, stdout, stderr): (&CargoCommand, &String, &String)| { - let cmd = cmd.as_cmd_string(); - if !stdout.is_empty() && !stderr.is_empty() { - log::log!(level, "\n{cmd}\nStdout:\n{stdout}\nStderr:\n{stderr}"); - } else if !stdout.is_empty() { - log::log!(level, "\n{cmd}\nStdout:\n{}", stdout.trim_end()); - } else if !stderr.is_empty() { - log::log!(level, "\n{cmd}\nStderr:\n{}", stderr.trim_end()); - } - } - }; - - successes.for_each(|(cmd, stdout, stderr)| { - if globals.verbose > 0 { - info!("✅ Success: {cmd}\n {}", cmd.as_cmd_string()); - } else { - info!("✅ Success: {cmd}"); - } - - log_stdout_stderr(Level::Debug)((cmd, stdout, stderr)); - }); - - errors.clone().for_each(|(cmd, stdout, stderr)| { - error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string()); - log_stdout_stderr(Level::Error)((cmd, stdout, stderr)); - }); - - command_errors - .clone() - .for_each(|(cmd, error)| error!("❌ Failed: {cmd}\n {}\n{error}", cmd.as_cmd_string())); - - let ecount = errors.count() + command_errors.count(); - if ecount != 0 { - log::error!("{ecount} commands failed."); - Err(()) - } else { - info!("🚀🚀🚀 All tasks succeeded 🚀🚀🚀"); - Ok(()) - } -} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 09bf8c7..1712d71 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,11 +1,10 @@ mod argument_parsing; mod build; -mod xtasks; mod cargo_command; +mod run; use argument_parsing::{ExtraArguments, Globals}; use clap::Parser; -use cargo_command::OutputMode; use core::fmt; use diffy::{create_patch, PatchFormatter}; use std::{ @@ -23,8 +22,8 @@ use log::{error, info, log_enabled, trace, Level}; use crate::{ argument_parsing::{Backends, BuildOrCheck, Cli, Commands}, build::init_build_dir, - xtasks::*, - cargo_command::{handle_results, run_command, run_successful, CargoCommand}, + cargo_command::CargoCommand, + run::*, }; #[derive(Debug, Clone, Copy)] diff --git a/xtask/src/run.rs b/xtask/src/run.rs index 32fafc8..49437d7 100644 --- a/xtask/src/run.rs +++ b/xtask/src/run.rs @@ -1,17 +1,37 @@ -use std::path::PathBuf; +use std::{ + fs::File, + io::Read, + path::PathBuf, + process::{Command, Stdio}, +}; use crate::{ argument_parsing::{Backends, BuildOrCheck, ExtraArguments, Globals, PackageOpt, TestMetadata}, cargo_command::{BuildMode, CargoCommand}, - command_parser, RunResult, + command_parser, RunResult, TestRunError, }; -use log::error; +use log::{error, info, Level}; #[cfg(feature = "rayon")] use rayon::prelude::*; use iters::*; +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum OutputMode { + PipedAndCollected, + Inherited, +} + +impl From for Stdio { + fn from(value: OutputMode) -> Self { + match value { + OutputMode::PipedAndCollected => Stdio::piped(), + OutputMode::Inherited => Stdio::inherit(), + } + } +} + #[derive(Debug)] pub enum FinalRunResult<'c> { Success(CargoCommand<'c>, RunResult), @@ -400,3 +420,134 @@ pub fn build_and_check_size<'c>( runner.run_and_coalesce() } + +pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::Result { + log::info!("👟 {command}"); + + let mut process = Command::new(command.executable()); + + process + .args(command.args()) + .stdout(Stdio::piped()) + .stderr(stderr_mode); + + if let Some(dir) = command.chdir() { + process.current_dir(dir.canonicalize()?); + } + + let result = process.output()?; + + let exit_status = result.status; + let stderr = String::from_utf8(result.stderr).unwrap_or("Not displayable".into()); + let stdout = String::from_utf8(result.stdout).unwrap_or("Not displayable".into()); + + if command.print_stdout_intermediate() && exit_status.success() { + log::info!("\n{}", stdout); + } + + if exit_status.success() { + log::info!("✅ Success.") + } else { + log::error!("❌ Command failed. Run to completion for the summary."); + } + + Ok(RunResult { + exit_status, + stdout, + stderr, + }) +} + +/// Check if `run` was successful. +/// returns Ok in case the run went as expected, +/// Err otherwise +pub fn run_successful(run: &RunResult, expected_output_file: &str) -> Result<(), TestRunError> { + let mut file_handle = + File::open(expected_output_file).map_err(|_| TestRunError::FileError { + file: expected_output_file.to_owned(), + })?; + let mut expected_output = String::new(); + file_handle + .read_to_string(&mut expected_output) + .map_err(|_| TestRunError::FileError { + file: expected_output_file.to_owned(), + })?; + + if expected_output != run.stdout { + Err(TestRunError::FileCmpError { + expected: expected_output.clone(), + got: run.stdout.clone(), + }) + } else if !run.exit_status.success() { + Err(TestRunError::CommandError(run.clone())) + } else { + Ok(()) + } +} + +pub fn handle_results(globals: &Globals, results: Vec) -> Result<(), ()> { + let errors = results.iter().filter_map(|r| { + if let FinalRunResult::Failed(c, r) = r { + Some((c, &r.stdout, &r.stderr)) + } else { + None + } + }); + + let successes = results.iter().filter_map(|r| { + if let FinalRunResult::Success(c, r) = r { + Some((c, &r.stdout, &r.stderr)) + } else { + None + } + }); + + let command_errors = results.iter().filter_map(|r| { + if let FinalRunResult::CommandError(c, e) = r { + Some((c, e)) + } else { + None + } + }); + + let log_stdout_stderr = |level: Level| { + move |(cmd, stdout, stderr): (&CargoCommand, &String, &String)| { + let cmd = cmd.as_cmd_string(); + if !stdout.is_empty() && !stderr.is_empty() { + log::log!(level, "\n{cmd}\nStdout:\n{stdout}\nStderr:\n{stderr}"); + } else if !stdout.is_empty() { + log::log!(level, "\n{cmd}\nStdout:\n{}", stdout.trim_end()); + } else if !stderr.is_empty() { + log::log!(level, "\n{cmd}\nStderr:\n{}", stderr.trim_end()); + } + } + }; + + successes.for_each(|(cmd, stdout, stderr)| { + if globals.verbose > 0 { + info!("✅ Success: {cmd}\n {}", cmd.as_cmd_string()); + } else { + info!("✅ Success: {cmd}"); + } + + log_stdout_stderr(Level::Debug)((cmd, stdout, stderr)); + }); + + errors.clone().for_each(|(cmd, stdout, stderr)| { + error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string()); + log_stdout_stderr(Level::Error)((cmd, stdout, stderr)); + }); + + command_errors + .clone() + .for_each(|(cmd, error)| error!("❌ Failed: {cmd}\n {}\n{error}", cmd.as_cmd_string())); + + let ecount = errors.count() + command_errors.count(); + if ecount != 0 { + log::error!("{ecount} commands failed."); + Err(()) + } else { + info!("🚀🚀🚀 All tasks succeeded 🚀🚀🚀"); + Ok(()) + } +} -- cgit v1.2.3 From b87d55f960178971652df95257ddd1ea4f39c6c0 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 16 Apr 2023 11:12:44 +0200 Subject: Move run into a subdirectory and split `iter` stuff into a module --- xtask/src/run.rs | 553 -------------------------------------------------- xtask/src/run/iter.rs | 48 +++++ xtask/src/run/mod.rs | 487 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 535 insertions(+), 553 deletions(-) delete mode 100644 xtask/src/run.rs create mode 100644 xtask/src/run/iter.rs create mode 100644 xtask/src/run/mod.rs (limited to 'xtask') diff --git a/xtask/src/run.rs b/xtask/src/run.rs deleted file mode 100644 index 49437d7..0000000 --- a/xtask/src/run.rs +++ /dev/null @@ -1,553 +0,0 @@ -use std::{ - fs::File, - io::Read, - path::PathBuf, - process::{Command, Stdio}, -}; - -use crate::{ - argument_parsing::{Backends, BuildOrCheck, ExtraArguments, Globals, PackageOpt, TestMetadata}, - cargo_command::{BuildMode, CargoCommand}, - command_parser, RunResult, TestRunError, -}; -use log::{error, info, Level}; - -#[cfg(feature = "rayon")] -use rayon::prelude::*; - -use iters::*; - -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum OutputMode { - PipedAndCollected, - Inherited, -} - -impl From for Stdio { - fn from(value: OutputMode) -> Self { - match value { - OutputMode::PipedAndCollected => Stdio::piped(), - OutputMode::Inherited => Stdio::inherit(), - } - } -} - -#[derive(Debug)] -pub enum FinalRunResult<'c> { - Success(CargoCommand<'c>, RunResult), - Failed(CargoCommand<'c>, RunResult), - CommandError(CargoCommand<'c>, anyhow::Error), -} - -fn run_and_convert<'a>( - (global, command, overwrite): (&Globals, CargoCommand<'a>, bool), -) -> FinalRunResult<'a> { - // Run the command - let result = command_parser(global, &command, overwrite); - - let output = match result { - // If running the command succeeded without looking at any of the results, - // log the data and see if the actual execution was succesfull too. - Ok(result) => { - if result.exit_status.success() { - FinalRunResult::Success(command, result) - } else { - FinalRunResult::Failed(command, result) - } - } - // If it didn't and some IO error occured, just panic - Err(e) => FinalRunResult::CommandError(command, e), - }; - - log::trace!("Final result: {output:?}"); - - output -} - -pub trait CoalescingRunner<'c> { - /// Run all the commands in this iterator, and coalesce the results into - /// one error (if any individual commands failed) - fn run_and_coalesce(self) -> Vec>; -} - -#[cfg(not(feature = "rayon"))] -mod iters { - use super::*; - - pub fn examples_iter(examples: &[String]) -> impl Iterator { - examples.into_iter() - } - - impl<'g, 'c, I> CoalescingRunner<'c> for I - where - I: Iterator, bool)>, - { - fn run_and_coalesce(self) -> Vec> { - self.map(run_and_convert).collect() - } - } -} - -#[cfg(feature = "rayon")] -mod iters { - use super::*; - - pub fn examples_iter(examples: &[String]) -> impl ParallelIterator { - examples.into_par_iter() - } - - impl<'g, 'c, I> CoalescingRunner<'c> for I - where - I: ParallelIterator, bool)>, - { - fn run_and_coalesce(self) -> Vec> { - self.map(run_and_convert).collect() - } - } -} - -/// Cargo command to either build or check -pub fn cargo<'c>( - globals: &Globals, - operation: BuildOrCheck, - cargoarg: &'c Option<&'c str>, - package: &'c PackageOpt, - backend: Backends, -) -> Vec> { - let runner = package - .packages() - .flat_map(|package| { - let target = backend.to_target(); - let features = package.features(target, backend, globals.partial); - - #[cfg(feature = "rayon")] - { - features.into_par_iter().map(move |f| (package, target, f)) - } - - #[cfg(not(feature = "rayon"))] - { - features.into_iter().map(move |f| (package, target, f)) - } - }) - .map(move |(package, target, features)| { - let target = target.into(); - let command = match operation { - BuildOrCheck::Check => CargoCommand::Check { - cargoarg, - package: Some(package.name()), - target, - features, - mode: BuildMode::Release, - dir: None, - }, - BuildOrCheck::Build => CargoCommand::Build { - cargoarg, - package: Some(package.name()), - target, - features, - mode: BuildMode::Release, - dir: None, - }, - }; - - (globals, command, false) - }); - - runner.run_and_coalesce() -} - -/// Cargo command to build a usage example. -/// -/// The usage examples are in examples/ -pub fn cargo_usage_example( - globals: &Globals, - operation: BuildOrCheck, - usage_examples: Vec, -) -> Vec> { - examples_iter(&usage_examples) - .map(|example| { - let path = format!("examples/{example}"); - - let command = match operation { - BuildOrCheck::Check => CargoCommand::Check { - cargoarg: &None, - mode: BuildMode::Release, - dir: Some(path.into()), - package: None, - target: None, - features: None, - }, - BuildOrCheck::Build => CargoCommand::Build { - cargoarg: &None, - package: None, - target: None, - features: None, - mode: BuildMode::Release, - dir: Some(path.into()), - }, - }; - (globals, command, false) - }) - .run_and_coalesce() -} - -/// Cargo command to either build or check all examples -/// -/// The examples are in rtic/examples -pub fn cargo_example<'c>( - globals: &Globals, - operation: BuildOrCheck, - cargoarg: &'c Option<&'c str>, - backend: Backends, - examples: &'c [String], -) -> Vec> { - let runner = examples_iter(examples).map(|example| { - let features = Some(backend.to_target().and_features(backend.to_rtic_feature())); - - let command = match operation { - BuildOrCheck::Check => CargoCommand::ExampleCheck { - cargoarg, - example, - target: Some(backend.to_target()), - features, - mode: BuildMode::Release, - }, - BuildOrCheck::Build => CargoCommand::ExampleBuild { - cargoarg, - example, - target: Some(backend.to_target()), - features, - mode: BuildMode::Release, - dir: Some(PathBuf::from("./rtic")), - }, - }; - (globals, command, false) - }); - runner.run_and_coalesce() -} - -/// Run cargo clippy on selected package -pub fn cargo_clippy<'c>( - globals: &Globals, - cargoarg: &'c Option<&'c str>, - package: &'c PackageOpt, - backend: Backends, -) -> Vec> { - let runner = package - .packages() - .flat_map(|package| { - let target = backend.to_target(); - let features = package.features(target, backend, globals.partial); - - #[cfg(feature = "rayon")] - { - features.into_par_iter().map(move |f| (package, target, f)) - } - - #[cfg(not(feature = "rayon"))] - { - features.into_iter().map(move |f| (package, target, f)) - } - }) - .map(move |(package, target, features)| { - let command = CargoCommand::Clippy { - cargoarg, - package: Some(package.name()), - target: target.into(), - features, - }; - - (globals, command, false) - }); - - runner.run_and_coalesce() -} - -/// Run cargo fmt on selected package -pub fn cargo_format<'c>( - globals: &Globals, - cargoarg: &'c Option<&'c str>, - package: &'c PackageOpt, - check_only: bool, -) -> Vec> { - let runner = package.packages().map(|p| { - ( - globals, - CargoCommand::Format { - cargoarg, - package: Some(p.name()), - check_only, - }, - false, - ) - }); - runner.run_and_coalesce() -} - -/// Run cargo doc -pub fn cargo_doc<'c>( - globals: &Globals, - cargoarg: &'c Option<&'c str>, - backend: Backends, - arguments: &'c Option, -) -> Vec> { - let features = Some(backend.to_target().and_features(backend.to_rtic_feature())); - - let command = CargoCommand::Doc { - cargoarg, - features, - arguments: arguments.clone(), - }; - - vec![run_and_convert((globals, command, false))] -} - -/// Run cargo test on the selected package or all packages -/// -/// If no package is specified, loop through all packages -pub fn cargo_test<'c>( - globals: &Globals, - package: &'c PackageOpt, - backend: Backends, -) -> Vec> { - package - .packages() - .map(|p| (globals, TestMetadata::match_package(p, backend), false)) - .run_and_coalesce() -} - -/// Use mdbook to build the book -pub fn cargo_book<'c>( - globals: &Globals, - arguments: &'c Option, -) -> Vec> { - vec![run_and_convert(( - globals, - CargoCommand::Book { - arguments: arguments.clone(), - }, - false, - ))] -} - -/// Run examples -/// -/// Supports updating the expected output via the overwrite argument -pub fn qemu_run_examples<'c>( - globals: &Globals, - cargoarg: &'c Option<&'c str>, - backend: Backends, - examples: &'c [String], - overwrite: bool, -) -> Vec> { - let target = backend.to_target(); - let features = Some(target.and_features(backend.to_rtic_feature())); - - examples_iter(examples) - .flat_map(|example| { - let target = target.into(); - let cmd_build = CargoCommand::ExampleBuild { - cargoarg: &None, - example, - target, - features: features.clone(), - mode: BuildMode::Release, - dir: Some(PathBuf::from("./rtic")), - }; - - let cmd_qemu = CargoCommand::Qemu { - cargoarg, - example, - target, - features: features.clone(), - mode: BuildMode::Release, - dir: Some(PathBuf::from("./rtic")), - }; - - #[cfg(not(feature = "rayon"))] - { - [cmd_build, cmd_qemu].into_iter() - } - - #[cfg(feature = "rayon")] - { - [cmd_build, cmd_qemu].into_par_iter() - } - }) - .map(|cmd| (globals, cmd, overwrite)) - .run_and_coalesce() -} - -/// Check the binary sizes of examples -pub fn build_and_check_size<'c>( - globals: &Globals, - cargoarg: &'c Option<&'c str>, - backend: Backends, - examples: &'c [String], - arguments: &'c Option, -) -> Vec> { - let target = backend.to_target(); - let features = Some(target.and_features(backend.to_rtic_feature())); - - let runner = examples_iter(examples).map(|example| { - let target = target.into(); - - // Make sure the requested example(s) are built - let cmd = CargoCommand::ExampleBuild { - cargoarg: &Some("--quiet"), - example, - target, - features: features.clone(), - mode: BuildMode::Release, - dir: Some(PathBuf::from("./rtic")), - }; - if let Err(err) = command_parser(globals, &cmd, false) { - error!("{err}"); - } - - let cmd = CargoCommand::ExampleSize { - cargoarg, - example, - target, - features: features.clone(), - mode: BuildMode::Release, - arguments: arguments.clone(), - dir: Some(PathBuf::from("./rtic")), - }; - (globals, cmd, false) - }); - - runner.run_and_coalesce() -} - -pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::Result { - log::info!("👟 {command}"); - - let mut process = Command::new(command.executable()); - - process - .args(command.args()) - .stdout(Stdio::piped()) - .stderr(stderr_mode); - - if let Some(dir) = command.chdir() { - process.current_dir(dir.canonicalize()?); - } - - let result = process.output()?; - - let exit_status = result.status; - let stderr = String::from_utf8(result.stderr).unwrap_or("Not displayable".into()); - let stdout = String::from_utf8(result.stdout).unwrap_or("Not displayable".into()); - - if command.print_stdout_intermediate() && exit_status.success() { - log::info!("\n{}", stdout); - } - - if exit_status.success() { - log::info!("✅ Success.") - } else { - log::error!("❌ Command failed. Run to completion for the summary."); - } - - Ok(RunResult { - exit_status, - stdout, - stderr, - }) -} - -/// Check if `run` was successful. -/// returns Ok in case the run went as expected, -/// Err otherwise -pub fn run_successful(run: &RunResult, expected_output_file: &str) -> Result<(), TestRunError> { - let mut file_handle = - File::open(expected_output_file).map_err(|_| TestRunError::FileError { - file: expected_output_file.to_owned(), - })?; - let mut expected_output = String::new(); - file_handle - .read_to_string(&mut expected_output) - .map_err(|_| TestRunError::FileError { - file: expected_output_file.to_owned(), - })?; - - if expected_output != run.stdout { - Err(TestRunError::FileCmpError { - expected: expected_output.clone(), - got: run.stdout.clone(), - }) - } else if !run.exit_status.success() { - Err(TestRunError::CommandError(run.clone())) - } else { - Ok(()) - } -} - -pub fn handle_results(globals: &Globals, results: Vec) -> Result<(), ()> { - let errors = results.iter().filter_map(|r| { - if let FinalRunResult::Failed(c, r) = r { - Some((c, &r.stdout, &r.stderr)) - } else { - None - } - }); - - let successes = results.iter().filter_map(|r| { - if let FinalRunResult::Success(c, r) = r { - Some((c, &r.stdout, &r.stderr)) - } else { - None - } - }); - - let command_errors = results.iter().filter_map(|r| { - if let FinalRunResult::CommandError(c, e) = r { - Some((c, e)) - } else { - None - } - }); - - let log_stdout_stderr = |level: Level| { - move |(cmd, stdout, stderr): (&CargoCommand, &String, &String)| { - let cmd = cmd.as_cmd_string(); - if !stdout.is_empty() && !stderr.is_empty() { - log::log!(level, "\n{cmd}\nStdout:\n{stdout}\nStderr:\n{stderr}"); - } else if !stdout.is_empty() { - log::log!(level, "\n{cmd}\nStdout:\n{}", stdout.trim_end()); - } else if !stderr.is_empty() { - log::log!(level, "\n{cmd}\nStderr:\n{}", stderr.trim_end()); - } - } - }; - - successes.for_each(|(cmd, stdout, stderr)| { - if globals.verbose > 0 { - info!("✅ Success: {cmd}\n {}", cmd.as_cmd_string()); - } else { - info!("✅ Success: {cmd}"); - } - - log_stdout_stderr(Level::Debug)((cmd, stdout, stderr)); - }); - - errors.clone().for_each(|(cmd, stdout, stderr)| { - error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string()); - log_stdout_stderr(Level::Error)((cmd, stdout, stderr)); - }); - - command_errors - .clone() - .for_each(|(cmd, error)| error!("❌ Failed: {cmd}\n {}\n{error}", cmd.as_cmd_string())); - - let ecount = errors.count() + command_errors.count(); - if ecount != 0 { - log::error!("{ecount} commands failed."); - Err(()) - } else { - info!("🚀🚀🚀 All tasks succeeded 🚀🚀🚀"); - Ok(()) - } -} diff --git a/xtask/src/run/iter.rs b/xtask/src/run/iter.rs new file mode 100644 index 0000000..d18ad49 --- /dev/null +++ b/xtask/src/run/iter.rs @@ -0,0 +1,48 @@ +use super::FinalRunResult; + +pub use iter::*; + +pub trait CoalescingRunner<'c> { + /// Run all the commands in this iterator, and coalesce the results into + /// one error (if any individual commands failed) + fn run_and_coalesce(self) -> Vec>; +} + +#[cfg(not(feature = "rayon"))] +mod iter { + use super::*; + use crate::{argument_parsing::Globals, cargo_command::*, run::run_and_convert}; + + pub fn into_iter(var: T) -> impl Iterator { + var.into_iter() + } + + impl<'g, 'c, I> CoalescingRunner<'c> for I + where + I: Iterator, bool)>, + { + fn run_and_coalesce(self) -> Vec> { + self.map(run_and_convert).collect() + } + } +} + +#[cfg(feature = "rayon")] +mod iter { + use super::*; + use crate::{argument_parsing::Globals, cargo_command::*, run::run_and_convert}; + use rayon::prelude::*; + + pub fn into_iter(var: T) -> impl ParallelIterator { + var.into_par_iter() + } + + impl<'g, 'c, I> CoalescingRunner<'c> for I + where + I: ParallelIterator, bool)>, + { + fn run_and_coalesce(self) -> Vec> { + self.map(run_and_convert).collect() + } + } +} diff --git a/xtask/src/run/mod.rs b/xtask/src/run/mod.rs new file mode 100644 index 0000000..daa6256 --- /dev/null +++ b/xtask/src/run/mod.rs @@ -0,0 +1,487 @@ +use std::{ + fs::File, + io::Read, + path::PathBuf, + process::{Command, Stdio}, +}; + +mod iter; +use iter::{into_iter, CoalescingRunner}; + +use crate::{ + argument_parsing::{Backends, BuildOrCheck, ExtraArguments, Globals, PackageOpt, TestMetadata}, + cargo_command::{BuildMode, CargoCommand}, + command_parser, RunResult, TestRunError, +}; + +use log::{error, info, Level}; + +#[cfg(feature = "rayon")] +use rayon::prelude::*; + +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum OutputMode { + PipedAndCollected, + Inherited, +} + +impl From for Stdio { + fn from(value: OutputMode) -> Self { + match value { + OutputMode::PipedAndCollected => Stdio::piped(), + OutputMode::Inherited => Stdio::inherit(), + } + } +} + +#[derive(Debug)] +pub enum FinalRunResult<'c> { + Success(CargoCommand<'c>, RunResult), + Failed(CargoCommand<'c>, RunResult), + CommandError(CargoCommand<'c>, anyhow::Error), +} + +fn run_and_convert<'a>( + (global, command, overwrite): (&Globals, CargoCommand<'a>, bool), +) -> FinalRunResult<'a> { + // Run the command + let result = command_parser(global, &command, overwrite); + + let output = match result { + // If running the command succeeded without looking at any of the results, + // log the data and see if the actual execution was succesfull too. + Ok(result) => { + if result.exit_status.success() { + FinalRunResult::Success(command, result) + } else { + FinalRunResult::Failed(command, result) + } + } + // If it didn't and some IO error occured, just panic + Err(e) => FinalRunResult::CommandError(command, e), + }; + + log::trace!("Final result: {output:?}"); + + output +} + +/// Cargo command to either build or check +pub fn cargo<'c>( + globals: &Globals, + operation: BuildOrCheck, + cargoarg: &'c Option<&'c str>, + package: &'c PackageOpt, + backend: Backends, +) -> Vec> { + let runner = package + .packages() + .flat_map(|package| { + let target = backend.to_target(); + let features = package.features(target, backend, globals.partial); + into_iter(features).map(move |f| (package, target, f)) + }) + .map(move |(package, target, features)| { + let target = target.into(); + let command = match operation { + BuildOrCheck::Check => CargoCommand::Check { + cargoarg, + package: Some(package.name()), + target, + features, + mode: BuildMode::Release, + dir: None, + }, + BuildOrCheck::Build => CargoCommand::Build { + cargoarg, + package: Some(package.name()), + target, + features, + mode: BuildMode::Release, + dir: None, + }, + }; + + (globals, command, false) + }); + + runner.run_and_coalesce() +} + +/// Cargo command to build a usage example. +/// +/// The usage examples are in examples/ +pub fn cargo_usage_example( + globals: &Globals, + operation: BuildOrCheck, + usage_examples: Vec, +) -> Vec> { + into_iter(&usage_examples) + .map(|example| { + let path = format!("examples/{example}"); + + let command = match operation { + BuildOrCheck::Check => CargoCommand::Check { + cargoarg: &None, + mode: BuildMode::Release, + dir: Some(path.into()), + package: None, + target: None, + features: None, + }, + BuildOrCheck::Build => CargoCommand::Build { + cargoarg: &None, + package: None, + target: None, + features: None, + mode: BuildMode::Release, + dir: Some(path.into()), + }, + }; + (globals, command, false) + }) + .run_and_coalesce() +} + +/// Cargo command to either build or check all examples +/// +/// The examples are in rtic/examples +pub fn cargo_example<'c>( + globals: &Globals, + operation: BuildOrCheck, + cargoarg: &'c Option<&'c str>, + backend: Backends, + examples: &'c [String], +) -> Vec> { + let runner = into_iter(examples).map(|example| { + let features = Some(backend.to_target().and_features(backend.to_rtic_feature())); + + let command = match operation { + BuildOrCheck::Check => CargoCommand::ExampleCheck { + cargoarg, + example, + target: Some(backend.to_target()), + features, + mode: BuildMode::Release, + }, + BuildOrCheck::Build => CargoCommand::ExampleBuild { + cargoarg, + example, + target: Some(backend.to_target()), + features, + mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), + }, + }; + (globals, command, false) + }); + runner.run_and_coalesce() +} + +/// Run cargo clippy on selected package +pub fn cargo_clippy<'c>( + globals: &Globals, + cargoarg: &'c Option<&'c str>, + package: &'c PackageOpt, + backend: Backends, +) -> Vec> { + let runner = package + .packages() + .flat_map(|package| { + let target = backend.to_target(); + let features = package.features(target, backend, globals.partial); + into_iter(features).map(move |f| (package, target, f)) + }) + .map(move |(package, target, features)| { + let command = CargoCommand::Clippy { + cargoarg, + package: Some(package.name()), + target: target.into(), + features, + }; + + (globals, command, false) + }); + + runner.run_and_coalesce() +} + +/// Run cargo fmt on selected package +pub fn cargo_format<'c>( + globals: &Globals, + cargoarg: &'c Option<&'c str>, + package: &'c PackageOpt, + check_only: bool, +) -> Vec> { + let runner = package.packages().map(|p| { + ( + globals, + CargoCommand::Format { + cargoarg, + package: Some(p.name()), + check_only, + }, + false, + ) + }); + runner.run_and_coalesce() +} + +/// Run cargo doc +pub fn cargo_doc<'c>( + globals: &Globals, + cargoarg: &'c Option<&'c str>, + backend: Backends, + arguments: &'c Option, +) -> Vec> { + let features = Some(backend.to_target().and_features(backend.to_rtic_feature())); + + let command = CargoCommand::Doc { + cargoarg, + features, + arguments: arguments.clone(), + }; + + vec![run_and_convert((globals, command, false))] +} + +/// Run cargo test on the selected package or all packages +/// +/// If no package is specified, loop through all packages +pub fn cargo_test<'c>( + globals: &Globals, + package: &'c PackageOpt, + backend: Backends, +) -> Vec> { + package + .packages() + .map(|p| (globals, TestMetadata::match_package(p, backend), false)) + .run_and_coalesce() +} + +/// Use mdbook to build the book +pub fn cargo_book<'c>( + globals: &Globals, + arguments: &'c Option, +) -> Vec> { + vec![run_and_convert(( + globals, + CargoCommand::Book { + arguments: arguments.clone(), + }, + false, + ))] +} + +/// Run examples +/// +/// Supports updating the expected output via the overwrite argument +pub fn qemu_run_examples<'c>( + globals: &Globals, + cargoarg: &'c Option<&'c str>, + backend: Backends, + examples: &'c [String], + overwrite: bool, +) -> Vec> { + let target = backend.to_target(); + let features = Some(target.and_features(backend.to_rtic_feature())); + + into_iter(examples) + .flat_map(|example| { + let target = target.into(); + let cmd_build = CargoCommand::ExampleBuild { + cargoarg: &None, + example, + target, + features: features.clone(), + mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), + }; + + let cmd_qemu = CargoCommand::Qemu { + cargoarg, + example, + target, + features: features.clone(), + mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), + }; + + into_iter([cmd_build, cmd_qemu]) + }) + .map(|cmd| (globals, cmd, overwrite)) + .run_and_coalesce() +} + +/// Check the binary sizes of examples +pub fn build_and_check_size<'c>( + globals: &Globals, + cargoarg: &'c Option<&'c str>, + backend: Backends, + examples: &'c [String], + arguments: &'c Option, +) -> Vec> { + let target = backend.to_target(); + let features = Some(target.and_features(backend.to_rtic_feature())); + + let runner = into_iter(examples).map(|example| { + let target = target.into(); + + // Make sure the requested example(s) are built + let cmd = CargoCommand::ExampleBuild { + cargoarg: &Some("--quiet"), + example, + target, + features: features.clone(), + mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), + }; + if let Err(err) = command_parser(globals, &cmd, false) { + error!("{err}"); + } + + let cmd = CargoCommand::ExampleSize { + cargoarg, + example, + target, + features: features.clone(), + mode: BuildMode::Release, + arguments: arguments.clone(), + dir: Some(PathBuf::from("./rtic")), + }; + (globals, cmd, false) + }); + + runner.run_and_coalesce() +} + +pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::Result { + log::info!("👟 {command}"); + + let mut process = Command::new(command.executable()); + + process + .args(command.args()) + .stdout(Stdio::piped()) + .stderr(stderr_mode); + + if let Some(dir) = command.chdir() { + process.current_dir(dir.canonicalize()?); + } + + let result = process.output()?; + + let exit_status = result.status; + let stderr = String::from_utf8(result.stderr).unwrap_or("Not displayable".into()); + let stdout = String::from_utf8(result.stdout).unwrap_or("Not displayable".into()); + + if command.print_stdout_intermediate() && exit_status.success() { + log::info!("\n{}", stdout); + } + + if exit_status.success() { + log::info!("✅ Success.") + } else { + log::error!("❌ Command failed. Run to completion for the summary."); + } + + Ok(RunResult { + exit_status, + stdout, + stderr, + }) +} + +/// Check if `run` was successful. +/// returns Ok in case the run went as expected, +/// Err otherwise +pub fn run_successful(run: &RunResult, expected_output_file: &str) -> Result<(), TestRunError> { + let mut file_handle = + File::open(expected_output_file).map_err(|_| TestRunError::FileError { + file: expected_output_file.to_owned(), + })?; + let mut expected_output = String::new(); + file_handle + .read_to_string(&mut expected_output) + .map_err(|_| TestRunError::FileError { + file: expected_output_file.to_owned(), + })?; + + if expected_output != run.stdout { + Err(TestRunError::FileCmpError { + expected: expected_output.clone(), + got: run.stdout.clone(), + }) + } else if !run.exit_status.success() { + Err(TestRunError::CommandError(run.clone())) + } else { + Ok(()) + } +} + +pub fn handle_results(globals: &Globals, results: Vec) -> Result<(), ()> { + let errors = results.iter().filter_map(|r| { + if let FinalRunResult::Failed(c, r) = r { + Some((c, &r.stdout, &r.stderr)) + } else { + None + } + }); + + let successes = results.iter().filter_map(|r| { + if let FinalRunResult::Success(c, r) = r { + Some((c, &r.stdout, &r.stderr)) + } else { + None + } + }); + + let command_errors = results.iter().filter_map(|r| { + if let FinalRunResult::CommandError(c, e) = r { + Some((c, e)) + } else { + None + } + }); + + let log_stdout_stderr = |level: Level| { + move |(cmd, stdout, stderr): (&CargoCommand, &String, &String)| { + let cmd = cmd.as_cmd_string(); + if !stdout.is_empty() && !stderr.is_empty() { + log::log!(level, "\n{cmd}\nStdout:\n{stdout}\nStderr:\n{stderr}"); + } else if !stdout.is_empty() { + log::log!(level, "\n{cmd}\nStdout:\n{}", stdout.trim_end()); + } else if !stderr.is_empty() { + log::log!(level, "\n{cmd}\nStderr:\n{}", stderr.trim_end()); + } + } + }; + + successes.for_each(|(cmd, stdout, stderr)| { + if globals.verbose > 0 { + info!("✅ Success: {cmd}\n {}", cmd.as_cmd_string()); + } else { + info!("✅ Success: {cmd}"); + } + + log_stdout_stderr(Level::Debug)((cmd, stdout, stderr)); + }); + + errors.clone().for_each(|(cmd, stdout, stderr)| { + error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string()); + log_stdout_stderr(Level::Error)((cmd, stdout, stderr)); + }); + + command_errors + .clone() + .for_each(|(cmd, error)| error!("❌ Failed: {cmd}\n {}\n{error}", cmd.as_cmd_string())); + + let ecount = errors.count() + command_errors.count(); + if ecount != 0 { + log::error!("{ecount} commands failed."); + Err(()) + } else { + info!("🚀🚀🚀 All tasks succeeded 🚀🚀🚀"); + Ok(()) + } +} -- cgit v1.2.3 From 65f1f4c1b7f8de50c6f9462a39c98dcd3a73b4cc Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 16 Apr 2023 11:26:23 +0200 Subject: Also separate all results and data --- xtask/src/main.rs | 126 +------------------------------ xtask/src/run/data.rs | 87 +++++++++++++++++++++ xtask/src/run/mod.rs | 191 ++++++++++++++++++----------------------------- xtask/src/run/results.rs | 122 ++++++++++++++++++++++++++++++ 4 files changed, 283 insertions(+), 243 deletions(-) create mode 100644 xtask/src/run/data.rs create mode 100644 xtask/src/run/results.rs (limited to 'xtask') diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 1712d71..30c3da0 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -3,26 +3,16 @@ mod build; mod cargo_command; mod run; -use argument_parsing::{ExtraArguments, Globals}; +use argument_parsing::ExtraArguments; use clap::Parser; use core::fmt; -use diffy::{create_patch, PatchFormatter}; -use std::{ - error::Error, - ffi::OsString, - fs::File, - io::prelude::*, - path::{Path, PathBuf}, - process::ExitStatus, - str, -}; +use std::{path::Path, str}; use log::{error, info, log_enabled, trace, Level}; use crate::{ argument_parsing::{Backends, BuildOrCheck, Cli, Commands}, build::init_build_dir, - cargo_command::CargoCommand, run::*, }; @@ -65,56 +55,6 @@ const ARMV7M: Target = Target::new("thumbv7m-none-eabi", false); const ARMV8MBASE: Target = Target::new("thumbv8m.base-none-eabi", false); const ARMV8MMAIN: Target = Target::new("thumbv8m.main-none-eabi", false); -#[derive(Debug, Clone)] -pub struct RunResult { - exit_status: ExitStatus, - stdout: String, - stderr: String, -} - -#[derive(Debug)] -pub enum TestRunError { - FileCmpError { expected: String, got: String }, - FileError { file: String }, - PathConversionError(OsString), - CommandError(RunResult), - IncompatibleCommand, -} -impl fmt::Display for TestRunError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - TestRunError::FileCmpError { expected, got } => { - let patch = create_patch(expected, got); - writeln!(f, "Differing output in files.\n")?; - let pf = PatchFormatter::new().with_color(); - writeln!(f, "{}", pf.fmt_patch(&patch))?; - write!( - f, - "See flag --overwrite-expected to create/update expected output." - ) - } - TestRunError::FileError { file } => { - write!(f, "File error on: {file}\nSee flag --overwrite-expected to create/update expected output.") - } - TestRunError::CommandError(e) => { - write!( - f, - "Command failed with exit status {}: {} {}", - e.exit_status, e.stdout, e.stderr - ) - } - TestRunError::PathConversionError(p) => { - write!(f, "Can't convert path from `OsString` to `String`: {p:?}") - } - TestRunError::IncompatibleCommand => { - write!(f, "Can't run that command in this context") - } - } - } -} - -impl Error for TestRunError {} - fn main() -> anyhow::Result<()> { // if there's an `xtask` folder, we're *probably* at the root of this repo (we can't just // check the name of `env::current_dir()` because people might clone it into a different name) @@ -299,65 +239,3 @@ fn main() -> anyhow::Result<()> { handle_results(globals, final_run_results).map_err(|_| anyhow::anyhow!("Commands failed")) } - -// run example binary `example` -fn command_parser( - glob: &Globals, - command: &CargoCommand, - overwrite: bool, -) -> anyhow::Result { - let output_mode = if glob.stderr_inherited { - OutputMode::Inherited - } else { - OutputMode::PipedAndCollected - }; - - match *command { - CargoCommand::Qemu { example, .. } | CargoCommand::Run { example, .. } => { - let run_file = format!("{example}.run"); - let expected_output_file = ["rtic", "ci", "expected", &run_file] - .iter() - .collect::() - .into_os_string() - .into_string() - .map_err(TestRunError::PathConversionError)?; - - // cargo run <..> - let cargo_run_result = run_command(command, output_mode)?; - - // Create a file for the expected output if it does not exist or mismatches - if overwrite { - let result = run_successful(&cargo_run_result, &expected_output_file); - if let Err(e) = result { - // FileError means the file did not exist or was unreadable - error!("Error: {e}"); - let mut file_handle = File::create(&expected_output_file).map_err(|_| { - TestRunError::FileError { - file: expected_output_file.clone(), - } - })?; - info!("Flag --overwrite-expected enabled"); - info!("Creating/updating file: {expected_output_file}"); - file_handle.write_all(cargo_run_result.stdout.as_bytes())?; - }; - } else { - run_successful(&cargo_run_result, &expected_output_file)?; - }; - - Ok(cargo_run_result) - } - CargoCommand::Format { .. } - | CargoCommand::ExampleCheck { .. } - | CargoCommand::ExampleBuild { .. } - | CargoCommand::Check { .. } - | CargoCommand::Build { .. } - | CargoCommand::Clippy { .. } - | CargoCommand::Doc { .. } - | CargoCommand::Test { .. } - | CargoCommand::Book { .. } - | CargoCommand::ExampleSize { .. } => { - let cargo_result = run_command(command, output_mode)?; - Ok(cargo_result) - } - } -} diff --git a/xtask/src/run/data.rs b/xtask/src/run/data.rs new file mode 100644 index 0000000..eacd72c --- /dev/null +++ b/xtask/src/run/data.rs @@ -0,0 +1,87 @@ +use std::{ + ffi::OsString, + process::{ExitStatus, Stdio}, +}; + +use diffy::{create_patch, PatchFormatter}; + +use crate::cargo_command::CargoCommand; + +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum OutputMode { + PipedAndCollected, + Inherited, +} + +impl From for Stdio { + fn from(value: OutputMode) -> Self { + match value { + OutputMode::PipedAndCollected => Stdio::piped(), + OutputMode::Inherited => Stdio::inherit(), + } + } +} + +#[derive(Debug, Clone)] +pub struct RunResult { + pub exit_status: ExitStatus, + pub stdout: String, + pub stderr: String, +} + +#[derive(Debug)] +pub enum FinalRunResult<'c> { + Success(CargoCommand<'c>, RunResult), + Failed(CargoCommand<'c>, RunResult), + CommandError(CargoCommand<'c>, anyhow::Error), +} + +#[derive(Debug)] +pub enum TestRunError { + FileCmpError { + expected: String, + got: String, + }, + FileError { + file: String, + }, + PathConversionError(OsString), + CommandError(RunResult), + #[allow(dead_code)] + IncompatibleCommand, +} + +impl core::fmt::Display for TestRunError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + TestRunError::FileCmpError { expected, got } => { + let patch = create_patch(expected, got); + writeln!(f, "Differing output in files.\n")?; + let pf = PatchFormatter::new().with_color(); + writeln!(f, "{}", pf.fmt_patch(&patch))?; + write!( + f, + "See flag --overwrite-expected to create/update expected output." + ) + } + TestRunError::FileError { file } => { + write!(f, "File error on: {file}\nSee flag --overwrite-expected to create/update expected output.") + } + TestRunError::CommandError(e) => { + write!( + f, + "Command failed with exit status {}: {} {}", + e.exit_status, e.stdout, e.stderr + ) + } + TestRunError::PathConversionError(p) => { + write!(f, "Can't convert path from `OsString` to `String`: {p:?}") + } + TestRunError::IncompatibleCommand => { + write!(f, "Can't run that command in this context") + } + } + } +} + +impl std::error::Error for TestRunError {} diff --git a/xtask/src/run/mod.rs b/xtask/src/run/mod.rs index daa6256..501849d 100644 --- a/xtask/src/run/mod.rs +++ b/xtask/src/run/mod.rs @@ -1,45 +1,30 @@ use std::{ fs::File, - io::Read, + io::Write, path::PathBuf, process::{Command, Stdio}, }; +mod results; +pub use results::handle_results; + +mod data; +use data::*; + mod iter; use iter::{into_iter, CoalescingRunner}; use crate::{ argument_parsing::{Backends, BuildOrCheck, ExtraArguments, Globals, PackageOpt, TestMetadata}, cargo_command::{BuildMode, CargoCommand}, - command_parser, RunResult, TestRunError, }; -use log::{error, info, Level}; +use log::{error, info}; #[cfg(feature = "rayon")] use rayon::prelude::*; -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum OutputMode { - PipedAndCollected, - Inherited, -} - -impl From for Stdio { - fn from(value: OutputMode) -> Self { - match value { - OutputMode::PipedAndCollected => Stdio::piped(), - OutputMode::Inherited => Stdio::inherit(), - } - } -} - -#[derive(Debug)] -pub enum FinalRunResult<'c> { - Success(CargoCommand<'c>, RunResult), - Failed(CargoCommand<'c>, RunResult), - CommandError(CargoCommand<'c>, anyhow::Error), -} +use self::results::run_successful; fn run_and_convert<'a>( (global, command, overwrite): (&Globals, CargoCommand<'a>, bool), @@ -66,6 +51,68 @@ fn run_and_convert<'a>( output } +// run example binary `example` +fn command_parser( + glob: &Globals, + command: &CargoCommand, + overwrite: bool, +) -> anyhow::Result { + let output_mode = if glob.stderr_inherited { + OutputMode::Inherited + } else { + OutputMode::PipedAndCollected + }; + + match *command { + CargoCommand::Qemu { example, .. } | CargoCommand::Run { example, .. } => { + let run_file = format!("{example}.run"); + let expected_output_file = ["rtic", "ci", "expected", &run_file] + .iter() + .collect::() + .into_os_string() + .into_string() + .map_err(TestRunError::PathConversionError)?; + + // cargo run <..> + let cargo_run_result = run_command(command, output_mode)?; + + // Create a file for the expected output if it does not exist or mismatches + if overwrite { + let result = run_successful(&cargo_run_result, &expected_output_file); + if let Err(e) = result { + // FileError means the file did not exist or was unreadable + error!("Error: {e}"); + let mut file_handle = File::create(&expected_output_file).map_err(|_| { + TestRunError::FileError { + file: expected_output_file.clone(), + } + })?; + info!("Flag --overwrite-expected enabled"); + info!("Creating/updating file: {expected_output_file}"); + file_handle.write_all(cargo_run_result.stdout.as_bytes())?; + }; + } else { + run_successful(&cargo_run_result, &expected_output_file)?; + }; + + Ok(cargo_run_result) + } + CargoCommand::Format { .. } + | CargoCommand::ExampleCheck { .. } + | CargoCommand::ExampleBuild { .. } + | CargoCommand::Check { .. } + | CargoCommand::Build { .. } + | CargoCommand::Clippy { .. } + | CargoCommand::Doc { .. } + | CargoCommand::Test { .. } + | CargoCommand::Book { .. } + | CargoCommand::ExampleSize { .. } => { + let cargo_result = run_command(command, output_mode)?; + Ok(cargo_result) + } + } +} + /// Cargo command to either build or check pub fn cargo<'c>( globals: &Globals, @@ -355,7 +402,7 @@ pub fn build_and_check_size<'c>( runner.run_and_coalesce() } -pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::Result { +fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::Result { log::info!("👟 {command}"); let mut process = Command::new(command.executable()); @@ -391,97 +438,3 @@ pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::R stderr, }) } - -/// Check if `run` was successful. -/// returns Ok in case the run went as expected, -/// Err otherwise -pub fn run_successful(run: &RunResult, expected_output_file: &str) -> Result<(), TestRunError> { - let mut file_handle = - File::open(expected_output_file).map_err(|_| TestRunError::FileError { - file: expected_output_file.to_owned(), - })?; - let mut expected_output = String::new(); - file_handle - .read_to_string(&mut expected_output) - .map_err(|_| TestRunError::FileError { - file: expected_output_file.to_owned(), - })?; - - if expected_output != run.stdout { - Err(TestRunError::FileCmpError { - expected: expected_output.clone(), - got: run.stdout.clone(), - }) - } else if !run.exit_status.success() { - Err(TestRunError::CommandError(run.clone())) - } else { - Ok(()) - } -} - -pub fn handle_results(globals: &Globals, results: Vec) -> Result<(), ()> { - let errors = results.iter().filter_map(|r| { - if let FinalRunResult::Failed(c, r) = r { - Some((c, &r.stdout, &r.stderr)) - } else { - None - } - }); - - let successes = results.iter().filter_map(|r| { - if let FinalRunResult::Success(c, r) = r { - Some((c, &r.stdout, &r.stderr)) - } else { - None - } - }); - - let command_errors = results.iter().filter_map(|r| { - if let FinalRunResult::CommandError(c, e) = r { - Some((c, e)) - } else { - None - } - }); - - let log_stdout_stderr = |level: Level| { - move |(cmd, stdout, stderr): (&CargoCommand, &String, &String)| { - let cmd = cmd.as_cmd_string(); - if !stdout.is_empty() && !stderr.is_empty() { - log::log!(level, "\n{cmd}\nStdout:\n{stdout}\nStderr:\n{stderr}"); - } else if !stdout.is_empty() { - log::log!(level, "\n{cmd}\nStdout:\n{}", stdout.trim_end()); - } else if !stderr.is_empty() { - log::log!(level, "\n{cmd}\nStderr:\n{}", stderr.trim_end()); - } - } - }; - - successes.for_each(|(cmd, stdout, stderr)| { - if globals.verbose > 0 { - info!("✅ Success: {cmd}\n {}", cmd.as_cmd_string()); - } else { - info!("✅ Success: {cmd}"); - } - - log_stdout_stderr(Level::Debug)((cmd, stdout, stderr)); - }); - - errors.clone().for_each(|(cmd, stdout, stderr)| { - error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string()); - log_stdout_stderr(Level::Error)((cmd, stdout, stderr)); - }); - - command_errors - .clone() - .for_each(|(cmd, error)| error!("❌ Failed: {cmd}\n {}\n{error}", cmd.as_cmd_string())); - - let ecount = errors.count() + command_errors.count(); - if ecount != 0 { - log::error!("{ecount} commands failed."); - Err(()) - } else { - info!("🚀🚀🚀 All tasks succeeded 🚀🚀🚀"); - Ok(()) - } -} diff --git a/xtask/src/run/results.rs b/xtask/src/run/results.rs new file mode 100644 index 0000000..072cbcf --- /dev/null +++ b/xtask/src/run/results.rs @@ -0,0 +1,122 @@ +use log::{error, info, log, Level}; + +use crate::{argument_parsing::Globals, cargo_command::CargoCommand}; + +use super::data::{FinalRunResult, RunResult, TestRunError}; + +const TARGET: &str = "xtask::results"; + +/// Check if `run` was successful. +/// returns Ok in case the run went as expected, +/// Err otherwise +pub fn run_successful(run: &RunResult, expected_output_file: &str) -> Result<(), TestRunError> { + let file = expected_output_file.to_string(); + + let expected_output = std::fs::read(expected_output_file) + .map(|d| String::from_utf8(d).map_err(|_| TestRunError::FileError { file: file.clone() })) + .map_err(|_| TestRunError::FileError { file })??; + + if expected_output != run.stdout { + Err(TestRunError::FileCmpError { + expected: expected_output.clone(), + got: run.stdout.clone(), + }) + } else if !run.exit_status.success() { + Err(TestRunError::CommandError(run.clone())) + } else { + Ok(()) + } +} + +pub fn handle_results(globals: &Globals, results: Vec) -> Result<(), ()> { + let errors = results.iter().filter_map(|r| { + if let FinalRunResult::Failed(c, r) = r { + Some((c, &r.stdout, &r.stderr)) + } else { + None + } + }); + + let successes = results.iter().filter_map(|r| { + if let FinalRunResult::Success(c, r) = r { + Some((c, &r.stdout, &r.stderr)) + } else { + None + } + }); + + let command_errors = results.iter().filter_map(|r| { + if let FinalRunResult::CommandError(c, e) = r { + Some((c, e)) + } else { + None + } + }); + + let log_stdout_stderr = |level: Level| { + move |(cmd, stdout, stderr): (&CargoCommand, &String, &String)| { + let cmd = cmd.as_cmd_string(); + if !stdout.is_empty() && !stderr.is_empty() { + log!( + target: TARGET, + level, + "\n{cmd}\nStdout:\n{stdout}\nStderr:\n{stderr}" + ); + } else if !stdout.is_empty() { + log!( + target: TARGET, + level, + "\n{cmd}\nStdout:\n{}", + stdout.trim_end() + ); + } else if !stderr.is_empty() { + log!( + target: TARGET, + level, + "\n{cmd}\nStderr:\n{}", + stderr.trim_end() + ); + } + } + }; + + successes.for_each(|(cmd, stdout, stderr)| { + if globals.verbose > 0 { + info!( + target: TARGET, + "✅ Success: {cmd}\n {}", + cmd.as_cmd_string() + ); + } else { + info!(target: TARGET, "✅ Success: {cmd}"); + } + + log_stdout_stderr(Level::Debug)((cmd, stdout, stderr)); + }); + + errors.clone().for_each(|(cmd, stdout, stderr)| { + error!( + target: TARGET, + "❌ Failed: {cmd}\n {}", + cmd.as_cmd_string() + ); + log_stdout_stderr(Level::Error)((cmd, stdout, stderr)); + }); + + command_errors.clone().for_each(|(cmd, error)| { + error!( + target: TARGET, + "❌ Failed: {cmd}\n {}\n{error}", + cmd.as_cmd_string() + ) + }); + + let ecount = errors.count() + command_errors.count(); + if ecount != 0 { + error!(target: TARGET, "{ecount} commands failed."); + Err(()) + } else { + info!(target: TARGET, "🚀🚀🚀 All tasks succeeded 🚀🚀🚀"); + Ok(()) + } +} -- cgit v1.2.3 From fd011cd5ecb65b076f94376214ff31e7edcff189 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 16 Apr 2023 11:37:36 +0200 Subject: FIx printing for Run and Qemu --- xtask/src/run/mod.rs | 58 ++++++++++++++++++++++++++++++++++++++++-------- xtask/src/run/results.rs | 24 +------------------- 2 files changed, 50 insertions(+), 32 deletions(-) (limited to 'xtask') diff --git a/xtask/src/run/mod.rs b/xtask/src/run/mod.rs index 501849d..035dc7a 100644 --- a/xtask/src/run/mod.rs +++ b/xtask/src/run/mod.rs @@ -24,8 +24,6 @@ use log::{error, info}; #[cfg(feature = "rayon")] use rayon::prelude::*; -use self::results::run_successful; - fn run_and_convert<'a>( (global, command, overwrite): (&Globals, CargoCommand<'a>, bool), ) -> FinalRunResult<'a> { @@ -65,6 +63,42 @@ fn command_parser( match *command { CargoCommand::Qemu { example, .. } | CargoCommand::Run { example, .. } => { + /// Check if `run` was successful. + /// returns Ok in case the run went as expected, + /// Err otherwise + pub fn run_successful( + run: &RunResult, + expected_output_file: &str, + ) -> Result<(), TestRunError> { + let file = expected_output_file.to_string(); + + let expected_output = std::fs::read(expected_output_file) + .map(|d| { + String::from_utf8(d) + .map_err(|_| TestRunError::FileError { file: file.clone() }) + }) + .map_err(|_| TestRunError::FileError { file })??; + + let res = if expected_output != run.stdout { + Err(TestRunError::FileCmpError { + expected: expected_output.clone(), + got: run.stdout.clone(), + }) + } else if !run.exit_status.success() { + Err(TestRunError::CommandError(run.clone())) + } else { + Ok(()) + }; + + if res.is_ok() { + log::info!("✅ Success."); + } else { + log::error!("❌ Command failed. Run to completion for the summary."); + } + + res + } + let run_file = format!("{example}.run"); let expected_output_file = ["rtic", "ci", "expected", &run_file] .iter() @@ -74,7 +108,7 @@ fn command_parser( .map_err(TestRunError::PathConversionError)?; // cargo run <..> - let cargo_run_result = run_command(command, output_mode)?; + let cargo_run_result = run_command(command, output_mode, false)?; // Create a file for the expected output if it does not exist or mismatches if overwrite { @@ -107,7 +141,7 @@ fn command_parser( | CargoCommand::Test { .. } | CargoCommand::Book { .. } | CargoCommand::ExampleSize { .. } => { - let cargo_result = run_command(command, output_mode)?; + let cargo_result = run_command(command, output_mode, true)?; Ok(cargo_result) } } @@ -402,7 +436,11 @@ pub fn build_and_check_size<'c>( runner.run_and_coalesce() } -fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::Result { +fn run_command( + command: &CargoCommand, + stderr_mode: OutputMode, + print_command_success: bool, +) -> anyhow::Result { log::info!("👟 {command}"); let mut process = Command::new(command.executable()); @@ -426,10 +464,12 @@ fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::Resul log::info!("\n{}", stdout); } - if exit_status.success() { - log::info!("✅ Success.") - } else { - log::error!("❌ Command failed. Run to completion for the summary."); + if print_command_success { + if exit_status.success() { + log::info!("✅ Success.") + } else { + log::error!("❌ Command failed. Run to completion for the summary."); + } } Ok(RunResult { diff --git a/xtask/src/run/results.rs b/xtask/src/run/results.rs index 072cbcf..b64e7b1 100644 --- a/xtask/src/run/results.rs +++ b/xtask/src/run/results.rs @@ -2,32 +2,10 @@ use log::{error, info, log, Level}; use crate::{argument_parsing::Globals, cargo_command::CargoCommand}; -use super::data::{FinalRunResult, RunResult, TestRunError}; +use super::data::FinalRunResult; const TARGET: &str = "xtask::results"; -/// Check if `run` was successful. -/// returns Ok in case the run went as expected, -/// Err otherwise -pub fn run_successful(run: &RunResult, expected_output_file: &str) -> Result<(), TestRunError> { - let file = expected_output_file.to_string(); - - let expected_output = std::fs::read(expected_output_file) - .map(|d| String::from_utf8(d).map_err(|_| TestRunError::FileError { file: file.clone() })) - .map_err(|_| TestRunError::FileError { file })??; - - if expected_output != run.stdout { - Err(TestRunError::FileCmpError { - expected: expected_output.clone(), - got: run.stdout.clone(), - }) - } else if !run.exit_status.success() { - Err(TestRunError::CommandError(run.clone())) - } else { - Ok(()) - } -} - pub fn handle_results(globals: &Globals, results: Vec) -> Result<(), ()> { let errors = results.iter().filter_map(|r| { if let FinalRunResult::Failed(c, r) = r { -- cgit v1.2.3 From 4d3361658b2487154d8140b06b140e72c0227441 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 16 Apr 2023 11:51:28 +0200 Subject: Unconditionally deny warnings for clippy --- xtask/src/cargo_command.rs | 21 +++++++++++++++++++-- xtask/src/run/mod.rs | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) (limited to 'xtask') diff --git a/xtask/src/cargo_command.rs b/xtask/src/cargo_command.rs index f399b87..9cf4f65 100644 --- a/xtask/src/cargo_command.rs +++ b/xtask/src/cargo_command.rs @@ -65,6 +65,7 @@ pub enum CargoCommand<'a> { package: Option, target: Option>, features: Option, + deny_warnings: bool, }, Format { cargoarg: &'a Option<&'a str>, @@ -244,10 +245,16 @@ impl core::fmt::Display for CargoCommand<'_> { package, target, features, + deny_warnings, } => { let details = details(target, None, features, cargoarg, None); let package = p(package); - write!(f, "Clippy {package} {details}") + let deny_warns = if *deny_warnings { + format!(" (deny warnings)") + } else { + format!("") + }; + write!(f, "Clippy{deny_warns} {package} {details}") } CargoCommand::Format { cargoarg, @@ -483,9 +490,19 @@ impl<'a> CargoCommand<'a> { cargoarg, package, features, + deny_warnings, // Target is added by build_args target: _, - } => self.build_args(true, cargoarg, features, None, p(package)), + } => { + let package = p(package); + let extra = if *deny_warnings { + vec!["--", "-D", "warnings"].into_iter() + } else { + vec![].into_iter() + }; + + self.build_args(true, cargoarg, features, None, package.chain(extra)) + } CargoCommand::Doc { cargoarg, features, diff --git a/xtask/src/run/mod.rs b/xtask/src/run/mod.rs index 035dc7a..ac35f5c 100644 --- a/xtask/src/run/mod.rs +++ b/xtask/src/run/mod.rs @@ -279,6 +279,7 @@ pub fn cargo_clippy<'c>( package: Some(package.name()), target: target.into(), features, + deny_warnings: true, }; (globals, command, false) -- cgit v1.2.3 From 2db26c1015f0a32fe43e71d60f5fd75dbb25f40c Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 16 Apr 2023 12:51:11 +0200 Subject: Deny on warnings in xtasks --- xtask/src/argument_parsing.rs | 16 +++++- xtask/src/cargo_command.rs | 110 +++++++++++++++++++++++++++++++----------- xtask/src/run/mod.rs | 19 +++++++- 3 files changed, 116 insertions(+), 29 deletions(-) (limited to 'xtask') diff --git a/xtask/src/argument_parsing.rs b/xtask/src/argument_parsing.rs index 8c8f7d2..d7c0262 100644 --- a/xtask/src/argument_parsing.rs +++ b/xtask/src/argument_parsing.rs @@ -94,7 +94,11 @@ impl Package { pub struct TestMetadata {} impl TestMetadata { - pub fn match_package(package: Package, backend: Backends) -> CargoCommand<'static> { + pub fn match_package( + deny_warnings: bool, + package: Package, + backend: Backends, + ) -> CargoCommand<'static> { match package { Package::Rtic => { let features = format!( @@ -107,32 +111,38 @@ impl TestMetadata { package: Some(package.name()), features, test: Some("ui".to_owned()), + deny_warnings, } } Package::RticMacros => CargoCommand::Test { package: Some(package.name()), features: Some(backend.to_rtic_macros_feature().to_owned()), test: None, + deny_warnings, }, Package::RticSync => CargoCommand::Test { package: Some(package.name()), features: Some("testing".to_owned()), test: None, + deny_warnings, }, Package::RticCommon => CargoCommand::Test { package: Some(package.name()), features: Some("testing".to_owned()), test: None, + deny_warnings, }, Package::RticMonotonics => CargoCommand::Test { package: Some(package.name()), features: None, test: None, + deny_warnings, }, Package::RticTime => CargoCommand::Test { package: Some(package.name()), features: Some("critical-section/std".into()), test: None, + deny_warnings, }, } } @@ -192,6 +202,10 @@ pub enum BuildOrCheck { #[derive(Parser, Clone)] pub struct Globals { + /// Error out on warnings + #[arg(short = 'D', long)] + pub deny_warnings: bool, + /// For which backend to build. #[arg(value_enum, short, default_value = "thumbv7", long, global = true)] pub backend: Option, diff --git a/xtask/src/cargo_command.rs b/xtask/src/cargo_command.rs index 9cf4f65..09487cb 100644 --- a/xtask/src/cargo_command.rs +++ b/xtask/src/cargo_command.rs @@ -28,6 +28,7 @@ pub enum CargoCommand<'a> { features: Option, mode: BuildMode, dir: Option, + deny_warnings: bool, }, ExampleBuild { cargoarg: &'a Option<&'a str>, @@ -36,6 +37,7 @@ pub enum CargoCommand<'a> { features: Option, mode: BuildMode, dir: Option, + deny_warnings: bool, }, ExampleCheck { cargoarg: &'a Option<&'a str>, @@ -43,6 +45,7 @@ pub enum CargoCommand<'a> { target: Option>, features: Option, mode: BuildMode, + deny_warnings: bool, }, Build { cargoarg: &'a Option<&'a str>, @@ -51,6 +54,7 @@ pub enum CargoCommand<'a> { features: Option, mode: BuildMode, dir: Option, + deny_warnings: bool, }, Check { cargoarg: &'a Option<&'a str>, @@ -59,6 +63,7 @@ pub enum CargoCommand<'a> { features: Option, mode: BuildMode, dir: Option, + deny_warnings: bool, }, Clippy { cargoarg: &'a Option<&'a str>, @@ -81,6 +86,7 @@ pub enum CargoCommand<'a> { package: Option, features: Option, test: Option, + deny_warnings: bool, }, Book { arguments: Option, @@ -123,6 +129,7 @@ impl core::fmt::Display for CargoCommand<'_> { } fn details( + deny_warnings: bool, target: &Option, mode: Option<&BuildMode>, features: &Option, @@ -150,14 +157,20 @@ impl core::fmt::Display for CargoCommand<'_> { format!("debug") }; + let deny_warnings = if deny_warnings { + format!("deny warnings, ") + } else { + format!("") + }; + if cargoarg.is_some() && path.is_some() { - format!("({target}, {mode}, {feat}, {carg}, {in_dir})") + format!("({deny_warnings}{target}, {mode}, {feat}, {carg}, {in_dir})") } else if cargoarg.is_some() { - format!("({target}, {mode}, {feat}, {carg})") + format!("({deny_warnings}{target}, {mode}, {feat}, {carg})") } else if path.is_some() { - format!("({target}, {mode}, {feat}, {in_dir})") + format!("({deny_warnings}{target}, {mode}, {feat}, {in_dir})") } else { - format!("({target}, {mode}, {feat})") + format!("({deny_warnings}{target}, {mode}, {feat})") } } @@ -173,7 +186,7 @@ impl core::fmt::Display for CargoCommand<'_> { write!( f, "Run example {example} {}", - details(target, Some(mode), features, cargoarg, dir.as_ref()) + details(false, target, Some(mode), features, cargoarg, dir.as_ref()) ) } CargoCommand::Qemu { @@ -183,8 +196,10 @@ impl core::fmt::Display for CargoCommand<'_> { features, mode, dir, + deny_warnings, } => { - let details = details(target, Some(mode), features, cargoarg, dir.as_ref()); + let warns = *deny_warnings; + let details = details(warns, target, Some(mode), features, cargoarg, dir.as_ref()); write!(f, "Run example {example} in QEMU {details}",) } CargoCommand::ExampleBuild { @@ -194,8 +209,10 @@ impl core::fmt::Display for CargoCommand<'_> { features, mode, dir, + deny_warnings, } => { - let details = details(target, Some(mode), features, cargoarg, dir.as_ref()); + let warns = *deny_warnings; + let details = details(warns, target, Some(mode), features, cargoarg, dir.as_ref()); write!(f, "Build example {example} {details}",) } CargoCommand::ExampleCheck { @@ -204,10 +221,11 @@ impl core::fmt::Display for CargoCommand<'_> { target, features, mode, + deny_warnings, } => write!( f, "Check example {example} {}", - details(target, Some(mode), features, cargoarg, None) + details(*deny_warnings, target, Some(mode), features, cargoarg, None) ), CargoCommand::Build { cargoarg, @@ -216,12 +234,14 @@ impl core::fmt::Display for CargoCommand<'_> { features, mode, dir, + deny_warnings, } => { let package = p(package); + let warns = *deny_warnings; write!( f, "Build {package} {}", - details(target, Some(mode), features, cargoarg, dir.as_ref()) + details(warns, target, Some(mode), features, cargoarg, dir.as_ref()) ) } @@ -232,12 +252,14 @@ impl core::fmt::Display for CargoCommand<'_> { features, mode, dir, + deny_warnings, } => { let package = p(package); + let warns = *deny_warnings; write!( f, "Check {package} {}", - details(target, Some(mode), features, cargoarg, dir.as_ref()) + details(warns, target, Some(mode), features, cargoarg, dir.as_ref()) ) } CargoCommand::Clippy { @@ -247,14 +269,9 @@ impl core::fmt::Display for CargoCommand<'_> { features, deny_warnings, } => { - let details = details(target, None, features, cargoarg, None); + let details = details(*deny_warnings, target, None, features, cargoarg, None); let package = p(package); - let deny_warns = if *deny_warnings { - format!(" (deny warnings)") - } else { - format!("") - }; - write!(f, "Clippy{deny_warns} {package} {details}") + write!(f, "Clippy {package} {details}") } CargoCommand::Format { cargoarg, @@ -297,14 +314,20 @@ impl core::fmt::Display for CargoCommand<'_> { package, features, test, + deny_warnings, } => { let p = p(package); let test = test .clone() .map(|t| format!("test {t}")) .unwrap_or("all tests".into()); + let deny_warnings = if *deny_warnings { + format!("deny warnings, ") + } else { + format!("") + }; let feat = feat(features); - write!(f, "Run {test} in {p} (features: {feat})") + write!(f, "Run {test} in {p} ({deny_warnings}features: {feat})") } CargoCommand::Book { arguments: _ } => write!(f, "Build the book"), CargoCommand::ExampleSize { @@ -316,7 +339,7 @@ impl core::fmt::Display for CargoCommand<'_> { arguments: _, dir, } => { - let details = details(target, Some(mode), features, cargoarg, dir.as_ref()); + let details = details(false, target, Some(mode), features, cargoarg, dir.as_ref()); write!(f, "Compute size of example {example} {details}") } } @@ -459,6 +482,8 @@ impl<'a> CargoCommand<'a> { dir: _, // Target is added by build_args target: _, + // deny_warnings is exposed through `rustflags` + deny_warnings: _, } => self.build_args( true, cargoarg, @@ -471,10 +496,12 @@ impl<'a> CargoCommand<'a> { package, features, mode, - // Dir is exposed through `chdir` - dir: _, // Target is added by build_args target: _, + // Dir is exposed through `chdir` + dir: _, + // deny_warnings is exposed through `rustflags` + deny_warnings: _, } => self.build_args(true, cargoarg, features, Some(mode), p(package)), CargoCommand::Check { cargoarg, @@ -485,23 +512,25 @@ impl<'a> CargoCommand<'a> { dir: _, // Target is added by build_args target: _, + // deny_warnings is exposed through `rustflags` + deny_warnings: _, } => self.build_args(true, cargoarg, features, Some(mode), p(package)), CargoCommand::Clippy { cargoarg, package, features, - deny_warnings, // Target is added by build_args target: _, + deny_warnings, } => { - let package = p(package); - let extra = if *deny_warnings { - vec!["--", "-D", "warnings"].into_iter() + let deny_warnings = if *deny_warnings { + vec!["--", "-D", "warnings"] } else { - vec![].into_iter() + vec![] }; - self.build_args(true, cargoarg, features, None, package.chain(extra)) + let extra = p(package).chain(deny_warnings); + self.build_args(true, cargoarg, features, None, extra) } CargoCommand::Doc { cargoarg, @@ -515,6 +544,8 @@ impl<'a> CargoCommand<'a> { package, features, test, + // deny_warnings is exposed through `rustflags` + deny_warnings: _, } => { let extra = if let Some(test) = test { vec!["--test", test] @@ -564,6 +595,8 @@ impl<'a> CargoCommand<'a> { dir: _, // Target is added by build_args target: _, + // deny_warnings is exposed through `rustflags` + deny_warnings: _, } => self.build_args( true, cargoarg, @@ -578,6 +611,8 @@ impl<'a> CargoCommand<'a> { mode, // Target is added by build_args target: _, + // deny_warnings is exposed through `rustflags` + deny_warnings: _, } => self.build_args( true, cargoarg, @@ -632,6 +667,27 @@ impl<'a> CargoCommand<'a> { } } + pub fn rustflags(&self) -> Option<&str> { + match self { + // Clippy is a special case: it sets deny warnings + // through an argument to rustc. + CargoCommand::Clippy { .. } => None, + CargoCommand::Check { deny_warnings, .. } + | CargoCommand::ExampleCheck { deny_warnings, .. } + | CargoCommand::Build { deny_warnings, .. } + | CargoCommand::ExampleBuild { deny_warnings, .. } + | CargoCommand::Test { deny_warnings, .. } + | CargoCommand::Qemu { deny_warnings, .. } => { + if *deny_warnings { + Some("-D warnings") + } else { + None + } + } + _ => None, + } + } + pub fn print_stdout_intermediate(&self) -> bool { match self { Self::ExampleSize { .. } => true, diff --git a/xtask/src/run/mod.rs b/xtask/src/run/mod.rs index ac35f5c..4032ea8 100644 --- a/xtask/src/run/mod.rs +++ b/xtask/src/run/mod.rs @@ -172,6 +172,7 @@ pub fn cargo<'c>( features, mode: BuildMode::Release, dir: None, + deny_warnings: globals.deny_warnings, }, BuildOrCheck::Build => CargoCommand::Build { cargoarg, @@ -180,6 +181,7 @@ pub fn cargo<'c>( features, mode: BuildMode::Release, dir: None, + deny_warnings: globals.deny_warnings, }, }; @@ -209,6 +211,7 @@ pub fn cargo_usage_example( package: None, target: None, features: None, + deny_warnings: globals.deny_warnings, }, BuildOrCheck::Build => CargoCommand::Build { cargoarg: &None, @@ -217,6 +220,7 @@ pub fn cargo_usage_example( features: None, mode: BuildMode::Release, dir: Some(path.into()), + deny_warnings: globals.deny_warnings, }, }; (globals, command, false) @@ -244,6 +248,7 @@ pub fn cargo_example<'c>( target: Some(backend.to_target()), features, mode: BuildMode::Release, + deny_warnings: globals.deny_warnings, }, BuildOrCheck::Build => CargoCommand::ExampleBuild { cargoarg, @@ -252,6 +257,7 @@ pub fn cargo_example<'c>( features, mode: BuildMode::Release, dir: Some(PathBuf::from("./rtic")), + deny_warnings: globals.deny_warnings, }, }; (globals, command, false) @@ -337,7 +343,10 @@ pub fn cargo_test<'c>( ) -> Vec> { package .packages() - .map(|p| (globals, TestMetadata::match_package(p, backend), false)) + .map(|p| { + let meta = TestMetadata::match_package(globals.deny_warnings, p, backend); + (globals, meta, false) + }) .run_and_coalesce() } @@ -378,6 +387,7 @@ pub fn qemu_run_examples<'c>( features: features.clone(), mode: BuildMode::Release, dir: Some(PathBuf::from("./rtic")), + deny_warnings: globals.deny_warnings, }; let cmd_qemu = CargoCommand::Qemu { @@ -387,6 +397,7 @@ pub fn qemu_run_examples<'c>( features: features.clone(), mode: BuildMode::Release, dir: Some(PathBuf::from("./rtic")), + deny_warnings: globals.deny_warnings, }; into_iter([cmd_build, cmd_qemu]) @@ -417,7 +428,9 @@ pub fn build_and_check_size<'c>( features: features.clone(), mode: BuildMode::Release, dir: Some(PathBuf::from("./rtic")), + deny_warnings: globals.deny_warnings, }; + if let Err(err) = command_parser(globals, &cmd, false) { error!("{err}"); } @@ -455,6 +468,10 @@ fn run_command( process.current_dir(dir.canonicalize()?); } + if let Some(rustflags) = command.rustflags() { + process.env("RUSTFLAGS", rustflags); + } + let result = process.output()?; let exit_status = result.status; -- cgit v1.2.3 From 85e2cd6d4b1ac801dbbce2295a6617a56f9ea5a0 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 16 Apr 2023 13:16:28 +0200 Subject: Also always deny warnings for doc --- xtask/src/cargo_command.rs | 30 ++++++++++++++++++++---------- xtask/src/run/mod.rs | 5 +++-- 2 files changed, 23 insertions(+), 12 deletions(-) (limited to 'xtask') diff --git a/xtask/src/cargo_command.rs b/xtask/src/cargo_command.rs index 09487cb..ef14a38 100644 --- a/xtask/src/cargo_command.rs +++ b/xtask/src/cargo_command.rs @@ -81,6 +81,7 @@ pub enum CargoCommand<'a> { cargoarg: &'a Option<&'a str>, features: Option, arguments: Option, + deny_warnings: bool, }, Test { package: Option, @@ -297,6 +298,7 @@ impl core::fmt::Display for CargoCommand<'_> { cargoarg, features, arguments, + deny_warnings, } => { let feat = feat(features); let carg = carg(cargoarg); @@ -304,10 +306,15 @@ impl core::fmt::Display for CargoCommand<'_> { .clone() .map(|a| format!("{a}")) .unwrap_or_else(|| "no extra arguments".into()); + let deny_warnings = if *deny_warnings { + format!("deny warnings, ") + } else { + format!("") + }; if cargoarg.is_some() { - write!(f, "Document ({feat}, {carg}, {arguments})") + write!(f, "Document ({deny_warnings}{feat}, {carg}, {arguments})") } else { - write!(f, "Document ({feat}, {arguments})") + write!(f, "Document ({deny_warnings}{feat}, {arguments})") } } CargoCommand::Test { @@ -482,7 +489,7 @@ impl<'a> CargoCommand<'a> { dir: _, // Target is added by build_args target: _, - // deny_warnings is exposed through `rustflags` + // deny_warnings is exposed through `extra_env` deny_warnings: _, } => self.build_args( true, @@ -500,7 +507,7 @@ impl<'a> CargoCommand<'a> { target: _, // Dir is exposed through `chdir` dir: _, - // deny_warnings is exposed through `rustflags` + // deny_warnings is exposed through `extra_env` deny_warnings: _, } => self.build_args(true, cargoarg, features, Some(mode), p(package)), CargoCommand::Check { @@ -512,7 +519,7 @@ impl<'a> CargoCommand<'a> { dir: _, // Target is added by build_args target: _, - // deny_warnings is exposed through `rustflags` + // deny_warnings is exposed through `extra_env` deny_warnings: _, } => self.build_args(true, cargoarg, features, Some(mode), p(package)), CargoCommand::Clippy { @@ -536,6 +543,8 @@ impl<'a> CargoCommand<'a> { cargoarg, features, arguments, + // deny_warnings is exposed through `extra_env` + deny_warnings: _, } => { let extra = Self::extra_args(arguments.as_ref()); self.build_args(true, cargoarg, features, None, extra) @@ -544,7 +553,7 @@ impl<'a> CargoCommand<'a> { package, features, test, - // deny_warnings is exposed through `rustflags` + // deny_warnings is exposed through `extra_env` deny_warnings: _, } => { let extra = if let Some(test) = test { @@ -595,7 +604,7 @@ impl<'a> CargoCommand<'a> { dir: _, // Target is added by build_args target: _, - // deny_warnings is exposed through `rustflags` + // deny_warnings is exposed through `extra_env` deny_warnings: _, } => self.build_args( true, @@ -611,7 +620,7 @@ impl<'a> CargoCommand<'a> { mode, // Target is added by build_args target: _, - // deny_warnings is exposed through `rustflags` + // deny_warnings is exposed through `extra_env` deny_warnings: _, } => self.build_args( true, @@ -667,11 +676,12 @@ impl<'a> CargoCommand<'a> { } } - pub fn rustflags(&self) -> Option<&str> { + pub fn extra_env(&self) -> Option<(&str, &str)> { match self { // Clippy is a special case: it sets deny warnings // through an argument to rustc. CargoCommand::Clippy { .. } => None, + CargoCommand::Doc { .. } => Some(("RUSTDOCFLAGS", "-D warnings")), CargoCommand::Check { deny_warnings, .. } | CargoCommand::ExampleCheck { deny_warnings, .. } | CargoCommand::Build { deny_warnings, .. } @@ -679,7 +689,7 @@ impl<'a> CargoCommand<'a> { | CargoCommand::Test { deny_warnings, .. } | CargoCommand::Qemu { deny_warnings, .. } => { if *deny_warnings { - Some("-D warnings") + Some(("RUSTFLAGS", "-D warnings")) } else { None } diff --git a/xtask/src/run/mod.rs b/xtask/src/run/mod.rs index 4032ea8..74179c5 100644 --- a/xtask/src/run/mod.rs +++ b/xtask/src/run/mod.rs @@ -328,6 +328,7 @@ pub fn cargo_doc<'c>( cargoarg, features, arguments: arguments.clone(), + deny_warnings: true, }; vec![run_and_convert((globals, command, false))] @@ -468,8 +469,8 @@ fn run_command( process.current_dir(dir.canonicalize()?); } - if let Some(rustflags) = command.rustflags() { - process.env("RUSTFLAGS", rustflags); + if let Some((k, v)) = command.extra_env() { + process.env(k, v); } let result = process.output()?; -- cgit v1.2.3 From bc92e43b113dae2a34db6b10812d84227ac5a6d6 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 16 Apr 2023 13:22:10 +0200 Subject: Rename + better printout --- xtask/src/cargo_command.rs | 2 +- xtask/src/run.rs | 499 +++++++++++++++++++++++++++++++++++++++++++++ xtask/src/run/mod.rs | 499 --------------------------------------------- 3 files changed, 500 insertions(+), 500 deletions(-) create mode 100644 xtask/src/run.rs delete mode 100644 xtask/src/run/mod.rs (limited to 'xtask') diff --git a/xtask/src/cargo_command.rs b/xtask/src/cargo_command.rs index ef14a38..b0102ce 100644 --- a/xtask/src/cargo_command.rs +++ b/xtask/src/cargo_command.rs @@ -149,7 +149,7 @@ impl core::fmt::Display for CargoCommand<'_> { let target = if let Some(target) = target { format!("{target}") } else { - format!("") + format!("") }; let mode = if let Some(mode) = mode { diff --git a/xtask/src/run.rs b/xtask/src/run.rs new file mode 100644 index 0000000..74179c5 --- /dev/null +++ b/xtask/src/run.rs @@ -0,0 +1,499 @@ +use std::{ + fs::File, + io::Write, + path::PathBuf, + process::{Command, Stdio}, +}; + +mod results; +pub use results::handle_results; + +mod data; +use data::*; + +mod iter; +use iter::{into_iter, CoalescingRunner}; + +use crate::{ + argument_parsing::{Backends, BuildOrCheck, ExtraArguments, Globals, PackageOpt, TestMetadata}, + cargo_command::{BuildMode, CargoCommand}, +}; + +use log::{error, info}; + +#[cfg(feature = "rayon")] +use rayon::prelude::*; + +fn run_and_convert<'a>( + (global, command, overwrite): (&Globals, CargoCommand<'a>, bool), +) -> FinalRunResult<'a> { + // Run the command + let result = command_parser(global, &command, overwrite); + + let output = match result { + // If running the command succeeded without looking at any of the results, + // log the data and see if the actual execution was succesfull too. + Ok(result) => { + if result.exit_status.success() { + FinalRunResult::Success(command, result) + } else { + FinalRunResult::Failed(command, result) + } + } + // If it didn't and some IO error occured, just panic + Err(e) => FinalRunResult::CommandError(command, e), + }; + + log::trace!("Final result: {output:?}"); + + output +} + +// run example binary `example` +fn command_parser( + glob: &Globals, + command: &CargoCommand, + overwrite: bool, +) -> anyhow::Result { + let output_mode = if glob.stderr_inherited { + OutputMode::Inherited + } else { + OutputMode::PipedAndCollected + }; + + match *command { + CargoCommand::Qemu { example, .. } | CargoCommand::Run { example, .. } => { + /// Check if `run` was successful. + /// returns Ok in case the run went as expected, + /// Err otherwise + pub fn run_successful( + run: &RunResult, + expected_output_file: &str, + ) -> Result<(), TestRunError> { + let file = expected_output_file.to_string(); + + let expected_output = std::fs::read(expected_output_file) + .map(|d| { + String::from_utf8(d) + .map_err(|_| TestRunError::FileError { file: file.clone() }) + }) + .map_err(|_| TestRunError::FileError { file })??; + + let res = if expected_output != run.stdout { + Err(TestRunError::FileCmpError { + expected: expected_output.clone(), + got: run.stdout.clone(), + }) + } else if !run.exit_status.success() { + Err(TestRunError::CommandError(run.clone())) + } else { + Ok(()) + }; + + if res.is_ok() { + log::info!("✅ Success."); + } else { + log::error!("❌ Command failed. Run to completion for the summary."); + } + + res + } + + let run_file = format!("{example}.run"); + let expected_output_file = ["rtic", "ci", "expected", &run_file] + .iter() + .collect::() + .into_os_string() + .into_string() + .map_err(TestRunError::PathConversionError)?; + + // cargo run <..> + let cargo_run_result = run_command(command, output_mode, false)?; + + // Create a file for the expected output if it does not exist or mismatches + if overwrite { + let result = run_successful(&cargo_run_result, &expected_output_file); + if let Err(e) = result { + // FileError means the file did not exist or was unreadable + error!("Error: {e}"); + let mut file_handle = File::create(&expected_output_file).map_err(|_| { + TestRunError::FileError { + file: expected_output_file.clone(), + } + })?; + info!("Flag --overwrite-expected enabled"); + info!("Creating/updating file: {expected_output_file}"); + file_handle.write_all(cargo_run_result.stdout.as_bytes())?; + }; + } else { + run_successful(&cargo_run_result, &expected_output_file)?; + }; + + Ok(cargo_run_result) + } + CargoCommand::Format { .. } + | CargoCommand::ExampleCheck { .. } + | CargoCommand::ExampleBuild { .. } + | CargoCommand::Check { .. } + | CargoCommand::Build { .. } + | CargoCommand::Clippy { .. } + | CargoCommand::Doc { .. } + | CargoCommand::Test { .. } + | CargoCommand::Book { .. } + | CargoCommand::ExampleSize { .. } => { + let cargo_result = run_command(command, output_mode, true)?; + Ok(cargo_result) + } + } +} + +/// Cargo command to either build or check +pub fn cargo<'c>( + globals: &Globals, + operation: BuildOrCheck, + cargoarg: &'c Option<&'c str>, + package: &'c PackageOpt, + backend: Backends, +) -> Vec> { + let runner = package + .packages() + .flat_map(|package| { + let target = backend.to_target(); + let features = package.features(target, backend, globals.partial); + into_iter(features).map(move |f| (package, target, f)) + }) + .map(move |(package, target, features)| { + let target = target.into(); + let command = match operation { + BuildOrCheck::Check => CargoCommand::Check { + cargoarg, + package: Some(package.name()), + target, + features, + mode: BuildMode::Release, + dir: None, + deny_warnings: globals.deny_warnings, + }, + BuildOrCheck::Build => CargoCommand::Build { + cargoarg, + package: Some(package.name()), + target, + features, + mode: BuildMode::Release, + dir: None, + deny_warnings: globals.deny_warnings, + }, + }; + + (globals, command, false) + }); + + runner.run_and_coalesce() +} + +/// Cargo command to build a usage example. +/// +/// The usage examples are in examples/ +pub fn cargo_usage_example( + globals: &Globals, + operation: BuildOrCheck, + usage_examples: Vec, +) -> Vec> { + into_iter(&usage_examples) + .map(|example| { + let path = format!("examples/{example}"); + + let command = match operation { + BuildOrCheck::Check => CargoCommand::Check { + cargoarg: &None, + mode: BuildMode::Release, + dir: Some(path.into()), + package: None, + target: None, + features: None, + deny_warnings: globals.deny_warnings, + }, + BuildOrCheck::Build => CargoCommand::Build { + cargoarg: &None, + package: None, + target: None, + features: None, + mode: BuildMode::Release, + dir: Some(path.into()), + deny_warnings: globals.deny_warnings, + }, + }; + (globals, command, false) + }) + .run_and_coalesce() +} + +/// Cargo command to either build or check all examples +/// +/// The examples are in rtic/examples +pub fn cargo_example<'c>( + globals: &Globals, + operation: BuildOrCheck, + cargoarg: &'c Option<&'c str>, + backend: Backends, + examples: &'c [String], +) -> Vec> { + let runner = into_iter(examples).map(|example| { + let features = Some(backend.to_target().and_features(backend.to_rtic_feature())); + + let command = match operation { + BuildOrCheck::Check => CargoCommand::ExampleCheck { + cargoarg, + example, + target: Some(backend.to_target()), + features, + mode: BuildMode::Release, + deny_warnings: globals.deny_warnings, + }, + BuildOrCheck::Build => CargoCommand::ExampleBuild { + cargoarg, + example, + target: Some(backend.to_target()), + features, + mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), + deny_warnings: globals.deny_warnings, + }, + }; + (globals, command, false) + }); + runner.run_and_coalesce() +} + +/// Run cargo clippy on selected package +pub fn cargo_clippy<'c>( + globals: &Globals, + cargoarg: &'c Option<&'c str>, + package: &'c PackageOpt, + backend: Backends, +) -> Vec> { + let runner = package + .packages() + .flat_map(|package| { + let target = backend.to_target(); + let features = package.features(target, backend, globals.partial); + into_iter(features).map(move |f| (package, target, f)) + }) + .map(move |(package, target, features)| { + let command = CargoCommand::Clippy { + cargoarg, + package: Some(package.name()), + target: target.into(), + features, + deny_warnings: true, + }; + + (globals, command, false) + }); + + runner.run_and_coalesce() +} + +/// Run cargo fmt on selected package +pub fn cargo_format<'c>( + globals: &Globals, + cargoarg: &'c Option<&'c str>, + package: &'c PackageOpt, + check_only: bool, +) -> Vec> { + let runner = package.packages().map(|p| { + ( + globals, + CargoCommand::Format { + cargoarg, + package: Some(p.name()), + check_only, + }, + false, + ) + }); + runner.run_and_coalesce() +} + +/// Run cargo doc +pub fn cargo_doc<'c>( + globals: &Globals, + cargoarg: &'c Option<&'c str>, + backend: Backends, + arguments: &'c Option, +) -> Vec> { + let features = Some(backend.to_target().and_features(backend.to_rtic_feature())); + + let command = CargoCommand::Doc { + cargoarg, + features, + arguments: arguments.clone(), + deny_warnings: true, + }; + + vec![run_and_convert((globals, command, false))] +} + +/// Run cargo test on the selected package or all packages +/// +/// If no package is specified, loop through all packages +pub fn cargo_test<'c>( + globals: &Globals, + package: &'c PackageOpt, + backend: Backends, +) -> Vec> { + package + .packages() + .map(|p| { + let meta = TestMetadata::match_package(globals.deny_warnings, p, backend); + (globals, meta, false) + }) + .run_and_coalesce() +} + +/// Use mdbook to build the book +pub fn cargo_book<'c>( + globals: &Globals, + arguments: &'c Option, +) -> Vec> { + vec![run_and_convert(( + globals, + CargoCommand::Book { + arguments: arguments.clone(), + }, + false, + ))] +} + +/// Run examples +/// +/// Supports updating the expected output via the overwrite argument +pub fn qemu_run_examples<'c>( + globals: &Globals, + cargoarg: &'c Option<&'c str>, + backend: Backends, + examples: &'c [String], + overwrite: bool, +) -> Vec> { + let target = backend.to_target(); + let features = Some(target.and_features(backend.to_rtic_feature())); + + into_iter(examples) + .flat_map(|example| { + let target = target.into(); + let cmd_build = CargoCommand::ExampleBuild { + cargoarg: &None, + example, + target, + features: features.clone(), + mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), + deny_warnings: globals.deny_warnings, + }; + + let cmd_qemu = CargoCommand::Qemu { + cargoarg, + example, + target, + features: features.clone(), + mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), + deny_warnings: globals.deny_warnings, + }; + + into_iter([cmd_build, cmd_qemu]) + }) + .map(|cmd| (globals, cmd, overwrite)) + .run_and_coalesce() +} + +/// Check the binary sizes of examples +pub fn build_and_check_size<'c>( + globals: &Globals, + cargoarg: &'c Option<&'c str>, + backend: Backends, + examples: &'c [String], + arguments: &'c Option, +) -> Vec> { + let target = backend.to_target(); + let features = Some(target.and_features(backend.to_rtic_feature())); + + let runner = into_iter(examples).map(|example| { + let target = target.into(); + + // Make sure the requested example(s) are built + let cmd = CargoCommand::ExampleBuild { + cargoarg: &Some("--quiet"), + example, + target, + features: features.clone(), + mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), + deny_warnings: globals.deny_warnings, + }; + + if let Err(err) = command_parser(globals, &cmd, false) { + error!("{err}"); + } + + let cmd = CargoCommand::ExampleSize { + cargoarg, + example, + target, + features: features.clone(), + mode: BuildMode::Release, + arguments: arguments.clone(), + dir: Some(PathBuf::from("./rtic")), + }; + (globals, cmd, false) + }); + + runner.run_and_coalesce() +} + +fn run_command( + command: &CargoCommand, + stderr_mode: OutputMode, + print_command_success: bool, +) -> anyhow::Result { + log::info!("👟 {command}"); + + let mut process = Command::new(command.executable()); + + process + .args(command.args()) + .stdout(Stdio::piped()) + .stderr(stderr_mode); + + if let Some(dir) = command.chdir() { + process.current_dir(dir.canonicalize()?); + } + + if let Some((k, v)) = command.extra_env() { + process.env(k, v); + } + + let result = process.output()?; + + let exit_status = result.status; + let stderr = String::from_utf8(result.stderr).unwrap_or("Not displayable".into()); + let stdout = String::from_utf8(result.stdout).unwrap_or("Not displayable".into()); + + if command.print_stdout_intermediate() && exit_status.success() { + log::info!("\n{}", stdout); + } + + if print_command_success { + if exit_status.success() { + log::info!("✅ Success.") + } else { + log::error!("❌ Command failed. Run to completion for the summary."); + } + } + + Ok(RunResult { + exit_status, + stdout, + stderr, + }) +} diff --git a/xtask/src/run/mod.rs b/xtask/src/run/mod.rs deleted file mode 100644 index 74179c5..0000000 --- a/xtask/src/run/mod.rs +++ /dev/null @@ -1,499 +0,0 @@ -use std::{ - fs::File, - io::Write, - path::PathBuf, - process::{Command, Stdio}, -}; - -mod results; -pub use results::handle_results; - -mod data; -use data::*; - -mod iter; -use iter::{into_iter, CoalescingRunner}; - -use crate::{ - argument_parsing::{Backends, BuildOrCheck, ExtraArguments, Globals, PackageOpt, TestMetadata}, - cargo_command::{BuildMode, CargoCommand}, -}; - -use log::{error, info}; - -#[cfg(feature = "rayon")] -use rayon::prelude::*; - -fn run_and_convert<'a>( - (global, command, overwrite): (&Globals, CargoCommand<'a>, bool), -) -> FinalRunResult<'a> { - // Run the command - let result = command_parser(global, &command, overwrite); - - let output = match result { - // If running the command succeeded without looking at any of the results, - // log the data and see if the actual execution was succesfull too. - Ok(result) => { - if result.exit_status.success() { - FinalRunResult::Success(command, result) - } else { - FinalRunResult::Failed(command, result) - } - } - // If it didn't and some IO error occured, just panic - Err(e) => FinalRunResult::CommandError(command, e), - }; - - log::trace!("Final result: {output:?}"); - - output -} - -// run example binary `example` -fn command_parser( - glob: &Globals, - command: &CargoCommand, - overwrite: bool, -) -> anyhow::Result { - let output_mode = if glob.stderr_inherited { - OutputMode::Inherited - } else { - OutputMode::PipedAndCollected - }; - - match *command { - CargoCommand::Qemu { example, .. } | CargoCommand::Run { example, .. } => { - /// Check if `run` was successful. - /// returns Ok in case the run went as expected, - /// Err otherwise - pub fn run_successful( - run: &RunResult, - expected_output_file: &str, - ) -> Result<(), TestRunError> { - let file = expected_output_file.to_string(); - - let expected_output = std::fs::read(expected_output_file) - .map(|d| { - String::from_utf8(d) - .map_err(|_| TestRunError::FileError { file: file.clone() }) - }) - .map_err(|_| TestRunError::FileError { file })??; - - let res = if expected_output != run.stdout { - Err(TestRunError::FileCmpError { - expected: expected_output.clone(), - got: run.stdout.clone(), - }) - } else if !run.exit_status.success() { - Err(TestRunError::CommandError(run.clone())) - } else { - Ok(()) - }; - - if res.is_ok() { - log::info!("✅ Success."); - } else { - log::error!("❌ Command failed. Run to completion for the summary."); - } - - res - } - - let run_file = format!("{example}.run"); - let expected_output_file = ["rtic", "ci", "expected", &run_file] - .iter() - .collect::() - .into_os_string() - .into_string() - .map_err(TestRunError::PathConversionError)?; - - // cargo run <..> - let cargo_run_result = run_command(command, output_mode, false)?; - - // Create a file for the expected output if it does not exist or mismatches - if overwrite { - let result = run_successful(&cargo_run_result, &expected_output_file); - if let Err(e) = result { - // FileError means the file did not exist or was unreadable - error!("Error: {e}"); - let mut file_handle = File::create(&expected_output_file).map_err(|_| { - TestRunError::FileError { - file: expected_output_file.clone(), - } - })?; - info!("Flag --overwrite-expected enabled"); - info!("Creating/updating file: {expected_output_file}"); - file_handle.write_all(cargo_run_result.stdout.as_bytes())?; - }; - } else { - run_successful(&cargo_run_result, &expected_output_file)?; - }; - - Ok(cargo_run_result) - } - CargoCommand::Format { .. } - | CargoCommand::ExampleCheck { .. } - | CargoCommand::ExampleBuild { .. } - | CargoCommand::Check { .. } - | CargoCommand::Build { .. } - | CargoCommand::Clippy { .. } - | CargoCommand::Doc { .. } - | CargoCommand::Test { .. } - | CargoCommand::Book { .. } - | CargoCommand::ExampleSize { .. } => { - let cargo_result = run_command(command, output_mode, true)?; - Ok(cargo_result) - } - } -} - -/// Cargo command to either build or check -pub fn cargo<'c>( - globals: &Globals, - operation: BuildOrCheck, - cargoarg: &'c Option<&'c str>, - package: &'c PackageOpt, - backend: Backends, -) -> Vec> { - let runner = package - .packages() - .flat_map(|package| { - let target = backend.to_target(); - let features = package.features(target, backend, globals.partial); - into_iter(features).map(move |f| (package, target, f)) - }) - .map(move |(package, target, features)| { - let target = target.into(); - let command = match operation { - BuildOrCheck::Check => CargoCommand::Check { - cargoarg, - package: Some(package.name()), - target, - features, - mode: BuildMode::Release, - dir: None, - deny_warnings: globals.deny_warnings, - }, - BuildOrCheck::Build => CargoCommand::Build { - cargoarg, - package: Some(package.name()), - target, - features, - mode: BuildMode::Release, - dir: None, - deny_warnings: globals.deny_warnings, - }, - }; - - (globals, command, false) - }); - - runner.run_and_coalesce() -} - -/// Cargo command to build a usage example. -/// -/// The usage examples are in examples/ -pub fn cargo_usage_example( - globals: &Globals, - operation: BuildOrCheck, - usage_examples: Vec, -) -> Vec> { - into_iter(&usage_examples) - .map(|example| { - let path = format!("examples/{example}"); - - let command = match operation { - BuildOrCheck::Check => CargoCommand::Check { - cargoarg: &None, - mode: BuildMode::Release, - dir: Some(path.into()), - package: None, - target: None, - features: None, - deny_warnings: globals.deny_warnings, - }, - BuildOrCheck::Build => CargoCommand::Build { - cargoarg: &None, - package: None, - target: None, - features: None, - mode: BuildMode::Release, - dir: Some(path.into()), - deny_warnings: globals.deny_warnings, - }, - }; - (globals, command, false) - }) - .run_and_coalesce() -} - -/// Cargo command to either build or check all examples -/// -/// The examples are in rtic/examples -pub fn cargo_example<'c>( - globals: &Globals, - operation: BuildOrCheck, - cargoarg: &'c Option<&'c str>, - backend: Backends, - examples: &'c [String], -) -> Vec> { - let runner = into_iter(examples).map(|example| { - let features = Some(backend.to_target().and_features(backend.to_rtic_feature())); - - let command = match operation { - BuildOrCheck::Check => CargoCommand::ExampleCheck { - cargoarg, - example, - target: Some(backend.to_target()), - features, - mode: BuildMode::Release, - deny_warnings: globals.deny_warnings, - }, - BuildOrCheck::Build => CargoCommand::ExampleBuild { - cargoarg, - example, - target: Some(backend.to_target()), - features, - mode: BuildMode::Release, - dir: Some(PathBuf::from("./rtic")), - deny_warnings: globals.deny_warnings, - }, - }; - (globals, command, false) - }); - runner.run_and_coalesce() -} - -/// Run cargo clippy on selected package -pub fn cargo_clippy<'c>( - globals: &Globals, - cargoarg: &'c Option<&'c str>, - package: &'c PackageOpt, - backend: Backends, -) -> Vec> { - let runner = package - .packages() - .flat_map(|package| { - let target = backend.to_target(); - let features = package.features(target, backend, globals.partial); - into_iter(features).map(move |f| (package, target, f)) - }) - .map(move |(package, target, features)| { - let command = CargoCommand::Clippy { - cargoarg, - package: Some(package.name()), - target: target.into(), - features, - deny_warnings: true, - }; - - (globals, command, false) - }); - - runner.run_and_coalesce() -} - -/// Run cargo fmt on selected package -pub fn cargo_format<'c>( - globals: &Globals, - cargoarg: &'c Option<&'c str>, - package: &'c PackageOpt, - check_only: bool, -) -> Vec> { - let runner = package.packages().map(|p| { - ( - globals, - CargoCommand::Format { - cargoarg, - package: Some(p.name()), - check_only, - }, - false, - ) - }); - runner.run_and_coalesce() -} - -/// Run cargo doc -pub fn cargo_doc<'c>( - globals: &Globals, - cargoarg: &'c Option<&'c str>, - backend: Backends, - arguments: &'c Option, -) -> Vec> { - let features = Some(backend.to_target().and_features(backend.to_rtic_feature())); - - let command = CargoCommand::Doc { - cargoarg, - features, - arguments: arguments.clone(), - deny_warnings: true, - }; - - vec![run_and_convert((globals, command, false))] -} - -/// Run cargo test on the selected package or all packages -/// -/// If no package is specified, loop through all packages -pub fn cargo_test<'c>( - globals: &Globals, - package: &'c PackageOpt, - backend: Backends, -) -> Vec> { - package - .packages() - .map(|p| { - let meta = TestMetadata::match_package(globals.deny_warnings, p, backend); - (globals, meta, false) - }) - .run_and_coalesce() -} - -/// Use mdbook to build the book -pub fn cargo_book<'c>( - globals: &Globals, - arguments: &'c Option, -) -> Vec> { - vec![run_and_convert(( - globals, - CargoCommand::Book { - arguments: arguments.clone(), - }, - false, - ))] -} - -/// Run examples -/// -/// Supports updating the expected output via the overwrite argument -pub fn qemu_run_examples<'c>( - globals: &Globals, - cargoarg: &'c Option<&'c str>, - backend: Backends, - examples: &'c [String], - overwrite: bool, -) -> Vec> { - let target = backend.to_target(); - let features = Some(target.and_features(backend.to_rtic_feature())); - - into_iter(examples) - .flat_map(|example| { - let target = target.into(); - let cmd_build = CargoCommand::ExampleBuild { - cargoarg: &None, - example, - target, - features: features.clone(), - mode: BuildMode::Release, - dir: Some(PathBuf::from("./rtic")), - deny_warnings: globals.deny_warnings, - }; - - let cmd_qemu = CargoCommand::Qemu { - cargoarg, - example, - target, - features: features.clone(), - mode: BuildMode::Release, - dir: Some(PathBuf::from("./rtic")), - deny_warnings: globals.deny_warnings, - }; - - into_iter([cmd_build, cmd_qemu]) - }) - .map(|cmd| (globals, cmd, overwrite)) - .run_and_coalesce() -} - -/// Check the binary sizes of examples -pub fn build_and_check_size<'c>( - globals: &Globals, - cargoarg: &'c Option<&'c str>, - backend: Backends, - examples: &'c [String], - arguments: &'c Option, -) -> Vec> { - let target = backend.to_target(); - let features = Some(target.and_features(backend.to_rtic_feature())); - - let runner = into_iter(examples).map(|example| { - let target = target.into(); - - // Make sure the requested example(s) are built - let cmd = CargoCommand::ExampleBuild { - cargoarg: &Some("--quiet"), - example, - target, - features: features.clone(), - mode: BuildMode::Release, - dir: Some(PathBuf::from("./rtic")), - deny_warnings: globals.deny_warnings, - }; - - if let Err(err) = command_parser(globals, &cmd, false) { - error!("{err}"); - } - - let cmd = CargoCommand::ExampleSize { - cargoarg, - example, - target, - features: features.clone(), - mode: BuildMode::Release, - arguments: arguments.clone(), - dir: Some(PathBuf::from("./rtic")), - }; - (globals, cmd, false) - }); - - runner.run_and_coalesce() -} - -fn run_command( - command: &CargoCommand, - stderr_mode: OutputMode, - print_command_success: bool, -) -> anyhow::Result { - log::info!("👟 {command}"); - - let mut process = Command::new(command.executable()); - - process - .args(command.args()) - .stdout(Stdio::piped()) - .stderr(stderr_mode); - - if let Some(dir) = command.chdir() { - process.current_dir(dir.canonicalize()?); - } - - if let Some((k, v)) = command.extra_env() { - process.env(k, v); - } - - let result = process.output()?; - - let exit_status = result.status; - let stderr = String::from_utf8(result.stderr).unwrap_or("Not displayable".into()); - let stdout = String::from_utf8(result.stdout).unwrap_or("Not displayable".into()); - - if command.print_stdout_intermediate() && exit_status.success() { - log::info!("\n{}", stdout); - } - - if print_command_success { - if exit_status.success() { - log::info!("✅ Success.") - } else { - log::error!("❌ Command failed. Run to completion for the summary."); - } - } - - Ok(RunResult { - exit_status, - stdout, - stderr, - }) -} -- cgit v1.2.3 From 319c2263f3cebd5d990ba5fee277ec9cf1b1d2f9 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 16 Apr 2023 13:27:46 +0200 Subject: Also print extra env variables as cmd_str --- xtask/src/cargo_command.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'xtask') diff --git a/xtask/src/cargo_command.rs b/xtask/src/cargo_command.rs index b0102ce..401bab4 100644 --- a/xtask/src/cargo_command.rs +++ b/xtask/src/cargo_command.rs @@ -355,6 +355,12 @@ impl core::fmt::Display for CargoCommand<'_> { impl<'a> CargoCommand<'a> { pub fn as_cmd_string(&self) -> String { + let env = if let Some((key, value)) = self.extra_env() { + format!("{key}=\"{value}\" ") + } else { + format!("") + }; + let cd = if let Some(Some(chdir)) = self.chdir().map(|p| p.to_str()) { format!("cd {chdir} && ") } else { @@ -363,7 +369,7 @@ impl<'a> CargoCommand<'a> { let executable = self.executable(); let args = self.args().join(" "); - format!("{cd}{executable} {args}") + format!("{env}{cd}{executable} {args}") } fn command(&self) -> &'static str { -- cgit v1.2.3 From 0ee2d2c2dbd794dd5f77281de3368c22ea4e7e33 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 16 Apr 2023 13:27:54 +0200 Subject: Actually chain these --- xtask/src/run.rs | 53 ++++++++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 27 deletions(-) (limited to 'xtask') diff --git a/xtask/src/run.rs b/xtask/src/run.rs index 74179c5..13d2e22 100644 --- a/xtask/src/run.rs +++ b/xtask/src/run.rs @@ -418,35 +418,34 @@ pub fn build_and_check_size<'c>( let target = backend.to_target(); let features = Some(target.and_features(backend.to_rtic_feature())); - let runner = into_iter(examples).map(|example| { - let target = target.into(); - - // Make sure the requested example(s) are built - let cmd = CargoCommand::ExampleBuild { - cargoarg: &Some("--quiet"), - example, - target, - features: features.clone(), - mode: BuildMode::Release, - dir: Some(PathBuf::from("./rtic")), - deny_warnings: globals.deny_warnings, - }; + let runner = into_iter(examples) + .flat_map(|example| { + let target = target.into(); - if let Err(err) = command_parser(globals, &cmd, false) { - error!("{err}"); - } + // Make sure the requested example(s) are built + let cmd_build = CargoCommand::ExampleBuild { + cargoarg: &Some("--quiet"), + example, + target, + features: features.clone(), + mode: BuildMode::Release, + dir: Some(PathBuf::from("./rtic")), + deny_warnings: globals.deny_warnings, + }; - let cmd = CargoCommand::ExampleSize { - cargoarg, - example, - target, - features: features.clone(), - mode: BuildMode::Release, - arguments: arguments.clone(), - dir: Some(PathBuf::from("./rtic")), - }; - (globals, cmd, false) - }); + let cmd_size = CargoCommand::ExampleSize { + cargoarg, + example, + target, + features: features.clone(), + mode: BuildMode::Release, + arguments: arguments.clone(), + dir: Some(PathBuf::from("./rtic")), + }; + + [cmd_build, cmd_size] + }) + .map(|cmd| (globals, cmd, false)); runner.run_and_coalesce() } -- cgit v1.2.3 From e4b673646a9e846fabf24d6776e2a915a5c7366d Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 16 Apr 2023 13:30:23 +0200 Subject: Tests should always deny warnings --- xtask/src/argument_parsing.rs | 18 +++++++----------- xtask/src/run.rs | 2 +- 2 files changed, 8 insertions(+), 12 deletions(-) (limited to 'xtask') diff --git a/xtask/src/argument_parsing.rs b/xtask/src/argument_parsing.rs index d7c0262..05d0ae4 100644 --- a/xtask/src/argument_parsing.rs +++ b/xtask/src/argument_parsing.rs @@ -94,11 +94,7 @@ impl Package { pub struct TestMetadata {} impl TestMetadata { - pub fn match_package( - deny_warnings: bool, - package: Package, - backend: Backends, - ) -> CargoCommand<'static> { + pub fn match_package(package: Package, backend: Backends) -> CargoCommand<'static> { match package { Package::Rtic => { let features = format!( @@ -111,38 +107,38 @@ impl TestMetadata { package: Some(package.name()), features, test: Some("ui".to_owned()), - deny_warnings, + deny_warnings: true, } } Package::RticMacros => CargoCommand::Test { package: Some(package.name()), features: Some(backend.to_rtic_macros_feature().to_owned()), test: None, - deny_warnings, + deny_warnings: true, }, Package::RticSync => CargoCommand::Test { package: Some(package.name()), features: Some("testing".to_owned()), test: None, - deny_warnings, + deny_warnings: true, }, Package::RticCommon => CargoCommand::Test { package: Some(package.name()), features: Some("testing".to_owned()), test: None, - deny_warnings, + deny_warnings: true, }, Package::RticMonotonics => CargoCommand::Test { package: Some(package.name()), features: None, test: None, - deny_warnings, + deny_warnings: true, }, Package::RticTime => CargoCommand::Test { package: Some(package.name()), features: Some("critical-section/std".into()), test: None, - deny_warnings, + deny_warnings: true, }, } } diff --git a/xtask/src/run.rs b/xtask/src/run.rs index 13d2e22..bf8d3b7 100644 --- a/xtask/src/run.rs +++ b/xtask/src/run.rs @@ -345,7 +345,7 @@ pub fn cargo_test<'c>( package .packages() .map(|p| { - let meta = TestMetadata::match_package(globals.deny_warnings, p, backend); + let meta = TestMetadata::match_package(p, backend); (globals, meta, false) }) .run_and_coalesce() -- cgit v1.2.3 From b7e4498a7136041d89541bdc7725c8c023fa5c9c Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 16 Apr 2023 14:14:49 +0200 Subject: Also allow denying for QEMU, and fix the link-arg problem caused by overriding RUSTFLAGS --- xtask/src/cargo_command.rs | 24 ++++++++++++++++++++---- xtask/src/run.rs | 7 +++++-- 2 files changed, 25 insertions(+), 6 deletions(-) (limited to 'xtask') diff --git a/xtask/src/cargo_command.rs b/xtask/src/cargo_command.rs index 401bab4..1d5f3c5 100644 --- a/xtask/src/cargo_command.rs +++ b/xtask/src/cargo_command.rs @@ -100,6 +100,7 @@ pub enum CargoCommand<'a> { mode: BuildMode, arguments: Option, dir: Option, + deny_warnings: bool, }, } @@ -345,8 +346,10 @@ impl core::fmt::Display for CargoCommand<'_> { mode, arguments: _, dir, + deny_warnings, } => { - let details = details(false, target, Some(mode), features, cargoarg, dir.as_ref()); + let warns = *deny_warnings; + let details = details(warns, target, Some(mode), features, cargoarg, dir.as_ref()); write!(f, "Compute size of example {example} {details}") } } @@ -645,6 +648,8 @@ impl<'a> CargoCommand<'a> { target: _, // dir is exposed through `chdir` dir: _, + // deny_warnings is exposed through `extra_env` + deny_warnings: _, } => { let extra = ["--example", example] .into_iter() @@ -688,12 +693,23 @@ impl<'a> CargoCommand<'a> { // through an argument to rustc. CargoCommand::Clippy { .. } => None, CargoCommand::Doc { .. } => Some(("RUSTDOCFLAGS", "-D warnings")), + + CargoCommand::Qemu { deny_warnings, .. } + | CargoCommand::ExampleBuild { deny_warnings, .. } + | CargoCommand::ExampleSize { deny_warnings, .. } => { + if *deny_warnings { + // NOTE: this also needs the link-arg because .cargo/config.toml + // is ignored if you set the RUSTFLAGS env variable. + Some(("RUSTFLAGS", "-D warnings -C link-arg=-Tlink.x")) + } else { + None + } + } + CargoCommand::Check { deny_warnings, .. } | CargoCommand::ExampleCheck { deny_warnings, .. } | CargoCommand::Build { deny_warnings, .. } - | CargoCommand::ExampleBuild { deny_warnings, .. } - | CargoCommand::Test { deny_warnings, .. } - | CargoCommand::Qemu { deny_warnings, .. } => { + | CargoCommand::Test { deny_warnings, .. } => { if *deny_warnings { Some(("RUSTFLAGS", "-D warnings")) } else { diff --git a/xtask/src/run.rs b/xtask/src/run.rs index bf8d3b7..6057551 100644 --- a/xtask/src/run.rs +++ b/xtask/src/run.rs @@ -381,13 +381,15 @@ pub fn qemu_run_examples<'c>( into_iter(examples) .flat_map(|example| { let target = target.into(); + let dir = Some(PathBuf::from("./rtic")); + let cmd_build = CargoCommand::ExampleBuild { cargoarg: &None, example, target, features: features.clone(), mode: BuildMode::Release, - dir: Some(PathBuf::from("./rtic")), + dir: dir.clone(), deny_warnings: globals.deny_warnings, }; @@ -397,7 +399,7 @@ pub fn qemu_run_examples<'c>( target, features: features.clone(), mode: BuildMode::Release, - dir: Some(PathBuf::from("./rtic")), + dir, deny_warnings: globals.deny_warnings, }; @@ -441,6 +443,7 @@ pub fn build_and_check_size<'c>( mode: BuildMode::Release, arguments: arguments.clone(), dir: Some(PathBuf::from("./rtic")), + deny_warnings: globals.deny_warnings, }; [cmd_build, cmd_size] -- cgit v1.2.3