Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20101229175820

Diff

Diff from 1.164 to:

Annotations

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

Annotated File View

ragge
1.164
1 /*      $Id: cc.c,v 1.164 2010/12/29 17:58:20 ragge Exp $       */
ragge
1.128
2 /*
3  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code and documentation must retain the above
10  * copyright notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditionsand the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * All advertising materials mentioning features or use of this software
15  * must display the following acknowledgement:
16  *      This product includes software developed or owned by Caldera
17  *      International, Inc.
18  * Neither the name of Caldera International, Inc. nor the names of other
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27  * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 /*
37  * Front-end to the C compiler.
38  *
39  * Brief description of its syntax:
40  * - Files that end with .c are passed via cpp->ccom->as->ld
41  * - Files that end with .i are passed via ccom->as->ld
42  * - Files that end with .s are passed as->ld
43  * - Files that end with .o are passed directly to ld
44  * - Multiple files may be given on the command line.
45  * - Unrecognized options are all sent directly to ld.
46  * -c or -S cannot be combined with -o if multiple files are given.
47  *
48  * This file should be rewritten readable.
49  */
50 #include "config.h"
51
52 #include <sys/types.h>
53 #ifdef HAVE_SYS_WAIT_H
54 #include <sys/wait.h>
55 #endif
56
57 #include <ctype.h>
58 #include <errno.h>
59 #include <fcntl.h>
60 #ifdef HAVE_LIBGEN_H
61 #include <libgen.h>
62 #endif
63 #include <signal.h>
64 #include <stdarg.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #ifdef HAVE_UNISTD_H
69 #include <unistd.h>
70 #endif
71
72 #ifdef WIN32
73 #include <windows.h>
74 #include <process.h>
75 #include <io.h>
76 #endif
77
78 #include "compat.h"
79
80 #include "ccconfig.h"
81 /* C command */
82
83 #define MKS(x) _MKS(x)
84 #define _MKS(x) #x
85
86 /*
87  * Many specific definitions, should be declared elsewhere.
88  */
89
90 #ifndef STDINC
91 #define STDINC          "/usr/include/"
92 #endif
93
94 #ifndef LIBDIR
95 #define LIBDIR          "/usr/lib/"
96 #endif
97
98 #ifndef PREPROCESSOR
99 #define PREPROCESSOR    "cpp"
100 #endif
101
102 #ifndef COMPILER
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 *);
gmcgarry
1.150
131 char *copy(char *, int);
132 char *setsuf(char *, char);
ragge
1.128
133 int getsuf(char *);
134 int main(intchar *[]);
135 void error(char *, ...);
136 void errorx(intchar *, ...);
137 int callsys(char [], char *[]);
138 int cunlink(char *);
139 void dexit(int);
140 void idexit(int);
141 char *gettmp(void);
142 void *ccmalloc(int size);
143 #ifdef WIN32
144 char *win32pathsubst(char *);
gmcgarry
1.149
145 char *win32commandline(char *, char *[]);
ragge
1.128
146 #endif
147 char    *av[MAXAV];
148 char    *clist[MAXFIL];
gmcgarry
1.150
149 char    *olist[MAXFIL];
ragge
1.128
150 char    *llist[MAXLIB];
151 char    *aslist[MAXAV];
gmcgarry
1.163
152 char    *cpplist[MAXAV];
ragge
1.128
153 char    alist[20];
154 char    *xlist[100];
155 int     xnum;
156 char    *mlist[100];
157 char    *flist[100];
158 char    *wlist[100];
159 char    *idirafter;
160 int     nm;
161 int     nf;
162 int     nw;
163 int     sspflag;
164 int     dflag;
165 int     pflag;
166 int     sflag;
167 int     cflag;
168 int     eflag;
169 int     gflag;
gmcgarry
1.145
170 int     rflag;
ragge
1.128
171 int     vflag;
172 int     tflag;
173 int     Eflag;
174 int     Oflag;
175 int     kflag;  /* generate PIC/pic code */
176 #define F_PIC   1
177 #define F_pic   2
178 int     Mflag;  /* dependencies only */
179 int     pgflag;
180 int     exfail;
181 int     Xflag;
182 int     Wallflag;
183 int     Wflag;
184 int     nostartfilesBstaticshared;
185 int     nostdincnostdlib;
186 int     onlyas;
187 int     pthreads;
188 int     xcflag;
189 int     ascpp;
190
191 char    *passp = LIBEXECDIR PREPROCESSOR;
192 char    *pass0 = LIBEXECDIR COMPILER;
193 char    *as = ASSEMBLER;
194 char    *ld = LINKER;
195 char    *Bflag;
196 char *cppadd[] = CPPADD;
197 #ifdef DYNLINKER
198 char *dynlinker[] = DYNLINKER;
199 #endif
200 #ifdef CRT0FILE
201 char *crt0file = CRT0FILE;
202 #endif
203 #ifdef CRT0FILE_PROFILE
204 char *crt0file_profile = CRT0FILE_PROFILE;
205 #endif
206 #ifdef STARTFILES
207 char *startfiles[] = STARTFILES;
208 char *endfiles[] = ENDFILES;
209 #endif
210 #ifdef STARTFILES_T
211 char *startfiles_T[] = STARTFILES_T;
212 char *endfiles_T[] = ENDFILES_T;
213 #endif
214 #ifdef STARTFILES_S
215 char *startfiles_S[] = STARTFILES_S;
216 char *endfiles_S[] = ENDFILES_S;
217 #endif
218 #ifdef MULTITARGET
219 char *mach = DEFMACH;
220 struct cppmd {
221         char *mach;
222         char *cppmdadd[MAXCPPMDARGS];
223 };
224
225 struct cppmd cppmds[] = CPPMDADDS;
226 #else
227 char *cppmdadd[] = CPPMDADD;
228 #endif
229 #ifdef LIBCLIBS
230 char *libclibs[] = LIBCLIBS;
231 #else
232 char *libclibs[] = { "-lc"NULL };
233 #endif
234 #ifdef LIBCLIBS_PROFILE
235 char *libclibs_profile[] = LIBCLIBS_PROFILE;
236 #else
237 char *libclibs_profile[] = { "-lc_p"NULL };
238 #endif
239 #ifndef STARTLABEL
240 #define STARTLABEL "__start"
241 #endif
242 char *incdir = STDINC;
gmcgarry
1.153
243 char *altincdir = INCLUDEDIR "pcc/";
gmcgarry
1.136
244 char *libdir = LIBDIR;
ragge
1.128
245 char *pccincdir = PCCINCDIR;
246 char *pcclibdir = PCCLIBDIR;
ragge
1.164
247 #ifdef mach_amd64
248 int amd64_i386;
249 #endif
ragge
1.128
250
251 /* handle gcc warning emulations */
252 struct Wflags {
253         char *name;
254         int flags;
255 #define INWALL          1
256 #define NEGATIVE        2
257 Wflags[] = {
gmcgarry
1.154
258         { "-Wtruncate"0 },
259         { "-Wno-truncate"NEGATIVE },
ragge
1.128
260         { "-Werror"0 },
261         { "-Wshadow"0 },
262         { "-Wno-shadow"NEGATIVE },
263         { "-Wpointer-sign"INWALL },
264         { "-Wno-pointer-sign"NEGATIVE },
265         { "-Wsign-compare"0 },
266         { "-Wno-sign-compare"NEGATIVE },
267         { "-Wunknown-pragmas"INWALL },
268         { "-Wno-unknown-pragmas"NEGATIVE },
269         { "-Wunreachable-code"0 },
270         { "-Wno-unreachable-code"NEGATIVE },
271         { 00 },
272 };
273
274 #define SZWFL   (sizeof(Wflags)/sizeof(Wflags[0]))
275
ragge
1.158
276 #ifndef USHORT
277 /* copied from mip/manifest.h */
278 #define USHORT          5
279 #define INT             6
280 #define UNSIGNED        7
281 #endif
282
ragge
1.157
283 /*
284  * Wide char defines.
285  */
286 #if WCHAR_TYPE == USHORT
287 #define WCT "short unsigned int"
288 #define WCM "65535U"
ragge
1.158
289 #if WCHAR_SIZE != 2
290 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
291 #endif
ragge
1.157
292 #elif WCHAR_TYPE == INT
293 #define WCT "int"
294 #define WCM "2147483647"
ragge
1.158
295 #if WCHAR_SIZE != 4
296 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
297 #endif
ragge
1.157
298 #elif WCHAR_TYPE == UNSIGNED
299 #define WCT "unsigned int"
ragge
1.158
300 #define WCM "4294967295U"
301 #if WCHAR_SIZE != 4
302 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
303 #endif
304 #else
305 #error WCHAR_TYPE not defined or invalid
ragge
1.157
306 #endif
307
ragge
1.160
308 #ifdef GCC_COMPAT
309 #ifndef REGISTER_PREFIX
310 #define REGISTER_PREFIX ""
311 #endif
312 #ifndef USER_LABEL_PREFIX
313 #define USER_LABEL_PREFIX ""
314 #endif
315 #endif
316
ragge
1.161
317 #ifndef PCC_PTRDIFF_TYPE
318 #define PCC_PTRDIFF_TYPE "long int"
319 #endif
320
ragge
1.128
321 int
322 main(int argcchar *argv[])
323 {
324         struct Wflags *Wf;
325         char *t, *u;
326         char *assource;
327         char **pv, *ptemp[MAXOPT], **pvt;
gmcgarry
1.163
328         int ncnlnasncppijcnxona;
ragge
1.128
329 #ifdef MULTITARGET
330         int k;
331 #endif
332
333 #ifdef WIN32
334         /* have to prefix path early.  -B may override */
335         incdir = win32pathsubst(incdir);
gmcgarry
1.153
336         altincdir = win32pathsubst(altincdir);
gmcgarry
1.136
337         libdir = win32pathsubst(libdir);
ragge
1.128
338         pccincdir = win32pathsubst(pccincdir);
339         pcclibdir = win32pathsubst(pcclibdir);
340         passp = win32pathsubst(passp);
341         pass0 = win32pathsubst(pass0);
342 #endif
343
gmcgarry
1.163
344         i = nc = nl = nas = ncpp = nxo = 0;
ragge
1.128
345         pv = ptemp;
346         while(++i < argc) {
347                 if (argv[i][0] == '-') {
348                         switch (argv[i][1]) {
349                         default:
350                                 goto passa;
351 #ifdef notyet
352         /* must add library options first (-L/-l/...) */
353                                 error("unrecognized option `-%c'"argv[i][1]);
354                                 break;
355 #endif
356
357                         case '-'/* double -'s */
ragge
1.130
358                                 if (strcmp(argv[i], "--version") == 0) {
ragge
1.128
359                                         printf("%s\n"VERSSTR);
ragge
1.130
360                                         return 0;
361                                 } else if (strcmp(argv[i], "--param") == 0)
ragge
1.128
362                                         /* NOTHING YET */;
363                                 else
gmcgarry
1.148
364                                         goto passa;
ragge
1.128
365                                 break;
366
367                         case 'B'/* other search paths for binaries */
368                                 Bflag = &argv[i][2];
369                                 break;
370
371 #ifdef MULTITARGET
372                         case 'b':
373                                 t = &argv[i][2];
374                                 if (*t == '\0' && i + 1 < argc) {
375                                         t = argv[i+1];
376                                         i++;
377                                 }
378                                 if (strncmp(t"?"1) == 0) {
379                                         /* show machine targets */
380                                         printf("Available machine targets:");
381                                         for (j=0cppmds[j].machj++)
382                                                 printf(" %s",cppmds[j].mach);
383                                         printf("\n");
384                                         exit(0);
385                                 }
386                                 for (j=0cppmds[j].machj++)
387                                         if (strcmp(tcppmds[j].mach) == 0) {
388                                                 mach = cppmds[j].mach;
389                                                 break;
390                                         }
391                                 if (cppmds[j].mach == NULL)
392                                         errorx(1"unknown target arch %s"t);
393                                 break;
394 #endif
395
396                         case 'X':
397                                 Xflag++;
398                                 break;
399                         case 'W'/* Ignore (most of) W-flags */
400                                 if (strncmp(argv[i], "-Wl,"4) == 0) {
401                                         /* options to the linker */
402                                         t = &argv[i][4];
403                                         while ((u = strchr(t','))) {
404                                                 *u++ = 0;
405                                                 llist[nl++] = t;
406                                                 t = u;
407                                         }
408                                         llist[nl++] = t;
409                                 } else if (strncmp(argv[i], "-Wa,"4) == 0) {
410                                         /* options to the assembler */
411                                         t = &argv[i][4];
412                                         while ((u = strchr(t','))) {
413                                                 *u++ = 0;
414                                                 aslist[nas++] = t;
415                                                 t = u;
416                                         }
417                                         aslist[nas++] = t;
ragge
1.137
418                                 } else if (strncmp(argv[i], "-Wc,"4) == 0) {
419                                         /* options to ccom */
420                                         t = &argv[i][4];
421                                         while ((u = strchr(t','))) {
422                                                 *u++ = 0;
423                                                 wlist[nw++] = t;
424                                                 t = u;
425                                         }
426                                         wlist[nw++] = t;
ragge
1.128
427                                 } else if (strncmp(argv[i], "-Wp,"4) == 0) {
428                                         /* preprocessor */
gmcgarry
1.163
429                                         t = &argv[i][4];
430                                         while ((u = strchr(t','))) {
431                                                 *u++ = 0;
432                                                 cpplist[ncpp++] = t;
433                                                 t = u;
434                                         }
435                                         cpplist[ncpp++] = t;
ragge
1.128
436                                 } else if (strcmp(argv[i], "-Wall") == 0) {
437                                         Wallflag = 1;
438                                 } else if (strcmp(argv[i], "-WW") == 0) {
439                                         Wflag = 1;
440                                 } else {
441                                         /* check and set if available */
442                                         for (Wf = WflagsWf->nameWf++) {
443                                                 if (strcmp(argv[i], Wf->name))
444                                                         continue;
445                                                 wlist[nw++] = Wf->name;
446                                         }
447                                 }
448                                 break;
449
450                         case 'f'/* GCC compatibility flags */
451                                 if (strcmp(argv[i], "-fPIC") == 0)
452                                         kflag = F_PIC;
453                                 else if (strcmp(argv[i], "-fpic") == 0)
454                                         kflag = F_pic;
455                                 else if (strcmp(argv[i],
456                                     "-fsigned-char") == 0)
457                                         flist[nf++] = argv[i];
458                                 else if (strcmp(argv[i],
459                                     "-fno-signed-char") == 0)
460                                         flist[nf++] = argv[i];
461                                 else if (strcmp(argv[i],
462                                     "-funsigned-char") == 0)
463                                         flist[nf++] = argv[i];
464                                 else if (strcmp(argv[i],
465                                     "-fno-unsigned-char") == 0)
466                                         flist[nf++] = argv[i];
467                                 else if (strcmp(argv[i],
468                                     "-fstack-protector") == 0) {
469                                         flist[nf++] = argv[i];
470                                         sspflag++;
471                                 } else if (strcmp(argv[i],
472                                     "-fstack-protector-all") == 0) {
473                                         flist[nf++] = argv[i];
474                                         sspflag++;
475                                 } else if (strcmp(argv[i],
476                                     "-fno-stack-protector") == 0) {
477                                         flist[nf++] = argv[i];
478                                         sspflag = 0;
479                                 } else if (strcmp(argv[i],
480                                     "-fno-stack-protector-all") == 0) {
481                                         flist[nf++] = argv[i];
482                                         sspflag = 0;
483                                 }
484                                 /* silently ignore the rest */
485                                 break;
486
487                         case 'g'/* create debug output */
488                                 gflag++;
489                                 break;
490
491                         case 'i':
492                                 if (strcmp(argv[i], "-isystem") == 0) {
493                                         *pv++ = "-S";
494                                         *pv++ = argv[++i];
495                                 } else if (strcmp(argv[i], "-include") == 0) {
496                                         *pv++ = "-i";
497                                         *pv++ = argv[++i];
498                                 } else if (strcmp(argv[i], "-idirafter") == 0) {
499                                         idirafter = argv[++i];
500                                 } else
501                                         goto passa;
502                                 break;
503
504                         case 'k'/* generate PIC code */
505                                 kflag = F_pic;
506                                 break;
507
508                         case 'm'/* target-dependent options */
ragge
1.164
509 #ifdef mach_amd64
510                                 /* need to call i386 ccom for this */
511                                 if (strcmp(argv[i], "-m32") == 0) {
512                                         pass0 = LIBEXECDIR "/ccom_i386";
513                                         amd64_i386 = 1;
514                                         break;
515                                 }
516 #endif
ragge
1.128
517                                 mlist[nm++] = argv[i];
518                                 break;
519
520                         case 'n'/* handle -n flags */
521                                 if (strcmp(argv[i], "-nostdinc") == 0)
522                                         nostdinc++;
523                                 else if (strcmp(argv[i], "-nostdlib") == 0) {
524                                         nostdlib++;
525                                         nostartfiles++;
526                                 } else if (strcmp(argv[i], "-nostartfiles") == 0)
527                                         nostartfiles = 1;
528                                 else
529                                         goto passa;
530                                 break;
531
532                         case 'p':
533                                 if (strcmp(argv[i], "-pg") == 0 ||
534                                     strcmp(argv[i], "-p") == 0)
535                                         pgflag++;
536                                 else if (strcmp(argv[i], "-pthread") == 0)
537                                         pthreads++;
538                                 else if (strcmp(argv[i], "-pipe") == 0)
539                                         /* NOTHING YET */;
gmcgarry
1.144
540                                 else if (strcmp(argv[i], "-pedantic") == 0)
541                                         /* NOTHING YET */;
ragge
1.162
542                                 else if (strcmp(argv[i],
543                                     "-print-prog-name=ld") == 0) {
544                                         printf("%s\n"LINKER);
545                                         return 0;
546                                 } else
ragge
1.128
547                                         errorx(1"unknown option %s"argv[i]);
548                                 break;
549
gmcgarry
1.145
550                         case 'r':
551                                 rflag = 1;
552                                 break;
553
ragge
1.128
554                         case 'x':
555                                 t = &argv[i][2];
556                                 if (*t == 0)
557                                         t = argv[++i];
558                                 if (strcmp(t"c") == 0)
559                                         xcflag = 1/* default */
560                                 else if (strcmp(t"assembler-with-cpp") == 0)
561                                         ascpp = 1;
562 #ifdef notyet
563                                 else if (strcmp(t"c++") == 0)
564                                         cxxflag++;
565 #endif
566                                 else
567                                         xlist[xnum++] = argv[i];
568                                 break;
569                         case 't':
570                                 tflag++;
571                                 break;
572                         case 'S':
573                                 sflag++;
574                                 cflag++;
575                                 break;
576                         case 'o':
577                                 if (outfile)
578                                         errorx(8"too many -o");
579                                 outfile = argv[++i];
580                                 break;
581                         case 'O':
582                                 if (argv[i][2] == '0')
583                                         Oflag = 0;
584                                 else
585                                         Oflag++;
586                                 break;
587                         case 'E':
588                                 Eflag++;
589                                 break;
590                         case 'P':
591                                 pflag++;
592                                 *pv++ = argv[i];
593                         case 'c':
594                                 cflag++;
595                                 break;
596
597 #if 0
598                         case '2':
599                                 if(argv[i][2] == '\0')
600                                         pref = "/lib/crt2.o";
601                                 else {
602                                         pref = "/lib/crt20.o";
603                                 }
604                                 break;
605 #endif
606                         case 'C':
gmcgarry
1.163
607                                 cpplist[ncpp++] = argv[i];
ragge
1.128
608                                 break;
609                         case 'D':
610                         case 'I':
611                         case 'U':
612                                 *pv++ = argv[i];
613                                 if (argv[i][2] == 0)
614                                         *pv++ = argv[++i];
615                                 if (pv >= ptemp+MAXOPT) {
616                                         error("Too many DIU options");
617                                         --pv;
618                                 }
619                                 break;
620
621                         case 'M':
622                                 Mflag++;
623                                 break;
624
625                         case 'd':
gmcgarry
1.144
626 #ifdef os_darwin
627                                 if (strcmp(argv[i], "-dynamiclib") == 0) {
628                                         shared = 1;
629                                 } else
630 #endif
631                                 if (strcmp(argv[i], "-d") == 0) {
632                                         dflag++;
633                                         strlcpy(alistargv[i], sizeof (alist));
634                                 }
ragge
1.128
635                                 break;
636                         case 'v':
637                                 printf("%s\n"VERSSTR);
638                                 vflag++;
639                                 break;
640
641                         case 's':
gmcgarry
1.144
642 #ifndef os_darwin
643                                 if (strcmp(argv[i], "-shared") == 0) {
ragge
1.128
644                                         shared = 1;
645 #ifndef os_win32
646                                         nostdlib = 1;
647 #endif
gmcgarry
1.144
648                                 } else
649 #endif
650                                 if (strcmp(argv[i], "-static") == 0) {
651                                         Bstatic = 1;
ragge
1.128
652                                 } else if (strncmp(argv[i], "-std"4) == 0) {
653                                         /* ignore gcc -std= */;
654                                 } else
655                                         goto passa;
656                                 break;
657                         }
658                 } else {
659                 passa:
660                         t = argv[i];
661                         if (*argv[i] == '-' && argv[i][1] == 'L')
662                                 ;
663                         else if((c=getsuf(t))=='c' || c=='S' || c=='i' ||
664                             c=='s'|| Eflag || xcflag) {
665                                 clist[nc++] = t;
666                                 if (nc>=MAXFIL) {
667                                         error("Too many source files");
668                                         exit(1);
669                                 }
670                         }
671
672                         /* Check for duplicate .o files. */
673                         for (j = getsuf(t) == 'o' ? 0 : nlj < nlj++) {
674                                 if (strcmp(llist[j], t) == 0)
675                                         break;
676                         }
gmcgarry
1.150
677                         if ((c=getsuf(t))!='c' && c!='S' &&
678                             c!='s' && c!='i' && j==nl) {
ragge
1.128
679                                 llist[nl++] = t;
680                                 if (nl >= MAXLIB) {
681                                         error("Too many object/library files");
682                                         exit(1);
683                                 }
684                                 if (getsuf(t)=='o')
685                                         nxo++;
686                         }
687                 }
688         }
689         /* Sanity checking */
690         if (nc == 0 && nl == 0)
691                 errorx(8"no input files");
692         if (outfile && (cflag || sflag || Eflag) && nc > 1)
693                 errorx(8"-o given with -c || -E || -S and more than one file");
694         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
695                 errorx(8"output file will be clobbered");
gmcgarry
1.150
696         if (nc==0)
ragge
1.128
697                 goto nocom;
698         if (pflag==0) {
699                 if (!sflag)
700                         tmp3 = gettmp();
701                 tmp4 = gettmp();
702         }
703         if (Bflag) {
gmcgarry
1.153
704                 altincdir = Bflag;
ragge
1.128
705                 pccincdir = Bflag;
706                 pcclibdir = Bflag;
707         }
708         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
709                 signal(SIGINTidexit);
710         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
711                 signal(SIGTERMidexit);
712 #ifdef MULTITARGET
ragge
1.133
713         pass0 = copy(LIBEXECDIR "/ccom_"k = strlen(mach));
714         strlcat(pass0machsizeof(LIBEXECDIR "/ccom_") + k);
ragge
1.128
715 #endif
716         pvt = pv;
717         for (i=0i<nci++) {
718                 /*
719                  * C preprocessor
720                  */
721                 if (nc>1 && !Eflag)
722                         printf("%s:\n"clist[i]);
723                 onlyas = 0;
724                 assource = tmp3;
725                 if (getsuf(clist[i])=='S')
726                         ascpp = 1;
727                 if (getsuf(clist[i])=='i') {
728                         if(Eflag)
729                                 continue;
730                         goto com;
731                 } else if (ascpp) {
732                         onlyas = 1;
733                 } else if (getsuf(clist[i])=='s') {
734                         assource = clist[i];
735                         goto assemble;
736                 }
737                 if (pflag)
738                         tmp4 = setsuf(clist[i], 'i');
739                 na = 0;
740                 av[na++] = "cpp";
741                 if (vflag)
742                         av[na++] = "-v";
743                 av[na++] = "-D__PCC__=" MKS(PCC_MAJOR);
744                 av[na++] = "-D__PCC_MINOR__=" MKS(PCC_MINOR);
745                 av[na++] = "-D__PCC_MINORMINOR__=" MKS(PCC_MINORMINOR);
gmcgarry
1.156
746 #ifndef os_win32
ragge
1.155
747 #ifdef GCC_COMPAT
748                 av[na++] = "-D__GNUC__=4";
749                 av[na++] = "-D__GNUC_MINOR__=3";
750                 av[na++] = "-D__GNUC_PATCHLEVEL__=1";
751                 av[na++] = "-D__GNUC_STDC_INLINE__=1";
752 #endif
gmcgarry
1.156
753 #endif
ragge
1.128
754                 if (ascpp)
755                         av[na++] = "-D__ASSEMBLER__";
756                 if (sspflag)
757                         av[na++] = "-D__SSP__=1";
758                 if (pthreads)
759                         av[na++] = "-D_PTHREADS";
760                 if (Mflag)
761                         av[na++] = "-M";
ragge
1.160
762                 if (Oflag)
763                         av[na++] = "-D__OPTIMIZE__";
764 #ifdef GCC_COMPAT
765                 av[na++] = "-D__REGISTER_PREFIX__=" REGISTER_PREFIX;
766                 av[na++] = "-D__USER_LABEL_PREFIX__=" USER_LABEL_PREFIX;
767                 if (Oflag)
768                         av[na++] = "-D__OPTIMIZE__";
769 #endif
ragge
1.128
770                 if (dflag)
771                         av[na++] = alist;
772                 for (j = 0cppadd[j]; j++)
773                         av[na++] = cppadd[j];
gmcgarry
1.163
774                 for (j = 0j < ncppj++)
775                         av[na++] = cpplist[j];
gmcgarry
1.149
776                 av[na++] = "-D__STDC_ISO_10646__=200009L";
ragge
1.157
777                 av[na++] = "-D__WCHAR_TYPE__=" WCT;
778                 av[na++] = "-D__SIZEOF_WCHAR_T__=" MKS(WCHAR_SIZE);
779                 av[na++] = "-D__WCHAR_MAX__=" WCM;
gmcgarry
1.142
780                 av[na++] = "-D__WINT_TYPE__=unsigned int";
gmcgarry
1.149
781                 av[na++] = "-D__SIZE_TYPE__=unsigned long";
ragge
1.161
782                 av[na++] = "-D__PTRDIFF_TYPE__=" PCC_PTRDIFF_TYPE;
gmcgarry
1.142
783                 av[na++] = "-D__SIZEOF_WINT_T__=4";
ragge
1.128
784 #ifdef MULTITARGET
785                 for (k = 0cppmds[k].machk++) {
786                         if (strcmp(cppmds[k].machmach) != 0)
787                                 continue;
788                         for (j = 0cppmds[k].cppmdadd[j]; j++)
789                                 av[na++] = cppmds[k].cppmdadd[j];
790                         break;
791                 }
792 #else
793                 for (j = 0cppmdadd[j]; j++)
794                         av[na++] = cppmdadd[j];
795 #endif
796                 if (tflag)
797                         av[na++] = "-t";
798                 for(pv=ptemppv <pvtpv++)
799                         av[na++] = *pv;
gmcgarry
1.153
800                 if (!nostdinc) {
801                         av[na++] = "-S"av[na++] = altincdir;
ragge
1.128
802                         av[na++] = "-S"av[na++] = incdir;
gmcgarry
1.153
803                         av[na++] = "-S"av[na++] = pccincdir;
804                 }
ragge
1.128
805                 if (idirafter) {
806                         av[na++] = "-I";
807                         av[na++] = idirafter;
808                 }
809                 av[na++] = clist[i];
810                 if (!Eflag && !Mflag)
811                         av[na++] = tmp4;
ragge
1.141
812                 if ((Eflag || Mflag) && outfile)
ragge
1.128
813                          ermfile = av[na++] = outfile;
814                 av[na++]=0;
gmcgarry
1.140
815                 if (callsys(passpav)) {
816                         exfail++;
817                         eflag++;
818                 }
ragge
1.128
819                 if (Eflag || Mflag)
820                         continue;
821                 if (onlyas) {
822                         assource = tmp4;
823                         goto assemble;
824                 }
825
826                 /*
827                  * C compiler
828                  */
829         com:
830                 na = 0;
831                 av[na++]= "ccom";
832                 if (Wallflag) {
833                         /* Set only the same flags as gcc */
834                         for (Wf = WflagsWf->nameWf++) {
835                                 if (Wf->flags != INWALL)
836                                         continue;
837                                 av[na++] = Wf->name;
838                         }
839                 }
840                 if (Wflag) {
841                         /* set all positive flags */
842                         for (Wf = WflagsWf->nameWf++) {
843                                 if (Wf->flags == NEGATIVE)
844                                         continue;
845                                 av[na++] = Wf->name;
846                         }
847                 }
848                 for (j = 0j < nwj++)
849                         av[na++] = wlist[j];
850                 for (j = 0j < nfj++)
851                         av[na++] = flist[j];
ragge
1.159
852 #if !defined(os_sunos) && !defined(mach_i386)
ragge
1.128
853                 if (vflag)
854                         av[na++] = "-v";
ragge
1.159
855 #endif
ragge
1.128
856                 if (pgflag)
857                         av[na++] = "-p";
858                 if (gflag)
859                         av[na++] = "-g";
860 #ifdef os_darwin
861                 /* darwin always wants PIC compilation */
862                 if (!Bstatic)
863                         av[na++] = "-k";
ragge
1.159
864 #elif defined(os_sunos) && defined(mach_i386)
865                 if (kflag) {
866                         av[na++] = "-K";
867                         av[na++] = "pic";
868                 }
ragge
1.128
869 #else
870                 if (kflag)
871                         av[na++] = "-k";
872 #endif
873                 if (Oflag) {
874                         av[na++] = "-xtemps";
875                         av[na++] = "-xdeljumps";
ragge
1.133
876                         av[na++] = "-xinline";
ragge
1.128
877                 }
878                 for (j = 0j < xnumj++)
879                         av[na++] = xlist[j];
880                 for (j = 0j < nmj++)
881                         av[na++] = mlist[j];
882                 if (getsuf(clist[i])=='i')
883                         av[na++] = clist[i];
884                 else
885                         av[na++] = tmp4/* created by cpp */
886                 if (pflag || exfail)
887                         {
888                         cflag++;
889                         continue;
890                         }
891                 if(sflag) {
892                         if (outfile)
893                                 tmp3 = outfile;
894                         else
895                                 tmp3 = setsuf(clist[i], 's');
896                 }
897                 ermfile = av[na++] = tmp3;
898 #if 0
899                 if (proflag) {
900                         av[3] = "-XP";
901                         av[4] = 0;
902                 } else
903                         av[3] = 0;
904 #endif
905                 av[na++] = NULL;
906                 if (callsys(pass0av)) {
907                         cflag++;
908                         eflag++;
909                         continue;
910                 }
911                 if (sflag)
912                         continue;
913
914                 /*
915                  * Assembler
916                  */
917         assemble:
918                 na = 0;
919                 av[na++] = as;
920                 for (j = 0j < nasj++)
921                         av[na++] = aslist[j];
gmcgarry
1.149
922 #if defined(os_win32) && defined(USE_YASM)
923                 av[na++] = "-p";
924                 av[na++] = "gnu";
925                 av[na++] = "-f";
926                 av[na++] = "win32";
927 #endif
ragge
1.128
928 #if defined(os_sunos) && defined(mach_sparc64)
929                 av[na++] = "-m64";
930 #endif
931 #if defined(os_darwin)
932                 if (Bstatic)
933                         av[na++] = "-static";
934 #endif
935                 if (vflag)
936                         av[na++] = "-v";
937                 if (kflag)
938                         av[na++] = "-k";
ragge
1.164
939 #ifdef mach_amd64
940                 if (amd64_i386)
941                         av[na++] = "--32";
942 #endif
ragge
1.128
943                 av[na++] = "-o";
944                 if (outfile && cflag)
945                         ermfile = av[na++] = outfile;
gmcgarry
1.150
946                 else if (cflag)
947                         ermfile = av[na++] = olist[i] = setsuf(clist[i], 'o');
ragge
1.128
948                 else
gmcgarry
1.150
949                         ermfile = av[na++] = olist[i] = gettmp();
ragge
1.128
950                 av[na++] = assource;
951                 if (dflag)
952                         av[na++] = alist;
953                 av[na++] = 0;
954                 if (callsys(asav)) {
955                         cflag++;
956                         eflag++;
957                         cunlink(tmp4);
958                         continue;
959                 }
960                 cunlink(tmp4);
961         }
962
963         if (Eflag || Mflag)
964                 dexit(eflag);
965
966         /*
967          * Linker
968          */
969 nocom:
gmcgarry
1.150
970         if (cflag==0 && nc+nl != 0) {
ragge
1.128
971                 j = 0;
972                 av[j++] = ld;
973 #ifndef MSLINKER
974                 if (vflag)
975                         av[j++] = "-v";
976 #endif
gmcgarry
1.131
977 #if !defined(os_sunos) && !defined(os_win32) && !defined(os_darwin)
ragge
1.128
978                 av[j++] = "-X";
979 #endif
980                 if (shared) {
gmcgarry
1.144
981 #ifdef os_darwin
982                         av[j++] = "-dylib";
983 #else
ragge
1.128
984                         av[j++] = "-shared";
gmcgarry
1.144
985 #endif
ragge
1.128
986 #ifdef os_win32
987                         av[j++] = "-Bdynamic";
988 #endif
989 #ifndef os_sunos
990                 } else {
991 #ifndef os_win32
992 #ifndef os_darwin
993                         av[j++] = "-d";
994 #endif
gmcgarry
1.145
995                         if (rflag) {
996                                 av[j++] = "-r";
997                         } else {
998                                 av[j++] = "-e";
999                                 av[j++] = STARTLABEL;
1000                         }
ragge
1.128
1001 #endif
1002 #endif
1003                         if (Bstatic == 0) { /* Dynamic linkage */
1004 #ifdef DYNLINKER
1005                                 for (i = 0dynlinker[i]; i++)
1006                                         av[j++] = dynlinker[i];
1007 #endif
1008                         } else {
1009 #ifdef os_darwin
1010                                 av[j++] = "-static";
1011 #else
1012                                 av[j++] = "-Bstatic";
1013 #endif
1014                         }
1015                 }
1016                 if (outfile) {
1017 #ifdef MSLINKER
gmcgarry
1.135
1018 #define OUTSTR  "/OUT:"
1019                         char *s = copy(OUTSTRi = strlen(outfile));
1020                         strlcat(soutfilesizeof(OUTSTR) + i);
ragge
1.128
1021                         av[j++] = s;
1022 #else
1023                         av[j++] = "-o";
1024                         av[j++] = outfile;
1025 #endif
1026                 }
1027 #ifdef STARTFILES_S
1028                 if (shared) {
1029                         if (!nostartfiles) {
1030                                 for (i = 0startfiles_S[i]; i++)
1031                                         av[j++] = Bprefix(startfiles_S[i]);
1032                         }
1033                 } else
1034 #endif
1035                 {
1036                         if (!nostartfiles) {
1037 #ifdef CRT0FILE_PROFILE
1038                                 if (pgflag) {
1039                                         av[j++] = Bprefix(crt0file_profile);
1040                                 } else
1041 #endif
1042                                 {
1043 #ifdef CRT0FILE