Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20120902084947

Diff

Diff from 1.249 to:

Annotations

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

Annotated File View

ragge
1.249
1 /*      $Id: cc.c,v 1.249 2012/09/02 08:49:47 ragge Exp $       */
ragge
1.222
2
3 /*-
4  * Copyright (c) 2011 Joerg Sonnenberger <joerg@NetBSD.org>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
ragge
1.128
32 /*
33  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  *
39  * Redistributions of source code and documentation must retain the above
40  * copyright notice, this list of conditions and the following disclaimer.
41  * Redistributions in binary form must reproduce the above copyright
42  * notice, this list of conditionsand the following disclaimer in the
43  * documentation and/or other materials provided with the distribution.
44  * All advertising materials mentioning features or use of this software
45  * must display the following acknowledgement:
46  *      This product includes software developed or owned by Caldera
47  *      International, Inc.
48  * Neither the name of Caldera International, Inc. nor the names of other
49  * contributors may be used to endorse or promote products derived from
50  * this software without specific prior written permission.
51  *
52  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
53  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
54  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
55  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
56  * DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
57  * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
61  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
62  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
63  * POSSIBILITY OF SUCH DAMAGE.
64  */
65
66 /*
67  * Front-end to the C compiler.
68  *
69  * Brief description of its syntax:
70  * - Files that end with .c are passed via cpp->ccom->as->ld
71  * - Files that end with .i are passed via ccom->as->ld
plunky
1.206
72  * - Files that end with .S are passed via cpp->as->ld
73  * - Files that end with .s are passed via as->ld
ragge
1.128
74  * - Files that end with .o are passed directly to ld
75  * - Multiple files may be given on the command line.
76  * - Unrecognized options are all sent directly to ld.
77  * -c or -S cannot be combined with -o if multiple files are given.
78  *
79  * This file should be rewritten readable.
80  */
81 #include "config.h"
82
83 #include <sys/types.h>
84 #ifdef HAVE_SYS_WAIT_H
85 #include <sys/wait.h>
86 #endif
87
88 #include <ctype.h>
89 #include <errno.h>
90 #include <fcntl.h>
91 #ifdef HAVE_LIBGEN_H
92 #include <libgen.h>
93 #endif
94 #include <signal.h>
95 #include <stdarg.h>
96 #include <stdio.h>
97 #include <stdlib.h>
98 #include <string.h>
99 #ifdef HAVE_UNISTD_H
100 #include <unistd.h>
101 #endif
ragge
1.222
102 #include <assert.h>
ragge
1.128
103
plunky
1.213
104 #ifdef os_win32
ragge
1.128
105 #include <windows.h>
106 #include <process.h>
107 #include <io.h>
gmcgarry
1.179
108 #define F_OK    0x00
109 #define R_OK    0x04
110 #define W_OK    0x02
111 #define X_OK    R_OK
ragge
1.128
112 #endif
113
114 #include "compat.h"
115
ragge
1.167
116 #include "macdefs.h"
ragge
1.218
117
118 #include "xalloc.h"
119 #include "strlist.h"
ragge
1.226
120
121 #include "ccconfig.h"
ragge
1.128
122 /* C command */
123
124 #define MKS(x) _MKS(x)
125 #define _MKS(x) #x
126
ragge
1.226
127 /* default program names in pcc */
128 /* May be overridden if cross-compiler is generated */
129 #ifndef CPPROGNAME
130 #define CPPROGNAME      "cpp"   /* cc used as cpp */
ragge
1.128
131 #endif
132 #ifndef PREPROCESSOR
ragge
1.226
133 #define PREPROCESSOR    "cpp"   /* "real" preprocessor name */
ragge
1.128
134 #endif
135 #ifndef COMPILER
gmcgarry
1.138
136 #define COMPILER        "ccom"
ragge
1.128
137 #endif
ragge
1.226
138 #ifndef CXXCOMPILER
139 #define CXXCOMPILER     "cxxcom"
140 #endif
ragge
1.128
141 #ifndef ASSEMBLER
142 #define ASSEMBLER       "as"
143 #endif
144 #ifndef LINKER
145 #define LINKER          "ld"
146 #endif
ragge
1.226
147 char    *passp = PREPROCESSOR;
148 char    *pass0 = COMPILER;
149 char    *passxx0 = CXXCOMPILER;
150 char    *as = ASSEMBLER;
151 char    *ld = LINKER;
152 char    *sysroot = "", *isysroot;
153
154
155 /* crt files using pcc default names */
156 #ifndef CRTBEGIN_S
157 #define CRTBEGIN_S      "crtbeginS.o"
158 #endif
159 #ifndef CRTEND_S
160 #define CRTEND_S        "crtendS.o"
161 #endif
162 #ifndef CRTBEGIN_T
163 #define CRTBEGIN_T      "crtbeginT.o"
164 #endif
165 #ifndef CRTEND_T
166 #define CRTEND_T        "crtendT.o"
167 #endif
168 #ifndef CRTBEGIN
169 #define CRTBEGIN        "crtbegin.o"
170 #endif
171 #ifndef CRTEND
172 #define CRTEND          "crtend.o"
173 #endif
174 #ifndef CRTI
175 #define CRTI            "crti.o"
176 #endif
177 #ifndef CRTN
178 #define CRTN            "crtn.o"
179 #endif
180 #ifndef CRT0
181 #define CRT0            "crt0.o"
182 #endif
183 #ifndef GCRT0
184 #define GCRT0           "gcrt0.o"
185 #endif
186
187 /* preprocessor stuff */
188 #ifndef STDINC
189 #define STDINC          "/usr/include/"
190 #endif
191
192 char *cppadd[] = CPPADD;
193 char *cppmdadd[] = CPPMDADD;
ragge
1.128
194
ragge
1.226
195 /* Dynamic linker definitions, per-target */
196 #ifndef DYNLINKER
197 #define DYNLINKER { 0 }
ragge
1.174
198 #endif
199
ragge
1.226
200 /* Default libraries and search paths */
201 #ifndef PCCLIBDIR       /* set by autoconf */
202 #define PCCLIBDIR       NULL
203 #endif
204 #ifndef DEFLIBDIRS      /* default library search paths */
205 #define DEFLIBDIRS      { "/usr/lib/", 0 }
206 #endif
207 #ifndef DEFLIBS         /* default libraries included */
208 #define DEFLIBS         { "-lpcc", "-lc", "-lpcc", 0 }
209 #endif
210 #ifndef DEFPROFLIBS     /* default profiling libraries */
211 #define DEFPROFLIBS     { "-lpcc", "-lc_p", "-lpcc", 0 }
212 #endif
213 #ifndef DEFCXXLIBS      /* default c++ libraries */
214 #define DEFCXXLIBS      { "-lp++", "-lpcc", "-lc", "-lpcc", 0 }
215 #endif
216 #ifndef STARTLABEL
217 #define STARTLABEL "__start"
218 #endif
ragge
1.174
219
ragge
1.226
220 char *dynlinker[] = DYNLINKER;
221 char *pcclibdir = PCCLIBDIR;
222 char *deflibdirs[] = DEFLIBDIRS;
223 char *deflibs[] = DEFLIBS;
224 char *defproflibs[] = DEFPROFLIBS;
225 char *defcxxlibs[] = DEFCXXLIBS;
226
ragge
1.233
227 char    *outfile, *MFfile;
ragge
1.220
228 static char **lav;
229 static int lac;
ragge
1.226
230 static char *find_file(const char *filestruct strlist *pathint mode);
231 static int preprocess_input(char *inputchar *outputint dodep);
232 static int compile_input(char *inputchar *output);
233 static int assemble_input(char *inputchar *output);
234 static int run_linker(void);
235 static int strlist_exec(struct strlist *l);
236
plunky
1.185
237 char *cat(const char *, const char *);
gmcgarry
1.150
238 char *setsuf(char *, char);
ragge
1.198
239 int cxxsuf(char *);
ragge
1.128
240 int getsuf(char *);
ragge
1.198
241 char *getsufp(char *s);
ragge
1.128
242 int main(intchar *[]);
243 void error(char *, ...);
244 void errorx(intchar *, ...);
245 int callsys(char [], char *[]);
246 int cunlink(char *);
ragge
1.226
247 void exandrm(char *);
ragge
1.128
248 void dexit(int);
249 void idexit(int);
250 char *gettmp(void);
ragge
1.218
251 void aerror(char *);
ragge
1.219
252 void oerror(char *);
ragge
1.223
253 void owarning(char *);
ragge
1.218
254 char *argnxt(char *, char *);
ragge
1.220
255 char *nxtopt(char *o);
ragge
1.222
256 void setup_cpp_flags(void);
ragge
1.226
257 void setup_ccom_flags(void);
258 void setup_as_flags(void);
259 void setup_ld_flags(void);
ragge
1.222
260 static void expand_sysroot(void);
plunky
1.213
261 #ifdef os_win32
ragge
1.128
262 char *win32pathsubst(char *);
gmcgarry
1.149
263 char *win32commandline(char *, char *[]);
ragge
1.128
264 #endif
265 int     sspflag;
plunky
1.214
266 int     freestanding;
ragge
1.226
267 int     Sflag;
ragge
1.128
268 int     cflag;
269 int     gflag;
gmcgarry
1.145
270 int     rflag;
ragge
1.128
271 int     vflag;
272 int     tflag;
273 int     Eflag;
274 int     Oflag;
275 int     kflag;  /* generate PIC/pic code */
276 #define F_PIC   1
277 #define F_pic   2
ragge
1.233
278 int     MflagneedMMDflag;   /* dependencies only */
ragge
1.128
279 int     pgflag;
280 int     Xflag;
281 int     nostartfilesBstaticshared;
282 int     nostdincnostdlib;
283 int     pthreads;
ragge
1.239
284 int     xgnu89xgnu99;
ragge
1.128
285 int     ascpp;
plunky
1.196
286 #ifdef CHAR_UNSIGNED
plunky
1.209
287 int     xuchar = 1;
plunky
1.196
288 #else
plunky
1.209
289 int     xuchar = 0;
plunky
1.196
290 #endif
ragge
1.198
291 int     cxxflag;
ragge
1.216
292 int     cppflag;
ragge
1.128
293
ragge
1.164
294 #ifdef mach_amd64
295 int amd64_i386;
296 #endif
ragge
1.128
297
ragge
1.218
298 #define match(a,b)      (strcmp(a,b) == 0)
299
ragge
1.128
300 /* handle gcc warning emulations */
301 struct Wflags {
302         char *name;
303         int flags;
304 #define INWALL          1
305 Wflags[] = {
plunky
1.215
306         { "truncate"0 },
307         { "strict-prototypes"0 },
308         { "missing-prototypes"0 },
309         { "implicit-int"INWALL },
310         { "implicit-function-declaration"INWALL },
311         { "shadow"0 },
312         { "pointer-sign"INWALL },
313         { "sign-compare"0 },
314         { "unknown-pragmas"INWALL },
315         { "unreachable-code"0 },
316         { NULL0 },
ragge
1.128
317 };
318
ragge
1.158
319 #ifndef USHORT
320 /* copied from mip/manifest.h */
321 #define USHORT          5
322 #define INT             6
323 #define UNSIGNED        7
324 #endif
325
ragge
1.157
326 /*
327  * Wide char defines.
328  */
329 #if WCHAR_TYPE == USHORT
330 #define WCT "short unsigned int"
331 #define WCM "65535U"
ragge
1.158
332 #if WCHAR_SIZE != 2
333 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
334 #endif
ragge
1.157
335 #elif WCHAR_TYPE == INT
336 #define WCT "int"
337 #define WCM "2147483647"
ragge
1.158
338 #if WCHAR_SIZE != 4
339 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
340 #endif
ragge
1.157
341 #elif WCHAR_TYPE == UNSIGNED
342 #define WCT "unsigned int"
ragge
1.158
343 #define WCM "4294967295U"
344 #if WCHAR_SIZE != 4
345 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
346 #endif
347 #else
348 #error WCHAR_TYPE not defined or invalid
ragge
1.157
349 #endif
350
ragge
1.160
351 #ifdef GCC_COMPAT
352 #ifndef REGISTER_PREFIX
353 #define REGISTER_PREFIX ""
354 #endif
355 #ifndef USER_LABEL_PREFIX
356 #define USER_LABEL_PREFIX ""
357 #endif
358 #endif
359
plunky
1.192
360 #ifndef PCC_WINT_TYPE
361 #define PCC_WINT_TYPE "unsigned int"
362 #endif
363
364 #ifndef PCC_SIZE_TYPE
365 #define PCC_SIZE_TYPE "unsigned long"
366 #endif
367
ragge
1.161
368 #ifndef PCC_PTRDIFF_TYPE
369 #define PCC_PTRDIFF_TYPE "long int"
370 #endif
371
ragge
1.216
372
ragge
1.218
373 struct strlist preprocessor_flags;
ragge
1.223
374 struct strlist depflags;
ragge
1.219
375 struct strlist incdirs;
ragge
1.221
376 struct strlist user_sysincdirs;
377 struct strlist includes;
ragge
1.222
378 struct strlist sysincdirs;
ragge
1.223
379 struct strlist dirafterdirs;
ragge
1.222
380 struct strlist crtdirs;
381 struct strlist libdirs;
382 struct strlist progdirs;
ragge
1.224
383 struct strlist early_linker_flags;
384 struct strlist middle_linker_flags;
385 struct strlist late_linker_flags;
386 struct strlist inputs;
ragge
1.225
387 struct strlist assembler_flags;
388 struct strlist temp_outputs;
ragge
1.226
389 struct strlist compiler_flags;
ragge
1.218
390
ragge
1.128
391 int
392 main(int argcchar *argv[])
393 {
394         struct Wflags *Wf;
ragge
1.226
395         struct string *s;
ragge
1.218
396         char *t, *u, *argp;
ragge
1.239
397         char *msuffix;
ragge
1.226
398         int ninputj;
ragge
1.222
399
ragge
1.220
400         lav = argv;
401         lac = argc;
plunky
1.242
402         ninput = 0;
ragge
1.128
403
ragge
1.222
404         strlist_init(&crtdirs);
405         strlist_init(&libdirs);
406         strlist_init(&progdirs);
ragge
1.218
407         strlist_init(&preprocessor_flags);
ragge
1.219
408         strlist_init(&incdirs);
ragge
1.221
409         strlist_init(&user_sysincdirs);
410         strlist_init(&includes);
ragge
1.222
411         strlist_init(&sysincdirs);
ragge
1.223
412         strlist_init(&dirafterdirs);
413         strlist_init(&depflags);
ragge
1.224
414         strlist_init(&early_linker_flags);
415         strlist_init(&middle_linker_flags);
416         strlist_init(&late_linker_flags);
417         strlist_init(&inputs);
ragge
1.225
418         strlist_init(&assembler_flags);
419         strlist_init(&temp_outputs);
ragge
1.226
420         strlist_init(&compiler_flags);
ragge
1.218
421
ragge
1.216
422         if ((t = strrchr(argv[0], '/')))
ragge
1.226
423                 t++;
ragge
1.229
424         else
425                 t = argv[0];
ragge
1.226
426
427         if (match(t"p++")) {
ragge
1.198
428                 cxxflag = 1;
ragge
1.226
429         } else if (match(t"cpp") || match(tCPPROGNAME)) {
430                 Eflag = cppflag = 1;
ragge
1.198
431         }
432
ragge
1.246
433 #ifdef EARLY_SETUP
434         EARLY_SETUP;
435 #endif
436
plunky
1.213
437 #ifdef os_win32
ragge
1.128
438         /* have to prefix path early.  -B may override */
439         incdir = win32pathsubst(incdir);
gmcgarry
1.153
440         altincdir = win32pathsubst(altincdir);
gmcgarry
1.136
441         libdir = win32pathsubst(libdir);
plunky
1.200
442 #ifdef PCCINCDIR
ragge
1.128
443         pccincdir = win32pathsubst(pccincdir);
plunky
1.199
444         pxxincdir = win32pathsubst(pxxincdir);
plunky
1.200
445 #endif
446 #ifdef PCCLIBDIR
ragge
1.128
447         pcclibdir = win32pathsubst(pcclibdir);
plunky
1.200
448 #endif
ragge
1.128
449         passp = win32pathsubst(passp);
450         pass0 = win32pathsubst(pass0);
gmcgarry
1.180
451 #ifdef STARTFILES
452         for (i = 0startfiles[i] != NULLi++)
453                 startfiles[i] = win32pathsubst(startfiles[i]);
454         for (i = 0endfiles[i] != NULLi++)
455                 endfiles[i] = win32pathsubst(endfiles[i]);
456 #endif
457 #ifdef STARTFILES_T
458         for (i = 0startfiles_T[i] != NULLi++)
459                 startfiles_T[i] = win32pathsubst(startfiles_T[i]);
460         for (i = 0endfiles_T[i] != NULLi++)
461                 endfiles_T[i] = win32pathsubst(endfiles_T[i]);
462 #endif
463 #ifdef STARTFILES_S
464         for (i = 0startfiles_S[i] != NULLi++)
465                 startfiles_S[i] = win32pathsubst(startfiles_S[i]);
466         for (i = 0endfiles_S[i] != NULLi++)
467                 endfiles_S[i] = win32pathsubst(endfiles_S[i]);
468 #endif
ragge
1.128
469 #endif
470
ragge
1.220
471         while (--lac) {
472                 ++lav;
473                 argp = *lav;
474
ragge
1.227
475 #ifdef EARLY_ARG_CHECK
476                 EARLY_ARG_CHECK;
477 #endif
478
ragge
1.225
479                 if (*argp != '-' || match(argp"-")) {
480                         /* Check for duplicate .o files. */
481                         if (getsuf(argp) == 'o') {
482                                 j = 0;
483                                 STRLIST_FOREACH(s, &inputs)
484                                         if (match(argps->value))
485                                                 j++;
486                                 if (j)
487                                         continue/* skip it */
488                         }
489                         strlist_append(&inputsargp);
plunky
1.242
490                         ninput++;
ragge
1.225
491                         continue;
492                 }
ragge
1.220
493
494                 switch (argp[1]) {
495                 default:
ragge
1.224
496                         owarning(argp);
ragge
1.220
497                         break;
ragge
1.128
498
ragge
1.220
499                 case '-'/* double -'s */
500                         if (match(argp"--version")) {
501                                 printf("%s\n"VERSSTR);
502                                 return 0;
503                         } else if (strncmp(argp"--sysroot="10) == 0) {
504                                 sysroot = argp + 10;
505                         } else if (strcmp(argp"--param") == 0) {
506                                 /* NOTHING YET */;
507                                 (void)nxtopt(0); /* ignore arg */
508                         } else
ragge
1.225
509                                 owarning(argp);
ragge
1.220
510                         break;
ragge
1.128
511
ragge
1.220
512                 case 'B'/* other search paths for binaries */
ragge
1.224
513                         t = nxtopt("-B");
514                         strlist_append(&crtdirst);
515                         strlist_append(&libdirst);
516                         strlist_append(&progdirst);
ragge
1.220
517                         break;
ragge
1.128
518
ragge
1.220
519                 case 'C':
520                         if (match(argp"-C") || match(argp"-CC"))
521                                 strlist_append(&preprocessor_flagsargp);
522                         else
523                                 oerror(argp);
524                         break;
ragge
1.218
525
ragge
1.220
526                 case 'X':
527                         Xflag++;
528                         break;
529                 case 'W'/* Ignore (most of) W-flags */
ragge
1.225
530                         if ((t = argnxt(argp"-Wl,"))) {
531                                 u = strtok(t",");
532                                 do {
ragge
1.235
533                                         strlist_append(&inputsu);
ragge
1.225
534                                 } while ((u = strtok(NULL",")) != NULL);
535                         } else if ((t = argnxt(argp"-Wa,"))) {
536                                 u = strtok(t",");
537                                 do {
538                                         strlist_append(&assembler_flagsu);
539                                 } while ((u = strtok(NULL",")) != NULL);
ragge
1.226
540                         } else if ((t = argnxt(argp"-Wc,"))) {
541                                 u = strtok(t",");
542                                 do {
543                                         strlist_append(&compiler_flagsu);
544                                 } while ((u = strtok(NULL",")) != NULL);
ragge
1.220
545                         } else if ((t = argnxt(argp"-Wp,"))) {
546                                 u = strtok(t",");
547                                 do {
548                                         strlist_append(&preprocessor_flagsu);
549                                 } while ((u = strtok(NULL",")) != NULL);
550                         } else if (strcmp(argp"-Werror") == 0) {
ragge
1.226
551                                 strlist_append(&compiler_flags"-Werror");
ragge
1.220
552                         } else if (strcmp(argp"-Wall") == 0) {
ragge
1.238
553                                 for (Wf = WflagsWf->nameWf++)
554                                         if (Wf->flags & INWALL)
555                                                 strlist_append(&compiler_flags,
556                                                     cat("-W"Wf->name));
ragge
1.220
557                         } else if (strcmp(argp"-WW") == 0) {
ragge
1.238
558                                 for (Wf = WflagsWf->nameWf++)
559                                         strlist_append(&compiler_flags,
560                                             cat("-W"Wf->name));
ragge
1.220
561                         } else {
562                                 /* pass through, if supported */
563                                 t = &argp[2];
564                                 if (strncmp(t"no-"3) == 0)
565                                         t += 3;
566                                 if (strncmp(t"error="6) == 0)
567                                         t += 6;
568                                 for (Wf = WflagsWf->nameWf++) {
569                                         if (strcmp(tWf->name) == 0)
ragge
1.226
570                                                 strlist_append(&compiler_flags,
571                                                     argp);
ragge
1.128
572                                 }
ragge
1.220
573                         }
574                         break;
ragge
1.128
575
ragge
1.220
576                 case 'f'/* GCC compatibility flags */
ragge
1.246
577                         u = &argp[2];
578                         j = 0;
579                         if (strncmp(u"no-"3) == 0)
580                                 j = 1u += 3;
581                         if (match(u"PIC") || match(u"pic")) {
582                                 kflag = j ? 0 : *u == 'P' ? F_PIC : F_pic;
583                         } else if (match(u"freestanding")) {
584                                 freestanding = j ? 0 : 1;
585                         } else if (match(u"signed-char")) {
586                                 xuchar = j ? 1 : 0;
587                         } else if (match(u"unsigned-char")) {
588                                 xuchar = j ? 0 : 1;
plunky
1.248
589                         } else if (match(u"stack-protector") ||
590                             match(u"stack-protector-all")) {
ragge
1.246
591                                 sspflag = j ? 0 : 1;
ragge
1.220
592                         }
593                         /* silently ignore the rest */
594                         break;
ragge
1.128
595
ragge
1.220
596                 case 'g'/* create debug output */
597                         if (argp[2] == '0')
598                                 gflag = 0;
599                         else
600                                 gflag++;
601                         break;
ragge
1.128
602
ragge
1.220
603                 case 'D':
604                 case 'U':
ragge
1.221
605                         strlist_append(&preprocessor_flagsargp);
ragge
1.220
606                         if (argp[2] != 0)
ragge
1.219
607                                 break;
ragge
1.221
608                         strlist_append(&preprocessor_flagsnxtopt(argp));
609                         break;
610
611                 case 'I'/* Add include dirs */
612                         strlist_append(&incdirsnxtopt("-I"));
ragge
1.220
613                         break;
ragge
1.219
614
ragge
1.220
615                 case 'i':
ragge
1.221
616                         if (match(argp"-isystem")) {
617                                 strlist_append(&user_sysincdirsnxtopt(0));
618                         } else if (match(argp"-include")) {
619                                 strlist_append(&includesnxtopt(0));
ragge
1.222
620                         } else if (match(argp"-isysroot")) {
621                                 isysroot = nxtopt(0);
ragge
1.220
622                         } else if (strcmp(argp"-idirafter") == 0) {
ragge
1.223
623                                 strlist_append(&dirafterdirsnxtopt(0));
ragge
1.220
624                         } else
ragge
1.225
625                                 owarning(argp);
ragge
1.220
626                         break;
ragge
1.128
627
ragge
1.220
628                 case 'k'/* generate PIC code */
ragge
1.247
629                         kflag = argp[2] ? argp[2] - '0' : F_pic;
ragge
1.220
630                         break;
ragge
1.128
631
ragge
1.224
632                 case 'l':
633                 case 'L':
ragge
1.226
634                         if (argp[2] == 0)
ragge
1.245
635                                 argp = cat(argpnxtopt(0));
636                         strlist_append(&inputsargp);
ragge
1.224
637                         break;
638
ragge
1.220
639                 case 'm'/* target-dependent options */
ragge
1.164
640 #ifdef mach_amd64
ragge
1.220
641                         /* need to call i386 ccom for this */
642                         if (strcmp(argp"-m32") == 0) {
643                                 pass0 = LIBEXECDIR "/ccom_i386";
644                                 amd64_i386 = 1;
645                                 break;
646                         }
ragge
1.164
647 #endif
ragge
1.225
648                         strlist_append(&middle_linker_flagsargp);
ragge
1.220
649                         if (argp[2] == 0) {
ragge
1.225
650                                 t = nxtopt(0);
651                                 strlist_append(&middle_linker_flagst);
ragge
1.220
652                         }
653                         break;
ragge
1.128
654
ragge
1.220
655                 case 'n'/* handle -n flags */
656                         if (strcmp(argp"-nostdinc") == 0)
657                                 nostdinc++;
658                         else if (strcmp(argp"-nostdlib") == 0) {
659                                 nostdlib++;
660                                 nostartfiles++;
661                         } else if (strcmp(argp"-nostartfiles") == 0)
662                                 nostartfiles = 1;
663                         else if (strcmp(argp"-nodefaultlibs") == 0)
664                                 nostdlib++;
665                         else
ragge
1.225
666                                 owarning(argp);
ragge
1.220
667                         break;
ragge
1.128
668
ragge
1.220
669                 case 'p':
670                         if (strcmp(argp"-pg") == 0 ||
671                             strcmp(argp"-p") == 0)
672                                 pgflag++;
673                         else if (strcmp(argp"-pthread") == 0)
674                                 pthreads++;
675                         else if (strcmp(argp"-pipe") == 0)
676                                 /* NOTHING YET */;
677                         else if (strcmp(argp"-pedantic") == 0)
678                                 /* NOTHING YET */;
679                         else if (strcmp(argp,
680                             "-print-prog-name=ld") == 0) {
681                                 printf("%s\n"LINKER);
682                                 return 0;
ragge
1.226
683 #ifdef notdef
684                         /* does not exist in gcc??? */
ragge
1.220
685                         } else if (strcmp(argp,
686                             "-print-multi-os-directory") == 0) {
687                                 printf("%s\n"MULTIOSDIR);
688                                 return 0;
ragge
1.226
689 #endif
ragge
1.220
690                         } else
691                                 oerror(argp);
692                         break;
ragge
1.128
693
ragge
1.220
694                 case 'r':
695                         rflag = 1;
696                         break;
gmcgarry
1.145
697
ragge
1.220
698                 case 'x':
699                         t = nxtopt("-x");
700                         if (match(t"c"))
ragge
1.239
701                                 strlist_append(&inputs")c");
ragge
1.220
702                         else if (match(t"assembler"))
ragge
1.239
703                                 strlist_append(&inputs")s");
ragge
1.220
704                         else if (match(t"assembler-with-cpp"))
ragge
1.239
705                                 strlist_append(&inputs")S");
ragge
1.220
706                         else if (match(t"c++"))
ragge
1.239
707                                 strlist_append(&inputs")c++");
ragge
1.226
708                         else {
709                                 strlist_append(&compiler_flags"-x");
710                                 strlist_append(&compiler_flagst);
711                         }
ragge
1.220
712                         break;
ragge
1.240
713                 case 'T':
714                         strlist_append(&inputsargp);
mickey
1.241
715                         if (argp[2] == 0 ||
716                             strcmp(argp"-Ttext") == 0 ||
717                             strcmp(argp"-Tdata") == 0 ||
718                             strcmp(argp"-Tbss") == 0)
719                                 strlist_append(&inputsnxtopt(0));
ragge
1.240
720                         break;
ragge
1.220
721                 case 't':
722                         tflag++;
723                         break;
724                 case 'S':
ragge
1.226
725                         Sflag++;
ragge
1.220
726                         cflag++;
727                         break;
728                 case 'o':
729                         if (outfile)
730                                 errorx(8"too many -o");
731                         outfile = nxtopt("-o");
732                         break;
733                 case 'O':
734                         if (argp[2] == '\0')
735                                 Oflag++;
ragge
1.226
736                         else if (argp[3] == '\0' &&
737                             isdigit((unsigned char)argp[2]))
ragge
1.220
738                                 Oflag = argp[2] - '0';
739                         else if (argp[3] == '\0' && argp[2] == 's')
740                                 Oflag = 1;      /* optimize for space only */
741                         else
742                                 oerror(argp);
743                         break;
744                 case 'E':
745                         Eflag++;
746                         break;
747                 case 'P':
ragge
1.221
748                         strlist_append(&preprocessor_flagsargp);
749                         break;
750
ragge
1.220
751                 case 'c':
ragge
1.227
752                         cflag++;
ragge
1.220
753                         break;
ragge
1.223
754
ragge
1.220
755                 case 'M':
ragge
1.233
756                         needM = 1;
757                         if (match(argp"-M")) {
ragge
1.223
758                                 Mflag++;
759                                 strlist_append(&depflagsargp);
ragge
1.233
760                         } else if (match(argp"-MP")) {
ragge
1.223
761                                 strlist_append(&depflags"-xMP");
ragge
1.233
762                         } else if (match(argp"-MF")) {
763                                 MFfile = nxtopt("-MF");
764                         } else if (match(argp"-MT") || match(argp"-MQ")) {
ragge
1.226
765                                 t = cat("-xMT,"nxtopt("-MT"));
ragge
1.220
766                                 t[3] = argp[2];
ragge
1.223
767                                 strlist_append(&depflagst);
ragge
1.233
768                         } else if (match(argp"-MD")) {
769                                 MDflag++;
770                                 needM = 0;
771                                 strlist_append(&depflags"-M");
772                         } else
ragge
1.220
773                                 oerror(argp);
774                         break;
ragge
1.128
775
ragge
1.220
776                 case 'd':
ragge
1.227
777                         owarning(argp);
ragge
1.220
778                         break;
779                 case 'v':
780                         printf("%s\n"VERSSTR);
781                         vflag++;
782                         break;
ragge
1.128
783
ragge
1.220
784                 case 's':
ragge
1.235
785                         if (match(argp"-shared")) {
ragge
1.220
786                                 shared = 1;
ragge
1.235
787                         } else if (match(argp"-static")) {
ragge
1.220
788                                 Bstatic = 1;
ragge
1.225
789                         } else if (match(argp"-symbolic")) {
790                                 strlist_append(&middle_linker_flags,
791                                     "-Bsymbolic");
ragge
1.220
792                         } else if (strncmp(argp"-std"4) == 0) {
793                                 if (strcmp(&argp[5], "gnu99") == 0 ||
794                                     strcmp(&argp[5], "gnu9x") == 0)
795                                         xgnu99 = 1;
796                                 if (strcmp(&argp[5], "gnu89") == 0)
797                                         xgnu89 = 1;
798                         } else
ragge
1.225
799                                 owarning(argp);
ragge
1.220
800                         break;
801                 }
802                 continue;
803
ragge
1.128
804         }
ragge
1.222
805
ragge
1.128
806         /* Sanity checking */
ragge
1.216
807         if (cppflag) {
ragge
1.224
808                 if (ninput == 0) {
809                         strlist_append(&inputs"-");
ragge
1.232
810                         ninput++;
ragge
1.224
811                 } else if (ninput > 2 || (ninput == 2 && outfile)) {
ragge
1.216
812                         errorx(8"too many files");
ragge
1.232
813                 } else if (ninput == 2) {
814                         outfile = STRLIST_NEXT(STRLIST_FIRST(&inputs))->value;
815                         STRLIST_FIRST(&inputs)->next = NULL;
816                         ninput--;
ragge
1.224
817                 }
ragge
1.216
818         }
ragge
1.225
819         if (ninput == 0)
ragge
1.128
820                 errorx(8"no input files");
ragge
1.226
821         if (outfile && (cflag || Sflag || Eflag) && ninput > 1)
ragge
1.128
822                 errorx(8"-o given with -c || -E || -S and more than one file");
ragge
1.224
823 #if 0
ragge
1.128
824         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
825                 errorx(8"output file will be clobbered");
ragge
1.224
826 #endif
ragge
1.223
827
ragge
1.233
828         if (needM && !Mflag && !MDflag)
ragge
1.223
829                 errorx(8"to make dependencies needs -M");
830
831
ragge
1.128
832         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
833                 signal(SIGINTidexit);
834         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
835                 signal(SIGTERMidexit);
ragge
1.222
836
ragge
1.226
837         /* after arg parsing */
838         strlist_append(&progdirsLIBEXECDIR);
839         if (pcclibdir)
840                 strlist_append(&crtdirspcclibdir);
841         for (j = 0deflibdirs[j]; j++)
842                 strlist_append(&crtdirsdeflibdirs[j]);
843
ragge
1.223
844         setup_cpp_flags();
ragge
1.226
845         setup_ccom_flags();
846         setup_as_flags();
ragge
1.223
847
848         if (isysroot == NULL)
849                 isysroot = sysroot;
850         expand_sysroot();
851
ragge
1.239
852         msuffix = NULL;
ragge
1.226
853         STRLIST_FOREACH(s, &inputs) {
854                 char *suffix;
855                 char *ifile, *ofile;
856
857                 ifile = s->value;
858                 if (ninput > 1 && !Eflag)
859                         printf("%s:\n"ifile);
860
ragge
1.239
861                 if (ifile[0] == ')') {
862                         msuffix = &ifile[1]; /* -x source type given */
863                         ascpp = match(msuffix"S");
864                         continue;
865                 }
866                 if (ifile[0] == '-' && ifile[1] != 0)
867                         suffix = "o"/* source files cannot begin with - */
868                 else if (msuffix)
869                         suffix = msuffix;
870                 else
871                         suffix = getsufp(ifile);
ragge
1.128
872                 /*
873                  * C preprocessor
874                  */
ragge
1.226
875                 if (match(suffix"c") || match(suffix"S") ||
876                     cxxsuf(s->value)) {
877                         /* find out next output file */
ragge
1.233
878                         if (Mflag || MDflag) {
879                                 char *Mofile;
880
881                                 if (MFfile)
882                                         Mofile = MFfile;
883                                 else if (outfile)
884                                         Mofile = setsuf(outfile'd');
885                                 else
886                                         Mofile = setsuf(ifile'd');
887                                 if (preprocess_input(ifileMofile1))
888                                         exandrm(Mofile);
889                         }
890                         if (Mflag)
891                                 continue;
892                         if (Eflag) {
ragge
1.226
893                                 /* last pass */
894                                 ofile = outfile;
895                         } else {
896                                 /* to temp file */
897                                 strlist_append(&temp_outputsofile = gettmp());
898                         }
899                         if (preprocess_input(ifileofile0))
900                                 exandrm(ofile);
901                         if (Eflag)
ragge
1.128
902                                 continue;
ragge
1.226
903                         ifile = ofile;
904                         suffix = match(suffix"S") ? "s" : "i";
ragge
1.128
905                 }
906
907                 /*
908                  * C compiler
909                  */
ragge
1.226
910                 if (match(suffix"i")) {
911                         /* find out next output file */
912                         if (Sflag) {
913                                 ofile = outfile;
914                                 if (outfile == NULL)
915                                         ofile = setsuf(s->value's');
916                         } else
917                                 strlist_append(&temp_outputsofile = gettmp());
918                         if (compile_input(ifileofile))
919                                 exandrm(ofile);
920                         if (Sflag)
921                                 continue;
922                         ifile = ofile;
923                         suffix = "s";
ragge
1.128
924                 }
925
926                 /*