Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20080420045413

Diff

Diff from 1.96 to:

Annotations

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

Annotated File View

gmcgarry
1.96
1 /*      $Id: cc.c,v 1.96 2008/04/20 04:54:13 gmcgarry Exp $     */
ragge
1.3
2 /*
3  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code and documentation must retain the above
10  * copyright notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditionsand the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * All advertising materials mentioning features or use of this software
15  * must display the following acknowledgement:
16  *      This product includes software developed or owned by Caldera
17  *      International, Inc.
18  * Neither the name of Caldera International, Inc. nor the names of other
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27  * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
ragge
1.2
35
ragge
1.1
36 /*
ragge
1.3
37  * Front-end to the C compiler.
ragge
1.18
38  *
39  * Brief description of its syntax:
40  * - Files that end with .c are passed via cpp->ccom->as->ld
ragge
1.59
41  * - Files that end with .i are passed via ccom->as->ld
ragge
1.18
42  * - Files that end with .s are passed as->ld
43  * - Files that end with .o are passed directly to ld
44  * - Multiple files may be given on the command line.
45  * - Unrecognized options are all sent directly to ld.
46  * -c or -S cannot be combined with -o if multiple files are given.
47  *
48  * This file should be rewritten readable.
ragge
1.1
49  */
ragge
1.80
50 #include "config.h"
51
ragge
1.9
52 #include <sys/types.h>
gmcgarry
1.86
53 #ifdef HAVE_SYS_WAIT_H
ragge
1.36
54 #include <sys/wait.h>
gmcgarry
1.86
55 #endif
ragge
1.2
56
ragge
1.1
57 #include <ctype.h>
stefan
1.72
58 #include <errno.h>
59 #include <fcntl.h>
gmcgarry
1.86
60 #ifdef HAVE_LIBGEN_H
stefan
1.72
61 #include <libgen.h>
gmcgarry
1.86
62 #endif
ragge
1.1
63 #include <signal.h>
stefan
1.72
64 #include <stdarg.h>
65 #include <stdio.h>
66 #include <stdlib.h>
ragge
1.4
67 #include <string.h>
gmcgarry
1.86
68 #ifdef HAVE_UNISTD_H
ragge
1.4
69 #include <unistd.h>
gmcgarry
1.86
70 #endif
71
gmcgarry
1.87
72 #ifdef WIN32
gmcgarry
1.86
73 #include <windows.h>
74 #include <process.h>
75 #include <io.h>
76 #endif
77
78 #include "compat.h"
ragge
1.24
79
80 #include "ccconfig.h"
ragge
1.3
81 /* C command */
ragge
1.1
82
ragge
1.20
83 #define MKS(x) _MKS(x)
84 #define _MKS(x) #x
85
86 /*
87  * Many specific definitions, should be declared elsewhere.
88  */
gmcgarry
1.79
89
90 #ifndef STDINC
91 #define STDINC          "/usr/include/"
92 #endif
93
94 #ifndef PREPROCESSOR
95 #define PREPROCESSOR    "cpp"
96 #endif
97
98 #ifndef COMPILER
99 #define COMPILER        "ccom";
100 #endif
101
102 #ifndef ASSEMBLER
103 #define ASSEMBLER       "as"
104 #endif
105
106 #ifndef LINKER
107 #define LINKER          "ld"
108 #endif
ragge
1.20
109
ragge
1.62
110 #define MAXFIL 10000
ragge
1.12
111 #define MAXLIB 10000
ragge
1.14
112 #define MAXAV  10000
ragge
1.3
113 #define MAXOPT 100
114 char    *tmp3;
115 char    *tmp4;
ragge
1.1
116 char    *outfile;
ragge
1.91
117 char *copy(char *, int),*setsuf(char *, char);
stefan
1.72
118 int getsuf(char *);
ragge
1.4
119 int main(intchar *[]);
ragge
1.36
120 void error(char *, ...);
stefan
1.72
121 void errorx(intchar *, ...);
ragge
1.4
122 int callsys(char [], char *[]);
123 int cunlink(char *);
stefan
1.72
124 void dexit(int);
ragge
1.4
125 void idexit(int);
pj
1.30
126 char *gettmp();
ragge
1.91
127 void *ccmalloc(int size);
ragge
1.14
128 char    *av[MAXAV];
ragge
1.3
129 char    *clist[MAXFIL];
130 char    *llist[MAXLIB];
ragge
1.4
131 char    alist[20];
ragge
1.43
132 char    *xlist[100];
133 int     xnum;
gmcgarry
1.78
134 char    *mlist[100];
ragge
1.83
135 char    *idirafter;
gmcgarry
1.78
136 int     nm;
ragge
1.63
137 int     Cflag;
ragge
1.43
138 int     dflag;
ragge
1.3
139 int     pflag;
140 int     sflag;
141 int     cflag;
142 int     eflag;
143 int     gflag;
ragge
1.5
144 int     vflag;
ragge
1.7
145 int     tflag;
ragge
1.8
146 int     Eflag;
ragge
1.4
147 int     Oflag;
ragge
1.58
148 int     kflag;  /* generate PIC/pic code */
149 #define F_PIC   1
150 #define F_pic   2
ragge
1.51
151 int     Mflag;  /* dependencies only */
ragge
1.52
152 int     pgflag;
ragge
1.1
153 int     exfail;
ragge
1.10
154 int     Xflag;
gmcgarry
1.82
155 int     Werror;
ragge
1.84
156 int     nostartfilesBstaticshared;
ragge
1.52
157 int     nostdincnostdlib;
ragge
1.43
158 int     onlyas;
ragge
1.52
159 int     pthreads;
ragge
1.91
160 int     xcflag;
ragge
1.14
161
gmcgarry
1.79
162 char    *passp = LIBEXECDIR "/" PREPROCESSOR;
163 char    *pass0 = LIBEXECDIR "/" COMPILER;
164 char    *as = ASSEMBLER;
165 char    *ld = LINKER;
ragge
1.47
166 char    *Bflag;
ragge
1.25
167 char *cppadd[] = CPPADD;
gmcgarry
1.94
168 #ifdef DYNLINKER
ragge
1.25
169 char *dynlinker[] = DYNLINKER;
gmcgarry
1.94
170 #endif
171 #ifdef CRT0FILE
ragge
1.25
172 char *crt0file = CRT0FILE;
gmcgarry
1.94
173 #endif
174 #ifdef CRT0FILE_PROFILE
gmcgarry
1.81
175 char *crt0file_profile = CRT0FILE_PROFILE;
gmcgarry
1.94
176 #endif
177 #ifdef STARTFILES
ragge
1.25
178 char *startfiles[] = STARTFILES;
179 char *endfiles[] = ENDFILES;
gmcgarry
1.94
180 #endif
ragge
1.84
181 #ifdef STARTFILES_S
182 char *startfiles_S[] = STARTFILES_S;
183 char *endfiles_S[] = ENDFILES_S;
184 #endif
ragge
1.25
185 char *cppmdadd[] = CPPMDADD;
ragge
1.50
186 #ifdef LIBCLIBS
187 char *libclibs[] = LIBCLIBS;
188 #else
189 char *libclibs[] = { "-lc"NULL };
190 #endif
gmcgarry
1.81
191 #ifdef LIBCLIBS_PROFILE
192 char *libclibs_profile[] = LIBCLIBS_PROFILE;
193 #else
194 char *libclibs_profile[] = { "-lc_p"NULL };
195 #endif
ragge
1.48
196 #ifndef STARTLABEL
197 #define STARTLABEL "__start"
198 #endif
ragge
1.1
199
ragge
1.4
200 int
pj
1.26
201 main(int argcchar *argv[])
202 {
ragge
1.15
203         char *t, *u;
ragge
1.1
204         char *assource;
ragge
1.3
205         char **pv, *ptemp[MAXOPT], **pvt;
ragge
1.36
206         int ncnlijcnxona;
ragge
1.3
207
ragge
1.36
208         i = nc = nl = nxo = 0;
ragge
1.3
209         pv = ptemp;
210         while(++i < argc) {
ragge
1.66
211                 if (argv[i][0] == '-') {
212                         switch (argv[i][1]) {
213                         default:
214                                 goto passa;
215 #ifdef notyet
216         /* must add library options first (-L/-l/...) */
217                                 error("unrecognized option `-%c'"argv[i][1]);
218                                 break;
219 #endif
ragge
1.22
220
ragge
1.66
221                         case 'B'/* other search paths for binaries */
222                                 Bflag = &argv[i][2];
223                                 break;
ragge
1.22
224
ragge
1.66
225                         case 'X':
226                                 Xflag++;
227                                 break;
228                         case 'W'/* Ignore (most of) W-flags */
gmcgarry
1.82
229                                 if (strncmp(argv[i], "-Werror"7) == 0) {
230                                         Werror = 1;
231                                 } else if (strncmp(argv[i], "-Wl,"4) == 0) {
ragge
1.66
232                                         /* options to the linker */
233                                         t = &argv[i][4];
234                                         while ((u = strchr(t','))) {
235                                                 *u++ = 0;
236                                                 llist[nl++] = t;
237                                                 t = u;
238                                         }
ragge
1.15
239                                         llist[nl++] = t;
ragge
1.83
240                                 } else if (strncmp(argv[i], "-Wp,"4) == 0) {
241                                         /* preprocessor */
242                                         if (!strncmp(argv[i], "-Wp,-C"6))
243                                                 Cflag++;
ragge
1.15
244                                 }
ragge
1.66
245                                 break;
ragge
1.15
246
ragge
1.66
247                         case 'f'/* GCC compatibility flags */
248                                 if (strcmp(argv[i], "-fPIC") == 0)
249                                         kflag = F_PIC;
250                                 if (strcmp(argv[i], "-fpic") == 0)
251                                         kflag = F_pic;
252                                 /* silently ignore the rest */
253                                 break;
ragge
1.14
254
ragge
1.66
255                         case 'g'/* create debug output */
256                                 gflag++;
257                                 break;
ragge
1.25
258
ragge
1.66
259                         case 'i':
260                                 if (strcmp(argv[i], "-isystem") == 0) {
261                                         *pv++ = "-S";
262                                         *pv++ = argv[++i];
263                                 } else if (strcmp(argv[i], "-include") == 0) {
264                                         *pv++ = "-i";
265                                         *pv++ = argv[++i];
ragge
1.83
266                                 } else if (strcmp(argv[i], "-idirafter") == 0) {
267                                         idirafter = argv[++i];
ragge
1.66
268                                 } else
269                                         goto passa;
270                                 break;
ragge
1.20
271
ragge
1.66
272                         case 'k'/* generate PIC code */
273                                 kflag = F_pic;
274                                 break;
ragge
1.49
275
gmcgarry
1.78
276                         case 'm'/* target-dependent options */
277                                 mlist[nm++] = argv[i];
278                                 break;
279
ragge
1.66
280                         case 'n'/* handle -n flags */
281                                 if (strcmp(argv[i], "-nostdinc") == 0)
282                                         nostdinc++;
283                                 else if (strcmp(argv[i], "-nostdlib") == 0) {
284                                         nostdlib++;
285                                         nostartfiles++;
286                                 } else if (strcmp(argv[i], "-nostartfiles") == 0)
287                                         nostartfiles = 1;
288                                 else
289                                         goto passa;
290                                 break;
ragge
1.14
291
ragge
1.66
292                         case 'p':
ragge
1.88
293                                 if (strcmp(argv[i], "-pg") == 0 ||
294                                     strcmp(argv[i], "-p") == 0)
ragge
1.66
295                                         pgflag++;
296                                 else if (strcmp(argv[i], "-pthread") == 0)
297                                         pthreads++;
gmcgarry
1.81
298                                 else if (strcmp(argv[i], "-pipe") == 0) {
299                                 } else
ragge
1.66
300                                         errorx(1"unknown option %s"argv[i]);
301                                 break;
ragge
1.52
302
ragge
1.66
303                         case 'x':
ragge
1.83
304                                 t = &argv[i][2];
305                                 if (*t == 0)
306                                         t = argv[++i];
307                                 if (strcmp(t"c") == 0)
ragge
1.91
308                                         xcflag = 1/* default */
ragge
1.83
309 #ifdef notyet
310                                 else if (strcmp(t"c++")
311                                         cxxflag++;
312 #endif
313                                 else
314                                         xlist[xnum++] = argv[i];
ragge
1.66
315                                 break;
316                         case 't':
317                                 tflag++;
318                                 break;
319                         case 'S':
320                                 sflag++;
321                                 cflag++;
322                                 break;
323                         case 'o':
324                                 if (outfile)
325                                         errorx(8"too many -o");
326                                 outfile = argv[++i];
327                                 break;
328                         case 'O':
gmcgarry
1.96
329                                 if (argv[i][2] == '0')
330                                         Oflag = 0;
331                                 else
ragge
1.85
332                                         Oflag++;
ragge
1.66
333                                 break;
334                         case 'E':
335                                 Eflag++;
336                                 break;
337                         case 'P':
338                                 pflag++;
339                                 *pv++ = argv[i];
340                         case 'c':
341                                 cflag++;
342                                 break;
ragge
1.3
343
ragge
1.24
344 #if 0
ragge
1.66
345                         case '2':
346                                 if(argv[i][2] == '\0')
347                                         pref = "/lib/crt2.o";
348                                 else {
349                                         pref = "/lib/crt20.o";
350                                 }
351                                 break;
ragge
1.24
352 #endif
ragge
1.66
353                         case 'C':
354                                 Cflag = 1;
355                                 break;
356                         case 'D':
357                         case 'I':
358                         case 'U':
359                                 *pv++ = argv[i];
360                                 if (argv[i][2] == 0)
361                                         *pv++ = argv[++i];
362                                 if (pv >= ptemp+MAXOPT) {
363                                         error("Too many DIU options");
364                                         --pv;
ragge
1.3
365                                 }
ragge
1.66
366                                 break;
ragge
1.3
367
ragge
1.66
368                         case 'M':
369                                 Mflag++;
370                                 break;
ragge
1.51
371
ragge
1.66
372                         case 'd':
373                                 dflag++;
ragge
1.67
374                                 strlcpy(alistargv[i], sizeof (alist));
ragge
1.66
375                                 break;
376                         case 'v':
377                                 printf("%s\n"VERSSTR);
378                                 vflag++;
379                                 break;
ragge
1.14
380
ragge
1.66
381                         case 's':
382                                 if (strcmp(argv[i], "-static") == 0)
383                                         Bstatic = 1;
ragge
1.84
384                                 else if (strcmp(argv[i], "-shared") == 0) {
385                                         shared = 1;
386                                         nostdlib = 1;
387                                 } else
ragge
1.66
388                                         goto passa;
389                                 break;
390                         }
ragge
1.3
391                 } else {
392                 passa:
393                         t = argv[i];
ragge
1.11
394                         if (*argv[i] == '-' && argv[i][1] == 'L')
395                                 ;
ragge
1.59
396                         else if((c=getsuf(t))=='c' || c=='S' || c=='i' ||
ragge
1.91
397                             c=='s'|| Eflag || xcflag) {
ragge
1.3
398                                 clist[nc++] = t;
ragge
1.66
399                                 if (nc>=MAXFIL) {
ragge
1.36
400                                         error("Too many source files");
ragge
1.3
401                                         exit(1);
ragge
1.66
402                                 }
ragge
1.3
403                                 t = setsuf(t'o');
404                         }
stefan
1.76
405
406                         /* Check for duplicate .o files. */
407                         for (j = getsuf(t) == 'o' ? 0 : nlj < nlj++) {
408                                 if (strcmp(llist[j], t) == 0)
409                                         break;
410                         }
411                         if (j == nl) {
ragge
1.3
412                                 llist[nl++] = t;
ragge
1.66
413                                 if (nl >= MAXLIB) {
ragge
1.36
414                                         error("Too many object/library files");
ragge
1.3
415                                         exit(1);
ragge
1.66
416                                 }
ragge
1.3
417                                 if (getsuf(t)=='o')
418                                         nxo++;
ragge
1.1
419                         }
420                 }
421         }
ragge
1.18
422         /* Sanity checking */
423         if (nc == 0 && nl == 0)
ragge
1.36
424                 errorx(8"no input files");
ragge
1.61
425         if (outfile && (cflag || sflag || Eflag) && nc > 1)
426                 errorx(8"-o given with -c || -E || -S and more than one file");
ragge
1.19
427         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
ragge
1.36
428                 errorx(8"output file will be clobbered");
ragge
1.4
429         if (gflagOflag = 0;
ragge
1.24
430 #if 0
431         if (proflag)
ragge
1.3
432                 pref = "/lib/mcrt0.o";
ragge
1.24
433 #endif
ragge
1.3
434         if(nc==0)
435                 goto nocom;
436         if (pflag==0) {
stefan
1.74
437                 if (!sflag)
438                         tmp3 = gettmp();
pj
1.30
439                 tmp4 = gettmp();
ragge
1.1
440         }
ragge
1.3
441         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
ragge
1.1
442                 signal(SIGINTidexit);
ragge
1.3
443         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
ragge
1.1
444                 signal(SIGTERMidexit);
ragge
1.3
445         pvt = pv;
ragge
1.1
446         for (i=0i<nci++) {
ragge
1.18
447                 /*
448                  * C preprocessor
449                  */
ragge
1.37
450                 if (nc>1 && !Eflag)
ragge
1.1
451                         printf("%s:\n"clist[i]);
ragge
1.43
452                 onlyas = 0;
pj
1.32
453                 assource = tmp3;
ragge
1.59
454                 if (getsuf(clist[i])=='i') {
455                         if(Eflag)
456                                 continue;
457                         goto com;
458                 } else if (getsuf(clist[i])=='s') {
ragge
1.1
459                         assource = clist[i];
ragge
1.43
460                         goto assemble;
stefan
1.73
461                 } else if (getsuf(clist[i])=='S')
ragge
1.43
462                         onlyas = 1;
ragge
1.1
463                 if (pflag)
464                         tmp4 = setsuf(clist[i], 'i');
ragge
1.5
465                 na = 0;
466                 av[na++] = "cpp";
ragge
1.60
467                 if (vflag)
468                         av[na++] = "-v";
ragge
1.20
469                 av[na++] = "-D__PCC__=" MKS(PCC_MAJOR);
ragge
1.40
470                 av[na++] = "-D__PCC_MINOR__=" MKS(PCC_MINOR);
471                 av[na++] = "-D__PCC_MINORMINOR__=" MKS(PCC_MINORMINOR);
ragge
1.52
472                 if (getsuf(clist[i])=='S')
473                         av[na++] = "-D__ASSEMBLER__";
474                 if (pthreads)
475                         av[na++] = "-D_PTHREADS";
ragge
1.63
476                 if (Cflag)
477                         av[na++] = "-C";
ragge
1.51
478                 if (Mflag)
479                         av[na++] = "-M";
ragge
1.57
480                 if (dflag)
481                         av[na++] = alist;
ragge
1.5
482                 for (j = 0cppadd[j]; j++)
483                         av[na++] = cppadd[j];
ragge
1.24
484                 for (j = 0cppmdadd[j]; j++)
485                         av[na++] = cppmdadd[j];
ragge
1.7
486                 if (tflag)
ragge
1.20
487                         av[na++] = "-t";
ragge
1.7
488                 for(pv=ptemppv <pvtpv++)
489                         av[na++] = *pv;
ragge
1.52
490                 if (!nostdinc)
491                         av[na++] = "-S"av[na++] = STDINC;
ragge
1.83
492                 if (idirafter) {
493                         av[na++] = "-I";
494                         av[na++] = idirafter;
495                 }
ragge
1.5
496                 av[na++] = clist[i];
ragge
1.51
497                 if (!Eflag && !Mflag)
ragge
1.20
498                         av[na++] = tmp4;
ragge
1.61
499                 if (Eflag && outfile)
500                          av[na++] = outfile;
ragge
1.3
501                 av[na++]=0;
502                 if (callsys(passpav))
503                         {exfail++; eflag++;}
ragge
1.51
504                 if (Eflag || Mflag)
ragge
1.37
505                         continue;
stefan
1.73
506                 if (onlyas) {
507                         assource = tmp4;
ragge
1.7
508                         goto assemble;
stefan
1.73
509                 }
ragge
1.18
510
511                 /*
512                  * C compiler
513                  */
ragge
1.59
514         com:
ragge
1.25
515                 na = 0;
516                 av[na++]= "ccom";
ragge
1.60
517                 if (vflag)
518                         av[na++] = "-v";
gmcgarry
1.81
519                 if (pgflag)
520                         av[na++] = "-p";
ragge
1.25
521                 if (gflag)
522                         av[na++] = "-g";
gmcgarry
1.94
523 #ifdef os_darwin
gmcgarry
1.82
524                 /* darwin always wants PIC compilation */
525                 av[na++] = "-k";
526 #else
ragge
1.49
527                 if (kflag)
528                         av[na++] = "-k";
gmcgarry
1.82
529 #endif
530                 if (Werror)
531                         av[na++] = "-Werror";
ragge
1.44
532                 if (Oflag) {
533                         av[na++] = "-xtemps";
ragge
1.64
534                         av[na++] = "-xdeljumps";
ragge
1.44
535                 }
ragge
1.45
536                 for (j = 0j < xnumj++)
537                         av[na++] = xlist[j];
gmcgarry
1.78
538                 for (j = 0j < nmj++)
539                         av[na++] = mlist[j];
ragge
1.68
540                 if (getsuf(clist[i])=='i')
541                         av[na++] = clist[i];
542                 else
543                         av[na++] = tmp4/* created by cpp */
ragge
1.3
544                 if (pflag || exfail)
545                         {
ragge
1.1
546                         cflag++;
547                         continue;
ragge
1.3
548                         }
ragge
1.16
549                 if(sflag) {
550                         if (outfile)
stefan
1.77
551                                 tmp3 = outfile;
ragge
1.16
552                         else
stefan
1.77
553                                 tmp3 = setsuf(clist[i], 's');
ragge
1.16
554                 }
ragge
1.25
555                 av[na++] = tmp3;
556 #if 0
ragge
1.3
557                 if (proflag) {
558                         av[3] = "-XP";
559                         av[4] = 0;
560                 } else
561                         av[3] = 0;
ragge
1.25
562 #endif
563                 av[na++] = NULL;
ragge
1.3
564                 if (callsys(pass0av)) {
ragge
1.1
565                         cflag++;
566                         eflag++;
567                         continue;
568                 }
569                 if (sflag)
570                         continue;
ragge
1.18
571
572                 /*
573                  * Assembler
574                  */
ragge
1.1
575         assemble:
ragge
1.49
576                 na = 0;
gmcgarry
1.79
577                 av[na++] = as;
ragge
1.60
578                 if (vflag)
579                         av[na++] = "-v";
ragge
1.49
580                 if (kflag)
581                         av[na++] = "-k";
582                 av[na++] = "-o";
ragge
1.18
583                 if (outfile && cflag)
ragge
1.49
584                         av[na++] = outfile;
ragge
1.18
585                 else
ragge
1.49
586                         av[na++] = setsuf(clist[i], 'o');
stefan
1.73
587                 av[na++] = assource;
ragge
1.49
588                 if (dflag)
589                         av[na++] = alist;
590                 av[na++] = 0;
gmcgarry
1.79
591                 if (callsys(asav)) {
ragge
1.1
592                         cflag++;
593                         eflag++;
ragge
1.7
594                         cunlink(tmp4);
ragge
1.1
595                         continue;
596                 }
ragge
1.7
597                 cunlink(tmp4);
ragge
1.1
598         }
ragge
1.18
599
ragge
1.51
600         if (Eflag || Mflag)
ragge
1.37
601                 dexit(eflag);
602
ragge
1.18
603         /*
604          * Linker
605          */
ragge
1.1
606 nocom:
607         if (cflag==0 && nl!=0) {
ragge
1.6
608                 j = 0;
gmcgarry
1.79
609                 av[j++] = ld;
gmcgarry
1.95
610 #ifndef os_win32
ragge
1.60
611                 if (vflag)
612                         av[j++] = "-v";
ragge
1.6
613                 av[j++] = "-X";
ragge
1.84
614                 if (shared) {
615                         av[j++] = "-shared";
616                 } else {
617                         av[j++] = "-d";
618                         av[j++] = "-e";
619                         av[j++] = STARTLABEL;
620                         if (Bstatic == 0) { /* Dynamic linkage */
gmcgarry
1.94
621 #ifdef DYNLINKER
ragge
1.84
622                                 for (i = 0dynlinker[i]; i++)
623                                         av[j++] = dynlinker[i];
gmcgarry
1.94
624 #endif
ragge
1.84
625                         } else
626                                 av[j++] = "-Bstatic";
627                 }
gmcgarry
1.95
628 #endif
ragge
1.1
629                 if (outfile) {
ragge
1.3
630                         av[j++] = "-o";
631                         av[j++] = outfile;
632                 }
ragge
1.84
633                 if (shared) {
634 #ifdef STARTFILES_S
635                         for (i = 0startfiles_S[i]; i++)
636                                 av[j++] = startfiles_S[i];
637 #endif
638                 } else {
639                         if (!nostartfiles) {
gmcgarry
1.94
640 #ifdef CRT0FILE_PROFILE
641                                 if (pgflag)
642                                 {
643                                         av[j++] = crt0file_profile;
644                                 }
645                                 else
646 #endif
647                                 {
648 #ifdef CRT0FILE
649                                         av[j++] = crt0file;
650 #endif
651                                 }
652 #ifdef STARTFILES
ragge
1.84
653                                 for (i = 0startfiles[i]; i++)
654                                         av[j++] = startfiles[i];
gmcgarry
1.94
655 #endif
ragge
1.84
656                         }
ragge
1.14
657                 }
ragge
1.24
658                 i = 0;
ragge
1.14
659                 while(i<nl) {
ragge
1.3
660                         av[j++] = llist[i++];
ragge
1.14
661                         if (j >= MAXAV)
ragge
1.36
662                                 error("Too many ld options");
ragge
1.14
663                 }
gmcgarry
1.82
664 #ifndef MACHOABI
665                 /* darwin assembler doesn't want -g */
ragge
1.65
666                 if (gflag)
667                         av[j++] = "-g";
gmcgarry
1.82
668 #endif
ragge
1.11
669 #if 0
ragge
1.3
670                 if (gflag)
671                         av[j++] = "-lg";
ragge
1.11
672 #endif
ragge
1.52
673                 if (pthreads)
674                         av[j++] = "-lpthread";
gmcgarry
1.81
675                 if (!nostdlib) {
676                         if (pgflag) {
677                                 for (i = 0libclibs_profile[i]; i++)
678                                         av[j++] = libclibs_profile[i];
679                         } else {
680                                 for (i = 0libclibs[i]; i++)
681                                         av[j++] = libclibs[i];
682                         }
683                 }
ragge
1.84
684                 if (shared) {
685 #ifdef STARTFILES_S
686                         for (i = 0endfiles_S[i]; i++)
687                                 av[j++] = endfiles_S[i];
688 #endif
689                 } else {
gmcgarry
1.95
690 #ifdef STARTFILES
ragge
1.84
691                         if (!nostartfiles) {
692                                 for (i = 0endfiles[i]; i++)
693                                         av[j++] = endfiles[i];
694                         }
gmcgarry
1.95
695 #endif
ragge
1.14
696                 }
ragge
1.3
697                 av[j++] = 0;
gmcgarry
1.79
698                 eflag |= callsys(ldav);
ragge
1.1
699                 if (nc==1 && nxo==1 && eflag==0)
ragge
1.3
700                         cunlink(setsuf(clist[0], 'o'));
ragge
1.46
701                 else if (nc > 0 && eflag == 0) {
702                         /* remove .o files XXX ugly */
703                         for (i = 0i < nci++)
704                                 cunlink(setsuf(clist[i], 'o'));
705                 }
ragge
1.1
706         }
ragge
1.36
707         dexit(eflag);
ragge
1.4
708         return 0;
ragge
1.1
709 }
710
ragge
1.36
711 /*
712  * exit and cleanup after interrupt.
713  */
ragge
1.4
714 void
715 idexit(int arg)
ragge
1.1
716 {
ragge
1.36
717         dexit(100);
ragge
1.1
718 }
719
ragge
1.36
720 /*
721  * exit and cleanup.
722  */
ragge
1.4
723 void
ragge
1.36
724 dexit(int eval)
ragge
1.1
725 {
ragge
1.10
726         if (!pflag && !Xflag) {
ragge
1.1
727                 if (sflag==0)
728                         cunlink(tmp3);
729                 cunlink(tmp4);
730         }
ragge
1.70
731         if (eval == 100)
732                 _exit(eval);
ragge
1.36
733         exit(eval);
ragge
1.1
734 }
735
ragge
1.36
736 static void
737 ccerror(char *sva_list ap)
ragge
1.1
738 {
ragge
1.36
739         vfprintf(Eflag ? stderr : stdoutsap);
ragge
1.8
740         putc('\n'Eflagstderr : stdout);
ragge
1.1
741         exfail++;
742         cflag++;
743         eflag++;
744 }
745
ragge
1.36
746 /*
747  * complain a bit.
748  */
749 void
750 error(char *s, ...)
751 {
752         va_list ap;
753
754         va_start(aps);
755         ccerror(sap);
756         va_end(ap);
757 }
758
759 /*
760  * complain a bit and then exit.
761  */
ragge
1.18
762 void
ragge
1.36
763 errorx(int evalchar *s, ...)
ragge
1.18
764 {
ragge
1.36
765         va_list ap;
766
767         va_start(aps);
768         ccerror(sap);
769         va_end(ap);
770         dexit(eval);
ragge
1.18
771 }
ragge
1.3
772
ragge
1.4
773 int
stefan
1.72
774 getsuf(char *as)
ragge
1.1
775 {
776         register char *s;
777
ragge
1.4
778         if ((s = strrchr(as'.')) && s[1] != '\0' && s[2] == '\0')
779                 return s[1];
ragge
1.3
780         return(0);
ragge
1.1
781 }
782
ragge
1.36
783 /*
784  * Get basename of string s and change its suffix to ch.
785  */
ragge
1.1
786 char *
ragge
1.36
787 setsuf(char *schar ch)
ragge
1.1
788 {
ragge
1.91
789         char *as;
790
791         s = copy(basename(s), 2);
792         if ((as = strrchr(s'.')) == NULL) {
793                 as = s + strlen(s);
794                 as[0] = '.';
795         }
796         as[1] = ch;
797         as[2] = '\0';
ragge
1.36
798         return(s);
ragge
1.1
799 }
800
gmcgarry
1.89
801 #ifdef WIN32
ragge
1.4
802 int
gmcgarry
1.86
803 callsys(char *fchar *v[])
ragge
1.56
804 {
stefan
1.75
805         int tstatus = 0;
gmcgarry
1.89
806         char cmd[MAX_PATH];
gmcgarry
1.93
807         int len;
gmcgarry
1.86
808         STARTUPINFO si;
809         PROCESS_INFORMATION pi;
810         DWORD exitCode;
811         BOOL ok;
812
gmcgarry
1.89
813         len = strlcpy(cmdfMAX_PATH);
814         for (t = 1v[t] && len < MAX_PATHt++) {
815                 len = strlcat(cmd" "MAX_PATH);
816                 len = strlcat(cmdv[t], MAX_PATH);
817         }
818
819         if (vflag)
820                 printf("%s\n"cmd);
821
gmcgarry
1.86
822         ZeroMemory(&sisizeof(STARTUPINFO));
823         si.cb = sizeof(STARTUPINFO);
gmcgarry
1.89
824         ZeroMemory(&pisizeof(PROCESS_INFORMATION));
825
826         ok = CreateProcess(NULL,  // the executable program
827                 cmd,   // the command line arguments
gmcgarry
1.86
828                 NULL,       // ignored
829                 NULL,       // ignored
830                 TRUE,       // inherit handles
gmcgarry
1.89
831                 HIGH_PRIORITY_CLASS,
gmcgarry
1.86
832                 NULL,       // ignored
833                 NULL,       // ignored
834                 &si,
835                 &pi);
836
837         if (!ok) {
838                 printf("Try Again\n");
839                 return 100;
840         }
841
gmcgarry
1.89
842         WaitForSingleObject(pi.hProcessINFINITE);
gmcgarry
1.93
843         GetExitCodeProcess(pi.hProcess, &exitCode);
gmcgarry
1.86
844         return (exitCode != 0);
gmcgarry
1.89
845 }
846
847 #else
848
849 int
850 callsys(char *fchar *v[])
851 {
852         int tstatus = 0;
853         pid_t p;
854         char *s;
855
856         if (vflag) {
857                 fprintf(stderr"%s "f);
858                 for (t = 1v[t]; t++)
859                         fprintf(stderr"%s "v[t]);
860                 fprintf(stderr"\n");
gmcgarry
1.86
861         }
862
stefan
1.75
863         if ((p = fork()) == 0) {
ragge
1.47
864                 if (Bflag) {
ra