msvsdepend.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/bin/sh
  2. # Output a Makefile rule describing the dependencies of a given source file.
  3. # Expected arguments are $(CC) $(CFLAGS) $(SRC) $(OBJ)
  4. set -f
  5. [ -n "$1" ] && [ -n "$3" ] && [ -n "$4" ] || exit 1
  6. # Add flags to only perform syntax checking and output a list of included files
  7. # Discard all output other than included files
  8. # Convert '\' directory separators to '/'
  9. # Remove system includes (hack: check for "/Program Files" string in path)
  10. # Add the source file itself as a dependency
  11. deps="$($1 $2 -nologo -showIncludes -W0 -Zs "$3" 2>&1 |
  12. grep '^Note: including file:' |
  13. sed 's/^Note: including file:[[:space:]]*\(.*\)$/\1/; s/\\/\//g' |
  14. sed '/\/[Pp]rogram [Ff]iles/d')
  15. $3"
  16. # Convert Windows paths to Unix paths if possible
  17. if command -v cygpath >/dev/null 2>&1 ; then
  18. IFS='
  19. '
  20. deps="$(cygpath -u -- $deps)"
  21. elif grep -q 'Microsoft' /proc/sys/kernel/osrelease 2>/dev/null ; then
  22. # Running under WSL. We don't have access to cygpath but since the Windows
  23. # file system resides under "/mnt/<drive_letter>/" we can simply replace
  24. # "C:" with "/mnt/c". This command uses a GNU extension to sed but that's
  25. # available on WSL so we don't need to limit ourselves by what POSIX says.
  26. deps="$(printf '%s' "$deps" | sed 's/^\([a-zA-Z]\):/\/mnt\/\L\1/')"
  27. fi
  28. # Escape characters as required to create valid Makefile file names
  29. escape() {
  30. sed 's/ /\\ /g; s/#/\\#/g; s/\$/\$\$/g'
  31. }
  32. # Remove prefixes that are equal to the working directory
  33. # Sort and remove duplicate entries
  34. # Escape and collapse the dependencies into one line
  35. deps="$(printf '%s' "$deps" |
  36. sed "s/^$(pwd | sed 's/\//\\\//g')\///; s/^\.\///" |
  37. sort | uniq |
  38. escape | tr -s '\n\r' ' ' | sed 's/^ *\(.*\) $/\1/')"
  39. # Escape the target file name as well
  40. target="$(printf '%s' "$4" | escape)"
  41. printf '%s: %s\n' "$target" "$deps"