cargo xtask is now ~40x faster

This commit is contained in:
Emil Fresk 2021-12-26 10:43:57 +01:00
parent bc883e393d
commit ef4e4aaaa3
13 changed files with 36 additions and 206 deletions

View file

@ -1,4 +0,0 @@
init(baseline = Instant(0))
foo(baseline = Instant(0))
UART0(baseline = Instant(904))
foo(baseline = Instant(904))

View file

@ -1,2 +0,0 @@
foo has been called 1 time
foo has been called 2 times

View file

@ -1 +0,0 @@
received message: 42

View file

@ -1 +0,0 @@
20000000 t ramfunc::bar::h9d6714fe5a3b0c89

View file

@ -1 +0,0 @@
00000162 t ramfunc::foo::h30e7789b08c08e19

View file

@ -1,2 +0,0 @@
UART1: local_to_uart1 = 1
UART0: local_to_uart0 = 1

View file

@ -1,2 +0,0 @@
bar(2)
foo(1)

View file

View file

@ -1,9 +1,4 @@
use std::{
fs,
path::{Path, PathBuf},
};
use crate::{command::BuildMode, TestRunError};
use std::{fs, path::Path};
const HEX_BUILD_ROOT: &str = "ci/builds";
@ -16,38 +11,3 @@ pub fn init_build_dir() -> anyhow::Result<()> {
fs::create_dir_all(HEX_BUILD_ROOT)
.map_err(|_| anyhow::anyhow!("Could not create directory: {}", HEX_BUILD_ROOT))
}
pub fn build_hexpath(
example: &str,
features: Option<&str>,
build_mode: BuildMode,
build_num: u32,
) -> anyhow::Result<String> {
let features = match features {
Some(f) => f,
None => "",
};
let filename = format!("{}_{}_{}_{}.hex", example, features, build_mode, build_num);
let mut path = PathBuf::from(HEX_BUILD_ROOT);
path.push(filename);
path.into_os_string()
.into_string()
.map_err(|e| anyhow::Error::new(TestRunError::PathConversionError(e)))
}
pub fn compare_builds(expected: String, got: String) -> anyhow::Result<()> {
let buf_1 = std::fs::read_to_string(expected.clone())?;
let buf_2 = std::fs::read_to_string(got.clone())?;
if buf_1 != buf_2 {
return Err(anyhow::Error::new(TestRunError::FileCmpError {
expected,
got,
}));
}
Ok(())
}

View file

@ -1,7 +1,7 @@
use crate::{RunResult, TestRunError};
use core::fmt;
use os_pipe::pipe;
use std::{fs::File, io::Read, path::Path, process::Command};
use std::{fs::File, io::Read, process::Command};
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq)]
@ -18,32 +18,23 @@ pub enum CargoCommand<'a> {
features: Option<&'a str>,
mode: BuildMode,
},
Build {
example: &'a str,
BuildAll {
target: &'a str,
features: Option<&'a str>,
mode: BuildMode,
},
Objcopy {
example: &'a str,
target: &'a str,
features: Option<&'a str>,
ihex: &'a str,
},
Size {
example_paths: Vec<&'a Path>,
},
Clean,
// Size {
// example_paths: Vec<&'a Path>,
// },
// Clean,
}
impl<'a> CargoCommand<'a> {
fn name(&self) -> &str {
match self {
CargoCommand::Run { .. } => "run",
CargoCommand::Size { example_paths: _ } => "rust-size",
CargoCommand::Clean => "clean",
CargoCommand::Build { .. } => "build",
CargoCommand::Objcopy { .. } => "objcopy",
// CargoCommand::Size { example_paths: _ } => "rust-size",
CargoCommand::BuildAll { .. } => "build",
}
}
@ -54,12 +45,6 @@ impl<'a> CargoCommand<'a> {
target,
features,
mode,
}
| CargoCommand::Build {
example,
target,
features,
mode,
} => {
let mut args = vec![self.name(), "--example", example, "--target", target];
@ -71,26 +56,23 @@ impl<'a> CargoCommand<'a> {
}
args
}
CargoCommand::Size { example_paths } => {
example_paths.iter().map(|p| p.to_str().unwrap()).collect()
}
CargoCommand::Clean => vec!["clean"],
CargoCommand::Objcopy {
example,
CargoCommand::BuildAll {
target,
features,
ihex,
mode,
} => {
let mut args = vec![self.name(), "--example", example, "--target", target];
let mut args = vec![self.name(), "--examples", "--target", target];
if let Some(feature_name) = features {
args.extend_from_slice(&["--features", feature_name]);
}
// this always needs to go at the end
args.extend_from_slice(&["--", "-O", "ihex", ihex]);
if let Some(flag) = mode.to_flag() {
args.push(flag);
}
args
}
} // CargoCommand::Size { example_paths } => {
// example_paths.iter().map(|p| p.to_str().unwrap()).collect()
// }
}
}
@ -99,7 +81,7 @@ impl<'a> CargoCommand<'a> {
// we need to cheat a little here:
// `cargo size` can't be ran on multiple files, so we're using `rust-size` instead
// which isn't a command that starts wizh `cargo`. So we're sneakily swapping them out :)
CargoCommand::Size { .. } => "rust-size",
// CargoCommand::Size { .. } => "rust-size",
_ => "cargo",
}
}

View file

@ -14,7 +14,7 @@ use std::{
use structopt::StructOpt;
use crate::{
build::{build_hexpath, compare_builds, init_build_dir},
build::init_build_dir,
command::{run_command, run_successful, BuildMode, CargoCommand},
};
@ -93,25 +93,6 @@ fn main() -> anyhow::Result<()> {
})
.collect();
// let examples = &[
// "idle",
// "init",
// "hardware",
// "preempt",
// "binds",
// "lock",
// "multilock",
// "only-shared-access",
// "task",
// "message",
// "capacity",
// "not-sync",
// "generics",
// "pool",
// "ramfunc",
// "peripherals-taken",
// ];
let opts = Options::from_args();
let target = &opts.target;
@ -120,11 +101,9 @@ fn main() -> anyhow::Result<()> {
if target == "all" {
for t in targets {
run_test(t, &examples)?;
build_test(t, &examples)?;
}
} else if targets.contains(&target.as_str()) {
run_test(&target, &examples)?;
build_test(&target, &examples)?;
} else {
eprintln!(
"The target you specified is not available. Available targets are:\
@ -139,6 +118,12 @@ fn main() -> anyhow::Result<()> {
}
fn run_test(target: &str, examples: &[String]) -> anyhow::Result<()> {
arm_example(&CargoCommand::BuildAll {
target,
features: None,
mode: BuildMode::Release,
})?;
for example in examples {
let cmd = CargoCommand::Run {
example,
@ -147,37 +132,16 @@ fn run_test(target: &str, examples: &[String]) -> anyhow::Result<()> {
mode: BuildMode::Release,
};
arm_example(&cmd, 1)?;
arm_example(
&CargoCommand::Build {
example,
target,
features: None,
mode: BuildMode::Release,
},
1,
)?;
arm_example(&cmd)?;
}
Ok(())
}
// run example binary `example`
fn arm_example(command: &CargoCommand, build_num: u32) -> anyhow::Result<()> {
fn arm_example(command: &CargoCommand) -> anyhow::Result<()> {
match *command {
CargoCommand::Run {
example,
target,
features,
mode,
}
| CargoCommand::Build {
example,
target,
features,
mode,
} => {
CargoCommand::Run { example, .. } => {
let run_file = format!("{}.run", example);
let expected_output_file = ["ci", "expected", &run_file]
.iter()
@ -197,77 +161,14 @@ fn arm_example(command: &CargoCommand, build_num: u32) -> anyhow::Result<()> {
_ => (),
}
// now, prepare to objcopy
let hexpath = build_hexpath(example, features, mode, build_num)?;
run_command(&CargoCommand::Objcopy {
example,
target,
features,
ihex: &hexpath,
})?;
Ok(())
}
_ => Err(anyhow::Error::new(TestRunError::IncompatibleCommand)),
CargoCommand::BuildAll { .. } => {
// command is either build or run
let cargo_run_result = run_command(&command)?;
println!("{}", cargo_run_result.output);
Ok(())
} // _ => Err(anyhow::Error::new(TestRunError::IncompatibleCommand)),
}
}
fn build_test(target: &str, examples: &[String]) -> anyhow::Result<()> {
run_command(&CargoCommand::Clean)?;
let mut built = vec![];
let build_path: PathBuf = ["target", target, "release", "examples"].iter().collect();
for example in examples {
let no_features = None;
arm_example(
&CargoCommand::Build {
target,
example,
mode: BuildMode::Release,
features: no_features,
},
2,
)?;
let expected = build_hexpath(example, no_features, BuildMode::Release, 1)?;
let got = build_hexpath(example, no_features, BuildMode::Release, 2)?;
compare_builds(expected, got)?;
arm_example(
&CargoCommand::Build {
target,
example,
mode: BuildMode::Release,
features: no_features,
},
2,
)?;
let expected = build_hexpath(example, no_features, BuildMode::Release, 1)?;
let got = build_hexpath(example, no_features, BuildMode::Release, 2)?;
compare_builds(expected, got)?;
built.push(build_path.join(example));
}
let example_paths: Vec<&Path> = built.iter().map(|p| p.as_path()).collect();
let size_run_result = run_command(&CargoCommand::Size { example_paths })?;
if size_run_result.exit_status.success() {
println!("{}", size_run_result.output);
}
Ok(())
}
// /// Check if lines in `output` contain `pattern` and print matching lines
// fn print_from_output(pattern: &str, lines: &str) {
// let lines = lines.split("\n");
// for line in lines {
// if line.contains(pattern) {
// println!("{}", line);
// }
// }
// }