Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 1 | # reproducible_build.bbclass |
| 2 | # |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 3 | # 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 Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 7 | # |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 8 | # 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 Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 10 | # |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 11 | # There are 4 ways we determine SOURCE_DATE_EPOCH: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 12 | # |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 13 | # 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 Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 16 | # |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 17 | # 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 Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 21 | # |
| 22 | # 3. Use the mtime of "known" files such as NEWS, CHANGLELOG, ... |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 23 | # This works for well-kept repositories distributed via tarball. |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 24 | # |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 25 | # 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 Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 29 | # |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 30 | # 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 Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 36 | |
| 37 | BUILD_REPRODUCIBLE_BINARIES ??= '1' |
| 38 | inherit ${@oe.utils.ifelse(d.getVar('BUILD_REPRODUCIBLE_BINARIES') == '1', 'reproducible_build_simple', '')} |
| 39 | |
| 40 | SDE_DIR ="${WORKDIR}/source-date-epoch" |
| 41 | SDE_FILE = "${SDE_DIR}/__source_date_epoch.txt" |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 42 | SDE_DEPLOYDIR = "${WORKDIR}/deploy-source-date-epoch" |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 43 | |
| 44 | SSTATETASKS += "do_deploy_source_date_epoch" |
| 45 | |
| 46 | do_deploy_source_date_epoch () { |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 47 | mkdir -p ${SDE_DEPLOYDIR} |
| 48 | if [ -e ${SDE_FILE} ]; then |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 49 | echo "Deploying SDE from ${SDE_FILE} -> ${SDE_DEPLOYDIR}." |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 50 | cp -p ${SDE_FILE} ${SDE_DEPLOYDIR}/__source_date_epoch.txt |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 51 | else |
| 52 | echo "${SDE_FILE} not found!" |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 53 | fi |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | python do_deploy_source_date_epoch_setscene () { |
| 57 | sstate_setscene(d) |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 58 | 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 Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 61 | 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 Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 68 | do_deploy_source_date_epoch[dirs] = "${SDE_DEPLOYDIR}" |
| 69 | do_deploy_source_date_epoch[sstate-plaindirs] = "${SDE_DEPLOYDIR}" |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 70 | addtask do_deploy_source_date_epoch_setscene |
| 71 | addtask do_deploy_source_date_epoch before do_configure after do_patch |
| 72 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 73 | def get_source_date_epoch_from_known_files(d, sourcedir): |
| 74 | source_date_epoch = None |
| 75 | newest_file = None |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 76 | known_files = set(["NEWS", "ChangeLog", "Changelog", "CHANGES"]) |
| 77 | for file in known_files: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 78 | filepath = os.path.join(sourcedir, file) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 79 | if os.path.isfile(filepath): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 80 | mtime = int(os.lstat(filepath).st_mtime) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 81 | # There may be more than one "known_file" present, if so, use the youngest one |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 82 | if not source_date_epoch or mtime > source_date_epoch: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 83 | source_date_epoch = mtime |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 84 | newest_file = filepath |
| 85 | if newest_file: |
| 86 | bb.debug(1, "SOURCE_DATE_EPOCH taken from: %s" % newest_file) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 87 | return source_date_epoch |
| 88 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 89 | def find_git_folder(d, sourcedir): |
| 90 | # First guess: WORKDIR/git |
| 91 | # This is the default git fetcher unpack path |
| 92 | workdir = d.getVar('WORKDIR') |
| 93 | gitpath = os.path.join(workdir, "git/.git") |
| 94 | if os.path.isdir(gitpath): |
| 95 | return gitpath |
| 96 | |
| 97 | # Second guess: ${S} |
| 98 | gitpath = os.path.join(sourcedir, ".git") |
| 99 | if os.path.isdir(gitpath): |
| 100 | return gitpath |
| 101 | |
| 102 | # Perhaps there was a subpath or destsuffix specified. |
| 103 | # Go looking in the WORKDIR |
| 104 | exclude = set(["build", "image", "license-destdir", "patches", "pseudo", |
| 105 | "recipe-sysroot", "recipe-sysroot-native", "sysroot-destdir", "temp"]) |
| 106 | for root, dirs, files in os.walk(workdir, topdown=True): |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 107 | dirs[:] = [d for d in dirs if d not in exclude] |
| 108 | if '.git' in dirs: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 109 | return root |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 110 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 111 | bb.warn("Failed to find a git repository in WORKDIR: %s" % workdir) |
| 112 | return None |
| 113 | |
| 114 | def get_source_date_epoch_from_git(d, sourcedir): |
| 115 | source_date_epoch = None |
| 116 | if "git://" in d.getVar('SRC_URI'): |
| 117 | gitpath = find_git_folder(d, sourcedir) |
| 118 | if gitpath: |
| 119 | import subprocess |
| 120 | source_date_epoch = int(subprocess.check_output(['git','log','-1','--pretty=%ct'], cwd=gitpath)) |
| 121 | bb.debug(1, "git repository: %s" % gitpath) |
| 122 | return source_date_epoch |
| 123 | |
| 124 | def get_source_date_epoch_from_youngest_file(d, sourcedir): |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 125 | if sourcedir == d.getVar('WORKDIR'): |
| 126 | # These sources are almost certainly not from a tarball |
| 127 | return None |
| 128 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 129 | # Do it the hard way: check all files and find the youngest one... |
| 130 | source_date_epoch = None |
| 131 | newest_file = None |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 132 | for root, dirs, files in os.walk(sourcedir, topdown=True): |
| 133 | files = [f for f in files if not f[0] == '.'] |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 134 | |
| 135 | for fname in files: |
| 136 | filename = os.path.join(root, fname) |
| 137 | try: |
| 138 | mtime = int(os.lstat(filename).st_mtime) |
| 139 | except ValueError: |
| 140 | mtime = 0 |
| 141 | if not source_date_epoch or mtime > source_date_epoch: |
| 142 | source_date_epoch = mtime |
| 143 | newest_file = filename |
| 144 | |
| 145 | if newest_file: |
| 146 | bb.debug(1, "Newest file found: %s" % newest_file) |
| 147 | return source_date_epoch |
| 148 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 149 | def fixed_source_date_epoch(): |
| 150 | bb.debug(1, "No tarball or git repo found to determine SOURCE_DATE_EPOCH") |
| 151 | return 0 |
| 152 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 153 | python create_source_date_epoch_stamp() { |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 154 | epochfile = d.getVar('SDE_FILE') |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 155 | # If it exists we need to regenerate as the sources may have changed |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 156 | if os.path.isfile(epochfile): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 157 | bb.debug(1, "Deleting existing SOURCE_DATE_EPOCH from: %s" % epochfile) |
| 158 | os.remove(epochfile) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 159 | |
| 160 | sourcedir = d.getVar('S') |
| 161 | source_date_epoch = ( |
| 162 | get_source_date_epoch_from_git(d, sourcedir) or |
| 163 | get_source_date_epoch_from_known_files(d, sourcedir) or |
| 164 | get_source_date_epoch_from_youngest_file(d, sourcedir) or |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 165 | fixed_source_date_epoch() # Last resort |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 166 | ) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 167 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 168 | bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 169 | bb.utils.mkdirhier(d.getVar('SDE_DIR')) |
| 170 | with open(epochfile, 'w') as f: |
| 171 | f.write(str(source_date_epoch)) |
| 172 | } |
| 173 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 174 | def get_source_date_epoch_value(d): |
| 175 | cached = d.getVar('__CACHED_SOURCE_DATE_EPOCH') |
| 176 | if cached: |
| 177 | return cached |
| 178 | |
| 179 | epochfile = d.getVar('SDE_FILE') |
| 180 | source_date_epoch = 0 |
| 181 | if os.path.isfile(epochfile): |
| 182 | with open(epochfile, 'r') as f: |
| 183 | s = f.read() |
| 184 | try: |
| 185 | source_date_epoch = int(s) |
| 186 | except ValueError: |
| 187 | bb.warn("SOURCE_DATE_EPOCH value '%s' is invalid. Reverting to 0" % s) |
| 188 | source_date_epoch = 0 |
| 189 | bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch) |
| 190 | else: |
| 191 | bb.debug(1, "Cannot find %s. SOURCE_DATE_EPOCH will default to %d" % (epochfile, source_date_epoch)) |
| 192 | |
| 193 | d.setVar('__CACHED_SOURCE_DATE_EPOCH', str(source_date_epoch)) |
| 194 | return str(source_date_epoch) |
| 195 | |
| 196 | export SOURCE_DATE_EPOCH ?= "${@get_source_date_epoch_value(d)}" |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 197 | BB_HASHBASE_WHITELIST += "SOURCE_DATE_EPOCH" |
| 198 | |
| 199 | python () { |
| 200 | if d.getVar('BUILD_REPRODUCIBLE_BINARIES') == '1': |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 201 | d.appendVarFlag("do_unpack", "postfuncs", " create_source_date_epoch_stamp") |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 202 | } |