11extern crate pkg_config;
22
33use 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
562fn 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