reproducible_build.bbclass 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # reproducible_build.bbclass
  2. #
  3. # Sets SOURCE_DATE_EPOCH in each component's build environment.
  4. # Upstream components (generally) respect this environment variable,
  5. # using it in place of the "current" date and time.
  6. # See https://reproducible-builds.org/specs/source-date-epoch/
  7. #
  8. # After sources are unpacked but before they are patched, we set a reproducible value for SOURCE_DATE_EPOCH.
  9. # This value should be reproducible for anyone who builds the same revision from the same sources.
  10. #
  11. # There are 4 ways we determine SOURCE_DATE_EPOCH:
  12. #
  13. # 1. Use the value from __source_date_epoch.txt file if this file exists.
  14. # This file was most likely created in the previous build by one of the following methods 2,3,4.
  15. # Alternatively, it can be provided by a recipe via SRC_URI.
  16. #
  17. # If the file does not exist:
  18. #
  19. # 2. If there is a git checkout, use the last git commit timestamp.
  20. # Git does not preserve file timestamps on checkout.
  21. #
  22. # 3. Use the mtime of "known" files such as NEWS, CHANGLELOG, ...
  23. # This works for well-kept repositories distributed via tarball.
  24. #
  25. # 4. Use the modification time of the youngest file in the source tree, if there is one.
  26. # This will be the newest file from the distribution tarball, if any.
  27. #
  28. # 5. Fall back to a fixed timestamp.
  29. #
  30. # Once the value of SOURCE_DATE_EPOCH is determined, it is stored in the recipe's SDE_FILE.
  31. # If none of these mechanisms are suitable, replace the do_deploy_source_date_epoch task
  32. # with recipe-specific functionality to write the appropriate SOURCE_DATE_EPOCH into the SDE_FILE.
  33. #
  34. # If this file is found by other tasks, the value is exported in the SOURCE_DATE_EPOCH variable.
  35. # SOURCE_DATE_EPOCH is set for all tasks that might use it (do_configure, do_compile, do_package, ...)
  36. BUILD_REPRODUCIBLE_BINARIES ??= '1'
  37. inherit ${@oe.utils.ifelse(d.getVar('BUILD_REPRODUCIBLE_BINARIES') == '1', 'reproducible_build_simple', '')}
  38. SDE_DIR ="${WORKDIR}/source-date-epoch"
  39. SDE_FILE = "${SDE_DIR}/__source_date_epoch.txt"
  40. SDE_DEPLOYDIR = "${WORKDIR}/deploy-source-date-epoch"
  41. SSTATETASKS += "do_deploy_source_date_epoch"
  42. do_deploy_source_date_epoch () {
  43. mkdir -p ${SDE_DEPLOYDIR}
  44. if [ -e ${SDE_FILE} ]; then
  45. echo "Deploying SDE from ${SDE_FILE} -> ${SDE_DEPLOYDIR}."
  46. cp -p ${SDE_FILE} ${SDE_DEPLOYDIR}/__source_date_epoch.txt
  47. else
  48. echo "${SDE_FILE} not found!"
  49. fi
  50. }
  51. python do_deploy_source_date_epoch_setscene () {
  52. sstate_setscene(d)
  53. bb.utils.mkdirhier(d.getVar('SDE_DIR'))
  54. sde_file = os.path.join(d.getVar('SDE_DEPLOYDIR'), '__source_date_epoch.txt')
  55. if os.path.exists(sde_file):
  56. target = d.getVar('SDE_FILE')
  57. bb.debug(1, "Moving setscene SDE file %s -> %s" % (sde_file, target))
  58. os.rename(sde_file, target)
  59. else:
  60. bb.debug(1, "%s not found!" % sde_file)
  61. }
  62. do_deploy_source_date_epoch[dirs] = "${SDE_DEPLOYDIR}"
  63. do_deploy_source_date_epoch[sstate-plaindirs] = "${SDE_DEPLOYDIR}"
  64. addtask do_deploy_source_date_epoch_setscene
  65. addtask do_deploy_source_date_epoch before do_configure after do_patch
  66. python create_source_date_epoch_stamp() {
  67. import oe.reproducible
  68. epochfile = d.getVar('SDE_FILE')
  69. # If it exists we need to regenerate as the sources may have changed
  70. if os.path.isfile(epochfile):
  71. bb.debug(1, "Deleting existing SOURCE_DATE_EPOCH from: %s" % epochfile)
  72. os.remove(epochfile)
  73. source_date_epoch = oe.reproducible.get_source_date_epoch(d, d.getVar('S'))
  74. bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch)
  75. bb.utils.mkdirhier(d.getVar('SDE_DIR'))
  76. with open(epochfile, 'w') as f:
  77. f.write(str(source_date_epoch))
  78. }
  79. def get_source_date_epoch_value(d):
  80. cached = d.getVar('__CACHED_SOURCE_DATE_EPOCH')
  81. if cached:
  82. return cached
  83. epochfile = d.getVar('SDE_FILE')
  84. source_date_epoch = 0
  85. if os.path.isfile(epochfile):
  86. with open(epochfile, 'r') as f:
  87. s = f.read()
  88. try:
  89. source_date_epoch = int(s)
  90. except ValueError:
  91. bb.warn("SOURCE_DATE_EPOCH value '%s' is invalid. Reverting to 0" % s)
  92. source_date_epoch = 0
  93. bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch)
  94. else:
  95. bb.debug(1, "Cannot find %s. SOURCE_DATE_EPOCH will default to %d" % (epochfile, source_date_epoch))
  96. d.setVar('__CACHED_SOURCE_DATE_EPOCH', str(source_date_epoch))
  97. return str(source_date_epoch)
  98. export SOURCE_DATE_EPOCH ?= "${@get_source_date_epoch_value(d)}"
  99. BB_HASHBASE_WHITELIST += "SOURCE_DATE_EPOCH"
  100. python () {
  101. if d.getVar('BUILD_REPRODUCIBLE_BINARIES') == '1':
  102. d.appendVarFlag("do_unpack", "postfuncs", " create_source_date_epoch_stamp")
  103. }