rstsr_mkl_ffi/
lib.rs

1#![doc = include_str!("../readme.md")]
2#![allow(non_camel_case_types)]
3#![allow(non_snake_case)]
4
5pub mod mkl_types;
6pub mod service;
7
8#[cfg(feature = "blas")]
9pub mod blas;
10#[cfg(feature = "cblas")]
11pub mod cblas;
12#[cfg(feature = "lapack")]
13pub mod lapack;
14#[cfg(feature = "lapacke")]
15pub mod lapacke;
16
17pub const CRATE_NAME: &str = "rstsr-mkl-ffi";
18pub const LIB_NAME: &str = "MKL"; // for code, e.g. "MKL"
19pub const LIB_NAME_SHOW: &str = "oneAPI MKL"; // for display, e.g. "oneMKL"
20pub const LIB_NAME_LINK: &str = "mkl_rt"; // for linking, e.g. "mkl_rt"
21
22#[cfg(feature = "dynamic_loading")]
23pub(crate) mod get_lib_candidates {
24    use super::*;
25    use std::{fmt::Debug, path::PathBuf};
26
27    pub(crate) fn get_lib_candidates() -> Vec<String> {
28        use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
29        let mut candidates = vec![];
30
31        // user defined candidates
32        for paths in [format!("RSTSR_DYLOAD_{LIB_NAME}").as_str(), "RSTSR_DYLOAD"] {
33            if let Ok(path) = std::env::var(paths) {
34                candidates.extend(path.split(":").map(|s| s.to_string()).collect::<Vec<_>>());
35            }
36        }
37
38        let mkl_rt_name = format!("{DLL_PREFIX}{LIB_NAME_LINK}{DLL_SUFFIX}");
39        let env_mkl_root = std::env::var("MKL_ROOT");
40        let env_mklroot = std::env::var("MKLROOT");
41        let mut possible_dir_paths = vec![
42            PathBuf::from("/usr/lib"),
43            PathBuf::from("/usr/local/lib"),
44            PathBuf::from("/opt/intel/oneapi/mkl/latest/lib"),
45            PathBuf::from("/opt/miniconda3/lib"),
46            PathBuf::from(r"C:\Program Files (x86)\Intel\oneAPI\mkl\latest\lib"),
47            PathBuf::from(r"D:\Program Files (x86)\Intel\oneAPI\mkl\latest\lib"),
48            PathBuf::from(r"C:\Program Files\Intel\oneAPI\mkl\latest\lib"),
49            PathBuf::from(r"D:\Program Files\Intel\oneAPI\mkl\latest\lib"),
50        ];
51        if let Ok(home) = std::env::var("HOME") {
52            possible_dir_paths
53                .push(PathBuf::from_iter([&home, "intel", "oneapi", "mkl", "latest", "lib"]));
54            possible_dir_paths.push(PathBuf::from_iter([&home, "miniconda3", "lib"]));
55        }
56        if let Some(home) = std::env::home_dir() {
57            let home = home.to_string_lossy().to_string();
58            possible_dir_paths
59                .push(PathBuf::from_iter([&home, "intel", "oneapi", "mkl", "latest", "lib"]));
60            possible_dir_paths.push(PathBuf::from_iter([&home, "miniconda3", "lib"]));
61        }
62        if let Ok(mkl_root) = env_mkl_root {
63            possible_dir_paths.push(PathBuf::from(mkl_root).join("lib"));
64        }
65        if let Ok(mklroot) = env_mklroot {
66            possible_dir_paths.push(PathBuf::from(mklroot).join("lib"));
67        }
68
69        let mut possible_lib_paths = vec![mkl_rt_name.clone()];
70        for path in possible_dir_paths {
71            possible_lib_paths.push(path.join(&mkl_rt_name).to_string_lossy().into());
72        }
73
74        candidates.extend(possible_lib_paths);
75        candidates
76    }
77
78    pub(crate) fn panic_no_lib_found<S: Debug>(candidates: &[S], err_msg: &str) -> ! {
79        panic!(
80            r#"
81This happens in crate `{CRATE_NAME}`.
82Unable to dynamically load the {LIB_NAME_SHOW} (`{LIB_NAME_LINK}`) shared library.
83Candidates: {candidates:#?}
84
85Please check
86- if dynamic-loading is not desired, please disable the `dynamic_loading` feature in your `Cargo.toml` (by something like --no-default-features).
87- if you want to provide custom {LIB_NAME_SHOW} library, use environment variable `RSTSR_DYLOAD_{LIB_NAME}` or `RSTSR_DYLOAD` to specify the path to the library.
88- if `lib{LIB_NAME_LINK}.so` (linux) or `lib{LIB_NAME_LINK}.dylib` (macOS) or `lib{LIB_NAME_LINK}.dll` (Windows) is installed on your system.
89- if `LD_LIBRARY_PATH` (linux) or `DYLD_LIBRARY_PATH` (macOS) or `PATH` (Windows) environment variable is set correctly (any path that's visible to linker).
90- this crate does not use things like `LD_PRELOAD` or `DYLD_INSERT_LIBRARIES` to load the library.
91- this crate does not support static linking of libraries when dynamic-loading.
92
93Error message(s):
94{err_msg}
95"#
96        )
97    }
98}