Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:plunky:20140606131903

Diff

Diff from 1.275 to:

Annotations

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

Annotated File View

plunky
1.275
1 /*      $Id: cc.c,v 1.275 2014/06/06 13:19:03 plunky 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 errorx(intchar *, ...);
244 int cunlink(char *);
ragge
1.226
245 void exandrm(char *);
ragge
1.128
246 void dexit(int);
247 void idexit(int);
248 char *gettmp(void);
ragge
1.219
249 void oerror(char *);
ragge
1.218
250 char *argnxt(char *, char *);
ragge
1.220
251 char *nxtopt(char *o);
ragge
1.222
252 void setup_cpp_flags(void);
ragge
1.226
253 void setup_ccom_flags(void);
254 void setup_as_flags(void);
255 void setup_ld_flags(void);
ragge
1.222
256 static void expand_sysroot(void);
plunky
1.213
257 #ifdef os_win32
ragge
1.128
258 char *win32pathsubst(char *);
plunky
1.250
259 char *win32commandline(struct strlist *l);
ragge
1.128
260 #endif
261 int     sspflag;
plunky
1.214
262 int     freestanding;
ragge
1.226
263 int     Sflag;
ragge
1.128
264 int     cflag;
265 int     gflag;
gmcgarry
1.145
266 int     rflag;
ragge
1.128
267 int     vflag;
268 int     tflag;
269 int     Eflag;
270 int     Oflag;
271 int     kflag;  /* generate PIC/pic code */
272 #define F_PIC   1
273 #define F_pic   2
ragge
1.233
274 int     MflagneedMMDflag;   /* dependencies only */
ragge
1.128
275 int     pgflag;
276 int     Xflag;
277 int     nostartfilesBstaticshared;
278 int     nostdincnostdlib;
279 int     pthreads;
ragge
1.239
280 int     xgnu89xgnu99;
ragge
1.128
281 int     ascpp;
plunky
1.196
282 #ifdef CHAR_UNSIGNED
plunky
1.209
283 int     xuchar = 1;
plunky
1.196
284 #else
plunky
1.209
285 int     xuchar = 0;
plunky
1.196
286 #endif
ragge
1.198
287 int     cxxflag;
ragge
1.216
288 int     cppflag;
ragge
1.254
289 int     printprognameprintfilename;
ragge
1.128
290
ragge
1.164
291 #ifdef mach_amd64
292 int amd64_i386;
293 #endif
ragge
1.128
294
ragge
1.218
295 #define match(a,b)      (strcmp(a,b) == 0)
296
ragge
1.128
297 /* handle gcc warning emulations */
298 struct Wflags {
299         char *name;
300         int flags;
301 #define INWALL          1
302 Wflags[] = {
plunky
1.215
303         { "truncate"0 },
304         { "strict-prototypes"0 },
305         { "missing-prototypes"0 },
306         { "implicit-int"INWALL },
307         { "implicit-function-declaration"INWALL },
308         { "shadow"0 },
309         { "pointer-sign"INWALL },
310         { "sign-compare"0 },
311         { "unknown-pragmas"INWALL },
312         { "unreachable-code"0 },
plunky
1.275
313         { "deprecated-declarations"0 },
plunky
1.215
314         { NULL0 },
ragge
1.128
315 };
316
ragge
1.158
317 #ifndef USHORT
318 /* copied from mip/manifest.h */
319 #define USHORT          5
320 #define INT             6
321 #define UNSIGNED        7
322 #endif
323
ragge
1.157
324 /*
325  * Wide char defines.
326  */
327 #if WCHAR_TYPE == USHORT
328 #define WCT "short unsigned int"
329 #define WCM "65535U"
ragge
1.158
330 #if WCHAR_SIZE != 2
331 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
332 #endif
ragge
1.157
333 #elif WCHAR_TYPE == INT
334 #define WCT "int"
335 #define WCM "2147483647"
ragge
1.158
336 #if WCHAR_SIZE != 4
337 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
338 #endif
ragge
1.157
339 #elif WCHAR_TYPE == UNSIGNED
340 #define WCT "unsigned int"
ragge
1.158
341 #define WCM "4294967295U"
342 #if WCHAR_SIZE != 4
343 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
344 #endif
345 #else
346 #error WCHAR_TYPE not defined or invalid
ragge
1.157
347 #endif
348
ragge
1.160
349 #ifdef GCC_COMPAT
350 #ifndef REGISTER_PREFIX
351 #define REGISTER_PREFIX ""
352 #endif
353 #ifndef USER_LABEL_PREFIX
354 #define USER_LABEL_PREFIX ""
355 #endif
356 #endif
357
plunky
1.192
358 #ifndef PCC_WINT_TYPE
359 #define PCC_WINT_TYPE "unsigned int"
360 #endif
361
362 #ifndef PCC_SIZE_TYPE
363 #define PCC_SIZE_TYPE "unsigned long"
364 #endif
365
ragge
1.161
366 #ifndef PCC_PTRDIFF_TYPE
367 #define PCC_PTRDIFF_TYPE "long int"
368 #endif
369
ragge
1.216
370
ragge
1.218
371 struct strlist preprocessor_flags;
ragge
1.223
372 struct strlist depflags;
ragge
1.219
373 struct strlist incdirs;
ragge
1.221
374 struct strlist user_sysincdirs;
375 struct strlist includes;
ragge
1.222
376 struct strlist sysincdirs;
ragge
1.223
377 struct strlist dirafterdirs;
ragge
1.222
378 struct strlist crtdirs;
379 struct strlist libdirs;
380 struct strlist progdirs;
ragge
1.224
381 struct strlist early_linker_flags;
382 struct strlist middle_linker_flags;
383 struct strlist late_linker_flags;
384 struct strlist inputs;
ragge
1.225
385 struct strlist assembler_flags;
386 struct strlist temp_outputs;
ragge
1.226
387 struct strlist compiler_flags;
ragge
1.218
388
ragge
1.128
389 int
390 main(int argcchar *argv[])
391 {
392         struct Wflags *Wf;
ragge
1.226
393         struct string *s;
ragge
1.218
394         char *t, *u, *argp;
ragge
1.239
395         char *msuffix;
ragge
1.226
396         int ninputj;
ragge
1.222
397
ragge
1.220
398         lav = argv;
399         lac = argc;
plunky
1.242
400         ninput = 0;
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.260
431 #ifdef PCC_EARLY_SETUP
432         PCC_EARLY_SETUP
ragge
1.246
433 #endif
434
plunky
1.213
435 #ifdef os_win32
ragge
1.128
436         /* have to prefix path early.  -B may override */
437         incdir = win32pathsubst(incdir);
gmcgarry
1.153
438         altincdir = win32pathsubst(altincdir);
gmcgarry
1.136
439         libdir = win32pathsubst(libdir);
plunky
1.200
440 #ifdef PCCINCDIR
ragge
1.128
441         pccincdir = win32pathsubst(pccincdir);
plunky
1.199
442         pxxincdir = win32pathsubst(pxxincdir);
plunky
1.200
443 #endif
444 #ifdef PCCLIBDIR
ragge
1.128
445         pcclibdir = win32pathsubst(pcclibdir);
plunky
1.200
446 #endif
ragge
1.128
447         passp = win32pathsubst(passp);
448         pass0 = win32pathsubst(pass0);
gmcgarry
1.180
449 #ifdef STARTFILES
450         for (i = 0startfiles[i] != NULLi++)
451                 startfiles[i] = win32pathsubst(startfiles[i]);
452         for (i = 0endfiles[i] != NULLi++)
453                 endfiles[i] = win32pathsubst(endfiles[i]);
454 #endif
455 #ifdef STARTFILES_T
456         for (i = 0startfiles_T[i] != NULLi++)
457                 startfiles_T[i] = win32pathsubst(startfiles_T[i]);
458         for (i = 0endfiles_T[i] != NULLi++)
459                 endfiles_T[i] = win32pathsubst(endfiles_T[i]);
460 #endif
461 #ifdef STARTFILES_S
462         for (i = 0startfiles_S[i] != NULLi++)
463                 startfiles_S[i] = win32pathsubst(startfiles_S[i]);
464         for (i = 0endfiles_S[i] != NULLi++)
465                 endfiles_S[i] = win32pathsubst(endfiles_S[i]);
466 #endif
ragge
1.128
467 #endif
468
ragge
1.220
469         while (--lac) {
470                 ++lav;
471                 argp = *lav;
472
plunky
1.260
473 #ifdef PCC_EARLY_ARG_CHECK
474                 PCC_EARLY_ARG_CHECK
ragge
1.227
475 #endif
476
ragge
1.225
477                 if (*argp != '-' || match(argp"-")) {
478                         /* Check for duplicate .o files. */
479                         if (getsuf(argp) == 'o') {
480                                 j = 0;
481                                 STRLIST_FOREACH(s, &inputs)
482                                         if (match(argps->value))
483                                                 j++;
484                                 if (j)
485                                         continue/* skip it */
486                         }
487                         strlist_append(&inputsargp);
plunky
1.242
488                         ninput++;
ragge
1.225
489                         continue;
490                 }
ragge
1.220
491
492                 switch (argp[1]) {
493                 default:
plunky
1.257
494                         oerror(argp);
ragge
1.220
495                         break;
ragge
1.128
496
ragge
1.220
497                 case '-'/* double -'s */
498                         if (match(argp"--version")) {
499                                 printf("%s\n"VERSSTR);
500                                 return 0;
501                         } else if (strncmp(argp"--sysroot="10) == 0) {
502                                 sysroot = argp + 10;
503                         } else if (strcmp(argp"--param") == 0) {
504                                 /* NOTHING YET */;
505                                 (void)nxtopt(0); /* ignore arg */
506                         } else
plunky
1.257
507                                 oerror(argp);
ragge
1.220
508                         break;
ragge
1.128
509
ragge
1.220
510                 case 'B'/* other search paths for binaries */
ragge
1.224
511                         t = nxtopt("-B");
512                         strlist_append(&crtdirst);
513                         strlist_append(&libdirst);
514                         strlist_append(&progdirst);
ragge
1.220
515                         break;
ragge
1.128
516
ragge
1.220
517                 case 'C':
518                         if (match(argp"-C") || match(argp"-CC"))
519                                 strlist_append(&preprocessor_flagsargp);
520                         else
521                                 oerror(argp);
522                         break;
ragge
1.218
523
ragge
1.255
524                 case 'c':
525                         cflag++;
526                         break;
527
528                 case 'd':
plunky
1.257
529                         oerror(argp);
ragge
1.220
530                         break;
ragge
1.255
531
532                 case 'E':
533                         Eflag++;
ragge
1.220
534                         break;
ragge
1.128
535
ragge
1.220
536                 case 'f'/* GCC compatibility flags */
ragge
1.246
537                         u = &argp[2];
538                         j = 0;
539                         if (strncmp(u"no-"3) == 0)
540                                 j = 1u += 3;
541                         if (match(u"PIC") || match(u"pic")) {
542                                 kflag = j ? 0 : *u == 'P' ? F_PIC : F_pic;
543                         } else if (match(u"freestanding")) {
544                                 freestanding = j ? 0 : 1;
545                         } else if (match(u"signed-char")) {
546                                 xuchar = j ? 1 : 0;
547                         } else if (match(u"unsigned-char")) {
548                                 xuchar = j ? 0 : 1;
plunky
1.248
549                         } else if (match(u"stack-protector") ||
550                             match(u"stack-protector-all")) {
ragge
1.246
551                                 sspflag = j ? 0 : 1;
ragge
1.220
552                         }
553                         /* silently ignore the rest */
554                         break;
ragge
1.128
555
ragge
1.220
556                 case 'g'/* create debug output */
557                         if (argp[2] == '0')
558                                 gflag = 0;
559                         else
560                                 gflag++;
561                         break;
ragge
1.128
562
ragge
1.255
563
564                 case 'X':
565                         Xflag++;
566                         break;
567
ragge
1.220
568                 case 'D':
569                 case 'U':
ragge
1.221
570                         strlist_append(&preprocessor_flagsargp);
ragge
1.220
571                         if (argp[2] != 0)
ragge
1.219
572                                 break;
ragge
1.221
573                         strlist_append(&preprocessor_flagsnxtopt(argp));
574                         break;
575
576                 case 'I'/* Add include dirs */
577                         strlist_append(&incdirsnxtopt("-I"));
ragge
1.220
578                         break;
ragge
1.219
579
ragge
1.220
580                 case 'i':
ragge
1.221
581                         if (match(argp"-isystem")) {
582                                 strlist_append(&user_sysincdirsnxtopt(0));
583                         } else if (match(argp"-include")) {
584                                 strlist_append(&includesnxtopt(0));
ragge
1.222
585                         } else if (match(argp"-isysroot")) {
586                                 isysroot = nxtopt(0);
ragge
1.220
587                         } else if (strcmp(argp"-idirafter") == 0) {
ragge
1.223
588                                 strlist_append(&dirafterdirsnxtopt(0));
ragge
1.220
589                         } else
plunky
1.257
590                                 oerror(argp);
ragge
1.220
591                         break;
ragge
1.128
592
ragge
1.220
593                 case 'k'/* generate PIC code */
ragge
1.247
594                         kflag = argp[2] ? argp[2] - '0' : F_pic;
ragge
1.220
595                         break;
ragge
1.128
596
ragge
1.224
597                 case 'l':
598                 case 'L':
ragge
1.226
599                         if (argp[2] == 0)
ragge
1.245
600                                 argp = cat(argpnxtopt(0));
601                         strlist_append(&inputsargp);
ragge
1.224
602                         break;
603
ragge
1.220
604                 case 'm'/* target-dependent options */
ragge
1.164
605 #ifdef mach_amd64
ragge
1.220
606                         /* need to call i386 ccom for this */
ragge
1.262
607                         if (strcmp(argp"-melf_i386") == 0) {
ragge
1.220
608                                 pass0 = LIBEXECDIR "/ccom_i386";
609                                 amd64_i386 = 1;
610                                 break;
611                         }
ragge
1.164
612 #endif
ragge
1.225
613                         strlist_append(&middle_linker_flagsargp);
ragge
1.220
614                         if (argp[2] == 0) {
ragge
1.225
615                                 t = nxtopt(0);
616                                 strlist_append(&middle_linker_flagst);
ragge
1.220
617                         }
618                         break;
ragge
1.128
619
ragge
1.220
620                 case 'n'/* handle -n flags */
621                         if (strcmp(argp"-nostdinc") == 0)
622                                 nostdinc++;
623                         else if (strcmp(argp"-nostdlib") == 0) {
624                                 nostdlib++;
625                                 nostartfiles++;
626                         } else if (strcmp(argp"-nostartfiles") == 0)
627                                 nostartfiles = 1;
628                         else if (strcmp(argp"-nodefaultlibs") == 0)
629                                 nostdlib++;
630                         else
plunky
1.257
631                                 oerror(argp);
ragge
1.220
632                         break;
ragge
1.128
633
ragge
1.220
634                 case 'p':
635                         if (strcmp(argp"-pg") == 0 ||
636                             strcmp(argp"-p") == 0)
637                                 pgflag++;
638                         else if (strcmp(argp"-pthread") == 0)
639                                 pthreads++;
640                         else if (strcmp(argp"-pipe") == 0)
641                                 /* NOTHING YET */;
642                         else if (strcmp(argp"-pedantic") == 0)
643                                 /* NOTHING YET */;
ragge
1.254
644                         else if ((t = argnxt(argp"-print-prog-name="))) {
645                                 fname = t;
646                                 printprogname = 1;
647                         } else if ((t = argnxt(argp"-print-file-name="))) {
648                                 fname = t;
649                                 printfilename = 1;
650                         } else if (match(argp"-print-libgcc-file-name")) {
651                                 fname = "libpcc.a";
652                                 printfilename = 1;
ragge
1.220
653                         } else
654                                 oerror(argp);
655                         break;
ragge
1.128
656
ragge
1.220
657                 case 'r':
658                         rflag = 1;
659                         break;
gmcgarry
1.145
660
ragge
1.240
661                 case 'T':
662                         strlist_append(&inputsargp);
mickey
1.241
663                         if (argp[2] == 0 ||
664                             strcmp(argp"-Ttext") == 0 ||
665                             strcmp(argp"-Tdata") == 0 ||
666                             strcmp(argp"-Tbss") == 0)
667                                 strlist_append(&inputsnxtopt(0));
ragge
1.240
668                         break;
ragge
1.255
669
670                 case 's':
671                         if (match(argp"-shared")) {
672                                 shared = 1;
673                         } else if (match(argp"-static")) {
674                                 Bstatic = 1;
675                         } else if (match(argp"-symbolic")) {
676                                 strlist_append(&middle_linker_flags,
677                                     "-Bsymbolic");
678                         } else if (strncmp(argp"-std"4) == 0) {
679                                 if (strcmp(&argp[5], "gnu99") == 0 ||
680                                     strcmp(&argp[5], "gnu9x") == 0)
681                                         xgnu99 = 1;
682                                 if (strcmp(&argp[5], "gnu89") == 0)
683                                         xgnu89 = 1;
684                         } else
plunky
1.257
685                                 oerror(argp);
ragge
1.220
686                         break;
ragge
1.255
687
ragge
1.220
688                 case 'S':
ragge
1.226
689                         Sflag++;
ragge
1.220
690                         cflag++;
691                         break;
ragge
1.255
692
693                 case 't':
694                         tflag++;
695                         break;
696
ragge
1.220
697                 case 'o':
698                         if (outfile)
699                                 errorx(8"too many -o");
700                         outfile = nxtopt("-o");
701                         break;
ragge
1.255
702
ragge
1.220
703                 case 'O':
704                         if (argp[2] == '\0')
705                                 Oflag++;
ragge
1.226
706                         else if (argp[3] == '\0' &&
707                             isdigit((unsigned char)argp[2]))
ragge
1.220
708                                 Oflag = argp[2] - '0';
709                         else if (argp[3] == '\0' && argp[2] == 's')
710                                 Oflag = 1;      /* optimize for space only */
711                         else
712                                 oerror(argp);
713                         break;
ragge
1.255
714
ragge
1.220
715                 case 'P':
ragge
1.221
716                         strlist_append(&preprocessor_flagsargp);
717                         break;
718
ragge
1.220
719                 case 'M':
ragge
1.233
720                         needM = 1;
721                         if (match(argp"-M")) {
ragge
1.223
722                                 Mflag++;
723                                 strlist_append(&depflagsargp);
ragge
1.233
724                         } else if (match(argp"-MP")) {
ragge
1.223
725                                 strlist_append(&depflags"-xMP");
ragge
1.233
726                         } else if (match(argp"-MF")) {
727                                 MFfile = nxtopt("-MF");
728                         } else if (match(argp"-MT") || match(argp"-MQ")) {
ragge
1.226
729                                 t = cat("-xMT,"nxtopt("-MT"));
ragge
1.220
730                                 t[3] = argp[2];
ragge
1.223
731                                 strlist_append(&depflagst);
ragge
1.233
732                         } else if (match(argp"-MD")) {
733                                 MDflag++;
734                                 needM = 0;
735                                 strlist_append(&depflags"-M");
736                         } else
ragge
1.220
737                                 oerror(argp);
738                         break;
ragge
1.128
739
ragge
1.220
740                 case 'v':
741                         printf("%s\n"VERSSTR);
742                         vflag++;
743                         break;
ragge
1.128
744
ragge
1.255
745                 case 'W'/* Ignore (most of) W-flags */
746                         if ((t = argnxt(argp"-Wl,"))) {
747                                 u = strtok(t",");
748                                 do {
749                                         strlist_append(&inputsu);
750                                 } while ((u = strtok(NULL",")) != NULL);
751                         } else if ((t = argnxt(argp"-Wa,"))) {
752                                 u = strtok(t",");
753                                 do {
754                                         strlist_append(&assembler_flagsu);
755                                 } while ((u = strtok(NULL",")) != NULL);
756                         } else if ((t = argnxt(argp"-Wc,"))) {
757                                 u = strtok(t",");
758                                 do {
759                                         strlist_append(&compiler_flagsu);
760                                 } while ((u = strtok(NULL",")) != NULL);
761                         } else if ((t = argnxt(argp"-Wp,"))) {
762                                 u = strtok(t",");
763                                 do {
764                                         strlist_append(&preprocessor_flagsu);
765                                 } while ((u = strtok(NULL",")) != NULL);
766                         } else if (strcmp(argp"-Werror") == 0) {
767                                 strlist_append(&compiler_flags"-Werror");
plunky
1.261
768                                 strlist_append(&preprocessor_flags"-E");
ragge
1.255
769                         } else if (strcmp(argp"-Wall") == 0) {
770                                 for (Wf = WflagsWf->nameWf++)
771                                         if (Wf->flags & INWALL)
772                                                 strlist_append(&compiler_flags,
773                                                     cat("-W"Wf->name));
774                         } else if (strcmp(argp"-WW") == 0) {
775                                 for (Wf = WflagsWf->nameWf++)
776                                         strlist_append(&compiler_flags,
777                                             cat("-W"Wf->name));
778                         } else {
779                                 /* pass through, if supported */
780                                 t = &argp[2];
781                                 if (strncmp(t"no-"3) == 0)
782                                         t += 3;
783                                 if (strncmp(t"error="6) == 0)
784                                         t += 6;
785                                 for (Wf = WflagsWf->nameWf++) {
786                                         if (strcmp(tWf->name) == 0)
787                                                 strlist_append(&compiler_flags,
788                                                     argp);
789                                 }
790                         }
791                         break;
792
793                 case 'x':
794                         t = nxtopt("-x");
plunky
1.267
795                         if (match(t"none"))
796                                 strlist_append(&inputs")");
797                         else if (match(t"c"))
ragge
1.255
798                                 strlist_append(&inputs")c");
799                         else if (match(t"assembler"))
800                                 strlist_append(&inputs")s");
801                         else if (match(t"assembler-with-cpp"))
802                                 strlist_append(&inputs")S");
803                         else if (match(t"c++"))
804                                 strlist_append(&inputs")c++");
805                         else {
806                                 strlist_append(&compiler_flags"-x");
807                                 strlist_append(&compiler_flagst);
808                         }
ragge
1.220
809                         break;
ragge
1.255
810
ragge
1.220
811                 }
812                 continue;
813
ragge
1.128
814         }
ragge
1.222
815
ragge
1.128
816         /* Sanity checking */
ragge
1.216
817         if (cppflag) {
ragge
1.224
818                 if (ninput == 0) {
819                         strlist_append(&inputs"-");
ragge
1.232
820                         ninput++;
ragge
1.224
821                 } else if (ninput > 2 || (ninput == 2 && outfile)) {
ragge
1.216
822                         errorx(8"too many files");
ragge
1.232
823                 } else if (ninput == 2) {
824                         outfile = STRLIST_NEXT(STRLIST_FIRST(&inputs))->value;
825                         STRLIST_FIRST(&inputs)->next = NULL;
826                         ninput--;
ragge
1.224
827                 }
ragge
1.216
828         }
ragge
1.254
829         if (ninput == 0 && !(printprogname || printfilename))
ragge
1.128
830                 errorx(8"no input files");
ragge
1.226
831         if (outfile && (cflag || Sflag || Eflag) && ninput > 1)
ragge
1.128
832                 errorx(8"-o given with -c || -E || -S and more than one file");
ragge
1.224
833 #if 0
ragge
1.128
834         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
835                 errorx(8"output file will be clobbered");
ragge
1.224
836 #endif
ragge
1.223
837
ragge
1.233
838         if (needM && !Mflag && !MDflag)
ragge
1.223
839                 errorx(8"to make dependencies needs -M");
840
841
ragge
1.128
842         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
843                 signal(SIGINTidexit);
844         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
845                 signal(SIGTERMidexit);
ragge
1.222
846
ragge
1.226
847         /* after arg parsing */
848         strlist_append(&progdirsLIBEXECDIR);
849         if (pcclibdir)
850                 strlist_append(&crtdirspcclibdir);
ragge
1.265
851         for (j = 0deflibdirs[j]; j++) {
852                 if (sysroot)
853                         deflibdirs[j] = cat(sysrootdeflibdirs[j]);
ragge
1.226
854                 strlist_append(&crtdirsdeflibdirs[j]);
ragge
1.265
855         }
ragge
1.226
856
ragge
1.223
857         setup_cpp_flags();
ragge
1.226
858         setup_ccom_flags();
859         setup_as_flags();
ragge
1.223
860
861         if (isysroot == NULL)
862                 isysroot = sysroot;
863         expand_sysroot();
864
ragge
1.254
865         if (printprogname) {
866                 printf("%s\n"find_file(fname, &progdirsX_OK));
867                 return 0;
868         } else if (printfilename) {
869                 printf("%s\n"find_file(fname, &crtdirsR_OK));
870                 return 0;
871         }
872
ragge
1.239
873         msuffix = NULL;
ragge
1.226
874         STRLIST_FOREACH(s, &inputs) {
875                 char *suffix;
mickey
1.270
876                 char *ifile, *ofile = NULL;
ragge
1.226
877
878                 ifile = s->value;
plunky
1.267
879                 if (ifile[0] == ')') { /* -x source type given */
880                         msuffix = ifile[1] ? &ifile[1] : NULL;
ragge
1.239
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                  */
plunky
1.267
894                 ascpp = match(suffix"S");
plunky
1.271
895                 if (ascpp || match(suffix"c") || cxxsuf(suffix)) {
ragge
1.226
896                         /* find out next output file */
ragge
1.233
897                         if (Mflag || MDflag) {
ragge
1.272
898                                 char *Mofile = NULL;
ragge
1.233
899
900                                 if (MFfile)
901                                         Mofile = MFfile;
902                                 else if (outfile)
903                                         Mofile = setsuf(outfile'd');
ragge
1.272
904                                 else if (MDflag)
ragge
1.233
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&nbs