2 # Populate builds using prebuilt packages where possible to speed up builds
3 # and allow staging to be reconstructed.
5 # To use it add that line to conf/local.conf:
7 # INHERIT += "packaged-staging"
12 # bitbake.conf set PSTAGING_ACTIVE = "0", this class sets to "1" if we're active
14 PSTAGE_PKGVERSION = "${PV}-${PR}"
15 PSTAGE_PKGARCH = "${BUILD_SYS}"
16 PSTAGE_EXTRAPATH ?= ""
17 PSTAGE_PKGPATH = "${DISTRO}${PSTAGE_EXTRAPATH}"
18 PSTAGE_PKGPN = "${@bb.data.expand('staging-${PN}-${MULTIMACH_ARCH}${TARGET_VENDOR}-${TARGET_OS}', d).replace('_', '-')}"
19 PSTAGE_PKGNAME = "${PSTAGE_PKGPN}_${PSTAGE_PKGVERSION}_${PSTAGE_PKGARCH}.ipk"
20 PSTAGE_PKG = "${DEPLOY_DIR_PSTAGE}/${PSTAGE_PKGPATH}/${PSTAGE_PKGNAME}"
22 # multimachine.bbclass will override this but add a default in case we're not using it
23 MULTIMACH_ARCH ?= "${PACKAGE_ARCH}"
25 PSTAGE_NATIVEDEPENDS = "\
30 BB_STAMP_WHITELIST = "${PSTAGE_NATIVEDEPENDS}"
36 # These classes encode staging paths into the binary data so can only be
37 # reused if the path doesn't change/
38 if bb.data.inherits_class('native', d) or bb.data.inherits_class('cross', d) or bb.data.inherits_class('sdk', d):
39 path = bb.data.getVar('PSTAGE_PKGPATH', d, 1)
40 path = path + bb.data.getVar('TMPDIR', d, 1).replace('/', '-')
41 bb.data.setVar('PSTAGE_PKGPATH', path, d)
43 # PSTAGE_NATIVEDEPENDS lists the packages we need before we can use packaged
44 # staging. There will always be some packages we depend on.
45 if bb.data.inherits_class('native', d):
46 pn = bb.data.getVar('PN', d, True)
47 nativedeps = bb.data.getVar('PSTAGE_NATIVEDEPENDS', d, True).split()
49 pstage_allowed = False
51 # Images aren't of interest to us
52 if bb.data.inherits_class('image', d):
53 pstage_allowed = False
55 # Add task dependencies if we're active, otherwise mark packaged staging
58 deps = bb.data.getVarFlag('do_setscene', 'depends', d) or ""
59 deps += " stagemanager-native:do_populate_staging"
60 bb.data.setVarFlag('do_setscene', 'depends', deps, d)
62 policy = bb.data.getVar("BB_STAMP_POLICY", d, True)
63 if policy == "whitelist" or policy == "full":
64 deps = bb.data.getVarFlag('do_setscene', 'recrdeptask', d) or ""
65 deps += " do_setscene"
66 bb.data.setVarFlag('do_setscene', 'recrdeptask', deps, d)
68 bb.data.setVar("PSTAGING_ACTIVE", "1", d)
70 bb.data.setVar("PSTAGING_ACTIVE", "0", d)
73 DEPLOY_DIR_PSTAGE = "${DEPLOY_DIR}/pstage"
74 PSTAGE_MACHCONFIG = "${DEPLOY_DIR_PSTAGE}/opkg.conf"
76 PSTAGE_PKGMANAGER = "stage-manager-ipkg"
78 PSTAGE_BUILD_CMD = "stage-manager-ipkg-build -o 0 -g 0"
79 PSTAGE_INSTALL_CMD = "${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -force-depends -o ${TMPDIR} install"
80 PSTAGE_UPDATE_CMD = "${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -o ${TMPDIR} update"
81 PSTAGE_REMOVE_CMD = "${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -force-depends -o ${TMPDIR} remove"
82 PSTAGE_LIST_CMD = "${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -o ${TMPDIR} list_installed"
84 PSTAGE_TMPDIR_STAGE = "${WORKDIR}/staging-pkg"
86 def pstage_manualclean(srcname, destvarname, d):
89 src = os.path.join(bb.data.getVar('PSTAGE_TMPDIR_STAGE', d, True), srcname)
90 dest = bb.data.getVar(destvarname, d, True)
92 for walkroot, dirs, files in os.walk(src):
94 filepath = os.path.join(walkroot, file).replace(src, dest)
95 bb.note("rm %s" % filepath)
96 os.system("rm %s" % filepath)
98 def pstage_set_pkgmanager(d):
100 path = bb.data.getVar("PATH", d, 1)
101 pkgmanager = bb.which(path, 'opkg-cl')
103 pkgmanager = bb.which(path, 'ipkg-cl')
105 bb.data.setVar("PSTAGE_PKGMANAGER", pkgmanager, d)
108 def pstage_cleanpackage(pkgname, d):
111 path = bb.data.getVar("PATH", d, 1)
112 pstage_set_pkgmanager(d)
113 list_cmd = bb.data.getVar("PSTAGE_LIST_CMD", d, True)
115 bb.note("Checking if staging package installed")
116 lf = bb.utils.lockfile(bb.data.expand("${STAGING_DIR}/staging.lock", d))
117 ret = os.system("PATH=\"%s\" %s | grep %s" % (path, list_cmd, pkgname))
119 bb.note("Yes. Uninstalling package from staging...")
120 removecmd = bb.data.getVar("PSTAGE_REMOVE_CMD", d, 1)
121 ret = os.system("PATH=\"%s\" %s %s" % (path, removecmd, pkgname))
123 bb.note("Failure removing staging package")
125 bb.note("No. Manually removing any installed files")
126 pstage_manualclean("staging", "STAGING_DIR", d)
127 pstage_manualclean("cross", "CROSS_DIR", d)
128 pstage_manualclean("deploy", "DEPLOY_DIR", d)
130 bb.utils.unlockfile(lf)
134 Clear the build and temp directories
137 removepkg = bb.data.expand("${PSTAGE_PKGPN}", d)
138 pstage_cleanpackage(removepkg, d)
140 stagepkg = bb.data.expand("${PSTAGE_PKG}", d)
141 bb.note("Removing staging package %s" % stagepkg)
142 os.system('rm -rf ' + stagepkg)
146 # Assemble appropriate opkg.conf
147 conffile=${PSTAGE_MACHCONFIG}
148 mkdir -p ${DEPLOY_DIR_PSTAGE}/pstaging_lists
149 if [ ! -e $conffile ]; then
150 ipkgarchs="${BUILD_SYS}"
152 for arch in $ipkgarchs; do
153 echo "arch $arch $priority" >> $conffile
154 priority=$(expr $priority + 5)
156 echo "dest root /" >> $conffile
158 if [ ! -e ${TMPDIR}${layout_libdir}/opkg/info/ ]; then
159 mkdir -p ${TMPDIR}${layout_libdir}/opkg/info/
161 if [ ! -e ${TMPDIR}${layout_libdir}/ipkg/ ]; then
162 cd ${TMPDIR}${layout_libdir}/
167 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"
169 SCENEFUNCS += "packagestage_scenefunc"
171 python packagestage_scenefunc () {
174 if bb.data.getVar("PSTAGING_ACTIVE", d, 1) == "0":
177 bb.build.exec_func("staging_helper", d)
179 removepkg = bb.data.expand("${PSTAGE_PKGPN}", d)
180 pstage_cleanpackage(removepkg, d)
182 stagepkg = bb.data.expand("${PSTAGE_PKG}", d)
184 if os.path.exists(stagepkg):
185 path = bb.data.getVar("PATH", d, 1)
186 pstage_set_pkgmanager(d)
187 file = bb.data.getVar("FILE", d, True)
188 bb.debug(2, "Packaged staging active for %s\n" % file)
191 # Install the staging package somewhere temporarily so we can extract the stamp files
193 cmd = bb.data.expand("${PSTAGE_PKGMANAGER} -force-depends -f ${PSTAGE_MACHCONFIG} -o ${WORKDIR}/tstage install", d)
194 ret = os.system("PATH=\"%s\" %s %s" % (path, cmd, stagepkg))
196 bb.fatal("Couldn't install the staging package to a temp directory")
199 # Copy the stamp files into the main stamps directoy
201 cmd = bb.data.expand("cp -dpR ${WORKDIR}/tstage/stamps/* ${TMPDIR}/stamps/", d)
204 bb.fatal("Couldn't copy the staging package stamp files")
207 # Iterate over the stamps seeing if they're valid. If we find any that
208 # are invalid or the task wasn't in the taskgraph, assume caution and
211 # FIXME - some tasks are safe to ignore in the task graph. e.g. package_write_*
213 taskscovered = bb.data.getVar("PSTAGE_TASKS_COVERED", d, True).split()
214 stamp = bb.data.getVar("STAMP", d, True)
215 for task in taskscovered:
217 stampfn = "%s.%s" % (stamp, task)
218 bb.debug(1, "Checking %s" % (stampfn))
219 if os.path.exists(stampfn):
220 stageok = bb.runqueue.check_stamp_fn(file, task, d)
221 bb.debug(1, "Result %s" % (stageok))
225 # Remove the stamps and files we added above
226 # FIXME - we should really only remove the stamps we added
227 os.system('rm -f ' + stamp + '.*')
228 os.system(bb.data.expand("rm -rf ${WORKDIR}/tstage", d))
231 bb.note("Staging package found, using it for %s." % file)
232 installcmd = bb.data.getVar("PSTAGE_INSTALL_CMD", d, 1)
233 lf = bb.utils.lockfile(bb.data.expand("${STAGING_DIR}/staging.lock", d))
234 ret = os.system("PATH=\"%s\" %s %s" % (path, installcmd, stagepkg))
235 bb.utils.unlockfile(lf)
237 bb.note("Failure installing prestage package")
239 bb.build.make_stamp("do_stage_package_populated", d)
241 bb.note("Staging package found but invalid for %s" % file)
244 packagestage_scenefunc[cleandirs] = "${PSTAGE_TMPDIR_STAGE}"
245 packagestage_scenefunc[dirs] = "${STAGING_DIR}"
247 addhandler packagedstage_stampfixing_eventhandler
248 python packagedstage_stampfixing_eventhandler() {
249 from bb.event import getName
252 if getName(e) == "StampUpdate":
253 taskscovered = bb.data.getVar("PSTAGE_TASKS_COVERED", e.data, 1).split()
254 for (fn, task) in e.targets:
257 if task in taskscovered:
258 stamp = "%s.do_stage_package_populated" % e.stampPrefix[fn]
259 if os.path.exists(stamp):
260 # We're targetting a task which was skipped with packaged staging
261 # so we need to remove the autogenerated stamps.
262 for task in taskscovered:
263 dir = "%s.do_%s" % (e.stampPrefix[fn], task)
264 os.system('rm -f ' + dir)
265 os.system('rm -f ' + stamp)
270 populate_staging_preamble () {
271 if [ "$PSTAGING_ACTIVE" = "1" ]; then
272 stage-manager -p ${STAGING_DIR} -c ${DEPLOY_DIR_PSTAGE}/stamp-cache-staging -u || true
273 stage-manager -p ${CROSS_DIR} -c ${DEPLOY_DIR_PSTAGE}/stamp-cache-cross -u || true
277 populate_staging_postamble () {
278 if [ "$PSTAGING_ACTIVE" = "1" ]; then
279 # list the packages currently installed in staging
280 # ${PSTAGE_LIST_CMD} | awk '{print $1}' > ${DEPLOY_DIR_PSTAGE}/installed-list
282 # exitcode == 5 is ok, it means the files change
284 stage-manager -p ${STAGING_DIR} -c ${DEPLOY_DIR_PSTAGE}/stamp-cache-staging -u -d ${PSTAGE_TMPDIR_STAGE}/staging
286 if [ "$exitcode" != "5" -a "$exitcode" != "0" ]; then
289 stage-manager -p ${CROSS_DIR} -c ${DEPLOY_DIR_PSTAGE}/stamp-cache-cross -u -d ${PSTAGE_TMPDIR_STAGE}/cross
290 if [ "$exitcode" != "5" -a "$exitcode" != "0" ]; then
297 do_populate_staging[lockfiles] = "${STAGING_DIR}/staging.lock"
298 do_populate_staging[dirs] =+ "${DEPLOY_DIR_PSTAGE}"
299 python do_populate_staging_prepend() {
300 bb.build.exec_func("populate_staging_preamble", d)
303 python do_populate_staging_append() {
304 bb.build.exec_func("populate_staging_postamble", d)
308 staging_packager () {
310 mkdir -p ${PSTAGE_TMPDIR_STAGE}/CONTROL
311 mkdir -p ${DEPLOY_DIR_PSTAGE}/${PSTAGE_PKGPATH}
313 echo "Package: ${PSTAGE_PKGPN}" > ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
314 echo "Version: ${PSTAGE_PKGVERSION}" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
315 echo "Description: ${DESCRIPTION}" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
316 echo "Section: ${SECTION}" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
317 echo "Priority: Optional" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
318 echo "Maintainer: ${MAINTAINER}" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
319 echo "Architecture: ${PSTAGE_PKGARCH}" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
321 # Protect against empty SRC_URI
322 if [ "${SRC_URI}" != "" ] ; then
323 echo "Source: ${SRC_URI}" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
325 echo "Source: OpenEmbedded" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
328 ${PSTAGE_BUILD_CMD} ${PSTAGE_TMPDIR_STAGE} ${DEPLOY_DIR_PSTAGE}/${PSTAGE_PKGPATH}
331 staging_package_installer () {
332 #${PSTAGE_INSTALL_CMD} ${PSTAGE_PKG}
334 STATUSFILE=${TMPDIR}${layout_libdir}/opkg/status
335 echo "Package: ${PSTAGE_PKGPN}" >> $STATUSFILE
336 echo "Version: ${PSTAGE_PKGVERSION}" >> $STATUSFILE
337 echo "Status: install user installed" >> $STATUSFILE
338 echo "Architecture: ${PSTAGE_PKGARCH}" >> $STATUSFILE
339 echo "" >> $STATUSFILE
341 CTRLFILE=${TMPDIR}${layout_libdir}/opkg/info/${PSTAGE_PKGPN}.control
342 echo "Package: ${PSTAGE_PKGPN}" > $CTRLFILE
343 echo "Version: ${PSTAGE_PKGVERSION}" >> $CTRLFILE
344 echo "Architecture: ${PSTAGE_PKGARCH}" >> $CTRLFILE
346 cd ${PSTAGE_TMPDIR_STAGE}
347 find -type f | grep -v ./CONTROL | sed -e 's/^\.//' > ${TMPDIR}${layout_libdir}/opkg/info/${PSTAGE_PKGPN}.list
350 python do_package_stage () {
351 if bb.data.getVar("PSTAGING_ACTIVE", d, 1) != "1":
355 # Handle deploy/ packages
357 bb.build.exec_func("read_subpackage_metadata", d)
358 stagepath = bb.data.getVar("PSTAGE_TMPDIR_STAGE", d, 1)
359 tmpdir = bb.data.getVar("TMPDIR", d, True)
360 packages = (bb.data.getVar('PACKAGES', d, 1) or "").split()
361 if len(packages) > 0:
362 if bb.data.inherits_class('package_ipk', d):
363 ipkpath = bb.data.getVar('DEPLOY_DIR_IPK', d, True).replace(tmpdir, stagepath)
364 if bb.data.inherits_class('package_deb', d):
365 debpath = bb.data.getVar('DEPLOY_DIR_DEB', d, True).replace(tmpdir, stagepath)
368 pkgname = bb.data.getVar('PKG_%s' % pkg, d, 1)
371 arch = bb.data.getVar('PACKAGE_ARCH_%s' % pkg, d, 1)
373 arch = bb.data.getVar('PACKAGE_ARCH', d, 1)
374 pr = bb.data.getVar('PR_%s' % pkg, d, 1)
376 pr = bb.data.getVar('PR', d, 1)
377 if not packaged(pkg, d):
379 if bb.data.inherits_class('package_ipk', d):
380 srcname = bb.data.expand(pkgname + "_${PV}-" + pr + "_" + arch + ".ipk", d)
381 srcfile = bb.data.expand("${DEPLOY_DIR_IPK}/" + arch + "/" + srcname, d)
382 if os.path.exists(srcfile):
383 destpath = ipkpath + "/" + arch + "/"
384 bb.mkdirhier(destpath)
385 bb.copyfile(srcfile, destpath + srcname)
387 if bb.data.inherits_class('package_deb', d):
389 srcname = bb.data.expand(pkgname + "_${PV}-" + pr + "_all.deb", d)
391 srcname = bb.data.expand(pkgname + "_${PV}-" + pr + "_${DPKG_ARCH}.deb", d)
392 srcfile = bb.data.expand("${DEPLOY_DIR_DEB}/" + arch + "/" + srcname, d)
393 if os.path.exists(srcfile):
394 destpath = debpath + "/" + arch + "/"
395 bb.mkdirhier(destpath)
396 bb.copyfile(srcfile, destpath + srcname)
399 # Handle stamps/ files
401 stampfn = bb.data.getVar("STAMP", d, True)
402 destdir = os.path.dirname(stampfn.replace(tmpdir, stagepath))
403 bb.mkdirhier(destdir)
404 # We need to include the package_stage stamp in the staging package so create one
405 bb.build.make_stamp("do_package_stage", d)
406 os.system("cp -dpR %s.do_* %s/" % (stampfn, destdir))
408 pstage_set_pkgmanager(d)
409 bb.build.exec_func("staging_helper", d)
410 bb.build.exec_func("staging_packager", d)
411 lf = bb.utils.lockfile(bb.data.expand("${STAGING_DIR}/staging.lock", d))
412 bb.build.exec_func("staging_package_installer", d)
413 bb.utils.unlockfile(lf)
417 # Note an assumption here is that do_deploy runs before do_package_write/do_populate_staging
419 addtask package_stage after do_package_write do_populate_staging before do_build
421 do_package_stage_all () {
424 do_package_stage_all[recrdeptask] = "do_package_stage"
425 addtask package_stage_all after do_package_stage before do_build