Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20120923083948

Diff

Diff from 1.255 to:

Annotations

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

Annotated File View

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