2023-04-15 13:45:58 +02:00
|
|
|
use log::{error, info, Level};
|
|
|
|
|
|
|
|
use crate::{
|
2023-04-16 11:02:49 +02:00
|
|
|
argument_parsing::Globals, xtasks::FinalRunResult, ExtraArguments, RunResult, Target,
|
2023-04-16 09:44:30 +02:00
|
|
|
TestRunError,
|
2023-04-15 13:45:58 +02:00
|
|
|
};
|
2021-08-26 10:58:59 +02:00
|
|
|
use core::fmt;
|
2023-04-15 00:09:50 +02:00
|
|
|
use std::{
|
|
|
|
fs::File,
|
|
|
|
io::Read,
|
2023-04-14 23:59:23 +02:00
|
|
|
path::PathBuf,
|
2023-04-15 00:09:50 +02:00
|
|
|
process::{Command, Stdio},
|
|
|
|
};
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2021-09-22 13:22:45 +02:00
|
|
|
#[allow(dead_code)]
|
2021-08-26 10:58:59 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
pub enum BuildMode {
|
|
|
|
Release,
|
|
|
|
Debug,
|
|
|
|
}
|
|
|
|
|
2023-04-15 00:26:16 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
pub enum OutputMode {
|
|
|
|
PipedAndCollected,
|
|
|
|
Inherited,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<OutputMode> for Stdio {
|
|
|
|
fn from(value: OutputMode) -> Self {
|
|
|
|
match value {
|
|
|
|
OutputMode::PipedAndCollected => Stdio::piped(),
|
|
|
|
OutputMode::Inherited => Stdio::inherit(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:22:45 +02:00
|
|
|
#[derive(Debug)]
|
2021-08-26 10:58:59 +02:00
|
|
|
pub enum CargoCommand<'a> {
|
2023-02-06 13:21:04 +01:00
|
|
|
// For future embedded-ci
|
|
|
|
#[allow(dead_code)]
|
2021-08-26 10:58:59 +02:00
|
|
|
Run {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &'a Option<&'a str>,
|
|
|
|
example: &'a str,
|
2023-04-16 11:00:39 +02:00
|
|
|
target: Option<Target<'a>>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2023-02-05 01:50:29 +01:00
|
|
|
mode: BuildMode,
|
2023-04-16 11:00:39 +02:00
|
|
|
dir: Option<PathBuf>,
|
2023-02-05 01:50:29 +01:00
|
|
|
},
|
2023-02-06 13:21:04 +01:00
|
|
|
Qemu {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &'a Option<&'a str>,
|
2021-08-26 10:58:59 +02:00
|
|
|
example: &'a str,
|
2023-04-16 11:00:39 +02:00
|
|
|
target: Option<Target<'a>>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2021-08-26 10:58:59 +02:00
|
|
|
mode: BuildMode,
|
2023-04-15 23:22:00 +02:00
|
|
|
dir: Option<PathBuf>,
|
2021-08-26 10:58:59 +02:00
|
|
|
},
|
2023-02-06 13:21:04 +01:00
|
|
|
ExampleBuild {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-02-06 13:21:04 +01:00
|
|
|
example: &'a str,
|
2023-04-16 11:00:39 +02:00
|
|
|
target: Option<Target<'a>>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2021-08-26 10:58:59 +02:00
|
|
|
mode: BuildMode,
|
2023-04-15 23:22:00 +02:00
|
|
|
dir: Option<PathBuf>,
|
2021-08-26 10:58:59 +02:00
|
|
|
},
|
2023-02-06 13:21:04 +01:00
|
|
|
ExampleCheck {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-02-06 13:21:04 +01:00
|
|
|
example: &'a str,
|
2023-04-16 11:00:39 +02:00
|
|
|
target: Option<Target<'a>>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2023-02-06 13:21:04 +01:00
|
|
|
mode: BuildMode,
|
2023-02-05 01:50:29 +01:00
|
|
|
},
|
2023-02-06 13:21:04 +01:00
|
|
|
Build {
|
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-04-16 09:44:30 +02:00
|
|
|
package: Option<String>,
|
2023-04-16 11:00:39 +02:00
|
|
|
target: Option<Target<'a>>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2023-02-06 13:21:04 +01:00
|
|
|
mode: BuildMode,
|
2023-04-16 11:00:39 +02:00
|
|
|
dir: Option<PathBuf>,
|
2023-02-06 13:21:04 +01:00
|
|
|
},
|
|
|
|
Check {
|
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-04-16 09:44:30 +02:00
|
|
|
package: Option<String>,
|
2023-04-16 11:00:39 +02:00
|
|
|
target: Option<Target<'a>>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2023-02-24 23:14:11 +01:00
|
|
|
mode: BuildMode,
|
2023-04-16 11:00:39 +02:00
|
|
|
dir: Option<PathBuf>,
|
2023-02-06 13:21:04 +01:00
|
|
|
},
|
|
|
|
Clippy {
|
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-04-16 09:44:30 +02:00
|
|
|
package: Option<String>,
|
2023-04-16 11:00:39 +02:00
|
|
|
target: Option<Target<'a>>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2023-02-06 13:21:04 +01:00
|
|
|
},
|
2023-02-24 00:10:01 +01:00
|
|
|
Format {
|
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-04-16 09:44:30 +02:00
|
|
|
package: Option<String>,
|
2023-02-24 00:10:01 +01:00
|
|
|
check_only: bool,
|
|
|
|
},
|
2023-02-24 22:56:36 +01:00
|
|
|
Doc {
|
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2023-03-02 22:02:19 +01:00
|
|
|
arguments: Option<ExtraArguments>,
|
2023-02-28 23:55:02 +01:00
|
|
|
},
|
|
|
|
Test {
|
2023-04-16 09:44:30 +02:00
|
|
|
package: Option<String>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
|
|
|
test: Option<String>,
|
2023-02-24 22:56:36 +01:00
|
|
|
},
|
2023-02-25 00:28:45 +01:00
|
|
|
Book {
|
2023-03-02 22:02:19 +01:00
|
|
|
arguments: Option<ExtraArguments>,
|
2023-02-25 00:28:45 +01:00
|
|
|
},
|
2023-02-06 13:21:04 +01:00
|
|
|
ExampleSize {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-02-04 15:22:43 +01:00
|
|
|
example: &'a str,
|
2023-04-16 11:00:39 +02:00
|
|
|
target: Option<Target<'a>>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2023-02-04 15:22:43 +01:00
|
|
|
mode: BuildMode,
|
2023-03-02 22:02:19 +01:00
|
|
|
arguments: Option<ExtraArguments>,
|
2023-04-15 23:22:00 +02:00
|
|
|
dir: Option<PathBuf>,
|
2023-02-04 15:22:43 +01:00
|
|
|
},
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
|
2023-04-15 12:21:11 +02:00
|
|
|
impl core::fmt::Display for CargoCommand<'_> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2023-04-16 11:00:39 +02:00
|
|
|
fn p(p: &Option<String>) -> String {
|
2023-04-15 13:18:18 +02:00
|
|
|
if let Some(package) = p {
|
|
|
|
format!("package {package}")
|
|
|
|
} else {
|
|
|
|
format!("default package")
|
|
|
|
}
|
2023-04-16 11:00:39 +02:00
|
|
|
}
|
2023-04-15 13:18:18 +02:00
|
|
|
|
2023-04-16 11:00:39 +02:00
|
|
|
fn feat(f: &Option<String>) -> String {
|
2023-04-15 13:18:18 +02:00
|
|
|
if let Some(features) = f {
|
|
|
|
format!("\"{features}\"")
|
|
|
|
} else {
|
|
|
|
format!("no features")
|
|
|
|
}
|
2023-04-16 11:00:39 +02:00
|
|
|
}
|
2023-04-15 13:18:18 +02:00
|
|
|
|
2023-04-16 11:00:39 +02:00
|
|
|
fn carg(f: &&Option<&str>) -> String {
|
2023-04-15 13:18:18 +02:00
|
|
|
if let Some(cargoarg) = f {
|
|
|
|
format!("{cargoarg}")
|
|
|
|
} else {
|
|
|
|
format!("no cargo args")
|
|
|
|
}
|
2023-04-16 11:00:39 +02:00
|
|
|
}
|
2023-04-15 13:18:18 +02:00
|
|
|
|
2023-04-16 11:00:39 +02:00
|
|
|
fn details(
|
|
|
|
target: &Option<Target>,
|
|
|
|
mode: Option<&BuildMode>,
|
|
|
|
features: &Option<String>,
|
|
|
|
cargoarg: &&Option<&str>,
|
|
|
|
path: Option<&PathBuf>,
|
|
|
|
) -> String {
|
2023-04-15 13:18:18 +02:00
|
|
|
let feat = feat(features);
|
|
|
|
let carg = carg(cargoarg);
|
2023-04-16 11:00:39 +02:00
|
|
|
let in_dir = if let Some(path) = path {
|
|
|
|
let path = path.to_str().unwrap_or("<can't display>");
|
|
|
|
format!("in {path}")
|
|
|
|
} else {
|
|
|
|
format!("")
|
|
|
|
};
|
|
|
|
|
|
|
|
let target = if let Some(target) = target {
|
|
|
|
format!("{target}")
|
|
|
|
} else {
|
|
|
|
format!("<host target>")
|
|
|
|
};
|
|
|
|
|
|
|
|
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() {
|
2023-04-15 13:18:18 +02:00
|
|
|
format!("({target}, {mode}, {feat}, {carg})")
|
2023-04-16 11:00:39 +02:00
|
|
|
} else if path.is_some() {
|
|
|
|
format!("({target}, {mode}, {feat}, {in_dir})")
|
2023-04-15 13:18:18 +02:00
|
|
|
} else {
|
|
|
|
format!("({target}, {mode}, {feat})")
|
|
|
|
}
|
2023-04-16 11:00:39 +02:00
|
|
|
}
|
2023-04-15 13:18:18 +02:00
|
|
|
|
|
|
|
match self {
|
|
|
|
CargoCommand::Run {
|
|
|
|
cargoarg,
|
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
2023-04-16 11:00:39 +02:00
|
|
|
dir,
|
|
|
|
} => {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Run example {example} {}",
|
|
|
|
details(target, Some(mode), features, cargoarg, dir.as_ref())
|
|
|
|
)
|
|
|
|
}
|
2023-04-15 13:18:18 +02:00
|
|
|
CargoCommand::Qemu {
|
|
|
|
cargoarg,
|
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
2023-04-15 23:22:00 +02:00
|
|
|
dir,
|
|
|
|
} => {
|
2023-04-16 11:00:39 +02:00
|
|
|
let details = details(target, Some(mode), features, cargoarg, dir.as_ref());
|
|
|
|
write!(f, "Run example {example} in QEMU {details}",)
|
2023-04-15 23:22:00 +02:00
|
|
|
}
|
2023-04-15 13:18:18 +02:00
|
|
|
CargoCommand::ExampleBuild {
|
|
|
|
cargoarg,
|
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
2023-04-15 23:22:00 +02:00
|
|
|
dir,
|
|
|
|
} => {
|
2023-04-16 11:00:39 +02:00
|
|
|
let details = details(target, Some(mode), features, cargoarg, dir.as_ref());
|
|
|
|
write!(f, "Build example {example} {details}",)
|
2023-04-15 23:22:00 +02:00
|
|
|
}
|
2023-04-15 13:18:18 +02:00
|
|
|
CargoCommand::ExampleCheck {
|
|
|
|
cargoarg,
|
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
} => write!(
|
|
|
|
f,
|
|
|
|
"Check example {example} {}",
|
2023-04-16 11:00:39 +02:00
|
|
|
details(target, Some(mode), features, cargoarg, None)
|
2023-04-15 13:18:18 +02:00
|
|
|
),
|
|
|
|
CargoCommand::Build {
|
|
|
|
cargoarg,
|
|
|
|
package,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
2023-04-16 11:00:39 +02:00
|
|
|
dir,
|
2023-04-15 13:18:18 +02:00
|
|
|
} => {
|
|
|
|
let package = p(package);
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Build {package} {}",
|
2023-04-16 11:00:39 +02:00
|
|
|
details(target, Some(mode), features, cargoarg, dir.as_ref())
|
2023-04-15 13:18:18 +02:00
|
|
|
)
|
|
|
|
}
|
2023-04-16 11:00:39 +02:00
|
|
|
|
2023-04-15 13:18:18 +02:00
|
|
|
CargoCommand::Check {
|
|
|
|
cargoarg,
|
|
|
|
package,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
2023-04-16 11:00:39 +02:00
|
|
|
dir,
|
2023-04-15 13:18:18 +02:00
|
|
|
} => {
|
|
|
|
let package = p(package);
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Check {package} {}",
|
2023-04-16 11:00:39 +02:00
|
|
|
details(target, Some(mode), features, cargoarg, dir.as_ref())
|
2023-04-15 13:18:18 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
CargoCommand::Clippy {
|
|
|
|
cargoarg,
|
|
|
|
package,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
} => {
|
2023-04-16 11:00:39 +02:00
|
|
|
let details = details(target, None, features, cargoarg, None);
|
2023-04-15 13:18:18 +02:00
|
|
|
let package = p(package);
|
2023-04-16 11:00:39 +02:00
|
|
|
write!(f, "Clippy {package} {details}")
|
2023-04-15 13:18:18 +02:00
|
|
|
}
|
|
|
|
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}"))
|
2023-04-15 15:16:43 +02:00
|
|
|
.unwrap_or_else(|| "no extra arguments".into());
|
2023-04-15 13:18:18 +02:00
|
|
|
if cargoarg.is_some() {
|
2023-04-15 15:16:43 +02:00
|
|
|
write!(f, "Document ({feat}, {carg}, {arguments})")
|
2023-04-15 13:18:18 +02:00
|
|
|
} else {
|
2023-04-15 15:16:43 +02:00
|
|
|
write!(f, "Document ({feat}, {arguments})")
|
2023-04-15 13:18:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
2023-04-15 13:45:58 +02:00
|
|
|
write!(f, "Run {test} in {p} (features: {feat})")
|
2023-04-15 13:18:18 +02:00
|
|
|
}
|
|
|
|
CargoCommand::Book { arguments: _ } => write!(f, "Build the book"),
|
|
|
|
CargoCommand::ExampleSize {
|
|
|
|
cargoarg,
|
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
arguments: _,
|
2023-04-15 23:22:00 +02:00
|
|
|
dir,
|
2023-04-15 13:18:18 +02:00
|
|
|
} => {
|
2023-04-16 11:00:39 +02:00
|
|
|
let details = details(target, Some(mode), features, cargoarg, dir.as_ref());
|
|
|
|
write!(f, "Compute size of example {example} {details}")
|
2023-04-15 13:18:18 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-15 12:21:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 10:58:59 +02:00
|
|
|
impl<'a> CargoCommand<'a> {
|
2023-04-15 13:55:56 +02:00
|
|
|
pub fn as_cmd_string(&self) -> String {
|
2023-04-15 23:22:00 +02:00
|
|
|
let cd = if let Some(Some(chdir)) = self.chdir().map(|p| p.to_str()) {
|
|
|
|
format!("cd {chdir} && ")
|
|
|
|
} else {
|
|
|
|
format!("")
|
|
|
|
};
|
|
|
|
|
2023-04-15 13:55:56 +02:00
|
|
|
let executable = self.executable();
|
|
|
|
let args = self.args().join(" ");
|
2023-04-15 23:22:00 +02:00
|
|
|
format!("{cd}{executable} {args}")
|
2023-04-15 13:55:56 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 23:59:23 +02:00
|
|
|
fn command(&self) -> &'static str {
|
2021-08-26 10:58:59 +02:00
|
|
|
match self {
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::Run { .. } | CargoCommand::Qemu { .. } => "run",
|
2023-04-16 11:00:39 +02:00
|
|
|
CargoCommand::ExampleCheck { .. } | CargoCommand::Check { .. } => "check",
|
|
|
|
CargoCommand::ExampleBuild { .. } | CargoCommand::Build { .. } => "build",
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::ExampleSize { .. } => "size",
|
|
|
|
CargoCommand::Clippy { .. } => "clippy",
|
2023-02-24 00:10:01 +01:00
|
|
|
CargoCommand::Format { .. } => "fmt",
|
2023-02-24 22:56:36 +01:00
|
|
|
CargoCommand::Doc { .. } => "doc",
|
2023-02-25 00:28:45 +01:00
|
|
|
CargoCommand::Book { .. } => "build",
|
2023-02-28 23:55:02 +01:00
|
|
|
CargoCommand::Test { .. } => "test",
|
2023-02-25 00:28:45 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-14 23:59:23 +02:00
|
|
|
pub fn executable(&self) -> &'static str {
|
2023-02-25 00:28:45 +01:00
|
|
|
match self {
|
|
|
|
CargoCommand::Run { .. }
|
|
|
|
| CargoCommand::Qemu { .. }
|
|
|
|
| CargoCommand::ExampleCheck { .. }
|
|
|
|
| CargoCommand::Check { .. }
|
|
|
|
| CargoCommand::ExampleBuild { .. }
|
|
|
|
| CargoCommand::Build { .. }
|
|
|
|
| CargoCommand::ExampleSize { .. }
|
|
|
|
| CargoCommand::Clippy { .. }
|
|
|
|
| CargoCommand::Format { .. }
|
2023-02-28 23:55:02 +01:00
|
|
|
| CargoCommand::Test { .. }
|
2023-04-16 11:00:39 +02:00
|
|
|
| CargoCommand::Doc { .. } => "cargo",
|
2023-02-25 00:28:45 +01:00
|
|
|
CargoCommand::Book { .. } => "mdbook",
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-16 11:00:39 +02:00
|
|
|
/// Build args using common arguments for all commands, and the
|
|
|
|
/// specific information provided
|
|
|
|
fn build_args<'i, T: Iterator<Item = &'i str>>(
|
|
|
|
&'i self,
|
|
|
|
nightly: bool,
|
|
|
|
cargoarg: &'i Option<&'i str>,
|
|
|
|
features: &'i Option<String>,
|
|
|
|
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<Item = &str> {
|
|
|
|
#[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()
|
|
|
|
}
|
|
|
|
|
2021-08-26 10:58:59 +02:00
|
|
|
pub fn args(&self) -> Vec<&str> {
|
2023-04-16 11:00:39 +02:00
|
|
|
fn p(package: &Option<String>) -> impl Iterator<Item = &str> {
|
|
|
|
if let Some(package) = package {
|
|
|
|
vec!["--package", &package].into_iter()
|
|
|
|
} else {
|
|
|
|
vec![].into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 10:58:59 +02:00
|
|
|
match self {
|
2023-02-06 13:21:04 +01:00
|
|
|
// For future embedded-ci, for now the same as Qemu
|
2021-08-26 10:58:59 +02:00
|
|
|
CargoCommand::Run {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg,
|
2021-08-26 10:58:59 +02:00
|
|
|
example,
|
|
|
|
features,
|
|
|
|
mode,
|
2023-04-16 11:00:39 +02:00
|
|
|
// 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(),
|
|
|
|
),
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::Qemu {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg,
|
2023-02-06 13:21:04 +01:00
|
|
|
example,
|
2021-08-26 10:58:59 +02:00
|
|
|
features,
|
2021-12-26 10:43:57 +01:00
|
|
|
mode,
|
2023-04-16 11:00:39 +02:00
|
|
|
// dir is exposed through `chdir`
|
2023-04-15 23:22:00 +02:00
|
|
|
dir: _,
|
2023-04-16 11:00:39 +02:00
|
|
|
// Target is added by build_args
|
|
|
|
target: _,
|
|
|
|
} => self.build_args(
|
|
|
|
true,
|
|
|
|
cargoarg,
|
|
|
|
features,
|
|
|
|
Some(mode),
|
|
|
|
["--example", example].into_iter(),
|
|
|
|
),
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::Build {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg,
|
2023-02-06 13:21:04 +01:00
|
|
|
package,
|
2023-02-05 01:50:29 +01:00
|
|
|
features,
|
2023-02-06 13:21:04 +01:00
|
|
|
mode,
|
2023-04-16 11:00:39 +02:00
|
|
|
// Dir is exposed through `chdir`
|
|
|
|
dir: _,
|
|
|
|
// Target is added by build_args
|
|
|
|
target: _,
|
|
|
|
} => self.build_args(true, cargoarg, features, Some(mode), p(package)),
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::Check {
|
|
|
|
cargoarg,
|
|
|
|
package,
|
|
|
|
features,
|
2023-02-24 23:14:11 +01:00
|
|
|
mode,
|
2023-04-16 11:00:39 +02:00
|
|
|
// Dir is exposed through `chdir`
|
|
|
|
dir: _,
|
|
|
|
// Target is added by build_args
|
|
|
|
target: _,
|
|
|
|
} => self.build_args(true, cargoarg, features, Some(mode), p(package)),
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::Clippy {
|
|
|
|
cargoarg,
|
|
|
|
package,
|
|
|
|
features,
|
2023-04-16 11:00:39 +02:00
|
|
|
// Target is added by build_args
|
|
|
|
target: _,
|
|
|
|
} => self.build_args(true, cargoarg, features, None, p(package)),
|
2023-03-02 22:02:19 +01:00
|
|
|
CargoCommand::Doc {
|
|
|
|
cargoarg,
|
|
|
|
features,
|
|
|
|
arguments,
|
|
|
|
} => {
|
2023-04-16 11:00:39 +02:00
|
|
|
let extra = Self::extra_args(arguments.as_ref());
|
|
|
|
self.build_args(true, cargoarg, features, None, extra)
|
2023-02-24 22:56:36 +01:00
|
|
|
}
|
2023-02-28 23:55:02 +01:00
|
|
|
CargoCommand::Test {
|
|
|
|
package,
|
|
|
|
features,
|
|
|
|
test,
|
|
|
|
} => {
|
2023-04-16 11:00:39 +02:00
|
|
|
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)
|
2023-02-28 23:55:02 +01:00
|
|
|
}
|
2023-03-02 22:02:19 +01:00
|
|
|
CargoCommand::Book { arguments } => {
|
2023-02-25 00:28:45 +01:00
|
|
|
let mut args = vec![];
|
|
|
|
|
2023-03-02 22:02:19 +01:00
|
|
|
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
|
2023-03-04 00:08:11 +01:00
|
|
|
// with default path to book
|
2023-03-02 22:02:54 +01:00
|
|
|
args.extend_from_slice(&[self.command()]);
|
2023-03-04 00:08:11 +01:00
|
|
|
args.extend_from_slice(&["book/en"]);
|
2023-02-25 00:28:45 +01:00
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
2023-02-24 00:10:01 +01:00
|
|
|
CargoCommand::Format {
|
|
|
|
cargoarg,
|
|
|
|
package,
|
|
|
|
check_only,
|
|
|
|
} => {
|
2023-04-16 11:00:39 +02:00
|
|
|
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),
|
|
|
|
)
|
2023-02-24 00:10:01 +01:00
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::ExampleBuild {
|
|
|
|
cargoarg,
|
|
|
|
example,
|
|
|
|
features,
|
|
|
|
mode,
|
2023-04-16 11:00:39 +02:00
|
|
|
// dir is exposed through `chdir`
|
2023-04-15 23:22:00 +02:00
|
|
|
dir: _,
|
2023-04-16 11:00:39 +02:00
|
|
|
// Target is added by build_args
|
|
|
|
target: _,
|
|
|
|
} => self.build_args(
|
|
|
|
true,
|
|
|
|
cargoarg,
|
|
|
|
features,
|
|
|
|
Some(mode),
|
|
|
|
["--example", example].into_iter(),
|
|
|
|
),
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::ExampleCheck {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg,
|
|
|
|
example,
|
|
|
|
features,
|
|
|
|
mode,
|
2023-04-16 11:00:39 +02:00
|
|
|
// Target is added by build_args
|
|
|
|
target: _,
|
|
|
|
} => self.build_args(
|
|
|
|
true,
|
|
|
|
cargoarg,
|
|
|
|
features,
|
|
|
|
Some(mode),
|
|
|
|
["--example", example].into_iter(),
|
|
|
|
),
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::ExampleSize {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg,
|
2023-02-04 15:22:43 +01:00
|
|
|
example,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
arguments,
|
2023-04-16 11:00:39 +02:00
|
|
|
// Target is added by build_args
|
|
|
|
target: _,
|
|
|
|
// dir is exposed through `chdir`
|
2023-04-15 23:22:00 +02:00
|
|
|
dir: _,
|
2023-02-04 15:22:43 +01:00
|
|
|
} => {
|
2023-04-16 11:00:39 +02:00
|
|
|
let extra = ["--example", example]
|
|
|
|
.into_iter()
|
|
|
|
.chain(Self::extra_args(arguments.as_ref()));
|
2023-04-14 23:59:23 +02:00
|
|
|
|
2023-04-16 11:00:39 +02:00
|
|
|
self.build_args(true, cargoarg, features, Some(mode), extra)
|
2023-04-14 23:59:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-16 11:00:39 +02:00
|
|
|
/// TODO: integrate this into `args` once `-C` becomes stable.
|
2023-04-14 23:59:23 +02:00
|
|
|
fn chdir(&self) -> Option<&PathBuf> {
|
|
|
|
match self {
|
2023-04-15 23:22:00 +02:00
|
|
|
CargoCommand::Qemu { dir, .. }
|
|
|
|
| CargoCommand::ExampleBuild { dir, .. }
|
2023-04-16 11:00:39 +02:00
|
|
|
| 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(),
|
2023-04-14 23:59:23 +02:00
|
|
|
_ => None,
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-15 21:16:45 +02:00
|
|
|
|
|
|
|
pub fn print_stdout_intermediate(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::ExampleSize { .. } => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BuildMode {
|
2023-02-04 15:22:43 +01:00
|
|
|
#[allow(clippy::wrong_self_convention)]
|
2021-08-26 10:58:59 +02:00
|
|
|
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",
|
|
|
|
};
|
|
|
|
|
2023-02-04 15:22:43 +01:00
|
|
|
write!(f, "{cmd}")
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-15 00:26:16 +02:00
|
|
|
pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::Result<RunResult> {
|
2023-04-15 13:45:58 +02:00
|
|
|
log::info!("👟 {command}");
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2023-04-14 23:59:23 +02:00
|
|
|
let mut process = Command::new(command.executable());
|
|
|
|
|
|
|
|
process
|
2023-04-15 00:09:50 +02:00
|
|
|
.args(command.args())
|
|
|
|
.stdout(Stdio::piped())
|
2023-04-14 23:59:23 +02:00
|
|
|
.stderr(stderr_mode);
|
|
|
|
|
|
|
|
if let Some(dir) = command.chdir() {
|
2023-04-15 23:22:00 +02:00
|
|
|
process.current_dir(dir.canonicalize()?);
|
2023-04-14 23:59:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let result = process.output()?;
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2023-04-15 00:09:50 +02:00
|
|
|
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());
|
2023-02-06 13:21:04 +01:00
|
|
|
|
2023-04-15 21:16:45 +02:00
|
|
|
if command.print_stdout_intermediate() && exit_status.success() {
|
|
|
|
log::info!("\n{}", stdout);
|
|
|
|
}
|
|
|
|
|
2023-04-15 23:22:00 +02:00
|
|
|
if exit_status.success() {
|
|
|
|
log::info!("✅ Success.")
|
|
|
|
} else {
|
2023-04-15 22:40:25 +02:00
|
|
|
log::error!("❌ Command failed. Run to completion for the summary.");
|
|
|
|
}
|
|
|
|
|
2021-08-26 10:58:59 +02:00
|
|
|
Ok(RunResult {
|
|
|
|
exit_status,
|
2023-02-08 22:09:32 +01:00
|
|
|
stdout,
|
|
|
|
stderr,
|
2021-08-26 10:58:59 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-08 19:16:36 +01:00
|
|
|
/// Check if `run` was successful.
|
2021-08-26 10:58:59 +02:00
|
|
|
/// returns Ok in case the run went as expected,
|
|
|
|
/// Err otherwise
|
2023-02-05 01:50:29 +01:00
|
|
|
pub fn run_successful(run: &RunResult, expected_output_file: &str) -> Result<(), TestRunError> {
|
2021-09-22 13:22:45 +02:00
|
|
|
let mut file_handle =
|
2023-02-05 01:50:29 +01:00
|
|
|
File::open(expected_output_file).map_err(|_| TestRunError::FileError {
|
|
|
|
file: expected_output_file.to_owned(),
|
2021-09-22 13:22:45 +02:00
|
|
|
})?;
|
2021-08-26 10:58:59 +02:00
|
|
|
let mut expected_output = String::new();
|
2021-09-22 13:22:45 +02:00
|
|
|
file_handle
|
|
|
|
.read_to_string(&mut expected_output)
|
|
|
|
.map_err(|_| TestRunError::FileError {
|
2023-02-05 01:50:29 +01:00
|
|
|
file: expected_output_file.to_owned(),
|
2021-09-22 13:22:45 +02:00
|
|
|
})?;
|
|
|
|
|
2023-02-08 22:09:32 +01:00
|
|
|
if expected_output != run.stdout {
|
2021-09-22 13:22:45 +02:00
|
|
|
Err(TestRunError::FileCmpError {
|
|
|
|
expected: expected_output.clone(),
|
2023-02-08 22:09:32 +01:00
|
|
|
got: run.stdout.clone(),
|
2021-09-22 13:22:45 +02:00
|
|
|
})
|
|
|
|
} else if !run.exit_status.success() {
|
|
|
|
Err(TestRunError::CommandError(run.clone()))
|
2021-08-26 10:58:59 +02:00
|
|
|
} else {
|
2021-09-22 13:22:45 +02:00
|
|
|
Ok(())
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-15 13:45:58 +02:00
|
|
|
|
2023-04-15 20:19:37 +02:00
|
|
|
pub fn handle_results(globals: &Globals, results: Vec<FinalRunResult>) -> Result<(), ()> {
|
2023-04-15 13:45:58 +02:00
|
|
|
let errors = results.iter().filter_map(|r| {
|
|
|
|
if let FinalRunResult::Failed(c, r) = r {
|
2023-04-15 20:44:06 +02:00
|
|
|
Some((c, &r.stdout, &r.stderr))
|
2023-04-15 13:45:58 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let successes = results.iter().filter_map(|r| {
|
|
|
|
if let FinalRunResult::Success(c, r) = r {
|
2023-04-15 20:44:06 +02:00
|
|
|
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))
|
2023-04-15 13:45:58 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let log_stdout_stderr = |level: Level| {
|
2023-04-15 22:40:25 +02:00
|
|
|
move |(cmd, stdout, stderr): (&CargoCommand, &String, &String)| {
|
|
|
|
let cmd = cmd.as_cmd_string();
|
2023-04-15 13:45:58 +02:00
|
|
|
if !stdout.is_empty() && !stderr.is_empty() {
|
2023-04-15 22:40:25 +02:00
|
|
|
log::log!(level, "\n{cmd}\nStdout:\n{stdout}\nStderr:\n{stderr}");
|
2023-04-15 13:45:58 +02:00
|
|
|
} else if !stdout.is_empty() {
|
2023-04-15 22:40:25 +02:00
|
|
|
log::log!(level, "\n{cmd}\nStdout:\n{}", stdout.trim_end());
|
2023-04-15 13:45:58 +02:00
|
|
|
} else if !stderr.is_empty() {
|
2023-04-15 22:40:25 +02:00
|
|
|
log::log!(level, "\n{cmd}\nStderr:\n{}", stderr.trim_end());
|
2023-04-15 13:45:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-04-15 22:40:25 +02:00
|
|
|
successes.for_each(|(cmd, stdout, stderr)| {
|
2023-04-15 13:55:56 +02:00
|
|
|
if globals.verbose > 0 {
|
2023-04-16 11:00:39 +02:00
|
|
|
info!("✅ Success: {cmd}\n {}", cmd.as_cmd_string());
|
2023-04-15 13:55:56 +02:00
|
|
|
} else {
|
2023-04-16 11:00:39 +02:00
|
|
|
info!("✅ Success: {cmd}");
|
2023-04-15 13:55:56 +02:00
|
|
|
}
|
2023-04-15 22:40:25 +02:00
|
|
|
|
|
|
|
log_stdout_stderr(Level::Debug)((cmd, stdout, stderr));
|
2023-04-15 13:45:58 +02:00
|
|
|
});
|
|
|
|
|
2023-04-15 22:40:25 +02:00
|
|
|
errors.clone().for_each(|(cmd, stdout, stderr)| {
|
2023-04-16 11:00:39 +02:00
|
|
|
error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string());
|
2023-04-15 22:40:25 +02:00
|
|
|
log_stdout_stderr(Level::Error)((cmd, stdout, stderr));
|
2023-04-15 13:45:58 +02:00
|
|
|
});
|
|
|
|
|
2023-04-15 20:44:06 +02:00
|
|
|
command_errors
|
|
|
|
.clone()
|
|
|
|
.for_each(|(cmd, error)| error!("❌ Failed: {cmd}\n {}\n{error}", cmd.as_cmd_string()));
|
|
|
|
|
2023-04-15 21:16:45 +02:00
|
|
|
let ecount = errors.count() + command_errors.count();
|
|
|
|
if ecount != 0 {
|
2023-04-15 20:19:37 +02:00
|
|
|
log::error!("{ecount} commands failed.");
|
|
|
|
Err(())
|
2023-04-15 13:45:58 +02:00
|
|
|
} else {
|
2023-04-15 20:19:37 +02:00
|
|
|
info!("🚀🚀🚀 All tasks succeeded 🚀🚀🚀");
|
2023-04-15 13:45:58 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|