From fbe9fbde7fe8fc06fd266d84554e66aeb72b0dec Mon Sep 17 00:00:00 2001 From: Yuyi Wang Date: Thu, 26 Jan 2023 20:30:57 +0800 Subject: [PATCH 01/10] Fix building with MSYS2-MinGW hdf5. --- hdf5-sys/build.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/hdf5-sys/build.rs b/hdf5-sys/build.rs index 13d73b18..4b537ce6 100644 --- a/hdf5-sys/build.rs +++ b/hdf5-sys/build.rs @@ -29,7 +29,7 @@ impl Version { } pub fn parse(s: &str) -> Option { - let re = Regex::new(r"^(1)\.(8|10|12|13)\.(\d\d?)(_\d+)?(-patch\d+)?$").ok()?; + let re = Regex::new(r"^(1)\.(8|10|12|13|14)\.(\d\d?)(_\d+)?(-patch\d+)?$").ok()?; let captures = re.captures(s)?; Some(Self { major: captures.get(1).and_then(|c| c.as_str().parse::().ok())?, @@ -73,6 +73,13 @@ fn is_root_dir>(path: P) -> bool { is_inc_dir(path.as_ref().join("include")) } +#[allow(dead_code)] +fn is_msvc() -> bool { + // `cfg!(target = "msvc")` will report wrong value when using + // MSVC toolchain targeting GNU. + std::env::var("CARGO_CFG_TARGET_ENV").unwrap() == "msvc" +} + #[derive(Clone, Debug)] struct RuntimeError(String); @@ -115,7 +122,7 @@ fn get_runtime_version_single>(path: P) -> Result Date: Fri, 27 Jan 2023 16:25:26 +0800 Subject: [PATCH 02/10] Add msys2-mingw test to CI. --- .github/workflows/ci.yml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 88a72e46..37d96ec6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -234,6 +234,33 @@ jobs: - name: Build and test all crates run: cargo test -vv + mingw: + name: mingw + runs-on: windows-latest + strategy: + fail-fast: false + matrix: + rust: [stable] + steps: + - name: Checkout repository + uses: actions/checkout@v2 + with: {submodules: true} + - name: Install Rust (${{matrix.rust}}) + uses: actions-rs/toolchain@v1 + with: {toolchain: '${{matrix.rust}}', profile: minimal, override: true} + - name: Install HDF5 + shell: pwsh + run: | + $env:PATH="$env:PATH;C:\msys64\mingw64\bin;C:\msys64\usr\bin;" + C:\msys64\usr\bin\pacman.exe -Syu --noconfirm + C:\msys64\usr\bin\pacman.exe -S --noconfirm mingw-w64-x86_64-hdf5 + - name: Build and test all crates + shell: pwsh + run: | + $env:HDF5_DIR="C:\msys64\mingw64" + $env:PATH="$env:PATH;C:\msys64\mingw64\bin;" + cargo test -vv + msrv: name: Minimal Supported Rust Version runs-on: ubuntu-18.04 From 98c80b421639f05cc004632df710730133e94335 Mon Sep 17 00:00:00 2001 From: Yuyi Wang Date: Fri, 27 Jan 2023 16:43:11 +0800 Subject: [PATCH 03/10] Update dll name. --- hdf5-sys/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hdf5-sys/build.rs b/hdf5-sys/build.rs index 4b537ce6..8a40be32 100644 --- a/hdf5-sys/build.rs +++ b/hdf5-sys/build.rs @@ -122,7 +122,7 @@ fn get_runtime_version_single>(path: P) -> Result Date: Fri, 27 Jan 2023 16:54:37 +0800 Subject: [PATCH 04/10] Specify GNU target in mingw test. --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 37d96ec6..ddc4a77e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -247,7 +247,7 @@ jobs: with: {submodules: true} - name: Install Rust (${{matrix.rust}}) uses: actions-rs/toolchain@v1 - with: {toolchain: '${{matrix.rust}}', profile: minimal, override: true} + with: {toolchain: '${{matrix.rust}}', target: x86_64-pc-windows-gnu, profile: minimal, override: true} - name: Install HDF5 shell: pwsh run: | @@ -259,7 +259,7 @@ jobs: run: | $env:HDF5_DIR="C:\msys64\mingw64" $env:PATH="$env:PATH;C:\msys64\mingw64\bin;" - cargo test -vv + cargo test -vv --target=x86_64-pc-windows-gnu msrv: name: Minimal Supported Rust Version From 70c51488617da71bf86e72ae2aa9659cf21af2b2 Mon Sep 17 00:00:00 2001 From: Yuyi Wang Date: Fri, 27 Jan 2023 18:09:38 +0800 Subject: [PATCH 05/10] Also use pkg-config on Windows. --- hdf5-sys/Cargo.toml | 2 +- hdf5-sys/build.rs | 35 +++++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/hdf5-sys/Cargo.toml b/hdf5-sys/Cargo.toml index a318eb70..32633799 100644 --- a/hdf5-sys/Cargo.toml +++ b/hdf5-sys/Cargo.toml @@ -33,7 +33,7 @@ deprecated = ["hdf5-src/deprecated"] libloading = "0.7" regex = { version = "1.3", features = ["std"] } -[target.'cfg(all(unix, not(target_os = "macos")))'.build-dependencies] +[target.'cfg(any(all(unix, not(target_os = "macos")), windows))'.build-dependencies] pkg-config = "0.3" [target.'cfg(windows)'.build-dependencies] diff --git a/hdf5-sys/build.rs b/hdf5-sys/build.rs index 8a40be32..ceaca6ed 100644 --- a/hdf5-sys/build.rs +++ b/hdf5-sys/build.rs @@ -75,7 +75,7 @@ fn is_root_dir>(path: P) -> bool { #[allow(dead_code)] fn is_msvc() -> bool { - // `cfg!(target = "msvc")` will report wrong value when using + // `cfg!(target_env = "msvc")` will report wrong value when using // MSVC toolchain targeting GNU. std::env::var("CARGO_CFG_TARGET_ENV").unwrap() == "msvc" } @@ -240,16 +240,24 @@ pub struct LibrarySearcher { pub inc_dir: Option, pub link_paths: Vec, pub user_provided_dir: bool, + pub pkg_conf_found: bool, } -#[cfg(all(unix, not(target_os = "macos")))] -mod unix { +#[cfg(any(all(unix, not(target_os = "macos")), windows))] +mod pkgconf { use super::{is_inc_dir, LibrarySearcher}; pub fn find_hdf5_via_pkg_config(config: &mut LibrarySearcher) { if config.inc_dir.is_some() { return; } + + // If we're going to windows-gnu we can use pkg-config, but only so long as + // we're coming from a windows host. + if cfg!(windows) { + std::env::set_var("PKG_CONFIG_ALLOW_CROSS", "1"); + } + // Try pkg-config. Note that HDF5 only ships pkg-config metadata // in CMake builds (which is not what homebrew uses, for example). // Still, this would work sometimes on Linux. @@ -279,8 +287,16 @@ mod unix { } else { println!("Unable to locate HDF5 headers from pkg-config info."); } + + config.pkg_conf_found = true; } } +} + +#[cfg(all(unix, not(target_os = "macos")))] +mod unix { + pub use super::pkgconf::find_hdf5_via_pkg_config; + use super::{is_inc_dir, LibrarySearcher}; pub fn find_hdf5_in_default_location(config: &mut LibrarySearcher) { if config.inc_dir.is_some() { @@ -370,6 +386,7 @@ mod macos { #[cfg(windows)] mod windows { + pub use super::pkgconf::find_hdf5_via_pkg_config; use super::*; use std::io; @@ -473,9 +490,9 @@ mod windows { pub fn validate_env_path(config: &LibrarySearcher) { if let Some(ref inc_dir) = config.inc_dir { let var_path = env::var("PATH").unwrap_or_else(|_| Default::default()); - let bin_dir = inc_dir.parent().unwrap().join("bin"); + let bin_dir = inc_dir.parent().unwrap().join("bin").canonicalize().unwrap(); for path in env::split_paths(&var_path) { - if path == bin_dir { + if path.canonicalize().unwrap() == bin_dir { println!("Found in PATH: {:?}", path); return; } @@ -538,6 +555,7 @@ impl LibrarySearcher { #[cfg(windows)] { self::windows::find_hdf5_via_winreg(self); + self::windows::find_hdf5_via_pkg_config(self); // the check below is for dynamic linking only self::windows::validate_env_path(self); } @@ -575,7 +593,12 @@ impl LibrarySearcher { assert_eq!(header.version, version, "HDF5 header version mismatch",); } let config = Config { inc_dir: inc_dir.clone(), link_paths, header }; - validate_runtime_version(&config); + // Don't check version if pkg-config finds the library and this is a windows target. + // We trust the pkg-config provided path, to avoid updating dll names every time + // the package updates. + if !(self.pkg_conf_found && cfg!(windows)) { + validate_runtime_version(&config); + } config } else { panic!("Unable to determine HDF5 location (set HDF5_DIR to specify it manually)."); From 372c9bfcb4cf95c0eff4167ad3154e82dd6a66df Mon Sep 17 00:00:00 2001 From: Yuyi Wang Date: Fri, 27 Jan 2023 18:10:08 +0800 Subject: [PATCH 06/10] Update CI not setting HDF5_DIR. --- .github/workflows/ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ddc4a77e..c14c2ce7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -257,7 +257,6 @@ jobs: - name: Build and test all crates shell: pwsh run: | - $env:HDF5_DIR="C:\msys64\mingw64" $env:PATH="$env:PATH;C:\msys64\mingw64\bin;" cargo test -vv --target=x86_64-pc-windows-gnu From ddfa31e7f703251ff8a1c93e378100714b828b57 Mon Sep 17 00:00:00 2001 From: Yuyi Wang Date: Fri, 27 Jan 2023 23:26:48 +0800 Subject: [PATCH 07/10] Install pkgconf in mingw CI. --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c14c2ce7..d73ba474 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -253,7 +253,7 @@ jobs: run: | $env:PATH="$env:PATH;C:\msys64\mingw64\bin;C:\msys64\usr\bin;" C:\msys64\usr\bin\pacman.exe -Syu --noconfirm - C:\msys64\usr\bin\pacman.exe -S --noconfirm mingw-w64-x86_64-hdf5 + C:\msys64\usr\bin\pacman.exe -S --noconfirm mingw-w64-x86_64-hdf5 mingw-w64-x86_64-pkgconf - name: Build and test all crates shell: pwsh run: | From 542c6883f956af9db19ee4ef6f89e717964b620c Mon Sep 17 00:00:00 2001 From: Yuyi Wang Date: Sat, 28 Jan 2023 10:28:10 +0800 Subject: [PATCH 08/10] Fix canonicalize to avoid non-exist path. --- hdf5-sys/build.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/hdf5-sys/build.rs b/hdf5-sys/build.rs index ceaca6ed..f014d93e 100644 --- a/hdf5-sys/build.rs +++ b/hdf5-sys/build.rs @@ -492,9 +492,11 @@ mod windows { let var_path = env::var("PATH").unwrap_or_else(|_| Default::default()); let bin_dir = inc_dir.parent().unwrap().join("bin").canonicalize().unwrap(); for path in env::split_paths(&var_path) { - if path.canonicalize().unwrap() == bin_dir { - println!("Found in PATH: {:?}", path); - return; + if let Ok(path) = path.canonicalize() { + if path == bin_dir { + println!("Found in PATH: {:?}", path); + return; + } } } panic!("{:?} not found in PATH.", bin_dir); From a7b649e9ec0319a9ad6e2ee7b2a392000e04cb78 Mon Sep 17 00:00:00 2001 From: Yuyi Wang Date: Tue, 7 Feb 2023 15:57:32 +0800 Subject: [PATCH 09/10] Add to changelog. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cecdb32a..58b1a20a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - Fixed a missing symbol when building `hdf5-src` with `libz-sys`. - Fixed a bug where errors were only silenced on the main thread. - Fixed a memory leak when opening datasets. +- Fixed library finding on Windows with MSYS2-distributed MinGW HDF5. ## 0.8.1 From 18ccfdc343eb8b7073fcd6ed09bfe9ad91e27a05 Mon Sep 17 00:00:00 2001 From: Yuyi Wang Date: Sat, 10 Jun 2023 19:22:50 +0800 Subject: [PATCH 10/10] Try to parse both versions on arch & mingw. --- hdf5-sys/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hdf5-sys/build.rs b/hdf5-sys/build.rs index 1bf66d7a..880725b8 100644 --- a/hdf5-sys/build.rs +++ b/hdf5-sys/build.rs @@ -29,7 +29,7 @@ impl Version { } pub fn parse(s: &str) -> Option { - let re = Regex::new(r"^(1)\.(8|10|12|13|14)\.(\d\d?)(_\d+)?(-(patch)?\d+)?$").ok()?; + let re = Regex::new(r"^(1)\.(8|10|12|13|14)\.(\d\d?)(_\d+)?((-|.)(patch)?\d+)?$").ok()?; let captures = re.captures(s)?; Some(Self { major: captures.get(1).and_then(|c| c.as_str().parse::().ok())?,