• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  Copyright 2021 Google, Inc.
3 //
4 //  Licensed under the Apache License, Version 2.0 (the "License");
5 //  you may not use this file except in compliance with the License.
6 //  You may obtain a copy of the License at:
7 //
8 //  http://www.apache.org/licenses/LICENSE-2.0
9 //
10 //  Unless required by applicable law or agreed to in writing, software
11 //  distributed under the License is distributed on an "AS IS" BASIS,
12 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 //  See the License for the specific language governing permissions and
14 //  limitations under the License.
15 
16 extern crate protoc_grpcio;
17 extern crate protoc_rust;
18 
19 use std::env;
20 use std::fs;
21 use std::io::Write;
22 use std::path::{Path, PathBuf};
23 
paths_to_strs<P: AsRef<Path>>(paths: &[P]) -> Vec<&str>24 fn paths_to_strs<P: AsRef<Path>>(paths: &[P]) -> Vec<&str> {
25     paths.iter().map(|p| p.as_ref().as_os_str().to_str().unwrap()).collect()
26 }
27 
28 // Generate mod.rs files for given input files.
gen_mod_rs<P: AsRef<Path>>(out_dir: PathBuf, inputs: &[P], grpc: bool)29 fn gen_mod_rs<P: AsRef<Path>>(out_dir: PathBuf, inputs: &[P], grpc: bool) {
30     // Will panic if file doesn't exist or it can't create it
31     let mut f = fs::File::create(out_dir.join("mod.rs")).unwrap();
32 
33     f.write_all(b"// Generated by build.rs\n\n").unwrap();
34 
35     for i in 0..inputs.len() {
36         let stem = inputs[i].as_ref().file_stem().unwrap();
37         f.write_all(format!("pub mod {}; \n", stem.to_str().unwrap()).as_bytes()).unwrap();
38         if grpc {
39             f.write_all(format!("pub mod {}_grpc;\n", stem.to_str().unwrap()).as_bytes()).unwrap();
40         }
41     }
42 }
43 
main()44 fn main() {
45     let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
46     let proto_out_dir = out_dir.join("proto_out");
47     let grpc_out_dir = out_dir.join("grpc_out");
48 
49     // Make sure to create the output directories before using it
50     match fs::create_dir(proto_out_dir.as_os_str().to_str().unwrap()) {
51         Err(e) => println!("Proto dir failed to be created: {}", e),
52         _ => (),
53     };
54 
55     match fs::create_dir(grpc_out_dir.as_os_str().to_str().unwrap()) {
56         Err(e) => println!("Grpc dir failed to be created: {}", e),
57         _ => (),
58     }
59 
60     // Proto root is //platform2/bt/system
61     let proto_root = match env::var("PLATFORM_SUBDIR") {
62         Ok(dir) => PathBuf::from(dir).join("bt/system"),
63         // Currently at //platform2/bt/system/gd/rust/facade_proto
64         Err(_) => {
65             PathBuf::from(env::current_dir().unwrap()).join("../../..").canonicalize().unwrap()
66         }
67     };
68 
69     //
70     // Generate protobuf output
71     //
72     let facade_dir = proto_root.join("blueberry/facade");
73     let proto_input_files = [facade_dir.join("common.proto")];
74     let proto_include_dirs = [facade_dir.clone()];
75 
76     protoc_rust::Codegen::new()
77         .out_dir(proto_out_dir.as_os_str().to_str().unwrap())
78         .inputs(&paths_to_strs(&proto_input_files))
79         .includes(&paths_to_strs(&proto_include_dirs))
80         .customize(Default::default())
81         .run()
82         .expect("protoc");
83 
84     //
85     // Generate grpc output
86     //
87     let grpc_proto_input_files = [
88         facade_dir.join("hci/hci_facade.proto"),
89         facade_dir.join("hci/controller_facade.proto"),
90         facade_dir.join("hal/hal_facade.proto"),
91         facade_dir.join("rootservice.proto"),
92     ];
93     let grpc_proto_include_dirs =
94         [facade_dir.join("hci"), facade_dir.join("hal"), facade_dir, proto_root];
95 
96     protoc_grpcio::compile_grpc_protos(
97         &grpc_proto_input_files,
98         &grpc_proto_include_dirs,
99         &grpc_out_dir,
100         None,
101     )
102     .expect("Failed to compile gRPC definitions");
103 
104     gen_mod_rs(proto_out_dir, &proto_input_files, false);
105     gen_mod_rs(grpc_out_dir, &grpc_proto_input_files, true);
106 }
107