mod bindgen_macros;
pub mod build;
pub mod build_env;
pub mod utils;
use crate::build::is_cross_compiling;
use crate::build_env::sysroot_path;
use crate::build_env::target_env;
use crate::utils::path_between;
use crate::utils::path_to_str;
use std::env;
use std::ffi::OsStr;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
#[doc(hidden)]
pub use anyhow::{Context, Result};
pub trait CommandExt {
fn arg_for_macos<S: AsRef<OsStr>>(&mut self, arg: S) -> Result<&mut Command>;
fn configure_prefix<P: AsRef<Path>>(&mut self, path: P) -> Result<&mut Command>;
fn with_pkgconfig(&mut self) -> Result<&mut Command>;
fn with_toolchain(&mut self) -> Result<&mut Command>;
}
impl CommandExt for Command {
fn arg_for_macos<S: AsRef<OsStr>>(&mut self, arg: S) -> Result<&mut Command> {
if env::var("TARGET")
.map(|target| target.contains("-apple-darwin"))
.unwrap_or(false)
{
self.arg(arg.as_ref());
}
Ok(self)
}
fn configure_prefix<P: AsRef<Path>>(&mut self, prefix_dir: P) -> Result<&mut Command> {
self.args(&[
"--prefix",
path_to_str(&path_between(
sysroot_path().unwrap_or(PathBuf::from("/")),
prefix_dir,
))?,
]);
Ok(self)
}
fn with_pkgconfig(&mut self) -> Result<&mut Command> {
if is_cross_compiling()? {
if let Ok(value) = target_env("PKG_CONFIG_PATH") {
log::info!("Running command with PKG_CONFIG_PATH:{:?}", value);
self.env("PKG_CONFIG_PATH", value);
}
if let Ok(value) = target_env("PKG_CONFIG_LIBDIR") {
log::info!("Running command with PKG_CONFIG_LIBDIR:{:?}", value);
self.env("PKG_CONFIG_LIBDIR", value);
}
if let Ok(value) = target_env("PKG_CONFIG_SYSROOT_DIR") {
log::info!("Running command with PKG_CONFIG_SYSROOT_DIR:{:?}", value);
self.env("PKG_CONFIG_SYSROOT_DIR", value);
}
}
Ok(self)
}
fn with_toolchain(&mut self) -> Result<&mut Command> {
if is_cross_compiling()? {
if let Ok(target) = env::var("TARGET") {
self.arg(format!("--host={}", target));
}
if let Ok(cc) = env::var("TARGET_CC") {
self.arg(format!("CC={}", cc));
}
if let Ok(ar) = env::var("TARGET_AR") {
self.arg(format!("AR={}", ar));
}
if let Ok(sysroot) = env::var("TARGET_SYSROOT") {
self.arg(format!("--with-sysroot={}", &sysroot));
}
}
Ok(self)
}
}