home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11645 < prev    next >
Encoding:
Text File  |  1992-07-27  |  5.4 KB  |  169 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!infonode!ingr!b11!davids!davids
  3. From: davids@davids.b11.ingr.com (David Reed Smith)
  4. Subject: Re: #include file dependancies
  5. In-Reply-To: davids@davids.b11.ingr.com's message of Thu, 23 Jul 1992 14: 46:52 GMT
  6. Message-ID: <1992Jul27.185703.13170@b11.b11.ingr.com>
  7. Followup-To: comp.lang.c
  8. Sender: usenet@b11.b11.ingr.com (Usenet Network)
  9. Reply-To: davids@davids.b11.ingr.com
  10. Organization: Intergraph Corp., Huntsville, AL
  11. References: <1992Jul23.144652.1785@b11.b11.ingr.com>
  12. Date: Mon, 27 Jul 1992 18:57:03 GMT
  13. Lines: 154
  14.  
  15. Ok, I've gotten enough requests, and I've gotten enough time to post
  16. my Perl script which figures out which include files are not needed.
  17.  
  18. As I stated before here's how the process works:
  19.  
  20. (1) You run mkmf (or something similar) to figure out the
  21.     dependencies.  mkmf is a makefile generator.  The first time you
  22.     run mkmf, you will need to tell it where to look for includes, if
  23.     you use include directories other than /usr/include.  Like this:
  24.  
  25.     mkmf "CFLAGS=-I../include -I../common/include -O"
  26.  
  27.     mkmf is present in comp.sources.misc, volume 1.  One possible
  28.     location:
  29.  
  30.     Host wuarchive.wustl.edu
  31.  
  32.         Location: /usenet/comp.sources.misc/volume01
  33.           DIRECTORY drwxrwxr-x        512  Aug 29 1991  mkmf
  34.  
  35. (2) Run mkid on all the source files, all the local include files, and
  36.     all the system includes - even system includes included by other
  37.     system includes (which mkmf figures out for you).
  38.  
  39.     In Makefiles generated by mkmf, there are 3 variables we are
  40.     interested in, 'SRCS', 'EXTHDRS' and 'HDRS'.  'SRCS' are all the C
  41.     source files, 'EXTHDRS' are all the include files not in the
  42.     current directory, and 'HDRS' are the include files in the current
  43.     directory.  It is easy to set up a Makefile target to do this for
  44.     you, like this:
  45.  
  46.     id:;        mkid $(SRCS) $(EXTHDRS) $(HDRS)
  47.  
  48.  
  49.     mkid is, according to the man page:
  50.  
  51.       Mkid builds a database that stores numbers and identifier
  52.           names, as well as the names of the files in which they
  53.           occur.  Mkid is particularly useful with large programs
  54.           spread out across multiple source files.  It serves as an
  55.           aid for program maintenance and as a guide for perusing a
  56.           program.
  57.  
  58.     mkid2 (the second version) was posted to comp.sources.unix, volume
  59.     24.  One possible location:
  60.  
  61.     Host wuarchive.wustl.edu
  62.  
  63.         Location: /usenet/comp.sources.unix/volume24
  64.           DIRECTORY drwxrwxr-x        512  Jun  7 1991  mkid2
  65.  
  66. (3) Run the included perl script, called 'check_includes', on a C
  67.     file.  It uses the 'fid' program (included in the mkid package) to
  68.     query the database for all the identifiers that occur both in a
  69.     include file and the C file.  If no identifiers occur in the
  70.     include file that are used by the C file, it reports that the
  71.     include file is unused.  This process is continued for all the
  72.     include files that the C file uses.  You need to give the perl
  73.     script the same '-Idirectory' arguments that you would give to cc.
  74.  
  75.     Now, this was one of the first perl scripts I wrote, and I haven't
  76.     touched it since, so I hope it makes since.
  77.  
  78. -------------------start of check_includes--------------------------------
  79. #!/usr/bin/perl
  80. #
  81. # Program:    check_includes
  82. # Date:        20 June 1990
  83. # Version:    1.00
  84. # Last Mod:    20 June 1990
  85. # Author:    David Reed Smith (davids@davids.b11.ingr.com)
  86. #
  87. # Usage:
  88. #    check_includes [-v] [-Idirectory] file(s)
  89. #        -Idirectory: adds "directory" to list of directories
  90. #            where check_includes searches to find include
  91. #            files
  92. #
  93. # Purpose:
  94. #    Perl script to see if the include files a source file includes
  95. #    are really necessary.  Uses the "mkid" and "fid" programs.
  96. #    "mkid" must be run before this script can execute.
  97. #
  98. # Bugs:
  99. #    
  100. #
  101.  
  102.  
  103. $#ARGV >= 0 || die "Usage: $0 [-Idirectory] file(s)\n";
  104.  
  105. @Dirs = ();
  106.  
  107. while ($_ = $ARGV[0], /^-/) {
  108.   shift;
  109.   /^-I(.*)/ && (push (@Dirs, $1));
  110. }
  111. foreach $file (@ARGV) {
  112.   print "file: $file...\n";
  113.   open (FILE, "$file");
  114.   while (<FILE>) {
  115.     if (/^#\s*include\s+/) {
  116.     # find include file name
  117.     if (/"(.*)"/) {            # "name" format
  118.       $inc_file = $1;
  119.       if (!-e $inc_file) {        # if file doesn't exist
  120.         SEARCH: foreach $dir (@Dirs) {    # look in all directories
  121.           next SEARCH if (!-e "$dir/$inc_file");
  122.           $inc_file = "$dir/$inc_file";
  123.         }
  124.         (-e $inc_file) || die "Can't find $inc_file\n";
  125.       }
  126.       }
  127.     elsif (/<(.*)>/) {        # <name> format
  128.       $inc_file = "/usr/include/" . $1;
  129.       (-e $inc_file) || die "Can't find $inc_file\n";
  130.     }
  131.     print "... includes file $inc_file";
  132.     open (SYMBOLS, "fid $file $inc_file |");
  133.     $used = 0;
  134.     while (<SYMBOLS>) {
  135.       # don't consider standard C symbols like "char" or "int"
  136.       if (/^char$/ || /^extern$/ || /^int$/ || /^long$/
  137.           || /^struct$/ || /^sizeof$/ || /^type$/
  138.           || /^ultrix$/ || /^unsigned$/ || /^void$/
  139.           || /^DEBUG$/ || /^\d+$/ || /^0x\d+$/) {
  140.         # do nothing
  141.       }
  142.       else {
  143.         if (! $used) {
  144.           print "\n";
  145.         }
  146.         $used = 1;
  147.         chop ($_);
  148.         print "$_ ";
  149.       }
  150.     }
  151.     if (! $used) {
  152.       print ": UNUSED";
  153.     }
  154.     print "\n";
  155.     close (SYMBOLS);
  156.       }
  157.   }
  158.   close (FILE);
  159. }
  160. -------------------end of check_includes-----------------------------------
  161.  
  162. davids
  163. --
  164. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  165. David Smith                davids@davids.b11.ingr.com (Internet)
  166. Intergraph Corporation             uunet!ingr!b11!davids!davids (UUCP)
  167. Huntsville, AL 35807            (205) 730-5752
  168. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  169.