Skip to content

Commit 664981f

Browse files
Windows support (#128)
* Preliminary Windows support * Start work on cross-platform build script * Fix compilation on macOS * Updated README, tidied up build script * Check linker version before starting compilation It doesn't seem like it's possible to change the linker from within the build script, however, we can retrieve the linker in use and give the user a suggestion if the linker will not work. * Switch to using Github repository for bindgen * Split Windows and Unix implementations into two files * Fix building on Windows * Remove `reqwest` and `zip` as dependencies on Unix * Fix guide tests on Windows * Started work on Windows CI * runs -> run * Use preinstalled LLVM on Windows * Debugging for Windows CI * Switch to upstream `rust-bindgen` master branch * Switch to `rust-lld` for Windows linking * Don't compile `cargo-php` on Windows * Switch to using skeptic for tests * cargo-php: Disable stub generation, fix ext install/remove The plan is to replace the stub generation by generating them with PHP code. This is cross-platform and means we don't need to worry about ABI. We also don't need to embed information into the library. * cargo-php: Fix on unix OS * Fix clippy lint * Updated README * Re-add CI for Unix + PHP 8.0 * Fix building on thread-safe PHP * Tidy up build scripts * Use dynamic lookup on Linux, test with TS Windows * Define `ZTS` when compiling PHP ZTS * Combine Windows and Unix CI, fix linking for Win32TS * Fix exclusions in build CI * rust-toolchain -> rust * Set LLVM version * Only build docs.rs on Ubuntu PHP 8.1 * Fix build on Linux thread-safe * Update guide example
1 parent 7520720 commit 664981f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1006
-371
lines changed

.cargo/config

Lines changed: 0 additions & 2 deletions
This file was deleted.

.cargo/config.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[target.'cfg(not(target_os = "windows"))']
2+
rustflags = ["-C", "link-arg=-Wl,-undefined,dynamic_lookup"]
3+
4+
[target.x86_64-pc-windows-msvc]
5+
linker = "rust-lld"
6+
7+
[target.i686-pc-windows-msvc]
8+
linker = "rust-lld"

.github/workflows/build.yml

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,58 @@ jobs:
1111
runs-on: ${{ matrix.os }}
1212
strategy:
1313
matrix:
14-
os:
15-
- ubuntu-latest
16-
- macos-latest
17-
rust-toolchain:
18-
- stable
19-
- nightly
20-
php:
21-
- '8.0'
22-
- '8.1'
23-
llvm:
24-
- '11.0'
14+
os: [ubuntu-latest, macos-latest, windows-latest]
15+
php: ['8.0', '8.1']
16+
rust: [stable, nightly]
17+
phpts: [ts, nts]
18+
exclude:
19+
# ext-php-rs requires nightly Rust when on Windows.
20+
- os: windows-latest
21+
rust: stable
22+
# setup-php doesn't support thread safe PHP on Linux and macOS.
23+
- os: macos-latest
24+
phpts: ts
25+
- os: ubuntu-latest
26+
phpts: ts
2527
steps:
2628
- name: Checkout code
2729
uses: actions/checkout@v2
2830
- name: Setup PHP
2931
uses: shivammathur/setup-php@v2
3032
with:
3133
php-version: ${{ matrix.php }}
34+
env:
35+
phpts: ${{ matrix.phpts }}
3236
- name: Setup Rust
3337
uses: actions-rs/toolchain@v1
3438
with:
35-
toolchain: ${{ matrix.rust-toolchain }}
39+
toolchain: ${{ matrix.rust }}
3640
override: true
3741
components: rustfmt, clippy
3842
- name: Setup LLVM & Clang
43+
if: "!contains(matrix.os, 'windows')"
3944
id: clang
4045
uses: KyleMayes/install-llvm-action@v1
4146
with:
42-
version: ${{ matrix.llvm }}
43-
directory: ${{ runner.temp }}/llvm-${{ matrix.llvm }}
47+
version: '13.0'
48+
directory: ${{ runner.temp }}/llvm
4449
- name: Configure Clang
50+
if: "!contains(matrix.os, 'windows')"
4551
run: |
46-
echo "LIBCLANG_PATH=${{ runner.temp }}/llvm-${{ matrix.llvm }}/lib" >> $GITHUB_ENV
52+
echo "LIBCLANG_PATH=${{ runner.temp }}/llvm/lib" >> $GITHUB_ENV
4753
echo "LLVM_VERSION=${{ steps.clang.outputs.version }}" >> $GITHUB_ENV
4854
- name: Configure Clang (macOS only)
4955
if: "contains(matrix.os, 'macos')"
5056
run: echo "SDKROOT=$(xcrun --show-sdk-path)" >> $GITHUB_ENV
51-
- name: Install mdbook
52-
uses: peaceiris/actions-mdbook@v1
53-
with:
54-
mdbook-version: latest
5557
- name: Build
5658
env:
5759
EXT_PHP_RS_TEST:
5860
run: cargo build --release --all-features --all
59-
- name: Test guide examples
60-
env:
61-
CARGO_PKG_NAME: mdbook-tests
62-
CARGO_PKG_VERSION: 0.1.0
63-
run: |
64-
mdbook test guide -L target/release/deps
6561
- name: Test inline examples
6662
uses: actions-rs/cargo@v1
6763
with:
6864
command: test
69-
args: --release --all
65+
args: --release --all --all-features
7066
- name: Run rustfmt
7167
uses: actions-rs/cargo@v1
7268
with:
@@ -78,7 +74,7 @@ jobs:
7874
command: clippy
7975
args: --all -- -D warnings
8076
- name: Build with docs stub
81-
if: "contains(matrix.os, 'ubuntu') && ${{ matrix.php }} == '8.1'"
77+
if: "contains(matrix.os, 'ubuntu') && matrix.php == '8.1'"
8278
env:
8379
DOCS_RS:
8480
run:

Cargo.toml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,20 @@ once_cell = "1.8.0"
1919
anyhow = { version = "1", optional = true }
2020
ext-php-rs-derive = { version = "=0.7.4", path = "./crates/macros" }
2121

22+
[dev-dependencies]
23+
skeptic = "0.13"
24+
2225
[build-dependencies]
23-
bindgen = { version = "0.59" }
24-
regex = "1"
26+
anyhow = "1"
27+
# bindgen = { version = "0.59" }
28+
bindgen = { git = "https://github.com/rust-lang/rust-bindgen", branch = "master" }
2529
cc = "1.0"
30+
skeptic = "0.13"
31+
32+
[target.'cfg(windows)'.build-dependencies]
33+
ureq = { version = "2.4", features = ["native-tls", "gzip"], default-features = false }
34+
native-tls = "0.2"
35+
zip = "0.5"
2636

2737
[features]
2838
closure = []

README.md

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
# ext-php-rs
22

3-
[<img align="right" src="https://discord.com/api/guilds/115233111977099271/widget.png?style=banner2">](https://discord.gg/dphp)
3+
[![Crates.io](https://img.shields.io/crates/v/ext-php-rs)](https://lib.rs/ext-php-rs)
4+
[![docs.rs](https://img.shields.io/docsrs/ext-php-rs/latest)](https://docs.rs/ext-php-rs)
5+
[![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)
6+
![CI Workflow Status](https://img.shields.io/github/workflow/status/davidcole1340/ext-php-rs/Build%20and%20Lint)
7+
[![Discord](https://img.shields.io/discord/115233111977099271)](https://discord.gg/dphp)
48

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

12+
- Documentation: <https://docs.rs/ext-php-rs>
13+
- Guide: <https://davidcole1340.github.io/ext-php-rs>
14+
815
## Example
916

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

1219
```rust
20+
#![cfg_attr(windows, feature(abi_vectorcall))]
21+
1322
use ext_php_rs::prelude::*;
1423

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

105114
## Requirements
106115

107-
- PHP 8.0 or later
108-
- No support is planned for lower versions.
109-
- Linux or Darwin-based OS
110-
- Rust - no idea which version
111-
- Clang 3.9 or greater
112-
113-
See the following links for the dependency crate requirements:
114-
115-
- [`cc`](https://github.com/alexcrichton/cc-rs#compile-time-requirements)
116-
- [`bindgen`](https://rust-lang.github.io/rust-bindgen/requirements.html)
116+
- Linux, macOS or Windows-based operating system.
117+
- PHP 8.0 or later.
118+
- No support is planned for earlier versions of PHP.
119+
- Rust.
120+
- Currently, we maintain no guarantee of a MSRV, however lib.rs suggests Rust
121+
1.57 at the time of writing.
122+
- Clang 5.0 or later.
123+
124+
### Windows Requirements
125+
126+
- Extensions can only be compiled for PHP installations sourced from
127+
<https://windows.php.net>. Support is planned for other installations
128+
eventually.
129+
- Rust nightly is required for Windows. This is due to the [vectorcall] calling
130+
convention being used by some PHP functions on Windows, which is only
131+
available as a nightly unstable feature in Rust.
132+
- It is suggested to use the `rust-lld` linker to link your extension. The MSVC
133+
linker (`link.exe`) is supported however you may run into issues if the linker
134+
version is not supported by your PHP installation. You can use the `rust-lld`
135+
linker by creating a `.cargo\config.toml` file with the following content:
136+
```toml
137+
# Replace target triple if you have a different architecture than x86_64
138+
[target.x86_64-pc-windows-msvc]
139+
linker = "rust-lld"
140+
```
141+
- The `cc` crate requires `cl.exe` to be present on your system. This is usually
142+
bundled with Microsoft Visual Studio.
143+
- `cargo-php`'s stub generation feature does not work on Windows. Rewriting this
144+
functionality to be cross-platform is on the roadmap.
145+
146+
[vectorcall]: https://docs.microsoft.com/en-us/cpp/cpp/vectorcall?view=msvc-170
117147

118148
## Cargo Features
119149

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

127157
## Usage
128158

129-
This project only works for PHP >= 8.0 (for now). Due to the fact that the PHP
130-
extension system relies heavily on C macros (which cannot be exported to Rust
131-
easily), structs have to be hard coded in.
132-
133159
Check out one of the example projects:
134160

135161
- [anonaddy-sequoia](https://gitlab.com/willbrowning/anonaddy-sequoia) - Sequoia
136162
encryption PHP extension.
137-
- [opus-php](https://github.com/davidcole1340/opus-php) -
138-
Audio encoder for the Opus codec in PHP.
163+
- [opus-php](https://github.com/davidcole1340/opus-php) - Audio encoder for the
164+
Opus codec in PHP.
139165

140166
## Contributions
141167

allowed_bindings.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ bind! {
2323
_zend_new_array,
2424
_zval_struct__bindgen_ty_1,
2525
_zval_struct__bindgen_ty_2,
26-
ext_php_rs_executor_globals,
27-
ext_php_rs_php_build_id,
28-
ext_php_rs_zend_object_alloc,
29-
ext_php_rs_zend_object_release,
30-
ext_php_rs_zend_string_init,
31-
ext_php_rs_zend_string_release,
26+
// ext_php_rs_executor_globals,
27+
// ext_php_rs_php_build_id,
28+
// ext_php_rs_zend_object_alloc,
29+
// ext_php_rs_zend_object_release,
30+
// ext_php_rs_zend_string_init,
31+
// ext_php_rs_zend_string_release,
3232
object_properties_init,
3333
php_info_print_table_end,
3434
php_info_print_table_header,
@@ -165,8 +165,8 @@ bind! {
165165
ZEND_DEBUG,
166166
ZEND_HAS_STATIC_IN_METHODS,
167167
ZEND_ISEMPTY,
168-
ZEND_MM_ALIGNMENT,
169-
ZEND_MM_ALIGNMENT_MASK,
168+
// ZEND_MM_ALIGNMENT,
169+
// ZEND_MM_ALIGNMENT_MASK,
170170
ZEND_MODULE_API_NO,
171171
ZEND_PROPERTY_EXISTS,
172172
ZEND_PROPERTY_ISSET,
@@ -189,10 +189,13 @@ bind! {
189189
zend_standard_class_def,
190190
zend_class_serialize_deny,
191191
zend_class_unserialize_deny,
192+
zend_executor_globals,
192193
zend_objects_store_del,
193194
gc_possible_root,
194195
ZEND_ACC_NOT_SERIALIZABLE,
195196
executor_globals,
196197
php_printf,
197-
__zend_malloc
198+
__zend_malloc,
199+
tsrm_get_ls_cache,
200+
executor_globals_offset
198201
}

0 commit comments

Comments
 (0)