Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ["PHP-8.5"]
php: ["PHP-8.5", "PHP-8.6"]
rust: ["1.99.0"]
php_zts: [true, false]
php_debug: [true, false]
Expand Down Expand Up @@ -108,7 +108,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ["PHP-8.5"]
php: ["PHP-8.5", "PHP-8.6"]
rust: ["1.99.0"]
php_zts: [true, false]
php_debug: [true, false]
Expand Down
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ php-ast = "0.26.0"
php-rs-parser = "0.26.0"
regex = "1.13.1"
rsphp-build-tools = { version = "0.1.2", path = "./crates/build-tools" }
rsphp-compat = { version = "0.1.2", path = "./crates/compat" }
rsphp-macros-c = { version = "0.1.2", path = "./crates/macros-c" }
rsphp-macros-rs = { version = "0.1.2", path = "./crates/macros-rs" }
rsphp-stub-parser = { version = "0.1.2", path = "./crates/stub-parser" }
Expand All @@ -210,6 +211,7 @@ walkdir = "2.5.0"

[dependencies]
bstr = { workspace = true, optional = true }
rsphp-compat = { workspace = true }
rsphp-macros-c = { workspace = true }
rsphp-sys = { workspace = true }

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The primary `rsphp` crate contains utilities for use at runtime in PHP
extensions. This workplace also includes the following crates:

- `rsphp-build-tools`
- `rsphp-compat`
- `rsphp-macros-c`
- `rsphp-macros-rs`
- `rsphp-stub-parser`
Expand All @@ -30,6 +31,10 @@ testing.
The `rsphp-sys` crate provides bindings for PHP's C ABI, and the
`rsphp-build-tools` crate provides tools for generating those bindings.

The `rsphp-compat` crate provides compatability aliases for items with different
names across different PHP versions; these can be used to simplify supporting
multiple versions of PHP.

## Argument information

When using the `rsphp-stub-parser` crate for generating argument information
Expand Down
24 changes: 24 additions & 0 deletions crates/compat/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "rsphp-compat"
description = "Compatability definitions for supporting multiple PHP versions in RSPHP extensions"
categories = ["development-tools"]
authors.workspace = true
edition.workspace = true
keywords.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
version.workspace = true

[package.metadata.docs.rs]
# Show documentation for private items
rustdoc-args = ["--document-private-items"]

[lints]
workspace = true

[dependencies]
rsphp-sys = { workspace = true }

[build-dependencies]
rsphp-sys = { workspace = true }
6 changes: 6 additions & 0 deletions crates/compat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# rsphp-compat

Compatability aliases for supporting multiple versions of PHP in extensions, see
the main [rsphp] README for more information.

[rsphp]: https://github.com/DanielEScherzer/rsphp
9 changes: 9 additions & 0 deletions crates/compat/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! Build script for [`rsphp_compat`] that `php_is_86` if building for PHP 8.6.

fn main() {
println!("cargo::rustc-check-cfg=cfg(php_is_86)");

if rsphp_sys::PHP_MAJOR_VERSION == 8 && rsphp_sys::PHP_MINOR_VERSION == 6 {
println!("cargo:rustc-cfg=php_is_86");
}
}
11 changes: 11 additions & 0 deletions crates/compat/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//! Compatability aliases for things that change names in different PHP versions.

#[cfg(not(php_is_86))]
mod php_85;
#[cfg(not(php_is_86))]
pub use php_85::*;

#[cfg(php_is_86)]
mod php_86;
#[cfg(php_is_86)]
pub use php_86::*;
8 changes: 8 additions & 0 deletions crates/compat/src/php_85.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//! Aliases for PHP 8.5.

/// The result type.
pub type ZendResultType = rsphp_sys::zend_result;
/// A failure.
pub const ZEND_RESULT_FAILURE: ZendResultType = rsphp_sys::ZEND_RESULT_CODE::FAILURE;
/// A success.
pub const ZEND_RESULT_SUCCESS: ZendResultType = rsphp_sys::ZEND_RESULT_CODE::SUCCESS;
8 changes: 8 additions & 0 deletions crates/compat/src/php_86.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//! Aliases for PHP 8.6.

/// The result type.
pub type ZendResultType = rsphp_sys::zend_result::Type;
/// A failure.
pub const ZEND_RESULT_FAILURE: ZendResultType = rsphp_sys::zend_result::FAILURE;
/// A success.
pub const ZEND_RESULT_SUCCESS: ZendResultType = rsphp_sys::zend_result::SUCCESS;
11 changes: 8 additions & 3 deletions crates/macros-c/build.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
//! Build script for [`rsphp_macros_rs`] that automatically sets `php_zend_zts`
//! if built with [`rsphp_sys::USING_ZST`] being `1`, and `php_zend_debug` if
//! built with [`rsphp_sys::ZEND_DEBUG`] being `1`.
//! Build script for [`rsphp_macros_rs`] that automatically sets
//! - `php_zend_zts` if built with [`rsphp_sys::USING_ZST`] being `1`.
//! - `php_zend_debug` if built with [`rsphp_sys::ZEND_DEBUG`] being `1`.
//! - `php_is_86` if built for PHP 8.6.

fn main() {
println!("cargo::rustc-check-cfg=cfg(php_zend_debug)");
println!("cargo::rustc-check-cfg=cfg(php_zend_zts)");
println!("cargo::rustc-check-cfg=cfg(php_is_86)");

if rsphp_sys::ZEND_DEBUG == 1 {
println!("cargo:rustc-cfg=php_zend_debug");
}
if rsphp_sys::USING_ZTS == 1 {
println!("cargo:rustc-cfg=php_zend_zts");
}
if rsphp_sys::PHP_MAJOR_VERSION == 8 && rsphp_sys::PHP_MINOR_VERSION == 6 {
println!("cargo:rustc-cfg=php_is_86");
}
}
7 changes: 7 additions & 0 deletions crates/macros-c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ mod zend_api;
mod zend_closures;
mod zend_compile;
mod zend_constants;
mod zend_globals;
mod zend_globals_macros;
mod zend_hash;
mod zend_ini;
mod zend_object_handlers;
mod zend_string;
mod zend_types;

#[cfg(any(doc, all(php_zend_zts, php_is_86)))]
pub use zend_globals::ZEND_CG_OFFSET;

#[cfg(any(doc, all(php_zend_zts, php_is_86)))]
pub use zend_globals::ZEND_EG_OFFSET;
53 changes: 51 additions & 2 deletions crates/macros-c/src/tsrm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,44 @@
//! statically linked with PHP (i.e. that `ZEND_ENABLE_STATIC_TSRMLS_CACHE` is
//! not enabled for ZTS builds). Thus, `ZEND_TSRMG` is an alias for TSRMG.

use crate::c_version;
use crate::{c_differences, c_version};

const _: () = {
assert!(size_of::<usize>() == 8, "usize should be 8 bytes");
};
const _: () = {
assert!(size_of::<isize>() == 8, "isize should be 8 bytes");
};
/// Get an aligned size for TSRM.
#[doc = c_version!(
"# define TSRM_ALIGNED_SIZE(size)",
"(((size) + INT64_C(15)) & ~INT64_C(15))"
)]
#[doc = c_differences!(
"The C version is defined differently for `SIZEOF_SIZE_T` being 4 or 8, we \
assert that it is always 8. Only the C version for the size being 8 is \
shown above."
)]
#[macro_export]
#[cfg(any(doc, php_zend_zts))]
macro_rules! TSRM_ALIGNED_SIZE {
($size:expr) => {{
#[expect(
clippy::cast_possible_wrap,
clippy::as_conversions,
reason = "Matches C code"
)]
let size: i64 = $size as i64;
let result_64: i64 = (size + 15_i64) & (!15_i64);
#[expect(
clippy::cast_possible_truncation,
clippy::as_conversions,
reason = "Matches C code, isize is 8 bytes"
)]
let result: isize = result_64 as isize;
result
}};
}

/// TSRM id unshuffling.
#[doc = c_version!("#define TSRM_UNSHUFFLE_RSRC_ID(rsrc_id) ((rsrc_id)-1)")]
Expand Down Expand Up @@ -78,7 +115,7 @@ macro_rules! TSRMG_FAST {
/// Bulk lookup of an overall structure in the TSRM cache.
#[doc = c_version!("#define TSRMG_FAST_BULK(offset, type) ((type) (((char*) tsrm_get_ls_cache())+(offset)))")]
#[macro_export]
#[cfg(any(doc, php_zend_zts))]
#[cfg(any(doc, all(php_zend_zts, not(php_is_86))))]
macro_rules! TSRMG_FAST_BULK {
($offset:expr, $ty:ty) => {{
// These are unsafe and so the macro is unsafe
Expand All @@ -87,3 +124,15 @@ macro_rules! TSRMG_FAST_BULK {
at_offset.cast::<$ty>()
}};
}

/// Bulk lookup of an overall structure in the TSRM cache.
#[macro_export]
#[cfg(all(not(doc), php_zend_zts, php_is_86))]
macro_rules! TSRMG_FAST_BULK {
($offset:expr, $ty:ty) => {{
// These are unsafe and so the macro is unsafe
let cache_base: *mut std::ffi::c_void = rsphp_sys::tsrm_get_ls_cache();
let at_offset: *mut std::ffi::c_void = cache_base.byte_offset($offset);
at_offset.cast::<$ty>()
}};
}
71 changes: 56 additions & 15 deletions crates/macros-c/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,82 @@
/// Create a markdown string with the C version of a macro, for use in doc
/// attributes.
#[cfg_attr(
all(not(doc), php_zend_zts),
expect(
unused_macro_rules,
reason = "ZTS/NTS variants documented on the NTS version"
)
)]
macro_rules! c_version {
($code:expr) => {
c_version!(@overall c_version!(@code $code))
};
($line1:literal, $line2:literal) => {
c_version!(@overall c_version!(@code $line1, $line2))
};
(NTS => $nts:literal, ZTS => $zts:literal,) => {
(85 => $code:literal, 86 => None,) => {
c_version!(
@overall
concat!(
c_version!(@subsection "NTS" c_version!(@code $nts)),
c_version!(@subsection "PHP 8.5" c_version!(@code $code)),
"\n\n",
c_version!(@subsection "ZTS" c_version!(@code $zts)),
c_version!(@subsection "PHP 8.6" "The corresponding C macro was removed.")
)
)
};
(NTS => [$nts1:literal, $nts2:literal,], ZTS => [$zts1:literal, $zts2:literal,],) => {
(85 => [$( $line85:literal, )+], 86 => None,) => {
c_version!(
@overall
concat!(
c_version!(@subsection "NTS" c_version!(@code $nts1, $nts2)),
c_version!(@subsection "PHP 8.5" c_version!(@code $( $line85, )+ )),
"\n\n",
c_version!(@subsection "ZTS" c_version!(@code $zts1, $zts2)),
c_version!(@subsection "PHP 8.6" "The corresponding C macro was removed.")
)
)
};
(85 => [$( $line85:literal, )+], 86 => [$( $line86:literal, )+],) => {
c_version!(
@overall
concat!(
c_version!(@subsection "PHP 8.5" c_version!(@code $( $line85, )+ )),
"\n\n",
c_version!(@subsection "PHP 8.6" c_version!(@code $( $line86, )+ ))
)
)
};
(
85 => [
NTS => [$( $line85NTS:literal, )+],
ZTS => [$( $line85ZTS:literal, )+],
],
86 => [
NTS => [$( $line86NTS:literal, )+],
ZTS => [$( $line86ZTS:literal, )+],
],
) => {
c_version!(
@overall
concat!(
c_version!(
@subsection
"PHP 8.5"
concat!(
c_version!(@subsubsection "NTS" c_version!(@code $( $line85NTS, )+ ) ),
"\n\n",
c_version!(@subsubsection "ZTS" c_version!(@code $( $line85ZTS, )+ ) ),
)
),
"\n\n",
c_version!(
@subsection
"PHP 8.6"
concat!(
c_version!(@subsubsection "NTS" c_version!(@code $( $line86NTS, )+ ) ),
"\n\n",
c_version!(@subsubsection "ZTS" c_version!(@code $( $line86ZTS, )+ ) ),
)
),
)
)
};

(@code $code:expr) => {
(@code $code:expr $(,)?) => {
concat!("```c\n", $code, "\n", "```")
};
(@code $line1:literal, $line2:literal) => {
(@code $line1:literal, $line2:literal $(,)?) => {
c_version!(@code concat!($line1, " \\", "\n", " ", $line2))
};
(@overall $contents:expr) => {
Expand All @@ -47,6 +85,9 @@ macro_rules! c_version {
(@subsection $label:literal $contents:expr) => {
concat!("## ", $label, "\n", "\n", $contents)
};
(@subsubsection $label:literal $contents:expr) => {
concat!("### ", $label, "\n", "\n", $contents)
};
}

/// Creates a markdown string with differences from the C version.
Expand Down
Loading
Loading