blob: 2f3bd90b07302be6efcfc0b2738e567677fbebd5 [file] [log] [blame]
Brad Bishop316dfdd2018-06-25 12:45:53 -04001# reproducible_build.bbclass
2#
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08003# 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 Bishop316dfdd2018-06-25 12:45:53 -04007#
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08008# 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 Bishop316dfdd2018-06-25 12:45:53 -040010#
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080011# There are 4 ways we determine SOURCE_DATE_EPOCH:
Brad Bishop316dfdd2018-06-25 12:45:53 -040012#
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080013# 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 Bishop316dfdd2018-06-25 12:45:53 -040016#
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080017# 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 Bishop316dfdd2018-06-25 12:45:53 -040021#
22# 3. Use the mtime of "known" files such as NEWS, CHANGLELOG, ...
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080023# This works for well-kept repositories distributed via tarball.
Brad Bishop316dfdd2018-06-25 12:45:53 -040024#
Brad Bishopa5c52ff2018-11-23 10:55:50 +130025# 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 Bishop316dfdd2018-06-25 12:45:53 -040029#
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080030# 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 Bishop316dfdd2018-06-25 12:45:53 -040036
37BUILD_REPRODUCIBLE_BINARIES ??= '1'
38inherit ${@oe.utils.ifelse(d.getVar('BUILD_REPRODUCIBLE_BINARIES') == '1', 'reproducible_build_simple', '')}
39
40SDE_DIR ="${WORKDIR}/source-date-epoch"
41SDE_FILE = "${SDE_DIR}/__source_date_epoch.txt"
Brad Bishop00e122a2019-10-05 11:10:57 -040042SDE_DEPLOYDIR = "${WORKDIR}/deploy-source-date-epoch"
Brad Bishop316dfdd2018-06-25 12:45:53 -040043
44SSTATETASKS += "do_deploy_source_date_epoch"
45
46do_deploy_source_date_epoch () {
Brad Bishop00e122a2019-10-05 11:10:57 -040047 mkdir -p ${SDE_DEPLOYDIR}
48 if [ -e ${SDE_FILE} ]; then
Andrew Geissler82c905d2020-04-13 13:39:40 -050049 echo "Deploying SDE from ${SDE_FILE} -> ${SDE_DEPLOYDIR}."
Brad Bishop00e122a2019-10-05 11:10:57 -040050 cp -p ${SDE_FILE} ${SDE_DEPLOYDIR}/__source_date_epoch.txt
Andrew Geissler82c905d2020-04-13 13:39:40 -050051 else
52 echo "${SDE_FILE} not found!"
Brad Bishop00e122a2019-10-05 11:10:57 -040053 fi
Brad Bishop316dfdd2018-06-25 12:45:53 -040054}
55
56python do_deploy_source_date_epoch_setscene () {
57 sstate_setscene(d)
Brad Bishop00e122a2019-10-05 11:10:57 -040058 bb.utils.mkdirhier(d.getVar('SDE_DIR'))
59 sde_file = os.path.join(d.getVar('SDE_DEPLOYDIR'), '__source_date_epoch.txt')
60 if os.path.exists(sde_file):
Andrew Geissler82c905d2020-04-13 13:39:40 -050061 target = d.getVar('SDE_FILE')
62 bb.debug(1, "Moving setscene SDE file %s -> %s" % (sde_file, target))
63 os.rename(sde_file, target)
64 else:
65 bb.debug(1, "%s not found!" % sde_file)
Brad Bishop316dfdd2018-06-25 12:45:53 -040066}
67
Brad Bishop00e122a2019-10-05 11:10:57 -040068do_deploy_source_date_epoch[dirs] = "${SDE_DEPLOYDIR}"
69do_deploy_source_date_epoch[sstate-plaindirs] = "${SDE_DEPLOYDIR}"
Brad Bishop316dfdd2018-06-25 12:45:53 -040070addtask do_deploy_source_date_epoch_setscene
71addtask do_deploy_source_date_epoch before do_configure after do_patch
72
Andrew Geissler82c905d2020-04-13 13:39:40 -050073python create_source_date_epoch_stamp() {
Andrew Geisslerb7d28612020-07-24 16:15:54 -050074 import oe.reproducible
75
Brad Bishop316dfdd2018-06-25 12:45:53 -040076 epochfile = d.getVar('SDE_FILE')
Andrew Geissler82c905d2020-04-13 13:39:40 -050077 # If it exists we need to regenerate as the sources may have changed
Brad Bishop316dfdd2018-06-25 12:45:53 -040078 if os.path.isfile(epochfile):
Andrew Geissler82c905d2020-04-13 13:39:40 -050079 bb.debug(1, "Deleting existing SOURCE_DATE_EPOCH from: %s" % epochfile)
80 os.remove(epochfile)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080081
Andrew Geisslerb7d28612020-07-24 16:15:54 -050082 source_date_epoch = oe.reproducible.get_source_date_epoch(d, d.getVar('S'))
Brad Bishop316dfdd2018-06-25 12:45:53 -040083
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080084 bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch)
Brad Bishop316dfdd2018-06-25 12:45:53 -040085 bb.utils.mkdirhier(d.getVar('SDE_DIR'))
86 with open(epochfile, 'w') as f:
87 f.write(str(source_date_epoch))
88}
89
Andrew Geissler82c905d2020-04-13 13:39:40 -050090def get_source_date_epoch_value(d):
91 cached = d.getVar('__CACHED_SOURCE_DATE_EPOCH')
92 if cached:
93 return cached
94
95 epochfile = d.getVar('SDE_FILE')
96 source_date_epoch = 0
97 if os.path.isfile(epochfile):
98 with open(epochfile, 'r') as f:
99 s = f.read()
100 try:
101 source_date_epoch = int(s)
102 except ValueError:
103 bb.warn("SOURCE_DATE_EPOCH value '%s' is invalid. Reverting to 0" % s)
104 source_date_epoch = 0
105 bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch)
106 else:
107 bb.debug(1, "Cannot find %s. SOURCE_DATE_EPOCH will default to %d" % (epochfile, source_date_epoch))
108
109 d.setVar('__CACHED_SOURCE_DATE_EPOCH', str(source_date_epoch))
110 return str(source_date_epoch)
111
112export SOURCE_DATE_EPOCH ?= "${@get_source_date_epoch_value(d)}"
Brad Bishop316dfdd2018-06-25 12:45:53 -0400113BB_HASHBASE_WHITELIST += "SOURCE_DATE_EPOCH"
114
115python () {
116 if d.getVar('BUILD_REPRODUCIBLE_BINARIES') == '1':
Andrew Geissler82c905d2020-04-13 13:39:40 -0500117 d.appendVarFlag("do_unpack", "postfuncs", " create_source_date_epoch_stamp")
Brad Bishop316dfdd2018-06-25 12:45:53 -0400118}