Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20081212075612

Diff

Diff from 1.135 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/pcc/pcc/cc/cc/cc.c

Annotated File View

gmcgarry
1.135
1 /*      $Id: cc.c,v 1.135 2008/12/12 07:56:12 gmcgarry Exp $    */
ragge
1.128
2 /*
3  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code and documentation must retain the above
10  * copyright notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditionsand the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * All advertising materials mentioning features or use of this software
15  * must display the following acknowledgement:
16  *      This product includes software developed or owned by Caldera
17  *      International, Inc.
18  * Neither the name of Caldera International, Inc. nor the names of other
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27  * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 /*
37  * Front-end to the C compiler.
38  *
39  * Brief description of its syntax:
40  * - Files that end with .c are passed via cpp->ccom->as->ld
41  * - Files that end with .i are passed via ccom->as->ld
42  * - Files that end with .s are passed as->ld
43  * - Files that end with .o are passed directly to ld
44  * - Multiple files may be given on the command line.
45  * - Unrecognized options are all sent directly to ld.
46  * -c or -S cannot be combined with -o if multiple files are given.
47  *
48  * This file should be rewritten readable.
49  */
50 #include "config.h"
51
52 #include <sys/types.h>
53 #ifdef HAVE_SYS_WAIT_H
54 #include <sys/wait.h>
55 #endif
56
57 #include <ctype.h>
58 #include <errno.h>
59 #include <fcntl.h>
60 #ifdef HAVE_LIBGEN_H
61 #include <libgen.h>
62 #endif
63 #include <signal.h>
64 #include <stdarg.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #ifdef HAVE_UNISTD_H
69 #include <unistd.h>
70 #endif
71
72 #ifdef WIN32
73 #include <windows.h>
74 #include <process.h>
75 #include <io.h>
76 #endif
77
78 #include "compat.h"
79
80 #include "ccconfig.h"
81 /* C command */
82
83 #define MKS(x) _MKS(x)
84 #define _MKS(x) #x
85
86 /*
87  * Many specific definitions, should be declared elsewhere.
88  */
89
90 #ifndef STDINC
91 #define STDINC          "/usr/include/"
92 #endif
93
94 #ifndef LIBDIR
95 #define LIBDIR          "/usr/lib/"
96 #endif
97
98 #ifndef PREPROCESSOR
99 #define PREPROCESSOR    "cpp"
100 #endif
101
102 #ifndef COMPILER
103 #define COMPILER        "ccom";
104 #endif
105
106 #ifndef ASSEMBLER
107 #define ASSEMBLER       "as"
108 #endif
109
110 #ifndef LINKER
111 #define LINKER          "ld"
112 #endif
113
114 #define OS MKS(TARGOS)
115 #define MACH MKS(TARGMACH)
116 #ifndef PCCINCDIR
117 #define PCCINCDIR       LIBDIR "pcc/" MACH "-" OS "/" PACKAGE_VERSION "/include"
118 #endif
119 #ifndef PCCLIBDIR
120 #define PCCLIBDIR       LIBDIR "pcc/" MACH "-" OS "/" PACKAGE_VERSION "/lib"
121 #endif
122
123 #define MAXFIL 10000
124 #define MAXLIB 10000
125 #define MAXAV  10000
126 #define MAXOPT 100
127 char    *tmp3;
128 char    *tmp4;
129 char    *outfile, *ermfile;
130 char *Bprefix(char *);
131 char *copy(char *, int),*setsuf(char *, char);
132 int getsuf(char *);
133 int main(intchar *[]);
134 void error(char *, ...);
135 void errorx(intchar *, ...);
136 int callsys(char [], char *[]);
137 int cunlink(char *);
138 void dexit(int);
139 void idexit(int);
140 char *gettmp(void);
141 void *ccmalloc(int size);
142 #ifdef WIN32
143 char *win32pathsubst(char *);
144 #endif
145 char    *av[MAXAV];
146 char    *clist[MAXFIL];
147 char    *llist[MAXLIB];
148 char    *aslist[MAXAV];
149 char    alist[20];
150 char    *xlist[100];
151 int     xnum;
152 char    *mlist[100];
153 char    *flist[100];
154 char    *wlist[100];
155 char    *idirafter;
156 int     nm;
157 int     nf;
158 int     nw;
159 int     sspflag;
160 int     Cflag;
gmcgarry
1.134
161 int     Vflag;
ragge
1.128
162 int     dflag;
163 int     pflag;
164 int     sflag;
165 int     cflag;
166 int     eflag;
167 int     gflag;
168 int     vflag;
169 int     tflag;
170 int     Eflag;
171 int     Oflag;
172 int     kflag;  /* generate PIC/pic code */
173 #define F_PIC   1
174 #define F_pic   2
175 int     Mflag;  /* dependencies only */
176 int     pgflag;
177 int     exfail;
178 int     Xflag;
179 int     Wallflag;
180 int     Wflag;
181 int     nostartfilesBstaticshared;
182 int     nostdincnostdlib;
183 int     onlyas;
184 int     pthreads;
185 int     xcflag;
186 int     ascpp;
187
188 char    *passp = LIBEXECDIR PREPROCESSOR;
189 char    *pass0 = LIBEXECDIR COMPILER;
190 char    *as = ASSEMBLER;
191 char    *ld = LINKER;
192 char    *Bflag;
193 char *cppadd[] = CPPADD;
194 #ifdef DYNLINKER
195 char *dynlinker[] = DYNLINKER;
196 #endif
197 #ifdef CRT0FILE
198 char *crt0file = CRT0FILE;
199 #endif
200 #ifdef CRT0FILE_PROFILE
201 char *crt0file_profile = CRT0FILE_PROFILE;
202 #endif
203 #ifdef STARTFILES
204 char *startfiles[] = STARTFILES;
205 char *endfiles[] = ENDFILES;
206 #endif
207 #ifdef STARTFILES_T
208 char *startfiles_T[] = STARTFILES_T;
209 char *endfiles_T[] = ENDFILES_T;
210 #endif
211 #ifdef STARTFILES_S
212 char *startfiles_S[] = STARTFILES_S;
213 char *endfiles_S[] = ENDFILES_S;
214 #endif
215 #ifdef MULTITARGET
216 char *mach = DEFMACH;
217 struct cppmd {
218         char *mach;
219         char *cppmdadd[MAXCPPMDARGS];
220 };
221
222 struct cppmd cppmds[] = CPPMDADDS;
223 #else
224 char *cppmdadd[] = CPPMDADD;
225 #endif
226 #ifdef LIBCLIBS
227 char *libclibs[] = LIBCLIBS;
228 #else
229 char *libclibs[] = { "-lc"NULL };
230 #endif
231 #ifdef LIBCLIBS_PROFILE
232 char *libclibs_profile[] = LIBCLIBS_PROFILE;
233 #else
234 char *libclibs_profile[] = { "-lc_p"NULL };
235 #endif
236 #ifndef STARTLABEL
237 #define STARTLABEL "__start"
238 #endif
239 char *incdir = STDINC;
240 char *pccincdir = PCCINCDIR;
241 char *pcclibdir = PCCLIBDIR;
242
243 /* handle gcc warning emulations */
244 struct Wflags {
245         char *name;
246         int flags;
247 #define INWALL          1
248 #define NEGATIVE        2
249 Wflags[] = {
250         { "-Werror"0 },
251         { "-Wshadow"0 },
252         { "-Wno-shadow"NEGATIVE },
253         { "-Wpointer-sign"INWALL },
254         { "-Wno-pointer-sign"NEGATIVE },
255         { "-Wsign-compare"0 },
256         { "-Wno-sign-compare"NEGATIVE },
257         { "-Wunknown-pragmas"INWALL },
258         { "-Wno-unknown-pragmas"NEGATIVE },
259         { "-Wunreachable-code"0 },
260         { "-Wno-unreachable-code"NEGATIVE },
261         { 00 },
262 };
263
264 #define SZWFL   (sizeof(Wflags)/sizeof(Wflags[0]))
265
266 int
267 main(int argcchar *argv[])
268 {
269         struct Wflags *Wf;
270         char *t, *u;
271         char *assource;
272         char **pv, *ptemp[MAXOPT], **pvt;
273         int ncnlnasijcnxona;
274 #ifdef MULTITARGET
275         int k;
276 #endif
277
278 #ifdef WIN32
279         /* have to prefix path early.  -B may override */
280         incdir = win32pathsubst(incdir);
281         pccincdir = win32pathsubst(pccincdir);
282         pcclibdir = win32pathsubst(pcclibdir);
283         passp = win32pathsubst(passp);
284         pass0 = win32pathsubst(pass0);
285 #endif
286
287         i = nc = nl = nas = nxo = 0;
288         pv = ptemp;
289         while(++i < argc) {
290                 if (argv[i][0] == '-') {
291                         switch (argv[i][1]) {
292                         default:
293                                 goto passa;
294 #ifdef notyet
295         /* must add library options first (-L/-l/...) */
296                                 error("unrecognized option `-%c'"argv[i][1]);
297                                 break;
298 #endif
299
300                         case '-'/* double -'s */
ragge
1.130
301                                 if (strcmp(argv[i], "--version") == 0) {
ragge
1.128
302                                         printf("%s\n"VERSSTR);
ragge
1.130
303                                         return 0;
304                                 } else if (strcmp(argv[i], "--param") == 0)
ragge
1.128
305                                         /* NOTHING YET */;
306                                 else
307                                         error("unrecognized option %s"argv[i]);
308                                 break;
309
310                         case 'B'/* other search paths for binaries */
311                                 Bflag = &argv[i][2];
312                                 break;
313
314 #ifdef MULTITARGET
315                         case 'b':
316                                 t = &argv[i][2];
317                                 if (*t == '\0' && i + 1 < argc) {
318                                         t = argv[i+1];
319                                         i++;
320                                 }
321                                 if (strncmp(t"?"1) == 0) {
322                                         /* show machine targets */
323                                         printf("Available machine targets:");
324                                         for (j=0cppmds[j].machj++)
325                                                 printf(" %s",cppmds[j].mach);
326                                         printf("\n");
327                                         exit(0);
328                                 }
329                                 for (j=0cppmds[j].machj++)
330                                         if (strcmp(tcppmds[j].mach) == 0) {
331                                                 mach = cppmds[j].mach;
332                                                 break;
333                                         }
334                                 if (cppmds[j].mach == NULL)
335                                         errorx(1"unknown target arch %s"t);
336                                 break;
337 #endif
338
339                         case 'X':
340                                 Xflag++;
341                                 break;
342                         case 'W'/* Ignore (most of) W-flags */
343                                 if (strncmp(argv[i], "-Wl,"4) == 0) {
344                                         /* options to the linker */
345                                         t = &argv[i][4];
346                                         while ((u = strchr(t','))) {
347                                                 *u++ = 0;
348                                                 llist[nl++] = t;
349                                                 t = u;
350                                         }
351                                         llist[nl++] = t;
352                                 } else if (strncmp(argv[i], "-Wa,"4) == 0) {
353                                         /* options to the assembler */
354                                         t = &argv[i][4];
355                                         while ((u = strchr(t','))) {
356                                                 *u++ = 0;
357                                                 aslist[nas++] = t;
358                                                 t = u;
359                                         }
360                                         aslist[nas++] = t;
361                                 } else if (strncmp(argv[i], "-Wp,"4) == 0) {
362                                         /* preprocessor */
363                                         if (!strncmp(argv[i], "-Wp,-C"6))
364                                                 Cflag++;
gmcgarry
1.134
365                                         else if (!strncmp(argv[i], "-Wp,-V"6))
366                                                 Vflag++;
ragge
1.128
367                                 } else if (strcmp(argv[i], "-Wall") == 0) {
368                                         Wallflag = 1;
369                                 } else if (strcmp(argv[i], "-WW") == 0) {
370                                         Wflag = 1;
371                                 } else {
372                                         /* check and set if available */
373                                         for (Wf = WflagsWf->nameWf++) {
374                                                 if (strcmp(argv[i], Wf->name))
375                                                         continue;
376                                                 wlist[nw++] = Wf->name;
377                                         }
378                                 }
379                                 break;
380
381                         case 'f'/* GCC compatibility flags */
382                                 if (strcmp(argv[i], "-fPIC") == 0)
383                                         kflag = F_PIC;
384                                 else if (strcmp(argv[i], "-fpic") == 0)
385                                         kflag = F_pic;
386                                 else if (strcmp(argv[i],
387                                     "-fsigned-char") == 0)
388                                         flist[nf++] = argv[i];
389                                 else if (strcmp(argv[i],
390                                     "-fno-signed-char") == 0)
391                                         flist[nf++] = argv[i];
392                                 else if (strcmp(argv[i],
393                                     "-funsigned-char") == 0)
394                                         flist[nf++] = argv[i];
395                                 else if (strcmp(argv[i],
396                                     "-fno-unsigned-char") == 0)
397                                         flist[nf++] = argv[i];
398                                 else if (strcmp(argv[i],
399                                     "-fstack-protector") == 0) {
400                                         flist[nf++] = argv[i];
401                                         sspflag++;
402                                 } else if (strcmp(argv[i],
403                                     "-fstack-protector-all") == 0) {
404                                         flist[nf++] = argv[i];
405                                         sspflag++;
406                                 } else if (strcmp(argv[i],
407                                     "-fno-stack-protector") == 0) {
408                                         flist[nf++] = argv[i];
409                                         sspflag = 0;
410                                 } else if (strcmp(argv[i],
411                                     "-fno-stack-protector-all") == 0) {
412                                         flist[nf++] = argv[i];
413                                         sspflag = 0;
414                                 }
415                                 /* silently ignore the rest */
416                                 break;
417
418                         case 'g'/* create debug output */
419                                 gflag++;
420                                 break;
421
422                         case 'i':
423                                 if (strcmp(argv[i], "-isystem") == 0) {
424                                         *pv++ = "-S";
425                                         *pv++ = argv[++i];
426                                 } else if (strcmp(argv[i], "-include") == 0) {
427                                         *pv++ = "-i";
428                                         *pv++ = argv[++i];
429                                 } else if (strcmp(argv[i], "-idirafter") == 0) {
430                                         idirafter = argv[++i];
431                                 } else
432                                         goto passa;
433                                 break;
434
435                         case 'k'/* generate PIC code */
436                                 kflag = F_pic;
437                                 break;
438
439                         case 'm'/* target-dependent options */
440                                 mlist[nm++] = argv[i];
441                                 break;
442
443                         case 'n'/* handle -n flags */
444                                 if (strcmp(argv[i], "-nostdinc") == 0)
445                                         nostdinc++;
446                                 else if (strcmp(argv[i], "-nostdlib") == 0) {
447                                         nostdlib++;
448                                         nostartfiles++;
449                                 } else if (strcmp(argv[i], "-nostartfiles") == 0)
450                                         nostartfiles = 1;
451                                 else
452                                         goto passa;
453                                 break;
454
455                         case 'p':
456                                 if (strcmp(argv[i], "-pg") == 0 ||
457                                     strcmp(argv[i], "-p") == 0)
458                                         pgflag++;
459                                 else if (strcmp(argv[i], "-pthread") == 0)
460                                         pthreads++;
461                                 else if (strcmp(argv[i], "-pipe") == 0)
462                                         /* NOTHING YET */;
463                                 else
464                                         errorx(1"unknown option %s"argv[i]);
465                                 break;
466
467                         case 'x':
468                                 t = &argv[i][2];
469                                 if (*t == 0)
470                                         t = argv[++i];
471                                 if (strcmp(t"c") == 0)
472                                         xcflag = 1/* default */
473                                 else if (strcmp(t"assembler-with-cpp") == 0)
474                                         ascpp = 1;
475 #ifdef notyet
476                                 else if (strcmp(t"c++") == 0)
477                                         cxxflag++;
478 #endif
479                                 else
480                                         xlist[xnum++] = argv[i];
481                                 break;
482                         case 't':
483                                 tflag++;
484                                 break;
485                         case 'S':
486                                 sflag++;
487                                 cflag++;
488                                 break;
489                         case 'o':
490                                 if (outfile)
491                                         errorx(8"too many -o");
492                                 outfile = argv[++i];
493                                 break;
494                         case 'O':
495                                 if (argv[i][2] == '0')
496                                         Oflag = 0;
497                                 else
498                                         Oflag++;
499                                 break;
500                         case 'E':
501                                 Eflag++;
502                                 break;
503                         case 'P':
504                                 pflag++;
505                                 *pv++ = argv[i];
506                         case 'c':
507                                 cflag++;
508                                 break;
509
510 #if 0
511                         case '2':
512                                 if(argv[i][2] == '\0')
513                                         pref = "/lib/crt2.o";
514                                 else {
515                                         pref = "/lib/crt20.o";
516                                 }
517                                 break;
518 #endif
519                         case 'C':
520                                 Cflag = 1;
521                                 break;
522                         case 'D':
523                         case 'I':
524                         case 'U':
525                                 *pv++ = argv[i];
526                                 if (argv[i][2] == 0)
527                                         *pv++ = argv[++i];
528                                 if (pv >= ptemp+MAXOPT) {
529                                         error("Too many DIU options");
530                                         --pv;
531                                 }
532                                 break;
533
534                         case 'M':
535                                 Mflag++;
536                                 break;
537
538                         case 'd':
539                                 dflag++;
540                                 strlcpy(alistargv[i], sizeof (alist));
541                                 break;
542                         case 'v':
543                                 printf("%s\n"VERSSTR);
544                                 vflag++;
545                                 break;
546
547                         case 's':
548                                 if (strcmp(argv[i], "-static") == 0)
549                                         Bstatic = 1;
550                                 else if (strcmp(argv[i], "-shared") == 0) {
551                                         shared = 1;
552 #ifndef os_win32
553                                         nostdlib = 1;
554 #endif
555                                 } else if (strncmp(argv[i], "-std"4) == 0) {
556                                         /* ignore gcc -std= */;
557                                 } else
558                                         goto passa;
559                                 break;
560                         }
561                 } else {
562                 passa:
563                         t = argv[i];
564                         if (*argv[i] == '-' && argv[i][1] == 'L')
565                                 ;
566                         else if((c=getsuf(t))=='c' || c=='S' || c=='i' ||
567                             c=='s'|| Eflag || xcflag) {
568                                 clist[nc++] = t;
569                                 if (nc>=MAXFIL) {
570                                         error("Too many source files");
571                                         exit(1);
572                                 }
573                                 t = setsuf(t'o');
574                         }
575
576                         /* Check for duplicate .o files. */
577                         for (j = getsuf(t) == 'o' ? 0 : nlj < nlj++) {
578                                 if (strcmp(llist[j], t) == 0)
579                                         break;
580                         }
581                         if (j == nl) {
582                                 llist[nl++] = t;
583                                 if (nl >= MAXLIB) {
584                                         error("Too many object/library files");
585                                         exit(1);
586                                 }
587                                 if (getsuf(t)=='o')
588                                         nxo++;
589                         }
590                 }
591         }
592         /* Sanity checking */
593         if (nc == 0 && nl == 0)
594                 errorx(8"no input files");
595         if (outfile && (cflag || sflag || Eflag) && nc > 1)
596                 errorx(8"-o given with -c || -E || -S and more than one file");
597         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
598                 errorx(8"output file will be clobbered");
599 #if 0
600         if (proflag)
601                 pref = "/lib/mcrt0.o";
602 #endif
603         if(nc==0)
604                 goto nocom;
605         if (pflag==0) {
606                 if (!sflag)
607                         tmp3 = gettmp();
608                 tmp4 = gettmp();
609         }
610         if (Bflag) {
611                 incdir = Bflag;
612                 pccincdir = Bflag;
613                 pcclibdir = Bflag;
614         }
615         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
616                 signal(SIGINTidexit);
617         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
618                 signal(SIGTERMidexit);
619 #ifdef MULTITARGET
ragge
1.133
620         pass0 = copy(LIBEXECDIR "/ccom_"k = strlen(mach));
621         strlcat(pass0machsizeof(LIBEXECDIR "/ccom_") + k);
ragge
1.128
622 #endif
623         pvt = pv;
624         for (i=0i<nci++) {
625                 /*
626                  * C preprocessor
627                  */
628                 if (nc>1 && !Eflag)
629                         printf("%s:\n"clist[i]);
630                 onlyas = 0;
631                 assource = tmp3;
632                 if (getsuf(clist[i])=='S')
633                         ascpp = 1;
634                 if (getsuf(clist[i])=='i') {
635                         if(Eflag)
636                                 continue;
637                         goto com;
638                 } else if (ascpp) {
639                         onlyas = 1;
640                 } else if (getsuf(clist[i])=='s') {
641                         assource = clist[i];
642                         goto assemble;
643                 }
644                 if (pflag)
645                         tmp4 = setsuf(clist[i], 'i');
646                 na = 0;
647                 av[na++] = "cpp";
648                 if (vflag)
649                         av[na++] = "-v";
650                 av[na++] = "-D__PCC__=" MKS(PCC_MAJOR);
651                 av[na++] = "-D__PCC_MINOR__=" MKS(PCC_MINOR);
652                 av[na++] = "-D__PCC_MINORMINOR__=" MKS(PCC_MINORMINOR);
653                 if (ascpp)
654                         av[na++] = "-D__ASSEMBLER__";
655                 if (sspflag)
656                         av[na++] = "-D__SSP__=1";
657                 if (pthreads)
658                         av[na++] = "-D_PTHREADS";
659                 if (Cflag)
660                         av[na++] = "-C";
661                 if (Mflag)
662                         av[na++] = "-M";
gmcgarry
1.134
663                 if (Vflag)
664                         av[na++] = "-V";
ragge
1.128
665                 if (dflag)
666                         av[na++] = alist;
667                 for (j = 0cppadd[j]; j++)
668                         av[na++] = cppadd[j];
669 #ifdef MULTITARGET
670                 for (k = 0cppmds[k].machk++) {
671                         if (strcmp(cppmds[k].machmach) != 0)
672                                 continue;
673                         for (j = 0cppmds[k].cppmdadd[j]; j++)
674                                 av[na++] = cppmds[k].cppmdadd[j];
675                         break;
676                 }
677 #else
678                 for (j = 0cppmdadd[j]; j++)
679                         av[na++] = cppmdadd[j];
680 #endif
681                 if (tflag)
682                         av[na++] = "-t";
683                 for(pv=ptemppv <pvtpv++)
684                         av[na++] = *pv;
685                 if (!nostdinc)
686                         av[na++] = "-S"av[na++] = incdir;
687                 av[na++] = "-I"av[na++] = pccincdir;
688                 if (idirafter) {
689                         av[na++] = "-I";
690                         av[na++] = idirafter;
691                 }
692                 av[na++] = clist[i];
693                 if (!Eflag && !Mflag)
694                         av[na++] = tmp4;
695                 if (Eflag && outfile)
696                          ermfile = av[na++] = outfile;
697                 av[na++]=0;
698                 if (callsys(passpav))
699                         {exfail++; eflag++;}
700                 if (Eflag || Mflag)
701                         continue;
702                 if (onlyas) {
703                         assource = tmp4;
704                         goto assemble;
705                 }
706
707                 /*
708                  * C compiler
709                  */
710         com:
711                 na = 0;
712                 av[na++]= "ccom";
713                 if (Wallflag) {
714                         /* Set only the same flags as gcc */
715                         for (Wf = WflagsWf->nameWf++) {
716                                 if (Wf->flags != INWALL)
717                                         continue;
718                                 av[na++] = Wf->name;
719                         }
720                 }
721                 if (Wflag) {
722                         /* set all positive flags */
723                         for (Wf = WflagsWf->nameWf++) {
724                                 if (Wf->flags == NEGATIVE)
725                                         continue;
726                                 av[na++] = Wf->name;
727                         }
728                 }
729                 for (j = 0j < nwj++)
730                         av[na++] = wlist[j];
731                 for (j = 0j < nfj++)
732                         av[na++] = flist[j];
733                 if (vflag)
734                         av[na++] = "-v";
735                 if (pgflag)
736                         av[na++] = "-p";
737                 if (gflag)
738                         av[na++] = "-g";
739 #ifdef os_darwin
740                 /* darwin always wants PIC compilation */
741                 if (!Bstatic)
742                         av[na++] = "-k";
743 #else
744                 if (kflag)
745                         av[na++] = "-k";
746 #endif
747                 if (Oflag) {
748                         av[na++] = "-xtemps";
749                         av[na++] = "-xdeljumps";
ragge
1.133
750                         av[na++] = "-xinline";
ragge
1.128
751                 }
752                 for (j = 0j < xnumj++)
753                         av[na++] = xlist[j];
754                 for (j = 0j < nmj++)
755                         av[na++] = mlist[j];
756                 if (getsuf(clist[i])=='i')
757                         av[na++] = clist[i];
758                 else
759                         av[na++] = tmp4/* created by cpp */
760                 if (pflag || exfail)
761                         {
762                         cflag++;
763                         continue;
764                         }
765                 if(sflag) {
766                         if (outfile)
767                                 tmp3 = outfile;
768                         else
769                                 tmp3 = setsuf(clist[i], 's');
770                 }
771                 ermfile = av[na++] = tmp3;
772 #if 0
773                 if (proflag) {
774                         av[3] = "-XP";
775                         av[4] = 0;
776                 } else
777                         av[3] = 0;
778 #endif
779                 av[na++] = NULL;
780                 if (callsys(pass0av)) {
781                         cflag++;
782                         eflag++;
783                         continue;
784                 }
785                 if (sflag)
786                         continue;
787
788                 /*
789                  * Assembler
790                  */
791         assemble:
792                 na = 0;
793                 av[na++] = as;
794                 for (j = 0j < nasj++)
795                         av[na++] = aslist[j];
796 #if defined(os_sunos) && defined(mach_sparc64)
797                 av[na++] = "-m64";
798 #endif
799 #if defined(os_darwin)
800                 if (Bstatic)
801                         av[na++] = "-static";
802 #endif
803                 if (vflag)
804                         av[na++] = "-v";
805                 if (kflag)
806                         av[na++] = "-k";
807                 av[na++] = "-o";
808                 if (outfile && cflag)
809                         ermfile = av[na++] = outfile;
810                 else
811                         ermfile = av[na++] = setsuf(clist[i], 'o');
812                 av[na++] = assource;
813                 if (dflag)
814                         av[na++] = alist;
815                 av[na++] = 0;
816                 if (callsys(asav)) {
817                         cflag++;
818                         eflag++;
819                         cunlink(tmp4);
820                         continue;
821                 }
822                 cunlink(tmp4);
823         }
824
825         if (Eflag || Mflag)
826                 dexit(eflag);
827
828         /*
829          * Linker
830          */
831 nocom:
832         if (cflag==0 && nl!=0) {
833                 j = 0;
834                 av[j++] = ld;
835 #ifndef MSLINKER
836                 if (vflag)
837                         av[j++] = "-v";
838 #endif
gmcgarry
1.131
839 #if !defined(os_sunos) && !defined(os_win32) && !defined(os_darwin)
ragge
1.128
840                 av[j++] = "-X";
841 #endif
842                 if (shared) {
843                         av[j++] = "-shared";
844 #ifdef os_win32
845                         av[j++] = "-Bdynamic";
846 #endif
847 #ifndef os_sunos
848                 } else {
849 #ifndef os_win32
850 #ifndef os_darwin
851                         av[j++] = "-d";
852 #endif
853                         av[j++] = "-e";
854                         av[j++] = STARTLABEL;
855 #endif
856 #endif
857                         if (Bstatic == 0) { /* Dynamic linkage */
858 #ifdef DYNLINKER
859                                 for (i = 0dynlinker[i]; i++)
860                                         av[j++] = dynlinker[i];
861 #endif
862                         } else {
863 #ifdef os_darwin
864                                 av[j++] = "-static";
865 #else
866                                 av[j++] = "-Bstatic";
867 #endif
868                         }
869                 }
870                 if (outfile) {
871 #ifdef MSLINKER
gmcgarry
1.135
872 #define OUTSTR  "/OUT:"
873                         char *s = copy(OUTSTRi = strlen(outfile));
874                         strlcat(soutfilesizeof(OUTSTR) + i);
ragge
1.128
875                         av[j++] = s;
876 #else
877                         av[j++] = "-o";
878                         av[j++] = outfile;
879 #endif
880                 }
881 #ifdef STARTFILES_S
882                 if (shared) {
883                         if (!nostartfiles) {
884                                 for (i = 0startfiles_S[i]; i++)
885                                         av[j++] = Bprefix(startfiles_S[i]);
886                         }
887                 } else
888 #endif
889                 {
890                         if (!nostartfiles) {
891 #ifdef CRT0FILE_PROFILE
892                                 if (pgflag) {
893                                         av[j++] = Bprefix(crt0file_profile);
894                                 } else
895 #endif
896                                 {
897 #ifdef CRT0FILE
898                                         av[j++] = Bprefix(crt0file);
899 #endif
900                                 }
901 #ifdef STARTFILES_T
902                                 if (Bstatic) {
903                                         for (i = 0startfiles_T[i]; i++)
ragge
1.129
904                                                 av[j++] = Bprefix(startfiles_T[i]);
ragge
1.128
905                                 } else
906 #endif
907                                 {
908 #ifdef STARTFILES
909                                         for (i = 0startfiles[i]; i++)
910                                                 av[j++] = Bprefix(startfiles[i]);
911 #endif
912                                 }
913                         }
914                 }
915                 i = 0;
916                 while(i<nl) {
917                         av[j++] = llist[i++];
918                         if (j >= MAXAV)
919                                 error("Too many ld options");
920                 }
921 #ifndef MACHOABI
922                 /* darwin assembler doesn't want -g */
923                 if (gflag)
924                         av[j++] = "-g";
925 #endif
926 #if 0
927                 if (gflag)
928                         av[j++] = "-lg";
929 #endif
930                 if (pthreads)
931                         av[j++] = "-lpthread";
932                 if (!nostdlib) {
933 #ifdef MSLINKER
ragge
1.133
934 #define DL      "/LIBPATH:"
ragge
1.128
935 #else
ragge
1.133
936 #define DL      "-L"
ragge
1.128
937 #endif
ragge
1.133
938                         char *s = copy(DLi = strlen(pcclibdir));
939                         strlcat(spcclibdirsizeof(DL) +i );
ragge
1.128
940                         av[j++] = s;
941                         if (pgflag) {
942                                 for (i = 0libclibs_profile[i]; i++)
943                                         av[j++] = Bprefix(libclibs_profile[i]);
944                         } else {
945                                 for (i = 0libclibs[i]; i++)
946                                         av[j++] = Bprefix(libclibs[i]);
947                         }
948                 }
949                 if (!nostartfiles) {
950 #ifdef STARTFILES_S
951                         if (shared) {
952                                 for (i = 0endfiles_S[i]; i++)
953                                         av[j++] = Bprefix(endfiles_S[i]);
954                         } else 
955 #endif
956                         {
957 #ifdef STARTFILES_T
958                                 if (Bstatic) {
959                                         for (i = 0endfiles_T[i]; i++)
960                                                 av[j++] = Bprefix(endfiles_T[i]);
961                                 } else
962 #endif
963                                 {
964 #ifdef STARTFILES
965                                         for (i = 0endfiles[i]; i++)
966                                                 av[j++] = Bprefix(endfiles[i]);
967 #endif
968                                 }
969                         }
970                 }
971                 av[j++] = 0;
972                 eflag |= callsys(ldav);
973                 if (nc==1 && nxo==1 && eflag==0)
974                         cunlink(setsuf(clist[0], 'o'));
975                 else if (nc > 0 && eflag == 0) {
976                         /* remove .o files XXX ugly */
977                         for (i = 0i < nci++)
978                                 cunlink(setsuf(clist[i], 'o'));
979                 }
980         }
981         dexit(eflag);
982         return 0;
983 }
984
985 /*
986  * exit and cleanup after interrupt.
987  */
988 void
989 idexit(int arg)
990 {
991         dexit(100);
992 }
993
994 /*
995  * exit and cleanup.
996  */
997 void
998 dexit(int eval)
999 {
1000         if (!pflag && !Xflag) {
1001                 if (sflag==0)
1002                         cunlink(tmp3);
1003                 cunlink(tmp4);
1004         }
1005         if (exfail || eflag)
1006                 cunlink(ermfile);
1007         if (eval == 100)
1008                 _exit(eval);
1009         exit(eval);
1010 }
1011
1012 static void
1013 ccerror(char *sva_list ap)
1014 {
1015         vfprintf(Eflag ? stderr : stdoutsap);
1016         putc('\n'Eflagstderr : stdout);
1017         exfail++;
1018         cflag++;
1019         eflag++;
1020 }
1021
1022 /*
1023  * complain a bit.
1024  */
1025 void
1026 error(char *s, ...)
1027 {
1028         va_list ap;
1029
1030         va_start(aps);
1031         ccerror(sap);
1032         va_end(ap);
1033 }
1034
1035 /*
1036  * complain a bit and then exit.
1037  */
1038 void
1039 errorx(int evalchar *s, ...)
1040 {
1041         va_list ap;
1042
1043         va_start(aps);
1044         ccerror(sap);
1045         va_end(ap);
1046         dexit(eval);
1047 }
1048
1049 char *
1050 Bprefix(char *s)
1051 {
1052         char *suffix;
1053         char *str;
ragge
1.133
1054         int i;
ragge
1.128
1055
1056         if (Bflag == NULL || s[0] != '/')
1057                 return s;
1058
1059         suffix = strrchr(s'/');
1060         if (suffix == NULL)
1061                 suffix = s;
1062
ragge
1.133
1063         str = copy(Bflagi = strlen(suffix));
1064         strlcat(strsuffixstrlen(Bflag) + i + 1);
ragge
1.128
1065         return str;
1066 }
1067
1068 int
1069 getsuf(char *s)
1070 {
1071         register char *p;
1072
1073         if ((p = strrchr(s'.')) && p[1] != '\0' && p[2] == '\0')
1074                 return p[1];
1075         return(0);
1076 }
1077
1078 /*
1079  * Get basename of string s and change its suffix to ch.
1080  */
1081 char *
1082 setsuf(char *schar ch)
1083 {
1084         char *p;
1085
1086         s = copy(basename(s), 2);
1087         if ((p = strrchr(s'.')) == NULL) {
1088                 p = s + strlen(s);
1089                 p[0] = '.';
1090         }
1091         p[1] = ch;
1092         p[2] = '\0';
1093         return(s);
1094 }
1095
1096 #ifdef WIN32
1097 int
1098 callsys(char *fchar *v[])
1099 {
gmcgarry
1.131
1100         int t;
ragge
1.128
1101         char cmd[MAX_PATH];
1102         int len;
1103         STARTUPINFO si;
1104         PROCESS_INFORMATION pi;
1105         DWORD exitCode;
1106         BOOL ok;
1107
1108         len = strlcpy(cmdfMAX_PATH);
1109         for (t = 1v[t] && len < MAX_PATHt++) {
1110                 len = strlcat(cmd" "MAX_PATH);
1111                 len = strlcat(cmdv[t], MAX_PATH);
1112         }
1113
1114         if (vflag)
1115                 printf("%s\n"cmd);
1116
1117         ZeroMemory(&sisizeof(STARTUPINFO));
1118         si.cb = sizeof(STARTUPINFO);
1119         ZeroMemory(&pisizeof(PROCESS_INFORMATION));
1120