| 1 |
#! /usr/bin/env sh |
| 2 |
# |
| 3 |
# $Header: /u/gcmpack/MITgcm/tools/xmakedepend,v 1.12 2013/08/22 20:49:59 jmc Exp $ |
| 4 |
# $Name: $ |
| 5 |
# |
| 6 |
# $TOG: mdepend.cpp /main/13 1997/06/20 21:12:18 kaleb $ |
| 7 |
# |
| 8 |
# Do the equivalent of the 'makedepend' program, but do it right. |
| 9 |
# |
| 10 |
# Usage: |
| 11 |
# |
| 12 |
# makedepend [cpp-flags] [-w width] [-s magic-string] [-f makefile] |
| 13 |
# [-o object-suffix] |
| 14 |
# |
| 15 |
# Notes: |
| 16 |
# |
| 17 |
# The C compiler used can be overridden with the environment |
| 18 |
# variable "CC". |
| 19 |
# |
| 20 |
# The "-v" switch of the "makedepend" program is not supported. |
| 21 |
# |
| 22 |
# |
| 23 |
# This script should |
| 24 |
# work on both USG and BSD systems. However, when System V.4 comes out, |
| 25 |
# USG users will probably have to change "silent" to "-s" instead of |
| 26 |
# "-" (at least, that is what the documentation implies). |
| 27 |
# |
| 28 |
# $XFree86: xc/config/util/mdepend.cpp,v 3.2 1997/06/29 07:54:20 dawes Exp $ |
| 29 |
|
| 30 |
CPPARGS="-Dlinux -D__i386__ -D_POSIX_C_SOURCE=199309L -D_POSIX_SOURCE -D_XOPEN_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_GNU_SOURCE " |
| 31 |
CPPARGS="-traditional $CPPARGS" |
| 32 |
|
| 33 |
CC="cpp $CPPARGS" |
| 34 |
if test -x /lib/cpp ; then |
| 35 |
CC="/lib/cpp $CPPARGS" |
| 36 |
fi |
| 37 |
|
| 38 |
silent='-' |
| 39 |
|
| 40 |
#TMP=./mdep$$ |
| 41 |
#- try to put temporary files in system-local /tmp dir |
| 42 |
TMP=/tmp/mdep$$ |
| 43 |
touch $TMP ; retVal=$? |
| 44 |
if [ $retVal -eq 0 ] ; then |
| 45 |
if test ! -r $TMP ; then TMP=./mdep$$ ; fi |
| 46 |
else |
| 47 |
TMP=./mdep$$ |
| 48 |
fi |
| 49 |
rm -f $TMP |
| 50 |
#echo "temp files: $TMP" |
| 51 |
|
| 52 |
CPPCMD=${TMP}a |
| 53 |
DEPENDLINES=${TMP}b |
| 54 |
TMPMAKEFILE=${TMP}c |
| 55 |
HEAD_MKFILE=${TMP}d |
| 56 |
ARGS=${TMP}e |
| 57 |
|
| 58 |
trap "rm -f ${TMP}*; exit 1" 1 2 15 |
| 59 |
trap "rm -f ${TMP}*; exit 0" 1 2 13 |
| 60 |
|
| 61 |
echo " \c" > $CPPCMD |
| 62 |
if [ `wc -c < $CPPCMD` -eq 1 ] |
| 63 |
then |
| 64 |
c="\c" |
| 65 |
n= |
| 66 |
else |
| 67 |
c= |
| 68 |
n="-n" |
| 69 |
fi |
| 70 |
|
| 71 |
echo $n "$c" >$ARGS |
| 72 |
|
| 73 |
files= |
| 74 |
makefile= |
| 75 |
magic_string='# DO NOT DELETE' |
| 76 |
objsuffix='.o' |
| 77 |
width=78 |
| 78 |
endmarker="" |
| 79 |
verbose=n |
| 80 |
append=n |
| 81 |
|
| 82 |
while [ $# != 0 ] |
| 83 |
do |
| 84 |
if [ "$endmarker"x != x ] && [ "$endmarker" = "$1" ]; then |
| 85 |
endmarker="" |
| 86 |
else |
| 87 |
case "$1" in |
| 88 |
-D*|-I*) |
| 89 |
echo $n " '$1'$c" >> $ARGS |
| 90 |
;; |
| 91 |
|
| 92 |
-g|-O) # ignore so we can just pass $(CFLAGS) in |
| 93 |
;; |
| 94 |
|
| 95 |
*) |
| 96 |
if [ "$endmarker"x = x ]; then |
| 97 |
case "$1" in |
| 98 |
-w) |
| 99 |
width="$2" |
| 100 |
shift |
| 101 |
;; |
| 102 |
-s) |
| 103 |
magic_string="$2" |
| 104 |
shift |
| 105 |
;; |
| 106 |
-f*) |
| 107 |
if [ "$1" = "-f-" ]; then |
| 108 |
makefile="-" |
| 109 |
else |
| 110 |
makefile="$2" |
| 111 |
shift |
| 112 |
fi |
| 113 |
;; |
| 114 |
-o) |
| 115 |
objsuffix="$2" |
| 116 |
shift |
| 117 |
;; |
| 118 |
|
| 119 |
--*) |
| 120 |
echo "$1" | sed 's/^\-\-//' >${TMP}end |
| 121 |
endmarker="`cat ${TMP}end`" |
| 122 |
rm -f ${TMP}end |
| 123 |
if [ "$endmarker"x = x ]; then |
| 124 |
endmarker="--" |
| 125 |
fi |
| 126 |
;; |
| 127 |
-v) |
| 128 |
verbose="y" |
| 129 |
;; |
| 130 |
|
| 131 |
-a) |
| 132 |
append="y" |
| 133 |
;; |
| 134 |
|
| 135 |
-cc) |
| 136 |
CC="$2" |
| 137 |
shift |
| 138 |
;; |
| 139 |
|
| 140 |
-cpp) |
| 141 |
CC="$2 $CPPARGS" |
| 142 |
shift |
| 143 |
;; |
| 144 |
|
| 145 |
-*) |
| 146 |
echo "Unknown option '$1' ignored" 1>&2 |
| 147 |
;; |
| 148 |
*) |
| 149 |
files="$files $1" |
| 150 |
;; |
| 151 |
esac |
| 152 |
fi |
| 153 |
;; |
| 154 |
esac |
| 155 |
fi |
| 156 |
shift |
| 157 |
done |
| 158 |
echo ' $*' >> $ARGS |
| 159 |
|
| 160 |
echo "#!/bin/sh" > $CPPCMD |
| 161 |
echo "exec $CC `cat $ARGS`" >> $CPPCMD |
| 162 |
chmod +x $CPPCMD |
| 163 |
rm $ARGS |
| 164 |
|
| 165 |
case "$makefile" in |
| 166 |
'') |
| 167 |
if [ -r makefile ] |
| 168 |
then |
| 169 |
makefile=makefile |
| 170 |
elif [ -r Makefile ] |
| 171 |
then |
| 172 |
makefile=Makefile |
| 173 |
else |
| 174 |
echo 'no makefile or Makefile found' 1>&2 |
| 175 |
exit 1 |
| 176 |
fi |
| 177 |
;; |
| 178 |
-) |
| 179 |
makefile=$TMPMAKEFILE |
| 180 |
;; |
| 181 |
esac |
| 182 |
|
| 183 |
if [ "$verbose"x = "y"x ]; then |
| 184 |
cat $CPPCMD |
| 185 |
fi |
| 186 |
|
| 187 |
echo '' > $DEPENDLINES |
| 188 |
for i in $files |
| 189 |
do |
| 190 |
$CPPCMD $i | sed -n "/^#/s;^;$i ;p" |
| 191 |
|
| 192 |
done | sed -e 's|/[^/.][^/]*/\.\.||g' -e 's|/\.[^.][^/]*/\.\.||g' -e 's|"||g' -e 's| \./| |' | awk '!/<.*>/{ |
| 193 |
if ($1 != $4 && $2 != "#ident" && $2 != "#pragma") |
| 194 |
|
| 195 |
{ |
| 196 |
ofile = substr ($1, 1, length ($1) - 2) "'"$objsuffix"'" |
| 197 |
print ofile, $4 |
| 198 |
} |
| 199 |
}' | sort -u | awk ' |
| 200 |
|
| 201 |
{ |
| 202 |
newrec = rec " " $2 |
| 203 |
if ($1 != old1) |
| 204 |
{ |
| 205 |
old1 = $1 |
| 206 |
if (rec != "") |
| 207 |
print rec |
| 208 |
rec = $1 ": " $2 |
| 209 |
} |
| 210 |
else if (length (newrec) > '"$width"') |
| 211 |
{ |
| 212 |
print rec |
| 213 |
rec = $1 ": " $2 |
| 214 |
} |
| 215 |
else |
| 216 |
rec = newrec |
| 217 |
} |
| 218 |
END { |
| 219 |
|
| 220 |
if (rec != "") |
| 221 |
print rec |
| 222 |
}' | egrep -v '^[^:]*:[ ]*$' >> $DEPENDLINES |
| 223 |
|
| 224 |
trap "" 1 2 13 15 # Now we are committed |
| 225 |
case "$makefile" in |
| 226 |
$TMPMAKEFILE) |
| 227 |
;; |
| 228 |
*) |
| 229 |
rm -f $makefile.bak |
| 230 |
cp $makefile $makefile.bak |
| 231 |
echo "Appending dependencies to $makefile" |
| 232 |
;; |
| 233 |
esac |
| 234 |
|
| 235 |
# If not -a, append the magic string and a blank line so that |
| 236 |
# /^$magic_string/+1,\$d can be used to delete everything from after |
| 237 |
# the magic string to the end of the file. Then, append a blank |
| 238 |
# line again and then the dependencies. |
| 239 |
if [ "$append" = "n" ] |
| 240 |
then |
| 241 |
cat >> $makefile << END_OF_APPEND |
| 242 |
|
| 243 |
$magic_string |
| 244 |
|
| 245 |
END_OF_APPEND |
| 246 |
# ed -h > /dev/null 2>&1 |
| 247 |
which ed > /dev/null 2>&1 |
| 248 |
RETVAL=$? |
| 249 |
if test x"$RETVAL" = x0 ; then |
| 250 |
if [ $verbose = y ]; then echo ' using line editor "ed"' ; fi |
| 251 |
ed $silent $makefile << END_OF_ED_SCRIPT |
| 252 |
/^$magic_string/+1,\$d |
| 253 |
w |
| 254 |
q |
| 255 |
END_OF_ED_SCRIPT |
| 256 |
else |
| 257 |
if [ $verbose = y ]; then echo ' using sed command' ; fi |
| 258 |
sed -n "1,/^$magic_string/p" $makefile > $HEAD_MKFILE |
| 259 |
mv $HEAD_MKFILE $makefile |
| 260 |
fi |
| 261 |
echo '' >>$makefile |
| 262 |
fi |
| 263 |
|
| 264 |
cat $DEPENDLINES >>$makefile |
| 265 |
|
| 266 |
case "$makefile" in |
| 267 |
$TMPMAKEFILE) |
| 268 |
cat $TMPMAKEFILE |
| 269 |
;; |
| 270 |
esac |
| 271 |
|
| 272 |
rm -f ${TMP}* |
| 273 |
exit 0 |