From 9b3ddd2086a9a8574b99e563705970bc0edf9476 Mon Sep 17 00:00:00 2001 From: ttyS3 Date: Sat, 21 Jan 2023 01:00:58 +0800 Subject: [PATCH] build: fix cross compile for windows under Linux host machine Refs: https://kazlauskas.me/entries/writing-proper-buildrs-scripts --- Cargo.toml | 2 +- build.rs | 28 ++++++++++++++++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 386ed0673..eda12b287 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/build.rs b/build.rs index b4d66f0aa..dab14deb9 100644 --- a/build.rs +++ b/build.rs @@ -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::*; @@ -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(); @@ -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;