Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20090108234512

Diff

Diff from 1.140 to:

Annotations

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

Annotated File View

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