Skip to content

Commit 45f5c9e

Browse files
committed
On Windows, try to infer HDF5 libdir from PATH
1 parent 3ff3684 commit 45f5c9e

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

appveyor.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,22 @@ matrix:
77
skip_tags: true
88
build: false
99

10-
init:
11-
- ps: iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))
12-
1310
environment:
1411
TARGET: x86_64-pc-windows-msvc
15-
HDF5_ROOT: "C:\\Program Files\\HDF_Group\\HDF5\\1.8.16"
12+
HDF5_BINDIR: "C:\\Program Files\\HDF_Group\\HDF5\\1.8.16\\bin"
1613
matrix:
1714
- CHANNEL: 1.8.0
1815
- CHANNEL: beta
1916
- CHANNEL: nightly
2017
install:
2118
- ps: Start-FileDownload "https://static.rust-lang.org/dist/rust-${env:CHANNEL}-${env:TARGET}.msi"
2219
- ps: Start-Process -FilePath msiexec -ArgumentList /i, rust-${env:CHANNEL}-${env:TARGET}.msi, INSTALLDIR="C:\Rust", /quiet -Wait
23-
- set PATH=%PATH%;C:\Rust\bin
2420
- rustc -vV
2521
- cargo -vV
2622
- ps: Invoke-WebRequest "http://www.hdfgroup.org/ftp/HDF5/current/bin/windows/extra/hdf5-1.8.16-win64-vs2015-shared.zip" -OutFile hdf5.zip
2723
- 7z x hdf5.zip -y
2824
- ps: Start-Process -FilePath msiexec -ArgumentList /i, "hdf5\HDF5-1.8.16-win64.msi", /quiet -Wait
29-
- set PATH=%PATH%;%HDF5_ROOT%\bin
30-
- set HDF5_LIBDIR=%HDF5_ROOT%\lib
25+
- set PATH=%PATH%;C:\Rust\bin;%HDF5_BINDIR%
3126

3227
test_script:
3328
- cd %APPVEYOR_BUILD_FOLDER%\libhdf5-lib

libhdf5-lib/build.rs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,63 @@
11
extern crate pkg_config;
22

33
use std::env;
4+
use std::fs;
5+
use std::path::PathBuf;
6+
7+
#[cfg(target_env = "msvc")]
8+
const IS_MSVC: bool = true;
9+
#[cfg(not(target_env = "msvc"))]
10+
const IS_MSVC: bool = false;
11+
12+
#[cfg(target_os = "windows")]
13+
const IS_WINDOWS: bool = true;
14+
#[cfg(not(target_os = "windows"))]
15+
const IS_WINDOWS: bool = false;
16+
17+
macro_rules! ok_or_continue {
18+
($r:expr) => ( match $r { Err(_) => continue, Ok(ok) => ok } )
19+
}
20+
21+
macro_rules! some_or_continue {
22+
($r:expr) => ( match $r { None => continue, Some(some) => some } )
23+
}
24+
25+
fn libdir_from_path() -> Option<String> {
26+
if !IS_WINDOWS || env::var("HDF5_LIBDIR").is_ok() {
27+
return None;
28+
}
29+
if let Ok(path) = env::var("PATH") {
30+
for path in path.split(";") {
31+
let dir = PathBuf::from(path);
32+
let dirname = some_or_continue!(dir.file_name());
33+
if dirname.to_str() != Some("bin") {
34+
continue;
35+
}
36+
let entries = ok_or_continue!(fs::read_dir(&dir));
37+
for entry in entries {
38+
let entry = ok_or_continue!(entry);
39+
let filename = entry.file_name();
40+
if filename.to_str() != Some("hdf5.dll") {
41+
continue;
42+
}
43+
let meta = ok_or_continue!(entry.metadata());
44+
if !meta.is_file() {
45+
continue;
46+
}
47+
if !IS_MSVC {
48+
return Some(path.into());
49+
}
50+
let parent = some_or_continue!(dir.parent());
51+
let libdir = parent.join("lib");
52+
if let Some(libdir) = libdir.to_str() {
53+
return Some(libdir.into());
54+
}
55+
}
56+
}
57+
}
58+
None
59+
}
60+
461

562
fn find_hdf5_libs() -> (Vec<String>, Vec<String>) {
663
let (mut libs, mut dirs) = (vec![], vec![]);
@@ -11,11 +68,14 @@ fn find_hdf5_libs() -> (Vec<String>, Vec<String>) {
1168
if let Ok(libdir) = env::var("HDF5_LIBDIR") {
1269
dirs.push(libdir);
1370
}
71+
if let Some(libdir) = libdir_from_path() {
72+
dirs.push(libdir);
73+
}
1474

1575
if let Ok(library) = pkg_config::Config::new().find("hdf5") {
1676
if dirs.is_empty() {
1777
for dir in library.link_paths.iter() {
18-
dirs.push(dir.to_str().unwrap().to_owned());
78+
dirs.push(dir.to_str().unwrap().into());
1979
}
2080
}
2181
if libs.is_empty() {
@@ -26,7 +86,7 @@ fn find_hdf5_libs() -> (Vec<String>, Vec<String>) {
2686
}
2787

2888
if libs.is_empty() {
29-
libs.push("hdf5".to_owned());
89+
libs.push("hdf5".into());
3090
}
3191

3292
(libs, dirs)

0 commit comments

Comments
 (0)