Skip to content

Commit

Permalink
Fix package detection and build when cross-compiling from MSVC to GNU
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
amyspark committed Aug 26, 2023
1 parent d1fa27c commit ef1a593
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion libusb1-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
27 changes: 13 additions & 14 deletions libusb1-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +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) => {
println!("Can't find libusb pkg: {:?}", e);
false
}
}
}

fn get_macos_major_version() -> Option<usize> {
if !cfg!(target_os = "macos") {
return None;
Expand All @@ -45,8 +34,17 @@ fn get_macos_major_version() -> Option<usize> {
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) => {
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)
Expand Down Expand Up @@ -171,8 +169,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"));
Expand Down

0 comments on commit ef1a593

Please sign in to comment.