const LINUX_LINKNAMES: &[&str] = &["mex", "mx", "mat", "eng"];
const WIN_LINKNAMES: &[&str] = &["libmex", "libmx", "libmat", "libeng"];
fn main() {
if std::env::var("DOCS_RS").is_ok(){
return
}
let platform = match std::env::var("CARGO_CFG_TARGET_OS")
.as_deref()
.expect("Environment variable 'CARGO_CFG_TARGET_OS' not found.")
{
"windows" => OS::Windows,
"linux" => OS::Linux,
"macos" => OS::MacOS,
unsupported_target => panic!("Target {unsupported_target} are currently unsupported."),
};
let target_env =
std::env::var("CARGO_CFG_TARGET_ENV").expect("'CARGO_CFG_TARGET_ENV' not found");
let matlabpath = get_matlab_path();
assert!(
std::path::Path::new(&matlabpath)
.try_exists()
.unwrap_or_else(|_| panic!("Cannot check existence of path {matlabpath}")),
"The path to the matlab installation does not exist: {matlabpath}"
);
let link_search_path = format!("{matlabpath}/{}", match (platform, target_env.as_str()) {
(OS::Windows, "msvc") => "extern/lib/win64/microsoft/",
(OS::Windows, "gnu") => "extern/lib/win64/mingw64/",
(OS::Linux, _) => "bin/glnxa64/",
_ => unimplemented!("Combination of {platform:?} and {target_env:?} not supported. Please raise an issue at https://github.com/Tastaturtaste/matlab-sys/issues with a description of your environment."),
});
assert!(
std::path::Path::new(&link_search_path)
.try_exists()
.unwrap_or_else(|_| panic!("Cannot check existence of path {link_search_path}")),
"The path to the matlab link libraries does not exist: {link_search_path}"
);
println!("cargo:rustc-link-search={link_search_path}");
match platform {
OS::Windows => {
for lib in WIN_LINKNAMES {
println!("cargo:rustc-link-lib={lib}");
}
}
OS::Linux => {
for lib in LINUX_LINKNAMES {
println!("cargo:rustc-link-lib={lib}");
}
}
OS::MacOS => unimplemented!(),
}
}
fn get_matlab_path() -> String {
if let Ok(path) = std::env::var("MATLABPATH") {
path
} else if let Ok(cmd_output) = std::process::Command::new("matlab")
.arg("-batch")
.arg("disp(matlabroot)")
.output()
{
String::from_utf8(cmd_output.stdout)
.expect("The path to the Matlab installation is not valid utf-8")
.trim() .to_owned()
} else {
panic!("Matlab installation to link against not found. Specify the path to the installation to link against in the environment variable 'MATLABPATH' or make sure Matlab is callable from the command line.")
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum OS {
Windows,
Linux,
MacOS,
}