Print this page
cw: give cw(1onbld) a new interface allowing for multiple arbitrary shadows

   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.


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


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


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
















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


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


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

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


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





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


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

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

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


1474                 exit(2);
1475         }
1476         if (op == CW_O_LINK && (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->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->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->path);
1582                 (void) fflush(stdout);
1583         }
1584 
1585         if (!(ctx->i_flags & CW_F_XLATE))
1586                 return;
1587 
1588         switch (ctx->i_compiler->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                 err(1, "out of memory");
1762 
1763         if ((token = strsep(&tspec, ",")) == NULL)
1764                 errx(1, "Compiler is missing a name: %s", spec);
1765         compiler->name = token;
1766 
1767         if ((token = strsep(&tspec, ",")) == NULL)
1768                 errx(1, "Compiler is missing a path: %s", spec);
1769         compiler->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->style = GNU;
1777         else if ((strcasecmp(token, "sun") == 0) ||
1778             (strcasecmp(token, "cc") == 0))
1779                 compiler->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 = newictx();
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         while ((ch = getopt_long(argc, argv, "C", longopts, NULL)) != -1) {
1816                 switch (ch) {
1817                 case 'c':
1818                         cflg = B_TRUE;
1819                         break;
1820                 case 'C':
1821                         Cflg = B_TRUE;
1822                         break;
1823                 case 'n':
1824                         nflg = B_TRUE;
1825                         break;
1826                 case 'p':
1827                         if (primary.path != NULL) {
1828                                 warnx("Only one primary compiler may be specified");
1829                                 usage();
1830                         }
1831 
1832                         if (parse_compiler(optarg, &primary) != 0)
1833                                 errx(1, "Couldn't parse %s as a compiler spec", optarg);
1834                         break;
1835                 case 's':
1836                         if (nshadows >= 10)
1837                                 errx(1, "May only use 10 shadows at the moment");
1838                         if (parse_compiler(optarg, &shadows[nshadows]) != 0)
1839                                 errx(1, "Couldn't parse %s as a compiler spec", optarg);
1840                         nshadows++;
1841                         break;
1842                 case 'v':
1843                         vflg = B_TRUE;
1844                         break;
1845                 default:
1846                         fprintf(stderr, "Did you forget '--'?\n");
1847                         usage();
1848                 }
1849         }
1850 
1851         if (primary.path == NULL) {
1852                 warnx("A primary compiler must be specified");


1853                 usage();

















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







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


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


1904         }
1905 
1906         if (!do_serial)
1907                 ret |= reap(main_ctx);
1908 
1909         return (ret);
1910 }