pg_buildext 7.74 KB
Newer Older
1
#!/bin/sh
2
#
3
# build a PostgreSQL module based on PGXS for given list of supported major
4 5
# versions
#
6
# (C) 2010 Dimitri Fontaine <dfontaine@hi-media.com>
7
# (C) 2011-2016 Christoph Berg <myon@debian.org>
8 9 10 11 12 13 14 15 16 17
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
18

19 20 21 22 23 24
set -eu

die() {
    echo "$(basename $0): error: $*" >&2
    exit 1
}
25

26 27 28 29 30 31 32 33 34 35 36
VENVARGS=""
while getopts "c:i:o:s" opt ; do
    case $opt in
	c|i|o) VENVARGS="$VENVARGS -$opt $OPTARG" ;;
	s) VENVARGS="$VENVARGS -$opt" ;;
	*) exit 1 ;;
    esac
done
# shift away args
shift $(($OPTIND - 1))

37
[ "${1:-}" ] || die "no action given"
38
action="$1"
39 40
if [ -d "${2:-}" ] && [ "${3:-}" ]; then # compat mode: $2 is source directory
    srcdir="${2:-}"
41 42 43 44
    shift
else
    srcdir="$PWD"
fi
45 46
target="${2:-}"
opt="${3:-}"
47

48
prepare_env() {
49
    local version=$1
50
    vtarget=`echo $target | sed -e "s:%v:$version:g"`
51
    pgc="/usr/lib/postgresql/$version/bin/pg_config"
52
    [ -e "$pgc" ] || die "$pgc does not exist"
53 54 55 56
}

configure() {
    prepare_env $1
57
    confopts=`echo $opt | sed -e "s:%v:$1:g"`
58 59 60

    mkdir -p $vtarget
    ( echo "calling configure in $vtarget" &&
61
      cd $vtarget && $srcdir/configure $confopts PG_CONFIG="$pgc" VPATH="$srcdir" )
62 63
}

64
build() {
65
    prepare_env $1
66 67 68
    if [ "$opt" ]; then
        cflags="$(echo $opt | sed -e "s:%v:$1:g")"
    fi
69 70

    mkdir -p $vtarget
71 72
    # if a Makefile was created by configure, use it, else the top level Makefile
    [ -f $vtarget/Makefile ] || makefile="-f $srcdir/Makefile"
73
    make -C $vtarget ${makefile:-} ${cflags:+CFLAGS="$cflags"} PG_CONFIG="$pgc" VPATH="$srcdir" srcdir="$srcdir" USE_PGXS=1
74 75 76 77
}

install() {
    prepare_env $1
78
    package=`echo $opt | sed -e "s:%v:$1:g"`
79 80 81 82

    mkdir -p $vtarget
    # if a Makefile was created by configure, use it, else the top level Makefile
    [ -f $vtarget/Makefile ] || makefile="-f $srcdir/Makefile"
83
    make -C $vtarget ${makefile:-} install DESTDIR="$srcdir/debian/$package" PG_CONFIG="$pgc" VPATH="$srcdir" srcdir="$srcdir" USE_PGXS=1
84 85
}

86
clean() {
87
    prepare_env $1
88 89 90

    # if a Makefile was created by configure, use it, else the top level Makefile
    [ -f $vtarget/Makefile ] || makefile="-f $srcdir/Makefile"
91
    [ -d $vtarget ] && make -C $vtarget clean ${makefile:-} PG_CONFIG="$pgc" VPATH="$srcdir" srcdir="$srcdir" USE_PGXS=1
92 93 94
    rm -rf $vtarget
}

95 96 97
loop() {
    echo "### $1 ###"
    prepare_env $1
98
    package=$(echo $target | sed -e "s:%v:$1:g")
99 100

    echo "# $1: make clean"
101
    make clean   PG_CONFIG="$pgc" USE_PGXS=1
102
    echo "# $1: make"
103
    make         PG_CONFIG="$pgc" USE_PGXS=1
104
    echo "# $1: make install"
105
    make install DESTDIR="$PWD/debian/$package" PG_CONFIG="$pgc" USE_PGXS=1
106 107 108
    echo "### done $1 ###"
}

109 110 111
installcheck() {
    prepare_env $1

112 113 114
    # ask pg_virtualenv to create a non-system cluster
    if [ "${NONROOT-unset}" = "unset" ]; then
        export NONROOT=1
115 116
    fi

117 118 119
    if [ "$target" ]; then # if target is given, use it, else stay in the top source dir
	# if a Makefile was created by configure, use it, else the top level Makefile
	[ -f $vtarget/Makefile ] || makefile="-f $srcdir/Makefile"
120
	if ! pg_virtualenv $VENVARGS -v $1 \
121
	    make -C $vtarget ${makefile:-} installcheck \
122 123 124 125 126 127 128 129
		PG_CONFIG="$pgc" VPATH="$srcdir" srcdir="$srcdir" USE_PGXS=1; then
	    if [ -r $vtarget/regression.diffs ]; then
		echo "**** $vtarget/regression.diffs ****"
		cat $vtarget/regression.diffs
	    fi
	    exit 1
	fi
    else
130
	if ! pg_virtualenv $VENVARGS -v $1 \
131 132 133 134 135 136 137 138 139 140
	    make installcheck PG_CONFIG="$pgc" srcdir="$srcdir" USE_PGXS=1; then
	    if [ -r regression.diffs ]; then
		echo "**** regression.diffs ****"
		cat regression.diffs
	    fi
	    exit 1
	fi
    fi
}

141
versions() {
142 143
    [ -e /usr/share/postgresql-common/supported-versions ] ||
	die "/usr/share/postgresql-common/supported-versions not found"
144
    [ -e debian/pgversions ] || die "debian/pgversions not found"
145
    supported_versions=$(/usr/share/postgresql-common/supported-versions)
146
    local version
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    while read version; do
	case $version in
	    all) echo "$supported_versions" ;;
	    [1-9]*+)
		for sup_version in $supported_versions; do
		    if expr $(echo "$version" | tr -d +) '<=' $sup_version > /dev/null; then echo "$sup_version"; fi
		done ;;
	    [1-9]*)
		for sup_version in $supported_versions; do
		    if [ "$version" = "$sup_version" ]; then echo "$sup_version"; fi
		done ;;
	    '#'*) ;;
	    '') ;;
	    *) echo "Syntax error in debian/pgversions: $version" >&2 ;;
	esac
    done < debian/pgversions
163 164
}

165 166
gencontrol() {
    tmpcontrol=$(mktemp debian/control.XXXXXX)
167 168 169 170 171
    if [ -f debian/tests/control.in ]; then
        tmptestscontrol=$(mktemp debian/tests/control.XXXXXX)
    fi
    trap "rm -f $tmpcontrol ${tmptestscontrol:-}" 0 2 3 15

172 173
    grep-dctrl -vP PGVERSION debian/control.in > $tmpcontrol
    for v in $(versions); do
174
        grep-dctrl -P PGVERSION debian/control.in | sed -e "s:PGVERSION:$v:g" >> $tmpcontrol
175
    done
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

    if [ -f debian/tests/control.in ]; then
        cp debian/tests/control.in $tmptestscontrol
        # find words (package names) containing PGVERSION
        REGEXP='[[:alnum:]-]*PGVERSION[[:alnum:]-]*'
        for pkgpattern in $(egrep -wo "$REGEXP" debian/tests/control.in); do
            repl=""
            # build an array of replacements separated by ,
            for v in $(versions); do
                repl="${repl:+$repl, }$(echo $pkgpattern | sed -e "s/PGVERSION/$v/g")"
            done
            # put array into control file
            sed -i -e "s/$pkgpattern/$repl/g" $tmptestscontrol
        done
    fi
}

updatecontrol() {
    cat $tmpcontrol > debian/control

    if [ -f debian/tests/control.in ]; then
        cat $tmptestscontrol > debian/tests/control
    fi
199 200
}

201 202 203 204 205 206 207 208 209 210 211 212
# when a version is included in the action, just act on this one (this is
# useful if some extra work needs to be done per version, so the loop over
# supported-versions needs to be in the script calling pg_buildext)

case $action in
    configure-*|build-*|install-*|clean-*|installcheck-*)
	a=${action%%-*}
	v=${action##$a-}
	echo "### $a $v ###"
	$a $v
	exit
	;;
213 214 215

    checkcontrol)
        gencontrol
216
        need_update=
217 218 219
        if ! diff -u debian/control $tmpcontrol; then
            if [ "${PG_UPDATECONTROL:-no}" != "no" ] || head -1 debian/changelog | egrep -q -- '-backports|-pgdg|-pgapt'; then
                echo "Notice: Updating debian/control from debian/control.in."
220 221 222 223 224
                need_update=1
            else
                echo "Error: debian/control needs updating from debian/control.in. Run 'pg_buildext updatecontrol'."
                echo "If you are seeing this message in a buildd log, a sourceful upload is required."
                exit 1
225 226
            fi
        fi
227
        if [ -f debian/tests/control.in ] && ! diff -u debian/tests/control $tmptestscontrol; then
228 229 230 231 232
            echo "Notice: Updating debian/tests/control from debian/tests/control.in."
            need_update=1
        fi
        [ "$need_update" ] && updatecontrol
        exit 0
233 234 235 236
        ;;

    updatecontrol)
        gencontrol
237
        updatecontrol
238 239
        exit
        ;;
240 241 242 243
esac

# loop over versions

244
for v in $(versions)
245 246 247 248 249 250
do
    case "$action" in
	"supported-versions")
	    echo $v
	    ;;

251
	configure|build|install|clean|loop)
252
	    [ "$target" ] || die "syntax: pg_buildext $action <target> [<opt>]"
253 254 255 256 257 258
	    echo "### $action $v ###"
	    $action $v
	    ;;

	installcheck)
	    echo "### $action $v ###"
259 260 261 262
	    $action $v
	    ;;

	*)
263
	    die "unsupported action '$action'"
264 265 266
	    ;;
    esac
done