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 () { |
| 47 | echo "Deploying SDE to ${SDE_DIR}." |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 48 | mkdir -p ${SDE_DEPLOYDIR} |
| 49 | if [ -e ${SDE_FILE} ]; then |
| 50 | cp -p ${SDE_FILE} ${SDE_DEPLOYDIR}/__source_date_epoch.txt |
| 51 | fi |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | python do_deploy_source_date_epoch_setscene () { |
| 55 | sstate_setscene(d) |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 56 | bb.utils.mkdirhier(d.getVar('SDE_DIR')) |
| 57 | sde_file = os.path.join(d.getVar('SDE_DEPLOYDIR'), '__source_date_epoch.txt') |
| 58 | if os.path.exists(sde_file): |
| 59 | os.rename(sde_file, d.getVar('SDE_FILE')) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 60 | } |
| 61 | |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 62 | do_deploy_source_date_epoch[dirs] = "${SDE_DEPLOYDIR}" |
| 63 | do_deploy_source_date_epoch[sstate-plaindirs] = "${SDE_DEPLOYDIR}" |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 64 | addtask do_deploy_source_date_epoch_setscene |
| 65 | addtask do_deploy_source_date_epoch before do_configure after do_patch |
| 66 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 67 | def get_source_date_epoch_from_known_files(d, sourcedir): |
| 68 | source_date_epoch = None |
| 69 | newest_file = None |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 70 | known_files = set(["NEWS", "ChangeLog", "Changelog", "CHANGES"]) |
| 71 | for file in known_files: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 72 | filepath = os.path.join(sourcedir, file) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 73 | if os.path.isfile(filepath): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 74 | mtime = int(os.lstat(filepath).st_mtime) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 75 | # 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] | 76 | if not source_date_epoch or mtime > source_date_epoch: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 77 | source_date_epoch = mtime |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 78 | newest_file = filepath |
| 79 | if newest_file: |
| 80 | bb.debug(1, "SOURCE_DATE_EPOCH taken from: %s" % newest_file) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 81 | return source_date_epoch |
| 82 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 83 | def find_git_folder(d, sourcedir): |
| 84 | # First guess: WORKDIR/git |
| 85 | # This is the default git fetcher unpack path |
| 86 | workdir = d.getVar('WORKDIR') |
| 87 | gitpath = os.path.join(workdir, "git/.git") |
| 88 | if os.path.isdir(gitpath): |
| 89 | return gitpath |
| 90 | |
| 91 | # Second guess: ${S} |
| 92 | gitpath = os.path.join(sourcedir, ".git") |
| 93 | if os.path.isdir(gitpath): |
| 94 | return gitpath |
| 95 | |
| 96 | # Perhaps there was a subpath or destsuffix specified. |
| 97 | # Go looking in the WORKDIR |
| 98 | exclude = set(["build", "image", "license-destdir", "patches", "pseudo", |
| 99 | "recipe-sysroot", "recipe-sysroot-native", "sysroot-destdir", "temp"]) |
| 100 | for root, dirs, files in os.walk(workdir, topdown=True): |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 101 | dirs[:] = [d for d in dirs if d not in exclude] |
| 102 | if '.git' in dirs: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 103 | return root |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 104 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 105 | bb.warn("Failed to find a git repository in WORKDIR: %s" % workdir) |
| 106 | return None |
| 107 | |
| 108 | def get_source_date_epoch_from_git(d, sourcedir): |
| 109 | source_date_epoch = None |
| 110 | if "git://" in d.getVar('SRC_URI'): |
| 111 | gitpath = find_git_folder(d, sourcedir) |
| 112 | if gitpath: |
| 113 | import subprocess |
| 114 | source_date_epoch = int(subprocess.check_output(['git','log','-1','--pretty=%ct'], cwd=gitpath)) |
| 115 | bb.debug(1, "git repository: %s" % gitpath) |
| 116 | return source_date_epoch |
| 117 | |
| 118 | def get_source_date_epoch_from_youngest_file(d, sourcedir): |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 119 | if sourcedir == d.getVar('WORKDIR'): |
| 120 | # These sources are almost certainly not from a tarball |
| 121 | return None |
| 122 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 123 | # Do it the hard way: check all files and find the youngest one... |
| 124 | source_date_epoch = None |
| 125 | newest_file = None |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 126 | for root, dirs, files in os.walk(sourcedir, topdown=True): |
| 127 | files = [f for f in files if not f[0] == '.'] |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 128 | |
| 129 | for fname in files: |
| 130 | filename = os.path.join(root, fname) |
| 131 | try: |
| 132 | mtime = int(os.lstat(filename).st_mtime) |
| 133 | except ValueError: |
| 134 | mtime = 0 |
| 135 | if not source_date_epoch or mtime > source_date_epoch: |
| 136 | source_date_epoch = mtime |
| 137 | newest_file = filename |
| 138 | |
| 139 | if newest_file: |
| 140 | bb.debug(1, "Newest file found: %s" % newest_file) |
| 141 | return source_date_epoch |
| 142 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 143 | def fixed_source_date_epoch(): |
| 144 | bb.debug(1, "No tarball or git repo found to determine SOURCE_DATE_EPOCH") |
| 145 | return 0 |
| 146 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 147 | python do_create_source_date_epoch_stamp() { |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 148 | epochfile = d.getVar('SDE_FILE') |
| 149 | if os.path.isfile(epochfile): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 150 | bb.debug(1, "Reusing SOURCE_DATE_EPOCH from: %s" % epochfile) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 151 | return |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 152 | |
| 153 | sourcedir = d.getVar('S') |
| 154 | source_date_epoch = ( |
| 155 | get_source_date_epoch_from_git(d, sourcedir) or |
| 156 | get_source_date_epoch_from_known_files(d, sourcedir) or |
| 157 | get_source_date_epoch_from_youngest_file(d, sourcedir) or |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 158 | fixed_source_date_epoch() # Last resort |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 159 | ) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 160 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 161 | bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 162 | bb.utils.mkdirhier(d.getVar('SDE_DIR')) |
| 163 | with open(epochfile, 'w') as f: |
| 164 | f.write(str(source_date_epoch)) |
| 165 | } |
| 166 | |
| 167 | BB_HASHBASE_WHITELIST += "SOURCE_DATE_EPOCH" |
| 168 | |
| 169 | python () { |
| 170 | if d.getVar('BUILD_REPRODUCIBLE_BINARIES') == '1': |
| 171 | d.appendVarFlag("do_unpack", "postfuncs", " do_create_source_date_epoch_stamp") |
| 172 | epochfile = d.getVar('SDE_FILE') |
| 173 | source_date_epoch = "0" |
| 174 | if os.path.isfile(epochfile): |
| 175 | with open(epochfile, 'r') as f: |
| 176 | source_date_epoch = f.read() |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 177 | bb.debug(1, "SOURCE_DATE_EPOCH: %s" % source_date_epoch) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 178 | d.setVar('SOURCE_DATE_EPOCH', source_date_epoch) |
| 179 | } |