summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2024-01-20 14:09:54 +0200
committerLars Wirzenius <liw@liw.fi>2024-01-20 14:09:54 +0200
commit3064f7e293228981340dec1b5c86788439d1b4d4 (patch)
treee0065edb6e8bf6beb1e0659011a8bf5eaa667540
parentd8bcea71acca62a240685fb6c3204b47e05a088a (diff)
downloadambient-driver-3064f7e293228981340dec1b5c86788439d1b4d4.tar.gz
feat: set CARGO_TARGET_DIR to /workspace/cache for Rust actions
This allows Rust builds to be incremental automatically. Signed-off-by: Lars Wirzenius <liw@liw.fi> Sponsored-by: author
-rw-r--r--src/action.rs70
1 files changed, 41 insertions, 29 deletions
diff --git a/src/action.rs b/src/action.rs
index 87aa002..c32a94a 100644
--- a/src/action.rs
+++ b/src/action.rs
@@ -12,6 +12,8 @@ use crate::{
vdrive::{VirtualDriveBuilder, VirtualDriveError},
};
+const RUST_ENVS: &[(&str, &str)] = &[("CARGO_TARGET_DIR", qemu::CACHE_DIR)];
+
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "action")]
#[serde(rename_all = "snake_case")]
@@ -138,7 +140,7 @@ impl Action {
Self::Mkdir { pathname } => mkdir(pathname),
Self::TarCreate { archive, directory } => tar_create(archive, directory),
Self::TarExtract { archive, directory } => tar_extract(archive, directory),
- Self::Spawn { argv } => spawn(argv),
+ Self::Spawn { argv } => spawn(argv, &[]),
Self::Shell { shell: snippet } => shell(snippet),
Self::RustupSetup => rustup_setup(),
Self::CargoFmt => cargo_fmt(),
@@ -202,7 +204,7 @@ fn tar_extract(archive: &Path, dirname: &Path) -> Result<(), ActionError> {
Ok(())
}
-fn spawn_in(argv: &[String], cwd: &Path) -> Result<(), ActionError> {
+fn spawn_in(argv: &[String], cwd: &Path, extra_env: &[(&str, &str)]) -> Result<(), ActionError> {
let argv0 = if let Some(argv0) = argv.first() {
argv0
} else {
@@ -212,6 +214,7 @@ fn spawn_in(argv: &[String], cwd: &Path) -> Result<(), ActionError> {
let mut cmd = Command::new(argv0)
.args(&argv[1..])
+ .envs(extra_env.iter().copied())
.stdin(Stdio::null())
.current_dir(cwd)
.spawn()
@@ -233,21 +236,21 @@ fn spawn_in(argv: &[String], cwd: &Path) -> Result<(), ActionError> {
fn spawn_str_in(argv: &[&str], cwd: &Path) -> Result<(), ActionError> {
let argv: Vec<String> = argv.iter().map(|s| s.to_string()).collect();
- spawn_in(&argv, cwd)
+ spawn_in(&argv, cwd, &[])
}
-fn spawn(argv: &[String]) -> Result<(), ActionError> {
- spawn_in(argv, Path::new(qemu::SOURCE_DIR))
+fn spawn(argv: &[String], extra_env: &[(&str, &str)]) -> Result<(), ActionError> {
+ spawn_in(argv, Path::new(qemu::SOURCE_DIR), extra_env)
}
fn shell(snippet: &str) -> Result<(), ActionError> {
let snippet = format!("set -xeuo pipefail\n{}", snippet);
- spawn(&["/bin/bash".into(), "-c".into(), snippet])
+ spawn(&["/bin/bash".into(), "-c".into(), snippet], &[])
}
-fn spawn_str(argv: &[&str]) -> Result<(), ActionError> {
+fn spawn_str(argv: &[&str], extra_env: &[(&str, &str)]) -> Result<(), ActionError> {
let argv: Vec<String> = argv.iter().map(|s| s.to_string()).collect();
- spawn(&argv)
+ spawn(&argv, extra_env)
}
fn rustup_setup() -> Result<(), ActionError> {
@@ -282,37 +285,46 @@ fn rustup_setup() -> Result<(), ActionError> {
}
fn cargo_fmt() -> Result<(), ActionError> {
- spawn_str(&["cargo", "fmt", "--offline", "--workspace", "--all-targets"])
+ spawn_str(
+ &["cargo", "fmt", "--offline", "--workspace", "--all-targets"],
+ RUST_ENVS,
+ )
}
fn cargo_clippy() -> Result<(), ActionError> {
- spawn_str(&["cargo", "clippy", "--all"])
+ spawn_str(&["cargo", "clippy", "--all"], RUST_ENVS)
}
fn cargo_build() -> Result<(), ActionError> {
- spawn_str(&[
- "cargo",
- "build",
- "--offline",
- "--workspace",
- "--all-targets",
- ])
+ spawn_str(
+ &[
+ "cargo",
+ "build",
+ "--offline",
+ "--workspace",
+ "--all-targets",
+ ],
+ RUST_ENVS,
+ )
}
fn cargo_test() -> Result<(), ActionError> {
- spawn_str(&["cargo", "test", "--offline", "--workspace"])
+ spawn_str(&["cargo", "test", "--offline", "--workspace"], RUST_ENVS)
}
fn cargo_install() -> Result<(), ActionError> {
- spawn_str(&[
- "cargo",
- "install",
- "--offline",
- "--bins",
- "--path=.",
- "--root",
- qemu::ARTIFACTS_DIR,
- ])
+ spawn_str(
+ &[
+ "cargo",
+ "install",
+ "--offline",
+ "--bins",
+ "--path=.",
+ "--root",
+ qemu::ARTIFACTS_DIR,
+ ],
+ RUST_ENVS,
+ )
}
fn deb() -> Result<(), ActionError> {
@@ -335,7 +347,7 @@ mv ../*_* {}
qemu::ARTIFACTS_DIR
);
- spawn_str(&["bash", "-c", &shell])
+ spawn_str(&["bash", "-c", &shell], RUST_ENVS)
}
#[derive(Debug, thiserror::Error)]
@@ -424,6 +436,6 @@ mod test {
// We can't use the Spawn action here, as it expects the
// SOURCE_DIR to exist. However, spawn_in does the same thing,
// except in a directory of our choosing.
- assert!(spawn_in(&["true".into()], Path::new("/")).is_ok());
+ assert!(spawn_in(&["true".into()], Path::new("/"), &[]).is_ok());
}
}