blob: 7e20d635b8c849355fa8be01a612191ffb28a0e4 [file] [log] [blame]
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use {
anyhow::{anyhow, bail, Context, Result},
ffx_core::ffx_plugin,
ffx_scrutiny_verify_args::{Command, SubCommand},
scrutiny_utils::path::relativize_path,
std::{fs, io::Write, path::PathBuf},
};
mod bootfs;
mod component_resolvers;
mod kernel_cmdline;
mod route_sources;
mod routes;
mod static_pkgs;
#[ffx_plugin()]
pub async fn scrutiny_verify(cmd: Command) -> Result<()> {
if cmd.depfile.is_some() && cmd.stamp.is_none() {
bail!("Cannot specify --depfile without --stamp");
}
let deps_set = match &cmd.subcommand {
SubCommand::Bootfs(cmd) => bootfs::verify(cmd).await,
SubCommand::ComponentResolvers(cmd) => component_resolvers::verify(cmd).await,
SubCommand::KernelCmdline(cmd) => kernel_cmdline::verify(cmd).await,
SubCommand::RouteSources(cmd) => route_sources::verify(cmd).await,
SubCommand::Routes(cmd) => routes::verify(cmd).await,
SubCommand::StaticPkgs(cmd) => static_pkgs::verify(cmd).await,
}?;
if let Some(depfile_path) = cmd.depfile.as_ref() {
let stamp_path = cmd
.stamp
.as_ref()
.ok_or_else(|| anyhow!("Cannot specify depfile without specifying stamp"))?;
let stamp_path = stamp_path.to_str().ok_or_else(|| {
anyhow!(
"Stamp path {:?} cannot be converted to string for writing to depfile",
stamp_path
)
})?;
let mut depfile = fs::File::create(depfile_path).context("failed to create depfile")?;
// Convert any absolute paths into paths relative to `build_path` to satisfy depfile format
// requirements.
let default_build_path = PathBuf::from(String::from("."));
let build_path = match &cmd.subcommand {
SubCommand::Bootfs(_) | SubCommand::KernelCmdline(_) => &default_build_path,
SubCommand::ComponentResolvers(subcommand) => &subcommand.build_path,
SubCommand::RouteSources(subcommand) => &subcommand.build_path,
SubCommand::Routes(subcommand) => &subcommand.build_path,
SubCommand::StaticPkgs(subcommand) => &subcommand.build_path,
};
let relative_dep_paths: Vec<PathBuf> =
deps_set.into_iter().map(|dep_path| relativize_path(build_path, dep_path)).collect();
let deps = relative_dep_paths
.iter()
.map(|dep_path| {
dep_path.to_str().ok_or_else(|| {
anyhow!("Failed to convert path for depfile to string: {:?}", dep_path)
})
})
.collect::<Result<Vec<&str>>>()?;
write!(depfile, "{}: {}", stamp_path, deps.join(" "))
.context("failed to write to depfile")?;
}
if let Some(stamp_path) = cmd.stamp.as_ref() {
fs::write(stamp_path, "Verified\n").context("failed to write stamp file")?;
}
Ok(())
}