Skip to content

Instantly share code, notes, and snippets.

Created September 13, 2019 13:57
What would you like to do?
if [[ $# -lt 2 ]]; then
echo "Usage: $0 <depfile> <command...>" >&2
exit 1
# The .d file we're going to write.
# The temporary file that holds strace output.
# Remove the temp file when we're done.
function cleanup {
rm -f "${STRACEFILE}"
trap cleanup EXIT
# strace the command and keep track of openat calls.
strace -o "${STRACEFILE}" -f -qq -y -e openat "$@"
# Function to de-dupe files and remove ones to ignore.
filterfiles() {
# This may require tweaking if for example the directory you build
# in is under /usr/.
sort | uniq | grep -E -v '^(/etc/|/var/|/proc/|/lib/|/usr/|/tmp/)'
# Files that were written by this command.
OUTPUTS=$(gawk 'match($0, /O_WRONLY.* = [0-9]+<(.+)>$/, arr) { print arr[1]; }' "${STRACEFILE}" | filterfiles | xargs echo)
# Files that were read by this command.
INPUTS=$(gawk 'match($0, /O_RDONLY.* = [0-9]+<(.+)>$/, arr) { print arr[1]; }' "${STRACEFILE}" | filterfiles | xargs echo)
# Generate the deps file.
echo "${OUTPUTS}: ${INPUTS}" > "${DEPSFILE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment