From 78833dc38097626f7dbee029cb17d8e9347f0bad Mon Sep 17 00:00:00 2001
From: Juan RP <xtraeme@gmail.com>
Date: Sun, 7 Jul 2019 08:23:28 +0200
Subject: [PATCH 1/2] xbps-src: multiple performance improvements.

- use xbps-checkvers(1) to resolve dependencies.
- all dependencies are installed at once for the host and target.
- the show-build-deps target is now much faster.
- the update-bulk/show-repo-updates targets are now much faster.
- the update-sys/show-sys-updates targets are now much faster.
- the bootstrap target now works on musl hosts.
- simplified some loops.
- use cut(1) rather than awk(1) where applicable.
- multiple random changes to improve performance.

Based on work started by @Duncaen on #12433

Close #12433
Close #11282
---
 .gitignore                                    |   1 +
 common/hooks/do-pkg/00-gen-pkg.sh             |   8 +-
 .../hooks/pre-pkg/04-generate-runtime-deps.sh |  41 +-
 common/hooks/pre-pkg/99-pkglint.sh            |   4 +-
 common/xbps-src/libexec/build.sh              |   4 -
 common/xbps-src/libexec/xbps-src-doinstall.sh |   2 +-
 common/xbps-src/libexec/xbps-src-prepkg.sh    |   2 +-
 common/xbps-src/shutils/build_dependencies.sh | 488 +++++++-----------
 common/xbps-src/shutils/bulk.sh               |  95 ++--
 common/xbps-src/shutils/chroot.sh             |  21 +-
 common/xbps-src/shutils/common.sh             |  16 +-
 common/xbps-src/shutils/cross.sh              |  13 +-
 common/xbps-src/shutils/pkgtarget.sh          |   4 +
 common/xbps-src/shutils/show.sh               |  41 +-
 xbps-src                                      | 326 ++++++------
 15 files changed, 446 insertions(+), 620 deletions(-)

diff --git a/.gitignore b/.gitignore
index acc6bf33479..591b3507593 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,3 +7,4 @@ masterdir*
 hostdir*
 etc/conf
 etc/virtual
+.xbps-checkvers.plist
diff --git a/common/hooks/do-pkg/00-gen-pkg.sh b/common/hooks/do-pkg/00-gen-pkg.sh
index 78ec2a1422a..c0ed088da9d 100644
--- a/common/hooks/do-pkg/00-gen-pkg.sh
+++ b/common/hooks/do-pkg/00-gen-pkg.sh
@@ -32,16 +32,16 @@ genpkg() {
 
 	_preserve=${preserve:+-p}
 	if [ -s ${PKGDESTDIR}/rdeps ]; then
-		_deps="$(cat ${PKGDESTDIR}/rdeps)"
+		_deps="$(<${PKGDESTDIR}/rdeps)"
 	fi
 	if [ -s ${PKGDESTDIR}/shlib-provides ]; then
-		_shprovides="$(cat ${PKGDESTDIR}/shlib-provides)"
+		_shprovides="$(<${PKGDESTDIR}/shlib-provides)"
 	fi
 	if [ -s ${PKGDESTDIR}/shlib-requires ]; then
-		_shrequires="$(cat ${PKGDESTDIR}/shlib-requires)"
+		_shrequires="$(<${PKGDESTDIR}/shlib-requires)"
 	fi
 	if [ -s ${XBPS_STATEDIR}/gitrev ]; then
-		_gitrevs="$(cat ${XBPS_STATEDIR}/gitrev)"
+		_gitrevs="$(<${XBPS_STATEDIR}/gitrev)"
 	fi
 
 	# Stripping whitespaces
diff --git a/common/hooks/pre-pkg/04-generate-runtime-deps.sh b/common/hooks/pre-pkg/04-generate-runtime-deps.sh
index 62077022711..6e45b7dd3c3 100644
--- a/common/hooks/pre-pkg/04-generate-runtime-deps.sh
+++ b/common/hooks/pre-pkg/04-generate-runtime-deps.sh
@@ -5,7 +5,7 @@
 #	- Generates shlib-requires file for xbps-create(1)
 
 add_rundep() {
-    local dep="$1" i= rpkgdep= _depname= _rdeps= found=
+    local dep="$1" i= rpkgdep= _depname= found=
 
     _depname="$($XBPS_UHELPER_CMD getpkgdepname ${dep} 2>/dev/null)"
     if [ -z "${_depname}" ]; then
@@ -68,26 +68,19 @@ hook() {
     exec < $depsftmp
     while read f; do
         lf=${f#${PKGDESTDIR}}
-	if [ "${skiprdeps/${lf}/}" != "${skiprdeps}" ]; then
-		msg_normal "Skipping dependency scan for ${lf}\n"
-		continue
-	fi
+	    if [ "${skiprdeps/${lf}/}" != "${skiprdeps}" ]; then
+		    msg_normal "Skipping dependency scan for ${lf}\n"
+		    continue
+	    fi
         case "$(file -bi "$f")" in
             application/x-*executable*|application/x-sharedlib*)
                 for nlib in $($OBJDUMP -p "$f"|grep NEEDED|awk '{print $2}'); do
-                    if [ -z "$verify_deps" ]; then
-                        verify_deps="$nlib"
-                        continue
-                    fi
+                    [ -z "$verify_deps" ] && verify_deps="$nlib" && continue
+                    found=0
                     for j in ${verify_deps}; do
-                        [ "$j" != "$nlib" ] && continue
-                        found_dup=1
-                        break
+                        [[ $j == $nlib ]] && found=1 && break
                     done
-                    if [ -z "$found_dup" ]; then
-                        verify_deps="$verify_deps $nlib"
-                    fi
-                    unset found_dup
+                    [[ $found -eq 0 ]] && verify_deps="$verify_deps $nlib"
                 done
                 ;;
         esac
@@ -97,13 +90,13 @@ hook() {
 
     #
     # Add required run time packages by using required shlibs resolved
-    # above, the mapping is done thru the mapping_shlib_binpkg.txt file.
+    # above, the mapping is done thru the common/shlibs file.
     #
     for f in ${verify_deps}; do
         unset _f j rdep _rdep rdepcnt soname _pkgname _rdepver found
         _f=$(echo "$f"|sed -E 's|\+|\\+|g')
-        rdep="$(grep -E "^${_f}[[:blank:]]+.*$" $mapshlibs|awk '{print $2}')"
-        rdepcnt="$(grep -E "^${_f}[[:blank:]]+.*$" $mapshlibs|awk '{print $2}'|wc -l)"
+        rdep="$(grep -E "^${_f}[[:blank:]]+.*$" $mapshlibs|cut -d ' ' -f2)"
+        rdepcnt="$(grep -E "^${_f}[[:blank:]]+.*$" $mapshlibs|cut -d ' ' -f2|wc -l)"
         if [ -z "$rdep" ]; then
             # Ignore libs by current pkg
             soname=$(find ${PKGDESTDIR} -name "$f")
@@ -121,15 +114,9 @@ hook() {
                 _pkgname=$($XBPS_UHELPER_CMD getpkgname "$j")
                 # if there's a SONAME matching pkgname, use it.
                 for x in ${pkgname} ${subpackages}; do
-                    if [ "${_pkgname}" = "${x}" ]; then
-                        found=1
-                        break
-                    fi
+                    [[ $_pkgname == $x ]] && found=1 && break
                 done
-                if [ -n "$found" ]; then
-                    _rdep=$j
-                    break
-                fi
+                [[ $found ]] && _rdep=$j && break
             done
             if [ -z "${_rdep}" ]; then
                 # otherwise pick up the first one.
diff --git a/common/hooks/pre-pkg/99-pkglint.sh b/common/hooks/pre-pkg/99-pkglint.sh
index 7ae183ae8e8..ab2b215508e 100644
--- a/common/hooks/pre-pkg/99-pkglint.sh
+++ b/common/hooks/pre-pkg/99-pkglint.sh
@@ -119,7 +119,7 @@ hook() {
 				msg_red "${pkgver}: SONAME bump detected: ${libname}.so.${conflictRev} -> ${libname}.so.${rev}\n"
 				msg_red "${pkgver}: please update common/shlibs with this line: \"${libname}.so.${rev} ${pkgver}\"\n"
 				msg_red "${pkgver}: all reverse dependencies should also be revbumped to be rebuilt against ${libname}.so.${rev}:\n"
-				_revdeps=$($XBPS_QUERY_XCMD -Rs ${libname}.so -p shlib-requires|awk '{print $1}')
+				_revdeps=$($XBPS_QUERY_XCMD -Rs ${libname}.so -p shlib-requires|cut -d ' ' -f1)
 				for x in ${_revdeps}; do
 					msg_red "   ${x%:}\n"
 				done
@@ -128,7 +128,7 @@ hook() {
 			# Try to match provided shlibs in virtual packages.
 			for f in ${provides}; do
 				_vpkgname="$($XBPS_UHELPER_CMD getpkgname ${f} 2>/dev/null)"
-				_spkgname="$(grep "^${filename}" $mapshlibs | awk '{print $2}')"
+				_spkgname="$(grep "^${filename}" $mapshlibs | cut -d ' ' -f2)"
 				_libpkgname="$($XBPS_UHELPER_CMD getpkgname ${_spkgname} 2>/dev/null)"
 				if [ -z "${_spkgname}" -o  -z "${_libpkgname}" ]; then
 					continue
diff --git a/common/xbps-src/libexec/build.sh b/common/xbps-src/libexec/build.sh
index 9908b336957..1f751ca13b9 100755
--- a/common/xbps-src/libexec/build.sh
+++ b/common/xbps-src/libexec/build.sh
@@ -43,10 +43,6 @@ if [ "$PKGNAME" != "$XBPS_TARGET_PKG" -o -z "$XBPS_SKIP_DEPS" ]; then
     install_pkg_deps $PKGNAME $XBPS_TARGET_PKG pkg $XBPS_CROSS_BUILD $XBPS_CROSS_PREPARE || exit $?
 fi
 
-if [ -z "$XBPS_CROSS_PREPARE" ]; then
-    install_cross_pkg $XBPS_CROSS_BUILD || exit $?
-fi
-
 # Fetch distfiles after installing required dependencies,
 # because some of them might be required for do_fetch().
 $XBPS_LIBEXECDIR/xbps-src-dofetch.sh $SOURCEPKG $XBPS_CROSS_BUILD || exit 1
diff --git a/common/xbps-src/libexec/xbps-src-doinstall.sh b/common/xbps-src/libexec/xbps-src-doinstall.sh
index b3d5d7276f8..14e117a923b 100755
--- a/common/xbps-src/libexec/xbps-src-doinstall.sh
+++ b/common/xbps-src/libexec/xbps-src-doinstall.sh
@@ -63,7 +63,7 @@ if [ ! -f $XBPS_SUBPKG_INSTALL_DONE ]; then
             run_func pkg_install
         fi
     fi
-    setup_pkg_depends ${pkgname:=$PKGNAME}
+    setup_pkg_depends ${pkgname:=$PKGNAME} || exit 1
     run_pkg_hooks post-install
     touch -f $XBPS_SUBPKG_INSTALL_DONE
 fi
diff --git a/common/xbps-src/libexec/xbps-src-prepkg.sh b/common/xbps-src/libexec/xbps-src-prepkg.sh
index 4be45fd0a14..34f91b3f5f9 100755
--- a/common/xbps-src/libexec/xbps-src-prepkg.sh
+++ b/common/xbps-src/libexec/xbps-src-prepkg.sh
@@ -43,7 +43,7 @@ if [ "$sourcepkg" != "$PKGNAME" ]; then
 fi
 
 source_file $XBPS_COMMONDIR/environment/build-style/${build_style}.sh
-setup_pkg_depends $pkgname
+setup_pkg_depends $pkgname || exit 1
 run_pkg_hooks pre-pkg
 
 touch -f $XBPS_PREPKG_DONE
diff --git a/common/xbps-src/shutils/build_dependencies.sh b/common/xbps-src/shutils/build_dependencies.sh
index 9bf37cdfc9d..fd327066e17 100644
--- a/common/xbps-src/shutils/build_dependencies.sh
+++ b/common/xbps-src/shutils/build_dependencies.sh
@@ -1,9 +1,9 @@
 # vim: set ts=4 sw=4 et:
 #
 setup_pkg_depends() {
-    local pkg="$1" j _pkgdepname _pkgdep _rpkgname _depname _depver _replacement
+    local pkg="$1" out="$2" j _rpkgname _depname _pkgname foo _deps
 
-    if [ -n "$pkg" ]; then
+    if [[ $pkg ]]; then
         # subpkg
         if declare -f ${pkg}_package >/dev/null; then
             ${pkg}_package
@@ -13,85 +13,67 @@ setup_pkg_depends() {
     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)"
+        if [[ ${_rpkgname} == virtual ]]; then
+            _pkgname=$(xbps-uhelper getpkgname $_depname 2>/dev/null)
+            [ -z "$_pkgname" ] && _pkgname="$_depname"
+            if [ -s ${XBPS_DISTDIR}/etc/virtual ]; then
+                foo=$(egrep "^${_pkgname}[[:blank:]]" ${XBPS_DISTDIR}/etc/virtual|cut -d ' ' -f2)
+            elif [ -s ${XBPS_DISTDIR}/etc/defaults.virtual ]; then
+                foo=$(egrep "^${_pkgname}[[:blank:]]" ${XBPS_DISTDIR}/etc/defaults.virtual|cut -d ' ' -f2)
             fi
-            if [ -z "${_pkgdepname}" ]; then
-                _pkgdepname="${_replacement}>=0"
+            if [ -z "$foo" ]; then
+                msg_error "$pkgver: failed to resolve virtual dependency for '$j' (missing from etc/virtual)\n"
             fi
-            run_depends+=" ${_depname}?${_pkgdepname}"
-            #echo "Adding dependency virtual:  ${_depname}?${_pkgdepname}"
+            _deps+="$foo "
         else
-            if [ -z "${_pkgdepname}" ]; then
-                _pkgdep="${_depname}>=0"
-            else
-                _pkgdep="${_depname}"
+            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
-            run_depends+=" ${_pkgdep}"
+            _deps+="$foo "
         fi
+        run_depends+="${_depname} "
     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
+
+    [[ $out && $_deps ]] && echo "$_deps"
+    return 0
 }
 
-# Install a required package dependency, like:
 #
-#	xbps-install -IAy <pkgname>
+# 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 pkg="$1" cross="$2" rval= tmplogf=
+    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
 
-    if [ -n "$cross" ]; then
-        $XBPS_INSTALL_XCMD -IAy "$pkg" >$tmplogf 2>&1
-    else
-        $XBPS_INSTALL_CMD -IAy "$pkg" >$tmplogf 2>&1
-    fi
+    cmd=$XBPS_INSTALL_CMD
+    [[ $cross ]] && cmd=$XBPS_INSTALL_XCMD
+    $cmd ${XBPS_SKIP_REMOTEREPOS:+-i} -AIy "$@" >$tmplogf 2>&1
     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
@@ -111,39 +93,6 @@ install_pkg_from_repos() {
     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.
@@ -156,12 +105,8 @@ check_installed_pkg() {
     pkgn="$($XBPS_UHELPER_CMD getpkgname ${pkg})"
     [ -z "$pkgn" ] && return 2
 
-    if [ -n "$cross" ]; then
-        uhelper="$XBPS_UHELPER_XCMD"
-    else
-        uhelper="$XBPS_UHELPER_CMD"
-    fi
-
+    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}"
@@ -171,42 +116,20 @@ check_installed_pkg() {
     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.
+# Build all dependencies required to build and run.
 #
 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 _vpkg curpkgdepname
+    local i j found style
 
-    local -a host_binpkg_deps check_binpkg_deps binpkg_deps
-    local -a host_missing_deps check_missing_deps missing_deps missing_rdeps
+    local -a host_binpkg_deps binpkg_deps
+    local -a host_missing_deps missing_deps missing_rdeps
 
     [ -z "$pkgname" ] && return 2
-
-    setup_pkg_depends
-
-    [ -n "$build_style" ] && style=" [$build_style]"
+    [ -z "$XBPS_CHECK_PKGS" ] && unset checkdepends
+    [[ $build_style ]] && style=" [$build_style]"
 
     for s in $build_helper; do
         style+=" [$s]"
@@ -218,197 +141,151 @@ install_pkg_deps() {
         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)."
+    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
-            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"
+            # 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
-        fi
-        host_missing_deps+=("${i}")
-    done
+        done < <($XBPS_CHECKVERS_CMD ${XBPS_SKIP_REMOTEREPOS:+-i} -D $XBPS_DISTDIR -sm ${hostmakedepends})
+    fi
 
     #
     # 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)."
+    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
-            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"
+            # 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
-        fi
-        check_missing_deps+=("${i}")
-    done
-
+        done < <($XBPS_CHECKVERS_CMD ${XBPS_SKIP_REMOTEREPOS:+-i} -D $XBPS_DISTDIR -sm ${checkdepends})
+    fi
 
     #
     # 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)."
+    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
-            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"
+            # 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
-        fi
-        missing_deps+=("${i}")
-    done
+        done < <($XBPS_CHECKVERS_XCMD ${XBPS_SKIP_REMOTEREPOS:+-i} -D $XBPS_DISTDIR -sm ${makedepends})
+    fi
 
     #
     # 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
+    if [[ ${depends} ]]; then
+        _deps=$(setup_pkg_depends "" 1) || exit 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
-            else
-                if [ -n "${_curpkg}" ]; then
-                    echo "   [runtime] ${_curpkg}:${_realpkg} (virtual dependency): not found."
+            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] ${_realpkg}: not found."
+                    echo "   [runtime] ${_vpkg}: not found"
+                    missing_rdeps+=("$_vpkg")
                 fi
-            fi
-            shift 2
-        else
-            if [ -n "${_curpkg}" ]; then
-                echo "   [runtime] ${_curpkg}:${_realpkg} (virtual dependency): not found."
             else
-                echo "   [runtime] ${_realpkg}: not found."
+                echo "   [runtime] ${_vpkg}: not found"
+                missing_rdeps+=("$_vpkg")
             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
+        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 ${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
@@ -429,18 +306,6 @@ install_pkg_deps() {
         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.
@@ -471,22 +336,25 @@ install_pkg_deps() {
         ) || exit $?
     done
 
-    if [ "$pkg" != "$targetpkg" ]; then
-        msg_normal "$pkg: building${style} (dependency of $targetpkg) ...\n"
+    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
 
-    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
+    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
 
-    for i in ${binpkg_deps[@]}; do
-        msg_normal "$pkgver: installing target dependency '$i' ...\n"
-        install_pkg_from_repos "$i" $cross
-    done
+    return 0
 }
diff --git a/common/xbps-src/shutils/bulk.sh b/common/xbps-src/shutils/bulk.sh
index 73947965282..ed5c4ce192d 100644
--- a/common/xbps-src/shutils/bulk.sh
+++ b/common/xbps-src/shutils/bulk.sh
@@ -1,83 +1,78 @@
 # vim: set ts=4 sw=4 et:
 
-bulk_getlink() {
-    local p="${1##*/}"
-    local target="$(readlink $XBPS_SRCPKGDIR/$p)"
-
-    if [ $? -eq 0 -a -n "$target" ]; then
-        p=$target
-    fi
-    echo $p
-}
-
 bulk_sortdeps() {
-    local _pkgs _pkg pkgs pkg found f x tmpf
-
-    _pkgs="$@"
-    # Iterate over the list and make sure that only real pkgs are
-    # added to our pkglist.
-    for pkg in ${_pkgs}; do
-        found=0
-        f=$(bulk_getlink $pkg)
-        for x in ${pkgs}; do
-            if [ "$x" = "${f}" ]; then
-                found=1
-                break
-            fi
-        done
-        if [ $found -eq 0 ]; then
-            pkgs+="${f} "
-        fi
-    done
+    local pkgs="$@"
+    local pkg _pkg
+    local NPROCS=$(($(nproc)*2))
+    local NRUNNING=0
 
     tmpf=$(mktemp) || exit 1
-    # Now make the real dependency graph of all pkgs to build.
-    # Perform a topological sort of all pkgs but only with build dependencies
-    # that are found in previous step.
+
+    # Perform a topological sort of all *direct* build dependencies.
     for pkg in ${pkgs}; do
-        _pkgs="$(./xbps-src show-build-deps $pkg 2>/dev/null)"
-        found=0
-        for x in ${_pkgs}; do
-            _pkg=$(bulk_getlink $x)
-            for f in ${pkgs}; do
-                if [ "${f}" != "${_pkg}" ]; then
-                    continue
-                fi
-                found=1
-                echo "${pkg} ${f}" >> $tmpf
+        if [ $NRUNNING -eq $NPROCS ]; then
+            NRUNNING=0
+            wait
+        fi
+        NRUNNING=$((NRUNNING+1))
+        (
+            for _pkg in $(./xbps-src show-build-deps $pkg 2>/dev/null); do
+                echo "$pkg $_pkg" >> $tmpf
             done
-        done
-        [ $found -eq 0 ] && echo "${pkg} ${pkg}" >> $tmpf
+            echo "$pkg $pkg" >> $tmpf
+        ) &
     done
+    wait
     tsort $tmpf|tac
     rm -f $tmpf
 }
 
 bulk_build() {
+    local sys="$1"
+    local NPROCS=$(($(nproc)*2))
+    local NRUNNING=0
 
     if [ "$XBPS_CROSS_BUILD" ]; then
         source ${XBPS_COMMONDIR}/cross-profiles/${XBPS_CROSS_BUILD}.sh
         export XBPS_ARCH=${XBPS_TARGET_MACHINE}
     fi
     if ! command -v xbps-checkvers &>/dev/null; then
-        msg_error "xbps-src: cannot find xbps-checkvers(8) command!\n"
+        msg_error "xbps-src: cannot find xbps-checkvers(1) command!\n"
     fi
 
-    bulk_sortdeps "$(xbps-checkvers ${1} --distdir=$XBPS_DISTDIR | awk '{print $2}')"
+    # Compare installed pkg versions vs srcpkgs
+    if [[ $sys ]]; then
+        xbps-checkvers -f '%n' -I -D $XBPS_DISTDIR
+        return $?
+    fi
+    # compare repo pkg versions vs srcpkgs
+    for f in $(xbps-checkvers -f '%n' -D $XBPS_DISTDIR); do
+        if [ $NRUNNING -eq $NPROCS ]; then
+            NRUNNING=0
+            wait
+        fi
+        NRUNNING=$((NRUNNING+1))
+        (
+            setup_pkg $f $XBPS_TARGET_MACHINE &>/dev/null
+            if show_avail &>/dev/null; then
+                echo "$f"
+            fi
+        ) &
+    done
+    wait
+    return $?
 }
 
 bulk_update() {
     local args="$1" pkgs f rval
 
     pkgs="$(bulk_build ${args})"
-    if [ -z "$pkgs" ]; then
-        return 0
-    fi
+    [[ -z $pkgs ]] && return 0
+
     msg_normal "xbps-src: the following packages must be rebuilt and updated:\n"
     for f in ${pkgs}; do
-        echo "   $f"
+        echo " $f"
     done
-    echo
     for f in ${pkgs}; do
         XBPS_TARGET_PKG=$f
         read_pkg
diff --git a/common/xbps-src/shutils/chroot.sh b/common/xbps-src/shutils/chroot.sh
index 17ba9dde6ca..7e28f1120dd 100644
--- a/common/xbps-src/shutils/chroot.sh
+++ b/common/xbps-src/shutils/chroot.sh
@@ -169,7 +169,7 @@ chroot_handler() {
     [ -z "$action" -a -z "$pkg" ] && return 1
 
     case "$action" in
-        fetch|extract|patch|build|check|configure|install|install-destdir|pkg|build-pkg|bootstrap-update|chroot)
+        fetch|extract|patch|configure|build|check|install|pkg|bootstrap-update|chroot)
             chroot_prepare || return $?
             chroot_init || return $?
             chroot_sync_repos || return $?
@@ -181,30 +181,13 @@ chroot_handler() {
             $XBPS_MASTERDIR $XBPS_DISTDIR "$XBPS_HOSTDIR" "$XBPS_CHROOT_CMD_ARGS" /bin/xbps-shell
         rv=$?
     else
-        [ -n "$XBPS_CROSS_BUILD" ] && arg="$arg -a $XBPS_CROSS_BUILD"
-        [ -n "$XBPS_KEEP_ALL" ] && arg="$arg -C"
-        [ -n "$NOCOLORS" ] && arg="$arg -L"
-        [ -n "$XBPS_BUILD_FORCEMODE" ] && arg="$arg -f"
-        [ -n "$XBPS_MAKEJOBS" ] && arg="$arg -j$XBPS_MAKEJOBS"
-        [ -n "$XBPS_DEBUG_PKGS" ] && arg="$arg -g"
-        [ -n "$XBPS_CHECK_PKGS" ] && arg="$arg -Q"
-        [ -n "$XBPS_BUILD_ONLY_ONE_PKG" ] && arg="$arg -1"
-        [ -n "$XBPS_QUIET" ] && arg="$arg -q"
-        [ -n "$XBPS_SKIP_DEPS" ] && arg="$arg -I"
-        [ -n "$XBPS_ALT_REPOSITORY" ] && arg="$arg -r $XBPS_ALT_REPOSITORY"
-        [ -n "$XBPS_USE_GIT_REVS" ] && arg="$arg -G"
-        [ -n "$XBPS_PKG_OPTIONS" ] && arg="$arg -o $XBPS_PKG_OPTIONS"
-        [ -n "$XBPS_TEMP_MASTERDIR" ] && arg="$arg -t -C"
-        [ -n "$XBPS_BINPKG_EXISTS" ] && arg="$arg -E"
-
-        action="$arg $action"
         env -i -- PATH="/usr/bin" SHELL=/bin/sh \
             HOME=/tmp IN_CHROOT=1 LC_COLLATE=C LANG=en_US.UTF-8 \
             SOURCE_DATE_EPOCH="$SOURCE_DATE_EPOCH" \
             XBPS_ALLOW_CHROOT_BREAKOUT="$XBPS_ALLOW_CHROOT_BREAKOUT" \
             $XBPS_COMMONDIR/chroot-style/${XBPS_CHROOT_CMD:=uunshare}.sh \
             $XBPS_MASTERDIR $XBPS_DISTDIR "$XBPS_HOSTDIR" "$XBPS_CHROOT_CMD_ARGS" \
-            /void-packages/xbps-src $action $pkg
+            /void-packages/xbps-src $XBPS_OPTIONS $action $pkg
         rv=$?
     fi
 
diff --git a/common/xbps-src/shutils/common.sh b/common/xbps-src/shutils/common.sh
index 5567619f8f3..b8d601d3fb2 100644
--- a/common/xbps-src/shutils/common.sh
+++ b/common/xbps-src/shutils/common.sh
@@ -294,7 +294,7 @@ setup_pkg() {
         XBPS_REMOVE_XCMD="env XBPS_TARGET_ARCH=$XBPS_TARGET_MACHINE $XBPS_REMOVE_CMD -r $XBPS_CROSS_BASE"
         XBPS_RINDEX_XCMD="env XBPS_TARGET_ARCH=$XBPS_TARGET_MACHINE $XBPS_RINDEX_CMD"
         XBPS_UHELPER_XCMD="env XBPS_TARGET_ARCH=$XBPS_TARGET_MACHINE xbps-uhelper -r $XBPS_CROSS_BASE"
-
+        XBPS_CHECKVERS_XCMD="env XBPS_TARGET_ARCH=$XBPS_TARGET_MACHINE xbps-checkvers -r $XBPS_CROSS_BASE --repository=$XBPS_REPOSITORY"
     else
         export XBPS_TARGET_MACHINE=${XBPS_ARCH:-$XBPS_MACHINE}
         unset XBPS_CROSS_BASE XBPS_CROSS_LDFLAGS XBPS_CROSS_FFLAGS
@@ -307,7 +307,7 @@ setup_pkg() {
         XBPS_REMOVE_XCMD="$XBPS_REMOVE_CMD"
         XBPS_RINDEX_XCMD="$XBPS_RINDEX_CMD"
         XBPS_UHELPER_XCMD="$XBPS_UHELPER_CMD"
-
+        XBPS_CHECKVERS_XCMD="$XBPS_CHECKVERS_CMD"
     fi
 
     export XBPS_INSTALL_XCMD XBPS_QUERY_XCMD XBPS_RECONFIGURE_XCMD \
@@ -402,7 +402,7 @@ setup_pkg() {
         arch="$XBPS_TARGET_MACHINE"
     fi
     if [ -n "$XBPS_BINPKG_EXISTS" ]; then
-        if [ "$($XBPS_QUERY_XCMD -R -ppkgver $pkgver 2>/dev/null)" = "$pkgver" ]; then
+        if [ "$($XBPS_QUERY_XCMD -i -R -ppkgver $pkgver 2>/dev/null)" = "$pkgver" ]; then
             exit_and_cleanup
         fi
     fi
@@ -556,17 +556,17 @@ setup_pkg() {
         wrksrc="$XBPS_BUILDDIR/$wrksrc"
     fi
 
-    if [ "$cross" -a "$nocross" -a "z$show_problems" != "zignore-problems" ]; then
+    if [ "$cross" -a "$nocross" -a "$show_problems" != "ignore-problems" ]; then
         msg_red "$pkgver: cannot be cross compiled, exiting...\n"
         msg_red "$pkgver: $nocross\n"
         exit 2
-    elif [ "$broken" -a "z$show_problems" != "zignore-problems" ]; then
+    elif [ "$broken" -a "$show_problems" != "ignore-problems" ]; then
         msg_red "$pkgver: cannot be built, it's currently broken; see the build log:\n"
         msg_red "$pkgver: $broken\n"
         exit 2
     fi
 
-    if [ -n "$restricted" -a -z "$XBPS_ALLOW_RESTRICTED" -a "z$show_problems" != "zignore-problems" ]; then
+    if [ -n "$restricted" -a -z "$XBPS_ALLOW_RESTRICTED" -a "$show_problems" != "ignore-problems" ]; then
         msg_red "$pkgver: does not allow redistribution of sources/binaries (restricted license).\n"
         msg_red "If you really need this software, run 'echo XBPS_ALLOW_RESTRICTED=yes >> etc/conf'\n"
         exit 2
@@ -575,9 +575,7 @@ setup_pkg() {
     export XBPS_STATEDIR="${XBPS_BUILDDIR}/.xbps-${sourcepkg}"
     export XBPS_WRAPPERDIR="${XBPS_STATEDIR}/wrappers"
 
-    if [ -n "$bootstrap" -a -z "$CHROOT_READY" -o -n "$IN_CHROOT" ]; then
-        mkdir -p $XBPS_WRAPPERDIR
-    fi
+    mkdir -p $XBPS_STATEDIR $XBPS_WRAPPERDIR
 
     source_file $XBPS_COMMONDIR/environment/build-style/${build_style}.sh
 
diff --git a/common/xbps-src/shutils/cross.sh b/common/xbps-src/shutils/cross.sh
index 12c9efa9b49..e72100986dd 100644
--- a/common/xbps-src/shutils/cross.sh
+++ b/common/xbps-src/shutils/cross.sh
@@ -24,11 +24,12 @@ remove_pkg_cross_deps() {
 
 prepare_cross_sysroot() {
     local cross="$1"
+    local statefile="$XBPS_MASTERDIR/.xbps-${cross}-done"
 
-    [ -z "$cross" -o "$cross" = "" ] && return 0
+    [ -z "$cross" -o "$cross" = "" -o -f $statefile ] && return 0
 
     # Check for cross-vpkg-dummy available for the target arch, otherwise build it.
-    pkg_available 'cross-vpkg-dummy>=0.30_1' $cross
+    pkg_available 'cross-vpkg-dummy>=0.31_1' $cross
     if [ $? -eq 0 ]; then
         $XBPS_LIBEXECDIR/build.sh cross-vpkg-dummy cross-vpkg-dummy pkg $cross init || return $?
     fi
@@ -40,7 +41,7 @@ prepare_cross_sysroot() {
     errlog=$(mktemp) || exit 1
     $XBPS_INSTALL_XCMD -Syfd cross-vpkg-dummy &>$errlog
     rval=$?
-    if [ $rval -ne 0 -a $rval -ne 17 ]; then
+    if [ $rval -ne 0 ]; then
         msg_red "failed to install cross-vpkg-dummy (error $rval)\n"
         cat $errlog
         rm -f $errlog
@@ -48,11 +49,15 @@ prepare_cross_sysroot() {
     fi
     rm -f $errlog
     # Create top level symlinks in sysroot.
-    XBPS_ARCH=$XBPS_TARGET_MACHINE xbps-reconfigure -r $XBPS_CROSS_BASE -f base-directories base-files &>/dev/null
+    XBPS_ARCH=$XBPS_TARGET_MACHINE xbps-reconfigure -r $XBPS_CROSS_BASE -f base-files &>/dev/null
     # Create a sysroot/include and sysroot/lib symlink just in case.
     ln -s usr/include ${XBPS_CROSS_BASE}/include
     ln -s usr/lib ${XBPS_CROSS_BASE}/lib
 
+    install_cross_pkg $cross || return 1
+
+    touch -f $statefile
+
     return 0
 }
 
diff --git a/common/xbps-src/shutils/pkgtarget.sh b/common/xbps-src/shutils/pkgtarget.sh
index a7be624173e..7d3ed1c78f4 100644
--- a/common/xbps-src/shutils/pkgtarget.sh
+++ b/common/xbps-src/shutils/pkgtarget.sh
@@ -54,6 +54,10 @@ remove_pkg_autodeps() {
     $XBPS_RECONFIGURE_CMD -a >> $tmplogf 2>&1
     echo yes | $XBPS_REMOVE_CMD -Ryod >> $tmplogf 2>&1
     rval=$?
+    if [ $rval -eq 0 ]; then
+        echo yes | $XBPS_REMOVE_CMD -Ryod >> $tmplogf 2>&1
+        rval=$?
+    fi
 
     if [ $rval -ne 0 ]; then
         msg_red "${pkgver:-xbps-src}: failed to remove autodeps: (returned $rval)\n"
diff --git a/common/xbps-src/shutils/show.sh b/common/xbps-src/shutils/show.sh
index e2fc956514e..67b2ec14810 100644
--- a/common/xbps-src/shutils/show.sh
+++ b/common/xbps-src/shutils/show.sh
@@ -61,44 +61,22 @@ show_avail() {
 }
 
 show_pkg_build_depends() {
-    local f x _pkgname _srcpkg _dep found result
+    local f x _pkgname _srcpkg found result
     local _deps="$1"
 
     result=$(mktemp) || exit 1
 
     # build time deps
     for f in ${_deps}; do
-        # ignore virtual deps
-        local _rpkg="${f%\?*}"
-        local _vpkg="${f#*\?}"
-
         # ignore virtual dependencies
-        if [ "${_rpkg}" != "${_vpkg}" ]; then
-            f="${_vpkg}"
-        fi
+        [[ ${f%\?*} != ${f#*\?} ]] && f=${f#*\?}
         unset found
         # check for subpkgs
         for x in ${subpackages}; do
-            _pkgname="$($XBPS_UHELPER_CMD getpkgdepname $f 2>/dev/null)"
-            if [ -z "${_pkgname}" ]; then
-                _pkgname="$($XBPS_UHELPER_CMD getpkgname $f 2>/dev/null)"
-            fi
-            if [ "${_pkgname}" = "$x" ]; then
-                found=1
-                break
-            fi
+            [[ $f == $x ]] && found=1 && break
         done
-        if [ -n "$found" ]; then
-            continue
-        fi
-        _pkgname="$($XBPS_UHELPER_CMD getpkgdepname $f 2>/dev/null)"
-        if [ -z "${_pkgname}" ]; then
-            _pkgname="$($XBPS_UHELPER_CMD getpkgname $f 2>/dev/null)"
-        fi
-        if [ -z "${_pkgname}" ]; then
-            _pkgname="$f"
-        fi
-        _pkgname=${_pkgname/-32bit}
+        [[ $found ]] && continue
+        _pkgname=${f/-32bit}
         _srcpkg=$(readlink -f ${XBPS_SRCPKGDIR}/${_pkgname})
         _srcpkg=${_srcpkg##*/}
         echo "${_srcpkg}" >> $result
@@ -108,18 +86,15 @@ show_pkg_build_depends() {
 }
 
 show_pkg_build_deps() {
-    setup_pkg_depends
-    show_pkg_build_depends "${host_build_depends} ${build_depends} ${run_depends}"
+    show_pkg_build_depends "${hostmakedepends} ${makedepends} $(setup_pkg_depends '' 1)"
 }
 
 show_pkg_hostmakedepends() {
-    setup_pkg_depends
-    show_pkg_build_depends "${host_build_depends}"
+    show_pkg_build_depends "${hostmakedepends}"
 }
 
 show_pkg_makedepends() {
-    setup_pkg_depends
-    show_pkg_build_depends "${build_depends}"
+    show_pkg_build_depends "${makedepends}"
 }
 
 show_pkg_build_options() {
diff --git a/xbps-src b/xbps-src
index 23d6164b18a..c7c41f47193 100755
--- a/xbps-src
+++ b/xbps-src
@@ -1,8 +1,6 @@
 #!/bin/bash
 # vim: set ts=4 sw=4 et:
 
-readonly PROGNAME="${0##*/}"
-
 print_cross_targets() {
     local f
     for f in common/cross-profiles/*.sh; do
@@ -210,7 +208,7 @@ _EOF
 check_reqhost_utils() {
     local broken
 
-    [ -n "$IN_CHROOT" ] && return 0
+    [ "$IN_CHROOT" ] && return 0
 
     for f in ${REQHOST_UTILS}; do
         if ! command -v ${f} &>/dev/null; then
@@ -218,7 +216,7 @@ check_reqhost_utils() {
             broken=1
         fi
     done
-    [ -n "$broken" ] && exit 1
+    [ "$broken" ] && exit 1
     [ -z "$1" ] && return 0
 
     for f in ${REQHOST_UTILS_BOOTSTRAP}; do
@@ -227,24 +225,7 @@ check_reqhost_utils() {
             broken=1
         fi
     done
-    [ -n "$broken" ] && exit 1
-}
-
-check_config_vars() {
-    if [ -s "$XBPS_CONFIG_FILE" ]; then
-        . $XBPS_CONFIG_FILE &>/dev/null
-    fi
-    if [ -z "$XBPS_MASTERDIR" ]; then
-        export XBPS_MASTERDIR="${XBPS_DISTDIR}/masterdir"
-    fi
-    if [ -z "$XBPS_HOSTDIR" ]; then
-        export XBPS_HOSTDIR="${XBPS_DISTDIR}/hostdir"
-        [ ! -d $XBPS_HOSTDIR ] && mkdir -p $XBPS_HOSTDIR
-    fi
-    if [ -d "$XBPS_MASTERDIR" -a ! -w "$XBPS_MASTERDIR" ]; then
-        echo "ERROR: not enough perms for masterdir $XBPS_MASTERDIR."
-        exit 1
-    fi
+    [ "$broken" ] && exit 1
 }
 
 check_build_requirements() {
@@ -264,20 +245,42 @@ check_build_requirements() {
     fi
 }
 
+chroot_check() {
+    if [ -f $XBPS_MASTERDIR/.xbps_chroot_init -o "$XBPS_CHROOT_CMD" = "ethereal" ]; then
+        export CHROOT_READY=1
+    fi
+}
+
+check_native_arch() {
+    if [ "$CHROOT_READY" ]; then
+        if [ -s $XBPS_MASTERDIR/.xbps_chroot_init ]; then
+            export XBPS_ARCH=$(<$XBPS_MASTERDIR/.xbps_chroot_init)
+        else
+            export XBPS_ARCH=$(xbps-uhelper arch)
+        fi
+    else
+        LDD=$(ldd --version 2>&1|head -1)
+        if [[ $LDD == *musl* ]]; then
+            export XBPS_ARCH=${XBPS_MACHINE%-musl}-musl
+        else
+            # XBPS_ARCH == $(uname -m)
+            export XBPS_ARCH=$(uname -m)
+        fi
+    fi
+}
+
 install_bbootstrap() {
-    [ -n "$CHROOT_READY" ] && return
+    [ "$CHROOT_READY" ] && return
     if [ "$1" = "bootstrap" ]; then
         unset XBPS_TARGET_PKG XBPS_INSTALL_ARGS
     else
         XBPS_TARGET_PKG="$1"
     fi
-    if [ -n "$XBPS_SKIP_REMOTEREPOS" ]; then
-        unset XBPS_INSTALL_ARGS
-    fi
+    [ "$XBPS_SKIP_REMOTEREPOS" ] && unset XBPS_INSTALL_ARGS
     # binary bootstrap
     msg_normal "Installing bootstrap from binary package repositories...\n"
     # XBPS_TARGET_PKG == arch
-    if [ -n "$XBPS_TARGET_PKG" ]; then
+    if [ "$XBPS_TARGET_PKG" ]; then
         _bootstrap_arch="env XBPS_TARGET_ARCH=$XBPS_TARGET_PKG"
         if [ "${XBPS_TARGET_PKG}" != "${XBPS_TARGET_PKG#*-}" ]; then
             _subarch="-${XBPS_TARGET_PKG#*-}"
@@ -308,17 +311,15 @@ reconfigure_bootstrap_pkgs() {
     # Reconfigure ca-certificates.
     msg_normal "Reconfiguring bootstrap packages...\n"
     for f in ${pkgs}; do
-        if xbps-query $f &>/dev/null; then
-            xbps-reconfigure -f $f
+        if xbps-query -r $XBPS_MASTERDIR $f &>/dev/null; then
+            xbps-reconfigure -r $XBPS_MASTERDIR -f $f
         fi
     done
     touch -f $statefile
 }
 
 bootstrap_update() {
-    if [ -z "$CHROOT_READY" ]; then
-        return
-    fi
+    [ -z "$CHROOT_READY" ] && return
     remove_pkg_autodeps
     msg_normal "xbps-src: cleaning up masterdir...\n"
     rm -rf $XBPS_MASTERDIR/builddir $XBPS_MASTERDIR/destdir
@@ -347,7 +348,7 @@ masterdir_zap() {
 
 exit_func() {
     wait
-    if [ -n "$sourcepkg" ]; then
+    if [ "$sourcepkg" ]; then
         remove_pkg $XBPS_CROSS_BUILD
     fi
     if [ -z "$IN_CHROOT" ]; then
@@ -390,14 +391,17 @@ setup_distfiles_mirror() {
     done
 }
 
-readonly XBPS_VERSION_REQ="0.55"
-readonly XBPS_VERSION=$(xbps-uhelper -V|awk '{print $2}')
-readonly XBPS_SRC_VERSION="113"
-export XBPS_MACHINE=$(xbps-uhelper arch)
-
 #
 # main()
 #
+readonly PROGNAME="${0##*/}"
+readonly XBPS_VERSION_REQ="0.55"
+XBPS_VERSION=$(xbps-uhelper -V)
+XBPS_VERSION=${XBPS_VERSION%%API*}
+XBPS_VERSION=${XBPS_VERSION##*:}
+readonly XBPS_SRC_VERSION="113"
+export XBPS_MACHINE=$(xbps-uhelper -C /dev/null arch)
+
 XBPS_OPTIONS=
 XBPS_OPTSTRING="1a:CEfgGhH:iIj:Lm:No:qQr:tV"
 
@@ -407,26 +411,26 @@ eval set -- $(getopt "$XBPS_OPTSTRING" "$@");
 while getopts "$XBPS_OPTSTRING" opt; do
     case $opt in
         1) export XBPS_BUILD_ONLY_ONE_PKG=yes; XBPS_OPTIONS+=" -1";;
-        a) readonly XBPS_CROSS_BUILD="$OPTARG"; XBPS_OPTIONS+=" -a $OPTARG";;
-        C) readonly XBPS_KEEP_ALL=1; XBPS_OPTIONS+=" -C";;
+        a) export XBPS_CROSS_BUILD="$OPTARG"; XBPS_OPTIONS+=" -a $OPTARG";;
+        C) export XBPS_KEEP_ALL=1; XBPS_OPTIONS+=" -C";;
         E) export XBPS_BINPKG_EXISTS=1; XBPS_OPTIONS+=" -E";;
-        f) readonly XBPS_BUILD_FORCEMODE=1; XBPS_OPTIONS+=" -f";;
-        G) readonly XBPS_USE_GIT_REVS=1; XBPS_OPTIONS+=" -G";;
-        g) readonly XBPS_DEBUG_PKGS=1; XBPS_OPTIONS+=" -g";;
-        H) readonly XBPS_HOSTDIR="$(readlink -f $OPTARG 2>/dev/null)"; XBPS_OPTIONS+=" -H $XBPS_HOSTDIR";;
+        f) export XBPS_BUILD_FORCEMODE=1; XBPS_OPTIONS+=" -f";;
+        G) export XBPS_USE_GIT_REVS=1; XBPS_OPTIONS+=" -G";;
+        g) export XBPS_DEBUG_PKGS=1; XBPS_OPTIONS+=" -g";;
+        H) export XBPS_HOSTDIR="$(readlink -f $OPTARG 2>/dev/null)"; XBPS_OPTIONS+=" -H $XBPS_HOSTDIR";;
         h) usage && exit 0;;
         i) export XBPS_INFORMATIVE_RUN=1; XBPS_OPTIONS+=" -i";;
-        I) readonly XBPS_SKIP_DEPS=1; XBPS_OPTIONS+=" -I";;
+        I) export XBPS_SKIP_DEPS=1; XBPS_OPTIONS+=" -I";;
         j) export XBPS_MAKEJOBS="$OPTARG"; XBPS_OPTIONS+=" -j $OPTARG";;
         L) export NOCOLORS=1; XBPS_OPTIONS+=" -L";;
-        m) readonly XBPS_MASTERDIR=$(readlink -f $OPTARG 2>/dev/null); XBPS_OPTIONS+=" -m $XBPS_MASTERDIR";;
-        N) readonly XBPS_SKIP_REMOTEREPOS=1; XBPS_OPTIONS+=" -N";;
-        o) readonly XBPS_PKG_OPTIONS="$OPTARG"; XBPS_OPTIONS+=" -o $OPTARG";;
+        m) export XBPS_MASTERDIR=$(readlink -f $OPTARG 2>/dev/null); XBPS_OPTIONS+=" -m $XBPS_MASTERDIR";;
+        N) export XBPS_SKIP_REMOTEREPOS=1; XBPS_OPTIONS+=" -N";;
+        o) export XBPS_PKG_OPTIONS="$OPTARG"; XBPS_OPTIONS+=" -o $OPTARG";;
         q) export XBPS_QUIET=1; XBPS_OPTIONS+=" -q";;
         Q) export XBPS_CHECK_PKGS=1; XBPS_OPTIONS+=" -Q";;
-        r) readonly XBPS_ALT_REPOSITORY="$OPTARG"; XBPS_OPTIONS+=" -r $OPTARG";;
+        r) export XBPS_ALT_REPOSITORY="$OPTARG"; XBPS_OPTIONS+=" -r $OPTARG";;
         t) export XBPS_TEMP_MASTERDIR=1; XBPS_OPTIONS+=" -t -C";;
-        V) echo $XBPS_SRC_VERSION && exit 0;;
+        V) echo "xbps-src-$XBPS_SRC_VERSION $(xbps-uhelper -V)" && exit 0;;
         --) shift; break;;
     esac
 done
@@ -437,10 +441,11 @@ shift $(($OPTIND - 1))
 # Check if stdout is a tty; if false disable colors.
 test -t 1 || export NOCOLORS=1
 # http://no-color.org
-if [ -n "${NO_COLOR+x}" ]; then
+if [ "${NO_COLOR+x}" ]; then
     export NOCOLORS=1
 fi
 
+# sane umask
 umask 022
 
 #
@@ -456,22 +461,13 @@ readonly REQHOST_UTILS="xbps-install xbps-query xbps-rindex xbps-uhelper \
 
 check_reqhost_utils
 
-# Try using chroot-git then git from the host system
-if command -v chroot-git &>/dev/null; then
-	XBPS_GIT_CMD=$(command -v chroot-git)
-elif command -v git &>/dev/null; then
-	XBPS_GIT_CMD=$(command -v git)
-else
-    echo "neither chroot-git or git are available in your system!"
-    exit 1
-fi
-
-readonly XBPS_GIT_CMD
-
-if [ -n "$IN_CHROOT" ]; then
+#
+# Set XBPS_CONFIG_FILE, XBPS_DISTDIR, XBPS_MASTERDIR
+# and XBPS_HOSTDIR.
+#
+if [ "$IN_CHROOT" ]; then
     readonly XBPS_CONFIG_FILE=/etc/xbps/xbps-src.conf
     readonly XBPS_DISTDIR=/void-packages
-    # needed before call to check_config_vars
     readonly XBPS_MASTERDIR=/
     readonly XBPS_HOSTDIR=/host
 else
@@ -491,18 +487,37 @@ else
     elif [ -s $HOME/.xbps-src.conf ]; then
         # ... fallback to ~/.xbps-src.conf otherwise.
         readonly XBPS_CONFIG_FILE=$HOME/.xbps-src.conf
-        . $XBPS_CONFIG_FILE
     fi
 fi
+# Read settings from config file
+[ -s "$XBPS_CONFIG_FILE" ] && . $XBPS_CONFIG_FILE &>/dev/null
 
-#
-# Check configuration vars before anyting else, and set defaults vars.
-#
-check_config_vars
+# if XBPS_MASTERDIR unset, defaults to $XBPS_DISTDIR/masterdir.
+: ${XBPS_MASTERDIR:=$XBPS_DISTDIR/masterdir}
+[ ! -d $XBPS_MASTERDIR ] &&  mkdir -p $XBPS_MASTERDIR
 
-for f in $XBPS_DISTDIR/common/xbps-src/shutils/*.sh; do
-    [ -r $f ] && . $f
-done
+# if XBPS_HOSTDIR unset, defaults to $XBPS_DISTDIR/hostdir.
+: ${XBPS_HOSTDIR:=$XBPS_DISTDIR/hostdir}
+[ ! -d $XBPS_HOSTDIR ] && mkdir -p $XBPS_HOSTDIR
+
+if [ -d "$XBPS_MASTERDIR" -a ! -w "$XBPS_MASTERDIR" ]; then
+    echo "ERROR: can't write to masterdir $XBPS_MASTERDIR."
+    exit 1
+elif [ -d "$XBPS_HOSTDIR" -a ! -w "$XBPS_HOSTDIR" ]; then
+    echo "ERROR: can't write to hostdir: $XBPS_HOSTDIR."
+    exit 1
+fi
+
+# Try using chroot-git then git from the host system
+if command -v chroot-git &>/dev/null; then
+    XBPS_GIT_CMD=$(command -v chroot-git)
+elif command -v git &>/dev/null; then
+    XBPS_GIT_CMD=$(command -v git)
+else
+    echo "neither chroot-git or git are available in your system!"
+    exit 1
+fi
+readonly XBPS_GIT_CMD
 
 if [ -n "$XBPS_HOSTDIR" ]; then
     export XBPS_REPOSITORY=$XBPS_HOSTDIR/binpkgs
@@ -516,80 +531,62 @@ fi
 if [ -z "$XBPS_ALT_REPOSITORY" ]; then
     pushd "$PWD" &>/dev/null
     cd $XBPS_DISTDIR
-    if [ -n "$IN_CHROOT" ]; then
-        _gitbranch="$(chroot-git symbolic-ref --short HEAD 2>/dev/null)"
-    else
-        _gitbranch="$($XBPS_GIT_CMD symbolic-ref --short HEAD 2>/dev/null)"
-    fi
-    if [ -n "${_gitbranch}" -a "${_gitbranch}" != "master" ]; then
+    _gitbranch="$($XBPS_GIT_CMD symbolic-ref --short HEAD 2>/dev/null)"
+    if [ "${_gitbranch}" -a "${_gitbranch}" != "master" ]; then
         export XBPS_ALT_REPOSITORY="${_gitbranch}"
         export XBPS_REPOSITORY="${XBPS_REPOSITORY}/${_gitbranch}"
-        if [ -z "$IN_CHROOT" ]; then
-            msg_normal "Using \`$XBPS_REPOSITORY\' as local repository.\n"
-        fi
      fi
      popd &>/dev/null
 else
     export XBPS_REPOSITORY="${XBPS_REPOSITORY}/${XBPS_ALT_REPOSITORY}"
-    if [ -z "$IN_CHROOT" ]; then
-        msg_normal "Using \`$XBPS_REPOSITORY\' as local repository.\n"
-    fi
 fi
 
-if [ -n "$IN_CHROOT" ]; then
+readonly XBPS_SRCPKGDIR=$XBPS_DISTDIR/srcpkgs
+readonly XBPS_COMMONDIR=$XBPS_DISTDIR/common
+readonly XBPS_SHUTILSDIR=$XBPS_COMMONDIR/xbps-src/shutils
+readonly XBPS_TRIGGERSDIR=$XBPS_SRCPKGDIR/xbps-triggers/files
+readonly XBPS_CROSSPFDIR=$XBPS_COMMONDIR/cross-profiles
+readonly XBPS_BUILDSTYLEDIR=$XBPS_COMMONDIR/build-style
+readonly XBPS_LIBEXECDIR=$XBPS_COMMONDIR/xbps-src/libexec
+readonly XBPS_BUILDHELPERDIR=$XBPS_COMMONDIR/build-helper
+
+if [ "$IN_CHROOT" ]; then
     readonly XBPS_UHELPER_CMD="xbps-uhelper"
-    readonly XBPS_INSTALL_CMD="xbps-install"
-    readonly XBPS_QUERY_CMD="xbps-query"
-    readonly XBPS_RINDEX_CMD="xbps-rindex"
+    readonly XBPS_INSTALL_CMD="xbps-install --repository=$XBPS_REPOSITORY"
+    readonly XBPS_QUERY_CMD="xbps-query --repository=$XBPS_REPOSITORY"
     readonly XBPS_RECONFIGURE_CMD="xbps-reconfigure"
     readonly XBPS_REMOVE_CMD="xbps-remove"
-    readonly XBPS_SRCPKGDIR=/void-packages/srcpkgs
-    readonly XBPS_COMMONDIR=/void-packages/common
+    readonly XBPS_CHECKVERS_CMD="xbps-checkvers --repository=$XBPS_REPOSITORY"
     readonly XBPS_DESTDIR=/destdir
     readonly XBPS_BUILDDIR=/builddir
-    readonly XBPS_SHUTILSDIR=$XBPS_COMMONDIR/xbps-src/shutils
-    readonly XBPS_TRIGGERSDIR=$XBPS_SRCPKGDIR/xbps-triggers/files
-    readonly XBPS_CROSSPFDIR=$XBPS_COMMONDIR/cross-profiles
-    readonly XBPS_BUILDSTYLEDIR=$XBPS_COMMONDIR/build-style
-    readonly XBPS_LIBEXECDIR=$XBPS_COMMONDIR/xbps-src/libexec
-    readonly XBPS_BUILDHELPERDIR=$XBPS_COMMONDIR/build-helper
 else
     readonly XBPS_UHELPER_CMD="xbps-uhelper -r $XBPS_MASTERDIR"
-    readonly XBPS_INSTALL_CMD="xbps-install -C /dev/null -c $XBPS_HOSTDIR/repocache --repository=$XBPS_REPOSITORY -r $XBPS_MASTERDIR"
-    readonly XBPS_QUERY_CMD="xbps-query -C /dev/null -c $XBPS_HOSTDIR/repocache -i --repository=$XBPS_REPOSITORY -r $XBPS_MASTERDIR"
-    readonly XBPS_RINDEX_CMD="xbps-rindex"
+    readonly XBPS_INSTALL_CMD="xbps-install -c $XBPS_HOSTDIR/repocache --repository=$XBPS_REPOSITORY -r $XBPS_MASTERDIR"
+    readonly XBPS_QUERY_CMD="xbps-query -c $XBPS_HOSTDIR/repocache --repository=$XBPS_REPOSITORY -r $XBPS_MASTERDIR"
     readonly XBPS_RECONFIGURE_CMD="xbps-reconfigure -r $XBPS_MASTERDIR"
     readonly XBPS_REMOVE_CMD="xbps-remove -r $XBPS_MASTERDIR"
-    readonly XBPS_SRCPKGDIR=$XBPS_DISTDIR/srcpkgs
-    readonly XBPS_COMMONDIR=$XBPS_DISTDIR/common
-    readonly XBPS_SHUTILSDIR=$XBPS_COMMONDIR/xbps-src/shutils
+    readonly XBPS_CHECKVERS_CMD="xbps-checkvers --repository=$XBPS_REPOSITORY"
     readonly XBPS_DESTDIR=$XBPS_MASTERDIR/destdir
     readonly XBPS_BUILDDIR=$XBPS_MASTERDIR/builddir
-    readonly XBPS_TRIGGERSDIR=$XBPS_SRCPKGDIR/xbps-triggers/files
-    readonly XBPS_CROSSPFDIR=$XBPS_COMMONDIR/cross-profiles
-    readonly XBPS_BUILDSTYLEDIR=$XBPS_COMMONDIR/build-style
-    readonly XBPS_LIBEXECDIR=$XBPS_COMMONDIR/xbps-src/libexec
-    readonly XBPS_BUILDHELPERDIR=$XBPS_COMMONDIR/build-helper
 fi
+readonly XBPS_RINDEX_CMD="xbps-rindex"
 readonly XBPS_FETCH_CMD="xbps-fetch"
 readonly XBPS_DIGEST_CMD="xbps-digest"
 readonly XBPS_CMPVER_CMD="xbps-uhelper cmpver"
 
 readonly XBPS_TARGET="$1"
-if [ -n "$2" ]; then
+if [ "$2" ]; then
     XBPS_TARGET_PKG="${2##*/}"
 fi
 
-chroot_check() {
-    if [ -f $XBPS_MASTERDIR/.xbps_chroot_init -o "$XBPS_CHROOT_CMD" = "ethereal" ]; then
-        export CHROOT_READY=1
-    fi
-}
-
+# Check for CHROOT_READY and set up XBPS_ARCH environment var for xbps.
 chroot_check
+check_native_arch
 
+# Reconfigure pkgs for 32bit on x86_64 and reexec itself.
+# XXX: how about 32bit userland on 64bit CPUs? (ppc, arm, etc).
 if [ -z "$XBPS_REINIT" -a -s $XBPS_MASTERDIR/.xbps_chroot_init ]; then
-    export XBPS_ARCH=${XBPS_ARCH:-$(cat $XBPS_MASTERDIR/.xbps_chroot_init)}
+    export XBPS_ARCH=${XBPS_ARCH:-$(<$XBPS_MASTERDIR/.xbps_chroot_init)}
     if [[ $XBPS_MACHINE == x86_64* ]] && [[ $XBPS_ARCH == i686* ]]; then
         # reconfigure pkgs via linux32
         linux32 xbps-reconfigure -r ${XBPS_MASTERDIR} -a &>/dev/null
@@ -598,20 +595,21 @@ if [ -z "$XBPS_REINIT" -a -s $XBPS_MASTERDIR/.xbps_chroot_init ]; then
         exec linux32 $0 ${XBPS_OPTIONS} $@
     fi
 fi
-if [ -n "$XBPS_ARCH" ]; then
+if [ "$XBPS_ARCH" ]; then
     export XBPS_MACHINE=$XBPS_ARCH
 fi
+# At this point if XBPS_TARGET_MACHINE isn't defined we assume
+# it's a native build.
 if [ -z "$XBPS_TARGET_MACHINE" ]; then
-    export XBPS_TARGET_MACHINE=${XBPS_ARCH:=$XBPS_MACHINE}
+        export XBPS_TARGET_MACHINE=$XBPS_MACHINE
 fi
 
-
 export XBPS_SHUTILSDIR XBPS_CROSSPFDIR XBPS_TRIGGERSDIR \
-    XBPS_SRCPKGDIR XBPS_COMMONDIR XBPS_BUILDDIR XBPS_REPO_DELTAS \
+    XBPS_SRCPKGDIR XBPS_COMMONDIR XBPS_BUILDDIR \
     XBPS_REPOSITORY XBPS_ALT_REPOSITORY XBPS_SRCDISTDIR XBPS_DIGEST_CMD \
     XBPS_UHELPER_CMD XBPS_INSTALL_CMD XBPS_QUERY_CMD XBPS_BUILD_ONLY_ONE_PKG \
-    XBPS_RINDEX_CMD XBPS_RECONFIGURE_CMD XBPS_REMOVE_CMD \
-    XBPS_CMPVER_CMD XBPS_FETCH_CMD XBPS_VERSION XBPS_APIVER XBPS_BUILDSTYLEDIR \
+    XBPS_RINDEX_CMD XBPS_RECONFIGURE_CMD XBPS_REMOVE_CMD XBPS_CHECKVERS_CMD \
+    XBPS_CMPVER_CMD XBPS_FETCH_CMD XBPS_VERSION XBPS_BUILDSTYLEDIR \
     XBPS_CPPFLAGS XBPS_CFLAGS XBPS_CXXFLAGS XBPS_FFLAGS XBPS_LDFLAGS \
     XBPS_MAKEJOBS XBPS_BUILD_FORCEMODE XBPS_USE_GIT_REVS XBPS_DEBUG_PKGS \
     XBPS_CHECK_PKGS XBPS_CCACHE XBPS_DISTCC XBPS_DISTCC_HOSTS XBPS_SKIP_DEPS \
@@ -629,7 +627,7 @@ for i in REPOSITORY DESTDIR BUILDDIR SRCDISTDIR; do
     unset val
 done
 
-# A temporary masterdir requires xbps-uchroot(8) and -O to use overlayfs
+# A temporary masterdir requires xbps-uchroot(1) and -O to use overlayfs
 # on tmpfs (available with xbps-0.45).
 if [ -z "$IN_CHROOT" -a -n "$XBPS_TEMP_MASTERDIR" ]; then
     export XBPS_CHROOT_CMD="uchroot"
@@ -640,31 +638,35 @@ fi
 #
 if [ -z "$IN_CHROOT" ]; then
     # In non chroot case always prefer host tools.
-    MYPATH="$XBPS_MASTERDIR/usr/bin:$XBPS_MASTERDIR/usr/sbin"
-    export PATH="$PATH:$MYPATH"
-else
-    MYPATH="/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
-    export PATH="$MYPATH"
-    if [ -n "$XBPS_CCACHE" ]; then
-        CCACHEPATH="/usr/lib/ccache/bin"
-        export CCACHE_DIR="$XBPS_HOSTDIR/ccache"
-        # Avoid not using cached files just due to compiler mtime
-        # changes when e.g. bootstrapping
-        export CCACHE_COMPILERCHECK=content CCACHE_COMPRESS=1
-        export PATH="$CCACHEPATH:$PATH"
-        mkdir -p $CCACHE_DIR
-    fi
-    if [ -n "$XBPS_DISTCC" ]; then
-        if [ -n "$XBPS_CCACHE" ]; then
-            export CCACHE_PREFIX="/usr/bin/distcc"
-        else
-            DISTCCPATH="/usr/lib/distcc/bin"
-            export PATH="$DISTCCPATH:$PATH"
-        fi
-        export DISTCC_DIR="$XBPS_HOSTDIR/distcc-${XBPS_CROSS_BUILD:-${XBPS_MACHINE}}"
-        export DISTCC_HOSTS="$XBPS_DISTCC_HOSTS"
-        mkdir -p $DISTCC_DIR
+    export PATH="$PATH:$XBPS_MASTERDIR/usr/bin"
+fi
+
+#
+# Set up ccache
+#
+if [ "$XBPS_CCACHE" ]; then
+    export CCACHEPATH="/usr/lib/ccache/bin"
+    export CCACHE_DIR="$XBPS_HOSTDIR/ccache"
+    # Avoid not using cached files just due to compiler mtime
+    # changes when e.g. bootstrapping
+    export CCACHE_COMPILERCHECK=content CCACHE_COMPRESS=1
+    export PATH="$CCACHEPATH:$PATH"
+    mkdir -p $CCACHE_DIR
+fi
+
+#
+# Set up distcc
+#
+if [ "$XBPS_DISTCC" ]; then
+    if [ "$XBPS_CCACHE" ]; then
+        export CCACHE_PREFIX="/usr/bin/distcc"
+    else
+        DISTCCPATH="/usr/lib/distcc/bin"
+        export PATH="$DISTCCPATH:$PATH"
     fi
+    export DISTCC_DIR="$XBPS_HOSTDIR/distcc-${XBPS_CROSS_BUILD:-${XBPS_MACHINE}}"
+    export DISTCC_HOSTS="$XBPS_DISTCC_HOSTS"
+    mkdir -p $DISTCC_DIR
 fi
 
 check_build_requirements
@@ -675,6 +677,12 @@ if [ -z "$IN_CHROOT" ]; then
         setup_distfiles_mirror
     fi
 fi
+#
+# Read funcs from helpers
+#
+for f in ${XBPS_SHUTILSDIR}/*.sh; do
+    [ -r "$f" ] && . $f
+done
 
 reconfigure_bootstrap_pkgs
 
@@ -689,18 +697,24 @@ case "$XBPS_TARGET" in
         # bootstrap from sources
         # check for required host utils
         check_reqhost_utils bootstrap
-        [ ! -d $XBPS_SRCPKGDIR/base-chroot ] && \
-            msg_error "Cannot find $XBPS_SRCPKGDIR/base-chroot directory!\n"
+        [[ $XBPS_MACHINE =~ musl ]] && subarch="-musl"
+        [ ! -d $XBPS_SRCPKGDIR/base-chroot${subarch} ] && \
+            msg_error "Cannot find $XBPS_SRCPKGDIR/base-chroot${subarch} directory!\n"
         bootstrap_vpkg=${XBPS_MASTERDIR}/etc/xbps.d/bootstrap-vpkgs.conf
         mkdir -p ${XBPS_MASTERDIR}/etc/xbps.d
         if [ ! -s ${bootstrap_vpkg} ]; then
             # Fool xbps to resolve dependencies.
-            echo 'virtualpkg=libgcc-4.4.0_1:base-files' >> ${bootstrap_vpkg}
-            echo 'virtualpkg=libstdc++-4.4.0_1:base-files' >> ${bootstrap_vpkg}
+            echo 'virtualpkg=libgcc:base-files' >> ${bootstrap_vpkg}
+            echo 'virtualpkg=libstdc++:base-files' >> ${bootstrap_vpkg}
         fi
-        $XBPS_LIBEXECDIR/build.sh base-chroot base-chroot $XBPS_TARGET || exit 1
+        (
+            export XBPS_ARCH=$XBPS_MACHINE
+            export XBPS_SKIP_REMOTEREPOS=1
+            $XBPS_LIBEXECDIR/build.sh \
+                base-chroot${subarch} base-chroot${subarch} $XBPS_TARGET || exit 1
+        ) || exit 1
         [ -d $XBPS_MASTERDIR ] && rm -rf $XBPS_MASTERDIR
-        install_bbootstrap bootstrap
+        install_bbootstrap ${XBPS_TARGET_PKG:=$XBPS_MACHINE}
         ;;
     bootstrap-update)
         if [ -n "$CHROOT_READY" -a -z "$IN_CHROOT" ]; then
@@ -780,7 +794,7 @@ case "$XBPS_TARGET" in
         show_pkg
         ;;
     show-avail)
-        read_pkg
+        read_pkg &>/dev/null
         show_avail
         ;;
     show-files)
@@ -857,7 +871,7 @@ case "$XBPS_TARGET" in
         bulk_build
         ;;
     show-sys-updates)
-        bulk_build -i
+        bulk_build -I
         ;;
     sort-dependencies)
         bulk_sortdeps ${@/$XBPS_TARGET/}
@@ -866,7 +880,7 @@ case "$XBPS_TARGET" in
         bulk_update
         ;;
     update-sys)
-        bulk_update -i
+        bulk_update -I
         ;;
     update-check)
         read_pkg ignore-problems

From 48cecafe7516e07861451a08526914d7c07bc19c Mon Sep 17 00:00:00 2001
From: Juan RP <xtraeme@gmail.com>
Date: Sun, 7 Jul 2019 08:36:05 +0200
Subject: [PATCH 2/2] xbps-src: verify all deps exist in srcpkgs.

---
 common/xbps-src/shutils/build_dependencies.sh | 23 +++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/common/xbps-src/shutils/build_dependencies.sh b/common/xbps-src/shutils/build_dependencies.sh
index fd327066e17..6d9d727e0d3 100644
--- a/common/xbps-src/shutils/build_dependencies.sh
+++ b/common/xbps-src/shutils/build_dependencies.sh
@@ -145,6 +145,12 @@ install_pkg_deps() {
     # Host build dependencies.
     #
     if [[ ${hostmakedepends} ]]; then
+        # check validity
+        for f in ${hostmakedepends}; do
+            if [ ! -f $XBPS_SRCPKGDIR/$f/template ]; then
+                msg_error "$pkgver: host dependency '$f' does not exist!\n"
+            fi
+        done
         while read -r _depname _deprepover _depver _subpkg _repourl; do
             _vpkg=${_subpkg}-${_depver}
             # binary package found in a repo
@@ -180,6 +186,12 @@ install_pkg_deps() {
     # Host check dependencies.
     #
     if [[ ${checkdepends} ]] && [[ $XBPS_CHECK_PKGS ]]; then
+        # check validity
+        for f in ${checkdepends}; do
+            if [ ! -f $XBPS_SRCPKGDIR/$f/template ]; then
+                msg_error "$pkgver: check dependency '$f' does not exist!\n"
+            fi
+        done
         while read -r _depname _deprepover _depver _subpkg _repourl; do
             _vpkg=${_subpkg}-${_depver}
             # binary package found in a repo
@@ -215,6 +227,12 @@ install_pkg_deps() {
     # Target build dependencies.
     #
     if [[ ${makedepends} ]]; then
+        # check validity
+        for f in ${makedepends}; do
+            if [ ! -f $XBPS_SRCPKGDIR/$f/template ]; then
+                msg_error "$pkgver: target dependency '$f' does not exist!\n"
+            fi
+        done
         while read -r _depname _deprepover _depver _subpkg _repourl; do
             _vpkg=${_subpkg}-${_depver}
             # binary package found in a repo
@@ -251,6 +269,11 @@ install_pkg_deps() {
     #
     if [[ ${depends} ]]; then
         _deps=$(setup_pkg_depends "" 1) || exit 1
+        for f in ${_deps}; do
+            if [ ! -f $XBPS_SRCPKGDIR/$f/template ]; then
+                msg_error "$pkgver: runtime dependency '$f' does not exist!\n"
+            fi
+        done
         while read -r _depname _deprepover _depver _subpkg _repourl; do
             _vpkg=${_subpkg}-${_depver}
             # binary package found in a repo
