blob: 213fb9cf9b61f7c4111ff3bde97efcdfc6a5f2c9 [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 Bishop6e60e8b2018-02-01 10:27:11 -050067 with open(d.expand('${IMAGE_ROOTFS}${IMAGE_BUILDINFO_FILE}'), 'w') as build:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050068 build.writelines((
69 '''-----------------------
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070Build Configuration: |
71-----------------------
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050072''',
73 buildinfo_target(d),
74 '''
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075-----------------------
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050076Layer Revisions: |
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077-----------------------
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050078''',
Patrick Williamsc0f7c042017-02-23 20:41:17 -060079 get_layer_revs(d),
80 '''
81'''
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050082 ))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083}
84
85IMAGE_PREPROCESS_COMMAND += "buildinfo;"