build_probe_mpi/os/
unix.rs

1#![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 super::super::Library;
13
14use pkg_config::Config;
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    // Capture the output of `mpicc -show`. This usually gives the actual compiler command line
68    // invoked by the `mpicc` compiler wrapper.
69    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        // Collect the libraries that an MPI C program should be linked to...
72        let libs = collect_args_with_prefix(output.as_ref(), "-l");
73        // ... and the library search directories...
74        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        // ... and the header search directories.
80        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
97/// splits a command line by space and collects all arguments that start with `prefix`
98fn 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
112/// Probe the environment for an installed MPI library
113pub 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> = Box::new(err);
121                errs.push(err)
122            }
123        }
124    }
125
126    if let Ok(cray_mpich_dir) = env::var("CRAY_MPICH_DIR") {
127        let pkg_config_mpich: PathBuf = [&cray_mpich_dir, "lib", "pkgconfig", "mpich.pc"]
128            .iter()
129            .collect();
130        match Config::new()
131            .cargo_metadata(false)
132            .probe(&pkg_config_mpich.to_string_lossy())
133        {
134            Ok(lib) => return Ok(Library::from(lib)),
135            Err(err) => {
136                let err: Box<dyn Error> = Box::new(err);
137                errs.push(err)
138            }
139        }
140    }
141
142    match probe_via_mpicc(&env::var("MPICC").unwrap_or_else(|_| String::from("mpicc"))) {
143        Ok(lib) => return Ok(lib),
144        Err(err) => {
145            let err: Box<dyn Error> = Box::new(err);
146            errs.push(err)
147        }
148    }
149
150    match Config::new().cargo_metadata(false).probe("mpich") {
151        Ok(lib) => return Ok(Library::from(lib)),
152        Err(err) => {
153            let err: Box<dyn Error> = Box::new(err);
154            errs.push(err)
155        }
156    }
157
158    match Config::new().cargo_metadata(false).probe("ompi") {
159        Ok(lib) => return Ok(Library::from(lib)),
160        Err(err) => {
161            let err: Box<dyn Error> = Box::new(err);
162            errs.push(err)
163        }
164    }
165
166    Err(errs)
167}
168
169#[cfg(test)]
170mod tests {
171    use super::unquote;
172
173    #[test]
174    fn double_quote() {
175        let s = "\"/usr/lib/my-mpi/include\"";
176        assert_eq!(Ok(String::from("/usr/lib/my-mpi/include")), unquote(s));
177    }
178
179    #[test]
180    fn single_quote() {
181        let s = "'/usr/lib/my-mpi/include'";
182        assert_eq!(Ok(String::from("/usr/lib/my-mpi/include")), unquote(s));
183    }
184
185    #[test]
186    fn backtick_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 no_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 unclosed_quote() {
199        let s = "'/usr/lib/my-mpi/include";
200        assert_eq!(unquote(s).unwrap_err().quote, '\'');
201        assert!(unquote(s).is_err());
202    }
203
204    use super::collect_args_with_prefix;
205
206    #[test]
207    fn flag_parsing_with_space() {
208        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"#;
209        assert_eq!(
210            collect_args_with_prefix(cmd, "-L"),
211            vec![
212                "/opt/intel/My Oneapi/mpi/2021.8.0/lib/release",
213                "/opt/intel/My Oneapi/mpi/2021.8.0/lib"
214            ]
215        );
216    }
217    #[test]
218    fn flag_parsing_without_space() {
219        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"#;
220        assert_eq!(
221            collect_args_with_prefix(cmd, "-L"),
222            vec!["/usr/lib/x86_64-linux-gnu/openmpi/lib"]
223        );
224    }
225}