mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-02-09 03:57:24 +00:00
protos: migrate to new build tool & cleanup.
As part of migrating to proto_build_tools, we've cleaned up some old code that was unused: * All of the ExternalProto tooling that was unused. * The vsock proto doesn't exist. BUG=b:256951877 TEST=builds Change-Id: I647395b4900d487179139050ab6750a464b528e7 Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4021592 Reviewed-by: Vikram Auradkar <auradkar@google.com> Commit-Queue: Noah Gold <nkgold@google.com>
This commit is contained in:
parent
ea5ffed771
commit
2131a614e5
3 changed files with 13 additions and 81 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1603,8 +1603,8 @@ name = "protos"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"kvm_sys",
|
||||
"proto_build_tools",
|
||||
"protobuf",
|
||||
"protoc-rust",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -13,4 +13,4 @@ kvm_sys = { path = "../kvm_sys", optional = true }
|
|||
protobuf = "2.3"
|
||||
|
||||
[build-dependencies]
|
||||
protoc-rust = "2.3"
|
||||
proto_build_tools = { path = "../proto_build_tools" }
|
||||
|
|
|
@ -4,105 +4,37 @@
|
|||
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
type Result<T> = std::result::Result<T, Box<dyn Error>>;
|
||||
|
||||
struct ExternalProto {
|
||||
// Where to find protos during builds within cros_sdk. Relative to
|
||||
// $SYSROOT environment variable set by emerge builds.
|
||||
dir_relative_to_sysroot: &'static str,
|
||||
|
||||
// Where to find protos during "cargo build" in a local developer
|
||||
// environment. Relative to the platform/crosvm/protos directory.
|
||||
dir_relative_to_us: &'static str,
|
||||
|
||||
// *.proto file expected to exist in both of the above directories.
|
||||
proto_file_name: &'static str,
|
||||
|
||||
// Code generated by proto compiler will be placed under
|
||||
// protos::generated::$module_name.
|
||||
module: &'static str,
|
||||
}
|
||||
|
||||
// Rustfmt bug: https://github.com/rust-lang/rustfmt/issues/3498
|
||||
#[rustfmt::skip]
|
||||
static EXTERNAL_PROTOS: &[ExternalProto] = &[];
|
||||
|
||||
struct LocalProto {
|
||||
// Corresponding to the input file src/$module.proto.
|
||||
module: &'static str,
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
static LOCAL_PROTOS: &[LocalProto] = &[
|
||||
#[cfg(feature = "plugin")]
|
||||
LocalProto { module: "plugin" },
|
||||
#[cfg(feature = "composite-disk")]
|
||||
LocalProto { module: "cdisk_spec" },
|
||||
LocalProto {
|
||||
module: "cdisk_spec",
|
||||
},
|
||||
];
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let out_dir = env::var("OUT_DIR")?;
|
||||
let sysroot = env::var_os("SYSROOT");
|
||||
|
||||
// Write out a Rust module that imports the modules generated by protoc.
|
||||
let generated = PathBuf::from(&out_dir).join("generated.rs");
|
||||
let out = File::create(generated)?;
|
||||
|
||||
// Compile external protos.
|
||||
for proto in EXTERNAL_PROTOS {
|
||||
let dir = match &sysroot {
|
||||
Some(dir) => PathBuf::from(dir).join(proto.dir_relative_to_sysroot),
|
||||
None => PathBuf::from(proto.dir_relative_to_us),
|
||||
};
|
||||
let input_path = dir.join(proto.proto_file_name);
|
||||
protoc(proto.module, input_path, &out)?;
|
||||
}
|
||||
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
|
||||
|
||||
// Compile protos from the local src directory.
|
||||
let mut proto_paths = Vec::new();
|
||||
for proto in LOCAL_PROTOS {
|
||||
let input_path = format!("src/{}.proto", proto.module);
|
||||
protoc(proto.module, input_path, &out)?;
|
||||
proto_paths.push(
|
||||
["src", &format!("{}.proto", proto.module)]
|
||||
.iter()
|
||||
.collect::<PathBuf>(),
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Compile a single proto file located at $input_path, placing the generated
|
||||
// code at $OUT_DIR/$module and emitting the right `pub mod $module` into the
|
||||
// output file.
|
||||
fn protoc<P: AsRef<Path>>(module: &str, input_path: P, mut out: &File) -> Result<()> {
|
||||
let input_path = input_path.as_ref();
|
||||
let input_dir = input_path.parent().unwrap();
|
||||
|
||||
// Place output in a subdirectory so that different protos with the same
|
||||
// common filename (like interface.proto) do not conflict.
|
||||
let out_dir = format!("{}/{}", env::var("OUT_DIR")?, module);
|
||||
fs::create_dir_all(&out_dir)?;
|
||||
|
||||
// Invoke protobuf compiler.
|
||||
protoc_rust::Codegen::new()
|
||||
.input(input_path.as_os_str().to_str().unwrap())
|
||||
.include(input_dir.as_os_str().to_str().unwrap())
|
||||
.out_dir(&out_dir)
|
||||
.run()
|
||||
.expect("protoc");
|
||||
|
||||
// Write out a `mod` that refers to the generated module.
|
||||
//
|
||||
// The lint suppression is because protoc-rust emits
|
||||
// #![cfg_attr(feature = "cargo-clippy", allow(clippy))]
|
||||
// which still works but is deprecated in favor of tool attributes:
|
||||
// #![allow(clippy::all)].
|
||||
let file_stem = input_path.file_stem().unwrap().to_str().unwrap();
|
||||
writeln!(out, "#[path = \"{}/{}.rs\"]", out_dir, file_stem)?;
|
||||
writeln!(out, "#[allow(renamed_and_removed_lints)]")?;
|
||||
writeln!(out, "pub mod {};", module)?;
|
||||
proto_build_tools::build_protos(&out_dir, proto_paths.as_slice());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue