blob: 40b292b139f6bbb405ec962bda90ac7e4a9b4629 [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.
522 # This awk script is somewhat messy, but handles where the size is not printed for device files under pseudo
523 if [ "$3" = "fakeroot" ] ; then
524 ( cd $1 && ${FAKEROOTENV} ${FAKEROOTCMD} find . ! -path . -printf "%M %-10u %-10g %10s %p -> %l\n" | sort -k5 | sed 's/ * -> $//' > $2 )
525 else
526 ( cd $1 && find . ! -path . -printf "%M %-10u %-10g %10s %p -> %l\n" | sort -k5 | sed 's/ * -> $//' > $2 )
527 fi
528}
529
530buildhistory_list_pkg_files() {
531 # Create individual files-in-package for each recipe's package
532 for pkgdir in $(find ${PKGDEST}/* -maxdepth 0 -type d); do
533 pkgname=$(basename $pkgdir)
534 outfolder="${BUILDHISTORY_DIR_PACKAGE}/$pkgname"
535 outfile="$outfolder/files-in-package.txt"
536 # Make sure the output folder exists so we can create the file
537 if [ ! -d $outfolder ] ; then
538 bbdebug 2 "Folder $outfolder does not exist, file $outfile not created"
539 continue
540 fi
541 buildhistory_list_files $pkgdir $outfile fakeroot
542 done
543}
544
545buildhistory_get_imageinfo() {
546 if [ "${@bb.utils.contains('BUILDHISTORY_FEATURES', 'image', '1', '0', d)}" = "0" ] ; then
547 return
548 fi
549
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500550 mkdir -p ${BUILDHISTORY_DIR_IMAGE}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500551 buildhistory_list_files ${IMAGE_ROOTFS} ${BUILDHISTORY_DIR_IMAGE}/files-in-image.txt
552
553 # Collect files requested in BUILDHISTORY_IMAGE_FILES
554 rm -rf ${BUILDHISTORY_DIR_IMAGE}/image-files
555 for f in ${BUILDHISTORY_IMAGE_FILES}; do
556 if [ -f ${IMAGE_ROOTFS}/$f ] ; then
557 mkdir -p ${BUILDHISTORY_DIR_IMAGE}/image-files/`dirname $f`
558 cp ${IMAGE_ROOTFS}/$f ${BUILDHISTORY_DIR_IMAGE}/image-files/$f
559 fi
560 done
561
562 # Record some machine-readable meta-information about the image
563 printf "" > ${BUILDHISTORY_DIR_IMAGE}/image-info.txt
564 cat >> ${BUILDHISTORY_DIR_IMAGE}/image-info.txt <<END
565${@buildhistory_get_imagevars(d)}
566END
567 imagesize=`du -ks ${IMAGE_ROOTFS} | awk '{ print $1 }'`
568 echo "IMAGESIZE = $imagesize" >> ${BUILDHISTORY_DIR_IMAGE}/image-info.txt
569
570 # Add some configuration information
571 echo "${MACHINE}: ${IMAGE_BASENAME} configured for ${DISTRO} ${DISTRO_VERSION}" > ${BUILDHISTORY_DIR_IMAGE}/build-id.txt
572
573 cat >> ${BUILDHISTORY_DIR_IMAGE}/build-id.txt <<END
574${@buildhistory_get_build_id(d)}
575END
576}
577
578buildhistory_get_sdkinfo() {
579 if [ "${@bb.utils.contains('BUILDHISTORY_FEATURES', 'sdk', '1', '0', d)}" = "0" ] ; then
580 return
581 fi
582
583 buildhistory_list_files ${SDK_OUTPUT} ${BUILDHISTORY_DIR_SDK}/files-in-sdk.txt
584
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500585 # Collect files requested in BUILDHISTORY_SDK_FILES
586 rm -rf ${BUILDHISTORY_DIR_SDK}/sdk-files
587 for f in ${BUILDHISTORY_SDK_FILES}; do
588 if [ -f ${SDK_OUTPUT}/${SDKPATH}/$f ] ; then
589 mkdir -p ${BUILDHISTORY_DIR_SDK}/sdk-files/`dirname $f`
590 cp ${SDK_OUTPUT}/${SDKPATH}/$f ${BUILDHISTORY_DIR_SDK}/sdk-files/$f
591 fi
592 done
593
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500594 # Record some machine-readable meta-information about the SDK
595 printf "" > ${BUILDHISTORY_DIR_SDK}/sdk-info.txt
596 cat >> ${BUILDHISTORY_DIR_SDK}/sdk-info.txt <<END
597${@buildhistory_get_sdkvars(d)}
598END
599 sdksize=`du -ks ${SDK_OUTPUT} | awk '{ print $1 }'`
600 echo "SDKSIZE = $sdksize" >> ${BUILDHISTORY_DIR_SDK}/sdk-info.txt
601}
602
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500603python buildhistory_get_extra_sdkinfo() {
604 import operator
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500605 from oe.sdk import get_extra_sdkinfo
606
607 sstate_dir = d.expand('${SDK_OUTPUT}/${SDKPATH}/sstate-cache')
608 extra_info = get_extra_sdkinfo(sstate_dir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500609
610 if d.getVar('BB_CURRENTTASK') == 'populate_sdk_ext' and \
611 "sdk" in (d.getVar('BUILDHISTORY_FEATURES') or "").split():
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500612 with open(d.expand('${BUILDHISTORY_DIR_SDK}/sstate-package-sizes.txt'), 'w') as f:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500613 filesizes_sorted = sorted(extra_info['filesizes'].items(), key=operator.itemgetter(1, 0), reverse=True)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500614 for fn, size in filesizes_sorted:
615 f.write('%10d KiB %s\n' % (size, fn))
616 with open(d.expand('${BUILDHISTORY_DIR_SDK}/sstate-task-sizes.txt'), 'w') as f:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500617 tasksizes_sorted = sorted(extra_info['tasksizes'].items(), key=operator.itemgetter(1, 0), reverse=True)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500618 for task, size in tasksizes_sorted:
619 f.write('%10d KiB %s\n' % (size, task))
620}
621
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500622# By using ROOTFS_POSTUNINSTALL_COMMAND we get in after uninstallation of
623# unneeded packages but before the removal of packaging files
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500624ROOTFS_POSTUNINSTALL_COMMAND += "buildhistory_list_installed_image ;"
625ROOTFS_POSTUNINSTALL_COMMAND += "buildhistory_get_image_installed ;"
626ROOTFS_POSTUNINSTALL_COMMAND[vardepvalueexclude] .= "| buildhistory_list_installed_image ;| buildhistory_get_image_installed ;"
627ROOTFS_POSTUNINSTALL_COMMAND[vardepsexclude] += "buildhistory_list_installed_image buildhistory_get_image_installed"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500628
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500629IMAGE_POSTPROCESS_COMMAND += "buildhistory_get_imageinfo ;"
630IMAGE_POSTPROCESS_COMMAND[vardepvalueexclude] .= "| buildhistory_get_imageinfo ;"
631IMAGE_POSTPROCESS_COMMAND[vardepsexclude] += "buildhistory_get_imageinfo"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500632
633# We want these to be the last run so that we get called after complementary package installation
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500634POPULATE_SDK_POST_TARGET_COMMAND_append = " buildhistory_list_installed_sdk_target;"
635POPULATE_SDK_POST_TARGET_COMMAND_append = " buildhistory_get_sdk_installed_target;"
636POPULATE_SDK_POST_TARGET_COMMAND[vardepvalueexclude] .= "| buildhistory_list_installed_sdk_target;| buildhistory_get_sdk_installed_target;"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500637
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500638POPULATE_SDK_POST_HOST_COMMAND_append = " buildhistory_list_installed_sdk_host;"
639POPULATE_SDK_POST_HOST_COMMAND_append = " buildhistory_get_sdk_installed_host;"
640POPULATE_SDK_POST_HOST_COMMAND[vardepvalueexclude] .= "| buildhistory_list_installed_sdk_host;| buildhistory_get_sdk_installed_host;"
641
642SDK_POSTPROCESS_COMMAND_append = " buildhistory_get_sdkinfo ; buildhistory_get_extra_sdkinfo; "
643SDK_POSTPROCESS_COMMAND[vardepvalueexclude] .= "| buildhistory_get_sdkinfo ; buildhistory_get_extra_sdkinfo; "
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500644
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500645python buildhistory_write_sigs() {
646 if not "task" in (d.getVar('BUILDHISTORY_FEATURES') or "").split():
647 return
648
649 # Create sigs file
650 if hasattr(bb.parse.siggen, 'dump_siglist'):
651 taskoutdir = os.path.join(d.getVar('BUILDHISTORY_DIR'), 'task')
652 bb.utils.mkdirhier(taskoutdir)
653 bb.parse.siggen.dump_siglist(os.path.join(taskoutdir, 'tasksigs.txt'))
654}
655
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500656def buildhistory_get_build_id(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500657 if d.getVar('BB_WORKERCONTEXT') != '1':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500658 return ""
659 localdata = bb.data.createCopy(d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500660 statuslines = []
661 for func in oe.data.typed_value('BUILDCFG_FUNCS', localdata):
662 g = globals()
663 if func not in g:
664 bb.warn("Build configuration function '%s' does not exist" % func)
665 else:
666 flines = g[func](localdata)
667 if flines:
668 statuslines.extend(flines)
669
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500670 statusheader = d.getVar('BUILDCFG_HEADER')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500671 return('\n%s\n%s\n' % (statusheader, '\n'.join(statuslines)))
672
673def buildhistory_get_metadata_revs(d):
674 # 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 -0500675 layers = (d.getVar("BBLAYERS") or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500676 medadata_revs = ["%-17s = %s:%s" % (os.path.basename(i), \
677 base_get_metadata_git_branch(i, None).strip(), \
678 base_get_metadata_git_revision(i, None)) \
679 for i in layers]
680 return '\n'.join(medadata_revs)
681
682def outputvars(vars, listvars, d):
683 vars = vars.split()
684 listvars = listvars.split()
685 ret = ""
686 for var in vars:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500687 value = d.getVar(var) or ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500688 if var in listvars:
689 # Squash out spaces
690 value = oe.utils.squashspaces(value)
691 ret += "%s = %s\n" % (var, value)
692 return ret.rstrip('\n')
693
694def buildhistory_get_imagevars(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500695 if d.getVar('BB_WORKERCONTEXT') != '1':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500696 return ""
697 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"
698 listvars = "USER_CLASSES IMAGE_CLASSES IMAGE_FEATURES IMAGE_LINGUAS IMAGE_INSTALL BAD_RECOMMENDATIONS PACKAGE_EXCLUDE"
699 return outputvars(imagevars, listvars, d)
700
701def buildhistory_get_sdkvars(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500702 if d.getVar('BB_WORKERCONTEXT') != '1':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500703 return ""
704 sdkvars = "DISTRO DISTRO_VERSION SDK_NAME SDK_VERSION SDKMACHINE SDKIMAGE_FEATURES BAD_RECOMMENDATIONS NO_RECOMMENDATIONS PACKAGE_EXCLUDE"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500705 if d.getVar('BB_CURRENTTASK') == 'populate_sdk_ext':
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500706 # Extensible SDK uses some additional variables
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600707 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 -0500708 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 -0500709 return outputvars(sdkvars, listvars, d)
710
711
712def buildhistory_get_cmdline(d):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500713 argv = d.getVar('BB_CMDLINE', False)
714 if argv:
715 if argv[0].endswith('bin/bitbake'):
716 bincmd = 'bitbake'
717 else:
718 bincmd = argv[0]
719 return '%s %s' % (bincmd, ' '.join(argv[1:]))
720 return ''
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500721
722
723buildhistory_single_commit() {
724 if [ "$3" = "" ] ; then
725 commitopts="${BUILDHISTORY_DIR}/ --allow-empty"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500726 shortlogprefix="No changes: "
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500727 else
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500728 commitopts=""
729 shortlogprefix=""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500730 fi
731 if [ "${BUILDHISTORY_BUILD_FAILURES}" = "0" ] ; then
732 result="succeeded"
733 else
734 result="failed"
735 fi
736 case ${BUILDHISTORY_BUILD_INTERRUPTED} in
737 1)
738 result="$result (interrupted)"
739 ;;
740 2)
741 result="$result (force interrupted)"
742 ;;
743 esac
744 commitmsgfile=`mktemp`
745 cat > $commitmsgfile << END
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500746${shortlogprefix}Build ${BUILDNAME} of ${DISTRO} ${DISTRO_VERSION} for machine ${MACHINE} on $2
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500747
748cmd: $1
749
750result: $result
751
752metadata revisions:
753END
754 cat ${BUILDHISTORY_DIR}/metadata-revs >> $commitmsgfile
755 git commit $commitopts -F $commitmsgfile --author "${BUILDHISTORY_COMMIT_AUTHOR}" > /dev/null
756 rm $commitmsgfile
757}
758
759buildhistory_commit() {
760 if [ ! -d ${BUILDHISTORY_DIR} ] ; then
761 # Code above that creates this dir never executed, so there can't be anything to commit
762 return
763 fi
764
765 # Create a machine-readable list of metadata revisions for each layer
766 cat > ${BUILDHISTORY_DIR}/metadata-revs <<END
767${@buildhistory_get_metadata_revs(d)}
768END
769
770 ( cd ${BUILDHISTORY_DIR}/
771 # Initialise the repo if necessary
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500772 if [ ! -e .git ] ; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500773 git init -q
774 else
775 git tag -f build-minus-3 build-minus-2 > /dev/null 2>&1 || true
776 git tag -f build-minus-2 build-minus-1 > /dev/null 2>&1 || true
777 git tag -f build-minus-1 > /dev/null 2>&1 || true
778 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600779
780 check_git_config
781
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500782 # Check if there are new/changed files to commit (other than metadata-revs)
783 repostatus=`git status --porcelain | grep -v " metadata-revs$"`
784 HOSTNAME=`hostname 2>/dev/null || echo unknown`
785 CMDLINE="${@buildhistory_get_cmdline(d)}"
786 if [ "$repostatus" != "" ] ; then
787 git add -A .
788 # porcelain output looks like "?? packages/foo/bar"
789 # Ensure we commit metadata-revs with the first commit
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500790 buildhistory_single_commit "$CMDLINE" "$HOSTNAME" dummy
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500791 git gc --auto --quiet
792 else
793 buildhistory_single_commit "$CMDLINE" "$HOSTNAME"
794 fi
795 if [ "${BUILDHISTORY_PUSH_REPO}" != "" ] ; then
796 git push -q ${BUILDHISTORY_PUSH_REPO}
797 fi) || true
798}
799
800python buildhistory_eventhandler() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500801 if e.data.getVar('BUILDHISTORY_FEATURES').strip():
802 reset = e.data.getVar("BUILDHISTORY_RESET")
803 olddir = e.data.getVar("BUILDHISTORY_OLD_DIR")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500804 if isinstance(e, bb.event.BuildStarted):
805 if reset:
806 import shutil
807 # Clean up after potentially interrupted build.
808 if os.path.isdir(olddir):
809 shutil.rmtree(olddir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500810 rootdir = e.data.getVar("BUILDHISTORY_DIR")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500811 entries = [ x for x in os.listdir(rootdir) if not x.startswith('.') ]
812 bb.utils.mkdirhier(olddir)
813 for entry in entries:
814 os.rename(os.path.join(rootdir, entry),
815 os.path.join(olddir, entry))
816 elif isinstance(e, bb.event.BuildCompleted):
817 if reset:
818 import shutil
819 shutil.rmtree(olddir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500820 if e.data.getVar("BUILDHISTORY_COMMIT") == "1":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500821 bb.note("Writing buildhistory")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500822 bb.build.exec_func("buildhistory_write_sigs", d)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500823 localdata = bb.data.createCopy(e.data)
824 localdata.setVar('BUILDHISTORY_BUILD_FAILURES', str(e._failures))
825 interrupted = getattr(e, '_interrupted', 0)
826 localdata.setVar('BUILDHISTORY_BUILD_INTERRUPTED', str(interrupted))
827 bb.build.exec_func("buildhistory_commit", localdata)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500828 else:
829 bb.note("No commit since BUILDHISTORY_COMMIT != '1'")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500830}
831
832addhandler buildhistory_eventhandler
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500833buildhistory_eventhandler[eventmask] = "bb.event.BuildCompleted bb.event.BuildStarted"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500834
835
836# FIXME this ought to be moved into the fetcher
837def _get_srcrev_values(d):
838 """
839 Return the version strings for the current recipe
840 """
841
842 scms = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500843 fetcher = bb.fetch.Fetch(d.getVar('SRC_URI').split(), d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500844 urldata = fetcher.ud
845 for u in urldata:
846 if urldata[u].method.supports_srcrev():
847 scms.append(u)
848
849 autoinc_templ = 'AUTOINC+'
850 dict_srcrevs = {}
851 dict_tag_srcrevs = {}
852 for scm in scms:
853 ud = urldata[scm]
854 for name in ud.names:
855 try:
856 rev = ud.method.sortable_revision(ud, d, name)
857 except TypeError:
858 # support old bitbake versions
859 rev = ud.method.sortable_revision(scm, ud, d, name)
860 # Clean this up when we next bump bitbake version
861 if type(rev) != str:
862 autoinc, rev = rev
863 elif rev.startswith(autoinc_templ):
864 rev = rev[len(autoinc_templ):]
865 dict_srcrevs[name] = rev
866 if 'tag' in ud.parm:
867 tag = ud.parm['tag'];
868 key = name+'_'+tag
869 dict_tag_srcrevs[key] = rev
870 return (dict_srcrevs, dict_tag_srcrevs)
871
872do_fetch[postfuncs] += "write_srcrev"
873do_fetch[vardepsexclude] += "write_srcrev"
874python write_srcrev() {
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500875 write_latest_srcrev(d, d.getVar('BUILDHISTORY_DIR_PACKAGE'))
876}
877
878def write_latest_srcrev(d, pkghistdir):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500879 srcrevfile = os.path.join(pkghistdir, 'latest_srcrev')
880
881 srcrevs, tag_srcrevs = _get_srcrev_values(d)
882 if srcrevs:
883 if not os.path.exists(pkghistdir):
884 bb.utils.mkdirhier(pkghistdir)
885 old_tag_srcrevs = {}
886 if os.path.exists(srcrevfile):
887 with open(srcrevfile) as f:
888 for line in f:
889 if line.startswith('# tag_'):
890 key, value = line.split("=", 1)
891 key = key.replace('# tag_', '').strip()
892 value = value.replace('"', '').strip()
893 old_tag_srcrevs[key] = value
894 with open(srcrevfile, 'w') as f:
895 orig_srcrev = d.getVar('SRCREV', False) or 'INVALID'
896 if orig_srcrev != 'INVALID':
897 f.write('# SRCREV = "%s"\n' % orig_srcrev)
898 if len(srcrevs) > 1:
899 for name, srcrev in srcrevs.items():
900 orig_srcrev = d.getVar('SRCREV_%s' % name, False)
901 if orig_srcrev:
902 f.write('# SRCREV_%s = "%s"\n' % (name, orig_srcrev))
903 f.write('SRCREV_%s = "%s"\n' % (name, srcrev))
904 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500905 f.write('SRCREV = "%s"\n' % next(iter(srcrevs.values())))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500906 if len(tag_srcrevs) > 0:
907 for name, srcrev in tag_srcrevs.items():
908 f.write('# tag_%s = "%s"\n' % (name, srcrev))
909 if name in old_tag_srcrevs and old_tag_srcrevs[name] != srcrev:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500910 pkg = d.getVar('PN')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500911 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))
912
913 else:
914 if os.path.exists(srcrevfile):
915 os.remove(srcrevfile)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500916
917do_testimage[postfuncs] += "write_ptest_result"
918do_testimage[vardepsexclude] += "write_ptest_result"
919
920python write_ptest_result() {
921 write_latest_ptest_result(d, d.getVar('BUILDHISTORY_DIR'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500922}
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500923
924def write_latest_ptest_result(d, histdir):
925 import glob
926 import subprocess
927 test_log_dir = d.getVar('TEST_LOG_DIR')
928 input_ptest = os.path.join(test_log_dir, 'ptest_log')
929 output_ptest = os.path.join(histdir, 'ptest')
930 if os.path.exists(input_ptest):
931 try:
932 # Lock it avoid race issue
933 lock = bb.utils.lockfile(output_ptest + "/ptest.lock")
934 bb.utils.mkdirhier(output_ptest)
935 oe.path.copytree(input_ptest, output_ptest)
936 # Sort test result
937 for result in glob.glob('%s/pass.fail.*' % output_ptest):
938 bb.debug(1, 'Processing %s' % result)
939 cmd = ['sort', result, '-o', result]
940 bb.debug(1, 'Running %s' % cmd)
941 ret = subprocess.call(cmd)
942 if ret != 0:
943 bb.error('Failed to run %s!' % cmd)
944 finally:
945 bb.utils.unlockfile(lock)