home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / makemak.lzh / MAKEMAK.C < prev    next >
C/C++ Source or Header  |  1988-02-21  |  14KB  |  546 lines

  1. /*
  2.     makemak.c --    This program will generate a make file suitable for use
  3.                         with Microsoft C or MASM. It makes an array of the names
  4.                          of all the files in the current directory. It than searches
  5.                          through the array for files that have an extension of C or
  6.                          ASM. If so, it prints a make style "filename.obj: filename.c"
  7.                          dependancy line. It then searches through the file to see if
  8.                         any local include files are specified. If    so, they are also
  9.                         specified in the dependancy list. Finally, the link line is
  10.                         printed.
  11.  
  12.                          Several make macro definitions are also printed. The source
  13.                          of these definitions can come from several places. First, a
  14.                         set of default definitions are built into the program. Second,
  15.                         three environment variables, "CL, LINK, and MASM" are checked
  16.                         and any values there become macro definitions. Lastly, a
  17.                         configuration file is checked and any definitions there will
  18.                         be included. This last set will override any definitions
  19.                         set internally or from the environment.
  20.  
  21.                         Now for the caveats. 1) THIS THING IS BY NO MEANS PERFECT.
  22.                         2) I have found that it will perform acceptably with little
  23.                         to    no editing of the generated make file. You may or may not
  24.                         find this to be the case. It will be a function of your own
  25.                         programming style and needs. 3) It requires that each program
  26.                         that you generate a make file for has it's own directory.
  27.                         If you do not use a seperate directory for each program, you
  28.                         run the risk of having a large number of source and object
  29.                         files    in your dependancy lists that are not related to the
  30.                         program you are trying to make. 4) You get what you pay for.
  31.  
  32.     makemak allows the following flags;
  33.         -d            show a dependency tree similar to "make' expects
  34.         -s            show times.  Display last modified times of files.
  35.         -v            verbose.  Running commentary during file parsing.
  36.         -h,H        show a brief list of options
  37.  
  38.     Much of this program is based on an article by Dave Taylor in the February,
  39.     1988 issue of Computer Language. It has been extensively modified for DOS.
  40.     The getopt function is from Augie Hansen's book "Proficient C".
  41.  
  42.     Compiled under Microsoft C 5.0
  43. */
  44.  
  45. /*    Last revised : Sun February 21, 1988 at 10:25:36 pm*/
  46. /* Loran W. Richardson
  47.     7083 Fairways Drive
  48.     Longmont, CO 80501
  49.     (303) 939-9743
  50.     CIS 70441,3037
  51. */
  52.  
  53. #include    <string.h>                /* string stuff        */
  54. #include <stdio.h>                /* standard stuff        */
  55. #include    <stdlib.h>                /* standard lib stuff*/
  56. #include <direct.h>                /* directory stuff    */
  57. #include    <dos.h>                    /* DOS stuff            */
  58. #include <errno.h>                /* system error        */
  59. #include    <search.h>
  60. #include <sys\types.h>            /* more types!!!        */
  61. #include <sys\stat.h>            /* stat stuff...        */
  62. #include    <sys\utime.h>
  63. #include    "makemak.h"
  64.  
  65. struct find_t dp;                    /* file entry in directory   */
  66.  
  67. struct dir_rec {
  68.     char name[FNAMELEN];            /* name of the file         */
  69.     long time;                        /* last modified time     */
  70.     int  checked;                    /* has it been checked?   */
  71.        } directory[MAXFILES];
  72.  
  73. int    count    = 0,                    /* how many files in directory?         */
  74.         verbose    = 0,                /* -v (verbose) flag set...            */
  75.         dependencies = 0,            /* -d (dependencies) flag set..         */
  76.         show_times = 0;            /* -s (show times) flag set            */
  77.         makefile = 1;                /* if no other options, make makefile */
  78.         srcs = 0;                    /* > 0 if source files are present    */
  79.         objs = 0;                    /* > 0 if obj files are present        */
  80.         hdrs = 0;                    /* > 0 if header files are present    */
  81.  
  82. extern int errno;                    /* system error number                    */
  83. extern char defaults[][SLEN];
  84. extern char mak_defs[][FNAMELEN];
  85. static char pgm[_MAX_FNAME] = {"makemak"};
  86.  
  87. char *cc_line    = {"\n\t$(CC) $(C_OPTS) $(CDEFS) %s\n\n" };
  88. char *mc_line    = {"\n\t$(MC) $(M_OPTS) $(MDEFS) %s;\n\n" };
  89. char *link_line = {"\t$(LINK) $(L_OPTS) $(OBJS), $(TARGET), $(LMAP), $(LIBS);\n" };
  90.  
  91. char compile_command[SLEN] = { "" };
  92.  
  93.  
  94. void     initially_read_in_directory();
  95. void     initialize_makefile();
  96. void     finish_makefile();
  97. void     get_file_modification_dates();
  98. void     figure_out_includes(int, int);
  99. int     check_for_include(char *);
  100. int     read_directory(char *);
  101. int     suffix(char *, char *);
  102. int    compare();                    /* must be declared w/o args for qsort */
  103. extern int getopt(int, char **, char *);
  104. extern void    mak_help(char *);
  105.  
  106.  
  107.  
  108. main(int argc, char *argv[])
  109. {
  110.     extern char    *getpname(char *, char *);
  111.     extern int mak_cfg(char *);
  112.  
  113.     extern int optind, opterr;
  114.     extern char *optarg;
  115.  
  116.     register int i;
  117.     int c;
  118.     int arg_err = 0;
  119.  
  120.     if (_osmajor < 2)
  121.         fprintf(stderr, "%s: makemak requires DOS 2.00 or later.\n", pgm);
  122.  
  123.     if (_osmajor >= 3)
  124.         getpname(*argv, pgm);
  125.  
  126.     opterr = 0;                            /* supress getopt error message! */
  127.  
  128.     while ((c = getopt(argc, argv, "dhHsv")) != EOF)
  129.         switch (c)
  130.             {
  131.             case 'd' :
  132.                     dependencies++;
  133.                     makefile = FALSE;
  134.                     break;
  135.             case 'v' :
  136.                     verbose++;
  137.                     makefile = FALSE;
  138.                     break;
  139.             case 's' :
  140.                     show_times++;
  141.                     makefile = FALSE;
  142.                     break;
  143.             case 'h' :
  144.             case 'H' :
  145.             case '?' : 
  146.                     arg_err = TRUE;
  147.                     break;
  148.             }
  149.  
  150.     if (dependencies && verbose)
  151.         {
  152.         fprintf(stderr, "%s: Invalid option combination.\n", pgm);
  153.         exit(4);
  154.         }
  155.     
  156.     argc -= optind;
  157.     argv += optind;
  158.  
  159.     if ((makefile && (argc == 0)) || arg_err)
  160.         {
  161.         mak_help(pgm);
  162.         exit(3);
  163.         }
  164.  
  165.     strcpy(defaults[TARGET], *argv); 
  166.  
  167.     if (mak_cfg(pgm) != 0)
  168.         {
  169.         fprintf(stderr, "%s: bad dependancy configuration file.\n", pgm);
  170.         exit(6);
  171.         }
  172.  
  173.     initially_read_in_directory();        /* start up stuff                */
  174.  
  175.     if (makefile)
  176.       initialize_makefile();
  177.  
  178. /*
  179.     if the user wants to see the dates spin through and
  180.     spit 'em all out!
  181. */
  182.  
  183.     if (show_times)
  184.         {
  185.         get_file_modification_dates();        /* read file dates           */
  186.         for (i=0; i < count; i++)
  187.             if (suffix(".c", directory[i].name) ||
  188.                 suffix(".h", directory[i].name) ||
  189.                 suffix(".asm", directory[i].name))   /* a legit file? */
  190.                 printf("%-15s %s", directory[i].name,
  191.                     ctime(&directory[i].time));
  192.         }
  193.  
  194. /* now let's go through and check all the source files */
  195.  
  196.     for (i=0; i < count; i++)
  197.         if (suffix(".c", directory[i].name) || suffix(".asm", directory[i].name))
  198.             figure_out_includes(i, 1);            /* a source file? */
  199.  
  200.     if (makefile)
  201.         finish_makefile();
  202.  
  203.     exit(0);
  204. }
  205.  
  206.  
  207.  
  208. void initially_read_in_directory()
  209. {
  210. /*
  211.     initialize the system variables and read in and sort the current directory...
  212. */
  213.  
  214.     if (_dos_findfirst(".\\*.*", _A_NORMAL, &dp) == 0)    /* current directory */
  215.         strcpy(directory[count++].name, strlwr(dp.name));
  216.  
  217.     while (read_directory(directory[count++].name) && count < MAXFILES)
  218.         ;
  219.  
  220.     if (count >= MAXFILES)
  221.         {
  222.         fprintf(stderr,
  223.                     "%s: Warning: more files than this program can deal with!\n",
  224.                      pgm);
  225.         fprintf(stderr,
  226.                     "%s continuing, but it might be wrong!\n", pgm);
  227.         }
  228.  
  229.     qsort((void *)directory, (size_t) --count, sizeof (directory[0]), compare);
  230. }
  231.  
  232.  
  233.  
  234. void initialize_makefile()
  235. {
  236. /*
  237.     outputs all the leading Makefile information as appropriate
  238. */
  239.  
  240. /* first off, let's dump the makefile_header stuff... */
  241.  
  242.     register int i, len;
  243.     FLAGS k;
  244.     char     buffer[SLEN];
  245.  
  246.     printf("# Custom make file generated by %s for %s\n\n", pgm, defaults[TARGET]);
  247.     for (k = TARGET; k <= LIBS; k++)
  248.         printf("%s = %s\n", mak_defs[k], defaults[k]);
  249.  
  250. /* next, we'll need to output "HDRS", "SRCS" and "OBJS" ... */
  251.  
  252.     printf("HDRS = ");
  253.  
  254.     for (len=8, i=0; i < count; i++)
  255.         if (suffix(".h", directory[i].name))
  256.             {
  257.             ++hdrs;
  258.             if (strlen(directory[i].name) + len > 75)
  259.                 {
  260.                 printf("  \\\n\t");
  261.                 len = 8;
  262.                 }
  263.             printf("%s ", directory[i].name);
  264.             len += strlen(directory[i].name)+1;
  265.             }
  266.  
  267.     putchar('\n');
  268.  
  269.     printf("SRCS = ");
  270.  
  271.     for (len = 8, i=0; i < count; i++)
  272.         if (suffix(".c", directory[i].name) || suffix(".asm", directory[i].name))
  273.             {
  274.             ++srcs;
  275.             if (strlen(directory[i].name) + len > 75)
  276.                 {
  277.                 printf("  \\\n\t");
  278.                 len = 8;
  279.                 }
  280.             printf("%s ", directory[i].name);
  281.             len += strlen(directory[i].name)+1;
  282.             }
  283.  
  284.     putchar('\n');
  285.  
  286.     printf("OBJS = ");
  287.  
  288.     for (len = 8, i=0; i < count; i++)
  289.         if (suffix(".c", directory[i].name) || suffix(".asm", directory[i].name))
  290.             {
  291.             ++objs;
  292.             if (strlen(directory[i].name) + len > 75)
  293.                 {
  294.                 printf("  \\\n\t");
  295.                 len = 8;
  296.                 }
  297.             strcpy(buffer, directory[i].name);
  298.             strcpy(strchr(buffer, '.'), ".obj");     /* make it a '.obj' file! */
  299.             printf("%s ", buffer);
  300.             len += strlen(buffer)+1;
  301.             }
  302.  
  303.     printf(" \n\n");
  304.  
  305. }
  306.  
  307.  
  308.  
  309. void finish_makefile()
  310. {
  311. /*
  312.     adds some standard stuff to the end of the makefile
  313. */
  314.  
  315.     printf(compile_command);
  316.  
  317. /* and the default binary target... */
  318.  
  319.     printf("$(TARGET):\t");
  320.     printf("%s", (objs ? "$(OBJS) " : ""));
  321.     printf("%s", (srcs ? "$(SRCS) " : ""));
  322.     printf("%s", (hdrs ? "$(HDRS)\n" : "\n"));
  323.     printf("%s", link_line);
  324. }
  325.  
  326.  
  327.  
  328. void get_file_modification_dates()
  329. {
  330. /*
  331.     do a 'stat' on each file in this directory, saving the last
  332.     modified date of each in the directory structure...
  333. */
  334.  
  335.     struct stat buffer;
  336.     register int i;
  337.  
  338.     for (i = 0; i < count ; i++)
  339.         if ((stat(directory[i].name, &buffer)) != 0)
  340.             {
  341.             fprintf(stderr,"%s: could not stat %s [%d] **\n",
  342.                 pgm, directory[i].name, errno);
  343.             exit(errno);
  344.             }
  345.         else
  346.             {
  347.             directory[i].time = buffer.st_mtime;
  348.             }
  349. }
  350.  
  351.  
  352.  
  353. void figure_out_includes(int index, int cnt)
  354. {
  355. /*
  356.         read the specified file, get all the files that this fellow
  357.     includes, then change the 'time' of the file entry in the
  358.     'directory' structure based on the times of the files it includes.
  359.     'cnt' is the nesting depth that we're currently at (for verbose
  360.     output and other I/O miscellany).
  361. */
  362.  
  363.     FILE *thefile;
  364.     char buffer[SLEN];
  365.     int  findex, i;
  366.  
  367.     if (verbose)
  368.         if (cnt == 1)
  369.             printf("Checking file \"%s\"\n", directory[index].name);
  370.         else
  371.             {
  372.             for (i=0;i<cnt;i++)
  373.                 printf("  ");
  374.             printf("includes file \"%s\"\n", directory[index].name);
  375.             }
  376.  
  377.     if (cnt == 1 && makefile)
  378.         {
  379.         if (strlen(compile_command) > 0)
  380.             printf(compile_command);
  381.         if (suffix(".c", directory[index].name))
  382.             sprintf(compile_command, cc_line, directory[index].name);
  383.         else if (suffix(".asm", directory[index].name))
  384.             sprintf(compile_command, mc_line, directory[index].name);
  385.         }
  386.  
  387.     if (dependencies || (makefile && cnt > 1))
  388.         printf("%s%s ", directory[index].name, cnt == 1 ? ":" : "");
  389.     else if (makefile && cnt == 1)
  390.         {
  391.         strcpy(buffer, directory[index].name);
  392.         strcpy(strchr(buffer, '.'), ".obj");
  393.         printf("%s:\t%s ", buffer, directory[index].name);
  394.         }
  395.  
  396.     if (!verbose && !dependencies && !makefile && directory[index].checked)
  397.         return;
  398.  
  399.     if ((thefile = fopen(directory[index].name,"r")) == NULL)
  400.         {
  401.         fprintf(stderr,"%s: could not open file %s for reading [%d] !\n",
  402.             pgm, directory[index].name, errno);
  403.         exit(errno);
  404.         }
  405.  
  406. /*
  407.     okay, now let's loop through this thing and try to get all the #include lines...
  408. */
  409.  
  410.     while (fgets(buffer, SLEN, thefile) != NULL)
  411.         {
  412.         if (buffer[0] == '#') /* hmmm...a compiler directive... */
  413.             if ((findex = check_for_include(buffer)) != -1)
  414.                 {
  415.                 figure_out_includes(findex, cnt+1);    /* recurse... */
  416.                 }
  417.         }
  418.  
  419.         directory[index].checked++;
  420.  
  421.         if (dependencies && cnt==1) printf("\n");
  422.  
  423.         (void) fclose(thefile);
  424. }
  425.  
  426.  
  427.  
  428. int check_for_include(char *line)
  429. {
  430. /*
  431.     Line is an m4 directive line - this routine figures out if it is
  432.     an 'include' line, and if so, what file is being included.  If the
  433.     file included is contained within this directory, then this routine
  434.     will return the index, otherwise it will return -1 if an error.
  435. */
  436.  
  437.     char *line_ptr, *word, *strtok();
  438.     int  i;
  439.  
  440.     line[0] = ' ';  /* remove the '#' */
  441.  
  442. /*
  443.     this first section is so we can have "# include" as well
  444.     as "#include" ... we simply get the first word token via
  445.     calls to 'strtok()'
  446. */
  447.  
  448.     line_ptr = (char *) line;    /* gets the address */
  449.  
  450.     if ((word = strtok(line_ptr," \t")) != NULL)
  451.         {
  452.         if (strcmp(word, "include") != 0)
  453.             return(-1);
  454.         }
  455.     else
  456.         return(-1);
  457.  
  458. /*
  459.     to get to here, it must be an include line and the internal strtok
  460.     pointer must be pointing at the filename surrounded by quotes or
  461.     '<>' characters... (note that the " in the strtok call will also
  462.     suffice to remove the quotes from the filename too)
  463. */
  464.  
  465.     if ((word = strtok(NULL, "\t \"")) != NULL)
  466.         {
  467.         if (word[0] == '<')
  468.             return(-1);
  469.         }
  470.     else
  471.         return(-1);
  472.  
  473. /*
  474.     to get to here, it must have included the file that is specified
  475.     as 'word' currently, and that file must be a specified file in
  476.     the local directory.
  477. */
  478.  
  479.     for (i=0; i < strlen(word); i++)
  480.         if (word[i] == '\\')
  481.             return(-1);
  482.  
  483. /*
  484.     now, finally, we know that 'word' must be a file in the
  485.     current directory, so we merely need to find it's index
  486.     into the directory structure of this program and return it!
  487. */
  488.  
  489.     for (i=0; i < count; i++)
  490.         if (strcmpi(word, directory[i].name) == 0)
  491.             return(i);
  492.  
  493. /* it wasn't in there??? */
  494.  
  495.     fprintf(stderr,"%s: %s not in directory.\n",
  496.             pgm, word);
  497.     return(-1);
  498. }
  499.  
  500.  
  501.  
  502. int read_directory(char *buffer)
  503. {
  504. /*
  505.     return the next name in the directory... returns zero when
  506.     we're out of entries.
  507. */
  508.  
  509.     if (_dos_findnext(&dp) == 0)
  510.         {
  511.         strcpy(buffer, strlwr(dp.name));
  512.         return(1);
  513.         }
  514.     
  515.     return(0);
  516. }
  517.  
  518.  
  519.  
  520. int suffix(char *sf, char *string)
  521. {
  522. /* returns true iff the suffix of 'string' is 'sf' */
  523.  
  524.     register int i, j;
  525.  
  526.     i = strlen(string);
  527.     j = strlen(sf);
  528.  
  529.     while (string[i] == sf[j] && j > 0)
  530.         {
  531.         i--;
  532.         j--;
  533.         }
  534.  
  535.     return(sf[0] == string[i] && j == 0);
  536. }
  537.  
  538.  
  539.  
  540. int compare(struct dir_rec *a, struct dir_rec *b)
  541. {
  542. /* strcmp on name field (for sort routine) */
  543.  
  544.     return( strcmp(a->name, b->name));
  545. }
  546.