blob: 87a6a1a48935ddc52b59d07c106dfe6ca676fbf8 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# Writes build information to target filesystem on /etc/build
3#
4# Copyright (C) 2014 Intel Corporation
5# Author: Alejandro Enedino Hernandez Samaniego <alejandro.hernandez@intel.com>
6#
7# Licensed under the MIT license, see COPYING.MIT for details
8#
9# Usage: add INHERIT += "image-buildinfo" to your conf file
10#
11
12# Desired variables to display
13IMAGE_BUILDINFO_VARS ?= "DISTRO DISTRO_VERSION"
14
Brad Bishop6e60e8b2018-02-01 10:27:11 -050015# Desired location of the output file in the image.
16IMAGE_BUILDINFO_FILE ??= "${sysconfdir}/build"
17
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018# From buildhistory.bbclass
19def image_buildinfo_outputvars(vars, listvars, d):
20 vars = vars.split()
21 listvars = listvars.split()
22 ret = ""
23 for var in vars:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050024 value = d.getVar(var) or ""
25 if (d.getVarFlag(var, 'type') == "list"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026 value = oe.utils.squashspaces(value)
27 ret += "%s = %s\n" % (var, value)
28 return ret.rstrip('\n')
29
30# Gets git branch's status (clean or dirty)
31def get_layer_git_status(path):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050032 import subprocess
33 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050034 subprocess.check_output("""cd %s; export PSEUDO_UNLOAD=1; set -e;
35 git diff --quiet --no-ext-diff
36 git diff --quiet --no-ext-diff --cached""" % path,
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050037 shell=True,
38 stderr=subprocess.STDOUT)
39 return ""
Patrick Williamsc0f7c042017-02-23 20:41:17 -060040 except subprocess.CalledProcessError as ex:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050041 # Silently treat errors as "modified", without checking for the
42 # (expected) return code 1 in a modified git repo. For example, we get
43 # output and a 129 return code when a layer isn't a git repo at all.
44 return "-- modified"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045
46# Returns layer revisions along with their respective status
47def get_layer_revs(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050048 layers = (d.getVar("BBLAYERS") or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049 medadata_revs = ["%-17s = %s:%s %s" % (os.path.basename(i), \
50 base_get_metadata_git_branch(i, None).strip(), \
51 base_get_metadata_git_revision(i, None), \
52 get_layer_git_status(i)) \
53 for i in layers]
54 return '\n'.join(medadata_revs)
55
56def buildinfo_target(d):
57 # Get context
Brad Bishop6e60e8b2018-02-01 10:27:11 -050058 if d.getVar('BB_WORKERCONTEXT') != '1':
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 return ""
60 # Single and list variables to be read
Brad Bishop6e60e8b2018-02-01 10:27:11 -050061 vars = (d.getVar("IMAGE_BUILDINFO_VARS") or "")
62 listvars = (d.getVar("IMAGE_BUILDINFO_LVARS") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063 return image_buildinfo_outputvars(vars, listvars, d)
64
65# Write build information to target filesystem
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050066python buildinfo () {
Brad Bishopf86d0552018-12-04 14:18:15 -080067 if not d.getVar('IMAGE_BUILDINFO_FILE'):
68 return
Brad Bishop6e60e8b2018-02-01 10:27:11 -050069 with open(d.expand('${IMAGE_ROOTFS}${IMAGE_BUILDINFO_FILE}'), 'w') as build:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050070 build.writelines((
71 '''-----------------------
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072Build Configuration: |
73-----------------------
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050074''',
75 buildinfo_target(d),
76 '''
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077-----------------------
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050078Layer Revisions: |
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079-----------------------
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050080''',
Patrick Williamsc0f7c042017-02-23 20:41:17 -060081 get_layer_revs(d),
82 '''
83'''
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050084 ))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085}
86
87IMAGE_PREPROCESS_COMMAND += "buildinfo;"