Skip to content

Commit db622e8

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 5f30f41 commit db622e8

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

nix/cargo-pgrx/buildPgrxExtension.nix

Lines changed: 48 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,26 @@ 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=$(command -v ${stdenv.cc.targetPrefix}rustc || command -v rustc)
94+
filtered_args=()
95+
for arg in "$@"; do
96+
if [[ -z "$arg" ]]; then
97+
continue
98+
fi
99+
if [[ "$arg" =~ postmaster_stub\.rs$ ]]; then
100+
if [[ ! -s "$arg" ]]; then
101+
continue
102+
fi
103+
fi
104+
filtered_args+=("$arg")
105+
done
106+
exec "$original_rustc" "''${filtered_args[@]}"
107+
'';
89108
maybeDebugFlag = lib.optionalString (buildType != "release") "--debug";
90109
maybeEnterBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) ''
91110
export CARGO_TARGET_DIR="$(pwd)/target"
@@ -94,6 +113,10 @@ let
94113
maybeLeaveBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) "popd";
95114
pgrxBinaryName = if builtins.compareVersions "0.7.4" cargo-pgrx.version >= 0 then "pgx" else "pgrx";
96115

116+
# The rustc wrapper is only needed for pgrx < 0.12.0
117+
# fixed upstream in pgcentralfoundation/pgrx#1435 and #1441
118+
needsRustcWrapper = builtins.compareVersions cargo-pgrx.version "0.12.0" < 0;
119+
97120
pgrxPostgresMajor = lib.versions.major postgresql.version;
98121
preBuildAndTest = ''
99122
export PGRX_HOME=$(mktemp -d)
@@ -137,48 +160,30 @@ let
137160
++ lib.optionals useFakeRustfmt [ fakeRustfmt ];
138161

139162
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
163+
runHook preBuild
164+
165+
echo "Executing cargo-pgrx buildPhase"
166+
${preBuildAndTest}
167+
${maybeEnterBuildAndTestSubdir}
168+
169+
export PGRX_BUILD_FLAGS="--frozen -j $NIX_BUILD_CORES ${builtins.concatStringsSep " " cargoBuildFlags}"
170+
export PGX_BUILD_FLAGS="$PGRX_BUILD_FLAGS"
171+
172+
${lib.optionalString needsRustcWrapper ''
173+
export PATH="${rustcWrapper}/bin:$PATH"
174+
export RUSTC="${rustcWrapper}/bin/rustc"
175+
''}
176+
177+
${lib.optionalString stdenv.hostPlatform.isDarwin ''RUSTFLAGS="''${RUSTFLAGS:+''${RUSTFLAGS} }-Clink-args=-Wl,-undefined,dynamic_lookup"''} \
178+
cargo ${pgrxBinaryName} package \
179+
--pg-config ${lib.getDev postgresql}/bin/pg_config \
180+
${maybeDebugFlag} \
181+
--features "${builtins.concatStringsSep " " buildFeatures}" \
182+
--out-dir "$out"
183+
184+
${maybeLeaveBuildAndTestSubdir}
185+
186+
runHook postBuild
182187
'';
183188

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

0 commit comments

Comments
 (0)