blob: eb7295570dbdedc03a7ce04a24f36dd13605303c [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# Records history of build output in order to detect regressions
3#
4# Based in part on testlab.bbclass and packagehistory.bbclass
5#
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05006# Copyright (C) 2011-2016 Intel Corporation
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007# Copyright (C) 2007-2011 Koen Kooi <koen@openembedded.org>
8#
9
10BUILDHISTORY_FEATURES ?= "image package sdk"
11BUILDHISTORY_DIR ?= "${TOPDIR}/buildhistory"
12BUILDHISTORY_DIR_IMAGE = "${BUILDHISTORY_DIR}/images/${MACHINE_ARCH}/${TCLIBC}/${IMAGE_BASENAME}"
13BUILDHISTORY_DIR_PACKAGE = "${BUILDHISTORY_DIR}/packages/${MULTIMACH_TARGET_SYS}/${PN}"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050014
15# Setting this to non-empty will remove the old content of the buildhistory as part of
16# the current bitbake invocation and replace it with information about what was built
17# during the build.
18#
19# This is meant to be used in continuous integration (CI) systems when invoking bitbake
20# for full world builds. The effect in that case is that information about packages
21# that no longer get build also gets removed from the buildhistory, which is not
22# the case otherwise.
23#
24# The advantage over manually cleaning the buildhistory outside of bitbake is that
25# the "version-going-backwards" check still works. When relying on that, be careful
26# about failed world builds: they will lead to incomplete information in the
27# buildhistory because information about packages that could not be built will
28# also get removed. A CI system should handle that by discarding the buildhistory
29# of failed builds.
30#
31# The expected usage is via auto.conf, but passing via the command line also works
32# with: BB_ENV_EXTRAWHITE=BUILDHISTORY_RESET BUILDHISTORY_RESET=1
33BUILDHISTORY_RESET ?= ""
34
35BUILDHISTORY_OLD_DIR = "${BUILDHISTORY_DIR}/${@ "old" if "${BUILDHISTORY_RESET}" else ""}"
36BUILDHISTORY_OLD_DIR_PACKAGE = "${BUILDHISTORY_OLD_DIR}/packages/${MULTIMACH_TARGET_SYS}/${PN}"
37BUILDHISTORY_DIR_SDK = "${BUILDHISTORY_DIR}/sdk/${SDK_NAME}${SDK_EXT}/${IMAGE_BASENAME}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038BUILDHISTORY_IMAGE_FILES ?= "/etc/passwd /etc/group"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050039BUILDHISTORY_SDK_FILES ?= "conf/local.conf conf/bblayers.conf conf/auto.conf conf/locked-sigs.inc conf/devtool.conf"
Brad Bishop316dfdd2018-06-25 12:45:53 -040040BUILDHISTORY_COMMIT ?= "1"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050041BUILDHISTORY_COMMIT_AUTHOR ?= "buildhistory <buildhistory@${DISTRO}>"
42BUILDHISTORY_PUSH_REPO ?= ""
Andrew Geissler82c905d2020-04-13 13:39:40 -050043BUILDHISTORY_TAG ?= "build"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044
45SSTATEPOSTINSTFUNCS_append = " buildhistory_emit_pkghistory"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050046# We want to avoid influencing the signatures of sstate tasks - first the function itself:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047sstate_install[vardepsexclude] += "buildhistory_emit_pkghistory"
48# then the value added to SSTATEPOSTINSTFUNCS:
49SSTATEPOSTINSTFUNCS[vardepvalueexclude] .= "| buildhistory_emit_pkghistory"
50
Brad Bishop6e60e8b2018-02-01 10:27:11 -050051# Similarly for our function that gets the output signatures
52SSTATEPOSTUNPACKFUNCS_append = " buildhistory_emit_outputsigs"
53sstate_installpkgdir[vardepsexclude] += "buildhistory_emit_outputsigs"
54SSTATEPOSTUNPACKFUNCS[vardepvalueexclude] .= "| buildhistory_emit_outputsigs"
55
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050056# All items excepts those listed here will be removed from a recipe's
57# build history directory by buildhistory_emit_pkghistory(). This is
58# necessary because some of these items (package directories, files that
59# we no longer emit) might be obsolete.
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060#
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050061# When extending build history, derive your class from buildhistory.bbclass
62# and extend this list here with the additional files created by the derived
63# class.
Brad Bishop96ff1982019-08-19 13:50:42 -040064BUILDHISTORY_PRESERVE = "latest latest_srcrev sysroot"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050065
Patrick Williamsc0f7c042017-02-23 20:41:17 -060066PATCH_GIT_USER_EMAIL ?= "buildhistory@oe"
67PATCH_GIT_USER_NAME ?= "OpenEmbedded"
68
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050069#
Brad Bishop96ff1982019-08-19 13:50:42 -040070# Write out the contents of the sysroot
71#
72buildhistory_emit_sysroot() {
73 mkdir --parents ${BUILDHISTORY_DIR_PACKAGE}
74 case ${CLASSOVERRIDE} in
75 class-native|class-cross|class-crosssdk)
76 BASE=${SYSROOT_DESTDIR}/${STAGING_DIR_NATIVE}
77 ;;
78 *)
79 BASE=${SYSROOT_DESTDIR}
80 ;;
81 esac
82 buildhistory_list_files_no_owners $BASE ${BUILDHISTORY_DIR_PACKAGE}/sysroot
83}
84
85#
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050086# Write out metadata about this package for comparison when writing future packages
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087#
88python buildhistory_emit_pkghistory() {
Brad Bishop96ff1982019-08-19 13:50:42 -040089 if d.getVar('BB_CURRENTTASK') in ['populate_sysroot', 'populate_sysroot_setscene']:
90 bb.build.exec_func("buildhistory_emit_sysroot", d)
91
Brad Bishop6e60e8b2018-02-01 10:27:11 -050092 if not d.getVar('BB_CURRENTTASK') in ['packagedata', 'packagedata_setscene']:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093 return 0
94
Brad Bishop6e60e8b2018-02-01 10:27:11 -050095 if not "package" in (d.getVar('BUILDHISTORY_FEATURES') or "").split():
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096 return 0
97
98 import re
99 import json
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800100 import shlex
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 import errno
102
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500103 pkghistdir = d.getVar('BUILDHISTORY_DIR_PACKAGE')
104 oldpkghistdir = d.getVar('BUILDHISTORY_OLD_DIR_PACKAGE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105
106 class RecipeInfo:
107 def __init__(self, name):
108 self.name = name
109 self.pe = "0"
110 self.pv = "0"
111 self.pr = "r0"
112 self.depends = ""
113 self.packages = ""
114 self.srcrev = ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500115 self.layer = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116
117
118 class PackageInfo:
119 def __init__(self, name):
120 self.name = name
121 self.pe = "0"
122 self.pv = "0"
123 self.pr = "r0"
124 # pkg/pkge/pkgv/pkgr should be empty because we want to be able to default them
125 self.pkg = ""
126 self.pkge = ""
127 self.pkgv = ""
128 self.pkgr = ""
129 self.size = 0
130 self.depends = ""
131 self.rprovides = ""
132 self.rdepends = ""
133 self.rrecommends = ""
134 self.rsuggests = ""
135 self.rreplaces = ""
136 self.rconflicts = ""
137 self.files = ""
138 self.filelist = ""
139 # Variables that need to be written to their own separate file
140 self.filevars = dict.fromkeys(['pkg_preinst', 'pkg_postinst', 'pkg_prerm', 'pkg_postrm'])
141
142 # Should check PACKAGES here to see if anything removed
143
144 def readPackageInfo(pkg, histfile):
145 pkginfo = PackageInfo(pkg)
146 with open(histfile, "r") as f:
147 for line in f:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500148 lns = line.split('=', 1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500149 name = lns[0].strip()
150 value = lns[1].strip(" \t\r\n").strip('"')
151 if name == "PE":
152 pkginfo.pe = value
153 elif name == "PV":
154 pkginfo.pv = value
155 elif name == "PR":
156 pkginfo.pr = value
157 elif name == "PKG":
158 pkginfo.pkg = value
159 elif name == "PKGE":
160 pkginfo.pkge = value
161 elif name == "PKGV":
162 pkginfo.pkgv = value
163 elif name == "PKGR":
164 pkginfo.pkgr = value
165 elif name == "RPROVIDES":
166 pkginfo.rprovides = value
167 elif name == "RDEPENDS":
168 pkginfo.rdepends = value
169 elif name == "RRECOMMENDS":
170 pkginfo.rrecommends = value
171 elif name == "RSUGGESTS":
172 pkginfo.rsuggests = value
173 elif name == "RREPLACES":
174 pkginfo.rreplaces = value
175 elif name == "RCONFLICTS":
176 pkginfo.rconflicts = value
177 elif name == "PKGSIZE":
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600178 pkginfo.size = int(value)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179 elif name == "FILES":
180 pkginfo.files = value
181 elif name == "FILELIST":
182 pkginfo.filelist = value
183 # Apply defaults
184 if not pkginfo.pkg:
185 pkginfo.pkg = pkginfo.name
186 if not pkginfo.pkge:
187 pkginfo.pkge = pkginfo.pe
188 if not pkginfo.pkgv:
189 pkginfo.pkgv = pkginfo.pv
190 if not pkginfo.pkgr:
191 pkginfo.pkgr = pkginfo.pr
192 return pkginfo
193
194 def getlastpkgversion(pkg):
195 try:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500196 histfile = os.path.join(oldpkghistdir, pkg, "latest")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500197 return readPackageInfo(pkg, histfile)
198 except EnvironmentError:
199 return None
200
201 def sortpkglist(string):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500202 pkgiter = re.finditer(r'[a-zA-Z0-9.+-]+( \([><=]+[^)]+\))?', string, 0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500203 pkglist = [p.group(0) for p in pkgiter]
204 pkglist.sort()
205 return ' '.join(pkglist)
206
207 def sortlist(string):
208 items = string.split(' ')
209 items.sort()
210 return ' '.join(items)
211
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500212 pn = d.getVar('PN')
213 pe = d.getVar('PE') or "0"
214 pv = d.getVar('PV')
215 pr = d.getVar('PR')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500216 layer = bb.utils.get_file_layer(d.getVar('FILE'), d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500217
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500218 pkgdata_dir = d.getVar('PKGDATA_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500219 packages = ""
220 try:
221 with open(os.path.join(pkgdata_dir, pn)) as f:
222 for line in f.readlines():
223 if line.startswith('PACKAGES: '):
224 packages = oe.utils.squashspaces(line.split(': ', 1)[1])
225 break
226 except IOError as e:
227 if e.errno == errno.ENOENT:
228 # Probably a -cross recipe, just ignore
229 return 0
230 else:
231 raise
232
233 packagelist = packages.split()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500234 preserve = d.getVar('BUILDHISTORY_PRESERVE').split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500235 if not os.path.exists(pkghistdir):
236 bb.utils.mkdirhier(pkghistdir)
237 else:
238 # Remove files for packages that no longer exist
239 for item in os.listdir(pkghistdir):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500240 if item not in preserve:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500241 if item not in packagelist:
242 itempath = os.path.join(pkghistdir, item)
243 if os.path.isdir(itempath):
244 for subfile in os.listdir(itempath):
245 os.unlink(os.path.join(itempath, subfile))
246 os.rmdir(itempath)
247 else:
248 os.unlink(itempath)
249
250 rcpinfo = RecipeInfo(pn)
251 rcpinfo.pe = pe
252 rcpinfo.pv = pv
253 rcpinfo.pr = pr
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500254 rcpinfo.depends = sortlist(oe.utils.squashspaces(d.getVar('DEPENDS') or ""))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500255 rcpinfo.packages = packages
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500256 rcpinfo.layer = layer
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500257 write_recipehistory(rcpinfo, d)
258
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500259 pkgdest = d.getVar('PKGDEST')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500260 for pkg in packagelist:
261 pkgdata = {}
262 with open(os.path.join(pkgdata_dir, 'runtime', pkg)) as f:
263 for line in f.readlines():
264 item = line.rstrip('\n').split(': ', 1)
265 key = item[0]
266 if key.endswith('_' + pkg):
267 key = key[:-len(pkg)-1]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800268 pkgdata[key] = item[1].encode('latin-1').decode('unicode_escape')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500269
270 pkge = pkgdata.get('PKGE', '0')
271 pkgv = pkgdata['PKGV']
272 pkgr = pkgdata['PKGR']
273 #
274 # Find out what the last version was
275 # Make sure the version did not decrease
276 #
277 lastversion = getlastpkgversion(pkg)
278 if lastversion:
279 last_pkge = lastversion.pkge
280 last_pkgv = lastversion.pkgv
281 last_pkgr = lastversion.pkgr
282 r = bb.utils.vercmp((pkge, pkgv, pkgr), (last_pkge, last_pkgv, last_pkgr))
283 if r < 0:
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500284 msg = "Package version for package %s went backwards which would break package feeds (from %s:%s-%s to %s:%s-%s)" % (pkg, last_pkge, last_pkgv, last_pkgr, pkge, pkgv, pkgr)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500285 package_qa_handle_error("version-going-backwards", msg, d)
286
287 pkginfo = PackageInfo(pkg)
288 # Apparently the version can be different on a per-package basis (see Python)
289 pkginfo.pe = pkgdata.get('PE', '0')
290 pkginfo.pv = pkgdata['PV']
291 pkginfo.pr = pkgdata['PR']
292 pkginfo.pkg = pkgdata['PKG']
293 pkginfo.pkge = pkge
294 pkginfo.pkgv = pkgv
295 pkginfo.pkgr = pkgr
296 pkginfo.rprovides = sortpkglist(oe.utils.squashspaces(pkgdata.get('RPROVIDES', "")))
297 pkginfo.rdepends = sortpkglist(oe.utils.squashspaces(pkgdata.get('RDEPENDS', "")))
298 pkginfo.rrecommends = sortpkglist(oe.utils.squashspaces(pkgdata.get('RRECOMMENDS', "")))
299 pkginfo.rsuggests = sortpkglist(oe.utils.squashspaces(pkgdata.get('RSUGGESTS', "")))
300 pkginfo.rreplaces = sortpkglist(oe.utils.squashspaces(pkgdata.get('RREPLACES', "")))
301 pkginfo.rconflicts = sortpkglist(oe.utils.squashspaces(pkgdata.get('RCONFLICTS', "")))
302 pkginfo.files = oe.utils.squashspaces(pkgdata.get('FILES', ""))
303 for filevar in pkginfo.filevars:
304 pkginfo.filevars[filevar] = pkgdata.get(filevar, "")
305
306 # Gather information about packaged files
307 val = pkgdata.get('FILES_INFO', '')
308 dictval = json.loads(val)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600309 filelist = list(dictval.keys())
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500310 filelist.sort()
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800311 pkginfo.filelist = " ".join([shlex.quote(x) for x in filelist])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500312
313 pkginfo.size = int(pkgdata['PKGSIZE'])
314
315 write_pkghistory(pkginfo, d)
316
317 # Create files-in-<package-name>.txt files containing a list of files of each recipe's package
318 bb.build.exec_func("buildhistory_list_pkg_files", d)
319}
320
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500321python buildhistory_emit_outputsigs() {
322 if not "task" in (d.getVar('BUILDHISTORY_FEATURES') or "").split():
323 return
324
325 import hashlib
326
327 taskoutdir = os.path.join(d.getVar('BUILDHISTORY_DIR'), 'task', 'output')
328 bb.utils.mkdirhier(taskoutdir)
329 currenttask = d.getVar('BB_CURRENTTASK')
330 pn = d.getVar('PN')
331 taskfile = os.path.join(taskoutdir, '%s.%s' % (pn, currenttask))
332
333 cwd = os.getcwd()
334 filesigs = {}
335 for root, _, files in os.walk(cwd):
336 for fname in files:
337 if fname == 'fixmepath':
338 continue
339 fullpath = os.path.join(root, fname)
340 try:
341 if os.path.islink(fullpath):
342 sha256 = hashlib.sha256(os.readlink(fullpath).encode('utf-8')).hexdigest()
343 elif os.path.isfile(fullpath):
344 sha256 = bb.utils.sha256_file(fullpath)
345 else:
346 continue
347 except OSError:
348 bb.warn('buildhistory: unable to read %s to get output signature' % fullpath)
349 continue
350 filesigs[os.path.relpath(fullpath, cwd)] = sha256
351 with open(taskfile, 'w') as f:
352 for fpath, fsig in sorted(filesigs.items(), key=lambda item: item[0]):
353 f.write('%s %s\n' % (fpath, fsig))
354}
355
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500356
357def write_recipehistory(rcpinfo, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500358 bb.debug(2, "Writing recipe history")
359
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500360 pkghistdir = d.getVar('BUILDHISTORY_DIR_PACKAGE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500361
362 infofile = os.path.join(pkghistdir, "latest")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600363 with open(infofile, "w") as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500364 if rcpinfo.pe != "0":
365 f.write(u"PE = %s\n" % rcpinfo.pe)
366 f.write(u"PV = %s\n" % rcpinfo.pv)
367 f.write(u"PR = %s\n" % rcpinfo.pr)
368 f.write(u"DEPENDS = %s\n" % rcpinfo.depends)
369 f.write(u"PACKAGES = %s\n" % rcpinfo.packages)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500370 f.write(u"LAYER = %s\n" % rcpinfo.layer)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500371
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500372 write_latest_srcrev(d, pkghistdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500373
374def write_pkghistory(pkginfo, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500375 bb.debug(2, "Writing package history for package %s" % pkginfo.name)
376
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500377 pkghistdir = d.getVar('BUILDHISTORY_DIR_PACKAGE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500378
379 pkgpath = os.path.join(pkghistdir, pkginfo.name)
380 if not os.path.exists(pkgpath):
381 bb.utils.mkdirhier(pkgpath)
382
383 infofile = os.path.join(pkgpath, "latest")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600384 with open(infofile, "w") as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500385 if pkginfo.pe != "0":
386 f.write(u"PE = %s\n" % pkginfo.pe)
387 f.write(u"PV = %s\n" % pkginfo.pv)
388 f.write(u"PR = %s\n" % pkginfo.pr)
389
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600390 if pkginfo.pkg != pkginfo.name:
391 f.write(u"PKG = %s\n" % pkginfo.pkg)
392 if pkginfo.pkge != pkginfo.pe:
393 f.write(u"PKGE = %s\n" % pkginfo.pkge)
394 if pkginfo.pkgv != pkginfo.pv:
395 f.write(u"PKGV = %s\n" % pkginfo.pkgv)
396 if pkginfo.pkgr != pkginfo.pr:
397 f.write(u"PKGR = %s\n" % pkginfo.pkgr)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500398 f.write(u"RPROVIDES = %s\n" % pkginfo.rprovides)
399 f.write(u"RDEPENDS = %s\n" % pkginfo.rdepends)
400 f.write(u"RRECOMMENDS = %s\n" % pkginfo.rrecommends)
401 if pkginfo.rsuggests:
402 f.write(u"RSUGGESTS = %s\n" % pkginfo.rsuggests)
403 if pkginfo.rreplaces:
404 f.write(u"RREPLACES = %s\n" % pkginfo.rreplaces)
405 if pkginfo.rconflicts:
406 f.write(u"RCONFLICTS = %s\n" % pkginfo.rconflicts)
407 f.write(u"PKGSIZE = %d\n" % pkginfo.size)
408 f.write(u"FILES = %s\n" % pkginfo.files)
409 f.write(u"FILELIST = %s\n" % pkginfo.filelist)
410
411 for filevar in pkginfo.filevars:
412 filevarpath = os.path.join(pkgpath, "latest.%s" % filevar)
413 val = pkginfo.filevars[filevar]
414 if val:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600415 with open(filevarpath, "w") as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500416 f.write(val)
417 else:
418 if os.path.exists(filevarpath):
419 os.unlink(filevarpath)
420
421#
422# rootfs_type can be: image, sdk_target, sdk_host
423#
424def buildhistory_list_installed(d, rootfs_type="image"):
425 from oe.rootfs import image_list_installed_packages
426 from oe.sdk import sdk_list_installed_packages
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500427 from oe.utils import format_pkg_list
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500428
429 process_list = [('file', 'bh_installed_pkgs.txt'),\
430 ('deps', 'bh_installed_pkgs_deps.txt')]
431
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500432 if rootfs_type == "image":
433 pkgs = image_list_installed_packages(d)
434 else:
435 pkgs = sdk_list_installed_packages(d, rootfs_type == "sdk_target")
436
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500437 for output_type, output_file in process_list:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500438 output_file_full = os.path.join(d.getVar('WORKDIR'), output_file)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500439
440 with open(output_file_full, 'w') as output:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500441 output.write(format_pkg_list(pkgs, output_type))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500442
443python buildhistory_list_installed_image() {
444 buildhistory_list_installed(d)
445}
446
447python buildhistory_list_installed_sdk_target() {
448 buildhistory_list_installed(d, "sdk_target")
449}
450
451python buildhistory_list_installed_sdk_host() {
452 buildhistory_list_installed(d, "sdk_host")
453}
454
455buildhistory_get_installed() {
456 mkdir -p $1
457
458 # Get list of installed packages
459 pkgcache="$1/installed-packages.tmp"
460 cat ${WORKDIR}/bh_installed_pkgs.txt | sort > $pkgcache && rm ${WORKDIR}/bh_installed_pkgs.txt
461
462 cat $pkgcache | awk '{ print $1 }' > $1/installed-package-names.txt
463 if [ -s $pkgcache ] ; then
464 cat $pkgcache | awk '{ print $2 }' | xargs -n1 basename > $1/installed-packages.txt
465 else
466 printf "" > $1/installed-packages.txt
467 fi
468
469 # Produce dependency graph
470 # First, quote each name to handle characters that cause issues for dot
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500471 sed 's:\([^| ]*\):"\1":g' ${WORKDIR}/bh_installed_pkgs_deps.txt > $1/depends.tmp &&
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500472 rm ${WORKDIR}/bh_installed_pkgs_deps.txt
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500473 # Remove lines with rpmlib(...) and config(...) dependencies, change the
474 # delimiter from pipe to "->", set the style for recommend lines and
475 # turn versioned dependencies into edge labels.
476 sed -i -e '/rpmlib(/d' \
477 -e '/config(/d' \
478 -e 's:|: -> :' \
479 -e 's:"\[REC\]":[style=dotted]:' \
480 -e 's:"\([<>=]\+\)" "\([^"]*\)":[label="\1 \2"]:' \
481 $1/depends.tmp
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500482 # Add header, sorted and de-duped contents and footer and then delete the temp file
483 printf "digraph depends {\n node [shape=plaintext]\n" > $1/depends.dot
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500484 cat $1/depends.tmp | sort -u >> $1/depends.dot
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500485 echo "}" >> $1/depends.dot
486 rm $1/depends.tmp
487
488 # Produce installed package sizes list
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500489 oe-pkgdata-util -p ${PKGDATA_DIR} read-value "PKGSIZE" -n -f $pkgcache > $1/installed-package-sizes.tmp
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500490 cat $1/installed-package-sizes.tmp | awk '{print $2 "\tKiB\t" $1}' | sort -n -r > $1/installed-package-sizes.txt
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500491 rm $1/installed-package-sizes.tmp
492
493 # We're now done with the cache, delete it
494 rm $pkgcache
495
496 if [ "$2" != "sdk" ] ; then
497 # Produce some cut-down graphs (for readability)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500498 grep -v kernel-image $1/depends.dot | grep -v kernel-3 | grep -v kernel-4 > $1/depends-nokernel.dot
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500499 grep -v libc6 $1/depends-nokernel.dot | grep -v libgcc > $1/depends-nokernel-nolibc.dot
500 grep -v update- $1/depends-nokernel-nolibc.dot > $1/depends-nokernel-nolibc-noupdate.dot
501 grep -v kernel-module $1/depends-nokernel-nolibc-noupdate.dot > $1/depends-nokernel-nolibc-noupdate-nomodules.dot
502 fi
503
504 # add complementary package information
505 if [ -e ${WORKDIR}/complementary_pkgs.txt ]; then
506 cp ${WORKDIR}/complementary_pkgs.txt $1
507 fi
508}
509
510buildhistory_get_image_installed() {
511 # Anything requiring the use of the packaging system should be done in here
512 # in case the packaging files are going to be removed for this image
513
514 if [ "${@bb.utils.contains('BUILDHISTORY_FEATURES', 'image', '1', '0', d)}" = "0" ] ; then
515 return
516 fi
517
518 buildhistory_get_installed ${BUILDHISTORY_DIR_IMAGE}
519}
520
521buildhistory_get_sdk_installed() {
522 # Anything requiring the use of the packaging system should be done in here
523 # in case the packaging files are going to be removed for this SDK
524
525 if [ "${@bb.utils.contains('BUILDHISTORY_FEATURES', 'sdk', '1', '0', d)}" = "0" ] ; then
526 return
527 fi
528
529 buildhistory_get_installed ${BUILDHISTORY_DIR_SDK}/$1 sdk
530}
531
532buildhistory_get_sdk_installed_host() {
533 buildhistory_get_sdk_installed host
534}
535
536buildhistory_get_sdk_installed_target() {
537 buildhistory_get_sdk_installed target
538}
539
540buildhistory_list_files() {
541 # List the files in the specified directory, but exclude date/time etc.
Brad Bishop19323692019-04-05 15:28:33 -0400542 # This is somewhat messy, but handles where the size is not printed for device files under pseudo
543 ( cd $1
544 find_cmd='find . ! -path . -printf "%M %-10u %-10g %10s %p -> %l\n"'
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500545 if [ "$3" = "fakeroot" ] ; then
Brad Bishop19323692019-04-05 15:28:33 -0400546 eval ${FAKEROOTENV} ${FAKEROOTCMD} $find_cmd
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500547 else
Brad Bishop19323692019-04-05 15:28:33 -0400548 eval $find_cmd
549 fi | sort -k5 | sed 's/ * -> $//' > $2 )
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500550}
551
Brad Bishop96ff1982019-08-19 13:50:42 -0400552buildhistory_list_files_no_owners() {
553 # List the files in the specified directory, but exclude date/time etc.
554 # Also don't output the ownership data, but instead output just - - so
555 # that the same parsing code as for _list_files works.
556 # This is somewhat messy, but handles where the size is not printed for device files under pseudo
557 ( cd $1
558 find_cmd='find . ! -path . -printf "%M - - %10s %p -> %l\n"'
559 if [ "$3" = "fakeroot" ] ; then
560 eval ${FAKEROOTENV} ${FAKEROOTCMD} "$find_cmd"
561 else
562 eval "$find_cmd"
563 fi | sort -k5 | sed 's/ * -> $//' > $2 )
564}
565
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500566buildhistory_list_pkg_files() {
567 # Create individual files-in-package for each recipe's package
568 for pkgdir in $(find ${PKGDEST}/* -maxdepth 0 -type d); do
569 pkgname=$(basename $pkgdir)
570 outfolder="${BUILDHISTORY_DIR_PACKAGE}/$pkgname"
571 outfile="$outfolder/files-in-package.txt"
572 # Make sure the output folder exists so we can create the file
573 if [ ! -d $outfolder ] ; then
574 bbdebug 2 "Folder $outfolder does not exist, file $outfile not created"
575 continue
576 fi
577 buildhistory_list_files $pkgdir $outfile fakeroot
578 done
579}
580
581buildhistory_get_imageinfo() {
582 if [ "${@bb.utils.contains('BUILDHISTORY_FEATURES', 'image', '1', '0', d)}" = "0" ] ; then
583 return
584 fi
585
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500586 mkdir -p ${BUILDHISTORY_DIR_IMAGE}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500587 buildhistory_list_files ${IMAGE_ROOTFS} ${BUILDHISTORY_DIR_IMAGE}/files-in-image.txt
588
589 # Collect files requested in BUILDHISTORY_IMAGE_FILES
590 rm -rf ${BUILDHISTORY_DIR_IMAGE}/image-files
591 for f in ${BUILDHISTORY_IMAGE_FILES}; do
592 if [ -f ${IMAGE_ROOTFS}/$f ] ; then
593 mkdir -p ${BUILDHISTORY_DIR_IMAGE}/image-files/`dirname $f`
594 cp ${IMAGE_ROOTFS}/$f ${BUILDHISTORY_DIR_IMAGE}/image-files/$f
595 fi
596 done
597
598 # Record some machine-readable meta-information about the image
599 printf "" > ${BUILDHISTORY_DIR_IMAGE}/image-info.txt
600 cat >> ${BUILDHISTORY_DIR_IMAGE}/image-info.txt <<END
601${@buildhistory_get_imagevars(d)}
602END
603 imagesize=`du -ks ${IMAGE_ROOTFS} | awk '{ print $1 }'`
604 echo "IMAGESIZE = $imagesize" >> ${BUILDHISTORY_DIR_IMAGE}/image-info.txt
605
606 # Add some configuration information
607 echo "${MACHINE}: ${IMAGE_BASENAME} configured for ${DISTRO} ${DISTRO_VERSION}" > ${BUILDHISTORY_DIR_IMAGE}/build-id.txt
608
609 cat >> ${BUILDHISTORY_DIR_IMAGE}/build-id.txt <<END
610${@buildhistory_get_build_id(d)}
611END
612}
613
614buildhistory_get_sdkinfo() {
615 if [ "${@bb.utils.contains('BUILDHISTORY_FEATURES', 'sdk', '1', '0', d)}" = "0" ] ; then
616 return
617 fi
618
619 buildhistory_list_files ${SDK_OUTPUT} ${BUILDHISTORY_DIR_SDK}/files-in-sdk.txt
620
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500621 # Collect files requested in BUILDHISTORY_SDK_FILES
622 rm -rf ${BUILDHISTORY_DIR_SDK}/sdk-files
623 for f in ${BUILDHISTORY_SDK_FILES}; do
624 if [ -f ${SDK_OUTPUT}/${SDKPATH}/$f ] ; then
625 mkdir -p ${BUILDHISTORY_DIR_SDK}/sdk-files/`dirname $f`
626 cp ${SDK_OUTPUT}/${SDKPATH}/$f ${BUILDHISTORY_DIR_SDK}/sdk-files/$f
627 fi
628 done
629
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500630 # Record some machine-readable meta-information about the SDK
631 printf "" > ${BUILDHISTORY_DIR_SDK}/sdk-info.txt
632 cat >> ${BUILDHISTORY_DIR_SDK}/sdk-info.txt <<END
633${@buildhistory_get_sdkvars(d)}
634END
635 sdksize=`du -ks ${SDK_OUTPUT} | awk '{ print $1 }'`
636 echo "SDKSIZE = $sdksize" >> ${BUILDHISTORY_DIR_SDK}/sdk-info.txt
637}
638
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500639python buildhistory_get_extra_sdkinfo() {
640 import operator
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500641 from oe.sdk import get_extra_sdkinfo
642
643 sstate_dir = d.expand('${SDK_OUTPUT}/${SDKPATH}/sstate-cache')
644 extra_info = get_extra_sdkinfo(sstate_dir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500645
646 if d.getVar('BB_CURRENTTASK') == 'populate_sdk_ext' and \
647 "sdk" in (d.getVar('BUILDHISTORY_FEATURES') or "").split():
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500648 with open(d.expand('${BUILDHISTORY_DIR_SDK}/sstate-package-sizes.txt'), 'w') as f:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500649 filesizes_sorted = sorted(extra_info['filesizes'].items(), key=operator.itemgetter(1, 0), reverse=True)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500650 for fn, size in filesizes_sorted:
651 f.write('%10d KiB %s\n' % (size, fn))
652 with open(d.expand('${BUILDHISTORY_DIR_SDK}/sstate-task-sizes.txt'), 'w') as f:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500653 tasksizes_sorted = sorted(extra_info['tasksizes'].items(), key=operator.itemgetter(1, 0), reverse=True)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500654 for task, size in tasksizes_sorted:
655 f.write('%10d KiB %s\n' % (size, task))
656}
657
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500658# By using ROOTFS_POSTUNINSTALL_COMMAND we get in after uninstallation of
659# unneeded packages but before the removal of packaging files
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500660ROOTFS_POSTUNINSTALL_COMMAND += "buildhistory_list_installed_image ;"
661ROOTFS_POSTUNINSTALL_COMMAND += "buildhistory_get_image_installed ;"
662ROOTFS_POSTUNINSTALL_COMMAND[vardepvalueexclude] .= "| buildhistory_list_installed_image ;| buildhistory_get_image_installed ;"
663ROOTFS_POSTUNINSTALL_COMMAND[vardepsexclude] += "buildhistory_list_installed_image buildhistory_get_image_installed"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500664
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500665IMAGE_POSTPROCESS_COMMAND += "buildhistory_get_imageinfo ;"
666IMAGE_POSTPROCESS_COMMAND[vardepvalueexclude] .= "| buildhistory_get_imageinfo ;"
667IMAGE_POSTPROCESS_COMMAND[vardepsexclude] += "buildhistory_get_imageinfo"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500668
669# We want these to be the last run so that we get called after complementary package installation
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500670POPULATE_SDK_POST_TARGET_COMMAND_append = " buildhistory_list_installed_sdk_target;"
671POPULATE_SDK_POST_TARGET_COMMAND_append = " buildhistory_get_sdk_installed_target;"
672POPULATE_SDK_POST_TARGET_COMMAND[vardepvalueexclude] .= "| buildhistory_list_installed_sdk_target;| buildhistory_get_sdk_installed_target;"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500673
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500674POPULATE_SDK_POST_HOST_COMMAND_append = " buildhistory_list_installed_sdk_host;"
675POPULATE_SDK_POST_HOST_COMMAND_append = " buildhistory_get_sdk_installed_host;"
676POPULATE_SDK_POST_HOST_COMMAND[vardepvalueexclude] .= "| buildhistory_list_installed_sdk_host;| buildhistory_get_sdk_installed_host;"
677
678SDK_POSTPROCESS_COMMAND_append = " buildhistory_get_sdkinfo ; buildhistory_get_extra_sdkinfo; "
679SDK_POSTPROCESS_COMMAND[vardepvalueexclude] .= "| buildhistory_get_sdkinfo ; buildhistory_get_extra_sdkinfo; "
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500680
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500681python buildhistory_write_sigs() {
682 if not "task" in (d.getVar('BUILDHISTORY_FEATURES') or "").split():
683 return
684
685 # Create sigs file
686 if hasattr(bb.parse.siggen, 'dump_siglist'):
687 taskoutdir = os.path.join(d.getVar('BUILDHISTORY_DIR'), 'task')
688 bb.utils.mkdirhier(taskoutdir)
689 bb.parse.siggen.dump_siglist(os.path.join(taskoutdir, 'tasksigs.txt'))
690}
691
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500692def buildhistory_get_build_id(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500693 if d.getVar('BB_WORKERCONTEXT') != '1':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500694 return ""
695 localdata = bb.data.createCopy(d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500696 statuslines = []
697 for func in oe.data.typed_value('BUILDCFG_FUNCS', localdata):
698 g = globals()
699 if func not in g:
700 bb.warn("Build configuration function '%s' does not exist" % func)
701 else:
702 flines = g[func](localdata)
703 if flines:
704 statuslines.extend(flines)
705
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500706 statusheader = d.getVar('BUILDCFG_HEADER')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500707 return('\n%s\n%s\n' % (statusheader, '\n'.join(statuslines)))
708
Brad Bishop19323692019-04-05 15:28:33 -0400709def buildhistory_get_modified(path):
710 # copied from get_layer_git_status() in image-buildinfo.bbclass
711 import subprocess
712 try:
713 subprocess.check_output("""cd %s; export PSEUDO_UNLOAD=1; set -e;
714 git diff --quiet --no-ext-diff
715 git diff --quiet --no-ext-diff --cached""" % path,
716 shell=True,
717 stderr=subprocess.STDOUT)
718 return ""
719 except subprocess.CalledProcessError as ex:
720 # Silently treat errors as "modified", without checking for the
721 # (expected) return code 1 in a modified git repo. For example, we get
722 # output and a 129 return code when a layer isn't a git repo at all.
723 return " -- modified"
724
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500725def buildhistory_get_metadata_revs(d):
726 # We want an easily machine-readable format here, so get_layers_branch_rev isn't quite what we want
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500727 layers = (d.getVar("BBLAYERS") or "").split()
Brad Bishop19323692019-04-05 15:28:33 -0400728 medadata_revs = ["%-17s = %s:%s%s" % (os.path.basename(i), \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500729 base_get_metadata_git_branch(i, None).strip(), \
Brad Bishop19323692019-04-05 15:28:33 -0400730 base_get_metadata_git_revision(i, None), \
731 buildhistory_get_modified(i)) \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500732 for i in layers]
733 return '\n'.join(medadata_revs)
734
735def outputvars(vars, listvars, d):
736 vars = vars.split()
737 listvars = listvars.split()
738 ret = ""
739 for var in vars:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500740 value = d.getVar(var) or ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500741 if var in listvars:
742 # Squash out spaces
743 value = oe.utils.squashspaces(value)
744 ret += "%s = %s\n" % (var, value)
745 return ret.rstrip('\n')
746
747def buildhistory_get_imagevars(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500748 if d.getVar('BB_WORKERCONTEXT') != '1':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500749 return ""
750 imagevars = "DISTRO DISTRO_VERSION USER_CLASSES IMAGE_CLASSES IMAGE_FEATURES IMAGE_LINGUAS IMAGE_INSTALL BAD_RECOMMENDATIONS NO_RECOMMENDATIONS PACKAGE_EXCLUDE ROOTFS_POSTPROCESS_COMMAND IMAGE_POSTPROCESS_COMMAND"
751 listvars = "USER_CLASSES IMAGE_CLASSES IMAGE_FEATURES IMAGE_LINGUAS IMAGE_INSTALL BAD_RECOMMENDATIONS PACKAGE_EXCLUDE"
752 return outputvars(imagevars, listvars, d)
753
754def buildhistory_get_sdkvars(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500755 if d.getVar('BB_WORKERCONTEXT') != '1':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500756 return ""
757 sdkvars = "DISTRO DISTRO_VERSION SDK_NAME SDK_VERSION SDKMACHINE SDKIMAGE_FEATURES BAD_RECOMMENDATIONS NO_RECOMMENDATIONS PACKAGE_EXCLUDE"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500758 if d.getVar('BB_CURRENTTASK') == 'populate_sdk_ext':
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500759 # Extensible SDK uses some additional variables
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600760 sdkvars += " SDK_LOCAL_CONF_WHITELIST SDK_LOCAL_CONF_BLACKLIST SDK_INHERIT_BLACKLIST SDK_UPDATE_URL SDK_EXT_TYPE SDK_RECRDEP_TASKS SDK_INCLUDE_PKGDATA SDK_INCLUDE_TOOLCHAIN"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500761 listvars = "SDKIMAGE_FEATURES BAD_RECOMMENDATIONS PACKAGE_EXCLUDE SDK_LOCAL_CONF_WHITELIST SDK_LOCAL_CONF_BLACKLIST SDK_INHERIT_BLACKLIST"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500762 return outputvars(sdkvars, listvars, d)
763
764
765def buildhistory_get_cmdline(d):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500766 argv = d.getVar('BB_CMDLINE', False)
767 if argv:
768 if argv[0].endswith('bin/bitbake'):
769 bincmd = 'bitbake'
770 else:
771 bincmd = argv[0]
772 return '%s %s' % (bincmd, ' '.join(argv[1:]))
773 return ''
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500774
775
776buildhistory_single_commit() {
777 if [ "$3" = "" ] ; then
778 commitopts="${BUILDHISTORY_DIR}/ --allow-empty"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500779 shortlogprefix="No changes: "
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500780 else
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500781 commitopts=""
782 shortlogprefix=""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500783 fi
784 if [ "${BUILDHISTORY_BUILD_FAILURES}" = "0" ] ; then
785 result="succeeded"
786 else
787 result="failed"
788 fi
789 case ${BUILDHISTORY_BUILD_INTERRUPTED} in
790 1)
791 result="$result (interrupted)"
792 ;;
793 2)
794 result="$result (force interrupted)"
795 ;;
796 esac
797 commitmsgfile=`mktemp`
798 cat > $commitmsgfile << END
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500799${shortlogprefix}Build ${BUILDNAME} of ${DISTRO} ${DISTRO_VERSION} for machine ${MACHINE} on $2
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500800
801cmd: $1
802
803result: $result
804
805metadata revisions:
806END
807 cat ${BUILDHISTORY_DIR}/metadata-revs >> $commitmsgfile
808 git commit $commitopts -F $commitmsgfile --author "${BUILDHISTORY_COMMIT_AUTHOR}" > /dev/null
809 rm $commitmsgfile
810}
811
812buildhistory_commit() {
813 if [ ! -d ${BUILDHISTORY_DIR} ] ; then
814 # Code above that creates this dir never executed, so there can't be anything to commit
815 return
816 fi
817
818 # Create a machine-readable list of metadata revisions for each layer
819 cat > ${BUILDHISTORY_DIR}/metadata-revs <<END
820${@buildhistory_get_metadata_revs(d)}
821END
822
823 ( cd ${BUILDHISTORY_DIR}/
824 # Initialise the repo if necessary
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500825 if [ ! -e .git ] ; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500826 git init -q
827 else
Andrew Geissler82c905d2020-04-13 13:39:40 -0500828 git tag -f ${BUILDHISTORY_TAG}-minus-3 ${BUILDHISTORY_TAG}-minus-2 > /dev/null 2>&1 || true
829 git tag -f ${BUILDHISTORY_TAG}-minus-2 ${BUILDHISTORY_TAG}-minus-1 > /dev/null 2>&1 || true
830 git tag -f ${BUILDHISTORY_TAG}-minus-1 > /dev/null 2>&1 || true
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500831 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600832
833 check_git_config
834
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500835 # Check if there are new/changed files to commit (other than metadata-revs)
836 repostatus=`git status --porcelain | grep -v " metadata-revs$"`
837 HOSTNAME=`hostname 2>/dev/null || echo unknown`
838 CMDLINE="${@buildhistory_get_cmdline(d)}"
839 if [ "$repostatus" != "" ] ; then
840 git add -A .
841 # porcelain output looks like "?? packages/foo/bar"
842 # Ensure we commit metadata-revs with the first commit
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500843 buildhistory_single_commit "$CMDLINE" "$HOSTNAME" dummy
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500844 git gc --auto --quiet
845 else
846 buildhistory_single_commit "$CMDLINE" "$HOSTNAME"
847 fi
848 if [ "${BUILDHISTORY_PUSH_REPO}" != "" ] ; then
849 git push -q ${BUILDHISTORY_PUSH_REPO}
850 fi) || true
851}
852
853python buildhistory_eventhandler() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500854 if e.data.getVar('BUILDHISTORY_FEATURES').strip():
855 reset = e.data.getVar("BUILDHISTORY_RESET")
856 olddir = e.data.getVar("BUILDHISTORY_OLD_DIR")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500857 if isinstance(e, bb.event.BuildStarted):
858 if reset:
859 import shutil
860 # Clean up after potentially interrupted build.
861 if os.path.isdir(olddir):
862 shutil.rmtree(olddir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500863 rootdir = e.data.getVar("BUILDHISTORY_DIR")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500864 entries = [ x for x in os.listdir(rootdir) if not x.startswith('.') ]
865 bb.utils.mkdirhier(olddir)
866 for entry in entries:
867 os.rename(os.path.join(rootdir, entry),
868 os.path.join(olddir, entry))
869 elif isinstance(e, bb.event.BuildCompleted):
870 if reset:
871 import shutil
872 shutil.rmtree(olddir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500873 if e.data.getVar("BUILDHISTORY_COMMIT") == "1":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500874 bb.note("Writing buildhistory")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500875 bb.build.exec_func("buildhistory_write_sigs", d)
Brad Bishopf3fd2882019-06-21 08:06:37 -0400876 import time
877 start=time.time()
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500878 localdata = bb.data.createCopy(e.data)
879 localdata.setVar('BUILDHISTORY_BUILD_FAILURES', str(e._failures))
880 interrupted = getattr(e, '_interrupted', 0)
881 localdata.setVar('BUILDHISTORY_BUILD_INTERRUPTED', str(interrupted))
882 bb.build.exec_func("buildhistory_commit", localdata)
Brad Bishopf3fd2882019-06-21 08:06:37 -0400883 stop=time.time()
884 bb.note("Writing buildhistory took: %s seconds" % round(stop-start))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500885 else:
886 bb.note("No commit since BUILDHISTORY_COMMIT != '1'")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500887}
888
889addhandler buildhistory_eventhandler
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500890buildhistory_eventhandler[eventmask] = "bb.event.BuildCompleted bb.event.BuildStarted"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500891
892
893# FIXME this ought to be moved into the fetcher
894def _get_srcrev_values(d):
895 """
896 Return the version strings for the current recipe
897 """
898
899 scms = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500900 fetcher = bb.fetch.Fetch(d.getVar('SRC_URI').split(), d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500901 urldata = fetcher.ud
902 for u in urldata:
903 if urldata[u].method.supports_srcrev():
904 scms.append(u)
905
906 autoinc_templ = 'AUTOINC+'
907 dict_srcrevs = {}
908 dict_tag_srcrevs = {}
909 for scm in scms:
910 ud = urldata[scm]
911 for name in ud.names:
912 try:
913 rev = ud.method.sortable_revision(ud, d, name)
914 except TypeError:
915 # support old bitbake versions
916 rev = ud.method.sortable_revision(scm, ud, d, name)
917 # Clean this up when we next bump bitbake version
918 if type(rev) != str:
919 autoinc, rev = rev
920 elif rev.startswith(autoinc_templ):
921 rev = rev[len(autoinc_templ):]
922 dict_srcrevs[name] = rev
923 if 'tag' in ud.parm:
924 tag = ud.parm['tag'];
925 key = name+'_'+tag
926 dict_tag_srcrevs[key] = rev
927 return (dict_srcrevs, dict_tag_srcrevs)
928
929do_fetch[postfuncs] += "write_srcrev"
930do_fetch[vardepsexclude] += "write_srcrev"
931python write_srcrev() {
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500932 write_latest_srcrev(d, d.getVar('BUILDHISTORY_DIR_PACKAGE'))
933}
934
935def write_latest_srcrev(d, pkghistdir):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500936 srcrevfile = os.path.join(pkghistdir, 'latest_srcrev')
937
938 srcrevs, tag_srcrevs = _get_srcrev_values(d)
939 if srcrevs:
940 if not os.path.exists(pkghistdir):
941 bb.utils.mkdirhier(pkghistdir)
942 old_tag_srcrevs = {}
943 if os.path.exists(srcrevfile):
944 with open(srcrevfile) as f:
945 for line in f:
946 if line.startswith('# tag_'):
947 key, value = line.split("=", 1)
948 key = key.replace('# tag_', '').strip()
949 value = value.replace('"', '').strip()
950 old_tag_srcrevs[key] = value
951 with open(srcrevfile, 'w') as f:
952 orig_srcrev = d.getVar('SRCREV', False) or 'INVALID'
953 if orig_srcrev != 'INVALID':
954 f.write('# SRCREV = "%s"\n' % orig_srcrev)
955 if len(srcrevs) > 1:
Brad Bishop19323692019-04-05 15:28:33 -0400956 for name, srcrev in sorted(srcrevs.items()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500957 orig_srcrev = d.getVar('SRCREV_%s' % name, False)
958 if orig_srcrev:
959 f.write('# SRCREV_%s = "%s"\n' % (name, orig_srcrev))
960 f.write('SRCREV_%s = "%s"\n' % (name, srcrev))
961 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500962 f.write('SRCREV = "%s"\n' % next(iter(srcrevs.values())))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500963 if len(tag_srcrevs) > 0:
Brad Bishop19323692019-04-05 15:28:33 -0400964 for name, srcrev in sorted(tag_srcrevs.items()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500965 f.write('# tag_%s = "%s"\n' % (name, srcrev))
966 if name in old_tag_srcrevs and old_tag_srcrevs[name] != srcrev:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500967 pkg = d.getVar('PN')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500968 bb.warn("Revision for tag %s in package %s was changed since last build (from %s to %s)" % (name, pkg, old_tag_srcrevs[name], srcrev))
969
970 else:
971 if os.path.exists(srcrevfile):
972 os.remove(srcrevfile)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500973
974do_testimage[postfuncs] += "write_ptest_result"
975do_testimage[vardepsexclude] += "write_ptest_result"
976
977python write_ptest_result() {
978 write_latest_ptest_result(d, d.getVar('BUILDHISTORY_DIR'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500979}
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500980
981def write_latest_ptest_result(d, histdir):
982 import glob
983 import subprocess
984 test_log_dir = d.getVar('TEST_LOG_DIR')
985 input_ptest = os.path.join(test_log_dir, 'ptest_log')
986 output_ptest = os.path.join(histdir, 'ptest')
987 if os.path.exists(input_ptest):
988 try:
989 # Lock it avoid race issue
990 lock = bb.utils.lockfile(output_ptest + "/ptest.lock")
991 bb.utils.mkdirhier(output_ptest)
992 oe.path.copytree(input_ptest, output_ptest)
993 # Sort test result
994 for result in glob.glob('%s/pass.fail.*' % output_ptest):
995 bb.debug(1, 'Processing %s' % result)
996 cmd = ['sort', result, '-o', result]
997 bb.debug(1, 'Running %s' % cmd)
998 ret = subprocess.call(cmd)
999 if ret != 0:
1000 bb.error('Failed to run %s!' % cmd)
1001 finally:
1002 bb.utils.unlockfile(lock)