home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8712 / mkmf / 1 / src / Mkmf.c < prev   
Encoding:
C/C++ Source or Header  |  1990-07-13  |  5.3 KB  |  222 lines

  1. static char *rcsid = "$Header: Mkmf.c,v 1.7 86/01/12 00:48:26 lepreau Exp $";
  2. #include "sccsid.h"
  3. /*
  4.  * mkmf - makefile editor
  5.  *
  6.  * Author: Peter J. Nicklin
  7.  */
  8. #include "Mkmf.h"
  9. #include "getarg.h"
  10. #include "hash.h"
  11. #include "null.h"
  12. #include "path.h"
  13. #include "target.h"
  14. #include "slist.h"
  15. #include "suffix.h"
  16. #include "system.h"
  17. #include "yesno.h"
  18.  
  19. char *L_MAKEFILE = "l.Makefile";    /* default library makefile template */
  20. char OBJSFX[SUFFIXSIZE] = ".o";        /* default object suffix */
  21. char *P_MAKEFILE = "p.Makefile";    /* default program makefile template */
  22. char *PGN = "mkmf";            /* program name */
  23. char idir[8096];            /* additional include directories. */
  24. int dashI = 0;                /* do additional inc. dirs? */
  25. int CFLAG = YES;            /* makefile creation message flag */
  26. int AFLAG = NO;                /* accept src files w/ leading dots? */
  27. int DEPEND = 1;                /* dependency analysis? */
  28. SLIST *HEADLIST;            /* header file name list */
  29. SLIST *LIBLIST;                /* library pathname list */
  30. SLIST *SRCLIST;                /* source file name list */
  31. HASH *MDEFTABLE;            /* macro definition table */
  32.  
  33. char *DEFRULE[] =            /* default preprocessor rules */
  34.     {
  35. #include "defaultrul.h"
  36.     NULL
  37.     };
  38.  
  39. SUFFIX DEFSFX[] =            /* default suffix list */
  40.     {
  41. #include "defaultsfx.h"
  42.     NULL, 0
  43.     };
  44.  
  45. main(argc, argv)
  46.     int argc;
  47.     char **argv;
  48. {
  49.     char *mfname = NULL;        /* makefile name */
  50.     char mfpath[PATHSIZE];        /* makefile template pathname */
  51.     HASHBLK *htb;            /* hash table block */
  52.     HASHBLK *htinstall();        /* install hash table entry */
  53.     HASHBLK *htlookup();        /* find hash table entry */
  54.     int buildliblist();        /* build list of library pathnames */
  55.     int buildruletable();        /* build table of preprocessor rules */
  56.     int buildsfxtable();        /* build table of suffixes */
  57.     int buildsrclist();        /* build list of source file names */
  58.     int findmf();            /* find makefile */
  59.     int status = 0;            /* exit status */
  60.     int storemacro();        /* store macro definition */
  61.     short iflag = NO;        /* interactive flag */
  62.     TARGET target;            /* type of makefile target */
  63.     void answer();            /* install answer in macro def table */
  64.     void editmf();            /* edit makefile */
  65.  
  66.     target.type = target.dest = VUNKNOWN;
  67.  
  68.     {
  69.     register char *s;        /* option pointer */
  70.     while (--argc > 0 && **++argv == '-')
  71.         {
  72.         for (s = argv[0]+1; *s != '\0'; s++)
  73.             switch (*s)
  74.                 {
  75.                 case 'F':
  76.                     P_MAKEFILE = L_MAKEFILE = GETARG(s);
  77.                     if (P_MAKEFILE==NULL || *P_MAKEFILE=='\0')
  78.                         {
  79.                         warn("missing template name");
  80.                         status = 1;
  81.                         }
  82.                     goto endfor;
  83.                 case 'I':
  84.                     /* check for an argument. */
  85.                     if (*(s + 1) == '\0') {
  86.                         warn("null include directory to -I");
  87.                         status = 1;
  88.                     } else if (dashI) {
  89.                         strcat(idir, " ");
  90.                         strcat(idir, argv[0]);
  91.                     } else {
  92.                         dashI = 1;
  93.                         strcpy(idir, argv[0]);
  94.                     }
  95.                     goto endfor;
  96.                 case 'a':
  97.                     AFLAG = YES;
  98.                     break;
  99.                 case 'c':
  100.                     CFLAG = NO;
  101.                     break;
  102.                 case 'd':
  103.                     /* turn OFF dependency analysis */
  104.                     DEPEND = 0;
  105.                     break;
  106.                 case 'f':
  107.                     mfname = GETARG(s);
  108.                     if (mfname == NULL || *mfname == '\0')
  109.                         {
  110.                         warn("missing makefile name");
  111.                         status = 1;
  112.                         }
  113.                     goto endfor;
  114.                 case 'i':
  115.                     iflag = YES;
  116.                     break;
  117.                 case 'l':
  118.                     target.type = VLIBRARY;
  119.                     break;
  120.                 default:
  121.                     badopt(**argv, *s);
  122.                     status = 1;
  123.                     goto endfor;
  124.                 }
  125.         endfor: continue;
  126.         }
  127.     }
  128.     
  129.     /* initialize macro definition table */
  130.     MDEFTABLE = htinit(MDEFTABLESIZE);
  131.  
  132.     /* get command line macro definitions */
  133.     for (; argc > 0; argc--, argv++)
  134.         if (storemacro(*argv) == NO)
  135.             {
  136.             warns("%s not a macro definition", *argv);
  137.             status = 1;
  138.             }
  139.  
  140.     /* add the additional include dirs, if extant. */
  141.     if (dashI && htinstall(MMOREINCDIRS, idir, VREADONLY, MDEFTABLE) == NULL)
  142.         status = 1;
  143.  
  144.     if (status == 1)
  145.         {
  146.         usage("[-cdil] [-f makefile] [-F template] [-Iinclude dir] [macroname=value...]");
  147.         exit(1);
  148.         }
  149.  
  150.  
  151.     /* determine the makefile name */
  152.     if (mfname == NULL)
  153.         if ((htb = htlookup(MMAKEFILE, MDEFTABLE)) != NULL)
  154.             mfname = htb->h_def;
  155.         else if (FILEXIST("makefile"))
  156.             mfname = "makefile";
  157.         else if (FILEXIST("Makefile"))
  158.             mfname = "Makefile";
  159.         else
  160.             mfname = "Makefile";
  161.     if (htinstall(MMAKEFILE, mfname, VREADWRITE, MDEFTABLE) == NULL)
  162.         exit(1);
  163.  
  164.  
  165.     /* find the makefile (template) and load useful macro definitions */
  166.     if (target.type == VUNKNOWN)
  167.         {
  168.         if (htlookup(MPROGRAM, MDEFTABLE) != NULL)
  169.             target.type = VPROGRAM;
  170.         else if (htlookup(MLIBRARY, MDEFTABLE) != NULL)
  171.             target.type = VLIBRARY;
  172.         }
  173.     if (findmf(mfname, mfpath, &target) == NO)
  174.         exit(1);
  175.     
  176.  
  177.     /* interactive option */
  178.     if (iflag == YES)
  179.         {
  180.         if (htlookup(MPROGRAM, MDEFTABLE) == NULL &&
  181.             htlookup(MLIBRARY, MDEFTABLE) == NULL)
  182.             if (target.type == VPROGRAM)
  183.                 {
  184.                 printf("program name? ");
  185.                 answer(MPROGRAM, VREADWRITE);
  186.                 }
  187.             else if (target.type == VLIBRARY)
  188.                 {
  189.                 printf("library name? ");
  190.                 answer(MLIBRARY, VREADWRITE);
  191.                 }
  192.         if (htlookup(MDESTDIR, MDEFTABLE) == NULL && target.dest == VDESTDIR)
  193.             {
  194.             printf("destination directory? ");
  195.             answer(MDESTDIR, VREADWRITE);
  196.             }
  197.         }
  198.  
  199.     /* build the suffix table */
  200.     if (buildsfxtable() == NO)
  201.         exit(1);
  202.  
  203.     /* build the rule table */
  204.     if (buildruletable() == NO)
  205.         exit(1);
  206.  
  207.     /* build the source code and header file name lists */
  208.     if (buildsrclist() == NO)
  209.         exit(1);
  210.     
  211.  
  212.     /* build the library pathname list */
  213.     if (buildliblist() == NO)
  214.         exit(1);
  215.  
  216.  
  217.     /* edit makefile */
  218.     editmf(mfname, mfpath);
  219.  
  220.     exit(0);
  221. }
  222.