#!/bin/sh12# Output a Makefile rule describing the dependencies of a given source file.3# Expected arguments are $(CC) $(CFLAGS) $(SRC) $(OBJ)45set -f67[ -n "$1" ] && [ -n "$3" ] && [ -n "$4" ] || exit 189# Add flags to only perform syntax checking and output a list of included files10# Discard all output other than included files11# Convert '\' directory separators to '/'12# Remove system includes (hack: check for "/Program Files" string in path)13# Add the source file itself as a dependency14deps="$($1 $2 -nologo -showIncludes -W0 -Zs "$3" 2>&1 |15grep '^Note: including file:' |16sed 's/^Note: including file:[[:space:]]*\(.*\)$/\1/; s/\\/\//g' |17sed '/\/[Pp]rogram [Ff]iles/d')18$3"1920# Convert Windows paths to Unix paths if possible21if command -v cygpath >/dev/null 2>&1 ; then22IFS='23'24deps="$(cygpath -u -- $deps)"25fi2627# Escape characters as required to create valid Makefile file names28escape() {29sed 's/ /\\ /g; s/#/\\#/g; s/\$/\$\$/g'30}3132# Remove prefixes that are equal to the working directory33# Sort and remove duplicate entries34# Escape and collapse the dependencies into one line35deps="$(printf '%s' "$deps" |36sed "s/^$(pwd | sed 's/\//\\\//g')\///; s/^\.\///" |37sort | uniq |38escape | tr -s '\n\r' ' ' | sed 's/^ *\(.*\) $/\1/')"3940# Escape the target file name as well41target="$(printf '%s' "$4" | escape)"4243printf '%s: %s\n' "$target" "$deps"444546