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 /* 530 * Enable as many warnings as exist, then disable those that we never 531 * ever want. 532 */ 533 newae(h, "-Wall"); 534 newae(h, "-Wextra"); 535 } 536 537 static void 538 optim_disable(struct aelist *h, int level) 539 { 540 if (level >= 2) { 541 newae(h, "-fno-strict-aliasing"); 542 newae(h, "-fno-unit-at-a-time"); 543 newae(h, "-fno-optimize-sibling-calls"); 544 } 545 } 546 547 /* ARGSUSED */ 548 static void 549 Xamode(struct aelist *h) 550 { 551 } 552 553 static void 554 Xcmode(struct aelist *h) 555 { 556 static int xconce; 557 558 if (xconce++) 559 return; 560 561 newae(h, "-ansi"); 562 newae(h, "-pedantic-errors"); 563 } 564 565 static void 566 Xsmode(struct aelist *h) 567 { 568 static int xsonce; 569 570 if (xsonce++) 571 return; 572 573 newae(h, "-traditional"); 574 newae(h, "-traditional-cpp"); 575 } 576 577 static void 578 usage() 579 { 580 (void) fprintf(stderr, 581 "usage: %s { -_cc | -_gcc | -_CC | -_g++ } [ -_compiler | ... ]\n", 582 progname); 583 exit(2); 584 } 585 586 static int 587 xlate_xtb(struct aelist *h, const char *xarg) 588 { 589 int i, j; 590 591 for (i = 0; i < xtbl_size; i++) { 592 if (strcmp(xtbl[i].x_arg, xarg) == 0) 593 break; 594 } 595 596 /* 597 * At the end of the table and so no matching "arg" entry 598 * found and so this must be a bad -xarch= flag. 599 */ 600 if (i == xtbl_size) 601 error(xarg); 602 603 for (j = 0; j < TRANS_ENTRY; j++) { 604 if (xtbl[i].x_trans[j] != NULL) 605 newae(h, xtbl[i].x_trans[j]); 606 else 607 break; 608 } 609 return (xtbl[i].x_flags); 610 611 } 612 613 static void 614 xlate(struct aelist *h, const char *xarg, const char **table) 615 { 616 while (*table != NULL && strcmp(xarg, *table) != 0) { 617 while (*table != NULL) 618 table++; 619 table++; 620 } 621 622 if (*table == NULL) 623 error(xarg); 624 625 table++; 626 627 while (*table != NULL) { 628 newae(h, *table); 629 table++; 630 } 631 } 632 633 static void 634 do_gcc(cw_ictx_t *ctx) 635 { 636 int c; 637 int pic = 0, nolibc = 0; 638 int in_output = 0, seen_o = 0, c_files = 0; 639 cw_op_t op = CW_O_LINK; 640 char *model = NULL; 641 int mflag = 0; 642 643 if (ctx->i_flags & CW_F_PROG) { 644 newae(ctx->i_ae, "--version"); 645 return; 646 } 647 648 newae(ctx->i_ae, "-fident"); 649 newae(ctx->i_ae, "-finline"); 650 newae(ctx->i_ae, "-fno-inline-functions"); 651 newae(ctx->i_ae, "-fno-builtin"); 652 newae(ctx->i_ae, "-fno-asm"); 653 newae(ctx->i_ae, "-fdiagnostics-show-option"); 654 newae(ctx->i_ae, "-nodefaultlibs"); 655 656 #if defined(__sparc) 657 /* 658 * The SPARC ldd and std instructions require 8-byte alignment of 659 * their address operand. gcc correctly uses them only when the 660 * ABI requires 8-byte alignment; unfortunately we have a number of 661 * pieces of buggy code that doesn't conform to the ABI. This 662 * flag makes gcc work more like Studio with -xmemalign=4. 663 */ 664 newae(ctx->i_ae, "-mno-integer-ldd-std"); 665 #endif 666 667 /* 668 * This is needed because 'u' is defined 669 * under a conditional on 'sun'. Should 670 * probably just remove the conditional, 671 * or make it be dependent on '__sun'. 672 * 673 * -Dunix is also missing in enhanced ANSI mode 674 */ 675 newae(ctx->i_ae, "-D__sun"); 676 677 /* 678 * Walk the argument list, translating as we go .. 679 */ 680 681 while (--ctx->i_oldargc > 0) { 682 char *arg = *++ctx->i_oldargv; 683 size_t arglen = strlen(arg); 684 685 if (*arg == '-') { 686 arglen--; 687 } else { 688 /* 689 * Discard inline files that gcc doesn't grok 690 */ 691 if (!in_output && arglen > 3 && 692 strcmp(arg + arglen - 3, ".il") == 0) 693 continue; 694 695 if (!in_output && arglen > 2 && 696 arg[arglen - 2] == '.' && 697 (arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' || 698 arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i')) 699 c_files++; 700 701 /* 702 * Otherwise, filenames and partial arguments 703 * are passed through for gcc to chew on. However, 704 * output is always discarded for the secondary 705 * compiler. 706 */ 707 if ((ctx->i_flags & CW_F_SHADOW) && in_output) 708 newae(ctx->i_ae, ctx->i_discard); 709 else 710 newae(ctx->i_ae, arg); 711 in_output = 0; 712 continue; 713 } 714 715 if (ctx->i_flags & CW_F_CXX) { 716 if (strncmp(arg, "-compat=", 8) == 0) { 717 /* discard -compat=4 and -compat=5 */ 718 continue; 719 } 720 if (strcmp(arg, "-Qoption") == 0) { 721 /* discard -Qoption and its two arguments */ 722 if (ctx->i_oldargc < 3) 723 error(arg); 724 ctx->i_oldargc -= 2; 725 ctx->i_oldargv += 2; 726 continue; 727 } 728 if (strcmp(arg, "-xwe") == 0) { 729 /* turn warnings into errors */ 730 newae(ctx->i_ae, "-Werror"); 731 continue; 732 } 733 if (strcmp(arg, "-noex") == 0) { 734 /* no exceptions */ 735 newae(ctx->i_ae, "-fno-exceptions"); 736 /* no run time type descriptor information */ 737 newae(ctx->i_ae, "-fno-rtti"); 738 continue; 739 } 740 if (strcmp(arg, "-pic") == 0) { 741 newae(ctx->i_ae, "-fpic"); 742 pic = 1; 743 continue; 744 } 745 if (strcmp(arg, "-PIC") == 0) { 746 newae(ctx->i_ae, "-fPIC"); 747 pic = 1; 748 continue; 749 } 750 if (strcmp(arg, "-norunpath") == 0) { 751 /* gcc has no corresponding option */ 752 continue; 753 } 754 if (strcmp(arg, "-nolib") == 0) { 755 /* -nodefaultlibs is on by default */ 756 nolibc = 1; 757 continue; 758 } 759 #if defined(__sparc) 760 if (strcmp(arg, "-cg92") == 0) { 761 mflag |= xlate_xtb(ctx->i_ae, "v8"); 762 xlate(ctx->i_ae, "super", xchip_tbl); 763 continue; 764 } 765 #endif /* __sparc */ 766 } 767 768 switch ((c = arg[1])) { 769 case '_': 770 if (strcmp(arg, "-_noecho") == 0) 771 ctx->i_flags &= ~CW_F_ECHO; 772 else if (strncmp(arg, "-_cc=", 5) == 0 || 773 strncmp(arg, "-_CC=", 5) == 0) 774 /* EMPTY */; 775 else if (strncmp(arg, "-_gcc=", 6) == 0 || 776 strncmp(arg, "-_g++=", 6) == 0) 777 newae(ctx->i_ae, arg + 6); 778 else 779 error(arg); 780 break; 781 case '#': 782 if (arglen == 1) { 783 newae(ctx->i_ae, "-v"); 784 break; 785 } 786 error(arg); 787 break; 788 case 'g': 789 newae(ctx->i_ae, "-gdwarf-2"); 790 break; 791 case 'E': 792 if (arglen == 1) { 793 newae(ctx->i_ae, "-xc"); 794 newae(ctx->i_ae, arg); 795 op = CW_O_PREPROCESS; 796 nolibc = 1; 797 break; 798 } 799 error(arg); 800 break; 801 case 'c': 802 case 'S': 803 if (arglen == 1) { 804 op = CW_O_COMPILE; 805 nolibc = 1; 806 } 807 /* FALLTHROUGH */ 808 case 'C': 809 case 'H': 810 case 'p': 811 if (arglen == 1) { 812 newae(ctx->i_ae, arg); 813 break; 814 } 815 error(arg); 816 break; 817 case 'A': 818 case 'h': 819 case 'I': 820 case 'i': 821 case 'L': 822 case 'l': 823 case 'R': 824 case 'U': 825 case 'u': 826 case 'w': 827 newae(ctx->i_ae, arg); 828 break; 829 case 'o': 830 seen_o = 1; 831 if (arglen == 1) { 832 in_output = 1; 833 newae(ctx->i_ae, arg); 834 } else if (ctx->i_flags & CW_F_SHADOW) { 835 newae(ctx->i_ae, "-o"); 836 newae(ctx->i_ae, ctx->i_discard); 837 } else { 838 newae(ctx->i_ae, arg); 839 } 840 break; 841 case 'D': 842 newae(ctx->i_ae, arg); 843 /* 844 * XXX Clearly a hack ... do we need _KADB too? 845 */ 846 if (strcmp(arg, "-D_KERNEL") == 0 || 847 strcmp(arg, "-D_BOOT") == 0) 848 newae(ctx->i_ae, "-ffreestanding"); 849 break; 850 case 'd': 851 if (arglen == 2) { 852 if (strcmp(arg, "-dy") == 0) { 853 newae(ctx->i_ae, "-Wl,-dy"); 854 break; 855 } 856 if (strcmp(arg, "-dn") == 0) { 857 newae(ctx->i_ae, "-Wl,-dn"); 858 break; 859 } 860 } 861 if (strcmp(arg, "-dalign") == 0) { 862 /* 863 * -dalign forces alignment in some cases; 864 * gcc does not need any flag to do this. 865 */ 866 break; 867 } 868 error(arg); 869 break; 870 case 'e': 871 if (strcmp(arg, 872 "-erroff=E_EMPTY_TRANSLATION_UNIT") == 0) { 873 /* 874 * Accept but ignore this -- gcc doesn't 875 * seem to complain about empty translation 876 * units 877 */ 878 break; 879 } 880 /* XX64 -- ignore all -erroff= options, for now */ 881 if (strncmp(arg, "-erroff=", 8) == 0) 882 break; 883 if (strcmp(arg, "-errtags=yes") == 0) { 884 warnings(ctx->i_ae); 885 break; 886 } 887 if (strcmp(arg, "-errwarn=%all") == 0) { 888 newae(ctx->i_ae, "-Werror"); 889 break; 890 } 891 error(arg); 892 break; 893 case 'f': 894 if (strcmp(arg, "-flags") == 0) { 895 newae(ctx->i_ae, "--help"); 896 break; 897 } 898 if (strncmp(arg, "-features=zla", 13) == 0) { 899 /* 900 * Accept but ignore this -- gcc allows 901 * zero length arrays. 902 */ 903 break; 904 } 905 error(arg); 906 break; 907 case 'G': 908 newae(ctx->i_ae, "-shared"); 909 nolibc = 1; 910 break; 911 case 'k': 912 if (strcmp(arg, "-keeptmp") == 0) { 913 newae(ctx->i_ae, "-save-temps"); 914 break; 915 } 916 error(arg); 917 break; 918 case 'K': 919 if (arglen == 1) { 920 if ((arg = *++ctx->i_oldargv) == NULL || 921 *arg == '\0') 922 error("-K"); 923 ctx->i_oldargc--; 924 } else { 925 arg += 2; 926 } 927 if (strcmp(arg, "pic") == 0) { 928 newae(ctx->i_ae, "-fpic"); 929 pic = 1; 930 break; 931 } 932 if (strcmp(arg, "PIC") == 0) { 933 newae(ctx->i_ae, "-fPIC"); 934 pic = 1; 935 break; 936 } 937 error("-K"); 938 break; 939 case 'm': 940 if (strcmp(arg, "-mt") == 0) { 941 newae(ctx->i_ae, "-D_REENTRANT"); 942 break; 943 } 944 if (strcmp(arg, "-m64") == 0) { 945 newae(ctx->i_ae, "-m64"); 946 #if defined(__x86) 947 newae(ctx->i_ae, "-mtune=opteron"); 948 #endif 949 mflag |= M64; 950 break; 951 } 952 if (strcmp(arg, "-m32") == 0) { 953 newae(ctx->i_ae, "-m32"); 954 mflag |= M32; 955 break; 956 } 957 error(arg); 958 break; 959 case 'B': /* linker options */ 960 case 'M': 961 case 'z': 962 { 963 char *opt; 964 size_t len; 965 char *s; 966 967 if (arglen == 1) { 968 opt = *++ctx->i_oldargv; 969 if (opt == NULL || *opt == '\0') 970 error(arg); 971 ctx->i_oldargc--; 972 } else { 973 opt = arg + 2; 974 } 975 len = strlen(opt) + 7; 976 if ((s = malloc(len)) == NULL) 977 nomem(); 978 (void) snprintf(s, len, "-Wl,-%c%s", c, opt); 979 newae(ctx->i_ae, s); 980 free(s); 981 } 982 break; 983 case 'n': 984 if (strcmp(arg, "-noqueue") == 0) { 985 /* 986 * Horrid license server stuff - n/a 987 */ 988 break; 989 } 990 error(arg); 991 break; 992 case 'O': 993 if (arglen == 1) { 994 newae(ctx->i_ae, "-O"); 995 break; 996 } 997 error(arg); 998 break; 999 case 'P': 1000 /* 1001 * We could do '-E -o filename.i', but that's hard, 1002 * and we don't need it for the case that's triggering 1003 * this addition. We'll require the user to specify 1004 * -o in the Makefile. If they don't they'll find out 1005 * in a hurry. 1006 */ 1007 newae(ctx->i_ae, "-E"); 1008 op = CW_O_PREPROCESS; 1009 nolibc = 1; 1010 break; 1011 case 'q': 1012 if (strcmp(arg, "-qp") == 0) { 1013 newae(ctx->i_ae, "-p"); 1014 break; 1015 } 1016 error(arg); 1017 break; 1018 case 's': 1019 if (arglen == 1) { 1020 newae(ctx->i_ae, "-Wl,-s"); 1021 break; 1022 } 1023 error(arg); 1024 break; 1025 case 't': 1026 if (arglen == 1) { 1027 newae(ctx->i_ae, "-Wl,-t"); 1028 break; 1029 } 1030 error(arg); 1031 break; 1032 case 'V': 1033 if (arglen == 1) { 1034 ctx->i_flags &= ~CW_F_ECHO; 1035 newae(ctx->i_ae, "--version"); 1036 break; 1037 } 1038 error(arg); 1039 break; 1040 case 'v': 1041 if (arglen == 1) { 1042 warnings(ctx->i_ae); 1043 break; 1044 } 1045 error(arg); 1046 break; 1047 case 'W': 1048 if (strncmp(arg, "-Wp,-xc99", 9) == 0) { 1049 /* 1050 * gcc's preprocessor will accept c99 1051 * regardless, so accept and ignore. 1052 */ 1053 break; 1054 } 1055 if (strncmp(arg, "-Wa,", 4) == 0 || 1056 strncmp(arg, "-Wp,", 4) == 0 || 1057 strncmp(arg, "-Wl,", 4) == 0) { 1058 newae(ctx->i_ae, arg); 1059 break; 1060 } 1061 if (strcmp(arg, "-W0,-xc99=pragma") == 0) { 1062 /* (undocumented) enables _Pragma */ 1063 break; 1064 } 1065 if (strcmp(arg, "-W0,-xc99=%none") == 0) { 1066 /* 1067 * This is a polite way of saying 1068 * "no c99 constructs allowed!" 1069 * For now, just accept and ignore this. 1070 */ 1071 break; 1072 } 1073 if (strcmp(arg, "-W0,-noglobal") == 0 || 1074 strcmp(arg, "-W0,-xglobalstatic") == 0) { 1075 /* 1076 * gcc doesn't prefix local symbols 1077 * in debug mode, so this is not needed. 1078 */ 1079 break; 1080 } 1081 if (strcmp(arg, "-W0,-Lt") == 0) { 1082 /* 1083 * Generate tests at the top of loops. 1084 * There is no direct gcc equivalent, ignore. 1085 */ 1086 break; 1087 } 1088 if (strcmp(arg, "-W0,-xdbggen=no%usedonly") == 0) { 1089 newae(ctx->i_ae, 1090 "-fno-eliminate-unused-debug-symbols"); 1091 newae(ctx->i_ae, 1092 "-fno-eliminate-unused-debug-types"); 1093 break; 1094 } 1095 if (strcmp(arg, "-W2,-xwrap_int") == 0) { 1096 /* 1097 * Use the legacy behaviour (pre-SS11) 1098 * for integer wrapping. 1099 * gcc does not need this. 1100 */ 1101 break; 1102 } 1103 if (strcmp(arg, "-W2,-Rcond_elim") == 0) { 1104 /* 1105 * Elimination and expansion of conditionals; 1106 * gcc has no direct equivalent. 1107 */ 1108 break; 1109 } 1110 if (strcmp(arg, "-Wd,-xsafe=unboundsym") == 0) { 1111 /* 1112 * Prevents optimizing away checks for 1113 * unbound weak symbol addresses. gcc does 1114 * not do this, so it's not needed. 1115 */ 1116 break; 1117 } 1118 if (strncmp(arg, "-Wc,-xcode=", 11) == 0) { 1119 xlate(ctx->i_ae, arg + 11, xcode_tbl); 1120 if (strncmp(arg + 11, "pic", 3) == 0) 1121 pic = 1; 1122 break; 1123 } 1124 if (strncmp(arg, "-Wc,-Qiselect", 13) == 0) { 1125 /* 1126 * Prevents insertion of register symbols. 1127 * gcc doesn't do this, so ignore it. 1128 */ 1129 break; 1130 } 1131 if (strcmp(arg, "-Wc,-Qassembler-ounrefsym=0") == 0) { 1132 /* 1133 * Prevents optimizing away of static variables. 1134 * gcc does not do this, so it's not needed. 1135 */ 1136 break; 1137 } 1138 #if defined(__x86) 1139 if (strcmp(arg, "-Wu,-xmodel=kernel") == 0) { 1140 newae(ctx->i_ae, "-ffreestanding"); 1141 newae(ctx->i_ae, "-mno-red-zone"); 1142 model = "-mcmodel=kernel"; 1143 nolibc = 1; 1144 break; 1145 } 1146 if (strcmp(arg, "-Wu,-save_args") == 0) { 1147 newae(ctx->i_ae, "-msave-args"); 1148 break; 1149 } 1150 #endif /* __x86 */ 1151 error(arg); 1152 break; 1153 case 'X': 1154 if (strcmp(arg, "-Xa") == 0 || 1155 strcmp(arg, "-Xt") == 0) { 1156 Xamode(ctx->i_ae); 1157 break; 1158 } 1159 if (strcmp(arg, "-Xc") == 0) { 1160 Xcmode(ctx->i_ae); 1161 break; 1162 } 1163 if (strcmp(arg, "-Xs") == 0) { 1164 Xsmode(ctx->i_ae); 1165 break; 1166 } 1167 error(arg); 1168 break; 1169 case 'x': 1170 if (arglen == 1) 1171 error(arg); 1172 switch (arg[2]) { 1173 #if defined(__x86) 1174 case '3': 1175 if (strcmp(arg, "-x386") == 0) { 1176 newae(ctx->i_ae, "-march=i386"); 1177 break; 1178 } 1179 error(arg); 1180 break; 1181 case '4': 1182 if (strcmp(arg, "-x486") == 0) { 1183 newae(ctx->i_ae, "-march=i486"); 1184 break; 1185 } 1186 error(arg); 1187 break; 1188 #endif /* __x86 */ 1189 case 'a': 1190 if (strncmp(arg, "-xarch=", 7) == 0) { 1191 mflag |= xlate_xtb(ctx->i_ae, arg + 7); 1192 break; 1193 } 1194 error(arg); 1195 break; 1196 case 'b': 1197 if (strncmp(arg, "-xbuiltin=", 10) == 0) { 1198 if (strcmp(arg + 10, "%all")) 1199 newae(ctx->i_ae, "-fbuiltin"); 1200 break; 1201 } 1202 error(arg); 1203 break; 1204 case 'C': 1205 /* Accept C++ style comments -- ignore */ 1206 if (strcmp(arg, "-xCC") == 0) 1207 break; 1208 error(arg); 1209 break; 1210 case 'c': 1211 if (strncmp(arg, "-xc99=%all", 10) == 0) { 1212 newae(ctx->i_ae, "-std=gnu99"); 1213 break; 1214 } 1215 if (strncmp(arg, "-xc99=%none", 11) == 0) { 1216 newae(ctx->i_ae, "-std=gnu89"); 1217 break; 1218 } 1219 if (strncmp(arg, "-xchip=", 7) == 0) { 1220 xlate(ctx->i_ae, arg + 7, xchip_tbl); 1221 break; 1222 } 1223 if (strncmp(arg, "-xcode=", 7) == 0) { 1224 xlate(ctx->i_ae, arg + 7, xcode_tbl); 1225 if (strncmp(arg + 7, "pic", 3) == 0) 1226 pic = 1; 1227 break; 1228 } 1229 if (strncmp(arg, "-xcache=", 8) == 0) 1230 break; 1231 if (strncmp(arg, "-xcrossfile", 11) == 0) 1232 break; 1233 error(arg); 1234 break; 1235 case 'd': 1236 if (strcmp(arg, "-xdepend") == 0) 1237 break; 1238 if (strncmp(arg, "-xdebugformat=", 14) == 0) 1239 break; 1240 error(arg); 1241 break; 1242 case 'F': 1243 /* 1244 * Compile for mapfile reordering, or unused 1245 * section elimination, syntax can be -xF or 1246 * more complex, like -xF=%all -- ignore. 1247 */ 1248 if (strncmp(arg, "-xF", 3) == 0) 1249 break; 1250 error(arg); 1251 break; 1252 case 'i': 1253 if (strncmp(arg, "-xinline", 8) == 0) 1254 /* No inlining; ignore */ 1255 break; 1256 if (strcmp(arg, "-xildon") == 0 || 1257 strcmp(arg, "-xildoff") == 0) 1258 /* No incremental linking; ignore */ 1259 break; 1260 error(arg); 1261 break; 1262 #if defined(__x86) 1263 case 'm': 1264 if (strcmp(arg, "-xmodel=kernel") == 0) { 1265 newae(ctx->i_ae, "-ffreestanding"); 1266 newae(ctx->i_ae, "-mno-red-zone"); 1267 model = "-mcmodel=kernel"; 1268 nolibc = 1; 1269 break; 1270 } 1271 error(arg); 1272 break; 1273 #endif /* __x86 */ 1274 case 'M': 1275 if (strcmp(arg, "-xM") == 0) { 1276 newae(ctx->i_ae, "-M"); 1277 break; 1278 } 1279 if (strcmp(arg, "-xM1") == 0) { 1280 newae(ctx->i_ae, "-MM"); 1281 break; 1282 } 1283 error(arg); 1284 break; 1285 case 'n': 1286 if (strcmp(arg, "-xnolib") == 0) { 1287 nolibc = 1; 1288 break; 1289 } 1290 error(arg); 1291 break; 1292 case 'O': 1293 if (strncmp(arg, "-xO", 3) == 0) { 1294 size_t len = strlen(arg); 1295 char *s; 1296 int c = *(arg + 3); 1297 int level; 1298 1299 if (len != 4 || !isdigit(c)) 1300 error(arg); 1301 1302 if ((s = malloc(len)) == NULL) 1303 nomem(); 1304 1305 level = atoi(arg + 3); 1306 if (level > 5) 1307 error(arg); 1308 if (level >= 2) { 1309 /* 1310 * For gcc-3.4.x at -O2 we 1311 * need to disable optimizations 1312 * that break ON. 1313 */ 1314 optim_disable(ctx->i_ae, level); 1315 /* 1316 * limit -xO3 to -O2 as well. 1317 */ 1318 level = 2; 1319 } 1320 (void) snprintf(s, len, "-O%d", level); 1321 newae(ctx->i_ae, s); 1322 free(s); 1323 break; 1324 } 1325 error(arg); 1326 break; 1327 case 'p': 1328 if (strcmp(arg, "-xpentium") == 0) { 1329 newae(ctx->i_ae, "-march=pentium"); 1330 break; 1331 } 1332 if (strcmp(arg, "-xpg") == 0) { 1333 newae(ctx->i_ae, "-pg"); 1334 break; 1335 } 1336 error(arg); 1337 break; 1338 case 'r': 1339 if (strncmp(arg, "-xregs=", 7) == 0) { 1340 xlate(ctx->i_ae, arg + 7, xregs_tbl); 1341 break; 1342 } 1343 error(arg); 1344 break; 1345 case 's': 1346 if (strcmp(arg, "-xs") == 0 || 1347 strcmp(arg, "-xspace") == 0 || 1348 strcmp(arg, "-xstrconst") == 0) 1349 break; 1350 error(arg); 1351 break; 1352 case 't': 1353 if (strcmp(arg, "-xtransition") == 0) { 1354 newae(ctx->i_ae, "-Wtransition"); 1355 break; 1356 } 1357 if (strcmp(arg, "-xtrigraphs=yes") == 0) { 1358 newae(ctx->i_ae, "-trigraphs"); 1359 break; 1360 } 1361 if (strcmp(arg, "-xtrigraphs=no") == 0) { 1362 newae(ctx->i_ae, "-notrigraphs"); 1363 break; 1364 } 1365 if (strncmp(arg, "-xtarget=", 9) == 0) { 1366 xlate(ctx->i_ae, arg + 9, xtarget_tbl); 1367 break; 1368 } 1369 error(arg); 1370 break; 1371 case 'e': 1372 case 'h': 1373 case 'l': 1374 default: 1375 error(arg); 1376 break; 1377 } 1378 break; 1379 case 'Y': 1380 if (arglen == 1) { 1381 if ((arg = *++ctx->i_oldargv) == NULL || 1382 *arg == '\0') 1383 error("-Y"); 1384 ctx->i_oldargc--; 1385 arglen = strlen(arg + 1); 1386 } else { 1387 arg += 2; 1388 } 1389 /* Just ignore -YS,... for now */ 1390 if (strncmp(arg, "S,", 2) == 0) 1391 break; 1392 if (strncmp(arg, "l,", 2) == 0) { 1393 char *s = strdup(arg); 1394 s[0] = '-'; 1395 s[1] = 'B'; 1396 newae(ctx->i_ae, s); 1397 free(s); 1398 break; 1399 } 1400 if (strncmp(arg, "I,", 2) == 0) { 1401 char *s = strdup(arg); 1402 s[0] = '-'; 1403 s[1] = 'I'; 1404 newae(ctx->i_ae, "-nostdinc"); 1405 newae(ctx->i_ae, s); 1406 free(s); 1407 break; 1408 } 1409 error(arg); 1410 break; 1411 case 'Q': 1412 /* 1413 * We could map -Qy into -Wl,-Qy etc. 1414 */ 1415 default: 1416 error(arg); 1417 break; 1418 } 1419 } 1420 1421 if (c_files > 1 && (ctx->i_flags & CW_F_SHADOW) && 1422 op != CW_O_PREPROCESS) { 1423 (void) fprintf(stderr, "%s: error: multiple source files are " 1424 "allowed only with -E or -P\n", progname); 1425 exit(2); 1426 } 1427 1428 /* 1429 * Make sure that we do not have any unintended interactions between 1430 * the xarch options passed in and the version of the Studio compiler 1431 * used. 1432 */ 1433 if ((mflag & (SS11|SS12)) == (SS11|SS12)) { 1434 (void) fprintf(stderr, 1435 "Conflicting \"-xarch=\" flags (both Studio 11 and 12)\n"); 1436 exit(2); 1437 } 1438 1439 switch (mflag) { 1440 case 0: 1441 /* FALLTHROUGH */ 1442 case M32: 1443 #if defined(__sparc) 1444 /* 1445 * Only -m32 is defined and so put in the missing xarch 1446 * translation. 1447 */ 1448 newae(ctx->i_ae, "-mcpu=v8"); 1449 newae(ctx->i_ae, "-mno-v8plus"); 1450 #endif 1451 break; 1452 case M64: 1453 #if defined(__sparc) 1454 /* 1455 * Only -m64 is defined and so put in the missing xarch 1456 * translation. 1457 */ 1458 newae(ctx->i_ae, "-mcpu=v9"); 1459 #endif 1460 break; 1461 case SS12: 1462 #if defined(__sparc) 1463 /* no -m32/-m64 flag used - this is an error for sparc builds */ 1464 (void) fprintf(stderr, "No -m32/-m64 flag defined\n"); 1465 exit(2); 1466 #endif 1467 break; 1468 case SS11: 1469 /* FALLTHROUGH */ 1470 case (SS11|M32): 1471 case (SS11|M64): 1472 break; 1473 case (SS12|M32): 1474 #if defined(__sparc) 1475 /* 1476 * Need to add in further 32 bit options because with SS12 1477 * the xarch=sparcvis option can be applied to 32 or 64 1478 * bit, and so the translatation table (xtbl) cannot handle 1479 * that. 1480 */ 1481 newae(ctx->i_ae, "-mv8plus"); 1482 #endif 1483 break; 1484 case (SS12|M64): 1485 break; 1486 default: 1487 (void) fprintf(stderr, 1488 "Incompatible -xarch= and/or -m32/-m64 options used.\n"); 1489 exit(2); 1490 } 1491 if (op == CW_O_LINK && (ctx->i_flags & CW_F_SHADOW)) 1492 exit(0); 1493 1494 if (model && !pic) 1495 newae(ctx->i_ae, model); 1496 if (!nolibc) 1497 newae(ctx->i_ae, "-lc"); 1498 if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) { 1499 newae(ctx->i_ae, "-o"); 1500 newae(ctx->i_ae, ctx->i_discard); 1501 } 1502 } 1503 1504 static void 1505 do_cc(cw_ictx_t *ctx) 1506 { 1507 int in_output = 0, seen_o = 0; 1508 cw_op_t op = CW_O_LINK; 1509 1510 if (ctx->i_flags & CW_F_PROG) { 1511 newae(ctx->i_ae, "-V"); 1512 return; 1513 } 1514 1515 while (--ctx->i_oldargc > 0) { 1516 char *arg = *++ctx->i_oldargv; 1517 1518 if (*arg != '-') { 1519 if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) { 1520 newae(ctx->i_ae, arg); 1521 } else { 1522 in_output = 0; 1523 newae(ctx->i_ae, ctx->i_discard); 1524 } 1525 continue; 1526 } 1527 switch (*(arg + 1)) { 1528 case '_': 1529 if (strcmp(arg, "-_noecho") == 0) { 1530 ctx->i_flags &= ~CW_F_ECHO; 1531 } else if (strncmp(arg, "-_cc=", 5) == 0 || 1532 strncmp(arg, "-_CC=", 5) == 0) { 1533 newae(ctx->i_ae, arg + 5); 1534 } else if (strncmp(arg, "-_gcc=", 6) != 0 && 1535 strncmp(arg, "-_g++=", 6) != 0) { 1536 (void) fprintf(stderr, 1537 "%s: invalid argument '%s'\n", progname, 1538 arg); 1539 exit(2); 1540 } 1541 break; 1542 case 'V': 1543 ctx->i_flags &= ~CW_F_ECHO; 1544 newae(ctx->i_ae, arg); 1545 break; 1546 case 'o': 1547 seen_o = 1; 1548 if (strlen(arg) == 2) { 1549 in_output = 1; 1550 newae(ctx->i_ae, arg); 1551 } else if (ctx->i_flags & CW_F_SHADOW) { 1552 newae(ctx->i_ae, "-o"); 1553 newae(ctx->i_ae, ctx->i_discard); 1554 } else { 1555 newae(ctx->i_ae, arg); 1556 } 1557 break; 1558 case 'c': 1559 case 'S': 1560 if (strlen(arg) == 2) 1561 op = CW_O_COMPILE; 1562 newae(ctx->i_ae, arg); 1563 break; 1564 case 'E': 1565 case 'P': 1566 if (strlen(arg) == 2) 1567 op = CW_O_PREPROCESS; 1568 /*FALLTHROUGH*/ 1569 default: 1570 newae(ctx->i_ae, arg); 1571 } 1572 } 1573 1574 if ((op == CW_O_LINK || op == CW_O_PREPROCESS) && 1575 (ctx->i_flags & CW_F_SHADOW)) 1576 exit(0); 1577 1578 if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) { 1579 newae(ctx->i_ae, "-o"); 1580 newae(ctx->i_ae, ctx->i_discard); 1581 } 1582 } 1583 1584 static void 1585 prepctx(cw_ictx_t *ctx) 1586 { 1587 const char *dir = NULL, *cmd; 1588 char *program = NULL; 1589 size_t len; 1590 1591 switch (CIDX(CC(ctx), ctx->i_flags)) { 1592 case CIDX(CW_C_CC, 0): 1593 program = getenv("CW_CC"); 1594 dir = getenv("CW_CC_DIR"); 1595 break; 1596 case CIDX(CW_C_CC, CW_F_CXX): 1597 program = getenv("CW_CPLUSPLUS"); 1598 dir = getenv("CW_CPLUSPLUS_DIR"); 1599 break; 1600 case CIDX(CW_C_GCC, 0): 1601 program = getenv("CW_GCC"); 1602 dir = getenv("CW_GCC_DIR"); 1603 break; 1604 case CIDX(CW_C_GCC, CW_F_CXX): 1605 program = getenv("CW_GPLUSPLUS"); 1606 dir = getenv("CW_GPLUSPLUS_DIR"); 1607 break; 1608 } 1609 1610 if (program == NULL) { 1611 if (dir == NULL) 1612 dir = default_dir[CC(ctx)]; 1613 cmd = cmds[CIDX(CC(ctx), ctx->i_flags)]; 1614 len = strlen(dir) + strlen(cmd) + 2; 1615 if ((program = malloc(len)) == NULL) 1616 nomem(); 1617 (void) snprintf(program, len, "%s/%s", dir, cmd); 1618 } 1619 1620 newae(ctx->i_ae, program); 1621 1622 if (ctx->i_flags & CW_F_PROG) { 1623 (void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ? 1624 "shadow" : "primary", program); 1625 (void) fflush(stdout); 1626 } 1627 1628 if (!(ctx->i_flags & CW_F_XLATE)) 1629 return; 1630 1631 switch (CC(ctx)) { 1632 case CW_C_CC: 1633 do_cc(ctx); 1634 break; 1635 case CW_C_GCC: 1636 do_gcc(ctx); 1637 break; 1638 } 1639 } 1640 1641 static int 1642 invoke(cw_ictx_t *ctx) 1643 { 1644 char **newargv; 1645 int ac; 1646 struct ae *a; 1647 1648 if ((newargv = calloc(sizeof (*newargv), ctx->i_ae->ael_argc + 1)) == 1649 NULL) 1650 nomem(); 1651 1652 if (ctx->i_flags & CW_F_ECHO) 1653 (void) fprintf(stderr, "+ "); 1654 1655 for (ac = 0, a = ctx->i_ae->ael_head; a; a = a->ae_next, ac++) { 1656 newargv[ac] = a->ae_arg; 1657 if (ctx->i_flags & CW_F_ECHO) 1658 (void) fprintf(stderr, "%s ", a->ae_arg); 1659 if (a == ctx->i_ae->ael_tail) 1660 break; 1661 } 1662 1663 if (ctx->i_flags & CW_F_ECHO) { 1664 (void) fprintf(stderr, "\n"); 1665 (void) fflush(stderr); 1666 } 1667 1668 if (!(ctx->i_flags & CW_F_EXEC)) 1669 return (0); 1670 1671 /* 1672 * We must fix up the environment here so that the 1673 * dependency files are not trampled by the shadow compiler. 1674 */ 1675 if ((ctx->i_flags & CW_F_SHADOW) && 1676 (unsetenv("SUNPRO_DEPENDENCIES") != 0 || 1677 unsetenv("DEPENDENCIES_OUTPUT") != 0)) { 1678 (void) fprintf(stderr, "error: environment setup failed: %s\n", 1679 strerror(errno)); 1680 return (-1); 1681 } 1682 1683 (void) execv(newargv[0], newargv); 1684 cw_perror("couldn't run %s", newargv[0]); 1685 1686 return (-1); 1687 } 1688 1689 static int 1690 reap(cw_ictx_t *ctx) 1691 { 1692 int status, ret = 0; 1693 char buf[1024]; 1694 struct stat s; 1695 1696 /* 1697 * Only wait for one specific child. 1698 */ 1699 if (ctx->i_pid <= 0) 1700 return (-1); 1701 1702 do { 1703 if (waitpid(ctx->i_pid, &status, 0) < 0) { 1704 cw_perror("cannot reap child"); 1705 return (-1); 1706 } 1707 if (status != 0) { 1708 if (WIFSIGNALED(status)) { 1709 ret = -WTERMSIG(status); 1710 break; 1711 } else if (WIFEXITED(status)) { 1712 ret = WEXITSTATUS(status); 1713 break; 1714 } 1715 } 1716 } while (!WIFEXITED(status) && !WIFSIGNALED(status)); 1717 1718 (void) unlink(ctx->i_discard); 1719 1720 if (stat(ctx->i_stderr, &s) < 0) { 1721 cw_perror("stat failed on child cleanup"); 1722 return (-1); 1723 } 1724 if (s.st_size != 0) { 1725 FILE *f; 1726 1727 if ((f = fopen(ctx->i_stderr, "r")) != NULL) { 1728 while (fgets(buf, sizeof (buf), f)) 1729 (void) fprintf(stderr, "%s", buf); 1730 (void) fflush(stderr); 1731 (void) fclose(f); 1732 } 1733 } 1734 (void) unlink(ctx->i_stderr); 1735 free(ctx->i_stderr); 1736 1737 /* 1738 * cc returns an error code when given -V; we want that to succeed. 1739 */ 1740 if (ctx->i_flags & CW_F_PROG) 1741 return (0); 1742 1743 return (ret); 1744 } 1745 1746 static int 1747 exec_ctx(cw_ictx_t *ctx, int block) 1748 { 1749 char *file; 1750 1751 /* 1752 * To avoid offending cc's sensibilities, the name of its output 1753 * file must end in '.o'. 1754 */ 1755 if ((file = tempnam(NULL, ".cw")) == NULL) { 1756 nomem(); 1757 return (-1); 1758 } 1759 (void) strlcpy(ctx->i_discard, file, MAXPATHLEN); 1760 (void) strlcat(ctx->i_discard, ".o", MAXPATHLEN); 1761 free(file); 1762 1763 if ((ctx->i_stderr = tempnam(NULL, ".cw")) == NULL) { 1764 nomem(); 1765 return (-1); 1766 } 1767 1768 if ((ctx->i_pid = fork()) == 0) { 1769 int fd; 1770 1771 (void) fclose(stderr); 1772 if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL, 1773 0666)) < 0) { 1774 cw_perror("open failed for standard error"); 1775 exit(1); 1776 } 1777 if (dup2(fd, 2) < 0) { 1778 cw_perror("dup2 failed for standard error"); 1779 exit(1); 1780 } 1781 if (fd != 2) 1782 (void) close(fd); 1783 if (freopen("/dev/fd/2", "w", stderr) == NULL) { 1784 cw_perror("freopen failed for /dev/fd/2"); 1785 exit(1); 1786 } 1787 prepctx(ctx); 1788 exit(invoke(ctx)); 1789 } 1790 1791 if (ctx->i_pid < 0) { 1792 cw_perror("fork failed"); 1793 return (1); 1794 } 1795 1796 if (block) 1797 return (reap(ctx)); 1798 1799 return (0); 1800 } 1801 1802 int 1803 main(int argc, char **argv) 1804 { 1805 cw_ictx_t *ctx = newictx(); 1806 cw_ictx_t *ctx_shadow = newictx(); 1807 const char *dir; 1808 int do_serial, do_shadow; 1809 int ret = 0; 1810 1811 if ((progname = strrchr(argv[0], '/')) == NULL) 1812 progname = argv[0]; 1813 else 1814 progname++; 1815 1816 if (ctx == NULL || ctx_shadow == NULL) 1817 nomem(); 1818 1819 ctx->i_flags = CW_F_ECHO|CW_F_XLATE; 1820 1821 /* 1822 * Figure out where to get our tools from. This depends on 1823 * the environment variables set at run time. 1824 */ 1825 if ((dir = getenv("SPRO_VROOT")) != NULL) { 1826 (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN, 1827 "%s/bin", dir); 1828 } else if ((dir = getenv("SPRO_ROOT")) != NULL) { 1829 (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN, 1830 "%s/SS12/bin", dir); 1831 } else if ((dir = getenv("BUILD_TOOLS")) != NULL) { 1832 (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN, 1833 "%s/SUNWspro/SS12/bin", dir); 1834 } 1835 1836 if ((dir = getenv("GCC_ROOT")) != NULL) { 1837 (void) snprintf(default_dir[CW_C_GCC], MAXPATHLEN, 1838 "%s/bin", dir); 1839 } 1840 1841 do_shadow = (getenv("CW_NO_SHADOW") ? 0 : 1); 1842 do_serial = (getenv("CW_SHADOW_SERIAL") ? 1 : 0); 1843 1844 if (getenv("CW_NO_EXEC") == NULL) 1845 ctx->i_flags |= CW_F_EXEC; 1846 1847 /* 1848 * The first argument must be one of "-_cc", "-_gcc", "-_CC", or "-_g++" 1849 */ 1850 if (argc == 1) 1851 usage(); 1852 argc--; 1853 argv++; 1854 if (strcmp(argv[0], "-_cc") == 0) { 1855 ctx->i_compiler = CW_C_CC; 1856 } else if (strcmp(argv[0], "-_gcc") == 0) { 1857 ctx->i_compiler = CW_C_GCC; 1858 } else if (strcmp(argv[0], "-_CC") == 0) { 1859 ctx->i_compiler = CW_C_CC; 1860 ctx->i_flags |= CW_F_CXX; 1861 } else if (strcmp(argv[0], "-_g++") == 0) { 1862 ctx->i_compiler = CW_C_GCC; 1863 ctx->i_flags |= CW_F_CXX; 1864 } else { 1865 /* assume "-_gcc" by default */ 1866 argc++; 1867 argv--; 1868 ctx->i_compiler = CW_C_GCC; 1869 } 1870 1871 /* 1872 * -_compiler - tell us the path to the primary compiler only 1873 */ 1874 if (argc > 1 && strcmp(argv[1], "-_compiler") == 0) { 1875 ctx->i_flags &= ~CW_F_XLATE; 1876 prepctx(ctx); 1877 (void) printf("%s\n", ctx->i_ae->ael_head->ae_arg); 1878 return (0); 1879 } 1880 1881 /* 1882 * -_versions - tell us the cw version, paths to all compilers, and 1883 * ask each for its version if we know how. 1884 */ 1885 if (argc > 1 && strcmp(argv[1], "-_versions") == 0) { 1886 (void) printf("cw version %s", CW_VERSION); 1887 if (!do_shadow) 1888 (void) printf(" (SHADOW MODE DISABLED)"); 1889 (void) printf("\n"); 1890 (void) fflush(stdout); 1891 ctx->i_flags &= ~CW_F_ECHO; 1892 ctx->i_flags |= CW_F_PROG|CW_F_EXEC; 1893 argc--; 1894 argv++; 1895 do_serial = 1; 1896 } 1897 1898 ctx->i_oldargc = argc; 1899 ctx->i_oldargv = argv; 1900 1901 ret |= exec_ctx(ctx, do_serial); 1902 1903 if (do_shadow) { 1904 (void) memcpy(ctx_shadow, ctx, sizeof (cw_ictx_t)); 1905 ctx_shadow->i_flags |= CW_F_SHADOW; 1906 ret |= exec_ctx(ctx_shadow, 1); 1907 } 1908 1909 if (!do_serial) 1910 ret |= reap(ctx); 1911 1912 return (ret); 1913 }