aboutsummaryrefslogtreecommitdiff
path: root/common/xbps-src/shutils/build_dependencies.sh
blob: 3c49fc650bc870dc79de6a1ec6d9da0e24e3b64f (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
# vim: set ts=4 sw=4 et:
#
setup_pkg_depends() {
    local pkg="$1" out="$2" j _rpkgname _depname foo _deps

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

    for j in ${depends}; do
        _rpkgname="${j%\?*}"
        _depname="${j#*\?}"
        if [[ ${_rpkgname} == virtual ]]; then
            if [ -s ${XBPS_DISTDIR}/etc/virtual ]; then
                foo=$(egrep "^${_depname}[[:blank:]]" ${XBPS_DISTDIR}/etc/virtual|cut -d ' ' -f2)
            elif [ -s ${XBPS_DISTDIR}/etc/defaults.virtual ]; then
                foo=$(egrep "^${_depname}[[:blank:]]" ${XBPS_DISTDIR}/etc/defaults.virtual|cut -d ' ' -f2)
            fi
            if [ -z "$foo" ]; then
                msg_error "$pkgver: failed to resolve virtual dependency for '$j' (missing from etc/virtual)\n"
            fi
            _deps+="$foo "
        else
            foo="$($XBPS_UHELPER_CMD getpkgdepname ${_depname} 2>/dev/null)"
            if [ -z "$foo" ]; then
                foo="$($XBPS_UHELPER_CMD getpkgname ${_depname} 2>/dev/null)"
                [ -z "$foo" ] && foo="${_depname}"
            fi
            _deps+="$foo "
        fi
        run_depends+="${_depname} "
    done

    [[ $out && $_deps ]] && echo "$_deps"
    return 0
}

#
# Install required package dependencies, like:
#
#	xbps-install -AIy <pkgs>
#
#       -A automatic mode
#       -I to ignore file conflicts
#       -y yes
#
# Returns 0 if package already installed or installed successfully.
# Any other error number otherwise.
#
# 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.
#
install_pkg_from_repos() {
    local cross="$1" rval tmplogf cmd
    shift

    [ $# -eq 0 ] && return 0

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

    cmd=$XBPS_INSTALL_CMD
    [[ $cross ]] && cmd=$XBPS_INSTALL_XCMD
    $cmd ${XBPS_SKIP_REMOTEREPOS:+-i} -AIy "$@" >$tmplogf 2>&1
    rval=$?

    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 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

    uhelper=$XBPS_UHELPER_CMD
    [[ $cross ]] && uhelper=$XBPS_UHELPER_XCMD
    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
}

#
# Build all dependencies required to build and run.
#
install_pkg_deps() {
    local pkg="$1" targetpkg="$2" target="$3" cross="$4" cross_prepare="$5"
    local _vpkg curpkgdepname
    local i j found style

    local -a host_binpkg_deps binpkg_deps
    local -a host_missing_deps missing_deps missing_rdeps

    [ -z "$pkgname" ] && return 2
    [ -z "$XBPS_CHECK_PKGS" ] && unset checkdepends
    [[ $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

    #
    # Host build dependencies.
    #
    if [[ ${hostmakedepends} ]]; then
        while read -r _depname _deprepover _depver _subpkg _repourl; do
            _vpkg=${_subpkg}-${_depver}
            # binary package found in a repo
            if [[ ${_depver} == ${_deprepover} ]]; then
                echo "   [host] ${_vpkg}: found (${_repourl})"
                host_binpkg_deps+=("${_vpkg}")
                continue
            fi
            # binary package not found
            if [[ $_depname != $_subpkg ]]; then
                # subpkg, check if it's a subpkg of itself
                found=0
                for f in ${subpackages}; do
                    if [[ ${_subpkg} == ${f} ]]; then
                        found=1
                        break
                    fi
                done
                if [[ $found -eq 1 ]]; then
                    echo "   [host] ${_vpkg}: not found (subpkg, ignored)"
                else
                    echo "   [host] ${_vpkg}: not found"
                    host_missing_deps+=("$_vpkg")
                fi
            else
                echo "   [host] ${_vpkg}: not found"
                host_missing_deps+=("$_vpkg")
            fi
        done < <($XBPS_CHECKVERS_CMD ${XBPS_SKIP_REMOTEREPOS:+-i} -D $XBPS_DISTDIR -sm ${hostmakedepends})
    fi

    #
    # Host check dependencies.
    #
    if [[ ${checkdepends} ]] && [[ $XBPS_CHECK_PKGS ]]; then
        while read -r _depname _deprepover _depver _subpkg _repourl; do
            _vpkg=${_subpkg}-${_depver}
            # binary package found in a repo
            if [[ ${_depver} == ${_deprepover} ]]; then
                echo "   [check] ${_vpkg}: found (${_repourl})"
                host_binpkg_deps+=("${_vpkg}")
                continue
            fi
            # binary package not found
            if [[ $_depname != $_subpkg ]]; then
                # subpkg, check if it's a subpkg of itself
                found=0
                for f in ${subpackages}; do
                    if [[ ${_subpkg} == ${f} ]]; then
                        found=1
                        break
                    fi
                done
                if [[ $found -eq 1 ]]; then
                    echo "   [check] ${_vpkg}: not found (subpkg, ignored)"
                else
                    echo "   [check] ${_vpkg}: not found"
                    host_missing_deps+=("$_vpkg")
                fi
            else
                echo "   [check] ${_vpkg}: not found"
                host_missing_deps+=("$_vpkg")
            fi
        done < <($XBPS_CHECKVERS_CMD ${XBPS_SKIP_REMOTEREPOS:+-i} -D $XBPS_DISTDIR -sm ${checkdepends})
    fi

    #
    # Target build dependencies.
    #
    if [[ ${makedepends} ]]; then
        while read -r _depname _deprepover _depver _subpkg _repourl; do
            _vpkg=${_subpkg}-${_depver}
            # binary package found in a repo
            if [[ ${_depver} == ${_deprepover} ]]; then
                echo "   [target] ${_vpkg}: found (${_repourl})"
                binpkg_deps+=("${_vpkg}")
                continue
            fi
            # binary package not found
            if [[ $_depname != $_subpkg ]]; then
                # subpkg, check if it's a subpkg of itself
                found=0
                for f in ${subpackages}; do
                    if [[ ${_subpkg} == ${f} ]]; then
                        found=1
                        break
                    fi
                done
                if [[ $found -eq 1 ]]; then
                    echo "   [target] ${_vpkg}: not found (subpkg, ignored)"
                else
                    echo "   [target] ${_vpkg}: not found"
                    missing_deps+=("$_vpkg")
                fi
            else
                echo "   [target] ${_vpkg}: not found"
                missing_deps+=("$_vpkg")
            fi
        done < <($XBPS_CHECKVERS_XCMD ${XBPS_SKIP_REMOTEREPOS:+-i} -D $XBPS_DISTDIR -sm ${makedepends})
    fi

    #
    # Target run time dependencies
    #
    if [[ ${depends} ]]; then
        _deps=$(setup_pkg_depends "" 1)
        while read -r _depname _deprepover _depver _subpkg _repourl; do
            _vpkg=${_subpkg}-${_depver}
            # binary package found in a repo
            if [[ ${_depver} == ${_deprepover} ]]; then
                echo "   [runtime] ${_vpkg}: found (${_repourl})"
                continue
            fi
            # binary package not found
            if [[ $_depname != $_subpkg ]]; then
                # subpkg, check if it's a subpkg of itself
                found=0
                for f in ${subpackages}; do
                    if [[ ${_subpkg} == ${f} ]]; then
                        found=1
                        break
                    fi
                done
                if [[ $found -eq 1 ]]; then
                    echo "   [runtime] ${_vpkg}: not found (subpkg, ignored)"
                else
                    echo "   [runtime] ${_vpkg}: not found"
                    missing_rdeps+=("$_vpkg")
                fi
            else
                echo "   [runtime] ${_vpkg}: not found"
                missing_rdeps+=("$_vpkg")
            fi
        done < <($XBPS_CHECKVERS_XCMD ${XBPS_SKIP_REMOTEREPOS:+-i} -D $XBPS_DISTDIR -sm $_deps)
        unset _deps
    fi

    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 ${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 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 [[ ${host_binpkg_deps} ]]; then
        if [ -z "$XBPS_QUIET" ]; then
            # normal messages in bold
            [[ $NOCOLORS ]] || printf "\033[1m"
            echo "=> $pkgver: installing host dependencies: ${host_binpkg_deps[@]} ..."
            [[ $NOCOLORS ]] || printf "\033[m"
        fi
        install_pkg_from_repos "" "${host_binpkg_deps[@]}"
    fi

    if [[ ${binpkg_deps} ]]; then
        if [ -z "$XBPS_QUIET" ]; then
            # normal messages in bold
            [[ $NOCOLORS ]] || printf "\033[1m"
            echo "=> $pkgver: installing target dependencies: ${binpkg_deps[@]} ..."
            [[ $NOCOLORS ]] || printf "\033[m"
        fi
        install_pkg_from_repos "$cross" "${binpkg_deps[@]}"
    fi

    return 0
}