Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20090107114129

Diff

Diff from 1.138 to:

Annotations

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

Annotated File View

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