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 / make.c < prev    next >
C/C++ Source or Header  |  1990-08-30  |  3KB  |  154 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.   make.c    within        WBmake
  11.  
  12. */
  13.  
  14. /* Make program to call from WB */
  15.  
  16. #include <stdio.h>
  17. #include "make.h"
  18.  
  19. extern FileInfo *read_makefile();
  20.  
  21. FileInfo *root;
  22.  
  23. /* In built funtions, like depend etc */
  24.  
  25. void
  26. do_depend()
  27.    {/* Search for all the dependencies */
  28.     printf("Make cannot yet do depends\n");
  29.     }
  30.  
  31. void
  32. do_blink()
  33.    {/* Create a <tool>.blink file */
  34.     FILE *b_file;
  35.     ConsCell *dep_obj;
  36.     char b_name[32];
  37.  
  38.     sprintf(b_name,"%s.blink",root->name);
  39.     b_file = fopen(b_name,"w");
  40.     fprintf(b_file,"FROM\n  clibs:crt0.o\n");
  41.     dep_obj = root->depends;
  42.     while(dep_obj)
  43.        {if(dep_obj->car->type == 'o')
  44.             fprintf(b_file,"  %s\n",dep_obj->car->name);
  45.         dep_obj = dep_obj->cdr;
  46.         }
  47.     fprintf(b_file,"TO\n  %s\nLIB\n  clibs:libc.a\n",root->name);
  48.     fclose(b_file);
  49.     }
  50.  
  51. void
  52. do_all()
  53.    {do_depend();
  54.     do_blink();
  55.     }
  56.  
  57. typedef void (*VoidFun)();
  58.  
  59. #define INBUILT_MAX 2
  60.  
  61. VoidFun inbuilt_funs[INBUILT_MAX] =
  62.    {do_depend,do_blink
  63.     };
  64.  
  65. char *inbuilt_names[INBUILT_MAX] =
  66.    {"$*.depend","$*.blink"};
  67.  
  68. int
  69. inbuilt_index(str)
  70.     char *str;
  71.    {int i;
  72.     for(i=0;i<INBUILT_MAX;i++)
  73.         if(strcmp(inbuilt_names[i],str) == 0)
  74.             return(i);
  75.     return(-1);
  76.     }
  77.  
  78. /* Variables set by command options */
  79. char makefile[20] = "Makefile";
  80. char target[32] = "";
  81. int  just_test  = 0;
  82.  
  83. doopts(argc,argv)
  84.     int argc;
  85.     char **argv;
  86.    {/* Step through the options setting the values */
  87.     int i;
  88.  
  89.     for(i=1;i<argc;i++)
  90.        {/* All switches muststart with '-' */
  91.         if(*argv[i]=='-')
  92.            {
  93.             /* Single switches only */
  94.             switch(argv[i][1])
  95.                {case 'f': case 'F':
  96.                     strcpy(makefile,&argv[i][2]);
  97.                     break;
  98.                 case '?':
  99.                     just_test = -1;
  100.                     break;
  101.                 default:
  102.                     printf("Unknown switch %c\n",argv[i][1]);
  103.                     printf("WBmake [-f<makefile>] [target]\n");
  104.                     exit(10);
  105.                 }
  106.             }
  107.           else
  108.            {/* Must be the target name, and last option */
  109.             strcpy(target,argv[i]);
  110.             return;
  111.             }
  112.         }
  113.     }
  114.  
  115. main(argc,argv)
  116.     int argc;
  117.     char **argv;
  118.    {/* Make the object */
  119.     FILE *temp;
  120.     char *temp_file;
  121.     char sys_cmnd[32];
  122.     int  inbuilt;
  123.  
  124.     doopts(argc,argv);
  125.  
  126.     /* Now read the Makefile */
  127.     inbuilt = inbuilt_index(target);
  128.     if(inbuilt==-1)
  129.        {root = read_makefile(makefile,target);
  130.  
  131.         if(root==NULL)
  132.            {printf("Cannot find rules for \"%s\"\n",target);
  133.             exit(10);
  134.             }
  135.         /* Hack to get arround the system return bug */
  136.         temp_file = tmpnam(NULL);
  137.         temp = fopen(temp_file,"w");
  138.         go_make(root,temp);
  139.         fclose(temp);
  140.         sprintf(sys_cmnd,"execute %s",temp_file);
  141.         system(sys_cmnd);
  142.         remove(temp_file);
  143.         }
  144.       else
  145.        {/* One of the inbuilt functions */
  146.         root = read_makefile(makefile,"");
  147.         if(root==NULL)
  148.            {printf("Cannot find object to make\n",target);
  149.             exit(10);
  150.             }
  151.         (*inbuilt_funs[inbuilt])();
  152.         }
  153.     }
  154.