ext-php-rs/build.rs

160 lines
5.0 KiB
Rust
Raw Normal View History

Implemented classes (#17) * Add PHP thread safety to build matrix * Revert "Add PHP thread safety to build matrix" This reverts commit 7de868e6b6e329b87c412af8daf61cba1a3e29c5. * Fixed arguments not being reconized by PHP * Added function to create interned Zend string * Changed statup/shutdown function types to Rust types Prevents users having to import from bindings * Added flags for classes * WIP: class builder * Added properties to classes * Added class constants * Added PHP base extensions Added class inheritance * Fixed memory leak with constants Created C wrapper which is compiled and linked in the `build.rs` script. In the process removed the `build_id()` function as now we can just export the macro via a function which is defined in `wrapper.c`. * Fixed lint issue * Added functions to set zval as different strings Regular strings, persistent strings and interned strings * Updated README with requirements * Fixed lint issues * Changed properties to use proper function * Free zend string when it is changed for a persistent one * WIP: overriding zend objects * Added comments to the top of modules. * Fixed some comments * Initialize hash map with initial size when converting from HashMap and Vec * Removed unused imports * WIP: object overrides * Added DerefMut implementation for ZendClassObject Tidied up lints that clippy was complaining about, removed debugging statements. * Fixed pointer dereference error in macro * Added missing comments for trait * All functions now take mutable references vs ptr Removed SetZval implementation for *mut Zval. Made bindings public, removed duplicate bindings. Fixed `create_object` handler not working. Note to self: smaller commits. * Showcase ability to use shared object * WIP: debugging property errors Changed `function` to `method` in classes Fixed doctest for `c_str` * Disabled class properties temporarily See #16 * Initialize args array with size
2021-04-02 03:18:45 +02:00
use std::{
env,
path::{Path, PathBuf},
process::Command,
str,
Implemented classes (#17) * Add PHP thread safety to build matrix * Revert "Add PHP thread safety to build matrix" This reverts commit 7de868e6b6e329b87c412af8daf61cba1a3e29c5. * Fixed arguments not being reconized by PHP * Added function to create interned Zend string * Changed statup/shutdown function types to Rust types Prevents users having to import from bindings * Added flags for classes * WIP: class builder * Added properties to classes * Added class constants * Added PHP base extensions Added class inheritance * Fixed memory leak with constants Created C wrapper which is compiled and linked in the `build.rs` script. In the process removed the `build_id()` function as now we can just export the macro via a function which is defined in `wrapper.c`. * Fixed lint issue * Added functions to set zval as different strings Regular strings, persistent strings and interned strings * Updated README with requirements * Fixed lint issues * Changed properties to use proper function * Free zend string when it is changed for a persistent one * WIP: overriding zend objects * Added comments to the top of modules. * Fixed some comments * Initialize hash map with initial size when converting from HashMap and Vec * Removed unused imports * WIP: object overrides * Added DerefMut implementation for ZendClassObject Tidied up lints that clippy was complaining about, removed debugging statements. * Fixed pointer dereference error in macro * Added missing comments for trait * All functions now take mutable references vs ptr Removed SetZval implementation for *mut Zval. Made bindings public, removed duplicate bindings. Fixed `create_object` handler not working. Note to self: smaller commits. * Showcase ability to use shared object * WIP: debugging property errors Changed `function` to `method` in classes Fixed doctest for `c_str` * Disabled class properties temporarily See #16 * Initialize args array with size
2021-04-02 03:18:45 +02:00
};
2021-03-09 00:40:12 +01:00
use regex::Regex;
2021-03-09 00:40:12 +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
println!("cargo:rerun-if-changed=src/wrapper.h");
println!("cargo:rerun-if-changed=src/wrapper.c");
2021-03-09 00:40:12 +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);
}
// Ensure the PHP API version is supported.
// We could easily use grep and sed here but eventually we want to support
// Windows, so it's easier to just use regex.
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() {
let stderr = str::from_utf8(&includes_cmd.stderr).unwrap_or("Unable to read stderr");
panic!("Error running `php -i`: {}", stderr);
}
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`.");
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);
}
let includes =
String::from_utf8(includes_cmd.stdout).expect("unable to parse `php-config` stdout");
2021-03-09 00:40:12 +01:00
Implemented classes (#17) * Add PHP thread safety to build matrix * Revert "Add PHP thread safety to build matrix" This reverts commit 7de868e6b6e329b87c412af8daf61cba1a3e29c5. * Fixed arguments not being reconized by PHP * Added function to create interned Zend string * Changed statup/shutdown function types to Rust types Prevents users having to import from bindings * Added flags for classes * WIP: class builder * Added properties to classes * Added class constants * Added PHP base extensions Added class inheritance * Fixed memory leak with constants Created C wrapper which is compiled and linked in the `build.rs` script. In the process removed the `build_id()` function as now we can just export the macro via a function which is defined in `wrapper.c`. * Fixed lint issue * Added functions to set zval as different strings Regular strings, persistent strings and interned strings * Updated README with requirements * Fixed lint issues * Changed properties to use proper function * Free zend string when it is changed for a persistent one * WIP: overriding zend objects * Added comments to the top of modules. * Fixed some comments * Initialize hash map with initial size when converting from HashMap and Vec * Removed unused imports * WIP: object overrides * Added DerefMut implementation for ZendClassObject Tidied up lints that clippy was complaining about, removed debugging statements. * Fixed pointer dereference error in macro * Added missing comments for trait * All functions now take mutable references vs ptr Removed SetZval implementation for *mut Zval. Made bindings public, removed duplicate bindings. Fixed `create_object` handler not working. Note to self: smaller commits. * Showcase ability to use shared object * WIP: debugging property errors Changed `function` to `method` in classes Fixed doctest for `c_str` * Disabled class properties temporarily See #16 * Initialize args array with size
2021-04-02 03:18:45 +02:00
// Build `wrapper.c` and link to Rust.
cc::Build::new()
.file("src/wrapper.c")
Implemented classes (#17) * Add PHP thread safety to build matrix * Revert "Add PHP thread safety to build matrix" This reverts commit 7de868e6b6e329b87c412af8daf61cba1a3e29c5. * Fixed arguments not being reconized by PHP * Added function to create interned Zend string * Changed statup/shutdown function types to Rust types Prevents users having to import from bindings * Added flags for classes * WIP: class builder * Added properties to classes * Added class constants * Added PHP base extensions Added class inheritance * Fixed memory leak with constants Created C wrapper which is compiled and linked in the `build.rs` script. In the process removed the `build_id()` function as now we can just export the macro via a function which is defined in `wrapper.c`. * Fixed lint issue * Added functions to set zval as different strings Regular strings, persistent strings and interned strings * Updated README with requirements * Fixed lint issues * Changed properties to use proper function * Free zend string when it is changed for a persistent one * WIP: overriding zend objects * Added comments to the top of modules. * Fixed some comments * Initialize hash map with initial size when converting from HashMap and Vec * Removed unused imports * WIP: object overrides * Added DerefMut implementation for ZendClassObject Tidied up lints that clippy was complaining about, removed debugging statements. * Fixed pointer dereference error in macro * Added missing comments for trait * All functions now take mutable references vs ptr Removed SetZval implementation for *mut Zval. Made bindings public, removed duplicate bindings. Fixed `create_object` handler not working. Note to self: smaller commits. * Showcase ability to use shared object * WIP: debugging property errors Changed `function` to `method` in classes Fixed doctest for `c_str` * Disabled class properties temporarily See #16 * Initialize args array with size
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),
Implemented classes (#17) * Add PHP thread safety to build matrix * Revert "Add PHP thread safety to build matrix" This reverts commit 7de868e6b6e329b87c412af8daf61cba1a3e29c5. * Fixed arguments not being reconized by PHP * Added function to create interned Zend string * Changed statup/shutdown function types to Rust types Prevents users having to import from bindings * Added flags for classes * WIP: class builder * Added properties to classes * Added class constants * Added PHP base extensions Added class inheritance * Fixed memory leak with constants Created C wrapper which is compiled and linked in the `build.rs` script. In the process removed the `build_id()` function as now we can just export the macro via a function which is defined in `wrapper.c`. * Fixed lint issue * Added functions to set zval as different strings Regular strings, persistent strings and interned strings * Updated README with requirements * Fixed lint issues * Changed properties to use proper function * Free zend string when it is changed for a persistent one * WIP: overriding zend objects * Added comments to the top of modules. * Fixed some comments * Initialize hash map with initial size when converting from HashMap and Vec * Removed unused imports * WIP: object overrides * Added DerefMut implementation for ZendClassObject Tidied up lints that clippy was complaining about, removed debugging statements. * Fixed pointer dereference error in macro * Added missing comments for trait * All functions now take mutable references vs ptr Removed SetZval implementation for *mut Zval. Made bindings public, removed duplicate bindings. Fixed `create_object` handler not working. Note to self: smaller commits. * Showcase ability to use shared object * WIP: debugging property errors Changed `function` to `method` in classes Fixed doctest for `c_str` * Disabled class properties temporarily See #16 * Initialize args array with size
2021-04-02 03:18:45 +02:00
)
.compile("wrapper");
let mut bindgen = bindgen::Builder::default()
.header("src/wrapper.h")
.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")
.no_copy("_zend_string")
.no_copy("_zend_array")
.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.");
let configure = Configure::get();
if configure.has_zts() {
println!("cargo:rustc-cfg=php_zts");
}
if configure.debug() {
println!("cargo:rustc-cfg=php_debug");
}
}
struct Configure(String);
impl Configure {
pub fn get() -> Self {
let cmd = Command::new("php-config")
.arg("--configure-options")
.output()
.expect("Unable to run `php-config --configure-options`. Please ensure it is visible in your PATH.");
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)
}
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
}
// Mock macro for the `allowed_bindings.rs` script.
macro_rules! bind {
($($s: ident),*) => {
&[$(
stringify!($s),
)*]
}
}
/// 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).
const ALLOWED_BINDINGS: &[&str] = include!("allowed_bindings.rs");