Как создать привязку Rust для серверной части postgresql с помощью rust bindgen?

Я сделал пакет Cargo со стандартным lib.rs, Cargo.toml с bindgen = "0.59" под [build-dependencies], wrapper.h с #include "rewrite/rewriteDefine.h" и build.rs вроде этого:

      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!");
}

У меня есть репо postgres с https://git.postgresql.org/git/postgresql.git на commit 3779ac62d709467fe6331c8f0285d42e7487a01c и clang 12.0.1, cargo 1.53.0 (4369396ce 2021-04-27) и rustc 1.53.0 (53cb21b) -06-17) установлен.

Сборка ходового груза:

      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

Абсолютный путь: /home/manbearpig/Documents/postgresql/src/include дает те же ошибки, что и относительный путь.

Некоторые из неизвестных имён типов находятся в postgresql/src/include/c.h. Я думаю, что они условно скомпилированы с директивами, использующими макроопределения из postgresql/src/include/pg_config.h

Добавление #include <stdbool.h> на wrapper.h заменяет ../postgresql/src/include/storage/itemptr.h:203:8: error: unknown type name 'bool', err: true с другой ошибкой.

1 ответ

Добавлять #include postgres.h в ваш wrapper.h:

postgres.h
 Основной включаемый файл для файлов PostgreSQL server.c

 Это должен быть первый файл, включаемый внутренними модулями PostgreSQL.
 Вместо этого клиентский код должен включать postgres_fe.h.

Другие вопросы по тегам