home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3098 / makedeps
Encoding:
Text File  |  1991-03-21  |  3.1 KB  |  156 lines

  1. eval 'exec /bin/perl -wS $0 ${1+"$@"}'
  2.     if 0;
  3.  
  4. # $Id: makedeps,v 1.6 1991/03/08 22:27:10 chip Exp $
  5. #
  6. # Make a list of dependencies for the given file(s).
  7. #
  8. # $Log: makedeps,v $
  9. # Revision 1.6  1991/03/08  22:27:10  chip
  10. # Add "-o" and "-u" options.
  11. #
  12. # Revision 1.5  1990/11/29  13:39:37  chip
  13. # Fix typo.
  14. #
  15. # Revision 1.4  90/09/28  11:01:51  chip
  16. # Handle C++.
  17. # Revision 1.3  90/05/03  16:49:30  chip
  18. # checked in with -k by chip at 90.08.17.15.17.16.
  19. # Revision 1.3  90/05/03  16:49:30  chip
  20. # Remove temp files each time around the loop.
  21. # Never mention temp files in output.
  22. # Revision 1.2  90/05/02  16:43:07  chip
  23. # Oops.  Write deps for the object file.
  24. # Revision 1.1  90/05/02  16:24:39  chip
  25. # Initial revision
  26.  
  27. ($ME = $0) =~ s#^.*/##;
  28.  
  29. ################################################################
  30. ## Configuration.
  31. ################################################################
  32.  
  33. $INF = "/usr/informix" unless $INF = $ENV{'INFORMIXDIR'};
  34. chop($HOME = `pwd`) unless $HOME = $ENV{'HOME'};
  35.  
  36. @COPY = ("cp");
  37. @CPP = ("cc", "-E");
  38. @CPLUSPP = ("g++", "-E");
  39. @ESQLC = ("$INF/lib/esqlc", "-e");
  40.  
  41. $T = "dep_$$";
  42.  
  43. ################################################################
  44. ## Options.
  45. ################################################################
  46.  
  47. undef $OPT_OUTPUT;
  48. $OPT_USER = 0;
  49. @CPPOPTS = ();
  50. while (@ARGV) {
  51.     $_ = $ARGV[0];
  52.     last unless /^-/;
  53.     if (/^-o(.*)$/) {
  54.         shift;
  55.         die "$ME: -o needs argument"
  56.             unless defined($OPT_OUTPUT = length($1) ? $1 : shift);
  57.     }
  58.     elsif (/^-u$/) {
  59.         shift;
  60.         $OPT_USER = 1;
  61.     }
  62.     else {
  63.         push(@CPPOPTS, shift);
  64.     }
  65. }
  66.  
  67. # Avoid warnings
  68. @CPP, @CPLUSPP;
  69.  
  70. &DEP("## These dependences were generated by the \"$ME\" program.\n");
  71. &DEP("## Any changes made by hand will be lost!\n");
  72.  
  73. foreach $file (@ARGV) {
  74.     unless (-f $file) {
  75.         print STDERR "$ME: $file: no such file\n";
  76.         next;
  77.     }
  78.  
  79.     if (($basename) = ($file =~ /^(\S+)\.c$/)) {
  80.         open(CPP, "@CPP @CPPOPTS $file |");
  81.     }
  82.     elsif (($basename) = ($file =~ /^(\S+)\.(C|cc|cxx|cpp)$/)) {
  83.         open(CPP, "@CPLUSPP @CPPOPTS $file |");
  84.     }
  85.     elsif (($basename) = ($file =~ /^(\S+)\.ec$/)) {
  86.         if (system(@COPY, $file, "$T.ec")
  87.          || system(@ESQLC, "$T.ec")) {
  88.             print STDERR "$ME: can't compile $file\n";
  89.             next;
  90.         }
  91.         open(CPP, "@CPP @CPPOPTS $T.c |");
  92.     }
  93.     else {
  94.         print STDERR "$ME: $file: not C, C++ or ESQL/C\n";
  95.         next;
  96.     }
  97.  
  98.     undef %D;
  99.     while (<CPP>) {
  100.         next unless @F = /^#\s*(line\s*)?(\d+)\s+"(.+)"/;
  101.         $i = @F[2];
  102.  
  103.         # Perform transformations on include filename here.
  104.         $i =~ s#//*#/#g;
  105.         $i =~ s#^\./##;
  106.  
  107.         $D{$i} = 1 unless grep($i eq $_, $file, "$T.c", "$T.ec");
  108.     }
  109.     close(CPP);
  110.  
  111.     &DEP("\n");
  112.     &DEP("# $file\n");
  113.     foreach (sort BYPATH keys(%D)) {
  114.         next if $OPT_USER && m#^/usr/include/#;
  115.         &DEP("$basename.o: $_\n");
  116.     }
  117.  
  118.     unlink("$T.ec", "$T.c");
  119. }
  120.  
  121. &DEPDONE;
  122. exit(0);
  123.  
  124. sub BYPATH {
  125.     $_ = 0;
  126.     --$_ if $a =~ m#^/#;
  127.     ++$_ if $b =~ m#^/#;
  128.     return $_ if $_;
  129.     return -1 if $a lt $b;
  130.     return 1 if $a gt $b;
  131.     return 0;
  132. }
  133.  
  134. sub DEP {
  135.     $DEP = "" unless defined($DEP);
  136.     for $x (@_) { $DEP .= $x; }
  137. }
  138.  
  139. sub DEPDONE {
  140.     if (defined($OPT_OUTPUT)) {
  141.         die "$ME: can't create $OUT: $!\n"
  142.             unless open(OUT, "> $OPT_OUTPUT");
  143.         print OUT $DEP;
  144.         close(OUT);
  145.     }
  146.     else {
  147.         print $DEP;
  148.     }
  149.     $DEP = "";
  150. }
  151.