xref: /aosp_15_r20/external/uwb/src/rust/uwb_uci_packets/build.rs (revision e0df40009cb5d71e642272d38ba1bb7ffccfce41)
1 // Copyright 2022, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 use std::path::Path;
16 use std::process::Command;
17 
main()18 fn main() {
19     let out_dir = std::env::var_os("OUT_DIR").unwrap();
20     let generated_file = "uci_packets.rs";
21     let dst_path = Path::new(&out_dir).join(generated_file);
22 
23     if Path::new(generated_file).exists() {
24         // Copy the rust code directly if the file exists.
25         let result = std::fs::copy(generated_file, &dst_path);
26         eprintln!("{} exists, copy to {:?}: {:?}", generated_file, dst_path, result);
27         return;
28     }
29 
30     // The binary should be compiled by `m pdlc` before calling cargo.
31     let output = Command::new("env")
32         .arg("pdlc")
33         .arg("--output-format")
34         .arg("rust")
35         .arg("uci_packets.pdl")
36         .output()
37         .unwrap();
38 
39     std::fs::write(&dst_path, &output.stdout)
40         .expect(&format!("Could not write {}", dst_path.display()));
41 
42     eprintln!(
43         "Status: {}, stdout: {}, stderr: {}",
44         output.status,
45         String::from_utf8_lossy(output.stdout.as_slice()),
46         String::from_utf8_lossy(output.stderr.as_slice())
47     );
48 }
49