Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 |
| 13 | IMAGE_BUILDINFO_VARS ?= "DISTRO DISTRO_VERSION" |
| 14 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 15 | # Desired location of the output file in the image. |
| 16 | IMAGE_BUILDINFO_FILE ??= "${sysconfdir}/build" |
| 17 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 18 | # From buildhistory.bbclass |
| 19 | def image_buildinfo_outputvars(vars, listvars, d): |
| 20 | vars = vars.split() |
| 21 | listvars = listvars.split() |
| 22 | ret = "" |
| 23 | for var in vars: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 24 | value = d.getVar(var) or "" |
| 25 | if (d.getVarFlag(var, 'type') == "list"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | 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) |
| 31 | def get_layer_git_status(path): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 32 | import subprocess |
| 33 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 34 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 37 | shell=True, |
| 38 | stderr=subprocess.STDOUT) |
| 39 | return "" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 40 | except subprocess.CalledProcessError as ex: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 41 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 45 | |
| 46 | # Returns layer revisions along with their respective status |
| 47 | def get_layer_revs(d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 48 | layers = (d.getVar("BBLAYERS") or "").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 49 | 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 | |
| 56 | def buildinfo_target(d): |
| 57 | # Get context |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 58 | if d.getVar('BB_WORKERCONTEXT') != '1': |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 59 | return "" |
| 60 | # Single and list variables to be read |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 61 | vars = (d.getVar("IMAGE_BUILDINFO_VARS") or "") |
| 62 | listvars = (d.getVar("IMAGE_BUILDINFO_LVARS") or "") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | return image_buildinfo_outputvars(vars, listvars, d) |
| 64 | |
| 65 | # Write build information to target filesystem |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 66 | python buildinfo () { |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 67 | if not d.getVar('IMAGE_BUILDINFO_FILE'): |
| 68 | return |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 69 | with open(d.expand('${IMAGE_ROOTFS}${IMAGE_BUILDINFO_FILE}'), 'w') as build: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 70 | build.writelines(( |
| 71 | '''----------------------- |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 72 | Build Configuration: | |
| 73 | ----------------------- |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 74 | ''', |
| 75 | buildinfo_target(d), |
| 76 | ''' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | ----------------------- |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 78 | Layer Revisions: | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 79 | ----------------------- |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 80 | ''', |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 81 | get_layer_revs(d), |
| 82 | ''' |
| 83 | ''' |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 84 | )) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | IMAGE_PREPROCESS_COMMAND += "buildinfo;" |