Debian Codegraph @

pg_buildext

raw
1 #!/bin/bash
2 #
3 # build a PostgreSQL module based on PGXS for given list of supported major
4 # versions
5 #
6 # (C) 2010 Dimitri Fontaine <[email protected]>
7 # (C) 2011-2025 Christoph Berg <[email protected]>
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18
19 set -eu
20
21 die() {
22 echo "$(basename $0): error: $*" >&2
23 exit 1
24 }
25
26 while getopts "c:i:m:o:s" opt ; do
27 case $opt in
28 c|i|o) PG_VIRTUALENV_ARGS="${PG_VIRTUALENV_ARGS:-} -$opt $OPTARG" ;;
29 m) MAKEVARS="$OPTARG" ;;
30 s) PG_VIRTUALENV_ARGS="${PG_VIRTUALENV_ARGS:-} -$opt" ;;
31 *) exit 1 ;;
32 esac
33 done
34 # shift away args
35 shift $(($OPTIND - 1))
36
37 # positional arguments: action [srcdir] [target [opt]]
38 [ "${1:-}" ] || die "no action given"
39 action="$1"
40 shift
41
42 if [ -d "${1:-}" ] && [ "${2:-}" ]; then # optional: source directory
43 srcdir="${1:-}"
44 [ "$srcdir" = "." ] && srcdir="$PWD"
45 shift
46 else
47 srcdir="$PWD"
48 fi
49 target="${1:-}"
50 opt="${2:-}"
51
52 # pass -j to make
53 parallel=$(echo ${DEB_BUILD_OPTIONS:-} | grep -o 'parallel=[0-9]*' || :)
54 if [ "$parallel" ]; then
55 MAKEVARS="${MAKEVARS:-} -j${parallel#parallel=}"
56 fi
57
58 prepare_env() {
59 local version=$1
60 vtarget=`echo $target | sed -e "s:%v:$version:g"`
61 export PG_CONFIG="/usr/lib/postgresql/$version/bin/pg_config"
62 [ -e "$PG_CONFIG" ] || die "$PG_CONFIG does not exist"
63 if [ "${CFLAGS:-}" ]; then
64 export COPT="$CFLAGS"
65 fi
66 }
67
68 configure() {
69 prepare_env $1
70 confopts=`echo $opt | sed -e "s:%v:$1:g"`
71
72 mkdir -p $vtarget
73 ( echo "calling configure in $vtarget" &&
74 cd $vtarget && $srcdir/configure $confopts PG_CONFIG="$PG_CONFIG" VPATH="$srcdir" ) || return $?
75 }
76
77 build() {
78 prepare_env $1
79 if [ "$opt" ]; then
80 cflags="$(echo $opt | sed -e "s:%v:$1:g")"
81 export COPT="${COPT:+$COPT }$cflags"
82 fi
83
84 mkdir -p $vtarget
85 # if a Makefile was created by configure, use it, else the top level Makefile
86 [ -f $vtarget/Makefile ] || makefile="-f $srcdir/Makefile"
87 make -C $vtarget ${makefile:-} PG_CONFIG="$PG_CONFIG" VPATH="$srcdir" USE_PGXS=1 ${MAKEVARS:-} || return $?
88 }
89
90 substvars() {
91 version="$1"
92 package="$2"
93
94 depends="postgresql-$version"
95 breaks=""
96 if [ -d "debian/$package/usr/lib/postgresql/$version/lib/bitcode" ]; then
97 # grab LLVM version from postgresql-server-dev-NN
98 dev_depends=$(dpkg-query --show --showformat='${Depends}' postgresql-server-dev-$version | grep -ow 'clang-[0-9]*')
99 llvm_version=${dev_depends#clang-}
100 if [ "$llvm_version" ]; then
101 # conflict with jit using earlier LLVM versions (bitcode is only upwards compatible)
102 breaks="${breaks:+$breaks, }postgresql-$version-jit-llvm (<< $llvm_version)"
103 fi
104 fi
105 echo "postgresql:Depends=$depends" >> "debian/$package.substvars"
106 echo "postgresql:Breaks=$breaks" >> "debian/$package.substvars"
107
108 if ! grep -q postgresql:Depends debian/control; then # compat: add to misc:Depends
109 if grep -q ^misc:Depends "debian/$package.substvars"; then
110 sed -i -e "s/^misc:Depends=/misc:Depends=$depends, /" "debian/$package.substvars"
111 else
112 echo "misc:Depends=$depends" >> "debian/$package.substvars"
113 fi
114 fi
115 }
116
117 install() {
118 prepare_env $1
119 package=`echo $opt | sed -e "s:%v:$1:g"`
120
121 mkdir -p $vtarget
122 # if a Makefile was created by configure, use it, else the top level Makefile
123 [ -f $vtarget/Makefile ] || makefile="-f $srcdir/Makefile"
124 make -C $vtarget ${makefile:-} install DESTDIR="$PWD/debian/$package" PG_CONFIG="$PG_CONFIG" VPATH="$srcdir" USE_PGXS=1 ${MAKEVARS:-} || return $?
125 substvars "$1" "$package"
126 }
127
128 clean() {
129 prepare_env $1
130 if [ "$vtarget" ]; then
131 rm -rf $vtarget
132 fi
133 }
134
135 loop() {
136 prepare_env $1
137 package=$(echo $target | sed -e "s:%v:$1:g")
138
139 echo "# $1: make"
140 make -C "$srcdir" PG_CONFIG="$PG_CONFIG" USE_PGXS=1 ${MAKEVARS:-} || return $?
141 echo "# $1: make install"
142 make -C "$srcdir" install DESTDIR="$PWD/debian/$package" PG_CONFIG="$PG_CONFIG" USE_PGXS=1 ${MAKEVARS:-} || return $?
143 substvars "$1" "$package"
144 echo "# $1: make clean"
145 make -C "$srcdir" clean PG_CONFIG="$PG_CONFIG" USE_PGXS=1 ${MAKEVARS:-} # clean errors are fatal
146 }
147
148 installcheck() {
149 prepare_env $1
150
151 # ask pg_regress to output unified diffs
152 export PG_REGRESS_DIFF_OPTS="-U3"
153
154 # ask pg_virtualenv to create a non-system cluster
155 if [ "${NONROOT-unset}" = "unset" ]; then
156 export NONROOT=1
157 fi
158
159 # if a package pattern is given, tell pg_virtualenv where the installed files are
160 if [ "$opt" ]; then
161 pkg=$(echo "$opt" | sed -e "s:%v:$1:g")
162 PKGARGS="-p $pkg"
163 DESTDIR="DESTDIR=$PWD/debian/$pkg"
164 fi
165
166 if [ "$target" ] && [ "$target" != "." ]; then # if target is given, use it, else stay in the top source dir
167 # if a Makefile was created by configure, use it, else the top level Makefile
168 [ -f $vtarget/Makefile ] || makefile="-f $srcdir/Makefile"
169 if ! pg_virtualenv ${PKGARGS:-} ${PG_VIRTUALENV_ARGS:-} -v $1 \
170 make -C $vtarget ${makefile:-} installcheck ${DESTDIR:-} \
171 PG_CONFIG="$PG_CONFIG" VPATH="$srcdir" subdir="$vtarget" USE_PGXS=1 \
172 PROVE_FLAGS=--verbose ${MAKEVARS:-}; then
173 for log in $vtarget/regression.diffs $vtarget/output_iso/regression.diffs; do
174 if [ -r $log ]; then
175 echo "**** $log ****"
176 cat $log
177 fi
178 done
179 return 1
180 fi
181 else
182 if ! pg_virtualenv ${PKGARGS:-} ${PG_VIRTUALENV_ARGS:-} -v $1 \
183 make installcheck ${DESTDIR:-} PG_CONFIG="$PG_CONFIG" subdir="$PWD" USE_PGXS=1 \
184 PROVE_FLAGS=--verbose ${MAKEVARS:-}; then
185 for log in regression.diffs output_iso/regression.diffs; do
186 if [ -r $log ]; then
187 echo "**** $log ****"
188 cat $log
189 fi
190 done
191 return 1
192 fi
193 # since we are in the top-level directory, clean up behind us
194 make clean PG_CONFIG="$PG_CONFIG" USE_PGXS=1 ${MAKEVARS:-}
195 fi
196 }
197
198 run_run () {
199 local v=$1
200 shift
201 prepare_env $v
202
203 (
204 echo + "${@//%v/$v}"
205 "${@//%v/$v}" < $PSQLTMP
206 ) || return $?
207 }
208 run_run_installed () {
209 run_run "$@"
210 }
211
212 run_psql () {
213 prepare_env $1
214
215 # ask pg_virtualenv to create a non-system cluster
216 if [ "${NONROOT-unset}" = "unset" ]; then
217 export NONROOT=1
218 fi
219
220 # if a package pattern is given, tell pg_virtualenv where the installed files are
221 if [ "$opt" ]; then
222 pkg=$(echo "$opt" | sed -e "s:%v:$1:g")
223 PKGARGS="-p $pkg"
224 export DESTDIR="$PWD/debian/$pkg"
225 fi
226
227 (
228 if [ "$target" ] && [ "$target" != "." ]; then # if target is given, use it, else stay in the top source dir
229 cd $target
230 fi
231 pg_virtualenv ${PKGARGS:-} ${PG_VIRTUALENV_ARGS:-} -v $1 \
232 psql -Xe -v ON_ERROR_STOP=1 < $PSQLTMP
233 ) || return $?
234 }
235
236 run_virtualenv () {
237 prepare_env $1
238
239 # ask pg_virtualenv to create a non-system cluster
240 if [ "${NONROOT-unset}" = "unset" ]; then
241 export NONROOT=1
242 fi
243
244 # if a package pattern is given, tell pg_virtualenv where the installed files are
245 if [ "$opt" ]; then
246 pkg=$(echo "$opt" | sed -e "s:%v:$1:g")
247 PKGARGS="-p $pkg"
248 export DESTDIR="$PWD/debian/$pkg"
249 fi
250
251 (
252 if [ "$target" ] && [ "$target" != "." ]; then # if target is given, use it, else stay in the top source dir
253 cd $target
254 fi
255 pg_virtualenv ${PKGARGS:-} ${PG_VIRTUALENV_ARGS:-} -v $1 ${SHELL:-/bin/sh} -ex < $PSQLTMP
256 ) || return $?
257 }
258
259 versions() {
260 [ -e /usr/share/postgresql-common/supported-versions ] ||
261 die "/usr/share/postgresql-common/supported-versions not found"
262 [ -e debian/pgversions ] || die "debian/pgversions not found"
263 supported_versions=$(/usr/share/postgresql-common/supported-versions)
264 local version
265 while read version; do
266 case $version in
267 all) echo "$supported_versions" ;;
268 [1-9]*+)
269 for sup_version in $supported_versions; do
270 if dpkg --compare-versions "${version%+}" le "$sup_version"; then echo "$sup_version"; fi
271 done ;;
272 [1-9]*)
273 for sup_version in $supported_versions; do
274 if [ "$version" = "$sup_version" ]; then echo "$sup_version"; fi
275 done ;;
276 '#'*) ;;
277 '') ;;
278 *) echo "Syntax error in debian/pgversions: $version" >&2 ;;
279 esac
280 done < debian/pgversions
281 }
282
283 # list of PostgreSQL versions for which packages built from this source are installed
284 # (not necessarily the same as listed in debian/control)
285 installed_versions() {
286 [ -e debian/control.in ] || die "debian/control.in not found"
287 if ! grep -q '^Package: .*PGVERSION' debian/control.in; then
288 die "No Package: PGVERSION pattern in debian/control.in found"
289 fi
290
291 # extract package name template(s) from control.in, split into prefix and suffix
292 perl -lne 'print "$1 $2" if /^Package: (.*)PGVERSION(.*)/' debian/control.in | \
293 while read prefix suffix; do
294 # translate templates to actually installed packages, and extract version numbers
295 dpkg-query --showformat '${db:Status-Status} ${Package}\n' --show "$prefix*$suffix" | \
296 grep '^installed' | cut -d ' ' -f 2 | sed -e "s/^$prefix//; s/$suffix\$//" | grep -E '^[0-9.]+$'
297 # if suffix is empty, the grep will filter out noise like '13-dbgsym'
298 done | sort -uV
299 }
300
301 gencontrol() {
302 tmpcontrol=$(mktemp debian/control.XXXXXX)
303 if [ -f debian/tests/control.in ]; then
304 tmptestscontrol=$(mktemp debian/tests/control.XXXXXX)
305 fi
306 trap "rm -f $tmpcontrol ${tmptestscontrol:-}" EXIT
307
308 export PGVERSIONS="$(versions)"
309 [ "$PGVERSIONS" ] || die "No current PostgreSQL versions are supported by this package"
310
311 perl -e \
312 '$/ = ""; # read paragraphs
313 my @pgversions = split /\s+/, $ENV{PGVERSIONS};
314 my $newest_pgversion = $pgversions[-1];
315
316 sub replace_all ($) {
317 $_ = shift;
318 my @out;
319 for my $version (@pgversions) {
320 push @out, s/PGVERSIONS/$version/rg;
321 }
322 return join ", ", @out;
323 }
324
325 while (<>) {
326 chomp;
327 if (/^Package: .*PGVERSION/) {
328 foreach my $version (@pgversions) {
329 push @out, s/PGVERSION/$version/rg;
330 }
331 } else {
332 s/(
333 [^[:space:](),]*
334 PGVERSIONS
335 [^[:space:](),]*
336 (?:\s*\([^(),]*\))? # optional part in parentheses
337 [^[:space:](),]*
338 )/replace_all($1)/xeg;
339 s/PGVERSION/$newest_pgversion/g;
340 push @out, $_;
341 }
342 }
343 print join("\n\n", @out), "\n";
344 ' debian/control.in > $tmpcontrol
345
346 if [ -f debian/tests/control.in ]; then
347 cp debian/tests/control.in $tmptestscontrol
348 # find words (package names) containing PGVERSION
349 REGEXP='[[:alnum:]-]*PGVERSION[[:alnum:]-]*'
350 for pkgpattern in $(grep -Ewo "$REGEXP" debian/tests/control.in | sort -u); do
351 repl=""
352 # build an array of replacements separated by ,
353 for v in $(versions); do
354 repl="${repl:+$repl, }$(echo $pkgpattern | sed -e "s/PGVERSION/$v/g")"
355 done
356 # put array into control file
357 grep -q "$pkgpattern" $tmptestscontrol # assert the pattern didn't get already removed by an earlier replacement
358 sed -i -e "s/$pkgpattern/$repl/" $tmptestscontrol
359 done
360 fi
361 }
362
363 updatecontrol() {
364 cat $tmpcontrol > debian/control
365
366 if [ -f debian/tests/control.in ]; then
367 cat $tmptestscontrol > debian/tests/control
368 fi
369
370 # re-check build-dependencies (no error raised)
371 ( dpkg-checkbuilddeps 2>&1 || : ) | sed -e 's/error/notice/'
372 }
373
374 # when a version is included in the action, just act on this one (this is
375 # useful if some extra work needs to be done per version, so the loop over
376 # supported-versions needs to be in the script calling pg_buildext)
377
378 case $action in
379 configure-*|build-*|install-*|clean-*|installcheck-*)
380 a=${action%%-*}
381 v=${action##$a-}
382 ret=0
383 echo "### PostgreSQL $v $a ###"
384 if $a $v; then
385 echo "### End $v $a ###"
386 else
387 ret=$?
388 echo "### End $v $a (FAILED with exit code $ret) ###"
389 fi
390 exit $ret
391 ;;
392
393 checkcontrol)
394 [ -f debian/control.in ] || exit 0 # silently exit if debian/control.in is not there
395 gencontrol
396 need_update=
397 if ! diff -u debian/control $tmpcontrol; then
398 if [ "${PG_UPDATECONTROL:-no}" != "no" ] || head -1 debian/changelog | grep -Eq -- '-backports|-pgdg|-pgapt'; then
399 echo "Notice: Updating debian/control from debian/control.in."
400 need_update=1
401 else
402 echo "Error: debian/control needs updating from debian/control.in. Run 'pg_buildext updatecontrol'."
403 echo "If you are seeing this message in a buildd log, a sourceful upload is required."
404 exit 1
405 fi
406 fi
407 if [ -f debian/tests/control.in ] && ! diff -u debian/tests/control $tmptestscontrol; then
408 echo "Notice: Updating debian/tests/control from debian/tests/control.in."
409 need_update=1
410 fi
411 [ "$need_update" ] && updatecontrol
412 exit 0
413 ;;
414
415 updatecontrol)
416 [ -f debian/control.in ] || die "debian/control.in is missing, cannot update debian/control"
417 gencontrol
418 updatecontrol
419 exit
420 ;;
421
422 clean)
423 if [ "$target" ]; then
424 pattern=$(echo "$target" | sed -e "s:%v:*:g")
425 echo rm -rf $pattern/
426 rm -rf $pattern/
427 fi
428 if [ "$opt" ]; then
429 pattern=$(echo "$opt" | sed -e "s:%v:*:g")
430 echo rm -rf debian/$pattern/ debian/$pattern.substvars
431 rm -rf debian/$pattern/ debian/$pattern.substvars
432 fi
433 if [ -f Makefile ]; then
434 make clean USE_PGXS=1
435 fi
436 exit
437 ;;
438
439 installed-versions)
440 installed_versions
441 exit
442 ;;
443
444 installcheck)
445 # prefer testing installed versions over supported versions as the set
446 # of versions might have changed (unless a package-pattern is provided)
447 if [ -f debian/control.in ] && [ -z "$opt" ]; then
448 versions=$(installed_versions)
449 else
450 versions=$(versions)
451 fi
452 [ "$versions" ] || exit 1
453 ret=0
454 for v in $versions; do
455 echo "### PostgreSQL $v $action ###"
456 if $action $v; then
457 echo "### End $v $action ###"
458 else
459 ret=$?
460 echo "### End $v $action (FAILED with exit code $ret) ###"
461 fi
462 done
463 exit $ret
464 ;;
465
466 run|run_installed|psql|virtualenv)
467 PSQLTMP=$(mktemp --tmpdir psql.XXXXXX)
468 trap "rm -f $PSQLTMP" EXIT
469 # if we are fed any input (and hence aren't reading from a terminal),
470 # store it in a tempfile
471 [ ! -t 0 ] && cat > $PSQLTMP
472
473 # prefer testing installed versions over supported versions as the set
474 # of versions might have changed (unless a package-pattern is provided)
475 if [ "$action" = "run_installed" ]; then
476 versions=$(installed_versions)
477 elif [ "$action" != "run" ] && [ -f debian/control.in ] && [ -z "$opt" ]; then
478 versions=$(installed_versions)
479 else
480 versions=$(versions)
481 fi
482 [ "$versions" ] || exit 1
483 ret=0
484 for v in $versions; do
485 echo "### PostgreSQL $v $action ###"
486 if run_$action $v "$@"; then
487 echo "### End $v $action ###"
488 else
489 ret=$?
490 echo "### End $v $action (FAILED with exit code $ret) ###"
491 fi
492 done
493 exit $ret
494 ;;
495 esac
496
497 # loop over versions
498
499 ret=0
500 for v in $(versions)
501 do
502 case "$action" in
503 "supported-versions")
504 echo $v
505 ;;
506
507 configure|build|install|clean|loop)
508 [ "$target" ] || die "syntax: pg_buildext $action <target> [<srcdir>] [<opt>]"
509 echo "### PostgreSQL $v $action ###"
510 if $action $v; then
511 echo "### End $v $action ###"
512 else
513 ret=$?
514 echo "### End $v $action (FAILED with exit code $ret) ###"
515 fi
516 ;;
517
518 *)
519 die "unsupported action '$action'"
520 ;;
521 esac
522 done
523
524 exit $ret