xref: /aosp_15_r20/system/nfc/tools/casimir/build.rs (revision 7eba2f3b06c51ae21384f6a4f14577b668a869b3)
1 // Copyright 2023, 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 //! Generate PDL backend for NCI and RF packets
16 
17 use std::env;
18 use std::fs::File;
19 use std::io::Write;
20 use std::path::{Path, PathBuf};
21 
main()22 fn main() {
23     install_generated_module(
24         "nci_packets.rs",
25         "NCI_PACKETS_PREBUILT",
26         &PathBuf::from("src/nci_packets.pdl").canonicalize().unwrap(),
27     );
28 
29     install_generated_module(
30         "rf_packets.rs",
31         "RF_PACKETS_PREBUILT",
32         &PathBuf::from("src/rf_packets.pdl").canonicalize().unwrap(),
33     );
34 
35     protoc_grpcio::compile_grpc_protos(&["casimir.proto"], &["src/proto"], &"src/proto", None)
36         .expect("gRPC generation failed");
37 }
38 
install_generated_module(module_name: &str, prebuilt_var: &str, pdl_name: &Path)39 fn install_generated_module(module_name: &str, prebuilt_var: &str, pdl_name: &Path) {
40     let module_prebuilt = match env::var(prebuilt_var) {
41         Ok(dir) => PathBuf::from(dir),
42         Err(_) => PathBuf::from(module_name),
43     };
44 
45     if Path::new(module_prebuilt.as_os_str()).exists() {
46         let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
47         std::fs::copy(
48             module_prebuilt.as_os_str().to_str().unwrap(),
49             out_dir.join(module_name).as_os_str().to_str().unwrap(),
50         )
51         .unwrap();
52     } else {
53         generate_module(pdl_name);
54     }
55 }
56 
generate_module(in_file: &Path)57 fn generate_module(in_file: &Path) {
58     let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
59     let mut out_file =
60         File::create(out_dir.join(in_file.file_name().unwrap()).with_extension("rs")).unwrap();
61 
62     println!("cargo:rerun-if-changed={}", in_file.display());
63 
64     let mut sources = pdl_compiler::ast::SourceDatabase::new();
65     let parsed_file = pdl_compiler::parser::parse_file(
66         &mut sources,
67         in_file.to_str().expect("Filename is not UTF-8"),
68     )
69     .expect("PDL parse failed");
70     let analyzed_file = pdl_compiler::analyzer::analyze(&parsed_file).expect("PDL analysis failed");
71     let rust_source = pdl_compiler::backends::rust_legacy::generate(&sources, &analyzed_file);
72     out_file.write_all(rust_source.as_bytes()).expect("Could not write to output file");
73 }
74