home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume3 / mmake / mmake.c next >
C/C++ Source or Header  |  1989-02-03  |  5KB  |  148 lines

  1. /*****************************************************************************
  2.  *                                                                           *
  3.  * mmake - Program to support make on multiple systems.  This program takes  *
  4.  *         the file MMakefile and runs the C preprocessor on it.  The result *
  5.  *         is written to Makefile, and then "make" is invoked.  Note that    *
  6.  *         the C preprocessor is only invoked if the file MMakefile is newer *
  7.  *         then Makefile.                                                    *
  8.  *                                                                           *
  9.  *         This program is designed to allow the same makefile to be used on *
  10.  *         unix(r) systems and MS-DOS(r) systems.  In order to make this     *
  11.  *         program portable across systems, it is not very smart about the   *
  12.  *         way it does certain things.  For example, in order to run the     *
  13.  *         C preprocessor, we use a system() call, rather than something     *
  14.  *         line popen() which is not available on MS-DOS.                    *
  15.  *                                                                           *
  16.  *         The file names were chosen so that once mmake has run and created *
  17.  *         Makefile, you can run "make" rather than "mmake" unless MMakefile *
  18.  *         changes.                                                          *
  19.  *                                                                           *
  20.  *         Mmake has no arguments, but certain command line arguments are    *
  21.  *         send to either CPP or make as appropriate.  Any argument which    *
  22.  *         begines with "-D", "-I", or "-U" is sent to CPP.  Any other flags *
  23.  *         are passed unchanged to make.                                     *
  24.  *                                                                           *
  25.  *         The output from the C preprocessor is written to a temporary file *
  26.  *         because this output contains blank lines which will cause make to *
  27.  *         detect a syntax error.  The temporary file is read back in and    *
  28.  *         the blank lines are removed with the result goint to Makefile.    *
  29.  *                                                                           *
  30.  *****************************************************************************/
  31.  
  32. #include <stdio.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <signal.h>
  36. #include <errno.h>
  37.  
  38. extern int errno;
  39. int temp_flag;
  40. char *temp_name;
  41.  
  42. main(argc, argv)
  43. int argc;
  44. char **argv;
  45. {
  46.     time_t mmtime, mtime;
  47.     struct stat sb;
  48.     int newer = 0;            /* DOS requires initialization */
  49.     char cpp_command[1024];
  50.     char make_command[1024];
  51.     char *temp_file;
  52.     char line[1024];
  53.     FILE *fpin, *fpout;
  54.     int cleanup();
  55.  
  56.     temp_flag = 0;            /* Indicates that temp file exists */
  57.     signal(SIGINT,cleanup);
  58.  
  59.     if (stat("MMakefile", &sb) < 0) {
  60.     perror("MMakefile");
  61.     exit(1);
  62.     } else
  63.     mmtime = sb.st_mtime;
  64.  
  65.     if (stat("Makefile", &sb) < 0) {
  66.     if (errno == ENOENT)
  67.         mtime = 0;
  68.     else {
  69.         perror("Makefile");
  70.         exit(1);
  71.     }
  72.     } else
  73.     mtime = sb.st_mtime;
  74.  
  75.     strcpy(cpp_command,"cc -E MMakefile ");
  76.     strcpy(make_command, "make ");
  77.  
  78.     if (mmtime > mtime) {        /* do we have to run CPP? */
  79.     newer = 1;
  80.     temp_file = mktemp("MMXXXXXX");
  81.     }
  82.  
  83.     /* Now scan the argument flags and put each in the appropriate
  84.        command line */
  85.     argc--;
  86.     argv++;
  87.     while(*argv) {
  88.     if ((strncmp(*argv,"-D",2) == 0) ||
  89.         (strncmp(*argv,"-U",2) == 0) ||
  90.         (strncmp(*argv,"-I",2) == 0)) {
  91.         strcat(cpp_command, *argv);
  92.         strcat(cpp_command, " ");
  93.     } else {
  94.         strcat(make_command, *argv);
  95.         strcat(make_command, " ");
  96.     }
  97.     argc--;
  98.     argv++;
  99.     }
  100.     strcat(cpp_command, ">");
  101.     if (newer) {
  102.     temp_flag = 1;            /* Say temp file exists */
  103.     temp_name = temp_file;        /* temp file name is temp_name */
  104.     strcat(cpp_command, temp_file);
  105.     } else
  106.     strcat(cpp_command, "Makefile");
  107.  
  108.     if (newer) {
  109.     if (system(cpp_command) != 0) {
  110.         fprintf(stderr, "CPP Failed!\n");
  111.         cleanup();
  112.     }
  113.     if ((fpin = fopen(temp_file, "r")) == NULL) {
  114.         perror(temp_file);
  115.         unlink(temp_file);
  116.         exit(1);
  117.     }
  118.     if ((fpout = fopen("Makefile", "w")) == NULL) {
  119.         perror("Makefile");
  120.         cleanup();
  121.     }
  122.     while (fgets(line, sizeof line, fpin)) {
  123.         if (*line != '\n')
  124.         fputs(line, fpout);
  125.     }
  126.     if (!feof(fpin)) {
  127.         perror("Makefile");
  128.         cleanup();
  129.     }
  130.     fclose(fpin);
  131.     fclose(fpout);
  132.     unlink(temp_file);
  133.     temp_flag = 0;            /* Temp file no longer exists */
  134.     }
  135.  
  136.     return (system(make_command));
  137. }
  138.  
  139. cleanup()
  140. {
  141.     if (temp_flag)
  142.     unlink(temp_name);
  143.     fprintf(stderr, "Mmake aborted.  %s\n", temp_flag ?
  144.         "temporary file removed." : "");
  145.  
  146.     exit(1);
  147. }
  148.