sys: Use includes from php-config --include-dir

If PHP headers are already available, it feels kinda redundant to clone
the PHP sources and rebuild them again just to generate bindings. So if
php-config is available in $PATH, we don't clone + compile anymore but
use the include directory printed via "php-config --include-dir".

Unfortunately CLang doesn't yet support asm goto, so we now include
php_config.h very early and undef HAVE_ASM_GOTO thereafter. The #ifndef
wrapper in php_config.h will prevent a second inclusion, so we don't get
HAVE_ASM_GOTO defined again.

Signed-off-by: aszlig <aszlig@nix.build>
This commit is contained in:
aszlig 2019-09-02 02:31:35 +02:00
commit 773a341cc2
No known key found for this signature in database
GPG key ID: 684089CE67EBB691
2 changed files with 85 additions and 70 deletions

View file

@ -53,25 +53,7 @@ fn exists(path: &str) -> bool {
Path::new(target(path).as_str()).exists() Path::new(target(path).as_str()).exists()
} }
fn main() { fn compile_php(php_version: &str, link_static: bool) -> () {
let cpus = format!("{}", num_cpus::get());
#[cfg(all(target_os = "linux"))]
let default_link_static = false;
#[cfg(all(target_os = "macos"))]
let default_link_static = true;
let php_version = option_env!("PHP_VERSION").unwrap_or(PHP_VERSION);
println!("cargo:rerun-if-env-changed=PHP_VERSION");
println!("cargo:rerun-if-env-changed=PHP_LINK_STATIC");
let link_dynamic = env::var_os("PHP_LINK_DYNAMIC")
.map(|_| true)
.unwrap_or(false);
let link_static = env::var_os("PHP_LINK_STATIC")
.map(|_| true)
.unwrap_or(default_link_static && !link_dynamic);
if !exists("php-src/LICENSE") {
println_stderr!("Setting up PHP {}", php_version); println_stderr!("Setting up PHP {}", php_version);
run_command_or_fail("/".to_string(), "mkdir", &["-p", &target("")]); run_command_or_fail("/".to_string(), "mkdir", &["-p", &target("")]);
run_command_or_fail( run_command_or_fail(
@ -136,10 +118,39 @@ fn main() {
// "--with-pic", // "--with-pic",
]; ];
run_command_or_fail(target("php-src"), "./configure", config); run_command_or_fail(target("php-src"), "./configure", config);
let cpus = format!("{}", num_cpus::get());
run_command_or_fail(target("php-src"), "make", &["-j", cpus.as_str()]); run_command_or_fail(target("php-src"), "make", &["-j", cpus.as_str()]);
} }
let include_dir = target("php-src"); fn main() {
#[cfg(all(target_os = "linux"))]
let default_link_static = false;
#[cfg(all(target_os = "macos"))]
let default_link_static = true;
let php_version = option_env!("PHP_VERSION").unwrap_or(PHP_VERSION);
println!("cargo:rerun-if-env-changed=PHP_VERSION");
println!("cargo:rerun-if-env-changed=PHP_LINK_STATIC");
let link_dynamic = env::var_os("PHP_LINK_DYNAMIC")
.map(|_| true)
.unwrap_or(false);
let link_static = env::var_os("PHP_LINK_STATIC")
.map(|_| true)
.unwrap_or(default_link_static && !link_dynamic);
let maybe_include_dir = Command::new("php-config")
.args(&["--include-dir"])
.output()
.map(|o| String::from_utf8(o.stdout).unwrap().trim_end().to_string())
.ok();
let include_dir = maybe_include_dir.unwrap_or_else(|| {
if !exists("php-src/LICENSE") {
compile_php(php_version, link_static);
}
target("php-src")
});
let includes = ["/", "/TSRM", "/Zend", "/main"] let includes = ["/", "/TSRM", "/Zend", "/main"]
.iter() .iter()

View file

@ -1,5 +1,9 @@
#ifndef PHP_RS_WRAPPER_H #ifndef PHP_RS_WRAPPER_H
#define PHP_RS_WRAPPER_H #define PHP_RS_WRAPPER_H
/* CLang doesn't support asm goto yet, so let's force-disable it. */
#include <main/php_config.h>
#undef HAVE_ASM_GOTO
#include <Zend/zend.h> #include <Zend/zend.h>
#include <Zend/zend_compile.h> #include <Zend/zend_compile.h>
#include <main/php.h> #include <main/php.h>