blob: 7571c116c82eb01c04f9d8b44c299b47a09b2b19 [file] [log] [blame]
Brad Bishop316dfdd2018-06-25 12:45:53 -04001# reproducible_build.bbclass
2#
Andrew Geissler5199d832021-09-24 16:47:35 -05003# 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 Bishop1a4b7ee2018-12-16 17:11:34 -08006# 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 Bishop316dfdd2018-06-25 12:45:53 -04009#
Andrew Geissler5199d832021-09-24 16:47:35 -050010# 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 Bishop316dfdd2018-06-25 12:45:53 -040014#
Andrew Geissler5199d832021-09-24 16:47:35 -050015# 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 Bishop316dfdd2018-06-25 12:45:53 -040032#
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080033# 1. Use the value from __source_date_epoch.txt file if this file exists.
Andrew Geissler5199d832021-09-24 16:47:35 -050034# This file was most likely created in the previous build by one of the
35# following methods 2,3,4.
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080036# Alternatively, it can be provided by a recipe via SRC_URI.
Brad Bishop316dfdd2018-06-25 12:45:53 -040037#
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080038# 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 Bishop316dfdd2018-06-25 12:45:53 -040042#
43# 3. Use the mtime of "known" files such as NEWS, CHANGLELOG, ...
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080044# This works for well-kept repositories distributed via tarball.
Brad Bishop316dfdd2018-06-25 12:45:53 -040045#
Andrew Geissler5199d832021-09-24 16:47:35 -050046# 4. Use the modification time of the youngest file in the source tree, if
47# there is one.
Brad Bishopa5c52ff2018-11-23 10:55:50 +130048# This will be the newest file from the distribution tarball, if any.
49#
Andrew Geissler5199d832021-09-24 16:47:35 -050050# 5. Fall back to a fixed timestamp (SOURCE_DATE_EPOCH_FALLBACK).
Brad Bishop316dfdd2018-06-25 12:45:53 -040051#
Andrew Geissler5199d832021-09-24 16:47:35 -050052# Once the value is determined, it is stored in the recipe's SDE_FILE.
Brad Bishop316dfdd2018-06-25 12:45:53 -040053
54BUILD_REPRODUCIBLE_BINARIES ??= '1'
Andrew Geissler5199d832021-09-24 16:47:35 -050055inherit reproducible_build_simple
Brad Bishop316dfdd2018-06-25 12:45:53 -040056
Andrew Geissler90fd73c2021-03-05 15:25:55 -060057SDE_DIR = "${WORKDIR}/source-date-epoch"
Brad Bishop316dfdd2018-06-25 12:45:53 -040058SDE_FILE = "${SDE_DIR}/__source_date_epoch.txt"
Brad Bishop00e122a2019-10-05 11:10:57 -040059SDE_DEPLOYDIR = "${WORKDIR}/deploy-source-date-epoch"
Brad Bishop316dfdd2018-06-25 12:45:53 -040060
Andrew Geisslerc926e172021-05-07 16:11:35 -050061# Enable compiler warning when the __TIME__, __DATE__ and __TIMESTAMP__ macros are used.
Patrick Williams213cb262021-08-07 19:21:33 -050062TARGET_CC_ARCH:append:class-target = " -Wdate-time"
Andrew Geisslerc926e172021-05-07 16:11:35 -050063
Andrew Geissler90fd73c2021-03-05 15:25:55 -060064# A SOURCE_DATE_EPOCH of '0' might be misinterpreted as no SDE
65export SOURCE_DATE_EPOCH_FALLBACK ??= "1302044400"
66
Brad Bishop316dfdd2018-06-25 12:45:53 -040067SSTATETASKS += "do_deploy_source_date_epoch"
68
69do_deploy_source_date_epoch () {
Brad Bishop00e122a2019-10-05 11:10:57 -040070 mkdir -p ${SDE_DEPLOYDIR}
71 if [ -e ${SDE_FILE} ]; then
Andrew Geissler82c905d2020-04-13 13:39:40 -050072 echo "Deploying SDE from ${SDE_FILE} -> ${SDE_DEPLOYDIR}."
Brad Bishop00e122a2019-10-05 11:10:57 -040073 cp -p ${SDE_FILE} ${SDE_DEPLOYDIR}/__source_date_epoch.txt
Andrew Geissler82c905d2020-04-13 13:39:40 -050074 else
75 echo "${SDE_FILE} not found!"
Brad Bishop00e122a2019-10-05 11:10:57 -040076 fi
Brad Bishop316dfdd2018-06-25 12:45:53 -040077}
78
79python do_deploy_source_date_epoch_setscene () {
80 sstate_setscene(d)
Brad Bishop00e122a2019-10-05 11:10:57 -040081 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 Geissler82c905d2020-04-13 13:39:40 -050084 target = d.getVar('SDE_FILE')
85 bb.debug(1, "Moving setscene SDE file %s -> %s" % (sde_file, target))
Andrew Geisslerc926e172021-05-07 16:11:35 -050086 bb.utils.rename(sde_file, target)
Andrew Geissler82c905d2020-04-13 13:39:40 -050087 else:
88 bb.debug(1, "%s not found!" % sde_file)
Brad Bishop316dfdd2018-06-25 12:45:53 -040089}
90
Brad Bishop00e122a2019-10-05 11:10:57 -040091do_deploy_source_date_epoch[dirs] = "${SDE_DEPLOYDIR}"
92do_deploy_source_date_epoch[sstate-plaindirs] = "${SDE_DEPLOYDIR}"
Brad Bishop316dfdd2018-06-25 12:45:53 -040093addtask do_deploy_source_date_epoch_setscene
94addtask do_deploy_source_date_epoch before do_configure after do_patch
95
Andrew Geissler82c905d2020-04-13 13:39:40 -050096python create_source_date_epoch_stamp() {
Andrew Geisslerb7d28612020-07-24 16:15:54 -050097 import oe.reproducible
98
Brad Bishop316dfdd2018-06-25 12:45:53 -040099 epochfile = d.getVar('SDE_FILE')
Andrew Geissler09036742021-06-25 14:25:14 -0500100 tmp_file = "%s.new" % epochfile
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800101
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500102 source_date_epoch = oe.reproducible.get_source_date_epoch(d, d.getVar('S'))
Brad Bishop316dfdd2018-06-25 12:45:53 -0400103
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800104 bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400105 bb.utils.mkdirhier(d.getVar('SDE_DIR'))
Andrew Geissler09036742021-06-25 14:25:14 -0500106 with open(tmp_file, 'w') as f:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400107 f.write(str(source_date_epoch))
Andrew Geissler09036742021-06-25 14:25:14 -0500108
109 os.rename(tmp_file, epochfile)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400110}
111
Andrew Geissler5199d832021-09-24 16:47:35 -0500112EPOCHTASK = "do_deploy_source_date_epoch"
113
114# Generate the stamp after do_unpack runs
115do_unpack[postfuncs] += "create_source_date_epoch_stamp"
116
Andrew Geissler82c905d2020-04-13 13:39:40 -0500117def get_source_date_epoch_value(d):
Patrick Williams93c203f2021-10-06 16:15:23 -0500118 epochfile = d.getVar('SDE_FILE')
119 cached, efile = d.getVar('__CACHED_SOURCE_DATE_EPOCH') or (None, None)
120 if cached and efile == epochfile:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500121 return cached
122
Patrick Williams93c203f2021-10-06 16:15:23 -0500123 if cached and epochfile != efile:
124 bb.debug(1, "Epoch file changed from %s to %s" % (efile, epochfile))
125
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600126 source_date_epoch = int(d.getVar('SOURCE_DATE_EPOCH_FALLBACK'))
Andrew Geissler09036742021-06-25 14:25:14 -0500127 try:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500128 with open(epochfile, 'r') as f:
129 s = f.read()
130 try:
131 source_date_epoch = int(s)
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600132 # workaround for old sstate with SDE_FILE content being 0 - use SOURCE_DATE_EPOCH_FALLBACK
133 if source_date_epoch == 0 :
134 source_date_epoch = int(d.getVar('SOURCE_DATE_EPOCH_FALLBACK'))
135 bb.warn("SOURCE_DATE_EPOCH value from sstate '%s' is deprecated/invalid. Reverting to SOURCE_DATE_EPOCH_FALLBACK '%s'" % (s, source_date_epoch))
Andrew Geissler82c905d2020-04-13 13:39:40 -0500136 except ValueError:
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600137 bb.warn("SOURCE_DATE_EPOCH value '%s' is invalid. Reverting to SOURCE_DATE_EPOCH_FALLBACK" % s)
138 source_date_epoch = int(d.getVar('SOURCE_DATE_EPOCH_FALLBACK'))
Andrew Geissler82c905d2020-04-13 13:39:40 -0500139 bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch)
Andrew Geissler09036742021-06-25 14:25:14 -0500140 except FileNotFoundError:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500141 bb.debug(1, "Cannot find %s. SOURCE_DATE_EPOCH will default to %d" % (epochfile, source_date_epoch))
142
Patrick Williams93c203f2021-10-06 16:15:23 -0500143 d.setVar('__CACHED_SOURCE_DATE_EPOCH', (str(source_date_epoch), epochfile))
Andrew Geissler82c905d2020-04-13 13:39:40 -0500144 return str(source_date_epoch)
145
146export SOURCE_DATE_EPOCH ?= "${@get_source_date_epoch_value(d)}"
Brad Bishop316dfdd2018-06-25 12:45:53 -0400147BB_HASHBASE_WHITELIST += "SOURCE_DATE_EPOCH"