Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ianloic
Created September 13, 2019 13:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ianloic/acf359d45914fa5d180fdce229f070a5 to your computer and use it in GitHub Desktop.
Save ianloic/acf359d45914fa5d180fdce229f070a5 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [[ $# -lt 2 ]]; then
echo "Usage: $0 <depfile> <command...>" >&2
exit 1
fi
# The .d file we're going to write.
DEPSFILE=$1
shift
# The temporary file that holds strace output.
STRACEFILE=$DEPSFILE.$$.strace
# 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