mirror of
https://github.com/imsnif/bandwhich.git
synced 2026-02-09 01:59:18 +08:00
Cache npcap SDK when building on Windows (#281)
* Cache npcap SDK on Windows * Call build function correctly * Log when local cache of SDK is found * Fix clippy warnings * Log to STDERR
This commit is contained in:
82
build.rs
82
build.rs
@@ -1,50 +1,78 @@
|
||||
fn main() {
|
||||
#[cfg(target_os = "windows")]
|
||||
download_windows_pcap_sdk()
|
||||
download_windows_npcap_sdk().unwrap();
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
fn download_windows_pcap_sdk() {
|
||||
fn download_windows_npcap_sdk() -> anyhow::Result<()> {
|
||||
use std::{
|
||||
env, fs,
|
||||
io::{self, Write},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
use anyhow::anyhow;
|
||||
use http_req::request;
|
||||
use zip::ZipArchive;
|
||||
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
|
||||
let out_dir = env::var("OUT_DIR").unwrap();
|
||||
// get npcap SDK
|
||||
const NPCAP_SDK: &str = "npcap-sdk-1.13.zip";
|
||||
|
||||
let mut pcap_zip = Vec::new();
|
||||
let res = request::get("https://npcap.com/dist/npcap-sdk-1.13.zip", &mut pcap_zip).unwrap();
|
||||
eprintln!("{res:?}");
|
||||
let npcap_sdk_download_url = format!("https://npcap.com/dist/{NPCAP_SDK}");
|
||||
let cache_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?).join("target");
|
||||
let npcap_sdk_cache_path = cache_dir.join(NPCAP_SDK);
|
||||
|
||||
let lib_dir = if cfg!(target_arch = "aarch64") {
|
||||
"Lib/ARM64"
|
||||
} else if cfg!(target_arch = "x86_64") {
|
||||
"Lib/x64"
|
||||
} else if cfg!(target_arch = "x86") {
|
||||
"Lib"
|
||||
} else {
|
||||
panic!("Unsupported target!")
|
||||
};
|
||||
let lib_name = "Packet.lib";
|
||||
let lib_path = format!("{lib_dir}/{lib_name}");
|
||||
let npcap_zip = match fs::read(&npcap_sdk_cache_path) {
|
||||
// use cached
|
||||
Ok(zip_data) => {
|
||||
eprintln!("Found cached npcap SDK");
|
||||
zip_data
|
||||
}
|
||||
// download SDK
|
||||
Err(_) => {
|
||||
eprintln!("Downloading npcap SDK");
|
||||
|
||||
let mut archive = ZipArchive::new(io::Cursor::new(pcap_zip)).unwrap();
|
||||
let mut pcap_lib = match archive.by_name(&lib_path) {
|
||||
Ok(lib) => lib,
|
||||
Err(err) => {
|
||||
panic!("{err}");
|
||||
// download
|
||||
let mut zip_data = vec![];
|
||||
let _res = request::get(npcap_sdk_download_url, &mut zip_data)?;
|
||||
|
||||
// write cache
|
||||
fs::create_dir_all(cache_dir)?;
|
||||
let mut cache = fs::File::create(npcap_sdk_cache_path)?;
|
||||
cache.write_all(&zip_data)?;
|
||||
|
||||
zip_data
|
||||
}
|
||||
};
|
||||
|
||||
fs::create_dir_all(format!("{out_dir}/{lib_dir}")).unwrap();
|
||||
let mut pcap_lib_file = fs::File::create(format!("{out_dir}/{lib_path}")).unwrap();
|
||||
io::copy(&mut pcap_lib, &mut pcap_lib_file).unwrap();
|
||||
pcap_lib_file.flush().unwrap();
|
||||
// extract DLL
|
||||
let lib_path = if cfg!(target_arch = "aarch64") {
|
||||
"Lib/ARM64/Packet.lib"
|
||||
} else if cfg!(target_arch = "x86_64") {
|
||||
"Lib/x64/Packet.lib"
|
||||
} else if cfg!(target_arch = "x86") {
|
||||
"Lib/Packet.lib"
|
||||
} else {
|
||||
panic!("Unsupported target!")
|
||||
};
|
||||
let mut archive = ZipArchive::new(io::Cursor::new(npcap_zip))?;
|
||||
let mut npcap_lib = archive.by_name(lib_path)?;
|
||||
|
||||
println!("cargo:rustc-link-search=native={out_dir}/{lib_dir}");
|
||||
// write DLL
|
||||
let lib_dir = PathBuf::from(env::var("OUT_DIR")?).join("npcap_sdk");
|
||||
let lib_path = lib_dir.join("Packet.lib");
|
||||
fs::create_dir_all(&lib_dir)?;
|
||||
let mut lib_file = fs::File::create(lib_path)?;
|
||||
io::copy(&mut npcap_lib, &mut lib_file)?;
|
||||
|
||||
println!(
|
||||
"cargo:rustc-link-search=native={}",
|
||||
lib_dir
|
||||
.to_str()
|
||||
.ok_or(anyhow!("{lib_dir:?} is not valid UTF-8"))?
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user