summaryrefslogtreecommitdiffstats
path: root/build.rs
blob: e844eebf29ee339b6e142a889186fd5e3cdf8d84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
fn main() {
    #[cfg(target_os = "windows")]
    download_windows_pcap_sdk()
}

#[cfg(target_os = "windows")]
fn download_windows_pcap_sdk() {
    use std::{
        env, fs,
        io::{self, Write},
    };

    use http_req::request;
    use zip::ZipArchive;

    println!("cargo:rerun-if-changed=build.rs");

    let out_dir = env::var("OUT_DIR").unwrap();

    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 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 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);
        }
    };

    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();

    println!("cargo:rustc-link-search=native={}/{}", out_dir, lib_dir);
}