Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20140820194220

Diff

Diff from 1.280 to:

Annotations

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

Annotated File View

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