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
use std::{io, path::*, process::Command};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
    #[error("Subprocess returns with non-zero status: {status}")]
    NonZeroExitStatus { status: i32 },

    #[error("Subprocess cannot start: {error:?}")]
    SubprocessCannotStart { error: io::Error },

    #[error("Fortran compiler not found. It is necessary to build LAPACK.")]
    FortranCompilerNotFound,

    #[error("Cannot canonicalize path in Linker flag: {}", path.display())]
    CannotCanonicalizePath { path: PathBuf },

    #[error("Makefile.conf does not exist in {}", out_dir.display())]
    MakeConfNotExist { out_dir: PathBuf },

    #[error("Library file does not exist: {}", path.display())]
    LibraryNotExist { path: PathBuf },

    #[error("Target {} is unsupported", target)]
    UnsupportedTarget { target: String },

    #[error("Insufficient cross compile information, need all of OPENBLAS_{{CC, FC, HOSTCC}}")]
    MissingCrossCompileInfo,

    #[error("Other IO errors: {0:?}")]
    IOError(#[from] io::Error),
}

pub(crate) trait CheckCall {
    fn check_call(&mut self) -> Result<(), Error>;
}

impl CheckCall for Command {
    fn check_call(&mut self) -> Result<(), Error> {
        match self.status() {
            Ok(status) => {
                if !status.success() {
                    Err(Error::NonZeroExitStatus {
                        status: status.code().unwrap_or(-1),
                    })
                } else {
                    Ok(())
                }
            }
            Err(error) => Err(Error::SubprocessCannotStart { error }),
        }
    }
}