blob: 8788ad7145cad8b2870740653c942fa44727e329 [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"
42
43SSTATETASKS += "do_deploy_source_date_epoch"
44
45do_deploy_source_date_epoch () {
46 echo "Deploying SDE to ${SDE_DIR}."
47}
48
49python do_deploy_source_date_epoch_setscene () {
50 sstate_setscene(d)
51}
52
53do_deploy_source_date_epoch[dirs] = "${SDE_DIR}"
54do_deploy_source_date_epoch[sstate-plaindirs] = "${SDE_DIR}"
55addtask do_deploy_source_date_epoch_setscene
56addtask do_deploy_source_date_epoch before do_configure after do_patch
57
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080058def get_source_date_epoch_from_known_files(d, sourcedir):
59 source_date_epoch = None
60 newest_file = None
Brad Bishop316dfdd2018-06-25 12:45:53 -040061 known_files = set(["NEWS", "ChangeLog", "Changelog", "CHANGES"])
62 for file in known_files:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080063 filepath = os.path.join(sourcedir, file)
Brad Bishop316dfdd2018-06-25 12:45:53 -040064 if os.path.isfile(filepath):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080065 mtime = int(os.lstat(filepath).st_mtime)
Brad Bishop316dfdd2018-06-25 12:45:53 -040066 # There may be more than one "known_file" present, if so, use the youngest one
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080067 if not source_date_epoch or mtime > source_date_epoch:
Brad Bishop316dfdd2018-06-25 12:45:53 -040068 source_date_epoch = mtime
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080069 newest_file = filepath
70 if newest_file:
71 bb.debug(1, "SOURCE_DATE_EPOCH taken from: %s" % newest_file)
Brad Bishop316dfdd2018-06-25 12:45:53 -040072 return source_date_epoch
73
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080074def find_git_folder(d, sourcedir):
75 # First guess: WORKDIR/git
76 # This is the default git fetcher unpack path
77 workdir = d.getVar('WORKDIR')
78 gitpath = os.path.join(workdir, "git/.git")
79 if os.path.isdir(gitpath):
80 return gitpath
81
82 # Second guess: ${S}
83 gitpath = os.path.join(sourcedir, ".git")
84 if os.path.isdir(gitpath):
85 return gitpath
86
87 # Perhaps there was a subpath or destsuffix specified.
88 # Go looking in the WORKDIR
89 exclude = set(["build", "image", "license-destdir", "patches", "pseudo",
90 "recipe-sysroot", "recipe-sysroot-native", "sysroot-destdir", "temp"])
91 for root, dirs, files in os.walk(workdir, topdown=True):
Brad Bishop316dfdd2018-06-25 12:45:53 -040092 dirs[:] = [d for d in dirs if d not in exclude]
93 if '.git' in dirs:
Brad Bishop316dfdd2018-06-25 12:45:53 -040094 return root
Brad Bishop316dfdd2018-06-25 12:45:53 -040095
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080096 bb.warn("Failed to find a git repository in WORKDIR: %s" % workdir)
97 return None
98
99def get_source_date_epoch_from_git(d, sourcedir):
100 source_date_epoch = None
101 if "git://" in d.getVar('SRC_URI'):
102 gitpath = find_git_folder(d, sourcedir)
103 if gitpath:
104 import subprocess
105 source_date_epoch = int(subprocess.check_output(['git','log','-1','--pretty=%ct'], cwd=gitpath))
106 bb.debug(1, "git repository: %s" % gitpath)
107 return source_date_epoch
108
109def get_source_date_epoch_from_youngest_file(d, sourcedir):
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300110 if sourcedir == d.getVar('WORKDIR'):
111 # These sources are almost certainly not from a tarball
112 return None
113
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800114 # Do it the hard way: check all files and find the youngest one...
115 source_date_epoch = None
116 newest_file = None
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800117 for root, dirs, files in os.walk(sourcedir, topdown=True):
118 files = [f for f in files if not f[0] == '.']
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800119
120 for fname in files:
121 filename = os.path.join(root, fname)
122 try:
123 mtime = int(os.lstat(filename).st_mtime)
124 except ValueError:
125 mtime = 0
126 if not source_date_epoch or mtime > source_date_epoch:
127 source_date_epoch = mtime
128 newest_file = filename
129
130 if newest_file:
131 bb.debug(1, "Newest file found: %s" % newest_file)
132 return source_date_epoch
133
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300134def fixed_source_date_epoch():
135 bb.debug(1, "No tarball or git repo found to determine SOURCE_DATE_EPOCH")
136 return 0
137
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800138python do_create_source_date_epoch_stamp() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400139 epochfile = d.getVar('SDE_FILE')
140 if os.path.isfile(epochfile):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800141 bb.debug(1, "Reusing SOURCE_DATE_EPOCH from: %s" % epochfile)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400142 return
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800143
144 sourcedir = d.getVar('S')
145 source_date_epoch = (
146 get_source_date_epoch_from_git(d, sourcedir) or
147 get_source_date_epoch_from_known_files(d, sourcedir) or
148 get_source_date_epoch_from_youngest_file(d, sourcedir) or
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300149 fixed_source_date_epoch() # Last resort
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800150 )
Brad Bishop316dfdd2018-06-25 12:45:53 -0400151
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800152 bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400153 bb.utils.mkdirhier(d.getVar('SDE_DIR'))
154 with open(epochfile, 'w') as f:
155 f.write(str(source_date_epoch))
156}
157
158BB_HASHBASE_WHITELIST += "SOURCE_DATE_EPOCH"
159
160python () {
161 if d.getVar('BUILD_REPRODUCIBLE_BINARIES') == '1':
162 d.appendVarFlag("do_unpack", "postfuncs", " do_create_source_date_epoch_stamp")
163 epochfile = d.getVar('SDE_FILE')
164 source_date_epoch = "0"
165 if os.path.isfile(epochfile):
166 with open(epochfile, 'r') as f:
167 source_date_epoch = f.read()
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800168 bb.debug(1, "SOURCE_DATE_EPOCH: %s" % source_date_epoch)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400169 d.setVar('SOURCE_DATE_EPOCH', source_date_epoch)
170}