image-buildinfo.bbclass 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. # Desired variables to display
  12. IMAGE_BUILDINFO_VARS ?= "DISTRO DISTRO_VERSION"
  13. # Desired location of the output file in the image.
  14. IMAGE_BUILDINFO_FILE ??= "${sysconfdir}/build"
  15. # From buildhistory.bbclass
  16. def image_buildinfo_outputvars(vars, d):
  17. vars = vars.split()
  18. ret = ""
  19. for var in vars:
  20. value = d.getVar(var) or ""
  21. if (d.getVarFlag(var, 'type') == "list"):
  22. value = oe.utils.squashspaces(value)
  23. ret += "%s = %s\n" % (var, value)
  24. return ret.rstrip('\n')
  25. # Gets git branch's status (clean or dirty)
  26. def get_layer_git_status(path):
  27. import subprocess
  28. try:
  29. subprocess.check_output("""cd %s; export PSEUDO_UNLOAD=1; set -e;
  30. git diff --quiet --no-ext-diff
  31. git diff --quiet --no-ext-diff --cached""" % path,
  32. shell=True,
  33. stderr=subprocess.STDOUT)
  34. return ""
  35. except subprocess.CalledProcessError as ex:
  36. # Silently treat errors as "modified", without checking for the
  37. # (expected) return code 1 in a modified git repo. For example, we get
  38. # output and a 129 return code when a layer isn't a git repo at all.
  39. return "-- modified"
  40. # Returns layer revisions along with their respective status
  41. def get_layer_revs(d):
  42. layers = (d.getVar("BBLAYERS") or "").split()
  43. medadata_revs = ["%-17s = %s:%s %s" % (os.path.basename(i), \
  44. base_get_metadata_git_branch(i, None).strip(), \
  45. base_get_metadata_git_revision(i, None), \
  46. get_layer_git_status(i)) \
  47. for i in layers]
  48. return '\n'.join(medadata_revs)
  49. def buildinfo_target(d):
  50. # Get context
  51. if d.getVar('BB_WORKERCONTEXT') != '1':
  52. return ""
  53. # Single and list variables to be read
  54. vars = (d.getVar("IMAGE_BUILDINFO_VARS") or "")
  55. return image_buildinfo_outputvars(vars, d)
  56. # Write build information to target filesystem
  57. python buildinfo () {
  58. if not d.getVar('IMAGE_BUILDINFO_FILE'):
  59. return
  60. with open(d.expand('${IMAGE_ROOTFS}${IMAGE_BUILDINFO_FILE}'), 'w') as build:
  61. build.writelines((
  62. '''-----------------------
  63. Build Configuration: |
  64. -----------------------
  65. ''',
  66. buildinfo_target(d),
  67. '''
  68. -----------------------
  69. Layer Revisions: |
  70. -----------------------
  71. ''',
  72. get_layer_revs(d),
  73. '''
  74. '''
  75. ))
  76. }
  77. IMAGE_PREPROCESS_COMMAND += "buildinfo;"