From 307a31518fa44125d7d56d26048c904c5f5fda21 Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Fri, 25 Aug 2023 22:04:23 -0300 Subject: [PATCH] Fix package detection and build when cross-compiling from MSVC to GNU I and @dragonmux found two issues when building libusb from a MSVC host to a MinGW target. When the build.rs script is built for the MSVC host, - the vcpkg crate is used for detection (which correctly detects the MinGW target and reports an unsupported ABI) - a MSVC flag is applied to the build (and thus breaking the build altogether) This commit fixes both issues by changing vcpkg to a Windows-wide dependency, and then if CARGO_CFG_TARGET_ENV is indeed MSVC, it is used and the /source-charset:utf-8 flag is applied. --- libusb1-sys/Cargo.toml | 2 +- libusb1-sys/build.rs | 35 +++++++++++++++++------------------ 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/libusb1-sys/Cargo.toml b/libusb1-sys/Cargo.toml index 519accf..2612e78 100644 --- a/libusb1-sys/Cargo.toml +++ b/libusb1-sys/Cargo.toml @@ -39,7 +39,7 @@ vendored = [] [dependencies] libc = "0.2" -[target.'cfg(target_env = "msvc")'.build-dependencies] +[target.'cfg(target_os = "windows")'.build-dependencies] vcpkg = "0.2" [build-dependencies] diff --git a/libusb1-sys/build.rs b/libusb1-sys/build.rs index 0fcad31..e3b16d2 100644 --- a/libusb1-sys/build.rs +++ b/libusb1-sys/build.rs @@ -19,21 +19,6 @@ pub fn link_framework(name: &str) { println!("cargo:rustc-link-lib=framework={}", name); } -#[cfg(target_env = "msvc")] -fn find_libusb_pkg(_statik: bool) -> bool { - match vcpkg::Config::new().find_package("libusb") { - Ok(_) => true, - Err(e) => { - if pkg_config::probe_library("libusb-1.0").is_ok() { - true - } else { - println!("Can't find libusb pkg: {:?}", e); - false - } - } - } -} - fn get_macos_major_version() -> Option { if !cfg!(target_os = "macos") { return None; @@ -49,8 +34,21 @@ fn get_macos_major_version() -> Option { Some(major) } -#[cfg(not(target_env = "msvc"))] fn find_libusb_pkg(statik: bool) -> bool { + if std::env::var("CARGO_CFG_TARGET_ENV") == Ok("msvc".into()) { + #[cfg(target_os = "windows")] + return match vcpkg::Config::new().find_package("libusb") { + Ok(_) => true, + Err(e) => { + if pkg_config::probe_library("libusb-1.0").is_ok() { + true + } else { + println!("Can't find libusb pkg: {:?}", e); + false + } + } + }; + } // https://github.com/rust-lang/rust/issues/96943 let needs_rustc_issue_96943_workaround: bool = get_macos_major_version() .map(|major| major >= 11) @@ -175,8 +173,9 @@ fn make_source() { } if std::env::var("CARGO_CFG_TARGET_OS") == Ok("windows".into()) { - #[cfg(target_env = "msvc")] - base_config.flag("/source-charset:utf-8"); + if std::env::var("CARGO_CFG_TARGET_ENV") == Ok("msvc".into()) { + base_config.flag("/source-charset:utf-8"); + } base_config.warnings(false); base_config.define("OS_WINDOWS", Some("1"));