Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20090129214218

Diff

Diff from 1.143 to:

Annotations

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

Annotated File View

gmcgarry
1.143
1 /*      $Id: cc.c,v 1.143 2009/01/29 21:42:18 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";
gmcgarry
1.143
684 #ifdef os_win32
685                 av[na++] = "-D__WCHAR_TYPE__=short";
686 #else
gmcgarry
1.142
687                 av[na++] = "-D__WCHAR_TYPE__=short unsigned int";
gmcgarry
1.143
688 #endif
gmcgarry
1.142
689                 av[na++] = "-D__SIZEOF_WCHAR_T__=2";
690 #else
691                 av[na++] = "-D__STDC_ISO_10646__=200009L";      /* glibc */
692                 av[na++] = "-D__WCHAR_MAX__=4294967295U";
693                 av[na++] = "-D__WCHAR_TYPE__=unsigned int";
694                 av[na++] = "-D__SIZEOF_WCHAR_T__=4";
695 #endif
gmcgarry
1.143
696 #ifdef os_win32
697                 av[na++] = "-D__WINT_TYPE__=int";
698 #else
gmcgarry
1.142
699                 av[na++] = "-D__WINT_TYPE__=unsigned int";
gmcgarry
1.143
700 #endif
gmcgarry
1.142
701                 av[na++] = "-D__SIZEOF_WINT_T__=4";
ragge
1.128
702 #ifdef MULTITARGET
703                 for (k = 0cppmds[k].machk++) {
704                         if (strcmp(cppmds[k].machmach) != 0)
705                                 continue;
706                         for (j = 0cppmds[k].cppmdadd[j]; j++)
707                                 av[na++] = cppmds[k].cppmdadd[j];
708                         break;
709                 }
710 #else
711                 for (j = 0cppmdadd[j]; j++)
712                         av[na++] = cppmdadd[j];
713 #endif
714                 if (tflag)
715                         av[na++] = "-t";
716                 for(pv=ptemppv <pvtpv++)
717                         av[na++] = *pv;
718                 if (!nostdinc)
719                         av[na++] = "-S"av[na++] = incdir;
720                 av[na++] = "-I"av[na++] = pccincdir;
721                 if (idirafter) {
722                         av[na++] = "-I";
723                         av[na++] = idirafter;
724                 }
725                 av[na++] = clist[i];
726                 if (!Eflag && !Mflag)
727                         av[na++] = tmp4;
ragge
1.141
728                 if ((Eflag || Mflag) && outfile)
ragge
1.128
729                          ermfile = av[na++] = outfile;
730                 av[na++]=0;
gmcgarry
1.140
731                 if (callsys(passpav)) {
732                         exfail++;
733                         eflag++;
734                 }
ragge
1.128
735                 if (Eflag || Mflag)
736                         continue;
737                 if (onlyas) {
738                         assource = tmp4;
739                         goto assemble;
740                 }
741
742                 /*
743                  * C compiler
744                  */
745         com:
746                 na = 0;
747                 av[na++]= "ccom";
748                 if (Wallflag) {
749                         /* Set only the same flags as gcc */
750                         for (Wf = WflagsWf->nameWf++) {
751                                 if (Wf->flags != INWALL)
752                                         continue;
753                                 av[na++] = Wf->name;
754                         }
755                 }
756                 if (Wflag) {
757                         /* set all positive flags */
758                         for (Wf = WflagsWf->nameWf++) {
759                                 if (Wf->flags == NEGATIVE)
760                                         continue;
761                                 av[na++] = Wf->name;
762                         }
763                 }
764                 for (j = 0j < nwj++)
765                         av[na++] = wlist[j];
766                 for (j = 0j < nfj++)
767                         av[na++] = flist[j];
768                 if (vflag)
769                         av[na++] = "-v";
770                 if (pgflag)
771                         av[na++] = "-p";
772                 if (gflag)
773                         av[na++] = "-g";
774 #ifdef os_darwin
775                 /* darwin always wants PIC compilation */
776                 if (!Bstatic)
777                         av[na++] = "-k";
778 #else
779                 if (kflag)
780                         av[na++] = "-k";
781 #endif
782                 if (Oflag) {
783                         av[na++] = "-xtemps";
784                         av[na++] = "-xdeljumps";
ragge
1.133
785                         av[na++] = "-xinline";
ragge
1.128
786                 }
787                 for (j = 0j < xnumj++)
788                         av[na++] = xlist[j];
789                 for (j = 0j < nmj++)
790                         av[na++] = mlist[j];
791                 if (getsuf(clist[i])=='i')
792                         av[na++] = clist[i];
793                 else
794                         av[na++] = tmp4/* created by cpp */
795                 if (pflag || exfail)
796                         {
797                         cflag++;
798                         continue;
799                         }
800                 if(sflag) {
801                         if (outfile)
802                                 tmp3 = outfile;
803                         else
804                                 tmp3 = setsuf(clist[i], 's');
805                 }
806                 ermfile = av[na++] = tmp3;
807 #if 0
808                 if (proflag) {
809                         av[3] = "-XP";
810                         av[4] = 0;
811                 } else
812                         av[3] = 0;
813 #endif
814                 av[na++] = NULL;
815                 if (callsys(pass0av)) {
816                         cflag++;
817                         eflag++;
818                         continue;
819                 }
820                 if (sflag)
821                         continue;
822
823                 /*
824                  * Assembler
825                  */
826         assemble:
827                 na = 0;
828                 av[na++] = as;
829                 for (j = 0j < nasj++)
830                         av[na++] = aslist[j];
831 #if defined(os_sunos) && defined(mach_sparc64)
832                 av[na++] = "-m64";
833 #endif
834 #if defined(os_darwin)
835                 if (Bstatic)
836                         av[na++] = "-static";
837 #endif
838                 if (vflag)
839                         av[na++] = "-v";
840                 if (kflag)
841                         av[na++] = "-k";
842                 av[na++] = "-o";
843                 if (outfile && cflag)
844                         ermfile = av[na++] = outfile;
845                 else
846                         ermfile = av[na++] = setsuf(clist[i], 'o');
847                 av[na++] = assource;
848                 if (dflag)
849                         av[na++] = alist;
850                 av[na++] = 0;
851                 if (callsys(asav)) {
852                         cflag++;
853                         eflag++;
854                         cunlink(tmp4);
855                         continue;
856                 }
857                 cunlink(tmp4);
858         }
859
860         if (Eflag || Mflag)
861                 dexit(eflag);
862
863         /*
864          * Linker
865          */
866 nocom:
867         if (cflag==0 && nl!=0) {
868                 j = 0;
869                 av[j++] = ld;
870 #ifndef MSLINKER
871                 if (vflag)
872                         av[j++] = "-v";
873 #endif
gmcgarry
1.131
874 #if !defined(os_sunos) && !defined(os_win32) && !defined(os_darwin)
ragge
1.128
875                 av[j++] = "-X";
876 #endif
877                 if (shared) {
878                         av[j++] = "-shared";
879 #ifdef os_win32
880                         av[j++] = "-Bdynamic";
881 #endif
882 #ifndef os_sunos
883                 } else {
884 #ifndef os_win32
885 #ifndef os_darwin
886                         av[j++] = "-d";
887 #endif
888                         av[j++] = "-e";
889                         av[j++] = STARTLABEL;
890 #endif
891 #endif
892                         if (Bstatic == 0) { /* Dynamic linkage */
893 #ifdef DYNLINKER
894                                 for (i = 0dynlinker[i]; i++)
895                                         av[j++] = dynlinker[i];
896 #endif
897                         } else {
898 #ifdef os_darwin
899                                 av[j++] = "-static";
900 #else
901                                 av[j++] = "-Bstatic";
902 #endif
903                         }
904                 }
905                 if (outfile) {
906 #ifdef MSLINKER
gmcgarry
1.135
907 #define OUTSTR  "/OUT:"
908                         char *s = copy(OUTSTRi = strlen(outfile));
909                         strlcat(soutfilesizeof(OUTSTR) + i);
ragge
1.128
910                         av[j++] = s;
911 #else
912                         av[j++] = "-o";
913                         av[j++] = outfile;
914 #endif
915                 }
916 #ifdef STARTFILES_S
917                 if (shared) {
918                         if (!nostartfiles) {
919                                 for (i = 0startfiles_S[i]; i++)
920                                         av[j++] = Bprefix(startfiles_S[i]);
921                         }
922                 } else
923 #endif
924                 {
925                         if (!nostartfiles) {
926 #ifdef CRT0FILE_PROFILE
927                                 if (pgflag) {
928                                         av[j++] = Bprefix(crt0file_profile);
929                                 } else
930 #endif
931                                 {
932 #ifdef CRT0FILE
933                                         av[j++] = Bprefix(crt0file);
934 #endif
935                                 }
936 #ifdef STARTFILES_T
937                                 if (Bstatic) {
938                                         for (i = 0startfiles_T[i]; i++)
ragge
1.129
939                                                 av[j++] = Bprefix(startfiles_T[i]);
ragge
1.128
940                                 } else
941 #endif
942                                 {
943 #ifdef STARTFILES
944                                         for (i = 0startfiles[i]; i++)
945                                                 av[j++] = Bprefix(startfiles[i]);
946 #endif
947                                 }
948                         }
949                 }
950                 i = 0;
951                 while(i<nl) {
952                         av[j++] = llist[i++];
953                         if (j >= MAXAV)
954                                 error("Too many ld options");
955                 }
956 #ifndef MACHOABI
957                 /* darwin assembler doesn't want -g */
958                 if (gflag)
959                         av[j++] = "-g";
960 #endif
961 #if 0
962                 if (gflag)
963                         av[j++] = "-lg";
964 #endif
965                 if (pthreads)
966                         av[j++] = "-lpthread";
967                 if (!nostdlib) {
968 #ifdef MSLINKER
ragge
1.133
969 #define DL      "/LIBPATH:"
ragge
1.128
970 #else
ragge
1.133
971 #define DL      "-L"
ragge
1.128
972 #endif
ragge
1.133
973                         char *s = copy(DLi = strlen(pcclibdir));
gmcgarry
1.136
974                         strlcat(spcclibdirsizeof(DL) + i);
ragge
1.128
975                         av[j++] = s;
gmcgarry
1.136
976 #ifdef os_win32
977                         s = copy(DLi = strlen(libdir));
978                         strlcat(slibdirsizeof(DL) + i);
979                         av[j++] = s;
980 #endif
ragge
1.128
981                         if (pgflag) {
982                                 for (i = 0libclibs_profile[i]; i++)
983                                         av[j++] = Bprefix(libclibs_profile[i]);
984                         } else {
985                                 for (i = 0libclibs[i]; i++)
986                                         av[j++] = Bprefix(libclibs[i]);
987                         }
988                 }
989                 if (!nostartfiles) {
990 #ifdef STARTFILES_S
991                         if (shared) {
992                                 for (i = 0endfiles_S[i]; i++)
993                                         av[j++] = Bprefix(endfiles_S[i]);
994                         } else 
995 #endif
996                         {
997 #ifdef STARTFILES_T
998                                 if (Bstatic) {
999                                         for (i = 0endfiles_T[i]; i++)
1000                                                 av[j++] = Bprefix(endfiles_T[i]);
1001                                 } else
1002 #endif
1003                                 {
1004 #ifdef STARTFILES
1005                                         for (i = 0endfiles[i]; i++)
1006                                                 av[j++] = Bprefix(endfiles[i]);
1007 #endif
1008                                 }
1009                         }
1010                 }
1011                 av[j++] = 0;
1012                 eflag |= callsys(ldav);
1013                 if (nc==1 && nxo==1 && eflag==0)
1014                         cunlink(setsuf(clist[0], 'o'));
1015                 else if (nc > 0 && eflag == 0) {
1016                         /* remove .o files XXX ugly */
1017                         for (i = 0i < nci++)
1018                                 cunlink(setsuf(clist[i], 'o'));
1019                 }
1020         }
1021         dexit(eflag);
1022         return 0;
1023 }
1024
1025 /*
1026  * exit and cleanup after interrupt.
1027  */
1028 void
1029 idexit(int arg)
1030 {
1031         dexit(100);
1032 }
1033
1034 /*
1035  * exit and cleanup.
1036  */
1037 void
1038 dexit(int eval)
1039 {
1040         if (!pflag && !Xflag) {
1041                 if (sflag==0)
1042                         cunlink(tmp3);
1043                 cunlink(tmp4);
1044         }
1045         if (exfail || eflag)
1046                 cunlink(ermfile);
1047         if (eval == 100)
1048                 _exit(eval);
1049         exit(eval);
1050 }
1051
1052 static void
1053 ccerror(char *sva_list ap)
1054 {
1055         vfprintf(Eflag ? stderr : stdoutsap);
1056         putc('\n'Eflagstderr : stdout);
1057         exfail++;
1058         cflag++;
1059         eflag++;
1060 }
1061
1062 /*
1063  * complain a bit.
1064  */
1065 void
1066 error(char *s, ...)
1067 {
1068         va_list ap;
1069
1070         va_start(aps);
1071         ccerror(sap);
1072         va_end(ap);
1073 }
1074
1075 /*
1076  * complain a bit and then exit.
1077  */
1078 void
1079 errorx(int evalchar *s, ...)
1080 {
1081         va_list ap;
1082
1083         va_start(aps);
1084         ccerror(sap);
1085         va_end(ap);
1086         dexit(eval);
1087 }
1088
1089 char *
1090 Bprefix(char *s)
1091 {
1092         char *suffix;
1093         char *str;
ragge
1.133
1094         int i;
ragge
1.128
1095
gmcgarry
1.139
1096 #ifdef WIN32
gmcgarry
1.136
1097
1098         /*  put here to save sprinkling it ~everywhere  */
1099         s =  win32pathsubst(s);
1100
1101         if (Bflag == NULL)
1102                 return s;
1103         suffix = strrchr(s'/');
1104         if (suffix == NULL)
1105                 suffix = strrchr(s'\\');
1106
1107 #else
1108
ragge
1.128
1109         if (Bflag == NULL || s[0] != '/')
1110                 return s;
gmcgarry
1.136
1111         suffix = strrchr(s'/');
1112
1113 #endif
ragge
1.128
1114
1115         if (suffix == NULL)
1116