Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 1 | # reproducible_build.bbclass |
| 2 | # |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame^] | 3 | # Sets the default SOURCE_DATE_EPOCH in each component's build environment. |
| 4 | # The format is number of seconds since the system epoch. |
| 5 | # |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 6 | # Upstream components (generally) respect this environment variable, |
| 7 | # using it in place of the "current" date and time. |
| 8 | # See https://reproducible-builds.org/specs/source-date-epoch/ |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 9 | # |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame^] | 10 | # The default value of SOURCE_DATE_EPOCH comes from the function |
| 11 | # get_source_date_epoch_value which reads from the SDE_FILE, or if the file |
| 12 | # is not available (or set to 0) will use the fallback of |
| 13 | # SOURCE_DATE_EPOCH_FALLBACK. |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 14 | # |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame^] | 15 | # The SDE_FILE is normally constructed from the function |
| 16 | # create_source_date_epoch_stamp which is typically added as a postfuncs to |
| 17 | # the do_unpack task. If a recipe does NOT have do_unpack, it should be added |
| 18 | # to a task that runs after the source is available and before the |
| 19 | # do_deploy_source_date_epoch task is executed. |
| 20 | # |
| 21 | # If a recipe wishes to override the default behavior it should set it's own |
| 22 | # SOURCE_DATE_EPOCH or override the do_deploy_source_date_epoch_stamp task |
| 23 | # with recipe-specific functionality to write the appropriate |
| 24 | # SOURCE_DATE_EPOCH into the SDE_FILE. |
| 25 | # |
| 26 | # SOURCE_DATE_EPOCH is intended to be a reproducible value. This value should |
| 27 | # be reproducible for anyone who builds the same revision from the same |
| 28 | # sources. |
| 29 | # |
| 30 | # There are 4 ways the create_source_date_epoch_stamp function determines what |
| 31 | # becomes SOURCE_DATE_EPOCH: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 32 | # |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 33 | # 1. Use the value from __source_date_epoch.txt file if this file exists. |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame^] | 34 | # This file was most likely created in the previous build by one of the |
| 35 | # following methods 2,3,4. |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 36 | # Alternatively, it can be provided by a recipe via SRC_URI. |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 37 | # |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 38 | # If the file does not exist: |
| 39 | # |
| 40 | # 2. If there is a git checkout, use the last git commit timestamp. |
| 41 | # Git does not preserve file timestamps on checkout. |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 42 | # |
| 43 | # 3. Use the mtime of "known" files such as NEWS, CHANGLELOG, ... |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 44 | # This works for well-kept repositories distributed via tarball. |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 45 | # |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame^] | 46 | # 4. Use the modification time of the youngest file in the source tree, if |
| 47 | # there is one. |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 48 | # This will be the newest file from the distribution tarball, if any. |
| 49 | # |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame^] | 50 | # 5. Fall back to a fixed timestamp (SOURCE_DATE_EPOCH_FALLBACK). |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 51 | # |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame^] | 52 | # Once the value is determined, it is stored in the recipe's SDE_FILE. |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 53 | |
| 54 | BUILD_REPRODUCIBLE_BINARIES ??= '1' |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame^] | 55 | inherit reproducible_build_simple |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 56 | |
Andrew Geissler | 90fd73c | 2021-03-05 15:25:55 -0600 | [diff] [blame] | 57 | SDE_DIR = "${WORKDIR}/source-date-epoch" |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 58 | SDE_FILE = "${SDE_DIR}/__source_date_epoch.txt" |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 59 | SDE_DEPLOYDIR = "${WORKDIR}/deploy-source-date-epoch" |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 60 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 61 | # Enable compiler warning when the __TIME__, __DATE__ and __TIMESTAMP__ macros are used. |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 62 | TARGET_CC_ARCH:append:class-target = " -Wdate-time" |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 63 | |
Andrew Geissler | 90fd73c | 2021-03-05 15:25:55 -0600 | [diff] [blame] | 64 | # A SOURCE_DATE_EPOCH of '0' might be misinterpreted as no SDE |
| 65 | export SOURCE_DATE_EPOCH_FALLBACK ??= "1302044400" |
| 66 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 67 | SSTATETASKS += "do_deploy_source_date_epoch" |
| 68 | |
| 69 | do_deploy_source_date_epoch () { |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 70 | mkdir -p ${SDE_DEPLOYDIR} |
| 71 | if [ -e ${SDE_FILE} ]; then |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 72 | echo "Deploying SDE from ${SDE_FILE} -> ${SDE_DEPLOYDIR}." |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 73 | cp -p ${SDE_FILE} ${SDE_DEPLOYDIR}/__source_date_epoch.txt |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 74 | else |
| 75 | echo "${SDE_FILE} not found!" |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 76 | fi |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | python do_deploy_source_date_epoch_setscene () { |
| 80 | sstate_setscene(d) |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 81 | bb.utils.mkdirhier(d.getVar('SDE_DIR')) |
| 82 | sde_file = os.path.join(d.getVar('SDE_DEPLOYDIR'), '__source_date_epoch.txt') |
| 83 | if os.path.exists(sde_file): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 84 | target = d.getVar('SDE_FILE') |
| 85 | bb.debug(1, "Moving setscene SDE file %s -> %s" % (sde_file, target)) |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 86 | bb.utils.rename(sde_file, target) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 87 | else: |
| 88 | bb.debug(1, "%s not found!" % sde_file) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 91 | do_deploy_source_date_epoch[dirs] = "${SDE_DEPLOYDIR}" |
| 92 | do_deploy_source_date_epoch[sstate-plaindirs] = "${SDE_DEPLOYDIR}" |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 93 | addtask do_deploy_source_date_epoch_setscene |
| 94 | addtask do_deploy_source_date_epoch before do_configure after do_patch |
| 95 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 96 | python create_source_date_epoch_stamp() { |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 97 | import oe.reproducible |
| 98 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 99 | epochfile = d.getVar('SDE_FILE') |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 100 | tmp_file = "%s.new" % epochfile |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 101 | |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 102 | 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] | 103 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 104 | bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 105 | bb.utils.mkdirhier(d.getVar('SDE_DIR')) |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 106 | with open(tmp_file, 'w') as f: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 107 | f.write(str(source_date_epoch)) |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 108 | |
| 109 | os.rename(tmp_file, epochfile) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame^] | 112 | EPOCHTASK = "do_deploy_source_date_epoch" |
| 113 | |
| 114 | # Generate the stamp after do_unpack runs |
| 115 | do_unpack[postfuncs] += "create_source_date_epoch_stamp" |
| 116 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 117 | def get_source_date_epoch_value(d): |
| 118 | cached = d.getVar('__CACHED_SOURCE_DATE_EPOCH') |
| 119 | if cached: |
| 120 | return cached |
| 121 | |
| 122 | epochfile = d.getVar('SDE_FILE') |
Andrew Geissler | 90fd73c | 2021-03-05 15:25:55 -0600 | [diff] [blame] | 123 | source_date_epoch = int(d.getVar('SOURCE_DATE_EPOCH_FALLBACK')) |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 124 | try: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 125 | with open(epochfile, 'r') as f: |
| 126 | s = f.read() |
| 127 | try: |
| 128 | source_date_epoch = int(s) |
Andrew Geissler | 90fd73c | 2021-03-05 15:25:55 -0600 | [diff] [blame] | 129 | # workaround for old sstate with SDE_FILE content being 0 - use SOURCE_DATE_EPOCH_FALLBACK |
| 130 | if source_date_epoch == 0 : |
| 131 | source_date_epoch = int(d.getVar('SOURCE_DATE_EPOCH_FALLBACK')) |
| 132 | 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] | 133 | except ValueError: |
Andrew Geissler | 90fd73c | 2021-03-05 15:25:55 -0600 | [diff] [blame] | 134 | bb.warn("SOURCE_DATE_EPOCH value '%s' is invalid. Reverting to SOURCE_DATE_EPOCH_FALLBACK" % s) |
| 135 | source_date_epoch = int(d.getVar('SOURCE_DATE_EPOCH_FALLBACK')) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 136 | bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch) |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 137 | except FileNotFoundError: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 138 | bb.debug(1, "Cannot find %s. SOURCE_DATE_EPOCH will default to %d" % (epochfile, source_date_epoch)) |
| 139 | |
| 140 | d.setVar('__CACHED_SOURCE_DATE_EPOCH', str(source_date_epoch)) |
| 141 | return str(source_date_epoch) |
| 142 | |
| 143 | export SOURCE_DATE_EPOCH ?= "${@get_source_date_epoch_value(d)}" |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 144 | BB_HASHBASE_WHITELIST += "SOURCE_DATE_EPOCH" |