use ::{BuildMeta, CcFlag, CcFlags};
use ::std::fmt;
pub(crate) fn probe_and_link() -> Result<BuildMeta, ProbeError> {
probe_and_link_via_pkgconfig()
}
pub(crate) enum ProbeError {
PkgConfig(::pkg_config::Error),
String(String),
}
impl From<::pkg_config::Error> for ProbeError {
fn from(e: ::pkg_config::Error) -> Self {
ProbeError::PkgConfig(e)
}
}
impl fmt::Display for ProbeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ProbeError::PkgConfig(e) => fmt::Display::fmt(e, f),
ProbeError::String(s) => fmt::Display::fmt(s, f),
}
}
}
fn probe_and_link_via_pkgconfig() -> Result<BuildMeta, ProbeError> {
let library = ::pkg_config::probe_library("liblammps")?;
let include_dirs = CcFlags({
library.include_paths.into_iter()
.map(Into::into).map(CcFlag::IncludeDir)
.collect()
});
let defines = CcFlags({
library.defines.into_iter()
.map(|(key, value)| match value {
Some(value) => CcFlag::Define(format!("{}={}", key, value)),
None => CcFlag::Define(format!("{}", key)),
})
.collect()
});
if cfg!(feature = "exceptions") {
let needle = CcFlag::Define(String::from("LAMMPS_EXCEPTIONS"));
if !defines.0.iter().any(|x| x == &needle) {
let msg = String::from("\
system lammps was built without -DLAMMPS_EXCEPTIONS \
(--features=exceptions)\
");
return Err(ProbeError::String(msg));
}
}
Ok(BuildMeta {
header: "lammps/library.h",
include_dirs,
defines,
})
}