iscsi-target: Add package iscsi-target
[vuplus_openembedded] / classes / packaged-staging.bbclass
1 #
2 # Populate builds using prebuilt packages where possible to speed up builds
3 # and allow staging to be reconstructed.
4 #
5 # To use it add that line to conf/local.conf:
6 #
7 # INHERIT += "packaged-staging"
8 #
9
10
11 #
12 # bitbake.conf set PSTAGING_ACTIVE = "0", this class sets to "1" if we're active
13
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}"
21
22 # multimachine.bbclass will override this but add a default in case we're not using it
23 MULTIMACH_ARCH ?= "${PACKAGE_ARCH}"
24
25 PSTAGE_NATIVEDEPENDS = "\
26     shasum-native \
27     stagemanager-native \
28     "
29
30 BB_STAMP_WHITELIST = "${PSTAGE_NATIVEDEPENDS}"
31
32 python () {
33     import bb
34     pstage_allowed = True
35
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)
42
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()
48         if pn in nativedeps:
49             pstage_allowed = False
50
51     # Images aren't of interest to us
52     if bb.data.inherits_class('image', d):
53         pstage_allowed = False
54
55     # Add task dependencies if we're active, otherwise mark packaged staging
56     # as inactive.
57     if pstage_allowed:
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)
61
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)
67
68         bb.data.setVar("PSTAGING_ACTIVE", "1", d)
69     else:
70         bb.data.setVar("PSTAGING_ACTIVE", "0", d)
71 }
72
73 DEPLOY_DIR_PSTAGE   = "${DEPLOY_DIR}/pstage"
74 PSTAGE_MACHCONFIG   = "${DEPLOY_DIR_PSTAGE}/opkg.conf"
75
76 PSTAGE_PKGMANAGER = "stage-manager-ipkg"
77
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"
83
84 PSTAGE_TMPDIR_STAGE     = "${WORKDIR}/staging-pkg"
85
86 def pstage_manualclean(srcname, destvarname, d):
87         import os, bb
88
89         src = os.path.join(bb.data.getVar('PSTAGE_TMPDIR_STAGE', d, True), srcname)
90         dest = bb.data.getVar(destvarname, d, True)
91
92         for walkroot, dirs, files in os.walk(src):
93                 for file in files:
94                         filepath = os.path.join(walkroot, file).replace(src, dest)
95                         bb.note("rm %s" % filepath)
96                         os.system("rm %s" % filepath)
97
98 def pstage_set_pkgmanager(d):
99     import bb
100     path = bb.data.getVar("PATH", d, 1)
101     pkgmanager = bb.which(path, 'opkg-cl')
102     if pkgmanager == "":
103         pkgmanager = bb.which(path, 'ipkg-cl')
104     if pkgmanager != "":
105         bb.data.setVar("PSTAGE_PKGMANAGER", pkgmanager, d)
106
107
108 def pstage_cleanpackage(pkgname, d):
109         import os, bb
110
111         path = bb.data.getVar("PATH", d, 1)
112         pstage_set_pkgmanager(d)
113         list_cmd = bb.data.getVar("PSTAGE_LIST_CMD", d, True)
114
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))
118         if ret == 0:
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))
122                 if ret != 0:
123                         bb.note("Failure removing staging package")
124         else:
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)
129
130         bb.utils.unlockfile(lf)
131
132 do_clean_prepend() {
133         """
134         Clear the build and temp directories
135         """
136
137         removepkg = bb.data.expand("${PSTAGE_PKGPN}", d)
138         pstage_cleanpackage(removepkg, d)
139
140         stagepkg = bb.data.expand("${PSTAGE_PKG}", d)
141         bb.note("Removing staging package %s" % stagepkg)
142         os.system('rm -rf ' + stagepkg)
143 }
144
145 staging_helper () {
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}"
151                 priority=1
152                 for arch in $ipkgarchs; do
153                         echo "arch $arch $priority" >> $conffile
154                         priority=$(expr $priority + 5)
155                 done
156                 echo "dest root /" >> $conffile
157         fi
158         if [ ! -e ${TMPDIR}${layout_libdir}/opkg/info/ ]; then
159                 mkdir -p ${TMPDIR}${layout_libdir}/opkg/info/
160         fi
161         if [ ! -e ${TMPDIR}${layout_libdir}/ipkg/ ]; then
162                 cd ${TMPDIR}${layout_libdir}/
163                 ln -s opkg/ ipkg
164         fi
165 }
166
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"
168
169 SCENEFUNCS += "packagestage_scenefunc"
170
171 python packagestage_scenefunc () {
172     import os
173
174     if bb.data.getVar("PSTAGING_ACTIVE", d, 1) == "0":
175         return
176
177     bb.build.exec_func("staging_helper", d)
178
179     removepkg = bb.data.expand("${PSTAGE_PKGPN}", d)
180     pstage_cleanpackage(removepkg, d)
181
182     stagepkg = bb.data.expand("${PSTAGE_PKG}", d)
183
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)
189
190         #
191         # Install the staging package somewhere temporarily so we can extract the stamp files
192         #
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))
195         if ret != 0:
196             bb.fatal("Couldn't install the staging package to a temp directory")
197
198         #
199         # Copy the stamp files into the main stamps directoy
200         #
201         cmd = bb.data.expand("cp -dpR ${WORKDIR}/tstage/stamps/* ${TMPDIR}/stamps/", d)
202         ret = os.system(cmd)
203         if ret != 0:
204             bb.fatal("Couldn't copy the staging package stamp files")
205
206         #
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 
209         # do a rebuild.
210         #
211         # FIXME - some tasks are safe to ignore in the task graph. e.g. package_write_*
212         stageok = True
213         taskscovered = bb.data.getVar("PSTAGE_TASKS_COVERED", d, True).split()
214         stamp = bb.data.getVar("STAMP", d, True)
215         for task in taskscovered:
216             task = 'do_' + task
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))
222                 if not stageok:
223                     break
224
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))
229
230         if stageok:
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)
236             if ret != 0:
237                 bb.note("Failure installing prestage package")
238
239             bb.build.make_stamp("do_stage_package_populated", d)
240         else:
241             bb.note("Staging package found but invalid for %s" % file)
242
243 }
244 packagestage_scenefunc[cleandirs] = "${PSTAGE_TMPDIR_STAGE}"
245 packagestage_scenefunc[dirs] = "${STAGING_DIR}"
246
247 addhandler packagedstage_stampfixing_eventhandler
248 python packagedstage_stampfixing_eventhandler() {
249     from bb.event import getName
250     import os
251
252     if getName(e) == "StampUpdate":
253         taskscovered = bb.data.getVar("PSTAGE_TASKS_COVERED", e.data, 1).split()
254         for (fn, task) in e.targets:
255             # strip off 'do_'
256             task = task[3:]
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)
266
267     return NotHandled
268 }
269
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
274         fi
275 }
276
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         
281
282                 # exitcode == 5 is ok, it means the files change
283                 set +e
284                 stage-manager -p ${STAGING_DIR} -c ${DEPLOY_DIR_PSTAGE}/stamp-cache-staging -u -d ${PSTAGE_TMPDIR_STAGE}/staging
285                 exitcode=$?
286                 if [ "$exitcode" != "5" -a "$exitcode" != "0" ]; then
287                         exit $exitcode
288                 fi
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
291                         exit $exitcode
292                 fi
293                 set -e
294         fi
295 }
296
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)
301 }
302
303 python do_populate_staging_append() {
304     bb.build.exec_func("populate_staging_postamble", d)
305 }
306
307
308 staging_packager () {
309
310         mkdir -p ${PSTAGE_TMPDIR_STAGE}/CONTROL
311         mkdir -p ${DEPLOY_DIR_PSTAGE}/${PSTAGE_PKGPATH}
312
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
320         
321         # Protect against empty SRC_URI
322         if [ "${SRC_URI}" != "" ] ; then                
323                 echo "Source: ${SRC_URI}"               >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
324         else
325                 echo "Source: OpenEmbedded"               >> ${PSTAGE_TMPDIR_STAGE}/CONTROL/control
326         fi
327         
328         ${PSTAGE_BUILD_CMD} ${PSTAGE_TMPDIR_STAGE} ${DEPLOY_DIR_PSTAGE}/${PSTAGE_PKGPATH}
329 }
330
331 staging_package_installer () {
332         #${PSTAGE_INSTALL_CMD} ${PSTAGE_PKG}
333
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
340
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
345
346         cd ${PSTAGE_TMPDIR_STAGE}
347         find -type f | grep -v ./CONTROL | sed -e 's/^\.//' > ${TMPDIR}${layout_libdir}/opkg/info/${PSTAGE_PKGPN}.list
348 }
349
350 python do_package_stage () {
351     if bb.data.getVar("PSTAGING_ACTIVE", d, 1) != "1":
352         return
353
354     #
355     # Handle deploy/ packages
356     #
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)
366
367         for pkg in packages:
368             pkgname = bb.data.getVar('PKG_%s' % pkg, d, 1)
369             if not pkgname:
370                 pkgname = pkg
371             arch = bb.data.getVar('PACKAGE_ARCH_%s' % pkg, d, 1)
372             if not arch:
373                 arch = bb.data.getVar('PACKAGE_ARCH', d, 1)
374             pr = bb.data.getVar('PR_%s' % pkg, d, 1)
375             if not pr:
376                 pr = bb.data.getVar('PR', d, 1)
377             if not packaged(pkg, d):
378                 continue
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)
386
387             if bb.data.inherits_class('package_deb', d):
388                 if arch == 'all':
389                     srcname = bb.data.expand(pkgname + "_${PV}-" + pr + "_all.deb", d)
390                 else:
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)
397
398     #
399     # Handle stamps/ files
400     #
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))
407
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)
414 }
415
416 #
417 # Note an assumption here is that do_deploy runs before do_package_write/do_populate_staging
418 #
419 addtask package_stage after do_package_write do_populate_staging before do_build
420
421 do_package_stage_all () {
422         :
423 }
424 do_package_stage_all[recrdeptask] = "do_package_stage"
425 addtask package_stage_all after do_package_stage before do_build