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