Search code examples
cpostgresqlrustclangbindgen

How do I create Rust bindings for the postgresql backend with rust bindgen?


I made a Cargo package which has the standard lib.rs, a Cargo.toml with bindgen = "0.59" under [build-dependencies], a wrapper.h with #include "rewrite/rewriteDefine.h" and a build.rs like so:

extern crate bindgen;

use std::env;
use std::path::PathBuf;

fn main() {
    // Tell cargo to invalidate the built crate whenever the wrapper changes
    println!("cargo:rerun-if-changed=wrapper.h");

    // The bindgen::Builder is the main entry point
    // to bindgen, and lets you build up options for
    // the resulting bindings.
    let bindings = bindgen::Builder::default()
        .clang_arg("-I../postgresql/src/include")
        // The input header we would like to generate
        // bindings for.
        .header("wrapper.h")
        // Tell cargo to invalidate the built crate whenever any of the
        // included header files changed.
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        // Finish the builder and generate the bindings.
        .generate()
        // Unwrap the Result and panic on failure.
        .expect("Unable to generate bindings");

    // Write the bindings to the $OUT_DIR/bindings.rs file.
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");
}

I have my postgres repo from https://git.postgresql.org/git/postgresql.git at commit 3779ac62d709467fe6331c8f0285d42e7487a01c and clang 12.0.1, cargo 1.53.0 (4369396ce 2021-04-27) and rustc 1.53.0 (53cb7b09b 2021-06-17) installed.

Running cargo build:

Compiling foo v0.1.0 (/home/manbearpig/Documents/foo)
error: failed to run custom build command for `foo v0.1.0 (/home/manbearpig/Documents/foo)`

Caused by:
  process didn't exit successfully: `/home/manbearpig/Documents/foo/target/debug/build/foo-e5423fb71d62f5d9/build-script-build` (exit code: 101)
  --- stdout
  cargo:rerun-if-changed=wrapper.h

  --- stderr
  ../postgresql/src/include/storage/block.h:31:9: error: unknown type name 'uint32'
  ../postgresql/src/include/storage/block.h:55:2: error: unknown type name 'uint16'
  ../postgresql/src/include/storage/block.h:56:2: error: unknown type name 'uint16'
  ../postgresql/src/include/storage/itemid.h:47:9: error: unknown type name 'uint16'
  ../postgresql/src/include/storage/itemid.h:48:9: error: unknown type name 'uint16'
  ../postgresql/src/include/storage/off.h:24:9: error: unknown type name 'uint16'
  ../postgresql/src/include/storage/itemptr.h:203:8: error: unknown type name 'bool'
  ../postgresql/src/include/storage/itemptr.h:204:8: error: unknown type name 'int32'
  ../postgresql/src/include/access/htup.h:64:2: error: unknown type name 'uint32'
  ../postgresql/src/include/access/htup.h:66:2: error: unknown type name 'Oid'
  ../postgresql/src/include/access/htup.h:81:8: error: unknown type name 'CommandId'
  ../postgresql/src/include/access/htup.h:82:8: error: unknown type name 'CommandId'
  ../postgresql/src/include/access/htup.h:84:12: error: unknown type name 'CommandId'
  ../postgresql/src/include/access/htup.h:84:29: error: unknown type name 'bool'
  ../postgresql/src/include/access/htup.h:87:8: error: unknown type name 'TransactionId'
  ../postgresql/src/include/nodes/bitmapset.h:44:9: error: unknown type name 'uint32'
  ../postgresql/src/include/nodes/bitmapset.h:45:9: error: unknown type name 'int32'
  ../postgresql/src/include/nodes/bitmapset.h:52:19: error: use of undeclared identifier 'FLEXIBLE_ARRAY_MEMBER'
  ../postgresql/src/include/nodes/bitmapset.h:79:8: error: unknown type name 'bool'
  fatal error: too many errors emitted, stopping now [-ferror-limit=]
  ../postgresql/src/include/storage/block.h:31:9: error: unknown type name 'uint32', err: true
  ../postgresql/src/include/storage/block.h:55:2: error: unknown type name 'uint16', err: true
  ../postgresql/src/include/storage/block.h:56:2: error: unknown type name 'uint16', err: true
  ../postgresql/src/include/storage/itemid.h:47:9: error: unknown type name 'uint16', err: true
  ../postgresql/src/include/storage/itemid.h:48:9: error: unknown type name 'uint16', err: true
  ../postgresql/src/include/storage/off.h:24:9: error: unknown type name 'uint16', err: true
  ../postgresql/src/include/storage/itemptr.h:203:8: error: unknown type name 'bool', err: true
  ../postgresql/src/include/storage/itemptr.h:204:8: error: unknown type name 'int32', err: true
  ../postgresql/src/include/access/htup.h:64:2: error: unknown type name 'uint32', err: true
  ../postgresql/src/include/access/htup.h:66:2: error: unknown type name 'Oid', err: true
  ../postgresql/src/include/access/htup.h:81:8: error: unknown type name 'CommandId', err: true
  ../postgresql/src/include/access/htup.h:82:8: error: unknown type name 'CommandId', err: true
  ../postgresql/src/include/access/htup.h:84:12: error: unknown type name 'CommandId', err: true
  ../postgresql/src/include/access/htup.h:84:29: error: unknown type name 'bool', err: true
  ../postgresql/src/include/access/htup.h:87:8: error: unknown type name 'TransactionId', err: true
  ../postgresql/src/include/nodes/bitmapset.h:44:9: error: unknown type name 'uint32', err: true
  ../postgresql/src/include/nodes/bitmapset.h:45:9: error: unknown type name 'int32', err: true
  ../postgresql/src/include/nodes/bitmapset.h:52:19: error: use of undeclared identifier 'FLEXIBLE_ARRAY_MEMBER', err: true
  ../postgresql/src/include/nodes/bitmapset.h:79:8: error: unknown type name 'bool', err: true
  fatal error: too many errors emitted, stopping now [-ferror-limit=], err: true
  thread 'main' panicked at 'Unable to generate bindings: ()', build.rs:24:10
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

An absolute path: /home/manbearpig/Documents/postgresql/src/include gives the same errors as the relative path.

Some of the unknown type names are in postgresql/src/include/c.h. I think they are conditionally compiled with directives using macro definitions from postgresql/src/include/pg_config.h.

Running bindgen c.h in the include folder outputs e.g.: pub type uint32 = ::std::os::raw::c_uint;.

Adding #include <stdbool.h> to wrapper.h replaces ../postgresql/src/include/storage/itemptr.h:203:8: error: unknown type name 'bool', err: true with another error.


Solution

  • Add #include postgres.h to your wrapper.h:

    postgres.h
     Primary include file for PostgreSQL server.c files

     This should be the first file included by PostgreSQL backend modules.
     Client-side code should include postgres_fe.h instead.