Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20081204212857

Diff

Diff from 1.133 to:

Annotations

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

Annotated File View

ragge
1.133
1 /*      $Id: cc.c,v 1.133 2008/12/04 21:28:57 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
ragge
1.133
617         pass0 = copy(LIBEXECDIR "/ccom_"k = strlen(mach));
618         strlcat(pass0machsizeof(LIBEXECDIR "/ccom_") + k);
ragge
1.128
619 #endif
620         pvt = pv;
621         for (i=0i<nci++) {
622                 /*
623                  * C preprocessor
624                  */
625                 if (nc>1 && !Eflag)
626                         printf("%s:\n"clist[i]);
627                 onlyas = 0;
628                 assource = tmp3;
629                 if (getsuf(clist[i])=='S')
630                         ascpp = 1;
631                 if (getsuf(clist[i])=='i') {
632                         if(Eflag)
633                                 continue;
634                         goto com;
635                 } else if (ascpp) {
636                         onlyas = 1;
637                 } else if (getsuf(clist[i])=='s') {
638                         assource = clist[i];
639                         goto assemble;
640                 }
641                 if (pflag)
642                         tmp4 = setsuf(clist[i], 'i');
643                 na = 0;
644                 av[na++] = "cpp";
645                 if (vflag)
646                         av[na++] = "-v";
647                 av[na++] = "-D__PCC__=" MKS(PCC_MAJOR);
648                 av[na++] = "-D__PCC_MINOR__=" MKS(PCC_MINOR);
649                 av[na++] = "-D__PCC_MINORMINOR__=" MKS(PCC_MINORMINOR);
650                 if (ascpp)
651                         av[na++] = "-D__ASSEMBLER__";
652                 if (sspflag)
653                         av[na++] = "-D__SSP__=1";
654                 if (pthreads)
655                         av[na++] = "-D_PTHREADS";
656                 if (Cflag)
657                         av[na++] = "-C";
658                 if (Mflag)
659                         av[na++] = "-M";
660                 if (dflag)
661                         av[na++] = alist;
662                 for (j = 0cppadd[j]; j++)
663                         av[na++] = cppadd[j];
664 #ifdef MULTITARGET
665                 for (k = 0cppmds[k].machk++) {
666                         if (strcmp(cppmds[k].machmach) != 0)
667                                 continue;
668                         for (j = 0cppmds[k].cppmdadd[j]; j++)
669                                 av[na++] = cppmds[k].cppmdadd[j];
670                         break;
671                 }
672 #else
673                 for (j = 0cppmdadd[j]; j++)
674                         av[na++] = cppmdadd[j];
675 #endif
676                 if (tflag)
677                         av[na++] = "-t";
678                 for(pv=ptemppv <pvtpv++)
679                         av[na++] = *pv;
680                 if (!nostdinc)
681                         av[na++] = "-S"av[na++] = incdir;
682                 av[na++] = "-I"av[na++] = pccincdir;
683                 if (idirafter) {
684                         av[na++] = "-I";
685                         av[na++] = idirafter;
686                 }
687                 av[na++] = clist[i];
688                 if (!Eflag && !Mflag)
689                         av[na++] = tmp4;
690                 if (Eflag && outfile)
691                          ermfile = av[na++] = outfile;
692                 av[na++]=0;
693                 if (callsys(passpav))
694                         {exfail++; eflag++;}
695                 if (Eflag || Mflag)
696                         continue;
697                 if (onlyas) {
698                         assource = tmp4;
699                         goto assemble;
700                 }
701
702                 /*
703                  * C compiler
704                  */
705         com:
706                 na = 0;
707                 av[na++]= "ccom";
708                 if (Wallflag) {
709                         /* Set only the same flags as gcc */
710                         for (Wf = WflagsWf->nameWf++) {
711                                 if (Wf->flags != INWALL)
712                                         continue;
713                                 av[na++] = Wf->name;
714                         }
715                 }
716                 if (Wflag) {
717                         /* set all positive flags */
718                         for (Wf = WflagsWf->nameWf++) {
719                                 if (Wf->flags == NEGATIVE)
720                                         continue;
721                                 av[na++] = Wf->name;
722                         }
723                 }
724                 for (j = 0j < nwj++)
725                         av[na++] = wlist[j];
726                 for (j = 0j < nfj++)
727                         av[na++] = flist[j];
728                 if (vflag)
729                         av[na++] = "-v";
730                 if (pgflag)
731                         av[na++] = "-p";
732                 if (gflag)
733                         av[na++] = "-g";
734 #ifdef os_darwin
735                 /* darwin always wants PIC compilation */
736                 if (!Bstatic)
737                         av[na++] = "-k";
738 #else
739                 if (kflag)
740                         av[na++] = "-k";
741 #endif
742                 if (Oflag) {
743                         av[na++] = "-xtemps";
744                         av[na++] = "-xdeljumps";
ragge
1.133
745                         av[na++] = "-xinline";
ragge
1.128
746                 }
747                 for (j = 0j < xnumj++)
748                         av[na++] = xlist[j];
749                 for (j = 0j < nmj++)
750                         av[na++] = mlist[j];
751                 if (getsuf(clist[i])=='i')
752                         av[na++] = clist[i];
753                 else
754                         av[na++] = tmp4/* created by cpp */
755                 if (pflag || exfail)
756                         {
757                         cflag++;
758                         continue;
759                         }
760                 if(sflag) {
761                         if (outfile)
762                                 tmp3 = outfile;
763                         else
764                                 tmp3 = setsuf(clist[i], 's');
765                 }
766                 ermfile = av[na++] = tmp3;
767 #if 0
768                 if (proflag) {
769                         av[3] = "-XP";
770                         av[4] = 0;
771                 } else
772                         av[3] = 0;
773 #endif
774                 av[na++] = NULL;
775                 if (callsys(pass0av)) {
776                         cflag++;
777                         eflag++;
778                         continue;
779                 }
780                 if (sflag)
781                         continue;
782
783                 /*
784                  * Assembler
785                  */
786         assemble:
787                 na = 0;
788                 av[na++] = as;
789                 for (j = 0j < nasj++)
790                         av[na++] = aslist[j];
791 #if defined(os_sunos) && defined(mach_sparc64)
792                 av[na++] = "-m64";
793 #endif
794 #if defined(os_darwin)
795                 if (Bstatic)
796                         av[na++] = "-static";
797 #endif
798                 if (vflag)
799                         av[na++] = "-v";
800                 if (kflag)
801                         av[na++] = "-k";
802                 av[na++] = "-o";
803                 if (outfile && cflag)
804                         ermfile = av[na++] = outfile;
805                 else
806                         ermfile = av[na++] = setsuf(clist[i], 'o');
807                 av[na++] = assource;
808                 if (dflag)
809                         av[na++] = alist;
810                 av[na++] = 0;
811                 if (callsys(asav)) {
812                         cflag++;
813                         eflag++;
814                         cunlink(tmp4);
815                         continue;
816                 }
817                 cunlink(tmp4);
818         }
819
820         if (Eflag || Mflag)
821                 dexit(eflag);
822
823         /*
824          * Linker
825          */
826 nocom:
827         if (cflag==0 && nl!=0) {
828                 j = 0;
829                 av[j++] = ld;
830 #ifndef MSLINKER
831                 if (vflag)
832                         av[j++] = "-v";
833 #endif
gmcgarry
1.131
834 #if !defined(os_sunos) && !defined(os_win32) && !defined(os_darwin)
ragge
1.128
835                 av[j++] = "-X";
836 #endif
837                 if (shared) {
838                         av[j++] = "-shared";
839 #ifdef os_win32
840                         av[j++] = "-Bdynamic";
841 #endif
842 #ifndef os_sunos
843                 } else {
844 #ifndef os_win32
845 #ifndef os_darwin
846                         av[j++] = "-d";
847 #endif
848                         av[j++] = "-e";
849                         av[j++] = STARTLABEL;
850 #endif
851 #endif
852                         if (Bstatic == 0) { /* Dynamic linkage */
853 #ifdef DYNLINKER
854                                 for (i = 0dynlinker[i]; i++)
855                                         av[j++] = dynlinker[i];
856 #endif
857                         } else {
858 #ifdef os_darwin
859                                 av[j++] = "-static";
860 #else
861                                 av[j++] = "-Bstatic";
862 #endif
863                         }
864                 }
865                 if (outfile) {
866 #ifdef MSLINKER
ragge
1.133
867 #define OUT     "/OUT:"
868                         char *s = copy(OUTi = strlen(outfile));
869                         strlcat(soutfilesizeof(OUT) + i);
ragge
1.128
870                         av[j++] = s;
871 #else
872                         av[j++] = "-o";
873                         av[j++] = outfile;
874 #endif
875                 }
876 #ifdef STARTFILES_S
877                 if (shared) {
878                         if (!nostartfiles) {
879                                 for (i = 0startfiles_S[i]; i++)
880                                         av[j++] = Bprefix(startfiles_S[i]);
881                         }
882                 } else
883 #endif
884                 {
885                         if (!nostartfiles) {
886 #ifdef CRT0FILE_PROFILE
887                                 if (pgflag) {
888                                         av[j++] = Bprefix(crt0file_profile);
889                                 } else
890 #endif
891                                 {
892 #ifdef CRT0FILE
893                                         av[j++] = Bprefix(crt0file);
894 #endif
895                                 }
896 #ifdef STARTFILES_T
897                                 if (Bstatic) {
898                                         for (i = 0startfiles_T[i]; i++)
ragge
1.129
899                                                 av[j++] = Bprefix(startfiles_T[i]);
ragge
1.128
900                                 } else
901 #endif
902                                 {
903 #ifdef STARTFILES
904                                         for (i = 0startfiles[i]; i++)
905                                                 av[j++] = Bprefix(startfiles[i]);
906 #endif
907                                 }
908                         }
909                 }
910                 i = 0;
911                 while(i<nl) {
912                         av[j++] = llist[i++];
913                         if (j >= MAXAV)
914                                 error("Too many ld options");
915                 }
916 #ifndef MACHOABI
917                 /* darwin assembler doesn't want -g */
918                 if (gflag)
919                         av[j++] = "-g";
920 #endif
921 #if 0
922                 if (gflag)
923                         av[j++] = "-lg";
924 #endif
925                 if (pthreads)
926                         av[j++] = "-lpthread";
927                 if (!nostdlib) {
928 #ifdef MSLINKER
ragge
1.133
929 #define DL      "/LIBPATH:"
ragge
1.128
930 #else
ragge
1.133
931 #define DL      "-L"
ragge
1.128
932 #endif
ragge
1.133
933                         char *s = copy(DLi = strlen(pcclibdir));
934                         strlcat(spcclibdirsizeof(DL) +i );
ragge
1.128
935                         av[j++] = s;
936                         if (pgflag) {
937                                 for (i = 0libclibs_profile[i]; i++)
938                                         av[j++] = Bprefix(libclibs_profile[i]);
939                         } else {
940                                 for (i = 0libclibs[i]; i++)
941                                         av[j++] = Bprefix(libclibs[i]);
942                         }
943                 }
944                 if (!nostartfiles) {
945 #ifdef STARTFILES_S
946                         if (shared) {
947                                 for (i = 0endfiles_S[i]; i++)
948                                         av[j++] = Bprefix(endfiles_S[i]);
949                         } else 
950 #endif
951                         {
952 #ifdef STARTFILES_T
953                                 if (Bstatic) {
954                                         for (i = 0endfiles_T[i]; i++)
955                                                 av[j++] = Bprefix(endfiles_T[i]);
956                                 } else
957 #endif
958                                 {
959 #ifdef STARTFILES
960                                         for (i = 0endfiles[i]; i++)
961                                                 av[j++] = Bprefix(endfiles[i]);
962 #endif
963                                 }
964                         }
965                 }
966                 av[j++] = 0;
967                 eflag |= callsys(ldav);
968                 if (nc==1 && nxo==1 && eflag==0)
969                         cunlink(setsuf(clist[0], 'o'));
970                 else if (nc > 0 && eflag == 0) {
971                         /* remove .o files XXX ugly */
972                         for (i = 0i < nci++)
973                                 cunlink(setsuf(clist[i], 'o'));
974                 }
975         }
976         dexit(eflag);
977         return 0;
978 }
979
980 /*
981  * exit and cleanup after interrupt.
982  */
983 void
984 idexit(int arg)
985 {
986         dexit(100);
987 }
988
989 /*
990  * exit and cleanup.
991  */
992 void
993 dexit(int eval)
994 {
995         if (!pflag && !Xflag) {
996                 if (sflag==0)
997                         cunlink(tmp3);
998                 cunlink(tmp4);
999         }
1000         if (exfail || eflag)
1001                 cunlink(ermfile);
1002         if (eval == 100)
1003                 _exit(eval);
1004         exit(eval);
1005 }
1006
1007 static void
1008 ccerror(char *sva_list ap)
1009 {
1010         vfprintf(Eflag ? stderr : stdoutsap);
1011         putc('\n'Eflagstderr : stdout);
1012         exfail++;
1013         cflag++;
1014         eflag++;
1015 }
1016
1017 /*
1018  * complain a bit.
1019  */
1020 void
1021 error(char *s, ...)
1022 {
1023         va_list ap;
1024
1025         va_start(aps);
1026         ccerror(sap);
1027         va_end(ap);
1028 }
1029
1030 /*
1031  * complain a bit and then exit.
1032  */
1033 void
1034 errorx(int evalchar *s, ...)
1035 {
1036         va_list ap;
1037
1038         va_start(aps);
1039         ccerror(sap);
1040         va_end(ap);
1041         dexit(eval);
1042 }
1043
1044 char *
1045 Bprefix(char *s)
1046 {
1047         char *suffix;
1048         char *str;
ragge
1.133
1049         int i;
ragge
1.128
1050
1051         if (Bflag == NULL || s[0] != '/')
1052                 return s;
1053
1054         suffix = strrchr(s'/');
1055         if (suffix == NULL)
1056                 suffix = s;
1057
ragge
1.133
1058         str = copy(Bflagi = strlen(suffix));
1059         strlcat(strsuffixstrlen(Bflag) + i + 1);
ragge
1.128
1060         return str;
1061 }
1062
1063 int
1064 getsuf(char *s)
1065 {
1066         register char *p;
1067
1068         if ((p = strrchr(s'.')) && p[1] != '\0' && p[2] == '\0')
1069                 return p[1];
1070         return(0);
1071 }
1072
1073 /*
1074  * Get basename of string s and change its suffix to ch.
1075  */
1076 char *
1077 setsuf(char *schar ch)
1078 {
1079         char *p;
1080
1081         s = copy(basename(s), 2);
1082         if ((p = strrchr(s'.')) == NULL) {
1083                 p = s + strlen(s);
1084                 p[0] = '.';
1085         }
1086         p[1] = ch;
1087         p[2] = '\0';
1088         return(s);
1089 }
1090
1091 #ifdef WIN32
1092 int
1093 callsys(char *fchar *v[])
1094 {
gmcgarry
1.131
1095         int t;
ragge
1.128
1096         char cmd[MAX_PATH];
1097         int len;
1098         STARTUPINFO si;
1099         PROCESS_INFORMATION pi;
1100         DWORD exitCode;
1101         BOOL ok;
1102
1103         len = strlcpy(cmdfMAX_PATH);
1104         for (t = 1v[t] && len < MAX_PATHt++) {
1105                 len = strlcat(cmd" "MAX_PATH);
1106                 len = strlcat(cmdv[t], MAX_PATH);
1107         }
1108
1109         if (vflag)
1110                 printf("%s\n"cmd);
1111
1112         ZeroMemory(&sisizeof(STARTUPINFO));
1113         si.cb = sizeof(STARTUPINFO);
1114         ZeroMemory(&pisizeof(PROCESS_INFORMATION));
1115
1116         ok = CreateProcess(NULL,  // the executable program
1117                 cmd,   // the command line arguments
1118                 NULL,       // ignored
1119                 NULL,       // ignored
1120                 TRUE,       // inherit handles
1121                 HIGH_PRIORITY_CLASS,
1122                 NULL,       // ignored
1123                 NULL,       // ignored
1124