1 //
2 // Copyright 2023 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-daemon` with dependencies.
17
18 use std::env;
19 use std::fs;
20 use std::path::PathBuf;
21
22 /// Adds all archive library dependencies from the specified `objs/archives` directory.
_all_archives_dependencies(objs_path: &str)23 fn _all_archives_dependencies(objs_path: &str) {
24 let archives_path = format!("{objs_path}/archives");
25 if let Ok(entry_lst) = fs::read_dir(&archives_path) {
26 println!("cargo:rustc-link-search=all={archives_path}");
27 for entry in entry_lst {
28 let entry = entry.unwrap();
29 let path = entry.path();
30 if path.is_file() {
31 if let Some(filename) = path.file_name() {
32 if let Some(filename_str) = filename.to_str() {
33 let lib_name = &filename_str[3..filename.len() - 2];
34 // "rootcanal.configuration.ControllerFeatures" conflicting symbols
35 if lib_name == "librootcanal_config" {
36 println!("cargo:warning=skip linking librootcanal_config to avoid conflicting symbols on rootcanal.configuration.ControllerFeatures");
37 continue;
38 }
39 println!("cargo:rustc-link-lib=static={lib_name}");
40 }
41 }
42 }
43 }
44 }
45 }
46 /// Configures linking for Linux test builds, including prebuilt PDL files and Rootcanal library.
_run_test_link()47 fn _run_test_link() {
48 // Linking libraries in objs/archives & objs/lib64
49 let objs_path = std::env::var("OBJS_PATH").unwrap_or("../objs".to_string());
50 println!("cargo:rustc-link-arg=-Wl,--allow-multiple-definition");
51 _all_archives_dependencies(&objs_path);
52 println!("cargo:rustc-link-lib=dylib=abseil_dll");
53
54 // Locate prebuilt pdl generated rust packet definition files
55 let prebuilts: [[&str; 2]; 5] = [
56 ["LINK_LAYER_PACKETS_PREBUILT", "link_layer_packets.rs"],
57 ["MAC80211_HWSIM_PACKETS_PREBUILT", "mac80211_hwsim_packets.rs"],
58 ["IEEE80211_PACKETS_PREBUILT", "ieee80211_packets.rs"],
59 ["LLC_PACKETS_PREBUILT", "llc_packets.rs"],
60 ["NETLINK_PACKETS_PREBUILT", "netlink_packets.rs"],
61 ];
62 let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
63 for [var, name] in prebuilts {
64 let env_prebuilt = env::var(var);
65 let out_file = out_dir.join(name);
66 // Check and use prebuilt pdl generated rust file from env var
67 if let Ok(prebuilt_path) = env_prebuilt {
68 println!("cargo:rerun-if-changed={}", prebuilt_path);
69 std::fs::copy(prebuilt_path.as_str(), out_file.as_os_str().to_str().unwrap()).unwrap();
70 // Prebuilt env var not set - check and use pdl generated file that is already present in out_dir
71 } else if out_file.exists() {
72 println!(
73 "cargo:warning=env var {} not set. Using prebuilt found at: {}",
74 var,
75 out_file.display()
76 );
77 } else {
78 panic!("Unable to find env var or prebuilt pdl generated rust file for: {}.", name);
79 };
80 }
81
82 // Linking Rootcanal Rust Library
83 if std::path::Path::new(&format!("{objs_path}/rootcanal/rust")).exists() {
84 println!("cargo:rustc-link-search=all={objs_path}/rootcanal/rust");
85 println!("cargo:rustc-link-lib=static=rootcanal_rs");
86 }
87 }
88
89 /// Configures C++ FFI bindings and Linux test linking.
main()90 fn main() {
91 // Linking for FFI
92 let _build = cxx_build::bridge("src/ffi.rs");
93 println!("cargo:rerun-if-changed=src/ffi.rs");
94
95 // TODO(379708365): Link libraries for Mac and Windows for integration test
96 #[cfg(target_os = "linux")]
97 _run_test_link()
98 }
99