xref: /aosp_15_r20/system/nfc/tools/casimir/build.rs (revision 7eba2f3b06c51ae21384f6a4f14577b668a869b3)
1*7eba2f3bSAndroid Build Coastguard Worker // Copyright 2023, The Android Open Source Project
2*7eba2f3bSAndroid Build Coastguard Worker //
3*7eba2f3bSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*7eba2f3bSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*7eba2f3bSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*7eba2f3bSAndroid Build Coastguard Worker //
7*7eba2f3bSAndroid Build Coastguard Worker //     http://www.apache.org/licenses/LICENSE-2.0
8*7eba2f3bSAndroid Build Coastguard Worker //
9*7eba2f3bSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*7eba2f3bSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*7eba2f3bSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*7eba2f3bSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*7eba2f3bSAndroid Build Coastguard Worker // limitations under the License.
14*7eba2f3bSAndroid Build Coastguard Worker 
15*7eba2f3bSAndroid Build Coastguard Worker //! Generate PDL backend for NCI and RF packets
16*7eba2f3bSAndroid Build Coastguard Worker 
17*7eba2f3bSAndroid Build Coastguard Worker use std::env;
18*7eba2f3bSAndroid Build Coastguard Worker use std::fs::File;
19*7eba2f3bSAndroid Build Coastguard Worker use std::io::Write;
20*7eba2f3bSAndroid Build Coastguard Worker use std::path::{Path, PathBuf};
21*7eba2f3bSAndroid Build Coastguard Worker 
main()22*7eba2f3bSAndroid Build Coastguard Worker fn main() {
23*7eba2f3bSAndroid Build Coastguard Worker     install_generated_module(
24*7eba2f3bSAndroid Build Coastguard Worker         "nci_packets.rs",
25*7eba2f3bSAndroid Build Coastguard Worker         "NCI_PACKETS_PREBUILT",
26*7eba2f3bSAndroid Build Coastguard Worker         &PathBuf::from("src/nci_packets.pdl").canonicalize().unwrap(),
27*7eba2f3bSAndroid Build Coastguard Worker     );
28*7eba2f3bSAndroid Build Coastguard Worker 
29*7eba2f3bSAndroid Build Coastguard Worker     install_generated_module(
30*7eba2f3bSAndroid Build Coastguard Worker         "rf_packets.rs",
31*7eba2f3bSAndroid Build Coastguard Worker         "RF_PACKETS_PREBUILT",
32*7eba2f3bSAndroid Build Coastguard Worker         &PathBuf::from("src/rf_packets.pdl").canonicalize().unwrap(),
33*7eba2f3bSAndroid Build Coastguard Worker     );
34*7eba2f3bSAndroid Build Coastguard Worker 
35*7eba2f3bSAndroid Build Coastguard Worker     protoc_grpcio::compile_grpc_protos(&["casimir.proto"], &["src/proto"], &"src/proto", None)
36*7eba2f3bSAndroid Build Coastguard Worker         .expect("gRPC generation failed");
37*7eba2f3bSAndroid Build Coastguard Worker }
38*7eba2f3bSAndroid Build Coastguard Worker 
install_generated_module(module_name: &str, prebuilt_var: &str, pdl_name: &Path)39*7eba2f3bSAndroid Build Coastguard Worker fn install_generated_module(module_name: &str, prebuilt_var: &str, pdl_name: &Path) {
40*7eba2f3bSAndroid Build Coastguard Worker     let module_prebuilt = match env::var(prebuilt_var) {
41*7eba2f3bSAndroid Build Coastguard Worker         Ok(dir) => PathBuf::from(dir),
42*7eba2f3bSAndroid Build Coastguard Worker         Err(_) => PathBuf::from(module_name),
43*7eba2f3bSAndroid Build Coastguard Worker     };
44*7eba2f3bSAndroid Build Coastguard Worker 
45*7eba2f3bSAndroid Build Coastguard Worker     if Path::new(module_prebuilt.as_os_str()).exists() {
46*7eba2f3bSAndroid Build Coastguard Worker         let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
47*7eba2f3bSAndroid Build Coastguard Worker         std::fs::copy(
48*7eba2f3bSAndroid Build Coastguard Worker             module_prebuilt.as_os_str().to_str().unwrap(),
49*7eba2f3bSAndroid Build Coastguard Worker             out_dir.join(module_name).as_os_str().to_str().unwrap(),
50*7eba2f3bSAndroid Build Coastguard Worker         )
51*7eba2f3bSAndroid Build Coastguard Worker         .unwrap();
52*7eba2f3bSAndroid Build Coastguard Worker     } else {
53*7eba2f3bSAndroid Build Coastguard Worker         generate_module(pdl_name);
54*7eba2f3bSAndroid Build Coastguard Worker     }
55*7eba2f3bSAndroid Build Coastguard Worker }
56*7eba2f3bSAndroid Build Coastguard Worker 
generate_module(in_file: &Path)57*7eba2f3bSAndroid Build Coastguard Worker fn generate_module(in_file: &Path) {
58*7eba2f3bSAndroid Build Coastguard Worker     let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
59*7eba2f3bSAndroid Build Coastguard Worker     let mut out_file =
60*7eba2f3bSAndroid Build Coastguard Worker         File::create(out_dir.join(in_file.file_name().unwrap()).with_extension("rs")).unwrap();
61*7eba2f3bSAndroid Build Coastguard Worker 
62*7eba2f3bSAndroid Build Coastguard Worker     println!("cargo:rerun-if-changed={}", in_file.display());
63*7eba2f3bSAndroid Build Coastguard Worker 
64*7eba2f3bSAndroid Build Coastguard Worker     let mut sources = pdl_compiler::ast::SourceDatabase::new();
65*7eba2f3bSAndroid Build Coastguard Worker     let parsed_file = pdl_compiler::parser::parse_file(
66*7eba2f3bSAndroid Build Coastguard Worker         &mut sources,
67*7eba2f3bSAndroid Build Coastguard Worker         in_file.to_str().expect("Filename is not UTF-8"),
68*7eba2f3bSAndroid Build Coastguard Worker     )
69*7eba2f3bSAndroid Build Coastguard Worker     .expect("PDL parse failed");
70*7eba2f3bSAndroid Build Coastguard Worker     let analyzed_file = pdl_compiler::analyzer::analyze(&parsed_file).expect("PDL analysis failed");
71*7eba2f3bSAndroid Build Coastguard Worker     let rust_source = pdl_compiler::backends::rust_legacy::generate(&sources, &analyzed_file);
72*7eba2f3bSAndroid Build Coastguard Worker     out_file.write_all(rust_source.as_bytes()).expect("Could not write to output file");
73*7eba2f3bSAndroid Build Coastguard Worker }
74