2021-04-02 03:18:45 +02:00
use std ::{
env ,
path ::{ Path , PathBuf } ,
process ::Command ,
2021-09-05 07:56:29 +02:00
str ,
2021-04-02 03:18:45 +02:00
} ;
2021-03-09 00:40:12 +01:00
2021-09-05 07:56:29 +02:00
use regex ::Regex ;
2021-03-09 00:40:12 +01:00
2021-03-11 00:34:47 +01:00
const MIN_PHP_API_VER : u32 = 20200930 ;
const MAX_PHP_API_VER : u32 = 20200930 ;
2021-03-09 00:40:12 +01:00
fn main ( ) {
// rerun if wrapper header is changed
2021-10-10 06:51:55 +02:00
println! ( " cargo:rerun-if-changed=src/wrapper.h " ) ;
println! ( " cargo:rerun-if-changed=src/wrapper.c " ) ;
2021-03-09 00:40:12 +01:00
2021-11-20 02:19:02 +01:00
let out_dir = env ::var_os ( " OUT_DIR " ) . expect ( " Failed to get OUT_DIR " ) ;
let out_path = PathBuf ::from ( out_dir ) . join ( " bindings.rs " ) ;
2021-09-05 08:18:09 +02:00
// check for docs.rs and use stub bindings if required
if env ::var ( " DOCS_RS " ) . is_ok ( ) {
println! ( " cargo:warning=docs.rs detected - using stub bindings " ) ;
println! ( " cargo:rustc-cfg=php_debug " ) ;
std ::fs ::copy ( " docsrs_bindings.rs " , out_path )
. expect ( " Unable to copy docs.rs stub bindings to output directory. " ) ;
return ;
}
2021-03-09 00:40:12 +01:00
// use php-config to fetch includes
let includes_cmd = Command ::new ( " php-config " )
. arg ( " --includes " )
. output ( )
. expect ( " Unable to run `php-config`. Please ensure it is visible in your PATH. " ) ;
if ! includes_cmd . status . success ( ) {
let stderr = String ::from_utf8 ( includes_cmd . stderr )
. unwrap_or_else ( | _ | String ::from ( " Unable to read stderr " ) ) ;
panic! ( " Error running `php-config`: {} " , stderr ) ;
}
2021-03-11 00:34:47 +01:00
// Ensure the PHP API version is supported.
2021-10-10 06:51:55 +02:00
// We could easily use grep and sed here but eventually we want to support
// Windows, so it's easier to just use regex.
2021-03-11 00:34:47 +01:00
let php_i_cmd = Command ::new ( " php " )
. arg ( " -i " )
. output ( )
. expect ( " Unable to run `php -i`. Please ensure it is visible in your PATH. " ) ;
if ! php_i_cmd . status . success ( ) {
2021-09-05 07:56:29 +02:00
let stderr = str ::from_utf8 ( & includes_cmd . stderr ) . unwrap_or ( " Unable to read stderr " ) ;
2021-03-11 00:34:47 +01:00
panic! ( " Error running `php -i`: {} " , stderr ) ;
}
2021-09-05 07:56:29 +02:00
let api_ver = Regex ::new ( r "PHP API => ([0-9]+)" )
. unwrap ( )
. captures_iter (
str ::from_utf8 ( & php_i_cmd . stdout ) . expect ( " Unable to parse `php -i` stdout as UTF-8 " ) ,
)
. next ( )
. and_then ( | ver | ver . get ( 1 ) )
. and_then ( | ver | ver . as_str ( ) . parse ::< u32 > ( ) . ok ( ) )
. expect ( " Unable to retrieve PHP API version from `php -i`. " ) ;
2021-03-11 00:34:47 +01:00
2021-09-05 07:56:29 +02:00
if api_ver < MIN_PHP_API_VER | | api_ver > MAX_PHP_API_VER {
panic! ( " The current version of PHP is not supported. Current PHP API version: {} , requires a version between {} and {} " , api_ver , MIN_PHP_API_VER , MAX_PHP_API_VER ) ;
}
2021-03-11 00:34:47 +01:00
2021-03-09 08:47:20 +01:00
let includes =
String ::from_utf8 ( includes_cmd . stdout ) . expect ( " unable to parse `php-config` stdout " ) ;
2021-03-09 00:40:12 +01:00
2021-04-02 03:18:45 +02:00
// Build `wrapper.c` and link to Rust.
cc ::Build ::new ( )
2021-10-10 06:51:55 +02:00
. file ( " src/wrapper.c " )
2021-04-02 03:18:45 +02:00
. includes (
str ::replace ( includes . as_ref ( ) , " -I " , " " )
. split ( ' ' )
2021-11-05 11:45:58 +01:00
. map ( Path ::new ) ,
2021-04-02 03:18:45 +02:00
)
. compile ( " wrapper " ) ;
2021-09-05 07:56:29 +02:00
let mut bindgen = bindgen ::Builder ::default ( )
2021-10-10 06:51:55 +02:00
. header ( " src/wrapper.h " )
2021-03-11 01:41:05 +01:00
. clang_args ( includes . split ( ' ' ) )
2021-03-09 00:40:12 +01:00
. parse_callbacks ( Box ::new ( bindgen ::CargoCallbacks ) )
. rustfmt_bindings ( true )
Add abstraction macros (#49)
* Remove uses of `unwrap`, improve library safety
* Started work on `#[php_function]` attribute
* Added `PhantomData` to `ZendHashTable`
Proper lifetimes for `HashTable` type
* `#[php_function]` now accepts `Vec<T>`
Refactored `ZendHashTable` iterators - there is now `Iter` and
`IntoIter` depending on whether it will consume the HashTable or not.
* Add support for nullable types
* Allow `optional` parameter on attribute
* Support primitive and `Option` return types
* Tidied and refactored attribute
* Added documentation for `#[php_function]`
Implemented `IntoZval` for `Option<T>` when `IntoZval` is also
implemented for `T`. `None` resolves to `null`.
* Added `#[php_method]` attribute
* Added `Callable` type, implemented `Drop` on `Zval`
While implementing `Drop`, the `Copy` derivation was removed, however,
this should not have been there in the first place (`Zval` is not valid
for `Copy` if it contains a string).
* Added some macro functions to example
* Add support for boolean arguments
* Added wrapper around binary data
Future support for binary arguments with `#[php_function]` macros.
Unpacking binary data is no longer unsafe. It was never really unsafe in
the beginning, as we were always reading valid memory, just the contents
of the data could not be trusted.
* Added `#[php_module]` attribute, added support for binary arguments
* Added defaults for functions
* Add defaults for methods
* Add startup function macro and prelude
* Refactored method adding
Now done through impl attribute
* Don't rename functions - generate another internal fn
Also hide all generated functions from docs
* Generate startup function when not already defined
* Add support for class and global constants
* Updated `skel` project
* Updated macro documentation, added executor globals
* Remove `Copy` bound for HashMap to Zval
* Updated documentation
* Add `FromZval` trait, updated docs
* Fixed clippy lints
* Fixed ZTS executor globals
* Fix clippy lint
2021-08-18 03:15:25 +02:00
. no_copy ( " _zend_value " )
2021-09-27 13:33:16 +02:00
. no_copy ( " _zend_string " )
2021-10-05 05:59:41 +02:00
. no_copy ( " _zend_array " )
2021-09-05 07:56:29 +02:00
. layout_tests ( env ::var ( " EXT_PHP_RS_TEST " ) . is_ok ( ) ) ;
for binding in ALLOWED_BINDINGS . iter ( ) {
bindgen = bindgen
. allowlist_function ( binding )
. allowlist_type ( binding )
. allowlist_var ( binding ) ;
}
bindgen
2021-03-09 00:40:12 +01:00
. generate ( )
. expect ( " Unable to generate bindings for PHP " )
2021-09-05 08:18:09 +02:00
. write_to_file ( out_path )
2021-03-09 00:40:12 +01:00
. expect ( " Unable to write bindings file. " ) ;
2021-05-19 10:45:39 +02:00
2021-09-04 14:53:29 +02:00
let configure = Configure ::get ( ) ;
if configure . has_zts ( ) {
2021-09-05 04:49:50 +02:00
println! ( " cargo:rustc-cfg=php_zts " ) ;
2021-05-19 10:45:39 +02:00
}
2021-09-04 14:53:29 +02:00
if configure . debug ( ) {
2021-09-05 04:49:50 +02:00
println! ( " cargo:rustc-cfg=php_debug " ) ;
2021-09-04 14:53:29 +02:00
}
2021-05-19 10:45:39 +02:00
}
2021-09-04 14:53:29 +02:00
struct Configure ( String ) ;
impl Configure {
pub fn get ( ) -> Self {
let cmd = Command ::new ( " php-config " )
2021-05-19 10:45:39 +02:00
. arg ( " --configure-options " )
. output ( )
. expect ( " Unable to run `php-config --configure-options`. Please ensure it is visible in your PATH. " ) ;
2021-09-04 14:53:29 +02:00
if ! cmd . status . success ( ) {
let stderr = String ::from_utf8 ( cmd . stderr )
. unwrap_or_else ( | _ | String ::from ( " Unable to read stderr " ) ) ;
panic! ( " Error running `php -i`: {} " , stderr ) ;
}
// check for the ZTS feature flag in configure
let stdout =
String ::from_utf8 ( cmd . stdout ) . expect ( " Unable to read stdout from `php-config`. " ) ;
Self ( stdout )
2021-05-19 10:45:39 +02:00
}
2021-09-04 14:53:29 +02:00
pub fn has_zts ( & self ) -> bool {
self . 0. contains ( " --enable-zts " )
}
pub fn debug ( & self ) -> bool {
self . 0. contains ( " --enable-debug " )
}
2021-03-09 00:40:12 +01:00
}
2021-09-05 07:56:29 +02:00
2021-11-20 02:19:02 +01:00
// Mock macro for the `allowed_bindings.rs` script.
macro_rules ! bind {
( $( $s : ident ) , * ) = > {
& [ $(
stringify! ( $s ) ,
) * ]
}
}
2021-10-10 06:51:55 +02:00
/// Array of functions/types used in `ext-php-rs` - used to allowlist when
/// generating bindings, as we don't want to generate bindings for everything
/// (i.e. stdlib headers).
2021-11-20 02:19:02 +01:00
const ALLOWED_BINDINGS : & [ & str ] = include! ( " allowed_bindings.rs " ) ;