Skip to content

Commit 356c1fd

Browse files
committed
Add support for docs.rs (hopefully)
1 parent 0056db3 commit 356c1fd

File tree

2 files changed

+1348
-40
lines changed

2 files changed

+1348
-40
lines changed

build.rs

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
11
use std::{
2-
collections::HashSet,
32
env,
43
path::{Path, PathBuf},
54
process::Command,
65
str,
76
};
87

9-
use bindgen::callbacks::{MacroParsingBehavior, ParseCallbacks};
108
use regex::Regex;
119

12-
extern crate bindgen;
13-
14-
// https://github.com/rust-lang/rust-bindgen/issues/687#issuecomment-450750547
15-
#[derive(Debug)]
16-
struct IgnoreMacros(HashSet<String>);
17-
18-
impl ParseCallbacks for IgnoreMacros {
19-
fn will_parse_macro(&self, name: &str) -> MacroParsingBehavior {
20-
if self.0.contains(name) {
21-
MacroParsingBehavior::Ignore
22-
} else {
23-
MacroParsingBehavior::Default
24-
}
25-
}
26-
}
27-
2810
const MIN_PHP_API_VER: u32 = 20200930;
2911
const MAX_PHP_API_VER: u32 = 20200930;
3012

@@ -33,6 +15,18 @@ fn main() {
3315
println!("cargo:rerun-if-changed=src/wrapper/wrapper.h");
3416
println!("cargo:rerun-if-changed=src/wrapper/wrapper.c");
3517

18+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("bindings.rs");
19+
20+
// check for docs.rs and use stub bindings if required
21+
if env::var("DOCS_RS").is_ok() {
22+
println!("cargo:warning=docs.rs detected - using stub bindings");
23+
println!("cargo:rustc-cfg=php_debug");
24+
25+
std::fs::copy("docsrs_bindings.rs", out_path)
26+
.expect("Unable to copy docs.rs stub bindings to output directory.");
27+
return;
28+
}
29+
3630
// use php-config to fetch includes
3731
let includes_cmd = Command::new("php-config")
3832
.arg("--includes")
@@ -85,31 +79,10 @@ fn main() {
8579
)
8680
.compile("wrapper");
8781

88-
let ignore_math_h_macros = IgnoreMacros(
89-
vec![
90-
// math.h:914 - enum which uses #define for values
91-
"FP_NAN".into(),
92-
"FP_INFINITE".into(),
93-
"FP_ZERO".into(),
94-
"FP_SUBNORMAL".into(),
95-
"FP_NORMAL".into(),
96-
// math.h:237 - enum which uses #define for values
97-
"FP_INT_UPWARD".into(),
98-
"FP_INT_DOWNWARD".into(),
99-
"FP_INT_TOWARDZERO".into(),
100-
"FP_INT_TONEARESTFROMZERO".into(),
101-
"FP_INT_TONEAREST".into(),
102-
]
103-
.into_iter()
104-
.collect(),
105-
);
106-
107-
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
10882
let mut bindgen = bindgen::Builder::default()
10983
.header("src/wrapper/wrapper.h")
11084
.clang_args(includes.split(' '))
11185
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
112-
.parse_callbacks(Box::new(ignore_math_h_macros))
11386
.rustfmt_bindings(true)
11487
.no_copy("_zend_value")
11588
.layout_tests(env::var("EXT_PHP_RS_TEST").is_ok());
@@ -124,7 +97,7 @@ fn main() {
12497
bindgen
12598
.generate()
12699
.expect("Unable to generate bindings for PHP")
127-
.write_to_file(out_path.join("bindings.rs"))
100+
.write_to_file(out_path)
128101
.expect("Unable to write bindings file.");
129102

130103
let configure = Configure::get();

0 commit comments

Comments
 (0)