-
-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathbuildPgrxExtension.nix
More file actions
255 lines (225 loc) · 9.49 KB
/
buildPgrxExtension.nix
File metadata and controls
255 lines (225 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# preBuildAndTest and some small other bits
# taken from https://github.com/tcdi/pgrx/blob/v0.9.4/nix/extension.nix
# (but now heavily modified)
# which uses MIT License with the following license file
#
# MIT License
#
# Portions Copyright 2019-2021 ZomboDB, LLC.
# Portions Copyright 2021-2022 Technology Concepts & Design, Inc. <support@tcdi.com>.
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
{
lib,
cargo-pgrx,
pkg-config,
rustPlatform,
stdenv,
writeShellScriptBin,
defaultBindgenHook,
sccache,
}:
# The idea behind: Use it mostly like rustPlatform.buildRustPackage and so
# we hand most of the arguments down.
#
# Additional arguments are:
# - `postgresql` postgresql package of the version of postgresql this extension should be build for.
# Needs to be the build platform variant.
# - `useFakeRustfmt` Whether to use a noop fake command as rustfmt. cargo-pgrx tries to call rustfmt.
# If the generated rust bindings aren't needed to use the extension, its a
# unnecessary and heavy dependency. If you set this to true, you also
# have to add `rustfmt` to `nativeBuildInputs`.
{
buildAndTestSubdir ? null,
buildType ? "release",
buildFeatures ? [ ],
cargoBuildFlags ? [ ],
postgresql,
# enable override to generate bindings using bindgenHook.
# Some older versions of cargo-pgrx use a bindgenHook that is not compatible with the
# current clang version present in stdenv
bindgenHook ? defaultBindgenHook,
# cargo-pgrx calls rustfmt on generated bindings, this is not strictly necessary, so we avoid the
# dependency here. Set to false and provide rustfmt in nativeBuildInputs, if you need it, e.g.
# if you include the generated code in the output via postInstall.
useFakeRustfmt ? true,
usePgTestCheckFeature ? true,
...
}@args:
let
rustfmtInNativeBuildInputs = lib.lists.any (dep: lib.getName dep == "rustfmt") (
args.nativeBuildInputs or [ ]
);
in
assert lib.asserts.assertMsg (
(args.installPhase or "") == ""
) "buildPgrxExtensions overwrites the installPhase, so providing one does nothing";
assert lib.asserts.assertMsg (
(args.buildPhase or "") == ""
) "buildPgrxExtensions overwrites the buildPhase, so providing one does nothing";
assert lib.asserts.assertMsg (useFakeRustfmt -> !rustfmtInNativeBuildInputs)
"The parameter useFakeRustfmt is set to true, but rustfmt is included in nativeBuildInputs. Either set useFakeRustfmt to false or remove rustfmt from nativeBuildInputs.";
assert lib.asserts.assertMsg (!useFakeRustfmt -> rustfmtInNativeBuildInputs)
"The parameter useFakeRustfmt is set to false, but rustfmt is not included in nativeBuildInputs. Either set useFakeRustfmt to true or add rustfmt from nativeBuildInputs.";
let
fakeRustfmt = writeShellScriptBin "rustfmt" ''
exit 0
'';
# Rustc wrapper for pgrx < 0.12.0 to filter out empty postmaster_stub.rs arguments
# This fixes an issue that causes build failures.
# Fixed upstream in pgcentralfoundation/pgrx#1435 and #1441, available from pgrx >= 0.12.
rustcWrapper = writeShellScriptBin "rustc" ''
# ORIGINAL_RUSTC is set in the buildPhase before this wrapper is added to PATH
original_rustc="''${ORIGINAL_RUSTC:-rustc}"
filtered_args=()
for arg in "$@"; do
if [[ -z "$arg" ]]; then
continue
fi
if [[ "$arg" =~ postmaster_stub\.rs$ ]]; then
if [[ ! -s "$arg" ]]; then
continue
fi
fi
filtered_args+=("$arg")
done
exec "$original_rustc" "''${filtered_args[@]}"
'';
maybeDebugFlag = lib.optionalString (buildType != "release") "--debug";
maybeEnterBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) ''
export CARGO_TARGET_DIR="$(pwd)/target"
pushd "${buildAndTestSubdir}"
'';
maybeLeaveBuildAndTestSubdir = lib.optionalString (buildAndTestSubdir != null) "popd";
pgrxBinaryName = if builtins.compareVersions "0.7.4" cargo-pgrx.version >= 0 then "pgx" else "pgrx";
# The rustc wrapper is only needed for pgrx < 0.12.0
# fixed upstream in pgcentralfoundation/pgrx#1435 and #1441
needsRustcWrapper = builtins.compareVersions cargo-pgrx.version "0.12.0" < 0;
pgrxPostgresMajor = lib.versions.major postgresql.version;
preBuildAndTest = ''
export PGRX_HOME=$(mktemp -d)
export PGX_HOME=$PGRX_HOME
export PGDATA="$PGRX_HOME/data-${pgrxPostgresMajor}/"
cargo-${pgrxBinaryName} ${pgrxBinaryName} init "--pg${pgrxPostgresMajor}" ${lib.getDev postgresql}/bin/pg_config
# unix sockets work in sandbox, too.
export PGHOST="$(mktemp -d)"
cat > "$PGDATA/postgresql.conf" <<EOF
listen_addresses = '''
unix_socket_directories = '$PGHOST'
EOF
# This is primarily for Mac or other Nix systems that don't use the nixbld user.
export USER="$(whoami)"
pg_ctl start
createuser -h localhost --superuser --createdb "$USER" || true
pg_ctl stop
'';
argsForBuildRustPackage = builtins.removeAttrs args [
"postgresql"
"useFakeRustfmt"
"usePgTestCheckFeature"
];
# so we don't accidentally `(rustPlatform.buildRustPackage argsForBuildRustPackage) // { ... }` because
# we forgot parentheses
finalArgs = argsForBuildRustPackage // {
buildInputs = (args.buildInputs or [ ]);
nativeBuildInputs =
(args.nativeBuildInputs or [ ])
++ [
cargo-pgrx
postgresql
pkg-config
bindgenHook
sccache
]
++ lib.optionals useFakeRustfmt [ fakeRustfmt ];
buildPhase = ''
runHook preBuild
echo "Platform: ${stdenv.system}"
echo "isDarwin: ${lib.boolToString stdenv.isDarwin}"
if [[ -d "/nix/var/cache/sccache" && -w "/nix/var/cache/sccache" ]]; then
if touch "/nix/var/cache/sccache/.test" 2>/dev/null && rm -f "/nix/var/cache/sccache/.test" 2>/dev/null; then
echo "sccache: cache directory available and writable, enabling"
${lib.optionalString stdenv.isDarwin ''
# Darwin: Use shared /tmp for TMPDIR to avoid sccache caching per-build temp paths
export TMPDIR=/tmp
export TEMP=/tmp
export TEMPDIR=/tmp
export TMP=/tmp
''}
export RUSTC_WRAPPER="${sccache}/bin/sccache"
export SCCACHE_DIR="/nix/var/cache/sccache"
export SCCACHE_CACHE_SIZE="50G"
export SCCACHE_LOG=debug
export SCCACHE_IDLE_TIMEOUT=0
# Use unique port per user to isolate sccache servers
USER_ID=$(id -u)
export SCCACHE_SERVER_PORT=$((4226 + USER_ID % 100))
else
echo "sccache: cache directory not accessible in sandbox, skipping"
fi
else
echo "sccache: cache directory not available, skipping"
fi
echo "Executing cargo-pgrx buildPhase"
${preBuildAndTest}
${maybeEnterBuildAndTestSubdir}
export PGRX_BUILD_FLAGS="--frozen -j $NIX_BUILD_CORES ${builtins.concatStringsSep " " cargoBuildFlags}"
export PGX_BUILD_FLAGS="$PGRX_BUILD_FLAGS"
${lib.optionalString needsRustcWrapper ''
export ORIGINAL_RUSTC="$(command -v ${stdenv.cc.targetPrefix}rustc || command -v rustc)"
export PATH="${rustcWrapper}/bin:$PATH"
export RUSTC="${rustcWrapper}/bin/rustc"
''}
${lib.optionalString stdenv.hostPlatform.isDarwin ''RUSTFLAGS="''${RUSTFLAGS:+''${RUSTFLAGS} }-Clink-args=-Wl,-undefined,dynamic_lookup"''} \
cargo ${pgrxBinaryName} package \
--pg-config ${lib.getDev postgresql}/bin/pg_config \
${maybeDebugFlag} \
--features "${builtins.concatStringsSep " " buildFeatures}" \
--out-dir "$out"
if [[ -n "''${RUSTC_WRAPPER:-}" ]]; then
echo "sccache stats:"
${sccache}/bin/sccache --show-stats
fi
${maybeLeaveBuildAndTestSubdir}
runHook postBuild
'';
preCheck = preBuildAndTest + args.preCheck or "";
installPhase = ''
runHook preInstall
echo "Executing buildPgrxExtension install"
${maybeEnterBuildAndTestSubdir}
cargo-${pgrxBinaryName} ${pgrxBinaryName} stop all
mv $out/${postgresql}/* $out
mv $out/${postgresql.lib}/* $out
rm -rf $out/nix
${maybeLeaveBuildAndTestSubdir}
runHook postInstall
'';
PGRX_PG_SYS_SKIP_BINDING_REWRITE = "1";
CARGO_BUILD_INCREMENTAL = "false";
RUST_BACKTRACE = "full";
checkNoDefaultFeatures = true;
checkFeatures =
(args.checkFeatures or [ ])
++ (lib.optionals usePgTestCheckFeature [ "pg_test" ])
++ [ "pg${pgrxPostgresMajor}" ];
};
in
rustPlatform.buildRustPackage finalArgs