Skip to content

Preliminary Windows support #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
ace0fea
Preliminary Windows support
davidcole1340 Mar 13, 2022
3990ad3
Start work on cross-platform build script
davidcole1340 Mar 13, 2022
91cee21
Fix compilation on macOS
davidcole1340 Mar 13, 2022
9c22179
Updated README, tidied up build script
davidcole1340 Mar 13, 2022
6b5bc6c
Check linker version before starting compilation
davidcole1340 Mar 13, 2022
69372c7
Switch to using Github repository for bindgen
davidcole1340 Mar 13, 2022
c91d7ae
Split Windows and Unix implementations into two files
davidcole1340 Mar 13, 2022
5303b1c
Fix building on Windows
davidcole1340 Mar 13, 2022
6343bc4
Remove `reqwest` and `zip` as dependencies on Unix
davidcole1340 Mar 13, 2022
a70f443
Fix guide tests on Windows
davidcole1340 Mar 14, 2022
ed423a8
Started work on Windows CI
davidcole1340 Mar 14, 2022
54e10f4
runs -> run
davidcole1340 Mar 14, 2022
7e52c1a
Use preinstalled LLVM on Windows
davidcole1340 Mar 14, 2022
84bb600
Debugging for Windows CI
davidcole1340 Mar 14, 2022
5629909
Switch to upstream `rust-bindgen` master branch
davidcole1340 Mar 15, 2022
6cdc28a
Switch to `rust-lld` for Windows linking
davidcole1340 Mar 15, 2022
75fb9a7
Don't compile `cargo-php` on Windows
davidcole1340 Mar 15, 2022
e38a711
Switch to using skeptic for tests
davidcole1340 Mar 15, 2022
89a1f14
cargo-php: Disable stub generation, fix ext install/remove
davidcole1340 Mar 16, 2022
9f80166
cargo-php: Fix on unix OS
davidcole1340 Mar 16, 2022
bc5143d
Fix clippy lint
davidcole1340 Mar 16, 2022
742cba0
Updated README
davidcole1340 Mar 16, 2022
bd13d44
Re-add CI for Unix + PHP 8.0
davidcole1340 Mar 16, 2022
843aaaa
Fix building on thread-safe PHP
davidcole1340 Mar 16, 2022
915e1bd
Tidy up build scripts
davidcole1340 Mar 16, 2022
07a2bad
Use dynamic lookup on Linux, test with TS Windows
davidcole1340 Mar 16, 2022
4c6342b
Define `ZTS` when compiling PHP ZTS
davidcole1340 Mar 16, 2022
733c658
Combine Windows and Unix CI, fix linking for Win32TS
davidcole1340 Mar 16, 2022
7d995f8
Fix exclusions in build CI
davidcole1340 Mar 16, 2022
b5070ce
rust-toolchain -> rust
davidcole1340 Mar 16, 2022
b65477f
Set LLVM version
davidcole1340 Mar 16, 2022
cd28d24
Only build docs.rs on Ubuntu PHP 8.1
davidcole1340 Mar 16, 2022
4787762
Fix build on Linux thread-safe
davidcole1340 Mar 17, 2022
ce45dc6
Update guide example
davidcole1340 Mar 17, 2022
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
2 changes: 0 additions & 2 deletions .cargo/config

This file was deleted.

8 changes: 8 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[target.'cfg(not(target_os = "windows"))']
rustflags = ["-C", "link-arg=-Wl,-undefined,dynamic_lookup"]

[target.x86_64-pc-windows-msvc]
linker = "rust-lld"

[target.i686-pc-windows-msvc]
linker = "rust-lld"
50 changes: 23 additions & 27 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,58 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
rust-toolchain:
- stable
- nightly
php:
- '8.0'
- '8.1'
llvm:
- '11.0'
os: [ubuntu-latest, macos-latest, windows-latest]
php: ['8.0', '8.1']
rust: [stable, nightly]
phpts: [ts, nts]
exclude:
# ext-php-rs requires nightly Rust when on Windows.
- os: windows-latest
rust: stable
# setup-php doesn't support thread safe PHP on Linux and macOS.
- os: macos-latest
phpts: ts
- os: ubuntu-latest
phpts: ts
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
env:
phpts: ${{ matrix.phpts }}
- name: Setup Rust
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust-toolchain }}
toolchain: ${{ matrix.rust }}
override: true
components: rustfmt, clippy
- name: Setup LLVM & Clang
if: "!contains(matrix.os, 'windows')"
id: clang
uses: KyleMayes/install-llvm-action@v1
with:
version: ${{ matrix.llvm }}
directory: ${{ runner.temp }}/llvm-${{ matrix.llvm }}
version: '13.0'
directory: ${{ runner.temp }}/llvm
- name: Configure Clang
if: "!contains(matrix.os, 'windows')"
run: |
echo "LIBCLANG_PATH=${{ runner.temp }}/llvm-${{ matrix.llvm }}/lib" >> $GITHUB_ENV
echo "LIBCLANG_PATH=${{ runner.temp }}/llvm/lib" >> $GITHUB_ENV
echo "LLVM_VERSION=${{ steps.clang.outputs.version }}" >> $GITHUB_ENV
- name: Configure Clang (macOS only)
if: "contains(matrix.os, 'macos')"
run: echo "SDKROOT=$(xcrun --show-sdk-path)" >> $GITHUB_ENV
- name: Install mdbook
uses: peaceiris/actions-mdbook@v1
with:
mdbook-version: latest
- name: Build
env:
EXT_PHP_RS_TEST:
run: cargo build --release --all-features --all
- name: Test guide examples
env:
CARGO_PKG_NAME: mdbook-tests
CARGO_PKG_VERSION: 0.1.0
run: |
mdbook test guide -L target/release/deps
- name: Test inline examples
uses: actions-rs/cargo@v1
with:
command: test
args: --release --all
args: --release --all --all-features
- name: Run rustfmt
uses: actions-rs/cargo@v1
with:
Expand All @@ -78,7 +74,7 @@ jobs:
command: clippy
args: --all -- -D warnings
- name: Build with docs stub
if: "contains(matrix.os, 'ubuntu') && ${{ matrix.php }} == '8.1'"
if: "contains(matrix.os, 'ubuntu') && matrix.php == '8.1'"
env:
DOCS_RS:
run:
Expand Down
14 changes: 12 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ once_cell = "1.8.0"
anyhow = { version = "1", optional = true }
ext-php-rs-derive = { version = "=0.7.3", path = "./crates/macros" }

[dev-dependencies]
skeptic = "0.13"

[build-dependencies]
bindgen = { version = "0.59" }
regex = "1"
anyhow = "1"
# bindgen = { version = "0.59" }
bindgen = { git = "https://github.com/rust-lang/rust-bindgen", branch = "master" }
cc = "1.0"
skeptic = "0.13"

[target.'cfg(windows)'.build-dependencies]
ureq = { version = "2.4", features = ["native-tls", "gzip"], default-features = false }
native-tls = "0.2"
zip = "0.5"

[features]
closure = []
Expand Down
60 changes: 43 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
# ext-php-rs

[<img align="right" src="https://discord.com/api/guilds/115233111977099271/widget.png?style=banner2">](https://discord.gg/dphp)
[![Crates.io](https://img.shields.io/crates/v/ext-php-rs)](https://lib.rs/ext-php-rs)
[![docs.rs](https://img.shields.io/docsrs/ext-php-rs/latest)](https://docs.rs/ext-php-rs)
[![Guide Workflow Status](https://img.shields.io/github/workflow/status/davidcole1340/ext-php-rs/Deploy%20documentation?label=guide)](https://davidcole1340.github.io/ext-php-rs)
![CI Workflow Status](https://img.shields.io/github/workflow/status/davidcole1340/ext-php-rs/Build%20and%20Lint)
[![Discord](https://img.shields.io/discord/115233111977099271)](https://discord.gg/dphp)

Bindings and abstractions for the Zend API to build PHP extensions natively in
Rust.

- Documentation: <https://docs.rs/ext-php-rs>
- Guide: <https://davidcole1340.github.io/ext-php-rs>

## Example

Export a simple function `function hello_world(string $name): string` to PHP:

```rust
#![cfg_attr(windows, feature(abi_vectorcall))]

use ext_php_rs::prelude::*;

/// Gives you a nice greeting!
Expand Down Expand Up @@ -104,16 +113,37 @@ best resource at the moment. This can be viewed at [docs.rs].

## Requirements

- PHP 8.0 or later
- No support is planned for lower versions.
- Linux or Darwin-based OS
- Rust - no idea which version
- Clang 3.9 or greater

See the following links for the dependency crate requirements:

- [`cc`](https://github.com/alexcrichton/cc-rs#compile-time-requirements)
- [`bindgen`](https://rust-lang.github.io/rust-bindgen/requirements.html)
- Linux, macOS or Windows-based operating system.
- PHP 8.0 or later.
- No support is planned for earlier versions of PHP.
- Rust.
- Currently, we maintain no guarantee of a MSRV, however lib.rs suggests Rust
1.57 at the time of writing.
- Clang 5.0 or later.

### Windows Requirements

- Extensions can only be compiled for PHP installations sourced from
<https://windows.php.net>. Support is planned for other installations
eventually.
- Rust nightly is required for Windows. This is due to the [vectorcall] calling
convention being used by some PHP functions on Windows, which is only
available as a nightly unstable feature in Rust.
- It is suggested to use the `rust-lld` linker to link your extension. The MSVC
linker (`link.exe`) is supported however you may run into issues if the linker
version is not supported by your PHP installation. You can use the `rust-lld`
linker by creating a `.cargo\config.toml` file with the following content:
```toml
# Replace target triple if you have a different architecture than x86_64
[target.x86_64-pc-windows-msvc]
linker = "rust-lld"
```
- The `cc` crate requires `cl.exe` to be present on your system. This is usually
bundled with Microsoft Visual Studio.
- `cargo-php`'s stub generation feature does not work on Windows. Rewriting this
functionality to be cross-platform is on the roadmap.

[vectorcall]: https://docs.microsoft.com/en-us/cpp/cpp/vectorcall?view=msvc-170

## Cargo Features

Expand All @@ -126,16 +156,12 @@ All features are disabled by default.

## Usage

This project only works for PHP >= 8.0 (for now). Due to the fact that the PHP
extension system relies heavily on C macros (which cannot be exported to Rust
easily), structs have to be hard coded in.

Check out one of the example projects:

- [anonaddy-sequoia](https://gitlab.com/willbrowning/anonaddy-sequoia) - Sequoia
encryption PHP extension.
- [opus-php](https://github.com/davidcole1340/opus-php) -
Audio encoder for the Opus codec in PHP.
- [opus-php](https://github.com/davidcole1340/opus-php) - Audio encoder for the
Opus codec in PHP.

## Contributions

Expand Down
21 changes: 12 additions & 9 deletions allowed_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ bind! {
_zend_new_array,
_zval_struct__bindgen_ty_1,
_zval_struct__bindgen_ty_2,
ext_php_rs_executor_globals,
ext_php_rs_php_build_id,
ext_php_rs_zend_object_alloc,
ext_php_rs_zend_object_release,
ext_php_rs_zend_string_init,
ext_php_rs_zend_string_release,
// ext_php_rs_executor_globals,
// ext_php_rs_php_build_id,
// ext_php_rs_zend_object_alloc,
// ext_php_rs_zend_object_release,
// ext_php_rs_zend_string_init,
// ext_php_rs_zend_string_release,
object_properties_init,
php_info_print_table_end,
php_info_print_table_header,
Expand Down Expand Up @@ -165,8 +165,8 @@ bind! {
ZEND_DEBUG,
ZEND_HAS_STATIC_IN_METHODS,
ZEND_ISEMPTY,
ZEND_MM_ALIGNMENT,
ZEND_MM_ALIGNMENT_MASK,
// ZEND_MM_ALIGNMENT,
// ZEND_MM_ALIGNMENT_MASK,
ZEND_MODULE_API_NO,
ZEND_PROPERTY_EXISTS,
ZEND_PROPERTY_ISSET,
Expand All @@ -189,10 +189,13 @@ bind! {
zend_standard_class_def,
zend_class_serialize_deny,
zend_class_unserialize_deny,
zend_executor_globals,
zend_objects_store_del,
gc_possible_root,
ZEND_ACC_NOT_SERIALIZABLE,
executor_globals,
php_printf,
__zend_malloc
__zend_malloc,
tsrm_get_ls_cache,
executor_globals_offset
}
Loading