Debian Codegraph @

cc/cc/cc.c

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
1 /* $Id: cc.c,v 1.328 2021/10/15 15:33:09 ragge Exp $ */
2
3 /*-
4 * Copyright (c) 2011 Joerg Sonnenberger <[email protected]>.
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
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
72 * - Files that end with .S are passed via cpp->as->ld
73 * - Files that end with .s are passed via as->ld
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 #include <signal.h>
92 #include <stdarg.h>
93 #include <stdio.h>
94 #include <stdlib.h>
95 #include <string.h>
96 #ifdef HAVE_UNISTD_H
97 #include <unistd.h>
98 #endif
99 #include <assert.h>
100 #include <time.h>
101
102 #ifdef _WIN32
103 #include <windows.h>
104 #include <process.h>
105 #include <io.h>
106 #define F_OK 0x00
107 #define R_OK 0x04
108 #define W_OK 0x02
109 #define X_OK R_OK
110 #endif
111
112 #include "compat.h"
113
114 #include "macdefs.h"
115
116 #include "xalloc.h"
117 #include "strlist.h"
118
119 #include "ccconfig.h"
120 /* C command */
121
122 #define CC_DRIVER
123 #include "softfloat.h" /* for CPP floating point macros */
124
125 #define MKS(x) _MKS(x)
126 #define _MKS(x) #x
127
128 /* default program names in pcc */
129 /* May be overridden if cross-compiler is generated */
130 #ifndef CXXPROGNAME /* name as C++ front end */
•131 #define CXXPROGNAME "++"
132 #endif
133 #ifndef CPPROGNAME
134 #define CPPROGNAME "cpp" /* name as CPP front end */
135 #endif
136 #ifndef PREPROCESSOR
137 #define PREPROCESSOR "cpp" /* "real" preprocessor name */
138 #endif
139 #ifndef COMPILER
140 #define COMPILER "ccom"
141 #endif
142 #ifndef CXXCOMPILER
143 #define CXXCOMPILER "cxxcom"
144 #endif
145 #ifndef ASSEMBLER
146 #define ASSEMBLER "as"
147 #endif
148 #ifndef LINKER
149 #define LINKER "ld"
150 #endif
151
152 #ifndef CC0
153 #define CC0 "cc0"
154 #endif
155 #ifndef CC1
156 #define CC1 "cc1"
157 #endif
158 #ifndef CC2
159 #define CC2 "cc2"
160 #endif
161 #ifndef CXX0
162 #define CXX0 "cxx0"
163 #endif
164 #ifndef CXX1
165 #define CXX1 "cxx1"
166 #endif
167
168 char *passp = PREPROCESSOR;
169 char *pass0 = COMPILER;
170 char *passxx0 = CXXCOMPILER;
171 char *as = ASSEMBLER;
172 char *ld = LINKER;
173 char *sysroot = "", *isysroot;
174
175
176 /* crt files using pcc default names */
177 #ifndef CRTBEGIN_S
178 #define CRTBEGIN_S "crtbeginS.o"
179 #endif
180 #ifndef CRTEND_S
181 #define CRTEND_S "crtendS.o"
182 #endif
183 #ifndef CRTBEGIN_T
184 #define CRTBEGIN_T "crtbeginT.o"
185 #endif
186 #ifndef CRTEND_T
187 #define CRTEND_T "crtendT.o"
188 #endif
189 #ifndef CRTBEGIN
190 #define CRTBEGIN "crtbegin.o"
191 #endif
192 #ifndef CRTEND
193 #define CRTEND "crtend.o"
194 #endif
195 #ifndef CRTI
196 #define CRTI "crti.o"
197 #endif
198 #ifndef CRTN
199 #define CRTN "crtn.o"
200 #endif
201 #ifndef CRT0
202 #define CRT0 "crt0.o"
203 #endif
204 #ifndef GCRT0
205 #define GCRT0 "gcrt0.o"
206 #endif
207 #ifndef RCRT0
208 #define RCRT0 "rcrt0.o"
209 #endif
210
211 /* preprocessor stuff */
212 #ifndef STDINC
213 #define STDINC "/usr/include/"
214 #endif
215 #ifdef MULTIARCH_PATH
216 #define STDINC_MA STDINC MULTIARCH_PATH "/"
217 #endif
218
219
220 char *cppadd[] = CPPADD;
221 char *cppmdadd[] = CPPMDADD;
222
223 /* Default libraries and search paths */
224 #ifndef PCCLIBDIR /* set by autoconf */
225 #define PCCLIBDIR NULL
226 #endif
227 #ifndef LIBDIR
228 #define LIBDIR "/usr/lib/"
229 #endif
230 #ifndef DEFLIBDIRS /* default library search paths */
231 #ifdef MULTIARCH_PATH
232 #define DEFLIBDIRS { LIBDIR, LIBDIR MULTIARCH_PATH "/", 0 }
233 #else
234 #define DEFLIBDIRS { LIBDIR, 0 }
235 #endif
236 #endif
237 #ifndef DEFLIBS /* default libraries included */
238 #define DEFLIBS { "-lpcc", "-lc", "-lpcc", 0 }
239 #endif
240 #ifndef DEFPROFLIBS /* default profiling libraries */
241 #define DEFPROFLIBS { "-lpcc", "-lc_p", "-lpcc", 0 }
242 #endif
243 #ifndef DEFCXXLIBS /* default c++ libraries */
•244 #define DEFCXXLIBS { "-lpcc", "-lc", "-lpcc", 0 }
245 #endif
246 #ifndef STARTLABEL
247 #define STARTLABEL "__start"
248 #endif
249 #ifndef DYNLINKARG
250 #define DYNLINKARG "-dynamic-linker"
251 #endif
252 #ifndef DYNLINKLIB
253 #define DYNLINKLIB NULL
254 #endif
255
256 char *dynlinkarg = DYNLINKARG;
257 char *dynlinklib = DYNLINKLIB;
258 char *pcclibdir = PCCLIBDIR;
259 char *deflibdirs[] = DEFLIBDIRS;
260 char *deflibs[] = DEFLIBS;
261 char *defproflibs[] = DEFPROFLIBS;
262 char *defcxxlibs[] = DEFCXXLIBS;
263
264 char *outfile, *MFfile, *fname;
265 static char **lav;
266 static int lac;
267 static char *find_file(const char *file, struct strlist *path, int mode);
268 static int preprocess_input(char *input, char *output, int dodep);
269 static int compile_input(char *input, char *output);
270 static int assemble_input(char *input, char *output);
271 static int run_linker(void);
272 static int strlist_exec(struct strlist *l);
273 static char *select_linker(char *);
274
275 char *cat(const char *, const char *);
276 char *setsuf(char *, char);
277 int cxxsuf(char *);
278 int getsuf(char *);
279 char *getsufp(char *s);
280 int main(int, char *[]);
281 void errorx(int, char *, ...);
282 int cunlink(char *);
283 void exandrm(char *);
284 void dexit(int);
285 void idexit(int);
286 char *gettmp(void);
287 void oerror(char *);
288 char *argnxt(char *, char *);
289 char *nxtopt(char *o);
290 void setup_cpp_flags(void);
291 void setup_ccom_flags(void);
292 void setup_as_flags(void);
293 void setup_ld_flags(void);
294 static void expand_sysroot(void);
295 #ifdef _WIN32
296 char *win32pathsubst(char *);
297 char *win32commandline(struct strlist *l);
298 #endif
299 int sspflag;
300 int freestanding;
301 int Sflag;
302 int cflag;
303 int gflag;
304 int rflag;
305 int vflag;
306 int noexec; /* -### */
307 int tflag;
308 int Eflag;
309 int Oflag;
310 int kflag; /* generate PIC/pic code */
311 #define F_PIC 1
312 #define F_pic 2
313 int Mflag, needM, MDflag, MMDflag; /* dependencies only */
314 int pgflag;
315 int pieflag;
316 int Xflag;
317 int nostartfiles, Bstatic, shared;
318 int nostdinc, nostdlib;
319 int pthreads;
320 int xgnu89, xgnu99, c89defs, c99defs, c11defs;
321 int ascpp;
322 #ifdef CHAR_UNSIGNED
323 int xuchar = 1;
324 #else
325 int xuchar = 0;
326 #endif
327 int cxxflag;
328 int cppflag;
329 int printprogname, printfilename, printsearchdirs;
330 enum { SC11, STRAD, SC89, SGNU89, SC99, SGNU99 } cstd;
331
332 #ifdef SOFTFLOAT
333 int softfloat = 1;
334 #else
335 int softfloat = 0;
336 #endif
337
338 #ifdef TARGET_BIG_ENDIAN
339 int bigendian = 1;
340 #else
341 int bigendian = 0;
342 #endif
343
344 #ifdef mach_amd64
345 int amd64_i386;
346 #endif
347
348 #define match(a,b) (strcmp(a,b) == 0)
•349 #define endsWith(string,suffix) ( \
350 strlen(string) >= sizeof(suffix) - 1 && \
351 !strcmp(string + strlen(string) - sizeof(suffix) + 1, suffix) \
352 )
353
354 /* handle gcc warning emulations */
355 struct Wflags {
356 char *name;
357 int flags;
358 #define INWALL 1
359 #define INWEXTRA 2
360 } Wflags[] = {
361 { "truncate", 0 },
362 { "strict-prototypes", 0 },
363 { "missing-prototypes", INWEXTRA },
364 { "implicit-int", INWALL },
365 { "implicit-function-declaration", INWALL },
366 { "shadow", INWEXTRA },
367 { "pointer-sign", INWALL },
368 { "sign-compare", INWEXTRA },
369 { "unknown-pragmas", INWALL },
370 { "unreachable-code", INWEXTRA },
371 { "deprecated-declarations", INWEXTRA },
372 { "attributes", 0 },
373 { "uninitialized", INWEXTRA },
374 { NULL, 0 },
375 };
376
377 #ifndef USHORT
378 /* copied from mip/manifest.h */
379 #define USHORT 5
380 #define INT 6
381 #define UNSIGNED 7
382 #endif
383
384 /*
385 * Wide char defines.
386 */
387 #if WCHAR_TYPE == USHORT
388 #define WCT "short unsigned int"
389 #define WCM "65535U"
390 #if WCHAR_SIZE != 2
391 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
392 #endif
393 #elif WCHAR_TYPE == INT
394 #define WCT "int"
395 #define WCM "2147483647"
396 #if WCHAR_SIZE != 4
397 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
398 #endif
399 #elif WCHAR_TYPE == UNSIGNED
400 #define WCT "unsigned int"
401 #define WCM "4294967295U"
402 #if WCHAR_SIZE != 4
403 #error WCHAR_TYPE vs. WCHAR_SIZE mismatch
404 #endif
405 #else
406 #error WCHAR_TYPE not defined or invalid
407 #endif
408
409 #ifdef GCC_COMPAT
410 #ifndef REGISTER_PREFIX
411 #define REGISTER_PREFIX ""
412 #endif
413 #ifndef USER_LABEL_PREFIX
414 #define USER_LABEL_PREFIX ""
415 #endif
416 #endif
417
418 #ifndef PCC_WINT_TYPE
419 #define PCC_WINT_TYPE "unsigned int"
420 #endif
421
422 #ifndef PCC_SIZE_TYPE
423 #define PCC_SIZE_TYPE "unsigned long"
424 #endif
425
426 #ifndef PCC_PTRDIFF_TYPE
427 #define PCC_PTRDIFF_TYPE "long int"
428 #endif
429
430
431 struct strlist preprocessor_flags;
432 struct strlist depflags;
433 struct strlist incdirs;
434 struct strlist user_sysincdirs;
435 struct strlist includes;
436 struct strlist sysincdirs;
437 struct strlist dirafterdirs;
438 struct strlist crtdirs;
439 struct strlist libdirs;
440 struct strlist progdirs;
441 struct strlist early_linker_flags;
442 struct strlist middle_linker_flags;
443 struct strlist late_linker_flags;
444 struct strlist inputs;
445 struct strlist assembler_flags;
446 struct strlist temp_outputs;
447 struct strlist compiler_flags;
448
449 int
450 main(int argc, char *argv[])
451 {
452 struct Wflags *Wf;
453 struct string *s;
454 char *t, *u, *argp;
455 char *msuffix;
456 int ninput, j;
457
458 lav = argv;
459 lac = argc;
460 ninput = 0;
461
462 strlist_init(&crtdirs);
463 strlist_init(&libdirs);
464 strlist_init(&progdirs);
465 strlist_init(&preprocessor_flags);
466 strlist_init(&incdirs);
467 strlist_init(&user_sysincdirs);
468 strlist_init(&includes);
469 strlist_init(&sysincdirs);
470 strlist_init(&dirafterdirs);
471 strlist_init(&depflags);
472 strlist_init(&early_linker_flags);
473 strlist_init(&middle_linker_flags);
474 strlist_init(&late_linker_flags);
475 strlist_init(&inputs);
476 strlist_init(&assembler_flags);
477 strlist_init(&temp_outputs);
478 strlist_init(&compiler_flags);
479
•480 if (endsWith(argv[0], CXXPROGNAME)) {
481 cxxflag = 1;
•482 } else if (endsWith(argv[0], CPPROGNAME)) {
483 Eflag = cppflag = 1;
484 }
485
486 #ifdef PCC_EARLY_SETUP
487 PCC_EARLY_SETUP
488 #endif
489
490 #ifdef _WIN32
491 /* have to prefix path early. -B may override */
492 incdir = win32pathsubst(incdir);
493 altincdir = win32pathsubst(altincdir);
494 libdir = win32pathsubst(libdir);
495 #ifdef PCCINCDIR
496 pccincdir = win32pathsubst(pccincdir);
497 pxxincdir = win32pathsubst(pxxincdir);
498 #endif
499 #ifdef PCCLIBDIR
500 pcclibdir = win32pathsubst(pcclibdir);
501 #endif
502 passp = win32pathsubst(passp);
503 pass0 = win32pathsubst(pass0);
504 #ifdef STARTFILES
505 for (i = 0; startfiles[i] != NULL; i++)
506 startfiles[i] = win32pathsubst(startfiles[i]);
507 for (i = 0; endfiles[i] != NULL; i++)
508 endfiles[i] = win32pathsubst(endfiles[i]);
509 #endif
510 #ifdef STARTFILES_T
511 for (i = 0; startfiles_T[i] != NULL; i++)
512 startfiles_T[i] = win32pathsubst(startfiles_T[i]);
513 for (i = 0; endfiles_T[i] != NULL; i++)
514 endfiles_T[i] = win32pathsubst(endfiles_T[i]);
515 #endif
516 #ifdef STARTFILES_S
517 for (i = 0; startfiles_S[i] != NULL; i++)
518 startfiles_S[i] = win32pathsubst(startfiles_S[i]);
519 for (i = 0; endfiles_S[i] != NULL; i++)
520 endfiles_S[i] = win32pathsubst(endfiles_S[i]);
521 #endif
522 #endif
523
524 while (--lac) {
525 ++lav;
526 argp = *lav;
527
528 #ifdef PCC_EARLY_ARG_CHECK
529 PCC_EARLY_ARG_CHECK
530 #endif
531
532 if (*argp != '-' || match(argp, "-")) {
533 /* Check for duplicate .o files. */
534 if (getsuf(argp) == 'o') {
535 j = 0;
536 STRLIST_FOREACH(s, &inputs)
537 if (match(argp, s->value))
538 j++;
539 if (j)
540 continue; /* skip it */
541 }
542 strlist_append(&inputs, argp);
543 ninput++;
544 continue;
545 }
546
547 switch (argp[1]) {
548 default:
549 oerror(argp);
550 break;
551
552 case '#':
553 if (match(argp, "-###")) {
554 printf("%s\n", VERSSTR);
555 vflag++;
556 noexec++;
557 } else
558 oerror(argp);
559 break;
560
561 case '-': /* double -'s */
562 if (match(argp, "--version")) {
563 printf("%s\n", VERSSTR);
564 return 0;
565 } else if (strncmp(argp, "--sysroot=", 10) == 0) {
566 sysroot = argp + 10;
567 } else if (strncmp(argp, "--sysroot", 9) == 0) {
568 sysroot = nxtopt(argp);
569 } else if (strcmp(argp, "--param") == 0) {
570 /* NOTHING YET */;
571 (void)nxtopt(0); /* ignore arg */
572 } else
573 oerror(argp);
574 break;
575
576 case 'a': /* only -ansi switch for now */
577 if (match(argp, "-ansi"))
578 cstd = SC89;
579 else
580 oerror(argp);
581 break;
582
583 case 'B': /* other search paths for binaries */
584 t = nxtopt("-B");
585 strlist_append(&crtdirs, t);
586 strlist_append(&libdirs, t);
587 strlist_append(&progdirs, t);
588 break;
589
590 case 'C':
591 if (match(argp, "-C") || match(argp, "-CC"))
592 strlist_append(&preprocessor_flags, argp);
593 else
594 oerror(argp);
595 break;
596
597 case 'c':
598 cflag++;
599 break;
600
601 case 'd': /* debug options */
602 if (match(argp, "-dumpmachine")) {
603 /* Print target and immediately exit */
604 puts(TARGSTR);
605 exit(0);
606 }
607 if (match(argp, "-dumpversion")) {
608 /* Print claimed gcc level, immediately exit */
609 puts("4.3.1");
610 exit(0);
611 }
612 for (t = &argp[2]; *t; t++) {
613 if (*t == 'M')
614 strlist_append(&preprocessor_flags, "-dM");
615
616 /* ignore others */
617 }
618 break;
619
620 case 'E':
621 Eflag++;
622 break;
623
624 case 'f': /* GCC compatibility flags */
625 u = &argp[2];
626 j = 0;
627 if (strncmp(u, "no-", 3) == 0)
628 j = 1, u += 3;
629 if (match(u, "PIC") || match(u, "pic")) {
630 kflag = j ? 0 : *u == 'P' ? F_PIC : F_pic;
631 } else if (match(u, "PIE") || match(u, "pie")) {
632 kflag = j ? 0 : *u == 'P' ? F_PIC : F_pic;
633 } else if (match(u, "freestanding")) {
634 freestanding = j ? 0 : 1;
635 } else if (match(u, "signed-char")) {
636 xuchar = j ? 1 : 0;
637 } else if (match(u, "unsigned-char")) {
638 xuchar = j ? 0 : 1;
639 } else if (match(u, "stack-protector") ||
640 match(u, "stack-protector-all")) {
641 sspflag = j ? 0 : 1;
642 } else if (match(u, "use-ld=")) {
643 /* ignore nonsense -fno-use-ld=* command */
644 if (j)
645 break;
646 u += 7;
647 ld = select_linker(u);
648 }
649 /* silently ignore the rest */
650 break;
651
652 case 'g': /* create debug output */
653 if (argp[2] == '0')
654 gflag = 0;
655 else
656 gflag++;
657 break;
658
659
660 case 'X':
661 Xflag++;
662 break;
663
664 case 'D':
665 case 'U':
666 strlist_append(&preprocessor_flags, argp);
667 if (argp[2] != 0)
668 break;
669 strlist_append(&preprocessor_flags, nxtopt(argp));
670 break;
671
672 case 'I': /* Add include dirs */
673 strlist_append(&incdirs, nxtopt("-I"));
674 break;
675
676 case 'i':
677 if (match(argp, "-isystem")) {
678 strlist_append(&user_sysincdirs, nxtopt(0));
679 } else if (match(argp, "-include")) {
680 strlist_append(&includes, nxtopt(0));
681 } else if (match(argp, "-isysroot")) {
682 isysroot = nxtopt(0);
683 } else if (strcmp(argp, "-idirafter") == 0) {
684 strlist_append(&dirafterdirs, nxtopt(0));
685 } else
686 oerror(argp);
687 break;
688
689 case 'k': /* generate PIC code */
690 kflag = argp[2] ? argp[2] - '0' : F_pic;
691 break;
692
693 case 'l':
694 case 'L':
695 if (argp[2] == 0)
696 argp = cat(argp, nxtopt(0));
697 strlist_append(&inputs, argp);
698 break;
699
700 case 'm': /* target-dependent options */
701 if (strncmp(argp, "-march=", 6) == 0) {
702 strlist_append(&compiler_flags, argp);
703 break;
704 }
705 #ifdef mach_amd64
706 if (strncmp(argp, "-mcmodel=", 9) == 0) {
707 strlist_append(&compiler_flags, argp);
708 break;
709 }
710 /* need to call i386 ccom for this */
711 if (strcmp(argp, "-melf_i386") == 0) {
712 pass0 = LIBEXECDIR "/ccom_i386";
713 amd64_i386 = 1;
714 break;
715 }
716 #endif
717 #if defined(mach_arm) || defined(mach_mips) || defined(mach_mips64)
718 if (match(argp, "-mbig-endian")) {
719 bigendian = 1;
720 strlist_append(&compiler_flags, argp);
721 break;
722 }
723 if (match(argp, "-mlittle-endian")) {
724 bigendian = 0;
725 strlist_append(&compiler_flags, argp);
726 break;
727 }
728 if (match(argp, "-msoft-float")) {
729 softfloat = 1;
730 strlist_append(&compiler_flags, argp);
731 break;
732 }
733 #endif
734 #if defined(mach_mips) || defined(mach_mips64)
735 if (match(argp, "-mhard-float")) {
736 softfloat = 0;
737 strlist_append(&compiler_flags, argp);
738 break;
739 }
740 #endif
741 strlist_append(&middle_linker_flags, argp);
742 if (argp[2] == 0) {
743 t = nxtopt(0);
744 strlist_append(&middle_linker_flags, t);
745 }
746 break;
747
748 case 'n': /* handle -n flags */
749 if (strcmp(argp, "-nostdinc") == 0)
750 nostdinc++;
751 else if (strcmp(argp, "-nostdlib") == 0) {
752 nostdlib++;
753 nostartfiles++;
754 } else if (strcmp(argp, "-nostartfiles") == 0)
755 nostartfiles = 1;
756 else if (strcmp(argp, "-nodefaultlibs") == 0)
757 nostdlib++;
758 else if (strcmp(argp, "-nopie") == 0) {
759 strlist_append(&middle_linker_flags, argp);
760 pieflag = 0;
761 } else
762 oerror(argp);
763 break;
764
765 case 'p':
766 if (strcmp(argp, "-pg") == 0 ||
767 strcmp(argp, "-p") == 0)
768 pgflag++;
769 else if (strcmp(argp, "-pthread") == 0)
770 pthreads++;
771 else if (strcmp(argp, "-pipe") == 0)
772 /* NOTHING YET */;
773 else if (strcmp(argp, "-pedantic") == 0)
774 /* NOTHING YET */;
775 else if ((t = argnxt(argp, "-print-prog-name="))) {
776 fname = t;
777 printprogname = 1;
778 } else if ((t = argnxt(argp, "-print-file-name="))) {
779 fname = t;
780 printfilename = 1;
781 } else if (match(argp, "-print-libgcc-file-name")) {
782 fname = "libpcc.a";
783 printfilename = 1;
784 } else if (match(argp, "-print-search-dirs")) {
785 printsearchdirs = 1;
786 } else if (match(argp, "-pie")) {
787 strlist_append(&middle_linker_flags, argp);
788 pieflag = 1;
789 } else
790 oerror(argp);
791 break;
792
793 case 'R':
794 if (argp[2] == 0)
795 argp = cat(argp, nxtopt(0));
796 strlist_append(&middle_linker_flags, argp);
797 break;
798
799 case 'r':
800 if (match(argp, "-rdynamic")) {
801 strlist_append(&middle_linker_flags,
802 "--export-dynamic");
803 } else if (match(argp, "-r")) {
804 rflag = 1;
805 } else
806 oerror(argp);
807 break;
808
809 case 'T':
810 strlist_append(&inputs, argp);
811 if (argp[2] == 0 ||
812 strcmp(argp, "-Ttext") == 0 ||
813 strcmp(argp, "-Tdata") == 0 ||
814 strcmp(argp, "-Tbss") == 0)
815 strlist_append(&inputs, nxtopt(0));
816 break;
817
818 case 's':
819 if (match(argp, "-shared")) {
820 shared = 1;
821 } else if (match(argp, "-static")) {
822 Bstatic = 1;
823 } else if (match(argp, "-symbolic")) {
824 strlist_append(&middle_linker_flags,
825 "-Bsymbolic");
826 } else if (strncmp(argp, "-std=", 5) == 0) {
827 if (strcmp(&argp[5], "c11") == 0)
828 cstd = SC11;
829 else if (strcmp(&argp[5], "gnu99") == 0 ||
830 strcmp(&argp[5], "gnu9x") == 0)
831 cstd = SGNU99;
832 else if (strcmp(&argp[5], "c89") == 0)
833 cstd = SC89;
834 else if (strcmp(&argp[5], "gnu89") == 0)
835 cstd = SGNU89;
836 else if (strcmp(&argp[5], "c99") == 0)
837 cstd = SC99;
838 else
839 oerror(argp);
840 } else if (match(argp, "-s")) {
841 strlist_append(&middle_linker_flags, argp);
842 } else
843 oerror(argp);
844 break;
845
846 case 'S':
847 Sflag++;
848 cflag++;
849 break;
850
851 case 't':
852 tflag++;
853 cstd = STRAD;
854 break;
855
856 case 'o':
857 if (outfile)
858 errorx(8, "too many -o");
859 outfile = nxtopt("-o");
860 break;
861
862 case 'O':
863 if (argp[2] == '\0')
864 /* gcc does -O1, clang does -O2 */
865 Oflag = 1; /* do what gcc does */
866 else if (argp[3] == '\0' &&
867 isdigit((unsigned char)argp[2]))
868 Oflag = argp[2] - '0';
869 else if (argp[3] == '\0' && argp[2] == 's')
870 Oflag = 1; /* optimize for space only */
871 else
872 oerror(argp);
873 break;
874
875 case 'P':
876 strlist_append(&preprocessor_flags, argp);
877 break;
878
879 case 'M':
880 needM = 1;
881 if (match(argp, "-M")) {
882 Mflag++;
883 strlist_append(&depflags, argp);
884 } else if (match(argp, "-MP")) {
885 strlist_append(&depflags, "-xMP");
886 } else if (match(argp, "-MF")) {
887 MFfile = nxtopt("-MF");
888 } else if (match(argp, "-MT") || match(argp, "-MQ")) {
889 t = cat("-xMT,", nxtopt("-MT"));
890 t[3] = argp[2];
891 strlist_append(&depflags, t);
892 } else if (match(argp, "-MD")) {
893 MDflag++;
894 needM = 0;
895 strlist_append(&depflags, "-M");
896 } else if (match(argp, "-MMD")) {
897 MMDflag++;
898 needM = 0;
899 strlist_append(&depflags, "-M");
900 strlist_append(&depflags, "-xMMD");
901 } else
902 oerror(argp);
903 break;
904
905 case 'v':
906 printf("%s\n", VERSSTR);
907 vflag++;
908 break;
909
910 case 'w': /* no warnings at all emitted */
911 strlist_append(&compiler_flags, "-w");
912 break;
913
914 case 'W': /* Ignore (most of) W-flags */
915 if ((t = argnxt(argp, "-Wl,"))) {
916 u = strtok(t, ",");
917 do {
918 strlist_append(&inputs, u);
919 } while ((u = strtok(NULL, ",")) != NULL);
920 } else if ((t = argnxt(argp, "-Wa,"))) {
921 u = strtok(t, ",");
922 do {
923 strlist_append(&assembler_flags, u);
924 } while ((u = strtok(NULL, ",")) != NULL);
925 } else if ((t = argnxt(argp, "-Wc,"))) {
926 u = strtok(t, ",");
927 do {
928 strlist_append(&compiler_flags, u);
929 } while ((u = strtok(NULL, ",")) != NULL);
930 } else if ((t = argnxt(argp, "-Wp,"))) {
931 u = strtok(t, ",");
932 do {
933 strlist_append(&preprocessor_flags, u);
934 } while ((u = strtok(NULL, ",")) != NULL);
935 } else if (strcmp(argp, "-Werror") == 0) {
936 strlist_append(&compiler_flags, "-Werror");
937 strlist_append(&preprocessor_flags, "-E");
938 } else if (strcmp(argp, "-Wall") == 0) {
939 for (Wf = Wflags; Wf->name; Wf++)
940 if (Wf->flags & INWALL)
941 strlist_append(&compiler_flags,
942 cat("-W", Wf->name));
943 } else if (strcmp(argp, "-Wextra") == 0 ||
944 strcmp(argp, "-W") == 0) {
945 for (Wf = Wflags; Wf->name; Wf++)
946 if (Wf->flags & INWEXTRA)
947 strlist_append(&compiler_flags,
948 cat("-W", Wf->name));
949 } else if (strcmp(argp, "-WW") == 0) {
950 for (Wf = Wflags; Wf->name; Wf++)
951 strlist_append(&compiler_flags,
952 cat("-W", Wf->name));
953 } else {
954 /* pass through, if supported */
955 t = &argp[2];
956 if (strncmp(t, "no-", 3) == 0)
957 t += 3;
958 if (strncmp(t, "error=", 6) == 0)
959 t += 6;
960 for (Wf = Wflags; Wf->name; Wf++) {
961 if (strcmp(t, Wf->name) == 0)
962 strlist_append(&compiler_flags,
963 argp);
964 }
965 }
966 break;
967
968 case 'x':
969 t = nxtopt("-x");
970 if (match(t, "none"))
971 strlist_append(&inputs, ")");
972 else if (match(t, "c"))
973 strlist_append(&inputs, ")c");
974 else if (match(t, "assembler"))
975 strlist_append(&inputs, ")s");
976 else if (match(t, "assembler-with-cpp"))
977 strlist_append(&inputs, ")S");
978 else if (match(t, "c++"))
979 strlist_append(&inputs, ")c++");
980 else {
981 strlist_append(&compiler_flags, "-x");
982 strlist_append(&compiler_flags, t);
983 }
984 break;
985
986 case 'z':
987 argp = cat(argp, nxtopt(0));
988 strlist_append(&middle_linker_flags, argp);
989 break;
990
991 }
992 continue;
993
994 }
995
996 /* Sanity checking */
997 if (cppflag) {
998 if (ninput == 0) {
999 strlist_append(&inputs, "-");
1000 ninput++;
1001 } else if (ninput > 2 || (ninput == 2 && outfile)) {
1002 errorx(8, "too many files");
1003 } else if (ninput == 2) {
1004 outfile = STRLIST_NEXT(STRLIST_FIRST(&inputs))->value;
1005 STRLIST_FIRST(&inputs)->next = NULL;
1006 ninput--;
1007 }
1008 }
1009 if (tflag && Eflag == 0)
1010 errorx(8,"-t only allowed fi -E given");
1011
1012 /* Correct C standard */
1013 switch (cstd) {
1014 case STRAD: break;
1015 case SC89: c89defs = 1; break;
1016 case SGNU89: xgnu89 = c89defs = 1; break;
1017 case SC99: c89defs = c99defs = 1; break;
1018 case SGNU99: c89defs = c99defs = xgnu99 = 1; break;
1019 case SC11: c89defs = c11defs = 1; break;
1020 }
1021
1022 if (ninput == 0 && !(printprogname || printfilename || printsearchdirs))
1023 errorx(8, "no input files");
1024 if (outfile && (cflag || Sflag || Eflag) && ninput > 1)
1025 errorx(8, "-o given with -c || -E || -S and more than one file");
1026 #if 0
1027 if (outfile && clist[0] && strcmp(outfile, clist[0]) == 0)
1028 errorx(8, "output file will be clobbered");
1029 #endif
1030
1031 if (needM && !Mflag && !MDflag && !MMDflag)
1032 errorx(8, "to make dependencies needs -M");
1033
1034
1035 if (signal(SIGINT, SIG_IGN) != SIG_IGN) /* interrupt */
1036 signal(SIGINT, idexit);
1037 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) /* terminate */
1038 signal(SIGTERM, idexit);
1039
1040 /* after arg parsing */
1041 strlist_append(&progdirs, LIBEXECDIR);
1042 if (pcclibdir)
1043 strlist_append(&crtdirs, pcclibdir);
1044 for (j = 0; deflibdirs[j]; j++) {
1045 if (sysroot)
1046 deflibdirs[j] = cat(sysroot, deflibdirs[j]);
1047 strlist_append(&crtdirs, deflibdirs[j]);
1048 }
1049
1050 setup_cpp_flags();
1051 setup_ccom_flags();
1052 setup_as_flags();
1053
1054 if (isysroot == NULL)
1055 isysroot = sysroot;
1056 expand_sysroot();
1057
1058 if (printprogname) {
1059 printf("%s\n", find_file(fname, &progdirs, X_OK));
1060 return 0;
1061 } else if (printfilename) {
1062 printf("%s\n", find_file(fname, &crtdirs, R_OK));
1063 return 0;
1064 } else if (printsearchdirs) {
1065 printf("install: %s\n", LIBEXECDIR);
1066 printf("programs: =");
1067 strlist_print(&progdirs, stdout, 0, ":");
1068 printf("\n");
1069 printf("libraries: =");
1070 strlist_print(&crtdirs, stdout, 0, ":");
1071 printf("\n");
1072 return 0;
1073 }
1074
1075 msuffix = NULL;
1076 STRLIST_FOREACH(s, &inputs) {
1077 char *suffix;
1078 char *ifile, *ofile = NULL;
1079
1080 ifile = s->value;
1081 if (ifile[0] == ')') { /* -x source type given */
1082 msuffix = ifile[1] ? &ifile[1] : NULL;
1083 continue;
1084 }
1085 if (ifile[0] == '-' && ifile[1] == 0)
1086 suffix = msuffix ? msuffix : "c";
1087 else if (ifile[0] == '-')
1088 suffix = "o"; /* source files cannot begin with - */
1089 else if (msuffix)
1090 suffix = msuffix;
1091 else
1092 suffix = getsufp(ifile);
1093 /*
1094 * C preprocessor
1095 */
1096 ascpp = match(suffix, "S");
1097 if (ascpp || cppflag || match(suffix, "c") || cxxsuf(suffix)) {
1098 /* find out next output file */
1099 if (Mflag || MDflag || MMDflag) {
1100 char *Mofile = NULL;
1101
1102 if (MFfile)
1103 Mofile = MFfile;
1104 else if (outfile)
1105 Mofile = setsuf(outfile, 'd');
1106 else if (MDflag || MMDflag)
1107 Mofile = setsuf(ifile, 'd');
1108 if (preprocess_input(ifile, Mofile, 1))
1109 exandrm(Mofile);
1110 }
1111 if (Mflag)
1112 continue;
1113 if (Eflag) {
1114 /* last pass */
1115 ofile = outfile;
1116 } else {
1117 /* to temp file */
1118 strlist_append(&temp_outputs, ofile = gettmp());
1119 }
1120 if (preprocess_input(ifile, ofile, 0))
1121 exandrm(ofile);
1122 if (Eflag)
1123 continue;
1124 ifile = ofile;
1125 suffix = match(suffix, "S") ? "s" : "i";
1126 }
1127
1128 /*
1129 * C compiler
1130 */
1131 if (match(suffix, "i")) {
1132 /* find out next output file */
1133 if (Sflag) {
1134 ofile = outfile;
1135 if (outfile == NULL)
1136 ofile = setsuf(s->value, 's');
1137 } else
1138 strlist_append(&temp_outputs, ofile = gettmp());
1139 if (compile_input(ifile, ofile))
1140 exandrm(ofile);
1141 if (Sflag)
1142 continue;
1143 ifile = ofile;
1144 suffix = "s";
1145 }
1146
1147 /*
1148 * Assembler
1149 */
1150 if (match(suffix, "s")) {
1151 if (cflag) {
1152 ofile = outfile;
1153 if (ofile == NULL)
1154 ofile = setsuf(s->value, 'o');
1155 } else {
1156 strlist_append(&temp_outputs, ofile = gettmp());
1157 /* strlist_append linker */
1158 }
1159 if (assemble_input(ifile, ofile))
1160 exandrm(ofile);
1161 ifile = ofile;
1162 }
1163
1164 strlist_append(&middle_linker_flags, ifile);
1165 }
1166
1167 if (cflag || Eflag || Mflag)
1168 dexit(0);
1169
1170 /*
1171 * Linker
1172 */
1173 setup_ld_flags();
1174 if (run_linker())
1175 exandrm(0);
1176
1177 #ifdef notdef
1178 strlist_free(&crtdirs);
1179 strlist_free(&libdirs);
1180 strlist_free(&progdirs);
1181 strlist_free(&incdirs);
1182 strlist_free(&preprocessor_flags);
1183 strlist_free(&user_sysincdirs);
1184 strlist_free(&includes);
1185 strlist_free(&sysincdirs);
1186 strlist_free(&dirafterdirs);
1187 strlist_free(&depflags);
1188 strlist_free(&early_linker_flags);
1189 strlist_free(&middle_linker_flags);
1190 strlist_free(&late_linker_flags);
1191 strlist_free(&inputs);
1192 strlist_free(&assembler_flags);
1193 strlist_free(&temp_outputs);
1194 strlist_free(&compiler_flags);
1195 #endif
1196 dexit(0);
1197 return 0;
1198 }
1199
1200 /*
1201 * exit and cleanup after interrupt.
1202 */
1203 void
1204 idexit(int arg)
1205 {
1206 dexit(100);
1207 }
1208
1209 /*
1210 * exit and cleanup.
1211 */
1212 void
1213 dexit(int eval)
1214 {
1215 struct string *s;
1216
1217 if (!Xflag) {
1218 STRLIST_FOREACH(s, &temp_outputs)
1219 cunlink(s->value);
1220 }
1221 exit(eval);
1222 }
1223
1224 /*
1225 * Called when something failed.
1226 */
1227 void
1228 exandrm(char *s)
1229 {
1230 if (s && *s)
1231 strlist_append(&temp_outputs, s);
1232 dexit(1);
1233 }
1234
1235 /*
1236 * complain and exit.
1237 */
1238 void
1239 errorx(int eval, char *s, ...)
1240 {
1241 va_list ap;
1242
1243 va_start(ap, s);
1244 fputs("error: ", stderr);
1245 vfprintf(stderr, s, ap);
1246 putc('\n', stderr);
1247 va_end(ap);
1248 dexit(eval);
1249 }
1250
1251 static char *
1252 find_file(const char *file, struct strlist *path, int mode)
1253 {
1254 struct string *s;
1255 char *f;
1256 size_t lf, lp;
1257 int need_sep;
1258
1259 lf = strlen(file);
1260 STRLIST_FOREACH(s, path) {
1261 lp = strlen(s->value);
1262 need_sep = (lp && s->value[lp - 1] != '/') ? 1 : 0;
1263 f = xmalloc(lp + lf + need_sep + 1);
1264 memcpy(f, s->value, lp);
1265 if (need_sep)
1266 f[lp] = '/';
1267 memcpy(f + lp + need_sep, file, lf + 1);
1268 if (access(f, mode) == 0)
1269 return f;
1270 free(f);
1271 }
1272 return xstrdup(file);
1273 }
1274
1275 #ifdef HAVE_CC2
1276 #ifdef NEED_CC2
1277 #define C2check 1
1278 #else
1279 #define C2check Oflag
1280 #endif
1281 #else
1282 #define C2check 0
1283 #endif
1284
1285 #ifdef TWOPASS
1286 static int
1287 compile_input(char *input, char *output)
1288 {
1289 struct strlist args;
1290 char *tfile;
1291 int retval;
1292
1293 strlist_append(&temp_outputs, tfile = gettmp());
1294
1295 strlist_init(&args);
1296 strlist_append_list(&args, &compiler_flags);
1297 strlist_append(&args, input);
1298 strlist_append(&args, tfile);
1299 strlist_prepend(&args,
1300 find_file(cxxflag ? CXX0 : CC0, &progdirs, X_OK));
1301 retval = strlist_exec(&args);
1302 strlist_free(&args);
1303 if (retval)
1304 return retval;
1305
1306 strlist_init(&args);
1307 strlist_append_list(&args, &compiler_flags);
1308 strlist_append(&args, tfile);
1309 strlist_append(&args, output);
1310 strlist_prepend(&args,
1311 find_file(cxxflag ? CXX1: CC1, &progdirs, X_OK));
1312 retval = strlist_exec(&args);
1313 strlist_free(&args);
1314 return retval;
1315 }
1316 #else
1317 static int
1318 compile_input(char *input, char *output)
1319 {
1320 struct strlist args;
1321 char *tfile;
1322 int retval;
1323
1324 tfile = output;
1325 if (C2check)
1326 strlist_append(&temp_outputs, tfile = gettmp());
1327
1328 strlist_init(&args);
1329 strlist_append_list(&args, &compiler_flags);
1330 strlist_append(&args, input);
1331 strlist_append(&args, tfile);
1332 strlist_prepend(&args,
1333 find_file(cxxflag ? passxx0 : pass0, &progdirs, X_OK));
1334 retval = strlist_exec(&args);
1335 strlist_free(&args);
1336 if (retval)
1337 return retval;
1338 if (C2check) {
1339 strlist_init(&args);
1340 strlist_append(&args, tfile);
1341 strlist_append(&args, output);
1342 strlist_prepend(&args, find_file(CC2, &progdirs, X_OK));
1343 retval = strlist_exec(&args);
1344 strlist_free(&args);
1345 }
1346 return retval;
1347 }
1348 #endif
1349
1350 static int
1351 assemble_input(char *input, char *output)
1352 {
1353 struct strlist args;
1354 int retval;
1355
1356 strlist_init(&args);
1357 #ifdef PCC_EARLY_AS_ARGS
1358 PCC_EARLY_AS_ARGS
1359 #endif
1360 strlist_append_list(&args, &assembler_flags);
1361 strlist_append(&args, "-o");
1362 strlist_append(&args, output);
1363 strlist_append(&args, input);
1364 strlist_prepend(&args,
1365 find_file(as, &progdirs, X_OK));
1366 #ifdef PCC_LATE_AS_ARGS
1367 PCC_LATE_AS_ARGS
1368 #endif
1369 retval = strlist_exec(&args);
1370 strlist_free(&args);
1371 return retval;
1372 }
1373
1374 static int
1375 preprocess_input(char *input, char *output, int dodep)
1376 {
1377 struct strlist args;
1378 struct string *s;
1379 int retval;
1380
1381 strlist_init(&args);
1382 strlist_append_list(&args, &preprocessor_flags);
1383 if (ascpp) {
1384 strlist_append(&args, "-A");
1385 strlist_append(&args, "-D__ASSEMBLER__");
1386 }
1387 STRLIST_FOREACH(s, &includes) {
1388 strlist_append(&args, "-i");
1389 strlist_append(&args, s->value);
1390 }
1391 STRLIST_FOREACH(s, &incdirs) {
1392 strlist_append(&args, "-I");
1393 strlist_append(&args, s->value);
1394 }
1395 STRLIST_FOREACH(s, &user_sysincdirs) {
1396 strlist_append(&args, "-S");
1397 strlist_append(&args, s->value);
1398 }
1399 if (!nostdinc) {
1400 STRLIST_FOREACH(s, &sysincdirs) {
1401 strlist_append(&args, "-S");
1402 strlist_append(&args, s->value);
1403 }
1404 }
1405 STRLIST_FOREACH(s, &dirafterdirs) {
1406 strlist_append(&args, "-S");
1407 strlist_append(&args, s->value);
1408 }
1409 if (dodep)
1410 strlist_append_list(&args, &depflags);
1411 strlist_append(&args, input);
1412 if (output)
1413 strlist_append(&args, output);
1414
1415 strlist_prepend(&args, find_file(passp, &progdirs, X_OK));
1416 retval = strlist_exec(&args);
1417 strlist_free(&args);
1418 return retval;
1419 }
1420
1421 static int
1422 run_linker(void)
1423 {
1424 struct strlist linker_flags;
1425 int retval;
1426
1427 if (outfile) {
1428 strlist_prepend(&early_linker_flags, outfile);
1429 strlist_prepend(&early_linker_flags, "-o");
1430 }
1431 strlist_init(&linker_flags);
1432 strlist_append_list(&linker_flags, &early_linker_flags);
1433 strlist_append_list(&linker_flags, &middle_linker_flags);
1434 strlist_append_list(&linker_flags, &late_linker_flags);
1435 strlist_prepend(&linker_flags, find_file(ld, &progdirs, X_OK));
1436
1437 retval = strlist_exec(&linker_flags);
1438
1439 strlist_free(&linker_flags);
1440 return retval;
1441 }
1442
1443 static char *
1444 select_linker(char *name)
1445 {
1446 static char ld_name[8];
1447
1448 /* Short names first. */
1449 if (strcmp(name, "bfd") == 0 ||
1450 strcmp(name, "gold") == 0 ||
1451 strcmp(name, "lld") == 0) {
1452 snprintf(ld_name, sizeof ld_name, "ld.%s", name);
1453 return ld_name;
1454 }
1455
1456 /* Must be absolute path otherwise. */
1457 if (name[0] != '/')
1458 return LINKER;
1459
1460 return name;
1461 }
1462
1463
1464
1465 static char *cxxt[] = { "cc", "cp", "cxx", "cpp", "CPP", "c++", "C" };
1466 int
1467 cxxsuf(char *s)
1468 {
1469 unsigned i;
1470 for (i = 0; i < sizeof(cxxt)/sizeof(cxxt[0]); i++)
1471 if (strcmp(s, cxxt[i]) == 0)
1472 return 1;
1473 return 0;
1474 }
1475
1476 char *
1477 getsufp(char *s)
1478 {
1479 register char *p;
1480
1481 if ((p = strrchr(s, '.')) && p[1] != '\0')
1482 return &p[1];
1483 return "";
1484 }
1485
1486 int
1487 getsuf(char *s)
1488 {
1489 register char *p;
1490
1491 if ((p = strrchr(s, '.')) && p[1] != '\0' && p[2] == '\0')
1492 return p[1];
1493 return(0);
1494 }
1495
1496 /*
1497 * Get basename of string s, copy it and change its suffix to ch.
1498 */
1499 char *
1500 setsuf(char *s, char ch)
1501 {
1502 char *e, *p, *rp;
1503
1504 e = NULL;
1505 for (p = s; *p; p++) {
1506 if (*p == '/')
1507 s = p + 1;
1508 if (*p == '.')
1509 e = p;
1510 }
1511 if (s > e)
1512 e = p;
1513
1514 rp = p = xmalloc(e - s + 3);
1515 while (s < e)
1516 *p++ = *s++;
1517
1518 *p++ = '.';
1519 *p++ = ch;
1520 *p = '\0';
1521 return rp;
1522 }
1523
1524 #ifdef _WIN32
1525
1526 static int
1527 strlist_exec(struct strlist *l)
1528 {
1529 char *cmd;
1530 STARTUPINFO si;
1531 PROCESS_INFORMATION pi;
1532 DWORD exitCode;
1533 BOOL ok;
1534
1535 cmd = win32commandline(l);
1536 if (vflag)
1537 printf("%s\n", cmd);
1538 if (noexec)
1539 return 0;
1540
1541 ZeroMemory(&si, sizeof(STARTUPINFO));
1542 si.cb = sizeof(STARTUPINFO);
1543 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
1544
1545 ok = CreateProcess(NULL, // the executable program
1546 cmd, // the command line arguments
1547 NULL, // ignored
1548 NULL, // ignored
1549 TRUE, // inherit handles
1550 HIGH_PRIORITY_CLASS,
1551 NULL, // ignored
1552 NULL, // ignored
1553 &si,
1554 &pi);
1555
1556 if (!ok)
1557 errorx(100, "Can't find %s\n", STRLIST_FIRST(l)->value);
1558
1559 WaitForSingleObject(pi.hProcess, INFINITE);
1560 GetExitCodeProcess(pi.hProcess, &exitCode);
1561 CloseHandle(pi.hProcess);
1562 CloseHandle(pi.hThread);
1563
1564 return (exitCode != 0);
1565 }
1566
1567 #else
1568
1569 static int
1570 strlist_exec(struct strlist *l)
1571 {
1572 sig_atomic_t exit_now = 0;
1573 sig_atomic_t child;
1574 char **argv;
1575 size_t argc;
1576 ssize_t result;
1577 int rv;
1578
1579 strlist_make_array(l, &argv, &argc);
1580 if (vflag) {
1581 printf("Calling ");
1582 strlist_print(l, stdout, noexec, " ");
1583 printf("\n");
1584 }
1585 if (noexec)
1586 return 0;
1587
1588 switch ((child = fork())) {
1589 case 0:
1590 execvp(argv[0], argv);
1591 result = write(STDERR_FILENO, "Exec of ", 8);
1592 result = write(STDERR_FILENO, argv[0], strlen(argv[0]));
1593 result = write(STDERR_FILENO, " failed\n", 8);
1594 (void)result;
1595 _exit(127);
1596 case -1:
1597 errorx(1, "fork failed");
1598 default:
1599 while (waitpid(child, &rv, 0) == -1 && errno == EINTR)
1600 /* nothing */(void)0;
1601 rv = WEXITSTATUS(rv);
1602 if (rv)
1603 errorx(1, "%s terminated with status %d", argv[0], rv);
1604 while (argc-- > 0)
1605 free(argv[argc]);
1606 free(argv);
1607 break;
1608 }
1609 return exit_now;
1610 }
1611
1612 #endif
1613
1614 /*
1615 * Catenate two (optional) strings together
1616 */
1617 char *
1618 cat(const char *a, const char *b)
1619 {
1620 size_t len;
1621 char *rv;
1622
1623 len = (a ? strlen(a) : 0) + (b ? strlen(b) : 0) + 1;
1624 rv = xmalloc(len);
1625 snprintf(rv, len, "%s%s", (a ? a : ""), (b ? b : ""));
1626 return rv;
1627 }
1628
1629 int
1630 cunlink(char *f)
1631 {
1632 if (f==0 || Xflag)
1633 return(0);
1634 return (unlink(f));
1635 }
1636
1637 #ifdef _WIN32
1638 char *
1639 gettmp(void)
1640 {
1641 DWORD pathSize;
1642 char pathBuffer[MAX_PATH + 1];
1643 char tempFilename[MAX_PATH];
1644 UINT uniqueNum;
1645
1646 pathSize = GetTempPath(sizeof(pathBuffer), pathBuffer);
1647 if (pathSize == 0 || pathSize > sizeof(pathBuffer))
1648 pathBuffer[0] = '\0';
1649 uniqueNum = GetTempFileName(pathBuffer, "ctm", 0, tempFilename);
1650 if (uniqueNum == 0)
1651 errorx(8, "GetTempFileName failed: path \"%s\"", pathBuffer);
1652
1653 return xstrdup(tempFilename);
1654 }
1655
1656 #else
1657
1658 char *
1659 gettmp(void)
1660 {
1661 char *sfn = xstrdup("/tmp/ctm.XXXXXX");
1662 int fd = -1;
1663
1664 if ((fd = mkstemp(sfn)) == -1)
1665 errorx(8, "%s: %s\n", sfn, strerror(errno));
1666 close(fd);
1667 return sfn;
1668 }
1669 #endif
1670
1671 static void
1672 expand_sysroot(void)
1673 {
1674 struct string *s;
1675 struct strlist *lists[] = { &crtdirs, &sysincdirs, &incdirs,
1676 &user_sysincdirs, &libdirs, &progdirs, &dirafterdirs, NULL };
1677 const char *sysroots[] = { sysroot, isysroot, isysroot, isysroot,
1678 sysroot, sysroot, isysroot, NULL };
1679 size_t i, sysroot_len, value_len;
1680 char *path;
1681
1682 assert(sizeof(lists) / sizeof(lists[0]) ==
1683 sizeof(sysroots) / sizeof(sysroots[0]));
1684
1685 for (i = 0; lists[i] != NULL; ++i) {
1686 STRLIST_FOREACH(s, lists[i]) {
1687 if (s->value[0] != '=')
1688 continue;
1689 sysroot_len = strlen(sysroots[i]);
1690 /* Skipped '=' compensates additional space for '\0' */
1691 value_len = strlen(s->value);
1692 path = xmalloc(sysroot_len + value_len);
1693 memcpy(path, sysroots[i], sysroot_len);
1694 memcpy(path + sysroot_len, s->value + 1, value_len);
1695 free(s->value);
1696 s->value = path;
1697 }
1698 }
1699 }
1700
1701 void
1702 oerror(char *s)
1703 {
1704 errorx(8, "unknown option '%s'", s);
1705 }
1706
1707 /*
1708 * See if m matches the beginning of string str, if it does return the
1709 * remaining of str, otherwise NULL.
1710 */
1711 char *
1712 argnxt(char *str, char *m)
1713 {
1714 if (strncmp(str, m, strlen(m)))
1715 return NULL; /* No match */
1716 return str + strlen(m);
1717 }
1718
1719 /*
1720 * Return next argument to option, or complain.
1721 */
1722 char *
1723 nxtopt(char *o)
1724 {
1725 size_t l;
1726
1727 if (o != NULL) {
1728 l = strlen(o);
1729 if (lav[0][l] != 0)
1730 return &lav[0][l];
1731 }
1732 if (lac == 1)
1733 errorx(8, "missing argument to '%s'", o);
1734 lav++;
1735 lac--;
1736 return lav[0];
1737 }
1738
1739 struct flgcheck {
1740 int *flag;
1741 int set;
1742 char *def;
1743 } cppflgcheck[] = {
1744 { &vflag, 1, "-v" },
1745 { &c99defs, 1, "-D__STDC_VERSION__=199901L" },
1746 { &c11defs, 1, "-D__STDC_VERSION__=201112L" },
1747 { &c89defs, 1, "-D__STDC__=1" },
1748 { &freestanding, 1, "-D__STDC_HOSTED__=0" },
1749 { &freestanding, 0, "-D__STDC_HOSTED__=1" },
1750 { &cxxflag, 1, "-D__cplusplus" },
1751 { &xuchar, 1, "-D__CHAR_UNSIGNED__" },
1752 { &sspflag, 1, "-D__SSP__" },
1753 { &pthreads, 1, "-D_PTHREADS" },
1754 { &Oflag, 1, "-D__OPTIMIZE__" },
1755 { &tflag, 1, "-t" },
1756 { &kflag, 1, "-D__PIC__" },
1757 { 0 },
1758 };
1759
1760 static void
1761 cksetflags(struct flgcheck *fs, struct strlist *sl, int which)
1762 {
1763 void (*fn)(struct strlist *, const char *);
1764
1765 fn = which == 'p' ? strlist_prepend : strlist_append;
1766 for (; fs->flag; fs++) {
1767 if (fs->set && *fs->flag)
1768 fn(sl, fs->def);
1769 if (!fs->set && !*fs->flag)
1770 fn(sl, fs->def);
1771 }
1772 }
1773
1774 #ifndef TARGET_LE
1775 #define TARGET_LE 1
1776 #define TARGET_BE 2
1777 #define TARGET_PDP 3
1778 #define TARGET_ANY 4
1779 #endif
1780
1781 static char *defflags[] = {
1782 "-D__PCC__=" MKS(PCC_MAJOR),
1783 "-D__PCC_MINOR__=" MKS(PCC_MINOR),
1784 "-D__PCC_MINORMINOR__=" MKS(PCC_MINORMINOR),
1785 "-D__VERSION__=" MKS(VERSSTR),
1786 "-D__SCHAR_MAX__=" MKS(MAX_CHAR),
1787 "-D__SHRT_MAX__=" MKS(MAX_SHORT),
1788 "-D__INT_MAX__=" MKS(MAX_INT),
1789 "-D__LONG_MAX__=" MKS(MAX_LONG),
1790 "-D__LONG_LONG_MAX__=" MKS(MAX_LONGLONG),
1791
1792 "-D__STDC_ISO_10646__=200009L",
1793 "-D__WCHAR_TYPE__=" WCT,
1794 "-D__SIZEOF_WCHAR_T__=" MKS(WCHAR_SIZE),
1795 "-D__WCHAR_MAX__=" WCM,
1796 "-D__WINT_TYPE__=" PCC_WINT_TYPE,
1797 "-D__SIZE_TYPE__=" PCC_SIZE_TYPE,
1798 "-D__PTRDIFF_TYPE__=" PCC_PTRDIFF_TYPE,
1799 "-D__SIZEOF_WINT_T__=4",
1800 "-D__ORDER_LITTLE_ENDIAN__=1234",
1801 "-D__ORDER_BIG_ENDIAN__=4321",
1802 "-D__ORDER_PDP_ENDIAN__=3412",
1803 #ifndef NO_C11
1804 "-D__STDC_UTF_16__=1",
1805 "-D__STDC_UTF_32__=1",
1806 "-D__STDC_NO_ATOMICS__=1",
1807 "-D__STDC_NO_THREADS__=1",
1808 #endif
1809
1810 /*
1811 * These should probably be changeable during runtime...
1812 */
1813 #if TARGET_ENDIAN == TARGET_BE
1814 "-D__FLOAT_WORD_ORDER__=__ORDER_BIG_ENDIAN__",
1815 "-D__BYTE_ORDER__=__ORDER_BIG_ENDIAN__",
1816 #elif TARGET_ENDIAN == TARGET_PDP
1817 "-D__FLOAT_WORD_ORDER__=__ORDER_PDP_ENDIAN__",
1818 "-D__BYTE_ORDER__=__ORDER_PDP_ENDIAN__",
1819 #elif TARGET_ENDIAN == TARGET_LE
1820 "-D__FLOAT_WORD_ORDER__=__ORDER_LITTLE_ENDIAN__",
1821 "-D__BYTE_ORDER__=__ORDER_LITTLE_ENDIAN__",
1822 #else
1823 #error Unknown endian...
1824 #endif
1825 };
1826
1827 static char *gcppflags[] = {
1828 #ifndef os_win32
1829 #ifdef GCC_COMPAT
1830 "-D__GNUC__=4",
1831 "-D__GNUC_MINOR__=3",
1832 "-D__GNUC_PATCHLEVEL__=1",
1833 "-D__REGISTER_PREFIX__=" REGISTER_PREFIX,
1834 "-D__USER_LABEL_PREFIX__=" USER_LABEL_PREFIX,
1835 #if SZLONG == 64
1836 "-D__SIZEOF_LONG__=8",
1837 #elif SZLONG == 32
1838 "-D__SIZEOF_LONG__=4",
1839 #endif
1840 #if SZPOINT(CHAR) == 64
1841 "-D__SIZEOF_POINTER__=8",
1842 #elif SZPOINT(CHAR) == 32
1843 "-D__SIZEOF_POINTER__=4",
1844 #endif
1845 #endif
1846 #endif
1847 NULL
1848 };
1849
1850 /* Use floating point definitions form softfloat.h */
1851
1852 static char *fpflags[] = {
1853 #ifdef TARGET_FLT_EVAL_METHOD
1854 "-D__FLT_EVAL_METHOD__=" MKS(TARGET_FLT_EVAL_METHOD),
1855 #endif
1856 #ifdef mach_i386
1857 "-D__STDC_IEC_559__",
1858 #endif
1859 #ifdef FLT_PREFIX
1860 "-D__FLT_RADIX__=" MKS(C(FLT_PREFIX,_RADIX)),
1861 "-D__FLT_DIG__=" MKS(C(FLT_PREFIX,_DIG)),
1862 "-D__FLT_EPSILON__=" MKS(C(FLT_PREFIX,_EPSILON)),
1863 "-D__FLT_MANT_DIG__=" MKS(C(FLT_PREFIX,_MANT_DIG)),
1864 "-D__FLT_MAX_10_EXP__=" MKS(C(FLT_PREFIX,_MAX_10_EXP)),
1865 "-D__FLT_MAX_EXP__=" MKS(C(FLT_PREFIX,_MAX_EXP)),
1866 "-D__FLT_MAX__=" MKS(C(FLT_PREFIX,_MAX)),
1867 "-D__FLT_MIN_10_EXP__=" MKS(C(FLT_PREFIX,_MIN_10_EXP)),
1868 "-D__FLT_MIN_EXP__=" MKS(C(FLT_PREFIX,_MIN_EXP)),
1869 "-D__FLT_MIN__=" MKS(C(FLT_PREFIX,_MIN)),
1870 "-D__FLT_HAS_SUBNORM__=" MKS(C(FLT_PREFIX,_HAS_SUBNORM)),
1871 "-D__FLT_TRUE_MIN__=" MKS(C(FLT_PREFIX,_TRUE_MIN)),
1872 #endif
1873 #ifdef DBL_PREFIX
1874 "-D__DBL_DIG__=" MKS(C(DBL_PREFIX,_DIG)),
1875 "-D__DBL_EPSILON__=" MKS(C(DBL_PREFIX,_EPSILON)),
1876 "-D__DBL_MANT_DIG__=" MKS(C(DBL_PREFIX,_MANT_DIG)),
1877 "-D__DBL_MAX_10_EXP__=" MKS(C(DBL_PREFIX,_MAX_10_EXP)),
1878 "-D__DBL_MAX_EXP__=" MKS(C(DBL_PREFIX,_MAX_EXP)),
1879 "-D__DBL_MAX__=" MKS(C(DBL_PREFIX,_MAX)),
1880 "-D__DBL_MIN_10_EXP__=" MKS(C(DBL_PREFIX,_MIN_10_EXP)),
1881 "-D__DBL_MIN_EXP__=" MKS(C(DBL_PREFIX,_MIN_EXP)),
1882 "-D__DBL_MIN__=" MKS(C(DBL_PREFIX,_MIN)),
1883 "-D__DBL_HAS_SUBNORM__=" MKS(C(DBL_PREFIX,_HAS_SUBNORM)),
1884 "-D__DBL_TRUE_MIN__=" MKS(C(DBL_PREFIX,_TRUE_MIN)),
1885 #endif
1886 #ifdef LDBL_PREFIX
1887 "-D__LDBL_DIG__=" MKS(C(LDBL_PREFIX,_DIG)),
1888 "-D__LDBL_EPSILON__=" MKS(C(LDBL_PREFIX,_EPSILON)),
1889 "-D__LDBL_MANT_DIG__=" MKS(C(LDBL_PREFIX,_MANT_DIG)),
1890 "-D__LDBL_MAX_10_EXP__=" MKS(C(LDBL_PREFIX,_MAX_10_EXP)),
1891 "-D__LDBL_MAX_EXP__=" MKS(C(LDBL_PREFIX,_MAX_EXP)),
1892 "-D__LDBL_MAX__=" MKS(C(LDBL_PREFIX,_MAX)),
1893 "-D__LDBL_MIN_10_EXP__=" MKS(C(LDBL_PREFIX,_MIN_10_EXP)),
1894 "-D__LDBL_MIN_EXP__=" MKS(C(LDBL_PREFIX,_MIN_EXP)),
1895 "-D__LDBL_MIN__=" MKS(C(LDBL_PREFIX,_MIN)),
1896 "-D__LDBL_HAS_SUBNORM__=" MKS(C(LDBL_PREFIX,_HAS_SUBNORM)),
1897 "-D__LDBL_TRUE_MIN__=" MKS(C(LDBL_PREFIX,_TRUE_MIN)),
1898 #endif
1899 NULL
1900 };
1901
1902 /*
1903 * Configure the standard cpp flags.
1904 */
1905 void
1906 setup_cpp_flags(void)
1907 {
1908 int i;
1909
1910 /* a bunch of misc defines */
1911 for (i = 0; i < (int)sizeof(defflags)/(int)sizeof(char *); i++)
1912 strlist_prepend(&preprocessor_flags, defflags[i]);
1913
1914 for (i = 0; gcppflags[i]; i++)
1915 strlist_prepend(&preprocessor_flags, gcppflags[i]);
1916 strlist_prepend(&preprocessor_flags, xgnu89 ?
1917 "-D__GNUC_GNU_INLINE__" : "-D__GNUC_STDC_INLINE__");
1918
1919 cksetflags(cppflgcheck, &preprocessor_flags, 'p');
1920
1921 /* Create time and date defines */
1922 if (tflag == 0) {
1923 char buf[100]; /* larger than needed */
1924 time_t t = time(NULL);
1925 char *n = ctime(&t);
1926
1927 n[19] = 0;
1928 snprintf(buf, sizeof buf, "-D__TIME__=\"%s\"", n+11);
1929 strlist_prepend(&preprocessor_flags, xstrdup(buf));
1930
1931 n[24] = n[11] = 0;
1932 snprintf(buf, sizeof buf, "-D__DATE__=\"%s%s\"", n+4, n+20);
1933 strlist_prepend(&preprocessor_flags, xstrdup(buf));
1934 }
1935
1936 for (i = 0; fpflags[i]; i++)
1937 strlist_prepend(&preprocessor_flags, fpflags[i]);
1938
1939 for (i = 0; cppadd[i]; i++)
1940 strlist_prepend(&preprocessor_flags, cppadd[i]);
1941 for (i = 0; cppmdadd[i]; i++)
1942 strlist_prepend(&preprocessor_flags, cppmdadd[i]);
1943
1944 /* Include dirs */
1945 strlist_append(&sysincdirs, "=" INCLUDEDIR "pcc/");
1946 #ifdef STDINC_MA
1947 strlist_append(&sysincdirs, "=" STDINC_MA);
1948 #endif
1949 #ifdef PCCINCDIR
1950 if (cxxflag)
1951 strlist_append(&sysincdirs, "=" PCCINCDIR "/c++");
1952 strlist_append(&sysincdirs, "=" PCCINCDIR);
1953 #endif
1954 strlist_append(&sysincdirs, "=" STDINC);
1955 }
1956
1957 struct flgcheck ccomflgcheck[] = {
1958 { &Oflag, 1, "-xtemps" },
1959 { &Oflag, 1, "-xdeljumps" },
1960 { &Oflag, 1, "-xinline" },
1961 { &Oflag, 1, "-xdce" },
1962 { &Oflag, 1, "-xssa" },
1963 { &freestanding, 1, "-ffreestanding" },
1964 { &pgflag, 1, "-p" },
1965 { &gflag, 1, "-g" },
1966 { &xgnu89, 1, "-xgnu89" },
1967 { &xgnu99, 1, "-xgnu99" },
1968 { &xuchar, 1, "-xuchar" },
1969 #if !defined(os_sunos) && !defined(mach_i386)
1970 { &vflag, 1, "-v" },
1971 #endif
1972 #if defined(os_darwin)
1973 { &Bstatic, 0, "-k" },
1974 #elif defined(os_sunos) && defined(mach_i386)
1975 { &kflag, 1, "-K" },
1976 { &kflag, 1, "pic" },
1977 #else
1978 { &kflag, 1, "-k" },
1979 #endif
1980 { &sspflag, 1, "-fstack-protector" },
1981 { 0 }
1982 };
1983
1984 void
1985 setup_ccom_flags(void)
1986 {
1987
1988 cksetflags(ccomflgcheck, &compiler_flags, 'a');
1989 }
1990
1991 #if defined(USE_YASM) || defined(os_win32) || defined(os_darwin) || \
1992 (defined(os_sunos) && defined(mach_sparc64))
1993 static int one = 1;
1994 #endif
1995
1996 struct flgcheck asflgcheck[] = {
1997 #if defined(USE_YASM)
1998 { &one, 1, "-p" },
1999 { &one, 1, "gnu" },
2000 { &one, 1, "-f" },
2001 #if defined(os_win32)
2002 { &one, 1, "win32" },
2003 #elif defined(os_darwin)
2004 { &one, 1, "macho" },
2005 #else
2006 { &one, 1, "elf" },
2007 #endif
2008 #endif
2009 #if defined(os_sunos) && defined(mach_sparc64)
2010 { &one, 1, "-m64" },
2011 #endif
2012 #if defined(os_darwin)
2013 { &Bstatic, 1, "-static" },
2014 #endif
2015 #if !defined(USE_YASM) && !defined(NO_AS_V)
2016 { &vflag, 1, "-v" },
2017 #endif
2018 #if defined(os_openbsd) && defined(mach_mips64)
2019 { &kflag, 1, "-KPIC" },
2020 #else
2021 { &kflag, 1, "-k" },
2022 #endif
2023 #ifdef os_darwin
2024 { &one, 1, "-arch" },
2025 #if mach_amd64
2026 { &amd64_i386, 1, "i386" },
2027 { &amd64_i386, 0, "x86_64" },
2028 #else
2029 { &one, 1, "i386" },
2030 #endif
2031 #else
2032 #ifdef mach_amd64
2033 { &amd64_i386, 1, "--32" },
2034 #endif
2035 #endif
2036 { 0 }
2037 };
2038 void
2039 setup_as_flags(void)
2040 {
2041 #ifdef PCC_SETUP_AS_ARGS
2042 PCC_SETUP_AS_ARGS
2043 #endif
2044 cksetflags(asflgcheck, &assembler_flags, 'a');
2045 }
2046
2047 struct flgcheck ldflgcheck[] = {
2048 #ifndef MSLINKER
2049 { &vflag, 1, "-v" },
2050 #endif
2051 #ifdef os_darwin
2052 { &shared, 1, "-dylib" },
2053 #elif defined(os_win32)
2054 { &shared, 1, "-Bdynamic" },
2055 #else
2056 { &shared, 1, "-shared" },
2057 #endif
2058 #if !defined(os_sunos) && !defined(os_win32)
2059 #ifndef os_darwin
2060 { &shared, 0, "-d" },
2061 #endif
2062 #endif
2063 #ifdef os_darwin
2064 { &Bstatic, 1, "-static" },
2065 #else
2066 { &Bstatic, 1, "-Bstatic" },
2067 #endif
2068 #if !defined(os_darwin) && !defined(os_sunos)
2069 { &gflag, 1, "-g" },
2070 #endif
2071 { &pthreads, 1, "-lpthread" },
2072 { 0 },
2073 };
2074
2075 static void
2076 strap(struct strlist *sh, struct strlist *cd, char *n, int where)
2077 {
2078 void (*fn)(struct strlist *, const char *);
2079 char *fil;
2080
2081 if (n == 0)
2082 return; /* no crtfile */
2083
2084 fn = where == 'p' ? strlist_prepend : strlist_append;
2085 fil = find_file(n, cd, R_OK);
2086 (*fn)(sh, fil);
2087 }
2088
2089 void
2090 setup_ld_flags(void)
2091 {
2092 char *b, *e;
2093 int i;
2094
2095 #ifdef PCC_SETUP_LD_ARGS
2096 PCC_SETUP_LD_ARGS
2097 #endif
2098
2099 cksetflags(ldflgcheck, &early_linker_flags, 'a');
2100 if (Bstatic == 0 && shared == 0 && rflag == 0) {
2101 if (dynlinklib) {
2102 strlist_append(&early_linker_flags, dynlinkarg);
2103 strlist_append(&early_linker_flags, dynlinklib);
2104 }
2105 #ifndef os_darwin
2106 strlist_append(&early_linker_flags, "-e");
2107 strlist_append(&early_linker_flags, STARTLABEL);
2108 #endif
2109 }
2110 if (shared == 0 && rflag)
2111 strlist_append(&early_linker_flags, "-r");
2112 #ifdef STARTLABEL_S
2113 if (shared == 1) {
2114 strlist_append(&early_linker_flags, "-e");
2115 strlist_append(&early_linker_flags, STARTLABEL_S);
2116 }
2117 #endif
2118 if (sysroot && *sysroot)
2119 strlist_append(&early_linker_flags, cat("--sysroot=", sysroot));
2120 if (!nostdlib) {
2121 /* library search paths */
2122 if (pcclibdir)
2123 strlist_append(&late_linker_flags,
2124 cat("-L", pcclibdir));
2125 for (i = 0; deflibdirs[i]; i++)
2126 strlist_append(&late_linker_flags,
2127 cat("-L", deflibdirs[i]));
2128 /* standard libraries */
2129 if (pgflag) {
2130 for (i = 0; defproflibs[i]; i++)
2131 strlist_append(&late_linker_flags,
2132 defproflibs[i]);
2133 } else if (cxxflag) {
2134 for (i = 0; defcxxlibs[i]; i++)
2135 strlist_append(&late_linker_flags,
2136 defcxxlibs[i]);
2137 } else {
2138 for (i = 0; deflibs[i]; i++)
2139 strlist_append(&late_linker_flags, deflibs[i]);
2140 }
2141 }
2142 if (!nostartfiles) {
2143 if (Bstatic) {
2144 b = CRTBEGIN_T;
2145 e = CRTEND_T;
2146 } else if (shared /* || pieflag */) {
2147 b = CRTBEGIN_S;
2148 e = CRTEND_S;
2149 } else {
2150 b = CRTBEGIN;
2151 e = CRTEND;
2152 }
2153 strap(&middle_linker_flags, &crtdirs, b, 'p');
2154 strap(&late_linker_flags, &crtdirs, e, 'a');
2155 strap(&middle_linker_flags, &crtdirs, CRTI, 'p');
2156 strap(&late_linker_flags, &crtdirs, CRTN, 'a');
2157 #ifdef os_win32
2158 /*
2159 * On Win32 Cygwin/MinGW runtimes, the profiling code gcrtN.o
2160 * comes in addition to crtN.o or dllcrtN.o
2161 */
2162 if (pgflag)
2163 strap(&middle_linker_flags, &crtdirs, GCRT0, 'p');
2164 if (shared == 0)
2165 b = CRT0;
2166 else
2167 b = CRT0_S; /* dllcrtN.o */
2168 strap(&middle_linker_flags, &crtdirs, b, 'p');
2169 #else
2170 if (shared == 0) {
2171 if (pgflag)
2172 b = GCRT0;
2173 else if (pieflag)
2174 b = RCRT0;
2175 else
2176 b = CRT0;
2177 #ifndef os_darwin
2178 strap(&middle_linker_flags, &crtdirs, b, 'p');
2179 #endif
2180 }
2181 #endif
2182 }
2183 }
2184
2185 #ifdef _WIN32
2186 char *
2187 win32pathsubst(char *s)
2188 {
2189 char env[1024];
2190 DWORD len;
2191
2192 len = ExpandEnvironmentStrings(s, env, sizeof(env));
2193 if (len == 0 || len > sizeof(env))
2194 errorx(8, "ExpandEnvironmentStrings failed, len %lu", len);
2195
2196 len--; /* skip nil */
2197 while (len-- > 0 && (env[len] == '/' || env[len] == '\\'))
2198 env[len] = '\0';
2199
2200 return xstrdup(env);
2201 }
2202
2203 char *
2204 win32commandline(struct strlist *l)
2205 {
2206 const struct string *s;
2207 char *cmd;
2208 char *p;
2209 int len;
2210 int j, k;
2211
2212 len = 0;
2213 STRLIST_FOREACH(s, l) {
2214 len++;
2215 for (j = 0; s->value[j] != '\0'; j++) {
2216 if (s->value[j] == '\"') {
2217 for (k = j-1; k >= 0 && s->value[k] == '\\'; k--)
2218 len++;
2219 len++;
2220 }
2221 len++;
2222 }
2223 for (k = j-1; k >= 0 && s->value[k] == '\\'; k--)
2224 len++;
2225 len++;
2226 len++;
2227 }
2228
2229 p = cmd = xmalloc(len);
2230
2231 STRLIST_FOREACH(s, l) {
2232 *p++ = '\"';
2233 for (j = 0; s->value[j] != '\0'; j++) {
2234 if (s->value[j] == '\"') {
2235 for (k = j-1; k >= 0 && s->value[k] == '\\'; k--)
2236 *p++ = '\\';
2237 *p++ = '\\';
2238 }
2239 *p++ = s->value[j];
2240 }
2241 for (k = j-1; k >= 0 && s->value[k] == '\\'; k--)
2242 *p++ = '\\';
2243 *p++ = '\"';
2244 *p++ = ' ';
2245 }
2246 p[-1] = '\0';
2247
2248 return cmd;
2249 }
2250 #endif