$NetBSD: patch-ai,v 1.4 2011/11/23 00:54:34 marino Exp $

--- ../FreeBSD/lib/portbuild	2011-10-16 00:52:55.000000000 +0000
+++ lib/portbuild
@@ -5,7 +5,7 @@
 . ${pb}/scripts/lib/tinderlib.sh
 
 usage () {
-    echo "usage: $0 <build name> <jail name> <portstree name> [-noclean] [-plistcheck] [-nullfs] [-fetch-original] [-nolog] ED PD FD BD RD TD pkgname dirname"
+    echo "usage: $0 <build name> <jail name> <portstree name> [-noclean] [-plistcheck] [-fetch-original] [-nolog] ED PD FD BD RD TD pkgname dirname"
     exit 1
 }
 
@@ -17,9 +17,10 @@ cleanup()
     cleandirs=$4
     pkgname=$5
     build=$6
-    nullfs=$7
 
-    umount ${chroot}/compat/linux/proc >/dev/null
+    if [ ${ARCH} = "i386" ]; then
+	umount ${chroot}/compat/linux/proc >/dev/null
+    fi
     umount ${chroot}/proc >/dev/null
 
     if [ $noclean -eq 0 ]; then
@@ -31,11 +32,8 @@ cleanup()
 	    rm -rf ${chroot}/tmp/*
 
 	    chroot ${chroot} /sbin/ldconfig -R
-	    if [ ${ARCH} = "i386" ]; then
-	        chroot ${chroot} /sbin/ldconfig -aout -R
-	    fi
 	else
-	    ${tc} resetBuild -b ${build} ${nullfs}
+	    ${tc} resetBuild -b ${build}
 	fi
     fi
 
@@ -56,13 +54,19 @@ mark_failed() {
 	return
     fi
 
-    dependents=$(grep ${pkgname} ${mf} | grep '^[[:space:]]\{1,\}@' | awk '{print $NF}' | sed -e 's|^/usr/ports/||' | grep -v ${portdir})
+    dependents=$(grep ${pkgname} ${mf} | \
+    	grep '^[[:space:]]\{1,\}@' | \
+    	awk '{print $NF}' | \
+    	sed -e 's|^/usr/pkgsrc/||' | \
+    	sed -e 's|^../../||' | \
+    	grep -v ${portdir})
     if [ $? -ne 0 ]; then
 	return
     fi
 
     for dep in ${dependents}; do
 	${tc} updatePortStatus -d ${dep} -b ${branch} -L -s DEPEND -p ${portdir}
+	execute_hook "postPortBuild" "PACKAGE_NAME=${pkgname} BUILD=${branch} JAIL=${jail} PORTSTREE=${portstree} CHROOT=${chroot} PORTDIR=${dep} PB=${pb} STATUS=DEPEND FAIL_REASON=DEPEND TOTAL_SIZE=0"
     done
 }
 
@@ -71,7 +75,6 @@ mark_failed() {
 error=0
 init=0
 noclean=0
-nullfs=""
 fetchorig=0
 nolog=0
 plistcheck=""
@@ -107,9 +110,6 @@ while [ $# -gt 0 -a ${doneargs} -eq 0 ];
     x-plistcheck)	plistcheck="plistcheck"
 			shift;;
 
-    x-nullfs)		nullfs="-n"
-			shift;;
-
     x-fetch-original)	fetchorig=1
 			shift;;
 
@@ -151,7 +151,7 @@ echo ${dirname}
 tc=$(tinderLoc scripts tc)
 chroot=$(tinderLoc buildroot ${build})
 echo "chroot is: ${chroot}"
-portdir=$(echo ${dirname} | sed -e 's|^/usr/ports/||')
+portdir=$(echo ${dirname} | sed -e 's|^/usr/pkgsrc/||')
 
 total_size=$(${tc} getPortTotalSize -d ${portdir} -b ${build})
 execute_hook "prePortBuild" "PACKAGE_NAME=${pkgname} BUILD=${build} JAIL=${jail} PORTSTREE=${portstree} CHROOT=${chroot} PORTDIR=${portdir} PB=${pb} TOTAL_SIZE=${total_size}"
@@ -167,20 +167,21 @@ if [ -f ${dudsfile} ]; then
     if grep -qxF ${pkgname} ${dudsfile}; then
         echo "skipping ${pkgname} (found in duds)"
 	${tc} updatePortStatus -d ${portdir} -b ${build} \
-		-L -s DUD -v ${pkgname}
+		-L -s DUD -v ${pkgname} -r arch
 	mark_failed ${tc} ${build} ${pkgname} ${portdir}
 	execute_hook "postPortBuild" "PACKAGE_NAME=${pkgname} BUILD=${build} JAIL=${jail} PORTSTREE=${portstree} CHROOT=${chroot} STATUS=DUD PORTDIR=${portdir} PB=${pb} FAIL_REASON=dud TOTAL_SIZE=0"
-        exit 1
+	# exit with success, we need all the ports to fire on the above hook
+        exit 0
     fi
 fi
 
 # directories to clean
+# These are created/recreated after blowing the directories away
 cleandirs="${LOCALBASE} /compat /var/db/pkg"
 
 for dir in ${cleandirs}; do
     cleanDirs ${build} ${chroot}${dir}
 done
-rm -rf ${chroot}/var/db/pkg/*
 
 # reset mtrees for plist checking
 mtree -deU -f ${chroot}/etc/mtree/BSD.root.dist \
@@ -190,17 +191,48 @@ mtree -deU -f ${chroot}/etc/mtree/BSD.va
 mtree -deU -f ${chroot}/etc/mtree/BSD.usr.dist \
       -p ${chroot}/usr >/dev/null 2>&1
 
-mkdir -p ${chroot}${LOCALBASE}
-if [ -f ${chroot}/a/ports/Templates/BSD.local.dist ]; then
-    mtree -deU -f ${chroot}/a/ports/Templates/BSD.local.dist -p ${chroot}${LOCALBASE} \
+if [ -f ${chroot}/a/pkgsrc/Templates/BSD.local.dist ]; then
+    mtree -deU -f ${chroot}/a/pkgsrc/Templates/BSD.local.dist -p ${chroot}${LOCALBASE} \
     	>/dev/null 2>&1
 else
     mtree -deU -f ${chroot}/etc/mtree/BSD.local.dist -p ${chroot}${LOCALBASE} \
     	>/dev/null 2>&1
 fi
 
-# mount linprocfs if required
-if [ "${ARCH}" = "i386" -o "${ARCH}" = "amd64" ]; then
+# pkgsrc is "helpful" that it removes directories that result empty after
+# as the plist removes files of a package.  This includes standard mtree
+# directories such as man/man1, so we need to touch a file in every directory
+# of pkgsrc to prevent this side-effect which often fails the fs state check.
+
+find ${chroot}${LOCALBASE}/ -type d -exec touch {}/.keep \;
+
+# copy over pkgsrc bootstrap tools, ${LOCALBASE}/bin & /etc exist now
+cp    ${chroot}/usr/4bootstrap/bmake   ${chroot}${LOCALBASE}/bin/
+cp    ${chroot}/usr/4bootstrap/pkg_*   ${chroot}${LOCALBASE}/sbin/
+cp    ${chroot}/usr/4bootstrap/mk.conf ${chroot}${LOCALBASE}/etc/
+cp -R ${chroot}/usr/4bootstrap/mk      ${chroot}${LOCALBASE}/share/
+
+# By default, Tinderbox/pkgsrc will build all vulnerable software and
+# accept all licenses. The next file will add these directives to the mk.conf.
+# If this is unwanted on a per-build bases, use the more_make.conf file
+# to override it.
+
+cat ${pb}/scripts/lib/boilerplate_mk.conf >> ${chroot}${LOCALBASE}/etc/mk.conf
+
+# Add per-build custom make options to etc/mk.conf
+# Intended to default versions e.g. PYTHON_VERSION_DEFAULT
+# During catenation, comments, blank lines, MAKE_JOBS= and PKG_OPTIONS.xxxx
+# variables are stripped out. The latter is handled by pkg_options, and it's
+# better not to have two sources.
+
+more_mkconf=$(tinderLoc options ${build})/more_mk.conf
+if [ -f ${more_mkconf} ]; then
+    cat ${more_mkconf} | awk "!/^[ \t]*($|#|PKG_OPTIONS\.|MAKE_JOBS=)/" \
+    >> ${chroot}${LOCALBASE}/etc/mk.conf
+fi
+
+# mount linprocfs if available
+if [ ${ARCH} = "i386" ]; then
     mkdir -p ${chroot}/compat/linux/proc
     mount -t linprocfs linprocfs ${chroot}/compat/linux/proc
 fi
@@ -251,8 +283,10 @@ if [ x"${CCACHE_ENABLED}" = x"1" ]; then
     export PATH=/opt:${PATH}
 fi
 
-if [ x"${OPTIONS_ENABLED}" = x"1" ]; then
-    export PORT_DBDIR=/var/db/ports
+export PKG_OPTIONS_FILE=
+original_options=$(tinderLoc options ${build})/pkg_options
+if [ x"${OPTIONS_ENABLED}" = x"1" -a -f ${original_options} ]; then
+    export PKG_OPTIONS_FILE=/var/db/options/pkg_options
 fi
 
 echo "building $pkgname in $chroot"
@@ -264,11 +298,7 @@ packages=$(tinderLoc packages ${build})
 major_version=$(echo ${jail} | sed -E -e 's|(^[[:digit:]]+).*$|\1|')
 
 if [ -n "${DISTFILE_CACHE}" ]; then
-    export DISTCACHE=/distcache
-    if [ ${fetchorig} -eq 0 ]; then
-        _MSO='file:///distcache/${DIST_SUBDIR}/'
-        export MASTER_SITE_OVERRIDE="${_MSO} ${MASTER_SITE_OVERRIDE}"
-    fi
+    export DISTDIR=/distcache
 fi
 
 if [ -n "${DISTFILE_URI}" ]; then
@@ -282,7 +312,7 @@ mkdir -p ${chroot}/tmp/depends ${chroot}
 echo "building ${pkgname} in directory ${chroot}" | \
     tee ${chroot}/tmp/${pkgname}.log
 
-trap  "cleanup ${chroot} ${noclean} ${error} \"${cleandirs}\" ${pkgname} ${build} ${nullfs}" 1 2 3 9 10 11 15
+trap  "cleanup ${chroot} ${noclean} ${error} \"${cleandirs}\" ${pkgname} ${build}" 1 2 3 9 10 11 15
 
 # Don't build in a world-writable standard directory because some ports
 # hardcode this path and try to load things from it at runtime, which is
@@ -294,7 +324,7 @@ if [ x"${CCACHE_ENABLED}" ]; then
     unset CCACHE_DISABLE
 fi
 
-_ldconfig_dirs="/lib /usr/lib /usr/lib/compat"
+_ldconfig_dirs="/usr/lib"
 ldconfig_dirs=""
 for i in ${_ldconfig_dirs}; do
     if [ -d ${chroot}/${i} ]; then
@@ -302,13 +332,11 @@ for i in ${_ldconfig_dirs}; do
     fi
 done
 chroot ${chroot} /sbin/ldconfig ${ldconfig_dirs}
-if [ ${ARCH} = "i386" ]; then
-    chroot ${chroot} /sbin/ldconfig -aout /usr/lib/aout /usr/lib/compat/aout
-fi
 
 set x $ED $FD $PD $BD $RD $TD
 shift 1
 
+MISSINGDEP=
 cd ${chroot}/tmp/depends
 while [ $# -gt 0 ]; do
 
@@ -324,6 +352,7 @@ while [ $# -gt 0 ]; do
 
     if [ ! -f $1 ]; then
 	echo "skipping package $1 for ${pkgname} since it is missing"
+	MISSINGDEP=$1
     fi
 
     shift
@@ -337,13 +366,17 @@ if [ $? -ne 0 ]; then
     exit 1
 fi
 
-chroot ${chroot} /buildscript ${dirname} 1 "$ED" "$PD" "$FD" "$BD" "$RD" "$TD" "${plistcheck}" 2>&1 | tee -a ${chroot}/tmp/${pkgname}.log
+if [ -z "${MISSINGDEP}" ]; then
+chroot ${chroot} /buildscript ${dirname} 1 "$ED" "$PD" "$FD" "$BD" "$RD" "$TD" "${pkgname}" "${plistcheck}" 2>&1 | tee -a ${chroot}/tmp/${pkgname}.log
 if [ -f ${chroot}/tmp/status ]; then
     error=$(cat ${chroot}/tmp/status)
 else
     error=255
 fi
 execute_hook "postPortChecksum" "PACKAGE_NAME=${pkgname} BUILD=${build} JAIL=${jail} PORTSTREE=${portstree} CHROOT=${chroot} PORTDIR=${portdir} ERROR=${error} PB=${pb} TOTAL_SIZE=${total_size}"
+else
+    error=200
+fi
 
 last_version=$(${tc} getPortLastBuiltVersion -d ${portdir} -b ${build})
 logsDir=$(tinderLoc buildlogs ${build})
@@ -351,14 +384,7 @@ errorsDir=$(tinderLoc builderrors ${buil
 
 if [ "${error}" = "0" ]; then
     ln -sf ${pkgname}.log2 ${chroot}/tmp/make.log
-    # Cache the distfiles if requested.  We will wait a maximum of
-    # ten minutes before giving up on the lock.
-    if [ -n "${DISTFILE_CACHE}" ]; then
-	lockf -t 600 ${chroot}${DISTCACHE}/.lock tar -C ${chroot}/${DISTDIR} \
-	    -cf - . | tar --unlink -C ${chroot}${DISTCACHE} -xvf -
-    fi
-
-    chroot ${chroot} /usr/bin/nice -n $nice /buildscript ${dirname} 2  "$ED" "$PD" "$FD" "$BD" "$RD" "$TD" "${plistcheck}" > ${chroot}/tmp/${pkgname}.log2 2>&1
+    chroot ${chroot} /usr/bin/nice -n $nice /buildscript ${dirname} 2  "$ED" "$PD" "$FD" "$BD" "$RD" "$TD" "${pkgname}" "${plistcheck}" > ${chroot}/tmp/${pkgname}.log2 2>&1
 
     grep pnohang ${chroot}/tmp/${pkgname}.log2
     cat ${chroot}/tmp/${pkgname}.log2 >> ${chroot}/tmp/${pkgname}.log
@@ -372,6 +398,7 @@ if [ "${error}" = "0" ]; then
         cp ${chroot}/tmp/${pkgname}.log ${logsDir}/${pkgname}.log
     fi
     if [ -n "${logdir}" ]; then
+        rm -f ${logdir}/${pkgname}.log${lext}
 	if [ ${docopy} -eq 1 ]; then
 	    cp ${logsDir}/${pkgname}.log${lext} ${logdir}/${pkgname}.log${lext}
 	else
@@ -388,6 +415,7 @@ if [ "${error}" = "0" ]; then
 	cp ${chroot}/tmp/work.tbz ${workDir}/${pkgname}.tbz
     fi
 
+    rm -f ${errorsDir}/${pkgname}.log*
     if [ "${error}" = "0" ]; then
         if [ ! -z "${last_version}" ]; then
 	    oldpkgs=$(find ${packages} -name "${last_version}${PKGSUFFIX}")
@@ -400,8 +428,8 @@ if [ "${error}" = "0" ]; then
 		tar --unlink -C ${packages} -xvf -
 	[ -f ${packages}/All/${pkgname}${PKGSUFFIX} ] && \
 		touch ${packages}/All/${pkgname}${PKGSUFFIX}
-	rm -f ${errorsDir}/${pkgname}.log*
-	if bzgrep -q "${pkgname} is marked as broken: " ${logsDir}/${pkgname}.log${lext} ; then
+	BROKE=`zgrep -q "${pkgname} is marked as broken: " ${logsDir}/${pkgname}.log${lext}`
+	if [ "${BROKE}" != "" ] ; then
 	    status="BROKEN"
 	elif [ -f ${chroot}/tmp/leftovers ]; then
 	    status="LEFTOVERS"
@@ -469,12 +497,19 @@ else
     if [ ${old_size} -gt ${total_size} ]; then
 	total_size=${old_size}
     fi
+
+    PORTSTATUS=FAIL
+    if [ "${error}" = "200" ]; then
+       PORTSTATUS=DEPEND
+       reason="-r depend_package -p ${MISSINGDEP}"
+       reasonenv="FAIL_REASON=depend"
+    fi
     ${tc} updatePortStatus -d ${portdir} -b ${build} \
-		-L -s FAIL -v ${pkgname} ${reason} -t ${total_size}
+		-L -s ${PORTSTATUS} -v ${pkgname} ${reason} -t ${total_size}
     mark_failed ${tc} ${build} ${pkgname} ${portdir}
     ${tc} sendBuildErrorMail -d ${portdir} -b ${build} -p ${pkgname} -x ${lext}
     execute_hook "postPortBuild" "PACKAGE_NAME=${pkgname} BUILD=${build} JAIL=${jail} PORTSTREE=${portstree} CHROOT=${chroot} STATUS=FAIL PORTDIR=${portdir} PB=${pb} ${reasonenv} TOTAL_SIZE=${total_size}"
 fi
 
-cleanup ${chroot} ${noclean} ${error} "${cleandirs}" ${pkgname} ${build} ${nullfs}
+cleanup ${chroot} ${noclean} ${error} "${cleandirs}" ${pkgname} ${build}
 exit ${error}
