Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20081203071424

Diff

Diff from 1.132 to:

Annotations

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

Annotated File View

ragge
1.132
1 /*      $Id: cc.c,v 1.132 2008/12/03 07:14:24 ragge 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;
161 int     dflag;
162 int     pflag;
163 int     sflag;
164 int     cflag;
165 int     eflag;
166 int     gflag;
167 int     vflag;
168 int     tflag;
169 int     Eflag;
170 int     Oflag;
171 int     kflag;  /* generate PIC/pic code */
172 #define F_PIC   1
173 #define F_pic   2
174 int     Mflag;  /* dependencies only */
175 int     pgflag;
176 int     exfail;
177 int     Xflag;
178 int     Wallflag;
179 int     Wflag;
180 int     nostartfilesBstaticshared;
181 int     nostdincnostdlib;
182 int     onlyas;
183 int     pthreads;
184 int     xcflag;
185 int     ascpp;
186
187 char    *passp = LIBEXECDIR PREPROCESSOR;
188 char    *pass0 = LIBEXECDIR COMPILER;
189 char    *as = ASSEMBLER;
190 char    *ld = LINKER;
191 char    *Bflag;
192 char *cppadd[] = CPPADD;
193 #ifdef DYNLINKER
194 char *dynlinker[] = DYNLINKER;
195 #endif
196 #ifdef CRT0FILE
197 char *crt0file = CRT0FILE;
198 #endif
199 #ifdef CRT0FILE_PROFILE
200 char *crt0file_profile = CRT0FILE_PROFILE;
201 #endif
202 #ifdef STARTFILES
203 char *startfiles[] = STARTFILES;
204 char *endfiles[] = ENDFILES;
205 #endif
206 #ifdef STARTFILES_T
207 char *startfiles_T[] = STARTFILES_T;
208 char *endfiles_T[] = ENDFILES_T;
209 #endif
210 #ifdef STARTFILES_S
211 char *startfiles_S[] = STARTFILES_S;
212 char *endfiles_S[] = ENDFILES_S;
213 #endif
214 #ifdef MULTITARGET
215 char *mach = DEFMACH;
216 struct cppmd {
217         char *mach;
218         char *cppmdadd[MAXCPPMDARGS];
219 };
220
221 struct cppmd cppmds[] = CPPMDADDS;
222 #else
223 char *cppmdadd[] = CPPMDADD;
224 #endif
225 #ifdef LIBCLIBS
226 char *libclibs[] = LIBCLIBS;
227 #else
228 char *libclibs[] = { "-lc"NULL };
229 #endif
230 #ifdef LIBCLIBS_PROFILE
231 char *libclibs_profile[] = LIBCLIBS_PROFILE;
232 #else
233 char *libclibs_profile[] = { "-lc_p"NULL };
234 #endif
235 #ifndef STARTLABEL
236 #define STARTLABEL "__start"
237 #endif
238 char *incdir = STDINC;
239 char *pccincdir = PCCINCDIR;
240 char *pcclibdir = PCCLIBDIR;
241
242 /* handle gcc warning emulations */
243 struct Wflags {
244         char *name;
245         int flags;
246 #define INWALL          1
247 #define NEGATIVE        2
248 Wflags[] = {
249         { "-Werror"0 },
250         { "-Wshadow"0 },
251         { "-Wno-shadow"NEGATIVE },
252         { "-Wpointer-sign"INWALL },
253         { "-Wno-pointer-sign"NEGATIVE },
254         { "-Wsign-compare"0 },
255         { "-Wno-sign-compare"NEGATIVE },
256         { "-Wunknown-pragmas"INWALL },
257         { "-Wno-unknown-pragmas"NEGATIVE },
258         { "-Wunreachable-code"0 },
259         { "-Wno-unreachable-code"NEGATIVE },
260         { 00 },
261 };
262
263 #define SZWFL   (sizeof(Wflags)/sizeof(Wflags[0]))
264
265 int
266 main(int argcchar *argv[])
267 {
268         struct Wflags *Wf;
269         char *t, *u;
270         char *assource;
271         char **pv, *ptemp[MAXOPT], **pvt;
272         int ncnlnasijcnxona;
273 #ifdef MULTITARGET
274         int k;
275 #endif
276
277 #ifdef WIN32
278         /* have to prefix path early.  -B may override */
279         incdir = win32pathsubst(incdir);
280         pccincdir = win32pathsubst(pccincdir);
281         pcclibdir = win32pathsubst(pcclibdir);
282         passp = win32pathsubst(passp);
283         pass0 = win32pathsubst(pass0);
284 #endif
285
286         i = nc = nl = nas = nxo = 0;
287         pv = ptemp;
288         while(++i < argc) {
289                 if (argv[i][0] == '-') {
290                         switch (argv[i][1]) {
291                         default:
292                                 goto passa;
293 #ifdef notyet
294         /* must add library options first (-L/-l/...) */
295                                 error("unrecognized option `-%c'"argv[i][1]);
296                                 break;
297 #endif
298
299                         case '-'/* double -'s */
ragge
1.130
300                                 if (strcmp(argv[i], "--version") == 0) {
ragge
1.128
301                                         printf("%s\n"VERSSTR);
ragge
1.130
302                                         return 0;
303                                 } else if (strcmp(argv[i], "--param") == 0)
ragge
1.128
304                                         /* NOTHING YET */;
305                                 else
306                                         error("unrecognized option %s"argv[i]);
307                                 break;
308
309                         case 'B'/* other search paths for binaries */
310                                 Bflag = &argv[i][2];
311                                 break;
312
313 #ifdef MULTITARGET
314                         case 'b':
315                                 t = &argv[i][2];
316                                 if (*t == '\0' && i + 1 < argc) {
317                                         t = argv[i+1];
318                                         i++;
319                                 }
320                                 if (strncmp(t"?"1) == 0) {
321                                         /* show machine targets */
322                                         printf("Available machine targets:");
323                                         for (j=0cppmds[j].machj++)
324                                                 printf(" %s",cppmds[j].mach);
325                                         printf("\n");
326                                         exit(0);
327                                 }
328                                 for (j=0cppmds[j].machj++)
329                                         if (strcmp(tcppmds[j].mach) == 0) {
330                                                 mach = cppmds[j].mach;
331                                                 break;
332                                         }
333                                 if (cppmds[j].mach == NULL)
334                                         errorx(1"unknown target arch %s"t);
335                                 break;
336 #endif
337
338                         case 'X':
339                                 Xflag++;
340                                 break;
341                         case 'W'/* Ignore (most of) W-flags */
342                                 if (strncmp(argv[i], "-Wl,"4) == 0) {
343                                         /* options to the linker */
344                                         t = &argv[i][4];
345                                         while ((u = strchr(t','))) {
346                                                 *u++ = 0;
347                                                 llist[nl++] = t;
348                                                 t = u;
349                                         }
350                                         llist[nl++] = t;
351                                 } else if (strncmp(argv[i], "-Wa,"4) == 0) {
352                                         /* options to the assembler */
353                                         t = &argv[i][4];
354                                         while ((u = strchr(t','))) {
355                                                 *u++ = 0;
356                                                 aslist[nas++] = t;
357                                                 t = u;
358                                         }
359                                         aslist[nas++] = t;
360                                 } else if (strncmp(argv[i], "-Wp,"4) == 0) {
361                                         /* preprocessor */
362                                         if (!strncmp(argv[i], "-Wp,-C"6))
363                                                 Cflag++;
364                                 } else if (strcmp(argv[i], "-Wall") == 0) {
365                                         Wallflag = 1;
366                                 } else if (strcmp(argv[i], "-WW") == 0) {
367                                         Wflag = 1;
368                                 } else {
369                                         /* check and set if available */
370                                         for (Wf = WflagsWf->nameWf++) {
371                                                 if (strcmp(argv[i], Wf->name))
372                                                         continue;
373                                                 wlist[nw++] = Wf->name;
374                                         }
375                                 }
376                                 break;
377
378                         case 'f'/* GCC compatibility flags */
379                                 if (strcmp(argv[i], "-fPIC") == 0)
380                                         kflag = F_PIC;
381                                 else if (strcmp(argv[i], "-fpic") == 0)
382                                         kflag = F_pic;
383                                 else if (strcmp(argv[i],
384                                     "-fsigned-char") == 0)
385                                         flist[nf++] = argv[i];
386                                 else if (strcmp(argv[i],
387                                     "-fno-signed-char") == 0)
388                                         flist[nf++] = argv[i];
389                                 else if (strcmp(argv[i],
390                                     "-funsigned-char") == 0)
391                                         flist[nf++] = argv[i];
392                                 else if (strcmp(argv[i],
393                                     "-fno-unsigned-char") == 0)
394                                         flist[nf++] = argv[i];
395                                 else if (strcmp(argv[i],
396                                     "-fstack-protector") == 0) {
397                                         flist[nf++] = argv[i];
398                                         sspflag++;
399                                 } else if (strcmp(argv[i],
400                                     "-fstack-protector-all") == 0) {
401                                         flist[nf++] = argv[i];
402                                         sspflag++;
403                                 } else if (strcmp(argv[i],
404                                     "-fno-stack-protector") == 0) {
405                                         flist[nf++] = argv[i];
406                                         sspflag = 0;
407                                 } else if (strcmp(argv[i],
408                                     "-fno-stack-protector-all") == 0) {
409                                         flist[nf++] = argv[i];
410                                         sspflag = 0;
411                                 }
412                                 /* silently ignore the rest */
413                                 break;
414
415                         case 'g'/* create debug output */
416                                 gflag++;
417                                 break;
418
419                         case 'i':
420                                 if (strcmp(argv[i], "-isystem") == 0) {
421                                         *pv++ = "-S";
422                                         *pv++ = argv[++i];
423                                 } else if (strcmp(argv[i], "-include") == 0) {
424                                         *pv++ = "-i";
425                                         *pv++ = argv[++i];
426                                 } else if (strcmp(argv[i], "-idirafter") == 0) {
427                                         idirafter = argv[++i];
428                                 } else
429                                         goto passa;
430                                 break;
431
432                         case 'k'/* generate PIC code */
433                                 kflag = F_pic;
434                                 break;
435
436                         case 'm'/* target-dependent options */
437                                 mlist[nm++] = argv[i];
438                                 break;
439
440                         case 'n'/* handle -n flags */
441                                 if (strcmp(argv[i], "-nostdinc") == 0)
442                                         nostdinc++;
443                                 else if (strcmp(argv[i], "-nostdlib") == 0) {
444                                         nostdlib++;
445                                         nostartfiles++;
446                                 } else if (strcmp(argv[i], "-nostartfiles") == 0)
447                                         nostartfiles = 1;
448                                 else
449                                         goto passa;
450                                 break;
451
452                         case 'p':
453                                 if (strcmp(argv[i], "-pg") == 0 ||
454                                     strcmp(argv[i], "-p") == 0)
455                                         pgflag++;
456                                 else if (strcmp(argv[i], "-pthread") == 0)
457                                         pthreads++;
458                                 else if (strcmp(argv[i], "-pipe") == 0)
459                                         /* NOTHING YET */;
460                                 else
461                                         errorx(1"unknown option %s"argv[i]);
462                                 break;
463
464                         case 'x':
465                                 t = &argv[i][2];
466                                 if (*t == 0)
467                                         t = argv[++i];
468                                 if (strcmp(t"c") == 0)
469                                         xcflag = 1/* default */
470                                 else if (strcmp(t"assembler-with-cpp") == 0)
471                                         ascpp = 1;
472 #ifdef notyet
473                                 else if (strcmp(t"c++") == 0)
474                                         cxxflag++;
475 #endif
476                                 else
477                                         xlist[xnum++] = argv[i];
478                                 break;
479                         case 't':
480                                 tflag++;
481                                 break;
482                         case 'S':
483                                 sflag++;
484                                 cflag++;
485                                 break;
486                         case 'o':
487                                 if (outfile)
488                                         errorx(8"too many -o");
489                                 outfile = argv[++i];
490                                 break;
491                         case 'O':
492                                 if (argv[i][2] == '0')
493                                         Oflag = 0;
494                                 else
495                                         Oflag++;
496                                 break;
497                         case 'E':
498                                 Eflag++;
499                                 break;
500                         case 'P':
501                                 pflag++;
502                                 *pv++ = argv[i];
503                         case 'c':
504                                 cflag++;
505                                 break;
506
507 #if 0
508                         case '2':
509                                 if(argv[i][2] == '\0')
510                                         pref = "/lib/crt2.o";
511                                 else {
512                                         pref = "/lib/crt20.o";
513                                 }
514                                 break;
515 #endif
516                         case 'C':
517                                 Cflag = 1;
518                                 break;
519                         case 'D':
520                         case 'I':
521                         case 'U':
522                                 *pv++ = argv[i];
523                                 if (argv[i][2] == 0)
524                                         *pv++ = argv[++i];
525                                 if (pv >= ptemp+MAXOPT) {
526                                         error("Too many DIU options");
527                                         --pv;
528                                 }
529                                 break;
530
531                         case 'M':
532                                 Mflag++;
533                                 break;
534
535                         case 'd':
536                                 dflag++;
537                                 strlcpy(alistargv[i], sizeof (alist));
538                                 break;
539                         case 'v':
540                                 printf("%s\n"VERSSTR);
541                                 vflag++;
542                                 break;
543
544                         case 's':
545                                 if (strcmp(argv[i], "-static") == 0)
546                                         Bstatic = 1;
547                                 else if (strcmp(argv[i], "-shared") == 0) {
548                                         shared = 1;
549 #ifndef os_win32
550                                         nostdlib = 1;
551 #endif
552                                 } else if (strncmp(argv[i], "-std"4) == 0) {
553                                         /* ignore gcc -std= */;
554                                 } else
555                                         goto passa;
556                                 break;
557                         }
558                 } else {
559                 passa:
560                         t = argv[i];
561                         if (*argv[i] == '-' && argv[i][1] == 'L')
562                                 ;
563                         else if((c=getsuf(t))=='c' || c=='S' || c=='i' ||
564                             c=='s'|| Eflag || xcflag) {
565                                 clist[nc++] = t;
566                                 if (nc>=MAXFIL) {
567                                         error("Too many source files");
568                                         exit(1);
569                                 }
570                                 t = setsuf(t'o');
571                         }
572
573                         /* Check for duplicate .o files. */
574                         for (j = getsuf(t) == 'o' ? 0 : nlj < nlj++) {
575                                 if (strcmp(llist[j], t) == 0)
576                                         break;
577                         }
578                         if (j == nl) {
579                                 llist[nl++] = t;
580                                 if (nl >= MAXLIB) {
581                                         error("Too many object/library files");
582                                         exit(1);
583                                 }
584                                 if (getsuf(t)=='o')
585                                         nxo++;
586                         }
587                 }
588         }
589         /* Sanity checking */
590         if (nc == 0 && nl == 0)
591                 errorx(8"no input files");
592         if (outfile && (cflag || sflag || Eflag) && nc > 1)
593                 errorx(8"-o given with -c || -E || -S and more than one file");
594         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
595                 errorx(8"output file will be clobbered");
596 #if 0
597         if (proflag)
598                 pref = "/lib/mcrt0.o";
599 #endif
600         if(nc==0)
601                 goto nocom;
602         if (pflag==0) {
603                 if (!sflag)
604                         tmp3 = gettmp();
605                 tmp4 = gettmp();
606         }
607         if (Bflag) {
608                 incdir = Bflag;
609                 pccincdir = Bflag;
610                 pcclibdir = Bflag;
611         }
612         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
613                 signal(SIGINTidexit);
614         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
615                 signal(SIGTERMidexit);
616 #ifdef MULTITARGET
617         asprintf(&pass0"%s%s%s"LIBEXECDIR"ccom_"mach);
618 #endif
619         pvt = pv;
620         for (i=0i<nci++) {
621                 /*
622                  * C preprocessor
623                  */
624                 if (nc>1 && !Eflag)
625                         printf("%s:\n"clist[i]);
626                 onlyas = 0;
627                 assource = tmp3;
628                 if (getsuf(clist[i])=='S')
629                         ascpp = 1;
630                 if (getsuf(clist[i])=='i') {
631                         if(Eflag)
632                                 continue;
633                         goto com;
634                 } else if (ascpp) {
635                         onlyas = 1;
636                 } else if (getsuf(clist[i])=='s') {
637                         assource = clist[i];
638                         goto assemble;
639                 }
640                 if (pflag)
641                         tmp4 = setsuf(clist[i], 'i');
642                 na = 0;
643                 av[na++] = "cpp";
644                 if (vflag)
645                         av[na++] = "-v";
646                 av[na++] = "-D__PCC__=" MKS(PCC_MAJOR);
647                 av[na++] = "-D__PCC_MINOR__=" MKS(PCC_MINOR);
648                 av[na++] = "-D__PCC_MINORMINOR__=" MKS(PCC_MINORMINOR);
649                 if (ascpp)
650                         av[na++] = "-D__ASSEMBLER__";
651                 if (sspflag)
652                         av[na++] = "-D__SSP__=1";
653                 if (pthreads)
654                         av[na++] = "-D_PTHREADS";
655                 if (Cflag)
656                         av[na++] = "-C";
657                 if (Mflag)
658                         av[na++] = "-M";
659                 if (dflag)
660                         av[na++] = alist;
661                 for (j = 0cppadd[j]; j++)
662                         av[na++] = cppadd[j];
663 #ifdef MULTITARGET
664                 for (k = 0cppmds[k].machk++) {
665                         if (strcmp(cppmds[k].machmach) != 0)
666                                 continue;
667                         for (j = 0cppmds[k].cppmdadd[j]; j++)
668                                 av[na++] = cppmds[k].cppmdadd[j];
669                         break;
670                 }
671 #else
672                 for (j = 0cppmdadd[j]; j++)
673                         av[na++] = cppmdadd[j];
674 #endif
675                 if (tflag)
676                         av[na++] = "-t";
677                 for(pv=ptemppv <pvtpv++)
678                         av[na++] = *pv;
679                 if (!nostdinc)
680                         av[na++] = "-S"av[na++] = incdir;
681                 av[na++] = "-I"av[na++] = pccincdir;
682                 if (idirafter) {
683                         av[na++] = "-I";
684                         av[na++] = idirafter;
685                 }
686                 av[na++] = clist[i];
687                 if (!Eflag && !Mflag)
688                         av[na++] = tmp4;
689                 if (Eflag && outfile)
690                          ermfile = av[na++] = outfile;
691                 av[na++]=0;
692                 if (callsys(passpav))
693                         {exfail++; eflag++;}
694                 if (Eflag || Mflag)
695                         continue;
696                 if (onlyas) {
697                         assource = tmp4;
698                         goto assemble;
699                 }
700
701                 /*
702                  * C compiler
703                  */
704         com:
705                 na = 0;
706                 av[na++]= "ccom";
707                 if (Wallflag) {
708                         /* Set only the same flags as gcc */
709                         for (Wf = WflagsWf->nameWf++) {
710                                 if (Wf->flags != INWALL)
711                                         continue;
712                                 av[na++] = Wf->name;
713                         }
714                 }
715                 if (Wflag) {
716                         /* set all positive flags */
717                         for (Wf = WflagsWf->nameWf++) {
718                                 if (Wf->flags == NEGATIVE)
719                                         continue;
720                                 av[na++] = Wf->name;
721                         }
722                 }
723                 for (j = 0j < nwj++)
724                         av[na++] = wlist[j];
725                 for (j = 0j < nfj++)
726                         av[na++] = flist[j];
727                 if (vflag)
728                         av[na++] = "-v";
729                 if (pgflag)
730                         av[na++] = "-p";
731                 if (gflag)
732                         av[na++] = "-g";
733 #ifdef os_darwin
734                 /* darwin always wants PIC compilation */
735                 if (!Bstatic)
736                         av[na++] = "-k";
737 #else
738                 if (kflag)
739                         av[na++] = "-k";
740 #endif
741                 if (Oflag) {
742                         av[na++] = "-xtemps";
743                         av[na++] = "-xdeljumps";
744                 }
745                 for (j = 0j < xnumj++)
746                         av[na++] = xlist[j];
747                 for (j = 0j < nmj++)
748                         av[na++] = mlist[j];
749                 if (getsuf(clist[i])=='i')
750                         av[na++] = clist[i];
751                 else
752                         av[na++] = tmp4/* created by cpp */
753                 if (pflag || exfail)
754                         {
755                         cflag++;
756                         continue;
757                         }
758                 if(sflag) {
759                         if (outfile)
760                                 tmp3 = outfile;
761                         else
762                                 tmp3 = setsuf(clist[i], 's');
763                 }
764                 ermfile = av[na++] = tmp3;
765 #if 0
766                 if (proflag) {
767                         av[3] = "-XP";
768                         av[4] = 0;
769                 } else
770                         av[3] = 0;
771 #endif
772                 av[na++] = NULL;
773                 if (callsys(pass0av)) {
774                         cflag++;
775                         eflag++;
776                         continue;
777                 }
778                 if (sflag)
779                         continue;
780
781                 /*
782                  * Assembler
783                  */
784         assemble:
785                 na = 0;
786                 av[na++] = as;
787                 for (j = 0j < nasj++)
788                         av[na++] = aslist[j];
789 #if defined(os_sunos) && defined(mach_sparc64)
790                 av[na++] = "-m64";
791 #endif
792 #if defined(os_darwin)
793                 if (Bstatic)
794                         av[na++] = "-static";
795 #endif
796                 if (vflag)
797                         av[na++] = "-v";
798                 if (kflag)
799                         av[na++] = "-k";
800                 av[na++] = "-o";
801                 if (outfile && cflag)
802                         ermfile = av[na++] = outfile;
803                 else
804                         ermfile = av[na++] = setsuf(clist[i], 'o');
805                 av[na++] = assource;
806                 if (dflag)
807                         av[na++] = alist;
808                 av[na++] = 0;
809                 if (callsys(asav)) {
810                         cflag++;
811                         eflag++;
812                         cunlink(tmp4);
813                         continue;
814                 }
815                 cunlink(tmp4);
816         }
817
818         if (Eflag || Mflag)
819                 dexit(eflag);
820
821         /*
822          * Linker
823          */
824 nocom:
825         if (cflag==0 && nl!=0) {
826                 j = 0;
827                 av[j++] = ld;
828 #ifndef MSLINKER
829                 if (vflag)
830                         av[j++] = "-v";
831 #endif
gmcgarry
1.131
832 #if !defined(os_sunos) && !defined(os_win32) && !defined(os_darwin)
ragge
1.128
833                 av[j++] = "-X";
834 #endif
835                 if (shared) {
836                         av[j++] = "-shared";
837 #ifdef os_win32
838                         av[j++] = "-Bdynamic";
839 #endif
840 #ifndef os_sunos
841                 } else {
842 #ifndef os_win32
843 #ifndef os_darwin
844                         av[j++] = "-d";
845 #endif
846                         av[j++] = "-e";
847                         av[j++] = STARTLABEL;
848 #endif
849 #endif
850                         if (Bstatic == 0) { /* Dynamic linkage */
851 #ifdef DYNLINKER
852                                 for (i = 0dynlinker[i]; i++)
853                                         av[j++] = dynlinker[i];
854 #endif
855                         } else {
856 #ifdef os_darwin
857                                 av[j++] = "-static";
858 #else
859                                 av[j++] = "-Bstatic";
860 #endif
861                         }
862                 }
863                 if (outfile) {
864 #ifdef MSLINKER
865                         char *s = copy("/OUT:"strlen(outfile));
866                         strcat(soutfile);
867                         av[j++] = s;
868 #else
869                         av[j++] = "-o";
870                         av[j++] = outfile;
871 #endif
872                 }
873 #ifdef STARTFILES_S
874                 if (shared) {
875                         if (!nostartfiles) {
876                                 for (i = 0startfiles_S[i]; i++)
877                                         av[j++] = Bprefix(startfiles_S[i]);
878                         }
879                 } else
880 #endif
881                 {
882                         if (!nostartfiles) {
883 #ifdef CRT0FILE_PROFILE
884                                 if (pgflag) {
885                                         av[j++] = Bprefix(crt0file_profile);
886                                 } else
887 #endif
888                                 {
889 #ifdef CRT0FILE
890                                         av[j++] = Bprefix(crt0file);
891 #endif
892                                 }
893 #ifdef STARTFILES_T
894                                 if (Bstatic) {
895                                         for (i = 0startfiles_T[i]; i++)
ragge
1.129
896                                                 av[j++] = Bprefix(startfiles_T[i]);
ragge
1.128
897                                 } else
898 #endif
899                                 {
900 #ifdef STARTFILES
901                                         for (i = 0startfiles[i]; i++)
902                                                 av[j++] = Bprefix(startfiles[i]);
903 #endif
904                                 }
905                         }
906                 }
907                 i = 0;
908                 while(i<nl) {
909                         av[j++] = llist[i++];
910                         if (j >= MAXAV)
911                                 error("Too many ld options");
912                 }
913 #ifndef MACHOABI
914                 /* darwin assembler doesn't want -g */
915                 if (gflag)
916                         av[j++] = "-g";
917 #endif
918 #if 0
919                 if (gflag)
920                         av[j++] = "-lg";
921 #endif
922                 if (pthreads)
923                         av[j++] = "-lpthread";
924                 if (!nostdlib) {
925 #ifdef MSLINKER
926                         char *s = copy("/LIBPATH:"strlen(pcclibdir));
927 #else
928                         char *s = copy("-L"strlen(pcclibdir));
929 #endif
930                         strcat(spcclibdir);
931                         av[j++] = s;
932                         if (pgflag) {
933                                 for (i = 0libclibs_profile[i]; i++)
934                                         av[j++] = Bprefix(libclibs_profile[i]);
935                         } else {
936                                 for (i = 0libclibs[i]; i++)
937                                         av[j++] = Bprefix(libclibs[i]);
938                         }
939                 }
940                 if (!nostartfiles) {
941 #ifdef STARTFILES_S
942                         if (shared) {
943                                 for (i = 0endfiles_S[i]; i++)
944                                         av[j++] = Bprefix(endfiles_S[i]);
945                         } else 
946 #endif
947                         {
948 #ifdef STARTFILES_T
949                                 if (Bstatic) {
950                                         for (i = 0endfiles_T[i]; i++)
951                                                 av[j++] = Bprefix(endfiles_T[i]);
952                                 } else
953 #endif
954                                 {
955 #ifdef STARTFILES
956                                         for (i = 0endfiles[i]; i++)
957                                                 av[j++] = Bprefix(endfiles[i]);
958 #endif
959                                 }
960                         }
961                 }
962                 av[j++] = 0;
963                 eflag |= callsys(ldav);
964                 if (nc==1 && nxo==1 && eflag==0)
965                         cunlink(setsuf(clist[0], 'o'));
966                 else if (nc > 0 && eflag == 0) {
967                         /* remove .o files XXX ugly */
968                         for (i = 0i < nci++)
969                                 cunlink(setsuf(clist[i], 'o'));
970                 }
971         }
972         dexit(eflag);
973         return 0;
974 }
975
976 /*
977  * exit and cleanup after interrupt.
978  */
979 void
980 idexit(int arg)
981 {
982         dexit(100);
983 }
984
985 /*
986  * exit and cleanup.
987  */
988 void
989 dexit(int eval)
990 {
991         if (!pflag && !Xflag) {
992                 if (sflag==0)
993                         cunlink(tmp3);
994                 cunlink(tmp4);
995         }
996         if (exfail || eflag)
997                 cunlink(ermfile);
998         if (eval == 100)
999                 _exit(eval);
1000         exit(eval);
1001 }
1002
1003 static void
1004 ccerror(char *sva_list ap)
1005 {
1006         vfprintf(Eflag ? stderr : stdoutsap);
1007         putc('\n'Eflagstderr : stdout);
1008         exfail++;
1009         cflag++;
1010         eflag++;
1011 }
1012
1013 /*
1014  * complain a bit.
1015  */
1016 void
1017 error(char *s, ...)
1018 {
1019         va_list ap;
1020
1021         va_start(aps);
1022         ccerror(sap);
1023         va_end(ap);
1024 }
1025
1026 /*
1027  * complain a bit and then exit.
1028  */
1029 void
1030 errorx(int evalchar *s, ...)
1031 {
1032         va_list ap;
1033
1034         va_start(aps);
1035         ccerror(sap);
1036         va_end(ap);
1037         dexit(eval);
1038 }
1039
1040 char *
1041 Bprefix(char *s)
1042 {
1043         char *suffix;
1044         char *str;
1045
1046         if (Bflag == NULL || s[0] != '/')
1047                 return s;
1048
1049         suffix = strrchr(s'/');
1050         if (suffix == NULL)
1051                 suffix = s;
1052
1053         str = copy(Bflagstrlen(suffix));
1054         strcat(strsuffix);
1055         return str;
1056 }
1057
1058 int
1059 getsuf(char *s)
1060 {
1061         register char *p;
1062
1063         if ((p = strrchr(s'.')) && p[1] != '\0' && p[2] == '\0')
1064                 return p[1];
1065         return(0);
1066 }
1067
1068 /*
1069  * Get basename of string s and change its suffix to ch.
1070  */
1071 char *
1072 setsuf(char *schar ch)
1073 {
1074         char *p;
1075
1076         s = copy(basename(s), 2);
1077         if ((p = strrchr(s'.')) == NULL) {
1078                 p = s + strlen(s);
1079                 p[0] = '.';
1080         }
1081         p[1] = ch;
1082         p[2] = '\0';
1083         return(s);
1084 }
1085
1086 #ifdef WIN32
1087 int
1088 callsys(char *fchar *v[])
1089 {
gmcgarry
1.131
1090         int t;
ragge
1.128
1091         char cmd[MAX_PATH];
1092         int len;
1093         STARTUPINFO si;
1094         PROCESS_INFORMATION pi;
1095         DWORD exitCode;
1096         BOOL ok;
1097
1098         len = strlcpy(cmdfMAX_PATH);
1099         for (t = 1v[t] && len < MAX_PATHt++) {
1100                 len = strlcat(cmd" "MAX_PATH);
1101                 len = strlcat(cmdv[t], MAX_PATH);
1102         }
1103
1104         if (vflag)
1105                 printf("%s\n"cmd);
1106
1107         ZeroMemory(&sisizeof(STARTUPINFO));
1108         si.cb = sizeof(STARTUPINFO);
1109         ZeroMemory(&pisizeof(PROCESS_INFORMATION));
1110
1111         ok = CreateProcess(NULL,  // the executable program
1112                 cmd,   // the command line arguments
1113                 NULL,       // ignored
1114                 NULL,       // ignored
1115                 TRUE,       // inherit handles
1116                 HIGH_PRIORITY_CLASS,
1117                 NULL,       // ignored
1118                 NULL,       // ignored
1119                 &si,
1120                 &pi);
1121
1122         if (!ok) {
1123                 fprintf(stderr"Can't find %s\n"f);
1124                 return 100;
1125         }
1126
1127         WaitForSingleObject(pi.hProcessINFINITE);
1128         GetExitCodeProcess(pi.hProcess, &exitCode);
1129         return (exitCode != 0);
1130 }
1131
1132 #else
1133
1134 int
1135