xref: /OK3568_Linux_fs/yocto/poky/meta/classes/image-buildinfo.bbclass (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1#
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
15# Desired location of the output file in the image.
16IMAGE_BUILDINFO_FILE ??= "${sysconfdir}/build"
17
18# From buildhistory.bbclass
19def image_buildinfo_outputvars(vars, d):
20    vars = vars.split()
21    ret = ""
22    for var in vars:
23        value = d.getVar(var) or ""
24        if (d.getVarFlag(var, 'type') == "list"):
25            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):
31    import subprocess
32    try:
33        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,
36                                shell=True,
37                                stderr=subprocess.STDOUT)
38        return ""
39    except subprocess.CalledProcessError as ex:
40        # 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"
44
45# Returns layer revisions along with their respective status
46def get_layer_revs(d):
47    layers = (d.getVar("BBLAYERS") or "").split()
48    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
57        if d.getVar('BB_WORKERCONTEXT') != '1':
58                return ""
59        # Single and list variables to be read
60        vars = (d.getVar("IMAGE_BUILDINFO_VARS") or "")
61        return image_buildinfo_outputvars(vars, d)
62
63# Write build information to target filesystem
64python buildinfo () {
65    if not d.getVar('IMAGE_BUILDINFO_FILE'):
66        return
67    with open(d.expand('${IMAGE_ROOTFS}${IMAGE_BUILDINFO_FILE}'), 'w') as build:
68        build.writelines((
69            '''-----------------------
70Build Configuration:  |
71-----------------------
72''',
73            buildinfo_target(d),
74            '''
75-----------------------
76Layer Revisions:      |
77-----------------------
78''',
79            get_layer_revs(d),
80            '''
81'''
82       ))
83}
84
85IMAGE_PREPROCESS_COMMAND += "buildinfo;"
86