Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20120812062352

Diff

Diff from 1.240 to:

Annotations

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

Annotated File View

ragge
1.240
1 /*      $Id: cc.c,v 1.240 2012/08/12 06:23:52 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
ragge
1.167
116 #include "macdefs.h"
ragge
1.218
117
118 #include "xalloc.h"
119 #include "strlist.h"
ragge
1.226
120
121 #include "ccconfig.h"
ragge
1.128
122 /* C command */
123
124 #define MKS(x) _MKS(x)
125 #define _MKS(x) #x
126
ragge
1.226
127 /* default program names in pcc */
128 /* May be overridden if cross-compiler is generated */
129 #ifndef CPPROGNAME
130 #define CPPROGNAME      "cpp"   /* cc used as cpp */
ragge
1.128
131 #endif
132 #ifndef PREPROCESSOR
ragge
1.226
133 #define PREPROCESSOR    "cpp"   /* "real" preprocessor name */
ragge
1.128
134 #endif
135 #ifndef COMPILER
gmcgarry
1.138
136 #define COMPILER        "ccom"
ragge
1.128
137 #endif
ragge
1.226
138 #ifndef CXXCOMPILER
139 #define CXXCOMPILER     "cxxcom"
140 #endif
ragge
1.128
141 #ifndef ASSEMBLER
142 #define ASSEMBLER       "as"
143 #endif
144 #ifndef LINKER
145 #define LINKER          "ld"
146 #endif
ragge
1.226
147 char    *passp = PREPROCESSOR;
148 char    *pass0 = COMPILER;
149 char    *passxx0 = CXXCOMPILER;
150 char    *as = ASSEMBLER;
151 char    *ld = LINKER;
152 char    *sysroot = "", *isysroot;
153
154
155 /* crt files using pcc default names */
156 #ifndef CRTBEGIN_S
157 #define CRTBEGIN_S      "crtbeginS.o"
158 #endif
159 #ifndef CRTEND_S
160 #define CRTEND_S        "crtendS.o"
161 #endif
162 #ifndef CRTBEGIN_T
163 #define CRTBEGIN_T      "crtbeginT.o"
164 #endif
165 #ifndef CRTEND_T
166 #define CRTEND_T        "crtendT.o"
167 #endif
168 #ifndef CRTBEGIN
169 #define CRTBEGIN        "crtbegin.o"
170 #endif
171 #ifndef CRTEND
172 #define CRTEND          "crtend.o"
173 #endif
174 #ifndef CRTI
175 #define CRTI            "crti.o"
176 #endif
177 #ifndef CRTN
178 #define CRTN            "crtn.o"
179 #endif
180 #ifndef CRT0
181 #define CRT0            "crt0.o"
182 #endif
183 #ifndef GCRT0
184 #define GCRT0           "gcrt0.o"
185 #endif
186
187 /* preprocessor stuff */
188 #ifndef STDINC
189 #define STDINC          "/usr/include/"
190 #endif
191
192 char *cppadd[] = CPPADD;
193 char *cppmdadd[] = CPPMDADD;
ragge
1.128
194
ragge
1.226
195 /* Dynamic linker definitions, per-target */
196 #ifndef DYNLINKER
197 #define DYNLINKER { 0 }
ragge
1.174
198 #endif
199
ragge
1.226
200 /* Default libraries and search paths */
201 #ifndef PCCLIBDIR       /* set by autoconf */
202 #define PCCLIBDIR       NULL
203 #endif
204 #ifndef DEFLIBDIRS      /* default library search paths */
205 #define DEFLIBDIRS      { "/usr/lib/", 0 }
206 #endif
207 #ifndef DEFLIBS         /* default libraries included */
208 #define DEFLIBS         { "-lpcc", "-lc", "-lpcc", 0 }
209 #endif
210 #ifndef DEFPROFLIBS     /* default profiling libraries */
211 #define DEFPROFLIBS     { "-lpcc", "-lc_p", "-lpcc", 0 }
212 #endif
213 #ifndef DEFCXXLIBS      /* default c++ libraries */
214 #define DEFCXXLIBS      { "-lp++", "-lpcc", "-lc", "-lpcc", 0 }
215 #endif
216 #ifndef STARTLABEL
217 #define STARTLABEL "__start"
218 #endif
ragge
1.174
219
ragge
1.226
220 char *dynlinker[] = DYNLINKER;
221 char *pcclibdir = PCCLIBDIR;
222 char *deflibdirs[] = DEFLIBDIRS;
223 char *deflibs[] = DEFLIBS;
224 char *defproflibs[] = DEFPROFLIBS;
225 char *defcxxlibs[] = DEFCXXLIBS;
226
ragge
1.233
227 char    *outfile, *MFfile;
ragge
1.220
228 static char **lav;
229 static int lac;
ragge
1.226
230 static char *find_file(const char *filestruct strlist *pathint mode);
231 static int preprocess_input(char *inputchar *outputint dodep);
232 static int compile_input(char *inputchar *output);
233 static int assemble_input(char *inputchar *output);
234 static int run_linker(void);
235 static int strlist_exec(struct strlist *l);
236
plunky
1.185
237 char *cat(const char *, const char *);
gmcgarry
1.150
238 char *setsuf(char *, char);
ragge
1.198
239 int cxxsuf(char *);
ragge
1.128
240 int getsuf(char *);
ragge
1.198
241 char *getsufp(char *s);
ragge
1.128
242 int main(intchar *[]);
243 void error(char *, ...);
244 void errorx(intchar *, ...);
245 int callsys(char [], char *[]);
246 int cunlink(char *);
ragge
1.226
247 void exandrm(char *);
ragge
1.128
248 void dexit(int);
249 void idexit(int);
250 char *gettmp(void);
ragge
1.218
251 void aerror(char *);
ragge
1.219
252 void oerror(char *);
ragge
1.223
253 void owarning(char *);
ragge
1.218
254 char *argnxt(char *, char *);
ragge
1.220
255 char *nxtopt(char *o);
ragge
1.222
256 void setup_cpp_flags(void);
ragge
1.226
257 void setup_ccom_flags(void);
258 void setup_as_flags(void);
259 void setup_ld_flags(void);
ragge
1.222
260 static void expand_sysroot(void);
plunky
1.213
261 #ifdef os_win32
ragge
1.128
262 char *win32pathsubst(char *);
gmcgarry
1.149
263 char *win32commandline(char *, char *[]);
ragge
1.128
264 #endif
265 int     sspflag;
plunky
1.214
266 int     freestanding;
ragge
1.226
267 int     Sflag;
ragge
1.128
268 int     cflag;
269 int     gflag;
gmcgarry
1.145
270 int     rflag;
ragge
1.128
271 int     vflag;
272 int     tflag;
273 int     Eflag;
274 int     Oflag;
275 int     kflag;  /* generate PIC/pic code */
276 #define F_PIC   1
277 #define F_pic   2
ragge
1.233
278 int     MflagneedMMDflag;   /* dependencies only */
ragge
1.128
279 int     pgflag;
280 int     Xflag;
281 int     nostartfilesBstaticshared;
282 int     nostdincnostdlib;
283 int     pthreads;
ragge
1.239
284 int     xgnu89xgnu99;
ragge
1.128
285 int     ascpp;
plunky
1.196
286 #ifdef CHAR_UNSIGNED
plunky
1.209
287 int     xuchar = 1;
plunky
1.196
288 #else
plunky
1.209
289 int     xuchar = 0;
plunky
1.196
290 #endif
ragge
1.198
291 int     cxxflag;
ragge
1.216
292 int     cppflag;
ragge
1.128
293
ragge
1.164
294 #ifdef mach_amd64
295 int amd64_i386;
296 #endif
ragge
1.128
297
ragge
1.218
298 #define match(a,b)      (strcmp(a,b) == 0)
299
ragge
1.128
300 /* handle gcc warning emulations */
301 struct Wflags {
302         char *name;
303         int flags;
304 #define INWALL          1
305 Wflags[] = {
plunky
1.215
306         { "truncate"0 },
307         { "strict-prototypes"0 },
308         { "missing-prototypes"0 },
309         { "implicit-int"INWALL },
310         { "implicit-function-declaration"INWALL },
311         { "shadow"0 },
312         { "pointer-sign"INWALL },
313         { "sign-compare"0 },
314         { "unknown-pragmas"INWALL },
315         { "unreachable-code"0 },
316         { NULL0 },
ragge
1.128
317 };
318
ragge
1.158
319 #ifndef USHORT
320 /* copied from mip/manifest.h */
321 #define USHORT          5
322 #define INT             6
323 #define UNSIGNED        7
324 #endif
325
ragge
1.157
326 /*
327  * Wide char defines.
328  */
329 #if WCHAR_TYPE == USHORT
330 #define WCT "short unsigned int"
331 #define WCM "65535U"
ragge
1.158
332 #if WCHAR_SIZE != 2
333 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
334 #endif
ragge
1.157
335 #elif WCHAR_TYPE == INT
336 #define WCT "int"
337 #define WCM "2147483647"
ragge
1.158
338 #if WCHAR_SIZE != 4
339 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
340 #endif
ragge
1.157
341 #elif WCHAR_TYPE == UNSIGNED
342 #define WCT "unsigned int"
ragge
1.158
343 #define WCM "4294967295U"
344 #if WCHAR_SIZE != 4
345 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
346 #endif
347 #else
348 #error WCHAR_TYPE not defined or invalid
ragge
1.157
349 #endif
350
ragge
1.160
351 #ifdef GCC_COMPAT
352 #ifndef REGISTER_PREFIX
353 #define REGISTER_PREFIX ""
354 #endif
355 #ifndef USER_LABEL_PREFIX
356 #define USER_LABEL_PREFIX ""
357 #endif
358 #endif
359
plunky
1.192
360 #ifndef PCC_WINT_TYPE
361 #define PCC_WINT_TYPE "unsigned int"
362 #endif
363
364 #ifndef PCC_SIZE_TYPE
365 #define PCC_SIZE_TYPE "unsigned long"
366 #endif
367
ragge
1.161
368 #ifndef PCC_PTRDIFF_TYPE
369 #define PCC_PTRDIFF_TYPE "long int"
370 #endif
371
ragge
1.216
372
ragge
1.218
373 struct strlist preprocessor_flags;
ragge
1.223
374 struct strlist depflags;
ragge
1.219
375 struct strlist incdirs;
ragge
1.221
376 struct strlist user_sysincdirs;
377 struct strlist includes;
ragge
1.222
378 struct strlist sysincdirs;
ragge
1.223
379 struct strlist dirafterdirs;
ragge
1.222
380 struct strlist crtdirs;
381 struct strlist libdirs;
382 struct strlist progdirs;
ragge
1.224
383 struct strlist early_linker_flags;
384 struct strlist middle_linker_flags;
385 struct strlist late_linker_flags;
386 struct strlist inputs;
ragge
1.225
387 struct strlist assembler_flags;
388 struct strlist temp_outputs;
ragge
1.226
389 struct strlist compiler_flags;
ragge
1.218
390
ragge
1.128
391 int
392 main(int argcchar *argv[])
393 {
394         struct Wflags *Wf;
ragge
1.226
395         struct string *s;
ragge
1.218
396         char *t, *u, *argp;
ragge
1.239
397         char *msuffix;
ragge
1.226
398         int ninputj;
ragge
1.222
399
ragge
1.220
400         lav = argv;
401         lac = argc;
ragge
1.128
402
ragge
1.222
403         strlist_init(&crtdirs);
404         strlist_init(&libdirs);
405         strlist_init(&progdirs);
ragge
1.218
406         strlist_init(&preprocessor_flags);
ragge
1.219
407         strlist_init(&incdirs);
ragge
1.221
408         strlist_init(&user_sysincdirs);
409         strlist_init(&includes);
ragge
1.222
410         strlist_init(&sysincdirs);
ragge
1.223
411         strlist_init(&dirafterdirs);
412         strlist_init(&depflags);
ragge
1.224
413         strlist_init(&early_linker_flags);
414         strlist_init(&middle_linker_flags);
415         strlist_init(&late_linker_flags);
416         strlist_init(&inputs);
ragge
1.225
417         strlist_init(&assembler_flags);
418         strlist_init(&temp_outputs);
ragge
1.226
419         strlist_init(&compiler_flags);
ragge
1.218
420
ragge
1.216
421         if ((t = strrchr(argv[0], '/')))
ragge
1.226
422                 t++;
ragge
1.229
423         else
424                 t = argv[0];
ragge
1.226
425
426         if (match(t"p++")) {
ragge
1.198
427                 cxxflag = 1;
ragge
1.226
428         } else if (match(t"cpp") || match(tCPPROGNAME)) {
429                 Eflag = cppflag = 1;
ragge
1.198
430         }
431
plunky
1.213
432 #ifdef os_win32
ragge
1.128
433         /* have to prefix path early.  -B may override */
434         incdir = win32pathsubst(incdir);
gmcgarry
1.153
435         altincdir = win32pathsubst(altincdir);
gmcgarry
1.136
436         libdir = win32pathsubst(libdir);
plunky
1.200
437 #ifdef PCCINCDIR
ragge
1.128
438         pccincdir = win32pathsubst(pccincdir);
plunky
1.199
439         pxxincdir = win32pathsubst(pxxincdir);
plunky
1.200
440 #endif
441 #ifdef PCCLIBDIR
ragge
1.128
442         pcclibdir = win32pathsubst(pcclibdir);
plunky
1.200
443 #endif
ragge
1.128
444         passp = win32pathsubst(passp);
445         pass0 = win32pathsubst(pass0);
gmcgarry
1.180
446 #ifdef STARTFILES
447         for (i = 0startfiles[i] != NULLi++)
448                 startfiles[i] = win32pathsubst(startfiles[i]);
449         for (i = 0endfiles[i] != NULLi++)
450                 endfiles[i] = win32pathsubst(endfiles[i]);
451 #endif
452 #ifdef STARTFILES_T
453         for (i = 0startfiles_T[i] != NULLi++)
454                 startfiles_T[i] = win32pathsubst(startfiles_T[i]);
455         for (i = 0endfiles_T[i] != NULLi++)
456                 endfiles_T[i] = win32pathsubst(endfiles_T[i]);
457 #endif
458 #ifdef STARTFILES_S
459         for (i = 0startfiles_S[i] != NULLi++)
460                 startfiles_S[i] = win32pathsubst(startfiles_S[i]);
461         for (i = 0endfiles_S[i] != NULLi++)
462                 endfiles_S[i] = win32pathsubst(endfiles_S[i]);
463 #endif
ragge
1.128
464 #endif
465
ragge
1.220
466         while (--lac) {
467                 ++lav;
468                 argp = *lav;
469
ragge
1.227
470 #ifdef EARLY_ARG_CHECK
471                 EARLY_ARG_CHECK;
472 #endif
473
ragge
1.225
474                 if (*argp != '-' || match(argp"-")) {
475                         /* Check for duplicate .o files. */
476                         if (getsuf(argp) == 'o') {
477                                 j = 0;
478                                 STRLIST_FOREACH(s, &inputs)
479                                         if (match(argps->value))
480                                                 j++;
481                                 if (j)
482                                         continue/* skip it */
483                         }
484                         strlist_append(&inputsargp);
485                         continue;
486                 }
ragge
1.220
487
488                 switch (argp[1]) {
489                 default:
ragge
1.224
490                         owarning(argp);
ragge
1.220
491                         break;
ragge
1.128
492
ragge
1.220
493                 case '-'/* double -'s */
494                         if (match(argp"--version")) {
495                                 printf("%s\n"VERSSTR);
496                                 return 0;
497                         } else if (strncmp(argp"--sysroot="10) == 0) {
498                                 sysroot = argp + 10;
499                         } else if (strcmp(argp"--param") == 0) {
500                                 /* NOTHING YET */;
501                                 (void)nxtopt(0); /* ignore arg */
502                         } else
ragge
1.225
503                                 owarning(argp);
ragge
1.220
504                         break;
ragge
1.128
505
ragge
1.220
506                 case 'B'/* other search paths for binaries */
ragge
1.224
507                         t = nxtopt("-B");
508                         strlist_append(&crtdirst);
509                         strlist_append(&libdirst);
510                         strlist_append(&progdirst);
ragge
1.220
511                         break;
ragge
1.128
512
ragge
1.220
513                 case 'C':
514                         if (match(argp"-C") || match(argp"-CC"))
515                                 strlist_append(&preprocessor_flagsargp);
516                         else
517                                 oerror(argp);
518                         break;
ragge
1.218
519
ragge
1.220
520                 case 'X':
521                         Xflag++;
522                         break;
523                 case 'W'/* Ignore (most of) W-flags */
ragge
1.225
524                         if ((t = argnxt(argp"-Wl,"))) {
525                                 u = strtok(t",");
526                                 do {
ragge
1.235
527                                         strlist_append(&inputsu);
ragge
1.225
528                                 } while ((u = strtok(NULL",")) != NULL);
529                         } else if ((t = argnxt(argp"-Wa,"))) {
530                                 u = strtok(t",");
531                                 do {
532                                         strlist_append(&assembler_flagsu);
533                                 } while ((u = strtok(NULL",")) != NULL);
ragge
1.226
534                         } else if ((t = argnxt(argp"-Wc,"))) {
535                                 u = strtok(t",");
536                                 do {
537                                         strlist_append(&compiler_flagsu);
538                                 } while ((u = strtok(NULL",")) != NULL);
ragge
1.220
539                         } else if ((t = argnxt(argp"-Wp,"))) {
540                                 u = strtok(t",");
541                                 do {
542                                         strlist_append(&preprocessor_flagsu);
543                                 } while ((u = strtok(NULL",")) != NULL);
544                         } else if (strcmp(argp"-Werror") == 0) {
ragge
1.226
545                                 strlist_append(&compiler_flags"-Werror");
ragge
1.220
546                         } else if (strcmp(argp"-Wall") == 0) {
ragge
1.238
547                                 for (Wf = WflagsWf->nameWf++)
548                                         if (Wf->flags & INWALL)
549                                                 strlist_append(&compiler_flags,
550                                                     cat("-W"Wf->name));
ragge
1.220
551                         } else if (strcmp(argp"-WW") == 0) {
ragge
1.238
552                                 for (Wf = WflagsWf->nameWf++)
553                                         strlist_append(&compiler_flags,
554                                             cat("-W"Wf->name));
ragge
1.220
555                         } else {
556                                 /* pass through, if supported */
557                                 t = &argp[2];
558                                 if (strncmp(t"no-"3) == 0)
559                                         t += 3;
560                                 if (strncmp(t"error="6) == 0)
561                                         t += 6;
562                                 for (Wf = WflagsWf->nameWf++) {
563                                         if (strcmp(tWf->name) == 0)
ragge
1.226
564                                                 strlist_append(&compiler_flags,
565                                                     argp);
ragge
1.128
566                                 }
ragge
1.220
567                         }
568                         break;
ragge
1.128
569
ragge
1.220
570                 case 'f'/* GCC compatibility flags */
571                         if (strcmp(argp"-fPIC") == 0)
572                                 kflag = F_PIC;
573                         else if (strcmp(argp"-fpic") == 0)
574                                 kflag = F_pic;
575                         else if (strcmp(argp"-ffreestanding") == 0)
576                                 freestanding = 1;
ragge
1.221
577                         else if (match(argp"-fsigned-char") ||
578                             match(argp"-fno-unsigned-char"))
ragge
1.220
579                                 xuchar = 0;
ragge
1.221
580                         else if (match(argp"-fno-signed-char") ||
581                             match(argp"-funsigned-char"))
ragge
1.220
582                                 xuchar = 1;
ragge
1.226
583                         else if (match(argp"-fstack-protector") ||
584                             match(argp"-fstack-protector-all")) {
ragge
1.220
585                                 sspflag++;
ragge
1.226
586                         } else if (match(argp"-fno-stack-protector") ||
587                             match(argp"-fno-stack-protector-all")) {
ragge
1.220
588                                 sspflag = 0;
589                         }
590                         /* silently ignore the rest */
591                         break;
ragge
1.128
592
ragge
1.220
593                 case 'g'/* create debug output */
594                         if (argp[2] == '0')
595                                 gflag = 0;
596                         else
597                                 gflag++;
598                         break;
ragge
1.128
599
ragge
1.220
600                 case 'D':
601                 case 'U':
ragge
1.221
602                         strlist_append(&preprocessor_flagsargp);
ragge
1.220
603                         if (argp[2] != 0)
ragge
1.219
604                                 break;
ragge
1.221
605                         strlist_append(&preprocessor_flagsnxtopt(argp));
606                         break;
607
608                 case 'I'/* Add include dirs */
609                         strlist_append(&incdirsnxtopt("-I"));
ragge
1.220
610                         break;
ragge
1.219
611
ragge
1.220
612                 case 'i':
ragge
1.221
613                         if (match(argp"-isystem")) {
614                                 strlist_append(&user_sysincdirsnxtopt(0));
615                         } else if (match(argp"-include")) {
616                                 strlist_append(&includesnxtopt(0));
ragge
1.222
617                         } else if (match(argp"-isysroot")) {
618                                 isysroot = nxtopt(0);
ragge
1.220
619                         } else if (strcmp(argp"-idirafter") == 0) {
ragge
1.223
620                                 strlist_append(&dirafterdirsnxtopt(0));
ragge
1.220
621                         } else
ragge
1.225
622                                 owarning(argp);
ragge
1.220
623                         break;
ragge
1.128
624
ragge
1.220
625                 case 'k'/* generate PIC code */
626                         kflag = F_pic;
627                         break;
ragge
1.128
628
ragge
1.224
629                 case 'l':
630                 case 'L':
631                         strlist_append(&late_linker_flagsargp);
ragge
1.226
632                         if (argp[2] == 0)
633                                 strlist_append(&late_linker_flagsnxtopt(0));
ragge
1.224
634                         break;
635
636
ragge
1.220
637                 case 'm'/* target-dependent options */
ragge
1.164
638 #ifdef mach_amd64
ragge
1.220
639                         /* need to call i386 ccom for this */
640                         if (strcmp(argp"-m32") == 0) {
641                                 pass0 = LIBEXECDIR "/ccom_i386";
642                                 amd64_i386 = 1;
643                                 break;
644                         }
ragge
1.164
645 #endif
ragge
1.225
646                         strlist_append(&middle_linker_flagsargp);
ragge
1.220
647                         if (argp[2] == 0) {
ragge
1.225
648                                 t = nxtopt(0);
649                                 strlist_append(&middle_linker_flagst);
ragge
1.220
650                         }
651                         break;
ragge
1.128
652
ragge
1.220
653                 case 'n'/* handle -n flags */
654                         if (strcmp(argp"-nostdinc") == 0)
655                                 nostdinc++;
656                         else if (strcmp(argp"-nostdlib") == 0) {
657                                 nostdlib++;
658                                 nostartfiles++;
659                         } else if (strcmp(argp"-nostartfiles") == 0)
660                                 nostartfiles = 1;
661                         else if (strcmp(argp"-nodefaultlibs") == 0)
662                                 nostdlib++;
663                         else
ragge
1.225
664                                 owarning(argp);
ragge
1.220
665                         break;
ragge
1.128
666
ragge
1.220
667                 case 'p':
668                         if (strcmp(argp"-pg") == 0 ||
669                             strcmp(argp"-p") == 0)
670                                 pgflag++;
671                         else if (strcmp(argp"-pthread") == 0)
672                                 pthreads++;
673                         else if (strcmp(argp"-pipe") == 0)
674                                 /* NOTHING YET */;
675                         else if (strcmp(argp"-pedantic") == 0)
676                                 /* NOTHING YET */;
677                         else if (strcmp(argp,
678                             "-print-prog-name=ld") == 0) {
679                                 printf("%s\n"LINKER);
680                                 return 0;
ragge
1.226
681 #ifdef notdef
682                         /* does not exist in gcc??? */
ragge
1.220
683                         } else if (strcmp(argp,
684                             "-print-multi-os-directory") == 0) {
685                                 printf("%s\n"MULTIOSDIR);
686                                 return 0;
ragge
1.226
687 #endif
ragge
1.220
688                         } else
689                                 oerror(argp);
690                         break;
ragge
1.128
691
ragge
1.220
692                 case 'r':
693                         rflag = 1;
694                         break;
gmcgarry
1.145
695
ragge
1.220
696                 case 'x':
697                         t = nxtopt("-x");
698                         if (match(t"c"))
ragge
1.239
699                                 strlist_append(&inputs")c");
ragge
1.220
700                         else if (match(t"assembler"))
ragge
1.239
701                                 strlist_append(&inputs")s");
ragge
1.220
702                         else if (match(t"assembler-with-cpp"))
ragge
1.239
703                                 strlist_append(&inputs")S");
ragge
1.220
704                         else if (match(t"c++"))
ragge
1.239
705                                 strlist_append(&inputs")c++");
ragge
1.226
706                         else {
707                                 strlist_append(&compiler_flags"-x");
708                                 strlist_append(&compiler_flagst);
709                         }
ragge
1.220
710                         break;
ragge
1.240
711                 case 'T':
712                         strlist_append(&inputsargp);
713                         break;
ragge
1.220
714                 case 't':
715                         tflag++;
716                         break;
717                 case 'S':
ragge
1.226
718                         Sflag++;
ragge
1.220
719                         cflag++;
720                         break;
721                 case 'o':
722                         if (outfile)
723                                 errorx(8"too many -o");
724                         outfile = nxtopt("-o");
725                         break;
726                 case 'O':
727                         if (argp[2] == '\0')
728                                 Oflag++;
ragge
1.226
729                         else if (argp[3] == '\0' &&
730                             isdigit((unsigned char)argp[2]))
ragge
1.220
731                                 Oflag = argp[2] - '0';
732                         else if (argp[3] == '\0' && argp[2] == 's')
733                                 Oflag = 1;      /* optimize for space only */
734                         else
735                                 oerror(argp);
736                         break;
737                 case 'E':
738                         Eflag++;
739                         break;
740                 case 'P':
ragge
1.221
741                         strlist_append(&preprocessor_flagsargp);
742                         break;
743
ragge
1.220
744                 case 'c':
ragge
1.227
745                         cflag++;
ragge
1.220
746                         break;
ragge
1.223
747
ragge
1.220
748                 case 'M':
ragge
1.233
749                         needM = 1;
750                         if (match(argp"-M")) {
ragge
1.223
751                                 Mflag++;
752                                 strlist_append(&depflagsargp);
ragge
1.233
753                         } else if (match(argp"-MP")) {
ragge
1.223
754                                 strlist_append(&depflags"-xMP");
ragge
1.233
755                         } else if (match(argp"-MF")) {
756                                 MFfile = nxtopt("-MF");
757                         } else if (match(argp"-MT") || match(argp"-MQ")) {
ragge
1.226
758                                 t = cat("-xMT,"nxtopt("-MT"));
ragge
1.220
759                                 t[3] = argp[2];
ragge
1.223
760                                 strlist_append(&depflagst);
ragge
1.233
761                         } else if (match(argp"-MD")) {
762                                 MDflag++;
763                                 needM = 0;
764                                 strlist_append(&depflags"-M");
765                         } else
ragge
1.220
766                                 oerror(argp);
767                         break;
ragge
1.128
768
ragge
1.220
769                 case 'd':
ragge
1.227
770                         owarning(argp);
ragge
1.220
771                         break;
772                 case 'v':
773                         printf("%s\n"VERSSTR);
774                         vflag++;
775                         break;
ragge
1.128
776
ragge
1.220
777                 case 's':
ragge
1.235
778                         if (match(argp"-shared")) {
ragge
1.220
779                                 shared = 1;
ragge
1.235
780                         } else if (match(argp"-static")) {
ragge
1.220
781                                 Bstatic = 1;
ragge
1.225
782                         } else if (match(argp"-symbolic")) {
783                                 strlist_append(&middle_linker_flags,
784                                     "-Bsymbolic");
ragge
1.220
785                         } else if (strncmp(argp"-std"4) == 0) {
786                                 if (strcmp(&argp[5], "gnu99") == 0 ||
787                                     strcmp(&argp[5], "gnu9x") == 0)
788                                         xgnu99 = 1;
789                                 if (strcmp(&argp[5], "gnu89") == 0)
790                                         xgnu89 = 1;
791                         } else
ragge
1.225
792                                 owarning(argp);
ragge
1.220
793                         break;
794                 }
795                 continue;
796
ragge
1.128
797         }
ragge
1.222
798
ragge
1.128
799         /* Sanity checking */
ragge
1.224
800         ninput = 0;
801         STRLIST_FOREACH(s, &inputs)
802                 ninput++;
ragge
1.216
803         if (cppflag) {
ragge
1.224
804                 if (ninput == 0) {
805                         strlist_append(&inputs"-");
ragge
1.232
806                         ninput++;
ragge
1.224
807                 } else if (ninput > 2 || (ninput == 2 && outfile)) {
ragge
1.216
808                         errorx(8"too many files");
ragge
1.232
809                 } else if (ninput == 2) {
810                         outfile = STRLIST_NEXT(STRLIST_FIRST(&inputs))->value;
811                         STRLIST_FIRST(&inputs)->next = NULL;
812                         ninput--;
ragge
1.224
813                 }
ragge
1.216
814         }
ragge
1.225
815         if (ninput == 0)
ragge
1.128
816                 errorx(8"no input files");
ragge
1.226
817         if (outfile && (cflag || Sflag || Eflag) && ninput > 1)
ragge
1.128
818                 errorx(8"-o given with -c || -E || -S and more than one file");
ragge
1.224
819 #if 0
ragge
1.128
820         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
821                 errorx(8"output file will be clobbered");
ragge
1.224
822 #endif
ragge
1.223
823
ragge
1.233
824         if (needM && !Mflag && !MDflag)
ragge
1.223
825                 errorx(8"to make dependencies needs -M");
826
827
ragge
1.128
828         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
829                 signal(SIGINTidexit);
830         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
831                 signal(SIGTERMidexit);
ragge
1.222
832
ragge
1.226
833         /* after arg parsing */
834         strlist_append(&progdirsLIBEXECDIR);
835         if (pcclibdir)
836                 strlist_append(&crtdirspcclibdir);
837         for (j = 0deflibdirs[j]; j++)
838                 strlist_append(&crtdirsdeflibdirs[j]);
839
ragge
1.223
840         setup_cpp_flags();
ragge
1.226
841         setup_ccom_flags();
842         setup_as_flags();
ragge
1.223
843
844         if (isysroot == NULL)
845                 isysroot = sysroot;
846         expand_sysroot();
847
ragge
1.239
848         msuffix = NULL;
ragge
1.226
849         STRLIST_FOREACH(s, &inputs) {
850                 char *suffix;
851                 char *ifile, *ofile;
852
853                 ifile = s->value;
854                 if (ninput > 1 && !Eflag)
855                         printf("%s:\n"ifile);
856
ragge
1.239
857                 if (ifile[0] == ')') {
858                         msuffix = &ifile[1]; /* -x source type given */
859                         ascpp = match(msuffix"S");
860                         continue;
861                 }
862                 if (ifile[0] == '-' && ifile[1] != 0)
863                         suffix = "o"/* source files cannot begin with - */
864                 else if (msuffix)
865                         suffix = msuffix;
866                 else
867                         suffix = getsufp(ifile);
ragge
1.128
868                 /*
869                  * C preprocessor
870                  */
ragge
1.226
871                 if (match(suffix"c") || match(suffix"S") ||
872                     cxxsuf(s->value)) {
873                         /* find out next output file */
ragge
1.233
874                         if (Mflag || MDflag) {
875                                 char *Mofile;
876
877                                 if (MFfile)
878                                         Mofile = MFfile;
879                                 else if (outfile)
880                                         Mofile = setsuf(outfile'd');
881                                 else
882                                         Mofile = setsuf(ifile'd');
883                                 if (preprocess_input(ifileMofile1))
884                                         exandrm(Mofile);
885                         }
886                         if (Mflag)
887                                 continue;
888                         if (Eflag) {
ragge
1.226
889                                 /* last pass */
890                                 ofile = outfile;
891                         } else {
892                                 /* to temp file */
893                                 strlist_append(&temp_outputsofile = gettmp());
894                         }
895                         if (preprocess_input(ifileofile0))
896                                 exandrm(ofile);
897                         if (Eflag)
ragge
1.128
898                                 continue;
ragge
1.226
899                         ifile = ofile;
900                         suffix = match(suffix"S") ? "s" : "i";
ragge
1.128
901                 }
902
903                 /*
904                  * C compiler
905                  */
ragge
1.226
906                 if (match(suffix"i")) {
907                         /* find out next output file */
908                         if (Sflag) {
909                                 ofile = outfile;
910                                 if (outfile == NULL)
911                                         ofile = setsuf(s->value's');
912                         } else
913                                 strlist_append(&temp_outputsofile = gettmp());
914                         if (compile_input(ifileofile))
915                                 exandrm(ofile);
916                         if (Sflag)
917                                 continue;
918                         ifile = ofile;
919                         suffix = "s";
ragge
1.128
920                 }
921
922                 /*
923                  * Assembler
924                  */
ragge
1.226
925                 if (match(suffix"s")) {
926                         if (cflag) {
927                                 ofile = outfile;
928                                 if (ofile == NULL)
929