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 = "${PACKAGE_ARCH}-${HOST_OS}"
16 PSTAGE_EXTRAPATH ?= "/${OELAYOUT_ABI}/${DISTRO_PR}/"
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 = "${PSTAGE_DIR}/${PSTAGE_PKGPATH}/${PSTAGE_PKGNAME}"
21 PSTAGE_WORKDIR = "${TMPDIR}/pstage"
22 PSTAGE_SCAN_CMD ?= "find ${PSTAGE_TMPDIR_STAGE} \( -name "*.la" -o -name "*-config" \) -type f"
24 PSTAGE_NATIVEDEPENDS = "\
29 BB_STAMP_WHITELIST = "${PSTAGE_NATIVEDEPENDS}"
31 python __anonymous() {
33 pstage_host_mangle = False
34 pstage_is_cross = False
36 # PSTAGE_NATIVEDEPENDS lists the packages we need before we can use
37 # packaged staging. There will always be some packages we depend on.
38 if bb.data.inherits_class('native', d):
39 pn = bb.data.getVar('PN', d, True)
40 nativedeps = bb.data.getVar('PSTAGE_NATIVEDEPENDS', d, True).split()
42 pstage_allowed = False
44 # Images aren't of interest to us
45 if bb.data.inherits_class('image', d):
46 pstage_allowed = False
48 # We need OVERRIDES to be evaluated and applied.
49 localdata = d.createCopy()
50 bb.data.update_data(localdata)
51 if localdata.getVar('PSTAGING_DISABLED', True) == "1":
52 pstage_allowed = False
54 # libtool-cross does not inherit cross.
55 if localdata.getVar('PN', True).startswith("libtool-cross"):
56 pstage_host_mangle = True
57 pstage_is_cross = True
59 # We need PSTAGE_PKGARCH to contain information about the target.
60 if bb.data.inherits_class('cross', d):
61 pstage_host_mangle = True
63 if pstage_host_mangle:
64 bb.data.setVar('PSTAGE_PKGARCH', "${HOST_SYS}-${PACKAGE_ARCH}-${TARGET_OS}", d)
66 # These classes encode staging paths data files so we must mangle them
68 if bb.data.inherits_class('native', d) or bb.data.inherits_class('nativesdk', d) or bb.data.inherits_class('cross', d) or bb.data.inherits_class('crosssdk', d) or bb.data.inherits_class('sdk', d):
69 pstage_is_cross = True
72 scan_cmd = "grep -Irl ${TMPDIR} ${PSTAGE_TMPDIR_STAGE}"
73 bb.data.setVar('PSTAGE_SCAN_CMD', scan_cmd, d)
75 # Add task dependencies if we're active, otherwise mark packaged staging
78 deps = bb.data.getVarFlag('do_setscene', 'depends', d) or ""
79 deps += " stagemanager-native:do_populate_sysroot"
80 bb.data.setVarFlag('do_setscene', 'depends', deps, d)
82 policy = bb.data.getVar("BB_STAMP_POLICY", d, True)
83 if policy == "whitelist" or policy == "full":
84 deps = bb.data.getVarFlag('do_setscene', 'recrdeptask', d) or ""
85 deps += " do_setscene"
86 bb.data.setVarFlag('do_setscene', 'recrdeptask', deps, d)
88 bb.data.setVar("PSTAGING_ACTIVE", "1", d)
90 bb.data.setVar("PSTAGING_ACTIVE", "0", d)
93 PSTAGE_MACHCONFIG = "${PSTAGE_WORKDIR}/opkg.conf"
95 PSTAGE_PKGMANAGER = "stage-manager-ipkg"
97 PSTAGE_BUILD_CMD = "stage-manager-ipkg-build -o 0 -g 0"
98 PSTAGE_INSTALL_CMD = "${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -force-depends -o ${TMPDIR} install"
99 PSTAGE_UPDATE_CMD = "${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -o ${TMPDIR} update"
100 PSTAGE_REMOVE_CMD = "${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -force-depends -o ${TMPDIR} remove"
101 PSTAGE_LIST_CMD = "${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -o ${TMPDIR} list_installed"
103 PSTAGE_TMPDIR_STAGE = "${WORKDIR}/staging-pkg"
105 def pstage_manualclean(srcname, destvarname, d):
106 src = os.path.join(bb.data.getVar('PSTAGE_TMPDIR_STAGE', d, True), srcname)
107 dest = bb.data.getVar(destvarname, d, True)
109 for walkroot, dirs, files in os.walk(src):
110 bb.debug(2, "rm %s" % walkroot)
112 # Avoid breaking the held lock
113 if (file == "staging.lock"):
115 filepath = os.path.join(walkroot, file).replace(src, dest)
116 oe.path.remove(filepath)
118 def pstage_set_pkgmanager(d):
119 path = bb.data.getVar("PATH", d, 1)
120 pkgmanager = bb.which(path, 'opkg-cl')
122 pkgmanager = bb.which(path, 'ipkg-cl')
124 bb.data.setVar("PSTAGE_PKGMANAGER", pkgmanager, d)
127 def pstage_cleanpackage(pkgname, d):
128 pstage_set_pkgmanager(d)
129 list_cmd = bb.data.getVar("PSTAGE_LIST_CMD", d, True)
131 bb.debug(2, "Checking if staging package installed")
132 lf = bb.utils.lockfile(bb.data.expand("${SYSROOT_LOCK}", d))
134 oe_run(d, "%s | grep %s" % (list_cmd, pkgname))
136 bb.debug(1, "Manually removing any installed files from staging...")
137 pstage_manualclean("sysroots", "STAGING_DIR", d)
138 pstage_manualclean("deploy", "DEPLOY_DIR", d)
140 bb.debug(1, "Uninstalling package from staging...")
141 removecmd = bb.data.getVar("PSTAGE_REMOVE_CMD", d, 1)
143 oe_run(d, "%s %s" % (removecmd, pkgname))
145 bb.note("Failure removing staging package")
147 bb.utils.unlockfile(lf)
151 Clear the build and temp directories
154 removepkg = bb.data.expand("${PSTAGE_PKGPN}", d)
155 pstage_cleanpackage(removepkg, d)
157 stagepkg = bb.data.expand("${PSTAGE_PKG}", d)
158 if os.path.exists(stagepkg):
159 bb.note("Removing staging package %s" % base_path_out(stagepkg, d))
160 oe.path.remove(stagepkg)
161 oe.path.remove(stagepkg + ".md5")
165 # Assemble appropriate opkg.conf
166 conffile=${PSTAGE_MACHCONFIG}
167 mkdir -p ${PSTAGE_WORKDIR}/pstaging_lists
168 arch="${PSTAGE_PKGARCH}"
169 if [ ! -e $conffile ]; then
170 echo "arch $arch 1" > $conffile
171 echo "dest root /" >> $conffile
172 elif [ `grep -c " $arch " $conffile` -eq 0 ]; then
173 priority=$(expr `grep -cE "^arch" $conffile` + 1)
174 sed -i -e "/dest/iarch $arch $priority" $conffile
176 if [ ! -e ${TMPDIR}${libdir_native}/opkg/info/ ]; then
177 mkdir -p ${TMPDIR}${libdir_native}/opkg/info/
179 if [ ! -e ${TMPDIR}${libdir_native}/ipkg/ ]; then
180 ln -sf opkg/ ${TMPDIR}${libdir_native}/ipkg || true
184 def staging_fetch(stagepkg, d):
187 # only try and fetch if the user has configured a mirror
188 if bb.data.getVar('PSTAGE_MIRROR', d) != "":
189 # Copy the data object and override DL_DIR and SRC_URI
191 dldir = bb.data.expand("${PSTAGE_DIR}/${PSTAGE_PKGPATH}", pd)
192 mirror = bb.data.expand("${PSTAGE_MIRROR}/${PSTAGE_PKGPATH}/", pd)
193 srcuri = mirror + os.path.basename(stagepkg)
194 bb.data.setVar('DL_DIR', dldir, pd)
195 bb.data.setVar('SRC_URI', srcuri, pd)
197 # Try a fetch from the pstage mirror, if it fails just return and
198 # we will build the package
200 bb.fetch.init([srcuri], pd)
201 bb.fetch.go(pd, [srcuri])
205 PSTAGE_TASKS_COVERED = "fetch unpack munge patch configure qa_configure rig_locales compile sizecheck install deploy package populate_sysroot package_write_deb package_write_ipk package_write package_stage qa_staging"
207 SCENEFUNCS += "packagestage_scenefunc"
209 python packagestage_scenefunc () {
211 if bb.data.getVar("PSTAGING_ACTIVE", d, 1) == "0":
214 bb.build.exec_func("staging_helper", d)
216 removepkg = bb.data.expand("${PSTAGE_PKGPN}", d)
218 pstage_cleanpackage(removepkg, d)
220 stagepkg = bb.data.expand("${PSTAGE_PKG}", d)
221 if not os.path.exists(stagepkg):
222 staging_fetch(stagepkg, d)
224 if os.path.exists(stagepkg):
225 pstage_set_pkgmanager(d)
226 file = bb.data.getVar("FILE", d, True)
227 bb.debug(2, "Packaged staging active for %s\n" % file)
230 # Install the staging package somewhere temporarily so we can extract the stamp files
232 bb.mkdirhier(bb.data.expand("${WORKDIR}/tstage/${libdir_native}/opkg/info/ ", d))
233 cmd = bb.data.expand("${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -force-depends -o ${WORKDIR}/tstage install", d)
235 oe_run(d, "%s %s" % (cmd, stagepkg))
237 bb.fatal("Couldn't install the staging package to a temp directory")
240 # Grab the staging lock now so that we don't have other threads try and
241 # validate or install based on these stamps being valid. This is a
242 # potential issue for certain BB_STAMP_POLICY values and enough
243 # concurrent threads.
245 lf = bb.utils.lockfile(bb.data.expand("${SYSROOT_LOCK}", d))
248 # Copy the stamp files into the main stamps directoy
250 cmd = bb.data.expand("cp -PpR ${WORKDIR}/tstage/stamps/* ${TMPDIR}/stamps/", d)
254 bb.utils.unlockfile(lf)
255 bb.fatal("Couldn't copy the staging package stamp files")
258 # Iterate over the stamps seeing if they're valid. If we find any that
259 # are invalid or the task wasn't in the taskgraph, assume caution and
262 # FIXME - some tasks are safe to ignore in the task graph. e.g. package_write_*
264 taskscovered = bb.data.getVar("PSTAGE_TASKS_COVERED", d, True).split()
265 stamp = bb.data.getVar("STAMP", d, True)
266 for task in taskscovered:
268 stampfn = "%s.%s" % (stamp, task)
269 bb.debug(1, "Checking %s" % (stampfn))
270 if os.path.exists(stampfn):
271 stageok = bb.runqueue.check_stamp_fn(file, task, d)
272 bb.debug(1, "Result %s" % (stageok))
274 bb.utils.unlockfile(lf)
277 # Remove the stamps and files we added above
278 # FIXME - we should really only remove the stamps we added
279 for fname in glob.glob(stamp + '.*'):
280 oe.path.remove(fname)
282 oe.path.remove(bb.data.expand("${WORKDIR}/tstage", d))
284 bb.note("Staging package found, using it for %s." % file)
285 installcmd = bb.data.getVar("PSTAGE_INSTALL_CMD", d, 1)
287 ret = oe_run(d, "%s %s" % (installcmd, stagepkg))
289 bb.note("Failure installing prestage package")
291 bb.utils.unlockfile(lf)
293 bb.build.exec_func("staging_package_libtoolhack", d)
295 bb.build.make_stamp("do_stage_package_populated", d)
297 bb.note("Staging package found but invalid for %s" % file)
300 packagestage_scenefunc[cleandirs] = "${PSTAGE_TMPDIR_STAGE}"
301 packagestage_scenefunc[dirs] = "${STAGING_DIR}"
303 addhandler packagedstage_stampfixing_eventhandler
304 python packagedstage_stampfixing_eventhandler() {
305 if bb.event.getName(e) == "StampUpdate":
306 taskscovered = bb.data.getVar("PSTAGE_TASKS_COVERED", e.data, 1).split()
307 for (fn, task) in e.targets:
310 if task in taskscovered:
311 stamp = "%s.do_stage_package_populated" % e.stampPrefix[fn]
312 if os.path.exists(stamp):
313 # We're targetting a task which was skipped with packaged staging
314 # so we need to remove the autogenerated stamps.
315 for task in taskscovered:
316 covered = "%s.do_%s" % (e.stampPrefix[fn], task)
317 oe.path.remove(covered)
318 oe.path.remove(stamp)
321 populate_sysroot_preamble () {
322 if [ "$PSTAGING_ACTIVE" = "1" ]; then
323 stage-manager -p ${STAGING_DIR} -c ${PSTAGE_WORKDIR}/stamp-cache-staging -u || true
327 populate_sysroot_postamble () {
328 if [ "$PSTAGING_ACTIVE" = "1" ]; then
329 # list the packages currently installed in staging
330 # ${PSTAGE_LIST_CMD} | awk '{print $1}' > ${PSTAGE_WORKDIR}/installed-list
332 # exitcode == 5 is ok, it means the files change
334 stage-manager -p ${STAGING_DIR} -c ${PSTAGE_WORKDIR}/stamp-cache-staging -u -d ${PSTAGE_TMPDIR_STAGE}/sysroots
336 if [ "$exitcode" != "5" -a "$exitcode" != "0" ]; then
343 packagedstaging_fastpath () {
344 mkdir -p ${PSTAGE_TMPDIR_STAGE}/sysroots/
345 cp -fpPR ${SYSROOT_DESTDIR}/${STAGING_DIR}/* ${PSTAGE_TMPDIR_STAGE}/sysroots/ || /bin/true
348 do_populate_sysroot[dirs] =+ "${PSTAGE_DIR}"
349 python populate_sysroot_prehook() {
350 bb.build.exec_func("populate_sysroot_preamble", d)
353 python populate_sysroot_posthook() {
354 bb.build.exec_func("populate_sysroot_postamble", d)
358 staging_packager () {
360 mkdir -p ${PSTAGE_TMPDIR_STAGE}/CONTROL
361 mkdir -p ${PSTAGE_DIR}/${PSTAGE_PKGPATH}
363 echo "Package: ${PSTAGE_PKGPN}" > ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
364 echo "Version: ${PSTAGE_PKGVERSION}" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
365 echo "Description: ${DESCRIPTION}" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
366 echo "Section: ${SECTION}" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
367 echo "Priority: Optional" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
368 echo "Maintainer: ${MAINTAINER}" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
369 echo "Architecture: ${PSTAGE_PKGARCH}" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
371 # Protect against empty SRC_URI
373 if [ "$srcuri" = "" ]; then
374 srcuri="OpenEmbedded"
376 echo "Source: $srcuri" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
378 # Deal with libtool not supporting sysroots
379 # Need to remove hardcoded paths and fix these when we install the
381 # Could someone please add sysroot support to libtool!
382 for i in `${PSTAGE_SCAN_CMD}` ; do \
383 sed -i -e s:${STAGING_DIR}:FIXMESTAGINGDIR:g $i
384 echo $i | sed -e 's:${PSTAGE_TMPDIR_STAGE}/::' >> ${PSTAGE_TMPDIR_STAGE}/sysroots/fixmepath
387 ${PSTAGE_BUILD_CMD} ${PSTAGE_TMPDIR_STAGE} ${PSTAGE_DIR}/${PSTAGE_PKGPATH}
390 staging_package_installer () {
391 #${PSTAGE_INSTALL_CMD} ${PSTAGE_PKG}
393 STATUSFILE=${TMPDIR}${libdir_native}/opkg/status
394 echo "Package: ${PSTAGE_PKGPN}" >> $STATUSFILE
395 echo "Version: ${PSTAGE_PKGVERSION}" >> $STATUSFILE
396 echo "Status: install user installed" >> $STATUSFILE
397 echo "Architecture: ${PSTAGE_PKGARCH}" >> $STATUSFILE
398 echo "" >> $STATUSFILE
400 CTRLFILE=${TMPDIR}${libdir_native}/opkg/info/${PSTAGE_PKGPN}.control
401 echo "Package: ${PSTAGE_PKGPN}" > $CTRLFILE
402 echo "Version: ${PSTAGE_PKGVERSION}" >> $CTRLFILE
403 echo "Architecture: ${PSTAGE_PKGARCH}" >> $CTRLFILE
405 cd ${PSTAGE_TMPDIR_STAGE}
406 find -type f | grep -v ./CONTROL | sed -e 's/^\.//' > ${TMPDIR}${libdir_native}/opkg/info/${PSTAGE_PKGPN}.list
409 python staging_package_libtoolhack () {
410 # Deal with libtool not supporting sysroots and add our new
412 tmpdir = bb.data.getVar('TMPDIR', d, True)
413 staging = bb.data.getVar('STAGING_DIR', d, True)
414 fixmefn = staging + "/fixmepath"
416 fixmefd = open(fixmefn,"r")
417 fixmefiles = fixmefd.readlines()
419 oe.path.remove(fixmefn)
420 for file in fixmefiles:
421 oe_run(d, "sed -i -e s:FIXMESTAGINGDIR:%s:g %s" % (staging, tmpdir + '/' + file))
426 python do_package_stage () {
427 import oe.packagedata
429 if bb.data.getVar("PSTAGING_ACTIVE", d, 1) != "1":
433 # Handle deploy/ packages
435 bb.build.exec_func("read_subpackage_metadata", d)
436 stagepath = bb.data.getVar("PSTAGE_TMPDIR_STAGE", d, 1)
437 tmpdir = bb.data.getVar("TMPDIR", d, True)
438 packages = (bb.data.getVar('PACKAGES', d, 1) or "").split()
439 if len(packages) > 0:
440 if bb.data.inherits_class('package_ipk', d):
441 ipkpath = bb.data.getVar('DEPLOY_DIR_IPK', d, True).replace(tmpdir, stagepath)
442 if bb.data.inherits_class('package_deb', d):
443 debpath = bb.data.getVar('DEPLOY_DIR_DEB', d, True).replace(tmpdir, stagepath)
444 if bb.data.inherits_class('package_rpm', d):
445 rpmpath = bb.data.getVar('DEPLOY_DIR_RPM', d, True).replace(tmpdir, stagepath)
448 pkgname = bb.data.getVar('PKG_%s' % pkg, d, 1)
451 arch = bb.data.getVar('PACKAGE_ARCH_%s' % pkg, d, 1)
453 arch = bb.data.getVar('PACKAGE_ARCH', d, 1)
454 pr = bb.data.getVar('PR_%s' % pkg, d, 1)
456 pr = bb.data.getVar('PR', d, 1)
457 if not oe.packagedata.packaged(pkg, d):
459 if bb.data.inherits_class('package_ipk', d):
460 srcname = bb.data.expand(pkgname + "_${PKGV}-" + pr + "${DISTRO_PR}" + "_" + arch + ".ipk", d)
461 srcfile = bb.data.expand("${DEPLOY_DIR_IPK}/" + arch + "/" + srcname, d)
462 if os.path.exists(srcfile):
463 destpath = ipkpath + "/" + arch + "/"
464 bb.mkdirhier(destpath)
465 bb.copyfile(srcfile, destpath + srcname)
467 if bb.data.inherits_class('package_deb', d):
469 srcname = bb.data.expand(pkgname + "_${PV}-" + pr + "${DISTRO_PR}" + "_all.deb", d)
471 srcname = bb.data.expand(pkgname + "_${PV}-" + pr + "${DISTRO_PR}" + "_${DPKG_ARCH}.deb", d)
472 srcfile = bb.data.expand("${DEPLOY_DIR_DEB}/" + arch + "/" + srcname, d)
473 if os.path.exists(srcfile):
474 destpath = debpath + "/" + arch + "/"
475 bb.mkdirhier(destpath)
476 bb.copyfile(srcfile, destpath + srcname)
478 if bb.data.inherits_class('package_rpm', d):
479 version = bb.data.getVar('PV', d, 1)
480 version = version.replace('-', '+')
481 bb.data.setVar('RPMPV', version, d)
482 srcname = bb.data.expand(pkgname + "-${RPMPV}-" + pr + "${DISTRO_PR}" + ".${TARGET_ARCH}.rpm", d)
483 srcfile = bb.data.expand("${DEPLOY_DIR_RPM}/" + arch + "/" + srcname, d)
484 if os.path.exists(srcfile):
485 destpath = rpmpath + "/" + arch + "/"
486 bb.mkdirhier(destpath)
487 bb.copyfile(srcfile, destpath + srcname)
491 # Handle stamps/ files
493 stampfn = bb.data.getVar("STAMP", d, True)
494 destdir = os.path.dirname(stampfn.replace(tmpdir, stagepath))
495 bb.mkdirhier(destdir)
496 # We need to include the package_stage stamp in the staging package so create one
497 bb.build.make_stamp("do_package_stage", d)
498 oe_run(d, "cp -PpR %s.do_* %s/" % (stampfn, destdir))
500 pstage_set_pkgmanager(d)
501 bb.build.exec_func("staging_helper", d)
502 bb.build.exec_func("staging_packager", d)
503 lf = bb.utils.lockfile(bb.data.expand("${SYSROOT_LOCK}", d))
504 bb.build.exec_func("staging_package_installer", d)
505 bb.utils.unlockfile(lf)
509 # Note an assumption here is that do_deploy runs before do_package_write/do_populate_sysroot
511 addtask package_stage after do_package_write do_populate_sysroot before do_build
513 do_package_stage_all () {
516 do_package_stage_all[recrdeptask] = "do_package_stage"
517 addtask package_stage_all after do_package_stage before do_build