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