Skip to content

Commit

Permalink
Update bindgen to 0.71
Browse files Browse the repository at this point in the history
This commit updates bindgen to 0.71. It also adds a test that verifies that the bundled bindings match the generated ones. Additionally it cleans up the generated bindings to only include items that are relevant for libpq
  • Loading branch information
weiznich committed Jan 9, 2025
1 parent 2f5bfc3 commit 18f3e44
Show file tree
Hide file tree
Showing 11 changed files with 4,806 additions and 6,531 deletions.
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ license = "MIT OR Apache-2.0"
repository = "https://github.com/sgrif/pq-sys"
links = "pq"
build = "build.rs"
edition = "2021"

[workspace]
members = ["pq-src"]
Expand All @@ -15,14 +16,19 @@ name = "pq_sys"

[dependencies]
pq-src = { path = "pq-src", version = ">=0.2, <0.4", optional = true , default-features = false }
libc = "0.2.100"

[build-dependencies]
pkg-config = { version = "0.3.0", optional = true }
bindgen = { version = "0.69.1", optional = true }
bindgen = { version = "0.71.0", optional = true }

[target.'cfg(target_env = "msvc")'.build-dependencies]
vcpkg = "0.2.6"

[dev-dependencies]
similar-asserts = "1.6.0"
bindgen = "0.71.0"

[features]
default = []
bundled = ["bundled_without_openssl", "pq-src/with-openssl"]
Expand Down
35 changes: 35 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# DEVELOPMENT NOTES

Use the following command to generate a new bindings file:
```sh
# keep the command in sync with the command in `src/make_bindings.rs"
bindgen wrapper.h \
--rustified-enum ".*" \
--no-derive-default \
--generate "functions,types,vars,methods,constructors,destructors" \
--allowlist-var "PG_.*" \
--allowlist-var "LIBPQ_.*" \
--allowlist-var "PQ.*" \
--allowlist-type "Oid" \
--allowlist-type "ConnStatusType" \
--allowlist-type "Postgres.*" \
--allowlist-type "pg.*" \
--allowlist-type "PG.*" \
--allowlist-type "PQ.*" \
--allowlist-type "pq.*" \
--allowlist-function "PQ.*" \
--allowlist-function "lo_.*" \
--allowlist-function "pg_.*" \
--opaque-type "FILE" \
--blocklist-type "FILE" \
--raw-line "use libc::FILE;" \
-- -I pq-src/source/src/interfaces/libpq/ -I pq-src/source/src/include/ -I pq-src/additional_include/
```

It is required to generate bindings for the following targets:

* Linux 64 bit
* Linux 32 bit (different field sizes, compilation fails otherwise due to const checks)
* Windows (MSVC) 64 bit (uses `#[repr(i32)]` instead of `#[repr(u32)]` for enums,
can simply replace that in the generated linux bindings)
* Windows (MSVC) 32 bit (same as 64 bit windows + 32 bit linux)
27 changes: 22 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,37 @@ fn main() {
// by pq-src
return;
}
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("bindings.rs");
#[cfg(feature = "buildtime_bindgen")]
{
let bindings = bindgen::Builder::default()
.rustified_enum(".*")
.header("wrapper.h")
let bindings = include!("src/make_bindings.rs")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");

let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.write_to_file(out_path)
.expect("Couldn't write bindings!");
}
#[cfg(not(feature = "buildtime_bindgen"))]
{
let target_env = std::env::var("CARGO_CFG_TARGET_ENV").expect("Set by cargo");
let target_ptr_size =
std::env::var("CARGO_CFG_TARGET_POINTER_WIDTH").expect("Set by cargo");
let bindings_name = match (target_env.as_str(), target_ptr_size.as_str()) {
("msvc", "32") => "src/bindings_windows_32.rs",
("msvc", "64") => "src/bindings_windows.rs",
(_, "32") => "src/bindings_linux_32.rs",
(_, "64") => "src/bindings_linux.rs",
(target_env, ptr_width) => {
panic!("Unsupported target: TargetEnv: `{target_env}`, PtrWidth: `{ptr_width}`\n\
If you use this target open an issue at https://github.com/sgrif/pq-sys/issues/new\
outlining the details of this target");
}
};
let source_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(bindings_name);
std::fs::copy(source_path, out_path).expect("Couldn't write bindings");
}

#[cfg(target_os = "windows")]
println!("cargo:rustc-link-lib=libpq");
Expand Down
Loading

0 comments on commit 18f3e44

Please sign in to comment.