Skip to content

Commit 1404949

Browse files
authored
feat(ffi): allow definging additional bindings
Add environment variable `EXT_PHP_RS_ALLOWED_BINDINGS` to specify bindings that should be included in addition to `allowed_bindings.rs`. Refs: #403
1 parent 1242e4d commit 1404949

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

build.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,16 @@ fn generate_bindings(defines: &[(&str, &str)], includes: &[PathBuf]) -> Result<S
209209
.allowlist_var(binding);
210210
}
211211

212+
let extension_allowed_bindings = env::var("EXT_PHP_RS_ALLOWED_BINDINGS").ok();
213+
if let Some(extension_allowed_bindings) = extension_allowed_bindings {
214+
for binding in extension_allowed_bindings.split(',') {
215+
bindgen = bindgen
216+
.allowlist_function(binding)
217+
.allowlist_type(binding)
218+
.allowlist_var(binding);
219+
}
220+
}
221+
212222
let bindings = bindgen
213223
.generate()
214224
.map_err(|_| anyhow!("Unable to generate bindings for PHP"))?
@@ -286,7 +296,7 @@ fn main() -> Result<()> {
286296
] {
287297
println!("cargo:rerun-if-changed={}", path.to_string_lossy());
288298
}
289-
for env_var in ["PHP", "PHP_CONFIG", "PATH"] {
299+
for env_var in ["PHP", "PHP_CONFIG", "PATH", "EXT_PHP_RS_ALLOWED_BINDINGS"] {
290300
println!("cargo:rerun-if-env-changed={env_var}");
291301
}
292302

guide/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
# Advanced Topics
3939

4040
- [Async](./advanced/async_impl.md)
41+
- [Allowed Bindings](./advanced/allowed_bindings.md)
4142

4243
# Migration Guides
4344
---
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Allowed Bindings
2+
3+
The extension limits the bindings that are generated by `bindgen` to a subset of the original bindings.
4+
Those bindings are defined in the `allowed_bindings.rs` file.
5+
6+
Should you need to add more bindings, you can do so by defining them as a comma-separated list in the `EXT_PHP_RS_ALLOWED_BINDINGS` environment variable.
7+
8+
This can be configured in your `.cargo/config.toml` file:
9+
10+
```toml
11+
[env]
12+
EXT_PHP_RS_ALLOWED_BINDINGS = "php_foo,php_bar"
13+
```
14+
15+
Your bindings should now appear in the `ext_php_rs::ffi` module.
16+
17+
<div class="warning">
18+
Pay attention to the PHP version
19+
20+
Be aware, that bindings that do not exist in the PHP version you are targeting will not be available.
21+
22+
Some bindings may also change between PHP versions, so make sure to test your extension with all PHP versions you are targeting.
23+
</div>
24+
25+
## Contributing
26+
27+
If you think that a binding should be added to the allowed bindings, please open an issue or a pull request on the [GitHub repository](https://github.com/davidcole1340/ext-php-rs) so that everyone can benefit from it.

0 commit comments

Comments
 (0)