1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /*
28 * Wrapper for the GNU C compiler to make it accept the Sun C compiler
29 * arguments where possible.
30 *
31 * Since the translation is inexact, this is something of a work-in-progress.
32 *
33 */
34
35 /* If you modify this file, you must increment CW_VERSION */
36 #define CW_VERSION "1.29"
37
38 /*
39 * -# Verbose mode
40 * -### Show compiler commands built by driver, no compilation
41 * -A<name[(tokens)]> Preprocessor predicate assertion
42 * -B<[static|dynamic]> Specify dynamic or static binding
43 * -C Prevent preprocessor from removing comments
44 * -c Compile only - produce .o files, suppress linking
45 * -cg92 Alias for -xtarget=ss1000
46 * -D<name[=token]> Associate name with token as if by #define
47 * -d[y|n] dynamic [-dy] or static [-dn] option to linker
48 * -E Compile source through preprocessor only, output to stdout
49 * -erroff=<t> Suppress warnings specified by tags t(%none, %all, <tag list>)
50 * -errtags=<a> Display messages with tags a(no, yes)
51 * -errwarn=<t> Treats warnings specified by tags t(%none, %all, <tag list>)
52 * as errors
53 * -fast Optimize using a selection of options
54 * -fd Report old-style function definitions and declarations
55 * -features=zla Allow zero-length arrays
56 * -flags Show this summary of compiler options
57 * -fnonstd Initialize floating-point hardware to non-standard preferences
58 * -fns[=<yes|no>] Select non-standard floating point mode
59 * -fprecision=<p> Set FP rounding precision mode p(single, double, extended)
60 * -fround=<r> Select the IEEE rounding mode in effect at startup
61 * -fsimple[=<n>] Select floating-point optimization preferences <n>
62 * -fsingle Use single-precision arithmetic (-Xt and -Xs modes only)
63 * -ftrap=<t> Select floating-point trapping mode in effect at startup
64 * -fstore force floating pt. values to target precision on assignment
65 * -G Build a dynamic shared library
66 * -g Compile for debugging
67 * -H Print path name of each file included during compilation
68 * -h <name> Assign <name> to generated dynamic shared library
69 * -I<dir> Add <dir> to preprocessor #include file search path
70 * -i Passed to linker to ignore any LD_LIBRARY_PATH setting
71 * -keeptmp Keep temporary files created during compilation
72 * -KPIC Compile position independent code with 32-bit addresses
73 * -Kpic Compile position independent code
74 * -L<dir> Pass to linker to add <dir> to the library search path
75 * -l<name> Link with library lib<name>.a or lib<name>.so
76 * -mc Remove duplicate strings from .comment section of output files
77 * -mr Remove all strings from .comment section of output files
78 * -mr,"string" Remove all strings and append "string" to .comment section
79 * -mt Specify options needed when compiling multi-threaded code
80 * -native Find available processor, generate code accordingly
81 * -nofstore Do not force floating pt. values to target precision
82 * on assignment
83 * -nolib Same as -xnolib
84 * -noqueue Disable queuing of compiler license requests
85 * -norunpath Do not build in a runtime path for shared libraries
86 * -O Use default optimization level (-xO2 or -xO3. Check man page.)
87 * -o <outputfile> Set name of output file to <outputfile>
88 * -P Compile source through preprocessor only, output to .i file
89 * -PIC Alias for -KPIC or -xcode=pic32
90 * -p Compile for profiling with prof
91 * -pic Alias for -Kpic or -xcode=pic13
92 * -Q[y|n] Emit/don't emit identification info to output file
93 * -qp Compile for profiling with prof
94 * -R<dir[:dir]> Build runtime search path list into executable
95 * -S Compile and only generate assembly code (.s)
96 * -s Strip symbol table from the executable file
97 * -t Turn off duplicate symbol warnings when linking
98 * -U<name> Delete initial definition of preprocessor symbol <name>
99 * -V Report version number of each compilation phase
100 * -v Do stricter semantic checking
101 * -W<c>,<arg> Pass <arg> to specified component <c> (a,l,m,p,0,2,h,i,u)
102 * -w Suppress compiler warning messages
103 * -Xa Compile assuming ANSI C conformance, allow K & R extensions
104 * (default mode)
105 * -Xc Compile assuming strict ANSI C conformance
106 * -Xs Compile assuming (pre-ANSI) K & R C style code
107 * -Xt Compile assuming K & R conformance, allow ANSI C
108 * -x386 Generate code for the 80386 processor
109 * -x486 Generate code for the 80486 processor
110 * -xarch=<a> Specify target architecture instruction set
111 * -xbuiltin[=<b>] When profitable inline, or substitute intrinisic functions
112 * for system functions, b={%all,%none}
113 * -xCC Accept C++ style comments
114 * -xchar_byte_order=<o> Specify multi-char byte order <o> (default, high, low)
115 * -xchip=<c> Specify the target processor for use by the optimizer
116 * -xcode=<c> Generate different code for forming addresses
117 * -xcrossfile[=<n>] Enable optimization and inlining across source files,
118 * n={0|1}
119 * -xe Perform only syntax/semantic checking, no code generation
120 * -xF Compile for later mapfile reordering or unused section
121 * elimination
122 * -xhelp=<f> Display on-line help information f(flags, readme, errors)
123 * -xildoff Cancel -xildon
124 * -xildon Enable use of the incremental linker, ild
125 * -xinline=[<a>,...,<a>] Attempt inlining of specified user routines,
126 * <a>={%auto,func,no%func}
127 * -xlibmieee Force IEEE 754 return values for math routines in
128 * exceptional cases
129 * -xlibmil Inline selected libm math routines for optimization
130 * -xlic_lib=sunperf Link in the Sun supplied performance libraries
131 * -xlicinfo Show license server information
132 * -xM Generate makefile dependencies
133 * -xM1 Generate makefile dependencies, but exclude /usr/include
134 * -xmaxopt=[off,1,2,3,4,5] maximum optimization level allowed on #pragma opt
135 * -xnolib Do not link with default system libraries
136 * -xnolibmil Cancel -xlibmil on command line
137 * -xO<n> Generate optimized code (n={1|2|3|4|5})
138 * -xP Print prototypes for function definitions
139 * -xpentium Generate code for the pentium processor
140 * -xpg Compile for profiling with gprof
141 * -xprofile=<p> Collect data for a profile or use a profile to optimize
142 * <p>={{collect,use}[:<path>],tcov}
143 * -xregs=<r> Control register allocation
144 * -xs Allow debugging without object (.o) files
145 * -xsb Compile for use with the WorkShop source browser
146 * -xsbfast Generate only WorkShop source browser info, no compilation
147 * -xsfpconst Represent unsuffixed floating point constants as single
148 * precision
149 * -xspace Do not do optimizations that increase code size
150 * -xstrconst Place string literals into read-only data segment
151 * -xtarget=<t> Specify target system for optimization
152 * -xtemp=<dir> Set directory for temporary files to <dir>
153 * -xtime Report the execution time for each compilation phase
154 * -xtransition Emit warnings for differences between K&R C and ANSI C
155 * -xtrigraphs[=<yes|no>] Enable|disable trigraph translation
156 * -xunroll=n Enable unrolling loops n times where possible
157 * -Y<c>,<dir> Specify <dir> for location of component <c> (a,l,m,p,0,h,i,u)
158 * -YA,<dir> Change default directory searched for components
159 * -YI,<dir> Change default directory searched for include files
160 * -YP,<dir> Change default directory for finding libraries files
161 * -YS,<dir> Change default directory for startup object files
162 */
163
164 /*
165 * Translation table:
166 */
167 /*
168 * -# -v
169 * -### error
170 * -A<name[(tokens)]> pass-thru
171 * -B<[static|dynamic]> pass-thru (syntax error for anything else)
172 * -C pass-thru
173 * -c pass-thru
174 * -cg92 -m32 -mcpu=v8 -mtune=supersparc (SPARC only)
175 * -D<name[=token]> pass-thru
176 * -dy or -dn -Wl,-dy or -Wl,-dn
177 * -E pass-thru
178 * -erroff=E_EMPTY_TRANSLATION_UNIT ignore
179 * -errtags=%all -Wall
180 * -errwarn=%all -Werror else -Wno-error
181 * -fast error
182 * -fd error
183 * -features=zla ignore
184 * -flags --help
185 * -fnonstd error
186 * -fns[=<yes|no>] error
187 * -fprecision=<p> error
188 * -fround=<r> error
189 * -fsimple[=<n>] error
190 * -fsingle[=<n>] error
191 * -ftrap=<t> error
192 * -fstore error
193 * -G pass-thru
194 * -g pass-thru
195 * -H pass-thru
196 * -h <name> pass-thru
197 * -I<dir> pass-thru
198 * -i pass-thru
199 * -keeptmp -save-temps
200 * -KPIC -fPIC
201 * -Kpic -fpic
202 * -L<dir> pass-thru
203 * -l<name> pass-thru
204 * -mc error
205 * -mr error
206 * -mr,"string" error
207 * -mt -D_REENTRANT
208 * -native error
209 * -nofstore error
210 * -nolib -nodefaultlibs
211 * -noqueue ignore
212 * -norunpath ignore
213 * -O -O1 (Check the man page to be certain)
214 * -o <outputfile> pass-thru
215 * -P -E -o filename.i (or error)
216 * -PIC -fPIC (C++ only)
217 * -p pass-thru
218 * -pic -fpic (C++ only)
219 * -Q[y|n] error
220 * -qp -p
221 * -R<dir[:dir]> pass-thru
222 * -S pass-thru
223 * -s -Wl,-s
224 * -t -Wl,-t
225 * -U<name> pass-thru
226 * -V --version
227 * -v -Wall
228 * -Wa,<arg> pass-thru
229 * -Wp,<arg> pass-thru except -xc99=<a>
230 * -Wl,<arg> pass-thru
231 * -W{m,0,2,h,i,u> error/ignore
232 * -Wu,-xmodel=kernel -ffreestanding -mcmodel=kernel -mno-red-zone
233 * -xmodel=kernel -ffreestanding -mcmodel=kernel -mno-red-zone
234 * -Wu,-save_args -msave-args
235 * -w pass-thru
236 * -Xa -std=iso9899:199409 or -ansi
237 * -Xc -ansi -pedantic
238 * -Xt error
239 * -Xs -traditional -std=c89
240 * -x386 -march=i386 (x86 only)
241 * -x486 -march=i486 (x86 only)
242 * -xarch=<a> table
243 * -xbuiltin[=<b>] -fbuiltin (-fno-builtin otherwise)
244 * -xCC ignore
245 * -xchar_byte_order=<o> error
246 * -xchip=<c> table
247 * -xcode=<c> table
248 * -xdebugformat=<format> ignore (always use dwarf-2 for gcc)
249 * -xcrossfile[=<n>] ignore
250 * -xe error
251 * -xF error
252 * -xhelp=<f> error
253 * -xildoff ignore
254 * -xildon ignore
255 * -xinline ignore
256 * -xlibmieee error
257 * -xlibmil error
258 * -xlic_lib=sunperf error
259 * -xM -M
260 * -xM1 -MM
261 * -xmaxopt=[...] error
262 * -xnolib -nodefaultlibs
263 * -xnolibmil error
264 * -xO<n> -O<n>
265 * -xP error
266 * -xpentium -march=pentium (x86 only)
267 * -xpg error
268 * -xprofile=<p> error
269 * -xregs=<r> table
270 * -xs error
271 * -xsb error
272 * -xsbfast error
273 * -xsfpconst error
274 * -xspace ignore (-not -Os)
275 * -xstrconst ignore
276 * -xtarget=<t> table
277 * -xtemp=<dir> error
278 * -xtime error
279 * -xtransition -Wtransition
280 * -xtrigraphs=<yes|no> -trigraphs -notrigraphs
281 * -xunroll=n error
282 * -W0,-xdbggen=no%usedonly -fno-eliminate-unused-debug-symbols
283 * -fno-eliminate-unused-debug-types
284 * -Y<c>,<dir> error
285 * -YA,<dir> error
286 * -YI,<dir> -nostdinc -I<dir>
287 * -YP,<dir> error
288 * -YS,<dir> error
289 */
290
291 #include <stdio.h>
292 #include <sys/types.h>
293 #include <unistd.h>
294 #include <string.h>
295 #include <stdlib.h>
296 #include <ctype.h>
297 #include <fcntl.h>
298 #include <errno.h>
299 #include <stdarg.h>
300 #include <sys/utsname.h>
301 #include <sys/param.h>
302 #include <sys/isa_defs.h>
303 #include <sys/wait.h>
304 #include <sys/stat.h>
305
306 #define CW_F_CXX 0x01
307 #define CW_F_SHADOW 0x02
308 #define CW_F_EXEC 0x04
309 #define CW_F_ECHO 0x08
310 #define CW_F_XLATE 0x10
311 #define CW_F_PROG 0x20
312
313 typedef enum cw_compiler {
314 CW_C_CC = 0,
315 CW_C_GCC
316 } cw_compiler_t;
317
318 static const char *cmds[] = {
319 "cc", "CC",
320 "gcc", "g++"
321 };
322
323 static char default_dir[2][MAXPATHLEN] = {
324 DEFAULT_CC_DIR,
325 DEFAULT_GCC_DIR,
326 };
327
328 #define CC(ctx) \
329 (((ctx)->i_flags & CW_F_SHADOW) ? \
330 ((ctx)->i_compiler == CW_C_CC ? CW_C_GCC : CW_C_CC) : \
331 (ctx)->i_compiler)
332
333 #define CIDX(compiler, flags) \
334 ((int)(compiler) << 1) + ((flags) & CW_F_CXX ? 1 : 0)
335
336 typedef enum cw_op {
337 CW_O_NONE = 0,
338 CW_O_PREPROCESS,
339 CW_O_COMPILE,
340 CW_O_LINK
341 } cw_op_t;
342
343 struct aelist {
344 struct ae {
345 struct ae *ae_next;
346 char *ae_arg;
347 } *ael_head, *ael_tail;
348 int ael_argc;
349 };
350
351 typedef struct cw_ictx {
352 cw_compiler_t i_compiler;
353 struct aelist *i_ae;
354 uint32_t i_flags;
355 int i_oldargc;
356 char **i_oldargv;
357 pid_t i_pid;
358 char i_discard[MAXPATHLEN];
359 char *i_stderr;
360 } cw_ictx_t;
361
362 /*
363 * Status values to indicate which Studio compiler and associated
364 * flags are being used.
365 */
366 #define M32 0x01 /* -m32 - only on Studio 12 */
367 #define M64 0x02 /* -m64 - only on Studio 12 */
368 #define SS11 0x100 /* Studio 11 */
369 #define SS12 0x200 /* Studio 12 */
370
371 #define TRANS_ENTRY 5
372 /*
373 * Translation table definition for the -xarch= flag. The "x_arg"
374 * value is translated into the appropriate gcc flags according
375 * to the values in x_trans[n]. The x_flags indicates what compiler
376 * is being used and what flags have been set via the use of
377 * "x_arg".
378 */
379 typedef struct xarch_table {
380 char *x_arg;
381 int x_flags;
382 char *x_trans[TRANS_ENTRY];
383 } xarch_table_t;
384
385 /*
386 * The translation table for the -xarch= flag used in the Studio compilers.
387 */
388 static const xarch_table_t xtbl[] = {
389 #if defined(__x86)
390 { "generic", SS11 },
391 { "generic64", (SS11|M64), { "-m64", "-mtune=opteron" } },
392 { "amd64", (SS11|M64), { "-m64", "-mtune=opteron" } },
393 { "386", SS11, { "-march=i386" } },
394 { "pentium_pro", SS11, { "-march=pentiumpro" } },
395 { "sse", SS11, { "-msse", "-mfpmath=sse" } },
396 { "sse2", SS11, { "-msse2", "-mfpmath=sse" } },
397 #elif defined(__sparc)
398 { "generic", (SS11|M32), { "-m32", "-mcpu=v8" } },
399 { "generic64", (SS11|M64), { "-m64", "-mcpu=v9" } },
400 { "v8", (SS11|M32), { "-m32", "-mcpu=v8", "-mno-v8plus" } },
401 { "v8plus", (SS11|M32), { "-m32", "-mcpu=v9", "-mv8plus" } },
402 { "v8plusa", (SS11|M32), { "-m32", "-mcpu=ultrasparc", "-mv8plus",
403 "-mvis" } },
404 { "v8plusb", (SS11|M32), { "-m32", "-mcpu=ultrasparc3", "-mv8plus",
405 "-mvis" } },
406 { "v9", (SS11|M64), { "-m64", "-mcpu=v9" } },
407 { "v9a", (SS11|M64), { "-m64", "-mcpu=ultrasparc", "-mvis" } },
408 { "v9b", (SS11|M64), { "-m64", "-mcpu=ultrasparc3", "-mvis" } },
409 { "sparc", SS12, { "-mcpu=v9", "-mv8plus" } },
410 { "sparcvis", SS12, { "-mcpu=ultrasparc", "-mvis" } },
411 { "sparcvis2", SS12, { "-mcpu=ultrasparc3", "-mvis" } }
412 #endif
413 };
414
415 static int xtbl_size = sizeof (xtbl) / sizeof (xarch_table_t);
416
417 static const char *progname;
418
419 static const char *xchip_tbl[] = {
420 #if defined(__x86)
421 "386", "-mtune=i386", NULL,
422 "486", "-mtune=i486", NULL,
423 "pentium", "-mtune=pentium", NULL,
424 "pentium_pro", "-mtune=pentiumpro", NULL,
425 #elif defined(__sparc)
426 "super", "-mtune=supersparc", NULL,
427 "ultra", "-mtune=ultrasparc", NULL,
428 "ultra3", "-mtune=ultrasparc3", NULL,
429 #endif
430 NULL, NULL
431 };
432
433 static const char *xcode_tbl[] = {
434 #if defined(__sparc)
435 "abs32", "-fno-pic", "-mcmodel=medlow", NULL,
436 "abs44", "-fno-pic", "-mcmodel=medmid", NULL,
437 "abs64", "-fno-pic", "-mcmodel=medany", NULL,
438 "pic13", "-fpic", NULL,
439 "pic32", "-fPIC", NULL,
440 #endif
441 NULL, NULL
442 };
443
444 static const char *xtarget_tbl[] = {
445 #if defined(__x86)
446 "pentium_pro", "-march=pentiumpro", NULL,
447 #endif /* __x86 */
448 NULL, NULL
449 };
450
451 static const char *xregs_tbl[] = {
452 #if defined(__sparc)
453 "appl", "-mapp-regs", NULL,
454 "no%appl", "-mno-app-regs", NULL,
455 "float", "-mfpu", NULL,
456 "no%float", "-mno-fpu", NULL,
457 #endif /* __sparc */
458 NULL, NULL
459 };
460
461 static void
462 nomem(void)
463 {
464 (void) fprintf(stderr, "%s: error: out of memory\n", progname);
465 exit(1);
466 }
467
468 static void
469 cw_perror(const char *fmt, ...)
470 {
471 va_list ap;
472 int saved_errno = errno;
473
474 (void) fprintf(stderr, "%s: error: ", progname);
475
476 va_start(ap, fmt);
477 (void) vfprintf(stderr, fmt, ap);
478 va_end(ap);
479
480 (void) fprintf(stderr, " (%s)\n", strerror(saved_errno));
481 }
482
483 static void
484 newae(struct aelist *ael, const char *arg)
485 {
486 struct ae *ae;
487
488 if ((ae = calloc(sizeof (*ae), 1)) == NULL)
489 nomem();
490 ae->ae_arg = strdup(arg);
491 if (ael->ael_tail == NULL)
492 ael->ael_head = ae;
493 else
494 ael->ael_tail->ae_next = ae;
495 ael->ael_tail = ae;
496 ael->ael_argc++;
497 }
498
499 static cw_ictx_t *
500 newictx(void)
501 {
502 cw_ictx_t *ctx = calloc(sizeof (cw_ictx_t), 1);
503 if (ctx)
504 if ((ctx->i_ae = calloc(sizeof (struct aelist), 1)) == NULL) {
505 free(ctx);
506 return (NULL);
507 }
508
509 return (ctx);
510 }
511
512 static void
513 error(const char *arg)
514 {
515 (void) fprintf(stderr,
516 "%s: error: mapping failed at or near arg '%s'\n", progname, arg);
517 exit(2);
518 }
519
520 /*
521 * Add the current favourite set of warnings to the gcc invocation.
522 */
523 static void
524 warnings(struct aelist *h)
525 {
526 static int warningsonce;
527
528 if (warningsonce++)
529 return;
530
531 /*
532 * Enable as many warnings as exist, then disable those that we never
533 * ever want.
534 */
535 newae(h, "-Wall");
536 newae(h, "-Wextra");
537 }
538
539 static void
540 optim_disable(struct aelist *h, int level)
541 {
542 if (level >= 2) {
543 newae(h, "-fno-strict-aliasing");
544 newae(h, "-fno-unit-at-a-time");
545 newae(h, "-fno-optimize-sibling-calls");
546 }
547 }
548
549 /* ARGSUSED */
550 static void
551 Xamode(struct aelist *h)
552 {
553 }
554
555 static void
556 Xcmode(struct aelist *h)
557 {
558 static int xconce;
559
560 if (xconce++)
561 return;
562
563 newae(h, "-ansi");
564 newae(h, "-pedantic-errors");
565 }
566
567 static void
568 Xsmode(struct aelist *h)
569 {
570 static int xsonce;
571
572 if (xsonce++)
573 return;
574
575 newae(h, "-traditional");
576 newae(h, "-traditional-cpp");
577 }
578
579 static void
580 usage()
581 {
582 (void) fprintf(stderr,
583 "usage: %s { -_cc | -_gcc | -_CC | -_g++ } [ -_compiler | ... ]\n",
584 progname);
585 exit(2);
586 }
587
588 static int
589 xlate_xtb(struct aelist *h, const char *xarg)
590 {
591 int i, j;
592
593 for (i = 0; i < xtbl_size; i++) {
594 if (strcmp(xtbl[i].x_arg, xarg) == 0)
595 break;
596 }
597
598 /*
599 * At the end of the table and so no matching "arg" entry
600 * found and so this must be a bad -xarch= flag.
601 */
602 if (i == xtbl_size)
603 error(xarg);
604
605 for (j = 0; j < TRANS_ENTRY; j++) {
606 if (xtbl[i].x_trans[j] != NULL)
607 newae(h, xtbl[i].x_trans[j]);
608 else
609 break;
610 }
611 return (xtbl[i].x_flags);
612
613 }
614
615 static void
616 xlate(struct aelist *h, const char *xarg, const char **table)
617 {
618 while (*table != NULL && strcmp(xarg, *table) != 0) {
619 while (*table != NULL)
620 table++;
621 table++;
622 }
623
624 if (*table == NULL)
625 error(xarg);
626
627 table++;
628
629 while (*table != NULL) {
630 newae(h, *table);
631 table++;
632 }
633 }
634
635 static void
636 do_gcc(cw_ictx_t *ctx)
637 {
638 int c;
639 int pic = 0, nolibc = 0;
640 int in_output = 0, seen_o = 0, c_files = 0;
641 cw_op_t op = CW_O_LINK;
642 char *model = NULL;
643 int mflag = 0;
644
645 if (ctx->i_flags & CW_F_PROG) {
646 newae(ctx->i_ae, "--version");
647 return;
648 }
649
650 newae(ctx->i_ae, "-fident");
651 newae(ctx->i_ae, "-finline");
652 newae(ctx->i_ae, "-fno-inline-functions");
653 newae(ctx->i_ae, "-fno-builtin");
654 newae(ctx->i_ae, "-fno-asm");
655 newae(ctx->i_ae, "-fdiagnostics-show-option");
656 newae(ctx->i_ae, "-nodefaultlibs");
657
658 #if defined(__sparc)
659 /*
660 * The SPARC ldd and std instructions require 8-byte alignment of
661 * their address operand. gcc correctly uses them only when the
662 * ABI requires 8-byte alignment; unfortunately we have a number of
663 * pieces of buggy code that doesn't conform to the ABI. This
664 * flag makes gcc work more like Studio with -xmemalign=4.
665 */
666 newae(ctx->i_ae, "-mno-integer-ldd-std");
667 #endif
668
669 /*
670 * This is needed because 'u' is defined
671 * under a conditional on 'sun'. Should
672 * probably just remove the conditional,
673 * or make it be dependent on '__sun'.
674 *
675 * -Dunix is also missing in enhanced ANSI mode
676 */
677 newae(ctx->i_ae, "-D__sun");
678
679 /*
680 * Walk the argument list, translating as we go ..
681 */
682
683 while (--ctx->i_oldargc > 0) {
684 char *arg = *++ctx->i_oldargv;
685 size_t arglen = strlen(arg);
686
687 if (*arg == '-') {
688 arglen--;
689 } else {
690 /*
691 * Discard inline files that gcc doesn't grok
692 */
693 if (!in_output && arglen > 3 &&
694 strcmp(arg + arglen - 3, ".il") == 0)
695 continue;
696
697 if (!in_output && arglen > 2 &&
698 arg[arglen - 2] == '.' &&
699 (arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' ||
700 arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i'))
701 c_files++;
702
703 /*
704 * Otherwise, filenames and partial arguments
705 * are passed through for gcc to chew on. However,
706 * output is always discarded for the secondary
707 * compiler.
708 */
709 if ((ctx->i_flags & CW_F_SHADOW) && in_output)
710 newae(ctx->i_ae, ctx->i_discard);
711 else
712 newae(ctx->i_ae, arg);
713 in_output = 0;
714 continue;
715 }
716
717 if (ctx->i_flags & CW_F_CXX) {
718 if (strncmp(arg, "-compat=", 8) == 0) {
719 /* discard -compat=4 and -compat=5 */
720 continue;
721 }
722 if (strcmp(arg, "-Qoption") == 0) {
723 /* discard -Qoption and its two arguments */
724 if (ctx->i_oldargc < 3)
725 error(arg);
726 ctx->i_oldargc -= 2;
727 ctx->i_oldargv += 2;
728 continue;
729 }
730 if (strcmp(arg, "-xwe") == 0) {
731 /* turn warnings into errors */
732 newae(ctx->i_ae, "-Werror");
733 continue;
734 }
735 if (strcmp(arg, "-noex") == 0) {
736 /* no exceptions */
737 newae(ctx->i_ae, "-fno-exceptions");
738 /* no run time type descriptor information */
739 newae(ctx->i_ae, "-fno-rtti");
740 continue;
741 }
742 if (strcmp(arg, "-pic") == 0) {
743 newae(ctx->i_ae, "-fpic");
744 pic = 1;
745 continue;
746 }
747 if (strcmp(arg, "-PIC") == 0) {
748 newae(ctx->i_ae, "-fPIC");
749 pic = 1;
750 continue;
751 }
752 if (strcmp(arg, "-norunpath") == 0) {
753 /* gcc has no corresponding option */
754 continue;
755 }
756 if (strcmp(arg, "-nolib") == 0) {
757 /* -nodefaultlibs is on by default */
758 nolibc = 1;
759 continue;
760 }
761 #if defined(__sparc)
762 if (strcmp(arg, "-cg92") == 0) {
763 mflag |= xlate_xtb(ctx->i_ae, "v8");
764 xlate(ctx->i_ae, "super", xchip_tbl);
765 continue;
766 }
767 #endif /* __sparc */
768 }
769
770 switch ((c = arg[1])) {
771 case '_':
772 if (strcmp(arg, "-_noecho") == 0)
773 ctx->i_flags &= ~CW_F_ECHO;
774 else if (strncmp(arg, "-_cc=", 5) == 0 ||
775 strncmp(arg, "-_CC=", 5) == 0)
776 /* EMPTY */;
777 else if (strncmp(arg, "-_gcc=", 6) == 0 ||
778 strncmp(arg, "-_g++=", 6) == 0)
779 newae(ctx->i_ae, arg + 6);
780 else
781 error(arg);
782 break;
783 case '#':
784 if (arglen == 1) {
785 newae(ctx->i_ae, "-v");
786 break;
787 }
788 error(arg);
789 break;
790 case 'g':
791 newae(ctx->i_ae, "-gdwarf-2");
792 break;
793 case 'E':
794 if (arglen == 1) {
795 newae(ctx->i_ae, "-xc");
796 newae(ctx->i_ae, arg);
797 op = CW_O_PREPROCESS;
798 nolibc = 1;
799 break;
800 }
801 error(arg);
802 break;
803 case 'c':
804 case 'S':
805 if (arglen == 1) {
806 op = CW_O_COMPILE;
807 nolibc = 1;
808 }
809 /* FALLTHROUGH */
810 case 'C':
811 case 'H':
812 case 'p':
813 if (arglen == 1) {
814 newae(ctx->i_ae, arg);
815 break;
816 }
817 error(arg);
818 break;
819 case 'A':
820 case 'h':
821 case 'I':
822 case 'i':
823 case 'L':
824 case 'l':
825 case 'R':
826 case 'U':
827 case 'u':
828 case 'w':
829 newae(ctx->i_ae, arg);
830 break;
831 case 'o':
832 seen_o = 1;
833 if (arglen == 1) {
834 in_output = 1;
835 newae(ctx->i_ae, arg);
836 } else if (ctx->i_flags & CW_F_SHADOW) {
837 newae(ctx->i_ae, "-o");
838 newae(ctx->i_ae, ctx->i_discard);
839 } else {
840 newae(ctx->i_ae, arg);
841 }
842 break;
843 case 'D':
844 newae(ctx->i_ae, arg);
845 /*
846 * XXX Clearly a hack ... do we need _KADB too?
847 */
848 if (strcmp(arg, "-D_KERNEL") == 0 ||
849 strcmp(arg, "-D_BOOT") == 0)
850 newae(ctx->i_ae, "-ffreestanding");
851 break;
852 case 'd':
853 if (arglen == 2) {
854 if (strcmp(arg, "-dy") == 0) {
855 newae(ctx->i_ae, "-Wl,-dy");
856 break;
857 }
858 if (strcmp(arg, "-dn") == 0) {
859 newae(ctx->i_ae, "-Wl,-dn");
860 break;
861 }
862 }
863 if (strcmp(arg, "-dalign") == 0) {
864 /*
865 * -dalign forces alignment in some cases;
866 * gcc does not need any flag to do this.
867 */
868 break;
869 }
870 error(arg);
871 break;
872 case 'e':
873 if (strcmp(arg,
874 "-erroff=E_EMPTY_TRANSLATION_UNIT") == 0) {
875 /*
876 * Accept but ignore this -- gcc doesn't
877 * seem to complain about empty translation
878 * units
879 */
880 break;
881 }
882 /* XX64 -- ignore all -erroff= options, for now */
883 if (strncmp(arg, "-erroff=", 8) == 0)
884 break;
885 if (strcmp(arg, "-errtags=yes") == 0) {
886 warnings(ctx->i_ae);
887 break;
888 }
889 if (strcmp(arg, "-errwarn=%all") == 0) {
890 newae(ctx->i_ae, "-Werror");
891 break;
892 }
893 error(arg);
894 break;
895 case 'f':
896 if (strcmp(arg, "-flags") == 0) {
897 newae(ctx->i_ae, "--help");
898 break;
899 }
900 if (strncmp(arg, "-features=zla", 13) == 0) {
901 /*
902 * Accept but ignore this -- gcc allows
903 * zero length arrays.
904 */
905 break;
906 }
907 error(arg);
908 break;
909 case 'G':
910 newae(ctx->i_ae, "-shared");
911 nolibc = 1;
912 break;
913 case 'k':
914 if (strcmp(arg, "-keeptmp") == 0) {
915 newae(ctx->i_ae, "-save-temps");
916 break;
917 }
918 error(arg);
919 break;
920 case 'K':
921 if (arglen == 1) {
922 if ((arg = *++ctx->i_oldargv) == NULL ||
923 *arg == '\0')
924 error("-K");
925 ctx->i_oldargc--;
926 } else {
927 arg += 2;
928 }
929 if (strcmp(arg, "pic") == 0) {
930 newae(ctx->i_ae, "-fpic");
931 pic = 1;
932 break;
933 }
934 if (strcmp(arg, "PIC") == 0) {
935 newae(ctx->i_ae, "-fPIC");
936 pic = 1;
937 break;
938 }
939 error("-K");
940 break;
941 case 'm':
942 if (strcmp(arg, "-mt") == 0) {
943 newae(ctx->i_ae, "-D_REENTRANT");
944 break;
945 }
946 if (strcmp(arg, "-m64") == 0) {
947 newae(ctx->i_ae, "-m64");
948 #if defined(__x86)
949 newae(ctx->i_ae, "-mtune=opteron");
950 #endif
951 mflag |= M64;
952 break;
953 }
954 if (strcmp(arg, "-m32") == 0) {
955 newae(ctx->i_ae, "-m32");
956 mflag |= M32;
957 break;
958 }
959 error(arg);
960 break;
961 case 'B': /* linker options */
962 case 'M':
963 case 'z':
964 {
965 char *opt;
966 size_t len;
967 char *s;
968
969 if (arglen == 1) {
970 opt = *++ctx->i_oldargv;
971 if (opt == NULL || *opt == '\0')
972 error(arg);
973 ctx->i_oldargc--;
974 } else {
975 opt = arg + 2;
976 }
977 len = strlen(opt) + 7;
978 if ((s = malloc(len)) == NULL)
979 nomem();
980 (void) snprintf(s, len, "-Wl,-%c%s", c, opt);
981 newae(ctx->i_ae, s);
982 free(s);
983 }
984 break;
985 case 'n':
986 if (strcmp(arg, "-noqueue") == 0) {
987 /*
988 * Horrid license server stuff - n/a
989 */
990 break;
991 }
992 error(arg);
993 break;
994 case 'O':
995 if (arglen == 1) {
996 newae(ctx->i_ae, "-O");
997 break;
998 }
999 error(arg);
1000 break;
1001 case 'P':
1002 /*
1003 * We could do '-E -o filename.i', but that's hard,
1004 * and we don't need it for the case that's triggering
1005 * this addition. We'll require the user to specify
1006 * -o in the Makefile. If they don't they'll find out
1007 * in a hurry.
1008 */
1009 newae(ctx->i_ae, "-E");
1010 op = CW_O_PREPROCESS;
1011 nolibc = 1;
1012 break;
1013 case 'q':
1014 if (strcmp(arg, "-qp") == 0) {
1015 newae(ctx->i_ae, "-p");
1016 break;
1017 }
1018 error(arg);
1019 break;
1020 case 's':
1021 if (arglen == 1) {
1022 newae(ctx->i_ae, "-Wl,-s");
1023 break;
1024 }
1025 error(arg);
1026 break;
1027 case 't':
1028 if (arglen == 1) {
1029 newae(ctx->i_ae, "-Wl,-t");
1030 break;
1031 }
1032 error(arg);
1033 break;
1034 case 'V':
1035 if (arglen == 1) {
1036 ctx->i_flags &= ~CW_F_ECHO;
1037 newae(ctx->i_ae, "--version");
1038 break;
1039 }
1040 error(arg);
1041 break;
1042 case 'v':
1043 if (arglen == 1) {
1044 warnings(ctx->i_ae);
1045 break;
1046 }
1047 error(arg);
1048 break;
1049 case 'W':
1050 if (strncmp(arg, "-Wp,-xc99", 9) == 0) {
1051 /*
1052 * gcc's preprocessor will accept c99
1053 * regardless, so accept and ignore.
1054 */
1055 break;
1056 }
1057 if (strncmp(arg, "-Wa,", 4) == 0 ||
1058 strncmp(arg, "-Wp,", 4) == 0 ||
1059 strncmp(arg, "-Wl,", 4) == 0) {
1060 newae(ctx->i_ae, arg);
1061 break;
1062 }
1063 if (strcmp(arg, "-W0,-xc99=pragma") == 0) {
1064 /* (undocumented) enables _Pragma */
1065 break;
1066 }
1067 if (strcmp(arg, "-W0,-xc99=%none") == 0) {
1068 /*
1069 * This is a polite way of saying
1070 * "no c99 constructs allowed!"
1071 * For now, just accept and ignore this.
1072 */
1073 break;
1074 }
1075 if (strcmp(arg, "-W0,-noglobal") == 0 ||
1076 strcmp(arg, "-W0,-xglobalstatic") == 0) {
1077 /*
1078 * gcc doesn't prefix local symbols
1079 * in debug mode, so this is not needed.
1080 */
1081 break;
1082 }
1083 if (strcmp(arg, "-W0,-Lt") == 0) {
1084 /*
1085 * Generate tests at the top of loops.
1086 * There is no direct gcc equivalent, ignore.
1087 */
1088 break;
1089 }
1090 if (strcmp(arg, "-W0,-xdbggen=no%usedonly") == 0) {
1091 newae(ctx->i_ae,
1092 "-fno-eliminate-unused-debug-symbols");
1093 newae(ctx->i_ae,
1094 "-fno-eliminate-unused-debug-types");
1095 break;
1096 }
1097 if (strcmp(arg, "-W2,-xwrap_int") == 0) {
1098 /*
1099 * Use the legacy behaviour (pre-SS11)
1100 * for integer wrapping.
1101 * gcc does not need this.
1102 */
1103 break;
1104 }
1105 if (strcmp(arg, "-W2,-Rcond_elim") == 0) {
1106 /*
1107 * Elimination and expansion of conditionals;
1108 * gcc has no direct equivalent.
1109 */
1110 break;
1111 }
1112 if (strcmp(arg, "-Wd,-xsafe=unboundsym") == 0) {
1113 /*
1114 * Prevents optimizing away checks for
1115 * unbound weak symbol addresses. gcc does
1116 * not do this, so it's not needed.
1117 */
1118 break;
1119 }
1120 if (strncmp(arg, "-Wc,-xcode=", 11) == 0) {
1121 xlate(ctx->i_ae, arg + 11, xcode_tbl);
1122 if (strncmp(arg + 11, "pic", 3) == 0)
1123 pic = 1;
1124 break;
1125 }
1126 if (strncmp(arg, "-Wc,-Qiselect", 13) == 0) {
1127 /*
1128 * Prevents insertion of register symbols.
1129 * gcc doesn't do this, so ignore it.
1130 */
1131 break;
1132 }
1133 if (strcmp(arg, "-Wc,-Qassembler-ounrefsym=0") == 0) {
1134 /*
1135 * Prevents optimizing away of static variables.
1136 * gcc does not do this, so it's not needed.
1137 */
1138 break;
1139 }
1140 #if defined(__x86)
1141 if (strcmp(arg, "-Wu,-xmodel=kernel") == 0) {
1142 newae(ctx->i_ae, "-ffreestanding");
1143 newae(ctx->i_ae, "-mno-red-zone");
1144 model = "-mcmodel=kernel";
1145 nolibc = 1;
1146 break;
1147 }
1148 if (strcmp(arg, "-Wu,-save_args") == 0) {
1149 newae(ctx->i_ae, "-msave-args");
1150 break;
1151 }
1152 #endif /* __x86 */
1153 error(arg);
1154 break;
1155 case 'X':
1156 if (strcmp(arg, "-Xa") == 0 ||
1157 strcmp(arg, "-Xt") == 0) {
1158 Xamode(ctx->i_ae);
1159 break;
1160 }
1161 if (strcmp(arg, "-Xc") == 0) {
1162 Xcmode(ctx->i_ae);
1163 break;
1164 }
1165 if (strcmp(arg, "-Xs") == 0) {
1166 Xsmode(ctx->i_ae);
1167 break;
1168 }
1169 error(arg);
1170 break;
1171 case 'x':
1172 if (arglen == 1)
1173 error(arg);
1174 switch (arg[2]) {
1175 #if defined(__x86)
1176 case '3':
1177 if (strcmp(arg, "-x386") == 0) {
1178 newae(ctx->i_ae, "-march=i386");
1179 break;
1180 }
1181 error(arg);
1182 break;
1183 case '4':
1184 if (strcmp(arg, "-x486") == 0) {
1185 newae(ctx->i_ae, "-march=i486");
1186 break;
1187 }
1188 error(arg);
1189 break;
1190 #endif /* __x86 */
1191 case 'a':
1192 if (strncmp(arg, "-xarch=", 7) == 0) {
1193 mflag |= xlate_xtb(ctx->i_ae, arg + 7);
1194 break;
1195 }
1196 error(arg);
1197 break;
1198 case 'b':
1199 if (strncmp(arg, "-xbuiltin=", 10) == 0) {
1200 if (strcmp(arg + 10, "%all"))
1201 newae(ctx->i_ae, "-fbuiltin");
1202 break;
1203 }
1204 error(arg);
1205 break;
1206 case 'C':
1207 /* Accept C++ style comments -- ignore */
1208 if (strcmp(arg, "-xCC") == 0)
1209 break;
1210 error(arg);
1211 break;
1212 case 'c':
1213 if (strncmp(arg, "-xc99=%all", 10) == 0) {
1214 newae(ctx->i_ae, "-std=gnu99");
1215 break;
1216 }
1217 if (strncmp(arg, "-xc99=%none", 11) == 0) {
1218 newae(ctx->i_ae, "-std=gnu89");
1219 break;
1220 }
1221 if (strncmp(arg, "-xchip=", 7) == 0) {
1222 xlate(ctx->i_ae, arg + 7, xchip_tbl);
1223 break;
1224 }
1225 if (strncmp(arg, "-xcode=", 7) == 0) {
1226 xlate(ctx->i_ae, arg + 7, xcode_tbl);
1227 if (strncmp(arg + 7, "pic", 3) == 0)
1228 pic = 1;
1229 break;
1230 }
1231 if (strncmp(arg, "-xcache=", 8) == 0)
1232 break;
1233 if (strncmp(arg, "-xcrossfile", 11) == 0)
1234 break;
1235 error(arg);
1236 break;
1237 case 'd':
1238 if (strcmp(arg, "-xdepend") == 0)
1239 break;
1240 if (strncmp(arg, "-xdebugformat=", 14) == 0)
1241 break;
1242 error(arg);
1243 break;
1244 case 'F':
1245 /*
1246 * Compile for mapfile reordering, or unused
1247 * section elimination, syntax can be -xF or
1248 * more complex, like -xF=%all -- ignore.
1249 */
1250 if (strncmp(arg, "-xF", 3) == 0)
1251 break;
1252 error(arg);
1253 break;
1254 case 'i':
1255 if (strncmp(arg, "-xinline", 8) == 0)
1256 /* No inlining; ignore */
1257 break;
1258 if (strcmp(arg, "-xildon") == 0 ||
1259 strcmp(arg, "-xildoff") == 0)
1260 /* No incremental linking; ignore */
1261 break;
1262 error(arg);
1263 break;
1264 #if defined(__x86)
1265 case 'm':
1266 if (strcmp(arg, "-xmodel=kernel") == 0) {
1267 newae(ctx->i_ae, "-ffreestanding");
1268 newae(ctx->i_ae, "-mno-red-zone");
1269 model = "-mcmodel=kernel";
1270 nolibc = 1;
1271 break;
1272 }
1273 error(arg);
1274 break;
1275 #endif /* __x86 */
1276 case 'M':
1277 if (strcmp(arg, "-xM") == 0) {
1278 newae(ctx->i_ae, "-M");
1279 break;
1280 }
1281 if (strcmp(arg, "-xM1") == 0) {
1282 newae(ctx->i_ae, "-MM");
1283 break;
1284 }
1285 error(arg);
1286 break;
1287 case 'n':
1288 if (strcmp(arg, "-xnolib") == 0) {
1289 nolibc = 1;
1290 break;
1291 }
1292 error(arg);
1293 break;
1294 case 'O':
1295 if (strncmp(arg, "-xO", 3) == 0) {
1296 size_t len = strlen(arg);
1297 char *s;
1298 int c = *(arg + 3);
1299 int level;
1300
1301 if (len != 4 || !isdigit(c))
1302 error(arg);
1303
1304 if ((s = malloc(len)) == NULL)
1305 nomem();
1306
1307 level = atoi(arg + 3);
1308 if (level > 5)
1309 error(arg);
1310 if (level >= 2) {
1311 /*
1312 * For gcc-3.4.x at -O2 we
1313 * need to disable optimizations
1314 * that break ON.
1315 */
1316 optim_disable(ctx->i_ae, level);
1317 /*
1318 * limit -xO3 to -O2 as well.
1319 */
1320 level = 2;
1321 }
1322 (void) snprintf(s, len, "-O%d", level);
1323 newae(ctx->i_ae, s);
1324 free(s);
1325 break;
1326 }
1327 error(arg);
1328 break;
1329 case 'p':
1330 if (strcmp(arg, "-xpentium") == 0) {
1331 newae(ctx->i_ae, "-march=pentium");
1332 break;
1333 }
1334 if (strcmp(arg, "-xpg") == 0) {
1335 newae(ctx->i_ae, "-pg");
1336 break;
1337 }
1338 error(arg);
1339 break;
1340 case 'r':
1341 if (strncmp(arg, "-xregs=", 7) == 0) {
1342 xlate(ctx->i_ae, arg + 7, xregs_tbl);
1343 break;
1344 }
1345 error(arg);
1346 break;
1347 case 's':
1348 if (strcmp(arg, "-xs") == 0 ||
1349 strcmp(arg, "-xspace") == 0 ||
1350 strcmp(arg, "-xstrconst") == 0)
1351 break;
1352 error(arg);
1353 break;
1354 case 't':
1355 if (strcmp(arg, "-xtransition") == 0) {
1356 newae(ctx->i_ae, "-Wtransition");
1357 break;
1358 }
1359 if (strcmp(arg, "-xtrigraphs=yes") == 0) {
1360 newae(ctx->i_ae, "-trigraphs");
1361 break;
1362 }
1363 if (strcmp(arg, "-xtrigraphs=no") == 0) {
1364 newae(ctx->i_ae, "-notrigraphs");
1365 break;
1366 }
1367 if (strncmp(arg, "-xtarget=", 9) == 0) {
1368 xlate(ctx->i_ae, arg + 9, xtarget_tbl);
1369 break;
1370 }
1371 error(arg);
1372 break;
1373 case 'e':
1374 case 'h':
1375 case 'l':
1376 default:
1377 error(arg);
1378 break;
1379 }
1380 break;
1381 case 'Y':
1382 if (arglen == 1) {
1383 if ((arg = *++ctx->i_oldargv) == NULL ||
1384 *arg == '\0')
1385 error("-Y");
1386 ctx->i_oldargc--;
1387 arglen = strlen(arg + 1);
1388 } else {
1389 arg += 2;
1390 }
1391 /* Just ignore -YS,... for now */
1392 if (strncmp(arg, "S,", 2) == 0)
1393 break;
1394 if (strncmp(arg, "l,", 2) == 0) {
1395 char *s = strdup(arg);
1396 s[0] = '-';
1397 s[1] = 'B';
1398 newae(ctx->i_ae, s);
1399 free(s);
1400 break;
1401 }
1402 if (strncmp(arg, "I,", 2) == 0) {
1403 char *s = strdup(arg);
1404 s[0] = '-';
1405 s[1] = 'I';
1406 newae(ctx->i_ae, "-nostdinc");
1407 newae(ctx->i_ae, s);
1408 free(s);
1409 break;
1410 }
1411 error(arg);
1412 break;
1413 case 'Q':
1414 /*
1415 * We could map -Qy into -Wl,-Qy etc.
1416 */
1417 default:
1418 error(arg);
1419 break;
1420 }
1421 }
1422
1423 if (c_files > 1 && (ctx->i_flags & CW_F_SHADOW) &&
1424 op != CW_O_PREPROCESS) {
1425 (void) fprintf(stderr, "%s: error: multiple source files are "
1426 "allowed only with -E or -P\n", progname);
1427 exit(2);
1428 }
1429
1430 /*
1431 * Make sure that we do not have any unintended interactions between
1432 * the xarch options passed in and the version of the Studio compiler
1433 * used.
1434 */
1435 if ((mflag & (SS11|SS12)) == (SS11|SS12)) {
1436 (void) fprintf(stderr,
1437 "Conflicting \"-xarch=\" flags (both Studio 11 and 12)\n");
1438 exit(2);
1439 }
1440
1441 switch (mflag) {
1442 case 0:
1443 /* FALLTHROUGH */
1444 case M32:
1445 #if defined(__sparc)
1446 /*
1447 * Only -m32 is defined and so put in the missing xarch
1448 * translation.
1449 */
1450 newae(ctx->i_ae, "-mcpu=v8");
1451 newae(ctx->i_ae, "-mno-v8plus");
1452 #endif
1453 break;
1454 case M64:
1455 #if defined(__sparc)
1456 /*
1457 * Only -m64 is defined and so put in the missing xarch
1458 * translation.
1459 */
1460 newae(ctx->i_ae, "-mcpu=v9");
1461 #endif
1462 break;
1463 case SS12:
1464 #if defined(__sparc)
1465 /* no -m32/-m64 flag used - this is an error for sparc builds */
1466 (void) fprintf(stderr, "No -m32/-m64 flag defined\n");
1467 exit(2);
1468 #endif
1469 break;
1470 case SS11:
1471 /* FALLTHROUGH */
1472 case (SS11|M32):
1473 case (SS11|M64):
1474 break;
1475 case (SS12|M32):
1476 #if defined(__sparc)
1477 /*
1478 * Need to add in further 32 bit options because with SS12
1479 * the xarch=sparcvis option can be applied to 32 or 64
1480 * bit, and so the translatation table (xtbl) cannot handle
1481 * that.
1482 */
1483 newae(ctx->i_ae, "-mv8plus");
1484 #endif
1485 break;
1486 case (SS12|M64):
1487 break;
1488 default:
1489 (void) fprintf(stderr,
1490 "Incompatible -xarch= and/or -m32/-m64 options used.\n");
1491 exit(2);
1492 }
1493 if (op == CW_O_LINK && (ctx->i_flags & CW_F_SHADOW))
1494 exit(0);
1495
1496 if (model && !pic)
1497 newae(ctx->i_ae, model);
1498 if (!nolibc)
1499 newae(ctx->i_ae, "-lc");
1500 if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1501 newae(ctx->i_ae, "-o");
1502 newae(ctx->i_ae, ctx->i_discard);
1503 }
1504 }
1505
1506 static void
1507 do_cc(cw_ictx_t *ctx)
1508 {
1509 int in_output = 0, seen_o = 0;
1510 cw_op_t op = CW_O_LINK;
1511
1512 if (ctx->i_flags & CW_F_PROG) {
1513 newae(ctx->i_ae, "-V");
1514 return;
1515 }
1516
1517 while (--ctx->i_oldargc > 0) {
1518 char *arg = *++ctx->i_oldargv;
1519
1520 if (*arg != '-') {
1521 if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) {
1522 newae(ctx->i_ae, arg);
1523 } else {
1524 in_output = 0;
1525 newae(ctx->i_ae, ctx->i_discard);
1526 }
1527 continue;
1528 }
1529 switch (*(arg + 1)) {
1530 case '_':
1531 if (strcmp(arg, "-_noecho") == 0) {
1532 ctx->i_flags &= ~CW_F_ECHO;
1533 } else if (strncmp(arg, "-_cc=", 5) == 0 ||
1534 strncmp(arg, "-_CC=", 5) == 0) {
1535 newae(ctx->i_ae, arg + 5);
1536 } else if (strncmp(arg, "-_gcc=", 6) != 0 &&
1537 strncmp(arg, "-_g++=", 6) != 0) {
1538 (void) fprintf(stderr,
1539 "%s: invalid argument '%s'\n", progname,
1540 arg);
1541 exit(2);
1542 }
1543 break;
1544 case 'V':
1545 ctx->i_flags &= ~CW_F_ECHO;
1546 newae(ctx->i_ae, arg);
1547 break;
1548 case 'o':
1549 seen_o = 1;
1550 if (strlen(arg) == 2) {
1551 in_output = 1;
1552 newae(ctx->i_ae, arg);
1553 } else if (ctx->i_flags & CW_F_SHADOW) {
1554 newae(ctx->i_ae, "-o");
1555 newae(ctx->i_ae, ctx->i_discard);
1556 } else {
1557 newae(ctx->i_ae, arg);
1558 }
1559 break;
1560 case 'c':
1561 case 'S':
1562 if (strlen(arg) == 2)
1563 op = CW_O_COMPILE;
1564 newae(ctx->i_ae, arg);
1565 break;
1566 case 'E':
1567 case 'P':
1568 if (strlen(arg) == 2)
1569 op = CW_O_PREPROCESS;
1570 /*FALLTHROUGH*/
1571 default:
1572 newae(ctx->i_ae, arg);
1573 }
1574 }
1575
1576 if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
1577 (ctx->i_flags & CW_F_SHADOW))
1578 exit(0);
1579
1580 if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1581 newae(ctx->i_ae, "-o");
1582 newae(ctx->i_ae, ctx->i_discard);
1583 }
1584 }
1585
1586 static void
1587 prepctx(cw_ictx_t *ctx)
1588 {
1589 const char *dir = NULL, *cmd;
1590 char *program = NULL;
1591 size_t len;
1592
1593 switch (CIDX(CC(ctx), ctx->i_flags)) {
1594 case CIDX(CW_C_CC, 0):
1595 program = getenv("CW_CC");
1596 dir = getenv("CW_CC_DIR");
1597 break;
1598 case CIDX(CW_C_CC, CW_F_CXX):
1599 program = getenv("CW_CPLUSPLUS");
1600 dir = getenv("CW_CPLUSPLUS_DIR");
1601 break;
1602 case CIDX(CW_C_GCC, 0):
1603 program = getenv("CW_GCC");
1604 dir = getenv("CW_GCC_DIR");
1605 break;
1606 case CIDX(CW_C_GCC, CW_F_CXX):
1607 program = getenv("CW_GPLUSPLUS");
1608 dir = getenv("CW_GPLUSPLUS_DIR");
1609 break;
1610 }
1611
1612 if (program == NULL) {
1613 if (dir == NULL)
1614 dir = default_dir[CC(ctx)];
1615 cmd = cmds[CIDX(CC(ctx), ctx->i_flags)];
1616 len = strlen(dir) + strlen(cmd) + 2;
1617 if ((program = malloc(len)) == NULL)
1618 nomem();
1619 (void) snprintf(program, len, "%s/%s", dir, cmd);
1620 }
1621
1622 newae(ctx->i_ae, program);
1623
1624 if (ctx->i_flags & CW_F_PROG) {
1625 (void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ?
1626 "shadow" : "primary", program);
1627 (void) fflush(stdout);
1628 }
1629
1630 if (!(ctx->i_flags & CW_F_XLATE))
1631 return;
1632
1633 switch (CC(ctx)) {
1634 case CW_C_CC:
1635 do_cc(ctx);
1636 break;
1637 case CW_C_GCC:
1638 do_gcc(ctx);
1639 break;
1640 }
1641 }
1642
1643 static int
1644 invoke(cw_ictx_t *ctx)
1645 {
1646 char **newargv;
1647 int ac;
1648 struct ae *a;
1649
1650 if ((newargv = calloc(sizeof (*newargv), ctx->i_ae->ael_argc + 1)) ==
1651 NULL)
1652 nomem();
1653
1654 if (ctx->i_flags & CW_F_ECHO)
1655 (void) fprintf(stderr, "+ ");
1656
1657 for (ac = 0, a = ctx->i_ae->ael_head; a; a = a->ae_next, ac++) {
1658 newargv[ac] = a->ae_arg;
1659 if (ctx->i_flags & CW_F_ECHO)
1660 (void) fprintf(stderr, "%s ", a->ae_arg);
1661 if (a == ctx->i_ae->ael_tail)
1662 break;
1663 }
1664
1665 if (ctx->i_flags & CW_F_ECHO) {
1666 (void) fprintf(stderr, "\n");
1667 (void) fflush(stderr);
1668 }
1669
1670 if (!(ctx->i_flags & CW_F_EXEC))
1671 return (0);
1672
1673 /*
1674 * We must fix up the environment here so that the
1675 * dependency files are not trampled by the shadow compiler.
1676 */
1677 if ((ctx->i_flags & CW_F_SHADOW) &&
1678 (unsetenv("SUNPRO_DEPENDENCIES") != 0 ||
1679 unsetenv("DEPENDENCIES_OUTPUT") != 0)) {
1680 (void) fprintf(stderr, "error: environment setup failed: %s\n",
1681 strerror(errno));
1682 return (-1);
1683 }
1684
1685 (void) execv(newargv[0], newargv);
1686 cw_perror("couldn't run %s", newargv[0]);
1687
1688 return (-1);
1689 }
1690
1691 static int
1692 reap(cw_ictx_t *ctx)
1693 {
1694 int status, ret = 0;
1695 char buf[1024];
1696 struct stat s;
1697
1698 /*
1699 * Only wait for one specific child.
1700 */
1701 if (ctx->i_pid <= 0)
1702 return (-1);
1703
1704 do {
1705 if (waitpid(ctx->i_pid, &status, 0) < 0) {
1706 cw_perror("cannot reap child");
1707 return (-1);
1708 }
1709 if (status != 0) {
1710 if (WIFSIGNALED(status)) {
1711 ret = -WTERMSIG(status);
1712 break;
1713 } else if (WIFEXITED(status)) {
1714 ret = WEXITSTATUS(status);
1715 break;
1716 }
1717 }
1718 } while (!WIFEXITED(status) && !WIFSIGNALED(status));
1719
1720 (void) unlink(ctx->i_discard);
1721
1722 if (stat(ctx->i_stderr, &s) < 0) {
1723 cw_perror("stat failed on child cleanup");
1724 return (-1);
1725 }
1726 if (s.st_size != 0) {
1727 FILE *f;
1728
1729 if ((f = fopen(ctx->i_stderr, "r")) != NULL) {
1730 while (fgets(buf, sizeof (buf), f))
1731 (void) fprintf(stderr, "%s", buf);
1732 (void) fflush(stderr);
1733 (void) fclose(f);
1734 }
1735 }
1736 (void) unlink(ctx->i_stderr);
1737 free(ctx->i_stderr);
1738
1739 /*
1740 * cc returns an error code when given -V; we want that to succeed.
1741 */
1742 if (ctx->i_flags & CW_F_PROG)
1743 return (0);
1744
1745 return (ret);
1746 }
1747
1748 static int
1749 exec_ctx(cw_ictx_t *ctx, int block)
1750 {
1751 char *file;
1752
1753 /*
1754 * To avoid offending cc's sensibilities, the name of its output
1755 * file must end in '.o'.
1756 */
1757 if ((file = tempnam(NULL, ".cw")) == NULL) {
1758 nomem();
1759 return (-1);
1760 }
1761 (void) strlcpy(ctx->i_discard, file, MAXPATHLEN);
1762 (void) strlcat(ctx->i_discard, ".o", MAXPATHLEN);
1763 free(file);
1764
1765 if ((ctx->i_stderr = tempnam(NULL, ".cw")) == NULL) {
1766 nomem();
1767 return (-1);
1768 }
1769
1770 if ((ctx->i_pid = fork()) == 0) {
1771 int fd;
1772
1773 (void) fclose(stderr);
1774 if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL,
1775 0666)) < 0) {
1776 cw_perror("open failed for standard error");
1777 exit(1);
1778 }
1779 if (dup2(fd, 2) < 0) {
1780 cw_perror("dup2 failed for standard error");
1781 exit(1);
1782 }
1783 if (fd != 2)
1784 (void) close(fd);
1785 if (freopen("/dev/fd/2", "w", stderr) == NULL) {
1786 cw_perror("freopen failed for /dev/fd/2");
1787 exit(1);
1788 }
1789 prepctx(ctx);
1790 exit(invoke(ctx));
1791 }
1792
1793 if (ctx->i_pid < 0) {
1794 cw_perror("fork failed");
1795 return (1);
1796 }
1797
1798 if (block)
1799 return (reap(ctx));
1800
1801 return (0);
1802 }
1803
1804 int
1805 main(int argc, char **argv)
1806 {
1807 cw_ictx_t *ctx = newictx();
1808 cw_ictx_t *ctx_shadow = newictx();
1809 const char *dir;
1810 int do_serial, do_shadow;
1811 int ret = 0;
1812
1813 if ((progname = strrchr(argv[0], '/')) == NULL)
1814 progname = argv[0];
1815 else
1816 progname++;
1817
1818 if (ctx == NULL || ctx_shadow == NULL)
1819 nomem();
1820
1821 ctx->i_flags = CW_F_ECHO|CW_F_XLATE;
1822
1823 /*
1824 * Figure out where to get our tools from. This depends on
1825 * the environment variables set at run time.
1826 */
1827 if ((dir = getenv("SPRO_VROOT")) != NULL) {
1828 (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
1829 "%s/bin", dir);
1830 } else if ((dir = getenv("SPRO_ROOT")) != NULL) {
1831 (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
1832 "%s/SS12/bin", dir);
1833 } else if ((dir = getenv("BUILD_TOOLS")) != NULL) {
1834 (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
1835 "%s/SUNWspro/SS12/bin", dir);
1836 }
1837
1838 if ((dir = getenv("GCC_ROOT")) != NULL) {
1839 (void) snprintf(default_dir[CW_C_GCC], MAXPATHLEN,
1840 "%s/bin", dir);
1841 }
1842
1843 do_shadow = (getenv("CW_NO_SHADOW") ? 0 : 1);
1844 do_serial = (getenv("CW_SHADOW_SERIAL") ? 1 : 0);
1845
1846 if (getenv("CW_NO_EXEC") == NULL)
1847 ctx->i_flags |= CW_F_EXEC;
1848
1849 /*
1850 * The first argument must be one of "-_cc", "-_gcc", "-_CC", or "-_g++"
1851 */
1852 if (argc == 1)
1853 usage();
1854 argc--;
1855 argv++;
1856 if (strcmp(argv[0], "-_cc") == 0) {
1857 ctx->i_compiler = CW_C_CC;
1858 } else if (strcmp(argv[0], "-_gcc") == 0) {
1859 ctx->i_compiler = CW_C_GCC;
1860 } else if (strcmp(argv[0], "-_CC") == 0) {
1861 ctx->i_compiler = CW_C_CC;
1862 ctx->i_flags |= CW_F_CXX;
1863 } else if (strcmp(argv[0], "-_g++") == 0) {
1864 ctx->i_compiler = CW_C_GCC;
1865 ctx->i_flags |= CW_F_CXX;
1866 } else {
1867 /* assume "-_gcc" by default */
1868 argc++;
1869 argv--;
1870 ctx->i_compiler = CW_C_GCC;
1871 }
1872
1873 /*
1874 * -_compiler - tell us the path to the primary compiler only
1875 */
1876 if (argc > 1 && strcmp(argv[1], "-_compiler") == 0) {
1877 ctx->i_flags &= ~CW_F_XLATE;
1878 prepctx(ctx);
1879 (void) printf("%s\n", ctx->i_ae->ael_head->ae_arg);
1880 return (0);
1881 }
1882
1883 /*
1884 * -_versions - tell us the cw version, paths to all compilers, and
1885 * ask each for its version if we know how.
1886 */
1887 if (argc > 1 && strcmp(argv[1], "-_versions") == 0) {
1888 (void) printf("cw version %s", CW_VERSION);
1889 if (!do_shadow)
1890 (void) printf(" (SHADOW MODE DISABLED)");
1891 (void) printf("\n");
1892 (void) fflush(stdout);
1893 ctx->i_flags &= ~CW_F_ECHO;
1894 ctx->i_flags |= CW_F_PROG|CW_F_EXEC;
1895 argc--;
1896 argv++;
1897 do_serial = 1;
1898 }
1899
1900 ctx->i_oldargc = argc;
1901 ctx->i_oldargv = argv;
1902
1903 ret |= exec_ctx(ctx, do_serial);
1904
1905 if (do_shadow) {
1906 (void) memcpy(ctx_shadow, ctx, sizeof (cw_ictx_t));
1907 ctx_shadow->i_flags |= CW_F_SHADOW;
1908 ret |= exec_ctx(ctx_shadow, 1);
1909 }
1910
1911 if (!do_serial)
1912 ret |= reap(ctx);
1913
1914 return (ret);
1915 }