Skip to content

Commit

Permalink
build: fix cross compile for windows under Linux host machine
Browse files Browse the repository at this point in the history
  • Loading branch information
ttys3 committed Jan 20, 2023
1 parent e5f78e4 commit 9b3ddd2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pnet_base = "0.31.0"
packet-builder = "0.6.0"
regex = "1"

[target.'cfg(target_os="windows")'.build-dependencies]
[build-dependencies]
http_req = "0.9.0"
zip = "0.6.3"

Expand Down
28 changes: 18 additions & 10 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use std::env;

fn main() {
#[cfg(target_os = "windows")]
download_winpcap_sdk();
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
if target_os.as_str() == "windows" {
download_winpcap_sdk();
}

#[cfg(target_os = "windows")]
#[cfg(windows)]
download_winpcap_dll();
}

#[cfg(target_os = "windows")]
fn download_winpcap_sdk() {
use http_req::request;
use std::env;
use std::fs::File;
use std::io::prelude::*;

Expand All @@ -30,10 +32,16 @@ fn download_winpcap_sdk() {
pcapzip = File::open(format!("{}{}", out_dir, "/npcap.zip")).unwrap();

let lib_name = "Packet.lib";
#[cfg(target_arch = "x86_64")]
let lib_dir = "Lib/x64";
#[cfg(target_arch = "x86")]
let lib_dir = "Lib";

let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let lib_dir = match target_arch.as_str() {
"x86_64" => {
"Lib/x64"
}
_ => {
"Lib"
}
};

let lib_path = format!("{}/{}", lib_dir, lib_name);
let mut zip_archive = zip::ZipArchive::new(pcapzip).unwrap();
Expand All @@ -55,7 +63,7 @@ fn download_winpcap_sdk() {
println!("cargo:rustc-link-search=native={}/{}", out_dir, lib_dir);
}

#[cfg(target_os = "windows")]
#[cfg(windows)]
fn download_winpcap_dll() {
use http_req::request;
use std::fs::File;
Expand Down

0 comments on commit 9b3ddd2

Please sign in to comment.