blob: 89f645b8584ee37a049b97f5aee843e51cd8405f [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):
118 cached = d.getVar('__CACHED_SOURCE_DATE_EPOCH')
119 if cached:
120 return cached
121
122 epochfile = d.getVar('SDE_FILE')
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600123 source_date_epoch = int(d.getVar('SOURCE_DATE_EPOCH_FALLBACK'))
Andrew Geissler09036742021-06-25 14:25:14 -0500124 try:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500125 with open(epochfile, 'r') as f:
126 s = f.read()
127 try:
128 source_date_epoch = int(s)
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600129 # 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 Geissler82c905d2020-04-13 13:39:40 -0500133 except ValueError:
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600134 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 Geissler82c905d2020-04-13 13:39:40 -0500136 bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch)
Andrew Geissler09036742021-06-25 14:25:14 -0500137 except FileNotFoundError:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500138 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
143export SOURCE_DATE_EPOCH ?= "${@get_source_date_epoch_value(d)}"
Brad Bishop316dfdd2018-06-25 12:45:53 -0400144BB_HASHBASE_WHITELIST += "SOURCE_DATE_EPOCH"