如何使用 rust bindgen 为 postgresql 后端创建 Rust 绑定?
How do I create Rust bindings for the postgresql backend with rust bindgen?
我制作了一个 Cargo 包裹,其中包含标准 lib.rs、Cargo.toml 和 [build-dependencies]
下的 bindgen = "0.59"
、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,提交 3779ac62d709467fe6331c8f0285d42e7487a01c 和 clang 12.0.1,cargo 1.53.0 (4369396ce 2021-04-27) 和 rustc 1.53.0 (53cb7b09b06-2021)安装。
运行 货物构建:
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
.
的宏定义的指令有条件地编译的
运行 bindgen c.h
在包含文件夹输出中,例如:pub type uint32 = ::std::os::raw::c_uint;
.
将 #include <stdbool.h>
添加到 wrapper.h 会将 ../postgresql/src/include/storage/itemptr.h:203:8: error: unknown type name 'bool', err: true
替换为另一个错误。
将 #include postgres.h
添加到您的 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.
我制作了一个 Cargo 包裹,其中包含标准 lib.rs、Cargo.toml 和 [build-dependencies]
下的 bindgen = "0.59"
、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,提交 3779ac62d709467fe6331c8f0285d42e7487a01c 和 clang 12.0.1,cargo 1.53.0 (4369396ce 2021-04-27) 和 rustc 1.53.0 (53cb7b09b06-2021)安装。
运行 货物构建:
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
.
运行 bindgen c.h
在包含文件夹输出中,例如:pub type uint32 = ::std::os::raw::c_uint;
.
将 #include <stdbool.h>
添加到 wrapper.h 会将 ../postgresql/src/include/storage/itemptr.h:203:8: error: unknown type name 'bool', err: true
替换为另一个错误。
将 #include postgres.h
添加到您的 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.