Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
52867 views
1
#!/bin/sh
2
3
# Output a Makefile rule describing the dependencies of a given source file.
4
# Expected arguments are $(CC) $(CFLAGS) $(SRC) $(OBJ)
5
6
set -f
7
8
[ -n "$1" ] && [ -n "$3" ] && [ -n "$4" ] || exit 1
9
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')
19
$3"
20
21
# Convert Windows paths to Unix paths if possible
22
if command -v cygpath >/dev/null 2>&1 ; then
23
IFS='
24
'
25
deps="$(cygpath -u -- $deps)"
26
fi
27
28
# Escape characters as required to create valid Makefile file names
29
escape() {
30
sed 's/ /\\ /g; s/#/\\#/g; s/\$/\$\$/g'
31
}
32
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/^\.\///" |
38
sort | uniq |
39
escape | tr -s '\n\r' ' ' | sed 's/^ *\(.*\) $/\1/')"
40
41
# Escape the target file name as well
42
target="$(printf '%s' "$4" | escape)"
43
44
printf '%s: %s\n' "$target" "$deps"
45
46