/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccsid_ecc[] = "@(#)ecc.c	1.15 12/3/92 12:57:46 LLNL ";

/*
	This is a simplified version of a driver for the ELROS
	preprocessor.  The driver serves the role that "cc" serves for
	C compilations:  "ecc" recognizes ELROS sources, performs
	the appropriate processing on those sources with the ELROS
	preprocessor "epp", and passes the preprocessed sources and
	all other options (non-ELROS sources) through to cc, adding
	default include path and library path options, and library
	load options for ELROS.

	This driver recognizes the following ELROS directives:

		-ROv	  Indicates "verbose" mode, echoing driver-
			  initiated commands
		-ROp	  Verbose mode with no execution of commands
		-ROe	  Indicates "preprocess only" mode, with echoing;
			  stops after running epp, doesn't compile ..c file.

Make instructions:

	The preprocessor symbol RO_DIR must be defined on the
	compilation line for this driver as a string containing the 
	pathname of the location of the ELROS root directory, e.g., 
	"/Gondor/usd/lindas/Elros".  The additional symbols of RO_RUNTIME
	and RO_PREPROCESSOR must be defined similarly.  Note that this
	should be an explicit path name--e.g. /Gondor/usd/lindas/Elros, not
	~lindas/Elros.

	The preprocessor symbol VERBOSE must be defined to indicate 
	whether or not the verbose option should be on by default.  This
	should be 1 if verbose execution is desired, 0 otherwise.

	Similarly, if the driver is for a runtime that supports ISODE,
	the symbol __RO_PLATFORM_ISODE must be defined.

*/

#define SYSTEM(S) \
{								\
	int code;						\
        if (verbose)						\
	    printf("  ... %s\n",S);	 			\
        if (execute) {						\
	    code = system((S));					\
	    if (code % 256)					\
            { fprintf(stderr,"(ecc) System call error\n");	\
	      exit(2);						\
            }							\
	    if (code)						\
            { fprintf(stderr,"(ecc) Command returned error: '%s'\n",(S));\
	      exit(code/256);					\
            }							\
        }							\
}

#ifdef __STDC__
#include <stdlib.h>
#else
#include <malloc.h>
#endif

#include <string.h>
#include <stdio.h>

#define ELROS 0
#define C     1

main(argc,argv)

   int argc;
   char *argv[];

{
   char *newargv[512];
   char command[512];
   int i, n, s;
   extern char *getenv();
   char *sources[40];
   char sourcetypes[40];
   char elros_root[80];
   char elros_epp[80];
   char elros_runtime[80];
   char parameters_dat[80];
   char any_h[80];
   char epp_out[80];
   char elros_incl[240];
   char elros_libs[240];
   char cpp_opts[240];
   char isode_incl[240];
   char cc_opts[240];
   char *arg;

   int verbose;         /* Indicates whether debug trace is on or off */
   int compile = 1;   /* Indicates whether C compilation will occur */
   int execute = 1;   /* Indicates whether execution will occur */
   int load = 1;      /* Indicates whether loading will occur */

   verbose = VERBOSE;

   isode_incl[0] = 0;

   cpp_opts[0] = 0;

#ifdef GCC
   strcpy(cc_opts,"gcc -c ");
#else
   strcpy(cc_opts,"cc -c ");
#endif

   if (argc < 2)
   {
      printf("Must specify input file\n");
      exit(1);
   }

   strcpy(elros_root,RO_DIR);
   strcat(elros_root,"/");

   sprintf(parameters_dat,"%sparameters.dat",elros_root);

   sprintf(any_h,"%sany.h",elros_root);

   sprintf(elros_epp,"%s/epp",RO_PREPROCESSOR);

   strcpy(elros_runtime,RO_RUNTIME);
   strcat(elros_runtime,"/");

   sprintf(elros_incl,"-I%sinclude ",elros_runtime);

   sprintf(elros_libs,"-L%slib -lelros ",elros_runtime);

#ifdef __RO_PLATFORM_ISODE
   strcat(elros_libs,"-lisode ");

   strcpy(isode_incl,"-I");
   strcat(isode_incl,elros_runtime);
   strcat(isode_incl,"include/isode ");

#endif

   strcat(elros_libs,"-lm ");

   n = 0;
   s = 0;

   for (i=1; i<argc; i++) {
      arg = argv[i];

      if (strcmp(arg,"-c") == 0) load = 0;

      else if (strcmp(arg,"-ROv") == 0) verbose = 1;

      else if (strcmp(arg,"-ROp") == 0) {
         verbose = 2; 
         execute = 0;
      }

      else if (strcmp(arg,"-ROe") == 0) {
         verbose = 1;
         compile = load = 0;
      }

      else if (strcmp(arg,"-g") == 0 || strcmp(arg,"-G") == 0) {
         strcat(cc_opts,arg);
         strcat(cc_opts," ");
      }

      else if (strncmp(arg,"-I",2) == 0) {
         strcat(cpp_opts,arg);
         strcat(cpp_opts," ");
      }

      else if (strncmp(arg,"-D",2)==0) {
         strcat(cpp_opts,arg);
         strcat(cpp_opts," ");
      }

      else if (strncmp(arg,"-U",2)==0) {
         strcat(cpp_opts,arg);
         strcat(cpp_opts," ");
      }

      else if (strncmp(arg,"-C",2)==0) {
         strcat(cpp_opts,arg);
         strcat(cpp_opts," ");
      }

      else if (strlen(arg) > 3 && strcmp(&(arg[strlen(arg)-3]),".ec") == 0) {
         sourcetypes[s] = ELROS;
         sources[s++] = arg;
      }

      else if ( strlen(arg) > 2  &&
                (strcmp(&(arg[strlen(arg)-2]),".c") == 0 ||
                 strcmp(&(arg[strlen(arg)-2]),".s") == 0) ) {
        sourcetypes[s] = C;
        sources[s++] = arg;
      }

      else { /* Treat as load directive */
        newargv[n++] = arg;
      }
   }

   for (i=0; i<s; i++){
     FILE *fp;

     arg = sources[i];

     if (s > 1) printf("%s:\n",arg);

     if (verbose != 2) {
       if ( (fp = fopen(arg,"r")) == NULL ) {
         printf("(ecc) Error: file %s cannot be opened\n",arg);
         exit(3);
       }
       else 
         fclose(fp);
     }

     if (sourcetypes[i] == ELROS) {

       sprintf(command,"rm -f %s..e",arg);
       SYSTEM(command);

       sprintf(command,
"cat %s %s | sed -e s#SOURCEFILE#%s# | /lib/cpp -DELROS_PASS1 %s%s%s > %s..e",
               any_h,arg,arg,cpp_opts,elros_incl,isode_incl,arg);
       SYSTEM(command);
 
#ifndef ELROS_DEBUG
       sprintf(epp_out,"/dev/null");
#else
       sprintf(epp_out,"%s..tree",arg);
       sprintf(command,"rm -f %s",epp_out);
       SYSTEM(command);
#endif

       sprintf(command,"%s %s..e %s > %s",
               elros_epp,arg,parameters_dat,epp_out);
       SYSTEM(command);

       if (compile) {
         strcpy(command,cc_opts);
         strcat(command,arg);
         strcat(command,"..c ");
         strcat(command,elros_incl);
         SYSTEM(command);
       }

       if (!verbose) {
         sprintf(command,"rm -f %s..c %s..h %s..e %s..t",arg,arg,arg,arg);
         SYSTEM(command);
       }
     }

     else { /* sourcetype is C */
       if (compile) {
         strcpy(command,cc_opts);
         strcat(command,cpp_opts);
         strcat(command,elros_incl);
         strcat(command,arg);
         SYSTEM(command);
       }
     }
   }

   if (load) {
#ifdef GCC
     strcpy(command,"gcc ");
#else
     strcpy(command,"cc ");
#endif

     /* list object files to load */
     for (i=0; i<s; i++) {
       if (sourcetypes[i] == ELROS) {
         strcat(command,sources[i]);
         strcat(command,"..o ");
       }
       else {
         int pos = strlen(command);
         strncpy(command+pos,sources[i],strlen(sources[i])-2);
         command[pos+strlen(sources[i])-2] = 0;
         strcat(command,".o ");
       }
     }

     /* Add load directives */
     for (i=0; i<n; i++) {
       strcat(command,newargv[i]);
       strcat(command," ");
     }

     strcat(command,elros_libs);

     SYSTEM(command);
   }

   exit(0);
}


