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