Print this page
Review from Robert, and nits
Review from Yuri

Split Close
Expand all
Collapse all
          --- old/usr/src/tools/cw/cw.c
          +++ new/usr/src/tools/cw/cw.c
↓ open down ↓ 327 lines elided ↑ open up ↑
 328  328                  char *ae_arg;
 329  329          } *ael_head, *ael_tail;
 330  330          int ael_argc;
 331  331  };
 332  332  
 333  333  typedef enum {
 334  334          GNU,
 335  335          SUN
 336  336  } compiler_style_t;
 337  337  
 338      -#define COMPILER_STYLE(comp) (comp->style == SUN ? "cc" : "gcc")
 339      -
 340  338  typedef struct {
 341      -        char *name;
 342      -        char *path;
 343      -        compiler_style_t style;
      339 +        char *c_name;
      340 +        char *c_path;
      341 +        compiler_style_t c_style;
 344  342  } cw_compiler_t;
 345  343  
 346  344  typedef struct cw_ictx {
 347  345          cw_compiler_t   *i_compiler;
 348  346          struct aelist   *i_ae;
 349  347          uint32_t        i_flags;
 350  348          int             i_oldargc;
 351  349          char            **i_oldargv;
 352  350          pid_t           i_pid;
 353  351          char            i_discard[MAXPATHLEN];
↓ open down ↓ 93 lines elided ↑ open up ↑
 447  445          "no%appl",      "-mno-app-regs", NULL,
 448  446          "float",        "-mfpu", NULL,
 449  447          "no%float",     "-mno-fpu", NULL,
 450  448  #endif  /* __sparc */
 451  449          NULL,           NULL
 452  450  };
 453  451  
 454  452  static void
 455  453  nomem(void)
 456  454  {
 457      -        (void) errx(1, "out of memory");
      455 +        errx(1, "out of memory");
 458  456  }
 459  457  
 460  458  static void
 461  459  newae(struct aelist *ael, const char *arg)
 462  460  {
 463  461          struct ae *ae;
 464  462  
 465  463          if ((ae = calloc(sizeof (*ae), 1)) == NULL)
 466  464                  nomem();
 467  465          ae->ae_arg = strdup(arg);
↓ open down ↓ 14 lines elided ↑ open up ↑
 482  480                          free(ctx);
 483  481                          return (NULL);
 484  482                  }
 485  483  
 486  484          return (ctx);
 487  485  }
 488  486  
 489  487  static void
 490  488  error(const char *arg)
 491  489  {
 492      -         errx(2, "error: mapping failed at or near arg '%s'\n", arg);
      490 +        errx(2, "error: mapping failed at or near arg '%s'\n", arg);
 493  491  }
 494  492  
 495  493  /*
 496  494   * Add the current favourite set of warnings to the gcc invocation.
 497  495   */
 498  496  static void
 499  497  warnings(struct aelist *h)
 500  498  {
 501  499          static int warningsonce;
 502  500  
↓ open down ↓ 13 lines elided ↑ open up ↑
 516  514  {
 517  515          if (level >= 2) {
 518  516                  newae(h, "-fno-strict-aliasing");
 519  517                  newae(h, "-fno-unit-at-a-time");
 520  518                  newae(h, "-fno-optimize-sibling-calls");
 521  519          }
 522  520  }
 523  521  
 524  522  /* ARGSUSED */
 525  523  static void
 526      -Xamode(struct aelist *h __attribute__((__unused__)))
      524 +Xamode(struct aelist __unused *h)
 527  525  {
 528  526  }
 529  527  
 530  528  static void
 531  529  Xcmode(struct aelist *h)
 532  530  {
 533  531          static int xconce;
 534  532  
 535  533          if (xconce++)
 536  534                  return;
↓ open down ↓ 114 lines elided ↑ open up ↑
 651  649          /*
 652  650           * This is needed because 'u' is defined
 653  651           * under a conditional on 'sun'.  Should
 654  652           * probably just remove the conditional,
 655  653           * or make it be dependent on '__sun'.
 656  654           *
 657  655           * -Dunix is also missing in enhanced ANSI mode
 658  656           */
 659  657          newae(ctx->i_ae, "-D__sun");
 660  658  
 661      -        if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->name) == -1)
      659 +        if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1)
 662  660                  nomem();
 663  661  
 664  662          /*
 665  663           * Walk the argument list, translating as we go ..
 666  664           */
 667  665          while (--ctx->i_oldargc > 0) {
 668  666                  char *arg = *++ctx->i_oldargv;
 669  667                  size_t arglen = strlen(arg);
 670  668  
 671  669                  if (*arg == '-') {
↓ open down ↓ 728 lines elided ↑ open up ↑
1400 1398                  default:
1401 1399                          error(arg);
1402 1400                          break;
1403 1401                  }
1404 1402          }
1405 1403  
1406 1404          free(nameflag);
1407 1405  
1408 1406          if (c_files > 1 && (ctx->i_flags & CW_F_SHADOW) &&
1409 1407              op != CW_O_PREPROCESS) {
1410      -                (void) errx(2, "multiple source files are "
     1408 +                errx(2, "multiple source files are "
1411 1409                      "allowed only with -E or -P");
1412 1410          }
1413 1411  
1414 1412          /*
1415 1413           * Make sure that we do not have any unintended interactions between
1416 1414           * the xarch options passed in and the version of the Studio compiler
1417 1415           * used.
1418 1416           */
1419 1417          if ((mflag & (SS11|SS12)) == (SS11|SS12)) {
1420      -                (void) errx(2,
     1418 +                errx(2,
1421 1419                      "Conflicting \"-xarch=\" flags (both Studio 11 and 12)\n");
1422 1420          }
1423 1421  
1424 1422          switch (mflag) {
1425 1423          case 0:
1426 1424                  /* FALLTHROUGH */
1427 1425          case M32:
1428 1426  #if defined(__sparc)
1429 1427                  /*
1430 1428                   * Only -m32 is defined and so put in the missing xarch
↓ open down ↓ 60 lines elided ↑ open up ↑
1491 1489  {
1492 1490          int in_output = 0, seen_o = 0;
1493 1491          cw_op_t op = CW_O_LINK;
1494 1492          char *nameflag;
1495 1493  
1496 1494          if (ctx->i_flags & CW_F_PROG) {
1497 1495                  newae(ctx->i_ae, "-V");
1498 1496                  return;
1499 1497          }
1500 1498  
1501      -        if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->name) == -1)
     1499 +        if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1)
1502 1500                  nomem();
1503 1501  
1504 1502          while (--ctx->i_oldargc > 0) {
1505 1503                  char *arg = *++ctx->i_oldargv;
1506 1504  
1507 1505                  if (strncmp(arg, "-_CC=", 5) == 0) {
1508 1506                          newae(ctx->i_ae, strchr(arg, '=') + 1);
1509 1507                          continue;
1510 1508                  }
1511 1509  
↓ open down ↓ 55 lines elided ↑ open up ↑
1567 1565  
1568 1566          if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1569 1567                  newae(ctx->i_ae, "-o");
1570 1568                  newae(ctx->i_ae, ctx->i_discard);
1571 1569          }
1572 1570  }
1573 1571  
1574 1572  static void
1575 1573  prepctx(cw_ictx_t *ctx)
1576 1574  {
1577      -        newae(ctx->i_ae, ctx->i_compiler->path);
     1575 +        newae(ctx->i_ae, ctx->i_compiler->c_path);
1578 1576  
1579 1577          if (ctx->i_flags & CW_F_PROG) {
1580 1578                  (void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ?
1581      -                    "shadow" : "primary", ctx->i_compiler->path);
     1579 +                    "shadow" : "primary", ctx->i_compiler->c_path);
1582 1580                  (void) fflush(stdout);
1583 1581          }
1584 1582  
1585 1583          if (!(ctx->i_flags & CW_F_XLATE))
1586 1584                  return;
1587 1585  
1588      -        switch (ctx->i_compiler->style) {
     1586 +        switch (ctx->i_compiler->c_style) {
1589 1587          case SUN:
1590 1588                  do_cc(ctx);
1591 1589                  break;
1592 1590          case GNU:
1593 1591                  do_gcc(ctx);
1594 1592                  break;
1595 1593          }
1596 1594  }
1597 1595  
1598 1596  static int
↓ open down ↓ 152 lines elided ↑ open up ↑
1751 1749  
1752 1750          return (0);
1753 1751  }
1754 1752  
1755 1753  static int
1756 1754  parse_compiler(const char *spec, cw_compiler_t *compiler)
1757 1755  {
1758 1756          char *tspec, *token;
1759 1757  
1760 1758          if ((tspec = strdup(spec)) == NULL)
1761      -                err(1, "out of memory");
     1759 +                nomem();
1762 1760  
1763 1761          if ((token = strsep(&tspec, ",")) == NULL)
1764 1762                  errx(1, "Compiler is missing a name: %s", spec);
1765      -        compiler->name = token;
     1763 +        compiler->c_name = token;
1766 1764  
1767 1765          if ((token = strsep(&tspec, ",")) == NULL)
1768 1766                  errx(1, "Compiler is missing a path: %s", spec);
1769      -        compiler->path = token;
     1767 +        compiler->c_path = token;
1770 1768  
1771 1769          if ((token = strsep(&tspec, ",")) == NULL)
1772 1770                  errx(1, "Compiler is missing a style: %s", spec);
1773 1771  
1774 1772          if ((strcasecmp(token, "gnu") == 0) ||
1775 1773              (strcasecmp(token, "gcc") == 0))
1776      -                compiler->style = GNU;
     1774 +                compiler->c_style = GNU;
1777 1775          else if ((strcasecmp(token, "sun") == 0) ||
1778 1776              (strcasecmp(token, "cc") == 0))
1779      -                compiler->style = SUN;
     1777 +                compiler->c_style = SUN;
1780 1778          else
1781 1779                  errx(1, "unknown compiler style: %s", token);
1782 1780  
1783 1781          if (tspec != NULL)
1784 1782                  errx(1, "Excess tokens in compiler: %s", spec);
1785 1783  
1786 1784          return (0);
1787 1785  }
1788 1786  
1789 1787  int
↓ open down ↓ 4 lines elided ↑ open up ↑
1794 1792          cw_compiler_t shadows[10];
1795 1793          int nshadows = 0;
1796 1794          int ret = 0;
1797 1795          boolean_t do_serial = B_FALSE;
1798 1796          boolean_t do_exec = B_FALSE;
1799 1797          boolean_t vflg = B_FALSE;
1800 1798          boolean_t Cflg = B_FALSE;
1801 1799          boolean_t cflg = B_FALSE;
1802 1800          boolean_t nflg = B_FALSE;
1803 1801  
1804      -        cw_ictx_t *main_ctx = newictx();
     1802 +        cw_ictx_t *main_ctx;
1805 1803  
1806 1804          static struct option longopts[] = {
1807 1805                  { "compiler", no_argument, NULL, 'c' },
1808      -                { "noecho", no_argument, NULL ,'n' },
     1806 +                { "noecho", no_argument, NULL, 'n' },
1809 1807                  { "primary", required_argument, NULL, 'p' },
1810 1808                  { "shadow", required_argument, NULL, 's' },
1811 1809                  { "versions", no_argument, NULL, 'v' },
1812 1810                  { NULL, 0, NULL, 0 },
1813 1811          };
1814 1812  
     1813 +
     1814 +        if ((main_ctx = newictx()) == NULL)
     1815 +                nomem();
     1816 +
     1817 +
1815 1818          while ((ch = getopt_long(argc, argv, "C", longopts, NULL)) != -1) {
1816 1819                  switch (ch) {
1817 1820                  case 'c':
1818 1821                          cflg = B_TRUE;
1819 1822                          break;
1820 1823                  case 'C':
1821 1824                          Cflg = B_TRUE;
1822 1825                          break;
1823 1826                  case 'n':
1824 1827                          nflg = B_TRUE;
1825 1828                          break;
1826 1829                  case 'p':
1827      -                        if (primary.path != NULL) {
1828      -                                warnx("Only one primary compiler may be specified");
     1830 +                        if (primary.c_path != NULL) {
     1831 +                                warnx("Only one primary compiler may "
     1832 +                                    "be specified");
1829 1833                                  usage();
1830 1834                          }
1831 1835  
1832 1836                          if (parse_compiler(optarg, &primary) != 0)
1833      -                                errx(1, "Couldn't parse %s as a compiler spec", optarg);
     1837 +                                errx(1, "Couldn't parse %s as a compiler spec",
     1838 +                                    optarg);
1834 1839                          break;
1835 1840                  case 's':
1836 1841                          if (nshadows >= 10)
1837      -                                errx(1, "May only use 10 shadows at the moment");
     1842 +                                errx(1, "May only use 10 shadows at "
     1843 +                                    "the moment");
1838 1844                          if (parse_compiler(optarg, &shadows[nshadows]) != 0)
1839      -                                errx(1, "Couldn't parse %s as a compiler spec", optarg);
     1845 +                                errx(1, "Couldn't parse %s as a compiler spec",
     1846 +                                    optarg);
1840 1847                          nshadows++;
1841 1848                          break;
1842 1849                  case 'v':
1843 1850                          vflg = B_TRUE;
1844 1851                          break;
1845 1852                  default:
1846      -                        fprintf(stderr, "Did you forget '--'?\n");
     1853 +                        (void) fprintf(stderr, "Did you forget '--'?\n");
1847 1854                          usage();
1848 1855                  }
1849 1856          }
1850 1857  
1851      -        if (primary.path == NULL) {
     1858 +        if (primary.c_path == NULL) {
1852 1859                  warnx("A primary compiler must be specified");
1853 1860                  usage();
1854 1861          }
1855 1862  
1856 1863          do_serial = (getenv("CW_SHADOW_SERIAL") == NULL) ? B_FALSE : B_TRUE;
1857 1864          do_exec = (getenv("CW_NO_EXEC") == NULL) ? B_TRUE : B_FALSE;
1858 1865  
1859 1866          /* Leave room for argv[0] */
1860 1867          argc -= (optind - 1);
1861 1868          argv += (optind - 1);
1862 1869  
1863      -        if (main_ctx == NULL)
1864      -                nomem();
1865      -
1866 1870          main_ctx->i_oldargc = argc;
1867 1871          main_ctx->i_oldargv = argv;
1868 1872          main_ctx->i_flags = CW_F_XLATE;
1869 1873          if (nflg == 0)
1870 1874                  main_ctx->i_flags |= CW_F_ECHO;
1871 1875          if (do_exec)
1872 1876                  main_ctx->i_flags |= CW_F_EXEC;
1873 1877          if (Cflg)
1874 1878                  main_ctx->i_flags |= CW_F_CXX;
1875 1879          main_ctx->i_compiler = &primary;
1876 1880  
1877 1881          if (cflg) {
1878      -                fputs(primary.path, stdout);
     1882 +                (void) fputs(primary.c_path, stdout);
1879 1883          }
1880 1884  
1881 1885          if (vflg) {
1882 1886                  (void) printf("cw version %s\n", CW_VERSION);
1883 1887                  (void) fflush(stdout);
1884 1888                  main_ctx->i_flags &= ~CW_F_ECHO;
1885 1889                  main_ctx->i_flags |= CW_F_PROG|CW_F_EXEC;
1886 1890                  do_serial = 1;
1887 1891          }
1888 1892  
1889 1893          ret |= exec_ctx(main_ctx, do_serial);
1890 1894  
1891 1895          for (int i = 0; i < nshadows; i++) {
1892      -                cw_ictx_t *shadow_ctx = newictx();
     1896 +                cw_ictx_t *shadow_ctx;
1893 1897  
1894      -                if (shadow_ctx == NULL)
     1898 +                if ((shadow_ctx = newictx()) == NULL)
1895 1899                          nomem();
1896 1900  
1897 1901                  memcpy(shadow_ctx, main_ctx, sizeof (cw_ictx_t));
1898 1902  
1899 1903                  shadow_ctx->i_flags |= CW_F_SHADOW;
1900 1904                  shadow_ctx->i_compiler = &shadows[i];
1901 1905  
1902 1906                  /* XXX: Would be nice to run these parallel, too */
1903 1907                  ret |= exec_ctx(shadow_ctx, 1);
1904 1908          }
1905 1909  
1906 1910          if (!do_serial)
1907 1911                  ret |= reap(main_ctx);
1908 1912  
1909 1913          return (ret);
1910 1914  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX