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
#![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)]

extern crate pkg_config;

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

use super::super::Library;

use pkg_config::Config;

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()
            .map(PathBuf::from)
            .collect();
        // ... and the header search directories.
        let headerdirs = collect_args_with_prefix(output.as_ref(), "-I")
            .into_iter()
            .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> {
    cmd.split_whitespace()
        .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)
}