1 use pkg_config::Config;
2 use std::env;
3 use std::fs;
4 use std::io::Write;
5 use std::path::{Path, PathBuf};
6
paths_to_strs<P: AsRef<Path>>(paths: &[P]) -> Vec<&str>7 fn paths_to_strs<P: AsRef<Path>>(paths: &[P]) -> Vec<&str> {
8 paths.iter().map(|p| p.as_ref().as_os_str().to_str().unwrap()).collect()
9 }
10
11 // Generate mod.rs files for given input files.
gen_mod_rs<P: AsRef<Path>>(out_dir: PathBuf, inputs: &[P])12 fn gen_mod_rs<P: AsRef<Path>>(out_dir: PathBuf, inputs: &[P]) {
13 // Will panic if file doesn't exist or it can't create it
14 let mut f = fs::File::create(out_dir.join("mod.rs")).unwrap();
15
16 f.write_all(b"// Generated by build.rs\n\n").unwrap();
17
18 for i in 0..inputs.len() {
19 let stem = inputs[i].as_ref().file_stem().unwrap();
20 f.write_all(format!("pub mod {}; \n", stem.to_str().unwrap()).as_bytes()).unwrap();
21 }
22 }
23
main()24 fn main() {
25 let target_dir = std::env::var_os("CARGO_TARGET_DIR").unwrap();
26 println!("cargo:rustc-link-search=native={}", target_dir.into_string().unwrap());
27
28 // Clang requires -lc++ instead of -lstdc++
29 println!("cargo:rustc-link-lib=c++");
30
31 // When cross-compiling, pkg-config is looking for dbus at the host libdir instead of the
32 // sysroot. Adding this dependency here forces the linker to include the current sysroot's
33 // libdir and fixes the build issues.
34 Config::new().probe("dbus-1").unwrap();
35 println!("cargo:rerun-if-changed=build.rs");
36
37 let system_api_root = PathBuf::from(std::env::var_os("CROS_SYSTEM_API_ROOT").unwrap());
38
39 let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
40 let proto_out_dir = out_dir.join("proto_out");
41 let proto_input_files = [system_api_root.join("dbus/power_manager/suspend.proto")];
42 let proto_include_dirs = [system_api_root.clone()];
43
44 // Make sure to create the output directories before using it
45 match fs::create_dir(proto_out_dir.as_os_str().to_str().unwrap()) {
46 Err(e) => println!("Proto dir failed to be created: {}", e),
47 _ => (),
48 };
49
50 protoc_rust::Codegen::new()
51 .out_dir(proto_out_dir.as_os_str().to_str().unwrap())
52 .inputs(&paths_to_strs(&proto_input_files))
53 .includes(&paths_to_strs(&proto_include_dirs))
54 .customize(Default::default())
55 .run()
56 .expect("Failed to run protoc");
57
58 gen_mod_rs(proto_out_dir, &proto_input_files);
59 }
60