pgrx icon indicating copy to clipboard operation
pgrx copied to clipboard

cargo-pgx cross compilation support

Open jaskij opened this issue 3 years ago • 15 comments

Work in progress for adding cross compilation support to cargo-pgx, as discussed in #955

jaskij avatar Dec 24 '22 13:12 jaskij

Now, the questions is, how do I test it? Since cargo-pgx must be in the same version as pgx required by the plugin being built.

jaskij avatar Dec 24 '22 13:12 jaskij

@thomcc I primarily marked this as a draft to discuss the interface - mostly around what is passed as an argument, and what is passed as an environmental variable.

Meanwhile, I'm going to document some caveats here:

  • host and target need compatible versions of PostgreSQL (not sure how much they need to much, if it must be the exact same version, or just same minor)
  • PG_CONFIG needs to be an absolute path
  • setting the path to pg_config is an absolute mess - there's PGX_PG_CONFIG, PG_CONFIG, --pg-config and I added --host-pg-config. My testing so far indicates that PG_CONFIG does not need to be set, but it's still early.

jaskij avatar Jan 14 '23 19:01 jaskij

I have absolutely no clue why that one test is failing, but it seems to be misconfigured, especiall since on my PC (with PG15 installed via cargo pgx init) that same command does not fail.

  [shim for PG v15] [stderr] Makefile:26: /usr/lib/postgresql/15/lib/pgxs/src/makefiles/pgxs.mk: No such file or directory
  [shim for PG v15] [stderr] make: *** No rule to make target '/usr/lib/postgresql/15/lib/pgxs/src/makefiles/pgxs.mk'.  Stop.

jaskij avatar Jan 14 '23 22:01 jaskij

A quick note on how to actually cross-compile with cargo-pgx, seems I forgot to post it yesterday.

  1. Read Thom's guide
  2. Additional requirements: -- host and target PostgreSQL (including server dev stuff and pg_config) -- user space qemu for your target architecture (eg qemu-aarch64)
  3. Make pg_config wrapper (below)
  4. Call PGX_PG_CONFIG_PATH=~/pg_config_wrapper cargo pgx package --sysroot=/usr/local/oecore-x86_64/sysroots/cortexa53-crypto-poky-linux --pg-config=/home/jaskij/pg_config_wrapper --target=aarch64-unknown-linux-gnu

pg_config wrapper:

#!/usr/bin/env sh
set -x
SDK_DIR="/usr/local/oecore-x86_64"
SYSROOT_DIR="$SDK_DIR/sysroots"
OE_HOST="x86_64-poky-linux"
OE_TARGET="cortexa53-crypto-poky-linux"
HOST_SYSROOT="$SYSROOT_DIR/$OE_HOST"
TARGET_SYSROOT="$SYSROOT_DIR/$OE_TARGET"

$HOST_SYSROOT/usr/bin/qemu-aarch64 -L $TARGET_SYSROOT  $TARGET_SYSROOT/usr/bin/pg_config "$@"

jaskij avatar Jan 15 '23 10:01 jaskij

As is, schema generation builds the extension for the host so that it can generate the bindings. This isn't a good solution, but is a workable one. Sadly, schema generation feels like black magic to me.

The current solution has two, huge and obvious, drawbacks:

  • requiring host PostgreSQL, at the very least in similar version to target's
  • doubling the already long build times

I'm not sure if it's in-scope for this MR though, and I would need a lot of guidance to implement it.

jaskij avatar Jan 15 '23 10:01 jaskij

@thomcc I see that your work on cross compilation was merged, that's what created the conflict. Do you think there is something I should be looking into or integrating with? Or can I just simply resolve the conflict and be done with it?

jaskij avatar Jan 28 '23 08:01 jaskij

You don't need to integrate with it really. There's some redundancy, but it largely handles a few cases that this completes (specifically the no cshim case where you don't care about cargo-pgx — it's basically for https://github.com/tcdi/plrust).

Resolving the conflict is sufficient, thanks.

thomcc avatar Feb 01 '23 16:02 thomcc

@thomcc conflict resolved.

If you're working on update to CROSS_COMPILE.md already, my instructions are condensed in this comment.

A cleaned up version of pg_config_wrapper:

#!/usr/bin/env sh
set -x
TARGET_SYSROOT="/usr/local/oecore-x86_64/sysroots/cortexa53-crypto-poky-linux"

qemu-aarch64 -L $TARGET_SYSROOT  $TARGET_SYSROOT/usr/bin/pg_config "$@"

jaskij avatar Feb 01 '23 23:02 jaskij

Can you rustfmt and push an update?

thomcc avatar Feb 02 '23 16:02 thomcc

Damnit, thought it was already formatted. Will do as soon as I'm at my PC.

jaskij avatar Feb 02 '23 16:02 jaskij

@thomcc done.

jaskij avatar Feb 03 '23 00:02 jaskij

Hmm, can you check the CI error? I didn't see this locally when I tested, but I think I was testing on pg14 rather than 15.

thomcc avatar Feb 03 '23 20:02 thomcc

Putting this PR on "pause" until we have some extra cycles over here in pgxland to make sure this doesn't actually break anything and is otherwise complete enough to accomplish the stated goal.

eeeebbbbrrrr avatar Mar 08 '23 16:03 eeeebbbbrrrr

If this gets rebased I think we can take a look at landing it?

workingjubilee avatar Jul 24 '23 22:07 workingjubilee

I don't have the time or energy to get on this now, but might have it at the beginning of September, feel free to ping me then if I don't show up.

jaskij avatar Jul 25 '23 17:07 jaskij