| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 1 | # reproducible_build.bbclass | 
|  | 2 | # | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 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/ | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 7 | # | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 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. | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 10 | # | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 11 | # There are 4 ways we determine SOURCE_DATE_EPOCH: | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 12 | # | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 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. | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 16 | # | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 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. | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 21 | # | 
|  | 22 | # 3. Use the mtime of "known" files such as NEWS, CHANGLELOG, ... | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 23 | #    This works for well-kept repositories distributed via tarball. | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 24 | # | 
| Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 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. | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 29 | # | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 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, ...) | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 36 |  | 
|  | 37 | BUILD_REPRODUCIBLE_BINARIES ??= '1' | 
|  | 38 | inherit ${@oe.utils.ifelse(d.getVar('BUILD_REPRODUCIBLE_BINARIES') == '1', 'reproducible_build_simple', '')} | 
|  | 39 |  | 
| Andrew Geissler | 90fd73c | 2021-03-05 15:25:55 -0600 | [diff] [blame] | 40 | SDE_DIR = "${WORKDIR}/source-date-epoch" | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 41 | SDE_FILE = "${SDE_DIR}/__source_date_epoch.txt" | 
| Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 42 | SDE_DEPLOYDIR = "${WORKDIR}/deploy-source-date-epoch" | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 43 |  | 
| Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 44 | # Enable compiler warning when the __TIME__, __DATE__ and __TIMESTAMP__ macros are used. | 
|  | 45 | TARGET_CC_ARCH_append_class-target = " -Wdate-time" | 
|  | 46 |  | 
| Andrew Geissler | 90fd73c | 2021-03-05 15:25:55 -0600 | [diff] [blame] | 47 | # A SOURCE_DATE_EPOCH of '0' might be misinterpreted as no SDE | 
|  | 48 | export SOURCE_DATE_EPOCH_FALLBACK ??= "1302044400" | 
|  | 49 |  | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 50 | SSTATETASKS += "do_deploy_source_date_epoch" | 
|  | 51 |  | 
|  | 52 | do_deploy_source_date_epoch () { | 
| Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 53 | mkdir -p ${SDE_DEPLOYDIR} | 
|  | 54 | if [ -e ${SDE_FILE} ]; then | 
| Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 55 | echo "Deploying SDE from ${SDE_FILE} -> ${SDE_DEPLOYDIR}." | 
| Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 56 | cp -p ${SDE_FILE} ${SDE_DEPLOYDIR}/__source_date_epoch.txt | 
| Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 57 | else | 
|  | 58 | echo "${SDE_FILE} not found!" | 
| Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 59 | fi | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
|  | 62 | python do_deploy_source_date_epoch_setscene () { | 
|  | 63 | sstate_setscene(d) | 
| Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 64 | bb.utils.mkdirhier(d.getVar('SDE_DIR')) | 
|  | 65 | sde_file = os.path.join(d.getVar('SDE_DEPLOYDIR'), '__source_date_epoch.txt') | 
|  | 66 | if os.path.exists(sde_file): | 
| Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 67 | target = d.getVar('SDE_FILE') | 
|  | 68 | bb.debug(1, "Moving setscene SDE file %s -> %s" % (sde_file, target)) | 
| Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 69 | bb.utils.rename(sde_file, target) | 
| Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 70 | else: | 
|  | 71 | bb.debug(1, "%s not found!" % sde_file) | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 72 | } | 
|  | 73 |  | 
| Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 74 | do_deploy_source_date_epoch[dirs] = "${SDE_DEPLOYDIR}" | 
|  | 75 | do_deploy_source_date_epoch[sstate-plaindirs] = "${SDE_DEPLOYDIR}" | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 76 | addtask do_deploy_source_date_epoch_setscene | 
|  | 77 | addtask do_deploy_source_date_epoch before do_configure after do_patch | 
|  | 78 |  | 
| Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 79 | python create_source_date_epoch_stamp() { | 
| Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 80 | import oe.reproducible | 
|  | 81 |  | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 82 | epochfile = d.getVar('SDE_FILE') | 
| Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 83 | tmp_file = "%s.new" % epochfile | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 84 |  | 
| Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 85 | source_date_epoch = oe.reproducible.get_source_date_epoch(d, d.getVar('S')) | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 86 |  | 
| Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 87 | bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch) | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 88 | bb.utils.mkdirhier(d.getVar('SDE_DIR')) | 
| Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 89 | with open(tmp_file, 'w') as f: | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 90 | f.write(str(source_date_epoch)) | 
| Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 91 |  | 
|  | 92 | os.rename(tmp_file, epochfile) | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
| Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 95 | def get_source_date_epoch_value(d): | 
|  | 96 | cached = d.getVar('__CACHED_SOURCE_DATE_EPOCH') | 
|  | 97 | if cached: | 
|  | 98 | return cached | 
|  | 99 |  | 
|  | 100 | epochfile = d.getVar('SDE_FILE') | 
| Andrew Geissler | 90fd73c | 2021-03-05 15:25:55 -0600 | [diff] [blame] | 101 | source_date_epoch = int(d.getVar('SOURCE_DATE_EPOCH_FALLBACK')) | 
| Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 102 | try: | 
| Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 103 | with open(epochfile, 'r') as f: | 
|  | 104 | s = f.read() | 
|  | 105 | try: | 
|  | 106 | source_date_epoch = int(s) | 
| Andrew Geissler | 90fd73c | 2021-03-05 15:25:55 -0600 | [diff] [blame] | 107 | # workaround for old sstate with SDE_FILE content being 0 - use SOURCE_DATE_EPOCH_FALLBACK | 
|  | 108 | if source_date_epoch == 0 : | 
|  | 109 | source_date_epoch = int(d.getVar('SOURCE_DATE_EPOCH_FALLBACK')) | 
|  | 110 | bb.warn("SOURCE_DATE_EPOCH value from sstate '%s' is deprecated/invalid. Reverting to SOURCE_DATE_EPOCH_FALLBACK '%s'" % (s, source_date_epoch)) | 
| Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 111 | except ValueError: | 
| Andrew Geissler | 90fd73c | 2021-03-05 15:25:55 -0600 | [diff] [blame] | 112 | bb.warn("SOURCE_DATE_EPOCH value '%s' is invalid. Reverting to SOURCE_DATE_EPOCH_FALLBACK" % s) | 
|  | 113 | source_date_epoch = int(d.getVar('SOURCE_DATE_EPOCH_FALLBACK')) | 
| Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 114 | bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch) | 
| Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 115 | except FileNotFoundError: | 
| Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 116 | bb.debug(1, "Cannot find %s. SOURCE_DATE_EPOCH will default to %d" % (epochfile, source_date_epoch)) | 
|  | 117 |  | 
|  | 118 | d.setVar('__CACHED_SOURCE_DATE_EPOCH', str(source_date_epoch)) | 
|  | 119 | return str(source_date_epoch) | 
|  | 120 |  | 
|  | 121 | export SOURCE_DATE_EPOCH ?= "${@get_source_date_epoch_value(d)}" | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 122 | BB_HASHBASE_WHITELIST += "SOURCE_DATE_EPOCH" | 
|  | 123 |  | 
|  | 124 | python () { | 
|  | 125 | if d.getVar('BUILD_REPRODUCIBLE_BINARIES') == '1': | 
| Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 126 | d.appendVarFlag("do_unpack", "postfuncs", " create_source_date_epoch_stamp") | 
| Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 127 | } |