Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:plunky:20140606130437

Diff

Diff from 1.274 to:

Annotations

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

Annotated File View

plunky
1.274
1 /*      $Id: cc.c,v 1.274 2014/06/06 13:04:37 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 },
313         { NULL0 },
ragge
1.128
314 };
315
ragge
1.158
316 #ifndef USHORT
317 /* copied from mip/manifest.h */
318 #define USHORT          5
319 #define INT             6
320 #define UNSIGNED        7
321 #endif
322
ragge
1.157
323 /*
324  * Wide char defines.
325  */
326 #if WCHAR_TYPE == USHORT
327 #define WCT "short unsigned int"
328 #define WCM "65535U"
ragge
1.158
329 #if WCHAR_SIZE != 2
330 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
331 #endif
ragge
1.157
332 #elif WCHAR_TYPE == INT
333 #define WCT "int"
334 #define WCM "2147483647"
ragge
1.158
335 #if WCHAR_SIZE != 4
336 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
337 #endif
ragge
1.157
338 #elif WCHAR_TYPE == UNSIGNED
339 #define WCT "unsigned int"
ragge
1.158
340 #define WCM "4294967295U"
341 #if WCHAR_SIZE != 4
342 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
343 #endif
344 #else
345 #error WCHAR_TYPE not defined or invalid
ragge
1.157
346 #endif
347
ragge
1.160
348 #ifdef GCC_COMPAT
349 #ifndef REGISTER_PREFIX
350 #define REGISTER_PREFIX ""
351 #endif
352 #ifndef USER_LABEL_PREFIX
353 #define USER_LABEL_PREFIX ""
354 #endif
355 #endif
356
plunky
1.192
357 #ifndef PCC_WINT_TYPE
358 #define PCC_WINT_TYPE "unsigned int"
359 #endif
360
361 #ifndef PCC_SIZE_TYPE
362 #define PCC_SIZE_TYPE "unsigned long"
363 #endif
364
ragge
1.161
365 #ifndef PCC_PTRDIFF_TYPE
366 #define PCC_PTRDIFF_TYPE "long int"
367 #endif
368
ragge
1.216
369
ragge
1.218
370 struct strlist preprocessor_flags;
ragge
1.223
371 struct strlist depflags;
ragge
1.219
372 struct strlist incdirs;
ragge
1.221
373 struct strlist user_sysincdirs;
374 struct strlist includes;
ragge
1.222
375 struct strlist sysincdirs;
ragge
1.223
376 struct strlist dirafterdirs;
ragge
1.222
377 struct strlist crtdirs;
378 struct strlist libdirs;
379 struct strlist progdirs;
ragge
1.224
380 struct strlist early_linker_flags;
381 struct strlist middle_linker_flags;
382 struct strlist late_linker_flags;
383 struct strlist inputs;
ragge
1.225
384 struct strlist assembler_flags;
385 struct strlist temp_outputs;
ragge
1.226
386 struct strlist compiler_flags;
ragge
1.218
387
ragge
1.128
388 int
389 main(int argcchar *argv[])
390 {
391         struct Wflags *Wf;
ragge
1.226
392         struct string *s;
ragge
1.218
393         char *t, *u, *argp;
ragge
1.239
394         char *msuffix;
ragge
1.226
395         int ninputj;
ragge
1.222
396
ragge
1.220
397         lav = argv;
398         lac = argc;
plunky
1.242
399         ninput = 0;
ragge
1.128
400
ragge
1.222
401         strlist_init(&crtdirs);
402         strlist_init(&libdirs);
403         strlist_init(&progdirs);
ragge
1.218
404         strlist_init(&preprocessor_flags);
ragge
1.219
405         strlist_init(&incdirs);
ragge
1.221
406         strlist_init(&user_sysincdirs);
407         strlist_init(&includes);
ragge
1.222
408         strlist_init(&sysincdirs);
ragge
1.223
409         strlist_init(&dirafterdirs);
410         strlist_init(&depflags);
ragge
1.224
411         strlist_init(&early_linker_flags);
412         strlist_init(&middle_linker_flags);
413         strlist_init(&late_linker_flags);
414         strlist_init(&inputs);
ragge
1.225
415         strlist_init(&assembler_flags);
416         strlist_init(&temp_outputs);
ragge
1.226
417         strlist_init(&compiler_flags);
ragge
1.218
418
ragge
1.216
419         if ((t = strrchr(argv[0], '/')))
ragge
1.226
420                 t++;
ragge
1.229
421         else
422                 t = argv[0];
ragge
1.226
423
424         if (match(t"p++")) {
ragge
1.198
425                 cxxflag = 1;
ragge
1.226
426         } else if (match(t"cpp") || match(tCPPROGNAME)) {
427                 Eflag = cppflag = 1;
ragge
1.198
428         }
429
plunky
1.260
430 #ifdef PCC_EARLY_SETUP
431         PCC_EARLY_SETUP
ragge
1.246
432 #endif
433
plunky
1.213
434 #ifdef os_win32
ragge
1.128
435         /* have to prefix path early.  -B may override */
436         incdir = win32pathsubst(incdir);
gmcgarry
1.153
437         altincdir = win32pathsubst(altincdir);
gmcgarry
1.136
438         libdir = win32pathsubst(libdir);
plunky
1.200
439 #ifdef PCCINCDIR
ragge
1.128
440         pccincdir = win32pathsubst(pccincdir);
plunky
1.199
441         pxxincdir = win32pathsubst(pxxincdir);
plunky
1.200
442 #endif
443 #ifdef PCCLIBDIR
ragge
1.128
444         pcclibdir = win32pathsubst(pcclibdir);
plunky
1.200
445 #endif
ragge
1.128
446         passp = win32pathsubst(passp);
447         pass0 = win32pathsubst(pass0);
gmcgarry
1.180
448 #ifdef STARTFILES
449         for (i = 0startfiles[i] != NULLi++)
450                 startfiles[i] = win32pathsubst(startfiles[i]);
451         for (i = 0endfiles[i] != NULLi++)
452                 endfiles[i] = win32pathsubst(endfiles[i]);
453 #endif
454 #ifdef STARTFILES_T
455         for (i = 0startfiles_T[i] != NULLi++)
456                 startfiles_T[i] = win32pathsubst(startfiles_T[i]);
457         for (i = 0endfiles_T[i] != NULLi++)
458                 endfiles_T[i] = win32pathsubst(endfiles_T[i]);
459 #endif
460 #ifdef STARTFILES_S
461         for (i = 0startfiles_S[i] != NULLi++)
462                 startfiles_S[i] = win32pathsubst(startfiles_S[i]);
463         for (i = 0endfiles_S[i] != NULLi++)
464                 endfiles_S[i] = win32pathsubst(endfiles_S[i]);
465 #endif
ragge
1.128
466 #endif
467
ragge
1.220
468         while (--lac) {
469                 ++lav;
470                 argp = *lav;
471
plunky
1.260
472 #ifdef PCC_EARLY_ARG_CHECK
473                 PCC_EARLY_ARG_CHECK
ragge
1.227
474 #endif
475
ragge
1.225
476                 if (*argp != '-' || match(argp"-")) {
477                         /* Check for duplicate .o files. */
478                         if (getsuf(argp) == 'o') {
479                                 j = 0;
480                                 STRLIST_FOREACH(s, &inputs)
481                                         if (match(argps->value))
482                                                 j++;
483                                 if (j)
484                                         continue/* skip it */
485                         }
486                         strlist_append(&inputsargp);
plunky
1.242
487                         ninput++;
ragge
1.225
488                         continue;
489                 }
ragge
1.220
490
491                 switch (argp[1]) {
492                 default:
plunky
1.257
493                         oerror(argp);
ragge
1.220
494                         break;
ragge
1.128
495
ragge
1.220
496                 case '-'/* double -'s */
497                         if (match(argp"--version")) {
498                                 printf("%s\n"VERSSTR);
499                                 return 0;
500                         } else if (strncmp(argp"--sysroot="10) == 0) {
501                                 sysroot = argp + 10;
502                         } else if (strcmp(argp"--param") == 0) {
503                                 /* NOTHING YET */;
504                                 (void)nxtopt(0); /* ignore arg */
505                         } else
plunky
1.257
506                                 oerror(argp);
ragge
1.220
507                         break;
ragge
1.128
508
ragge
1.220
509                 case 'B'/* other search paths for binaries */
ragge
1.224
510                         t = nxtopt("-B");
511                         strlist_append(&crtdirst);
512                         strlist_append(&libdirst);
513                         strlist_append(&progdirst);
ragge
1.220
514                         break;
ragge
1.128
515
ragge
1.220
516                 case 'C':
517                         if (match(argp"-C") || match(argp"-CC"))
518                                 strlist_append(&preprocessor_flagsargp);
519                         else
520                                 oerror(argp);
521                         break;
ragge
1.218
522
ragge
1.255
523                 case 'c':
524                         cflag++;
525                         break;
526
527                 case 'd':
plunky
1.257
528                         oerror(argp);
ragge
1.220
529                         break;
ragge
1.255
530
531                 case 'E':
532                         Eflag++;
ragge
1.220
533                         break;
ragge
1.128
534
ragge
1.220
535                 case 'f'/* GCC compatibility flags */
ragge
1.246
536                         u = &argp[2];
537                         j = 0;
538                         if (strncmp(u"no-"3) == 0)
539                                 j = 1u += 3;
540                         if (match(u"PIC") || match(u"pic")) {
541                                 kflag = j ? 0 : *u == 'P' ? F_PIC : F_pic;
542                         } else if (match(u"freestanding")) {
543                                 freestanding = j ? 0 : 1;
544                         } else if (match(u"signed-char")) {
545                                 xuchar = j ? 1 : 0;
546                         } else if (match(u"unsigned-char")) {
547                                 xuchar = j ? 0 : 1;
plunky
1.248
548                         } else if (match(u"stack-protector") ||
549                             match(u"stack-protector-all")) {
ragge
1.246
550                                 sspflag = j ? 0 : 1;
ragge
1.220
551                         }
552                         /* silently ignore the rest */
553                         break;
ragge
1.128
554
ragge
1.220
555                 case 'g'/* create debug output */
556                         if (argp[2] == '0')
557                                 gflag = 0;
558                         else
559                                 gflag++;
560                         break;
ragge
1.128
561
ragge
1.255
562
563                 case 'X':
564                         Xflag++;
565                         break;
566
ragge
1.220
567                 case 'D':
568                 case 'U':
ragge
1.221
569                         strlist_append(&preprocessor_flagsargp);
ragge
1.220
570                         if (argp[2] != 0)
ragge
1.219
571                                 break;
ragge
1.221
572                         strlist_append(&preprocessor_flagsnxtopt(argp));
573                         break;
574
575                 case 'I'/* Add include dirs */
576                         strlist_append(&incdirsnxtopt("-I"));
ragge
1.220
577                         break;
ragge
1.219
578
ragge
1.220
579                 case 'i':
ragge
1.221
580                         if (match(argp"-isystem")) {
581                                 strlist_append(&user_sysincdirsnxtopt(0));
582                         } else if (match(argp"-include")) {
583                                 strlist_append(&includesnxtopt(0));
ragge
1.222
584                         } else if (match(argp"-isysroot")) {
585                                 isysroot = nxtopt(0);
ragge
1.220
586                         } else if (strcmp(argp"-idirafter") == 0) {
ragge
1.223
587                                 strlist_append(&dirafterdirsnxtopt(0));
ragge
1.220
588                         } else
plunky
1.257
589                                 oerror(argp);
ragge
1.220
590                         break;
ragge
1.128
591
ragge
1.220
592                 case 'k'/* generate PIC code */
ragge
1.247
593                         kflag = argp[2] ? argp[2] - '0' : F_pic;
ragge
1.220
594                         break;
ragge
1.128
595
ragge
1.224
596                 case 'l':
597                 case 'L':
ragge
1.226
598                         if (argp[2] == 0)
ragge
1.245
599                                 argp = cat(argpnxtopt(0));
600                         strlist_append(&inputsargp);
ragge
1.224
601                         break;
602
ragge
1.220
603                 case 'm'/* target-dependent options */
ragge
1.164
604 #ifdef mach_amd64
ragge
1.220
605                         /* need to call i386 ccom for this */
ragge
1.262
606                         if (strcmp(argp"-melf_i386") == 0) {
ragge
1.220
607                                 pass0 = LIBEXECDIR "/ccom_i386";
608                                 amd64_i386 = 1;
609                                 break;
610                         }
ragge
1.164
611 #endif
ragge
1.225
612                         strlist_append(&middle_linker_flagsargp);
ragge
1.220
613                         if (argp[2] == 0) {
ragge
1.225
614                                 t = nxtopt(0);
615                                 strlist_append(&middle_linker_flagst);
ragge
1.220
616                         }
617                         break;
ragge
1.128
618
ragge
1.220
619                 case 'n'/* handle -n flags */
620                         if (strcmp(argp"-nostdinc") == 0)
621                                 nostdinc++;
622                         else if (strcmp(argp"-nostdlib") == 0) {
623                                 nostdlib++;
624                                 nostartfiles++;
625                         } else if (strcmp(argp"-nostartfiles") == 0)
626                                 nostartfiles = 1;
627                         else if (strcmp(argp"-nodefaultlibs") == 0)
628                                 nostdlib++;
629                         else
plunky
1.257
630                                 oerror(argp);
ragge
1.220
631                         break;
ragge
1.128
632
ragge
1.220
633                 case 'p':
634                         if (strcmp(argp"-pg") == 0 ||
635                             strcmp(argp"-p") == 0)
636                                 pgflag++;
637                         else if (strcmp(argp"-pthread") == 0)
638                                 pthreads++;
639                         else if (strcmp(argp"-pipe") == 0)
640                                 /* NOTHING YET */;
641                         else if (strcmp(argp"-pedantic") == 0)
642                                 /* NOTHING YET */;
ragge
1.254
643                         else if ((t = argnxt(argp"-print-prog-name="))) {
644                                 fname = t;
645                                 printprogname = 1;
646                         } else if ((t = argnxt(argp"-print-file-name="))) {
647                                 fname = t;
648                                 printfilename = 1;
649                         } else if (match(argp"-print-libgcc-file-name")) {
650                                 fname = "libpcc.a";
651                                 printfilename = 1;
ragge
1.220
652                         } else
653                                 oerror(argp);
654                         break;
ragge
1.128
655
ragge
1.220
656                 case 'r':
657                         rflag = 1;
658                         break;
gmcgarry
1.145
659
ragge
1.240
660                 case 'T':
661                         strlist_append(&inputsargp);
mickey
1.241
662                         if (argp[2] == 0 ||
663                             strcmp(argp"-Ttext") == 0 ||
664                             strcmp(argp"-Tdata") == 0 ||
665                             strcmp(argp"-Tbss") == 0)
666                                 strlist_append(&inputsnxtopt(0));
ragge
1.240
667                         break;
ragge
1.255
668
669                 case 's':
670                         if (match(argp"-shared")) {
671                                 shared = 1;
672                         } else if (match(argp"-static")) {
673                                 Bstatic = 1;
674                         } else if (match(argp"-symbolic")) {
675                                 strlist_append(&middle_linker_flags,
676                                     "-Bsymbolic");
677                         } else if (strncmp(argp"-std"4) == 0) {
678                                 if (strcmp(&argp[5], "gnu99") == 0 ||
679                                     strcmp(&argp[5], "gnu9x") == 0)
680                                         xgnu99 = 1;
681                                 if (strcmp(&argp[5], "gnu89") == 0)
682                                         xgnu89 = 1;
683                         } else
plunky
1.257
684                                 oerror(argp);
ragge
1.220
685                         break;
ragge
1.255
686
ragge
1.220
687                 case 'S':
ragge
1.226
688                         Sflag++;
ragge
1.220
689                         cflag++;
690                         break;
ragge
1.255
691
692                 case 't':
693                         tflag++;
694                         break;
695
ragge
1.220
696                 case 'o':
697                         if (outfile)
698                                 errorx(8"too many -o");
699                         outfile = nxtopt("-o");
700                         break;
ragge
1.255
701
ragge
1.220
702                 case 'O':
703                         if (argp[2] == '\0')
704                                 Oflag++;
ragge
1.226
705                         else if (argp[3] == '\0' &&
706                             isdigit((unsigned char)argp[2]))
ragge
1.220
707                                 Oflag = argp[2] - '0';
708                         else if (argp[3] == '\0' && argp[2] == 's')
709                                 Oflag = 1;      /* optimize for space only */
710                         else
711                                 oerror(argp);
712                         break;
ragge
1.255
713
ragge
1.220
714                 case 'P':
ragge
1.221
715                         strlist_append(&preprocessor_flagsargp);
716                         break;
717
ragge
1.220
718                 case 'M':
ragge
1.233
719                         needM = 1;
720                         if (match(argp"-M")) {
ragge
1.223
721                                 Mflag++;
722                                 strlist_append(&depflagsargp);
ragge
1.233
723                         } else if (match(argp"-MP")) {
ragge
1.223
724                                 strlist_append(&depflags"-xMP");
ragge
1.233
725                         } else if (match(argp"-MF")) {
726                                 MFfile = nxtopt("-MF");
727                         } else if (match(argp"-MT") || match(argp"-MQ")) {
ragge
1.226
728                                 t = cat("-xMT,"nxtopt("-MT"));
ragge
1.220
729                                 t[3] = argp[2];
ragge
1.223
730                                 strlist_append(&depflagst);
ragge
1.233
731                         } else if (match(argp"-MD")) {
732                                 MDflag++;
733                                 needM = 0;
734                                 strlist_append(&depflags"-M");
735                         } else
ragge
1.220
736                                 oerror(argp);
737                         break;
ragge
1.128
738
ragge
1.220
739                 case 'v':
740                         printf("%s\n"VERSSTR);
741                         vflag++;
742                         break;
ragge
1.128
743
ragge
1.255
744                 case 'W'/* Ignore (most of) W-flags */
745                         if ((t = argnxt(argp"-Wl,"))) {
746                                 u = strtok(t",");
747                                 do {
748                                         strlist_append(&inputsu);
749                                 } while ((u = strtok(NULL",")) != NULL);
750                         } else if ((t = argnxt(argp"-Wa,"))) {
751                                 u = strtok(t",");
752                                 do {
753                                         strlist_append(&assembler_flagsu);
754                                 } while ((u = strtok(NULL",")) != NULL);
755                         } else if ((t = argnxt(argp"-Wc,"))) {
756                                 u = strtok(t",");
757                                 do {
758                                         strlist_append(&compiler_flagsu);
759                                 } while ((u = strtok(NULL",")) != NULL);
760                         } else if ((t = argnxt(argp"-Wp,"))) {
761                                 u = strtok(t",");
762                                 do {
763                                         strlist_append(&preprocessor_flagsu);
764                                 } while ((u = strtok(NULL",")) != NULL);
765                         } else if (strcmp(argp"-Werror") == 0) {
766                                 strlist_append(&compiler_flags"-Werror");
plunky
1.261
767                                 strlist_append(&preprocessor_flags"-E");
ragge
1.255
768                         } else if (strcmp(argp"-Wall") == 0) {
769                                 for (Wf = WflagsWf->nameWf++)
770                                         if (Wf->flags & INWALL)
771                                                 strlist_append(&compiler_flags,
772                                                     cat("-W"Wf->name));
773                         } else if (strcmp(argp"-WW") == 0) {
774                                 for (Wf = WflagsWf->nameWf++)
775                                         strlist_append(&compiler_flags,
776                                             cat("-W"Wf->name));
777                         } else {
778                                 /* pass through, if supported */
779                                 t = &argp[2];
780                                 if (strncmp(t"no-"3) == 0)
781                                         t += 3;
782                                 if (strncmp(t"error="6) == 0)
783                                         t += 6;
784                                 for (Wf = WflagsWf->nameWf++) {
785                                         if (strcmp(tWf->name) == 0)
786                                                 strlist_append(&compiler_flags,
787                                                     argp);
788                                 }
789                         }
790                         break;
791
792                 case 'x':
793                         t = nxtopt("-x");
plunky
1.267
794                         if (match(t"none"))
795                                 strlist_append(&inputs")");
796                         else if (match(t"c"))
ragge
1.255
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);
ragge
1.265
850         for (j = 0deflibdirs[j]; j++) {
851                 if (sysroot)
852                         deflibdirs[j] = cat(sysrootdeflibdirs[j]);
ragge
1.226
853                 strlist_append(&crtdirsdeflibdirs[j]);
ragge
1.265
854         }
ragge
1.226
855
ragge
1.223
856         setup_cpp_flags();
ragge
1.226
857         setup_ccom_flags();
858         setup_as_flags();
ragge
1.223
859
860         if (isysroot == NULL)
861                 isysroot = sysroot;
862         expand_sysroot();
863
ragge
1.254
864         if (printprogname) {
865                 printf("%s\n"find_file(fname, &progdirsX_OK));
866                 return 0;
867         } else if (printfilename) {
868                 printf("%s\n"find_file(fname, &crtdirsR_OK));
869                 return 0;
870         }
871
ragge
1.239
872         msuffix = NULL;
ragge
1.226
873         STRLIST_FOREACH(s, &inputs) {
874                 char *suffix;
mickey
1.270
875                 char *ifile, *ofile = NULL;
ragge
1.226
876
877                 ifile = s->value;
plunky
1.267
878                 if (ifile[0] == ')') { /* -x source type given */
879                         msuffix = ifile[1] ? &ifile[1] : NULL;
ragge
1.239
880                         continue;
881                 }
ragge
1.255
882                 if (ifile[0] == '-' && ifile[1] == 0)
883                         suffix = msuffix ? msuffix : "c";
884                 else if (ifile[0] == '-')
ragge
1.239
885                         suffix = "o"/* source files cannot begin with - */
886                 else if (msuffix)
887                         suffix = msuffix;
888                 else
889                         suffix = getsufp(ifile);
ragge
1.128
890                 /*
891                  * C preprocessor
892                  */
plunky
1.267
893                 ascpp = match(suffix"S");
plunky
1.271
894                 if (ascpp || match(suffix"c") || cxxsuf(suffix)) {
ragge
1.226
895                         /* find out next output file */
ragge
1.233
896                         if (Mflag || MDflag) {
ragge
1.272
897                                 char *Mofile = NULL;
ragge
1.233
898
899                                 if (MFfile)
900                                         Mofile = MFfile;
901                                 else if (outfile)
902                                         Mofile = setsuf(outfile'd');
ragge
1.272
903                                 else if (MDflag)
ragge
1.233
904                                         Mofile = setsuf(ifile'd');
905                                 if (preprocess_input(ifileMofile1))
906                                         exandrm(Mofile);
907                         }
908                         if (Mflag)
909                                 continue;
910                         if (Eflag) {
ragge
1.226
911                                 /* last pass */
912                                 ofile = outfile;
913                         } else {
914                                 /* to temp file */
915                                 strlist_append(&temp_outputsofile = gettmp());
916                         }
917                         if (preprocess_input(ifileofile0))
918                                 exandrm(ofile);
919                         if (Eflag)
ragge
1.128
920                                 continue;
ragge
1.226
921                         ifile = ofile;
922                         suffix = match(suffix"S") ? "s" : "i";
ragge
1.128
923                 }
924
925                 /*
926                  * C compiler
927                  */
ragge
1.226
928                 if (match(suffix"i")) {
929                         /* find out next output file */
930                         if (Sflag) {
931                                 ofile = outfile;
932                                 if (outfile == NULL)
933                                         ofile = setsuf(s->value's');
934                         } else
935                                 strlist_append(&temp_outputsofile = gettmp());
936                         if (compile_input(ifileofile))
937                                 exandrm(ofile);