Floss: Add protobuf build to btmanagerd
Prepares build system to support protobuf with powerd D-Bus integration.
Bug: 224606285
Tag: #floss
Test: Manual - build.py
Change-Id: I9b701a84d11dde3ed9414711a371707106f28b57
diff --git a/build.py b/build.py
index 2bffdc3..b32c5eb 100755
--- a/build.py
+++ b/build.py
@@ -246,6 +246,7 @@
self.env['CARGO_HOME'] = os.path.join(self.output_dir, 'cargo_home')
self.env['RUSTFLAGS'] = self._generate_rustflags()
self.env['CXX_ROOT_PATH'] = os.path.join(self.platform_dir, 'bt')
+ self.env['CROS_SYSTEM_API_ROOT'] = os.path.join(self.platform_dir, 'system_api')
def run_command(self, target, args, cwd=None, env=None):
""" Run command and stream the output.
@@ -606,6 +607,7 @@
# Symlink things
symlinks = [
(os.path.join(self.git_dir, 'platform2', 'common-mk'), os.path.join(self.staging_dir, 'common-mk')),
+ (os.path.join(self.git_dir, 'platform2', 'system_api'), os.path.join(self.staging_dir, 'system_api')),
(os.path.join(self.git_dir, 'platform2', '.gn'), os.path.join(self.staging_dir, '.gn')),
(os.path.join(self.bt_dir), os.path.join(self.staging_dir, 'bt')),
(os.path.join(self.git_dir, 'rust_crates'), os.path.join(self.external_dir, 'rust')),
diff --git a/system/gd/rust/linux/mgmt/Cargo.toml b/system/gd/rust/linux/mgmt/Cargo.toml
index 295fa8e..bf4e74b 100644
--- a/system/gd/rust/linux/mgmt/Cargo.toml
+++ b/system/gd/rust/linux/mgmt/Cargo.toml
@@ -21,6 +21,7 @@
inotify = "*"
log = "0.4.14"
nix = "*"
+protobuf = "2.0"
regex = "1.5"
serde_json = "1.0"
syslog = "4.0"
@@ -28,6 +29,7 @@
[build-dependencies]
pkg-config = "0.3.19"
+protoc-rust = "*"
[[bin]]
name = "btmanagerd"
diff --git a/system/gd/rust/linux/mgmt/build.rs b/system/gd/rust/linux/mgmt/build.rs
index ae291b0..1c273e5 100644
--- a/system/gd/rust/linux/mgmt/build.rs
+++ b/system/gd/rust/linux/mgmt/build.rs
@@ -1,4 +1,27 @@
+extern crate protoc_rust;
+
use pkg_config::Config;
+use std::env;
+use std::fs;
+use std::io::Write;
+use std::path::{Path, PathBuf};
+
+fn paths_to_strs<P: AsRef<Path>>(paths: &[P]) -> Vec<&str> {
+ paths.iter().map(|p| p.as_ref().as_os_str().to_str().unwrap()).collect()
+}
+
+// Generate mod.rs files for given input files.
+fn gen_mod_rs<P: AsRef<Path>>(out_dir: PathBuf, inputs: &[P]) {
+ // Will panic if file doesn't exist or it can't create it
+ let mut f = fs::File::create(out_dir.join("mod.rs")).unwrap();
+
+ f.write_all(b"// Generated by build.rs\n\n").unwrap();
+
+ for i in 0..inputs.len() {
+ let stem = inputs[i].as_ref().file_stem().unwrap();
+ f.write_all(format!("pub mod {}; \n", stem.to_str().unwrap()).as_bytes()).unwrap();
+ }
+}
fn main() {
let target_dir = std::env::var_os("CARGO_TARGET_DIR").unwrap();
@@ -9,4 +32,27 @@
// libdir and fixes the build issues.
Config::new().probe("dbus-1").unwrap();
println!("cargo:rerun-if-changed=build.rs");
+
+ let system_api_root = PathBuf::from(std::env::var_os("CROS_SYSTEM_API_ROOT").unwrap());
+
+ let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
+ let proto_out_dir = out_dir.join("proto_out");
+ let proto_input_files = [system_api_root.join("dbus/power_manager/suspend.proto")];
+ let proto_include_dirs = [system_api_root.clone()];
+
+ // Make sure to create the output directories before using it
+ match fs::create_dir(proto_out_dir.as_os_str().to_str().unwrap()) {
+ Err(e) => println!("Proto dir failed to be created: {}", e),
+ _ => (),
+ };
+
+ protoc_rust::Codegen::new()
+ .out_dir(proto_out_dir.as_os_str().to_str().unwrap())
+ .inputs(&paths_to_strs(&proto_input_files))
+ .includes(&paths_to_strs(&proto_include_dirs))
+ .customize(Default::default())
+ .run()
+ .expect("Failed to run protoc");
+
+ gen_mod_rs(proto_out_dir, &proto_input_files);
}
diff --git a/system/gd/rust/linux/mgmt/src/lib.rs b/system/gd/rust/linux/mgmt/src/lib.rs
index 35e2ca9..6a091f2 100644
--- a/system/gd/rust/linux/mgmt/src/lib.rs
+++ b/system/gd/rust/linux/mgmt/src/lib.rs
@@ -1 +1,5 @@
pub mod iface_bluetooth_manager;
+
+// protoc-rust generates all modules and exports them in mod.rs
+// We have to include them all here to make them available for crate export.
+include!(concat!(env!("OUT_DIR"), "/proto_out/mod.rs"));