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 use std::process::Command;
19
main()20 fn main() {
21 let packets_prebuilt = match env::var("HCI_PACKETS_PREBUILT") {
22 Ok(dir) => PathBuf::from(dir),
23 Err(_) => PathBuf::from("hci_packets.rs"),
24 };
25 if Path::new(packets_prebuilt.as_os_str()).exists() {
26 let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
27 let outputted = out_dir.join("../../hci/hci_packets.rs");
28 std::fs::copy(
29 packets_prebuilt.as_os_str().to_str().unwrap(),
30 out_dir.join(outputted.file_name().unwrap()).as_os_str().to_str().unwrap(),
31 )
32 .unwrap();
33 } else {
34 generate_packets();
35 }
36 }
37
generate_packets()38 fn generate_packets() {
39 let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
40
41 let gd_root = match env::var("PLATFORM_SUBDIR") {
42 Ok(dir) => PathBuf::from(dir).join("bt/gd"),
43 // Currently at //platform2/gd/rust/rust/packets
44 Err(_) => PathBuf::from(env::current_dir().unwrap()).join("../..").canonicalize().unwrap(),
45 };
46
47 let input_files = [gd_root.join("hci/hci_packets.pdl")];
48 let outputted = [out_dir.join("../../hci/hci_packets.rs")];
49
50 // Find the packetgen tool. Expecting it at CARGO_HOME/bin
51 let packetgen = match env::var("CARGO_HOME") {
52 Ok(dir) => PathBuf::from(dir).join("bin").join("bluetooth_packetgen"),
53 Err(_) => PathBuf::from("bluetooth_packetgen"),
54 };
55
56 if !Path::new(packetgen.as_os_str()).exists() {
57 panic!(
58 "Unable to locate bluetooth packet generator:{:?}",
59 packetgen.as_os_str().to_str().unwrap()
60 );
61 }
62
63 for i in 0..input_files.len() {
64 println!("cargo:rerun-if-changed={}", input_files[i].display());
65 let output = Command::new(packetgen.as_os_str().to_str().unwrap())
66 .arg("--source_root=".to_owned() + gd_root.as_os_str().to_str().unwrap())
67 .arg("--out=".to_owned() + out_dir.as_os_str().to_str().unwrap())
68 .arg("--include=bt/gd")
69 .arg("--rust")
70 .arg(input_files[i].as_os_str().to_str().unwrap())
71 .output()
72 .unwrap();
73
74 println!(
75 "Status: {}, stdout: {}, stderr: {}",
76 output.status,
77 String::from_utf8_lossy(output.stdout.as_slice()),
78 String::from_utf8_lossy(output.stderr.as_slice())
79 );
80
81 // File will be at ${OUT_DIR}/../../${input_files[i].strip('.pdl')}.rs
82 std::fs::rename(
83 outputted[i].as_os_str().to_str().unwrap(),
84 out_dir.join(outputted[i].file_name().unwrap()).as_os_str().to_str().unwrap(),
85 )
86 .unwrap();
87 }
88 }
89