2023-04-16 11:05:41 +02:00
|
|
|
use crate::{ExtraArguments, Target};
|
2021-08-26 10:58:59 +02:00
|
|
|
use core::fmt;
|
2023-04-16 11:05:41 +02:00
|
|
|
use std::path::PathBuf;
|
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,
|
|
|
|
}
|
|
|
|
|
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>,
|
2023-04-16 12:51:11 +02:00
|
|
|
deny_warnings: bool,
|
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>,
|
2023-04-16 12:51:11 +02:00
|
|
|
deny_warnings: bool,
|
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-04-16 12:51:11 +02:00
|
|
|
deny_warnings: bool,
|
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-04-16 12:51:11 +02:00
|
|
|
deny_warnings: bool,
|
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-04-16 12:51:11 +02:00
|
|
|
deny_warnings: bool,
|
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-04-16 11:51:28 +02:00
|
|
|
deny_warnings: bool,
|
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-04-16 13:16:28 +02:00
|
|
|
deny_warnings: bool,
|
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-04-16 12:51:11 +02:00
|
|
|
deny_warnings: bool,
|
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-04-16 14:14:49 +02:00
|
|
|
deny_warnings: bool,
|
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(
|
2023-04-16 12:51:11 +02:00
|
|
|
deny_warnings: bool,
|
2023-04-16 11:00:39 +02:00
|
|
|
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 {
|
2023-04-16 13:22:10 +02:00
|
|
|
format!("<no explicit target>")
|
2023-04-16 11:00:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let mode = if let Some(mode) = mode {
|
|
|
|
format!("{mode}")
|
|
|
|
} else {
|
|
|
|
format!("debug")
|
|
|
|
};
|
|
|
|
|
2023-04-16 12:51:11 +02:00
|
|
|
let deny_warnings = if deny_warnings {
|
|
|
|
format!("deny warnings, ")
|
|
|
|
} else {
|
|
|
|
format!("")
|
|
|
|
};
|
|
|
|
|
2023-04-16 11:00:39 +02:00
|
|
|
if cargoarg.is_some() && path.is_some() {
|
2023-04-16 12:51:11 +02:00
|
|
|
format!("({deny_warnings}{target}, {mode}, {feat}, {carg}, {in_dir})")
|
2023-04-16 11:00:39 +02:00
|
|
|
} else if cargoarg.is_some() {
|
2023-04-16 12:51:11 +02:00
|
|
|
format!("({deny_warnings}{target}, {mode}, {feat}, {carg})")
|
2023-04-16 11:00:39 +02:00
|
|
|
} else if path.is_some() {
|
2023-04-16 12:51:11 +02:00
|
|
|
format!("({deny_warnings}{target}, {mode}, {feat}, {in_dir})")
|
2023-04-15 13:18:18 +02:00
|
|
|
} else {
|
2023-04-16 12:51:11 +02:00
|
|
|
format!("({deny_warnings}{target}, {mode}, {feat})")
|
2023-04-15 13:18:18 +02:00
|
|
|
}
|
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} {}",
|
2023-04-16 12:51:11 +02:00
|
|
|
details(false, target, Some(mode), features, cargoarg, dir.as_ref())
|
2023-04-16 11:00:39 +02:00
|
|
|
)
|
|
|
|
}
|
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 12:51:11 +02:00
|
|
|
deny_warnings,
|
2023-04-15 23:22:00 +02:00
|
|
|
} => {
|
2023-04-16 12:51:11 +02:00
|
|
|
let warns = *deny_warnings;
|
|
|
|
let details = details(warns, target, Some(mode), features, cargoarg, dir.as_ref());
|
2023-04-16 11:00:39 +02:00
|
|
|
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 12:51:11 +02:00
|
|
|
deny_warnings,
|
2023-04-15 23:22:00 +02:00
|
|
|
} => {
|
2023-04-16 12:51:11 +02:00
|
|
|
let warns = *deny_warnings;
|
|
|
|
let details = details(warns, target, Some(mode), features, cargoarg, dir.as_ref());
|
2023-04-16 11:00:39 +02:00
|
|
|
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,
|
2023-04-16 12:51:11 +02:00
|
|
|
deny_warnings,
|
2023-04-15 13:18:18 +02:00
|
|
|
} => write!(
|
|
|
|
f,
|
|
|
|
"Check example {example} {}",
|
2023-04-16 12:51:11 +02:00
|
|
|
details(*deny_warnings, 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-16 12:51:11 +02:00
|
|
|
deny_warnings,
|
2023-04-15 13:18:18 +02:00
|
|
|
} => {
|
|
|
|
let package = p(package);
|
2023-04-16 12:51:11 +02:00
|
|
|
let warns = *deny_warnings;
|
2023-04-15 13:18:18 +02:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Build {package} {}",
|
2023-04-16 12:51:11 +02:00
|
|
|
details(warns, 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-16 12:51:11 +02:00
|
|
|
deny_warnings,
|
2023-04-15 13:18:18 +02:00
|
|
|
} => {
|
|
|
|
let package = p(package);
|
2023-04-16 12:51:11 +02:00
|
|
|
let warns = *deny_warnings;
|
2023-04-15 13:18:18 +02:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Check {package} {}",
|
2023-04-16 12:51:11 +02:00
|
|
|
details(warns, 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:51:28 +02:00
|
|
|
deny_warnings,
|
2023-04-15 13:18:18 +02:00
|
|
|
} => {
|
2023-04-16 12:51:11 +02:00
|
|
|
let details = details(*deny_warnings, target, None, features, cargoarg, None);
|
2023-04-15 13:18:18 +02:00
|
|
|
let package = p(package);
|
2023-04-16 12:51:11 +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,
|
2023-04-16 13:16:28 +02:00
|
|
|
deny_warnings,
|
2023-04-15 13:18:18 +02:00
|
|
|
} => {
|
|
|
|
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-16 13:16:28 +02:00
|
|
|
let deny_warnings = if *deny_warnings {
|
|
|
|
format!("deny warnings, ")
|
|
|
|
} else {
|
|
|
|
format!("")
|
|
|
|
};
|
2023-04-15 13:18:18 +02:00
|
|
|
if cargoarg.is_some() {
|
2023-04-16 13:16:28 +02:00
|
|
|
write!(f, "Document ({deny_warnings}{feat}, {carg}, {arguments})")
|
2023-04-15 13:18:18 +02:00
|
|
|
} else {
|
2023-04-16 13:16:28 +02:00
|
|
|
write!(f, "Document ({deny_warnings}{feat}, {arguments})")
|
2023-04-15 13:18:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CargoCommand::Test {
|
|
|
|
package,
|
|
|
|
features,
|
|
|
|
test,
|
2023-04-16 12:51:11 +02:00
|
|
|
deny_warnings,
|
2023-04-15 13:18:18 +02:00
|
|
|
} => {
|
|
|
|
let p = p(package);
|
|
|
|
let test = test
|
|
|
|
.clone()
|
|
|
|
.map(|t| format!("test {t}"))
|
|
|
|
.unwrap_or("all tests".into());
|
2023-04-16 12:51:11 +02:00
|
|
|
let deny_warnings = if *deny_warnings {
|
|
|
|
format!("deny warnings, ")
|
|
|
|
} else {
|
|
|
|
format!("")
|
|
|
|
};
|
2023-04-15 13:18:18 +02:00
|
|
|
let feat = feat(features);
|
2023-04-16 12:51:11 +02:00
|
|
|
write!(f, "Run {test} in {p} ({deny_warnings}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-16 14:14:49 +02:00
|
|
|
deny_warnings,
|
2023-04-15 13:18:18 +02:00
|
|
|
} => {
|
2023-04-16 14:14:49 +02:00
|
|
|
let warns = *deny_warnings;
|
|
|
|
let details = details(warns, target, Some(mode), features, cargoarg, dir.as_ref());
|
2023-04-16 11:00:39 +02:00
|
|
|
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-16 13:27:46 +02:00
|
|
|
let env = if let Some((key, value)) = self.extra_env() {
|
|
|
|
format!("{key}=\"{value}\" ")
|
|
|
|
} else {
|
|
|
|
format!("")
|
|
|
|
};
|
|
|
|
|
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-16 13:27:46 +02:00
|
|
|
format!("{env}{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: _,
|
2023-04-16 13:16:28 +02:00
|
|
|
// deny_warnings is exposed through `extra_env`
|
2023-04-16 12:51:11 +02:00
|
|
|
deny_warnings: _,
|
2023-04-16 11:00:39 +02:00
|
|
|
} => 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
|
|
|
// Target is added by build_args
|
|
|
|
target: _,
|
2023-04-16 12:51:11 +02:00
|
|
|
// Dir is exposed through `chdir`
|
|
|
|
dir: _,
|
2023-04-16 13:16:28 +02:00
|
|
|
// deny_warnings is exposed through `extra_env`
|
2023-04-16 12:51:11 +02:00
|
|
|
deny_warnings: _,
|
2023-04-16 11:00:39 +02:00
|
|
|
} => 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: _,
|
2023-04-16 13:16:28 +02:00
|
|
|
// deny_warnings is exposed through `extra_env`
|
2023-04-16 12:51:11 +02:00
|
|
|
deny_warnings: _,
|
2023-04-16 11:00:39 +02:00
|
|
|
} => 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: _,
|
2023-04-16 12:51:11 +02:00
|
|
|
deny_warnings,
|
2023-04-16 11:51:28 +02:00
|
|
|
} => {
|
2023-04-16 12:51:11 +02:00
|
|
|
let deny_warnings = if *deny_warnings {
|
|
|
|
vec!["--", "-D", "warnings"]
|
2023-04-16 11:51:28 +02:00
|
|
|
} else {
|
2023-04-16 12:51:11 +02:00
|
|
|
vec![]
|
2023-04-16 11:51:28 +02:00
|
|
|
};
|
|
|
|
|
2023-04-16 12:51:11 +02:00
|
|
|
let extra = p(package).chain(deny_warnings);
|
|
|
|
self.build_args(true, cargoarg, features, None, extra)
|
2023-04-16 11:51:28 +02:00
|
|
|
}
|
2023-03-02 22:02:19 +01:00
|
|
|
CargoCommand::Doc {
|
|
|
|
cargoarg,
|
|
|
|
features,
|
|
|
|
arguments,
|
2023-04-16 13:16:28 +02:00
|
|
|
// deny_warnings is exposed through `extra_env`
|
|
|
|
deny_warnings: _,
|
2023-03-02 22:02:19 +01:00
|
|
|
} => {
|
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 13:16:28 +02:00
|
|
|
// deny_warnings is exposed through `extra_env`
|
2023-04-16 12:51:11 +02:00
|
|
|
deny_warnings: _,
|
2023-02-28 23:55:02 +01:00
|
|
|
} => {
|
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: _,
|
2023-04-16 13:16:28 +02:00
|
|
|
// deny_warnings is exposed through `extra_env`
|
2023-04-16 12:51:11 +02:00
|
|
|
deny_warnings: _,
|
2023-04-16 11:00:39 +02:00
|
|
|
} => 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: _,
|
2023-04-16 13:16:28 +02:00
|
|
|
// deny_warnings is exposed through `extra_env`
|
2023-04-16 12:51:11 +02:00
|
|
|
deny_warnings: _,
|
2023-04-16 11:00:39 +02:00
|
|
|
} => 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-04-16 14:14:49 +02:00
|
|
|
// deny_warnings is exposed through `extra_env`
|
|
|
|
deny_warnings: _,
|
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-16 11:05:41 +02:00
|
|
|
pub fn chdir(&self) -> Option<&PathBuf> {
|
2023-04-14 23:59:23 +02:00
|
|
|
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
|
|
|
|
2023-04-16 13:16:28 +02:00
|
|
|
pub fn extra_env(&self) -> Option<(&str, &str)> {
|
2023-04-16 12:51:11 +02:00
|
|
|
match self {
|
|
|
|
// Clippy is a special case: it sets deny warnings
|
|
|
|
// through an argument to rustc.
|
|
|
|
CargoCommand::Clippy { .. } => None,
|
2023-04-16 13:16:28 +02:00
|
|
|
CargoCommand::Doc { .. } => Some(("RUSTDOCFLAGS", "-D warnings")),
|
2023-04-16 14:14:49 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-16 12:51:11 +02:00
|
|
|
CargoCommand::Check { deny_warnings, .. }
|
|
|
|
| CargoCommand::ExampleCheck { deny_warnings, .. }
|
|
|
|
| CargoCommand::Build { deny_warnings, .. }
|
2023-04-16 14:14:49 +02:00
|
|
|
| CargoCommand::Test { deny_warnings, .. } => {
|
2023-04-16 12:51:11 +02:00
|
|
|
if *deny_warnings {
|
2023-04-16 13:16:28 +02:00
|
|
|
Some(("RUSTFLAGS", "-D warnings"))
|
2023-04-16 12:51:11 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|