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