pbuilder-satisfydepends-funcs 11.5 KB
Newer Older
1
#! /bin/bash
2
#   pbuilder -- personal Debian package builder
3 4 5
#   Copyright © 2001-2007 Junichi Uekawa <dancer@debian.org>
#               2007      Loïc Minier <lool@dooz.org>
#               2015      Mattia Rizzolo <mattia@debian.org>
6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
#   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.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20 21
###############################################################################

22 23
# module to satisfy build dependencies; common functions

24
. "${BASH_SOURCE%/*}/pbuilder-modules"
25

26
package_versions() {
27
    local PACKAGE="$1"
28
    LC_ALL=C $CHROOTEXEC apt-cache show "$PACKAGE" | sed -n 's/^Version: //p'
29 30
}

31
candidate_version() {
32 33
    local PACKAGE="$1"
    LC_ALL=C $CHROOTEXEC apt-cache policy "$PACKAGE" | sed -n 's/ *Candidate: //p'
34 35
}

36
checkbuilddep_versiondeps() {
37 38 39 40 41 42
    local PACKAGE="$1"
    local COMPARESTRING="$2"
    local DEPSVERSION="$3"
    local PACKAGEVERSIONS=$( package_versions "$PACKAGE" | xargs)
    # no versioned provides.
    if [ "${FORCEVERSION}" = "yes" ]; then
43
        return 0;
44 45
    fi
    for PACKAGEVERSION in $PACKAGEVERSIONS ; do
46 47 48 49
        if dpkg --compare-versions "$PACKAGEVERSION" "$COMPARESTRING" "$DEPSVERSION"; then
            # satisfies depends
            return 0;
        fi
50 51 52 53 54 55
    done
    echo "      Tried versions: $PACKAGEVERSIONS"
    # cannot satisfy depends
    return 1;
}

Loïc Minier's avatar
Loïc Minier committed
56
get_source_control_field() {
57
    get822field "$1" "$DEBIAN_CONTROL"
58
}
59

60 61 62 63 64 65
get_build_depends_field() {
    get_source_control_field "$1" | sed -n '
# Note the h; b loop; here so that there is no leading
# newline in the hold space
h
b loop
Loïc Minier's avatar
Loïc Minier committed
66 67 68
: store
H
: loop
69
$ b done
Loïc Minier's avatar
Loïc Minier committed
70
n
71 72
b store
: done
Loïc Minier's avatar
Loïc Minier committed
73
x
74 75 76 77 78 79 80 81 82 83 84
# output on single line
s/\n//g
# change series of tabs and spaces into a space
s/[\t ]\+/ /g
# normalize space before and after commas
s/ *, */, /g
# normalize space before and after pipes
s/ *| */ | /g
# normalize space before and after parentheses
s/ *( */ (/g
s/ *) */)/g
85 86 87
# normalize space before and after brackets
s/ *\[ */ [/g
s/ *\] */]/g
88
# normalize space before and after restriction list angle brackets
89 90
s/\([^(< ]\) *< */\1 </g
s/\([^(> ]\) *> */\1>/g
91 92
# normalize space between restriction lists
s/> *</> </g
93 94
# normalize space after exclamation mark
s/! */!/g
95 96 97 98 99
# normalize space between operator and version
s/(\(>>\|>=\|>\|==\|=\|<=\|<<\|<\|!=\) *\([^)]*\))/(\1 \2)/g
# normalize space at beginning and end of line
s/^ *//
s/ *$//
100
p'
101 102
}

103
get_build_deps() {
104 105
    local output

106
    output="$(get_build_depends_field "Build-Depends")"
107
    output="${output%, }"
108 109
    case "$BINARY_ARCH" in
        any)
110
            output="${output:+$output, }$(get_build_depends_field "Build-Depends-Indep")"
111
            output="${output%, }"
112
            output="${output:+$output, }$(get_build_depends_field "Build-Depends-Arch")"
113 114 115
            output="${output%, }"
            ;;
        binary)
116
            output="${output:+$output, }$(get_build_depends_field "Build-Depends-Arch")"
117 118 119
            output="${output%, }"
            ;;
        all)
120
            output="${output:+$output, }$(get_build_depends_field "Build-Depends-Indep")"
121 122 123
            output="${output%, }"
            ;;
    esac
124
    echo "$output"
125 126 127
}

get_build_conflicts() {
128 129
    local output

130
    output="$(get_build_depends_field "Build-Conflicts")"
131 132 133
    output="${output%, }"
     case "$BINARY_ARCH" in
        any)
134
            output="${output:+$output, }$(get_build_depends_field "Build-Conflicts-Indep")"
135
            output="${output%, }"
136
            output="${output:+$output, }$(get_build_depends_field "Build-Conflicts-Arch")"
137 138 139
            output="${output%, }"
            ;;
        binary)
140
            output="${output:+$output, }$(get_build_depends_field "Build-Conflicts-Arch")"
141 142 143
            output="${output%, }"
            ;;
        all)
144
            output="${output:+$output, }$(get_build_depends_field "Build-Conflicts-Indep")"
145 146 147
            output="${output%, }"
            ;;
    esac
148
    echo "$output"
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
# filter out dependencies sent on input not for this arch; deps can have
# multiple lines; output is on a single line or "" if empty
filter_arch_deps() {
    local arch="$1"
    local INSTALLPKGMULTI
    local INSTALLPKG

    # split on ","
    sed 's/[[:space:]]*,[[:space:]]*/\n/g' |
    while read INSTALLPKGMULTI; do
        echo "$INSTALLPKGMULTI" |
            # split on "|"
            sed 's/[[:space:]]*|[[:space:]]*/\n/g' |
            while read INSTALLPKG; do
                if echo "$INSTALLPKG" | grep -q '\['; then
                    if checkbuilddep_archdeps "$INSTALLPKG" "$arch"; then
                        continue
                    fi
                fi
                # output the selected package
                echo "$INSTALLPKG"
            done |
            # remove the arch list and add " | " between entries
            sed 's/\[.*\]//; $,$! s/$/ |/' |
            xargs --no-run-if-empty
    done |
    # add ", " between entries
    sed '$,$! s/$/,/' |
    xargs --no-run-if-empty
}

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
# filter out dependencies sent on input not for selected build profiles; deps
# can have multiple lines; output is on a single line or "" if empty
filter_restriction_deps() {
    local profiles="$1"
    local INSTALLPKGMULTI
    local INSTALLPKG

    # split on ","
    sed 's/[[:space:]]*,[[:space:]]*/\n/g' |
    while read INSTALLPKGMULTI; do
        echo "$INSTALLPKGMULTI" |
            # split on "|"
            sed 's/[[:space:]]*|[[:space:]]*/\n/g' |
            while read INSTALLPKG; do
                if echo "$INSTALLPKG" | grep -q '<'; then
                    if checkbuilddep_restrictiondeps "$INSTALLPKG" "$profiles"; then
                        continue
                    fi
                fi
                # output the selected package
                echo "$INSTALLPKG"
            done |
            # remove the restriction list and add " | " between entries
            sed 's/<.*>//; $,$! s/$/ |/' |
            xargs --no-run-if-empty
    done |
    # add ", " between entries
    sed '$,$! s/$/,/' |
    xargs --no-run-if-empty
}

213
checkbuilddep_archdeps() {
214 215 216
    # returns FALSE on INSTALL
    local INSTALLPKG="$1"
    local ARCH="$2"
217
    # architectures listed between [ and ] for this dep
Junichi Uekawa's avatar
Junichi Uekawa committed
218
    local DEP_ARCHES="$(echo "$INSTALLPKG" | sed -e 's/.*\[\(.*\)\].*/\1/' -e 'y|/| |')"
219 220 221 222 223 224 225
    local PKG="$(echo "$INSTALLPKG" | cut -d ' ' -f 1)"
    local USE_IT
    local IGNORE_IT
    local INCLUDE
    # Use 'dpkg-architecture' to support architecture wildcards.
    for d in $DEP_ARCHES; do
        if echo "$d" | grep -q '!'; then
226 227
            d="$(echo "$d" | sed 's/!//')"
            if dpkg-architecture -a"$ARCH" -i"$d" -f; then
228 229 230
                IGNORE_IT="yes"
            fi
        else
231
            if dpkg-architecture -a"$ARCH" -i"$d" -f; then
232 233 234 235 236 237
                USE_IT="yes"
            fi
            INCLUDE="yes"
        fi
    done
    if [ $IGNORE_IT ] && [ $USE_IT ]; then
238 239
        printf "W: inconsistent arch restriction on %s: "  "$PKG" >&2
        printf "%s depedency\n" "$DEP_ARCHES" >&2
240
    fi
241 242
    if [ $IGNORE_IT ] || ( [ $INCLUDE ] && [ ! $USE_IT ] ); then
        return 0
243
    fi
244
    return 1
245 246
}

247 248 249 250 251
checkbuilddep_restrictiondeps() {
    # returns FALSE on INSTALL
    local INSTALLPKG="$1"
    local PROFILES="$2"
    # restrictions listed between < and > for this dep
252
    local DEP_RESTRICTIONS="$(echo "$INSTALLPKG" | sed -e 's/[^<]*<\(.*\)>.*/\1/' -e 'y|/| |' -e 's/>\s\+</;/g')"
253 254 255 256 257
    local PKG="$(echo "$INSTALLPKG" | cut -d ' ' -f 1)"
    local SEEN_PROFILE
    local PROFILE
    local NEGATED
    local FOUND
258 259 260 261
    if [ "$DEP_RESTRICTIONS" = "$INSTALLPKG" ]; then
        # there is not a build profile, rather it's a version costraint
        return 1
    fi
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    IFS=';' read -ra RESTRLISTS <<< "$DEP_RESTRICTIONS"
    for restrlist in "${RESTRLISTS[@]}"; do
        SEEN_PROFILE="yes"
        for restr in $restrlist; do
            if [[ "$restr" == '!'* ]]; then
                NEGATED="yes"
                PROFILE=${restr#!}
            else
                NEGATED="no"
                PROFILE=${restr}
            fi
            FOUND="no"
            for p in $PROFILES; do
                if [ "$p" = "$PROFILE" ]; then
                    FOUND="yes"
                    break
                fi
            done
            if [ "$FOUND" = "$NEGATED" ]; then
                SEEN_PROFILE="no"
                break
            fi
        done

        if [ "$SEEN_PROFILE" = "yes" ]; then
            return 1
        fi
    done
    return 0
}

293
checkbuilddep_provides() {
294 295
    local PACKAGENAME="$1"
    # PROVIDED needs to be used outside of this function.
296
    PROVIDED=$($CHROOTEXEC apt-cache showpkg "$PACKAGENAME" \
297
        | awk '{p=0}/^Reverse Provides:/,/^$/{p=1}{if(p && ($0 !~ "Reverse Provides:")){PACKAGE=$1}} END{print PACKAGE}')
298 299 300 301
}

# returns either "package=version", to append to an apt-get install line, or
# package
302
versioneddep_to_aptcmd() {
303
    local INSTALLPKG="$1"
304

305 306 307 308 309 310
    local PACKAGE
    local PACKAGE_WITHVERSION
    local PACKAGEVERSIONS
    local CANDIDATE_VERSION
    local COMPARESTRING
    local DEPSVERSION
311

312 313
    PACKAGE="$(echo "$INSTALLPKG" | sed -e 's/^[/]*//' -e 's/[[/(].*//')"
    PACKAGE_WITHVERSION="$PACKAGE"
314

315 316 317 318 319
    # if not versionned, we skip directly to outputting $PACKAGE
    if echo "$INSTALLPKG" | grep '[(]' > /dev/null; then
        # package versions returned by APT, in reversed order
        PACKAGEVERSIONS="$( package_versions "$PACKAGE" | tac | xargs )"
        CANDIDATE_VERSION="$( candidate_version "$PACKAGE" )"
320

321 322 323 324 325 326 327 328 329
        COMPARESTRING="$(echo "$INSTALLPKG" | tr "/" " " | sed 's/^.*( *\(<<\|<=\|>=\|=\|<\|>>\|>\) *\(.*\)).*$/\1/')"
        DEPSVERSION="$(echo "$INSTALLPKG" | tr "/" " " | sed 's/^.*( *\(<<\|<=\|>=\|=\|<\|>>\|>\) *\(.*\)).*$/\2/')"
        # if strictly versionned, we skip to outputting that version
        if [ "=" = "$COMPARESTRING" ]; then
            PACKAGE_WITHVERSION="$PACKAGE=$DEPSVERSION"
        else
            # try the candidate version, then all available versions (asc)
            for VERSION in $CANDIDATE_VERSION $PACKAGEVERSIONS; do
                if dpkg --compare-versions "$VERSION" "$COMPARESTRING" "$DEPSVERSION"; then
330
                    if [ "$VERSION" != "$CANDIDATE_VERSION" ]; then
331 332 333 334 335 336 337
                        PACKAGE_WITHVERSION="$PACKAGE=$VERSION"
                    fi
                    break;
                fi
            done
        fi
    fi
338

339
    echo "$PACKAGE_WITHVERSION"
340 341
}

342
print_help() {
343 344 345 346 347
    # print out help message
    cat <<EOF
pbuilder-satisfydepends -- satisfy dependencies
Copyright 2002-2007  Junichi Uekawa <dancer@debian.org>

348 349 350 351 352 353 354
--help              give help
--control           specify control file (debian/control, *.dsc)
--chroot            operate inside chroot
--binary-all        include binary-all
--binary-arch       include binary-arch only
--binary-indep      include binary-indep only
--eatmydata         wrap the chroots commands with eatmydata
355

356
Debugging options:
357 358
--force-version     skip version check.
--continue-fail     continue even when failed.
359
--internal-chrootexec specify the command to execute instead of \`chroot\`
360
--echo              echo mode, do nothing. (--force-version required for most operation)
361

362 363 364
EOF
}