2021-08-26 10:58:59 +02:00
|
|
|
mod build;
|
|
|
|
mod command;
|
|
|
|
|
|
|
|
use anyhow::bail;
|
2023-02-06 13:21:04 +01:00
|
|
|
use clap::{Args, Parser, Subcommand};
|
2021-08-26 10:58:59 +02:00
|
|
|
use core::fmt;
|
2023-02-05 02:07:20 +01:00
|
|
|
use rayon::prelude::*;
|
2021-08-26 10:58:59 +02:00
|
|
|
use std::{
|
|
|
|
error::Error,
|
|
|
|
ffi::OsString,
|
2023-02-05 01:50:29 +01:00
|
|
|
fs::File,
|
|
|
|
io::prelude::*,
|
2021-08-26 10:58:59 +02:00
|
|
|
path::{Path, PathBuf},
|
|
|
|
process,
|
|
|
|
process::ExitStatus,
|
|
|
|
str,
|
|
|
|
};
|
2023-02-05 01:50:29 +01:00
|
|
|
|
|
|
|
use env_logger::Env;
|
2023-02-08 22:09:32 +01:00
|
|
|
use exitcode;
|
2023-02-05 01:50:29 +01:00
|
|
|
use log::{debug, error, info, log_enabled, trace, Level};
|
2021-08-26 10:58:59 +02:00
|
|
|
|
|
|
|
use crate::{
|
2021-12-26 10:43:57 +01:00
|
|
|
build::init_build_dir,
|
2021-08-26 10:58:59 +02:00
|
|
|
command::{run_command, run_successful, BuildMode, CargoCommand},
|
|
|
|
};
|
|
|
|
|
2023-02-06 13:21:04 +01:00
|
|
|
// x86_64-unknown-linux-gnu
|
|
|
|
const _X86_64: &str = "x86_64-unknown-linux-gnu";
|
2021-08-26 10:58:59 +02:00
|
|
|
const ARMV6M: &str = "thumbv6m-none-eabi";
|
|
|
|
const ARMV7M: &str = "thumbv7m-none-eabi";
|
2023-02-05 19:39:29 +01:00
|
|
|
const ARMV8MBASE: &str = "thumbv8m.base-none-eabi";
|
|
|
|
const ARMV8MMAIN: &str = "thumbv8m.main-none-eabi";
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2023-02-20 20:09:51 +01:00
|
|
|
const DEFAULT_FEATURES: &str = "test-critical-section";
|
|
|
|
|
|
|
|
#[derive(clap::ValueEnum, Copy, Clone, Default, Debug)]
|
|
|
|
enum Backends {
|
|
|
|
Thumbv6,
|
|
|
|
#[default]
|
|
|
|
Thumbv7,
|
|
|
|
Thumbv8Base,
|
|
|
|
Thumbv8Main,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Backends {
|
|
|
|
fn to_target(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
Backends::Thumbv6 => ARMV6M,
|
|
|
|
Backends::Thumbv7 => ARMV7M,
|
|
|
|
Backends::Thumbv8Base => ARMV8MBASE,
|
|
|
|
Backends::Thumbv8Main => ARMV8MMAIN,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_rtic_feature(&self) -> &str {
|
|
|
|
match self {
|
2023-02-20 20:56:18 +01:00
|
|
|
Backends::Thumbv6 => "thumbv6-backend",
|
|
|
|
Backends::Thumbv7 => "thumbv7-backend",
|
|
|
|
Backends::Thumbv8Base => "thumbv8base-backend",
|
|
|
|
Backends::Thumbv8Main => "thumbv8main-backend",
|
2023-02-20 20:09:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-05 01:50:29 +01:00
|
|
|
|
2023-02-25 00:11:12 +01:00
|
|
|
#[derive(Copy, Clone, Default, Debug)]
|
|
|
|
enum BuildOrCheck {
|
|
|
|
#[default]
|
|
|
|
Check,
|
|
|
|
Build,
|
|
|
|
}
|
|
|
|
|
2023-02-05 01:50:29 +01:00
|
|
|
#[derive(Parser)]
|
|
|
|
#[command(author, version, about, long_about = None)]
|
|
|
|
/// RTIC xtask powered testing toolbox
|
|
|
|
struct Cli {
|
2023-02-24 23:14:11 +01:00
|
|
|
/// For which backend to build (defaults to thumbv7)
|
2023-02-20 20:09:51 +01:00
|
|
|
#[arg(value_enum, short, long)]
|
2023-02-23 19:34:52 +01:00
|
|
|
backend: Option<Backends>,
|
2023-02-05 01:50:29 +01:00
|
|
|
|
2023-02-06 13:21:04 +01:00
|
|
|
/// List of comma separated examples to include, all others are excluded
|
2023-02-05 01:50:29 +01:00
|
|
|
///
|
2023-02-06 13:21:04 +01:00
|
|
|
/// If omitted all examples are included
|
2023-02-04 15:47:23 +01:00
|
|
|
///
|
2023-02-05 01:50:29 +01:00
|
|
|
/// Example: `cargo xtask --example complex,spawn,init`
|
|
|
|
/// would include complex, spawn and init
|
|
|
|
#[arg(short, long, group = "example_group")]
|
2023-02-04 15:47:23 +01:00
|
|
|
example: Option<String>,
|
2023-02-05 01:50:29 +01:00
|
|
|
|
|
|
|
/// List of comma separated examples to exclude, all others are included
|
|
|
|
///
|
2023-02-06 13:21:04 +01:00
|
|
|
/// If omitted all examples are included
|
2023-02-05 01:50:29 +01:00
|
|
|
///
|
|
|
|
/// Example: `cargo xtask --excludeexample complex,spawn,init`
|
|
|
|
/// would exclude complex, spawn and init
|
|
|
|
#[arg(long, group = "example_group")]
|
|
|
|
exampleexclude: Option<String>,
|
|
|
|
|
|
|
|
/// Enable more verbose output, repeat up to `-vvv` for even more
|
|
|
|
#[arg(short, long, action = clap::ArgAction::Count)]
|
|
|
|
verbose: u8,
|
|
|
|
|
2023-02-06 13:21:04 +01:00
|
|
|
/// Subcommand selecting operation
|
2023-02-05 01:50:29 +01:00
|
|
|
#[command(subcommand)]
|
2023-02-06 13:21:04 +01:00
|
|
|
command: Commands,
|
2023-02-05 01:50:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Subcommand)]
|
|
|
|
enum Commands {
|
2023-02-24 23:14:11 +01:00
|
|
|
/// Check formatting
|
|
|
|
FormatCheck(Package),
|
|
|
|
|
|
|
|
/// Format code
|
|
|
|
Format(Package),
|
|
|
|
|
|
|
|
/// Run clippy
|
|
|
|
Clippy(Package),
|
|
|
|
|
|
|
|
/// Check all packages
|
|
|
|
Check(Package),
|
|
|
|
|
|
|
|
/// Build all packages
|
|
|
|
Build(Package),
|
|
|
|
|
|
|
|
/// Check all examples
|
|
|
|
ExampleCheck,
|
|
|
|
|
|
|
|
/// Build all examples
|
|
|
|
ExampleBuild,
|
|
|
|
|
2023-02-05 01:50:29 +01:00
|
|
|
/// Run `cargo size` on selected or all examples
|
2023-02-04 15:22:43 +01:00
|
|
|
///
|
|
|
|
/// To pass options to `cargo size`, add `--` and then the following
|
|
|
|
/// arguments will be passed on
|
|
|
|
///
|
2023-02-05 01:50:29 +01:00
|
|
|
/// Example: `cargo xtask size -- -A`
|
|
|
|
Size(Size),
|
|
|
|
|
|
|
|
/// Run examples in QEMU and compare against expected output
|
|
|
|
///
|
|
|
|
/// Example runtime output is matched against `rtic/ci/expected/`
|
2023-02-06 13:21:04 +01:00
|
|
|
///
|
|
|
|
/// Requires that an ARM target is selected
|
|
|
|
Qemu(QemuAndRun),
|
|
|
|
|
|
|
|
/// Run examples through embedded-ci and compare against expected output
|
|
|
|
///
|
|
|
|
/// unimplemented!() For now TODO, equal to Qemu
|
|
|
|
///
|
|
|
|
/// Example runtime output is matched against `rtic/ci/expected/`
|
|
|
|
///
|
|
|
|
/// Requires that an ARM target is selected
|
|
|
|
Run(QemuAndRun),
|
|
|
|
|
2023-02-24 22:56:36 +01:00
|
|
|
/// Build docs
|
|
|
|
Doc,
|
2023-02-06 13:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Args, Debug)]
|
|
|
|
/// Restrict to package, or run on whole workspace
|
|
|
|
struct Package {
|
|
|
|
/// For which package/workspace member to operate
|
|
|
|
///
|
|
|
|
/// If omitted, work on all
|
|
|
|
package: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Args, Debug)]
|
|
|
|
struct QemuAndRun {
|
|
|
|
/// If expected output is missing or mismatching, recreate the file
|
|
|
|
///
|
|
|
|
/// This overwrites only missing or mismatching
|
|
|
|
#[arg(long)]
|
|
|
|
overwrite_expected: bool,
|
2023-02-05 01:50:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Parser)]
|
|
|
|
struct Size {
|
2023-02-04 15:22:43 +01:00
|
|
|
/// Options to pass to `cargo size`
|
2023-02-05 01:50:29 +01:00
|
|
|
#[command(subcommand)]
|
2023-02-04 15:22:43 +01:00
|
|
|
sizearguments: Option<Sizearguments>,
|
|
|
|
}
|
|
|
|
|
2023-02-05 01:50:29 +01:00
|
|
|
#[derive(Clone, Debug, PartialEq, Parser)]
|
2023-02-04 15:22:43 +01:00
|
|
|
pub enum Sizearguments {
|
2023-02-05 01:50:29 +01:00
|
|
|
/// All remaining flags and options
|
|
|
|
#[command(external_subcommand)]
|
2023-02-04 15:22:43 +01:00
|
|
|
Other(Vec<String>),
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:22:45 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2021-08-26 10:58:59 +02:00
|
|
|
pub struct RunResult {
|
|
|
|
exit_status: ExitStatus,
|
2023-02-08 22:09:32 +01:00
|
|
|
stdout: String,
|
|
|
|
stderr: String,
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2021-09-22 13:22:45 +02:00
|
|
|
pub enum TestRunError {
|
|
|
|
FileCmpError { expected: String, got: String },
|
|
|
|
FileError { file: String },
|
2021-08-26 10:58:59 +02:00
|
|
|
PathConversionError(OsString),
|
|
|
|
CommandError(RunResult),
|
|
|
|
IncompatibleCommand,
|
|
|
|
}
|
2023-02-06 13:03:15 +01:00
|
|
|
use diffy::{create_patch, PatchFormatter};
|
2021-08-26 10:58:59 +02:00
|
|
|
|
|
|
|
impl fmt::Display for TestRunError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2021-09-22 13:22:45 +02:00
|
|
|
TestRunError::FileCmpError { expected, got } => {
|
2023-02-06 13:03:15 +01:00
|
|
|
let patch = create_patch(expected, got);
|
2023-02-04 15:22:43 +01:00
|
|
|
writeln!(f, "Differing output in files.\n")?;
|
2023-02-06 13:03:15 +01:00
|
|
|
let pf = PatchFormatter::new().with_color();
|
|
|
|
writeln!(f, "{}", pf.fmt_patch(&patch))?;
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"See flag --overwrite-expected to create/update expected output."
|
|
|
|
)
|
2021-09-22 13:22:45 +02:00
|
|
|
}
|
|
|
|
TestRunError::FileError { file } => {
|
2023-02-06 13:03:15 +01:00
|
|
|
write!(f, "File error on: {file}\nSee flag --overwrite-expected to create/update expected output.")
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
TestRunError::CommandError(e) => {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Command failed with exit status {}: {}",
|
2023-02-08 22:09:32 +01:00
|
|
|
e.exit_status, e.stdout
|
2021-08-26 10:58:59 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
TestRunError::PathConversionError(p) => {
|
2023-02-04 15:22:43 +01:00
|
|
|
write!(f, "Can't convert path from `OsString` to `String`: {p:?}")
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
TestRunError::IncompatibleCommand => {
|
|
|
|
write!(f, "Can't run that command in this context")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for TestRunError {}
|
|
|
|
|
|
|
|
fn main() -> anyhow::Result<()> {
|
2021-09-17 15:44:33 +02:00
|
|
|
// 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)
|
|
|
|
let probably_running_from_repo_root = Path::new("./xtask").exists();
|
2023-02-04 15:22:43 +01:00
|
|
|
if !probably_running_from_repo_root {
|
|
|
|
bail!("xtasks can only be executed from the root of the `rtic` repository");
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
|
2023-02-05 19:39:29 +01:00
|
|
|
let examples: Vec<_> = std::fs::read_dir("./rtic/examples")?
|
2023-01-08 19:56:47 +01:00
|
|
|
.filter_map(|p| p.ok())
|
|
|
|
.map(|p| p.path())
|
|
|
|
.filter(|p| p.display().to_string().ends_with(".rs"))
|
|
|
|
.map(|path| path.file_stem().unwrap().to_str().unwrap().to_string())
|
2021-09-22 13:22:45 +02:00
|
|
|
.collect();
|
|
|
|
|
2023-02-05 01:50:29 +01:00
|
|
|
let cli = Cli::parse();
|
|
|
|
|
|
|
|
let env_logger_default_level = match cli.verbose {
|
|
|
|
0 => Env::default().default_filter_or("error"),
|
|
|
|
1 => Env::default().default_filter_or("info"),
|
|
|
|
2 => Env::default().default_filter_or("debug"),
|
|
|
|
_ => Env::default().default_filter_or("trace"),
|
|
|
|
};
|
|
|
|
env_logger::Builder::from_env(env_logger_default_level)
|
|
|
|
.format_module_path(false)
|
|
|
|
.format_timestamp(None)
|
|
|
|
.init();
|
2023-01-08 19:56:47 +01:00
|
|
|
|
2023-02-05 01:50:29 +01:00
|
|
|
trace!("default logging level: {0}", cli.verbose);
|
|
|
|
|
2023-02-23 19:34:52 +01:00
|
|
|
let backend = if let Some(backend) = cli.backend {
|
|
|
|
backend
|
|
|
|
} else {
|
|
|
|
Backends::default()
|
|
|
|
};
|
2023-02-05 01:50:29 +01:00
|
|
|
|
2023-02-06 13:21:04 +01:00
|
|
|
let example = cli.example;
|
|
|
|
let exampleexclude = cli.exampleexclude;
|
|
|
|
|
|
|
|
let examples_to_run = {
|
|
|
|
let mut examples_to_run = examples.clone();
|
|
|
|
|
|
|
|
if let Some(example) = example {
|
|
|
|
examples_to_run = examples.clone();
|
|
|
|
let examples_to_exclude = example.split(',').collect::<Vec<&str>>();
|
|
|
|
// From the list of all examples, remove all not listed as included
|
|
|
|
for ex in examples_to_exclude {
|
|
|
|
examples_to_run.retain(|x| *x.as_str() == *ex);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(example) = exampleexclude {
|
|
|
|
examples_to_run = examples.clone();
|
|
|
|
let examples_to_exclude = example.split(',').collect::<Vec<&str>>();
|
|
|
|
// From the list of all examples, remove all those listed as excluded
|
|
|
|
for ex in examples_to_exclude {
|
|
|
|
examples_to_run.retain(|x| *x.as_str() != *ex);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if log_enabled!(Level::Trace) {
|
|
|
|
trace!("All examples:\n{examples:?} number: {}", examples.len());
|
|
|
|
trace!(
|
|
|
|
"examples_to_run:\n{examples_to_run:?} number: {}",
|
|
|
|
examples_to_run.len()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if examples_to_run.is_empty() {
|
|
|
|
error!(
|
|
|
|
"\nThe example(s) you specified is not available. Available examples are:\
|
|
|
|
\n{examples:#?}\n\
|
|
|
|
By default if example flag is emitted, all examples are tested.",
|
|
|
|
);
|
2023-02-08 22:09:32 +01:00
|
|
|
process::exit(exitcode::USAGE);
|
2023-02-06 13:21:04 +01:00
|
|
|
} else {
|
|
|
|
}
|
|
|
|
examples_to_run
|
|
|
|
};
|
|
|
|
|
2021-09-20 17:35:15 +02:00
|
|
|
init_build_dir()?;
|
2023-02-05 01:50:29 +01:00
|
|
|
#[allow(clippy::if_same_then_else)]
|
2023-02-24 23:31:49 +01:00
|
|
|
let cargologlevel = if log_enabled!(Level::Trace) {
|
2023-02-05 01:50:29 +01:00
|
|
|
Some("-v")
|
2023-02-06 13:21:04 +01:00
|
|
|
} else if log_enabled!(Level::Debug) {
|
|
|
|
None
|
2023-02-05 01:50:29 +01:00
|
|
|
} else if log_enabled!(Level::Info) {
|
|
|
|
None
|
|
|
|
} else if log_enabled!(Level::Warn) || log_enabled!(Level::Error) {
|
2023-02-06 13:21:04 +01:00
|
|
|
None
|
2023-02-05 01:50:29 +01:00
|
|
|
} else {
|
|
|
|
// Off case
|
|
|
|
Some("--quiet")
|
|
|
|
};
|
2021-09-20 17:35:15 +02:00
|
|
|
|
2023-02-05 01:50:29 +01:00
|
|
|
match cli.command {
|
2023-02-24 23:14:11 +01:00
|
|
|
Commands::FormatCheck(args) => {
|
|
|
|
info!("Running cargo fmt: {args:?}");
|
|
|
|
let check_only = true;
|
2023-02-24 23:31:49 +01:00
|
|
|
cargo_format(&cargologlevel, &args, check_only)?;
|
2023-02-24 23:14:11 +01:00
|
|
|
}
|
|
|
|
Commands::Format(args) => {
|
|
|
|
info!("Running cargo fmt --check: {args:?}");
|
|
|
|
let check_only = false;
|
2023-02-24 23:31:49 +01:00
|
|
|
cargo_format(&cargologlevel, &args, check_only)?;
|
2023-02-24 23:14:11 +01:00
|
|
|
}
|
|
|
|
Commands::Clippy(args) => {
|
|
|
|
info!("Running clippy on backend: {backend:?}");
|
2023-02-24 23:31:49 +01:00
|
|
|
cargo_clippy(&cargologlevel, &args, backend)?;
|
2023-02-24 23:14:11 +01:00
|
|
|
}
|
|
|
|
Commands::Check(args) => {
|
|
|
|
info!("Checking on backend: {backend:?}");
|
2023-02-25 00:11:12 +01:00
|
|
|
cargo(BuildOrCheck::Check, &cargologlevel, &args, backend)?;
|
2023-02-24 23:14:11 +01:00
|
|
|
}
|
|
|
|
Commands::Build(args) => {
|
|
|
|
info!("Building for backend: {backend:?}");
|
2023-02-25 00:11:12 +01:00
|
|
|
cargo(BuildOrCheck::Build, &cargologlevel, &args, backend)?;
|
2023-02-24 23:14:11 +01:00
|
|
|
}
|
|
|
|
Commands::ExampleCheck => {
|
|
|
|
info!("Checking on backend: {backend:?}");
|
2023-02-25 00:11:12 +01:00
|
|
|
cargo_example(
|
|
|
|
BuildOrCheck::Check,
|
|
|
|
&cargologlevel,
|
|
|
|
backend,
|
|
|
|
&examples_to_run,
|
|
|
|
)?;
|
2023-02-24 23:14:11 +01:00
|
|
|
}
|
|
|
|
Commands::ExampleBuild => {
|
|
|
|
info!("Building for backend: {backend:?}");
|
2023-02-25 00:11:12 +01:00
|
|
|
cargo_example(
|
|
|
|
BuildOrCheck::Build,
|
|
|
|
&cargologlevel,
|
|
|
|
backend,
|
|
|
|
&examples_to_run,
|
|
|
|
)?;
|
2023-02-24 23:14:11 +01:00
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
Commands::Size(arguments) => {
|
|
|
|
// x86_64 target not valid
|
2023-02-20 20:09:51 +01:00
|
|
|
info!("Measuring for backend: {backend:?}");
|
|
|
|
build_and_check_size(
|
2023-02-24 23:31:49 +01:00
|
|
|
&cargologlevel,
|
2023-02-20 20:09:51 +01:00
|
|
|
backend,
|
|
|
|
&examples_to_run,
|
|
|
|
&arguments.sizearguments,
|
|
|
|
)?;
|
2023-02-04 16:55:29 +01:00
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
Commands::Qemu(args) | Commands::Run(args) => {
|
|
|
|
// x86_64 target not valid
|
2023-02-20 20:09:51 +01:00
|
|
|
info!("Testing for backend: {backend:?}");
|
|
|
|
run_test(
|
2023-02-24 23:31:49 +01:00
|
|
|
&cargologlevel,
|
2023-02-20 20:09:51 +01:00
|
|
|
backend,
|
|
|
|
&examples_to_run,
|
|
|
|
args.overwrite_expected,
|
|
|
|
)?;
|
2023-02-05 01:50:29 +01:00
|
|
|
}
|
2023-02-24 22:56:36 +01:00
|
|
|
Commands::Doc => {
|
|
|
|
info!("Running cargo doc on backend: {backend:?}");
|
2023-02-24 23:31:49 +01:00
|
|
|
cargo_doc(&cargologlevel, backend)?;
|
2023-02-24 22:56:36 +01:00
|
|
|
}
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-25 00:11:12 +01:00
|
|
|
fn cargo(
|
|
|
|
operation: BuildOrCheck,
|
2023-02-20 20:09:51 +01:00
|
|
|
cargoarg: &Option<&str>,
|
|
|
|
package: &Package,
|
|
|
|
backend: Backends,
|
|
|
|
) -> anyhow::Result<()> {
|
2023-02-25 00:11:12 +01:00
|
|
|
// rtic crate has features which needs special handling
|
|
|
|
let rtic_features = &format!("{},{}", DEFAULT_FEATURES, backend.to_rtic_feature());
|
|
|
|
let features: Option<&str>;
|
|
|
|
let packages = package_filter(package);
|
|
|
|
features = if packages.contains(&"rtic".to_owned()) {
|
|
|
|
Some(&rtic_features)
|
2023-02-24 01:05:21 +01:00
|
|
|
} else {
|
2023-02-25 00:11:12 +01:00
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let command = match operation {
|
|
|
|
BuildOrCheck::Check => CargoCommand::Check {
|
|
|
|
cargoarg,
|
|
|
|
package: packages,
|
|
|
|
target: backend.to_target(),
|
|
|
|
features,
|
|
|
|
mode: BuildMode::Release,
|
|
|
|
},
|
|
|
|
BuildOrCheck::Build => CargoCommand::Build {
|
|
|
|
cargoarg,
|
|
|
|
package: packages,
|
|
|
|
target: backend.to_target(),
|
|
|
|
features,
|
|
|
|
mode: BuildMode::Release,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
command_parser(&command, false)?;
|
2023-02-05 01:50:29 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-25 00:11:12 +01:00
|
|
|
fn cargo_example(
|
|
|
|
operation: BuildOrCheck,
|
2023-02-20 20:09:51 +01:00
|
|
|
cargoarg: &Option<&str>,
|
|
|
|
backend: Backends,
|
2023-02-25 00:11:12 +01:00
|
|
|
examples: &[String],
|
2023-02-20 20:09:51 +01:00
|
|
|
) -> anyhow::Result<()> {
|
2023-02-25 00:11:12 +01:00
|
|
|
let s = format!("{},{}", DEFAULT_FEATURES, backend.to_rtic_feature());
|
|
|
|
let features: Option<&str> = Some(&s);
|
2023-02-24 01:05:21 +01:00
|
|
|
|
2023-02-25 00:11:12 +01:00
|
|
|
examples.into_par_iter().for_each(|example| {
|
|
|
|
let command = match operation {
|
|
|
|
BuildOrCheck::Check => CargoCommand::ExampleCheck {
|
2023-02-24 01:05:21 +01:00
|
|
|
cargoarg,
|
2023-02-25 00:11:12 +01:00
|
|
|
example,
|
2023-02-24 01:05:21 +01:00
|
|
|
target: backend.to_target(),
|
|
|
|
features,
|
2023-02-24 23:14:11 +01:00
|
|
|
mode: BuildMode::Release,
|
2023-02-24 01:05:21 +01:00
|
|
|
},
|
2023-02-25 00:11:12 +01:00
|
|
|
BuildOrCheck::Build => CargoCommand::ExampleBuild {
|
2023-02-24 01:05:21 +01:00
|
|
|
cargoarg,
|
2023-02-25 00:11:12 +01:00
|
|
|
example,
|
2023-02-24 01:05:21 +01:00
|
|
|
target: backend.to_target(),
|
2023-02-25 00:11:12 +01:00
|
|
|
features,
|
2023-02-24 23:14:11 +01:00
|
|
|
mode: BuildMode::Release,
|
2023-02-24 01:05:21 +01:00
|
|
|
},
|
2023-02-25 00:11:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(err) = command_parser(&command, false) {
|
|
|
|
error!("{err}");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-06 13:21:04 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-20 20:09:51 +01:00
|
|
|
fn cargo_clippy(
|
|
|
|
cargoarg: &Option<&str>,
|
|
|
|
package: &Package,
|
|
|
|
backend: Backends,
|
|
|
|
) -> anyhow::Result<()> {
|
2023-02-24 01:05:21 +01:00
|
|
|
let packages_to_check = package_filter(package);
|
|
|
|
if packages_to_check.contains(&"rtic".to_owned()) {
|
|
|
|
// rtic crate has features which needs special handling
|
|
|
|
let s = format!("{},{}", DEFAULT_FEATURES, backend.to_rtic_feature());
|
|
|
|
let features: Option<&str> = Some(&s);
|
|
|
|
|
|
|
|
command_parser(
|
|
|
|
&CargoCommand::Clippy {
|
|
|
|
cargoarg,
|
|
|
|
package: package_filter(package),
|
|
|
|
target: backend.to_target(),
|
|
|
|
features,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
} else {
|
|
|
|
command_parser(
|
|
|
|
&CargoCommand::Clippy {
|
|
|
|
cargoarg,
|
|
|
|
package: package_filter(package),
|
|
|
|
target: backend.to_target(),
|
|
|
|
features: None,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
}
|
2023-02-05 01:50:29 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-24 00:10:01 +01:00
|
|
|
fn cargo_format(
|
|
|
|
cargoarg: &Option<&str>,
|
|
|
|
package: &Package,
|
|
|
|
check_only: bool,
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
command_parser(
|
|
|
|
&CargoCommand::Format {
|
|
|
|
cargoarg,
|
|
|
|
package: package_filter(package),
|
|
|
|
check_only,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-24 22:56:36 +01:00
|
|
|
fn cargo_doc(cargoarg: &Option<&str>, backend: Backends) -> anyhow::Result<()> {
|
|
|
|
let s = format!("{}", backend.to_rtic_feature());
|
|
|
|
let features: Option<&str> = Some(&s);
|
|
|
|
|
|
|
|
command_parser(&CargoCommand::Doc { cargoarg, features }, false)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-04 15:22:43 +01:00
|
|
|
fn run_test(
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &Option<&str>,
|
2023-02-20 20:09:51 +01:00
|
|
|
backend: Backends,
|
2023-02-04 15:22:43 +01:00
|
|
|
examples: &[String],
|
2023-02-05 01:50:29 +01:00
|
|
|
overwrite: bool,
|
2023-02-04 15:22:43 +01:00
|
|
|
) -> anyhow::Result<()> {
|
2023-02-20 20:09:51 +01:00
|
|
|
let s = format!("{},{}", DEFAULT_FEATURES, backend.to_rtic_feature());
|
|
|
|
let features: Option<&str> = Some(&s);
|
|
|
|
|
2023-02-05 02:07:20 +01:00
|
|
|
examples.into_par_iter().for_each(|example| {
|
2023-02-06 13:21:04 +01:00
|
|
|
let cmd = CargoCommand::ExampleBuild {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &Some("--quiet"),
|
|
|
|
example,
|
2023-02-20 20:09:51 +01:00
|
|
|
target: backend.to_target(),
|
|
|
|
features,
|
2023-02-05 01:50:29 +01:00
|
|
|
mode: BuildMode::Release,
|
|
|
|
};
|
2023-02-06 13:21:04 +01:00
|
|
|
if let Err(err) = command_parser(&cmd, false) {
|
|
|
|
error!("{err}");
|
|
|
|
}
|
|
|
|
|
|
|
|
let cmd = CargoCommand::Qemu {
|
|
|
|
cargoarg,
|
|
|
|
example,
|
2023-02-20 20:09:51 +01:00
|
|
|
target: backend.to_target(),
|
|
|
|
features,
|
2023-02-06 13:21:04 +01:00
|
|
|
mode: BuildMode::Release,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(err) = command_parser(&cmd, overwrite) {
|
|
|
|
error!("{err}");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-02-05 01:50:29 +01:00
|
|
|
|
|
|
|
fn build_and_check_size(
|
|
|
|
cargoarg: &Option<&str>,
|
2023-02-20 20:09:51 +01:00
|
|
|
backend: Backends,
|
2023-02-05 01:50:29 +01:00
|
|
|
examples: &[String],
|
|
|
|
size_arguments: &Option<Sizearguments>,
|
|
|
|
) -> anyhow::Result<()> {
|
2023-02-20 20:09:51 +01:00
|
|
|
let s = format!("{},{}", DEFAULT_FEATURES, backend.to_rtic_feature());
|
|
|
|
let features: Option<&str> = Some(&s);
|
|
|
|
|
2023-02-05 02:07:20 +01:00
|
|
|
examples.into_par_iter().for_each(|example| {
|
2023-02-05 01:50:29 +01:00
|
|
|
// Make sure the requested example(s) are built
|
2023-02-06 13:21:04 +01:00
|
|
|
let cmd = CargoCommand::ExampleBuild {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &Some("--quiet"),
|
|
|
|
example,
|
2023-02-20 20:09:51 +01:00
|
|
|
target: backend.to_target(),
|
|
|
|
features,
|
2023-02-05 01:50:29 +01:00
|
|
|
mode: BuildMode::Release,
|
|
|
|
};
|
2023-02-06 13:21:04 +01:00
|
|
|
if let Err(err) = command_parser(&cmd, false) {
|
|
|
|
error!("{err}");
|
|
|
|
}
|
2023-02-05 01:50:29 +01:00
|
|
|
|
2023-02-06 13:21:04 +01:00
|
|
|
let cmd = CargoCommand::ExampleSize {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg,
|
|
|
|
example,
|
2023-02-20 20:09:51 +01:00
|
|
|
target: backend.to_target(),
|
|
|
|
features,
|
2023-02-05 01:50:29 +01:00
|
|
|
mode: BuildMode::Release,
|
|
|
|
arguments: size_arguments.clone(),
|
|
|
|
};
|
2023-02-06 13:21:04 +01:00
|
|
|
if let Err(err) = command_parser(&cmd, false) {
|
|
|
|
error!("{err}");
|
|
|
|
}
|
2023-02-05 02:07:20 +01:00
|
|
|
});
|
2021-08-26 10:58:59 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-06 13:21:04 +01:00
|
|
|
fn package_filter(package: &Package) -> Vec<String> {
|
|
|
|
// TODO Parse Cargo.toml workspace definition instead?
|
|
|
|
let packages: Vec<String> = [
|
|
|
|
"rtic".to_owned(),
|
2023-02-06 13:48:52 +01:00
|
|
|
"rtic-arbiter".to_owned(),
|
2023-02-06 13:21:04 +01:00
|
|
|
"rtic-channel".to_owned(),
|
|
|
|
"rtic-common".to_owned(),
|
|
|
|
"rtic-macros".to_owned(),
|
|
|
|
"rtic-monotonics".to_owned(),
|
|
|
|
"rtic-time".to_owned(),
|
|
|
|
]
|
|
|
|
.to_vec();
|
|
|
|
|
|
|
|
let package_selected;
|
|
|
|
|
|
|
|
if let Some(package) = package.package.clone() {
|
|
|
|
if packages.contains(&package) {
|
|
|
|
debug!("\nTesting package: {package}");
|
|
|
|
// If we managed to filter, set the packages to test to only this one
|
|
|
|
package_selected = vec![package]
|
|
|
|
} else {
|
|
|
|
error!(
|
|
|
|
"\nThe package you specified is not available. Available packages are:\
|
|
|
|
\n{packages:#?}\n\
|
|
|
|
By default all packages are tested.",
|
|
|
|
);
|
2023-02-08 22:09:32 +01:00
|
|
|
process::exit(exitcode::USAGE);
|
2023-02-06 13:21:04 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
package_selected = packages;
|
|
|
|
}
|
|
|
|
package_selected
|
|
|
|
}
|
|
|
|
|
2021-08-26 10:58:59 +02:00
|
|
|
// run example binary `example`
|
2023-02-06 13:21:04 +01:00
|
|
|
fn command_parser(command: &CargoCommand, overwrite: bool) -> anyhow::Result<()> {
|
2021-08-26 10:58:59 +02:00
|
|
|
match *command {
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::Qemu { example, .. } | CargoCommand::Run { example, .. } => {
|
2023-02-04 15:22:43 +01:00
|
|
|
let run_file = format!("{example}.run");
|
2023-02-04 16:47:17 +01:00
|
|
|
let expected_output_file = ["rtic", "ci", "expected", &run_file]
|
2021-08-26 10:58:59 +02:00
|
|
|
.iter()
|
|
|
|
.collect::<PathBuf>()
|
|
|
|
.into_os_string()
|
|
|
|
.into_string()
|
2023-02-04 15:22:43 +01:00
|
|
|
.map_err(TestRunError::PathConversionError)?;
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2023-02-05 01:50:29 +01:00
|
|
|
// cargo run <..>
|
2023-02-06 13:21:04 +01:00
|
|
|
info!("Running example: {example}");
|
2023-02-04 15:22:43 +01:00
|
|
|
let cargo_run_result = run_command(command)?;
|
2023-02-08 22:09:32 +01:00
|
|
|
info!("{}", cargo_run_result.stdout);
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2023-02-05 01:50:29 +01:00
|
|
|
// 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(),
|
|
|
|
}
|
|
|
|
})?;
|
2023-02-06 13:03:15 +01:00
|
|
|
info!("Flag --overwrite-expected enabled");
|
2023-02-05 01:50:29 +01:00
|
|
|
info!("Creating/updating file: {expected_output_file}");
|
2023-02-08 22:09:32 +01:00
|
|
|
file_handle.write_all(cargo_run_result.stdout.as_bytes())?;
|
2023-02-05 01:50:29 +01:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
run_successful(&cargo_run_result, &expected_output_file)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::ExampleBuild { .. }
|
|
|
|
| CargoCommand::ExampleCheck { .. }
|
|
|
|
| CargoCommand::Build { .. }
|
|
|
|
| CargoCommand::Check { .. }
|
|
|
|
| CargoCommand::Clippy { .. }
|
2023-02-24 22:56:36 +01:00
|
|
|
| CargoCommand::Doc { .. }
|
2023-02-24 00:10:01 +01:00
|
|
|
| CargoCommand::Format { .. }
|
2023-02-06 13:21:04 +01:00
|
|
|
| CargoCommand::ExampleSize { .. } => {
|
|
|
|
let cargo_result = run_command(command)?;
|
2023-02-08 22:09:32 +01:00
|
|
|
if let Some(exit_code) = cargo_result.exit_status.code() {
|
|
|
|
if exit_code != exitcode::OK {
|
|
|
|
error!("Exit code from command: {exit_code}");
|
|
|
|
if !cargo_result.stdout.is_empty() {
|
|
|
|
info!("{}", cargo_result.stdout);
|
|
|
|
}
|
|
|
|
if !cargo_result.stderr.is_empty() {
|
|
|
|
error!("{}", cargo_result.stderr);
|
|
|
|
}
|
|
|
|
process::exit(exit_code);
|
|
|
|
} else {
|
|
|
|
if !cargo_result.stdout.is_empty() {
|
|
|
|
info!("{}", cargo_result.stdout);
|
|
|
|
}
|
|
|
|
if !cargo_result.stderr.is_empty() {
|
|
|
|
info!("{}", cargo_result.stderr);
|
|
|
|
}
|
|
|
|
}
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|