diff options
author | David Tolnay <dtolnay@chromium.org> | 2019-04-05 20:34:50 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2019-04-12 14:49:57 -0700 |
commit | 65928af6c9ebf46abcd4fbd908fc76137e9843cd (patch) | |
tree | 5aaacc0e15f10031d09de71dd81c6b5c1e254a81 | |
parent | 1aca8b72694fcbefc4f31bf49f41e7491ad29db4 (diff) | |
download | crosvm-65928af6c9ebf46abcd4fbd908fc76137e9843cd.tar crosvm-65928af6c9ebf46abcd4fbd908fc76137e9843cd.tar.gz crosvm-65928af6c9ebf46abcd4fbd908fc76137e9843cd.tar.bz2 crosvm-65928af6c9ebf46abcd4fbd908fc76137e9843cd.tar.lz crosvm-65928af6c9ebf46abcd4fbd908fc76137e9843cd.tar.xz crosvm-65928af6c9ebf46abcd4fbd908fc76137e9843cd.tar.zst crosvm-65928af6c9ebf46abcd4fbd908fc76137e9843cd.zip |
protos: Merge plugin_proto crate under protos::plugin
This de-duplicates the two separate build.rs files dealing with proto compilation. The trunks interface.proto will be exposed under protos::trunks and the plugin proto will be exposed under protos::plugin. BUG=none TEST=cargo check TEST=cargo check --features tpm TEST=cargo check --features plugin TEST=cargo check --features tpm,plugin TEST=FEATURES=test emerge-nami crosvm TEST=FEATURES=test USE=crosvm-tpm emerge-nami crosvm TEST=FEATURES=test USE=crosvm-plugin emerge-nami crosvm TEST=FEATURES=test USE='crosvm-tpm crosvm-plugin' emerge-nami crosvm TEST=local kokoro CQ-DEPEND=CL:1553971 Change-Id: I203b654a38e9d671a508156ae06dfb6f70047c4f Reviewed-on: https://chromium-review.googlesource.com/1556417 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: David Tolnay <dtolnay@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
-rw-r--r-- | Cargo.lock | 15 | ||||
-rw-r--r-- | Cargo.toml | 4 | ||||
-rw-r--r-- | crosvm_plugin/Cargo.toml | 2 | ||||
-rw-r--r-- | crosvm_plugin/src/lib.rs | 6 | ||||
-rw-r--r-- | devices/Cargo.toml | 2 | ||||
-rw-r--r-- | plugin_proto/Cargo.toml | 13 | ||||
-rw-r--r-- | plugin_proto/build.rs | 30 | ||||
-rw-r--r-- | protos/Cargo.toml | 5 | ||||
-rw-r--r-- | protos/build.rs | 98 | ||||
-rw-r--r-- | protos/src/lib.rs | 10 | ||||
-rw-r--r-- | protos/src/plugin.proto (renamed from plugin_proto/protos/plugin.proto) | 0 | ||||
-rw-r--r-- | protos/src/plugin.rs (renamed from plugin_proto/src/lib.rs) | 4 | ||||
-rw-r--r-- | protos/tests/trunks.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 4 | ||||
-rw-r--r-- | src/plugin/process.rs | 6 | ||||
-rw-r--r-- | src/plugin/vcpu.rs | 2 |
16 files changed, 96 insertions, 107 deletions
diff --git a/Cargo.lock b/Cargo.lock index 19667d1..eab4e90 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -100,8 +100,8 @@ dependencies = [ "msg_socket 0.1.0", "net_util 0.1.0", "p9 0.1.0", - "plugin_proto 0.17.0", "protobuf 2.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "protos 0.1.0", "qcow 0.1.0", "rand_ish 0.1.0", "render_node_forward 0.1.0", @@ -120,8 +120,8 @@ dependencies = [ "kvm 0.1.0", "kvm_sys 0.1.0", "libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)", - "plugin_proto 0.17.0", "protobuf 2.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "protos 0.1.0", "sys_util 0.1.0", ] @@ -327,16 +327,6 @@ version = "0.3.11" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "plugin_proto" -version = "0.17.0" -dependencies = [ - "cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", - "kvm_sys 0.1.0", - "protobuf 2.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "protoc-rust 2.4.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] name = "poll_token_derive" version = "0.1.0" dependencies = [ @@ -389,6 +379,7 @@ dependencies = [ name = "protos" version = "0.1.0" dependencies = [ + "kvm_sys 0.1.0", "protobuf 2.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "protoc-rust 2.4.2 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/Cargo.toml b/Cargo.toml index 27b128c..6baf7e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ exclude = [ default-no-sandbox = [] gpu = ["devices/gpu"] gpu-forward = ["render_node_forward"] -plugin = ["plugin_proto", "crosvm_plugin", "protobuf"] +plugin = ["protos/plugin", "crosvm_plugin", "protobuf"] sandboxed-libusb = ["devices/sandboxed-libusb", "vm_control/sandboxed-libusb"] tpm = ["devices/tpm"] wl-dmabuf = ["devices/wl-dmabuf", "gpu_buffer", "resources/wl-dmabuf"] @@ -49,8 +49,8 @@ libcras = "*" msg_socket = { path = "msg_socket" } net_util = { path = "net_util" } p9 = { path = "p9" } -plugin_proto = { path = "plugin_proto", optional = true } protobuf = { version = "2.3", optional = true } +protos = { path = "protos", optional = true } qcow = { path = "qcow" } rand_ish = { path = "rand_ish" } render_node_forward = { path = "render_node_forward", optional = true } diff --git a/crosvm_plugin/Cargo.toml b/crosvm_plugin/Cargo.toml index 379d837..6805898 100644 --- a/crosvm_plugin/Cargo.toml +++ b/crosvm_plugin/Cargo.toml @@ -11,6 +11,6 @@ crate-type = ["cdylib"] kvm = { path = "../kvm" } kvm_sys = { path = "../kvm_sys" } libc = "*" -plugin_proto = { path = "../plugin_proto" } protobuf = "2.3" +protos = { path = "../protos", features = ["plugin"] } sys_util = { path = "../sys_util" } diff --git a/crosvm_plugin/src/lib.rs b/crosvm_plugin/src/lib.rs index ac6b358..8b0a222 100644 --- a/crosvm_plugin/src/lib.rs +++ b/crosvm_plugin/src/lib.rs @@ -6,7 +6,7 @@ //! This module implements the dynamically loaded client library API used by a crosvm plugin, //! defined in `crosvm.h`. It implements the client half of the plugin protocol, which is defined in -//! the plugin_proto module. +//! the `protos::plugin` module. //! //! To implement the `crosvm.h` C API, each function and struct definition is repeated here, with //! concrete definitions for each struct. Most functions are thin shims to the underlying object @@ -18,8 +18,8 @@ extern crate kvm; extern crate kvm_sys; extern crate libc; -extern crate plugin_proto; extern crate protobuf; +extern crate protos; extern crate sys_util; use std::env; @@ -50,7 +50,7 @@ use kvm_sys::{ kvm_vcpu_events, kvm_xcrs, }; -use plugin_proto::*; +use protos::plugin::*; // Needs to be large enough to receive all the VCPU sockets. const MAX_DATAGRAM_FD: usize = 32; diff --git a/devices/Cargo.toml b/devices/Cargo.toml index d916df8..7a09f4a 100644 --- a/devices/Cargo.toml +++ b/devices/Cargo.toml @@ -6,7 +6,7 @@ authors = ["The Chromium OS Authors"] [features] gpu = ["gpu_buffer", "gpu_display", "gpu_renderer"] sandboxed-libusb = ["usb_util/sandboxed-libusb"] -tpm = ["protos", "tpm2"] +tpm = ["protos/trunks", "tpm2"] wl-dmabuf = [] [dependencies] diff --git a/plugin_proto/Cargo.toml b/plugin_proto/Cargo.toml deleted file mode 100644 index ea2dac9..0000000 --- a/plugin_proto/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "plugin_proto" -version = "0.17.0" -authors = ["The Chromium OS Authors"] -build = "build.rs" - -[dependencies] -protobuf = "2.3" -kvm_sys = { path = "../kvm_sys" } - -[build-dependencies] -cc = "=1.0.25" -protoc-rust = "2.3" diff --git a/plugin_proto/build.rs b/plugin_proto/build.rs deleted file mode 100644 index d2ef8c9..0000000 --- a/plugin_proto/build.rs +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2017 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. - -extern crate protoc_rust; - -use std::env; -use std::fs; -use std::io::Write; -use std::path::PathBuf; - -fn main() { - let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); - - protoc_rust::run(protoc_rust::Args { - out_dir: out_dir.as_os_str().to_str().unwrap(), - input: &["protos/plugin.proto"], - includes: &["protos"], - ..Default::default() - }) - .expect("protoc"); - - let mut mod_out = fs::File::create(out_dir.join("proto_include.rs")).unwrap(); - writeln!( - mod_out, - "#[path = \"{}\"] pub mod plugin_proto;\npub use plugin_proto::*;", - out_dir.join("plugin.rs").display() - ) - .unwrap(); -} diff --git a/protos/Cargo.toml b/protos/Cargo.toml index 1bc2c49..9fd0e5a 100644 --- a/protos/Cargo.toml +++ b/protos/Cargo.toml @@ -4,7 +4,12 @@ version = "0.1.0" authors = ["The Chromium OS Authors"] edition = "2018" +[features] +plugin = ["kvm_sys"] +trunks = [] + [dependencies] +kvm_sys = { path = "../kvm_sys", optional = true } protobuf = "2.3" [build-dependencies] diff --git a/protos/build.rs b/protos/build.rs index f4f5d97..fa839ca 100644 --- a/protos/build.rs +++ b/protos/build.rs @@ -4,15 +4,15 @@ use std::env; use std::error::Error; -use std::fs::File; +use std::fs::{self, File}; use std::io::Write; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; type Result<T> = std::result::Result<T, Box<dyn Error>>; -struct Proto { - // Where to find protos during builds within cros_sdk. Relative to $SYSROOT - // environment variable set by emerge builds. +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 @@ -22,57 +22,85 @@ struct Proto { // *.proto file expected to exist in both of the above directories. proto_file_name: &'static str, - // Code generated by proto compiler will be exported under - // protos::$module_name. - name_for_module: &'static str, + // Code generated by proto compiler will be placed under + // protos::generated::$module_name. + module: &'static str, } -static PROTOS: &[Proto] = &[Proto { - dir_relative_to_sysroot: "usr/include/chromeos/dbus/trunks", - dir_relative_to_us: "../../../platform2/trunks", - proto_file_name: "interface.proto", - name_for_module: "trunks", -}]; - -fn paths_to_strs(paths: &[PathBuf]) -> Vec<&str> { - paths - .iter() - .map(|p| p.as_os_str().to_str().unwrap()) - .collect() +// 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", + proto_file_name: "interface.proto", + module: "trunks", + }, +]; + +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" }, +]; + 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 protos_rs = PathBuf::from(&out_dir).join("protos.rs"); - let mut out = File::create(protos_rs)?; - - let mut input_files = Vec::new(); - let mut include_dirs = Vec::new(); + let generated = PathBuf::from(&out_dir).join("generated.rs"); + let out = File::create(generated)?; - for proto in PROTOS { + // 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)?; + } - input_files.push(dir.join(proto.proto_file_name)); - include_dirs.push(dir); - - let generated_module = proto.proto_file_name.replace(".proto", ""); - writeln!(out, "#[path = \"{}/{}.rs\"]", out_dir, generated_module)?; - writeln!(out, "pub mod {};", proto.name_for_module)?; + // 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)?; } - // Invoke proto compiler. + 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::run(protoc_rust::Args { out_dir: &out_dir, - input: &paths_to_strs(&input_files), - includes: &paths_to_strs(&include_dirs), + includes: &[input_dir.as_os_str().to_str().unwrap()], + input: &[input_path.as_os_str().to_str().unwrap()], ..Default::default() })?; + // Write out a `mod` that refers to the generated module. + let file_stem = input_path.file_stem().unwrap().to_str().unwrap(); + writeln!(out, "#[path = \"{}/{}.rs\"]", out_dir, file_stem)?; + writeln!(out, "pub mod {};", module)?; + Ok(()) } diff --git a/protos/src/lib.rs b/protos/src/lib.rs index 5381935..3fdbdc0 100644 --- a/protos/src/lib.rs +++ b/protos/src/lib.rs @@ -2,4 +2,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -include!(concat!(env!("OUT_DIR"), "/protos.rs")); +mod generated { + include!(concat!(env!("OUT_DIR"), "/generated.rs")); +} + +#[cfg(feature = "plugin")] +pub mod plugin; + +#[cfg(feature = "trunks")] +pub use generated::trunks; diff --git a/plugin_proto/protos/plugin.proto b/protos/src/plugin.proto index 5b6eec2..5b6eec2 100644 --- a/plugin_proto/protos/plugin.proto +++ b/protos/src/plugin.proto diff --git a/plugin_proto/src/lib.rs b/protos/src/plugin.rs index ff95be9..3e58fe3 100644 --- a/plugin_proto/src/lib.rs +++ b/protos/src/plugin.rs @@ -1,6 +1,4 @@ -extern crate kvm_sys; -extern crate protobuf; -include!(concat!(env!("OUT_DIR"), "/proto_include.rs")); +pub use crate::generated::plugin::*; /// Converts protobuf representation of CpuId data into KVM format. pub fn cpuid_proto_to_kvm(entry: &CpuidEntry) -> kvm_sys::kvm_cpuid_entry2 { diff --git a/protos/tests/trunks.rs b/protos/tests/trunks.rs index 574c110..39723ae 100644 --- a/protos/tests/trunks.rs +++ b/protos/tests/trunks.rs @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#![cfg(feature = "trunks")] + mod common; use crate::common::test_round_trip; diff --git a/src/main.rs b/src/main.rs index 2242d71..ff466e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,9 +28,9 @@ extern crate data_model; extern crate gpu_buffer; extern crate msg_socket; #[cfg(feature = "plugin")] -extern crate plugin_proto; -#[cfg(feature = "plugin")] extern crate protobuf; +#[cfg(feature = "plugin")] +extern crate protos; extern crate rand_ish; extern crate resources; extern crate sync; diff --git a/src/plugin/process.rs b/src/plugin/process.rs index 1ab77cb..b249cea 100644 --- a/src/plugin/process.rs +++ b/src/plugin/process.rs @@ -22,10 +22,10 @@ use libc::{pid_t, waitpid, EINVAL, ENODATA, ENOTTY, WEXITSTATUS, WIFEXITED, WNOH use protobuf; use protobuf::Message; -use ::plugin_proto::*; use io_jail::Minijail; use kvm::{dirty_log_bitmap_size, Datamatch, IoeventAddress, IrqRoute, IrqSource, PicId, Vm}; use kvm_sys::{kvm_clock_data, kvm_ioapic_state, kvm_pic_state, kvm_pit_state2}; +use protos::plugin::*; use sync::Mutex; use sys_util::{ Error as SysError, EventFd, GuestAddress, Killable, MemoryMapping, Result as SysResult, @@ -110,8 +110,8 @@ pub enum ProcessStatus { /// Creates, owns, and handles messages from a plugin process. /// /// A plugin process has control over a single VM and a fixed number of VCPUs via a set of unix -/// domain socket connections and a protocol defined in `plugin_proto`. The plugin process is run in -/// an unprivileged manner as a child process spawned via a path to a arbitrary executable. +/// domain socket connections and a protocol defined in `protos::plugin`. The plugin process is run +/// in an unprivileged manner as a child process spawned via a path to a arbitrary executable. pub struct Process { started: bool, plugin_pid: pid_t, diff --git a/src/plugin/vcpu.rs b/src/plugin/vcpu.rs index 32e0f81..1e6b782 100644 --- a/src/plugin/vcpu.rs +++ b/src/plugin/vcpu.rs @@ -15,13 +15,13 @@ use libc::{EINVAL, ENOENT, ENOTTY, EPERM, EPIPE, EPROTO}; use protobuf; use protobuf::Message; -use ::plugin_proto::*; use data_model::DataInit; use kvm::{CpuId, Vcpu}; use kvm_sys::{ kvm_debugregs, kvm_fpu, kvm_lapic_state, kvm_mp_state, kvm_msr_entry, kvm_msrs, kvm_regs, kvm_sregs, kvm_vcpu_events, kvm_xcrs, KVM_CPUID_FLAG_SIGNIFCANT_INDEX, }; +use protos::plugin::*; use sync::Mutex; use super::*; |