blob: 94c585d4cd98d0523fc258b6faf8edb76215b7a2 [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
Brad Bishop19323692019-04-05 15:28:33 -040019def image_buildinfo_outputvars(vars, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050020 vars = vars.split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021 ret = ""
22 for var in vars:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050023 value = d.getVar(var) or ""
24 if (d.getVarFlag(var, 'type') == "list"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025 value = oe.utils.squashspaces(value)
26 ret += "%s = %s\n" % (var, value)
27 return ret.rstrip('\n')
28
29# Gets git branch's status (clean or dirty)
30def get_layer_git_status(path):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050031 import subprocess
32 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050033 subprocess.check_output("""cd %s; export PSEUDO_UNLOAD=1; set -e;
34 git diff --quiet --no-ext-diff
35 git diff --quiet --no-ext-diff --cached""" % path,
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050036 shell=True,
37 stderr=subprocess.STDOUT)
38 return ""
Patrick Williamsc0f7c042017-02-23 20:41:17 -060039 except subprocess.CalledProcessError as ex:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050040 # Silently treat errors as "modified", without checking for the
41 # (expected) return code 1 in a modified git repo. For example, we get
42 # output and a 129 return code when a layer isn't a git repo at all.
43 return "-- modified"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044
45# Returns layer revisions along with their respective status
46def get_layer_revs(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050047 layers = (d.getVar("BBLAYERS") or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048 medadata_revs = ["%-17s = %s:%s %s" % (os.path.basename(i), \
49 base_get_metadata_git_branch(i, None).strip(), \
50 base_get_metadata_git_revision(i, None), \
51 get_layer_git_status(i)) \
52 for i in layers]
53 return '\n'.join(medadata_revs)
54
55def buildinfo_target(d):
56 # Get context
Brad Bishop6e60e8b2018-02-01 10:27:11 -050057 if d.getVar('BB_WORKERCONTEXT') != '1':
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058 return ""
59 # Single and list variables to be read
Brad Bishop6e60e8b2018-02-01 10:27:11 -050060 vars = (d.getVar("IMAGE_BUILDINFO_VARS") or "")
Brad Bishop19323692019-04-05 15:28:33 -040061 return image_buildinfo_outputvars(vars, d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062
63# Write build information to target filesystem
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050064python buildinfo () {
Brad Bishopf86d0552018-12-04 14:18:15 -080065 if not d.getVar('IMAGE_BUILDINFO_FILE'):
66 return
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;"