Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20090124214348

Diff

Diff from 1.142 to:

Annotations

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

Annotated File View

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