ack/fast/driver/driver.c

1198 lines
23 KiB
C
Raw Normal View History

1991-10-25 18:23:32 +00:00
/* fcc/fm2/fpc
2019-03-17 14:42:58 +00:00
Driver for fast ACK compilers.
1991-10-25 18:23:32 +00:00
2019-03-17 14:42:58 +00:00
Derived from the C compiler driver from Minix.
1991-10-25 18:23:32 +00:00
2019-03-17 14:42:58 +00:00
Compile this file with
cc -O -I<ACK home directory>/config -DF?? driver.c
where F?? is either FCC, FPC, or FM2.
Install the resulting binaries in the EM bin directory.
Suggested names: afcc, afm2, and afpc.
*/
1991-10-25 18:23:32 +00:00
#if FM2+FPC+FCC > 1
Something wrong here! Only one of FM2, FPC, or FCC must be defined
#endif
#ifdef i386
#define MACHNAME "i386"
#define SYSNAME "i386"
#endif
2019-03-17 14:42:58 +00:00
#include <string.h>
#include <stdlib.h>
1991-10-25 18:23:32 +00:00
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdarg.h>
2019-03-17 14:42:58 +00:00
#include <limits.h>
#include <stddef.h>
#include "smap.h"
#include "stringlist.h"
2019-03-17 14:42:58 +00:00
#include "em_path.h"
#include "system.h"
#if(CHAR_BIT!=8)
We do not support machines with non 8-bit characters.
#endif
/* Create em based compiler if otherwise not specified.
* Size of EM machine depends on current machine. */
#ifndef MACHNAME
#if(INT_MAX==32767)
#define MACHNAME "em22"
#define SYSNAME "em22"
#else
2019-03-17 14:42:58 +00:00
#define MACHNAME "em44"
#define SYSNAME "em44"
#endif
#endif
1991-10-25 18:23:32 +00:00
/*
2019-03-17 14:42:58 +00:00
Version producing ACK .o files in one pass.
*/
1991-10-25 18:23:32 +00:00
#define MAXARGC 256 /* maximum number of arguments allowed in a list */
2019-03-17 14:42:58 +00:00
#define USTR_SIZE FILENAME_MAX /* maximum length of string variable */
1991-10-25 18:23:32 +00:00
typedef char USTRING[USTR_SIZE];
2019-03-17 14:42:58 +00:00
struct arglist
{
1991-10-25 18:23:32 +00:00
int al_argc;
char *al_argv[MAXARGC];
};
2019-03-17 14:42:58 +00:00
#define CPP_NAME "$H/lib/ack/em_cpp"
#define LD_NAME "$H/lib/ack/em_ass"
#define CV_NAME "$H/lib/ack/$S/cv"
1991-10-25 18:23:32 +00:00
2019-03-17 14:42:58 +00:00
static char *CPP;
static char *COMP;
static char *LD;
2019-03-17 14:42:58 +00:00
static char *cc = "cc";
2019-03-17 14:42:58 +00:00
static int kids = -1;
static int ecount = 0;
1991-10-25 18:23:32 +00:00
struct size_info
2019-03-17 14:42:58 +00:00
{
/* size in bytes */
int size;
/* alignment */
int align;
};
/** Contains information on the different systems supported. */
struct system_information
{
/** Architecture / machine name */
char* arch;
/** Platform name */
char* platform;
/** Linker path and name */
char *linker;
/** Runtime startup file */
char *startup;
/** Converter path and name */
char *cv;
/** C preprocessor command line arguments. */
struct arglist cpp_flags;
/** Compiler flags. */
struct arglist compiler_flags;
};
const struct system_information machine_info[2] =
{
{
"em22" /* arch */,
"em22" /* platform */,
"$H/lib/ack/em_ass" /* linker */,
1991-11-01 11:55:34 +00:00
#ifdef FCC
"c-ansi.m" /* startup file */,
1991-11-01 11:55:34 +00:00
#else
#ifdef FPC
"pascal.m" /* startup file */,
#ifdef FM2
"modula2.m" /* startup file */,
1991-11-01 11:55:34 +00:00
#endif
#endif
#endif
NULL /* converter */,
/** CPP Flags */
{17,{
"-DEM_WSIZE=2",
"-DEM_PSIZE=2",
"-DEM_SSIZE=2",
"-DEM_LSIZE=4",
"-DEM_FSIZE=4",
"-DEM_DSIZE=8",
"-DEM_XSIZE=8",
"-D_EM_WSIZE=2",
"-D_EM_PSIZE=2",
"-D_EM_SSIZE=2",
"-D_EM_LSIZE=4",
"-D_EM_FSIZE=4",
"-D_EM_DSIZE=8",
"-D_EM_XSIZE=8",
"-Dem22",
"-D__em22",
"-D__unix"}},
/* compiler flags */
#ifdef FCC
{1, {
"-Vw2.2i2.2p2.2f4.2s2.2l4.2d8.2x8.2"
}}
#else
#ifdef FPC
/* pc Flags */
{1, {
"-Vw2.2i2.2l4.2p2.2f8.2S2.2"
}}
#else
#ifdef FM2
2019-06-15 15:46:18 +00:00
/* modula-2 Flags */
{1, {
"-Vw2.2i2.2l4.2p2.2f8.2S2.2"
}}
#endif
#endif
#endif
},
{
"em24" /* arch */,
"em24" /* platform */,
"$H/lib/ack/em_ass" /* linker */,
#ifdef FCC
"c-ansi.m" /* startup file */,
#else
#ifdef FPC
"pascal.m" /* startup file */,
#ifdef FM2
"modula2.m" /* startup file */,
#endif
#endif
#endif
NULL /* converter */,
/** CPP Flags */
{17,{
"-DEM_WSIZE=2",
"-DEM_PSIZE=4",
"-DEM_SSIZE=2",
"-DEM_LSIZE=4",
"-DEM_FSIZE=4",
"-DEM_DSIZE=8",
"-DEM_XSIZE=8",
"-D_EM_WSIZE=2",
"-D_EM_PSIZE=4",
"-D_EM_SSIZE=2",
"-D_EM_LSIZE=4",
"-D_EM_FSIZE=4",
"-D_EM_DSIZE=8",
"-D_EM_XSIZE=8",
"-Dem24",
"-D__em24",
"-D__unix"}},
/* compiler flags */
#ifdef FCC
{1, {
"-Vw2.2i2.2p4.2f4.2s2.2l4.2d8.2x8.2"
}}
#else
#ifdef FPC
/* pc Flags */
{1, {
"-Vw2.2i2.2l4.2p4.2f8.2S2.2"
}}
#else
#ifdef FM2
2019-06-15 15:46:18 +00:00
/* modula-2 Flags */
{1, {
"-Vw2.2i2.2l4.2p4.2f8.2S2.2"
}}
#endif
#endif
#endif
}
};
struct arglist CPP_FLAGS = {0,{NULL}};
2019-03-17 14:42:58 +00:00
struct arglist LD_HEAD =
{ 2,
{ "$H/lib/$S/head_em",
#ifdef FCC
"$H/lib/$S/head_$A"
#endif
#ifdef FM2
"$H/lib/$S/head_m2"
#endif
#ifdef FPC
"$H/lib/$S/head_pc"
#endif
2019-03-17 14:42:58 +00:00
}};
2019-03-17 14:42:58 +00:00
struct arglist align =
{ 5,
{
#ifdef sun3
"-a0:4",
"-a1:4",
"-a2:0x20000",
"-a3:4",
"-b0:0x2020"
#endif
#ifdef vax4
"-a0:4",
"-a1:4",
"-a2:0x400",
"-a3:4",
"-b0:0"
#endif
#ifdef i386
"-a0:4",
"-a1:4",
"-a2:4",
"-a3:4",
"-b1:0x1880000"
#endif
2019-03-17 14:42:58 +00:00
} };
1991-10-25 18:23:32 +00:00
struct arglist COMP_FLAGS;
2019-03-17 14:42:58 +00:00
static char *o_FILE = "a.out"; /* default name for executable file */
1991-10-25 18:23:32 +00:00
#define cleanup(str) (str && str[0] && remove(str))
2019-03-17 14:42:58 +00:00
#define init(al) ((al)->al_argc = 0)
1991-10-25 18:23:32 +00:00
2019-03-17 14:42:58 +00:00
static char ProgCall[FILENAME_MAX];
1991-10-25 18:23:32 +00:00
2019-03-17 14:42:58 +00:00
static struct arglist SRCFILES;
static struct arglist LDFILES;
1991-10-25 18:23:32 +00:00
2019-03-17 14:42:58 +00:00
static int RET_CODE = 0;
1991-10-25 18:23:32 +00:00
2019-03-17 14:42:58 +00:00
/* The environment variable pointing to ACK_HOME */
static char* ackhome = 0;
/* The environment variable pointing to the temporary directory. */
static char* tmpdir = 0;
static struct arglist LD_FLAGS;
1991-10-25 18:23:32 +00:00
2019-03-17 14:42:58 +00:00
static struct arglist CALL_VEC;
1991-10-25 18:23:32 +00:00
2019-03-17 14:42:58 +00:00
static int o_flag = 0;
static int c_flag = 0;
static int g_flag = 0;
static int v_flag = 0;
static int O_flag = 0;
/* Only ANSI C is now supported. */
static int ansi_c = 1;
static int cv_flag = 0;
1991-10-25 18:23:32 +00:00
char *mkstr(char *, ...);
2019-03-17 14:42:58 +00:00
static char *alloc(unsigned int);
static char *extension(char *);
2019-06-15 15:46:18 +00:00
static char *expand_string(char *s, struct system_information *);
2019-03-17 14:42:58 +00:00
static void error(char *, char *, char *);
static void warning(char *, char *, char *);
static void panic(char *);
static void append(register struct arglist *, char *);
2019-06-15 15:46:18 +00:00
static void expand(register struct arglist *, struct system_information *);
2019-03-17 14:42:58 +00:00
static void concat(struct arglist *, struct arglist *);
static int runvec(struct arglist *, char *);
static int needsprep(char *);
static USTRING ofile;
static USTRING BASE;
static char tmp_file[L_tmpnam];
1991-10-25 18:23:32 +00:00
int noexec = 0;
2019-03-17 14:42:58 +00:00
void trapcc(int sig)
1991-10-25 18:23:32 +00:00
{
cleanup(ofile);
cleanup(tmp_file);
2019-03-17 14:42:58 +00:00
if (ackhome != 0)
{
free(ackhome);
}
if (tmpdir != 0)
{
free(tmpdir);
}
exit(EXIT_FAILURE);
1991-10-25 18:23:32 +00:00
}
#ifdef FCC
#define lang_suffix() "c"
2019-03-17 14:42:58 +00:00
#define comp_name() "$H/lib/ack/em_cemcom.ansi"
#define ansi_c_name() "$H/lib/ack/em_cemcom.ansi"
#endif /* FCC */
1991-10-25 18:23:32 +00:00
#ifdef FM2
#define lang_suffix() "mod"
2019-03-17 14:42:58 +00:00
#define comp_name() "$H/lib/ack/em_m2"
#endif /* FM2 */
1991-10-25 18:23:32 +00:00
#ifdef FPC
#define lang_suffix() "p"
2019-03-17 14:42:58 +00:00
#define comp_name() "$H/lib/ack/em_pc"
#endif /* FPC */
1991-10-25 18:23:32 +00:00
2019-06-15 15:46:18 +00:00
/** Default library directories to search in. */
#define LIB_DIR_1 "$H/share/ack/$S"
#define LIB_DIR_2 "$H/lib/ack/plat/$S"
1991-10-25 18:23:32 +00:00
#ifdef FCC
2019-03-17 14:42:58 +00:00
int lang_opt(char *str)
1991-10-25 18:23:32 +00:00
{
2019-03-17 14:42:58 +00:00
switch (str[1])
{
1991-11-13 17:07:30 +00:00
case 'R':
2019-03-17 14:42:58 +00:00
if (!ansi_c)
{
1991-11-13 17:07:30 +00:00
append(&COMP_FLAGS, str);
return 1;
}
break;
2019-03-17 14:42:58 +00:00
case '-': /* debug options */
1991-11-01 11:55:34 +00:00
append(&COMP_FLAGS, str);
return 1;
2019-03-17 14:42:58 +00:00
case 'w': /* disable warnings */
if (!ansi_c)
{
1991-11-13 17:07:30 +00:00
append(&COMP_FLAGS, str);
return 1;
}
2019-03-17 14:42:58 +00:00
if (str[2])
{
1991-10-29 09:26:46 +00:00
str[1] = '-';
append(&COMP_FLAGS, &str[1]);
1991-10-25 18:23:32 +00:00
}
2019-03-17 14:42:58 +00:00
else
append(&COMP_FLAGS, "-a");
1991-10-25 18:23:32 +00:00
return 1;
}
return 0;
}
#endif /* FCC */
1991-10-25 18:23:32 +00:00
#ifdef FM2
2019-03-17 14:42:58 +00:00
int lang_opt(char *str)
1991-10-25 18:23:32 +00:00
{
2019-03-17 14:42:58 +00:00
switch(str[1])
{
case '-': /* debug options */
case 'w': /* disable warnings */
case 'R': /* no runtime checks */
case 'W': /* add warnings */
case 'L': /* no line numbers */
case 'A': /* extra array bound checks */
case '3': /* only accept 3rd edition Modula-2 */
1991-10-25 18:23:32 +00:00
append(&COMP_FLAGS, str);
return 1;
2019-03-17 14:42:58 +00:00
case 'I':
1991-10-25 18:23:32 +00:00
append(&COMP_FLAGS, str);
2019-03-17 14:42:58 +00:00
break; /* !!! */
case 'U': /* underscores in identifiers allowed */
if (str[2] == '\0')
{
1991-10-25 18:23:32 +00:00
append(&COMP_FLAGS, str);
return 1;
}
break;
2019-03-17 14:42:58 +00:00
case 'e': /* local extension for Modula-2 compiler:
procedure constants
*/
1991-10-25 18:23:32 +00:00
str[1] = 'l';
append(&COMP_FLAGS, str);
return 1;
}
return 0;
}
#endif /* FM2 */
1991-10-25 18:23:32 +00:00
#ifdef FPC
2019-03-17 14:42:58 +00:00
int lang_opt(char *str)
1991-10-25 18:23:32 +00:00
{
2019-03-17 14:42:58 +00:00
switch(str[1])
{
case '-': /* debug options */
case 'a': /* enable assertions */
case 'd': /* allow doubles (longs) */
case 'i': /* set size of integer sets */
case 't': /* tracing */
case 'w': /* disable warnings */
case 'A': /* extra array bound checks */
case 'C': /* distinguish between lower case and upper case */
case 'L': /* no FIL and LIN instructions */
case 'R': /* no runtime checks */
1991-10-25 18:23:32 +00:00
append(&COMP_FLAGS, str);
return 1;
2019-03-17 14:42:58 +00:00
case 'u':
case 'U':
1991-10-25 18:23:32 +00:00
/* underscores in identifiers */
2019-03-17 14:42:58 +00:00
case 's':
1991-10-25 18:23:32 +00:00
/* only compile standard pascal */
2019-03-17 14:42:58 +00:00
case 'c':
1991-10-25 18:23:32 +00:00
/* C type strings */
2019-03-17 14:42:58 +00:00
if (str[2] == '+' && str[3] == '\0')
{
1991-10-25 18:23:32 +00:00
str[2] = 0;
append(&COMP_FLAGS, str);
return 1;
}
}
return 0;
}
#endif /* FPC */
1991-10-25 18:23:32 +00:00
2019-03-17 14:42:58 +00:00
char* stringdup(const char* s)
{
char *p;
2019-03-17 14:42:58 +00:00
if (s == NULL)
return NULL;
p = alloc(strlen(s) + sizeof(char));
2019-03-17 14:42:58 +00:00
strcpy(p, s);
return p;
}
char* getackhome(void)
{
2019-03-24 16:09:42 +00:00
char *value = getenv("ACK_HOME");
2019-03-17 14:42:58 +00:00
if (value == NULL)
{
#ifndef EM_DIR
panic("ACK_HOME must be set.");
#else
2019-03-24 16:09:42 +00:00
return EM_DIR;
2019-03-17 14:42:58 +00:00
#endif
}
return value;
}
#define LIB_PREFIX "lib"
#define LIB_SUFFIX ".a"
/** From a library library directory list,
* search for the libraries that exist, and return
* the full path to the directory where the
* library is located.
*
* The search starts from the first added directory
* item at the command line.
*
* @param[in] dirs The directory list to search in.
* @param[in] lib The library name to search for.
* @returns The full path specification, allocated in the heap, it must
* be freed by the caller.
*/
char* search_library_path(struct stringlist *dirs, char* lib)
{
char fname[FILENAME_MAX];
FILE *fd;
int len;
int dirs_count;
int index = stringlist_count(dirs)-1;
dirs_count = stringlist_count(dirs);
for (index = 0; index < dirs_count; index++)
{
strcpy(fname, stringlist_get(dirs,index));
len = strlen(fname);
/* len-1 is the NULL character */
if (fname[len-1] != '/')
{
/* Add trailing slash */
fname[len] = '/';
fname[len+1] = 0;
len++;
}
fname[len] = 0;
strcat(fname,lib);
fd = fopen(fname,"rb");
if (fd!=NULL)
{
/* Return the path! */
fclose(fd);
return stringdup(fname);
}
}
return NULL;
}
2019-03-17 14:42:58 +00:00
2019-06-15 15:46:18 +00:00
2019-03-17 14:42:58 +00:00
int main(int argc, char *argv[])
1991-10-25 18:23:32 +00:00
{
/* Contains the directories being searched for libraries */
struct stringlist library_dirs;
/* Contains the library lists */
struct stringlist libraries;
/* Contains the directory paths. */
struct smap paths;
/* Contains the src file list */
struct stringlist srcfiles;
1991-10-25 18:23:32 +00:00
char *str;
char *startup_file;
1991-10-25 18:23:32 +00:00
char **argvec;
int count;
int index;
int libs_count;
1991-10-25 18:23:32 +00:00
char *ext;
2019-03-17 14:42:58 +00:00
FILE* fd;
1991-10-25 18:23:32 +00:00
register struct arglist *call = &CALL_VEC;
char tmpbuffer[256];
1991-10-25 18:23:32 +00:00
char *file;
char *ldfile;
2019-03-17 14:42:58 +00:00
char *INCLUDE = NULL;
1991-10-25 18:23:32 +00:00
int compile_cnt = 0;
struct system_information* sys_info;
startup_file = NULL;
sys_info = &machine_info[0];
strcpy(tmpbuffer, LIB_PREFIX);
stringlist_init(&library_dirs);
stringlist_init(&libraries);
stringlist_init(&srcfiles);
smap_init(&paths);
1991-10-25 18:23:32 +00:00
2019-03-24 16:09:42 +00:00
ackhome = stringdup(getackhome());
2019-03-17 14:42:58 +00:00
if (ackhome == NULL)
{
panic("ACK_HOME Environment variable is not set.");
}
2019-03-24 16:09:42 +00:00
tmpdir = stringdup(sys_gettmpdir());
2019-03-17 14:42:58 +00:00
if (tmpdir == NULL)
{
panic("TMPDIR Environment variable is not set.");
}
1991-10-25 18:23:32 +00:00
setbuf(stdout, (char *) 0);
2019-03-17 14:42:58 +00:00
/* get basebame of application. */
sys_basename(*argv++, ProgCall);
2019-03-17 14:42:58 +00:00
/* get compiler to use. */
2019-06-15 15:46:18 +00:00
COMP = expand_string(comp_name(),sys_info);
2019-03-17 14:42:58 +00:00
/* get c pre-processor to use */
2019-06-15 15:46:18 +00:00
CPP = expand_string(CPP_NAME,sys_info);
/** get linker to use */
2019-06-15 15:46:18 +00:00
LD = expand_string(sys_info->linker,sys_info);
/* Add system directory path. */
stringlist_add(&library_dirs,expand_string(LIB_DIR_1,sys_info));
stringlist_add(&library_dirs,expand_string(LIB_DIR_2,sys_info));
1991-10-25 18:23:32 +00:00
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
signal(SIGINT, trapcc);
2019-03-17 14:42:58 +00:00
if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
signal(SIGTERM, trapcc);
while (--argc > 0)
{
if (*(str = *argv++) != '-')
{
stringlist_add(&srcfiles,stringdup(str));
1991-10-25 18:23:32 +00:00
continue;
}
2019-03-17 14:42:58 +00:00
if (lang_opt(str))
{
1991-10-25 18:23:32 +00:00
}
2019-03-17 14:42:58 +00:00
else
switch (str[1])
{
case 'c': /* stop after producing .o files */
c_flag = 1;
break;
case 'D': /* preprocessor #define */
case 'U': /* preprocessor #undef */
append(&CPP_FLAGS, str);
break;
case 'I': /* include directory */
append(&CPP_FLAGS, str);
break;
case 'g': /* debugger support */
append(&COMP_FLAGS, str);
g_flag = 1;
break;
case 'o': /* target file */
if (argc-- >= 0)
{
o_flag = 1;
o_FILE = *argv++;
ext = extension(o_FILE);
if (ext != o_FILE && !strcmp(ext, lang_suffix()))
{
error("-o would overwrite %s", o_FILE, NULL);
}
1991-10-25 18:23:32 +00:00
}
2019-03-17 14:42:58 +00:00
break;
case 'u': /* mark identifier as undefined */
append(&LD_FLAGS, str);
2019-03-17 14:42:58 +00:00
if (argc-- >= 0)
append(&LD_FLAGS, *argv++);
break;
case 'O': /* use built in peephole optimizer */
O_flag = 1;
break;
case 'v': /* verbose */
v_flag++;
if (str[2] == 'n')
noexec = 1;
break;
case 'L':
stringlist_add(&library_dirs,stringdup(str+2));
break;
2019-03-17 14:42:58 +00:00
case 'l': /* library file */
stringlist_add(&srcfiles,stringdup(str));
2019-03-17 14:42:58 +00:00
break;
case 'M': /* use other compiler (for testing) */
2019-03-30 16:52:27 +00:00
free(COMP);
COMP = alloc(strlen(str)+2-1);
2019-03-17 14:42:58 +00:00
strcpy(COMP, str + 2);
break;
2019-03-24 16:09:42 +00:00
case 'P': /* use other cpp (for testing) */
2019-03-30 16:52:27 +00:00
free(CPP);
CPP = alloc(strlen(str)+2-1);
2019-03-24 16:09:42 +00:00
strcpy(CPP, str + 2);
break;
case 'X': /* use other linker (for testing) */
LD = alloc(strlen(str)+2-1);
strcpy(LD, str + 2);
break;
2019-03-17 14:42:58 +00:00
case 's': /* strip */
if (str[2] == '\0')
{
append(&LD_FLAGS, str);
break;
}
/* fall through */
default:
warning("%s flag ignored", str, NULL);
break;
}
1991-10-25 18:23:32 +00:00
}
/* Add C preprocessor flags. */
for (count = 0; count < sys_info->cpp_flags.al_argc; count++)
{
append(&CPP_FLAGS, sys_info->cpp_flags.al_argv[count]);
}
/* Add compiler flags. */
for (count = 0; count < sys_info->compiler_flags.al_argc; count++)
{
append(&COMP_FLAGS, sys_info->compiler_flags.al_argv[count]);
}
2019-06-15 15:46:18 +00:00
#ifdef FPC
append(&COMP_FLAGS, "-d");
#endif
/* Now for each srcfile, if it starts with -l then replace it with the correct real
* path immediately.
*/
count = stringlist_count(&srcfiles);
for (index = 0; index < count; index++)
{
str = stringlist_get(&srcfiles,index);
/* -l parameter */
if (*str == '-')
{
/* Search for the directory where this library might be located. */
strcpy(tmpbuffer,LIB_PREFIX);
strcat(tmpbuffer,str+2);
strcat(tmpbuffer,LIB_SUFFIX);
str = search_library_path(&library_dirs,tmpbuffer);
if (str != NULL)
{
stringlist_add(&srcfiles,str);
append(&SRCFILES,str);
}
} else
{
append(&SRCFILES,str);
}
}
2019-03-17 14:42:58 +00:00
if (ecount)
{
2019-03-17 14:42:58 +00:00
exit(EXIT_FAILURE);
}
1991-10-25 18:23:32 +00:00
count = SRCFILES.al_argc;
argvec = &(SRCFILES.al_argv[0]);
2019-03-17 14:42:58 +00:00
while (count-- > 0)
{
1991-10-25 18:23:32 +00:00
ext = extension(*argvec);
2019-03-17 14:42:58 +00:00
if (*argvec[0] != '-' && ext != *argvec++
&& (!strcmp(ext, lang_suffix())))
{
1991-10-25 18:23:32 +00:00
compile_cnt++;
}
}
2019-03-17 14:42:58 +00:00
if (compile_cnt > 1 && c_flag && o_flag)
{
warning("-o flag ignored", NULL, NULL);
1991-10-25 18:23:32 +00:00
o_flag = 0;
}
#ifdef FM2
2019-06-15 15:46:18 +00:00
INCLUDE = expand_string("-I$H/lib/m2",sys_info);
#endif /* FM2 */
#ifdef FCC
2019-03-17 14:42:58 +00:00
/* INCLUDE = expand_string(
ansi_c ? "-I$H/include/tail_ac" : "-I$H/include/_tail_cc"); */
2019-06-15 15:46:18 +00:00
INCLUDE = expand_string("-I$H/share/ack/include",sys_info);
/* append(&COMP_FLAGS, "-L");*/
#endif /* FCC */
1991-10-25 18:23:32 +00:00
count = SRCFILES.al_argc;
argvec = &(SRCFILES.al_argv[0]);
2019-03-17 14:42:58 +00:00
/* For argument file */
2019-03-17 14:42:58 +00:00
while (count-- > 0)
{
1991-10-25 18:23:32 +00:00
register char *f;
2019-03-17 14:42:58 +00:00
sys_basename(file = *argvec++, BASE);
1991-10-25 18:23:32 +00:00
ext = extension(file);
/* if not standard input and file is equal to the supported language suffix,
* then compile it */
2019-03-17 14:42:58 +00:00
if (file[0] != '-' && ext != file && (!strcmp(ext, lang_suffix())))
{
if (compile_cnt > 1)
printf("%s\n", file);
1991-10-25 18:23:32 +00:00
2019-03-17 14:42:58 +00:00
ldfile = c_flag ? ofile : alloc((unsigned) strlen(BASE) + 3);
/****************************************************
* Run C preprocessor on the source files and save
* result in temporary output file.
****************************************************/
1991-10-25 18:23:32 +00:00
if (
#ifdef FCC
2019-03-17 14:42:58 +00:00
/* (!strcmp(ext, "s")) && */
1991-10-25 18:23:32 +00:00
#endif
2019-03-17 14:42:58 +00:00
needsprep(file))
{
if (sys_tmpnam(tmp_file)==NULL)
{
panic("Cannot get temporary filename.");
}
fd = fopen(tmp_file,"w+");
if (fd==NULL)
{
fclose(fd);
panic("Cannot write temporary file.");
}
1991-10-25 18:23:32 +00:00
init(call);
append(call, CPP);
concat(call, &CPP_FLAGS);
append(call, INCLUDE);
append(call, file);
2019-03-17 14:42:58 +00:00
if (runvec(call, tmp_file)==EXIT_SUCCESS)
{
/* The input file is now the preprocessor
* output file.
*/
1991-10-25 18:23:32 +00:00
file = tmp_file;
}
2019-03-17 14:42:58 +00:00
else
{
1991-10-25 18:23:32 +00:00
remove(tmp_file);
tmp_file[0] = '\0';
continue;
}
}
2019-03-17 14:42:58 +00:00
/****************************************************
* Compile the source file.
****************************************************/
1991-10-25 18:23:32 +00:00
init(call);
2019-03-17 14:42:58 +00:00
if (o_flag && c_flag)
{
1991-10-25 18:23:32 +00:00
f = o_FILE;
}
2019-03-17 14:42:58 +00:00
else
{
f = mkstr(ldfile, BASE, ".", "o", (char *) 0);
}
append(call, COMP);
concat(call, &COMP_FLAGS);
#ifdef FM2
append(call, INCLUDE);
1991-10-25 18:23:32 +00:00
#endif
2019-03-17 14:42:58 +00:00
append(call, file);
append(call, f);
if (runvec(call, (char *) 0)==EXIT_SUCCESS)
{
1991-10-25 18:23:32 +00:00
file = f;
}
2019-03-17 14:42:58 +00:00
else
{
1991-10-25 18:23:32 +00:00
remove(f);
continue;
}
cleanup(tmp_file);
tmp_file[0] = '\0';
} /* endif compiling source file. */
1991-10-25 18:23:32 +00:00
2019-03-17 14:42:58 +00:00
else if (file[0] != '-' && strcmp(ext, "o") && strcmp(ext, "a"))
{
warning("file with unknown suffix (%s) passed to the loader", ext,
NULL);
1991-10-25 18:23:32 +00:00
}
if (c_flag)
continue;
2019-03-17 14:42:58 +00:00
/* Add the output object file to the list of files to link. */
1991-10-25 18:23:32 +00:00
append(&LDFILES, file);
}
/* See if we need to convert the flags. */
cv_flag = sys_info->cv!=NULL;
1991-10-25 18:23:32 +00:00
/* *.s to a.out */
2019-03-17 14:42:58 +00:00
if (RET_CODE == 0 && LDFILES.al_argc > 0)
{
1991-10-25 18:23:32 +00:00
init(call);
/* expand(&LD_HEAD);
2019-03-17 14:42:58 +00:00
// cc = "cc.2g";
expand(&LD_TAIL);*/
append(call, LD);
/* concat(call, &align);*/
append(call, "-p");
append(call, "-sx");
append(call, "-o");
2019-03-17 14:42:58 +00:00
if (cv_flag)
2019-03-17 14:42:58 +00:00
{
if (sys_tmpnam(tmp_file)==NULL)
{
panic("Cannot get temporary filename.");
}
append(call, tmp_file);
} else
{
append(call, o_FILE);
}
/* concat(call, &LD_HEAD);
concat(call, &LD_FLAGS);*/
if (sys_info->startup!=NULL)
{
startup_file = search_library_path(&library_dirs,sys_info->startup);
if (startup_file!=NULL)
{
append(call,startup_file);
} else
{
panic("Cannot find startup file.");
}
2019-03-17 14:42:58 +00:00
}
1991-10-25 18:23:32 +00:00
concat(call, &LDFILES);
2019-03-17 14:42:58 +00:00
/* if (g_flag)
append(call, expand_string("$H/lib/$M/tail_db"));
#ifdef FCC
2019-03-17 14:42:58 +00:00
if (!ansi_c)
append(call, expand_string("$H/lib/$S/tail_cc.1s"));
#endif
2019-03-17 14:42:58 +00:00
concat(call, &LD_TAIL);*/
if (runvec(call, (char *) 0)==EXIT_FAILURE)
{
cleanup(tmp_file);
exit(RET_CODE);
}
/* Check if we need to convert the file to
* a standard executable file for the specific
* target.
*
*/
if (cv_flag)
{
init(call);
2019-06-15 15:46:18 +00:00
append(call, expand_string(sys_info->cv, sys_info));
append(call, tmp_file);
append(call, o_FILE);
if (runvec(call, (char *) 0)==EXIT_FAILURE)
{
cleanup(tmp_file);
exit(RET_CODE);
}
cleanup(tmp_file);
}
1991-10-25 18:23:32 +00:00
}
2019-03-24 16:09:42 +00:00
if ((LDFILES.al_argc == 0) && (SRCFILES.al_argc==0))
{
panic("No input source files or input object files specified.");
}
stringlist_free(&library_dirs,1);
stringlist_free(&libraries,1);
if (startup_file!=NULL)
free(startup_file);
smap_free(&paths,1,1);
1991-10-25 18:23:32 +00:00
exit(RET_CODE);
}
2019-03-17 14:42:58 +00:00
static int needsprep(char *name)
1991-10-25 18:23:32 +00:00
{
2019-03-17 14:42:58 +00:00
/* FILE *file;
1991-10-25 18:23:32 +00:00
char fc;
2019-03-17 14:42:58 +00:00
file = fopen(name, "r");
if (file == 0)
return 0;
if (fread(file, &fc, 1) != 1)
fc = 0;
fclose(file);
return fc == '#';*/
return 1;
1991-10-25 18:23:32 +00:00
}
2019-03-17 14:42:58 +00:00
static char * alloc(unsigned int u)
1991-10-25 18:23:32 +00:00
{
char *p = malloc(u);
2019-03-17 14:42:58 +00:00
if (p == NULL)
1991-10-25 18:23:32 +00:00
panic("no space");
return p;
}
2019-06-15 15:46:18 +00:00
static char * expand_string(char *s, struct system_information *sysinfo)
{
2019-03-17 14:42:58 +00:00
char buf[1024];
register char *p = s;
register char *q = &buf[0];
int expanded = 0;
2019-03-17 14:42:58 +00:00
if (!p)
return p;
while (*p)
{
if (*p == '$')
{
p++;
expanded = 1;
2019-03-17 14:42:58 +00:00
switch (*p++)
{
case 'A':
2019-03-17 14:42:58 +00:00
if (ansi_c)
strcpy(q, "ac");
else
strcpy(q, cc);
break;
case 'H':
2019-03-17 14:42:58 +00:00
strcpy(q, ackhome);
break;
case 'M':
2019-06-15 15:46:18 +00:00
strcpy(q, sysinfo->arch);
break;
case 'S':
2019-06-15 15:46:18 +00:00
strcpy(q, sysinfo->platform);
break;
default:
panic("internal error");
break;
}
2019-03-17 14:42:58 +00:00
while (*q)
q++;
}
2019-03-17 14:42:58 +00:00
else
*q++ = *p++;
}
2019-03-17 14:42:58 +00:00
if (!expanded)
return s;
*q++ = '\0';
2019-03-30 16:52:27 +00:00
/* Do not forget the missing null character. */
p = alloc((unsigned int) (q - buf));
return strcpy(p, buf);
}
2019-03-17 14:42:58 +00:00
static void append(register struct arglist *al, char *arg)
1991-10-25 18:23:32 +00:00
{
2019-03-17 14:42:58 +00:00
if (!arg || !*arg)
return;
1991-10-25 18:23:32 +00:00
if (al->al_argc >= MAXARGC)
panic("argument list overflow");
al->al_argv[(al->al_argc)++] = arg;
}
2019-06-15 15:46:18 +00:00
static void expand(register struct arglist *al, struct system_information *sysinfo)
{
register int i = al->al_argc;
register char **p = &(al->al_argv[0]);
2019-03-17 14:42:58 +00:00
while (i-- > 0)
{
2019-06-15 15:46:18 +00:00
*p = expand_string(*p,sysinfo);
p++;
}
}
2019-03-17 14:42:58 +00:00
static void concat(struct arglist *al1, struct arglist *al2)
1991-10-25 18:23:32 +00:00
{
2019-03-17 14:42:58 +00:00
register int i = al2->al_argc;
1991-10-25 18:23:32 +00:00
register char **p = &(al1->al_argv[al1->al_argc]);
register char **q = &(al2->al_argv[0]);
if ((al1->al_argc += i) >= MAXARGC)
panic("argument list overflow");
2019-03-17 14:42:58 +00:00
while (i-- > 0)
{
*p++ = *q++;
1991-10-25 18:23:32 +00:00
}
}
2019-03-17 14:42:58 +00:00
char *mkstr(char *dst, ...)
{
va_list ap;
1991-10-25 18:23:32 +00:00
va_start(ap, dst);
{
register char *p;
register char *q;
q = dst;
p = va_arg(ap, char *);
2019-03-17 14:42:58 +00:00
while (p)
{
while ((*q++ = *p++) != 0)
;
q--;
p = va_arg(ap, char *);
}
}
va_end(ap);
return dst;
}
2019-03-17 14:42:58 +00:00
static char *extension(char *fn)
1991-10-25 18:23:32 +00:00
{
2019-03-17 14:42:58 +00:00
register char *c = fn;
1991-10-25 18:23:32 +00:00
2019-03-17 14:42:58 +00:00
while (*c++)
;
while (*--c != '.' && c >= fn)
1991-10-25 18:23:32 +00:00
{
}
2019-03-17 14:42:58 +00:00
if (c++ < fn || !*c)
return fn;
return c;
1991-10-25 18:23:32 +00:00
}
2019-03-17 14:42:58 +00:00
/* Converts an argument structure to a string.
* The memory allocated should be freed by
* the caller.
* */
char *arg2str(struct arglist *vec)
1991-10-25 18:23:32 +00:00
{
2019-03-17 14:42:58 +00:00
int i;
unsigned int length;
char *p;
length = 0;
for (i = 0; i < vec->al_argc; i++)
{
length += strlen(vec->al_argv[i]);
1991-10-25 18:23:32 +00:00
}
2019-03-17 14:42:58 +00:00
/* Add space character */
length += vec->al_argc * sizeof(char);
p = alloc(length + sizeof(char));
*p = '\0';
for (i = 0; i < vec->al_argc; i++)
{
strcat(p, vec->al_argv[i]);
strcat(p, " ");
}
return p;
1991-10-25 18:23:32 +00:00
}
2019-03-17 14:42:58 +00:00
/* Run the system command, and outputs to "outp" if not
* NULL.
*/
static int runvec(struct arglist *vec, char *outp)
1991-10-25 18:23:32 +00:00
{
2019-03-17 14:42:58 +00:00
int status;
char *p;
char *redirect;
1991-10-25 18:23:32 +00:00
2019-03-17 14:42:58 +00:00
redirect = NULL;
1991-10-25 18:23:32 +00:00
2019-03-17 14:42:58 +00:00
/* Check if a command interpreter is available. */
if (system(NULL) == 0)
{
panic("No command interpreter available!");
}
if(outp != NULL)
{
2019-03-25 16:58:56 +00:00
/* Don't forget the null character. */
redirect = alloc(strlen(outp)+sizeof(char)*3);
2019-03-17 14:42:58 +00:00
strcpy(redirect,"1>");
strcat(redirect,outp);
append(vec,redirect);
}
p = arg2str(vec);
if ((v_flag) && (p != NULL))
{
fprintf(stdout,"Executing : %s\n",p);
}
1991-10-25 18:23:32 +00:00
2019-03-17 14:42:58 +00:00
status = system(p);
free(p);
if (outp != NULL)
{
free(redirect);
1991-10-25 18:23:32 +00:00
}
2019-03-17 14:42:58 +00:00
if (status == 0)
{
RET_CODE = EXIT_SUCCESS;
1991-10-25 18:23:32 +00:00
}
2019-03-17 14:42:58 +00:00
else
{
RET_CODE = EXIT_FAILURE;
1991-10-25 18:23:32 +00:00
}
2019-03-17 14:42:58 +00:00
return status;
1991-10-25 18:23:32 +00:00
}
/*VARARGS1*/
2019-03-17 14:42:58 +00:00
static void error(char *str, char *s1, char *s2)
1991-10-25 18:23:32 +00:00
{
fprintf(stderr, "%s: ", ProgCall);
fprintf(stderr, str, s1, s2);
putc('\n', stderr);
ecount++;
}
/*VARARGS1*/
2019-03-17 14:42:58 +00:00
static void warning(char *str, char *s1, char *s2)
1991-10-25 18:23:32 +00:00
{
fprintf(stderr, "%s: (warning) ", ProgCall);
fprintf(stderr, str, s1, s2);
putc('\n', stderr);
}
2019-03-17 14:42:58 +00:00
static void panic(char *str)
1991-10-25 18:23:32 +00:00
{
2019-03-17 14:42:58 +00:00
error(str, NULL, NULL);
1991-10-25 18:23:32 +00:00
trapcc(SIGINT);
}