Skip to content

Commit 075b923

Browse files
committed
refactor(pgrx): conditionally apply rustc wrapper when < 0.12
Replace inline bash script generation with writeShellScriptBin for rustc wrapper that filters empty postmaster_stub.rs arguments. Apply wrapper only for pgrx < 0.12 since issue was fixed upstream in pgrx#1435 and pgrx#1441.
1 parent e6fa582 commit 075b923

File tree

1 file changed

+50
-43
lines changed

1 file changed

+50
-43
lines changed

nix/cargo-pgrx/buildPgrxExtension.nix

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
# SOFTWARE.
2929
{
3030
lib,
31-
bash,
3231
cargo-pgrx,
3332
pkg-config,
3433
rustPlatform,
@@ -86,6 +85,27 @@ let
8685
fakeRustfmt = writeShellScriptBin "rustfmt" ''
8786
exit 0
8887
'';
88+
89+
# Rustc wrapper for pgrx < 0.12.0 to filter out empty postmaster_stub.rs arguments
90+
# This fixes an issue that causes build failures.
91+
# Fixed upstream in pgcentralfoundation/pgrx#1435 and #1441, available from pgrx >= 0.12.
92+
rustcWrapper = writeShellScriptBin "rustc" ''
93+
# ORIGINAL_RUSTC is set in the buildPhase before this wrapper is added to PATH
94+
original_rustc="''${ORIGINAL_RUSTC:-rustc}"
95+
filtered_args=()
96+
for arg in "$@"; do
97+
if [[ -z "$arg" ]]; then
98+
continue
99+
fi
100+
if [[ "$arg" =~ postmaster_stub\.rs$ ]]; then
101+
if [[ ! -s "$arg" ]]; then
102+
continue
103+
fi
104+
fi
105+
filtered_args+=("$arg")
106+
done
107+
exec "$original_rustc" "''${filtered_args[@]}"
108+
'';
89109
maybeDebugFlag = lib.optionalString (buildType != "release") "--debug";
90110
maybeEnterBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) ''
91111
export CARGO_TARGET_DIR="$(pwd)/target"
@@ -94,6 +114,10 @@ let
94114
maybeLeaveBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) "popd";
95115
pgrxBinaryName = if builtins.compareVersions "0.7.4" cargo-pgrx.version >= 0 then "pgx" else "pgrx";
96116

117+
# The rustc wrapper is only needed for pgrx < 0.12.0
118+
# fixed upstream in pgcentralfoundation/pgrx#1435 and #1441
119+
needsRustcWrapper = builtins.compareVersions cargo-pgrx.version "0.12.0" < 0;
120+
97121
pgrxPostgresMajor = lib.versions.major postgresql.version;
98122
preBuildAndTest = ''
99123
export PGRX_HOME=$(mktemp -d)
@@ -137,48 +161,31 @@ let
137161
++ lib.optionals useFakeRustfmt [ fakeRustfmt ];
138162

139163
buildPhase = ''
140-
runHook preBuild
141-
142-
echo "Executing cargo-pgrx buildPhase"
143-
${preBuildAndTest}
144-
${maybeEnterBuildAndTestSubdir}
145-
146-
export PGRX_BUILD_FLAGS="--frozen -j $NIX_BUILD_CORES ${builtins.concatStringsSep " " cargoBuildFlags}"
147-
export PGX_BUILD_FLAGS="$PGRX_BUILD_FLAGS"
148-
149-
# Wrap rustc to filter out empty postmaster_stub.rs arguments
150-
original_rustc=$(command -v rustc)
151-
mkdir -p $out/bin
152-
cat > $out/bin/rustc << EOF
153-
#!${bash}/bin/bash
154-
filtered_args=()
155-
for arg in "\$@"; do
156-
if [[ -z "\$arg" ]]; then
157-
continue
158-
fi
159-
if [[ "\$arg" =~ postmaster_stub\.rs$ ]]; then
160-
if [[ ! -s "\$arg" ]]; then
161-
continue
162-
fi
163-
fi
164-
filtered_args+=("\$arg")
165-
done
166-
exec "$original_rustc" "\''${filtered_args[@]}"
167-
EOF
168-
chmod +x $out/bin/rustc
169-
export PATH="$out/bin:$PATH"
170-
export RUSTC="$out/bin/rustc"
171-
172-
${lib.optionalString stdenv.hostPlatform.isDarwin ''RUSTFLAGS="''${RUSTFLAGS:+''${RUSTFLAGS} }-Clink-args=-Wl,-undefined,dynamic_lookup"''} \
173-
cargo ${pgrxBinaryName} package \
174-
--pg-config ${lib.getDev postgresql}/bin/pg_config \
175-
${maybeDebugFlag} \
176-
--features "${builtins.concatStringsSep " " buildFeatures}" \
177-
--out-dir "$out"
178-
179-
${maybeLeaveBuildAndTestSubdir}
180-
181-
runHook postBuild
164+
runHook preBuild
165+
166+
echo "Executing cargo-pgrx buildPhase"
167+
${preBuildAndTest}
168+
${maybeEnterBuildAndTestSubdir}
169+
170+
export PGRX_BUILD_FLAGS="--frozen -j $NIX_BUILD_CORES ${builtins.concatStringsSep " " cargoBuildFlags}"
171+
export PGX_BUILD_FLAGS="$PGRX_BUILD_FLAGS"
172+
173+
${lib.optionalString needsRustcWrapper ''
174+
export ORIGINAL_RUSTC="$(command -v ${stdenv.cc.targetPrefix}rustc || command -v rustc)"
175+
export PATH="${rustcWrapper}/bin:$PATH"
176+
export RUSTC="${rustcWrapper}/bin/rustc"
177+
''}
178+
179+
${lib.optionalString stdenv.hostPlatform.isDarwin ''RUSTFLAGS="''${RUSTFLAGS:+''${RUSTFLAGS} }-Clink-args=-Wl,-undefined,dynamic_lookup"''} \
180+
cargo ${pgrxBinaryName} package \
181+
--pg-config ${lib.getDev postgresql}/bin/pg_config \
182+
${maybeDebugFlag} \
183+
--features "${builtins.concatStringsSep " " buildFeatures}" \
184+
--out-dir "$out"
185+
186+
${maybeLeaveBuildAndTestSubdir}
187+
188+
runHook postBuild
182189
'';
183190

184191
preCheck = preBuildAndTest + args.preCheck or "";

0 commit comments

Comments
 (0)