Skip to content

Commit 7dbcd06

Browse files
committed
Revise error handling in Library::seek_directory
1 parent 0c35a34 commit 7dbcd06

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

intel-mkl-tool/src/entry.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{mkl, Config, LinkType, VALID_CONFIGS};
2-
use anyhow::{bail, Result};
2+
use anyhow::{bail, ensure, Context, Result};
33
use derive_more::Deref;
44
use std::{
55
collections::{HashMap, HashSet},
@@ -50,8 +50,11 @@ impl Library {
5050
/// but do not follow symbolic links.
5151
/// - This will not seek directory named `ia32*`
5252
///
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>> {
5454
let root_dir = root_dir.as_ref();
55+
if !root_dir.is_dir() {
56+
return Ok(None);
57+
}
5558
let mut library_dir = None;
5659
let mut include_dir = None;
5760
let mut iomp5_dir = None;
@@ -62,19 +65,18 @@ impl Library {
6265
}
6366
let (stem, ext) = match (path.file_stem(), path.extension()) {
6467
(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")?,
6770
),
6871
_ => continue,
6972
};
7073
// Skip directory for ia32
7174
if path.components().any(|c| {
7275
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+
}
7880
}
7981
}
8082
false
@@ -98,14 +100,14 @@ impl Library {
98100
match (config.link, ext) {
99101
(LinkType::Static, mkl::EXTENSION_STATIC) => match name {
100102
"mkl_core" => {
101-
assert!(
103+
ensure!(
102104
library_dir.replace(dir).is_none(),
103105
"Two or more MKL found in {}",
104106
root_dir.display()
105107
)
106108
}
107109
"iomp5" => {
108-
assert!(
110+
ensure!(
109111
iomp5_dir.replace(dir).is_none(),
110112
"Two or more MKL found in {}",
111113
root_dir.display()
@@ -115,14 +117,14 @@ impl Library {
115117
},
116118
(LinkType::Dynamic, mkl::EXTENSION_SHARED) => match name {
117119
"mkl_rt" => {
118-
assert!(
120+
ensure!(
119121
library_dir.replace(dir).is_none(),
120122
"Two or more MKL found in {}",
121123
root_dir.display()
122124
)
123125
}
124126
"iomp5" => {
125-
assert!(
127+
ensure!(
126128
iomp5_dir.replace(dir).is_none(),
127129
"Two or more MKL found in {}",
128130
root_dir.display()
@@ -136,15 +138,15 @@ impl Library {
136138
if library_dir == iomp5_dir {
137139
iomp5_dir = None;
138140
}
139-
match (library_dir, include_dir) {
141+
Ok(match (library_dir, include_dir) {
140142
(Some(library_dir), Some(include_dir)) => Some(Library::Directory {
141143
config,
142144
include_dir,
143145
library_dir,
144146
iomp5_dir,
145147
}),
146148
_ => None,
147-
}
149+
})
148150
}
149151

150152
/// Seek MKL in system
@@ -162,13 +164,13 @@ impl Library {
162164
return Ok(lib);
163165
}
164166
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)? {
166168
return Ok(lib);
167169
}
168170
}
169171
for path in ["/opt/intel", "C:/Program Files (x86)/IntelSWTools/"] {
170172
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)? {
172174
return Ok(lib);
173175
}
174176
}

0 commit comments

Comments
 (0)