cleanup sys

This commit is contained in:
Robin Appelman 2019-04-01 00:06:19 +02:00
commit 99bdad5ec0
8 changed files with 8 additions and 5021 deletions

View file

@ -3,7 +3,7 @@ use std::intrinsics::transmute;
use libc::*; use libc::*;
use crate::zend::bindings::zend_error; use ivory_sys::zend_error;
extern "C" { extern "C" {
pub fn php_printf(format: *const c_char, ...) -> size_t; pub fn php_printf(format: *const c_char, ...) -> size_t;

File diff suppressed because it is too large Load diff

View file

@ -5,4 +5,3 @@ pub use self::zval::{ExecuteData, ZVal, PhpVal};
mod module; mod module;
mod function; mod function;
mod zval; mod zval;
pub mod bindings;

View file

@ -4,9 +4,7 @@ use std::mem::size_of;
use std::os::raw::c_char; use std::os::raw::c_char;
use std::str; use std::str;
use crate::zend::bindings::zend_string; use ivory_sys::{zend_execute_data, zval, zend_string};
use super::bindings::{zend_execute_data, zval};
#[repr(transparent)] #[repr(transparent)]
pub struct ExecuteData(zend_execute_data); pub struct ExecuteData(zend_execute_data);

View file

@ -1,48 +1,3 @@
# php-sys # ivory-sys
Bindings to php. Bindings to php.
Note: I have only tested this without ZTS.
## PHP
In order to compile php-sys, we need development headers and the libphp7 library. That library may come in the form of `libphp7.so` or `libphp7.a` depending on how you install/compile PHP.
### From Package
* For Ubuntu, please refer to the [.travis.yml](../.travis.yml) _install_ section for the commands.
* For Mac OS X, I could not find a set of packages that worked.
### From Source
Some basic instructions on how to install PHP so you can embed it into Rust.
#### Mac OS X
I had to use brew to install bison. I believe autoconf and other tools were either already installed or provided by Mac OS X. Brew installed some modified version of libiconv which confused PHP. I also had some problems, so I stopped building xml related stuff. To build I had to do:
```
$ ./genfiles
$ ./buildconf --force
$ PATH="/usr/local/opt/bison/bin:$PATH" ./configure --enable-debug --enable-embed=static --without-iconv --disable-libxml --disable-dom --disable-xml --disable-simplexml --disable-xmlwriter --disable-xmlreader --without-pear
$ PATH="/usr/local/opt/bison/bin:$PATH" make
$ PATH="/usr/local/opt/bison/bin:$PATH" make test
```
Note: I embed a static library on Mac OS X. If you want to do embed PHP with a shared library, then use `--enable-embed=shared`.
#### Linux
Here are the dependencies needed (in apt-get form):
```bash
$ apt-get install git make gcc libxml2-dev autoconf bison valgrind clang re2c
```
```
$ ./genfiles
$ ./buildconf --force
$ ./configure --enable-debug --enable-embed=shared
$ make
$ make test
```

View file

@ -2,15 +2,12 @@ extern crate bindgen;
extern crate cc; extern crate cc;
extern crate num_cpus; extern crate num_cpus;
use bindgen::callbacks::{MacroParsingBehavior, ParseCallbacks};
use bindgen::Builder; use bindgen::Builder;
use std::collections::HashSet;
use std::env; use std::env;
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Command; use std::process::Command;
use std::sync::{Arc, RwLock};
const PHP_VERSION: &'static str = concat!("php-", env!("CARGO_PKG_VERSION")); const PHP_VERSION: &'static str = concat!("php-", env!("CARGO_PKG_VERSION"));
@ -52,25 +49,6 @@ fn exists(path: &str) -> bool {
Path::new(target(path).as_str()).exists() Path::new(target(path).as_str()).exists()
} }
/// This is needed to prevent bindgen to create multiple definitions of the same macro and fail
#[derive(Debug)]
struct MacroCallback {
macros: Arc<RwLock<HashSet<String>>>,
}
impl ParseCallbacks for MacroCallback {
fn will_parse_macro(&self, name: &str) -> MacroParsingBehavior {
self.macros.write().unwrap().insert(name.into());
match name {
"FP_NAN" | "FP_INFINITE" | "FP_ZERO" | "FP_SUBNORMAL" | "FP_NORMAL" => {
MacroParsingBehavior::Ignore
}
_ => MacroParsingBehavior::Default,
}
}
}
fn main() { fn main() {
let cpus = format!("{}", num_cpus::get()); let cpus = format!("{}", num_cpus::get());
#[cfg(all(target_os = "linux"))] #[cfg(all(target_os = "linux"))]
@ -78,7 +56,6 @@ fn main() {
#[cfg(all(target_os = "macos"))] #[cfg(all(target_os = "macos"))]
let default_link_static = true; let default_link_static = true;
let php_version = option_env!("PHP_VERSION").unwrap_or(PHP_VERSION); let php_version = option_env!("PHP_VERSION").unwrap_or(PHP_VERSION);
let macros = Arc::new(RwLock::new(HashSet::new()));
println!("cargo:rerun-if-env-changed=PHP_VERSION"); println!("cargo:rerun-if-env-changed=PHP_VERSION");
println!("cargo:rerun-if-env-changed=PHP_LINK_STATIC"); println!("cargo:rerun-if-env-changed=PHP_LINK_STATIC");
@ -166,12 +143,6 @@ fn main() {
} }
let include_dir = target("php-src"); let include_dir = target("php-src");
let lib_dir = target("php-src/libs");
let link_type = if link_static { "=static" } else { "" };
println!("cargo:rustc-link-lib{}=php7", link_type);
println!("cargo:rustc-link-search=native={}", lib_dir);
let includes = ["/", "/TSRM", "/Zend", "/main"] let includes = ["/", "/TSRM", "/Zend", "/main"]
.iter() .iter()
@ -181,36 +152,14 @@ fn main() {
let bindings = Builder::default() let bindings = Builder::default()
.rustfmt_bindings(true) .rustfmt_bindings(true)
.clang_args(includes) .clang_args(includes)
.whitelist_function("_zend_file_handle__bindgen_ty_1")
.whitelist_function("php_execute_script")
.whitelist_function("php_module_startup")
.whitelist_function("php_request_shutdown")
.whitelist_function("php_request_startup")
.whitelist_function("phprpm_fopen")
.whitelist_function("sapi_send_headers")
.whitelist_function("sapi_startup")
.whitelist_function("sg_request_info")
.whitelist_function("sg_sapi_headers")
.whitelist_function("sg_server_context")
.whitelist_function("sg_server_context")
.whitelist_function("sg_set_server_context")
.whitelist_function("sg_set_server_context")
.whitelist_function("ts_resource_ex")
.whitelist_function("tsrm_startup")
.whitelist_function("zend_error") .whitelist_function("zend_error")
.whitelist_function("zend_signal_startup") .whitelist_function("php_info_print_table_start")
.whitelist_function("zend_tsrmls_cache_update") .whitelist_function("php_info_print_table_row")
.whitelist_var("SAPI_HEADER_SENT_SUCCESSFULLY") .whitelist_function("php_info_print_table_end")
.whitelist_type("sapi_headers_struc") .whitelist_function("php_printf")
.whitelist_type("sapi_module_struc")
.whitelist_type("sapi_request_info")
.whitelist_type("ZEND_RESULT_CODE")
.whitelist_type("zval") .whitelist_type("zval")
.whitelist_type("zend_execute_data") .whitelist_type("zend_execute_data")
.whitelist_var("zend_stream_type_ZEND_HANDLE_FP") .derive_default(false)
.parse_callbacks(Box::new(MacroCallback {
macros: macros.clone(),
})).derive_default(true)
.header("wrapper.h") .header("wrapper.h")
.generate() .generate()
.expect("Unable to generate bindings"); .expect("Unable to generate bindings");
@ -219,13 +168,4 @@ fn main() {
bindings bindings
.write_to_file(out_path.join("bindings.rs")) .write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!"); .expect("Couldn't write bindings!");
cc::Build::new()
.file("src/shim.c")
.include(&include_dir)
.flag("-fPIC")
.flag("-m64")
.include(&format!("{}/TSRM", include_dir))
.include(&format!("{}/Zend", include_dir))
.include(&format!("{}/main", include_dir))
.compile("foo");
} }

View file

@ -1,17 +1,5 @@
#![allow(non_upper_case_globals)] #![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
#![allow(non_snake_case)] #![allow(non_snake_case)]
use std::os::raw::{c_char, c_uchar, c_void};
//extern "C" {
//// pub fn sg_request_info() -> *mut sapi_request_info;
// pub fn sg_server_context() -> *mut c_void;
// pub fn sg_set_server_context(context: *mut c_void);
//// pub fn sg_sapi_headers() -> *mut sapi_headers_struct;
// pub fn sg_headers_sent() -> c_uchar;
// pub fn sg_set_headers_sent(is_sent: c_uchar);
// pub fn zend_tsrmls_cache_update();
//// pub fn phprpm_fopen(filename: *const c_char, mode: *const c_char) -> *mut FILE;
//}
include!(concat!(env!("OUT_DIR"), "/bindings.rs")); include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

View file

@ -1,48 +0,0 @@
/**
* This file defines some simple C functions to make ffi interop easier. The
* majority of the functions are wrapping some pre-processor define that is
* critical to PHP internals.
*/
#include <Zend/zend.h>
#include <main/php.h>
#include <sapi/embed/php_embed.h>
//sapi_request_info * sg_request_info() {
// return &SG(request_info);
//}
void * sg_server_context() {
return SG(server_context);
}
void sg_set_server_context(void *context) {
SG(server_context) = context;
}
//sapi_headers_struct * sg_sapi_headers() {
// return &SG(sapi_headers);
//}
unsigned char sg_headers_sent() {
return SG(headers_sent);
}
void sg_set_headers_sent(unsigned char is_sent) {
SG(headers_sent) = is_sent;
}
void zend_tsrmls_cache_update() {
ZEND_TSRMLS_CACHE_UPDATE();
}
/*
* Create a wrapper around fopen
*
* I tried to use libc, but libc defines FILE different from the php bindings
* generated by bindgen. Thus, it was easier to wrap this and expose it in Rust
* via an extern fn definition.
*/
FILE *phprpm_fopen(const char *filename, const char *mode) {
return fopen(filename, mode);
}