• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  Copyright 2022 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     install_generated_module(
22         "lmp_packets.rs",
23         "LMP_PACKETS_PREBUILT",
24         &PathBuf::from("lmp_packets.pdl").canonicalize().unwrap(),
25     );
26     install_generated_module(
27         "hci_packets.rs",
28         "HCI_PACKETS_PREBUILT",
29         &PathBuf::from("../packets/hci/hci_packets.pdl").canonicalize().unwrap(),
30     );
31 }
32 
install_generated_module(module_name: &str, prebuilt_var: &str, pdl_name: &PathBuf)33 fn install_generated_module(module_name: &str, prebuilt_var: &str, pdl_name: &PathBuf) {
34     let module_prebuilt = match env::var(prebuilt_var) {
35         Ok(dir) => PathBuf::from(dir),
36         Err(_) => PathBuf::from(module_name),
37     };
38 
39     if Path::new(module_prebuilt.as_os_str()).exists() {
40         let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
41         std::fs::copy(
42             module_prebuilt.as_os_str().to_str().unwrap(),
43             out_dir.join(module_name).as_os_str().to_str().unwrap(),
44         )
45         .unwrap();
46     } else {
47         generate_module(pdl_name);
48     }
49 }
50 
generate_module(pdl_name: &PathBuf)51 fn generate_module(pdl_name: &PathBuf) {
52     let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
53 
54     // Find the packetgen tool. Expecting it at CARGO_HOME/bin
55     let packetgen = match env::var("CARGO_HOME") {
56         Ok(dir) => PathBuf::from(dir).join("bin").join("bluetooth_packetgen"),
57         Err(_) => PathBuf::from("bluetooth_packetgen"),
58     };
59 
60     if !Path::new(packetgen.as_os_str()).exists() {
61         panic!(
62             "bluetooth_packetgen not found in the current environment: {:?}",
63             packetgen.as_os_str().to_str().unwrap()
64         );
65     }
66 
67     println!("cargo:rerun-if-changed={}", pdl_name.display());
68     let output = Command::new(packetgen.as_os_str().to_str().unwrap())
69         .arg("--out=".to_owned() + out_dir.as_os_str().to_str().unwrap())
70         .arg("--include=".to_owned() + pdl_name.parent().unwrap().as_os_str().to_str().unwrap())
71         .arg("--rust")
72         .arg(pdl_name)
73         .output()
74         .unwrap();
75 
76     println!(
77         "Status: {}, stdout: {}, stderr: {}",
78         output.status,
79         String::from_utf8_lossy(output.stdout.as_slice()),
80         String::from_utf8_lossy(output.stderr.as_slice())
81     );
82 
83     assert!(output.status.success());
84 }
85