• 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 protobuf_codegen;
17 extern crate protoc_grpcio;
18 extern crate protoc_rust;
19 
20 use std::env;
21 use std::fs;
22 use std::io::Write;
23 use std::path::{Path, PathBuf};
24 
paths_to_strs<P: AsRef<Path>>(paths: &[P]) -> Vec<&str>25 fn paths_to_strs<P: AsRef<Path>>(paths: &[P]) -> Vec<&str> {
26     paths.iter().map(|p| p.as_ref().as_os_str().to_str().unwrap()).collect()
27 }
28 
29 // Generate mod.rs files for given input files.
gen_mod_rs<P: AsRef<Path>>(out_dir: PathBuf, inputs: &[P], grpc: bool)30 fn gen_mod_rs<P: AsRef<Path>>(out_dir: PathBuf, inputs: &[P], grpc: bool) {
31     // Will panic if file doesn't exist or it can't create it
32     let mut f = fs::File::create(out_dir.join("mod.rs")).unwrap();
33 
34     f.write_all(b"// Generated by build.rs\n\n").unwrap();
35 
36     for i in 0..inputs.len() {
37         let stem = inputs[i].as_ref().file_stem().unwrap();
38         f.write_all(format!("pub mod {}; \n", stem.to_str().unwrap()).as_bytes()).unwrap();
39         if grpc {
40             f.write_all(format!("pub mod {}_grpc;\n", stem.to_str().unwrap()).as_bytes()).unwrap();
41         }
42     }
43 }
44 
main()45 fn main() {
46     let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
47     let proto_out_dir = out_dir.join("proto_out");
48     let grpc_out_dir = out_dir.join("grpc_out");
49 
50     // Make sure to create the output directories before using it
51     match fs::create_dir(proto_out_dir.as_os_str().to_str().unwrap()) {
52         Err(e) => println!("Proto dir failed to be created: {}", e),
53         _ => (),
54     };
55 
56     match fs::create_dir(grpc_out_dir.as_os_str().to_str().unwrap()) {
57         Err(e) => println!("Grpc dir failed to be created: {}", e),
58         _ => (),
59     }
60 
61     // Proto root is //platform2/bt/system
62     let proto_root = match env::var("PLATFORM_SUBDIR") {
63         Ok(dir) => PathBuf::from(dir).join("bt/system"),
64         // Currently at //platform2/bt/system/gd/rust/facade_proto
65         Err(_) => {
66             PathBuf::from(env::current_dir().unwrap()).join("../../..").canonicalize().unwrap()
67         }
68     };
69 
70     //
71     // Generate protobuf output
72     //
73     let facade_dir = proto_root.join("blueberry/facade");
74     let proto_input_files = [facade_dir.join("common.proto")];
75     let proto_include_dirs = [facade_dir.clone()];
76 
77     protoc_rust::Codegen::new()
78         .out_dir(proto_out_dir.as_os_str().to_str().unwrap())
79         .inputs(&paths_to_strs(&proto_input_files))
80         .includes(&paths_to_strs(&proto_include_dirs))
81         .customize(Default::default())
82         .run()
83         .expect("protoc");
84 
85     //
86     // Generate grpc output
87     //
88     let grpc_proto_input_files = [
89         facade_dir.join("hci/hci_facade.proto"),
90         facade_dir.join("hci/controller_facade.proto"),
91         facade_dir.join("hal/hal_facade.proto"),
92         facade_dir.join("rootservice.proto"),
93     ];
94     let grpc_proto_include_dirs =
95         [facade_dir.join("hci"), facade_dir.join("hal"), facade_dir, proto_root];
96 
97     protoc_grpcio::compile_grpc_protos(
98         &grpc_proto_input_files,
99         &grpc_proto_include_dirs,
100         &grpc_out_dir,
101         None,
102     )
103     .expect("Failed to compile gRPC definitions");
104 
105     gen_mod_rs(proto_out_dir, &proto_input_files, false);
106     gen_mod_rs(grpc_out_dir, &grpc_proto_input_files, true);
107 }
108