summaryrefslogtreecommitdiffstats
path: root/meta/classes/reproducible_build.bbclass
diff options
context:
space:
mode:
Diffstat (limited to 'meta/classes/reproducible_build.bbclass')
-rw-r--r--meta/classes/reproducible_build.bbclass90
1 files changed, 56 insertions, 34 deletions
diff --git a/meta/classes/reproducible_build.bbclass b/meta/classes/reproducible_build.bbclass
index 2f3bd90b07..3c01dbd5b3 100644
--- a/meta/classes/reproducible_build.bbclass
+++ b/meta/classes/reproducible_build.bbclass
@@ -1,17 +1,38 @@
1# reproducible_build.bbclass 1# reproducible_build.bbclass
2# 2#
3# Sets SOURCE_DATE_EPOCH in each component's build environment. 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#
4# Upstream components (generally) respect this environment variable, 6# Upstream components (generally) respect this environment variable,
5# using it in place of the "current" date and time. 7# using it in place of the "current" date and time.
6# See https://reproducible-builds.org/specs/source-date-epoch/ 8# See https://reproducible-builds.org/specs/source-date-epoch/
7# 9#
8# After sources are unpacked but before they are patched, we set a reproducible value for SOURCE_DATE_EPOCH. 10# The default value of SOURCE_DATE_EPOCH comes from the function
9# This value should be reproducible for anyone who builds the same revision from the same sources. 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.
14#
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.
10# 29#
11# There are 4 ways we determine SOURCE_DATE_EPOCH: 30# There are 4 ways the create_source_date_epoch_stamp function determines what
31# becomes SOURCE_DATE_EPOCH:
12# 32#
13# 1. Use the value from __source_date_epoch.txt file if this file exists. 33# 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. 34# This file was most likely created in the previous build by one of the
35# following methods 2,3,4.
15# Alternatively, it can be provided by a recipe via SRC_URI. 36# Alternatively, it can be provided by a recipe via SRC_URI.
16# 37#
17# If the file does not exist: 38# If the file does not exist:
@@ -22,25 +43,24 @@
22# 3. Use the mtime of "known" files such as NEWS, CHANGLELOG, ... 43# 3. Use the mtime of "known" files such as NEWS, CHANGLELOG, ...
23# This works for well-kept repositories distributed via tarball. 44# This works for well-kept repositories distributed via tarball.
24# 45#
25# 4. Use the modification time of the youngest file in the source tree, if there is one. 46# 4. Use the modification time of the youngest file in the source tree, if
47# there is one.
26# This will be the newest file from the distribution tarball, if any. 48# This will be the newest file from the distribution tarball, if any.
27# 49#
28# 5. Fall back to a fixed timestamp. 50# 5. Fall back to a fixed timestamp (SOURCE_DATE_EPOCH_FALLBACK).
29# 51#
30# Once the value of SOURCE_DATE_EPOCH is determined, it is stored in the recipe's SDE_FILE. 52# Once the value 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, ...)
36 53
37BUILD_REPRODUCIBLE_BINARIES ??= '1' 54BUILD_REPRODUCIBLE_BINARIES ??= '1'
38inherit ${@oe.utils.ifelse(d.getVar('BUILD_REPRODUCIBLE_BINARIES') == '1', 'reproducible_build_simple', '')} 55inherit reproducible_build_simple
39 56
40SDE_DIR ="${WORKDIR}/source-date-epoch" 57SDE_DIR = "${WORKDIR}/source-date-epoch"
41SDE_FILE = "${SDE_DIR}/__source_date_epoch.txt" 58SDE_FILE = "${SDE_DIR}/__source_date_epoch.txt"
42SDE_DEPLOYDIR = "${WORKDIR}/deploy-source-date-epoch" 59SDE_DEPLOYDIR = "${WORKDIR}/deploy-source-date-epoch"
43 60
61# A SOURCE_DATE_EPOCH of '0' might be misinterpreted as no SDE
62export SOURCE_DATE_EPOCH_FALLBACK ??= "1302044400"
63
44SSTATETASKS += "do_deploy_source_date_epoch" 64SSTATETASKS += "do_deploy_source_date_epoch"
45 65
46do_deploy_source_date_epoch () { 66do_deploy_source_date_epoch () {
@@ -74,45 +94,47 @@ python create_source_date_epoch_stamp() {
74 import oe.reproducible 94 import oe.reproducible
75 95
76 epochfile = d.getVar('SDE_FILE') 96 epochfile = d.getVar('SDE_FILE')
77 # If it exists we need to regenerate as the sources may have changed 97 tmp_file = "%s.new" % epochfile
78 if os.path.isfile(epochfile):
79 bb.debug(1, "Deleting existing SOURCE_DATE_EPOCH from: %s" % epochfile)
80 os.remove(epochfile)
81 98
82 source_date_epoch = oe.reproducible.get_source_date_epoch(d, d.getVar('S')) 99 source_date_epoch = oe.reproducible.get_source_date_epoch(d, d.getVar('S'))
83 100
84 bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch) 101 bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch)
85 bb.utils.mkdirhier(d.getVar('SDE_DIR')) 102 bb.utils.mkdirhier(d.getVar('SDE_DIR'))
86 with open(epochfile, 'w') as f: 103 with open(tmp_file, 'w') as f:
87 f.write(str(source_date_epoch)) 104 f.write(str(source_date_epoch))
105
106 os.rename(tmp_file, epochfile)
88} 107}
89 108
109EPOCHTASK = "do_deploy_source_date_epoch"
110
111# Generate the stamp after do_unpack runs
112do_unpack[postfuncs] += "create_source_date_epoch_stamp"
113
90def get_source_date_epoch_value(d): 114def get_source_date_epoch_value(d):
91 cached = d.getVar('__CACHED_SOURCE_DATE_EPOCH') 115 epochfile = d.getVar('SDE_FILE')
92 if cached: 116 cached, efile = d.getVar('__CACHED_SOURCE_DATE_EPOCH') or (None, None)
117 if cached and efile == epochfile:
93 return cached 118 return cached
94 119
95 epochfile = d.getVar('SDE_FILE') 120 if cached and epochfile != efile:
96 source_date_epoch = 0 121 bb.debug(1, "Epoch file changed from %s to %s" % (efile, epochfile))
97 if os.path.isfile(epochfile): 122
123 source_date_epoch = int(d.getVar('SOURCE_DATE_EPOCH_FALLBACK'))
124 try:
98 with open(epochfile, 'r') as f: 125 with open(epochfile, 'r') as f:
99 s = f.read() 126 s = f.read()
100 try: 127 try:
101 source_date_epoch = int(s) 128 source_date_epoch = int(s)
102 except ValueError: 129 except ValueError:
103 bb.warn("SOURCE_DATE_EPOCH value '%s' is invalid. Reverting to 0" % s) 130 bb.warn("SOURCE_DATE_EPOCH value '%s' is invalid. Reverting to SOURCE_DATE_EPOCH_FALLBACK" % s)
104 source_date_epoch = 0 131 source_date_epoch = int(d.getVar('SOURCE_DATE_EPOCH_FALLBACK'))
105 bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch) 132 bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch)
106 else: 133 except FileNotFoundError:
107 bb.debug(1, "Cannot find %s. SOURCE_DATE_EPOCH will default to %d" % (epochfile, source_date_epoch)) 134 bb.debug(1, "Cannot find %s. SOURCE_DATE_EPOCH will default to %d" % (epochfile, source_date_epoch))
108 135
109 d.setVar('__CACHED_SOURCE_DATE_EPOCH', str(source_date_epoch)) 136 d.setVar('__CACHED_SOURCE_DATE_EPOCH', (str(source_date_epoch), epochfile))
110 return str(source_date_epoch) 137 return str(source_date_epoch)
111 138
112export SOURCE_DATE_EPOCH ?= "${@get_source_date_epoch_value(d)}" 139export SOURCE_DATE_EPOCH ?= "${@get_source_date_epoch_value(d)}"
113BB_HASHBASE_WHITELIST += "SOURCE_DATE_EPOCH" 140BB_HASHBASE_WHITELIST += "SOURCE_DATE_EPOCH"
114
115python () {
116 if d.getVar('BUILD_REPRODUCIBLE_BINARIES') == '1':
117 d.appendVarFlag("do_unpack", "postfuncs", " create_source_date_epoch_stamp")
118}