1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#![deny(missing_docs)]
#![warn(missing_copy_implementations)]
#![warn(trivial_casts)]
#![warn(trivial_numeric_casts)]
#![warn(unused_extern_crates)]
#![warn(unused_import_braces)]
#![warn(unused_qualifications)]

use core::fmt;
use std::{self, env, error::Error, path::PathBuf, process::Command};

use super::super::Library;

use pkg_config::Config;

#[derive(Debug, PartialEq)]
struct UnquoteError {
    quote: char,
}

impl UnquoteError {
    fn new(quote: char) -> UnquoteError {
        UnquoteError { quote }
    }
}
impl Error for UnquoteError {}

impl fmt::Display for UnquoteError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Quotes '{}' not closed.", self.quote)
    }
}

fn unquote(s: &str) -> Result<String, UnquoteError> {
    if s.chars().count() < 2 {
        return Ok(String::from(s));
    }

    let quote = s.chars().next().unwrap();

    if quote != '"' && quote != '\'' && quote != '`' {
        return Ok(String::from(s));
    }

    if s.chars().last().unwrap() != quote {
        return Err(UnquoteError::new(quote));
    }

    let s = &s[1..s.len() - 1];
    Ok(String::from(s))
}

impl From<pkg_config::Library> for Library {
    fn from(lib: pkg_config::Library) -> Self {
        Library {
            mpicc: None,
            libs: lib.libs,
            lib_paths: lib.link_paths,
            include_paths: lib.include_paths,
            version: lib.version,
            _priv: (),
        }
    }
}

fn probe_via_mpicc(mpicc: &str) -> std::io::Result<Library> {
    // Capture the output of `mpicc -show`. This usually gives the actual compiler command line
    // invoked by the `mpicc` compiler wrapper.
    Command::new(mpicc).arg("-show").output().map(|cmd| {
        let output = String::from_utf8(cmd.stdout).expect("mpicc output is not valid UTF-8");
        // Collect the libraries that an MPI C program should be linked to...
        let libs = collect_args_with_prefix(output.as_ref(), "-l");
        // ... and the library search directories...
        let libdirs = collect_args_with_prefix(output.as_ref(), "-L")
            .into_iter()
            .filter_map(|x| unquote(&x).ok())
            .map(PathBuf::from)
            .collect();
        // ... and the header search directories.
        let headerdirs = collect_args_with_prefix(output.as_ref(), "-I")
            .into_iter()
            .filter_map(|x| unquote(&x).ok())
            .map(PathBuf::from)
            .collect();

        Library {
            mpicc: Some(mpicc.to_string()),
            libs,
            lib_paths: libdirs,
            include_paths: headerdirs,
            version: String::from("unknown"),
            _priv: (),
        }
    })
}

/// splits a command line by space and collects all arguments that start with `prefix`
fn collect_args_with_prefix(cmd: &str, prefix: &str) -> Vec<String> {
    shell_words::split(cmd)
        .unwrap()
        .iter()
        .filter_map(|arg| {
            if arg.starts_with(prefix) {
                Some(arg[2..].to_owned())
            } else {
                None
            }
        })
        .collect()
}

/// Probe the environment for an installed MPI library
pub fn probe() -> Result<Library, Vec<Box<dyn Error>>> {
    let mut errs = vec![];

    if let Ok(mpi_pkg_config) = env::var("MPI_PKG_CONFIG") {
        match Config::new().cargo_metadata(false).probe(&mpi_pkg_config) {
            Ok(lib) => return Ok(Library::from(lib)),
            Err(err) => {
                let err: Box<dyn Error> = Box::new(err);
                errs.push(err)
            }
        }
    }

    if let Ok(cray_mpich_dir) = env::var("CRAY_MPICH_DIR") {
        let pkg_config_mpich: PathBuf = [&cray_mpich_dir, "lib", "pkgconfig", "mpich.pc"]
            .iter()
            .collect();
        match Config::new()
            .cargo_metadata(false)
            .probe(&pkg_config_mpich.to_string_lossy())
        {
            Ok(lib) => return Ok(Library::from(lib)),
            Err(err) => {
                let err: Box<dyn Error> = Box::new(err);
                errs.push(err)
            }
        }
    }

    match probe_via_mpicc(&env::var("MPICC").unwrap_or_else(|_| String::from("mpicc"))) {
        Ok(lib) => return Ok(lib),
        Err(err) => {
            let err: Box<dyn Error> = Box::new(err);
            errs.push(err)
        }
    }

    match Config::new().cargo_metadata(false).probe("mpich") {
        Ok(lib) => return Ok(Library::from(lib)),
        Err(err) => {
            let err: Box<dyn Error> = Box::new(err);
            errs.push(err)
        }
    }

    match Config::new().cargo_metadata(false).probe("ompi") {
        Ok(lib) => return Ok(Library::from(lib)),
        Err(err) => {
            let err: Box<dyn Error> = Box::new(err);
            errs.push(err)
        }
    }

    Err(errs)
}

#[cfg(test)]
mod tests {
    use super::unquote;

    #[test]
    fn double_quote() {
        let s = "\"/usr/lib/my-mpi/include\"";
        assert_eq!(Ok(String::from("/usr/lib/my-mpi/include")), unquote(s));
    }

    #[test]
    fn single_quote() {
        let s = "'/usr/lib/my-mpi/include'";
        assert_eq!(Ok(String::from("/usr/lib/my-mpi/include")), unquote(s));
    }

    #[test]
    fn backtick_quote() {
        let s = "`/usr/lib/my-mpi/include`";
        assert_eq!(Ok(String::from("/usr/lib/my-mpi/include")), unquote(s));
    }

    #[test]
    fn no_quote() {
        let s = "/usr/lib/my-mpi/include";
        assert_eq!(Ok(String::from("/usr/lib/my-mpi/include")), unquote(s));
    }

    #[test]
    fn unclosed_quote() {
        let s = "'/usr/lib/my-mpi/include";
        assert_eq!(unquote(s).unwrap_err().quote, '\'');
        assert!(unquote(s).is_err());
    }

    use super::collect_args_with_prefix;

    #[test]
    fn flag_parsing_with_space() {
        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"#;
        assert_eq!(
            collect_args_with_prefix(cmd, "-L"),
            vec![
                "/opt/intel/My Oneapi/mpi/2021.8.0/lib/release",
                "/opt/intel/My Oneapi/mpi/2021.8.0/lib"
            ]
        );
    }
    #[test]
    fn flag_parsing_without_space() {
        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"#;
        assert_eq!(
            collect_args_with_prefix(cmd, "-L"),
            vec!["/usr/lib/x86_64-linux-gnu/openmpi/lib"]
        );
    }
}