build_probe_mpi/os/
unix.rs1#![deny(missing_docs)]
2#![warn(missing_copy_implementations)]
3#![warn(trivial_casts)]
4#![warn(trivial_numeric_casts)]
5#![warn(unused_extern_crates)]
6#![warn(unused_import_braces)]
7#![warn(unused_qualifications)]
8
9use core::fmt;
10use std::{env, error::Error, path::PathBuf, process::Command};
11
12use pkg_config::Config;
13
14use super::super::Library;
15
16#[derive(Debug, PartialEq)]
17struct UnquoteError {
18 quote: char,
19}
20
21impl UnquoteError {
22 fn new(quote: char) -> UnquoteError {
23 UnquoteError { quote }
24 }
25}
26impl Error for UnquoteError {}
27
28impl fmt::Display for UnquoteError {
29 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30 write!(f, "Quotes '{}' not closed.", self.quote)
31 }
32}
33
34fn unquote(s: &str) -> Result<String, UnquoteError> {
35 if s.chars().count() < 2 {
36 return Ok(String::from(s));
37 }
38
39 let quote = s.chars().next().unwrap();
40
41 if quote != '"' && quote != '\'' && quote != '`' {
42 return Ok(String::from(s));
43 }
44
45 if s.chars().last().unwrap() != quote {
46 return Err(UnquoteError::new(quote));
47 }
48
49 let s = &s[1..s.len() - 1];
50 Ok(String::from(s))
51}
52
53impl From<pkg_config::Library> for Library {
54 fn from(lib: pkg_config::Library) -> Self {
55 Library {
56 mpicc: None,
57 libs: lib.libs,
58 lib_paths: lib.link_paths,
59 include_paths: lib.include_paths,
60 version: lib.version,
61 _priv: (),
62 }
63 }
64}
65
66fn probe_via_mpicc(mpicc: &str) -> std::io::Result<Library> {
67 Command::new(mpicc).arg("-show").output().map(|cmd| {
70 let output = String::from_utf8(cmd.stdout).expect("mpicc output is not valid UTF-8");
71 let libs = collect_args_with_prefix(output.as_ref(), "-l");
73 let libdirs = collect_args_with_prefix(output.as_ref(), "-L")
75 .into_iter()
76 .filter_map(|x| unquote(&x).ok())
77 .map(PathBuf::from)
78 .collect();
79 let headerdirs = collect_args_with_prefix(output.as_ref(), "-I")
81 .into_iter()
82 .filter_map(|x| unquote(&x).ok())
83 .map(PathBuf::from)
84 .collect();
85
86 Library {
87 mpicc: Some(mpicc.to_string()),
88 libs,
89 lib_paths: libdirs,
90 include_paths: headerdirs,
91 version: String::from("unknown"),
92 _priv: (),
93 }
94 })
95}
96
97fn collect_args_with_prefix(cmd: &str, prefix: &str) -> Vec<String> {
99 shell_words::split(cmd)
100 .unwrap()
101 .iter()
102 .filter_map(|arg| {
103 if arg.starts_with(prefix) {
104 Some(arg[2..].to_owned())
105 } else {
106 None
107 }
108 })
109 .collect()
110}
111
112pub fn probe() -> Result<Library, Vec<Box<dyn Error>>> {
114 let mut errs = vec![];
115
116 if let Ok(mpi_pkg_config) = env::var("MPI_PKG_CONFIG") {
117 match Config::new().cargo_metadata(false).probe(&mpi_pkg_config) {
118 Ok(lib) => return Ok(Library::from(lib)),
119 Err(_err) => {
120 let err: Box<dyn Error> = "Error: Environmental variable $MPI_PKG_CONFIG is set, but is not valid. Please check that it holds the address to a file ending in `.pc`.\nSee https://github.com/rsmpi/rsmpi/blob/main/README.md for more details.\nIf you mean to use another method to find an MPI library, unset $MPI_PKG_CONFIG".into();
121 errs.push(err);
122 return Err(errs);
123 }
124 }
125 } else {
126 let err: Box<dyn Error> =
127 "Environmental variable $MPI_PKG_CONFIG is not set. Trying next method".into();
128 errs.push(err);
129 }
130
131 if let Ok(cray_mpich_dir) = env::var("CRAY_MPICH_DIR") {
132 let pkg_config_mpich: PathBuf = [&cray_mpich_dir, "lib", "pkgconfig", "mpich.pc"]
133 .iter()
134 .collect();
135 match Config::new()
136 .cargo_metadata(false)
137 .probe(&pkg_config_mpich.to_string_lossy())
138 {
139 Ok(lib) => return Ok(Library::from(lib)),
140 Err(err) => {
141 let err: Box<dyn Error> = Box::new(err);
142 errs.push(err)
143 }
144 }
145 } else {
146 let err: Box<dyn Error> =
147 "Environmental variable $CRAY_MPICH_DIR is not set. Trying next method".into();
148 errs.push(err);
149 }
150
151 match probe_via_mpicc(&env::var("MPICC").unwrap_or_else(|_| String::from("mpicc"))) {
152 Ok(lib) => return Ok(lib),
153 Err(err) => {
154 let err: Box<dyn Error> =
155 format!("MPICC failed with error: {}. Trying next method", err).into();
156 errs.push(err);
157 }
158 }
159
160 match Config::new().cargo_metadata(false).probe("mpich") {
161 Ok(lib) => return Ok(Library::from(lib)),
162 Err(_err) => {
163 let err: Box<dyn Error> =
164 "Fallback 1: mpich was not found. To use this fallback, set $PKG_CONFIG_PATH to a path to a folder containing mpich.pc. Note that the recommended installation is through the $MPI_PKG_CONFIG. Trying next method".into();
165 errs.push(err);
166 }
167 }
168
169 match Config::new().cargo_metadata(false).probe("ompi") {
170 Ok(lib) => return Ok(Library::from(lib)),
171 Err(_err) => {
172 let err: Box<dyn Error> =
173 "Fallback 2: ompi (Open MPI) was not found. To use this fallback, set $PKG_CONFIG_PATH to a path to a folder containing ompi.pc. Note that the recommended installation is through the $MPI_PKG_CONFIG".into();
174 errs.push(err);
175 }
176 }
177
178 Err(errs)
179}
180
181#[cfg(test)]
182mod tests {
183 use super::unquote;
184
185 #[test]
186 fn double_quote() {
187 let s = "\"/usr/lib/my-mpi/include\"";
188 assert_eq!(Ok(String::from("/usr/lib/my-mpi/include")), unquote(s));
189 }
190
191 #[test]
192 fn single_quote() {
193 let s = "'/usr/lib/my-mpi/include'";
194 assert_eq!(Ok(String::from("/usr/lib/my-mpi/include")), unquote(s));
195 }
196
197 #[test]
198 fn backtick_quote() {
199 let s = "`/usr/lib/my-mpi/include`";
200 assert_eq!(Ok(String::from("/usr/lib/my-mpi/include")), unquote(s));
201 }
202
203 #[test]
204 fn no_quote() {
205 let s = "/usr/lib/my-mpi/include";
206 assert_eq!(Ok(String::from("/usr/lib/my-mpi/include")), unquote(s));
207 }
208
209 #[test]
210 fn unclosed_quote() {
211 let s = "'/usr/lib/my-mpi/include";
212 assert_eq!(unquote(s).unwrap_err().quote, '\'');
213 assert!(unquote(s).is_err());
214 }
215
216 use super::collect_args_with_prefix;
217
218 #[test]
219 fn flag_parsing_with_space() {
220 let cmd = r#"gcc -I"/opt/intel/My Oneapi/mpi/2021.8.0/include" -L"/opt/intel/My Oneapi/mpi/2021.8.0/lib/release" -L"/opt/intel/My Oneapi/mpi/2021.8.0/lib" -Xlinker --enable-new-dtags -Xlinker -rpath -Xlinker "/opt/intel/My Oneapi/mpi/2021.8.0/lib/release" -Xlinker -rpath -Xlinker "/opt/intel/My Oneapi/mpi/2021.8.0/lib" -lmpifort -lmpi -lrt -lpthread -Wl,-z,now -Wl,-z,relro -Wl,-z,noexecstack -Xlinker --enable-new-dtags -ldl"#;
221 assert_eq!(
222 collect_args_with_prefix(cmd, "-L"),
223 vec![
224 "/opt/intel/My Oneapi/mpi/2021.8.0/lib/release",
225 "/opt/intel/My Oneapi/mpi/2021.8.0/lib"
226 ]
227 );
228 }
229 #[test]
230 fn flag_parsing_without_space() {
231 let cmd = r#"gcc -I/usr/lib/x86_64-linux-gnu/openmpi/include -I/usr/lib/x86_64-linux-gnu/openmpi/include/openmpi -L/usr/lib/x86_64-linux-gnu/openmpi/lib -lmpi"#;
232 assert_eq!(
233 collect_args_with_prefix(cmd, "-L"),
234 vec!["/usr/lib/x86_64-linux-gnu/openmpi/lib"]
235 );
236 }
237}