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() {
32 # We need PSTAGE_PKGARCH to contain information about the target.
33 if bb.data.inherits_class('cross', d):
34 bb.data.setVar('PSTAGE_PKGARCH', "${HOST_SYS}-${PACKAGE_ARCH}-${TARGET_OS}", d)
40 # These classes encode staging paths into the binary data so can only be
41 # reused if the path doesn't change/
42 if bb.data.inherits_class('native', d) or bb.data.inherits_class('cross', d) or bb.data.inherits_class('sdk', d) or bb.data.inherits_class('crosssdk', d):
43 path = bb.data.getVar('PSTAGE_PKGPATH', d, 1)
44 path = path + bb.data.getVar('TMPDIR', d, 1).replace('/', '-')
45 bb.data.setVar('PSTAGE_PKGPATH', path, d)
46 scan_cmd = "grep -Irl ${STAGING_DIR} ${PSTAGE_TMPDIR_STAGE}"
47 bb.data.setVar('PSTAGE_SCAN_CMD', scan_cmd, d)
49 # PSTAGE_NATIVEDEPENDS lists the packages we need before we can use packaged
50 # staging. There will always be some packages we depend on.
51 if bb.data.inherits_class('native', d):
52 pn = bb.data.getVar('PN', d, True)
53 nativedeps = bb.data.getVar('PSTAGE_NATIVEDEPENDS', d, True).split()
55 pstage_allowed = False
57 # Images aren't of interest to us
58 if bb.data.inherits_class('image', d):
59 pstage_allowed = False
61 if bb.data.getVar('PSTAGING_DISABLED', d, True) == "1":
62 pstage_allowed = False
64 # Add task dependencies if we're active, otherwise mark packaged staging
67 deps = bb.data.getVarFlag('do_setscene', 'depends', d) or ""
68 deps += " stagemanager-native:do_populate_sysroot"
69 bb.data.setVarFlag('do_setscene', 'depends', deps, d)
71 policy = bb.data.getVar("BB_STAMP_POLICY", d, True)
72 if policy == "whitelist" or policy == "full":
73 deps = bb.data.getVarFlag('do_setscene', 'recrdeptask', d) or ""
74 deps += " do_setscene"
75 bb.data.setVarFlag('do_setscene', 'recrdeptask', deps, d)
77 bb.data.setVar("PSTAGING_ACTIVE", "1", d)
79 bb.data.setVar("PSTAGING_ACTIVE", "0", d)
82 PSTAGE_MACHCONFIG = "${PSTAGE_WORKDIR}/opkg.conf"
84 PSTAGE_PKGMANAGER = "stage-manager-ipkg"
86 PSTAGE_BUILD_CMD = "stage-manager-ipkg-build -o 0 -g 0"
87 PSTAGE_INSTALL_CMD = "${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -force-depends -o ${TMPDIR} install"
88 PSTAGE_UPDATE_CMD = "${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -o ${TMPDIR} update"
89 PSTAGE_REMOVE_CMD = "${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -force-depends -o ${TMPDIR} remove"
90 PSTAGE_LIST_CMD = "${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -o ${TMPDIR} list_installed"
92 PSTAGE_TMPDIR_STAGE = "${WORKDIR}/staging-pkg"
94 def pstage_manualclean(srcname, destvarname, d):
95 src = os.path.join(bb.data.getVar('PSTAGE_TMPDIR_STAGE', d, True), srcname)
96 dest = bb.data.getVar(destvarname, d, True)
98 for walkroot, dirs, files in os.walk(src):
99 bb.debug("rm %s" % walkroot)
101 # Avoid breaking the held lock
102 if (file == "staging.lock"):
104 filepath = os.path.join(walkroot, file).replace(src, dest)
105 oe.path.remove(filepath)
107 def pstage_set_pkgmanager(d):
108 path = bb.data.getVar("PATH", d, 1)
109 pkgmanager = bb.which(path, 'opkg-cl')
111 pkgmanager = bb.which(path, 'ipkg-cl')
113 bb.data.setVar("PSTAGE_PKGMANAGER", pkgmanager, d)
116 def pstage_cleanpackage(pkgname, d):
117 path = bb.data.getVar("PATH", d, 1)
118 pstage_set_pkgmanager(d)
119 list_cmd = bb.data.getVar("PSTAGE_LIST_CMD", d, True)
121 bb.debug(2, "Checking if staging package installed")
122 lf = bb.utils.lockfile(bb.data.expand("${SYSROOT_LOCK}", d))
123 ret = os.system("PATH=\"%s\" %s | grep %s" % (path, list_cmd, pkgname))
125 bb.debug(1, "Uninstalling package from staging...")
126 removecmd = bb.data.getVar("PSTAGE_REMOVE_CMD", d, 1)
127 ret = os.system("PATH=\"%s\" %s %s" % (path, removecmd, pkgname))
129 bb.note("Failure removing staging package")
131 bb.debug(1, "Manually removing any installed files from staging...")
132 pstage_manualclean("sysroots", "STAGING_DIR", d)
133 pstage_manualclean("cross", "CROSS_DIR", d)
134 pstage_manualclean("deploy", "DEPLOY_DIR", d)
136 bb.utils.unlockfile(lf)
140 Clear the build and temp directories
143 removepkg = bb.data.expand("${PSTAGE_PKGPN}", d)
144 pstage_cleanpackage(removepkg, d)
146 stagepkg = bb.data.expand("${PSTAGE_PKG}", d)
147 if os.path.exists(stagepkg):
148 bb.note("Removing staging package %s" % base_path_out(stagepkg, d))
149 oe.path.remove(stagepkg)
150 oe.path.remove(stagepkg + ".md5")
154 # Assemble appropriate opkg.conf
155 conffile=${PSTAGE_MACHCONFIG}
156 mkdir -p ${PSTAGE_WORKDIR}/pstaging_lists
157 arch="${PSTAGE_PKGARCH}"
158 if [ ! -e $conffile ]; then
159 echo "arch $arch 1" > $conffile
160 echo "dest root /" >> $conffile
161 elif [ `grep -c " $arch " $conffile` -eq 0 ]; then
162 priority=$(expr `grep -cE "^arch" $conffile` + 1)
163 sed -i -e "/dest/iarch $arch $priority" $conffile
165 if [ ! -e ${TMPDIR}${libdir_native}/opkg/info/ ]; then
166 mkdir -p ${TMPDIR}${libdir_native}/opkg/info/
168 if [ ! -e ${TMPDIR}${libdir_native}/ipkg/ ]; then
169 ln -sf opkg/ ${TMPDIR}${libdir_native}/ipkg || true
173 def staging_fetch(stagepkg, d):
176 # only try and fetch if the user has configured a mirror
177 if bb.data.getVar('PSTAGE_MIRROR', d) != "":
178 # Copy the data object and override DL_DIR and SRC_URI
180 dldir = bb.data.expand("${PSTAGE_DIR}/${PSTAGE_PKGPATH}", pd)
181 mirror = bb.data.expand("${PSTAGE_MIRROR}/${PSTAGE_PKGPATH}/", pd)
182 srcuri = mirror + os.path.basename(stagepkg)
183 bb.data.setVar('DL_DIR', dldir, pd)
184 bb.data.setVar('SRC_URI', srcuri, pd)
186 # Try a fetch from the pstage mirror, if it fails just return and
187 # we will build the package
189 bb.fetch.init([srcuri], pd)
190 bb.fetch.go(pd, [srcuri])
194 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"
196 SCENEFUNCS += "packagestage_scenefunc"
198 python packagestage_scenefunc () {
200 if bb.data.getVar("PSTAGING_ACTIVE", d, 1) == "0":
203 bb.build.exec_func("staging_helper", d)
205 removepkg = bb.data.expand("${PSTAGE_PKGPN}", d)
207 pstage_cleanpackage(removepkg, d)
209 stagepkg = bb.data.expand("${PSTAGE_PKG}", d)
210 if not os.path.exists(stagepkg):
211 staging_fetch(stagepkg, d)
213 if os.path.exists(stagepkg):
214 path = bb.data.getVar("PATH", d, 1)
215 pstage_set_pkgmanager(d)
216 file = bb.data.getVar("FILE", d, True)
217 bb.debug(2, "Packaged staging active for %s\n" % file)
220 # Install the staging package somewhere temporarily so we can extract the stamp files
222 bb.mkdirhier(bb.data.expand("${WORKDIR}/tstage/${libdir_native}/opkg/info/ ", d))
223 cmd = bb.data.expand("${PSTAGE_PKGMANAGER} -f ${PSTAGE_MACHCONFIG} -force-depends -o ${WORKDIR}/tstage install", d)
224 ret = os.system("PATH=\"%s\" %s %s" % (path, cmd, stagepkg))
226 bb.fatal("Couldn't install the staging package to a temp directory")
229 # Grab the staging lock now so that we don't have other threads try and
230 # validate or install based on these stamps being valid. This is a
231 # potential issue for certain BB_STAMP_POLICY values and enough
232 # concurrent threads.
234 lf = bb.utils.lockfile(bb.data.expand("${SYSROOT_LOCK}", d))
237 # Copy the stamp files into the main stamps directoy
239 cmd = bb.data.expand("cp -dpR ${WORKDIR}/tstage/stamps/* ${TMPDIR}/stamps/", d)
242 bb.utils.unlockfile(lf)
243 bb.fatal("Couldn't copy the staging package stamp files")
246 # Iterate over the stamps seeing if they're valid. If we find any that
247 # are invalid or the task wasn't in the taskgraph, assume caution and
250 # FIXME - some tasks are safe to ignore in the task graph. e.g. package_write_*
252 taskscovered = bb.data.getVar("PSTAGE_TASKS_COVERED", d, True).split()
253 stamp = bb.data.getVar("STAMP", d, True)
254 for task in taskscovered:
256 stampfn = "%s.%s" % (stamp, task)
257 bb.debug(1, "Checking %s" % (stampfn))
258 if os.path.exists(stampfn):
259 stageok = bb.runqueue.check_stamp_fn(file, task, d)
260 bb.debug(1, "Result %s" % (stageok))
262 bb.utils.unlockfile(lf)
265 # Remove the stamps and files we added above
266 # FIXME - we should really only remove the stamps we added
267 for fname in glob.glob(stamp + '.*'):
268 oe.path.remove(fname)
270 oe.path.remove(bb.data.expand("${WORKDIR}/tstage", d))
272 bb.note("Staging package found, using it for %s." % file)
273 installcmd = bb.data.getVar("PSTAGE_INSTALL_CMD", d, 1)
274 ret = os.system("PATH=\"%s\" %s %s" % (path, installcmd, stagepkg))
275 bb.utils.unlockfile(lf)
277 bb.note("Failure installing prestage package")
279 bb.build.exec_func("staging_package_libtoolhack", d)
281 bb.build.make_stamp("do_stage_package_populated", d)
283 bb.note("Staging package found but invalid for %s" % file)
286 packagestage_scenefunc[cleandirs] = "${PSTAGE_TMPDIR_STAGE}"
287 packagestage_scenefunc[dirs] = "${STAGING_DIR}"
289 addhandler packagedstage_stampfixing_eventhandler
290 python packagedstage_stampfixing_eventhandler() {
291 if bb.event.getName(e) == "StampUpdate":
292 taskscovered = bb.data.getVar("PSTAGE_TASKS_COVERED", e.data, 1).split()
293 for (fn, task) in e.targets:
296 if task in taskscovered:
297 stamp = "%s.do_stage_package_populated" % e.stampPrefix[fn]
298 if os.path.exists(stamp):
299 # We're targetting a task which was skipped with packaged staging
300 # so we need to remove the autogenerated stamps.
301 for task in taskscovered:
302 covered = "%s.do_%s" % (e.stampPrefix[fn], task)
303 oe.path.remove(covered)
304 oe.path.remove(stamp)
307 populate_sysroot_preamble () {
308 if [ "$PSTAGING_ACTIVE" = "1" ]; then
309 stage-manager -p ${STAGING_DIR} -c ${PSTAGE_WORKDIR}/stamp-cache-staging -u || true
310 stage-manager -p ${CROSS_DIR} -c ${PSTAGE_WORKDIR}/stamp-cache-cross -u || true
314 populate_sysroot_postamble () {
315 if [ "$PSTAGING_ACTIVE" = "1" ]; then
316 # list the packages currently installed in staging
317 # ${PSTAGE_LIST_CMD} | awk '{print $1}' > ${PSTAGE_WORKDIR}/installed-list
319 # exitcode == 5 is ok, it means the files change
321 stage-manager -p ${STAGING_DIR} -c ${PSTAGE_WORKDIR}/stamp-cache-staging -u -d ${PSTAGE_TMPDIR_STAGE}/sysroots
323 if [ "$exitcode" != "5" -a "$exitcode" != "0" ]; then
326 stage-manager -p ${CROSS_DIR} -c ${PSTAGE_WORKDIR}/stamp-cache-cross -u -d ${PSTAGE_TMPDIR_STAGE}/cross/${BASE_PACKAGE_ARCH}
327 if [ "$exitcode" != "5" -a "$exitcode" != "0" ]; then
334 packagedstaging_fastpath () {
335 if [ "$PSTAGING_ACTIVE" = "1" ]; then
336 mkdir -p ${PSTAGE_TMPDIR_STAGE}/sysroots/
337 mkdir -p ${PSTAGE_TMPDIR_STAGE}/cross/${BASE_PACKAGE_ARCH}/
338 cp -fpPR ${SYSROOT_DESTDIR}/${STAGING_DIR}/* ${PSTAGE_TMPDIR_STAGE}/sysroots/ || /bin/true
339 cp -fpPR ${SYSROOT_DESTDIR}/${CROSS_DIR}/* ${PSTAGE_TMPDIR_STAGE}/cross/${BASE_PACKAGE_ARCH}/ || /bin/true
343 do_populate_sysroot[dirs] =+ "${PSTAGE_DIR}"
344 python populate_sysroot_prehook() {
345 bb.build.exec_func("populate_sysroot_preamble", d)
348 python populate_sysroot_posthook() {
349 bb.build.exec_func("populate_sysroot_postamble", d)
353 staging_packager () {
355 mkdir -p ${PSTAGE_TMPDIR_STAGE}/CONTROL
356 mkdir -p ${PSTAGE_DIR}/${PSTAGE_PKGPATH}
358 echo "Package: ${PSTAGE_PKGPN}" > ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
359 echo "Version: ${PSTAGE_PKGVERSION}" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
360 echo "Description: ${DESCRIPTION}" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
361 echo "Section: ${SECTION}" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
362 echo "Priority: Optional" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
363 echo "Maintainer: ${MAINTAINER}" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
364 echo "Architecture: ${PSTAGE_PKGARCH}" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
366 # Protect against empty SRC_URI
368 if [ "$srcuri" == "" ]; then
369 srcuri="OpenEmbedded"
371 echo "Source: $srcuri" >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
373 # Deal with libtool not supporting sysroots
374 # Need to remove hardcoded paths and fix these when we install the
376 # Could someone please add sysroot support to libtool!
377 for i in `${PSTAGE_SCAN_CMD}` ; do \
378 sed -i -e s:${STAGING_DIR}:FIXMESTAGINGDIR:g $i
379 echo $i | sed -e 's:${PSTAGE_TMPDIR_STAGE}/::' >> ${PSTAGE_TMPDIR_STAGE}/sysroots/fixmepath
382 ${PSTAGE_BUILD_CMD} ${PSTAGE_TMPDIR_STAGE} ${PSTAGE_DIR}/${PSTAGE_PKGPATH}
385 staging_package_installer () {
386 #${PSTAGE_INSTALL_CMD} ${PSTAGE_PKG}
388 STATUSFILE=${TMPDIR}${libdir_native}/opkg/status
389 echo "Package: ${PSTAGE_PKGPN}" >> $STATUSFILE
390 echo "Version: ${PSTAGE_PKGVERSION}" >> $STATUSFILE
391 echo "Status: install user installed" >> $STATUSFILE
392 echo "Architecture: ${PSTAGE_PKGARCH}" >> $STATUSFILE
393 echo "" >> $STATUSFILE
395 CTRLFILE=${TMPDIR}${libdir_native}/opkg/info/${PSTAGE_PKGPN}.control
396 echo "Package: ${PSTAGE_PKGPN}" > $CTRLFILE
397 echo "Version: ${PSTAGE_PKGVERSION}" >> $CTRLFILE
398 echo "Architecture: ${PSTAGE_PKGARCH}" >> $CTRLFILE
400 cd ${PSTAGE_TMPDIR_STAGE}
401 find -type f | grep -v ./CONTROL | sed -e 's/^\.//' > ${TMPDIR}${libdir_native}/opkg/info/${PSTAGE_PKGPN}.list
404 python staging_package_libtoolhack () {
405 # Deal with libtool not supporting sysroots and add our new
407 tmpdir = bb.data.getVar('TMPDIR', d, True)
408 staging = bb.data.getVar('STAGING_DIR', d, True)
409 fixmefn = staging + "/fixmepath"
411 fixmefd = open(fixmefn,"r")
412 fixmefiles = fixmefd.readlines()
414 oe.path.remove(fixmefn)
415 for file in fixmefiles:
416 os.system("sed -i -e s:FIXMESTAGINGDIR:%s:g %s" % (staging, tmpdir + '/' + file))
421 python do_package_stage () {
422 if bb.data.getVar("PSTAGING_ACTIVE", d, 1) != "1":
426 # Handle deploy/ packages
428 bb.build.exec_func("read_subpackage_metadata", d)
429 stagepath = bb.data.getVar("PSTAGE_TMPDIR_STAGE", d, 1)
430 tmpdir = bb.data.getVar("TMPDIR", d, True)
431 packages = (bb.data.getVar('PACKAGES', d, 1) or "").split()
432 if len(packages) > 0:
433 if bb.data.inherits_class('package_ipk', d):
434 ipkpath = bb.data.getVar('DEPLOY_DIR_IPK', d, True).replace(tmpdir, stagepath)
435 if bb.data.inherits_class('package_deb', d):
436 debpath = bb.data.getVar('DEPLOY_DIR_DEB', d, True).replace(tmpdir, stagepath)
437 if bb.data.inherits_class('package_rpm', d):
438 rpmpath = bb.data.getVar('DEPLOY_DIR_RPM', d, True).replace(tmpdir, stagepath)
441 pkgname = bb.data.getVar('PKG_%s' % pkg, d, 1)
444 arch = bb.data.getVar('PACKAGE_ARCH_%s' % pkg, d, 1)
446 arch = bb.data.getVar('PACKAGE_ARCH', d, 1)
447 pr = bb.data.getVar('PR_%s' % pkg, d, 1)
449 pr = bb.data.getVar('PR', d, 1)
450 if not packaged(pkg, d):
452 if bb.data.inherits_class('package_ipk', d):
453 srcname = bb.data.expand(pkgname + "_${PV}-" + pr + "${DISTRO_PR}" + "_" + arch + ".ipk", d)
454 srcfile = bb.data.expand("${DEPLOY_DIR_IPK}/" + arch + "/" + srcname, d)
455 if os.path.exists(srcfile):
456 destpath = ipkpath + "/" + arch + "/"
457 bb.mkdirhier(destpath)
459 bb.copyfile(srcfile, destpath + srcname)
461 if bb.data.inherits_class('package_deb', d):
463 srcname = bb.data.expand(pkgname + "_${PV}-" + pr + "${DISTRO_PR}" + "_all.deb", d)
465 srcname = bb.data.expand(pkgname + "_${PV}-" + pr + "${DISTRO_PR}" + "_${DPKG_ARCH}.deb", d)
466 srcfile = bb.data.expand("${DEPLOY_DIR_DEB}/" + arch + "/" + srcname, d)
467 if os.path.exists(srcfile):
468 destpath = debpath + "/" + arch + "/"
469 bb.mkdirhier(destpath)
470 bb.copyfile(srcfile, destpath + srcname)
472 if bb.data.inherits_class('package_rpm', d):
473 version = bb.data.getVar('PV', d, 1)
474 version = version.replace('-', '+')
475 bb.data.setVar('RPMPV', version, d)
476 srcname = bb.data.expand(pkgname + "-${RPMPV}-" + pr + "${DISTRO_PR}" + ".${TARGET_ARCH}.rpm", d)
477 srcfile = bb.data.expand("${DEPLOY_DIR_RPM}/" + arch + "/" + srcname, d)
478 if os.path.exists(srcfile):
479 destpath = rpmpath + "/" + arch + "/"
480 bb.mkdirhier(destpath)
481 bb.copyfile(srcfile, destpath + srcname)
485 # Handle stamps/ files
487 stampfn = bb.data.getVar("STAMP", d, True)
488 destdir = os.path.dirname(stampfn.replace(tmpdir, stagepath))
489 bb.mkdirhier(destdir)
490 # We need to include the package_stage stamp in the staging package so create one
491 bb.build.make_stamp("do_package_stage", d)
492 os.system("cp -dpR %s.do_* %s/" % (stampfn, destdir))
494 pstage_set_pkgmanager(d)
495 bb.build.exec_func("staging_helper", d)
496 bb.build.exec_func("staging_packager", d)
497 lf = bb.utils.lockfile(bb.data.expand("${SYSROOT_LOCK}", d))
498 bb.build.exec_func("staging_package_installer", d)
499 bb.utils.unlockfile(lf)
503 # Note an assumption here is that do_deploy runs before do_package_write/do_populate_sysroot
505 addtask package_stage after do_package_write do_populate_sysroot before do_build
507 do_package_stage_all () {
510 do_package_stage_all[recrdeptask] = "do_package_stage"
511 addtask package_stage_all after do_package_stage before do_build