• 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_rust;
18 
19 use protoc_rust::Customize;
20 use std::env;
21 use std::path::{Path, PathBuf};
22 
paths_to_strs<P: AsRef<Path>>(paths: &[P]) -> Vec<&str>23 fn paths_to_strs<P: AsRef<Path>>(paths: &[P]) -> Vec<&str> {
24     paths.iter().map(|p| p.as_ref().as_os_str().to_str().unwrap()).collect()
25 }
26 
main()27 fn main() {
28     // Proto root is //tools/netsim/src/proto
29     let proto_root = match env::var("PLATFORM_SUBDIR") {
30         Ok(dir) => PathBuf::from(dir).join("tools/netsim"),
31         // Currently at //tools/netsim/rust/backend-proto
32         Err(_) => PathBuf::from(env::current_dir().unwrap()).join("../..").canonicalize().unwrap(),
33     };
34 
35     let proto_out_dir = proto_root.join("rust/backend-proto/src");
36 
37     // Generate protobuf output
38     let proto_dir = proto_root.join("src/proto");
39     let proto_input_files = [
40         proto_dir.join("packet_streamer.proto"),
41         proto_dir.join("hci_packet.proto"),
42         proto_dir.join("startup.proto"),
43         proto_dir.join("common.proto"),
44     ];
45     let proto_include_dirs = [proto_dir.clone()];
46 
47     println!("cargo:warning=proto_outdir={:?}", proto_out_dir);
48 
49     protoc_rust::Codegen::new()
50         .out_dir(proto_out_dir.as_os_str().to_str().unwrap())
51         .inputs(&paths_to_strs(&proto_input_files))
52         .includes(&paths_to_strs(&proto_include_dirs))
53         .customize(Customize { serde_derive: Some(true), ..Default::default() })
54         .run()
55         .expect("protoc");
56 }
57