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