xref: /aosp_15_r20/tools/netsim/rust/packets/build.rs (revision cf78ab8cffb8fc9207af348f23af247fb04370a6)
1 //
2 //  Copyright 2024 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::PathBuf;
18 
main()19 fn main() {
20     // Locate prebuilt pdl generated rust packet definition files
21     let prebuilts: [[&str; 2]; 5] = [
22         ["LINK_LAYER_PACKETS_PREBUILT", "link_layer_packets.rs"],
23         ["MAC80211_HWSIM_PACKETS_PREBUILT", "mac80211_hwsim_packets.rs"],
24         ["IEEE80211_PACKETS_PREBUILT", "ieee80211_packets.rs"],
25         ["LLC_PACKETS_PREBUILT", "llc_packets.rs"],
26         ["NETLINK_PACKETS_PREBUILT", "netlink_packets.rs"],
27     ];
28     let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
29     for [var, name] in prebuilts {
30         let env_prebuilt = env::var(var);
31         let out_file = out_dir.join(name);
32         // Check and use prebuilt pdl generated rust file from env var
33         if let Ok(prebuilt_path) = env_prebuilt {
34             println!("cargo:rerun-if-changed={}", prebuilt_path);
35             std::fs::copy(prebuilt_path.as_str(), out_file.as_os_str().to_str().unwrap()).unwrap();
36         // Prebuilt env var not set - check and use pdl generated file that is already present in out_dir
37         } else if out_file.exists() {
38             println!(
39                 "cargo:warning=env var {} not set. Using prebuilt found at: {}",
40                 var,
41                 out_file.display()
42             );
43         } else {
44             panic!("Unable to find env var or prebuilt pdl generated rust file for: {}.", name);
45         };
46     }
47 }
48