crosvm/power_monitor/build.rs

64 lines
2.2 KiB
Rust
Raw Normal View History

// Copyright 2020 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
fn main() {
#[cfg(feature = "powerd")]
{
extern crate protoc_rust;
use std::env;
use std::fmt::Write as FmtWrite;
use std::fs;
use std::io::Write;
use std::path::Path;
use std::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()
}
Reland "github: Minimize dependencies for document generation" This is a reland of commit 2b85d4d1e521ef66f165bca21741b52607acdb22 Diff from the original CL: * Changed the return value of `main()` in power_monitor/build.rs * Ran cargo-check with all features enabled Original change's description: > github: Minimize dependencies for document generation > > Instead of install full dependencies with `install-deps` in GitHub > action, minimize dependencies because we use GitHub only for document > generation. > > - Passed `CARGO_DOC` environment when running cargo-doc command so we > can skip unnecessary build flow when we just want to generate API docs. > - Added a new script `install-docs-deps` to install only doc-related > dependencies. > > BUG=none > TEST=test on GitHub Action at my personal repository > > Change-Id: Ibe988ab43215e285d946812bdd6c1536ae87b50e > Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3578144 > Tested-by: kokoro <noreply+kokoro@google.com> > Reviewed-by: Daniel Verkamp <dverkamp@chromium.org> > Reviewed-by: Anton Romanov <romanton@google.com> > Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org> Bug=none TEST=cargo check --all-features Change-Id: I77ee6543910e3fe7f69be48f8f965eda3433d4e6 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3584063 Auto-Submit: Keiichi Watanabe <keiichiw@chromium.org> Reviewed-by: Dennis Kempin <denniskempin@google.com> Tested-by: kokoro <noreply+kokoro@google.com> Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org>
2022-04-08 10:51:32 +00:00
// Skip building dependencies when generating documents.
if std::env::var("CARGO_DOC").is_ok() {
return;
}
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let proto_root = match env::var("SYSROOT") {
Ok(dir) => PathBuf::from(dir).join("usr/include/chromeos"),
// Make this work when typing "cargo build" in platform/crosvm/power_monitor
Err(_) => PathBuf::from("../../../platform2/system_api"),
};
let power_manager_dir = proto_root.join("dbus/power_manager");
let input_files = [power_manager_dir.join("power_supply_properties.proto")];
let include_dirs = [power_manager_dir];
protoc_rust::Codegen::new()
.inputs(&paths_to_strs(&input_files))
.includes(&paths_to_strs(&include_dirs))
.out_dir(out_dir.as_os_str().to_str().unwrap())
.run()
.expect("protoc");
let mut path_include_mods = String::new();
for input_file in input_files.iter() {
let stem = input_file.file_stem().unwrap().to_str().unwrap();
let mod_path = out_dir.join(format!("{}.rs", stem));
writeln!(
&mut path_include_mods,
"#[path = \"{}\"]",
mod_path.display()
)
.unwrap();
writeln!(&mut path_include_mods, "pub mod {};", stem).unwrap();
}
let mut mod_out = fs::File::create(out_dir.join("powerd_proto.rs")).unwrap();
writeln!(mod_out, "pub mod system_api {{\n{}}}", path_include_mods).unwrap();
}
}