Update drivers
[vuplus_openembedded] / classes / packaged-staging.bbclass
index 9aba940..2be0feb 100644 (file)
 #
-# Populated ${STAGING} using packages
+# Populate builds using prebuilt packages where possible to speed up builds
+# and allow staging to be reconstructed.
 #
 # To use it add that line to conf/local.conf:
 #
 # INHERIT += "packaged-staging"
 #
-# You also need ipkg-cl and ipkg-make-index installed on your host
-# put ipkg-build from org.openembedded.packaged-staging/contrib/ in your $PATH
-
-# BUGS:
-# * does not distinguish between -native, -cross and other packages
-
-# TODO:
-# * also build a feed for native and cross packages 
-# * make package detection a bit smarter (search for compatible archs)
-# * make do_clean clean staging as well
-
-# Summary:
-# This class will have two modes of operation:
-# PSTAGE_MODE = 'repopulate': repopulated staging from scratch for each packages
-# PSTAGE_MODE = 'append': append each package to staging (current behaviour)
-
-inherit package
-
-DEPENDS = "stagemanager-native"
-
-DEPLOY_DIR_PSTAGE      = "${DEPLOY_DIR}/pstage" 
-
-PSTAGE_BUILD_CMD        = "${IPKGBUILDCMD}"
-PSTAGE_INSTALL_CMD      = "ipkg-cl install -force-depends -f ${DEPLOY_DIR_PSTAGE}/ipkg.conf -o "
-PSTAGE_UPDATE_CMD      = "ipkg-cl update -f ${DEPLOY_DIR_PSTAGE}/ipkg.conf -o "
-PSTAGE_LIST_CMD                = "ipkg-cl list_installed -f ${DEPLOY_DIR_PSTAGE}/ipkg.conf -o "
-PSTAGE_PKGNAME                 = "staging-${PN}_${PV}-${PR}_${PACKAGE_ARCH}.ipk"
-PCROSS_PKGNAME         = "cross-${PN}_${PV}-${PR}_${BUILD_ARCH}.ipk"
-
-SPAWNFILE              = "${STAGING_DIR}/pkgmaps/${P}-${PR}.spawn"
-SPAWNIPK                = "${spawn}"
-
-PSTAGE_TMPDIR_STAGE     = "${TMPDIR}/tmp-staging"
-PSTAGE_TMPDIR_CROSS     = "${TMPDIR}/tmp-cross"
-
-STAGING_BASEDIR                = "${STAGING_LIBDIR}/.."
-
-PACKAGEFUNCS += "do_write_ipk_list"
-
-python do_write_ipk_list () {
-        import os, sys
-        ipkdir = bb.data.getVar('DEPLOY_DIR_IPK', d, 1)
-        stagingdir = bb.data.getVar('STAGING_DIR', d, 1)
-        tmpdir = bb.data.getVar('TMPDIR', d, 1)
-        p = bb.data.getVar('P', d, 1)
-        pr = bb.data.getVar('PR', d, 1)
-
-        packages = bb.data.getVar('PACKAGES', d, 1)
-        if not packages:
-                bb.debug(1, "PACKAGES not defined, nothing to package")
-                return
-
-        if packages == []:
-                bb.debug(1, "No packages; nothing to do")
-                return
-
-        # Generate ipk.conf if it or the stamp doesnt exist
-        listfile = os.path.join(stagingdir,"pkgmaps","%s-%s.spawn" %  ( p , pr ))
-        os.system('mkdir -p ' + stagingdir + '/pkgmaps')
-        if not os.access(listfile, os.R_OK):
-                os.system('rm -f ' + listfile)
-                f = open(listfile,"w")
-                for spawn in packages.split():
-                        #check if the packagename has changed due to debian shlib renaming
-                        localdata = bb.data.createCopy(d)
-                        pkgname = bb.data.getVar('PKG_%s' % spawn, localdata, 1)
-                        if not pkgname:
-                                pkgname = spawn
-                        f.write("%s\n" % pkgname)
-                f.close()
-}
 
 
-do_clean_append() {
-       """clear the build and temp directories"""
-        stagepkg = bb.data.expand("${DEPLOY_DIR_PSTAGE}/${PSTAGE_PKGNAME}", d)
-        if stagepkg == '//': raise bb.build.FuncFailed("wrong DATADIR")
-        bb.note("removing " + stagepkg)
-        os.system('rm -rf ' + stagepkg)
+#
+# bitbake.conf set PSTAGING_ACTIVE = "0", this class sets to "1" if we're active
+# 
+PSTAGE_PKGVERSION = "${PV}-${PR}"
+PSTAGE_PKGARCH    = "${BUILD_SYS}"
+PSTAGE_EXTRAPATH  ?= "/${OELAYOUT_ABI}/${DISTRO_PR}/"
+PSTAGE_PKGPATH    = "${DISTRO}${PSTAGE_EXTRAPATH}"
+PSTAGE_PKGPN      = "${@bb.data.expand('staging-${PN}-${MULTIMACH_ARCH}${TARGET_VENDOR}-${TARGET_OS}', d).replace('_', '-')}"
+PSTAGE_PKGNAME    = "${PSTAGE_PKGPN}_${PSTAGE_PKGVERSION}_${PSTAGE_PKGARCH}.ipk"
+PSTAGE_PKG        ?= "${DEPLOY_DIR_PSTAGE}/${PSTAGE_PKGPATH}/${PSTAGE_PKGNAME}"
+
+PSTAGE_NATIVEDEPENDS = "\
+    shasum-native \
+    stagemanager-native \
+    "
+
+BB_STAMP_WHITELIST = "${PSTAGE_NATIVEDEPENDS}"
+
+python () {
+    pstage_allowed = True
+
+    # These classes encode staging paths into the binary data so can only be
+    # reused if the path doesn't change/
+    if bb.data.inherits_class('native', d) or bb.data.inherits_class('cross', d) or bb.data.inherits_class('sdk', d):
+        path = bb.data.getVar('PSTAGE_PKGPATH', d, 1)
+        path = path + bb.data.getVar('TMPDIR', d, 1).replace('/', '-')
+        bb.data.setVar('PSTAGE_PKGPATH', path, d)
+
+    # PSTAGE_NATIVEDEPENDS lists the packages we need before we can use packaged 
+    # staging. There will always be some packages we depend on.
+    if bb.data.inherits_class('native', d):
+        pn = bb.data.getVar('PN', d, True)
+        nativedeps = bb.data.getVar('PSTAGE_NATIVEDEPENDS', d, True).split()
+        if pn in nativedeps:
+            pstage_allowed = False
+
+    # Images aren't of interest to us
+    if bb.data.inherits_class('image', d):
+        pstage_allowed = False
+
+    if bb.data.getVar('PSTAGING_DISABLED', d, True) == "1":
+        pstage_allowed = False
+
+    # Add task dependencies if we're active, otherwise mark packaged staging
+    # as inactive.
+    if pstage_allowed:
+        deps = bb.data.getVarFlag('do_setscene', 'depends', d) or ""
+        deps += " stagemanager-native:do_populate_staging"
+        bb.data.setVarFlag('do_setscene', 'depends', deps, d)
+
+        policy = bb.data.getVar("BB_STAMP_POLICY", d, True)
+        if policy == "whitelist" or policy == "full":
+           deps = bb.data.getVarFlag('do_setscene', 'recrdeptask', d) or ""
+           deps += " do_setscene"
+           bb.data.setVarFlag('do_setscene', 'recrdeptask', deps, d)
+
+        bb.data.setVar("PSTAGING_ACTIVE", "1", d)
+    else:
+        bb.data.setVar("PSTAGING_ACTIVE", "0", d)
 }
 
+DEPLOY_DIR_PSTAGE   ?= "${DEPLOY_DIR}/pstage"
+PSTAGE_MACHCONFIG   = "${DEPLOY_DIR_PSTAGE}/opkg.conf"
+
+PSTAGE_PKGMANAGER = "stage-manager-ipkg"
+
+PSTAGE_BUILD_CMD        = "stage-manager-ipkg-build -o 0 -g 0"
+PSTAGE_INSTALL_CMD      = "${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -force-depends -o ${TMPDIR} install"
+PSTAGE_UPDATE_CMD       = "${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -o ${TMPDIR} update"
+PSTAGE_REMOVE_CMD       = "${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -force-depends -o ${TMPDIR} remove"
+PSTAGE_LIST_CMD         = "${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -o ${TMPDIR} list_installed"
+
+PSTAGE_TMPDIR_STAGE     = "${WORKDIR}/staging-pkg"
+
+def pstage_manualclean(srcname, destvarname, d):
+       src = os.path.join(bb.data.getVar('PSTAGE_TMPDIR_STAGE', d, True), srcname)
+       dest = bb.data.getVar(destvarname, d, True)
+
+       for walkroot, dirs, files in os.walk(src):
+               for file in files:
+                       filepath = os.path.join(walkroot, file).replace(src, dest)
+                       bb.note("rm %s" % filepath)
+                       os.system("rm %s" % filepath)
+
+def pstage_set_pkgmanager(d):
+    path = bb.data.getVar("PATH", d, 1)
+    pkgmanager = bb.which(path, 'opkg-cl')
+    if pkgmanager == "":
+        pkgmanager = bb.which(path, 'ipkg-cl')
+    if pkgmanager != "":
+        bb.data.setVar("PSTAGE_PKGMANAGER", pkgmanager, d)
+
+
+def pstage_cleanpackage(pkgname, d):
+       path = bb.data.getVar("PATH", d, 1)
+       pstage_set_pkgmanager(d)
+       list_cmd = bb.data.getVar("PSTAGE_LIST_CMD", d, True)
+
+       bb.debug(2, "Checking if staging package installed")
+       lf = bb.utils.lockfile(bb.data.expand("${SYSROOT_LOCK}", d))
+       ret = os.system("PATH=\"%s\" %s | grep %s" % (path, list_cmd, pkgname))
+       if ret == 0:
+               bb.debug(1, "Uninstalling package from staging...")
+               removecmd = bb.data.getVar("PSTAGE_REMOVE_CMD", d, 1)
+               ret = os.system("PATH=\"%s\" %s %s" % (path, removecmd, pkgname))
+               if ret != 0:
+                       bb.note("Failure removing staging package")
+       else:
+               bb.debug(1, "Manually removing any installed files from staging...")
+               pstage_manualclean("staging", "STAGING_DIR", d)
+               pstage_manualclean("cross", "CROSS_DIR", d)
+               pstage_manualclean("deploy", "DEPLOY_DIR", d)
+
+       bb.utils.unlockfile(lf)
+
+do_clean_prepend() {
+       """
+       Clear the build and temp directories
+       """
+
+       removepkg = bb.data.expand("${PSTAGE_PKGPN}", d)
+       pstage_cleanpackage(removepkg, d)
+
+       stagepkg = bb.data.expand("${PSTAGE_PKG}", d)
+       bb.note("Removing staging package %s" % base_path_out(stagepkg, d))
+       os.system('rm -rf ' + stagepkg)
+}
 
-do_stage_prepend() {
-
-       stage-manager -p ${STAGING_DIR} -c ${DEPLOY_DIR_PSTAGE}/staging-stamp-cache -u
-
-       stage-manager -p ${CROSS_DIR} -c ${DEPLOY_DIR_PSTAGE}/cross-stamp-cache -u
-
-       if [ ! -e ${STAGING_BASEDIR} ]; then
-               mkdir -p ${STAGING_BASEDIR}
+staging_helper () {
+       # Assemble appropriate opkg.conf
+       conffile=${PSTAGE_MACHCONFIG}
+       mkdir -p ${DEPLOY_DIR_PSTAGE}/pstaging_lists
+       if [ ! -e $conffile ]; then
+               ipkgarchs="${BUILD_SYS}"
+               priority=1
+               for arch in $ipkgarchs; do
+                       echo "arch $arch $priority" >> $conffile
+                       priority=$(expr $priority + 5)
+               done
+               echo "dest root /" >> $conffile
        fi
-
-       if [ ! -e ${DEPLOY_DIR_PSTAGE} ]; then
-               mkdir -p ${DEPLOY_DIR_PSTAGE}
+       if [ ! -e ${TMPDIR}${libdir_native}/opkg/info/ ]; then
+               mkdir -p ${TMPDIR}${libdir_native}/opkg/info/
        fi
-
-       if [ -e ${STAGING_BASEDIR}/usr ]; then
-               oenote "${STAGING_BASEDIR}/usr already present, leaving it alone"
-       else
-               oenote "${STAGING_BASEDIR}/usr not present, symlinking it"
-               ln -s ${STAGING_BASEDIR}/ ${STAGING_BASEDIR}/usr
+       if [ ! -e ${TMPDIR}${libdir_native}/ipkg/ ]; then
+               ln -sf opkg/ ${TMPDIR}${libdir_native}/ipkg || true
        fi
+}
 
-       #assemble appropriate ipkg.conf
-       if [ -e ${DEPLOY_DIR_PSTAGE}/ipkg.conf ]; then
-               rm ${DEPLOY_DIR_PSTAGE}/ipkg.conf
-       fi
+PSTAGE_TASKS_COVERED = "fetch unpack munge patch configure qa_configure rig_locales compile sizecheck install deploy package populate_staging package_write_deb package_write_ipk package_write package_stage qa_staging"
+
+SCENEFUNCS += "packagestage_scenefunc"
+
+python packagestage_scenefunc () {
+    if bb.data.getVar("PSTAGING_ACTIVE", d, 1) == "0":
+        return
+
+    bb.build.exec_func("staging_helper", d)
+
+    removepkg = bb.data.expand("${PSTAGE_PKGPN}", d)
+
+    pstage_cleanpackage(removepkg, d)
+
+    stagepkg = bb.data.expand("${PSTAGE_PKG}", d)
+
+    if os.path.exists(stagepkg):
+        path = bb.data.getVar("PATH", d, 1)
+        pstage_set_pkgmanager(d)
+        file = bb.data.getVar("FILE", d, True)
+        bb.debug(2, "Packaged staging active for %s\n" % file)
+
+        #
+        # Install the staging package somewhere temporarily so we can extract the stamp files
+        #
+        bb.mkdirhier(bb.data.expand("${WORKDIR}/tstage/${libdir_native}/opkg/info/ ", d))
+        cmd = bb.data.expand("${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -force-depends -o ${WORKDIR}/tstage install", d)
+        ret = os.system("PATH=\"%s\" %s %s" % (path, cmd, stagepkg))
+        if ret != 0:
+            bb.fatal("Couldn't install the staging package to a temp directory")
+
+        #
+        # Copy the stamp files into the main stamps directoy
+        #
+        cmd = bb.data.expand("cp -dpR ${WORKDIR}/tstage/stamps/* ${TMPDIR}/stamps/", d)
+        ret = os.system(cmd)
+        if ret != 0:
+            bb.fatal("Couldn't copy the staging package stamp files")
+
+        #
+        # Iterate over the stamps seeing if they're valid. If we find any that
+        # are invalid or the task wasn't in the taskgraph, assume caution and 
+        # do a rebuild.
+        #
+        # FIXME - some tasks are safe to ignore in the task graph. e.g. package_write_*
+        stageok = True
+        taskscovered = bb.data.getVar("PSTAGE_TASKS_COVERED", d, True).split()
+        stamp = bb.data.getVar("STAMP", d, True)
+        for task in taskscovered:
+            task = 'do_' + task
+            stampfn = "%s.%s" % (stamp, task)
+            bb.debug(1, "Checking %s" % (stampfn))
+            if os.path.exists(stampfn):
+                stageok = bb.runqueue.check_stamp_fn(file, task, d)
+                bb.debug(1, "Result %s" % (stageok))
+                if not stageok:
+                    break
+
+        # Remove the stamps and files we added above
+        # FIXME - we should really only remove the stamps we added
+        os.system('rm -f ' + stamp + '.*')
+        os.system(bb.data.expand("rm -rf ${WORKDIR}/tstage", d))
+
+        if stageok:
+            bb.note("Staging package found, using it for %s." % file)
+            installcmd = bb.data.getVar("PSTAGE_INSTALL_CMD", d, 1)
+            lf = bb.utils.lockfile(bb.data.expand("${SYSROOT_LOCK}", d))
+            ret = os.system("PATH=\"%s\" %s %s" % (path, installcmd, stagepkg))
+            bb.utils.unlockfile(lf)
+            if ret != 0:
+                bb.note("Failure installing prestage package")
+
+            bb.build.make_stamp("do_stage_package_populated", d)
+        else:
+            bb.note("Staging package found but invalid for %s" % file)
 
-       ipkgarchs="${BUILD_ARCH} all any noarch ${TARGET_ARCH} ${IPKG_ARCHS} ${IPKG_EXTRA_ARCHS} ${MACHINE}"
-       priority=1
-       for arch in $ipkgarchs; do
-               echo "arch $arch $priority" >> ${DEPLOY_DIR_PSTAGE}/ipkg.conf
-               priority=$(expr $priority + 5)
-       done
-       echo "src oe file:${DEPLOY_DIR_IPK}" >> ${DEPLOY_DIR_PSTAGE}/ipkg.conf 
-       export OLD_PWD=`pwd`
-       cd ${DEPLOY_DIR_IPK} && rm *${BUILD_ARCH}.ipk -f ; ipkg-make-index -p Packages . ; cd ${OLD_PWD}
-       ${PSTAGE_UPDATE_CMD} ${STAGING_BASEDIR}
-
-       #check for generated packages
-       if [ -e ${SPAWNFILE} ]; then
-               oenote "List of spawned packages found: ${P}.spawn"
-               for spawn in `cat ${SPAWNFILE} | grep -v locale | grep -v dbg | grep -v gconv | grep -v charmap` ; do \
-                       if [ -e ${DEPLOY_DIR_IPK}/${spawn}_* ]; then
-                                       ${PSTAGE_INSTALL_CMD} ${STAGING_BASEDIR} ${spawn}          
-                               # clean up .la files to avoid having references to the builddirs in the binaries
-                               for lafile in ${STAGING_LIBDIR}/*.la ; do \
-                                       sed -i s:installed=yes:installed=no:g ${lafile} || true
-                               done
-
-                               #fix up linker script to poin to staging
-                               if [ -e ${STAGING_LIBDIR}/libc.so ]; then
-                                       sed -i s:\ /lib:\ ${STAGING_LIBDIR}:g ${STAGING_LIBDIR}/libc.so
-                                       sed -i s:\ /usr/lib:\ ${STAGING_LIBDIR}:g ${STAGING_LIBDIR}/libc.so
-                               fi
-                       else
-                               oenote "${spawn} not found, probably empty package"
-                       fi
-               done
-               exit 0
-       else
-               oenote "Spawn file not found!"
+}
+packagestage_scenefunc[cleandirs] = "${PSTAGE_TMPDIR_STAGE}"
+packagestage_scenefunc[dirs] = "${STAGING_DIR}"
+
+addhandler packagedstage_stampfixing_eventhandler
+python packagedstage_stampfixing_eventhandler() {
+    if bb.event.getName(e) == "StampUpdate":
+        taskscovered = bb.data.getVar("PSTAGE_TASKS_COVERED", e.data, 1).split()
+        for (fn, task) in e.targets:
+            # strip off 'do_'
+            task = task[3:]
+            if task in taskscovered:
+                stamp = "%s.do_stage_package_populated" % e.stampPrefix[fn]
+                if os.path.exists(stamp):
+                    # We're targetting a task which was skipped with packaged staging
+                    # so we need to remove the autogenerated stamps.
+                    for task in taskscovered:
+                        dir = "%s.do_%s" % (e.stampPrefix[fn], task)
+                        os.system('rm -f ' + dir)
+                    os.system('rm -f ' + stamp)
+
+    return NotHandled
+}
+
+populate_staging_preamble () {
+       if [ "$PSTAGING_ACTIVE" = "1" ]; then
+               stage-manager -p ${STAGING_DIR} -c ${DEPLOY_DIR_PSTAGE}/stamp-cache-staging -u || true
+               stage-manager -p ${CROSS_DIR} -c ${DEPLOY_DIR_PSTAGE}/stamp-cache-cross -u || true
        fi
+}
 
-        if [ -e ${DEPLOY_DIR_PSTAGE}/${PCROSS_PKGNAME} ]; then
-                oenote "Cross stuff already packaged, using that instead"
-                ${PSTAGE_INSTALL_CMD} ${CROSS_DIR}  ${DEPLOY_DIR_PSTAGE}/${PCROSS_PKGNAME}
-        fi
+populate_staging_postamble () {
+       if [ "$PSTAGING_ACTIVE" = "1" ]; then
+               # list the packages currently installed in staging
+               # ${PSTAGE_LIST_CMD} | awk '{print $1}' > ${DEPLOY_DIR_PSTAGE}/installed-list         
+
+               # exitcode == 5 is ok, it means the files change
+               set +e
+               stage-manager -p ${STAGING_DIR} -c ${DEPLOY_DIR_PSTAGE}/stamp-cache-staging -u -d ${PSTAGE_TMPDIR_STAGE}/staging
+               exitcode=$?
+               if [ "$exitcode" != "5" -a "$exitcode" != "0" ]; then
+                       exit $exitcode
+               fi
+               stage-manager -p ${CROSS_DIR} -c ${DEPLOY_DIR_PSTAGE}/stamp-cache-cross -u -d ${PSTAGE_TMPDIR_STAGE}/cross/${BASE_PACKAGE_ARCH}
+               if [ "$exitcode" != "5" -a "$exitcode" != "0" ]; then
+                       exit $exitcode
+               fi
+               set -e
+       fi
+}
 
-       if [ -e ${DEPLOY_DIR_PSTAGE}/${PSTAGE_PKGNAME} ]; then
-               oenote "Staging stuff already packaged, using that instead"
-               ${PSTAGE_INSTALL_CMD} ${STAGING_DIR}  ${DEPLOY_DIR_PSTAGE}/${PSTAGE_PKGNAME}
-               exit 0      
+packagedstaging_fastpath () {
+       if [ "$PSTAGING_ACTIVE" = "1" ]; then
+               mkdir -p ${PSTAGE_TMPDIR_STAGE}/staging/
+               mkdir -p ${PSTAGE_TMPDIR_STAGE}/cross/
+               cp -fpPR ${SYSROOT_DESTDIR}${STAGING_DIR}/* ${PSTAGE_TMPDIR_STAGE}/staging/ || /bin/true
+               cp -fpPR ${SYSROOT_DESTDIR}${CROSS_DIR}/* ${PSTAGE_TMPDIR_STAGE}/cross/ || /bin/true
        fi
+}
 
+do_populate_staging[dirs] =+ "${DEPLOY_DIR_PSTAGE}"
+python populate_staging_prehook() {
+    bb.build.exec_func("populate_staging_preamble", d)
+}
 
-       mkdir -p ${STAGING_BINDIR}
-       mkdir -p ${STAGING_LIBDIR}
-       mkdir -p ${STAGING_INCDIR}
-       mkdir -p ${STAGING_DATADIR}/aclocal
+python populate_staging_posthook() {
+    bb.build.exec_func("populate_staging_postamble", d)
 }
 
-do_stage_append() {
 
-       mkdir -p ${DEPLOY_DIR_PSTAGE}
+staging_packager () {
 
-       # list the packages currently installed in staging
-       ${PSTAGE_LIST_CMD} ${STAGING_DIR} | awk '{print $1}' > ${DEPLOY_DIR_PSTAGE}/installed-staging_list         
-       ${PSTAGE_LIST_CMD} ${CROSS_DIR} | awk '{print $1}' > ${DEPLOY_DIR_PSTAGE}/installed-cross_list
+       mkdir -p ${PSTAGE_TMPDIR_STAGE}/CONTROL
+       mkdir -p ${DEPLOY_DIR_PSTAGE}/${PSTAGE_PKGPATH}
 
-       set +e
-       rm -rf ${PSTAGE_TMPDIR_STAGE}
-       stage-manager -p ${STAGING_DIR} -c ${DEPLOY_DIR_PSTAGE}/staging-stamp-cache -u -d ${PSTAGE_TMPDIR_STAGE}
-       rc=$?
-       set -e
+       echo "Package: ${PSTAGE_PKGPN}"         >  ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
+       echo "Version: ${PSTAGE_PKGVERSION}"    >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
+       echo "Description: ${DESCRIPTION}"      >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
+       echo "Section: ${SECTION}"              >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
+       echo "Priority: Optional"               >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
+       echo "Maintainer: ${MAINTAINER}"        >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
+       echo "Architecture: ${PSTAGE_PKGARCH}"  >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
+       
+       # Protect against empty SRC_URI
+       if [ "${SRC_URI}" != "" ] ; then                
+               echo "Source: ${SRC_URI}"               >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
+       else
+               echo "Source: OpenEmbedded"               >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
+       fi
+        
+       ${PSTAGE_BUILD_CMD} ${PSTAGE_TMPDIR_STAGE} ${DEPLOY_DIR_PSTAGE}/${PSTAGE_PKGPATH}
+}
 
-       if [ $rc == 5 ]; then
+staging_package_installer () {
+       #${PSTAGE_INSTALL_CMD} ${PSTAGE_PKG}
 
-               #make a package for staging
-               mkdir -p ${PSTAGE_TMPDIR_STAGE}/CONTROL
+       STATUSFILE=${TMPDIR}${libdir_native}/opkg/status
+       echo "Package: ${PSTAGE_PKGPN}"        >> $STATUSFILE
+       echo "Version: ${PSTAGE_PKGVERSION}"   >> $STATUSFILE
+       echo "Status: install user installed"  >> $STATUSFILE
+       echo "Architecture: ${PSTAGE_PKGARCH}" >> $STATUSFILE
+       echo "" >> $STATUSFILE
 
-               echo "Package: staging-${PN}"           >  ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
-               echo "Version: ${PV}-${PR}"             >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
-               echo "Description: ${DESCRIPTION}"      >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
-               echo "Section: ${SECTION}"              >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
-               echo "Priority: Optional"               >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
-               echo "Maintainer: ${MAINTAINER}"        >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
-               echo "Architecture: ${PACKAGE_ARCH}"    >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
-               echo "Source: ${SRC_URI}"               >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
+       CTRLFILE=${TMPDIR}${libdir_native}/opkg/info/${PSTAGE_PKGPN}.control
+       echo "Package: ${PSTAGE_PKGPN}"        > $CTRLFILE
+       echo "Version: ${PSTAGE_PKGVERSION}"   >> $CTRLFILE
+       echo "Architecture: ${PSTAGE_PKGARCH}" >> $CTRLFILE
 
-               ${PSTAGE_BUILD_CMD} ${PSTAGE_TMPDIR_STAGE} ${DEPLOY_DIR_PSTAGE}
+       cd ${PSTAGE_TMPDIR_STAGE}
+       find -type f | grep -v ./CONTROL | sed -e 's/^\.//' > ${TMPDIR}${libdir_native}/opkg/info/${PSTAGE_PKGPN}.list
+}
 
-               ${PSTAGE_INSTALL_CMD} ${STAGING_DIR} ${DEPLOY_DIR_PSTAGE}/${PSTAGE_PKGNAME}
-       fi
+python do_package_stage () {
+    if bb.data.getVar("PSTAGING_ACTIVE", d, 1) != "1":
+        return
+
+    #
+    # Handle deploy/ packages
+    #
+    bb.build.exec_func("read_subpackage_metadata", d)
+    stagepath = bb.data.getVar("PSTAGE_TMPDIR_STAGE", d, 1)
+    tmpdir = bb.data.getVar("TMPDIR", d, True)
+    packages = (bb.data.getVar('PACKAGES', d, 1) or "").split()
+    if len(packages) > 0:
+        if bb.data.inherits_class('package_ipk', d):
+            ipkpath = bb.data.getVar('DEPLOY_DIR_IPK', d, True).replace(tmpdir, stagepath)
+        if bb.data.inherits_class('package_deb', d):
+            debpath = bb.data.getVar('DEPLOY_DIR_DEB', d, True).replace(tmpdir, stagepath)
+        if bb.data.inherits_class('package_rpm', d):
+            rpmpath = bb.data.getVar('DEPLOY_DIR_RPM', d, True).replace(tmpdir, stagepath)
+
+        for pkg in packages:
+            pkgname = bb.data.getVar('PKG_%s' % pkg, d, 1)
+            if not pkgname:
+                pkgname = pkg
+            arch = bb.data.getVar('PACKAGE_ARCH_%s' % pkg, d, 1)
+            if not arch:
+                arch = bb.data.getVar('PACKAGE_ARCH', d, 1)
+            pr = bb.data.getVar('PR_%s' % pkg, d, 1)
+            if not pr:
+                pr = bb.data.getVar('PR', d, 1)
+            if not packaged(pkg, d):
+                continue
+            if bb.data.inherits_class('package_ipk', d):
+                srcname = bb.data.expand(pkgname + "_${PV}-" + pr + "${DISTRO_PR}" + "_" + arch + ".ipk", d)
+                srcfile = bb.data.expand("${DEPLOY_DIR_IPK}/" + arch + "/" + srcname, d)
+                if os.path.exists(srcfile):
+                    destpath = ipkpath + "/" + arch + "/"
+                    bb.mkdirhier(destpath)
+                    bb.copyfile(srcfile, destpath + srcname)
+
+            if bb.data.inherits_class('package_deb', d):
+                if arch == 'all':
+                    srcname = bb.data.expand(pkgname + "_${PV}-" + pr + "${DISTRO_PR}" + "_all.deb", d)
+                else:
+                    srcname = bb.data.expand(pkgname + "_${PV}-" + pr + "${DISTRO_PR}" + "_${DPKG_ARCH}.deb", d)
+                srcfile = bb.data.expand("${DEPLOY_DIR_DEB}/" + arch + "/" + srcname, d)
+                if os.path.exists(srcfile):
+                    destpath = debpath + "/" + arch + "/" 
+                    bb.mkdirhier(destpath)
+                    bb.copyfile(srcfile, destpath + srcname)
+
+            if bb.data.inherits_class('package_rpm', d):
+               version = bb.data.getVar('PV', d, 1)
+               version = version.replace('-', '+')
+               bb.data.setVar('RPMPV', version, d)
+                srcname = bb.data.expand(pkgname + "-${RPMPV}-" + pr + "${DISTRO_PR}" + ".${TARGET_ARCH}.rpm", d)
+                srcfile = bb.data.expand("${DEPLOY_DIR_RPM}/" + arch + "/" + srcname, d)
+                if os.path.exists(srcfile):
+                    destpath = rpmpath + "/" + arch + "/" 
+                    bb.mkdirhier(destpath)
+                    bb.copyfile(srcfile, destpath + srcname)
+
+
+    #
+    # Handle stamps/ files
+    #
+    stampfn = bb.data.getVar("STAMP", d, True)
+    destdir = os.path.dirname(stampfn.replace(tmpdir, stagepath))
+    bb.mkdirhier(destdir)
+    # We need to include the package_stage stamp in the staging package so create one
+    bb.build.make_stamp("do_package_stage", d)
+    os.system("cp -dpR %s.do_* %s/" % (stampfn, destdir))
+
+    pstage_set_pkgmanager(d)
+    bb.build.exec_func("staging_helper", d)
+    bb.build.exec_func("staging_packager", d)
+    lf = bb.utils.lockfile(bb.data.expand("${SYSROOT_LOCK}", d))
+    bb.build.exec_func("staging_package_installer", d)
+    bb.utils.unlockfile(lf)
+}
 
-       set +e
-       rm -rf ${PSTAGE_TMPDIR_CROSS}
-       stage-manager -p ${CROSS_DIR} -c ${DEPLOY_DIR_PSTAGE}/cross-stamp-cache -u -d ${PSTAGE_TMPDIR_CROSS}
-       rc=$?
-       set -e
-
-       if [ $rc == 5 ]; then
-
-               #make a package for cross
-               mkdir -p ${PSTAGE_TMPDIR_CROSS}/CONTROL
-
-               echo "Package: cross-${PN}"             >  ${PSTAGE_TMPDIR_CROSS}/CONTROL/control
-               echo "Version: ${PV}-${PR}"             >> ${PSTAGE_TMPDIR_CROSS}/CONTROL/control
-               echo "Description: ${DESCRIPTION}"      >> ${PSTAGE_TMPDIR_CROSS}/CONTROL/control
-               echo "Section: ${SECTION}"              >> ${PSTAGE_TMPDIR_CROSS}/CONTROL/control
-               echo "Priority: Optional"               >> ${PSTAGE_TMPDIR_CROSS}/CONTROL/control
-               echo "Maintainer: ${MAINTAINER}"        >> ${PSTAGE_TMPDIR_CROSS}/CONTROL/control
-               echo "Architecture: ${BUILD_ARCH}"      >> ${PSTAGE_TMPDIR_CROSS}/CONTROL/control
-               echo "Source: ${SRC_URI}"               >> ${PSTAGE_TMPDIR_CROSS}/CONTROL/control
-       
-               ${PSTAGE_BUILD_CMD} ${PSTAGE_TMPDIR_CROSS} ${DEPLOY_DIR_PSTAGE}
+#
+# Note an assumption here is that do_deploy runs before do_package_write/do_populate_staging
+#
+addtask package_stage after do_package_write do_populate_staging before do_build
 
-               ${PSTAGE_INSTALL_CMD} ${CROSS_DIR} ${DEPLOY_DIR_PSTAGE}/${PCROSS_PKGNAME}
-       fi
+do_package_stage_all () {
+       :
 }
-
+do_package_stage_all[recrdeptask] = "do_package_stage"
+addtask package_stage_all after do_package_stage before do_build