blob: 796f68cf8f60bfbd8a96cf35a917e41a25786924 [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 ?= ""
43
44SSTATEPOSTINSTFUNCS_append = " buildhistory_emit_pkghistory"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050045# We want to avoid influencing the signatures of sstate tasks - first the function itself:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046sstate_install[vardepsexclude] += "buildhistory_emit_pkghistory"
47# then the value added to SSTATEPOSTINSTFUNCS:
48SSTATEPOSTINSTFUNCS[vardepvalueexclude] .= "| buildhistory_emit_pkghistory"
49
Brad Bishop6e60e8b2018-02-01 10:27:11 -050050# Similarly for our function that gets the output signatures
51SSTATEPOSTUNPACKFUNCS_append = " buildhistory_emit_outputsigs"
52sstate_installpkgdir[vardepsexclude] += "buildhistory_emit_outputsigs"
53SSTATEPOSTUNPACKFUNCS[vardepvalueexclude] .= "| buildhistory_emit_outputsigs"
54
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050055# All items excepts those listed here will be removed from a recipe's
56# build history directory by buildhistory_emit_pkghistory(). This is
57# necessary because some of these items (package directories, files that
58# we no longer emit) might be obsolete.
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059#
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050060# When extending build history, derive your class from buildhistory.bbclass
61# and extend this list here with the additional files created by the derived
62# class.
63BUILDHISTORY_PRESERVE = "latest latest_srcrev"
64
Patrick Williamsc0f7c042017-02-23 20:41:17 -060065PATCH_GIT_USER_EMAIL ?= "buildhistory@oe"
66PATCH_GIT_USER_NAME ?= "OpenEmbedded"
67
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050068#
69# Write out metadata about this package for comparison when writing future packages
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070#
71python buildhistory_emit_pkghistory() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050072 if not d.getVar('BB_CURRENTTASK') in ['packagedata', 'packagedata_setscene']:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 return 0
74
Brad Bishop6e60e8b2018-02-01 10:27:11 -050075 if not "package" in (d.getVar('BUILDHISTORY_FEATURES') or "").split():
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 return 0
77
78 import re
79 import json
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080080 import shlex
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081 import errno
82
Brad Bishop6e60e8b2018-02-01 10:27:11 -050083 pkghistdir = d.getVar('BUILDHISTORY_DIR_PACKAGE')
84 oldpkghistdir = d.getVar('BUILDHISTORY_OLD_DIR_PACKAGE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085
86 class RecipeInfo:
87 def __init__(self, name):
88 self.name = name
89 self.pe = "0"
90 self.pv = "0"
91 self.pr = "r0"
92 self.depends = ""
93 self.packages = ""
94 self.srcrev = ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -050095 self.layer = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096
97
98 class PackageInfo:
99 def __init__(self, name):
100 self.name = name
101 self.pe = "0"
102 self.pv = "0"
103 self.pr = "r0"
104 # pkg/pkge/pkgv/pkgr should be empty because we want to be able to default them
105 self.pkg = ""
106 self.pkge = ""
107 self.pkgv = ""
108 self.pkgr = ""
109 self.size = 0
110 self.depends = ""
111 self.rprovides = ""
112 self.rdepends = ""
113 self.rrecommends = ""
114 self.rsuggests = ""
115 self.rreplaces = ""
116 self.rconflicts = ""
117 self.files = ""
118 self.filelist = ""
119 # Variables that need to be written to their own separate file
120 self.filevars = dict.fromkeys(['pkg_preinst', 'pkg_postinst', 'pkg_prerm', 'pkg_postrm'])
121
122 # Should check PACKAGES here to see if anything removed
123
124 def readPackageInfo(pkg, histfile):
125 pkginfo = PackageInfo(pkg)
126 with open(histfile, "r") as f:
127 for line in f:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500128 lns = line.split('=', 1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500129 name = lns[0].strip()
130 value = lns[1].strip(" \t\r\n").strip('"')
131 if name == "PE":
132 pkginfo.pe = value
133 elif name == "PV":
134 pkginfo.pv = value
135 elif name == "PR":
136 pkginfo.pr = value
137 elif name == "PKG":
138 pkginfo.pkg = value
139 elif name == "PKGE":
140 pkginfo.pkge = value
141 elif name == "PKGV":
142 pkginfo.pkgv = value
143 elif name == "PKGR":
144 pkginfo.pkgr = value
145 elif name == "RPROVIDES":
146 pkginfo.rprovides = value
147 elif name == "RDEPENDS":
148 pkginfo.rdepends = value
149 elif name == "RRECOMMENDS":
150 pkginfo.rrecommends = value
151 elif name == "RSUGGESTS":
152 pkginfo.rsuggests = value
153 elif name == "RREPLACES":
154 pkginfo.rreplaces = value
155 elif name == "RCONFLICTS":
156 pkginfo.rconflicts = value
157 elif name == "PKGSIZE":
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600158 pkginfo.size = int(value)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500159 elif name == "FILES":
160 pkginfo.files = value
161 elif name == "FILELIST":
162 pkginfo.filelist = value
163 # Apply defaults
164 if not pkginfo.pkg:
165 pkginfo.pkg = pkginfo.name
166 if not pkginfo.pkge:
167 pkginfo.pkge = pkginfo.pe
168 if not pkginfo.pkgv:
169 pkginfo.pkgv = pkginfo.pv
170 if not pkginfo.pkgr:
171 pkginfo.pkgr = pkginfo.pr
172 return pkginfo
173
174 def getlastpkgversion(pkg):
175 try:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500176 histfile = os.path.join(oldpkghistdir, pkg, "latest")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500177 return readPackageInfo(pkg, histfile)
178 except EnvironmentError:
179 return None
180
181 def sortpkglist(string):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500182 pkgiter = re.finditer(r'[a-zA-Z0-9.+-]+( \([><=]+[^)]+\))?', string, 0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500183 pkglist = [p.group(0) for p in pkgiter]
184 pkglist.sort()
185 return ' '.join(pkglist)
186
187 def sortlist(string):
188 items = string.split(' ')
189 items.sort()
190 return ' '.join(items)
191
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500192 pn = d.getVar('PN')
193 pe = d.getVar('PE') or "0"
194 pv = d.getVar('PV')
195 pr = d.getVar('PR')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500196 layer = bb.utils.get_file_layer(d.getVar('FILE'), d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500197
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500198 pkgdata_dir = d.getVar('PKGDATA_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500199 packages = ""
200 try:
201 with open(os.path.join(pkgdata_dir, pn)) as f:
202 for line in f.readlines():
203 if line.startswith('PACKAGES: '):
204 packages = oe.utils.squashspaces(line.split(': ', 1)[1])
205 break
206 except IOError as e:
207 if e.errno == errno.ENOENT:
208 # Probably a -cross recipe, just ignore
209 return 0
210 else:
211 raise
212
213 packagelist = packages.split()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500214 preserve = d.getVar('BUILDHISTORY_PRESERVE').split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500215 if not os.path.exists(pkghistdir):
216 bb.utils.mkdirhier(pkghistdir)
217 else:
218 # Remove files for packages that no longer exist
219 for item in os.listdir(pkghistdir):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500220 if item not in preserve:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500221 if item not in packagelist:
222 itempath = os.path.join(pkghistdir, item)
223 if os.path.isdir(itempath):
224 for subfile in os.listdir(itempath):
225 os.unlink(os.path.join(itempath, subfile))
226 os.rmdir(itempath)
227 else:
228 os.unlink(itempath)
229
230 rcpinfo = RecipeInfo(pn)
231 rcpinfo.pe = pe
232 rcpinfo.pv = pv
233 rcpinfo.pr = pr
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500234 rcpinfo.depends = sortlist(oe.utils.squashspaces(d.getVar('DEPENDS') or ""))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500235 rcpinfo.packages = packages
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500236 rcpinfo.layer = layer
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500237 write_recipehistory(rcpinfo, d)
238
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500239 pkgdest = d.getVar('PKGDEST')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500240 for pkg in packagelist:
241 pkgdata = {}
242 with open(os.path.join(pkgdata_dir, 'runtime', pkg)) as f:
243 for line in f.readlines():
244 item = line.rstrip('\n').split(': ', 1)
245 key = item[0]
246 if key.endswith('_' + pkg):
247 key = key[:-len(pkg)-1]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800248 pkgdata[key] = item[1].encode('latin-1').decode('unicode_escape')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500249
250 pkge = pkgdata.get('PKGE', '0')
251 pkgv = pkgdata['PKGV']
252 pkgr = pkgdata['PKGR']
253 #
254 # Find out what the last version was
255 # Make sure the version did not decrease
256 #
257 lastversion = getlastpkgversion(pkg)
258 if lastversion:
259 last_pkge = lastversion.pkge
260 last_pkgv = lastversion.pkgv
261 last_pkgr = lastversion.pkgr
262 r = bb.utils.vercmp((pkge, pkgv, pkgr), (last_pkge, last_pkgv, last_pkgr))
263 if r < 0:
264 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)
265 package_qa_handle_error("version-going-backwards", msg, d)
266
267 pkginfo = PackageInfo(pkg)
268 # Apparently the version can be different on a per-package basis (see Python)
269 pkginfo.pe = pkgdata.get('PE', '0')
270 pkginfo.pv = pkgdata['PV']
271 pkginfo.pr = pkgdata['PR']
272 pkginfo.pkg = pkgdata['PKG']
273 pkginfo.pkge = pkge
274 pkginfo.pkgv = pkgv
275 pkginfo.pkgr = pkgr
276 pkginfo.rprovides = sortpkglist(oe.utils.squashspaces(pkgdata.get('RPROVIDES', "")))
277 pkginfo.rdepends = sortpkglist(oe.utils.squashspaces(pkgdata.get('RDEPENDS', "")))
278 pkginfo.rrecommends = sortpkglist(oe.utils.squashspaces(pkgdata.get('RRECOMMENDS', "")))
279 pkginfo.rsuggests = sortpkglist(oe.utils.squashspaces(pkgdata.get('RSUGGESTS', "")))
280 pkginfo.rreplaces = sortpkglist(oe.utils.squashspaces(pkgdata.get('RREPLACES', "")))
281 pkginfo.rconflicts = sortpkglist(oe.utils.squashspaces(pkgdata.get('RCONFLICTS', "")))
282 pkginfo.files = oe.utils.squashspaces(pkgdata.get('FILES', ""))
283 for filevar in pkginfo.filevars:
284 pkginfo.filevars[filevar] = pkgdata.get(filevar, "")
285
286 # Gather information about packaged files
287 val = pkgdata.get('FILES_INFO', '')
288 dictval = json.loads(val)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600289 filelist = list(dictval.keys())
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500290 filelist.sort()
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800291 pkginfo.filelist = " ".join([shlex.quote(x) for x in filelist])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500292
293 pkginfo.size = int(pkgdata['PKGSIZE'])
294
295 write_pkghistory(pkginfo, d)
296
297 # Create files-in-<package-name>.txt files containing a list of files of each recipe's package
298 bb.build.exec_func("buildhistory_list_pkg_files", d)
299}
300
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500301python buildhistory_emit_outputsigs() {
302 if not "task" in (d.getVar('BUILDHISTORY_FEATURES') or "").split():
303 return
304
305 import hashlib
306
307 taskoutdir = os.path.join(d.getVar('BUILDHISTORY_DIR'), 'task', 'output')
308 bb.utils.mkdirhier(taskoutdir)
309 currenttask = d.getVar('BB_CURRENTTASK')
310 pn = d.getVar('PN')
311 taskfile = os.path.join(taskoutdir, '%s.%s' % (pn, currenttask))
312
313 cwd = os.getcwd()
314 filesigs = {}
315 for root, _, files in os.walk(cwd):
316 for fname in files:
317 if fname == 'fixmepath':
318 continue
319 fullpath = os.path.join(root, fname)
320 try:
321 if os.path.islink(fullpath):
322 sha256 = hashlib.sha256(os.readlink(fullpath).encode('utf-8')).hexdigest()
323 elif os.path.isfile(fullpath):
324 sha256 = bb.utils.sha256_file(fullpath)
325 else:
326 continue
327 except OSError:
328 bb.warn('buildhistory: unable to read %s to get output signature' % fullpath)
329 continue
330 filesigs[os.path.relpath(fullpath, cwd)] = sha256
331 with open(taskfile, 'w') as f:
332 for fpath, fsig in sorted(filesigs.items(), key=lambda item: item[0]):
333 f.write('%s %s\n' % (fpath, fsig))
334}
335
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500336
337def write_recipehistory(rcpinfo, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500338 bb.debug(2, "Writing recipe history")
339
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500340 pkghistdir = d.getVar('BUILDHISTORY_DIR_PACKAGE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500341
342 infofile = os.path.join(pkghistdir, "latest")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600343 with open(infofile, "w") as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500344 if rcpinfo.pe != "0":
345 f.write(u"PE = %s\n" % rcpinfo.pe)
346 f.write(u"PV = %s\n" % rcpinfo.pv)
347 f.write(u"PR = %s\n" % rcpinfo.pr)
348 f.write(u"DEPENDS = %s\n" % rcpinfo.depends)
349 f.write(u"PACKAGES = %s\n" % rcpinfo.packages)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500350 f.write(u"LAYER = %s\n" % rcpinfo.layer)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500351
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500352 write_latest_srcrev(d, pkghistdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500353
354def write_pkghistory(pkginfo, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500355 bb.debug(2, "Writing package history for package %s" % pkginfo.name)
356
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500357 pkghistdir = d.getVar('BUILDHISTORY_DIR_PACKAGE')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500358
359 pkgpath = os.path.join(pkghistdir, pkginfo.name)
360 if not os.path.exists(pkgpath):
361 bb.utils.mkdirhier(pkgpath)
362
363 infofile = os.path.join(pkgpath, "latest")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600364 with open(infofile, "w") as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500365 if pkginfo.pe != "0":
366 f.write(u"PE = %s\n" % pkginfo.pe)
367 f.write(u"PV = %s\n" % pkginfo.pv)
368 f.write(u"PR = %s\n" % pkginfo.pr)
369
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600370 if pkginfo.pkg != pkginfo.name:
371 f.write(u"PKG = %s\n" % pkginfo.pkg)
372 if pkginfo.pkge != pkginfo.pe:
373 f.write(u"PKGE = %s\n" % pkginfo.pkge)
374 if pkginfo.pkgv != pkginfo.pv:
375 f.write(u"PKGV = %s\n" % pkginfo.pkgv)
376 if pkginfo.pkgr != pkginfo.pr:
377 f.write(u"PKGR = %s\n" % pkginfo.pkgr)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500378 f.write(u"RPROVIDES = %s\n" % pkginfo.rprovides)
379 f.write(u"RDEPENDS = %s\n" % pkginfo.rdepends)
380 f.write(u"RRECOMMENDS = %s\n" % pkginfo.rrecommends)
381 if pkginfo.rsuggests:
382 f.write(u"RSUGGESTS = %s\n" % pkginfo.rsuggests)
383 if pkginfo.rreplaces:
384 f.write(u"RREPLACES = %s\n" % pkginfo.rreplaces)
385 if pkginfo.rconflicts:
386 f.write(u"RCONFLICTS = %s\n" % pkginfo.rconflicts)
387 f.write(u"PKGSIZE = %d\n" % pkginfo.size)
388 f.write(u"FILES = %s\n" % pkginfo.files)
389 f.write(u"FILELIST = %s\n" % pkginfo.filelist)
390
391 for filevar in pkginfo.filevars:
392 filevarpath = os.path.join(pkgpath, "latest.%s" % filevar)
393 val = pkginfo.filevars[filevar]
394 if val:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600395 with open(filevarpath, "w") as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500396 f.write(val)
397 else:
398 if os.path.exists(filevarpath):
399 os.unlink(filevarpath)
400
401#
402# rootfs_type can be: image, sdk_target, sdk_host
403#
404def buildhistory_list_installed(d, rootfs_type="image"):
405 from oe.rootfs import image_list_installed_packages
406 from oe.sdk import sdk_list_installed_packages
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500407 from oe.utils import format_pkg_list
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500408
409 process_list = [('file', 'bh_installed_pkgs.txt'),\
410 ('deps', 'bh_installed_pkgs_deps.txt')]
411
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500412 if rootfs_type == "image":
413 pkgs = image_list_installed_packages(d)
414 else:
415 pkgs = sdk_list_installed_packages(d, rootfs_type == "sdk_target")
416
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500417 for output_type, output_file in process_list:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500418 output_file_full = os.path.join(d.getVar('WORKDIR'), output_file)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500419
420 with open(output_file_full, 'w') as output:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500421 output.write(format_pkg_list(pkgs, output_type))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500422
423python buildhistory_list_installed_image() {
424 buildhistory_list_installed(d)
425}
426
427python buildhistory_list_installed_sdk_target() {
428 buildhistory_list_installed(d, "sdk_target")
429}
430
431python buildhistory_list_installed_sdk_host() {
432 buildhistory_list_installed(d, "sdk_host")
433}
434
435buildhistory_get_installed() {
436 mkdir -p $1
437
438 # Get list of installed packages
439 pkgcache="$1/installed-packages.tmp"
440 cat ${WORKDIR}/bh_installed_pkgs.txt | sort > $pkgcache && rm ${WORKDIR}/bh_installed_pkgs.txt
441
442 cat $pkgcache | awk '{ print $1 }' > $1/installed-package-names.txt
443 if [ -s $pkgcache ] ; then
444 cat $pkgcache | awk '{ print $2 }' | xargs -n1 basename > $1/installed-packages.txt
445 else
446 printf "" > $1/installed-packages.txt
447 fi
448
449 # Produce dependency graph
450 # First, quote each name to handle characters that cause issues for dot
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500451 sed 's:\([^| ]*\):"\1":g' ${WORKDIR}/bh_installed_pkgs_deps.txt > $1/depends.tmp &&
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500452 rm ${WORKDIR}/bh_installed_pkgs_deps.txt
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500453 # Remove lines with rpmlib(...) and config(...) dependencies, change the
454 # delimiter from pipe to "->", set the style for recommend lines and
455 # turn versioned dependencies into edge labels.
456 sed -i -e '/rpmlib(/d' \
457 -e '/config(/d' \
458 -e 's:|: -> :' \
459 -e 's:"\[REC\]":[style=dotted]:' \
460 -e 's:"\([<>=]\+\)" "\([^"]*\)":[label="\1 \2"]:' \
461 $1/depends.tmp
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500462 # Add header, sorted and de-duped contents and footer and then delete the temp file
463 printf "digraph depends {\n node [shape=plaintext]\n" > $1/depends.dot
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500464 cat $1/depends.tmp | sort -u >> $1/depends.dot
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500465 echo "}" >> $1/depends.dot
466 rm $1/depends.tmp
467
468 # Produce installed package sizes list
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500469 oe-pkgdata-util -p ${PKGDATA_DIR} read-value "PKGSIZE" -n -f $pkgcache > $1/installed-package-sizes.tmp
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500470 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 -0500471 rm $1/installed-package-sizes.tmp
472
473 # We're now done with the cache, delete it
474 rm $pkgcache
475
476 if [ "$2" != "sdk" ] ; then
477 # Produce some cut-down graphs (for readability)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500478 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 -0500479 grep -v libc6 $1/depends-nokernel.dot | grep -v libgcc > $1/depends-nokernel-nolibc.dot
480 grep -v update- $1/depends-nokernel-nolibc.dot > $1/depends-nokernel-nolibc-noupdate.dot
481 grep -v kernel-module $1/depends-nokernel-nolibc-noupdate.dot > $1/depends-nokernel-nolibc-noupdate-nomodules.dot
482 fi
483
484 # add complementary package information
485 if [ -e ${WORKDIR}/complementary_pkgs.txt ]; then
486 cp ${WORKDIR}/complementary_pkgs.txt $1
487 fi
488}
489
490buildhistory_get_image_installed() {
491 # Anything requiring the use of the packaging system should be done in here
492 # in case the packaging files are going to be removed for this image
493
494 if [ "${@bb.utils.contains('BUILDHISTORY_FEATURES', 'image', '1', '0', d)}" = "0" ] ; then
495 return
496 fi
497
498 buildhistory_get_installed ${BUILDHISTORY_DIR_IMAGE}
499}
500
501buildhistory_get_sdk_installed() {
502 # Anything requiring the use of the packaging system should be done in here
503 # in case the packaging files are going to be removed for this SDK
504
505 if [ "${@bb.utils.contains('BUILDHISTORY_FEATURES', 'sdk', '1', '0', d)}" = "0" ] ; then
506 return
507 fi
508
509 buildhistory_get_installed ${BUILDHISTORY_DIR_SDK}/$1 sdk
510}
511
512buildhistory_get_sdk_installed_host() {
513 buildhistory_get_sdk_installed host
514}
515
516buildhistory_get_sdk_installed_target() {
517 buildhistory_get_sdk_installed target
518}
519
520buildhistory_list_files() {
521 # List the files in the specified directory, but exclude date/time etc.
Brad Bishop19323692019-04-05 15:28:33 -0400522 # This is somewhat messy, but handles where the size is not printed for device files under pseudo
523 ( cd $1
524 find_cmd='find . ! -path . -printf "%M %-10u %-10g %10s %p -> %l\n"'
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500525 if [ "$3" = "fakeroot" ] ; then
Brad Bishop19323692019-04-05 15:28:33 -0400526 eval ${FAKEROOTENV} ${FAKEROOTCMD} $find_cmd
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500527 else
Brad Bishop19323692019-04-05 15:28:33 -0400528 eval $find_cmd
529 fi | sort -k5 | sed 's/ * -> $//' > $2 )
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500530}
531
532buildhistory_list_pkg_files() {
533 # Create individual files-in-package for each recipe's package
534 for pkgdir in $(find ${PKGDEST}/* -maxdepth 0 -type d); do
535 pkgname=$(basename $pkgdir)
536 outfolder="${BUILDHISTORY_DIR_PACKAGE}/$pkgname"
537 outfile="$outfolder/files-in-package.txt"
538 # Make sure the output folder exists so we can create the file
539 if [ ! -d $outfolder ] ; then
540 bbdebug 2 "Folder $outfolder does not exist, file $outfile not created"
541 continue
542 fi
543 buildhistory_list_files $pkgdir $outfile fakeroot
544 done
545}
546
547buildhistory_get_imageinfo() {
548 if [ "${@bb.utils.contains('BUILDHISTORY_FEATURES', 'image', '1', '0', d)}" = "0" ] ; then
549 return
550 fi
551
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500552 mkdir -p ${BUILDHISTORY_DIR_IMAGE}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500553 buildhistory_list_files ${IMAGE_ROOTFS} ${BUILDHISTORY_DIR_IMAGE}/files-in-image.txt
554
555 # Collect files requested in BUILDHISTORY_IMAGE_FILES
556 rm -rf ${BUILDHISTORY_DIR_IMAGE}/image-files
557 for f in ${BUILDHISTORY_IMAGE_FILES}; do
558 if [ -f ${IMAGE_ROOTFS}/$f ] ; then
559 mkdir -p ${BUILDHISTORY_DIR_IMAGE}/image-files/`dirname $f`
560 cp ${IMAGE_ROOTFS}/$f ${BUILDHISTORY_DIR_IMAGE}/image-files/$f
561 fi
562 done
563
564 # Record some machine-readable meta-information about the image
565 printf "" > ${BUILDHISTORY_DIR_IMAGE}/image-info.txt
566 cat >> ${BUILDHISTORY_DIR_IMAGE}/image-info.txt <<END
567${@buildhistory_get_imagevars(d)}
568END
569 imagesize=`du -ks ${IMAGE_ROOTFS} | awk '{ print $1 }'`
570 echo "IMAGESIZE = $imagesize" >> ${BUILDHISTORY_DIR_IMAGE}/image-info.txt
571
572 # Add some configuration information
573 echo "${MACHINE}: ${IMAGE_BASENAME} configured for ${DISTRO} ${DISTRO_VERSION}" > ${BUILDHISTORY_DIR_IMAGE}/build-id.txt
574
575 cat >> ${BUILDHISTORY_DIR_IMAGE}/build-id.txt <<END
576${@buildhistory_get_build_id(d)}
577END
578}
579
580buildhistory_get_sdkinfo() {
581 if [ "${@bb.utils.contains('BUILDHISTORY_FEATURES', 'sdk', '1', '0', d)}" = "0" ] ; then
582 return
583 fi
584
585 buildhistory_list_files ${SDK_OUTPUT} ${BUILDHISTORY_DIR_SDK}/files-in-sdk.txt
586
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500587 # Collect files requested in BUILDHISTORY_SDK_FILES
588 rm -rf ${BUILDHISTORY_DIR_SDK}/sdk-files
589 for f in ${BUILDHISTORY_SDK_FILES}; do
590 if [ -f ${SDK_OUTPUT}/${SDKPATH}/$f ] ; then
591 mkdir -p ${BUILDHISTORY_DIR_SDK}/sdk-files/`dirname $f`
592 cp ${SDK_OUTPUT}/${SDKPATH}/$f ${BUILDHISTORY_DIR_SDK}/sdk-files/$f
593 fi
594 done
595
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500596 # Record some machine-readable meta-information about the SDK
597 printf "" > ${BUILDHISTORY_DIR_SDK}/sdk-info.txt
598 cat >> ${BUILDHISTORY_DIR_SDK}/sdk-info.txt <<END
599${@buildhistory_get_sdkvars(d)}
600END
601 sdksize=`du -ks ${SDK_OUTPUT} | awk '{ print $1 }'`
602 echo "SDKSIZE = $sdksize" >> ${BUILDHISTORY_DIR_SDK}/sdk-info.txt
603}
604
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500605python buildhistory_get_extra_sdkinfo() {
606 import operator
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500607 from oe.sdk import get_extra_sdkinfo
608
609 sstate_dir = d.expand('${SDK_OUTPUT}/${SDKPATH}/sstate-cache')
610 extra_info = get_extra_sdkinfo(sstate_dir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500611
612 if d.getVar('BB_CURRENTTASK') == 'populate_sdk_ext' and \
613 "sdk" in (d.getVar('BUILDHISTORY_FEATURES') or "").split():
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500614 with open(d.expand('${BUILDHISTORY_DIR_SDK}/sstate-package-sizes.txt'), 'w') as f:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500615 filesizes_sorted = sorted(extra_info['filesizes'].items(), key=operator.itemgetter(1, 0), reverse=True)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500616 for fn, size in filesizes_sorted:
617 f.write('%10d KiB %s\n' % (size, fn))
618 with open(d.expand('${BUILDHISTORY_DIR_SDK}/sstate-task-sizes.txt'), 'w') as f:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500619 tasksizes_sorted = sorted(extra_info['tasksizes'].items(), key=operator.itemgetter(1, 0), reverse=True)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500620 for task, size in tasksizes_sorted:
621 f.write('%10d KiB %s\n' % (size, task))
622}
623
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500624# By using ROOTFS_POSTUNINSTALL_COMMAND we get in after uninstallation of
625# unneeded packages but before the removal of packaging files
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500626ROOTFS_POSTUNINSTALL_COMMAND += "buildhistory_list_installed_image ;"
627ROOTFS_POSTUNINSTALL_COMMAND += "buildhistory_get_image_installed ;"
628ROOTFS_POSTUNINSTALL_COMMAND[vardepvalueexclude] .= "| buildhistory_list_installed_image ;| buildhistory_get_image_installed ;"
629ROOTFS_POSTUNINSTALL_COMMAND[vardepsexclude] += "buildhistory_list_installed_image buildhistory_get_image_installed"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500630
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500631IMAGE_POSTPROCESS_COMMAND += "buildhistory_get_imageinfo ;"
632IMAGE_POSTPROCESS_COMMAND[vardepvalueexclude] .= "| buildhistory_get_imageinfo ;"
633IMAGE_POSTPROCESS_COMMAND[vardepsexclude] += "buildhistory_get_imageinfo"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500634
635# We want these to be the last run so that we get called after complementary package installation
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500636POPULATE_SDK_POST_TARGET_COMMAND_append = " buildhistory_list_installed_sdk_target;"
637POPULATE_SDK_POST_TARGET_COMMAND_append = " buildhistory_get_sdk_installed_target;"
638POPULATE_SDK_POST_TARGET_COMMAND[vardepvalueexclude] .= "| buildhistory_list_installed_sdk_target;| buildhistory_get_sdk_installed_target;"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500639
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500640POPULATE_SDK_POST_HOST_COMMAND_append = " buildhistory_list_installed_sdk_host;"
641POPULATE_SDK_POST_HOST_COMMAND_append = " buildhistory_get_sdk_installed_host;"
642POPULATE_SDK_POST_HOST_COMMAND[vardepvalueexclude] .= "| buildhistory_list_installed_sdk_host;| buildhistory_get_sdk_installed_host;"
643
644SDK_POSTPROCESS_COMMAND_append = " buildhistory_get_sdkinfo ; buildhistory_get_extra_sdkinfo; "
645SDK_POSTPROCESS_COMMAND[vardepvalueexclude] .= "| buildhistory_get_sdkinfo ; buildhistory_get_extra_sdkinfo; "
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500646
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500647python buildhistory_write_sigs() {
648 if not "task" in (d.getVar('BUILDHISTORY_FEATURES') or "").split():
649 return
650
651 # Create sigs file
652 if hasattr(bb.parse.siggen, 'dump_siglist'):
653 taskoutdir = os.path.join(d.getVar('BUILDHISTORY_DIR'), 'task')
654 bb.utils.mkdirhier(taskoutdir)
655 bb.parse.siggen.dump_siglist(os.path.join(taskoutdir, 'tasksigs.txt'))
656}
657
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500658def buildhistory_get_build_id(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500659 if d.getVar('BB_WORKERCONTEXT') != '1':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500660 return ""
661 localdata = bb.data.createCopy(d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500662 statuslines = []
663 for func in oe.data.typed_value('BUILDCFG_FUNCS', localdata):
664 g = globals()
665 if func not in g:
666 bb.warn("Build configuration function '%s' does not exist" % func)
667 else:
668 flines = g[func](localdata)
669 if flines:
670 statuslines.extend(flines)
671
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500672 statusheader = d.getVar('BUILDCFG_HEADER')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500673 return('\n%s\n%s\n' % (statusheader, '\n'.join(statuslines)))
674
Brad Bishop19323692019-04-05 15:28:33 -0400675def buildhistory_get_modified(path):
676 # copied from get_layer_git_status() in image-buildinfo.bbclass
677 import subprocess
678 try:
679 subprocess.check_output("""cd %s; export PSEUDO_UNLOAD=1; set -e;
680 git diff --quiet --no-ext-diff
681 git diff --quiet --no-ext-diff --cached""" % path,
682 shell=True,
683 stderr=subprocess.STDOUT)
684 return ""
685 except subprocess.CalledProcessError as ex:
686 # Silently treat errors as "modified", without checking for the
687 # (expected) return code 1 in a modified git repo. For example, we get
688 # output and a 129 return code when a layer isn't a git repo at all.
689 return " -- modified"
690
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500691def buildhistory_get_metadata_revs(d):
692 # 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 -0500693 layers = (d.getVar("BBLAYERS") or "").split()
Brad Bishop19323692019-04-05 15:28:33 -0400694 medadata_revs = ["%-17s = %s:%s%s" % (os.path.basename(i), \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500695 base_get_metadata_git_branch(i, None).strip(), \
Brad Bishop19323692019-04-05 15:28:33 -0400696 base_get_metadata_git_revision(i, None), \
697 buildhistory_get_modified(i)) \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500698 for i in layers]
699 return '\n'.join(medadata_revs)
700
701def outputvars(vars, listvars, d):
702 vars = vars.split()
703 listvars = listvars.split()
704 ret = ""
705 for var in vars:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500706 value = d.getVar(var) or ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500707 if var in listvars:
708 # Squash out spaces
709 value = oe.utils.squashspaces(value)
710 ret += "%s = %s\n" % (var, value)
711 return ret.rstrip('\n')
712
713def buildhistory_get_imagevars(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500714 if d.getVar('BB_WORKERCONTEXT') != '1':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500715 return ""
716 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"
717 listvars = "USER_CLASSES IMAGE_CLASSES IMAGE_FEATURES IMAGE_LINGUAS IMAGE_INSTALL BAD_RECOMMENDATIONS PACKAGE_EXCLUDE"
718 return outputvars(imagevars, listvars, d)
719
720def buildhistory_get_sdkvars(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500721 if d.getVar('BB_WORKERCONTEXT') != '1':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500722 return ""
723 sdkvars = "DISTRO DISTRO_VERSION SDK_NAME SDK_VERSION SDKMACHINE SDKIMAGE_FEATURES BAD_RECOMMENDATIONS NO_RECOMMENDATIONS PACKAGE_EXCLUDE"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500724 if d.getVar('BB_CURRENTTASK') == 'populate_sdk_ext':
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500725 # Extensible SDK uses some additional variables
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600726 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 -0500727 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 -0500728 return outputvars(sdkvars, listvars, d)
729
730
731def buildhistory_get_cmdline(d):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500732 argv = d.getVar('BB_CMDLINE', False)
733 if argv:
734 if argv[0].endswith('bin/bitbake'):
735 bincmd = 'bitbake'
736 else:
737 bincmd = argv[0]
738 return '%s %s' % (bincmd, ' '.join(argv[1:]))
739 return ''
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500740
741
742buildhistory_single_commit() {
743 if [ "$3" = "" ] ; then
744 commitopts="${BUILDHISTORY_DIR}/ --allow-empty"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500745 shortlogprefix="No changes: "
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500746 else
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500747 commitopts=""
748 shortlogprefix=""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500749 fi
750 if [ "${BUILDHISTORY_BUILD_FAILURES}" = "0" ] ; then
751 result="succeeded"
752 else
753 result="failed"
754 fi
755 case ${BUILDHISTORY_BUILD_INTERRUPTED} in
756 1)
757 result="$result (interrupted)"
758 ;;
759 2)
760 result="$result (force interrupted)"
761 ;;
762 esac
763 commitmsgfile=`mktemp`
764 cat > $commitmsgfile << END
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500765${shortlogprefix}Build ${BUILDNAME} of ${DISTRO} ${DISTRO_VERSION} for machine ${MACHINE} on $2
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500766
767cmd: $1
768
769result: $result
770
771metadata revisions:
772END
773 cat ${BUILDHISTORY_DIR}/metadata-revs >> $commitmsgfile
774 git commit $commitopts -F $commitmsgfile --author "${BUILDHISTORY_COMMIT_AUTHOR}" > /dev/null
775 rm $commitmsgfile
776}
777
778buildhistory_commit() {
779 if [ ! -d ${BUILDHISTORY_DIR} ] ; then
780 # Code above that creates this dir never executed, so there can't be anything to commit
781 return
782 fi
783
784 # Create a machine-readable list of metadata revisions for each layer
785 cat > ${BUILDHISTORY_DIR}/metadata-revs <<END
786${@buildhistory_get_metadata_revs(d)}
787END
788
789 ( cd ${BUILDHISTORY_DIR}/
790 # Initialise the repo if necessary
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500791 if [ ! -e .git ] ; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500792 git init -q
793 else
794 git tag -f build-minus-3 build-minus-2 > /dev/null 2>&1 || true
795 git tag -f build-minus-2 build-minus-1 > /dev/null 2>&1 || true
796 git tag -f build-minus-1 > /dev/null 2>&1 || true
797 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600798
799 check_git_config
800
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500801 # Check if there are new/changed files to commit (other than metadata-revs)
802 repostatus=`git status --porcelain | grep -v " metadata-revs$"`
803 HOSTNAME=`hostname 2>/dev/null || echo unknown`
804 CMDLINE="${@buildhistory_get_cmdline(d)}"
805 if [ "$repostatus" != "" ] ; then
806 git add -A .
807 # porcelain output looks like "?? packages/foo/bar"
808 # Ensure we commit metadata-revs with the first commit
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500809 buildhistory_single_commit "$CMDLINE" "$HOSTNAME" dummy
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500810 git gc --auto --quiet
811 else
812 buildhistory_single_commit "$CMDLINE" "$HOSTNAME"
813 fi
814 if [ "${BUILDHISTORY_PUSH_REPO}" != "" ] ; then
815 git push -q ${BUILDHISTORY_PUSH_REPO}
816 fi) || true
817}
818
819python buildhistory_eventhandler() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500820 if e.data.getVar('BUILDHISTORY_FEATURES').strip():
821 reset = e.data.getVar("BUILDHISTORY_RESET")
822 olddir = e.data.getVar("BUILDHISTORY_OLD_DIR")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500823 if isinstance(e, bb.event.BuildStarted):
824 if reset:
825 import shutil
826 # Clean up after potentially interrupted build.
827 if os.path.isdir(olddir):
828 shutil.rmtree(olddir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500829 rootdir = e.data.getVar("BUILDHISTORY_DIR")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500830 entries = [ x for x in os.listdir(rootdir) if not x.startswith('.') ]
831 bb.utils.mkdirhier(olddir)
832 for entry in entries:
833 os.rename(os.path.join(rootdir, entry),
834 os.path.join(olddir, entry))
835 elif isinstance(e, bb.event.BuildCompleted):
836 if reset:
837 import shutil
838 shutil.rmtree(olddir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500839 if e.data.getVar("BUILDHISTORY_COMMIT") == "1":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500840 bb.note("Writing buildhistory")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500841 bb.build.exec_func("buildhistory_write_sigs", d)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500842 localdata = bb.data.createCopy(e.data)
843 localdata.setVar('BUILDHISTORY_BUILD_FAILURES', str(e._failures))
844 interrupted = getattr(e, '_interrupted', 0)
845 localdata.setVar('BUILDHISTORY_BUILD_INTERRUPTED', str(interrupted))
846 bb.build.exec_func("buildhistory_commit", localdata)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500847 else:
848 bb.note("No commit since BUILDHISTORY_COMMIT != '1'")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500849}
850
851addhandler buildhistory_eventhandler
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500852buildhistory_eventhandler[eventmask] = "bb.event.BuildCompleted bb.event.BuildStarted"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500853
854
855# FIXME this ought to be moved into the fetcher
856def _get_srcrev_values(d):
857 """
858 Return the version strings for the current recipe
859 """
860
861 scms = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500862 fetcher = bb.fetch.Fetch(d.getVar('SRC_URI').split(), d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500863 urldata = fetcher.ud
864 for u in urldata:
865 if urldata[u].method.supports_srcrev():
866 scms.append(u)
867
868 autoinc_templ = 'AUTOINC+'
869 dict_srcrevs = {}
870 dict_tag_srcrevs = {}
871 for scm in scms:
872 ud = urldata[scm]
873 for name in ud.names:
874 try:
875 rev = ud.method.sortable_revision(ud, d, name)
876 except TypeError:
877 # support old bitbake versions
878 rev = ud.method.sortable_revision(scm, ud, d, name)
879 # Clean this up when we next bump bitbake version
880 if type(rev) != str:
881 autoinc, rev = rev
882 elif rev.startswith(autoinc_templ):
883 rev = rev[len(autoinc_templ):]
884 dict_srcrevs[name] = rev
885 if 'tag' in ud.parm:
886 tag = ud.parm['tag'];
887 key = name+'_'+tag
888 dict_tag_srcrevs[key] = rev
889 return (dict_srcrevs, dict_tag_srcrevs)
890
891do_fetch[postfuncs] += "write_srcrev"
892do_fetch[vardepsexclude] += "write_srcrev"
893python write_srcrev() {
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500894 write_latest_srcrev(d, d.getVar('BUILDHISTORY_DIR_PACKAGE'))
895}
896
897def write_latest_srcrev(d, pkghistdir):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500898 srcrevfile = os.path.join(pkghistdir, 'latest_srcrev')
899
900 srcrevs, tag_srcrevs = _get_srcrev_values(d)
901 if srcrevs:
902 if not os.path.exists(pkghistdir):
903 bb.utils.mkdirhier(pkghistdir)
904 old_tag_srcrevs = {}
905 if os.path.exists(srcrevfile):
906 with open(srcrevfile) as f:
907 for line in f:
908 if line.startswith('# tag_'):
909 key, value = line.split("=", 1)
910 key = key.replace('# tag_', '').strip()
911 value = value.replace('"', '').strip()
912 old_tag_srcrevs[key] = value
913 with open(srcrevfile, 'w') as f:
914 orig_srcrev = d.getVar('SRCREV', False) or 'INVALID'
915 if orig_srcrev != 'INVALID':
916 f.write('# SRCREV = "%s"\n' % orig_srcrev)
917 if len(srcrevs) > 1:
Brad Bishop19323692019-04-05 15:28:33 -0400918 for name, srcrev in sorted(srcrevs.items()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500919 orig_srcrev = d.getVar('SRCREV_%s' % name, False)
920 if orig_srcrev:
921 f.write('# SRCREV_%s = "%s"\n' % (name, orig_srcrev))
922 f.write('SRCREV_%s = "%s"\n' % (name, srcrev))
923 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500924 f.write('SRCREV = "%s"\n' % next(iter(srcrevs.values())))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500925 if len(tag_srcrevs) > 0:
Brad Bishop19323692019-04-05 15:28:33 -0400926 for name, srcrev in sorted(tag_srcrevs.items()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500927 f.write('# tag_%s = "%s"\n' % (name, srcrev))
928 if name in old_tag_srcrevs and old_tag_srcrevs[name] != srcrev:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500929 pkg = d.getVar('PN')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500930 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))
931
932 else:
933 if os.path.exists(srcrevfile):
934 os.remove(srcrevfile)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500935
936do_testimage[postfuncs] += "write_ptest_result"
937do_testimage[vardepsexclude] += "write_ptest_result"
938
939python write_ptest_result() {
940 write_latest_ptest_result(d, d.getVar('BUILDHISTORY_DIR'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500941}
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500942
943def write_latest_ptest_result(d, histdir):
944 import glob
945 import subprocess
946 test_log_dir = d.getVar('TEST_LOG_DIR')
947 input_ptest = os.path.join(test_log_dir, 'ptest_log')
948 output_ptest = os.path.join(histdir, 'ptest')
949 if os.path.exists(input_ptest):
950 try:
951 # Lock it avoid race issue
952 lock = bb.utils.lockfile(output_ptest + "/ptest.lock")
953 bb.utils.mkdirhier(output_ptest)
954 oe.path.copytree(input_ptest, output_ptest)
955 # Sort test result
956 for result in glob.glob('%s/pass.fail.*' % output_ptest):
957 bb.debug(1, 'Processing %s' % result)
958 cmd = ['sort', result, '-o', result]
959 bb.debug(1, 'Running %s' % cmd)
960 ret = subprocess.call(cmd)
961 if ret != 0:
962 bb.error('Failed to run %s!' % cmd)
963 finally:
964 bb.utils.unlockfile(lock)