1 // Copyright 2020 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
main()5 fn main() {
6     #[cfg(feature = "powerd")]
7     {
8         extern crate protoc_rust;
9 
10         use std::env;
11         use std::fmt::Write as FmtWrite;
12         use std::fs;
13         use std::io::Write;
14         use std::path::{Path, PathBuf};
15 
16         fn paths_to_strs<P: AsRef<Path>>(paths: &[P]) -> Vec<&str> {
17             paths
18                 .iter()
19                 .map(|p| p.as_ref().as_os_str().to_str().unwrap())
20                 .collect()
21         }
22 
23         let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
24         let proto_root = match env::var("SYSROOT") {
25             Ok(dir) => PathBuf::from(dir).join("usr/include/chromeos"),
26             // Make this work when typing "cargo build" in platform/crosvm/power_monitor
27             Err(_) => PathBuf::from("../../../platform2/system_api"),
28         };
29 
30         let power_manager_dir = proto_root.join("dbus/power_manager");
31         let input_files = [power_manager_dir.join("power_supply_properties.proto")];
32         let include_dirs = [power_manager_dir];
33 
34         protoc_rust::run(protoc_rust::Args {
35             out_dir: out_dir.as_os_str().to_str().unwrap(),
36             input: &paths_to_strs(&input_files),
37             includes: &paths_to_strs(&include_dirs),
38             customize: Default::default(),
39         })
40         .expect("protoc");
41 
42         let mut path_include_mods = String::new();
43         for input_file in input_files.iter() {
44             let stem = input_file.file_stem().unwrap().to_str().unwrap();
45             let mod_path = out_dir.join(format!("{}.rs", stem));
46             writeln!(
47                 &mut path_include_mods,
48                 "#[path = \"{}\"]",
49                 mod_path.display()
50             )
51             .unwrap();
52             writeln!(&mut path_include_mods, "pub mod {};", stem).unwrap();
53         }
54 
55         let mut mod_out = fs::File::create(out_dir.join("powerd_proto.rs")).unwrap();
56         writeln!(mod_out, "pub mod system_api {{\n{}}}", path_include_mods).unwrap();
57     }
58 }
59