From 94bc9c8baaee18a8a3403ca29bfc03fe3495cdbb Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Mon, 16 Nov 2026 13:58:39 -0800 Subject: [PATCH] Build tools: add `PhpConfigRunner` for running `php-config` --- crates/build-tools/src/lib.rs | 4 ++ crates/build-tools/src/php_config_runner.rs | 68 +++++++++++++++++++++ crates/sys/bindings.rs | 4 +- crates/sys/build.rs | 19 +----- 4 files changed, 77 insertions(+), 18 deletions(-) create mode 100644 crates/build-tools/src/php_config_runner.rs diff --git a/crates/build-tools/src/lib.rs b/crates/build-tools/src/lib.rs index 89d20ce..c04f8aa 100644 --- a/crates/build-tools/src/lib.rs +++ b/crates/build-tools/src/lib.rs @@ -27,3 +27,7 @@ pub use int_casts::IntTypeCastCallbacks; /// Module for the `ExplicitIntTypesCallbacks` callbacks. mod int_types; pub use int_types::ExplicitIntTypesCallbacks; + +/// Module for the `PhpConfigRunner` utility. +mod php_config_runner; +pub use php_config_runner::PhpConfigRunner; diff --git a/crates/build-tools/src/php_config_runner.rs b/crates/build-tools/src/php_config_runner.rs new file mode 100644 index 0000000..a31c100 --- /dev/null +++ b/crates/build-tools/src/php_config_runner.rs @@ -0,0 +1,68 @@ +use std::ffi::{OsStr, OsString}; +use std::process::Command; + +/// Utility for running `php-config` commands. +#[derive(Debug)] +pub struct PhpConfigRunner { + /// Actual Command object. + inner: Command, + /// Arguments added to the command, for use in debugging. + args: Vec, +} + +impl PhpConfigRunner { + /// Create a new command runner. + #[must_use] + pub fn new() -> Self { + Self { + inner: Command::new("php-config"), + args: vec![], + } + } + + /// Add an argument. + pub fn arg(&mut self, arg: S) -> &mut Self + where + S: AsRef, + { + self.args.push(arg.as_ref().to_os_string()); + let _: &mut Command = self.inner.arg(arg); + self + } + + /// Run the command and return the output, which must be UTF-8. + /// + /// # Panics + /// + /// Panics if the command fails or returns output that is not valid UTF-8. + #[expect(clippy::panic, reason = "Panic is at build time")] + pub fn run(&mut self) -> String { + let output = self.inner.output().unwrap_or_else(|err| { + panic!( + "php-config {} should succeed, got {err}", + self.args + .iter() + .map(|arg| arg.display().to_string()) + .collect::>() + .join(" ") + ) + }); + let stdout_full = std::str::from_utf8(&output.stdout).unwrap_or_else(|err| { + panic!( + "php-config {} should result in UTF-8, got {err}", + self.args + .iter() + .map(|arg| arg.display().to_string()) + .collect::>() + .join(" ") + ) + }); + stdout_full.trim().to_owned() + } +} + +impl Default for PhpConfigRunner { + fn default() -> Self { + Self::new() + } +} diff --git a/crates/sys/bindings.rs b/crates/sys/bindings.rs index b3b02b7..348c79d 100644 --- a/crates/sys/bindings.rs +++ b/crates/sys/bindings.rs @@ -498,11 +498,11 @@ pub const ENABLE_CHROOT_FUNC: u32 = 1; pub const GWINSZ_IN_SYS_IOCTL: u32 = 1; pub const MAJOR_IN_SYSMACROS: u32 = 1; pub const PCRE2_CODE_UNIT_WIDTH: u32 = 8; -pub const PHP_BUILD_SYSTEM : & [u8 ; 106] = b"Linux 77050b0d8e26 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 GNU/Linux\0" ; +pub const PHP_BUILD_SYSTEM : & [u8 ; 106] = b"Linux b5e4910a2d64 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 GNU/Linux\0" ; pub const PHP_CAN_SUPPORT_PROC_OPEN: u32 = 1; pub const PHP_OS: &[u8; 6] = b"Linux\0"; pub const PHP_SIGCHILD: u32 = 0; -pub const PHP_UNAME : & [u8 ; 106] = b"Linux 77050b0d8e26 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 GNU/Linux\0" ; +pub const PHP_UNAME : & [u8 ; 106] = b"Linux b5e4910a2d64 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 GNU/Linux\0" ; pub const PHP_USE_PHP_CRYPT_R: u32 = 1; pub const PHP_WRITE_STDOUT: u32 = 1; pub const SIZEOF_INT: u32 = 4; diff --git a/crates/sys/build.rs b/crates/sys/build.rs index 2153028..e5e8c81 100644 --- a/crates/sys/build.rs +++ b/crates/sys/build.rs @@ -6,7 +6,7 @@ use bindgen::EnumVariation; use rsphp_build_tools::{ AnonymousTypeNames, BuildIdCallbacks, CommentProcessor, ExplicitIntTypesCallbacks, - IntTypeCastCallbacks, ZendHeaderIterator, + IntTypeCastCallbacks, PhpConfigRunner, ZendHeaderIterator, }; fn main() { @@ -17,21 +17,8 @@ fn main() { println!("cargo:rerun-if-changed=build.rs"); - let include_dir_output = std::process::Command::new("php-config") - .arg("--include-dir") - .output() - .expect("php-config --include-dir should succeed"); - let include_dir_full = std::str::from_utf8(&include_dir_output.stdout) - .expect("php-config --include-dir should result in UTF8"); - let include_dir = include_dir_full.trim(); - - let includes_output = std::process::Command::new("php-config") - .arg("--includes") - .output() - .expect("php-config --includes should succeed"); - let includes_full = std::str::from_utf8(&includes_output.stdout) - .expect("php-config --includes should result in UTF8"); - let includes = includes_full.trim(); + let include_dir = PhpConfigRunner::new().arg("--include-dir").run(); + let includes = PhpConfigRunner::new().arg("--includes").run(); let mut builder = bindgen::Builder::default();