2023-03-01 00:10:07 +01:00
|
|
|
mod argument_parsing;
|
2021-08-26 10:58:59 +02:00
|
|
|
mod build;
|
2023-04-16 11:02:49 +02:00
|
|
|
mod cargo_command;
|
2023-04-16 11:05:41 +02:00
|
|
|
mod run;
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2023-04-16 11:26:23 +02:00
|
|
|
use argument_parsing::ExtraArguments;
|
2023-03-01 00:10:07 +01:00
|
|
|
use clap::Parser;
|
2021-08-26 10:58:59 +02:00
|
|
|
use core::fmt;
|
2023-04-16 11:26:23 +02:00
|
|
|
use std::{path::Path, str};
|
2023-02-05 01:50:29 +01:00
|
|
|
|
2023-04-15 13:45:58 +02:00
|
|
|
use log::{error, info, log_enabled, trace, Level};
|
2021-08-26 10:58:59 +02:00
|
|
|
|
|
|
|
use crate::{
|
2023-04-15 10:03:52 +02:00
|
|
|
argument_parsing::{Backends, BuildOrCheck, Cli, Commands},
|
2021-12-26 10:43:57 +01:00
|
|
|
build::init_build_dir,
|
2023-04-16 11:05:41 +02:00
|
|
|
run::*,
|
2021-08-26 10:58:59 +02:00
|
|
|
};
|
|
|
|
|
2023-04-15 00:50:46 +02:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct Target<'a> {
|
|
|
|
triple: &'a str,
|
|
|
|
has_std: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Target<'a> {
|
2023-04-15 12:38:17 +02:00
|
|
|
const DEFAULT_FEATURES: &str = "test-critical-section";
|
2023-04-15 00:50:46 +02:00
|
|
|
|
|
|
|
pub const fn new(triple: &'a str, has_std: bool) -> Self {
|
|
|
|
Self { triple, has_std }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn triple(&self) -> &str {
|
|
|
|
self.triple
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has_std(&self) -> bool {
|
|
|
|
self.has_std
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn and_features(&self, features: &str) -> String {
|
2023-04-15 12:38:17 +02:00
|
|
|
format!("{},{}", Self::DEFAULT_FEATURES, features)
|
2023-04-15 00:50:46 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2023-04-15 13:18:18 +02:00
|
|
|
impl core::fmt::Display for Target<'_> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.triple)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-15 00:50:46 +02:00
|
|
|
// x86_64-unknown-linux-gnu
|
|
|
|
const _X86_64: Target = Target::new("x86_64-unknown-linux-gnu", true);
|
|
|
|
const ARMV6M: Target = Target::new("thumbv6m-none-eabi", false);
|
|
|
|
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);
|
2023-02-20 20:09:51 +01:00
|
|
|
|
2021-08-26 10:58:59 +02:00
|
|
|
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 {
|
2023-04-15 12:21:11 +02:00
|
|
|
return Err(anyhow::anyhow!(
|
|
|
|
"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();
|
|
|
|
|
2023-04-15 00:26:16 +02:00
|
|
|
let globals = &cli.globals;
|
|
|
|
|
|
|
|
let env_logger_default_level = match globals.verbose {
|
2023-04-15 10:10:04 +02:00
|
|
|
0 => "info",
|
|
|
|
1 => "debug",
|
|
|
|
_ => "trace",
|
2023-02-05 01:50:29 +01:00
|
|
|
};
|
2023-04-15 10:10:04 +02:00
|
|
|
|
|
|
|
pretty_env_logger::formatted_builder()
|
|
|
|
.parse_filters(&std::env::var("RUST_LOG").unwrap_or(env_logger_default_level.into()))
|
2023-02-05 01:50:29 +01:00
|
|
|
.init();
|
2023-01-08 19:56:47 +01:00
|
|
|
|
2023-04-15 00:26:16 +02:00
|
|
|
trace!("default logging level: {0}", globals.verbose);
|
2023-02-05 01:50:29 +01:00
|
|
|
|
2023-04-14 23:59:23 +02:00
|
|
|
log::debug!(
|
|
|
|
"Stderr of child processes is inherited: {}",
|
|
|
|
globals.stderr_inherited
|
|
|
|
);
|
|
|
|
log::debug!("Partial features: {}", globals.partial);
|
|
|
|
|
2023-04-15 00:26:16 +02:00
|
|
|
let backend = if let Some(backend) = globals.backend {
|
2023-02-23 19:34:52 +01:00
|
|
|
backend
|
|
|
|
} else {
|
|
|
|
Backends::default()
|
|
|
|
};
|
2023-02-05 01:50:29 +01:00
|
|
|
|
2023-04-15 00:26:16 +02:00
|
|
|
let example = globals.example.clone();
|
|
|
|
let exampleexclude = globals.exampleexclude.clone();
|
2023-02-06 13:21:04 +01:00
|
|
|
|
|
|
|
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-04-15 12:21:11 +02:00
|
|
|
return Err(anyhow::anyhow!("Incorrect usage"));
|
2023-02-06 13:21:04 +01:00
|
|
|
} else {
|
2023-04-15 12:21:11 +02:00
|
|
|
examples_to_run
|
2023-02-06 13:21:04 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-04-15 13:45:58 +02:00
|
|
|
let final_run_results = match &cli.command {
|
2023-04-15 14:43:40 +02:00
|
|
|
Commands::Format(args) => cargo_format(globals, &cargologlevel, &args.package, args.check),
|
2023-02-24 23:14:11 +01:00
|
|
|
Commands::Clippy(args) => {
|
|
|
|
info!("Running clippy on backend: {backend:?}");
|
2023-04-15 13:45:58 +02:00
|
|
|
cargo_clippy(globals, &cargologlevel, &args, backend)
|
2023-02-24 23:14:11 +01:00
|
|
|
}
|
|
|
|
Commands::Check(args) => {
|
|
|
|
info!("Checking on backend: {backend:?}");
|
2023-04-15 13:45:58 +02:00
|
|
|
cargo(globals, BuildOrCheck::Check, &cargologlevel, &args, backend)
|
2023-02-24 23:14:11 +01:00
|
|
|
}
|
|
|
|
Commands::Build(args) => {
|
|
|
|
info!("Building for backend: {backend:?}");
|
2023-04-15 13:45:58 +02:00
|
|
|
cargo(globals, 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(
|
2023-04-15 00:26:16 +02:00
|
|
|
globals,
|
2023-02-25 00:11:12 +01:00
|
|
|
BuildOrCheck::Check,
|
|
|
|
&cargologlevel,
|
|
|
|
backend,
|
|
|
|
&examples_to_run,
|
2023-04-15 13:45:58 +02:00
|
|
|
)
|
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(
|
2023-04-15 00:26:16 +02:00
|
|
|
globals,
|
2023-02-25 00:11:12 +01:00
|
|
|
BuildOrCheck::Build,
|
|
|
|
&cargologlevel,
|
|
|
|
backend,
|
|
|
|
&examples_to_run,
|
2023-04-15 13:45:58 +02:00
|
|
|
)
|
2023-02-24 23:14:11 +01:00
|
|
|
}
|
2023-02-28 23:55:02 +01:00
|
|
|
Commands::Size(args) => {
|
2023-02-06 13:21:04 +01:00
|
|
|
// x86_64 target not valid
|
2023-02-20 20:09:51 +01:00
|
|
|
info!("Measuring for backend: {backend:?}");
|
2023-04-15 00:26:16 +02:00
|
|
|
build_and_check_size(
|
|
|
|
globals,
|
|
|
|
&cargologlevel,
|
|
|
|
backend,
|
|
|
|
&examples_to_run,
|
|
|
|
&args.arguments,
|
2023-04-15 13:45:58 +02:00
|
|
|
)
|
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:?}");
|
2023-04-15 22:19:13 +02:00
|
|
|
qemu_run_examples(
|
2023-04-15 00:26:16 +02:00
|
|
|
globals,
|
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-04-15 13:45:58 +02:00
|
|
|
)
|
2023-02-05 01:50:29 +01:00
|
|
|
}
|
2023-03-02 22:02:19 +01:00
|
|
|
Commands::Doc(args) => {
|
2023-02-24 22:56:36 +01:00
|
|
|
info!("Running cargo doc on backend: {backend:?}");
|
2023-04-15 13:45:58 +02:00
|
|
|
cargo_doc(globals, &cargologlevel, backend, &args.arguments)
|
2023-02-24 22:56:36 +01:00
|
|
|
}
|
2023-02-28 23:55:02 +01:00
|
|
|
Commands::Test(args) => {
|
|
|
|
info!("Running cargo test on backend: {backend:?}");
|
2023-04-15 13:45:58 +02:00
|
|
|
cargo_test(globals, &args, backend)
|
2023-02-28 23:55:02 +01:00
|
|
|
}
|
2023-03-02 22:02:19 +01:00
|
|
|
Commands::Book(args) => {
|
|
|
|
info!("Running mdbook");
|
2023-04-15 13:45:58 +02:00
|
|
|
cargo_book(globals, &args.arguments)
|
2023-02-25 00:28:45 +01:00
|
|
|
}
|
2023-04-15 20:57:27 +02:00
|
|
|
Commands::UsageExampleCheck(examples) => {
|
2023-04-14 23:59:23 +02:00
|
|
|
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()?)
|
|
|
|
}
|
2023-04-15 13:45:58 +02:00
|
|
|
};
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2023-04-15 20:19:37 +02:00
|
|
|
handle_results(globals, final_run_results).map_err(|_| anyhow::anyhow!("Commands failed"))
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|