2019-04-05 18:56:44 +00:00
|
|
|
// Copyright 2019 The Chromium OS 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 std::env;
|
|
|
|
use std::error::Error;
|
2019-04-06 03:34:50 +00:00
|
|
|
use std::fs::{self, File};
|
2019-04-05 18:56:44 +00:00
|
|
|
use std::io::Write;
|
2019-04-06 03:34:50 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-04-05 18:56:44 +00:00
|
|
|
|
|
|
|
type Result<T> = std::result::Result<T, Box<dyn Error>>;
|
|
|
|
|
2019-04-06 03:34:50 +00:00
|
|
|
struct ExternalProto {
|
|
|
|
// Where to find protos during builds within cros_sdk. Relative to
|
|
|
|
// $SYSROOT environment variable set by emerge builds.
|
2019-04-05 18:56:44 +00:00
|
|
|
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,
|
|
|
|
|
2019-04-06 03:34:50 +00:00
|
|
|
// Code generated by proto compiler will be placed under
|
|
|
|
// protos::generated::$module_name.
|
|
|
|
module: &'static str,
|
2019-04-05 18:56:44 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 03:34:50 +00:00
|
|
|
// Rustfmt bug: https://github.com/rust-lang/rustfmt/issues/3498
|
|
|
|
#[rustfmt::skip]
|
|
|
|
static EXTERNAL_PROTOS: &[ExternalProto] = &[
|
|
|
|
#[cfg(feature = "trunks")]
|
|
|
|
ExternalProto {
|
|
|
|
dir_relative_to_sysroot: "usr/include/chromeos/dbus/trunks",
|
|
|
|
dir_relative_to_us: "../../../platform2/trunks",
|
2021-07-08 15:12:34 +00:00
|
|
|
proto_file_name: "trunks_interface.proto",
|
2019-04-06 03:34:50 +00:00
|
|
|
module: "trunks",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
struct LocalProto {
|
|
|
|
// Corresponding to the input file src/$module.proto.
|
|
|
|
module: &'static str,
|
2019-04-05 18:56:44 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 03:34:50 +00:00
|
|
|
#[rustfmt::skip]
|
|
|
|
static LOCAL_PROTOS: &[LocalProto] = &[
|
|
|
|
#[cfg(feature = "plugin")]
|
|
|
|
LocalProto { module: "plugin" },
|
2019-07-08 23:14:24 +00:00
|
|
|
#[cfg(feature = "composite-disk")]
|
|
|
|
LocalProto { module: "cdisk_spec" },
|
2019-04-06 03:34:50 +00:00
|
|
|
];
|
|
|
|
|
2019-04-05 18:56:44 +00:00
|
|
|
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.
|
2019-04-06 03:34:50 +00:00
|
|
|
let generated = PathBuf::from(&out_dir).join("generated.rs");
|
|
|
|
let out = File::create(generated)?;
|
2019-04-05 18:56:44 +00:00
|
|
|
|
2019-04-06 03:34:50 +00:00
|
|
|
// Compile external protos.
|
|
|
|
for proto in EXTERNAL_PROTOS {
|
2019-04-05 18:56:44 +00:00
|
|
|
let dir = match &sysroot {
|
|
|
|
Some(dir) => PathBuf::from(dir).join(proto.dir_relative_to_sysroot),
|
|
|
|
None => PathBuf::from(proto.dir_relative_to_us),
|
|
|
|
};
|
2019-04-06 03:34:50 +00:00
|
|
|
let input_path = dir.join(proto.proto_file_name);
|
|
|
|
protoc(proto.module, input_path, &out)?;
|
|
|
|
}
|
2019-04-05 18:56:44 +00:00
|
|
|
|
2019-04-06 03:34:50 +00:00
|
|
|
// Compile protos from the local src directory.
|
|
|
|
for proto in LOCAL_PROTOS {
|
|
|
|
let input_path = format!("src/{}.proto", proto.module);
|
|
|
|
protoc(proto.module, input_path, &out)?;
|
2019-04-05 18:56:44 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 03:34:50 +00:00
|
|
|
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.
|
2021-07-13 17:03:11 +00:00
|
|
|
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");
|
2019-04-05 18:56:44 +00:00
|
|
|
|
2019-04-06 03:34:50 +00:00
|
|
|
// Write out a `mod` that refers to the generated module.
|
2019-04-15 16:36:51 +00:00
|
|
|
//
|
|
|
|
// 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)].
|
2019-04-06 03:34:50 +00:00
|
|
|
let file_stem = input_path.file_stem().unwrap().to_str().unwrap();
|
|
|
|
writeln!(out, "#[path = \"{}/{}.rs\"]", out_dir, file_stem)?;
|
2019-04-15 16:36:51 +00:00
|
|
|
writeln!(out, "#[allow(renamed_and_removed_lints)]")?;
|
2019-04-06 03:34:50 +00:00
|
|
|
writeln!(out, "pub mod {};", module)?;
|
|
|
|
|
2019-04-05 18:56:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|