-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
86 lines (78 loc) · 2.88 KB
/
Copy pathbuild.rs
File metadata and controls
86 lines (78 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std::env;
use std::path::PathBuf;
use std::process::Command;
const PHP_HEADERS: &str = r#"
#include <php.h>
#include <php_main.h>
#include <php_ini.h>
#include <php_variables.h>
#include <SAPI.h>
#include <zend_ini.h>
#include <zend_exceptions.h>
#include <zend_stream.h>
#include <zend_extensions.h>
#include <zend_API.h>
#include <ext/random/php_random.h>
#include <ext/random/php_random_csprng.h>
"#;
fn php_config(arg: &str) -> String {
let bin = env::var("PHP_CONFIG").unwrap_or_else(|_| "/opt/php/bin/php-config".into());
let out = Command::new(&bin)
.arg(arg)
.output()
.unwrap_or_else(|e| panic!("{bin} {arg}: {e}"));
assert!(out.status.success(), "{bin} {arg} failed");
String::from_utf8(out.stdout).unwrap().trim().to_string()
}
fn main() {
let prefix = php_config("--prefix");
let includes = php_config("--includes");
let clang_args: Vec<String> = includes.split_whitespace().map(String::from).collect();
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let wrappers = out_dir.join("php_static_wrappers.c");
let header = out_dir.join("stoke_php.h");
std::fs::write(&header, PHP_HEADERS).expect("write stoke_php.h");
let bindings = bindgen::Builder::default()
.header(header.to_str().unwrap())
.clang_args(&clang_args)
.clang_arg("-DZEND_ENABLE_STATIC_TSRMLS_CACHE=1")
.wrap_static_fns(true)
.wrap_static_fns_path(&wrappers)
.allowlist_file(".*/(php|Zend|TSRM|main|ext/standard)/.*")
.allowlist_var("sapi_module")
.allowlist_var("sapi_globals")
.allowlist_var("executor_globals")
.allowlist_var("core_globals")
.allowlist_var("compiler_globals")
.allowlist_var("zend_extensions")
.allowlist_var("module_registry")
.derive_default(false)
.layout_tests(false)
.generate_comments(false)
.blocklist_function("q_?ecvt(_r)?")
.blocklist_function("strtold")
.generate()
.expect("bindgen failed on php headers");
bindings
.write_to_file(out_dir.join("php_sys.rs"))
.expect("write php_sys.rs");
if wrappers.exists() {
let mut build = cc::Build::new();
build.file(&wrappers).include(&out_dir);
for a in &clang_args {
build.flag(a);
}
build.warnings(false).compile("php_static_wrappers");
}
println!("cargo:rustc-link-search=native={prefix}/lib");
println!("cargo:rustc-link-lib=static=php");
for lib in php_config("--libs").split_whitespace() {
if let Some(name) = lib.strip_prefix("-l") {
println!("cargo:rustc-link-lib=dylib={name}");
} else if let Some(path) = lib.strip_prefix("-L") {
println!("cargo:rustc-link-search=native={path}");
}
}
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-env-changed=PHP_CONFIG");
}