1# 2# SPDX-License-Identifier: GPL-2.0-only 3# 4import os 5import subprocess 6import bb 7 8def get_source_date_epoch_from_known_files(d, sourcedir): 9 source_date_epoch = None 10 newest_file = None 11 known_files = set(["NEWS", "ChangeLog", "Changelog", "CHANGES"]) 12 for file in known_files: 13 filepath = os.path.join(sourcedir, file) 14 if os.path.isfile(filepath): 15 mtime = int(os.lstat(filepath).st_mtime) 16 # There may be more than one "known_file" present, if so, use the youngest one 17 if not source_date_epoch or mtime > source_date_epoch: 18 source_date_epoch = mtime 19 newest_file = filepath 20 if newest_file: 21 bb.debug(1, "SOURCE_DATE_EPOCH taken from: %s" % newest_file) 22 return source_date_epoch 23 24def find_git_folder(d, sourcedir): 25 # First guess: WORKDIR/git 26 # This is the default git fetcher unpack path 27 workdir = d.getVar('WORKDIR') 28 gitpath = os.path.join(workdir, "git/.git") 29 if os.path.isdir(gitpath): 30 return gitpath 31 32 # Second guess: ${S} 33 gitpath = os.path.join(sourcedir, ".git") 34 if os.path.isdir(gitpath): 35 return gitpath 36 37 # Perhaps there was a subpath or destsuffix specified. 38 # Go looking in the WORKDIR 39 exclude = set(["build", "image", "license-destdir", "patches", "pseudo", 40 "recipe-sysroot", "recipe-sysroot-native", "sysroot-destdir", "temp"]) 41 for root, dirs, files in os.walk(workdir, topdown=True): 42 dirs[:] = [d for d in dirs if d not in exclude] 43 if '.git' in dirs: 44 return root 45 46 bb.warn("Failed to find a git repository in WORKDIR: %s" % workdir) 47 return None 48 49def get_source_date_epoch_from_git(d, sourcedir): 50 if not "git://" in d.getVar('SRC_URI') and not "gitsm://" in d.getVar('SRC_URI'): 51 return None 52 53 gitpath = find_git_folder(d, sourcedir) 54 if not gitpath: 55 return None 56 57 # Check that the repository has a valid HEAD; it may not if subdir is used 58 # in SRC_URI 59 p = subprocess.run(['git', '--git-dir', gitpath, 'rev-parse', 'HEAD'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 60 if p.returncode != 0: 61 bb.debug(1, "%s does not have a valid HEAD: %s" % (gitpath, p.stdout.decode('utf-8'))) 62 return None 63 64 bb.debug(1, "git repository: %s" % gitpath) 65 p = subprocess.run(['git', '--git-dir', gitpath, 'log', '-1', '--pretty=%ct'], check=True, stdout=subprocess.PIPE) 66 return int(p.stdout.decode('utf-8')) 67 68def get_source_date_epoch_from_youngest_file(d, sourcedir): 69 if sourcedir == d.getVar('WORKDIR'): 70 # These sources are almost certainly not from a tarball 71 return None 72 73 # Do it the hard way: check all files and find the youngest one... 74 source_date_epoch = None 75 newest_file = None 76 for root, dirs, files in os.walk(sourcedir, topdown=True): 77 files = [f for f in files if not f[0] == '.'] 78 79 for fname in files: 80 filename = os.path.join(root, fname) 81 try: 82 mtime = int(os.lstat(filename).st_mtime) 83 except ValueError: 84 mtime = 0 85 if not source_date_epoch or mtime > source_date_epoch: 86 source_date_epoch = mtime 87 newest_file = filename 88 89 if newest_file: 90 bb.debug(1, "Newest file found: %s" % newest_file) 91 return source_date_epoch 92 93def fixed_source_date_epoch(d): 94 bb.debug(1, "No tarball or git repo found to determine SOURCE_DATE_EPOCH") 95 source_date_epoch = d.getVar('SOURCE_DATE_EPOCH_FALLBACK') 96 if source_date_epoch: 97 bb.debug(1, "Using SOURCE_DATE_EPOCH_FALLBACK") 98 return int(source_date_epoch) 99 return 0 100 101def get_source_date_epoch(d, sourcedir): 102 return ( 103 get_source_date_epoch_from_git(d, sourcedir) or 104 get_source_date_epoch_from_known_files(d, sourcedir) or 105 get_source_date_epoch_from_youngest_file(d, sourcedir) or 106 fixed_source_date_epoch(d) # Last resort 107 ) 108 109