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: Toomas Soome <tsoome@me.com>
Reviewed? by: Yuri Pankov <yuripv@yuripv.net>
Reviewed? by: Robert Mustacchi <rm@joyent.com>
Reviewed? by: Jason King <jason.king@joyent.com>

   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);


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


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


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


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

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



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





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

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


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


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


1759          */
1760         if ((file = tempnam(NULL, ".cw")) == NULL) {
1761                 nomem();
1762                 return (-1);
1763         }
1764         (void) strlcpy(ctx->i_discard, file, MAXPATHLEN);
1765         (void) strlcat(ctx->i_discard, ".o", MAXPATHLEN);
1766         free(file);
1767 
1768         if ((ctx->i_stderr = tempnam(NULL, ".cw")) == NULL) {
1769                 nomem();
1770                 return (-1);
1771         }
1772 
1773         if ((ctx->i_pid = fork()) == 0) {
1774                 int fd;
1775 
1776                 (void) fclose(stderr);
1777                 if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL,
1778                     0666)) < 0) {
1779                         cw_perror("open failed for standard error");
1780                         exit(1);
1781                 }
1782                 if (dup2(fd, 2) < 0) {
1783                         cw_perror("dup2 failed for standard error");
1784                         exit(1);
1785                 }
1786                 if (fd != 2)
1787                         (void) close(fd);
1788                 if (freopen("/dev/fd/2", "w", stderr) == NULL) {
1789                         cw_perror("freopen failed for /dev/fd/2");
1790                         exit(1);
1791                 }
1792                 prepctx(ctx);
1793                 exit(invoke(ctx));
1794         }
1795 
1796         if (ctx->i_pid < 0) {
1797                 cw_perror("fork failed");
1798                 return (1);
1799         }
1800 
1801         if (block)
1802                 return (reap(ctx));
1803 
1804         return (0);
1805 }
1806 


































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

















1815 
1816         if ((progname = strrchr(argv[0], '/')) == NULL)
1817                 progname = argv[0];
1818         else
1819                 progname++;
1820 
1821         if (ctx == NULL || ctx_shadow == NULL)
1822                 nomem();
1823 
1824         ctx->i_flags = CW_F_ECHO|CW_F_XLATE;
1825 
1826         /*
1827          * Figure out where to get our tools from.  This depends on
1828          * the environment variables set at run time.
1829          */
1830         if ((dir = getenv("SPRO_VROOT")) != NULL) {
1831                 (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
1832                     "%s/bin", dir);
1833         } else if ((dir = getenv("SPRO_ROOT")) != NULL) {
1834                 (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
1835                     "%s/SS12/bin", dir);
1836         } else if ((dir = getenv("BUILD_TOOLS")) != NULL) {
1837                 (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
1838                     "%s/SUNWspro/SS12/bin", dir);



1839         }
1840 
1841         if ((dir = getenv("GCC_ROOT")) != NULL) {
1842                 (void) snprintf(default_dir[CW_C_GCC], MAXPATHLEN,
1843                     "%s/bin", dir);

















1844         }
1845 
1846         do_shadow = (getenv("CW_NO_SHADOW") ? 0 : 1);
1847         do_serial = (getenv("CW_SHADOW_SERIAL") ? 1 : 0);
1848 
1849         if (getenv("CW_NO_EXEC") == NULL)
1850                 ctx->i_flags |= CW_F_EXEC;
1851 
1852         /*
1853          * The first argument must be one of "-_cc", "-_gcc", "-_CC", or "-_g++"
1854          */
1855         if (argc == 1)
1856                 usage();
1857         argc--;
1858         argv++;
1859         if (strcmp(argv[0], "-_cc") == 0) {
1860                 ctx->i_compiler = CW_C_CC;
1861         } else if (strcmp(argv[0], "-_gcc") == 0) {
1862                 ctx->i_compiler = CW_C_GCC;
1863         } else if (strcmp(argv[0], "-_CC") == 0) {
1864                 ctx->i_compiler = CW_C_CC;
1865                 ctx->i_flags |= CW_F_CXX;
1866         } else if (strcmp(argv[0], "-_g++") == 0) {
1867                 ctx->i_compiler = CW_C_GCC;
1868                 ctx->i_flags |= CW_F_CXX;
1869         } else {
1870                 /* assume "-_gcc" by default */
1871                 argc++;
1872                 argv--;
1873                 ctx->i_compiler = CW_C_GCC;
1874         }
1875 
1876         /*
1877          * -_compiler - tell us the path to the primary compiler only
1878          */
1879         if (argc > 1 && strcmp(argv[1], "-_compiler") == 0) {
1880                 ctx->i_flags &= ~CW_F_XLATE;
1881                 prepctx(ctx);
1882                 (void) printf("%s\n", ctx->i_ae->ael_head->ae_arg);
1883                 return (0);












1884         }
1885 
1886         /*
1887          * -_versions - tell us the cw version, paths to all compilers, and
1888          *              ask each for its version if we know how.
1889          */
1890         if (argc > 1 && strcmp(argv[1], "-_versions") == 0) {
1891                 (void) printf("cw version %s", CW_VERSION);
1892                 if (!do_shadow)
1893                         (void) printf(" (SHADOW MODE DISABLED)");
1894                 (void) printf("\n");
1895                 (void) fflush(stdout);
1896                 ctx->i_flags &= ~CW_F_ECHO;
1897                 ctx->i_flags |= CW_F_PROG|CW_F_EXEC;
1898                 argc--;
1899                 argv++;
1900                 do_serial = 1;
1901         }
1902 
1903         ctx->i_oldargc = argc;
1904         ctx->i_oldargv = argv;







1905 
1906         ret |= exec_ctx(ctx, do_serial);

1907 
1908         if (do_shadow) {
1909                 (void) memcpy(ctx_shadow, ctx, sizeof (cw_ictx_t));
1910                 ctx_shadow->i_flags |= CW_F_SHADOW;
1911                 ret |= exec_ctx(ctx_shadow, 1);
1912         }
1913 
1914         if (!do_serial)
1915                 ret |= reap(ctx);
1916 
1917         return (ret);
1918 }
   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         cw_compiler_t   *i_compiler;
 346         struct aelist   *i_ae;
 347         uint32_t        i_flags;
 348         int             i_oldargc;
 349         char            **i_oldargv;
 350         pid_t           i_pid;
 351         char            i_discard[MAXPATHLEN];
 352         char            *i_stderr;
 353 } cw_ictx_t;
 354 
 355 /*
 356  * Status values to indicate which Studio compiler and associated
 357  * flags are being used.
 358  */
 359 #define M32             0x01    /* -m32 - only on Studio 12 */
 360 #define M64             0x02    /* -m64 - only on Studio 12 */
 361 #define SS11            0x100   /* Studio 11 */
 362 #define SS12            0x200   /* Studio 12 */
 363 
 364 #define TRANS_ENTRY     5
 365 /*
 366  * Translation table definition for the -xarch= flag. The "x_arg"
 367  * value is translated into the appropriate gcc flags according
 368  * to the values in x_trans[n]. The x_flags indicates what compiler
 369  * is being used and what flags have been set via the use of
 370  * "x_arg".
 371  */
 372 typedef struct xarch_table {
 373         char    *x_arg;
 374         int     x_flags;
 375         char    *x_trans[TRANS_ENTRY];
 376 } xarch_table_t;
 377 
 378 /*
 379  * The translation table for the -xarch= flag used in the Studio compilers.
 380  */
 381 static const xarch_table_t xtbl[] = {
 382 #if defined(__x86)
 383         { "generic",    SS11, {NULL} },
 384         { "generic64",  (SS11|M64), { "-m64", "-mtune=opteron" } },
 385         { "amd64",      (SS11|M64), { "-m64", "-mtune=opteron" } },
 386         { "386",        SS11,   { "-march=i386" } },
 387         { "pentium_pro", SS11,  { "-march=pentiumpro" } },
 388         { "sse",        SS11, { "-msse", "-mfpmath=sse" } },
 389         { "sse2",       SS11, { "-msse2", "-mfpmath=sse" } },
 390 #elif defined(__sparc)
 391         { "generic",    (SS11|M32), { "-m32", "-mcpu=v8" } },
 392         { "generic64",  (SS11|M64), { "-m64", "-mcpu=v9" } },
 393         { "v8",         (SS11|M32), { "-m32", "-mcpu=v8", "-mno-v8plus" } },
 394         { "v8plus",     (SS11|M32), { "-m32", "-mcpu=v9", "-mv8plus" } },
 395         { "v8plusa",    (SS11|M32), { "-m32", "-mcpu=ultrasparc", "-mv8plus",
 396                         "-mvis" } },
 397         { "v8plusb",    (SS11|M32), { "-m32", "-mcpu=ultrasparc3", "-mv8plus",
 398                         "-mvis" } },
 399         { "v9",         (SS11|M64), { "-m64", "-mcpu=v9" } },
 400         { "v9a",        (SS11|M64), { "-m64", "-mcpu=ultrasparc", "-mvis" } },
 401         { "v9b",        (SS11|M64), { "-m64", "-mcpu=ultrasparc3", "-mvis" } },
 402         { "sparc",      SS12, { "-mcpu=v9", "-mv8plus" } },
 403         { "sparcvis",   SS12, { "-mcpu=ultrasparc", "-mvis" } },
 404         { "sparcvis2",  SS12, { "-mcpu=ultrasparc3", "-mvis" } }
 405 #endif
 406 };
 407 
 408 static int xtbl_size = sizeof (xtbl) / sizeof (xarch_table_t);
 409 


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


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
















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


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


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

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


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





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


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

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

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


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







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

































1578 
1579         if (ctx->i_flags & CW_F_PROG) {
1580                 (void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ?
1581                     "shadow" : "primary", ctx->i_compiler->c_path);
1582                 (void) fflush(stdout);
1583         }
1584 
1585         if (!(ctx->i_flags & CW_F_XLATE))
1586                 return;
1587 
1588         switch (ctx->i_compiler->c_style) {
1589         case SUN:
1590                 do_cc(ctx);
1591                 break;
1592         case GNU:
1593                 do_gcc(ctx);
1594                 break;
1595         }
1596 }
1597 
1598 static int
1599 invoke(cw_ictx_t *ctx)
1600 {
1601         char **newargv;
1602         int ac;
1603         struct ae *a;
1604 
1605         if ((newargv = calloc(sizeof (*newargv), ctx->i_ae->ael_argc + 1)) ==
1606             NULL)
1607                 nomem();
1608 
1609         if (ctx->i_flags & CW_F_ECHO)
1610                 (void) fprintf(stderr, "+ ");
1611 
1612         for (ac = 0, a = ctx->i_ae->ael_head; a; a = a->ae_next, ac++) {


1621                 (void) fprintf(stderr, "\n");
1622                 (void) fflush(stderr);
1623         }
1624 
1625         if (!(ctx->i_flags & CW_F_EXEC))
1626                 return (0);
1627 
1628         /*
1629          * We must fix up the environment here so that the
1630          * dependency files are not trampled by the shadow compiler.
1631          */
1632         if ((ctx->i_flags & CW_F_SHADOW) &&
1633             (unsetenv("SUNPRO_DEPENDENCIES") != 0 ||
1634             unsetenv("DEPENDENCIES_OUTPUT") != 0)) {
1635                 (void) fprintf(stderr, "error: environment setup failed: %s\n",
1636                     strerror(errno));
1637                 return (-1);
1638         }
1639 
1640         (void) execv(newargv[0], newargv);
1641         warn("couldn't run %s", newargv[0]);
1642 
1643         return (-1);
1644 }
1645 
1646 static int
1647 reap(cw_ictx_t *ctx)
1648 {
1649         int status, ret = 0;
1650         char buf[1024];
1651         struct stat s;
1652 
1653         /*
1654          * Only wait for one specific child.
1655          */
1656         if (ctx->i_pid <= 0)
1657                 return (-1);
1658 
1659         do {
1660                 if (waitpid(ctx->i_pid, &status, 0) < 0) {
1661                         warn("cannot reap child");
1662                         return (-1);
1663                 }
1664                 if (status != 0) {
1665                         if (WIFSIGNALED(status)) {
1666                                 ret = -WTERMSIG(status);
1667                                 break;
1668                         } else if (WIFEXITED(status)) {
1669                                 ret = WEXITSTATUS(status);
1670                                 break;
1671                         }
1672                 }
1673         } while (!WIFEXITED(status) && !WIFSIGNALED(status));
1674 
1675         (void) unlink(ctx->i_discard);
1676 
1677         if (stat(ctx->i_stderr, &s) < 0) {
1678                 warn("stat failed on child cleanup");
1679                 return (-1);
1680         }
1681         if (s.st_size != 0) {
1682                 FILE *f;
1683 
1684                 if ((f = fopen(ctx->i_stderr, "r")) != NULL) {
1685                         while (fgets(buf, sizeof (buf), f))
1686                                 (void) fprintf(stderr, "%s", buf);
1687                         (void) fflush(stderr);
1688                         (void) fclose(f);
1689                 }
1690         }
1691         (void) unlink(ctx->i_stderr);
1692         free(ctx->i_stderr);
1693 
1694         /*
1695          * cc returns an error code when given -V; we want that to succeed.
1696          */
1697         if (ctx->i_flags & CW_F_PROG)
1698                 return (0);


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

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

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

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

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




1815 
1816         if ((main_ctx = newictx()) == NULL)
1817                 nomem();
1818 

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                         if (parse_compiler(optarg, &primary) != 0)
1839                                 errx(1, "Couldn't parse %s as a compiler spec",
1840                                     optarg);
1841                         break;
1842                 case 's':
1843                         if (nshadows >= 10)
1844                                 errx(1, "May only use 10 shadows at "
1845                                     "the moment");
1846                         if (parse_compiler(optarg, &shadows[nshadows]) != 0)
1847                                 errx(1, "Couldn't parse %s as a compiler spec",
1848                                     optarg);
1849                         nshadows++;
1850                         break;
1851                 case 'v':
1852                         vflg = B_TRUE;
1853                         break;
1854                 default:
1855                         (void) fprintf(stderr, "Did you forget '--'?\n");
1856                         usage();
1857                 }
1858         }
1859 
1860         if (primary.c_path == NULL) {
1861                 warnx("A primary compiler must be specified");








1862                 usage();

















1863         }
1864 
1865         do_serial = (getenv("CW_SHADOW_SERIAL") == NULL) ? B_FALSE : B_TRUE;
1866         do_exec = (getenv("CW_NO_EXEC") == NULL) ? B_TRUE : B_FALSE;
1867 
1868         /* Leave room for argv[0] */
1869         argc -= (optind - 1);
1870         argv += (optind - 1);
1871 
1872         main_ctx->i_oldargc = argc;
1873         main_ctx->i_oldargv = argv;
1874         main_ctx->i_flags = CW_F_XLATE;
1875         if (nflg == 0)
1876                 main_ctx->i_flags |= CW_F_ECHO;
1877         if (do_exec)
1878                 main_ctx->i_flags |= CW_F_EXEC;
1879         if (Cflg)
1880                 main_ctx->i_flags |= CW_F_CXX;
1881         main_ctx->i_compiler = &primary;
1882 
1883         if (cflg) {
1884                 (void) fputs(primary.c_path, stdout);
1885         }
1886 
1887         if (vflg) {
1888                 (void) printf("cw version %s\n", CW_VERSION);







1889                 (void) fflush(stdout);
1890                 main_ctx->i_flags &= ~CW_F_ECHO;
1891                 main_ctx->i_flags |= CW_F_PROG|CW_F_EXEC;


1892                 do_serial = 1;
1893         }
1894 
1895         ret |= exec_ctx(main_ctx, do_serial);
1896 
1897         for (int i = 0; i < nshadows; i++) {
1898                 cw_ictx_t *shadow_ctx;
1899 
1900                 if ((shadow_ctx = newictx()) == NULL)
1901                         nomem();
1902 
1903                 memcpy(shadow_ctx, main_ctx, sizeof (cw_ictx_t));
1904 
1905                 shadow_ctx->i_flags |= CW_F_SHADOW;
1906                 shadow_ctx->i_compiler = &shadows[i];
1907 
1908                 /* XXX: Would be nice to run these parallel, too */
1909                 ret |= exec_ctx(shadow_ctx, 1);


1910         }
1911 
1912         if (!do_serial)
1913                 ret |= reap(main_ctx);
1914 
1915         return (ret);
1916 }