diff --git a/test-distro/Cargo.toml b/test-distro/Cargo.toml index 3623cee5..5ec27d2f 100644 --- a/test-distro/Cargo.toml +++ b/test-distro/Cargo.toml @@ -35,4 +35,4 @@ nix = { workspace = true, features = [ ] } object = { workspace = true, features = ["elf", "read_core", "std"] } walkdir = { workspace = true } -xz2 = { workspace = true } +xz2 = { workspace = true, optional = true } # Optional to allow building without a cross toolchain. diff --git a/test-distro/src/lib.rs b/test-distro/src/lib.rs index c18b2beb..77a9b299 100644 --- a/test-distro/src/lib.rs +++ b/test-distro/src/lib.rs @@ -37,17 +37,25 @@ pub fn read_to_end(path: &std::path::Path, compressed: bool) -> anyhow::Result Some(len), - Err(std::num::TryFromIntError { .. }) => None, + #[cfg(feature = "xz2")] + { + let stat = f.metadata().context("metadata()")?; + #[expect(clippy::manual_ok_err)] + let len = match usize::try_from(stat.len()) { + Ok(len) => Some(len), + Err(std::num::TryFromIntError { .. }) => None, + } + .and_then(|len| len.checked_mul(2)) + .ok_or_else(|| anyhow::anyhow!("2 * {stat:?}.len() is too large to fit in a usize"))?; + contents.reserve(len); + + xz2::read::XzDecoder::new(f).read_to_end(&mut contents) } - .and_then(|len| len.checked_mul(2)) - .ok_or_else(|| anyhow::anyhow!("2 * {stat:?}.len() is too large to fit in a usize"))?; - contents.reserve(len); - xz2::read::XzDecoder::new(f).read_to_end(&mut contents) + #[cfg(not(feature = "xz2"))] + { + anyhow::bail!("cannot read {} without xz2 feature", path.display()); + } } else { f.read_to_end(&mut contents) } diff --git a/xtask/src/run.rs b/xtask/src/run.rs index 0cf1738d..187828f9 100644 --- a/xtask/src/run.rs +++ b/xtask/src/run.rs @@ -338,10 +338,11 @@ pub fn run(opts: Options) -> Result<()> { let target = format!("{guest_arch}-unknown-linux-musl"); - let test_distro: Vec<(String, PathBuf)> = build(Some(&target), |cmd| { - cmd.args(["--package", "test-distro", "--profile", "release"]) - }) - .context("building test-distro package failed")?; + let test_distro_args = + ["--package", "test-distro", "--release", "--features", "xz2"]; + let test_distro: Vec<(String, PathBuf)> = + build(Some(&target), |cmd| cmd.args(test_distro_args)) + .context("building test-distro package failed")?; let binaries = binaries(Some(&target))?; @@ -415,16 +416,9 @@ pub fn run(opts: Options) -> Result<()> { // Preparing the `modules.alias` file inside the VM as part of // `/init` is slow. It's faster to prepare it here. Command::new("cargo") - .args([ - "run", - "--package", - "test-distro", - "--bin", - "depmod", - "--release", - "--", - "-b", - ]) + .arg("run") + .args(test_distro_args) + .args(["--bin", "depmod", "--", "-b"]) .arg(&modules_dir) .status() .context("failed to run depmod")?;