pgcentralfoundation / pgrx

Build Postgres Extensions with Rust!
Other
3.66k stars 249 forks source link

Missing flags during `pgrx-pg-sys` when compiling against `pg15` on macOS M1 causing `cargo pgrx install` to fail #1334

Open philippemnoel opened 1 year ago

philippemnoel commented 1 year ago

What

When I run

cargo pgrx init --pg15=`which pg_config`
cargo pgrx install

on my M1 macOS, it fails with error:

  --- stderr
  build_paths=BuildPaths { manifest_dir: "/Users/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pgrx-pg-sys-0.9.8", out_dir: "/Users/user/Documents/Sources/paradedb/target/debug/build/pgrx-pg-sys-5cb11818365f8f32/out", src_dir: "/Users/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pgrx-pg-sys-0.9.8/src", shim_src: "/Users/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pgrx-pg-sys-0.9.8/cshim", shim_dst: "/Users/user/Documents/Sources/paradedb/target/debug/build/pgrx-pg-sys-5cb11818365f8f32/out/cshim" }
  Generating bindings for pg15
  /opt/homebrew/opt/postgresql@15/include/postgresql/server/c.h:75:10: fatal error: 'libintl.h' file not found
  Error: bindgen failed for pg15

  Caused by:
     0: Unable to generate bindings for pg15
     1: clang diagnosed error: /opt/homebrew/opt/postgresql@15/include/postgresql/server/c.h:75:10: fatal error: 'libintl.h' file not found

The solution is to manually run:

export BINDGEN_EXTRA_CLANG_ARGS="-I/opt/homebrew/include"

before, which fixes it. Surprisingly, this is not an issue on pg14.

For more context, see https://github.com/paradedb/paradedb/issues/362 and the recent conversation in Discord with the team: https://discord.com/channels/710918545906597938/1117863684916645930/1162057010276225064

How to fix

According to thomcc, the fix would to be fully pass the pg_config --cppflags flags during compilation

thomcc commented 1 year ago

An annoying thing about this is that fixing it makes our binding situations worse again, in some cases (we end up adding bad bindings for these functions). I tried a hacky fix for it here https://gist.github.com/thomcc/efa9554a6a50024ea1bb46f345a3cc5e but it actually is the wrong approach, ugh.

thomcc commented 1 year ago

Actually this issue is way more awful. If you install postgresql@15 with brew, it doesn't link any binaries or anything, because it's a special version of another package (brews default postgresql package is 14).

So, if you install PG with homebrew and are trying to use a version other than 14, then which pg_config will point at the wrong one. $(brew --prefix postgresql@15)/bin/pg_config is what you need to do.

eeeebbbbrrrr commented 1 year ago

Amazing.

Does cargo-pgrx need some kind of cargo pgrx init --smart thing that can use some heuristics to just figure this stuff out depending on operating system and distro?

ugh, saying that out loud gives me chills.

workingjubilee commented 7 months ago

cc https://github.com/Homebrew/homebrew-core/issues/167593

Based on the remarks there, when interacting with homebrew, we should simply avoid sanctioning any installs that touch the "default" postgresql package and only interact with postgres@14, postgres@15, and postgres@16.