Print this page
9128 cw(1onbld) should be able to run multiple shadows
9129 file-locking tests shouldn't build multiple source files in one compiler invocation
9130 DTrace tst.gcc.d isn't useful
9132 cw(1onbld) shouldn't shadow pure preprocessing
Reviewed by: Andy Fiddaman <omnios@citrus-it.net>

   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  */
  25 /*
  26  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  27  * Use is subject to license terms.
  28  */
  29 
  30 /*
  31  * Wrapper for the GNU C compiler to make it accept the Sun C compiler
  32  * arguments where possible.
  33  *
  34  * Since the translation is inexact, this is something of a work-in-progress.
  35  *
  36  */
  37 
  38 /* If you modify this file, you must increment CW_VERSION */
  39 #define CW_VERSION      "1.30"
  40 
  41 /*
  42  * -#           Verbose mode
  43  * -###         Show compiler commands built by driver, no compilation
  44  * -A<name[(tokens)]>     Preprocessor predicate assertion
  45  * -B<[static|dynamic]>   Specify dynamic or static binding
  46  * -C           Prevent preprocessor from removing comments
  47  * -c           Compile only - produce .o files, suppress linking
  48  * -cg92        Alias for -xtarget=ss1000
  49  * -D<name[=token]>       Associate name with token as if by #define
  50  * -d[y|n]      dynamic [-dy] or static [-dn] option to linker
  51  * -E           Compile source through preprocessor only, output to stdout
  52  * -erroff=<t>    Suppress warnings specified by tags t(%none, %all, <tag list>)
  53  * -errtags=<a>   Display messages with tags a(no, yes)
  54  * -errwarn=<t>   Treats warnings specified by tags t(%none, %all, <tag list>)
  55  *              as errors
  56  * -fast        Optimize using a selection of options
  57  * -fd          Report old-style function definitions and declarations
  58  * -features=zla        Allow zero-length arrays
  59  * -flags       Show this summary of compiler options


 274  * -xsb                         error
 275  * -xsbfast                     error
 276  * -xsfpconst                   error
 277  * -xspace                      ignore (-not -Os)
 278  * -xstrconst                   ignore
 279  * -xtarget=<t>                   table
 280  * -xtemp=<dir>                   error
 281  * -xtime                       error
 282  * -xtransition                 -Wtransition
 283  * -xtrigraphs=<yes|no>           -trigraphs -notrigraphs
 284  * -xunroll=n                   error
 285  * -W0,-xdbggen=no%usedonly     -fno-eliminate-unused-debug-symbols
 286  *                              -fno-eliminate-unused-debug-types
 287  * -Y<c>,<dir>                      error
 288  * -YA,<dir>                      error
 289  * -YI,<dir>                      -nostdinc -I<dir>
 290  * -YP,<dir>                      error
 291  * -YS,<dir>                      error
 292  */
 293 
 294 #include <stdio.h>
 295 #include <sys/types.h>
 296 #include <unistd.h>
 297 #include <string.h>
 298 #include <stdlib.h>
 299 #include <ctype.h>
 300 #include <fcntl.h>
 301 #include <errno.h>
 302 #include <stdarg.h>
 303 #include <sys/utsname.h>





 304 #include <sys/param.h>
 305 #include <sys/isa_defs.h>
 306 #include <sys/wait.h>
 307 #include <sys/stat.h>



 308 
 309 #define CW_F_CXX        0x01
 310 #define CW_F_SHADOW     0x02
 311 #define CW_F_EXEC       0x04
 312 #define CW_F_ECHO       0x08
 313 #define CW_F_XLATE      0x10
 314 #define CW_F_PROG       0x20
 315 
 316 typedef enum cw_compiler {
 317         CW_C_CC = 0,
 318         CW_C_GCC
 319 } cw_compiler_t;
 320 
 321 static const char *cmds[] = {
 322         "cc", "CC",
 323         "gcc", "g++"
 324 };
 325 
 326 static char default_dir[2][MAXPATHLEN] = {
 327         DEFAULT_CC_DIR,
 328         DEFAULT_GCC_DIR,
 329 };
 330 
 331 #define CC(ctx) \
 332         (((ctx)->i_flags & CW_F_SHADOW) ? \
 333             ((ctx)->i_compiler == CW_C_CC ? CW_C_GCC : CW_C_CC) : \
 334             (ctx)->i_compiler)
 335 
 336 #define CIDX(compiler, flags)   \
 337         ((int)(compiler) << 1) + ((flags) & CW_F_CXX ? 1 : 0)
 338 
 339 typedef enum cw_op {
 340         CW_O_NONE = 0,
 341         CW_O_PREPROCESS,
 342         CW_O_COMPILE,
 343         CW_O_LINK
 344 } cw_op_t;
 345 
 346 struct aelist {
 347         struct ae {
 348                 struct ae *ae_next;
 349                 char *ae_arg;
 350         } *ael_head, *ael_tail;
 351         int ael_argc;
 352 };
 353 











 354 typedef struct cw_ictx {
 355         cw_compiler_t   i_compiler;

 356         struct aelist   *i_ae;
 357         uint32_t        i_flags;
 358         int             i_oldargc;
 359         char            **i_oldargv;
 360         pid_t           i_pid;
 361         char            i_discard[MAXPATHLEN];
 362         char            *i_stderr;
 363 } cw_ictx_t;
 364 
 365 /*
 366  * Status values to indicate which Studio compiler and associated
 367  * flags are being used.
 368  */
 369 #define M32             0x01    /* -m32 - only on Studio 12 */
 370 #define M64             0x02    /* -m64 - only on Studio 12 */
 371 #define SS11            0x100   /* Studio 11 */
 372 #define SS12            0x200   /* Studio 12 */
 373 
 374 #define TRANS_ENTRY     5
 375 /*
 376  * Translation table definition for the -xarch= flag. The "x_arg"
 377  * value is translated into the appropriate gcc flags according
 378  * to the values in x_trans[n]. The x_flags indicates what compiler
 379  * is being used and what flags have been set via the use of
 380  * "x_arg".
 381  */
 382 typedef struct xarch_table {
 383         char    *x_arg;
 384         int     x_flags;
 385         char    *x_trans[TRANS_ENTRY];
 386 } xarch_table_t;
 387 
 388 /*
 389  * The translation table for the -xarch= flag used in the Studio compilers.
 390  */
 391 static const xarch_table_t xtbl[] = {
 392 #if defined(__x86)
 393         { "generic",    SS11 },
 394         { "generic64",  (SS11|M64), { "-m64", "-mtune=opteron" } },
 395         { "amd64",      (SS11|M64), { "-m64", "-mtune=opteron" } },
 396         { "386",        SS11,   { "-march=i386" } },
 397         { "pentium_pro", SS11,  { "-march=pentiumpro" } },
 398         { "sse",        SS11, { "-msse", "-mfpmath=sse" } },
 399         { "sse2",       SS11, { "-msse2", "-mfpmath=sse" } },
 400 #elif defined(__sparc)
 401         { "generic",    (SS11|M32), { "-m32", "-mcpu=v8" } },
 402         { "generic64",  (SS11|M64), { "-m64", "-mcpu=v9" } },
 403         { "v8",         (SS11|M32), { "-m32", "-mcpu=v8", "-mno-v8plus" } },
 404         { "v8plus",     (SS11|M32), { "-m32", "-mcpu=v9", "-mv8plus" } },
 405         { "v8plusa",    (SS11|M32), { "-m32", "-mcpu=ultrasparc", "-mv8plus",
 406                         "-mvis" } },
 407         { "v8plusb",    (SS11|M32), { "-m32", "-mcpu=ultrasparc3", "-mv8plus",
 408                         "-mvis" } },
 409         { "v9",         (SS11|M64), { "-m64", "-mcpu=v9" } },
 410         { "v9a",        (SS11|M64), { "-m64", "-mcpu=ultrasparc", "-mvis" } },
 411         { "v9b",        (SS11|M64), { "-m64", "-mcpu=ultrasparc3", "-mvis" } },
 412         { "sparc",      SS12, { "-mcpu=v9", "-mv8plus" } },
 413         { "sparcvis",   SS12, { "-mcpu=ultrasparc", "-mvis" } },
 414         { "sparcvis2",  SS12, { "-mcpu=ultrasparc3", "-mvis" } }
 415 #endif
 416 };
 417 
 418 static int xtbl_size = sizeof (xtbl) / sizeof (xarch_table_t);
 419 
 420 static const char *progname;
 421 
 422 static const char *xchip_tbl[] = {
 423 #if defined(__x86)
 424         "386",          "-mtune=i386", NULL,
 425         "486",          "-mtune=i486", NULL,
 426         "pentium",      "-mtune=pentium", NULL,
 427         "pentium_pro",  "-mtune=pentiumpro", NULL,
 428 #elif defined(__sparc)
 429         "super",        "-mtune=supersparc", NULL,
 430         "ultra",        "-mtune=ultrasparc", NULL,
 431         "ultra3",       "-mtune=ultrasparc3", NULL,
 432 #endif
 433         NULL,           NULL
 434 };
 435 
 436 static const char *xcode_tbl[] = {
 437 #if defined(__sparc)
 438         "abs32",        "-fno-pic", "-mcmodel=medlow", NULL,
 439         "abs44",        "-fno-pic", "-mcmodel=medmid", NULL,
 440         "abs64",        "-fno-pic", "-mcmodel=medany", NULL,
 441         "pic13",        "-fpic", NULL,


 447 static const char *xtarget_tbl[] = {
 448 #if defined(__x86)
 449         "pentium_pro",  "-march=pentiumpro", NULL,
 450 #endif  /* __x86 */
 451         NULL,           NULL
 452 };
 453 
 454 static const char *xregs_tbl[] = {
 455 #if defined(__sparc)
 456         "appl",         "-mapp-regs", NULL,
 457         "no%appl",      "-mno-app-regs", NULL,
 458         "float",        "-mfpu", NULL,
 459         "no%float",     "-mno-fpu", NULL,
 460 #endif  /* __sparc */
 461         NULL,           NULL
 462 };
 463 
 464 static void
 465 nomem(void)
 466 {
 467         (void) fprintf(stderr, "%s: error: out of memory\n", progname);
 468         exit(1);
 469 }
 470 
 471 static void
 472 cw_perror(const char *fmt, ...)
 473 {
 474         va_list ap;
 475         int saved_errno = errno;
 476 
 477         (void) fprintf(stderr, "%s: error: ", progname);
 478 
 479         va_start(ap, fmt);
 480         (void) vfprintf(stderr, fmt, ap);
 481         va_end(ap);
 482 
 483         (void) fprintf(stderr, " (%s)\n", strerror(saved_errno));
 484 }
 485 
 486 static void
 487 newae(struct aelist *ael, const char *arg)
 488 {
 489         struct ae *ae;
 490 
 491         if ((ae = calloc(sizeof (*ae), 1)) == NULL)
 492                 nomem();
 493         ae->ae_arg = strdup(arg);
 494         if (ael->ael_tail == NULL)
 495                 ael->ael_head = ae;
 496         else
 497                 ael->ael_tail->ae_next = ae;
 498         ael->ael_tail = ae;
 499         ael->ael_argc++;
 500 }
 501 
 502 static cw_ictx_t *
 503 newictx(void)
 504 {
 505         cw_ictx_t *ctx = calloc(sizeof (cw_ictx_t), 1);
 506         if (ctx)
 507                 if ((ctx->i_ae = calloc(sizeof (struct aelist), 1)) == NULL) {
 508                         free(ctx);
 509                         return (NULL);
 510                 }
 511 
 512         return (ctx);
 513 }
 514 
 515 static void
 516 error(const char *arg)
 517 {
 518         (void) fprintf(stderr,
 519             "%s: error: mapping failed at or near arg '%s'\n", progname, arg);
 520         exit(2);
 521 }
 522 
 523 /*
 524  * Add the current favourite set of warnings to the gcc invocation.
 525  */
 526 static void
 527 warnings(struct aelist *h)
 528 {
 529         static int warningsonce;
 530 
 531         if (warningsonce++)
 532                 return;
 533 
 534         /*
 535          * Enable as many warnings as exist, then disable those that we never
 536          * ever want.
 537          */
 538         newae(h, "-Wall");
 539         newae(h, "-Wextra");
 540 }
 541 
 542 static void
 543 optim_disable(struct aelist *h, int level)
 544 {
 545         if (level >= 2) {
 546                 newae(h, "-fno-strict-aliasing");
 547                 newae(h, "-fno-unit-at-a-time");
 548                 newae(h, "-fno-optimize-sibling-calls");
 549         }
 550 }
 551 
 552 /* ARGSUSED */
 553 static void
 554 Xamode(struct aelist *h)
 555 {
 556 }
 557 
 558 static void
 559 Xcmode(struct aelist *h)
 560 {
 561         static int xconce;
 562 
 563         if (xconce++)
 564                 return;
 565 
 566         newae(h, "-ansi");
 567         newae(h, "-pedantic-errors");
 568 }
 569 
 570 static void
 571 Xsmode(struct aelist *h)
 572 {
 573         static int xsonce;
 574 
 575         if (xsonce++)
 576                 return;
 577 
 578         newae(h, "-traditional");
 579         newae(h, "-traditional-cpp");
 580 }
 581 
 582 static void
 583 usage()
 584 {

 585         (void) fprintf(stderr,
 586             "usage: %s { -_cc | -_gcc | -_CC | -_g++ } [ -_compiler | ... ]\n",
 587             progname);





 588         exit(2);
 589 }
 590 
 591 static int
 592 xlate_xtb(struct aelist *h, const char *xarg)
 593 {
 594         int     i, j;
 595 
 596         for (i = 0; i < xtbl_size; i++) {
 597                 if (strcmp(xtbl[i].x_arg, xarg) == 0)
 598                         break;
 599         }
 600 
 601         /*
 602          * At the end of the table and so no matching "arg" entry
 603          * found and so this must be a bad -xarch= flag.
 604          */
 605         if (i == xtbl_size)
 606                 error(xarg);
 607 


 626 
 627         if (*table == NULL)
 628                 error(xarg);
 629 
 630         table++;
 631 
 632         while (*table != NULL) {
 633                 newae(h, *table);
 634                 table++;
 635         }
 636 }
 637 
 638 static void
 639 do_gcc(cw_ictx_t *ctx)
 640 {
 641         int c;
 642         int pic = 0, nolibc = 0;
 643         int in_output = 0, seen_o = 0, c_files = 0;
 644         cw_op_t op = CW_O_LINK;
 645         char *model = NULL;

 646         int     mflag = 0;
 647 
 648         if (ctx->i_flags & CW_F_PROG) {
 649                 newae(ctx->i_ae, "--version");
 650                 return;
 651         }
 652 
 653         newae(ctx->i_ae, "-fident");
 654         newae(ctx->i_ae, "-finline");
 655         newae(ctx->i_ae, "-fno-inline-functions");
 656         newae(ctx->i_ae, "-fno-builtin");
 657         newae(ctx->i_ae, "-fno-asm");
 658         newae(ctx->i_ae, "-fdiagnostics-show-option");
 659         newae(ctx->i_ae, "-nodefaultlibs");
 660 
 661 #if defined(__sparc)
 662         /*
 663          * The SPARC ldd and std instructions require 8-byte alignment of
 664          * their address operand.  gcc correctly uses them only when the
 665          * ABI requires 8-byte alignment; unfortunately we have a number of
 666          * pieces of buggy code that doesn't conform to the ABI.  This
 667          * flag makes gcc work more like Studio with -xmemalign=4.
 668          */
 669         newae(ctx->i_ae, "-mno-integer-ldd-std");
 670 #endif
 671 
 672         /*
 673          * This is needed because 'u' is defined
 674          * under a conditional on 'sun'.  Should
 675          * probably just remove the conditional,
 676          * or make it be dependent on '__sun'.
 677          *
 678          * -Dunix is also missing in enhanced ANSI mode
 679          */
 680         newae(ctx->i_ae, "-D__sun");
 681 



 682         /*
 683          * Walk the argument list, translating as we go ..
 684          */
 685 
 686         while (--ctx->i_oldargc > 0) {
 687                 char *arg = *++ctx->i_oldargv;
 688                 size_t arglen = strlen(arg);
 689 
 690                 if (*arg == '-') {
 691                         arglen--;
 692                 } else {
 693                         /*
 694                          * Discard inline files that gcc doesn't grok
 695                          */
 696                         if (!in_output && arglen > 3 &&
 697                             strcmp(arg + arglen - 3, ".il") == 0)
 698                                 continue;
 699 
 700                         if (!in_output && arglen > 2 &&
 701                             arg[arglen - 2] == '.' &&
 702                             (arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' ||
 703                             arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i'))
 704                                 c_files++;
 705 
 706                         /*
 707                          * Otherwise, filenames and partial arguments
 708                          * are passed through for gcc to chew on.  However,
 709                          * output is always discarded for the secondary
 710                          * compiler.
 711                          */
 712                         if ((ctx->i_flags & CW_F_SHADOW) && in_output)
 713                                 newae(ctx->i_ae, ctx->i_discard);
 714                         else
 715                                 newae(ctx->i_ae, arg);
 716                         in_output = 0;
 717                         continue;
 718                 }
 719 
 720                 if (ctx->i_flags & CW_F_CXX) {




 721                         if (strncmp(arg, "-compat=", 8) == 0) {
 722                                 /* discard -compat=4 and -compat=5 */
 723                                 continue;
 724                         }
 725                         if (strcmp(arg, "-Qoption") == 0) {
 726                                 /* discard -Qoption and its two arguments */
 727                                 if (ctx->i_oldargc < 3)
 728                                         error(arg);
 729                                 ctx->i_oldargc -= 2;
 730                                 ctx->i_oldargv += 2;
 731                                 continue;
 732                         }
 733                         if (strcmp(arg, "-xwe") == 0) {
 734                                 /* turn warnings into errors */
 735                                 newae(ctx->i_ae, "-Werror");
 736                                 continue;
 737                         }
 738                         if (strcmp(arg, "-noex") == 0) {
 739                                 /* no exceptions */
 740                                 newae(ctx->i_ae, "-fno-exceptions");


 755                         if (strcmp(arg, "-norunpath") == 0) {
 756                                 /* gcc has no corresponding option */
 757                                 continue;
 758                         }
 759                         if (strcmp(arg, "-nolib") == 0) {
 760                                 /* -nodefaultlibs is on by default */
 761                                 nolibc = 1;
 762                                 continue;
 763                         }
 764 #if defined(__sparc)
 765                         if (strcmp(arg, "-cg92") == 0) {
 766                                 mflag |= xlate_xtb(ctx->i_ae, "v8");
 767                                 xlate(ctx->i_ae, "super", xchip_tbl);
 768                                 continue;
 769                         }
 770 #endif  /* __sparc */
 771                 }
 772 
 773                 switch ((c = arg[1])) {
 774                 case '_':
 775                         if (strcmp(arg, "-_noecho") == 0)
 776                                 ctx->i_flags &= ~CW_F_ECHO;
 777                         else if (strncmp(arg, "-_cc=", 5) == 0 ||
 778                             strncmp(arg, "-_CC=", 5) == 0)
 779                                 /* EMPTY */;
 780                         else if (strncmp(arg, "-_gcc=", 6) == 0 ||
 781                             strncmp(arg, "-_g++=", 6) == 0)
 782                                 newae(ctx->i_ae, arg + 6);
 783                         else
 784                                 error(arg);
 785                         break;
 786                 case '#':
 787                         if (arglen == 1) {
 788                                 newae(ctx->i_ae, "-v");
 789                                 break;
 790                         }
 791                         error(arg);
 792                         break;
 793                 case 'g':
 794                         newae(ctx->i_ae, "-gdwarf-2");
 795                         break;
 796                 case 'E':
 797                         if (arglen == 1) {
 798                                 newae(ctx->i_ae, "-xc");
 799                                 newae(ctx->i_ae, arg);
 800                                 op = CW_O_PREPROCESS;
 801                                 nolibc = 1;
 802                                 break;
 803                         }
 804                         error(arg);


1404                                 char *s = strdup(arg);
1405                                 s[0] = '-';
1406                                 s[1] = 'I';
1407                                 newae(ctx->i_ae, "-nostdinc");
1408                                 newae(ctx->i_ae, s);
1409                                 free(s);
1410                                 break;
1411                         }
1412                         error(arg);
1413                         break;
1414                 case 'Q':
1415                         /*
1416                          * We could map -Qy into -Wl,-Qy etc.
1417                          */
1418                 default:
1419                         error(arg);
1420                         break;
1421                 }
1422         }
1423 


1424         if (c_files > 1 && (ctx->i_flags & CW_F_SHADOW) &&
1425             op != CW_O_PREPROCESS) {
1426                 (void) fprintf(stderr, "%s: error: multiple source files are "
1427                     "allowed only with -E or -P\n", progname);
1428                 exit(2);
1429         }
1430 
1431         /*
1432          * Make sure that we do not have any unintended interactions between
1433          * the xarch options passed in and the version of the Studio compiler
1434          * used.
1435          */
1436         if ((mflag & (SS11|SS12)) == (SS11|SS12)) {
1437                 (void) fprintf(stderr,
1438                     "Conflicting \"-xarch=\" flags (both Studio 11 and 12)\n");
1439                 exit(2);
1440         }
1441 
1442         switch (mflag) {
1443         case 0:
1444                 /* FALLTHROUGH */
1445         case M32:
1446 #if defined(__sparc)
1447                 /*
1448                  * Only -m32 is defined and so put in the missing xarch
1449                  * translation.
1450                  */
1451                 newae(ctx->i_ae, "-mcpu=v8");
1452                 newae(ctx->i_ae, "-mno-v8plus");
1453 #endif
1454                 break;
1455         case M64:
1456 #if defined(__sparc)
1457                 /*
1458                  * Only -m64 is defined and so put in the missing xarch
1459                  * translation.


1474         case (SS11|M64):
1475                 break;
1476         case (SS12|M32):
1477 #if defined(__sparc)
1478                 /*
1479                  * Need to add in further 32 bit options because with SS12
1480                  * the xarch=sparcvis option can be applied to 32 or 64
1481                  * bit, and so the translatation table (xtbl) cannot handle
1482                  * that.
1483                  */
1484                 newae(ctx->i_ae, "-mv8plus");
1485 #endif
1486                 break;
1487         case (SS12|M64):
1488                 break;
1489         default:
1490                 (void) fprintf(stderr,
1491                     "Incompatible -xarch= and/or -m32/-m64 options used.\n");
1492                 exit(2);
1493         }
1494         if (op == CW_O_LINK && (ctx->i_flags & CW_F_SHADOW))


1495                 exit(0);
1496 
1497         if (model && !pic)
1498                 newae(ctx->i_ae, model);
1499         if (!nolibc)
1500                 newae(ctx->i_ae, "-lc");
1501         if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1502                 newae(ctx->i_ae, "-o");
1503                 newae(ctx->i_ae, ctx->i_discard);
1504         }
1505 }
1506 
1507 static void
1508 do_cc(cw_ictx_t *ctx)
1509 {
1510         int in_output = 0, seen_o = 0;
1511         cw_op_t op = CW_O_LINK;

1512 
1513         if (ctx->i_flags & CW_F_PROG) {
1514                 newae(ctx->i_ae, "-V");
1515                 return;
1516         }
1517 



1518         while (--ctx->i_oldargc > 0) {
1519                 char *arg = *++ctx->i_oldargv;
1520 





1521                 if (*arg != '-') {
1522                         if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) {
1523                                 newae(ctx->i_ae, arg);
1524                         } else {
1525                                 in_output = 0;
1526                                 newae(ctx->i_ae, ctx->i_discard);
1527                         }
1528                         continue;
1529                 }
1530                 switch (*(arg + 1)) {
1531                 case '_':
1532                         if (strcmp(arg, "-_noecho") == 0) {
1533                                 ctx->i_flags &= ~CW_F_ECHO;
1534                         } else if (strncmp(arg, "-_cc=", 5) == 0 ||
1535                             strncmp(arg, "-_CC=", 5) == 0) {
1536                                 newae(ctx->i_ae, arg + 5);
1537                         } else if (strncmp(arg, "-_gcc=", 6) != 0 &&
1538                             strncmp(arg, "-_g++=", 6) != 0) {
1539                                 (void) fprintf(stderr,
1540                                     "%s: invalid argument '%s'\n", progname,
1541                                     arg);
1542                                 exit(2);
1543                         }
1544                         break;

1545                 case 'V':
1546                         ctx->i_flags &= ~CW_F_ECHO;
1547                         newae(ctx->i_ae, arg);
1548                         break;
1549                 case 'o':
1550                         seen_o = 1;
1551                         if (strlen(arg) == 2) {
1552                                 in_output = 1;
1553                                 newae(ctx->i_ae, arg);
1554                         } else if (ctx->i_flags & CW_F_SHADOW) {
1555                                 newae(ctx->i_ae, "-o");
1556                                 newae(ctx->i_ae, ctx->i_discard);
1557                         } else {
1558                                 newae(ctx->i_ae, arg);
1559                         }
1560                         break;
1561                 case 'c':
1562                 case 'S':
1563                         if (strlen(arg) == 2)
1564                                 op = CW_O_COMPILE;
1565                         newae(ctx->i_ae, arg);
1566                         break;
1567                 case 'E':
1568                 case 'P':
1569                         if (strlen(arg) == 2)
1570                                 op = CW_O_PREPROCESS;
1571                 /*FALLTHROUGH*/
1572                 default:
1573                         newae(ctx->i_ae, arg);
1574                 }
1575         }
1576 


1577         if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
1578             (ctx->i_flags & CW_F_SHADOW))
1579                 exit(0);
1580 
1581         if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1582                 newae(ctx->i_ae, "-o");
1583                 newae(ctx->i_ae, ctx->i_discard);
1584         }
1585 }
1586 
1587 static void
1588 prepctx(cw_ictx_t *ctx)
1589 {
1590         const char *dir = NULL, *cmd;
1591         char *program = NULL;
1592         size_t len;
1593 
1594         switch (CIDX(CC(ctx), ctx->i_flags)) {
1595                 case CIDX(CW_C_CC, 0):
1596                         program = getenv("CW_CC");
1597                         dir = getenv("CW_CC_DIR");
1598                         break;
1599                 case CIDX(CW_C_CC, CW_F_CXX):
1600                         program = getenv("CW_CPLUSPLUS");
1601                         dir = getenv("CW_CPLUSPLUS_DIR");
1602                         break;
1603                 case CIDX(CW_C_GCC, 0):
1604                         program = getenv("CW_GCC");
1605                         dir = getenv("CW_GCC_DIR");
1606                         break;
1607                 case CIDX(CW_C_GCC, CW_F_CXX):
1608                         program = getenv("CW_GPLUSPLUS");
1609                         dir = getenv("CW_GPLUSPLUS_DIR");
1610                         break;
1611         }
1612 
1613         if (program == NULL) {
1614                 if (dir == NULL)
1615                         dir = default_dir[CC(ctx)];
1616                 cmd = cmds[CIDX(CC(ctx), ctx->i_flags)];
1617                 len = strlen(dir) + strlen(cmd) + 2;
1618                 if ((program = malloc(len)) == NULL)
1619                         nomem();
1620                 (void) snprintf(program, len, "%s/%s", dir, cmd);
1621         }
1622 
1623         newae(ctx->i_ae, program);
1624 
1625         if (ctx->i_flags & CW_F_PROG) {
1626                 (void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ?
1627                     "shadow" : "primary", program);
1628                 (void) fflush(stdout);
1629         }
1630 
1631         if (!(ctx->i_flags & CW_F_XLATE))
1632                 return;
1633 
1634         switch (CC(ctx)) {
1635         case CW_C_CC:
1636                 do_cc(ctx);
1637                 break;
1638         case CW_C_GCC:
1639                 do_gcc(ctx);
1640                 break;
1641         }
1642 }
1643 
1644 static int
1645 invoke(cw_ictx_t *ctx)
1646 {
1647         char **newargv;
1648         int ac;
1649         struct ae *a;
1650 
1651         if ((newargv = calloc(sizeof (*newargv), ctx->i_ae->ael_argc + 1)) ==
1652             NULL)
1653                 nomem();
1654 
1655         if (ctx->i_flags & CW_F_ECHO)
1656                 (void) fprintf(stderr, "+ ");
1657 
1658         for (ac = 0, a = ctx->i_ae->ael_head; a; a = a->ae_next, ac++) {
1659                 newargv[ac] = a->ae_arg;
1660                 if (ctx->i_flags & CW_F_ECHO)
1661                         (void) fprintf(stderr, "%s ", a->ae_arg);
1662                 if (a == ctx->i_ae->ael_tail)
1663                         break;
1664         }
1665 
1666         if (ctx->i_flags & CW_F_ECHO) {
1667                 (void) fprintf(stderr, "\n");
1668                 (void) fflush(stderr);
1669         }
1670 
1671         if (!(ctx->i_flags & CW_F_EXEC))
1672                 return (0);
1673 
1674         /*
1675          * We must fix up the environment here so that the
1676          * dependency files are not trampled by the shadow compiler.


1677          */
1678         if ((ctx->i_flags & CW_F_SHADOW) &&
1679             (unsetenv("SUNPRO_DEPENDENCIES") != 0 ||
1680             unsetenv("DEPENDENCIES_OUTPUT") != 0)) {

1681                 (void) fprintf(stderr, "error: environment setup failed: %s\n",
1682                     strerror(errno));
1683                 return (-1);
1684         }
1685 
1686         (void) execv(newargv[0], newargv);
1687         cw_perror("couldn't run %s", newargv[0]);
1688 
1689         return (-1);
1690 }
1691 
1692 static int
1693 reap(cw_ictx_t *ctx)
1694 {
1695         int status, ret = 0;
1696         char buf[1024];
1697         struct stat s;
1698 
1699         /*
1700          * Only wait for one specific child.
1701          */
1702         if (ctx->i_pid <= 0)
1703                 return (-1);
1704 
1705         do {
1706                 if (waitpid(ctx->i_pid, &status, 0) < 0) {
1707                         cw_perror("cannot reap child");
1708                         return (-1);
1709                 }
1710                 if (status != 0) {
1711                         if (WIFSIGNALED(status)) {
1712                                 ret = -WTERMSIG(status);
1713                                 break;
1714                         } else if (WIFEXITED(status)) {
1715                                 ret = WEXITSTATUS(status);
1716                                 break;
1717                         }
1718                 }
1719         } while (!WIFEXITED(status) && !WIFSIGNALED(status));
1720 
1721         (void) unlink(ctx->i_discard);
1722 
1723         if (stat(ctx->i_stderr, &s) < 0) {
1724                 cw_perror("stat failed on child cleanup");
1725                 return (-1);
1726         }
1727         if (s.st_size != 0) {
1728                 FILE *f;
1729 
1730                 if ((f = fopen(ctx->i_stderr, "r")) != NULL) {
1731                         while (fgets(buf, sizeof (buf), f))
1732                                 (void) fprintf(stderr, "%s", buf);
1733                         (void) fflush(stderr);
1734                         (void) fclose(f);
1735                 }
1736         }
1737         (void) unlink(ctx->i_stderr);
1738         free(ctx->i_stderr);
1739 
1740         /*
1741          * cc returns an error code when given -V; we want that to succeed.
1742          */
1743         if (ctx->i_flags & CW_F_PROG)
1744                 return (0);


1757          */
1758         if ((file = tempnam(NULL, ".cw")) == NULL) {
1759                 nomem();
1760                 return (-1);
1761         }
1762         (void) strlcpy(ctx->i_discard, file, MAXPATHLEN);
1763         (void) strlcat(ctx->i_discard, ".o", MAXPATHLEN);
1764         free(file);
1765 
1766         if ((ctx->i_stderr = tempnam(NULL, ".cw")) == NULL) {
1767                 nomem();
1768                 return (-1);
1769         }
1770 
1771         if ((ctx->i_pid = fork()) == 0) {
1772                 int fd;
1773 
1774                 (void) fclose(stderr);
1775                 if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL,
1776                     0666)) < 0) {
1777                         cw_perror("open failed for standard error");
1778                         exit(1);
1779                 }
1780                 if (dup2(fd, 2) < 0) {
1781                         cw_perror("dup2 failed for standard error");
1782                         exit(1);
1783                 }
1784                 if (fd != 2)
1785                         (void) close(fd);
1786                 if (freopen("/dev/fd/2", "w", stderr) == NULL) {
1787                         cw_perror("freopen failed for /dev/fd/2");
1788                         exit(1);
1789                 }

1790                 prepctx(ctx);
1791                 exit(invoke(ctx));
1792         }
1793 
1794         if (ctx->i_pid < 0) {
1795                 cw_perror("fork failed");
1796                 return (1);
1797         }
1798 
1799         if (block)
1800                 return (reap(ctx));
1801 
1802         return (0);
1803 }
1804 
































1805 int
1806 main(int argc, char **argv)
1807 {
1808         cw_ictx_t *ctx = newictx();
1809         cw_ictx_t *ctx_shadow = newictx();
1810         const char *dir;
1811         int do_serial, do_shadow;
1812         int ret = 0;






1813 
1814         if ((progname = strrchr(argv[0], '/')) == NULL)
1815                 progname = argv[0];
1816         else
1817                 progname++;
1818 
1819         if (ctx == NULL || ctx_shadow == NULL)










1820                 nomem();
1821 
1822         ctx->i_flags = CW_F_ECHO|CW_F_XLATE;
















1823 
1824         /*
1825          * Figure out where to get our tools from.  This depends on
1826          * the environment variables set at run time.
1827          */
1828         if ((dir = getenv("SPRO_VROOT")) != NULL) {
1829                 (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
1830                     "%s/bin", dir);
1831         } else if ((dir = getenv("SPRO_ROOT")) != NULL) {
1832                 (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
1833                     "%s/SS12/bin", dir);
1834         } else if ((dir = getenv("BUILD_TOOLS")) != NULL) {
1835                 (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
1836                     "%s/SUNWspro/SS12/bin", dir);


1837         }

1838 
1839         if ((dir = getenv("GNUC_ROOT")) != NULL) {
1840                 (void) snprintf(default_dir[CW_C_GCC], MAXPATHLEN,
1841                     "%s/bin", dir);
1842         }
1843 
1844         do_shadow = (getenv("CW_NO_SHADOW") ? 0 : 1);
1845         do_serial = (getenv("CW_SHADOW_SERIAL") ? 1 : 0);
1846 
1847         if (getenv("CW_NO_EXEC") == NULL)
1848                 ctx->i_flags |= CW_F_EXEC;

1849 
1850         /*
1851          * The first argument must be one of "-_cc", "-_gcc", "-_CC", or "-_g++"
1852          */
1853         if (argc == 1)
1854                 usage();
1855         argc--;
1856         argv++;
1857         if (strcmp(argv[0], "-_cc") == 0) {
1858                 ctx->i_compiler = CW_C_CC;
1859         } else if (strcmp(argv[0], "-_gcc") == 0) {
1860                 ctx->i_compiler = CW_C_GCC;
1861         } else if (strcmp(argv[0], "-_CC") == 0) {
1862                 ctx->i_compiler = CW_C_CC;
1863                 ctx->i_flags |= CW_F_CXX;
1864         } else if (strcmp(argv[0], "-_g++") == 0) {
1865                 ctx->i_compiler = CW_C_GCC;
1866                 ctx->i_flags |= CW_F_CXX;
1867         } else {
1868                 /* assume "-_gcc" by default */
1869                 argc++;
1870                 argv--;
1871                 ctx->i_compiler = CW_C_GCC;
1872         }
1873 
1874         /*
1875          * -_compiler - tell us the path to the primary compiler only
1876          */
1877         if (argc > 1 && strcmp(argv[1], "-_compiler") == 0) {
1878                 ctx->i_flags &= ~CW_F_XLATE;
1879                 prepctx(ctx);
1880                 (void) printf("%s\n", ctx->i_ae->ael_head->ae_arg);
1881                 return (0);
1882         }
1883 
1884         /*
1885          * -_versions - tell us the cw version, paths to all compilers, and
1886          *              ask each for its version if we know how.
1887          */
1888         if (argc > 1 && strcmp(argv[1], "-_versions") == 0) {
1889                 (void) printf("cw version %s", CW_VERSION);
1890                 if (!do_shadow)
1891                         (void) printf(" (SHADOW MODE DISABLED)");
1892                 (void) printf("\n");
1893                 (void) fflush(stdout);
1894                 ctx->i_flags &= ~CW_F_ECHO;
1895                 ctx->i_flags |= CW_F_PROG|CW_F_EXEC;
1896                 argc--;
1897                 argv++;
1898                 do_serial = 1;
1899         }
1900 
1901         ctx->i_oldargc = argc;
1902         ctx->i_oldargv = argv;
1903 
1904         ret |= exec_ctx(ctx, do_serial);


1905 
1906         if (do_shadow) {
1907                 (void) memcpy(ctx_shadow, ctx, sizeof (cw_ictx_t));
1908                 ctx_shadow->i_flags |= CW_F_SHADOW;
1909                 ret |= exec_ctx(ctx_shadow, 1);








1910         }


1911 
1912         if (!do_serial)
1913                 ret |= reap(ctx);






1914 
1915         return (ret);
1916 }
   1 
   2 /*
   3  * CDDL HEADER START
   4  *
   5  * The contents of this file are subject to the terms of the
   6  * Common Development and Distribution License (the "License").
   7  * You may not use this file except in compliance with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 
  23 /*
  24  * Copyright 2018, Richard Lowe.
  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      "2.0"
  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


 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 <ctype.h>
 296 #include <err.h>
 297 #include <errno.h>
 298 #include <fcntl.h>
 299 #include <getopt.h>
 300 #include <stdio.h>
 301 #include <stdlib.h>
 302 #include <string.h>
 303 #include <unistd.h>
 304 
 305 #include <sys/param.h>


 306 #include <sys/stat.h>
 307 #include <sys/types.h>
 308 #include <sys/utsname.h>
 309 #include <sys/wait.h>
 310 
 311 #define CW_F_CXX        0x01
 312 #define CW_F_SHADOW     0x02
 313 #define CW_F_EXEC       0x04
 314 #define CW_F_ECHO       0x08
 315 #define CW_F_XLATE      0x10
 316 #define CW_F_PROG       0x20
 317 























 318 typedef enum cw_op {
 319         CW_O_NONE = 0,
 320         CW_O_PREPROCESS,
 321         CW_O_COMPILE,
 322         CW_O_LINK
 323 } cw_op_t;
 324 
 325 struct aelist {
 326         struct ae {
 327                 struct ae *ae_next;
 328                 char *ae_arg;
 329         } *ael_head, *ael_tail;
 330         int ael_argc;
 331 };
 332 
 333 typedef enum {
 334         GNU,
 335         SUN
 336 } compiler_style_t;
 337 
 338 typedef struct {
 339         char *c_name;
 340         char *c_path;
 341         compiler_style_t c_style;
 342 } cw_compiler_t;
 343 
 344 typedef struct cw_ictx {
 345         struct cw_ictx  *i_next;
 346         cw_compiler_t   *i_compiler;
 347         struct aelist   *i_ae;
 348         uint32_t        i_flags;
 349         int             i_oldargc;
 350         char            **i_oldargv;
 351         pid_t           i_pid;
 352         char            i_discard[MAXPATHLEN];
 353         char            *i_stderr;
 354 } cw_ictx_t;
 355 
 356 /*
 357  * Status values to indicate which Studio compiler and associated
 358  * flags are being used.
 359  */
 360 #define M32             0x01    /* -m32 - only on Studio 12 */
 361 #define M64             0x02    /* -m64 - only on Studio 12 */
 362 #define SS11            0x100   /* Studio 11 */
 363 #define SS12            0x200   /* Studio 12 */
 364 
 365 #define TRANS_ENTRY     5
 366 /*
 367  * Translation table definition for the -xarch= flag. The "x_arg"
 368  * value is translated into the appropriate gcc flags according
 369  * to the values in x_trans[n]. The x_flags indicates what compiler
 370  * is being used and what flags have been set via the use of
 371  * "x_arg".
 372  */
 373 typedef struct xarch_table {
 374         char    *x_arg;
 375         int     x_flags;
 376         char    *x_trans[TRANS_ENTRY];
 377 } xarch_table_t;
 378 
 379 /*
 380  * The translation table for the -xarch= flag used in the Studio compilers.
 381  */
 382 static const xarch_table_t xtbl[] = {
 383 #if defined(__x86)
 384         { "generic",    SS11, {NULL} },
 385         { "generic64",  (SS11|M64), { "-m64", "-mtune=opteron" } },
 386         { "amd64",      (SS11|M64), { "-m64", "-mtune=opteron" } },
 387         { "386",        SS11,   { "-march=i386" } },
 388         { "pentium_pro", SS11,  { "-march=pentiumpro" } },
 389         { "sse",        SS11, { "-msse", "-mfpmath=sse" } },
 390         { "sse2",       SS11, { "-msse2", "-mfpmath=sse" } },
 391 #elif defined(__sparc)
 392         { "generic",    (SS11|M32), { "-m32", "-mcpu=v8" } },
 393         { "generic64",  (SS11|M64), { "-m64", "-mcpu=v9" } },
 394         { "v8",         (SS11|M32), { "-m32", "-mcpu=v8", "-mno-v8plus" } },
 395         { "v8plus",     (SS11|M32), { "-m32", "-mcpu=v9", "-mv8plus" } },
 396         { "v8plusa",    (SS11|M32), { "-m32", "-mcpu=ultrasparc", "-mv8plus",
 397                         "-mvis" } },
 398         { "v8plusb",    (SS11|M32), { "-m32", "-mcpu=ultrasparc3", "-mv8plus",
 399                         "-mvis" } },
 400         { "v9",         (SS11|M64), { "-m64", "-mcpu=v9" } },
 401         { "v9a",        (SS11|M64), { "-m64", "-mcpu=ultrasparc", "-mvis" } },
 402         { "v9b",        (SS11|M64), { "-m64", "-mcpu=ultrasparc3", "-mvis" } },
 403         { "sparc",      SS12, { "-mcpu=v9", "-mv8plus" } },
 404         { "sparcvis",   SS12, { "-mcpu=ultrasparc", "-mvis" } },
 405         { "sparcvis2",  SS12, { "-mcpu=ultrasparc3", "-mvis" } }
 406 #endif
 407 };
 408 
 409 static int xtbl_size = sizeof (xtbl) / sizeof (xarch_table_t);
 410 


 411 static const char *xchip_tbl[] = {
 412 #if defined(__x86)
 413         "386",          "-mtune=i386", NULL,
 414         "486",          "-mtune=i486", NULL,
 415         "pentium",      "-mtune=pentium", NULL,
 416         "pentium_pro",  "-mtune=pentiumpro", NULL,
 417 #elif defined(__sparc)
 418         "super",        "-mtune=supersparc", NULL,
 419         "ultra",        "-mtune=ultrasparc", NULL,
 420         "ultra3",       "-mtune=ultrasparc3", NULL,
 421 #endif
 422         NULL,           NULL
 423 };
 424 
 425 static const char *xcode_tbl[] = {
 426 #if defined(__sparc)
 427         "abs32",        "-fno-pic", "-mcmodel=medlow", NULL,
 428         "abs44",        "-fno-pic", "-mcmodel=medmid", NULL,
 429         "abs64",        "-fno-pic", "-mcmodel=medany", NULL,
 430         "pic13",        "-fpic", NULL,


 436 static const char *xtarget_tbl[] = {
 437 #if defined(__x86)
 438         "pentium_pro",  "-march=pentiumpro", NULL,
 439 #endif  /* __x86 */
 440         NULL,           NULL
 441 };
 442 
 443 static const char *xregs_tbl[] = {
 444 #if defined(__sparc)
 445         "appl",         "-mapp-regs", NULL,
 446         "no%appl",      "-mno-app-regs", NULL,
 447         "float",        "-mfpu", NULL,
 448         "no%float",     "-mno-fpu", NULL,
 449 #endif  /* __sparc */
 450         NULL,           NULL
 451 };
 452 
 453 static void
 454 nomem(void)
 455 {
 456         errx(1, "out of memory");

 457 }
 458 
 459 static void















 460 newae(struct aelist *ael, const char *arg)
 461 {
 462         struct ae *ae;
 463 
 464         if ((ae = calloc(sizeof (*ae), 1)) == NULL)
 465                 nomem();
 466         ae->ae_arg = strdup(arg);
 467         if (ael->ael_tail == NULL)
 468                 ael->ael_head = ae;
 469         else
 470                 ael->ael_tail->ae_next = ae;
 471         ael->ael_tail = ae;
 472         ael->ael_argc++;
 473 }
 474 
 475 static cw_ictx_t *
 476 newictx(void)
 477 {
 478         cw_ictx_t *ctx = calloc(sizeof (cw_ictx_t), 1);
 479         if (ctx)
 480                 if ((ctx->i_ae = calloc(sizeof (struct aelist), 1)) == NULL) {
 481                         free(ctx);
 482                         return (NULL);
 483                 }
 484 
 485         return (ctx);
 486 }
 487 
 488 static void
 489 error(const char *arg)
 490 {
 491         errx(2, "error: mapping failed at or near arg '%s'", arg);


 492 }
 493 
 494 /*
 495  * Add the current favourite set of warnings to the gcc invocation.
 496  */
 497 static void
 498 warnings(struct aelist *h)
 499 {
 500         static int warningsonce;
 501 
 502         if (warningsonce++)
 503                 return;
 504 
 505         /*
 506          * Enable as many warnings as exist, then disable those that we never
 507          * ever want.
 508          */
 509         newae(h, "-Wall");
 510         newae(h, "-Wextra");
 511 }
 512 
 513 static void
 514 optim_disable(struct aelist *h, int level)
 515 {
 516         if (level >= 2) {
 517                 newae(h, "-fno-strict-aliasing");
 518                 newae(h, "-fno-unit-at-a-time");
 519                 newae(h, "-fno-optimize-sibling-calls");
 520         }
 521 }
 522 
 523 /* ARGSUSED */
 524 static void
 525 Xamode(struct aelist __unused *h)
 526 {
 527 }
 528 
 529 static void
 530 Xcmode(struct aelist *h)
 531 {
 532         static int xconce;
 533 
 534         if (xconce++)
 535                 return;
 536 
 537         newae(h, "-ansi");
 538         newae(h, "-pedantic-errors");
 539 }
 540 
 541 static void
 542 Xsmode(struct aelist *h)
 543 {
 544         static int xsonce;
 545 
 546         if (xsonce++)
 547                 return;
 548 
 549         newae(h, "-traditional");
 550         newae(h, "-traditional-cpp");
 551 }
 552 
 553 static void
 554 usage()
 555 {
 556         extern char *__progname;
 557         (void) fprintf(stderr,
 558             "usage: %s [-C] [--versions] --primary <compiler> "
 559             "[--shadow <compiler>]... -- cflags...\n",
 560             __progname);
 561         (void) fprintf(stderr, "compilers take the form: name,path,style\n"
 562             " - name: a unique name usable in flag specifiers\n"
 563             " - path: path to the compiler binary\n"
 564             " - style: the style of flags expected: either sun or gnu\n");
 565         exit(2);
 566 }
 567 
 568 static int
 569 xlate_xtb(struct aelist *h, const char *xarg)
 570 {
 571         int     i, j;
 572 
 573         for (i = 0; i < xtbl_size; i++) {
 574                 if (strcmp(xtbl[i].x_arg, xarg) == 0)
 575                         break;
 576         }
 577 
 578         /*
 579          * At the end of the table and so no matching "arg" entry
 580          * found and so this must be a bad -xarch= flag.
 581          */
 582         if (i == xtbl_size)
 583                 error(xarg);
 584 


 603 
 604         if (*table == NULL)
 605                 error(xarg);
 606 
 607         table++;
 608 
 609         while (*table != NULL) {
 610                 newae(h, *table);
 611                 table++;
 612         }
 613 }
 614 
 615 static void
 616 do_gcc(cw_ictx_t *ctx)
 617 {
 618         int c;
 619         int pic = 0, nolibc = 0;
 620         int in_output = 0, seen_o = 0, c_files = 0;
 621         cw_op_t op = CW_O_LINK;
 622         char *model = NULL;
 623         char *nameflag;
 624         int     mflag = 0;
 625 
 626         if (ctx->i_flags & CW_F_PROG) {
 627                 newae(ctx->i_ae, "--version");
 628                 return;
 629         }
 630 
 631         newae(ctx->i_ae, "-fident");
 632         newae(ctx->i_ae, "-finline");
 633         newae(ctx->i_ae, "-fno-inline-functions");
 634         newae(ctx->i_ae, "-fno-builtin");
 635         newae(ctx->i_ae, "-fno-asm");
 636         newae(ctx->i_ae, "-fdiagnostics-show-option");
 637         newae(ctx->i_ae, "-nodefaultlibs");
 638 
 639 #if defined(__sparc)
 640         /*
 641          * The SPARC ldd and std instructions require 8-byte alignment of
 642          * their address operand.  gcc correctly uses them only when the
 643          * ABI requires 8-byte alignment; unfortunately we have a number of
 644          * pieces of buggy code that doesn't conform to the ABI.  This
 645          * flag makes gcc work more like Studio with -xmemalign=4.
 646          */
 647         newae(ctx->i_ae, "-mno-integer-ldd-std");
 648 #endif
 649 
 650         /*
 651          * This is needed because 'u' is defined
 652          * under a conditional on 'sun'.  Should
 653          * probably just remove the conditional,
 654          * or make it be dependent on '__sun'.
 655          *
 656          * -Dunix is also missing in enhanced ANSI mode
 657          */
 658         newae(ctx->i_ae, "-D__sun");
 659 
 660         if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1)
 661                 nomem();
 662 
 663         /*
 664          * Walk the argument list, translating as we go ..
 665          */

 666         while (--ctx->i_oldargc > 0) {
 667                 char *arg = *++ctx->i_oldargv;
 668                 size_t arglen = strlen(arg);
 669 
 670                 if (*arg == '-') {
 671                         arglen--;
 672                 } else {
 673                         /*
 674                          * Discard inline files that gcc doesn't grok
 675                          */
 676                         if (!in_output && arglen > 3 &&
 677                             strcmp(arg + arglen - 3, ".il") == 0)
 678                                 continue;
 679 
 680                         if (!in_output && arglen > 2 &&
 681                             arg[arglen - 2] == '.' &&
 682                             (arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' ||
 683                             arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i'))
 684                                 c_files++;
 685 
 686                         /*
 687                          * Otherwise, filenames and partial arguments
 688                          * are passed through for gcc to chew on.  However,
 689                          * output is always discarded for the secondary
 690                          * compiler.
 691                          */
 692                         if ((ctx->i_flags & CW_F_SHADOW) && in_output)
 693                                 newae(ctx->i_ae, ctx->i_discard);
 694                         else
 695                                 newae(ctx->i_ae, arg);
 696                         in_output = 0;
 697                         continue;
 698                 }
 699 
 700                 if (ctx->i_flags & CW_F_CXX) {
 701                         if (strncmp(arg, "-_g++=", 6) == 0) {
 702                                 newae(ctx->i_ae, strchr(arg, '=') + 1);
 703                                 continue;
 704                         }
 705                         if (strncmp(arg, "-compat=", 8) == 0) {
 706                                 /* discard -compat=4 and -compat=5 */
 707                                 continue;
 708                         }
 709                         if (strcmp(arg, "-Qoption") == 0) {
 710                                 /* discard -Qoption and its two arguments */
 711                                 if (ctx->i_oldargc < 3)
 712                                         error(arg);
 713                                 ctx->i_oldargc -= 2;
 714                                 ctx->i_oldargv += 2;
 715                                 continue;
 716                         }
 717                         if (strcmp(arg, "-xwe") == 0) {
 718                                 /* turn warnings into errors */
 719                                 newae(ctx->i_ae, "-Werror");
 720                                 continue;
 721                         }
 722                         if (strcmp(arg, "-noex") == 0) {
 723                                 /* no exceptions */
 724                                 newae(ctx->i_ae, "-fno-exceptions");


 739                         if (strcmp(arg, "-norunpath") == 0) {
 740                                 /* gcc has no corresponding option */
 741                                 continue;
 742                         }
 743                         if (strcmp(arg, "-nolib") == 0) {
 744                                 /* -nodefaultlibs is on by default */
 745                                 nolibc = 1;
 746                                 continue;
 747                         }
 748 #if defined(__sparc)
 749                         if (strcmp(arg, "-cg92") == 0) {
 750                                 mflag |= xlate_xtb(ctx->i_ae, "v8");
 751                                 xlate(ctx->i_ae, "super", xchip_tbl);
 752                                 continue;
 753                         }
 754 #endif  /* __sparc */
 755                 }
 756 
 757                 switch ((c = arg[1])) {
 758                 case '_':
 759                         if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) ||
 760                             (strncmp(arg, "-_gcc=", 6) == 0) ||
 761                             (strncmp(arg, "-_gnu=", 6) == 0)) {
 762                                 newae(ctx->i_ae, strchr(arg, '=') + 1);
 763                         }





 764                         break;
 765                 case '#':
 766                         if (arglen == 1) {
 767                                 newae(ctx->i_ae, "-v");
 768                                 break;
 769                         }
 770                         error(arg);
 771                         break;
 772                 case 'g':
 773                         newae(ctx->i_ae, "-gdwarf-2");
 774                         break;
 775                 case 'E':
 776                         if (arglen == 1) {
 777                                 newae(ctx->i_ae, "-xc");
 778                                 newae(ctx->i_ae, arg);
 779                                 op = CW_O_PREPROCESS;
 780                                 nolibc = 1;
 781                                 break;
 782                         }
 783                         error(arg);


1383                                 char *s = strdup(arg);
1384                                 s[0] = '-';
1385                                 s[1] = 'I';
1386                                 newae(ctx->i_ae, "-nostdinc");
1387                                 newae(ctx->i_ae, s);
1388                                 free(s);
1389                                 break;
1390                         }
1391                         error(arg);
1392                         break;
1393                 case 'Q':
1394                         /*
1395                          * We could map -Qy into -Wl,-Qy etc.
1396                          */
1397                 default:
1398                         error(arg);
1399                         break;
1400                 }
1401         }
1402 
1403         free(nameflag);
1404 
1405         if (c_files > 1 && (ctx->i_flags & CW_F_SHADOW) &&
1406             op != CW_O_PREPROCESS) {
1407                 errx(2, "multiple source files are "
1408                     "allowed only with -E or -P");

1409         }
1410 
1411         /*
1412          * Make sure that we do not have any unintended interactions between
1413          * the xarch options passed in and the version of the Studio compiler
1414          * used.
1415          */
1416         if ((mflag & (SS11|SS12)) == (SS11|SS12)) {
1417                 errx(2,
1418                     "Conflicting \"-xarch=\" flags (both Studio 11 and 12)\n");

1419         }
1420 
1421         switch (mflag) {
1422         case 0:
1423                 /* FALLTHROUGH */
1424         case M32:
1425 #if defined(__sparc)
1426                 /*
1427                  * Only -m32 is defined and so put in the missing xarch
1428                  * translation.
1429                  */
1430                 newae(ctx->i_ae, "-mcpu=v8");
1431                 newae(ctx->i_ae, "-mno-v8plus");
1432 #endif
1433                 break;
1434         case M64:
1435 #if defined(__sparc)
1436                 /*
1437                  * Only -m64 is defined and so put in the missing xarch
1438                  * translation.


1453         case (SS11|M64):
1454                 break;
1455         case (SS12|M32):
1456 #if defined(__sparc)
1457                 /*
1458                  * Need to add in further 32 bit options because with SS12
1459                  * the xarch=sparcvis option can be applied to 32 or 64
1460                  * bit, and so the translatation table (xtbl) cannot handle
1461                  * that.
1462                  */
1463                 newae(ctx->i_ae, "-mv8plus");
1464 #endif
1465                 break;
1466         case (SS12|M64):
1467                 break;
1468         default:
1469                 (void) fprintf(stderr,
1470                     "Incompatible -xarch= and/or -m32/-m64 options used.\n");
1471                 exit(2);
1472         }
1473 
1474         if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
1475             (ctx->i_flags & CW_F_SHADOW))
1476                 exit(0);
1477 
1478         if (model && !pic)
1479                 newae(ctx->i_ae, model);
1480         if (!nolibc)
1481                 newae(ctx->i_ae, "-lc");
1482         if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1483                 newae(ctx->i_ae, "-o");
1484                 newae(ctx->i_ae, ctx->i_discard);
1485         }
1486 }
1487 
1488 static void
1489 do_cc(cw_ictx_t *ctx)
1490 {
1491         int in_output = 0, seen_o = 0;
1492         cw_op_t op = CW_O_LINK;
1493         char *nameflag;
1494 
1495         if (ctx->i_flags & CW_F_PROG) {
1496                 newae(ctx->i_ae, "-V");
1497                 return;
1498         }
1499 
1500         if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1)
1501                 nomem();
1502 
1503         while (--ctx->i_oldargc > 0) {
1504                 char *arg = *++ctx->i_oldargv;
1505 
1506                 if (strncmp(arg, "-_CC=", 5) == 0) {
1507                         newae(ctx->i_ae, strchr(arg, '=') + 1);
1508                         continue;
1509                 }
1510 
1511                 if (*arg != '-') {
1512                         if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) {
1513                                 newae(ctx->i_ae, arg);
1514                         } else {
1515                                 in_output = 0;
1516                                 newae(ctx->i_ae, ctx->i_discard);
1517                         }
1518                         continue;
1519                 }
1520                 switch (*(arg + 1)) {
1521                 case '_':
1522                         if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) ||
1523                             (strncmp(arg, "-_cc=", 5) == 0) ||
1524                             (strncmp(arg, "-_sun=", 6) == 0)) {
1525                                 newae(ctx->i_ae, strchr(arg, '=') + 1);







1526                         }
1527                         break;
1528 
1529                 case 'V':
1530                         ctx->i_flags &= ~CW_F_ECHO;
1531                         newae(ctx->i_ae, arg);
1532                         break;
1533                 case 'o':
1534                         seen_o = 1;
1535                         if (strlen(arg) == 2) {
1536                                 in_output = 1;
1537                                 newae(ctx->i_ae, arg);
1538                         } else if (ctx->i_flags & CW_F_SHADOW) {
1539                                 newae(ctx->i_ae, "-o");
1540                                 newae(ctx->i_ae, ctx->i_discard);
1541                         } else {
1542                                 newae(ctx->i_ae, arg);
1543                         }
1544                         break;
1545                 case 'c':
1546                 case 'S':
1547                         if (strlen(arg) == 2)
1548                                 op = CW_O_COMPILE;
1549                         newae(ctx->i_ae, arg);
1550                         break;
1551                 case 'E':
1552                 case 'P':
1553                         if (strlen(arg) == 2)
1554                                 op = CW_O_PREPROCESS;
1555                 /*FALLTHROUGH*/
1556                 default:
1557                         newae(ctx->i_ae, arg);
1558                 }
1559         }
1560 
1561         free(nameflag);
1562 
1563         if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
1564             (ctx->i_flags & CW_F_SHADOW))
1565                 exit(0);
1566 
1567         if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1568                 newae(ctx->i_ae, "-o");
1569                 newae(ctx->i_ae, ctx->i_discard);
1570         }
1571 }
1572 
1573 static void
1574 prepctx(cw_ictx_t *ctx)
1575 {
1576         newae(ctx->i_ae, ctx->i_compiler->c_path);


1577 































1578         if (ctx->i_flags & CW_F_PROG) {
1579                 (void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ?
1580                     "shadow" : "primary", ctx->i_compiler->c_path);
1581                 (void) fflush(stdout);
1582         }
1583 
1584         if (!(ctx->i_flags & CW_F_XLATE))
1585                 return;
1586 
1587         switch (ctx->i_compiler->c_style) {
1588         case SUN:
1589                 do_cc(ctx);
1590                 break;
1591         case GNU:
1592                 do_gcc(ctx);
1593                 break;
1594         }
1595 }
1596 
1597 static int
1598 invoke(cw_ictx_t *ctx)
1599 {
1600         char **newargv;
1601         int ac;
1602         struct ae *a;
1603 
1604         if ((newargv = calloc(sizeof (*newargv), ctx->i_ae->ael_argc + 1)) ==
1605             NULL)
1606                 nomem();
1607 
1608         if (ctx->i_flags & CW_F_ECHO)
1609                 (void) fprintf(stderr, "+ ");
1610 
1611         for (ac = 0, a = ctx->i_ae->ael_head; a; a = a->ae_next, ac++) {
1612                 newargv[ac] = a->ae_arg;
1613                 if (ctx->i_flags & CW_F_ECHO)
1614                         (void) fprintf(stderr, "%s ", a->ae_arg);
1615                 if (a == ctx->i_ae->ael_tail)
1616                         break;
1617         }
1618 
1619         if (ctx->i_flags & CW_F_ECHO) {
1620                 (void) fprintf(stderr, "\n");
1621                 (void) fflush(stderr);
1622         }
1623 
1624         if (!(ctx->i_flags & CW_F_EXEC))
1625                 return (0);
1626 
1627         /*
1628          * We must fix up the environment here so that the dependency files are
1629          * not trampled by the shadow compiler. Also take care of GCC
1630          * environment variables that will throw off gcc. This assumes a primary
1631          * gcc.
1632          */
1633         if ((ctx->i_flags & CW_F_SHADOW) &&
1634             (unsetenv("SUNPRO_DEPENDENCIES") != 0 ||
1635             unsetenv("DEPENDENCIES_OUTPUT") != 0 ||
1636             unsetenv("GCC_ROOT") != 0)) {
1637                 (void) fprintf(stderr, "error: environment setup failed: %s\n",
1638                     strerror(errno));
1639                 return (-1);
1640         }
1641 
1642         (void) execv(newargv[0], newargv);
1643         warn("couldn't run %s", newargv[0]);
1644 
1645         return (-1);
1646 }
1647 
1648 static int
1649 reap(cw_ictx_t *ctx)
1650 {
1651         int status, ret = 0;
1652         char buf[1024];
1653         struct stat s;
1654 
1655         /*
1656          * Only wait for one specific child.
1657          */
1658         if (ctx->i_pid <= 0)
1659                 return (-1);
1660 
1661         do {
1662                 if (waitpid(ctx->i_pid, &status, 0) < 0) {
1663                         warn("cannot reap child");
1664                         return (-1);
1665                 }
1666                 if (status != 0) {
1667                         if (WIFSIGNALED(status)) {
1668                                 ret = -WTERMSIG(status);
1669                                 break;
1670                         } else if (WIFEXITED(status)) {
1671                                 ret = WEXITSTATUS(status);
1672                                 break;
1673                         }
1674                 }
1675         } while (!WIFEXITED(status) && !WIFSIGNALED(status));
1676 
1677         (void) unlink(ctx->i_discard);
1678 
1679         if (stat(ctx->i_stderr, &s) < 0) {
1680                 warn("stat failed on child cleanup");
1681                 return (-1);
1682         }
1683         if (s.st_size != 0) {
1684                 FILE *f;
1685 
1686                 if ((f = fopen(ctx->i_stderr, "r")) != NULL) {
1687                         while (fgets(buf, sizeof (buf), f))
1688                                 (void) fprintf(stderr, "%s", buf);
1689                         (void) fflush(stderr);
1690                         (void) fclose(f);
1691                 }
1692         }
1693         (void) unlink(ctx->i_stderr);
1694         free(ctx->i_stderr);
1695 
1696         /*
1697          * cc returns an error code when given -V; we want that to succeed.
1698          */
1699         if (ctx->i_flags & CW_F_PROG)
1700                 return (0);


1713          */
1714         if ((file = tempnam(NULL, ".cw")) == NULL) {
1715                 nomem();
1716                 return (-1);
1717         }
1718         (void) strlcpy(ctx->i_discard, file, MAXPATHLEN);
1719         (void) strlcat(ctx->i_discard, ".o", MAXPATHLEN);
1720         free(file);
1721 
1722         if ((ctx->i_stderr = tempnam(NULL, ".cw")) == NULL) {
1723                 nomem();
1724                 return (-1);
1725         }
1726 
1727         if ((ctx->i_pid = fork()) == 0) {
1728                 int fd;
1729 
1730                 (void) fclose(stderr);
1731                 if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL,
1732                     0666)) < 0) {
1733                         err(1, "open failed for standard error");

1734                 }
1735                 if (dup2(fd, 2) < 0) {
1736                         err(1, "dup2 failed for standard error");

1737                 }
1738                 if (fd != 2)
1739                         (void) close(fd);
1740                 if (freopen("/dev/fd/2", "w", stderr) == NULL) {
1741                         err(1, "freopen failed for /dev/fd/2");

1742                 }
1743 
1744                 prepctx(ctx);
1745                 exit(invoke(ctx));
1746         }
1747 
1748         if (ctx->i_pid < 0) {
1749                 err(1, "fork failed");

1750         }
1751 
1752         if (block)
1753                 return (reap(ctx));
1754 
1755         return (0);
1756 }
1757 
1758 static void
1759 parse_compiler(const char *spec, cw_compiler_t *compiler)
1760 {
1761         char *tspec, *token;
1762 
1763         if ((tspec = strdup(spec)) == NULL)
1764                 nomem();
1765 
1766         if ((token = strsep(&tspec, ",")) == NULL)
1767                 errx(1, "Compiler is missing a name: %s", spec);
1768         compiler->c_name = token;
1769 
1770         if ((token = strsep(&tspec, ",")) == NULL)
1771                 errx(1, "Compiler is missing a path: %s", spec);
1772         compiler->c_path = token;
1773 
1774         if ((token = strsep(&tspec, ",")) == NULL)
1775                 errx(1, "Compiler is missing a style: %s", spec);
1776 
1777         if ((strcasecmp(token, "gnu") == 0) ||
1778             (strcasecmp(token, "gcc") == 0))
1779                 compiler->c_style = GNU;
1780         else if ((strcasecmp(token, "sun") == 0) ||
1781             (strcasecmp(token, "cc") == 0))
1782                 compiler->c_style = SUN;
1783         else
1784                 errx(1, "unknown compiler style: %s", token);
1785 
1786         if (tspec != NULL)
1787                 errx(1, "Excess tokens in compiler: %s", spec);
1788 }
1789 
1790 int
1791 main(int argc, char **argv)
1792 {
1793         int ch;
1794         cw_compiler_t primary = { NULL, NULL, 0 };
1795         cw_compiler_t shadows[10];
1796         int nshadows = 0;
1797         int ret = 0;
1798         boolean_t do_serial = B_FALSE;
1799         boolean_t do_exec = B_FALSE;
1800         boolean_t vflg = B_FALSE;
1801         boolean_t Cflg = B_FALSE;
1802         boolean_t cflg = B_FALSE;
1803         boolean_t nflg = B_FALSE;
1804 
1805         cw_ictx_t *main_ctx;



1806 
1807         static struct option longopts[] = {
1808                 { "compiler", no_argument, NULL, 'c' },
1809                 { "noecho", no_argument, NULL, 'n' },
1810                 { "primary", required_argument, NULL, 'p' },
1811                 { "shadow", required_argument, NULL, 's' },
1812                 { "versions", no_argument, NULL, 'v' },
1813                 { NULL, 0, NULL, 0 },
1814         };
1815 
1816 
1817         if ((main_ctx = newictx()) == NULL)
1818                 nomem();
1819 
1820         while ((ch = getopt_long(argc, argv, "C", longopts, NULL)) != -1) {
1821                 switch (ch) {
1822                 case 'c':
1823                         cflg = B_TRUE;
1824                         break;
1825                 case 'C':
1826                         Cflg = B_TRUE;
1827                         break;
1828                 case 'n':
1829                         nflg = B_TRUE;
1830                         break;
1831                 case 'p':
1832                         if (primary.c_path != NULL) {
1833                                 warnx("Only one primary compiler may "
1834                                     "be specified");
1835                                 usage();
1836                         }
1837 
1838                         parse_compiler(optarg, &primary);
1839                         break;
1840                 case 's':
1841                         if (nshadows >= 10)
1842                                 errx(1, "May only use 10 shadows at "
1843                                     "the moment");
1844                         parse_compiler(optarg, &shadows[nshadows]);
1845                         nshadows++;
1846                         break;
1847                 case 'v':
1848                         vflg = B_TRUE;
1849                         break;
1850                 default:
1851                         (void) fprintf(stderr, "Did you forget '--'?\n");
1852                         usage();
1853                 }
1854         }
1855 
1856         if (primary.c_path == NULL) {
1857                 warnx("A primary compiler must be specified");
1858                 usage();
1859         }
1860 
1861         do_serial = (getenv("CW_SHADOW_SERIAL") == NULL) ? B_FALSE : B_TRUE;
1862         do_exec = (getenv("CW_NO_EXEC") == NULL) ? B_TRUE : B_FALSE;
1863 
1864         /* Leave room for argv[0] */
1865         argc -= (optind - 1);
1866         argv += (optind - 1);
1867 
1868         main_ctx->i_oldargc = argc;
1869         main_ctx->i_oldargv = argv;
1870         main_ctx->i_flags = CW_F_XLATE;
1871         if (nflg == 0)
1872                 main_ctx->i_flags |= CW_F_ECHO;
1873         if (do_exec)
1874                 main_ctx->i_flags |= CW_F_EXEC;
1875         if (Cflg)
1876                 main_ctx->i_flags |= CW_F_CXX;
1877         main_ctx->i_compiler = &primary;













1878 
1879         if (cflg) {
1880                 (void) fputs(primary.c_path, stdout);






1881         }
1882 
1883         if (vflg) {
1884                 (void) printf("cw version %s\n", CW_VERSION);







1885                 (void) fflush(stdout);
1886                 main_ctx->i_flags &= ~CW_F_ECHO;
1887                 main_ctx->i_flags |= CW_F_PROG | CW_F_EXEC;


1888                 do_serial = 1;
1889         }
1890 
1891         ret |= exec_ctx(main_ctx, do_serial);

1892 
1893         for (int i = 0; i < nshadows; i++) {
1894                 int r;
1895                 cw_ictx_t *shadow_ctx;
1896 
1897                 if ((shadow_ctx = newictx()) == NULL)
1898                         nomem();
1899 
1900                 memcpy(shadow_ctx, main_ctx, sizeof (cw_ictx_t));
1901 
1902                 shadow_ctx->i_flags |= CW_F_SHADOW;
1903                 shadow_ctx->i_compiler = &shadows[i];
1904 
1905                 r = exec_ctx(shadow_ctx, do_serial);
1906                 if (r == 0) {
1907                         shadow_ctx->i_next = main_ctx->i_next;
1908                         main_ctx->i_next = shadow_ctx;
1909                 }
1910                 ret |= r;
1911         }
1912 
1913         if (!do_serial) {
1914                 cw_ictx_t *next = main_ctx;
1915                 while (next != NULL) {
1916                         cw_ictx_t *toreap = next;
1917                         next = next->i_next;
1918                         ret |= reap(toreap);
1919                 }
1920         }
1921 
1922         return (ret);
1923 }