home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CLFEB88.ZIP / DEPENDS.C < prev    next >
Encoding:
Text File  |  1988-02-04  |  15.9 KB  |  471 lines

  1. An Incremental Compilation Package in C by Dave Taylor 
  2.  
  3. /**                             depends.c                               **/
  4.  
  5. /** This program tweaks with the last modified dates of the files in the 
  6.     current directory based on what files include what other files.  The end 
  7.     result of this is that the 'create' program can recompile as needed based 
  8.     on the modification of not just the source file but also the included 
  9.     files too!
  10.  
  11.         Depends allows the following flags;
  12.          -d             show a dependency tree similar to "make' expects
  13.          -m             generate a real 'makefile'
  14.          -n             no execute.  Display commands to be used only.
  15.          -s             show times.  Display last modified times of files.
  16.          -v             verbose.  Running commentary during file parsing.
  17.  
  18.     (C) Copyright 1986, 1987 by Dave Taylor
  19. **/
  20.  
  21. #include <stdio.h>
  22. #include <ndir.h>                       /* directory stuff           */
  23. #include <errno.h>                      /* system error              */
  24. #include <sys/types.h>                  /* more types!!!             */
  25. #include <sys/stat.h>                   /* stat stuff...             */
  26.  
  27. #define MAXFILES        250             /* max files total in dir    */
  28. #define FNAMELEN        20              /* max file name length      */
  29. #define SLEN            80              /* regular length string...  */
  30.  
  31. DIR *dirp;                              /* directory structure...    */
  32. struct direct *dp;                      /* file entry in directory   */
  33.  
  34. struct dir_rec {
  35.         char name[FNAMELEN];            /* name of the file          */
  36.         long time;                      /* last modified time        */
  37.         int  changed;                   /* has it changed?           */
  38.         int  checked;                   /* has it been checked?      */
  39.        } directory[MAXFILES];   
  40.  
  41. int  count      = 0,                    /* how many files in directory? */
  42.      no_execute = 0,                    /* -n (no execute) flag set     */
  43.      verbose    = 0,                    /* -v (verbose) flag set...     */
  44.      dependencies = 0,                  /* -d (dependencies) flag set.. */
  45.      makefile   = 0,                    /* -m (makefile) flag set..     */
  46.      show_times = 0;                    /* -s (show times) flag set     */
  47.  
  48. extern int errno;                       /* system error number          */
  49.  
  50. char *makefile_header[] = { 
  51.         "# Custom makefile generated by the 'depends' program",
  52.         "#",
  53.         "# The following might need to be customized...",
  54.         " ",
  55.         "CC     = cc",è        "CFLAGS = -O",
  56.         "PC     = pc",
  57.         "PFLAGS = -O",
  58.         "LIBS   =",
  59.         "DEFS   =",
  60.         " ",
  61.         "# The following should be changed to the final name of the binary",
  62.         "TARGET = a.out",
  63.         " ",
  64.         "",
  65.         };
  66.  
  67. char *cc_line   = {"\n\t$(CC) $(CFLAGS) $(DEFS) %s\n\n" };
  68. char *pc_line   = {"\n\t$(PC) $(PFLAGS) $(DEFS) %s\n\n" };
  69. char *link_line = {"\t$(CC) $(CFLAGS) $(DEFS) $(OBJS) -o $(TARGET) $(LIBS)\n" };
  70.  
  71. char compile_command[SLEN] = { "" };
  72.  
  73. int   strcmp(), compare();
  74. char *strcpy(), *ctime(), *strncpy();
  75. void  qsort(), exit();
  76.  
  77. main(argc, argv)
  78. int argc;
  79. char *argv[];
  80. {
  81.         register int i;
  82.         
  83.         parse_arguments(argc, argv);    /* starting arguments... */
  84.  
  85.         initially_read_in_directory();                         /* start up stuff        */
  86.  
  87.         get_file_modification_dates();                    /* read file dates       */
  88.  
  89.         /** if the user wants to see the dates spin through and 
  90.             spit 'em all out! **/
  91.  
  92.         if (show_times) 
  93.           for (i=0; i < count; i++)
  94.             if (suffix(".c", directory[i].name) || 
  95.                 suffix(".h", directory[i].name) || 
  96.                 suffix(".p", directory[i].name))   /* a legit file? */
  97.                 printf("%-15s %s", directory[i].name,
  98.                        ctime(&directory[i].time));
  99.  
  100.         /** now let's go through and check all the source files **/
  101.  
  102.         for (i=0; i < count; i++)
  103.           if (suffix(".c", directory[i].name) || 
  104.               suffix(".p", directory[i].name))   /* a source file? */
  105.             figure_out_includes(i, 1);
  106.           
  107.         change_file_dates();    /* based on internal modifications... */
  108.  
  109.         fini();                 /* all done! */è
  110.         exit(0);
  111. }
  112.  
  113. parse_arguments(argc, argv)
  114. int argc;
  115. char *argv[];
  116. {
  117.         /** parse the starting arguments setting the flags etc as specified...
  118.             fail from this routine if bad args! **/
  119.         
  120.         int c;
  121.         extern int optind, opterr;
  122.  
  123.         opterr = 0;     /* supress getopt error message! */
  124.  
  125.         while ((c = getopt(argc, argv, "dhmnvs")) != EOF) 
  126.           switch (c) {
  127.             case 'd' : dependencies++;                  break;
  128.             case 'm' : makefile++; no_execute++;        break;
  129.             case 'n' : no_execute++;                    break;
  130.             case 'v' : verbose++;                       break;
  131.             case 's' : show_times++;                    break;
  132.             case 'h' :
  133.             default  : fprintf(stderr, "Usage: %s [-dhmnvs]\n", argv[0]);
  134.                        fprintf(stderr, "where:\n\
  135.    -d\t\tdepends - show dependencies in 'make'-style format\n\
  136.    -h\t\thelp - give some help on the starting flags\n\
  137.    -m\t\tmake - actually generate a real makefile\n\
  138.    -n\t\tno-action - just list what would be done\n\
  139.    -v\t\tverbose - show all includes in verbose format\n\
  140.    -s\t\tshow times of all files checked\n");
  141.                        exit(1);
  142.           }
  143.  
  144.         if ((makefile && verbose) || (dependencies && verbose)) {
  145.           fprintf(stderr, "That combination of flags doesn't make sense.\n");
  146.           exit(1);
  147.         }
  148. }
  149.         
  150. initially_read_in_directory()
  151. {       
  152.         /* initialize the system variables and read in and sort the current 
  153.            directory... */
  154.  
  155.         dirp = opendir(".");    /* current directory */
  156.  
  157.         while (read_directory(directory[count++].name) && count < MAXFILES)
  158.                 directory[count-1].changed = 0;
  159.  
  160.         if (count >= MAXFILES) {
  161.           fprintf(stderr,
  162.         "*** Warning: read more files than this program can deal with! ***\n");
  163.           fprintf(stderr,è        "***          Depends continuing, but it might be wrong!       ***\n");
  164.         }
  165.  
  166.         qsort(directory, (unsigned) --count, sizeof ( directory[0] ), compare);
  167.  
  168.         if (makefile) 
  169.           initialize_makefile();
  170. }
  171.  
  172. fini()
  173. {
  174.         /* close everything and let's leave! */
  175.  
  176.         if (makefile)
  177.           finish_makefile();
  178.  
  179.         closedir(dirp);
  180. }
  181.  
  182. initialize_makefile()
  183. {
  184.         /** outputs all the leading Makefile information as appropriate **/
  185.  
  186.         /* first off, let's dump the makefile_header stuff... */
  187.  
  188.         register int i, len;
  189.         char     buffer[SLEN];
  190.  
  191.         for (i=0; strlen(makefile_header[i]) > 0; i++)
  192.           puts(makefile_header[i]);
  193.  
  194.         /* next, we'll need to output "HDRS", "SRCS" and "OBJS" ... */
  195.  
  196.         printf("HDRS  = ");
  197.  
  198.         for (len=8, i=0; i < count; i++)
  199.           if (suffix(".h", directory[i].name)) {
  200.             if (strlen(directory[i].name) + len > 75) {
  201.                printf("  \\n\t");
  202.                len = 8;
  203.             }
  204.             printf("%s ", directory[i].name);
  205.             len += strlen(directory[i].name)+1;
  206.           }
  207.         
  208.         putchar('\n');
  209.  
  210.         printf("SRCS  = ");
  211.  
  212.         for (len = 8, i=0; i < count; i++)
  213.           if (suffix(".c", directory[i].name)||suffix(".p", directory[i].name)){
  214.             if (strlen(directory[i].name) + len > 75) {
  215.                printf("  \\n\t");
  216.                len = 8;
  217.             }è            printf("%s ", directory[i].name);
  218.             len += strlen(directory[i].name)+1;
  219.           }
  220.         
  221.         putchar('\n');
  222.  
  223.         printf("OBJS  = ");
  224.  
  225.         for (len = 8, i=0; i < count; i++)
  226.           if (suffix(".c", directory[i].name)||suffix(".p", directory[i].name)){
  227.             if (strlen(directory[i].name) + len > 75) {
  228.                printf("  \\n\t");
  229.                len = 8;
  230.             }
  231.             strcpy(buffer, directory[i].name);
  232.             buffer[strlen(buffer)-1] = 'o';     /* make it a '.o' file! */
  233.             printf("%s ", buffer);
  234.             len += strlen(buffer)+1;
  235.           }
  236.         
  237.         printf("\n\n");
  238.  
  239.         /* and the default binary target... */
  240.  
  241.         printf("$(TARGET): $(OBJS) $(SRCS) $(HDRS)\n%s\n\n",
  242.                 link_line);
  243.  
  244. }
  245.  
  246. finish_makefile()
  247. {
  248.         /** adds some standard stuff to the end of the makefile **/
  249.  
  250.         printf(compile_command);
  251.         printf("clean: $(OBJS)\n\trm -f $(OBJS)\n\n");
  252.         printf("lint:\n\tlint $(SRCS) > LINT.OUT\n\n");
  253.         printf("listing:\n\tlpr $(SRCS)\n");
  254. }
  255.  
  256. get_file_modification_dates()
  257. {
  258.         /** do a 'stat' on each file in this directory, saving the last 
  259.             modified date of each in the directory structure... **/
  260.  
  261.         struct stat buffer;
  262.         register int i;
  263.  
  264.         for (i = 0; i < count ; i++) 
  265.           if ((stat(directory[i].name, &buffer)) != 0) {
  266.             fprintf(stderr,"** could not stat %s [%d] **\n", 
  267.                     directory[i].name, errno);
  268.             exit(errno);
  269.           }
  270.           else {
  271.             directory[i].time = buffer.st_mtime;è          }
  272. }
  273.  
  274. figure_out_includes(index, cnt)
  275. int index, cnt;
  276. {
  277.         /** read the specified file, get all the files that this fellow 
  278.             includes, then change the 'time' of the file entry in the 
  279.             'directory' structure based on the times of the files it includes.
  280.             'cnt' is the nesting depth that we're currently at (for verbose 
  281.              output and other I/O miscellany).
  282.         **/
  283.  
  284.         FILE *thefile;
  285.         char buffer[SLEN];
  286.         int  findex, i;
  287.         
  288.         if (verbose)
  289.           if (cnt == 1)
  290.             printf("Checking file \"%s\"\n", directory[index].name);
  291.           else {
  292.             for (i=0;i<cnt;i++) 
  293.               printf("  ");
  294.             printf("includes file \"%s\"\n", directory[index].name);
  295.           }
  296.          
  297.         if (cnt == 1 && makefile) {
  298.           if (strlen(compile_command) > 0)
  299.             printf(compile_command);
  300.           if (suffix(".c", directory[index].name))
  301.             sprintf(compile_command, cc_line, directory[index].name);
  302.           else
  303.             sprintf(compile_command, pc_line, directory[index].name);
  304.         }
  305.           
  306.         if (dependencies || (makefile && cnt > 1))
  307.           printf("%s%s ", directory[index].name, cnt==1?":":"");
  308.         else if (makefile && cnt == 1) {
  309.           strcpy(buffer, directory[index].name);
  310.           buffer[strlen(buffer)-1] = 'o';
  311.           printf("%s: %s ", buffer, directory[index].name);
  312.         }
  313.  
  314.         if (!verbose && !dependencies && !makefile && directory[index].checked)
  315.           return;
  316.  
  317.         if ((thefile = fopen(directory[index].name,"r")) == NULL) {
  318.           fprintf(stderr,"*** could not open file %s for reading [%d] ! ***\n",
  319.                   directory[index].name, errno);
  320.           exit(errno);
  321.         }
  322.         
  323.         /** okay, now let's loop through this thing and try to get all the
  324.             #include lines... **/
  325. è        while (fgets(buffer, SLEN, thefile) != NULL) {
  326.           if (buffer[0] == '#') /* hmmm...a compiler directive... */
  327.             if ((findex = check_for_include(buffer)) != -1) {
  328.               figure_out_includes(findex, cnt+1);       /* recurse... */ 
  329.               if (directory[index].time < directory[findex].time) { 
  330.                  directory[index].time = directory[findex].time; 
  331.                  directory[index].changed++;
  332.               }
  333.             }
  334.         }
  335.         
  336.         directory[index].checked++;
  337.  
  338.         if (dependencies && cnt==1) printf("\n");
  339.  
  340.         (void) fclose(thefile);
  341. }
  342.           
  343. change_file_dates()
  344. {
  345.         /** Change the files that have the 'changed' bit set, meaning that their
  346.             modified times are wrong based on the files they include... **/
  347.  
  348.         register int i;
  349.         struct utimbuf {
  350.                 long    actime;         /* last accessed time */
  351.                 long    modtime;        /* last modified time */
  352.                } time_struct;
  353.         
  354.         for (i=0; i < count; i++) 
  355.           if (directory[i].changed) {
  356.             printf("\ttouch %s\n", directory[i].name);
  357.             if (! no_execute) {
  358.               time_struct.actime = directory[i].time;
  359.               time_struct.modtime= directory[i].time;
  360.               if (utime(directory[i].name, &time_struct) != 0) {
  361.                 fprintf(stderr,"*** could not change file times [%d] ***\n",
  362.                                errno);
  363.                 exit(errno);
  364.               }
  365.             }
  366.           }
  367. }
  368.  
  369. int
  370. check_for_include(line)
  371. char *line;
  372. {
  373.         /** Line is an m4 directive line - this routine figures out if it is 
  374.             an 'include' line, and if so, what file is being included.  If the 
  375.             file included is contained within this directory, then this routine
  376.             will return the index, otherwise it will return -1 if an error.
  377.         **/
  378.  
  379.         char *line_ptr, *word, *strtok();è        int  i;
  380.  
  381.         line[0] = ' ';  /* remove the '#' */
  382.  
  383.         /* this first section is so we can have "# include" as well
  384.            as "#include" ... we simply get the first word token via
  385.            calls to 'strtok()' */
  386.  
  387.         line_ptr = (char *) line;       /* gets the address */
  388.         
  389.         if ((word = strtok(line_ptr," \t")) != NULL) {
  390.           if (strcmp(word, "include") != 0) 
  391.             return(-1);
  392.         }
  393.         else
  394.           return(-1);
  395.  
  396.         /** to get to here, it must be an include line and the internal strtok 
  397.             pointer must be pointing at the filename surrounded by quotes or 
  398.             '<>' characters... (note that the " in the strtok call will also
  399.             suffice to remove the quotes from the filename too)  **/
  400.  
  401.         if ((word = strtok(NULL, "\t \"")) != NULL) {
  402.           if (word[0] == '<') 
  403.             return(-1);
  404.         }
  405.         else
  406.           return(-1);
  407.  
  408.         /** to get to here, it must have included the file that is specified 
  409.             as 'word' currently, and that file must be a specified file in 
  410.             the local directory. **/
  411.  
  412.         for (i=0; i < strlen(word); i++)
  413.           if (word[i] == '/') 
  414.             return(-1);
  415.  
  416.         /** now, finally, we know that 'word' must be a file in the
  417.             current directory, so we merely need to find it's index
  418.             into the directory structure of this program and return
  419.             it! **/
  420.         
  421.         for (i=0; i < count; i++)
  422.           if (strcmp(word, directory[i].name) == 0) 
  423.             return(i); 
  424.  
  425.         /* it wasn't in there??? */
  426.  
  427.         fprintf(stderr,"*** couldn't find %s in directory! ***\n", 
  428.                         word);
  429.         return(-1);
  430. }
  431.  
  432. int
  433. read_directory(buffer)èchar *buffer;
  434. {
  435.         /** return the next name in the directory... returns zero when 
  436.             we're out of entries. **/
  437.  
  438.         if ((dp = readdir(dirp)) != NULL) 
  439.           strncpy(buffer, dp->d_name, FNAMELEN);
  440.         
  441.         return(dp != NULL? 1 : 0);
  442. }
  443.  
  444. int
  445. suffix(sf, string)
  446. char *sf, *string;
  447. {
  448.         /** returns true iff the suffix of 'string' is 'sf' **/
  449.  
  450.         register int i, j;
  451.  
  452.         i = strlen(string);
  453.         j = strlen(sf);
  454.  
  455.         while (string[i] == sf[j] && j > 0) {
  456.           i--;
  457.           j--;
  458.         }
  459.  
  460.         return(sf[0] == string[i] && j == 0);
  461. }
  462.  
  463. int
  464. compare(a, b)
  465. struct dir_rec *a, *b;
  466. {
  467.         /** strcmp on name field (for sort routine) **/
  468.  
  469.         return( strcmp( a->name, b->name ));
  470. }
  471.