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 | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 25 | # 4. If the above steps fail, use the modification time of the youngest file in the source tree. |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 26 | # |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 27 | # Once the value of SOURCE_DATE_EPOCH is determined, it is stored in the recipe's SDE_FILE. |
| 28 | # If none of these mechanisms are suitable, replace the do_deploy_source_date_epoch task |
| 29 | # with recipe-specific functionality to write the appropriate SOURCE_DATE_EPOCH into the SDE_FILE. |
| 30 | # |
| 31 | # If this file is found by other tasks, the value is exported in the SOURCE_DATE_EPOCH variable. |
| 32 | # 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] | 33 | |
| 34 | BUILD_REPRODUCIBLE_BINARIES ??= '1' |
| 35 | inherit ${@oe.utils.ifelse(d.getVar('BUILD_REPRODUCIBLE_BINARIES') == '1', 'reproducible_build_simple', '')} |
| 36 | |
| 37 | SDE_DIR ="${WORKDIR}/source-date-epoch" |
| 38 | SDE_FILE = "${SDE_DIR}/__source_date_epoch.txt" |
| 39 | |
| 40 | SSTATETASKS += "do_deploy_source_date_epoch" |
| 41 | |
| 42 | do_deploy_source_date_epoch () { |
| 43 | echo "Deploying SDE to ${SDE_DIR}." |
| 44 | } |
| 45 | |
| 46 | python do_deploy_source_date_epoch_setscene () { |
| 47 | sstate_setscene(d) |
| 48 | } |
| 49 | |
| 50 | do_deploy_source_date_epoch[dirs] = "${SDE_DIR}" |
| 51 | do_deploy_source_date_epoch[sstate-plaindirs] = "${SDE_DIR}" |
| 52 | addtask do_deploy_source_date_epoch_setscene |
| 53 | addtask do_deploy_source_date_epoch before do_configure after do_patch |
| 54 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 55 | def get_source_date_epoch_from_known_files(d, sourcedir): |
| 56 | source_date_epoch = None |
| 57 | newest_file = None |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 58 | known_files = set(["NEWS", "ChangeLog", "Changelog", "CHANGES"]) |
| 59 | for file in known_files: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 60 | filepath = os.path.join(sourcedir, file) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 61 | if os.path.isfile(filepath): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 62 | mtime = int(os.lstat(filepath).st_mtime) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 63 | # 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^] | 64 | if not source_date_epoch or mtime > source_date_epoch: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 65 | source_date_epoch = mtime |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 66 | newest_file = filepath |
| 67 | if newest_file: |
| 68 | bb.debug(1, "SOURCE_DATE_EPOCH taken from: %s" % newest_file) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 69 | return source_date_epoch |
| 70 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 71 | def find_git_folder(d, sourcedir): |
| 72 | # First guess: WORKDIR/git |
| 73 | # This is the default git fetcher unpack path |
| 74 | workdir = d.getVar('WORKDIR') |
| 75 | gitpath = os.path.join(workdir, "git/.git") |
| 76 | if os.path.isdir(gitpath): |
| 77 | return gitpath |
| 78 | |
| 79 | # Second guess: ${S} |
| 80 | gitpath = os.path.join(sourcedir, ".git") |
| 81 | if os.path.isdir(gitpath): |
| 82 | return gitpath |
| 83 | |
| 84 | # Perhaps there was a subpath or destsuffix specified. |
| 85 | # Go looking in the WORKDIR |
| 86 | exclude = set(["build", "image", "license-destdir", "patches", "pseudo", |
| 87 | "recipe-sysroot", "recipe-sysroot-native", "sysroot-destdir", "temp"]) |
| 88 | for root, dirs, files in os.walk(workdir, topdown=True): |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 89 | dirs[:] = [d for d in dirs if d not in exclude] |
| 90 | if '.git' in dirs: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 91 | return root |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 92 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 93 | bb.warn("Failed to find a git repository in WORKDIR: %s" % workdir) |
| 94 | return None |
| 95 | |
| 96 | def get_source_date_epoch_from_git(d, sourcedir): |
| 97 | source_date_epoch = None |
| 98 | if "git://" in d.getVar('SRC_URI'): |
| 99 | gitpath = find_git_folder(d, sourcedir) |
| 100 | if gitpath: |
| 101 | import subprocess |
| 102 | source_date_epoch = int(subprocess.check_output(['git','log','-1','--pretty=%ct'], cwd=gitpath)) |
| 103 | bb.debug(1, "git repository: %s" % gitpath) |
| 104 | return source_date_epoch |
| 105 | |
| 106 | def get_source_date_epoch_from_youngest_file(d, sourcedir): |
| 107 | # Do it the hard way: check all files and find the youngest one... |
| 108 | source_date_epoch = None |
| 109 | newest_file = None |
| 110 | # Just in case S = WORKDIR |
| 111 | exclude = set(["build", "image", "license-destdir", "patches", "pseudo", |
| 112 | "recipe-sysroot", "recipe-sysroot-native", "sysroot-destdir", "temp"]) |
| 113 | for root, dirs, files in os.walk(sourcedir, topdown=True): |
| 114 | files = [f for f in files if not f[0] == '.'] |
| 115 | dirs[:] = [d for d in dirs if d not in exclude] |
| 116 | |
| 117 | for fname in files: |
| 118 | filename = os.path.join(root, fname) |
| 119 | try: |
| 120 | mtime = int(os.lstat(filename).st_mtime) |
| 121 | except ValueError: |
| 122 | mtime = 0 |
| 123 | if not source_date_epoch or mtime > source_date_epoch: |
| 124 | source_date_epoch = mtime |
| 125 | newest_file = filename |
| 126 | |
| 127 | if newest_file: |
| 128 | bb.debug(1, "Newest file found: %s" % newest_file) |
| 129 | return source_date_epoch |
| 130 | |
| 131 | python do_create_source_date_epoch_stamp() { |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 132 | epochfile = d.getVar('SDE_FILE') |
| 133 | if os.path.isfile(epochfile): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 134 | bb.debug(1, "Reusing SOURCE_DATE_EPOCH from: %s" % epochfile) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 135 | return |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 136 | |
| 137 | sourcedir = d.getVar('S') |
| 138 | source_date_epoch = ( |
| 139 | get_source_date_epoch_from_git(d, sourcedir) or |
| 140 | get_source_date_epoch_from_known_files(d, sourcedir) or |
| 141 | get_source_date_epoch_from_youngest_file(d, sourcedir) or |
| 142 | 0 # Last resort |
| 143 | ) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 144 | if source_date_epoch == 0: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 145 | # empty folder, not a single file ... |
| 146 | bb.debug(1, "No files found to determine SOURCE_DATE_EPOCH") |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 147 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 148 | bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 149 | bb.utils.mkdirhier(d.getVar('SDE_DIR')) |
| 150 | with open(epochfile, 'w') as f: |
| 151 | f.write(str(source_date_epoch)) |
| 152 | } |
| 153 | |
| 154 | BB_HASHBASE_WHITELIST += "SOURCE_DATE_EPOCH" |
| 155 | |
| 156 | python () { |
| 157 | if d.getVar('BUILD_REPRODUCIBLE_BINARIES') == '1': |
| 158 | d.appendVarFlag("do_unpack", "postfuncs", " do_create_source_date_epoch_stamp") |
| 159 | epochfile = d.getVar('SDE_FILE') |
| 160 | source_date_epoch = "0" |
| 161 | if os.path.isfile(epochfile): |
| 162 | with open(epochfile, 'r') as f: |
| 163 | source_date_epoch = f.read() |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 164 | bb.debug(1, "SOURCE_DATE_EPOCH: %s" % source_date_epoch) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 165 | d.setVar('SOURCE_DATE_EPOCH', source_date_epoch) |
| 166 | } |