From d191a8d9b7ba2b9cac3596dbfdc3479b4bd58228 Mon Sep 17 00:00:00 2001 From: Ellie Frost Date: Tue, 1 Jan 2019 08:46:54 +0000 Subject: [PATCH] Ignore macros that cause duplicate issues --- pg-extend/build.rs | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/pg-extend/build.rs b/pg-extend/build.rs index 565939bc..a82beaf8 100644 --- a/pg-extend/build.rs +++ b/pg-extend/build.rs @@ -7,12 +7,12 @@ extern crate bindgen; +use std::collections::HashSet; use std::env; use std::path::PathBuf; fn main() { - let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()) - .join("postgres.rs"); + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("postgres.rs"); if !out_path.exists() { let pg_include = env::var("PG_INCLUDE_PATH") @@ -27,17 +27,33 @@ fn main() { // The bindgen::Builder is the main entry point // to bindgen, and lets you build up options for // the resulting bindings. + + let ignored_macros = IgnoreMacros( + vec![ + "FP_INFINITE".into(), + "FP_NAN".into(), + "FP_NORMAL".into(), + "FP_SUBNORMAL".into(), + "FP_ZERO".into(), + "IPPORT_RESERVED".into(), + ] + .into_iter() + .collect(), + ); + let bindings = bindgen::Builder::default() - .clang_arg(format!("-I{}",pg_include)) + .clang_arg(format!("-I{}", pg_include)) // The input header we would like to generate // bindings for. .header("wrapper.h") + .parse_callbacks(Box::new(ignored_macros)) .rustfmt_bindings(true) // FIXME: add this back .layout_tests(false); - // Finish the builder and generate the bindings. - let bindings = bindings.generate() + // Finish the builder and generate the bindings. + let bindings = bindings + .generate() // Unwrap the Result and panic on failure. .expect("Unable to generate bindings"); @@ -47,3 +63,16 @@ fn main() { .expect("Couldn't write bindings!"); } } + +#[derive(Debug)] +struct IgnoreMacros(HashSet); + +impl bindgen::callbacks::ParseCallbacks for IgnoreMacros { + fn will_parse_macro(&self, name: &str) -> bindgen::callbacks::MacroParsingBehavior { + if self.0.contains(name) { + bindgen::callbacks::MacroParsingBehavior::Ignore + } else { + bindgen::callbacks::MacroParsingBehavior::Default + } + } +}