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>

Split Close
Expand all
Collapse all
          --- old/usr/src/tools/cw/cw.c
          +++ new/usr/src/tools/cw/cw.c
        1 +
   1    2  /*
   2    3   * CDDL HEADER START
   3    4   *
   4    5   * The contents of this file are subject to the terms of the
   5    6   * Common Development and Distribution License (the "License").
   6    7   * You may not use this file except in compliance with the License.
   7    8   *
   8    9   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9   10   * or http://www.opensolaris.org/os/licensing.
  10   11   * See the License for the specific language governing permissions
↓ open down ↓ 2 lines elided ↑ open up ↑
  13   14   * When distributing Covered Code, include this CDDL HEADER in each
  14   15   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   16   * If applicable, add the following below this CDDL HEADER, with the
  16   17   * fields enclosed by brackets "[]" replaced with your own identifying
  17   18   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   19   *
  19   20   * CDDL HEADER END
  20   21   */
  21   22  
  22   23  /*
  23      - * Copyright 2011, Richard Lowe.
       24 + * Copyright 2018, Richard Lowe.
  24   25   */
  25   26  /*
  26   27   * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  27   28   * Use is subject to license terms.
  28   29   */
  29   30  
  30   31  /*
  31   32   * Wrapper for the GNU C compiler to make it accept the Sun C compiler
  32   33   * arguments where possible.
  33   34   *
  34   35   * Since the translation is inexact, this is something of a work-in-progress.
  35   36   *
  36   37   */
  37   38  
  38   39  /* If you modify this file, you must increment CW_VERSION */
  39      -#define CW_VERSION      "1.30"
       40 +#define CW_VERSION      "2.0"
  40   41  
  41   42  /*
  42   43   * -#           Verbose mode
  43   44   * -###         Show compiler commands built by driver, no compilation
  44   45   * -A<name[(tokens)]>   Preprocessor predicate assertion
  45   46   * -B<[static|dynamic]> Specify dynamic or static binding
  46   47   * -C           Prevent preprocessor from removing comments
  47   48   * -c           Compile only - produce .o files, suppress linking
  48   49   * -cg92        Alias for -xtarget=ss1000
  49   50   * -D<name[=token]>     Associate name with token as if by #define
↓ open down ↓ 234 lines elided ↑ open up ↑
 284  285   * -xunroll=n                   error
 285  286   * -W0,-xdbggen=no%usedonly     -fno-eliminate-unused-debug-symbols
 286  287   *                              -fno-eliminate-unused-debug-types
 287  288   * -Y<c>,<dir>                  error
 288  289   * -YA,<dir>                    error
 289  290   * -YI,<dir>                    -nostdinc -I<dir>
 290  291   * -YP,<dir>                    error
 291  292   * -YS,<dir>                    error
 292  293   */
 293  294  
 294      -#include <stdio.h>
 295      -#include <sys/types.h>
 296      -#include <unistd.h>
 297      -#include <string.h>
 298      -#include <stdlib.h>
 299  295  #include <ctype.h>
 300      -#include <fcntl.h>
      296 +#include <err.h>
 301  297  #include <errno.h>
 302      -#include <stdarg.h>
 303      -#include <sys/utsname.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 +
 304  305  #include <sys/param.h>
 305      -#include <sys/isa_defs.h>
 306      -#include <sys/wait.h>
 307  306  #include <sys/stat.h>
      307 +#include <sys/types.h>
      308 +#include <sys/utsname.h>
      309 +#include <sys/wait.h>
 308  310  
 309  311  #define CW_F_CXX        0x01
 310  312  #define CW_F_SHADOW     0x02
 311  313  #define CW_F_EXEC       0x04
 312  314  #define CW_F_ECHO       0x08
 313  315  #define CW_F_XLATE      0x10
 314  316  #define CW_F_PROG       0x20
 315  317  
 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  318  typedef enum cw_op {
 340  319          CW_O_NONE = 0,
 341  320          CW_O_PREPROCESS,
 342  321          CW_O_COMPILE,
 343  322          CW_O_LINK
 344  323  } cw_op_t;
 345  324  
 346  325  struct aelist {
 347  326          struct ae {
 348  327                  struct ae *ae_next;
 349  328                  char *ae_arg;
 350  329          } *ael_head, *ael_tail;
 351  330          int ael_argc;
 352  331  };
 353  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 +
 354  344  typedef struct cw_ictx {
 355      -        cw_compiler_t   i_compiler;
      345 +        cw_compiler_t   *i_compiler;
 356  346          struct aelist   *i_ae;
 357  347          uint32_t        i_flags;
 358  348          int             i_oldargc;
 359  349          char            **i_oldargv;
 360  350          pid_t           i_pid;
 361  351          char            i_discard[MAXPATHLEN];
 362  352          char            *i_stderr;
 363  353  } cw_ictx_t;
 364  354  
 365  355  /*
↓ open down ↓ 17 lines elided ↑ open up ↑
 383  373          char    *x_arg;
 384  374          int     x_flags;
 385  375          char    *x_trans[TRANS_ENTRY];
 386  376  } xarch_table_t;
 387  377  
 388  378  /*
 389  379   * The translation table for the -xarch= flag used in the Studio compilers.
 390  380   */
 391  381  static const xarch_table_t xtbl[] = {
 392  382  #if defined(__x86)
 393      -        { "generic",    SS11 },
      383 +        { "generic",    SS11, {NULL} },
 394  384          { "generic64",  (SS11|M64), { "-m64", "-mtune=opteron" } },
 395  385          { "amd64",      (SS11|M64), { "-m64", "-mtune=opteron" } },
 396  386          { "386",        SS11,   { "-march=i386" } },
 397  387          { "pentium_pro", SS11,  { "-march=pentiumpro" } },
 398  388          { "sse",        SS11, { "-msse", "-mfpmath=sse" } },
 399  389          { "sse2",       SS11, { "-msse2", "-mfpmath=sse" } },
 400  390  #elif defined(__sparc)
 401  391          { "generic",    (SS11|M32), { "-m32", "-mcpu=v8" } },
 402  392          { "generic64",  (SS11|M64), { "-m64", "-mcpu=v9" } },
 403  393          { "v8",         (SS11|M32), { "-m32", "-mcpu=v8", "-mno-v8plus" } },
↓ open down ↓ 6 lines elided ↑ open up ↑
 410  400          { "v9a",        (SS11|M64), { "-m64", "-mcpu=ultrasparc", "-mvis" } },
 411  401          { "v9b",        (SS11|M64), { "-m64", "-mcpu=ultrasparc3", "-mvis" } },
 412  402          { "sparc",      SS12, { "-mcpu=v9", "-mv8plus" } },
 413  403          { "sparcvis",   SS12, { "-mcpu=ultrasparc", "-mvis" } },
 414  404          { "sparcvis2",  SS12, { "-mcpu=ultrasparc3", "-mvis" } }
 415  405  #endif
 416  406  };
 417  407  
 418  408  static int xtbl_size = sizeof (xtbl) / sizeof (xarch_table_t);
 419  409  
 420      -static const char *progname;
 421      -
 422  410  static const char *xchip_tbl[] = {
 423  411  #if defined(__x86)
 424  412          "386",          "-mtune=i386", NULL,
 425  413          "486",          "-mtune=i486", NULL,
 426  414          "pentium",      "-mtune=pentium", NULL,
 427  415          "pentium_pro",  "-mtune=pentiumpro", NULL,
 428  416  #elif defined(__sparc)
 429  417          "super",        "-mtune=supersparc", NULL,
 430  418          "ultra",        "-mtune=ultrasparc", NULL,
 431  419          "ultra3",       "-mtune=ultrasparc3", NULL,
↓ open down ↓ 25 lines elided ↑ open up ↑
 457  445          "no%appl",      "-mno-app-regs", NULL,
 458  446          "float",        "-mfpu", NULL,
 459  447          "no%float",     "-mno-fpu", NULL,
 460  448  #endif  /* __sparc */
 461  449          NULL,           NULL
 462  450  };
 463  451  
 464  452  static void
 465  453  nomem(void)
 466  454  {
 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));
      455 +        errx(1, "out of memory");
 484  456  }
 485  457  
 486  458  static void
 487  459  newae(struct aelist *ael, const char *arg)
 488  460  {
 489  461          struct ae *ae;
 490  462  
 491  463          if ((ae = calloc(sizeof (*ae), 1)) == NULL)
 492  464                  nomem();
 493  465          ae->ae_arg = strdup(arg);
↓ open down ↓ 14 lines elided ↑ open up ↑
 508  480                          free(ctx);
 509  481                          return (NULL);
 510  482                  }
 511  483  
 512  484          return (ctx);
 513  485  }
 514  486  
 515  487  static void
 516  488  error(const char *arg)
 517  489  {
 518      -        (void) fprintf(stderr,
 519      -            "%s: error: mapping failed at or near arg '%s'\n", progname, arg);
 520      -        exit(2);
      490 +        errx(2, "error: mapping failed at or near arg '%s'\n", arg);
 521  491  }
 522  492  
 523  493  /*
 524  494   * Add the current favourite set of warnings to the gcc invocation.
 525  495   */
 526  496  static void
 527  497  warnings(struct aelist *h)
 528  498  {
 529  499          static int warningsonce;
 530  500  
↓ open down ↓ 13 lines elided ↑ open up ↑
 544  514  {
 545  515          if (level >= 2) {
 546  516                  newae(h, "-fno-strict-aliasing");
 547  517                  newae(h, "-fno-unit-at-a-time");
 548  518                  newae(h, "-fno-optimize-sibling-calls");
 549  519          }
 550  520  }
 551  521  
 552  522  /* ARGSUSED */
 553  523  static void
 554      -Xamode(struct aelist *h)
      524 +Xamode(struct aelist __unused *h)
 555  525  {
 556  526  }
 557  527  
 558  528  static void
 559  529  Xcmode(struct aelist *h)
 560  530  {
 561  531          static int xconce;
 562  532  
 563  533          if (xconce++)
 564  534                  return;
↓ open down ↓ 10 lines elided ↑ open up ↑
 575  545          if (xsonce++)
 576  546                  return;
 577  547  
 578  548          newae(h, "-traditional");
 579  549          newae(h, "-traditional-cpp");
 580  550  }
 581  551  
 582  552  static void
 583  553  usage()
 584  554  {
      555 +        extern char *__progname;
 585  556          (void) fprintf(stderr,
 586      -            "usage: %s { -_cc | -_gcc | -_CC | -_g++ } [ -_compiler | ... ]\n",
 587      -            progname);
      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");
 588  564          exit(2);
 589  565  }
 590  566  
 591  567  static int
 592  568  xlate_xtb(struct aelist *h, const char *xarg)
 593  569  {
 594  570          int     i, j;
 595  571  
 596  572          for (i = 0; i < xtbl_size; i++) {
 597  573                  if (strcmp(xtbl[i].x_arg, xarg) == 0)
↓ open down ↓ 38 lines elided ↑ open up ↑
 636  612  }
 637  613  
 638  614  static void
 639  615  do_gcc(cw_ictx_t *ctx)
 640  616  {
 641  617          int c;
 642  618          int pic = 0, nolibc = 0;
 643  619          int in_output = 0, seen_o = 0, c_files = 0;
 644  620          cw_op_t op = CW_O_LINK;
 645  621          char *model = NULL;
      622 +        char *nameflag;
 646  623          int     mflag = 0;
 647  624  
 648  625          if (ctx->i_flags & CW_F_PROG) {
 649  626                  newae(ctx->i_ae, "--version");
 650  627                  return;
 651  628          }
 652  629  
 653  630          newae(ctx->i_ae, "-fident");
 654  631          newae(ctx->i_ae, "-finline");
 655  632          newae(ctx->i_ae, "-fno-inline-functions");
↓ open down ↓ 16 lines elided ↑ open up ↑
 672  649          /*
 673  650           * This is needed because 'u' is defined
 674  651           * under a conditional on 'sun'.  Should
 675  652           * probably just remove the conditional,
 676  653           * or make it be dependent on '__sun'.
 677  654           *
 678  655           * -Dunix is also missing in enhanced ANSI mode
 679  656           */
 680  657          newae(ctx->i_ae, "-D__sun");
 681  658  
      659 +        if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1)
      660 +                nomem();
      661 +
 682  662          /*
 683  663           * Walk the argument list, translating as we go ..
 684  664           */
 685      -
 686  665          while (--ctx->i_oldargc > 0) {
 687  666                  char *arg = *++ctx->i_oldargv;
 688  667                  size_t arglen = strlen(arg);
 689  668  
 690  669                  if (*arg == '-') {
 691  670                          arglen--;
 692  671                  } else {
 693  672                          /*
 694  673                           * Discard inline files that gcc doesn't grok
 695  674                           */
↓ open down ↓ 15 lines elided ↑ open up ↑
 711  690                           */
 712  691                          if ((ctx->i_flags & CW_F_SHADOW) && in_output)
 713  692                                  newae(ctx->i_ae, ctx->i_discard);
 714  693                          else
 715  694                                  newae(ctx->i_ae, arg);
 716  695                          in_output = 0;
 717  696                          continue;
 718  697                  }
 719  698  
 720  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 +                        }
 721  704                          if (strncmp(arg, "-compat=", 8) == 0) {
 722  705                                  /* discard -compat=4 and -compat=5 */
 723  706                                  continue;
 724  707                          }
 725  708                          if (strcmp(arg, "-Qoption") == 0) {
 726  709                                  /* discard -Qoption and its two arguments */
 727  710                                  if (ctx->i_oldargc < 3)
 728  711                                          error(arg);
 729  712                                  ctx->i_oldargc -= 2;
 730  713                                  ctx->i_oldargv += 2;
↓ open down ↓ 34 lines elided ↑ open up ↑
 765  748                          if (strcmp(arg, "-cg92") == 0) {
 766  749                                  mflag |= xlate_xtb(ctx->i_ae, "v8");
 767  750                                  xlate(ctx->i_ae, "super", xchip_tbl);
 768  751                                  continue;
 769  752                          }
 770  753  #endif  /* __sparc */
 771  754                  }
 772  755  
 773  756                  switch ((c = arg[1])) {
 774  757                  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);
      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 +                        }
 785  763                          break;
 786  764                  case '#':
 787  765                          if (arglen == 1) {
 788  766                                  newae(ctx->i_ae, "-v");
 789  767                                  break;
 790  768                          }
 791  769                          error(arg);
 792  770                          break;
 793  771                  case 'g':
 794  772                          newae(ctx->i_ae, "-gdwarf-2");
↓ open down ↓ 621 lines elided ↑ open up ↑
1416 1394                  case 'Q':
1417 1395                          /*
1418 1396                           * We could map -Qy into -Wl,-Qy etc.
1419 1397                           */
1420 1398                  default:
1421 1399                          error(arg);
1422 1400                          break;
1423 1401                  }
1424 1402          }
1425 1403  
     1404 +        free(nameflag);
     1405 +
1426 1406          if (c_files > 1 && (ctx->i_flags & CW_F_SHADOW) &&
1427 1407              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);
     1408 +                errx(2, "multiple source files are "
     1409 +                    "allowed only with -E or -P");
1431 1410          }
1432 1411  
1433 1412          /*
1434 1413           * Make sure that we do not have any unintended interactions between
1435 1414           * the xarch options passed in and the version of the Studio compiler
1436 1415           * used.
1437 1416           */
1438 1417          if ((mflag & (SS11|SS12)) == (SS11|SS12)) {
1439      -                (void) fprintf(stderr,
     1418 +                errx(2,
1440 1419                      "Conflicting \"-xarch=\" flags (both Studio 11 and 12)\n");
1441      -                exit(2);
1442 1420          }
1443 1421  
1444 1422          switch (mflag) {
1445 1423          case 0:
1446 1424                  /* FALLTHROUGH */
1447 1425          case M32:
1448 1426  #if defined(__sparc)
1449 1427                  /*
1450 1428                   * Only -m32 is defined and so put in the missing xarch
1451 1429                   * translation.
↓ open down ↓ 34 lines elided ↑ open up ↑
1486 1464                  newae(ctx->i_ae, "-mv8plus");
1487 1465  #endif
1488 1466                  break;
1489 1467          case (SS12|M64):
1490 1468                  break;
1491 1469          default:
1492 1470                  (void) fprintf(stderr,
1493 1471                      "Incompatible -xarch= and/or -m32/-m64 options used.\n");
1494 1472                  exit(2);
1495 1473          }
1496      -        if (op == CW_O_LINK && (ctx->i_flags & CW_F_SHADOW))
     1474 +
     1475 +        if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
     1476 +            (ctx->i_flags & CW_F_SHADOW))
1497 1477                  exit(0);
1498 1478  
1499 1479          if (model && !pic)
1500 1480                  newae(ctx->i_ae, model);
1501 1481          if (!nolibc)
1502 1482                  newae(ctx->i_ae, "-lc");
1503 1483          if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1504 1484                  newae(ctx->i_ae, "-o");
1505 1485                  newae(ctx->i_ae, ctx->i_discard);
1506 1486          }
1507 1487  }
1508 1488  
1509 1489  static void
1510 1490  do_cc(cw_ictx_t *ctx)
1511 1491  {
1512 1492          int in_output = 0, seen_o = 0;
1513 1493          cw_op_t op = CW_O_LINK;
     1494 +        char *nameflag;
1514 1495  
1515 1496          if (ctx->i_flags & CW_F_PROG) {
1516 1497                  newae(ctx->i_ae, "-V");
1517 1498                  return;
1518 1499          }
1519 1500  
     1501 +        if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1)
     1502 +                nomem();
     1503 +
1520 1504          while (--ctx->i_oldargc > 0) {
1521 1505                  char *arg = *++ctx->i_oldargv;
1522 1506  
     1507 +                if (strncmp(arg, "-_CC=", 5) == 0) {
     1508 +                        newae(ctx->i_ae, strchr(arg, '=') + 1);
     1509 +                        continue;
     1510 +                }
     1511 +
1523 1512                  if (*arg != '-') {
1524 1513                          if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) {
1525 1514                                  newae(ctx->i_ae, arg);
1526 1515                          } else {
1527 1516                                  in_output = 0;
1528 1517                                  newae(ctx->i_ae, ctx->i_discard);
1529 1518                          }
1530 1519                          continue;
1531 1520                  }
1532 1521                  switch (*(arg + 1)) {
1533 1522                  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);
     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);
1545 1527                          }
1546 1528                          break;
     1529 +
1547 1530                  case 'V':
1548 1531                          ctx->i_flags &= ~CW_F_ECHO;
1549 1532                          newae(ctx->i_ae, arg);
1550 1533                          break;
1551 1534                  case 'o':
1552 1535                          seen_o = 1;
1553 1536                          if (strlen(arg) == 2) {
1554 1537                                  in_output = 1;
1555 1538                                  newae(ctx->i_ae, arg);
1556 1539                          } else if (ctx->i_flags & CW_F_SHADOW) {
↓ open down ↓ 12 lines elided ↑ open up ↑
1569 1552                  case 'E':
1570 1553                  case 'P':
1571 1554                          if (strlen(arg) == 2)
1572 1555                                  op = CW_O_PREPROCESS;
1573 1556                  /*FALLTHROUGH*/
1574 1557                  default:
1575 1558                          newae(ctx->i_ae, arg);
1576 1559                  }
1577 1560          }
1578 1561  
     1562 +        free(nameflag);
     1563 +
1579 1564          if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
1580 1565              (ctx->i_flags & CW_F_SHADOW))
1581 1566                  exit(0);
1582 1567  
1583 1568          if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1584 1569                  newae(ctx->i_ae, "-o");
1585 1570                  newae(ctx->i_ae, ctx->i_discard);
1586 1571          }
1587 1572  }
1588 1573  
1589 1574  static void
1590 1575  prepctx(cw_ictx_t *ctx)
1591 1576  {
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);
     1577 +        newae(ctx->i_ae, ctx->i_compiler->c_path);
1626 1578  
1627 1579          if (ctx->i_flags & CW_F_PROG) {
1628 1580                  (void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ?
1629      -                    "shadow" : "primary", program);
     1581 +                    "shadow" : "primary", ctx->i_compiler->c_path);
1630 1582                  (void) fflush(stdout);
1631 1583          }
1632 1584  
1633 1585          if (!(ctx->i_flags & CW_F_XLATE))
1634 1586                  return;
1635 1587  
1636      -        switch (CC(ctx)) {
1637      -        case CW_C_CC:
     1588 +        switch (ctx->i_compiler->c_style) {
     1589 +        case SUN:
1638 1590                  do_cc(ctx);
1639 1591                  break;
1640      -        case CW_C_GCC:
     1592 +        case GNU:
1641 1593                  do_gcc(ctx);
1642 1594                  break;
1643 1595          }
1644 1596  }
1645 1597  
1646 1598  static int
1647 1599  invoke(cw_ictx_t *ctx)
1648 1600  {
1649 1601          char **newargv;
1650 1602          int ac;
↓ open down ↓ 28 lines elided ↑ open up ↑
1679 1631           */
1680 1632          if ((ctx->i_flags & CW_F_SHADOW) &&
1681 1633              (unsetenv("SUNPRO_DEPENDENCIES") != 0 ||
1682 1634              unsetenv("DEPENDENCIES_OUTPUT") != 0)) {
1683 1635                  (void) fprintf(stderr, "error: environment setup failed: %s\n",
1684 1636                      strerror(errno));
1685 1637                  return (-1);
1686 1638          }
1687 1639  
1688 1640          (void) execv(newargv[0], newargv);
1689      -        cw_perror("couldn't run %s", newargv[0]);
     1641 +        warn("couldn't run %s", newargv[0]);
1690 1642  
1691 1643          return (-1);
1692 1644  }
1693 1645  
1694 1646  static int
1695 1647  reap(cw_ictx_t *ctx)
1696 1648  {
1697 1649          int status, ret = 0;
1698 1650          char buf[1024];
1699 1651          struct stat s;
1700 1652  
1701 1653          /*
1702 1654           * Only wait for one specific child.
1703 1655           */
1704 1656          if (ctx->i_pid <= 0)
1705 1657                  return (-1);
1706 1658  
1707 1659          do {
1708 1660                  if (waitpid(ctx->i_pid, &status, 0) < 0) {
1709      -                        cw_perror("cannot reap child");
     1661 +                        warn("cannot reap child");
1710 1662                          return (-1);
1711 1663                  }
1712 1664                  if (status != 0) {
1713 1665                          if (WIFSIGNALED(status)) {
1714 1666                                  ret = -WTERMSIG(status);
1715 1667                                  break;
1716 1668                          } else if (WIFEXITED(status)) {
1717 1669                                  ret = WEXITSTATUS(status);
1718 1670                                  break;
1719 1671                          }
1720 1672                  }
1721 1673          } while (!WIFEXITED(status) && !WIFSIGNALED(status));
1722 1674  
1723 1675          (void) unlink(ctx->i_discard);
1724 1676  
1725 1677          if (stat(ctx->i_stderr, &s) < 0) {
1726      -                cw_perror("stat failed on child cleanup");
     1678 +                warn("stat failed on child cleanup");
1727 1679                  return (-1);
1728 1680          }
1729 1681          if (s.st_size != 0) {
1730 1682                  FILE *f;
1731 1683  
1732 1684                  if ((f = fopen(ctx->i_stderr, "r")) != NULL) {
1733 1685                          while (fgets(buf, sizeof (buf), f))
1734 1686                                  (void) fprintf(stderr, "%s", buf);
1735 1687                          (void) fflush(stderr);
1736 1688                          (void) fclose(f);
↓ open down ↓ 32 lines elided ↑ open up ↑
1769 1721                  nomem();
1770 1722                  return (-1);
1771 1723          }
1772 1724  
1773 1725          if ((ctx->i_pid = fork()) == 0) {
1774 1726                  int fd;
1775 1727  
1776 1728                  (void) fclose(stderr);
1777 1729                  if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL,
1778 1730                      0666)) < 0) {
1779      -                        cw_perror("open failed for standard error");
1780      -                        exit(1);
     1731 +                        err(1, "open failed for standard error");
1781 1732                  }
1782 1733                  if (dup2(fd, 2) < 0) {
1783      -                        cw_perror("dup2 failed for standard error");
1784      -                        exit(1);
     1734 +                        err(1, "dup2 failed for standard error");
1785 1735                  }
1786 1736                  if (fd != 2)
1787 1737                          (void) close(fd);
1788 1738                  if (freopen("/dev/fd/2", "w", stderr) == NULL) {
1789      -                        cw_perror("freopen failed for /dev/fd/2");
1790      -                        exit(1);
     1739 +                        err(1, "freopen failed for /dev/fd/2");
1791 1740                  }
1792 1741                  prepctx(ctx);
1793 1742                  exit(invoke(ctx));
1794 1743          }
1795 1744  
1796 1745          if (ctx->i_pid < 0) {
1797      -                cw_perror("fork failed");
1798      -                return (1);
     1746 +                err(1, "fork failed");
1799 1747          }
1800 1748  
1801 1749          if (block)
1802 1750                  return (reap(ctx));
1803 1751  
1804 1752          return (0);
1805 1753  }
1806 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 +
1807 1789  int
1808 1790  main(int argc, char **argv)
1809 1791  {
1810      -        cw_ictx_t *ctx = newictx();
1811      -        cw_ictx_t *ctx_shadow = newictx();
1812      -        const char *dir;
1813      -        int do_serial, do_shadow;
     1792 +        int ch;
     1793 +        cw_compiler_t primary = { NULL, NULL, 0 };
     1794 +        cw_compiler_t shadows[10];
     1795 +        int nshadows = 0;
1814 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 +        };
1815 1814  
1816      -        if ((progname = strrchr(argv[0], '/')) == NULL)
1817      -                progname = argv[0];
1818      -        else
1819      -                progname++;
1820 1815  
1821      -        if (ctx == NULL || ctx_shadow == NULL)
     1816 +        if ((main_ctx = newictx()) == NULL)
1822 1817                  nomem();
1823 1818  
1824      -        ctx->i_flags = CW_F_ECHO|CW_F_XLATE;
1825 1819  
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);
     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 +                        }
1848 1837  
1849      -        if (getenv("CW_NO_EXEC") == NULL)
1850      -                ctx->i_flags |= CW_F_EXEC;
     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 +        }
1851 1859  
1852      -        /*
1853      -         * The first argument must be one of "-_cc", "-_gcc", "-_CC", or "-_g++"
1854      -         */
1855      -        if (argc == 1)
     1860 +        if (primary.c_path == NULL) {
     1861 +                warnx("A primary compiler must be specified");
1856 1862                  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 1863          }
1875 1864  
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);
     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);
1884 1885          }
1885 1886  
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");
     1887 +        if (vflg) {
     1888 +                (void) printf("cw version %s\n", CW_VERSION);
1895 1889                  (void) fflush(stdout);
1896      -                ctx->i_flags &= ~CW_F_ECHO;
1897      -                ctx->i_flags |= CW_F_PROG|CW_F_EXEC;
1898      -                argc--;
1899      -                argv++;
     1890 +                main_ctx->i_flags &= ~CW_F_ECHO;
     1891 +                main_ctx->i_flags |= CW_F_PROG|CW_F_EXEC;
1900 1892                  do_serial = 1;
1901 1893          }
1902 1894  
1903      -        ctx->i_oldargc = argc;
1904      -        ctx->i_oldargv = argv;
     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));
1905 1904  
1906      -        ret |= exec_ctx(ctx, do_serial);
     1905 +                shadow_ctx->i_flags |= CW_F_SHADOW;
     1906 +                shadow_ctx->i_compiler = &shadows[i];
1907 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);
     1908 +                /* XXX: Would be nice to run these parallel, too */
     1909 +                ret |= exec_ctx(shadow_ctx, 1);
1912 1910          }
1913 1911  
1914 1912          if (!do_serial)
1915      -                ret |= reap(ctx);
     1913 +                ret |= reap(main_ctx);
1916 1914  
1917 1915          return (ret);
1918 1916  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX