Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20100831172811

Diff

Diff from 1.160 to:

Annotations

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

Annotated File View

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