home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff384.lzh / NorthC / Example2.LZH / make / gomake.c < prev    next >
C/C++ Source or Header  |  1990-08-30  |  5KB  |  199 lines

  1. /*
  2.   (c) 1990 S.Hawtin.
  3.   Permission is granted to copy this file provided that:
  4.    1) It is not used for commercial gain
  5.    2) This notice is included in all copies
  6.    3) Altered copies are marked as such.
  7.  
  8.   No liability is accepted for the contents of the file.
  9.  
  10.   gomake.c    within        WBmake
  11.  
  12. */
  13.  
  14. /* Actually run the make over the dependancy tree */
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <libraries/dos.h>
  19. #include "make.h"
  20.  
  21. extern long dstodate();
  22. extern ConsCell *defaults;
  23. extern int just_test;
  24. extern ConsCell *cons();
  25. extern FileInfo *find_file();
  26.  
  27. add_cmnd(fptr,base,file,cmnd)
  28.     FILE *fptr;
  29.     char *base;
  30.     FileInfo *file;
  31.     char *cmnd;
  32.    {/* Construct the command string and add it to the file */
  33.     char full_cmnd[256];
  34.     int  i,oc,ic;
  35.  
  36.     oc = 0;
  37.     for(i=0;cmnd[i]!='\0';i++)
  38.        {/* Go through the string */
  39.         if(cmnd[i]=='$')
  40.            {/* Some form of variable */
  41.             i++;
  42.             switch(cmnd[i])
  43.                {case '*':
  44.                     for(ic=0;base[ic]!='\0';ic++)
  45.                         full_cmnd[oc++] = base[ic];
  46.                     break;
  47.                 case '$':
  48.                     full_cmnd[oc++] = '$';
  49.                     break;
  50.                 default:
  51.                     printf("Cannot do $%c yet\n",cmnd[i]);
  52.                 }
  53.             }
  54.           else
  55.             full_cmnd[oc++] = cmnd[i];
  56.         }
  57.     full_cmnd[oc] = '\0';
  58.  
  59.     /* So full_cmnd now has the complete command, all variables have
  60.        been given values */
  61.     if(fptr)
  62.        {fprintf(fptr,"echo \"%s\"\n",full_cmnd);
  63.         if(just_test==0)
  64.             fprintf(fptr,"%s\n",full_cmnd);
  65.         }
  66. /*  system(curr->contents); */
  67.     }
  68.  
  69. get_base(str,file)
  70.     char *str;
  71.     FileInfo *file;
  72.    {/* Get the base file name into the buffer, with the suffix removed */
  73.     char *temp;
  74.  
  75.     strcpy(str,file->name);
  76.     temp = strrchr(str,'.');
  77.     if(temp)
  78.         *temp = '\0';
  79.     }
  80.  
  81. really_make(file,fptr)
  82.     FileInfo *file;
  83.     FILE     *fptr;
  84.    {/* First try to run the create string */
  85.     String *curr;
  86.     ConsCell *this_cons;
  87.     char base_name[32];
  88.  
  89.     get_base(base_name,file);
  90.  
  91.     curr = file->create;
  92.     if(curr)
  93.        {/* Run each line of the string as a command */
  94.         while(curr)
  95.            {
  96.             add_cmnd(fptr,base_name,file,curr->contents);
  97.             curr = curr->next;
  98.             }
  99.         }
  100.       else
  101.        {
  102.         printf("Don't know how to make %s\n",file->name);
  103.         }
  104.     }
  105.  
  106. add_create(file,on_dep)
  107.     FileInfo *file;
  108.     int      on_dep;
  109.    {/* Find the default create method for this file and add it to
  110.       the file's create */
  111.     extern APTR Lock();
  112.     APTR lock;
  113.     ConsCell *def_list;
  114.  
  115.     def_list = defaults;
  116.     while(def_list)
  117.        {/* Is this default method for our file type? */
  118.         if(def_list->car->type == file->type)
  119.            {/* Can we find the source file */
  120.             char srcname[32];
  121.             char extens[12];
  122.  
  123.             get_base(srcname,file);
  124.             get_base(extens,def_list->car);
  125.             strcat(srcname,extens);
  126.  
  127.             if(on_dep)
  128.                {/* Look on dependancy list */
  129.                 ConsCell *dep_list;
  130.                 dep_list = file->depends;
  131.                 while(dep_list)
  132.                    {if(strcmp(srcname,dep_list->car->name)==0)
  133.                        {file->create = def_list->car->create;
  134.                         return;
  135.                         }
  136.                     dep_list = dep_list->cdr;
  137.                     }
  138.                 }
  139.               else
  140.                {/* So we now have to look for the file within the current
  141.                    directory */
  142.                 lock = Lock(srcname,ACCESS_READ);
  143.                 if(lock!=0)
  144.                    {/* Got the lock, the file exists */
  145.                     UnLock(lock);
  146.                     file->depends = cons(find_file(srcname,-1),file->depends);
  147.                     file->create = def_list->car->create;
  148.                     return;
  149.                     }
  150.                 }
  151.             }
  152.         def_list = def_list->cdr;
  153.         }
  154.     if(on_dep)
  155.         add_create(file,0);
  156.     }
  157.  
  158. go_make(file,fptr)
  159.     FileInfo *file;
  160.     FILE *fptr;
  161.    {/* Recursive search down the tree of dependancies */
  162.     ConsCell *depend;
  163.     struct DateStamp ds;
  164.     long     last_src = 0;
  165.  
  166.     if(file->flags & MAKING)
  167.        {printf("Dependancy loop with %s\n",file->name);
  168.         return;
  169.         }
  170.     file->flags |= MAKING;
  171.     /* Do we need to work out how to make this file ? */
  172.     if(file->create == NULL)
  173.        {/* No create method, go look the file up */
  174.         add_create(file,-1);
  175.         }
  176.     /* Make all the files this file depends on */
  177.     depend = file->depends;
  178.     while(depend)
  179.        {go_make(depend->car,fptr);
  180.         if(depend->car->date > last_src)
  181.             last_src = depend->car->date;
  182.         depend = depend->cdr;
  183.         }
  184.     /* OK now should we make this one ? */
  185.     if(last_src > file->date)
  186.        {/* Make the file */
  187.         really_make(file,fptr);
  188.         /* Now set up the date to reflect the fact we have just made it */
  189.         DateStamp(&ds);
  190.         file->date = dstodate(&ds);
  191.         }
  192.     if(strcmp(file->name,"always")==0)
  193.        {/* Always update always */
  194.         DateStamp(&ds);
  195.         file->date = dstodate(&ds);
  196.         }
  197.     file->flags &= ~MAKING;
  198.     }
  199.