home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / bin / ccmakedep < prev    next >
Encoding:
Text File  |  2006-12-20  |  4.9 KB  |  287 lines

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