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 * Copyright 2019 Joyent, Inc. 31 */ 32 33 /* 34 * Wrapper for the GNU C compiler to make it accept the Sun C compiler 35 * arguments where possible. 36 * 37 * Since the translation is inexact, this is something of a work-in-progress. 38 * 39 */ 40 41 /* If you modify this file, you must increment CW_VERSION */ 42 #define CW_VERSION "6.1" 43 44 /* 45 * -# Verbose mode 46 * -### Show compiler commands built by driver, no compilation 47 * -A<name[(tokens)]> Preprocessor predicate assertion 48 * -C Prevent preprocessor from removing comments 49 * -c Compile only - produce .o files, suppress linking 50 * -cg92 Alias for -xtarget=ss1000 51 * -D<name[=token]> Associate name with token as if by #define 52 * -d[y|n] dynamic [-dy] or static [-dn] option to linker 53 * -E Compile source through preprocessor only, output to stdout 54 * -erroff=<t> Suppress warnings specified by tags t(%none, %all, <tag list>) 55 * -errtags=<a> Display messages with tags a(no, yes) 56 * -errwarn=<t> Treats warnings specified by tags t(%none, %all, <tag list>) 57 * as errors 58 * -fast Optimize using a selection of options 59 * -fd Report old-style function definitions and declarations 60 * -fnonstd Initialize floating-point hardware to non-standard preferences 61 * -fns[=<yes|no>] Select non-standard floating point mode 62 * -fprecision=<p> Set FP rounding precision mode p(single, double, extended) 63 * -fround=<r> Select the IEEE rounding mode in effect at startup 64 * -fsimple[=<n>] Select floating-point optimization preferences <n> 65 * -fsingle Use single-precision arithmetic (-Xt and -Xs modes only) 66 * -ftrap=<t> Select floating-point trapping mode in effect at startup 67 * -fstore force floating pt. values to target precision on assignment 68 * -g Compile for debugging 69 * -H Print path name of each file included during compilation 70 * -h <name> Assign <name> to generated dynamic shared library 71 * -I<dir> Add <dir> to preprocessor #include file search path 72 * -i Passed to linker to ignore any LD_LIBRARY_PATH setting 73 * -keeptmp Keep temporary files created during compilation 74 * -L<dir> Pass to linker to add <dir> to the library search path 75 * -l<name> Link with library lib<name>.a or lib<name>.so 76 * -mc Remove duplicate strings from .comment section of output files 77 * -mr Remove all strings from .comment section of output files 78 * -mr,"string" Remove all strings and append "string" to .comment section 79 * -mt Specify options needed when compiling multi-threaded code 80 * -native Find available processor, generate code accordingly 81 * -nofstore Do not force floating pt. values to target precision 82 * on assignment 83 * -norunpath Do not build in a runtime path for shared libraries 84 * -O Use default optimization level (-xO2 or -xO3. Check man page.) 85 * -o <outputfile> Set name of output file to <outputfile> 86 * -P Compile source through preprocessor only, output to .i file 87 * -p Compile for profiling with prof 88 * -Q[y|n] Emit/don't emit identification info to output file 89 * -R<dir[:dir]> Build runtime search path list into executable 90 * -S Compile and only generate assembly code (.s) 91 * -s Strip symbol table from the executable file 92 * -t Turn off duplicate symbol warnings when linking 93 * -U<name> Delete initial definition of preprocessor symbol <name> 94 * -V Report version number of each compilation phase 95 * -v Do stricter semantic checking 96 * -W<c>,<arg> Pass <arg> to specified component <c> (a,l,m,p,0,2,h,i,u) 97 * -w Suppress compiler warning messages 98 * -Xa Compile assuming ANSI C conformance, allow K & R extensions 99 * (default mode) 100 * -Xs Compile assuming (pre-ANSI) K & R C style code 101 * -Xt Compile assuming K & R conformance, allow ANSI C 102 * -xarch=<a> Specify target architecture instruction set 103 * -xbuiltin[=<b>] When profitable inline, or substitute intrinisic functions 104 * for system functions, b={%all,%none} 105 * -xCC Accept C++ style comments 106 * -xchip=<c> Specify the target processor for use by the optimizer 107 * -xcode=<c> Generate different code for forming addresses 108 * -xcrossfile[=<n>] Enable optimization and inlining across source files, 109 * n={0|1} 110 * -xe Perform only syntax/semantic checking, no code generation 111 * -xF Compile for later mapfile reordering or unused section 112 * elimination 113 * -xhelp=<f> Display on-line help information f(flags, readme, errors) 114 * -xildoff Cancel -xildon 115 * -xildon Enable use of the incremental linker, ild 116 * -xinline=[<a>,...,<a>] Attempt inlining of specified user routines, 117 * <a>={%auto,func,no%func} 118 * -xlibmieee Force IEEE 754 return values for math routines in 119 * exceptional cases 120 * -xlibmil Inline selected libm math routines for optimization 121 * -xlic_lib=sunperf Link in the Sun supplied performance libraries 122 * -xlicinfo Show license server information 123 * -xmaxopt=[off,1,2,3,4,5] maximum optimization level allowed on #pragma opt 124 * -xO<n> Generate optimized code (n={1|2|3|4|5}) 125 * -xP Print prototypes for function definitions 126 * -xprofile=<p> Collect data for a profile or use a profile to optimize 127 * <p>={{collect,use}[:<path>],tcov} 128 * -xregs=<r> Control register allocation 129 * -xs Allow debugging without object (.o) files 130 * -xsb Compile for use with the WorkShop source browser 131 * -xsbfast Generate only WorkShop source browser info, no compilation 132 * -xsfpconst Represent unsuffixed floating point constants as single 133 * precision 134 * -xspace Do not do optimizations that increase code size 135 * -xstrconst Place string literals into read-only data segment 136 * -xtarget=<t> Specify target system for optimization 137 * -xtemp=<dir> Set directory for temporary files to <dir> 138 * -xtime Report the execution time for each compilation phase 139 * -xunroll=n Enable unrolling loops n times where possible 140 * -Y<c>,<dir> Specify <dir> for location of component <c> (a,l,m,p,0,h,i,u) 141 * -YA,<dir> Change default directory searched for components 142 * -YI,<dir> Change default directory searched for include files 143 * -YP,<dir> Change default directory for finding libraries files 144 * -YS,<dir> Change default directory for startup object files 145 */ 146 147 /* 148 * Translation table: 149 */ 150 /* 151 * -# -v 152 * -### error 153 * -A<name[(tokens)]> pass-thru 154 * -B<[static|dynamic]> pass-thru (syntax error for anything else) 155 * -C pass-thru 156 * -c pass-thru 157 * -cg92 -m32 -mcpu=v8 -mtune=supersparc (SPARC only) 158 * -D<name[=token]> pass-thru 159 * -E pass-thru 160 * -erroff=E_EMPTY_TRANSLATION_UNIT ignore 161 * -errtags=%all -Wall 162 * -errwarn=%all -Werror else -Wno-error 163 * -fast error 164 * -fd error 165 * -fnonstd error 166 * -fns[=<yes|no>] error 167 * -fprecision=<p> error 168 * -fround=<r> error 169 * -fsimple[=<n>] error 170 * -fsingle[=<n>] error 171 * -ftrap=<t> error 172 * -fstore error 173 * -g pass-thru 174 * -H pass-thru 175 * -I<dir> pass-thru 176 * -i pass-thru 177 * -keeptmp -save-temps 178 * -L<dir> pass-thru 179 * -l<name> pass-thru 180 * -mc error 181 * -mr error 182 * -mr,"string" error 183 * -mt -D_REENTRANT 184 * -native error 185 * -nofstore error 186 * -nolib -nodefaultlibs 187 * -norunpath ignore 188 * -O -O1 (Check the man page to be certain) 189 * -o <outputfile> pass-thru 190 * -P -E -o filename.i (or error) 191 * -p pass-thru 192 * -Q[y|n] error 193 * -R<dir[:dir]> pass-thru 194 * -S pass-thru 195 * -U<name> pass-thru 196 * -V --version 197 * -v -Wall 198 * -Wa,<arg> pass-thru 199 * -Wp,<arg> pass-thru except -xc99=<a> 200 * -Wl,<arg> pass-thru 201 * -W{m,0,2,h,i,u> error/ignore 202 * -xmodel=kernel -ffreestanding -mcmodel=kernel -mno-red-zone 203 * -Wu,-save_args -msave-args 204 * -w pass-thru 205 * -Xa -std=iso9899:199409 or -ansi 206 * -Xt error 207 * -Xs -traditional -std=c89 208 * -xarch=<a> table 209 * -xbuiltin[=<b>] -fbuiltin (-fno-builtin otherwise) 210 * -xCC ignore 211 * -xchip=<c> table 212 * -xcode=<c> table 213 * -xcrossfile[=<n>] ignore 214 * -xe error 215 * -xF error 216 * -xhelp=<f> error 217 * -xildoff ignore 218 * -xildon ignore 219 * -xinline ignore 220 * -xlibmieee error 221 * -xlibmil error 222 * -xlic_lib=sunperf error 223 * -xmaxopt=[...] error 224 * -xO<n> -O<n> 225 * -xP error 226 * -xprofile=<p> error 227 * -xregs=<r> table 228 * -xs error 229 * -xsb error 230 * -xsbfast error 231 * -xsfpconst error 232 * -xspace ignore (-not -Os) 233 * -xstrconst ignore 234 * -xtarget=<t> table 235 * -xtemp=<dir> error 236 * -xtime error 237 * -xtransition -Wtransition 238 * -xunroll=n error 239 * -Y<c>,<dir> error 240 * -YA,<dir> error 241 * -YI,<dir> -nostdinc -I<dir> 242 * -YP,<dir> error 243 * -YS,<dir> error 244 */ 245 246 #include <ctype.h> 247 #include <err.h> 248 #include <errno.h> 249 #include <fcntl.h> 250 #include <getopt.h> 251 #include <stdio.h> 252 #include <stdlib.h> 253 #include <stdbool.h> 254 #include <string.h> 255 #include <unistd.h> 256 #include <dirent.h> 257 258 #include <sys/param.h> 259 #include <sys/stat.h> 260 #include <sys/types.h> 261 #include <sys/utsname.h> 262 #include <sys/wait.h> 263 264 #define CW_F_CXX 0x01 265 #define CW_F_SHADOW 0x02 266 #define CW_F_EXEC 0x04 267 #define CW_F_ECHO 0x08 268 #define CW_F_XLATE 0x10 269 #define CW_F_PROG 0x20 270 271 typedef enum cw_op { 272 CW_O_NONE = 0, 273 CW_O_PREPROCESS, 274 CW_O_COMPILE, 275 CW_O_LINK 276 } cw_op_t; 277 278 struct aelist { 279 struct ae { 280 struct ae *ae_next; 281 char *ae_arg; 282 } *ael_head, *ael_tail; 283 int ael_argc; 284 }; 285 286 typedef enum { 287 GNU, 288 SUN, 289 SMATCH 290 } compiler_style_t; 291 292 typedef struct { 293 char *c_name; 294 char *c_path; 295 compiler_style_t c_style; 296 } cw_compiler_t; 297 298 typedef struct cw_ictx { 299 struct cw_ictx *i_next; 300 cw_compiler_t *i_compiler; 301 char *i_linker; 302 struct aelist *i_ae; 303 uint32_t i_flags; 304 int i_oldargc; 305 char **i_oldargv; 306 pid_t i_pid; 307 char *i_tmpdir; 308 char *i_stderr; 309 } cw_ictx_t; 310 311 /* 312 * Status values to indicate which Studio compiler and associated 313 * flags are being used. 314 */ 315 #define M32 0x01 /* -m32 - only on Studio 12 */ 316 #define M64 0x02 /* -m64 - only on Studio 12 */ 317 #define SS11 0x100 /* Studio 11 */ 318 #define SS12 0x200 /* Studio 12 */ 319 320 #define TRANS_ENTRY 5 321 /* 322 * Translation table definition for the -xarch= flag. The "x_arg" 323 * value is translated into the appropriate gcc flags according 324 * to the values in x_trans[n]. The x_flags indicates what compiler 325 * is being used and what flags have been set via the use of 326 * "x_arg". 327 */ 328 typedef struct xarch_table { 329 char *x_arg; 330 int x_flags; 331 char *x_trans[TRANS_ENTRY]; 332 } xarch_table_t; 333 334 /* 335 * The translation table for the -xarch= flag used in the Studio compilers. 336 */ 337 static const xarch_table_t xtbl[] = { 338 #if defined(__x86) 339 { "generic", SS11, {NULL} }, 340 { "generic64", (SS11|M64), { "-m64", "-mtune=opteron" } }, 341 { "amd64", (SS11|M64), { "-m64", "-mtune=opteron" } }, 342 { "386", SS11, { "-march=i386" } }, 343 { "pentium_pro", SS11, { "-march=pentiumpro" } }, 344 { "sse", SS11, { "-msse", "-mfpmath=sse" } }, 345 { "sse2", SS11, { "-msse2", "-mfpmath=sse" } }, 346 #elif defined(__sparc) 347 { "generic", (SS11|M32), { "-m32", "-mcpu=v8" } }, 348 { "generic64", (SS11|M64), { "-m64", "-mcpu=v9" } }, 349 { "v8", (SS11|M32), { "-m32", "-mcpu=v8", "-mno-v8plus" } }, 350 { "v8plus", (SS11|M32), { "-m32", "-mcpu=v9", "-mv8plus" } }, 351 { "v8plusa", (SS11|M32), { "-m32", "-mcpu=ultrasparc", "-mv8plus", 352 "-mvis" } }, 353 { "v8plusb", (SS11|M32), { "-m32", "-mcpu=ultrasparc3", "-mv8plus", 354 "-mvis" } }, 355 { "v9", (SS11|M64), { "-m64", "-mcpu=v9" } }, 356 { "v9a", (SS11|M64), { "-m64", "-mcpu=ultrasparc", "-mvis" } }, 357 { "v9b", (SS11|M64), { "-m64", "-mcpu=ultrasparc3", "-mvis" } }, 358 { "sparc", SS12, { "-mcpu=v9", "-mv8plus" } }, 359 { "sparcvis", SS12, { "-mcpu=ultrasparc", "-mvis" } }, 360 { "sparcvis2", SS12, { "-mcpu=ultrasparc3", "-mvis" } } 361 #endif 362 }; 363 364 static int xtbl_size = sizeof (xtbl) / sizeof (xarch_table_t); 365 366 static const char *xchip_tbl[] = { 367 #if defined(__x86) 368 "386", "-mtune=i386", NULL, 369 "486", "-mtune=i486", NULL, 370 "pentium", "-mtune=pentium", NULL, 371 "pentium_pro", "-mtune=pentiumpro", NULL, 372 #elif defined(__sparc) 373 "super", "-mtune=supersparc", NULL, 374 "ultra", "-mtune=ultrasparc", NULL, 375 "ultra3", "-mtune=ultrasparc3", NULL, 376 #endif 377 NULL, NULL 378 }; 379 380 static const char *xcode_tbl[] = { 381 #if defined(__sparc) 382 "abs32", "-fno-pic", "-mcmodel=medlow", NULL, 383 "abs44", "-fno-pic", "-mcmodel=medmid", NULL, 384 "abs64", "-fno-pic", "-mcmodel=medany", NULL, 385 "pic13", "-fpic", NULL, 386 "pic32", "-fPIC", NULL, 387 #endif 388 NULL, NULL 389 }; 390 391 static const char *xtarget_tbl[] = { 392 #if defined(__x86) 393 "pentium_pro", "-march=pentiumpro", NULL, 394 #endif /* __x86 */ 395 NULL, NULL 396 }; 397 398 static const char *xregs_tbl[] = { 399 #if defined(__sparc) 400 "appl", "-mapp-regs", NULL, 401 "no%appl", "-mno-app-regs", NULL, 402 "float", "-mfpu", NULL, 403 "no%float", "-mno-fpu", NULL, 404 #endif /* __sparc */ 405 NULL, NULL 406 }; 407 408 static void 409 nomem(void) 410 { 411 errx(1, "out of memory"); 412 } 413 414 static void 415 newae(struct aelist *ael, const char *arg) 416 { 417 struct ae *ae; 418 419 if ((ae = calloc(1, sizeof (*ae))) == NULL) 420 nomem(); 421 ae->ae_arg = strdup(arg); 422 if (ael->ael_tail == NULL) 423 ael->ael_head = ae; 424 else 425 ael->ael_tail->ae_next = ae; 426 ael->ael_tail = ae; 427 ael->ael_argc++; 428 } 429 430 static cw_ictx_t * 431 newictx(void) 432 { 433 cw_ictx_t *ctx = calloc(1, sizeof (cw_ictx_t)); 434 if (ctx) 435 if ((ctx->i_ae = calloc(1, sizeof (struct aelist))) == NULL) { 436 free(ctx); 437 return (NULL); 438 } 439 440 return (ctx); 441 } 442 443 static void 444 error(const char *arg) 445 { 446 errx(2, "error: mapping failed at or near arg '%s'", arg); 447 } 448 449 /* 450 * Add the current favourite set of warnings to the gcc invocation. 451 */ 452 static void 453 warnings(struct aelist *h) 454 { 455 static int warningsonce; 456 457 if (warningsonce++) 458 return; 459 460 /* 461 * Enable as many warnings as exist, then disable those that we never 462 * ever want. 463 */ 464 newae(h, "-Wall"); 465 newae(h, "-Wextra"); 466 } 467 468 static void 469 optim_disable(struct aelist *h, int level) 470 { 471 if (level >= 2) { 472 newae(h, "-fno-strict-aliasing"); 473 newae(h, "-fno-unit-at-a-time"); 474 newae(h, "-fno-optimize-sibling-calls"); 475 } 476 } 477 478 static void 479 Xsmode(struct aelist *h) 480 { 481 static int xsonce; 482 483 if (xsonce++) 484 return; 485 486 newae(h, "-traditional"); 487 newae(h, "-traditional-cpp"); 488 } 489 490 static void 491 usage(void) 492 { 493 extern char *__progname; 494 (void) fprintf(stderr, 495 "usage: %s [-C] [--versions] --primary <compiler> " 496 "[--shadow <compiler>]... -- cflags...\n", 497 __progname); 498 (void) fprintf(stderr, "compilers take the form: name,path,style\n" 499 " - name: a unique name usable in flag specifiers\n" 500 " - path: path to the compiler binary\n" 501 " - style: the style of flags expected: either sun or gnu\n"); 502 exit(2); 503 } 504 505 static int 506 xlate_xtb(struct aelist *h, const char *xarg) 507 { 508 int i, j; 509 510 for (i = 0; i < xtbl_size; i++) { 511 if (strcmp(xtbl[i].x_arg, xarg) == 0) 512 break; 513 } 514 515 /* 516 * At the end of the table and so no matching "arg" entry 517 * found and so this must be a bad -xarch= flag. 518 */ 519 if (i == xtbl_size) 520 error(xarg); 521 522 for (j = 0; j < TRANS_ENTRY; j++) { 523 if (xtbl[i].x_trans[j] != NULL) 524 newae(h, xtbl[i].x_trans[j]); 525 else 526 break; 527 } 528 return (xtbl[i].x_flags); 529 530 } 531 532 static void 533 xlate(struct aelist *h, const char *xarg, const char **table) 534 { 535 while (*table != NULL && strcmp(xarg, *table) != 0) { 536 while (*table != NULL) 537 table++; 538 table++; 539 } 540 541 if (*table == NULL) 542 error(xarg); 543 544 table++; 545 546 while (*table != NULL) { 547 newae(h, *table); 548 table++; 549 } 550 } 551 552 /* 553 * The compiler wants the output file to end in appropriate extension. If 554 * we're generating a name from whole cloth (path == NULL), we assume that 555 * extension to be .o, otherwise we match the extension of the caller. 556 */ 557 static char * 558 discard_file_name(cw_ictx_t *ctx, const char *path) 559 { 560 char *ret, *ext; 561 char tmpl[] = "cwXXXXXX"; 562 563 if (path == NULL) { 564 ext = ".o"; 565 } else { 566 ext = strrchr(path, '.'); 567 } 568 569 /* 570 * We need absolute control over where the temporary file goes, since 571 * we rely on it for cleanup so tempnam(3C) and tmpnam(3C) are 572 * inappropriate (they use TMPDIR, preferentially). 573 * 574 * mkstemp(3C) doesn't actually help us, since the temporary file 575 * isn't used by us, only its name. 576 */ 577 if (mktemp(tmpl) == NULL) 578 nomem(); 579 580 (void) asprintf(&ret, "%s/%s%s", ctx->i_tmpdir, tmpl, 581 (ext != NULL) ? ext : ""); 582 583 if (ret == NULL) 584 nomem(); 585 586 return (ret); 587 } 588 589 static bool 590 is_source_file(const char *path) 591 { 592 char *ext = strrchr(path, '.'); 593 594 if ((ext == NULL) || (*(ext + 1) == '\0')) 595 return (false); 596 597 ext += 1; 598 599 if ((strcasecmp(ext, "c") == 0) || 600 (strcmp(ext, "cc") == 0) || 601 (strcmp(ext, "i") == 0) || 602 (strcasecmp(ext, "s") == 0) || 603 (strcmp(ext, "cpp") == 0)) { 604 return (true); 605 } 606 607 return (false); 608 } 609 610 611 static void 612 do_gcc(cw_ictx_t *ctx) 613 { 614 int c; 615 int nolibc = 0; 616 int in_output = 0, seen_o = 0, c_files = 0; 617 cw_op_t op = CW_O_LINK; 618 char *model = NULL; 619 char *nameflag; 620 int mflag = 0; 621 622 if (ctx->i_flags & CW_F_PROG) { 623 newae(ctx->i_ae, "--version"); 624 return; 625 } 626 627 newae(ctx->i_ae, "-fident"); 628 newae(ctx->i_ae, "-finline"); 629 newae(ctx->i_ae, "-fno-inline-functions"); 630 newae(ctx->i_ae, "-fno-builtin"); 631 newae(ctx->i_ae, "-fno-asm"); 632 newae(ctx->i_ae, "-fdiagnostics-show-option"); 633 newae(ctx->i_ae, "-nodefaultlibs"); 634 635 #if defined(__sparc) 636 /* 637 * The SPARC ldd and std instructions require 8-byte alignment of 638 * their address operand. gcc correctly uses them only when the 639 * ABI requires 8-byte alignment; unfortunately we have a number of 640 * pieces of buggy code that doesn't conform to the ABI. This 641 * flag makes gcc work more like Studio with -xmemalign=4. 642 */ 643 newae(ctx->i_ae, "-mno-integer-ldd-std"); 644 #endif 645 646 /* 647 * This is needed because 'u' is defined 648 * under a conditional on 'sun'. Should 649 * probably just remove the conditional, 650 * or make it be dependent on '__sun'. 651 * 652 * -Dunix is also missing in enhanced ANSI mode 653 */ 654 newae(ctx->i_ae, "-D__sun"); 655 656 if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1) 657 nomem(); 658 659 /* 660 * Walk the argument list, translating as we go .. 661 */ 662 while (--ctx->i_oldargc > 0) { 663 char *arg = *++ctx->i_oldargv; 664 size_t arglen = strlen(arg); 665 666 if (*arg == '-') { 667 arglen--; 668 } else { 669 /* 670 * Discard inline files that gcc doesn't grok 671 */ 672 if (!in_output && arglen > 3 && 673 strcmp(arg + arglen - 3, ".il") == 0) 674 continue; 675 676 if (!in_output && is_source_file(arg)) 677 c_files++; 678 679 /* 680 * Otherwise, filenames and partial arguments 681 * are passed through for gcc to chew on. However, 682 * output is always discarded for the secondary 683 * compiler. 684 */ 685 if ((ctx->i_flags & CW_F_SHADOW) && in_output) { 686 newae(ctx->i_ae, discard_file_name(ctx, arg)); 687 } else { 688 newae(ctx->i_ae, arg); 689 } 690 in_output = 0; 691 continue; 692 } 693 694 if (ctx->i_flags & CW_F_CXX) { 695 if (strncmp(arg, "-_g++=", 6) == 0) { 696 newae(ctx->i_ae, strchr(arg, '=') + 1); 697 continue; 698 } 699 if (strncmp(arg, "-compat=", 8) == 0) { 700 /* discard -compat=4 and -compat=5 */ 701 continue; 702 } 703 if (strcmp(arg, "-Qoption") == 0) { 704 /* discard -Qoption and its two arguments */ 705 if (ctx->i_oldargc < 3) 706 error(arg); 707 ctx->i_oldargc -= 2; 708 ctx->i_oldargv += 2; 709 continue; 710 } 711 if (strcmp(arg, "-xwe") == 0) { 712 /* turn warnings into errors */ 713 newae(ctx->i_ae, "-Werror"); 714 continue; 715 } 716 if (strcmp(arg, "-norunpath") == 0) { 717 /* gcc has no corresponding option */ 718 continue; 719 } 720 if (strcmp(arg, "-nolib") == 0) { 721 /* -nodefaultlibs is on by default */ 722 nolibc = 1; 723 continue; 724 } 725 #if defined(__sparc) 726 if (strcmp(arg, "-cg92") == 0) { 727 mflag |= xlate_xtb(ctx->i_ae, "v8"); 728 xlate(ctx->i_ae, "super", xchip_tbl); 729 continue; 730 } 731 #endif /* __sparc */ 732 } 733 734 switch ((c = arg[1])) { 735 case '_': 736 if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) || 737 (strncmp(arg, "-_gcc=", 6) == 0) || 738 (strncmp(arg, "-_gnu=", 6) == 0)) { 739 newae(ctx->i_ae, strchr(arg, '=') + 1); 740 } 741 break; 742 case '#': 743 if (arglen == 1) { 744 newae(ctx->i_ae, "-v"); 745 break; 746 } 747 error(arg); 748 break; 749 case 'f': 750 if ((strcmp(arg, "-fpic") == 0) || 751 (strcmp(arg, "-fPIC") == 0)) { 752 newae(ctx->i_ae, arg); 753 break; 754 } 755 error(arg); 756 break; 757 case 'E': 758 if (arglen == 1) { 759 newae(ctx->i_ae, "-xc"); 760 newae(ctx->i_ae, arg); 761 op = CW_O_PREPROCESS; 762 nolibc = 1; 763 break; 764 } 765 error(arg); 766 break; 767 case 'c': 768 case 'S': 769 if (arglen == 1) { 770 op = CW_O_COMPILE; 771 nolibc = 1; 772 } 773 /* FALLTHROUGH */ 774 case 'C': 775 case 'H': 776 case 'p': 777 if (arglen == 1) { 778 newae(ctx->i_ae, arg); 779 break; 780 } 781 error(arg); 782 break; 783 case 'A': 784 case 'g': 785 case 'I': 786 case 'i': 787 case 'L': 788 case 'l': 789 case 'R': 790 case 'U': 791 case 'u': 792 case 'w': 793 newae(ctx->i_ae, arg); 794 break; 795 case 'o': 796 seen_o = 1; 797 if (arglen == 1) { 798 in_output = 1; 799 newae(ctx->i_ae, arg); 800 } else if (ctx->i_flags & CW_F_SHADOW) { 801 newae(ctx->i_ae, "-o"); 802 newae(ctx->i_ae, discard_file_name(ctx, arg)); 803 } else { 804 newae(ctx->i_ae, arg); 805 } 806 break; 807 case 'D': 808 newae(ctx->i_ae, arg); 809 /* 810 * XXX Clearly a hack ... do we need _KADB too? 811 */ 812 if (strcmp(arg, "-D_KERNEL") == 0 || 813 strcmp(arg, "-D_BOOT") == 0) 814 newae(ctx->i_ae, "-ffreestanding"); 815 break; 816 case 'd': 817 if (strcmp(arg, "-dalign") == 0) { 818 /* 819 * -dalign forces alignment in some cases; 820 * gcc does not need any flag to do this. 821 */ 822 break; 823 } 824 error(arg); 825 break; 826 case 'e': 827 if (strcmp(arg, 828 "-erroff=E_EMPTY_TRANSLATION_UNIT") == 0) { 829 /* 830 * Accept but ignore this -- gcc doesn't 831 * seem to complain about empty translation 832 * units 833 */ 834 break; 835 } 836 /* XX64 -- ignore all -erroff= options, for now */ 837 if (strncmp(arg, "-erroff=", 8) == 0) 838 break; 839 if (strcmp(arg, "-errtags=yes") == 0) { 840 warnings(ctx->i_ae); 841 break; 842 } 843 if (strcmp(arg, "-errwarn=%all") == 0) { 844 newae(ctx->i_ae, "-Werror"); 845 break; 846 } 847 error(arg); 848 break; 849 case 'k': 850 if (strcmp(arg, "-keeptmp") == 0) { 851 newae(ctx->i_ae, "-save-temps"); 852 break; 853 } 854 error(arg); 855 break; 856 case 'm': 857 if (strcmp(arg, "-mt") == 0) { 858 newae(ctx->i_ae, "-D_REENTRANT"); 859 break; 860 } 861 if (strcmp(arg, "-m64") == 0) { 862 newae(ctx->i_ae, "-m64"); 863 #if defined(__x86) 864 newae(ctx->i_ae, "-mtune=opteron"); 865 #endif 866 mflag |= M64; 867 break; 868 } 869 if (strcmp(arg, "-m32") == 0) { 870 newae(ctx->i_ae, "-m32"); 871 mflag |= M32; 872 break; 873 } 874 error(arg); 875 break; 876 case 'O': 877 if (arglen == 1) { 878 newae(ctx->i_ae, "-O"); 879 break; 880 } 881 error(arg); 882 break; 883 case 'P': 884 /* 885 * We could do '-E -o filename.i', but that's hard, 886 * and we don't need it for the case that's triggering 887 * this addition. We'll require the user to specify 888 * -o in the Makefile. If they don't they'll find out 889 * in a hurry. 890 */ 891 newae(ctx->i_ae, "-E"); 892 op = CW_O_PREPROCESS; 893 nolibc = 1; 894 break; 895 case 's': 896 if (strcmp(arg, "-shared") == 0) { 897 newae(ctx->i_ae, "-shared"); 898 nolibc = 1; 899 break; 900 } 901 error(arg); 902 break; 903 904 case 'V': 905 if (arglen == 1) { 906 ctx->i_flags &= ~CW_F_ECHO; 907 newae(ctx->i_ae, "--version"); 908 break; 909 } 910 error(arg); 911 break; 912 case 'v': 913 if (arglen == 1) { 914 warnings(ctx->i_ae); 915 break; 916 } 917 error(arg); 918 break; 919 case 'W': 920 if (strncmp(arg, "-Wp,-xc99", 9) == 0) { 921 /* 922 * gcc's preprocessor will accept c99 923 * regardless, so accept and ignore. 924 */ 925 break; 926 } 927 if (strncmp(arg, "-Wa,", 4) == 0 || 928 strncmp(arg, "-Wp,", 4) == 0 || 929 strncmp(arg, "-Wl,", 4) == 0) { 930 newae(ctx->i_ae, arg); 931 break; 932 } 933 if (strcmp(arg, "-W0,-noglobal") == 0 || 934 strcmp(arg, "-W0,-xglobalstatic") == 0) { 935 /* 936 * gcc doesn't prefix local symbols 937 * in debug mode, so this is not needed. 938 */ 939 break; 940 } 941 if (strcmp(arg, "-W0,-Lt") == 0) { 942 /* 943 * Generate tests at the top of loops. 944 * There is no direct gcc equivalent, ignore. 945 */ 946 break; 947 } 948 if (strcmp(arg, "-W2,-xwrap_int") == 0) { 949 /* 950 * Use the legacy behaviour (pre-SS11) 951 * for integer wrapping. 952 * gcc does not need this. 953 */ 954 break; 955 } 956 if (strcmp(arg, "-Wd,-xsafe=unboundsym") == 0) { 957 /* 958 * Prevents optimizing away checks for 959 * unbound weak symbol addresses. gcc does 960 * not do this, so it's not needed. 961 */ 962 break; 963 } 964 if (strncmp(arg, "-Wc,-xcode=", 11) == 0) { 965 xlate(ctx->i_ae, arg + 11, xcode_tbl); 966 break; 967 } 968 if (strncmp(arg, "-Wc,-Qiselect", 13) == 0) { 969 /* 970 * Prevents insertion of register symbols. 971 * gcc doesn't do this, so ignore it. 972 */ 973 break; 974 } 975 if (strcmp(arg, "-Wc,-Qassembler-ounrefsym=0") == 0) { 976 /* 977 * Prevents optimizing away of static variables. 978 * gcc does not do this, so it's not needed. 979 */ 980 break; 981 } 982 #if defined(__x86) 983 if (strcmp(arg, "-Wu,-save_args") == 0) { 984 newae(ctx->i_ae, "-msave-args"); 985 break; 986 } 987 #endif /* __x86 */ 988 error(arg); 989 break; 990 case 'X': 991 if (strcmp(arg, "-Xa") == 0 || 992 strcmp(arg, "-Xt") == 0) { 993 break; 994 } 995 if (strcmp(arg, "-Xs") == 0) { 996 Xsmode(ctx->i_ae); 997 break; 998 } 999 error(arg); 1000 break; 1001 case 'x': 1002 if (arglen == 1) 1003 error(arg); 1004 switch (arg[2]) { 1005 case 'a': 1006 if (strncmp(arg, "-xarch=", 7) == 0) { 1007 mflag |= xlate_xtb(ctx->i_ae, arg + 7); 1008 break; 1009 } 1010 error(arg); 1011 break; 1012 case 'b': 1013 if (strncmp(arg, "-xbuiltin=", 10) == 0) { 1014 if (strcmp(arg + 10, "%all")) 1015 newae(ctx->i_ae, "-fbuiltin"); 1016 break; 1017 } 1018 error(arg); 1019 break; 1020 case 'C': 1021 /* Accept C++ style comments -- ignore */ 1022 if (strcmp(arg, "-xCC") == 0) 1023 break; 1024 error(arg); 1025 break; 1026 case 'c': 1027 if (strncmp(arg, "-xc99=%all", 10) == 0) { 1028 newae(ctx->i_ae, "-std=gnu99"); 1029 break; 1030 } 1031 if (strncmp(arg, "-xc99=%none", 11) == 0) { 1032 newae(ctx->i_ae, "-std=gnu89"); 1033 break; 1034 } 1035 if (strncmp(arg, "-xchip=", 7) == 0) { 1036 xlate(ctx->i_ae, arg + 7, xchip_tbl); 1037 break; 1038 } 1039 if (strncmp(arg, "-xcode=", 7) == 0) { 1040 xlate(ctx->i_ae, arg + 7, xcode_tbl); 1041 break; 1042 } 1043 if (strncmp(arg, "-xcrossfile", 11) == 0) 1044 break; 1045 error(arg); 1046 break; 1047 case 'F': 1048 /* 1049 * Compile for mapfile reordering, or unused 1050 * section elimination, syntax can be -xF or 1051 * more complex, like -xF=%all -- ignore. 1052 */ 1053 if (strncmp(arg, "-xF", 3) == 0) 1054 break; 1055 error(arg); 1056 break; 1057 case 'i': 1058 if (strncmp(arg, "-xinline", 8) == 0) 1059 /* No inlining; ignore */ 1060 break; 1061 if (strcmp(arg, "-xildon") == 0 || 1062 strcmp(arg, "-xildoff") == 0) 1063 /* No incremental linking; ignore */ 1064 break; 1065 error(arg); 1066 break; 1067 #if defined(__x86) 1068 case 'm': 1069 if (strcmp(arg, "-xmodel=kernel") == 0) { 1070 newae(ctx->i_ae, "-ffreestanding"); 1071 newae(ctx->i_ae, "-mno-red-zone"); 1072 model = "-mcmodel=kernel"; 1073 nolibc = 1; 1074 break; 1075 } 1076 error(arg); 1077 break; 1078 #endif /* __x86 */ 1079 case 'O': 1080 if (strncmp(arg, "-xO", 3) == 0) { 1081 size_t len = strlen(arg); 1082 char *s = NULL; 1083 int c = *(arg + 3); 1084 int level; 1085 1086 if (len != 4 || !isdigit(c)) 1087 error(arg); 1088 1089 level = atoi(arg + 3); 1090 if (level > 5) 1091 error(arg); 1092 if (level >= 2) { 1093 /* 1094 * For gcc-3.4.x at -O2 we 1095 * need to disable optimizations 1096 * that break ON. 1097 */ 1098 optim_disable(ctx->i_ae, level); 1099 /* 1100 * limit -xO3 to -O2 as well. 1101 */ 1102 level = 2; 1103 } 1104 if (asprintf(&s, "-O%d", level) == -1) 1105 nomem(); 1106 newae(ctx->i_ae, s); 1107 free(s); 1108 break; 1109 } 1110 error(arg); 1111 break; 1112 case 'r': 1113 if (strncmp(arg, "-xregs=", 7) == 0) { 1114 xlate(ctx->i_ae, arg + 7, xregs_tbl); 1115 break; 1116 } 1117 error(arg); 1118 break; 1119 case 's': 1120 if (strcmp(arg, "-xs") == 0 || 1121 strcmp(arg, "-xspace") == 0 || 1122 strcmp(arg, "-xstrconst") == 0) 1123 break; 1124 error(arg); 1125 break; 1126 case 't': 1127 if (strncmp(arg, "-xtarget=", 9) == 0) { 1128 xlate(ctx->i_ae, arg + 9, xtarget_tbl); 1129 break; 1130 } 1131 error(arg); 1132 break; 1133 case 'e': 1134 case 'h': 1135 case 'l': 1136 default: 1137 error(arg); 1138 break; 1139 } 1140 break; 1141 case 'Y': 1142 if (arglen == 1) { 1143 if ((arg = *++ctx->i_oldargv) == NULL || 1144 *arg == '\0') 1145 error("-Y"); 1146 ctx->i_oldargc--; 1147 arglen = strlen(arg + 1); 1148 } else { 1149 arg += 2; 1150 } 1151 /* Just ignore -YS,... for now */ 1152 if (strncmp(arg, "S,", 2) == 0) 1153 break; 1154 if (strncmp(arg, "I,", 2) == 0) { 1155 char *s = strdup(arg); 1156 s[0] = '-'; 1157 s[1] = 'I'; 1158 newae(ctx->i_ae, "-nostdinc"); 1159 newae(ctx->i_ae, s); 1160 free(s); 1161 break; 1162 } 1163 error(arg); 1164 break; 1165 case 'Q': 1166 /* 1167 * We could map -Qy into -Wl,-Qy etc. 1168 */ 1169 default: 1170 error(arg); 1171 break; 1172 } 1173 } 1174 1175 free(nameflag); 1176 1177 /* 1178 * When compiling multiple source files in a single invocation some 1179 * compilers output objects into the current directory with 1180 * predictable and conventional names. 1181 * 1182 * We prevent any attempt to compile multiple files at once so that 1183 * any such objects created by a shadow can't escape into a later 1184 * link-edit. 1185 */ 1186 if (c_files > 1 && op != CW_O_PREPROCESS) { 1187 errx(2, "multiple source files are " 1188 "allowed only with -E or -P"); 1189 } 1190 1191 /* 1192 * Make sure that we do not have any unintended interactions between 1193 * the xarch options passed in and the version of the Studio compiler 1194 * used. 1195 */ 1196 if ((mflag & (SS11|SS12)) == (SS11|SS12)) { 1197 errx(2, 1198 "Conflicting \"-xarch=\" flags (both Studio 11 and 12)\n"); 1199 } 1200 1201 switch (mflag) { 1202 case 0: 1203 /* FALLTHROUGH */ 1204 case M32: 1205 #if defined(__sparc) 1206 /* 1207 * Only -m32 is defined and so put in the missing xarch 1208 * translation. 1209 */ 1210 newae(ctx->i_ae, "-mcpu=v8"); 1211 newae(ctx->i_ae, "-mno-v8plus"); 1212 #endif 1213 break; 1214 case M64: 1215 #if defined(__sparc) 1216 /* 1217 * Only -m64 is defined and so put in the missing xarch 1218 * translation. 1219 */ 1220 newae(ctx->i_ae, "-mcpu=v9"); 1221 #endif 1222 break; 1223 case SS12: 1224 #if defined(__sparc) 1225 /* no -m32/-m64 flag used - this is an error for sparc builds */ 1226 (void) fprintf(stderr, "No -m32/-m64 flag defined\n"); 1227 exit(2); 1228 #endif 1229 break; 1230 case SS11: 1231 /* FALLTHROUGH */ 1232 case (SS11|M32): 1233 case (SS11|M64): 1234 break; 1235 case (SS12|M32): 1236 #if defined(__sparc) 1237 /* 1238 * Need to add in further 32 bit options because with SS12 1239 * the xarch=sparcvis option can be applied to 32 or 64 1240 * bit, and so the translatation table (xtbl) cannot handle 1241 * that. 1242 */ 1243 newae(ctx->i_ae, "-mv8plus"); 1244 #endif 1245 break; 1246 case (SS12|M64): 1247 break; 1248 default: 1249 (void) fprintf(stderr, 1250 "Incompatible -xarch= and/or -m32/-m64 options used.\n"); 1251 exit(2); 1252 } 1253 1254 if (ctx->i_flags & CW_F_SHADOW) { 1255 if (op == CW_O_PREPROCESS) 1256 exit(0); 1257 else if (op == CW_O_LINK && c_files == 0) 1258 exit(0); 1259 } 1260 1261 if (model != NULL) 1262 newae(ctx->i_ae, model); 1263 if (!nolibc) 1264 newae(ctx->i_ae, "-lc"); 1265 if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) { 1266 newae(ctx->i_ae, "-o"); 1267 newae(ctx->i_ae, discard_file_name(ctx, NULL)); 1268 } 1269 } 1270 1271 static void 1272 do_smatch(cw_ictx_t *ctx) 1273 { 1274 if (ctx->i_flags & CW_F_PROG) { 1275 newae(ctx->i_ae, "--version"); 1276 return; 1277 } 1278 1279 /* 1280 * Some sources shouldn't run smatch at all. 1281 */ 1282 for (int i = 0; i < ctx->i_oldargc; i++) { 1283 char *arg = ctx->i_oldargv[i]; 1284 1285 if (strcmp(arg, "-_smatch=off") == 0) { 1286 ctx->i_flags &= ~ (CW_F_EXEC | CW_F_ECHO); 1287 return; 1288 } 1289 } 1290 1291 /* 1292 * smatch can handle gcc's options. 1293 */ 1294 do_gcc(ctx); 1295 } 1296 1297 static void 1298 do_cc(cw_ictx_t *ctx) 1299 { 1300 int in_output = 0, seen_o = 0, c_files = 0; 1301 cw_op_t op = CW_O_LINK; 1302 char *nameflag; 1303 1304 if (ctx->i_flags & CW_F_PROG) { 1305 newae(ctx->i_ae, "-V"); 1306 return; 1307 } 1308 1309 if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1) 1310 nomem(); 1311 1312 while (--ctx->i_oldargc > 0) { 1313 char *arg = *++ctx->i_oldargv; 1314 1315 if (strncmp(arg, "-_CC=", 5) == 0) { 1316 newae(ctx->i_ae, strchr(arg, '=') + 1); 1317 continue; 1318 } 1319 1320 if (*arg != '-') { 1321 if (!in_output && is_source_file(arg)) 1322 c_files++; 1323 1324 if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) { 1325 newae(ctx->i_ae, arg); 1326 } else { 1327 in_output = 0; 1328 newae(ctx->i_ae, discard_file_name(ctx, arg)); 1329 } 1330 continue; 1331 } 1332 switch (*(arg + 1)) { 1333 case '_': 1334 if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) || 1335 (strncmp(arg, "-_cc=", 5) == 0) || 1336 (strncmp(arg, "-_sun=", 6) == 0)) { 1337 newae(ctx->i_ae, strchr(arg, '=') + 1); 1338 } 1339 break; 1340 1341 case 'V': 1342 ctx->i_flags &= ~CW_F_ECHO; 1343 newae(ctx->i_ae, arg); 1344 break; 1345 case 'o': 1346 seen_o = 1; 1347 if (strlen(arg) == 2) { 1348 in_output = 1; 1349 newae(ctx->i_ae, arg); 1350 } else if (ctx->i_flags & CW_F_SHADOW) { 1351 newae(ctx->i_ae, "-o"); 1352 newae(ctx->i_ae, discard_file_name(ctx, arg)); 1353 } else { 1354 newae(ctx->i_ae, arg); 1355 } 1356 break; 1357 case 'c': 1358 case 'S': 1359 if (strlen(arg) == 2) 1360 op = CW_O_COMPILE; 1361 newae(ctx->i_ae, arg); 1362 break; 1363 case 'E': 1364 case 'P': 1365 if (strlen(arg) == 2) 1366 op = CW_O_PREPROCESS; 1367 /*FALLTHROUGH*/ 1368 default: 1369 newae(ctx->i_ae, arg); 1370 } 1371 } 1372 1373 free(nameflag); 1374 1375 /* See the comment on this same code in do_gcc() */ 1376 if (c_files > 1 && op != CW_O_PREPROCESS) { 1377 errx(2, "multiple source files are " 1378 "allowed only with -E or -P"); 1379 } 1380 1381 if (ctx->i_flags & CW_F_SHADOW) { 1382 if (op == CW_O_PREPROCESS) 1383 exit(0); 1384 else if (op == CW_O_LINK && c_files == 0) 1385 exit(0); 1386 } 1387 1388 if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) { 1389 newae(ctx->i_ae, "-o"); 1390 newae(ctx->i_ae, discard_file_name(ctx, NULL)); 1391 } 1392 } 1393 1394 static void 1395 prepctx(cw_ictx_t *ctx) 1396 { 1397 newae(ctx->i_ae, ctx->i_compiler->c_path); 1398 1399 if (ctx->i_flags & CW_F_PROG) { 1400 (void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ? 1401 "shadow" : "primary", ctx->i_compiler->c_path); 1402 (void) fflush(stdout); 1403 } 1404 1405 /* 1406 * If LD_ALTEXEC is already set, the expectation would be that that 1407 * link-editor is run, as such we need to leave it the environment 1408 * alone and let that happen. 1409 */ 1410 if ((ctx->i_linker != NULL) && (getenv("LD_ALTEXEC") == NULL)) 1411 setenv("LD_ALTEXEC", ctx->i_linker, 1); 1412 1413 if (!(ctx->i_flags & CW_F_XLATE)) 1414 return; 1415 1416 switch (ctx->i_compiler->c_style) { 1417 case SUN: 1418 do_cc(ctx); 1419 break; 1420 case GNU: 1421 do_gcc(ctx); 1422 break; 1423 case SMATCH: 1424 do_smatch(ctx); 1425 break; 1426 } 1427 } 1428 1429 static int 1430 invoke(cw_ictx_t *ctx) 1431 { 1432 char **newargv, *makeflags; 1433 int ac; 1434 struct ae *a; 1435 1436 newargv = calloc(ctx->i_ae->ael_argc + 1, sizeof (*newargv)); 1437 if (newargv == NULL) 1438 nomem(); 1439 1440 /* 1441 * Check to see if the silent make flag is present (-s), if so, do not 1442 * echo. The MAKEFLAGS environment variable is set by dmake. By 1443 * observation it appears to place short flags without any arguments 1444 * first followed by any long form flags or flags with arguments. 1445 */ 1446 makeflags = getenv("MAKEFLAGS"); 1447 if (makeflags != NULL) { 1448 size_t makeflags_len = strlen(makeflags); 1449 for (size_t i = 0; i < makeflags_len; i++) { 1450 if (makeflags[i] == 's') { 1451 ctx->i_flags &= ~CW_F_ECHO; 1452 break; 1453 } 1454 /* end of short flags */ 1455 if (makeflags[i] == ' ') 1456 break; 1457 } 1458 } 1459 1460 if (ctx->i_flags & CW_F_ECHO) 1461 (void) fprintf(stderr, "+ "); 1462 1463 for (ac = 0, a = ctx->i_ae->ael_head; a; a = a->ae_next, ac++) { 1464 newargv[ac] = a->ae_arg; 1465 if (ctx->i_flags & CW_F_ECHO) 1466 (void) fprintf(stderr, "%s ", a->ae_arg); 1467 if (a == ctx->i_ae->ael_tail) 1468 break; 1469 } 1470 1471 if (ctx->i_flags & CW_F_ECHO) { 1472 (void) fprintf(stderr, "\n"); 1473 (void) fflush(stderr); 1474 } 1475 1476 if (!(ctx->i_flags & CW_F_EXEC)) 1477 return (0); 1478 1479 /* 1480 * We must fix up the environment here so that the dependency files are 1481 * not trampled by the shadow compiler. Also take care of GCC 1482 * environment variables that will throw off gcc. This assumes a primary 1483 * gcc. 1484 */ 1485 if ((ctx->i_flags & CW_F_SHADOW) && 1486 (unsetenv("SUNPRO_DEPENDENCIES") != 0 || 1487 unsetenv("DEPENDENCIES_OUTPUT") != 0 || 1488 unsetenv("GCC_ROOT") != 0)) { 1489 (void) fprintf(stderr, "error: environment setup failed: %s\n", 1490 strerror(errno)); 1491 return (-1); 1492 } 1493 1494 (void) execv(newargv[0], newargv); 1495 warn("couldn't run %s", newargv[0]); 1496 1497 return (-1); 1498 } 1499 1500 static int 1501 reap(cw_ictx_t *ctx) 1502 { 1503 int status, ret = 0; 1504 char buf[1024]; 1505 struct stat s; 1506 1507 /* 1508 * Only wait for one specific child. 1509 */ 1510 if (ctx->i_pid <= 0) 1511 return (-1); 1512 1513 do { 1514 if (waitpid(ctx->i_pid, &status, 0) < 0) { 1515 warn("cannot reap child"); 1516 return (-1); 1517 } 1518 if (status != 0) { 1519 if (WIFSIGNALED(status)) { 1520 ret = -WTERMSIG(status); 1521 break; 1522 } else if (WIFEXITED(status)) { 1523 ret = WEXITSTATUS(status); 1524 break; 1525 } 1526 } 1527 } while (!WIFEXITED(status) && !WIFSIGNALED(status)); 1528 1529 if (stat(ctx->i_stderr, &s) < 0) { 1530 warn("stat failed on child cleanup"); 1531 return (-1); 1532 } 1533 if (s.st_size != 0) { 1534 FILE *f; 1535 1536 if ((f = fopen(ctx->i_stderr, "r")) != NULL) { 1537 while (fgets(buf, sizeof (buf), f)) 1538 (void) fprintf(stderr, "%s", buf); 1539 (void) fflush(stderr); 1540 (void) fclose(f); 1541 } 1542 } 1543 (void) unlink(ctx->i_stderr); 1544 free(ctx->i_stderr); 1545 1546 /* 1547 * cc returns an error code when given -V; we want that to succeed. 1548 */ 1549 if (ctx->i_flags & CW_F_PROG) 1550 return (0); 1551 1552 return (ret); 1553 } 1554 1555 static int 1556 exec_ctx(cw_ictx_t *ctx, int block) 1557 { 1558 if ((ctx->i_stderr = tempnam(ctx->i_tmpdir, "cw")) == NULL) { 1559 nomem(); 1560 return (-1); 1561 } 1562 1563 if ((ctx->i_pid = fork()) == 0) { 1564 int fd; 1565 1566 (void) fclose(stderr); 1567 if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL, 1568 0666)) < 0) { 1569 err(1, "open failed for standard error"); 1570 } 1571 if (dup2(fd, 2) < 0) { 1572 err(1, "dup2 failed for standard error"); 1573 } 1574 if (fd != 2) 1575 (void) close(fd); 1576 if (freopen("/dev/fd/2", "w", stderr) == NULL) { 1577 err(1, "freopen failed for /dev/fd/2"); 1578 } 1579 1580 prepctx(ctx); 1581 exit(invoke(ctx)); 1582 } 1583 1584 if (ctx->i_pid < 0) { 1585 err(1, "fork failed"); 1586 } 1587 1588 if (block) 1589 return (reap(ctx)); 1590 1591 return (0); 1592 } 1593 1594 static void 1595 parse_compiler(const char *spec, cw_compiler_t *compiler) 1596 { 1597 char *tspec, *token; 1598 1599 if ((tspec = strdup(spec)) == NULL) 1600 nomem(); 1601 1602 if ((token = strsep(&tspec, ",")) == NULL) 1603 errx(1, "Compiler is missing a name: %s", spec); 1604 compiler->c_name = token; 1605 1606 if ((token = strsep(&tspec, ",")) == NULL) 1607 errx(1, "Compiler is missing a path: %s", spec); 1608 compiler->c_path = token; 1609 1610 if ((token = strsep(&tspec, ",")) == NULL) 1611 errx(1, "Compiler is missing a style: %s", spec); 1612 1613 if ((strcasecmp(token, "gnu") == 0) || 1614 (strcasecmp(token, "gcc") == 0)) { 1615 compiler->c_style = GNU; 1616 } else if ((strcasecmp(token, "sun") == 0) || 1617 (strcasecmp(token, "cc") == 0)) { 1618 compiler->c_style = SUN; 1619 } else if ((strcasecmp(token, "smatch") == 0)) { 1620 compiler->c_style = SMATCH; 1621 } else { 1622 errx(1, "unknown compiler style: %s", token); 1623 } 1624 1625 if (tspec != NULL) 1626 errx(1, "Excess tokens in compiler: %s", spec); 1627 } 1628 1629 static void 1630 cleanup(cw_ictx_t *ctx) 1631 { 1632 DIR *dirp; 1633 struct dirent *dp; 1634 char buf[MAXPATHLEN]; 1635 1636 if ((dirp = opendir(ctx->i_tmpdir)) == NULL) { 1637 if (errno != ENOENT) { 1638 err(1, "couldn't open temp directory: %s", 1639 ctx->i_tmpdir); 1640 } else { 1641 return; 1642 } 1643 } 1644 1645 errno = 0; 1646 while ((dp = readdir(dirp)) != NULL) { 1647 (void) snprintf(buf, MAXPATHLEN, "%s/%s", ctx->i_tmpdir, 1648 dp->d_name); 1649 1650 if (strcmp(dp->d_name, ".") == 0 || 1651 strcmp(dp->d_name, "..") == 0) { 1652 continue; 1653 } 1654 1655 if (unlink(buf) == -1) 1656 err(1, "failed to unlink temp file: %s", dp->d_name); 1657 errno = 0; 1658 } 1659 1660 if (errno != 0) { 1661 err(1, "failed to read temporary directory: %s", 1662 ctx->i_tmpdir); 1663 } 1664 1665 (void) closedir(dirp); 1666 if (rmdir(ctx->i_tmpdir) != 0) { 1667 err(1, "failed to unlink temporary directory: %s", 1668 ctx->i_tmpdir); 1669 } 1670 } 1671 1672 int 1673 main(int argc, char **argv) 1674 { 1675 int ch; 1676 cw_compiler_t primary = { NULL, NULL, 0 }; 1677 cw_compiler_t shadows[10]; 1678 int nshadows = 0; 1679 int ret = 0; 1680 bool do_serial; 1681 bool do_exec; 1682 bool vflg = false; 1683 bool Cflg = false; 1684 bool cflg = false; 1685 bool nflg = false; 1686 char *tmpdir; 1687 1688 cw_ictx_t *main_ctx; 1689 1690 static struct option longopts[] = { 1691 { "compiler", no_argument, NULL, 'c' }, 1692 { "linker", required_argument, NULL, 'l' }, 1693 { "noecho", no_argument, NULL, 'n' }, 1694 { "primary", required_argument, NULL, 'p' }, 1695 { "shadow", required_argument, NULL, 's' }, 1696 { "versions", no_argument, NULL, 'v' }, 1697 { NULL, 0, NULL, 0 }, 1698 }; 1699 1700 1701 if ((main_ctx = newictx()) == NULL) 1702 nomem(); 1703 1704 while ((ch = getopt_long(argc, argv, "C", longopts, NULL)) != -1) { 1705 switch (ch) { 1706 case 'c': 1707 cflg = true; 1708 break; 1709 case 'C': 1710 Cflg = true; 1711 break; 1712 case 'l': 1713 if ((main_ctx->i_linker = strdup(optarg)) == NULL) 1714 nomem(); 1715 break; 1716 case 'n': 1717 nflg = true; 1718 break; 1719 case 'p': 1720 if (primary.c_path != NULL) { 1721 warnx("Only one primary compiler may " 1722 "be specified"); 1723 usage(); 1724 } 1725 1726 parse_compiler(optarg, &primary); 1727 break; 1728 case 's': 1729 if (nshadows >= 10) 1730 errx(1, "May only use 10 shadows at " 1731 "the moment"); 1732 parse_compiler(optarg, &shadows[nshadows]); 1733 nshadows++; 1734 break; 1735 case 'v': 1736 vflg = true; 1737 break; 1738 default: 1739 (void) fprintf(stderr, "Did you forget '--'?\n"); 1740 usage(); 1741 } 1742 } 1743 1744 if (primary.c_path == NULL) { 1745 warnx("A primary compiler must be specified"); 1746 usage(); 1747 } 1748 1749 do_serial = getenv("CW_SHADOW_SERIAL") != NULL; 1750 do_exec = getenv("CW_NO_EXEC") == NULL; 1751 1752 /* Leave room for argv[0] */ 1753 argc -= (optind - 1); 1754 argv += (optind - 1); 1755 1756 main_ctx->i_oldargc = argc; 1757 main_ctx->i_oldargv = argv; 1758 main_ctx->i_flags = CW_F_XLATE; 1759 if (nflg == 0) 1760 main_ctx->i_flags |= CW_F_ECHO; 1761 if (do_exec) 1762 main_ctx->i_flags |= CW_F_EXEC; 1763 if (Cflg) 1764 main_ctx->i_flags |= CW_F_CXX; 1765 main_ctx->i_compiler = &primary; 1766 1767 if (cflg) { 1768 (void) fputs(primary.c_path, stdout); 1769 } 1770 1771 if (vflg) { 1772 (void) printf("cw version %s\n", CW_VERSION); 1773 (void) fflush(stdout); 1774 main_ctx->i_flags &= ~CW_F_ECHO; 1775 main_ctx->i_flags |= CW_F_PROG | CW_F_EXEC; 1776 do_serial = 1; 1777 } 1778 1779 tmpdir = getenv("TMPDIR"); 1780 if (tmpdir == NULL) 1781 tmpdir = "/tmp"; 1782 1783 if (asprintf(&main_ctx->i_tmpdir, "%s/cw.XXXXXX", tmpdir) == -1) 1784 nomem(); 1785 1786 if ((main_ctx->i_tmpdir = mkdtemp(main_ctx->i_tmpdir)) == NULL) 1787 errx(1, "failed to create temporary directory"); 1788 1789 ret |= exec_ctx(main_ctx, do_serial); 1790 1791 for (int i = 0; i < nshadows; i++) { 1792 int r; 1793 cw_ictx_t *shadow_ctx; 1794 1795 if ((shadow_ctx = newictx()) == NULL) 1796 nomem(); 1797 1798 (void) memcpy(shadow_ctx, main_ctx, sizeof (cw_ictx_t)); 1799 1800 shadow_ctx->i_flags |= CW_F_SHADOW; 1801 shadow_ctx->i_compiler = &shadows[i]; 1802 1803 r = exec_ctx(shadow_ctx, do_serial); 1804 if (r == 0) { 1805 shadow_ctx->i_next = main_ctx->i_next; 1806 main_ctx->i_next = shadow_ctx; 1807 } 1808 ret |= r; 1809 } 1810 1811 if (!do_serial) { 1812 cw_ictx_t *next = main_ctx; 1813 while (next != NULL) { 1814 cw_ictx_t *toreap = next; 1815 next = next->i_next; 1816 ret |= reap(toreap); 1817 } 1818 } 1819 1820 cleanup(main_ctx); 1821 return (ret); 1822 } 1823