test-distro: build without cross toolchain

Make the xz2 dependency optional to allow building without a C cross
compiler. This allows clippy.sh to be used on e.g. macOS more easily:

```
./clippy.sh --target x86_64-unknown-linux-gnu --exclude-features xz2
```
main
Tamir Duberstein 1 week ago
parent edae5cd676
commit 5732b2c203

@ -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.

@ -37,17 +37,25 @@ pub fn read_to_end(path: &std::path::Path, compressed: bool) -> anyhow::Result<V
let mut contents = Vec::new();
if compressed {
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,
#[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)
}

@ -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")?;

Loading…
Cancel
Save