Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20120803143510

Diff

Diff from 1.225 to:

Annotations

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

Annotated File View

ragge
1.225
1 /*      $Id: cc.c,v 1.225 2012/08/03 14:35:10 ragge Exp $       */
ragge
1.222
2
3 /*-
4  * Copyright (c) 2011 Joerg Sonnenberger <joerg@NetBSD.org>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
ragge
1.128
32 /*
33  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  *
39  * Redistributions of source code and documentation must retain the above
40  * copyright notice, this list of conditions and the following disclaimer.
41  * Redistributions in binary form must reproduce the above copyright
42  * notice, this list of conditionsand the following disclaimer in the
43  * documentation and/or other materials provided with the distribution.
44  * All advertising materials mentioning features or use of this software
45  * must display the following acknowledgement:
46  *      This product includes software developed or owned by Caldera
47  *      International, Inc.
48  * Neither the name of Caldera International, Inc. nor the names of other
49  * contributors may be used to endorse or promote products derived from
50  * this software without specific prior written permission.
51  *
52  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
53  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
54  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
55  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
56  * DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
57  * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
61  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
62  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
63  * POSSIBILITY OF SUCH DAMAGE.
64  */
65
66 /*
67  * Front-end to the C compiler.
68  *
69  * Brief description of its syntax:
70  * - Files that end with .c are passed via cpp->ccom->as->ld
71  * - Files that end with .i are passed via ccom->as->ld
plunky
1.206
72  * - Files that end with .S are passed via cpp->as->ld
73  * - Files that end with .s are passed via as->ld
ragge
1.128
74  * - Files that end with .o are passed directly to ld
75  * - Multiple files may be given on the command line.
76  * - Unrecognized options are all sent directly to ld.
77  * -c or -S cannot be combined with -o if multiple files are given.
78  *
79  * This file should be rewritten readable.
80  */
81 #include "config.h"
82
83 #include <sys/types.h>
84 #ifdef HAVE_SYS_WAIT_H
85 #include <sys/wait.h>
86 #endif
87
88 #include <ctype.h>
89 #include <errno.h>
90 #include <fcntl.h>
91 #ifdef HAVE_LIBGEN_H
92 #include <libgen.h>
93 #endif
94 #include <signal.h>
95 #include <stdarg.h>
96 #include <stdio.h>
97 #include <stdlib.h>
98 #include <string.h>
99 #ifdef HAVE_UNISTD_H
100 #include <unistd.h>
101 #endif
ragge
1.222
102 #include <assert.h>
ragge
1.128
103
plunky
1.213
104 #ifdef os_win32
ragge
1.128
105 #include <windows.h>
106 #include <process.h>
107 #include <io.h>
gmcgarry
1.179
108 #define F_OK    0x00
109 #define R_OK    0x04
110 #define W_OK    0x02
111 #define X_OK    R_OK
ragge
1.128
112 #endif
113
114 #include "compat.h"
115
116 #include "ccconfig.h"
ragge
1.167
117 #include "macdefs.h"
ragge
1.218
118
119 #include "xalloc.h"
120 #include "strlist.h"
ragge
1.128
121 /* C command */
122
123 #define MKS(x) _MKS(x)
124 #define _MKS(x) #x
125
126 /*
127  * Many specific definitions, should be declared elsewhere.
128  */
129
130 #ifndef STDINC
131 #define STDINC          "/usr/include/"
132 #endif
133
134 #ifndef LIBDIR
135 #define LIBDIR          "/usr/lib/"
136 #endif
137
138 #ifndef PREPROCESSOR
139 #define PREPROCESSOR    "cpp"
140 #endif
141
142 #ifndef COMPILER
gmcgarry
1.138
143 #define COMPILER        "ccom"
ragge
1.128
144 #endif
145
146 #ifndef ASSEMBLER
147 #define ASSEMBLER       "as"
148 #endif
149
150 #ifndef LINKER
151 #define LINKER          "ld"
152 #endif
153
ragge
1.174
154 #ifndef MULTIOSDIR
155 #define MULTIOSDIR      "."
156 #endif
157
158
ragge
1.128
159 #define MAXAV  10000
160 char    *tmp3;
161 char    *tmp4;
162 char    *outfile, *ermfile;
ragge
1.220
163 static char **lav;
164 static int lac;
ragge
1.175
165 static void add_prefix(const char *);
166 static char *find_file(const char *, int);
ragge
1.224
167 static char *find_file2(const char *filestruct strlist *pathint mode);
plunky
1.183
168 char *copy(const char *, int);
plunky
1.185
169 char *cat(const char *, const char *);
gmcgarry
1.150
170 char *setsuf(char *, char);
ragge
1.198
171 int cxxsuf(char *);
ragge
1.128
172 int getsuf(char *);
ragge
1.198
173 char *getsufp(char *s);
ragge
1.128
174 int main(intchar *[]);
175 void error(char *, ...);
176 void errorx(intchar *, ...);
177 int callsys(char [], char *[]);
178 int cunlink(char *);
179 void dexit(int);
180 void idexit(int);
181 char *gettmp(void);
182 void *ccmalloc(int size);
ragge
1.218
183 void aerror(char *);
ragge
1.219
184 void oerror(char *);
ragge
1.223
185 void owarning(char *);
ragge
1.218
186 char *argnxt(char *, char *);
ragge
1.220
187 char *nxtopt(char *o);
ragge
1.222
188 void setup_cpp_flags(void);
189 static void expand_sysroot(void);
plunky
1.213
190 #ifdef os_win32
ragge
1.128
191 char *win32pathsubst(char *);
gmcgarry
1.149
192 char *win32commandline(char *, char *[]);
ragge
1.128
193 #endif
194 char    *av[MAXAV];
195 char    *xlist[100];
196 int     xnum;
197 char    *mlist[100];
198 char    *flist[100];
199 char    *wlist[100];
200 int     nm;
201 int     nf;
202 int     nw;
203 int     sspflag;
plunky
1.214
204 int     freestanding;
ragge
1.128
205 int     pflag;
206 int     sflag;
207 int     cflag;
208 int     eflag;
209 int     gflag;
gmcgarry
1.145
210 int     rflag;
ragge
1.128
211 int     vflag;
212 int     tflag;
213 int     Eflag;
214 int     Oflag;
215 int     kflag;  /* generate PIC/pic code */
216 #define F_PIC   1
217 #define F_pic   2
ragge
1.223
218 int     MflagneedM;   /* dependencies only */
ragge
1.128
219 int     pgflag;
220 int     exfail;
221 int     Xflag;
222 int     Wallflag;
223 int     Wflag;
224 int     nostartfilesBstaticshared;
225 int     nostdincnostdlib;
226 int     onlyas;
227 int     pthreads;
plunky
1.217
228 int     xasmxcflagxgnu89xgnu99;
ragge
1.128
229 int     ascpp;
plunky
1.196
230 #ifdef CHAR_UNSIGNED
plunky
1.209
231 int     xuchar = 1;
plunky
1.196
232 #else
plunky
1.209
233 int     xuchar = 0;
plunky
1.196
234 #endif
ragge
1.198
235 int     cxxflag;
ragge
1.216
236 int     cppflag;
ragge
1.128
237
238 char    *passp = LIBEXECDIR PREPROCESSOR;
239 char    *pass0 = LIBEXECDIR COMPILER;
ragge
1.212
240 char    *passxx0 = LIBEXECDIR "cxxcom";
ragge
1.128
241 char    *as = ASSEMBLER;
242 char    *ld = LINKER;
ragge
1.222
243 char    *sysroot = "", *isysroot;
ragge
1.128
244 char *cppadd[] = CPPADD;
245 #ifdef DYNLINKER
246 char *dynlinker[] = DYNLINKER;
247 #endif
248 #ifdef CRT0FILE
249 char *crt0file = CRT0FILE;
250 #endif
251 #ifdef CRT0FILE_PROFILE
252 char *crt0file_profile = CRT0FILE_PROFILE;
253 #endif
254 #ifdef STARTFILES
255 char *startfiles[] = STARTFILES;
256 char *endfiles[] = ENDFILES;
257 #endif
258 #ifdef STARTFILES_T
259 char *startfiles_T[] = STARTFILES_T;
260 char *endfiles_T[] = ENDFILES_T;
261 #endif
262 #ifdef STARTFILES_S
263 char *startfiles_S[] = STARTFILES_S;
264 char *endfiles_S[] = ENDFILES_S;
265 #endif
266 char *cppmdadd[] = CPPMDADD;
267 #ifdef LIBCLIBS
268 char *libclibs[] = LIBCLIBS;
269 #else
270 char *libclibs[] = { "-lc"NULL };
271 #endif
272 #ifdef LIBCLIBS_PROFILE
273 char *libclibs_profile[] = LIBCLIBS_PROFILE;
274 #else
275 char *libclibs_profile[] = { "-lc_p"NULL };
276 #endif
277 #ifndef STARTLABEL
278 #define STARTLABEL "__start"
279 #endif
plunky
1.200
280 #ifdef PCCLIBDIR
ragge
1.128
281 char *pcclibdir = PCCLIBDIR;
plunky
1.200
282 #endif
ragge
1.164
283 #ifdef mach_amd64
284 int amd64_i386;
285 #endif
ragge
1.128
286
ragge
1.218
287 #define match(a,b)      (strcmp(a,b) == 0)
288
ragge
1.128
289 /* handle gcc warning emulations */
290 struct Wflags {
291         char *name;
292         int flags;
293 #define INWALL          1
294 Wflags[] = {
plunky
1.215
295         { "truncate"0 },
296         { "strict-prototypes"0 },
297         { "missing-prototypes"0 },
298         { "implicit-int"INWALL },
299         { "implicit-function-declaration"INWALL },
300         { "shadow"0 },
301         { "pointer-sign"INWALL },
302         { "sign-compare"0 },
303         { "unknown-pragmas"INWALL },
304         { "unreachable-code"0 },
305         { NULL0 },
ragge
1.128
306 };
307
ragge
1.158
308 #ifndef USHORT
309 /* copied from mip/manifest.h */
310 #define USHORT          5
311 #define INT             6
312 #define UNSIGNED        7
313 #endif
314
ragge
1.157
315 /*
316  * Wide char defines.
317  */
318 #if WCHAR_TYPE == USHORT
319 #define WCT "short unsigned int"
320 #define WCM "65535U"
ragge
1.158
321 #if WCHAR_SIZE != 2
322 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
323 #endif
ragge
1.157
324 #elif WCHAR_TYPE == INT
325 #define WCT "int"
326 #define WCM "2147483647"
ragge
1.158
327 #if WCHAR_SIZE != 4
328 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
329 #endif
ragge
1.157
330 #elif WCHAR_TYPE == UNSIGNED
331 #define WCT "unsigned int"
ragge
1.158
332 #define WCM "4294967295U"
333 #if WCHAR_SIZE != 4
334 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
335 #endif
336 #else
337 #error WCHAR_TYPE not defined or invalid
ragge
1.157
338 #endif
339
ragge
1.160
340 #ifdef GCC_COMPAT
341 #ifndef REGISTER_PREFIX
342 #define REGISTER_PREFIX ""
343 #endif
344 #ifndef USER_LABEL_PREFIX
345 #define USER_LABEL_PREFIX ""
346 #endif
347 #endif
348
plunky
1.192
349 #ifndef PCC_WINT_TYPE
350 #define PCC_WINT_TYPE "unsigned int"
351 #endif
352
353 #ifndef PCC_SIZE_TYPE
354 #define PCC_SIZE_TYPE "unsigned long"
355 #endif
356
ragge
1.161
357 #ifndef PCC_PTRDIFF_TYPE
358 #define PCC_PTRDIFF_TYPE "long int"
359 #endif
360
ragge
1.216
361 #ifndef CPPROGNAME
362 #define CPPROGNAME      "cpp"
363 #endif
364
ragge
1.218
365 struct strlist preprocessor_flags;
ragge
1.223
366 struct strlist depflags;
ragge
1.219
367 struct strlist incdirs;
ragge
1.221
368 struct strlist user_sysincdirs;
369 struct strlist includes;
ragge
1.222
370 struct strlist sysincdirs;
ragge
1.223
371 struct strlist dirafterdirs;
ragge
1.222
372 struct strlist crtdirs;
373 struct strlist libdirs;
374 struct strlist progdirs;
ragge
1.224
375 struct strlist early_linker_flags;
376 struct strlist middle_linker_flags;
377 struct strlist late_linker_flags;
378 struct strlist inputs;
ragge
1.225
379 struct strlist assembler_flags;
380 struct strlist temp_outputs;
ragge
1.218
381
ragge
1.128
382 int
383 main(int argcchar *argv[])
384 {
ragge
1.224
385         struct string *s, *ss;
ragge
1.128
386         struct Wflags *Wf;
ragge
1.218
387         char *t, *u, *argp;
ragge
1.128
388         char *assource;
ragge
1.225
389         int ninputijnxona;
ragge
1.222
390
ragge
1.220
391         lav = argv;
392         lac = argc;
ragge
1.128
393
ragge
1.222
394         strlist_init(&crtdirs);
395         strlist_init(&libdirs);
396         strlist_init(&progdirs);
ragge
1.218
397         strlist_init(&preprocessor_flags);
ragge
1.219
398         strlist_init(&incdirs);
ragge
1.221
399         strlist_init(&user_sysincdirs);
400         strlist_init(&includes);
ragge
1.222
401         strlist_init(&sysincdirs);
ragge
1.223
402         strlist_init(&dirafterdirs);
403         strlist_init(&depflags);
ragge
1.224
404         strlist_init(&early_linker_flags);
405         strlist_init(&middle_linker_flags);
406         strlist_init(&late_linker_flags);
407         strlist_init(&inputs);
ragge
1.225
408         strlist_init(&assembler_flags);
409         strlist_init(&temp_outputs);
ragge
1.218
410
ragge
1.216
411         if ((t = strrchr(argv[0], '/')))
412                 t = copy(t+10);
413         else
414                 t = argv[0];
415         if (strcmp(t"p++") == 0) {
ragge
1.198
416                 cxxflag = 1;
417                 pass0 = passxx0;
ragge
1.216
418         } else if (strcmp(t"cpp") == 0 || strcmp(tCPPROGNAME) == 0) {
419                 cppflag = 1;
420                 Eflag = 1;
ragge
1.198
421         }
422
plunky
1.213
423 #ifdef os_win32
ragge
1.128
424         /* have to prefix path early.  -B may override */
425         incdir = win32pathsubst(incdir);
gmcgarry
1.153
426         altincdir = win32pathsubst(altincdir);
gmcgarry
1.136
427         libdir = win32pathsubst(libdir);
plunky
1.200
428 #ifdef PCCINCDIR
ragge
1.128
429         pccincdir = win32pathsubst(pccincdir);
plunky
1.199
430         pxxincdir = win32pathsubst(pxxincdir);
plunky
1.200
431 #endif
432 #ifdef PCCLIBDIR
ragge
1.128
433         pcclibdir = win32pathsubst(pcclibdir);
plunky
1.200
434 #endif
ragge
1.128
435         passp = win32pathsubst(passp);
436         pass0 = win32pathsubst(pass0);
gmcgarry
1.180
437 #ifdef STARTFILES
438         for (i = 0startfiles[i] != NULLi++)
439                 startfiles[i] = win32pathsubst(startfiles[i]);
440         for (i = 0endfiles[i] != NULLi++)
441                 endfiles[i] = win32pathsubst(endfiles[i]);
442 #endif
443 #ifdef STARTFILES_T
444         for (i = 0startfiles_T[i] != NULLi++)
445                 startfiles_T[i] = win32pathsubst(startfiles_T[i]);
446         for (i = 0endfiles_T[i] != NULLi++)
447                 endfiles_T[i] = win32pathsubst(endfiles_T[i]);
448 #endif
449 #ifdef STARTFILES_S
450         for (i = 0startfiles_S[i] != NULLi++)
451                 startfiles_S[i] = win32pathsubst(startfiles_S[i]);
452         for (i = 0endfiles_S[i] != NULLi++)
453                 endfiles_S[i] = win32pathsubst(endfiles_S[i]);
454 #endif
ragge
1.128
455 #endif
456
ragge
1.225
457         nxo = 0;
ragge
1.220
458         while (--lac) {
459                 ++lav;
460                 argp = *lav;
461
ragge
1.225
462                 if (*argp != '-' || match(argp"-")) {
463                         /* Check for duplicate .o files. */
464                         if (getsuf(argp) == 'o') {
465                                 j = 0;
466                                 STRLIST_FOREACH(s, &inputs)
467                                         if (match(argps->value))
468                                                 j++;
469                                 if (j)
470                                         continue/* skip it */
471                         }
472                         strlist_append(&inputsargp);
473                         continue;
474                 }
ragge
1.220
475
476                 switch (argp[1]) {
477                 default:
ragge
1.224
478                         owarning(argp);
ragge
1.220
479                         break;
ragge
1.128
480
ragge
1.220
481                 case '-'/* double -'s */
482                         if (match(argp"--version")) {
483                                 printf("%s\n"VERSSTR);
484                                 return 0;
485                         } else if (strncmp(argp"--sysroot="10) == 0) {
486                                 sysroot = argp + 10;
487                         } else if (strcmp(argp"--param") == 0) {
488                                 /* NOTHING YET */;
489                                 (void)nxtopt(0); /* ignore arg */
490                         } else
ragge
1.225
491                                 owarning(argp);
ragge
1.220
492                         break;
ragge
1.128
493
ragge
1.220
494                 case 'B'/* other search paths for binaries */
ragge
1.224
495                         t = nxtopt("-B");
496                         strlist_append(&crtdirst);
497                         strlist_append(&libdirst);
498                         strlist_append(&progdirst);
499                         add_prefix(t);
ragge
1.220
500                         break;
ragge
1.128
501
ragge
1.220
502                 case 'C':
503                         if (match(argp"-C") || match(argp"-CC"))
504                                 strlist_append(&preprocessor_flagsargp);
505                         else
506                                 oerror(argp);
507                         break;
ragge
1.218
508
ragge
1.220
509                 case 'X':
510                         Xflag++;
511                         break;
512                 case 'W'/* Ignore (most of) W-flags */
ragge
1.225
513                         if ((t = argnxt(argp"-Wl,"))) {
514                                 u = strtok(t",");
515                                 do {
516                                         strlist_append(&middle_linker_flagsu);
517                                 } while ((u = strtok(NULL",")) != NULL);
518                         } else if ((t = argnxt(argp"-Wa,"))) {
519                                 u = strtok(t",");
520                                 do {
521                                         strlist_append(&assembler_flagsu);
522                                 } while ((u = strtok(NULL",")) != NULL);
ragge
1.220
523                         } else if (strncmp(argp"-Wc,"4) == 0) {
524                                 /* options to ccom */
525                                 t = &argp[4];
526                                 while ((u = strchr(t','))) {
527                                         *u++ = 0;
ragge
1.137
528                                         wlist[nw++] = t;
ragge
1.220
529                                         t = u;
530                                 }
531                                 wlist[nw++] = t;
532                         } else if ((t = argnxt(argp"-Wp,"))) {
533                                 u = strtok(t",");
534                                 do {
535                                         strlist_append(&preprocessor_flagsu);
536                                 } while ((u = strtok(NULL",")) != NULL);
537                         } else if (strcmp(argp"-Werror") == 0) {
538                                 wlist[nw++] = argp;
539                         } else if (strcmp(argp"-Wall") == 0) {
540                                 Wallflag = 1;
541                         } else if (strcmp(argp"-WW") == 0) {
542                                 Wflag = 1;
543                         } else {
544                                 /* pass through, if supported */
545                                 t = &argp[2];
546                                 if (strncmp(t"no-"3) == 0)
547                                         t += 3;
548                                 if (strncmp(t"error="6) == 0)
549                                         t += 6;
550                                 for (Wf = WflagsWf->nameWf++) {
551                                         if (strcmp(tWf->name) == 0)
552                                                 wlist[nw++] = argp;
ragge
1.128
553                                 }
ragge
1.220
554                         }
555                         break;
ragge
1.128
556
ragge
1.220
557                 case 'f'/* GCC compatibility flags */
558                         if (strcmp(argp"-fPIC") == 0)
559                                 kflag = F_PIC;
560                         else if (strcmp(argp"-fpic") == 0)
561                                 kflag = F_pic;
562                         else if (strcmp(argp"-ffreestanding") == 0)
563                                 freestanding = 1;
ragge
1.221
564                         else if (match(argp"-fsigned-char") ||
565                             match(argp"-fno-unsigned-char"))
ragge
1.220
566                                 xuchar = 0;
ragge
1.221
567                         else if (match(argp"-fno-signed-char") ||
568                             match(argp"-funsigned-char"))
ragge
1.220
569                                 xuchar = 1;
ragge
1.221
570                         else if (strcmp(argp"-fstack-protector") == 0) {
ragge
1.220
571                                 flist[nf++] = argp;
572                                 sspflag++;
ragge
1.221
573                         } else if (strcmp(argp"-fstack-protector-all") == 0) {
ragge
1.220
574                                 flist[nf++] = argp;
575                                 sspflag++;
ragge
1.221
576                         } else if (strcmp(argp"-fno-stack-protector") == 0) {
ragge
1.220
577                                 flist[nf++] = argp;
578                                 sspflag = 0;
ragge
1.221
579                         } else if (strcmp(argp"-fno-stack-protector-all") == 0) {
ragge
1.220
580                                 flist[nf++] = argp;
581                                 sspflag = 0;
582                         }
583                         /* silently ignore the rest */
584                         break;
ragge
1.128
585
ragge
1.220
586                 case 'g'/* create debug output */
587                         if (argp[2] == '0')
588                                 gflag = 0;
589                         else
590                                 gflag++;
591                         break;
ragge
1.128
592
ragge
1.220
593                 case 'D':
594                 case 'U':
ragge
1.221
595                         strlist_append(&preprocessor_flagsargp);
ragge
1.220
596                         if (argp[2] != 0)
ragge
1.219
597                                 break;
ragge
1.221
598                         strlist_append(&preprocessor_flagsnxtopt(argp));
599                         break;
600
601                 case 'I'/* Add include dirs */
602                         strlist_append(&incdirsnxtopt("-I"));
ragge
1.220
603                         break;
ragge
1.219
604
ragge
1.220
605                 case 'i':
ragge
1.221
606                         if (match(argp"-isystem")) {
607                                 strlist_append(&user_sysincdirsnxtopt(0));
608                         } else if (match(argp"-include")) {
609                                 strlist_append(&includesnxtopt(0));
ragge
1.222
610                         } else if (match(argp"-isysroot")) {
611                                 isysroot = nxtopt(0);
ragge
1.220
612                         } else if (strcmp(argp"-idirafter") == 0) {
ragge
1.223
613                                 strlist_append(&dirafterdirsnxtopt(0));
gmcgarry
1.172
614 #ifdef os_darwin
ragge
1.225
615                         } else if (match(argp"-install_name")) {
616                                 strlist_append(&middle_linker_flagsargp);
617                                 strlist_append(&middle_linker_flagsnxtopt(0));
gmcgarry
1.172
618 #endif
ragge
1.220
619                         } else
ragge
1.225
620                                 owarning(argp);
ragge
1.220
621                         break;
ragge
1.128
622
ragge
1.220
623                 case 'k'/* generate PIC code */
624                         kflag = F_pic;
625                         break;
ragge
1.128
626
ragge
1.224
627                 case 'l':
628                 case 'L':
629                         strlist_append(&late_linker_flagsargp);
630                         break;
631
632
ragge
1.220
633                 case 'm'/* target-dependent options */
ragge
1.164
634 #ifdef mach_amd64
ragge
1.220
635                         /* need to call i386 ccom for this */
636                         if (strcmp(argp"-m32") == 0) {
637                                 pass0 = LIBEXECDIR "/ccom_i386";
638                                 amd64_i386 = 1;
639                                 break;
640                         }
ragge
1.164
641 #endif
ragge
1.220
642                         mlist[nm++] = argp;
ragge
1.225
643                         strlist_append(&middle_linker_flagsargp);
ragge
1.220
644                         if (argp[2] == 0) {
ragge
1.225
645                                 t = nxtopt(0);
646                                 strlist_append(&middle_linker_flagst);
647                                 mlist[nm++] = t;
ragge
1.220
648                         }
649                         break;
ragge
1.128
650
ragge
1.220
651                 case 'n'/* handle -n flags */
652                         if (strcmp(argp"-nostdinc") == 0)
653                                 nostdinc++;
654                         else if (strcmp(argp"-nostdlib") == 0) {
655                                 nostdlib++;
656                                 nostartfiles++;
657                         } else if (strcmp(argp"-nostartfiles") == 0)
658                                 nostartfiles = 1;
659                         else if (strcmp(argp"-nodefaultlibs") == 0)
660                                 nostdlib++;
661                         else
ragge
1.225
662                                 owarning(argp);
ragge
1.220
663                         break;
ragge
1.128
664
ragge
1.220
665                 case 'p':
666                         if (strcmp(argp"-pg") == 0 ||
667                             strcmp(argp"-p") == 0)
668                                 pgflag++;
669                         else if (strcmp(argp"-pthread") == 0)
670                                 pthreads++;
671                         else if (strcmp(argp"-pipe") == 0)
672                                 /* NOTHING YET */;
673                         else if (strcmp(argp"-pedantic") == 0)
674                                 /* NOTHING YET */;
675                         else if (strcmp(argp,
676                             "-print-prog-name=ld") == 0) {
677                                 printf("%s\n"LINKER);
678                                 return 0;
679                         } else if (strcmp(argp,
680                             "-print-multi-os-directory") == 0) {
681                                 printf("%s\n"MULTIOSDIR);
682                                 return 0;
683                         } else
684                                 oerror(argp);
685                         break;
ragge
1.128
686
ragge
1.220
687                 case 'r':
688                         rflag = 1;
689                         break;
gmcgarry
1.145
690
ragge
1.220
691                 case 'x':
692                         t = nxtopt("-x");
693                         if (match(t"c"))
694                                 xcflag = 1/* default */
695                         else if (match(t"assembler"))
696                                 xasm = 1;
697                         else if (match(t"assembler-with-cpp"))
698                                 ascpp = 1;
699                         else if (match(t"c++"))
700                                 cxxflag++;
701                         else if (argp[2])
702                                 xlist[xnum++] = argp;
703                         else
704                                 xlist[xnum++] = "-x"xlist[xnum++] = t;
705                         break;
706                 case 't':
707                         tflag++;
708                         break;
709                 case 'S':
710                         sflag++;
711                         cflag++;
712                         break;
713                 case 'o':
714                         if (outfile)
715                                 errorx(8"too many -o");
716                         outfile = nxtopt("-o");
717                         break;
718                 case 'O':
719                         if (argp[2] == '\0')
720                                 Oflag++;
721                         else if (argp[3] == '\0' && isdigit((unsigned char)argp[2]))
722                                 Oflag = argp[2] - '0';
723                         else if (argp[3] == '\0' && argp[2] == 's')
724                                 Oflag = 1;      /* optimize for space only */
725                         else
726                                 oerror(argp);
727                         break;
728                 case 'E':
729                         Eflag++;
730                         break;
731                 case 'P':
732                         pflag++;
ragge
1.221
733                         strlist_append(&preprocessor_flagsargp);
734                         break;
735
ragge
1.220
736                 case 'c':
gmcgarry
1.172
737 #ifdef os_darwin
ragge
1.225
738                         if (match(argp"-compatibility_version") ||
739                             match(argp"-current_version")) {
740                                 strlist_append(&middle_linker_flagsargp);
741                                 strlist_append(&middle_linker_flagsnxtopt(0));
ragge
1.220
742                         } else
gmcgarry
1.172
743 #endif
ragge
1.220
744                                 cflag++;
745                         break;
ragge
1.223
746
ragge
1.220
747                 case 'M':
748                         switch (argp[2]) {
ragge
1.223
749                         case '\0':
750                                 Mflag++;
751                                 strlist_append(&depflagsargp);
752                                 break;
753                         case 'P':
754                                 needM = 1;
755                                 strlist_append(&depflags"-xMP");
756                                 break;
757                         case 'F':
758                                 needM = 1;
759                                 outfile = nxtopt("-MF");
760                                 break;
ragge
1.220
761                         case 'T':
762                         case 'Q':
ragge
1.223
763                                 needM = 1;
764                                 j = strlen(u = nxtopt("-MT"));
765                                 t = copy("-xMT,"j);
ragge
1.220
766                                 strlcat(tuj+6);
767                                 t[3] = argp[2];
ragge
1.223
768                                 strlist_append(&depflagst);
ragge
1.128
769                                 break;
ragge
1.220
770                         default:
771                                 oerror(argp);
772                         }
773                         break;
ragge
1.128
774
ragge
1.220
775                 case 'd':
gmcgarry
1.144
776 #ifdef os_darwin
ragge
1.220
777                         if (strcmp(argp"-dynamiclib") == 0) {
778                                 shared = 1;
779                         } else
gmcgarry
1.144
780 #endif
ragge
1.220
781                         break;
782                 case 'v':
783                         printf("%s\n"VERSSTR);
784                         vflag++;
785                         break;
ragge
1.128
786
ragge
1.220
787                 case 's':
gmcgarry
1.144
788 #ifndef os_darwin
ragge
1.220
789                         if (strcmp(argp"-shared") == 0) {
790                                 shared = 1;
791                         } else
792 #endif
793                         if (strcmp(argp"-static") == 0) {
794                                 Bstatic = 1;
ragge
1.225
795                         } else if (match(argp"-symbolic")) {
796                                 strlist_append(&middle_linker_flags,
797                                     "-Bsymbolic");
ragge
1.220
798                         } else if (strncmp(argp"-std"4) == 0) {
799                                 if (strcmp(&argp[5], "gnu99") == 0 ||
800                                     strcmp(&argp[5], "gnu9x") == 0)
801                                         xgnu99 = 1;
802                                 if (strcmp(&argp[5], "gnu89") == 0)
803                                         xgnu89 = 1;
804                         } else
ragge
1.225
805                                 owarning(argp);
ragge
1.220
806                         break;
807                 }
808                 continue;
809
ragge
1.128
810         }
ragge
1.222
811
ragge
1.128
812         /* Sanity checking */
ragge
1.224
813         ninput = 0;
814         STRLIST_FOREACH(s, &inputs)
815                 ninput++;
ragge
1.216
816         if (cppflag) {
ragge
1.224
817                 if (ninput == 0) {
818                         strlist_append(&inputs"-");
819                 } else if (ninput > 2 || (ninput == 2 && outfile)) {
ragge
1.216
820                         errorx(8"too many files");
ragge
1.224
821 #if 0
822                 } else if (ninput == 2) {
ragge
1.216
823                         outfile = clist[--nc];
ragge
1.224
824 #endif
825                 }
ragge
1.216
826         }
ragge
1.225
827         if (ninput == 0)
ragge
1.128
828                 errorx(8"no input files");
ragge
1.224
829         if (outfile && (cflag || sflag || Eflag) && ninput > 1)
ragge
1.128
830                 errorx(8"-o given with -c || -E || -S and more than one file");
ragge
1.224
831 #if 0
ragge
1.128
832         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
833                 errorx(8"output file will be clobbered");
ragge
1.224
834 #endif
ragge
1.223
835
836         if (needM && !Mflag)
837                 errorx(8"to make dependencies needs -M");
838
839
ragge
1.224
840         if (ninput == 0)
ragge
1.128
841                 goto nocom;
842         if (pflag==0) {
843                 if (!sflag)
844                         tmp3 = gettmp();
845                 tmp4 = gettmp();
846         }
847         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
848                 signal(SIGINTidexit);
849         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
850                 signal(SIGTERMidexit);
ragge
1.222
851
ragge
1.223
852         setup_cpp_flags();
853
854         if (isysroot == NULL)
855                 isysroot = sysroot;
856         expand_sysroot();
857
ragge
1.224
858         STRLIST_FOREACH(ss, &inputs) {
ragge
1.128
859                 /*
860                  * C preprocessor
861                  */
ragge
1.225
862                 if (getsuf(ss->value) == 'o' || getsuf(ss->value) == 'a')
863                         continue;
864
ragge
1.224
865                 if (ninput > 1 && !Eflag)
866                         printf("%s:\n"ss->value);
ragge
1.128
867                 onlyas = 0;
868                 assource = tmp3;
ragge
1.224
869                 if (getsuf(ss->value)=='S')
ragge
1.128
870                         ascpp = 1;
ragge
1.224
871                 if (getsuf(ss->value)=='i') {
ragge
1.128
872                         if(Eflag)
873                                 continue;
874                         goto com;
875                 } else if (ascpp) {
876                         onlyas = 1;
ragge
1.224
877                 } else if (xasm || getsuf(ss->value)=='s') {
878                         assource = ss->value;
ragge
1.128
879                         goto assemble;
880                 }
ragge
1.222
881
882
ragge
1.128
883                 if (pflag)
ragge
1.224
884                         tmp4 = setsuf(s->value'i');
ragge
1.128
885                 na = 0;
886                 av[na++] = "cpp";
ragge
1.222
887
ragge
1.223
888                 STRLIST_FOREACH(s, &depflags) {
889                         av[na++] = s->value;
890                 }
ragge
1.218
891                 STRLIST_FOREACH(s, &preprocessor_flags) {
892                         av[na++] = s->value;
893                 }
ragge
1.221
894                 STRLIST_FOREACH(s, &includes) {
895                         av[na++] = "-i";
896                         av[na++] = s->value;
897                 }
ragge
1.219
898                 STRLIST_FOREACH(s, &incdirs) {
ragge
1.221
899                         av[na++] = "-I";
900                         av[na++] = s->value;
901                 }
902                 STRLIST_FOREACH(s, &user_sysincdirs) {
903                         av[na++] = "-S";
ragge
1.219
904                         av[na++] = s->value;
905                 }
gmcgarry
1.153
906                 if (!nostdinc) {
ragge
1.222
907                         STRLIST_FOREACH(s, &sysincdirs) {
908                                 av[na++] = "-S";
909                                 av[na++] = s->value;
910                         }
gmcgarry
1.153
911                 }
ragge
1.223
912                 STRLIST_FOREACH(s, &dirafterdirs) {
913                         av[na++] = "-S";
914                         av[na++] = s->value;
915                 }
ragge
1.222
916
ragge
1.224
917                 av[na++] = ss->value;
ragge
1.128
918                 if (!Eflag && !Mflag)
919                         av[na++] = tmp4;
ragge
1.141
920                 if ((Eflag || Mflag) && outfile)
ragge
1.128
921                          ermfile = av[na++] = outfile;
922                 av[na++]=0;
gmcgarry
1.140
923                 if (callsys(passpav)) {
924                         exfail++;
925                         eflag++;
926                 }
ragge
1.128
927                 if (Eflag || Mflag)
928                         continue;
929                 if (onlyas) {
930                         assource = tmp4;
931                         goto assemble;
932                 }
933
934                 /*
935                  * C compiler
936                  */
937         com:
938                 na = 0;
ragge
1.198
939                 av[na++]= cxxflag ? "c++com" : "ccom";
plunky
1.215
940                 if (Wflag || Wallflag) {
941                         /* -Wall is same as gcc, -WW is all flags */
ragge
1.128
942