3 # Output a Makefile rule describing the dependencies of a given source file.
4 # Expected arguments are $(CC) $(CFLAGS) $(SRC) $(OBJ)
8 [ -n "$1" ] && [ -n "$3" ] && [ -n "$4" ] || exit 1
10 # Add flags to only perform syntax checking and output a list of included files
11 # Discard all output other than included files
12 # Convert '\' directory separators to '/'
13 # Remove system includes (hack: check for "/Program Files" string in path)
14 # Add the source file itself as a dependency
15 deps="$($1 $2 -nologo -showIncludes -W0 -Zs "$3" 2>&1 |
16 grep '^Note: including file:' |
17 sed 's/^Note: including file:[[:space:]]*\(.*\)$/\1/; s/\\/\//g' |
18 sed '/\/[Pp]rogram [Ff]iles/d')
21 # Convert Windows paths to Unix paths if possible
22 if command -v cygpath >/dev/null 2>&1 ; then
25 deps="$(cygpath -u -- $deps)"
28 # Escape characters as required to create valid Makefile file names
30 sed 's/ /\\ /g; s/#/\\#/g; s/\$/\$\$/g'
33 # Remove prefixes that are equal to the working directory
34 # Sort and remove duplicate entries
35 # Escape and collapse the dependencies into one line
36 deps="$(printf '%s' "$deps" |
37 sed "s/^$(pwd | sed 's/\//\\\//g')\///; s/^\.\///" |
39 escape | tr -s '\n\r' ' ' | sed 's/^ *\(.*\) $/\1/')"
41 # Escape the target file name as well
42 target="$(printf '%s' "$4" | escape)"
44 printf '%s: %s\n' "$target" "$deps"