aboutsummaryrefslogtreecommitdiff
path: root/common/xbps-src/shutils/build_dependencies.sh
blob: 9bf37cdfc9d97d9dd31cda64717e222b61a022f2 (plain) (blame)
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
256
257
258
259
260
261
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# vim: set ts=4 sw=4 et:
#
setup_pkg_depends() {
    local pkg="$1" j _pkgdepname _pkgdep _rpkgname _depname _depver _replacement

    if [ -n "$pkg" ]; then
        # subpkg
        if declare -f ${pkg}_package >/dev/null; then
            ${pkg}_package
        fi
    fi

    for j in ${depends}; do
        _rpkgname="${j%\?*}"
        _depname="${j#*\?}"
        _pkgdepname="$($XBPS_UHELPER_CMD getpkgdepname ${_depname} 2>/dev/null)"
        if [ -z "${_pkgdepname}" ]; then
            _pkgdepname="$($XBPS_UHELPER_CMD getpkgname ${_depname} 2>/dev/null)"
        fi
        if [ -s ${XBPS_DISTDIR}/etc/virtual ]; then
            _replacement=$(egrep "^${_pkgdepname:-${_depname}}[[:blank:]]" ${XBPS_DISTDIR}/etc/virtual|cut -d ' ' -f2)
        elif [ -s ${XBPS_DISTDIR}/etc/defaults.virtual ]; then
            _replacement=$(egrep "^${_pkgdepname:-${_depname}}[[:blank:]]" ${XBPS_DISTDIR}/etc/defaults.virtual|cut -d ' ' -f2)
        fi
        if [ "${_rpkgname}" = "virtual" ]; then
            if [ -z "${_replacement}" ]; then
                msg_error "$pkgver: failed to resolve virtual dependency for '$j' (missing from etc/virtual)\n"
            fi
            _pkgdepname="$($XBPS_UHELPER_CMD getpkgdepname ${_replacement} 2>/dev/null)"
            if [ -z "${_pkgdepname}" ]; then
                _pkgdepname="$($XBPS_UHELPER_CMD getpkgname ${_replacement} 2>/dev/null)"
            fi
            if [ -z "${_pkgdepname}" ]; then
                _pkgdepname="${_replacement}>=0"
            fi
            run_depends+=" ${_depname}?${_pkgdepname}"
            #echo "Adding dependency virtual:  ${_depname}?${_pkgdepname}"
        else
            if [ -z "${_pkgdepname}" ]; then
                _pkgdep="${_depname}>=0"
            else
                _pkgdep="${_depname}"
            fi
            run_depends+=" ${_pkgdep}"
        fi
    done
    for j in ${hostmakedepends}; do
        _depname="${j%\?*}"
        _depver=$(srcpkg_get_version ${_depname}) || exit $?
        host_build_depends+=" ${_depname}-${_depver}"
    done
    if [ -n "$XBPS_CHECK_PKGS" ]; then
        for j in ${checkdepends}; do
            _depname="${j%\?*}"
            _depver=$(srcpkg_get_version ${_depname}) || exit $?
            host_check_depends+=" ${_depname}-${_depver}"
        done
    fi
    for j in ${makedepends}; do
        _depname="${j%\?*}"
        _depver=$(srcpkg_get_version ${_depname}) || exit $?
        build_depends+=" ${_depname}-${_depver}"
    done
}

# Install a required package dependency, like:
#
#	xbps-install -IAy <pkgname>
#
# Returns 0 if package already installed or installed successfully.
# Any other error number otherwise.
#
install_pkg_from_repos() {
    local pkg="$1" cross="$2" rval= tmplogf=

    mkdir -p $XBPS_STATEDIR
    tmplogf=${XBPS_STATEDIR}/xbps_${XBPS_TARGET_MACHINE}_bdep_${pkg}.log

    if [ -n "$cross" ]; then
        $XBPS_INSTALL_XCMD -IAy "$pkg" >$tmplogf 2>&1
    else
        $XBPS_INSTALL_CMD -IAy "$pkg" >$tmplogf 2>&1
    fi
    rval=$?
    # xbps-install can return:
    #
    # SUCCESS  (0): package installed successfully.
    # ENOENT   (2): package missing in repositories.
    # ENXIO    (6): package depends on invalid dependencies.
    # EAGAIN  (11): package conflicts.
    # EEXIST  (17): file conflicts in transaction (XBPS_FLAG_IGNORE_FILE_CONFLICTS unset)
    # ENODEV  (19): package depends on missing dependencies.
    # ENOTSUP (95): no repositories registered.
    #
    case "$rval" in
        0) # success, check if there are errors.
           errortmpf=$(mktemp) || exit 1
           grep ^ERROR $tmplogf > $errortmpf
           [ -s $errortmpf ] && cat $errortmpf
           rm -f $errortmpf
           ;;
        *)
           [ -z "$XBPS_KEEP_ALL" ] && remove_pkg_autodeps
           msg_red "$pkgver: failed to install '$1' dependency! (error $rval)\n"
           cat $tmplogf
           rm -f $tmplogf
           msg_error "Please see above for the real error, exiting...\n"
           ;;
    esac
    rm -f $tmplogf
    return $rval
}

#
# Returns 0 if pkgpattern in $1 is matched against current installed
# package, 1 if no match and 2 if not installed.
#
check_pkgdep_matched() {
    local pkg="$1" checkver="$2" cross="$3" uhelper= pkgn= iver=

    [ "$build_style" = "meta" ] && return 2
    [ -z "$pkg" ] && return 255

    pkgn="$($XBPS_UHELPER_CMD getpkgdepname ${pkg} 2>/dev/null)"
    if [ -z "$pkgn" ]; then
        pkgn="$($XBPS_UHELPER_CMD getpkgname ${pkg} 2>/dev/null)"
    fi
    [ -z "$pkgn" ] && return 255

    if [ -n "$cross" ]; then
        uhelper="$XBPS_UHELPER_XCMD"
    else
        uhelper="$XBPS_UHELPER_CMD"
    fi

    iver="$($uhelper $checkver $pkgn)"
    if [ $? -eq 0 -a -n "$iver" ]; then
        $XBPS_UHELPER_CMD pkgmatch "${pkgn}-${iver}" "${pkg}"
        [ $? -eq 1 ] && return 0
    else
        return 2
    fi

    return 1
}

#
# Returns 0 if pkgpattern in $1 is installed and greater than current
# installed package, otherwise 1.
#
check_installed_pkg() {
    local pkg="$1" cross="$2" uhelper= pkgn= iver=

    [ -z "$pkg" ] && return 2

    pkgn="$($XBPS_UHELPER_CMD getpkgname ${pkg})"
    [ -z "$pkgn" ] && return 2

    if [ -n "$cross" ]; then
        uhelper="$XBPS_UHELPER_XCMD"
    else
        uhelper="$XBPS_UHELPER_CMD"
    fi

    iver="$($uhelper version $pkgn)"
    if [ $? -eq 0 -a -n "$iver" ]; then
        $XBPS_CMPVER_CMD "${pkgn}-${iver}" "${pkg}"
        [ $? -eq 0 -o $? -eq 1 ] && return 0
    fi

    return 1
}

srcpkg_get_version() {
    local pkg="$1"
    # Run this in a sub-shell to avoid polluting our env.
    (
    unset XBPS_BINPKG_EXISTS
    setup_pkg $pkg || exit $?
    echo "${version}_${revision}"
    ) || msg_error "$pkgver: failed to transform dependency $pkg\n"
}

srcpkg_get_pkgver() {
    local pkg="$1"
    # Run this in a sub-shell to avoid polluting our env.
    (
    unset XBPS_BINPKG_EXISTS
    setup_pkg $pkg || exit $?
    echo "${sourcepkg}-${version}_${revision}"
    ) || msg_error "$pkgver: failed to transform dependency $pkg\n"
}

#
# Installs all dependencies required by a package.
#
install_pkg_deps() {
    local pkg="$1" targetpkg="$2" target="$3" cross="$4" cross_prepare="$5"
    local rval _realpkg _vpkg _curpkg curpkgdepname pkgn iver
    local i j found rundep repo style

    local -a host_binpkg_deps check_binpkg_deps binpkg_deps
    local -a host_missing_deps check_missing_deps missing_deps missing_rdeps

    [ -z "$pkgname" ] && return 2

    setup_pkg_depends

    [ -n "$build_style" ] && style=" [$build_style]"

    for s in $build_helper; do
        style+=" [$s]"
    done

    if [ "$pkg" != "$targetpkg" ]; then
        msg_normal "$pkgver: building${style} (dependency of $targetpkg) ...\n"
    else
        msg_normal "$pkgver: building${style} ...\n"
    fi

    if [ -z "$build_depends" -a -z "$host_build_depends" -a -z "$host_check_depends" -a -z "$run_depends" ]; then
        return 0
    fi

    #
    # Host build dependencies.
    #
    for i in ${host_build_depends}; do
        _realpkg=$($XBPS_UHELPER_CMD getpkgname "$i" 2>/dev/null)
        check_pkgdep_matched "$i" version
        local rval=$?
        if [ $rval -eq 0 ]; then
            echo "   [host] ${i}: installed."
            continue
        elif [ $rval -eq 1 ]; then
            iver=$($XBPS_UHELPER_CMD version ${_realpkg})
            if [ $? -eq 0 -a -n "$iver" ]; then
                echo "   [host] ${i}: installed $iver (virtualpkg)."
                continue
            else
                echo "   [host] ${i}: unresolved build dependency!"
                return 1
            fi
        else
            repo=$($XBPS_QUERY_CMD -R -prepository ${i} 2>/dev/null)
            if [ -n "${repo}" ]; then
                echo "   [host] ${i}: found ($repo)"
                host_binpkg_deps+=("${i}")
                continue
            else
                echo "   [host] ${i}: not found."
                if [ -z "$cross" ]; then
                    if [ "${_realpkg}" = "$targetpkg" ]; then
                        msg_error "${pkg}: [host] build loop detected: ${_realpkg} <-> ${targetpkg} [depends on itself]\n"
                    elif [ "${_realpkg}" = "$pkg" ]; then
                        msg_error "${pkg}: [host] build loop detected: $pkg <-> ${_realpkg}\n"
                    fi
                fi
            fi
        fi
        host_missing_deps+=("${i}")
    done

    #
    # Host check dependencies.
    #
    for i in ${host_check_depends}; do
        _realpkg="$($XBPS_UHELPER_CMD getpkgname $i 2>/dev/null)"
        check_pkgdep_matched "$i" version
        local rval=$?
        if [ $rval -eq 0 ]; then
            echo "   [check] ${i}: installed."
            continue
        elif [ $rval -eq 1 ]; then
            iver=$($XBPS_UHELPER_CMD version ${_realpkg})
            if [ $? -eq 0 -a -n "$iver" ]; then
                echo "   [check] ${i}: installed $iver (virtualpkg)."
                continue
            else
                echo "   [check] ${i}: unresolved check dependency!"
                return 1
            fi
        else
            repo=$($XBPS_QUERY_CMD -R -prepository ${i} 2>/dev/null)
            if [ -n "${repo}" ]; then
                echo "   [check] ${i}: found ($repo)"
                check_binpkg_deps+=("${i}")
                continue
            else
                echo "   [check] ${i}: not found."
                if [ "${_realpkg}" = "$targetpkg" ]; then
                    msg_error "${pkg}: [check] build loop detected: ${_realpkg} <-> ${targetpkg} [depends on itself]!\n"
                elif [ "${_realpkg}" = "$pkg" ]; then
                    msg_error "${pkg}: [check] build loop detected: $pkg <-> ${_realpkg}\n"
                fi
            fi
        fi
        check_missing_deps+=("${i}")
    done


    #
    # Target build dependencies.
    #
    for i in ${build_depends}; do
        _realpkg="$($XBPS_UHELPER_CMD getpkgname $i 2>/dev/null)"
        # Check if dependency is a subpkg, if it is, ignore it.
        unset found
        for j in ${subpackages}; do
            [ "$j" = "${_realpkg}" ] && found=1 && break
        done
        [ -n "$found" ] && continue
        check_pkgdep_matched "${i}" version $cross
        local rval=$?
        if [ $rval -eq 0 ]; then
            echo "   [target] ${i}: installed."
            continue
        elif [ $rval -eq 1 ]; then
            iver=$($XBPS_UHELPER_XCMD version ${_realpkg})
            if [ $? -eq 0 -a -n "$iver" ]; then
                echo "   [target] ${i}: installed $iver (virtualpkg)."
                continue
            else
                echo "   [target] ${i}: unresolved build dependency!"
                return 1
            fi
        else
            repo=$($XBPS_QUERY_XCMD -R -prepository ${i} 2>/dev/null)
            if [ -n "${repo}" ]; then
                echo "   [target] ${i}: found ($repo)"
                binpkg_deps+=("${i}")
                continue
            else
                echo "   [target] ${i}: not found."
                if [ "${_realpkg}" = "$targetpkg" ]; then
                    msg_error "${pkg}: [target] build loop detected: ${_realpkg} <-> ${targetpkg} [depends on itself]\n"
                elif [ "${_realpkg}" = "$pkg" ]; then
                    msg_error "${pkg}: [target] build loop detected: $pkg <-> ${_realpkg}\n"
                fi
            fi
        fi
        missing_deps+=("${i}")
    done

    #
    # Target run time dependencies
    #
    for i in ${run_depends}; do
        _realpkg="${i%\?*}"
        _curpkg="${_realpkg}"
        _vpkg="${i#*\?}"
        if [ "${_realpkg}" != "${_vpkg}" ]; then
            _realpkg="${_vpkg}"
        else
            unset _curpkg
        fi
        pkgn=$($XBPS_UHELPER_CMD getpkgdepname "${_realpkg}")
        if [ -z "$pkgn" ]; then
            pkgn=$($XBPS_UHELPER_CMD getpkgname "${_realpkg}")
            if [ -z "$pkgn" ]; then
                msg_error "$pkgver: invalid runtime dependency: ${_realpkg}\n"
            fi
        fi
        # Check if dependency is a subpkg, if it is, ignore it.
        unset found
        for j in ${subpackages}; do
            [ "$j" = "${pkgn}" ] && found=1 && break
        done
        [ -n "$found" ] && continue
        _props=$($XBPS_QUERY_XCMD -R -ppkgver,repository ${_realpkg} 2>/dev/null)
        if [ -n "${_props}" ]; then
            set -- ${_props}
            $XBPS_UHELPER_CMD pkgmatch ${1} "${_realpkg}"
            if [ $? -eq 1 ]; then
                if [ -n "${_curpkg}" ]; then
                    echo "   [runtime] ${_curpkg}:${_realpkg} (virtual dependency): found $1 ($2)"
                else
                    echo "   [runtime] ${_realpkg}: found $1 ($2)"
                fi
                shift 2
                continue
            else
                if [ -n "${_curpkg}" ]; then
                    echo "   [runtime] ${_curpkg}:${_realpkg} (virtual dependency): not found."
                else
                    echo "   [runtime] ${_realpkg}: not found."
                fi
            fi
            shift 2
        else
            if [ -n "${_curpkg}" ]; then
                echo "   [runtime] ${_curpkg}:${_realpkg} (virtual dependency): not found."
            else
                echo "   [runtime] ${_realpkg}: not found."
            fi
        fi
        if [ "${_realpkg}" = "$targetpkg" ]; then
            msg_error "${pkg}: [run] build loop detected: ${_realpkg} <-> ${targetpkg} [depends on itself]\n"
        elif [ "${_realpkg}" = "$pkg" ]; then
            msg_error "${pkg}: [run] build loop detected: $pkg <-> ${_realpkg}\n"
        fi
        missing_rdeps+=("${_realpkg}")
    done

    if [ -n "$XBPS_BUILD_ONLY_ONE_PKG" ]; then
           for i in ${host_missing_deps[@]}; do
                   msg_error "dep ${i} not found: -1 passed: instructed not to build\n"
           done
           for i in ${check_missing_deps[@]}; do
                   msg_error "dep ${i} not found: -1 passed: instructed not to build\n"
           done
           for i in ${missing_rdeps[@]}; do
                   msg_error "dep ${i} not found: -1 passed: instructed not to build\n"
           done
           for i in ${missing_deps[@]}; do
                   msg_error "dep ${i} not found: -1 passed: instructed not to build\n"
           done
    fi

    # Missing host dependencies, build from srcpkgs.
    for i in ${host_missing_deps[@]}; do
        # packages not found in repos, install from source.
        (
        curpkgdepname=$($XBPS_UHELPER_CMD getpkgname "$i" 2>/dev/null)
        setup_pkg $curpkgdepname
        exec env XBPS_DEPENDENCY=1 XBPS_BINPKG_EXISTS=1 \
            $XBPS_LIBEXECDIR/build.sh $sourcepkg $pkg $target || exit $?
        ) || exit $?
        host_binpkg_deps+=("$i")
    done

    # Missing check dependencies, build from srcpkgs.
    for i in ${check_missing_deps[@]}; do
        # packages not found in repos, install from source.
        (
        curpkgdepname=$($XBPS_UHELPER_CMD getpkgname "$i" 2>/dev/null)
        setup_pkg $curpkgdepname
        exec env XBPS_DEPENDENCY=1 XBPS_BINPKG_EXISTS=1 \
            $XBPS_LIBEXECDIR/build.sh $sourcepkg $pkg $target || exit $?
        ) || exit $?
        check_binpkg_deps+=("$i")
    done

    # Missing target dependencies, build from srcpkgs.
    for i in ${missing_deps[@]}; do
        # packages not found in repos, install from source.
        (

        curpkgdepname=$($XBPS_UHELPER_CMD getpkgname "$i" 2>/dev/null)
        setup_pkg $curpkgdepname $cross
        exec env XBPS_DEPENDENCY=1 XBPS_BINPKG_EXISTS=1 \
            $XBPS_LIBEXECDIR/build.sh $sourcepkg $pkg $target $cross $cross_prepare || exit $?
        ) || exit $?
        binpkg_deps+=("$i")
    done

    # Target runtime missing dependencies, build from srcpkgs.
    for i in ${missing_rdeps[@]}; do
        # packages not found in repos, install from source.
        (
        curpkgdepname=$($XBPS_UHELPER_CMD getpkgdepname "$i" 2>/dev/null)
        if [ -z "$curpkgdepname" ]; then
            curpkgdepname=$($XBPS_UHELPER_CMD getpkgname "$i" 2>/dev/null)
            if [ -z "$curpkgdepname" ]; then
                curpkgdepname="$i"
            fi
        fi
        setup_pkg $curpkgdepname $cross
        exec env XBPS_DEPENDENCY=1 XBPS_BINPKG_EXISTS=1 \
            $XBPS_LIBEXECDIR/build.sh $sourcepkg $pkg $target $cross $cross_prepare || exit $?
        ) || exit $?
    done

    if [ "$pkg" != "$targetpkg" ]; then
        msg_normal "$pkg: building${style} (dependency of $targetpkg) ...\n"
    fi

    for i in ${host_binpkg_deps[@]}; do
        msg_normal "$pkgver: installing host dependency '$i' ...\n"
        install_pkg_from_repos "${i}"
    done

    for i in ${check_binpkg_deps[@]}; do
        msg_normal "$pkgver: installing check dependency '$i' ...\n"
        install_pkg_from_repos "${i}"
    done

    for i in ${binpkg_deps[@]}; do
        msg_normal "$pkgver: installing target dependency '$i' ...\n"
        install_pkg_from_repos "$i" $cross
    done
}