Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20120809130725

Diff

Diff from 1.238 to:

Annotations

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

Annotated File View

ragge
1.238
1 /*      $Id: cc.c,v 1.238 2012/08/09 13:07:25 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;
plunky
1.217
284 int     xasmxcflagxgnu89xgnu99;
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.226
397         int ninputj;
ragge
1.222
398
ragge
1.220
399         lav = argv;
400         lac = argc;
ragge
1.128
401
ragge
1.222
402         strlist_init(&crtdirs);
403         strlist_init(&libdirs);
404         strlist_init(&progdirs);
ragge
1.218
405         strlist_init(&preprocessor_flags);
ragge
1.219
406         strlist_init(&incdirs);
ragge
1.221
407         strlist_init(&user_sysincdirs);
408         strlist_init(&includes);
ragge
1.222
409         strlist_init(&sysincdirs);
ragge
1.223
410         strlist_init(&dirafterdirs);
411         strlist_init(&depflags);
ragge
1.224
412         strlist_init(&early_linker_flags);
413         strlist_init(&middle_linker_flags);
414         strlist_init(&late_linker_flags);
415         strlist_init(&inputs);
ragge
1.225
416         strlist_init(&assembler_flags);
417         strlist_init(&temp_outputs);
ragge
1.226
418         strlist_init(&compiler_flags);
ragge
1.218
419
ragge
1.216
420         if ((t = strrchr(argv[0], '/')))
ragge
1.226
421                 t++;
ragge
1.229
422         else
423                 t = argv[0];
ragge
1.226
424
425         if (match(t"p++")) {
ragge
1.198
426                 cxxflag = 1;
ragge
1.226
427         } else if (match(t"cpp") || match(tCPPROGNAME)) {
428                 Eflag = cppflag = 1;
ragge
1.198
429         }
430
plunky
1.213
431 #ifdef os_win32
ragge
1.128
432         /* have to prefix path early.  -B may override */
433         incdir = win32pathsubst(incdir);
gmcgarry
1.153
434         altincdir = win32pathsubst(altincdir);
gmcgarry
1.136
435         libdir = win32pathsubst(libdir);
plunky
1.200
436 #ifdef PCCINCDIR
ragge
1.128
437         pccincdir = win32pathsubst(pccincdir);
plunky
1.199
438         pxxincdir = win32pathsubst(pxxincdir);
plunky
1.200
439 #endif
440 #ifdef PCCLIBDIR
ragge
1.128
441         pcclibdir = win32pathsubst(pcclibdir);
plunky
1.200
442 #endif
ragge
1.128
443         passp = win32pathsubst(passp);
444         pass0 = win32pathsubst(pass0);
gmcgarry
1.180
445 #ifdef STARTFILES
446         for (i = 0startfiles[i] != NULLi++)
447                 startfiles[i] = win32pathsubst(startfiles[i]);
448         for (i = 0endfiles[i] != NULLi++)
449                 endfiles[i] = win32pathsubst(endfiles[i]);
450 #endif
451 #ifdef STARTFILES_T
452         for (i = 0startfiles_T[i] != NULLi++)
453                 startfiles_T[i] = win32pathsubst(startfiles_T[i]);
454         for (i = 0endfiles_T[i] != NULLi++)
455                 endfiles_T[i] = win32pathsubst(endfiles_T[i]);
456 #endif
457 #ifdef STARTFILES_S
458         for (i = 0startfiles_S[i] != NULLi++)
459                 startfiles_S[i] = win32pathsubst(startfiles_S[i]);
460         for (i = 0endfiles_S[i] != NULLi++)
461                 endfiles_S[i] = win32pathsubst(endfiles_S[i]);
462 #endif
ragge
1.128
463 #endif
464
ragge
1.220
465         while (--lac) {
466                 ++lav;
467                 argp = *lav;
468
ragge
1.227
469 #ifdef EARLY_ARG_CHECK
470                 EARLY_ARG_CHECK;
471 #endif
472
ragge
1.225
473                 if (*argp != '-' || match(argp"-")) {
474                         /* Check for duplicate .o files. */
475                         if (getsuf(argp) == 'o') {
476                                 j = 0;
477                                 STRLIST_FOREACH(s, &inputs)
478                                         if (match(argps->value))
479                                                 j++;
480                                 if (j)
481                                         continue/* skip it */
482                         }
483                         strlist_append(&inputsargp);
484                         continue;
485                 }
ragge
1.220
486
487                 switch (argp[1]) {
488                 default:
ragge
1.224
489                         owarning(argp);
ragge
1.220
490                         break;
ragge
1.128
491
ragge
1.220
492                 case '-'/* double -'s */
493                         if (match(argp"--version")) {
494                                 printf("%s\n"VERSSTR);
495                                 return 0;
496                         } else if (strncmp(argp"--sysroot="10) == 0) {
497                                 sysroot = argp + 10;
498                         } else if (strcmp(argp"--param") == 0) {
499                                 /* NOTHING YET */;
500                                 (void)nxtopt(0); /* ignore arg */
501                         } else
ragge
1.225
502                                 owarning(argp);
ragge
1.220
503                         break;
ragge
1.128
504
ragge
1.220
505                 case 'B'/* other search paths for binaries */
ragge
1.224
506                         t = nxtopt("-B");
507                         strlist_append(&crtdirst);
508                         strlist_append(&libdirst);
509                         strlist_append(&progdirst);
ragge
1.220
510                         break;
ragge
1.128
511
ragge
1.220
512                 case 'C':
513                         if (match(argp"-C") || match(argp"-CC"))
514                                 strlist_append(&preprocessor_flagsargp);
515                         else
516                                 oerror(argp);
517                         break;
ragge
1.218
518
ragge
1.220
519                 case 'X':
520                         Xflag++;
521                         break;
522                 case 'W'/* Ignore (most of) W-flags */
ragge
1.225
523                         if ((t = argnxt(argp"-Wl,"))) {
524                                 u = strtok(t",");
525                                 do {
ragge
1.235
526                                         strlist_append(&inputsu);
ragge
1.225
527                                 } while ((u = strtok(NULL",")) != NULL);
528                         } else if ((t = argnxt(argp"-Wa,"))) {
529                                 u = strtok(t",");
530                                 do {
531                                         strlist_append(&assembler_flagsu);
532                                 } while ((u = strtok(NULL",")) != NULL);
ragge
1.226
533                         } else if ((t = argnxt(argp"-Wc,"))) {
534                                 u = strtok(t",");
535                                 do {
536                                         strlist_append(&compiler_flagsu);
537                                 } while ((u = strtok(NULL",")) != NULL);
ragge
1.220
538                         } else if ((t = argnxt(argp"-Wp,"))) {
539                                 u = strtok(t",");
540                                 do {
541                                         strlist_append(&preprocessor_flagsu);
542                                 } while ((u = strtok(NULL",")) != NULL);
543                         } else if (strcmp(argp"-Werror") == 0) {
ragge
1.226
544                                 strlist_append(&compiler_flags"-Werror");
ragge
1.220
545                         } else if (strcmp(argp"-Wall") == 0) {
ragge
1.238
546                                 for (Wf = WflagsWf->nameWf++)
547                                         if (Wf->flags & INWALL)
548                                                 strlist_append(&compiler_flags,
549                                                     cat("-W"Wf->name));
ragge
1.220
550                         } else if (strcmp(argp"-WW") == 0) {
ragge
1.238
551                                 for (Wf = WflagsWf->nameWf++)
552                                         strlist_append(&compiler_flags,
553                                             cat("-W"Wf->name));
ragge
1.220
554                         } else {
555                                 /* pass through, if supported */
556                                 t = &argp[2];
557                                 if (strncmp(t"no-"3) == 0)
558                                         t += 3;
559                                 if (strncmp(t"error="6) == 0)
560                                         t += 6;
561                                 for (Wf = WflagsWf->nameWf++) {
562                                         if (strcmp(tWf->name) == 0)
ragge
1.226
563                                                 strlist_append(&compiler_flags,
564                                                     argp);
ragge
1.128
565                                 }
ragge
1.220
566                         }
567                         break;
ragge
1.128
568
ragge
1.220
569                 case 'f'/* GCC compatibility flags */
570                         if (strcmp(argp"-fPIC") == 0)
571                                 kflag = F_PIC;
572                         else if (strcmp(argp"-fpic") == 0)
573                                 kflag = F_pic;
574                         else if (strcmp(argp"-ffreestanding") == 0)
575                                 freestanding = 1;
ragge
1.221
576                         else if (match(argp"-fsigned-char") ||
577                             match(argp"-fno-unsigned-char"))
ragge
1.220
578                                 xuchar = 0;
ragge
1.221
579                         else if (match(argp"-fno-signed-char") ||
580                             match(argp"-funsigned-char"))
ragge
1.220
581                                 xuchar = 1;
ragge
1.226
582                         else if (match(argp"-fstack-protector") ||
583                             match(argp"-fstack-protector-all")) {
ragge
1.220
584                                 sspflag++;
ragge
1.226
585                         } else if (match(argp"-fno-stack-protector") ||
586                             match(argp"-fno-stack-protector-all")) {
ragge
1.220
587                                 sspflag = 0;
588                         }
589                         /* silently ignore the rest */
590                         break;
ragge
1.128
591
ragge
1.220
592                 case 'g'/* create debug output */
593                         if (argp[2] == '0')
594                                 gflag = 0;
595                         else
596                                 gflag++;
597                         break;
ragge
1.128
598
ragge
1.220
599                 case 'D':
600                 case 'U':
ragge
1.221
601                         strlist_append(&preprocessor_flagsargp);
ragge
1.220
602                         if (argp[2] != 0)
ragge
1.219
603                                 break;
ragge
1.221
604                         strlist_append(&preprocessor_flagsnxtopt(argp));
605                         break;
606
607                 case 'I'/* Add include dirs */
608                         strlist_append(&incdirsnxtopt("-I"));
ragge
1.220
609                         break;
ragge
1.219
610
ragge
1.220
611                 case 'i':
ragge
1.221
612                         if (match(argp"-isystem")) {
613                                 strlist_append(&user_sysincdirsnxtopt(0));
614                         } else if (match(argp"-include")) {
615                                 strlist_append(&includesnxtopt(0));
ragge
1.222
616                         } else if (match(argp"-isysroot")) {
617                                 isysroot = nxtopt(0);
ragge
1.220
618                         } else if (strcmp(argp"-idirafter") == 0) {
ragge
1.223
619                                 strlist_append(&dirafterdirsnxtopt(0));
ragge
1.220
620                         } else
ragge
1.225
621                                 owarning(argp);
ragge
1.220
622                         break;
ragge
1.128
623
ragge
1.220
624                 case 'k'/* generate PIC code */
625                         kflag = F_pic;
626                         break;
ragge
1.128
627
ragge
1.224
628                 case 'l':
629                 case 'L':
630                         strlist_append(&late_linker_flagsargp);
ragge
1.226
631                         if (argp[2] == 0)
632                                 strlist_append(&late_linker_flagsnxtopt(0));
ragge
1.224
633                         break;
634
635
ragge
1.220
636                 case 'm'/* target-dependent options */
ragge
1.164
637 #ifdef mach_amd64
ragge
1.220
638                         /* need to call i386 ccom for this */
639                         if (strcmp(argp"-m32") == 0) {
640                                 pass0 = LIBEXECDIR "/ccom_i386";
641                                 amd64_i386 = 1;
642                                 break;
643                         }
ragge
1.164
644 #endif
ragge
1.225
645                         strlist_append(&middle_linker_flagsargp);
ragge
1.220
646                         if (argp[2] == 0) {
ragge
1.225
647                                 t = nxtopt(0);
648                                 strlist_append(&middle_linker_flagst);
ragge
1.220
649                         }
650                         break;
ragge
1.128
651
ragge
1.220
652                 case 'n'/* handle -n flags */
653                         if (strcmp(argp"-nostdinc") == 0)
654                                 nostdinc++;
655                         else if (strcmp(argp"-nostdlib") == 0) {
656                                 nostdlib++;
657                                 nostartfiles++;
658                         } else if (strcmp(argp"-nostartfiles") == 0)
659                                 nostartfiles = 1;
660                         else if (strcmp(argp"-nodefaultlibs") == 0)
661                                 nostdlib++;
662                         else
ragge
1.225
663                                 owarning(argp);
ragge
1.220
664                         break;
ragge
1.128
665
ragge
1.220
666                 case 'p':
667                         if (strcmp(argp"-pg") == 0 ||
668                             strcmp(argp"-p") == 0)
669                                 pgflag++;
670                         else if (strcmp(argp"-pthread") == 0)
671                                 pthreads++;
672                         else if (strcmp(argp"-pipe") == 0)
673                                 /* NOTHING YET */;
674                         else if (strcmp(argp"-pedantic") == 0)
675                                 /* NOTHING YET */;
676                         else if (strcmp(argp,
677                             "-print-prog-name=ld") == 0) {
678                                 printf("%s\n"LINKER);
679                                 return 0;
ragge
1.226
680 #ifdef notdef
681                         /* does not exist in gcc??? */
ragge
1.220
682                         } else if (strcmp(argp,
683                             "-print-multi-os-directory") == 0) {
684                                 printf("%s\n"MULTIOSDIR);
685                                 return 0;
ragge
1.226
686 #endif
ragge
1.220
687                         } else
688                                 oerror(argp);
689                         break;
ragge
1.128
690
ragge
1.220
691                 case 'r':
692                         rflag = 1;
693                         break;
gmcgarry
1.145
694
ragge
1.220
695                 case 'x':
696                         t = nxtopt("-x");
697                         if (match(t"c"))
698                                 xcflag = 1/* default */
699                         else if (match(t"assembler"))
700                                 xasm = 1;
701                         else if (match(t"assembler-with-cpp"))
702                                 ascpp = 1;
703                         else if (match(t"c++"))
704                                 cxxflag++;
ragge
1.226
705                         else {
706                                 strlist_append(&compiler_flags"-x");
707                                 strlist_append(&compiler_flagst);
708                         }
ragge
1.220
709                         break;
710                 case 't':
711                         tflag++;
712                         break;
713                 case 'S':
ragge
1.226
714                         Sflag++;
ragge
1.220
715                         cflag++;
716                         break;
717                 case 'o':
718                         if (outfile)
719                                 errorx(8"too many -o");
720                         outfile = nxtopt("-o");
721                         break;
722                 case 'O':
723                         if (argp[2] == '\0')
724                                 Oflag++;
ragge
1.226
725                         else if (argp[3] == '\0' &&
726                             isdigit((unsigned char)argp[2]))
ragge
1.220
727                                 Oflag = argp[2] - '0';
728                         else if (argp[3] == '\0' && argp[2] == 's')
729                                 Oflag = 1;      /* optimize for space only */
730                         else
731                                 oerror(argp);
732                         break;
733                 case 'E':
734                         Eflag++;
735                         break;
736                 case 'P':
ragge
1.221
737                         strlist_append(&preprocessor_flagsargp);
738                         break;
739
ragge
1.220
740                 case 'c':
ragge
1.227
741                         cflag++;
ragge
1.220
742                         break;
ragge
1.223
743
ragge
1.220
744                 case 'M':
ragge
1.233
745                         needM = 1;
746                         if (match(argp"-M")) {
ragge
1.223
747                                 Mflag++;
748                                 strlist_append(&depflagsargp);
ragge
1.233
749                         } else if (match(argp"-MP")) {
ragge
1.223
750                                 strlist_append(&depflags"-xMP");
ragge
1.233
751                         } else if (match(argp"-MF")) {
752                                 MFfile = nxtopt("-MF");
753                         } else if (match(argp"-MT") || match(argp"-MQ")) {
ragge
1.226
754                                 t = cat("-xMT,"nxtopt("-MT"));
ragge
1.220
755                                 t[3] = argp[2];
ragge
1.223
756                                 strlist_append(&depflagst);
ragge
1.233
757                         } else if (match(argp"-MD")) {
758                                 MDflag++;
759                                 needM = 0;
760                                 strlist_append(&depflags"-M");
761                         } else
ragge
1.220
762                                 oerror(argp);
763                         break;
ragge
1.128
764
ragge
1.220
765                 case 'd':
ragge
1.227
766                         owarning(argp);
ragge
1.220
767                         break;
768                 case 'v':
769                         printf("%s\n"VERSSTR);
770                         vflag++;
771                         break;
ragge
1.128
772
ragge
1.220
773                 case 's':
ragge
1.235
774                         if (match(argp"-shared")) {
ragge
1.220
775                                 shared = 1;
ragge
1.235
776                         } else if (match(argp"-static")) {
ragge
1.220
777                                 Bstatic = 1;
ragge
1.225
778                         } else if (match(argp"-symbolic")) {
779                                 strlist_append(&middle_linker_flags,
780                                     "-Bsymbolic");
ragge
1.220
781                         } else if (strncmp(argp"-std"4) == 0) {
782                                 if (strcmp(&argp[5], "gnu99") == 0 ||
783                                     strcmp(&argp[5], "gnu9x") == 0)
784                                         xgnu99 = 1;
785                                 if (strcmp(&argp[5], "gnu89") == 0)
786                                         xgnu89 = 1;
787                         } else
ragge
1.225
788                                 owarning(argp);
ragge
1.220
789                         break;
790                 }
791                 continue;
792
ragge
1.128
793         }
ragge
1.222
794
ragge
1.128
795         /* Sanity checking */
ragge
1.224
796         ninput = 0;
797         STRLIST_FOREACH(s, &inputs)
798                 ninput++;
ragge
1.216
799         if (cppflag) {
ragge
1.224
800                 if (ninput == 0) {
801                         strlist_append(&inputs"-");
ragge
1.232
802                         ninput++;
803                         xcflag++;
ragge
1.224
804                 } else if (ninput > 2 || (ninput == 2 && outfile)) {
ragge
1.216
805                         errorx(8"too many files");
ragge
1.232
806                 } else if (ninput == 2) {
807                         outfile = STRLIST_NEXT(STRLIST_FIRST(&inputs))->value;
808                         STRLIST_FIRST(&inputs)->next = NULL;
809                         ninput--;
ragge
1.224
810                 }
ragge
1.216
811         }
ragge
1.225
812         if (ninput == 0)
ragge
1.128
813                 errorx(8"no input files");
ragge
1.226
814         if (outfile && (cflag || Sflag || Eflag) && ninput > 1)
ragge
1.128
815                 errorx(8"-o given with -c || -E || -S and more than one file");
ragge
1.224
816 #if 0
ragge
1.128
817         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
818                 errorx(8"output file will be clobbered");
ragge
1.224
819 #endif
ragge
1.223
820
ragge
1.233
821         if (needM && !Mflag && !MDflag)
ragge
1.223
822                 errorx(8"to make dependencies needs -M");
823
824
ragge
1.128
825         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
826                 signal(SIGINTidexit);
827         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
828                 signal(SIGTERMidexit);
ragge
1.222
829
ragge
1.226
830         /* after arg parsing */
831         strlist_append(&progdirsLIBEXECDIR);
832         if (pcclibdir)
833                 strlist_append(&crtdirspcclibdir);
834         for (j = 0deflibdirs[j]; j++)
835                 strlist_append(&crtdirsdeflibdirs[j]);
836
ragge
1.223
837         setup_cpp_flags();
ragge
1.226
838         setup_ccom_flags();
839         setup_as_flags();
ragge
1.223
840
841         if (isysroot == NULL)
842                 isysroot = sysroot;
843         expand_sysroot();
844
ragge
1.226
845         STRLIST_FOREACH(s, &inputs) {
846                 char *suffix;
847                 char *ifile, *ofile;
848
849                 ifile = s->value;
850                 if (ninput > 1 && !Eflag)
851                         printf("%s:\n"ifile);
852
853                 suffix = getsufp(ifile);
854                 if (xasm)
855                         suffix = "s";
856                 else if (ascpp)
857                         suffix = "S";
858                 else if (xcflag)
859                         suffix = "c";
ragge
1.128
860                 /*
861                  * C preprocessor
862                  */
ragge
1.226
863                 if (match(suffix"c") || match(suffix"S") ||
864                     cxxsuf(s->value)) {
865                         /* find out next output file */
ragge
1.233
866                         if (Mflag || MDflag) {
867                                 char *Mofile;
868
869                                 if (MFfile)
870                                         Mofile = MFfile;
871                                 else if (outfile)
872                                         Mofile = setsuf(outfile'd');
873                                 else
874                                         Mofile = setsuf(ifile'd');
875                                 if (preprocess_input(ifileMofile1))
876                                         exandrm(Mofile);
877                         }
878                         if (Mflag)
879                                 continue;
880                         if (Eflag) {
ragge
1.226
881                                 /* last pass */
882                                 ofile = outfile;
883                         } else {
884                                 /* to temp file */
885                                 strlist_append(&temp_outputsofile = gettmp());
886                         }
887                         if (preprocess_input(ifileofile0))
888                                 exandrm(ofile);
889                         if (Eflag)
ragge
1.128
890                                 continue;
ragge
1.226
891                         ifile = ofile;
892                         suffix = match(suffix"S") ? "s" : "i";
ragge
1.128
893                 }
894
895                 /*
896                  * C compiler
897                  */
ragge
1.226
898                 if (match(suffix"i")) {
899                         /* find out next output file */
900                         if (Sflag) {
901                                 ofile = outfile;
902                                 if (outfile == NULL)
903                                         ofile = setsuf(s->value's');
904                         } else
905                                 strlist_append(&temp_outputsofile = gettmp());
906                         if (compile_input(ifileofile))
907                                 exandrm(ofile);
908                         if (Sflag)
909                                 continue;
910                         ifile = ofile;
911                         suffix = "s";
ragge
1.128
912                 }
913
914                 /*
915                  * Assembler
916                  */
ragge
1.226
917                 if (match(suffix"s")) {
918                         if (cflag) {
919                                 ofile = outfile;
920                                 if (ofile == NULL)
921                                         ofile = setsuf(s->value'o');
922                         } else {
923                                 strlist_append(&temp_outputsofile = gettmp());
924                                 /* strlist_append linker */
925                         }
926                         if (assemble_input(ifileofile))
927                                 exandrm(ofile);
928                         ifile = ofile;
ragge
1.225
929                 }
ragge
1.226
930                 strlist_append(&middle_linker_flagsifile);
ragge
1.128
931