1
1
use crate :: { mkl, Config , LinkType , VALID_CONFIGS } ;
2
- use anyhow:: { bail, Result } ;
2
+ use anyhow:: { bail, ensure , Context , Result } ;
3
3
use derive_more:: Deref ;
4
4
use std:: {
5
5
collections:: { HashMap , HashSet } ,
@@ -50,8 +50,11 @@ impl Library {
50
50
/// but do not follow symbolic links.
51
51
/// - This will not seek directory named `ia32*`
52
52
///
53
- pub fn seek_directory ( config : Config , root_dir : impl AsRef < Path > ) -> Option < Self > {
53
+ pub fn seek_directory ( config : Config , root_dir : impl AsRef < Path > ) -> Result < Option < Self > > {
54
54
let root_dir = root_dir. as_ref ( ) ;
55
+ if !root_dir. is_dir ( ) {
56
+ return Ok ( None ) ;
57
+ }
55
58
let mut library_dir = None ;
56
59
let mut include_dir = None ;
57
60
let mut iomp5_dir = None ;
@@ -62,19 +65,18 @@ impl Library {
62
65
}
63
66
let ( stem, ext) = match ( path. file_stem ( ) , path. extension ( ) ) {
64
67
( Some ( stem) , Some ( ext) ) => (
65
- stem. to_str ( ) . expect ( "Non UTF8 filename" ) ,
66
- ext. to_str ( ) . expect ( "Non UTF8 filename" ) ,
68
+ stem. to_str ( ) . context ( "Non UTF8 filename" ) ? ,
69
+ ext. to_str ( ) . context ( "Non UTF8 filename" ) ? ,
67
70
) ,
68
71
_ => continue ,
69
72
} ;
70
73
// Skip directory for ia32
71
74
if path. components ( ) . any ( |c| {
72
75
if let std:: path:: Component :: Normal ( c) = c {
73
- if c. to_str ( )
74
- . expect ( "Non-UTF8 directory name" )
75
- . starts_with ( "ia32" )
76
- {
77
- return true ;
76
+ if let Some ( c) = c. to_str ( ) {
77
+ if c. starts_with ( "ia32" ) {
78
+ return true ;
79
+ }
78
80
}
79
81
}
80
82
false
@@ -98,14 +100,14 @@ impl Library {
98
100
match ( config. link , ext) {
99
101
( LinkType :: Static , mkl:: EXTENSION_STATIC ) => match name {
100
102
"mkl_core" => {
101
- assert ! (
103
+ ensure ! (
102
104
library_dir. replace( dir) . is_none( ) ,
103
105
"Two or more MKL found in {}" ,
104
106
root_dir. display( )
105
107
)
106
108
}
107
109
"iomp5" => {
108
- assert ! (
110
+ ensure ! (
109
111
iomp5_dir. replace( dir) . is_none( ) ,
110
112
"Two or more MKL found in {}" ,
111
113
root_dir. display( )
@@ -115,14 +117,14 @@ impl Library {
115
117
} ,
116
118
( LinkType :: Dynamic , mkl:: EXTENSION_SHARED ) => match name {
117
119
"mkl_rt" => {
118
- assert ! (
120
+ ensure ! (
119
121
library_dir. replace( dir) . is_none( ) ,
120
122
"Two or more MKL found in {}" ,
121
123
root_dir. display( )
122
124
)
123
125
}
124
126
"iomp5" => {
125
- assert ! (
127
+ ensure ! (
126
128
iomp5_dir. replace( dir) . is_none( ) ,
127
129
"Two or more MKL found in {}" ,
128
130
root_dir. display( )
@@ -136,15 +138,15 @@ impl Library {
136
138
if library_dir == iomp5_dir {
137
139
iomp5_dir = None ;
138
140
}
139
- match ( library_dir, include_dir) {
141
+ Ok ( match ( library_dir, include_dir) {
140
142
( Some ( library_dir) , Some ( include_dir) ) => Some ( Library :: Directory {
141
143
config,
142
144
include_dir,
143
145
library_dir,
144
146
iomp5_dir,
145
147
} ) ,
146
148
_ => None ,
147
- }
149
+ } )
148
150
}
149
151
150
152
/// Seek MKL in system
@@ -162,13 +164,13 @@ impl Library {
162
164
return Ok ( lib) ;
163
165
}
164
166
if let Ok ( mklroot) = std:: env:: var ( "MKLROOT" ) {
165
- if let Some ( lib) = Self :: seek_directory ( config, mklroot) {
167
+ if let Some ( lib) = Self :: seek_directory ( config, mklroot) ? {
166
168
return Ok ( lib) ;
167
169
}
168
170
}
169
171
for path in [ "/opt/intel" , "C:/Program Files (x86)/IntelSWTools/" ] {
170
172
let path = Path :: new ( path) ;
171
- if let Some ( lib) = Self :: seek_directory ( config, path) {
173
+ if let Some ( lib) = Self :: seek_directory ( config, path) ? {
172
174
return Ok ( lib) ;
173
175
}
174
176
}
0 commit comments