home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ME22-OS2.ZIP / MACROS.ZIP / GENMAKE.M < prev    next >
Text File  |  1988-09-19  |  5KB  |  181 lines

  1. /*
  2.     GENMAKE - a makefile generator macro for the ME Text Editor
  3.     
  4.     (C) Copyright 1988  Marc Adler/Magma Systems    All Rights Reserved
  5.  
  6.     This macro will examine all of the .C files in the current directory
  7.     and will generate a makefile from the files. You are prompted for
  8.     the compile command, name of the exe file to be created, the name of
  9.     the link file, the name of the default libraries to use, and the name
  10.     of the makefile to generate.
  11.     
  12.     You can set the MEC environment variable to be the command string
  13.     which your compiler expects.
  14.  
  15. */
  16.  
  17. genmake()
  18. {
  19.   OrigBuffer = currbuf();
  20.   ObjListLine = 1;
  21.  
  22.   /*
  23.     Ask the user for the compile command.
  24.   */
  25.   if ((compile_cmd = getenv("MEC")) == "")
  26.   {
  27.     if ((compile_cmd = get_tty_str("Compile cmd: ")) == "")
  28.       return;
  29.   }
  30.  
  31.   /*
  32.     Create a temp buffer to hold the makefile.
  33.     Generate an $(OBJS) macro.
  34.   */
  35.   make_buf = setcurrbuf(create_buffer("makegen.$$$"));
  36.   insert("OBJS=\n\n");
  37.   
  38.   /*
  39.     We will search for all C filesin the current directory.
  40.   */
  41.   set_filespec("*.c");
  42.  
  43.   while ((origfname = fname = next_filespec()) != "")
  44.   {
  45.     /* Let the user know which file we are processing. */
  46.     message("Processing file [%s]", fname);
  47.  
  48.     if ((pos = index(fname, ".")) > 0)
  49.       fname = substr(fname, 1, pos-1);
  50.  
  51.     /*
  52.        We will tack another file onto the list of object files defined
  53.        by the $(OBJS) macro.
  54.     */
  55.     goline(ObjListLine);  goeol();
  56.     if (currcol() > 65)
  57.     {
  58.       /* Too many OBJ files. Go onto the next line */
  59.       insert(" \\\n");
  60.       insert(repstr(" ", 10));
  61.       ObjListLine++;
  62.     }
  63.     insert(sprintf("%s.OBJ ", fname));
  64.  
  65.     /*
  66.       Generate the dependency line   "xxx.OBJ:  xxx.C"
  67.     */
  68.     goeof();
  69.     insert(fname);  insert(".OBJ :");
  70.     setcol(20);
  71.     insert(fname);  insert(".C ");
  72.  
  73.     /*
  74.       Read the C file into an invisible buffer.
  75.     */
  76.     c_buf = setcurrbuf(create_buffer(origfname));
  77.     
  78.     /*
  79.       Search the C file for all lines which contain
  80.          #include "xxxx"
  81.     */
  82.     while (fsearch("^#include *\"\\c"))
  83.     {
  84.       cl = substr(currline(), currcol(), 100);
  85.       if ((pos = index(cl, "\"")) > 0)
  86.         cl = substr(cl, 1, pos - 1);
  87.  
  88.       message("Found include file [%s]", cl);
  89.  
  90.       /*
  91.         Insert the include file in the dependency list.
  92.       */
  93.       setcurrbuf(make_buf);
  94.       goeol();
  95.       insert(" "); insert(cl);
  96.  
  97.       setcurrbuf(c_buf);
  98.       if (!down())
  99.         break;
  100.       gobol();
  101.     }
  102.     
  103.     /*
  104.       Generate the compile command.
  105.     */
  106.     setcurrbuf(make_buf);
  107.     goeol();
  108.     insert("\n");
  109.     setcol(20);
  110.     insert(sprintf("%s %s\n\n", compile_cmd, origfname));
  111.  
  112.     delete_buffer(c_buf);
  113.   }
  114.  
  115.   
  116.   /*
  117.     Get the name of the EXE file and the link file. The user needs only to
  118.     respond with the rootname of the EXE file; we will append the extension.
  119.   */
  120.   exename = get_tty_str("What is the name of the exe file? ");
  121.   if (index(exename, ".") == 0)
  122.     exename = strcat(exename, ".exe");
  123.   if ((linkname = get_tty_str("What is the name of the link file? [lnk] ")) == "")
  124.     linkname = "lnk";
  125.  
  126.   /*
  127.     Generate the dependency line and command for the EXE file.
  128.   */
  129.   insert(sprintf("%s : $(OBJS)\n", exename));
  130.   setcol(20);
  131.   insert(sprintf("link /co @%s\n", linkname));
  132.  
  133.   /*
  134.     Copy the $(OBJS) macro into the link file. Get rid of the
  135.     "OBJS=" string and change all \ to +.
  136.   */
  137.   link_buf = create_buffer("link.$$$");
  138.   gobof();
  139.   markline();
  140.   while (strlen(currline()) > 1)
  141.     if (!down())  break;
  142.   up();
  143.   markgroup();
  144.   copy();
  145.   setcurrbuf(link_buf);
  146.   paste();
  147.   gobof();
  148.   for (i = 1;  i <= 5;  i++)  delchar();    /* delete "OBJS=" */
  149.   fsubst("\\", "+", 0);
  150.   goeof();  gobol();
  151.   insert(sprintf("%s\nnul.lst\n", exename));
  152.  
  153.   /*
  154.     Ask the user for the libraries to use.
  155.   */
  156.   if ((libs = get_tty_str("What libs? [Press ENTER for defaults] ")) != "")
  157.     insert(libs);
  158.   insert("\n");
  159.   writefile(linkname);
  160.  
  161.   /*
  162.     Write out the makefile. The default name of the makefile will be
  163.     the rootname of the resulting EXE file.
  164.   */
  165.   if ((pos = index(exename, ".")) > 0)
  166.     exename = substr(exename, 1, pos-1);
  167.   if ((makename = 
  168.        get_tty_str("What is the name of the makefile? [%s] ", exename)) == "")
  169.     makename = exename;
  170.   setcurrbuf(make_buf);
  171.   writefile(makename);
  172.  
  173.   /*
  174.     Get rid of the temp buffers and insure that the focus is with the
  175.     original buffer.
  176.   */
  177.   delete_buffer(link_buf);
  178.   delete_buffer(make_buf);
  179.   show_buffer(OrigBuffer);
  180. }
  181.