2022-03-18 04:36:51 +01:00
|
|
|
use std::{path::PathBuf, process::Command};
|
|
|
|
|
2022-11-28 14:57:34 +01:00
|
|
|
use anyhow::{bail, Context, Result};
|
2022-03-18 04:36:51 +01:00
|
|
|
|
2022-12-11 20:08:50 +01:00
|
|
|
use crate::{find_executable, path_from_env, PHPInfo, PHPProvider};
|
2022-03-18 04:36:51 +01:00
|
|
|
|
2022-11-28 14:57:34 +01:00
|
|
|
pub struct Provider {}
|
2022-03-18 04:36:51 +01:00
|
|
|
|
2022-11-28 14:57:34 +01:00
|
|
|
impl Provider {
|
2022-03-18 04:36:51 +01:00
|
|
|
/// Runs `php-config` with one argument, returning the stdout.
|
|
|
|
fn php_config(&self, arg: &str) -> Result<String> {
|
2022-12-11 20:08:50 +01:00
|
|
|
let cmd = Command::new(self.find_bin()?)
|
2022-03-18 04:36:51 +01:00
|
|
|
.arg(arg)
|
|
|
|
.output()
|
|
|
|
.context("Failed to run `php-config`")?;
|
|
|
|
let stdout = String::from_utf8_lossy(&cmd.stdout);
|
|
|
|
if !cmd.status.success() {
|
|
|
|
let stderr = String::from_utf8_lossy(&cmd.stderr);
|
|
|
|
bail!("Failed to run `php-config`: {} {}", stdout, stderr);
|
|
|
|
}
|
|
|
|
Ok(stdout.to_string())
|
|
|
|
}
|
2022-12-11 20:08:50 +01:00
|
|
|
|
|
|
|
fn find_bin(&self) -> Result<PathBuf> {
|
|
|
|
// If path is given via env, it takes priority.
|
|
|
|
if let Some(path) = path_from_env("PHP_CONFIG") {
|
|
|
|
if !path.try_exists()? {
|
|
|
|
// If path was explicitly given and it can't be found, this is a hard error
|
|
|
|
bail!("php-config executable not found at {:?}", path);
|
|
|
|
}
|
|
|
|
return Ok(path);
|
|
|
|
}
|
|
|
|
find_executable("php-config").with_context(|| {
|
|
|
|
"Could not find `php-config` executable. \
|
|
|
|
Please ensure `php-config` is in your PATH or the \
|
|
|
|
`PHP_CONFIG` environment variable is set."
|
|
|
|
})
|
|
|
|
}
|
2022-03-18 04:36:51 +01:00
|
|
|
}
|
|
|
|
|
2022-11-28 14:57:34 +01:00
|
|
|
impl<'a> PHPProvider<'a> for Provider {
|
|
|
|
fn new(_: &'a PHPInfo) -> Result<Self> {
|
|
|
|
Ok(Self {})
|
2022-03-18 04:36:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_includes(&self) -> Result<Vec<PathBuf>> {
|
|
|
|
Ok(self
|
|
|
|
.php_config("--includes")?
|
|
|
|
.split(' ')
|
|
|
|
.map(|s| s.trim_start_matches("-I"))
|
|
|
|
.map(PathBuf::from)
|
|
|
|
.collect())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_defines(&self) -> Result<Vec<(&'static str, &'static str)>> {
|
|
|
|
Ok(vec![])
|
|
|
|
}
|
2023-10-20 14:08:10 +02:00
|
|
|
|
|
|
|
fn print_extra_link_args(&self) -> Result<()> {
|
|
|
|
#[cfg(feature = "embed")]
|
|
|
|
println!("cargo:rustc-link-lib=php");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-03-18 04:36:51 +01:00
|
|
|
}
|