1aa9ef484SJohn Levon 27c478bd9Sstevel@tonic-gate /* 37c478bd9Sstevel@tonic-gate * CDDL HEADER START 47c478bd9Sstevel@tonic-gate * 57c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 602e56f3fSwesolows * Common Development and Distribution License (the "License"). 702e56f3fSwesolows * You may not use this file except in compliance with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 2202e56f3fSwesolows 2325c28e83SPiotr Jasiukajtis /* 24aa9ef484SJohn Levon * Copyright 2018, Richard Lowe. 2525c28e83SPiotr Jasiukajtis */ 267c478bd9Sstevel@tonic-gate /* 277fbf8d03SScott Rotondo * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 287c478bd9Sstevel@tonic-gate * Use is subject to license terms. 29*1f5207b7SJohn Levon * 30*1f5207b7SJohn Levon * Copyright 2019 Joyent, Inc. 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate /* 347c478bd9Sstevel@tonic-gate * Wrapper for the GNU C compiler to make it accept the Sun C compiler 357c478bd9Sstevel@tonic-gate * arguments where possible. 367c478bd9Sstevel@tonic-gate * 377c478bd9Sstevel@tonic-gate * Since the translation is inexact, this is something of a work-in-progress. 389a70fc3bSMark J. Nelson * 397c478bd9Sstevel@tonic-gate */ 407c478bd9Sstevel@tonic-gate 419a70fc3bSMark J. Nelson /* If you modify this file, you must increment CW_VERSION */ 42fb23c574SRichard Lowe #define CW_VERSION "3.0" 439a70fc3bSMark J. Nelson 447c478bd9Sstevel@tonic-gate /* 457c478bd9Sstevel@tonic-gate * -# Verbose mode 467c478bd9Sstevel@tonic-gate * -### Show compiler commands built by driver, no compilation 477c478bd9Sstevel@tonic-gate * -A<name[(tokens)]> Preprocessor predicate assertion 487c478bd9Sstevel@tonic-gate * -B<[static|dynamic]> Specify dynamic or static binding 497c478bd9Sstevel@tonic-gate * -C Prevent preprocessor from removing comments 507c478bd9Sstevel@tonic-gate * -c Compile only - produce .o files, suppress linking 517c478bd9Sstevel@tonic-gate * -cg92 Alias for -xtarget=ss1000 527c478bd9Sstevel@tonic-gate * -D<name[=token]> Associate name with token as if by #define 537c478bd9Sstevel@tonic-gate * -d[y|n] dynamic [-dy] or static [-dn] option to linker 547c478bd9Sstevel@tonic-gate * -E Compile source through preprocessor only, output to stdout 557c478bd9Sstevel@tonic-gate * -erroff=<t> Suppress warnings specified by tags t(%none, %all, <tag list>) 567c478bd9Sstevel@tonic-gate * -errtags=<a> Display messages with tags a(no, yes) 577c478bd9Sstevel@tonic-gate * -errwarn=<t> Treats warnings specified by tags t(%none, %all, <tag list>) 587c478bd9Sstevel@tonic-gate * as errors 597c478bd9Sstevel@tonic-gate * -fast Optimize using a selection of options 607c478bd9Sstevel@tonic-gate * -fd Report old-style function definitions and declarations 617c478bd9Sstevel@tonic-gate * -fnonstd Initialize floating-point hardware to non-standard preferences 627c478bd9Sstevel@tonic-gate * -fns[=<yes|no>] Select non-standard floating point mode 637c478bd9Sstevel@tonic-gate * -fprecision=<p> Set FP rounding precision mode p(single, double, extended) 647c478bd9Sstevel@tonic-gate * -fround=<r> Select the IEEE rounding mode in effect at startup 657c478bd9Sstevel@tonic-gate * -fsimple[=<n>] Select floating-point optimization preferences <n> 667c478bd9Sstevel@tonic-gate * -fsingle Use single-precision arithmetic (-Xt and -Xs modes only) 677c478bd9Sstevel@tonic-gate * -ftrap=<t> Select floating-point trapping mode in effect at startup 687c478bd9Sstevel@tonic-gate * -fstore force floating pt. values to target precision on assignment 697c478bd9Sstevel@tonic-gate * -G Build a dynamic shared library 707c478bd9Sstevel@tonic-gate * -g Compile for debugging 717c478bd9Sstevel@tonic-gate * -H Print path name of each file included during compilation 727c478bd9Sstevel@tonic-gate * -h <name> Assign <name> to generated dynamic shared library 737c478bd9Sstevel@tonic-gate * -I<dir> Add <dir> to preprocessor #include file search path 747c478bd9Sstevel@tonic-gate * -i Passed to linker to ignore any LD_LIBRARY_PATH setting 757c478bd9Sstevel@tonic-gate * -keeptmp Keep temporary files created during compilation 767c478bd9Sstevel@tonic-gate * -L<dir> Pass to linker to add <dir> to the library search path 777c478bd9Sstevel@tonic-gate * -l<name> Link with library lib<name>.a or lib<name>.so 787c478bd9Sstevel@tonic-gate * -mc Remove duplicate strings from .comment section of output files 797c478bd9Sstevel@tonic-gate * -mr Remove all strings from .comment section of output files 807c478bd9Sstevel@tonic-gate * -mr,"string" Remove all strings and append "string" to .comment section 817c478bd9Sstevel@tonic-gate * -mt Specify options needed when compiling multi-threaded code 827c478bd9Sstevel@tonic-gate * -native Find available processor, generate code accordingly 837c478bd9Sstevel@tonic-gate * -nofstore Do not force floating pt. values to target precision 847c478bd9Sstevel@tonic-gate * on assignment 857c478bd9Sstevel@tonic-gate * -norunpath Do not build in a runtime path for shared libraries 86662492f5Ssherrym * -O Use default optimization level (-xO2 or -xO3. Check man page.) 877c478bd9Sstevel@tonic-gate * -o <outputfile> Set name of output file to <outputfile> 887c478bd9Sstevel@tonic-gate * -P Compile source through preprocessor only, output to .i file 897c478bd9Sstevel@tonic-gate * -p Compile for profiling with prof 907c478bd9Sstevel@tonic-gate * -Q[y|n] Emit/don't emit identification info to output file 917c478bd9Sstevel@tonic-gate * -R<dir[:dir]> Build runtime search path list into executable 927c478bd9Sstevel@tonic-gate * -S Compile and only generate assembly code (.s) 937c478bd9Sstevel@tonic-gate * -s Strip symbol table from the executable file 94159cf8a6Swesolows * -t Turn off duplicate symbol warnings when linking 957c478bd9Sstevel@tonic-gate * -U<name> Delete initial definition of preprocessor symbol <name> 967c478bd9Sstevel@tonic-gate * -V Report version number of each compilation phase 977c478bd9Sstevel@tonic-gate * -v Do stricter semantic checking 987c478bd9Sstevel@tonic-gate * -W<c>,<arg> Pass <arg> to specified component <c> (a,l,m,p,0,2,h,i,u) 997c478bd9Sstevel@tonic-gate * -w Suppress compiler warning messages 1007c478bd9Sstevel@tonic-gate * -Xa Compile assuming ANSI C conformance, allow K & R extensions 1017c478bd9Sstevel@tonic-gate * (default mode) 1027c478bd9Sstevel@tonic-gate * -Xs Compile assuming (pre-ANSI) K & R C style code 1037c478bd9Sstevel@tonic-gate * -Xt Compile assuming K & R conformance, allow ANSI C 1047c478bd9Sstevel@tonic-gate * -xarch=<a> Specify target architecture instruction set 1057c478bd9Sstevel@tonic-gate * -xbuiltin[=<b>] When profitable inline, or substitute intrinisic functions 1067c478bd9Sstevel@tonic-gate * for system functions, b={%all,%none} 1077c478bd9Sstevel@tonic-gate * -xCC Accept C++ style comments 1087c478bd9Sstevel@tonic-gate * -xchip=<c> Specify the target processor for use by the optimizer 1097c478bd9Sstevel@tonic-gate * -xcode=<c> Generate different code for forming addresses 1107c478bd9Sstevel@tonic-gate * -xcrossfile[=<n>] Enable optimization and inlining across source files, 1117c478bd9Sstevel@tonic-gate * n={0|1} 1127c478bd9Sstevel@tonic-gate * -xe Perform only syntax/semantic checking, no code generation 113f795e658Srie * -xF Compile for later mapfile reordering or unused section 114f795e658Srie * elimination 1157c478bd9Sstevel@tonic-gate * -xhelp=<f> Display on-line help information f(flags, readme, errors) 1167c478bd9Sstevel@tonic-gate * -xildoff Cancel -xildon 1177c478bd9Sstevel@tonic-gate * -xildon Enable use of the incremental linker, ild 1187c478bd9Sstevel@tonic-gate * -xinline=[<a>,...,<a>] Attempt inlining of specified user routines, 1197c478bd9Sstevel@tonic-gate * <a>={%auto,func,no%func} 1207c478bd9Sstevel@tonic-gate * -xlibmieee Force IEEE 754 return values for math routines in 1217c478bd9Sstevel@tonic-gate * exceptional cases 1227c478bd9Sstevel@tonic-gate * -xlibmil Inline selected libm math routines for optimization 1237c478bd9Sstevel@tonic-gate * -xlic_lib=sunperf Link in the Sun supplied performance libraries 1247c478bd9Sstevel@tonic-gate * -xlicinfo Show license server information 1257c478bd9Sstevel@tonic-gate * -xmaxopt=[off,1,2,3,4,5] maximum optimization level allowed on #pragma opt 1267c478bd9Sstevel@tonic-gate * -xO<n> Generate optimized code (n={1|2|3|4|5}) 1277c478bd9Sstevel@tonic-gate * -xP Print prototypes for function definitions 1287c478bd9Sstevel@tonic-gate * -xprofile=<p> Collect data for a profile or use a profile to optimize 1297c478bd9Sstevel@tonic-gate * <p>={{collect,use}[:<path>],tcov} 1307c478bd9Sstevel@tonic-gate * -xregs=<r> Control register allocation 1317c478bd9Sstevel@tonic-gate * -xs Allow debugging without object (.o) files 1327c478bd9Sstevel@tonic-gate * -xsb Compile for use with the WorkShop source browser 1337c478bd9Sstevel@tonic-gate * -xsbfast Generate only WorkShop source browser info, no compilation 1347c478bd9Sstevel@tonic-gate * -xsfpconst Represent unsuffixed floating point constants as single 1357c478bd9Sstevel@tonic-gate * precision 1367c478bd9Sstevel@tonic-gate * -xspace Do not do optimizations that increase code size 1377c478bd9Sstevel@tonic-gate * -xstrconst Place string literals into read-only data segment 1387c478bd9Sstevel@tonic-gate * -xtarget=<t> Specify target system for optimization 1397c478bd9Sstevel@tonic-gate * -xtemp=<dir> Set directory for temporary files to <dir> 1407c478bd9Sstevel@tonic-gate * -xtime Report the execution time for each compilation phase 1417c478bd9Sstevel@tonic-gate * -xunroll=n Enable unrolling loops n times where possible 1427c478bd9Sstevel@tonic-gate * -Y<c>,<dir> Specify <dir> for location of component <c> (a,l,m,p,0,h,i,u) 1437c478bd9Sstevel@tonic-gate * -YA,<dir> Change default directory searched for components 1447c478bd9Sstevel@tonic-gate * -YI,<dir> Change default directory searched for include files 1457c478bd9Sstevel@tonic-gate * -YP,<dir> Change default directory for finding libraries files 1467c478bd9Sstevel@tonic-gate * -YS,<dir> Change default directory for startup object files 1477c478bd9Sstevel@tonic-gate */ 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate /* 1507c478bd9Sstevel@tonic-gate * Translation table: 1517c478bd9Sstevel@tonic-gate */ 1527c478bd9Sstevel@tonic-gate /* 1537c478bd9Sstevel@tonic-gate * -# -v 1547c478bd9Sstevel@tonic-gate * -### error 1557c478bd9Sstevel@tonic-gate * -A<name[(tokens)]> pass-thru 1567c478bd9Sstevel@tonic-gate * -B<[static|dynamic]> pass-thru (syntax error for anything else) 1577c478bd9Sstevel@tonic-gate * -C pass-thru 1587c478bd9Sstevel@tonic-gate * -c pass-thru 1597c478bd9Sstevel@tonic-gate * -cg92 -m32 -mcpu=v8 -mtune=supersparc (SPARC only) 1607c478bd9Sstevel@tonic-gate * -D<name[=token]> pass-thru 1617c478bd9Sstevel@tonic-gate * -dy or -dn -Wl,-dy or -Wl,-dn 1627c478bd9Sstevel@tonic-gate * -E pass-thru 1637c478bd9Sstevel@tonic-gate * -erroff=E_EMPTY_TRANSLATION_UNIT ignore 1647c478bd9Sstevel@tonic-gate * -errtags=%all -Wall 1657c478bd9Sstevel@tonic-gate * -errwarn=%all -Werror else -Wno-error 1667c478bd9Sstevel@tonic-gate * -fast error 1677c478bd9Sstevel@tonic-gate * -fd error 1687c478bd9Sstevel@tonic-gate * -fnonstd error 1697c478bd9Sstevel@tonic-gate * -fns[=<yes|no>] error 1707c478bd9Sstevel@tonic-gate * -fprecision=<p> error 1717c478bd9Sstevel@tonic-gate * -fround=<r> error 1727c478bd9Sstevel@tonic-gate * -fsimple[=<n>] error 1737c478bd9Sstevel@tonic-gate * -fsingle[=<n>] error 1747c478bd9Sstevel@tonic-gate * -ftrap=<t> error 1757c478bd9Sstevel@tonic-gate * -fstore error 1767c478bd9Sstevel@tonic-gate * -G pass-thru 1777c478bd9Sstevel@tonic-gate * -g pass-thru 1787c478bd9Sstevel@tonic-gate * -H pass-thru 1797c478bd9Sstevel@tonic-gate * -h <name> pass-thru 1807c478bd9Sstevel@tonic-gate * -I<dir> pass-thru 1817c478bd9Sstevel@tonic-gate * -i pass-thru 1827c478bd9Sstevel@tonic-gate * -keeptmp -save-temps 1837c478bd9Sstevel@tonic-gate * -L<dir> pass-thru 1847c478bd9Sstevel@tonic-gate * -l<name> pass-thru 1857c478bd9Sstevel@tonic-gate * -mc error 1867c478bd9Sstevel@tonic-gate * -mr error 1877c478bd9Sstevel@tonic-gate * -mr,"string" error 1887c478bd9Sstevel@tonic-gate * -mt -D_REENTRANT 1897c478bd9Sstevel@tonic-gate * -native error 1907c478bd9Sstevel@tonic-gate * -nofstore error 1917c478bd9Sstevel@tonic-gate * -nolib -nodefaultlibs 1927c478bd9Sstevel@tonic-gate * -norunpath ignore 193662492f5Ssherrym * -O -O1 (Check the man page to be certain) 1947c478bd9Sstevel@tonic-gate * -o <outputfile> pass-thru 1957c478bd9Sstevel@tonic-gate * -P -E -o filename.i (or error) 1967c478bd9Sstevel@tonic-gate * -p pass-thru 1977c478bd9Sstevel@tonic-gate * -Q[y|n] error 1987c478bd9Sstevel@tonic-gate * -R<dir[:dir]> pass-thru 1997c478bd9Sstevel@tonic-gate * -S pass-thru 2007c478bd9Sstevel@tonic-gate * -s -Wl,-s 201159cf8a6Swesolows * -t -Wl,-t 2027c478bd9Sstevel@tonic-gate * -U<name> pass-thru 2037c478bd9Sstevel@tonic-gate * -V --version 2047c478bd9Sstevel@tonic-gate * -v -Wall 2057c478bd9Sstevel@tonic-gate * -Wa,<arg> pass-thru 2067c478bd9Sstevel@tonic-gate * -Wp,<arg> pass-thru except -xc99=<a> 2077c478bd9Sstevel@tonic-gate * -Wl,<arg> pass-thru 2087c478bd9Sstevel@tonic-gate * -W{m,0,2,h,i,u> error/ignore 20954836668Spetede * -xmodel=kernel -ffreestanding -mcmodel=kernel -mno-red-zone 210d430274bSsherrym * -Wu,-save_args -msave-args 2117c478bd9Sstevel@tonic-gate * -w pass-thru 2127c478bd9Sstevel@tonic-gate * -Xa -std=iso9899:199409 or -ansi 2137c478bd9Sstevel@tonic-gate * -Xt error 2147c478bd9Sstevel@tonic-gate * -Xs -traditional -std=c89 2157c478bd9Sstevel@tonic-gate * -xarch=<a> table 21602e56f3fSwesolows * -xbuiltin[=<b>] -fbuiltin (-fno-builtin otherwise) 2177c478bd9Sstevel@tonic-gate * -xCC ignore 2187c478bd9Sstevel@tonic-gate * -xchip=<c> table 2197c478bd9Sstevel@tonic-gate * -xcode=<c> table 2208c1a3716Ssherrym * -xdebugformat=<format> ignore (always use dwarf-2 for gcc) 221159cf8a6Swesolows * -xcrossfile[=<n>] ignore 2227c478bd9Sstevel@tonic-gate * -xe error 2237c478bd9Sstevel@tonic-gate * -xF error 2247c478bd9Sstevel@tonic-gate * -xhelp=<f> error 2257c478bd9Sstevel@tonic-gate * -xildoff ignore 2267c478bd9Sstevel@tonic-gate * -xildon ignore 2277c478bd9Sstevel@tonic-gate * -xinline ignore 2287c478bd9Sstevel@tonic-gate * -xlibmieee error 2297c478bd9Sstevel@tonic-gate * -xlibmil error 2307c478bd9Sstevel@tonic-gate * -xlic_lib=sunperf error 2317c478bd9Sstevel@tonic-gate * -xmaxopt=[...] error 2327c478bd9Sstevel@tonic-gate * -xO<n> -O<n> 2337c478bd9Sstevel@tonic-gate * -xP error 2347c478bd9Sstevel@tonic-gate * -xprofile=<p> error 2357c478bd9Sstevel@tonic-gate * -xregs=<r> table 2367c478bd9Sstevel@tonic-gate * -xs error 2377c478bd9Sstevel@tonic-gate * -xsb error 2387c478bd9Sstevel@tonic-gate * -xsbfast error 2397c478bd9Sstevel@tonic-gate * -xsfpconst error 2407c478bd9Sstevel@tonic-gate * -xspace ignore (-not -Os) 2417c478bd9Sstevel@tonic-gate * -xstrconst ignore 2427c478bd9Sstevel@tonic-gate * -xtarget=<t> table 2437c478bd9Sstevel@tonic-gate * -xtemp=<dir> error 2447c478bd9Sstevel@tonic-gate * -xtime error 2457c478bd9Sstevel@tonic-gate * -xtransition -Wtransition 2467c478bd9Sstevel@tonic-gate * -xunroll=n error 2478c1a3716Ssherrym * -W0,-xdbggen=no%usedonly -fno-eliminate-unused-debug-symbols 2488c1a3716Ssherrym * -fno-eliminate-unused-debug-types 2497c478bd9Sstevel@tonic-gate * -Y<c>,<dir> error 2507c478bd9Sstevel@tonic-gate * -YA,<dir> error 2517c478bd9Sstevel@tonic-gate * -YI,<dir> -nostdinc -I<dir> 2527c478bd9Sstevel@tonic-gate * -YP,<dir> error 2537c478bd9Sstevel@tonic-gate * -YS,<dir> error 2547c478bd9Sstevel@tonic-gate */ 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate #include <ctype.h> 257aa9ef484SJohn Levon #include <err.h> 25880ab886dSwesolows #include <errno.h> 259aa9ef484SJohn Levon #include <fcntl.h> 260aa9ef484SJohn Levon #include <getopt.h> 261aa9ef484SJohn Levon #include <stdio.h> 262aa9ef484SJohn Levon #include <stdlib.h> 263aa9ef484SJohn Levon #include <string.h> 264aa9ef484SJohn Levon #include <unistd.h> 265aa9ef484SJohn Levon 2667c478bd9Sstevel@tonic-gate #include <sys/param.h> 26780ab886dSwesolows #include <sys/stat.h> 268aa9ef484SJohn Levon #include <sys/types.h> 269aa9ef484SJohn Levon #include <sys/utsname.h> 270aa9ef484SJohn Levon #include <sys/wait.h> 27180ab886dSwesolows 27280ab886dSwesolows #define CW_F_CXX 0x01 27380ab886dSwesolows #define CW_F_SHADOW 0x02 27480ab886dSwesolows #define CW_F_EXEC 0x04 27580ab886dSwesolows #define CW_F_ECHO 0x08 27680ab886dSwesolows #define CW_F_XLATE 0x10 2771912d2c4Swesolows #define CW_F_PROG 0x20 27880ab886dSwesolows 27980ab886dSwesolows typedef enum cw_op { 28080ab886dSwesolows CW_O_NONE = 0, 28180ab886dSwesolows CW_O_PREPROCESS, 28280ab886dSwesolows CW_O_COMPILE, 28380ab886dSwesolows CW_O_LINK 28480ab886dSwesolows } cw_op_t; 28580ab886dSwesolows 28680ab886dSwesolows struct aelist { 28780ab886dSwesolows struct ae { 28880ab886dSwesolows struct ae *ae_next; 28980ab886dSwesolows char *ae_arg; 29080ab886dSwesolows } *ael_head, *ael_tail; 29180ab886dSwesolows int ael_argc; 29280ab886dSwesolows }; 29380ab886dSwesolows 294aa9ef484SJohn Levon typedef enum { 295aa9ef484SJohn Levon GNU, 296*1f5207b7SJohn Levon SUN, 297*1f5207b7SJohn Levon SMATCH 298aa9ef484SJohn Levon } compiler_style_t; 299aa9ef484SJohn Levon 300aa9ef484SJohn Levon typedef struct { 301aa9ef484SJohn Levon char *c_name; 302aa9ef484SJohn Levon char *c_path; 303aa9ef484SJohn Levon compiler_style_t c_style; 304aa9ef484SJohn Levon } cw_compiler_t; 305aa9ef484SJohn Levon 30680ab886dSwesolows typedef struct cw_ictx { 307aa9ef484SJohn Levon struct cw_ictx *i_next; 308aa9ef484SJohn Levon cw_compiler_t *i_compiler; 30980ab886dSwesolows struct aelist *i_ae; 31080ab886dSwesolows uint32_t i_flags; 31180ab886dSwesolows int i_oldargc; 31280ab886dSwesolows char **i_oldargv; 31380ab886dSwesolows pid_t i_pid; 31467deef8cSDan McDonald char i_discard[MAXPATHLEN]; 3151912d2c4Swesolows char *i_stderr; 31680ab886dSwesolows } cw_ictx_t; 31780ab886dSwesolows 318e521259dSpetede /* 319e521259dSpetede * Status values to indicate which Studio compiler and associated 320e521259dSpetede * flags are being used. 321e521259dSpetede */ 322e521259dSpetede #define M32 0x01 /* -m32 - only on Studio 12 */ 323e521259dSpetede #define M64 0x02 /* -m64 - only on Studio 12 */ 324e521259dSpetede #define SS11 0x100 /* Studio 11 */ 325e521259dSpetede #define SS12 0x200 /* Studio 12 */ 3267c478bd9Sstevel@tonic-gate 327e521259dSpetede #define TRANS_ENTRY 5 328e521259dSpetede /* 329e521259dSpetede * Translation table definition for the -xarch= flag. The "x_arg" 330e521259dSpetede * value is translated into the appropriate gcc flags according 331e521259dSpetede * to the values in x_trans[n]. The x_flags indicates what compiler 332e521259dSpetede * is being used and what flags have been set via the use of 333e521259dSpetede * "x_arg". 334e521259dSpetede */ 335e521259dSpetede typedef struct xarch_table { 336e521259dSpetede char *x_arg; 337e521259dSpetede int x_flags; 338e521259dSpetede char *x_trans[TRANS_ENTRY]; 339e521259dSpetede } xarch_table_t; 340e521259dSpetede 341e521259dSpetede /* 342e521259dSpetede * The translation table for the -xarch= flag used in the Studio compilers. 343e521259dSpetede */ 344e521259dSpetede static const xarch_table_t xtbl[] = { 3457c478bd9Sstevel@tonic-gate #if defined(__x86) 346aa9ef484SJohn Levon { "generic", SS11, {NULL} }, 3477a6460b6Spetede { "generic64", (SS11|M64), { "-m64", "-mtune=opteron" } }, 3487a6460b6Spetede { "amd64", (SS11|M64), { "-m64", "-mtune=opteron" } }, 3497a6460b6Spetede { "386", SS11, { "-march=i386" } }, 3507a6460b6Spetede { "pentium_pro", SS11, { "-march=pentiumpro" } }, 35125c28e83SPiotr Jasiukajtis { "sse", SS11, { "-msse", "-mfpmath=sse" } }, 35225c28e83SPiotr Jasiukajtis { "sse2", SS11, { "-msse2", "-mfpmath=sse" } }, 3537c478bd9Sstevel@tonic-gate #elif defined(__sparc) 3547a6460b6Spetede { "generic", (SS11|M32), { "-m32", "-mcpu=v8" } }, 3557a6460b6Spetede { "generic64", (SS11|M64), { "-m64", "-mcpu=v9" } }, 3567a6460b6Spetede { "v8", (SS11|M32), { "-m32", "-mcpu=v8", "-mno-v8plus" } }, 3577a6460b6Spetede { "v8plus", (SS11|M32), { "-m32", "-mcpu=v9", "-mv8plus" } }, 3587a6460b6Spetede { "v8plusa", (SS11|M32), { "-m32", "-mcpu=ultrasparc", "-mv8plus", 3597a6460b6Spetede "-mvis" } }, 3607a6460b6Spetede { "v8plusb", (SS11|M32), { "-m32", "-mcpu=ultrasparc3", "-mv8plus", 3617a6460b6Spetede "-mvis" } }, 3627a6460b6Spetede { "v9", (SS11|M64), { "-m64", "-mcpu=v9" } }, 3637a6460b6Spetede { "v9a", (SS11|M64), { "-m64", "-mcpu=ultrasparc", "-mvis" } }, 3647a6460b6Spetede { "v9b", (SS11|M64), { "-m64", "-mcpu=ultrasparc3", "-mvis" } }, 3657a6460b6Spetede { "sparc", SS12, { "-mcpu=v9", "-mv8plus" } }, 3667a6460b6Spetede { "sparcvis", SS12, { "-mcpu=ultrasparc", "-mvis" } }, 3677a6460b6Spetede { "sparcvis2", SS12, { "-mcpu=ultrasparc3", "-mvis" } } 3687c478bd9Sstevel@tonic-gate #endif 3697c478bd9Sstevel@tonic-gate }; 3707c478bd9Sstevel@tonic-gate 371e521259dSpetede static int xtbl_size = sizeof (xtbl) / sizeof (xarch_table_t); 372e521259dSpetede 3737c478bd9Sstevel@tonic-gate static const char *xchip_tbl[] = { 3747c478bd9Sstevel@tonic-gate #if defined(__x86) 3757c478bd9Sstevel@tonic-gate "386", "-mtune=i386", NULL, 3767c478bd9Sstevel@tonic-gate "486", "-mtune=i486", NULL, 3777c478bd9Sstevel@tonic-gate "pentium", "-mtune=pentium", NULL, 3787c478bd9Sstevel@tonic-gate "pentium_pro", "-mtune=pentiumpro", NULL, 3797c478bd9Sstevel@tonic-gate #elif defined(__sparc) 3807c478bd9Sstevel@tonic-gate "super", "-mtune=supersparc", NULL, 3817c478bd9Sstevel@tonic-gate "ultra", "-mtune=ultrasparc", NULL, 3827c478bd9Sstevel@tonic-gate "ultra3", "-mtune=ultrasparc3", NULL, 3837c478bd9Sstevel@tonic-gate #endif 3847c478bd9Sstevel@tonic-gate NULL, NULL 3857c478bd9Sstevel@tonic-gate }; 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate static const char *xcode_tbl[] = { 3887c478bd9Sstevel@tonic-gate #if defined(__sparc) 3897c478bd9Sstevel@tonic-gate "abs32", "-fno-pic", "-mcmodel=medlow", NULL, 3907c478bd9Sstevel@tonic-gate "abs44", "-fno-pic", "-mcmodel=medmid", NULL, 3917c478bd9Sstevel@tonic-gate "abs64", "-fno-pic", "-mcmodel=medany", NULL, 3927c478bd9Sstevel@tonic-gate "pic13", "-fpic", NULL, 3937c478bd9Sstevel@tonic-gate "pic32", "-fPIC", NULL, 3947c478bd9Sstevel@tonic-gate #endif 3957c478bd9Sstevel@tonic-gate NULL, NULL 3967c478bd9Sstevel@tonic-gate }; 3977c478bd9Sstevel@tonic-gate 3987c478bd9Sstevel@tonic-gate static const char *xtarget_tbl[] = { 3997c478bd9Sstevel@tonic-gate #if defined(__x86) 4007c478bd9Sstevel@tonic-gate "pentium_pro", "-march=pentiumpro", NULL, 4017c478bd9Sstevel@tonic-gate #endif /* __x86 */ 4027c478bd9Sstevel@tonic-gate NULL, NULL 4037c478bd9Sstevel@tonic-gate }; 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate static const char *xregs_tbl[] = { 4067c478bd9Sstevel@tonic-gate #if defined(__sparc) 4077c478bd9Sstevel@tonic-gate "appl", "-mapp-regs", NULL, 4087c478bd9Sstevel@tonic-gate "no%appl", "-mno-app-regs", NULL, 4097c478bd9Sstevel@tonic-gate "float", "-mfpu", NULL, 4107c478bd9Sstevel@tonic-gate "no%float", "-mno-fpu", NULL, 4117c478bd9Sstevel@tonic-gate #endif /* __sparc */ 4127c478bd9Sstevel@tonic-gate NULL, NULL 4137c478bd9Sstevel@tonic-gate }; 4147c478bd9Sstevel@tonic-gate 41580ab886dSwesolows static void 41680ab886dSwesolows nomem(void) 41780ab886dSwesolows { 418aa9ef484SJohn Levon errx(1, "out of memory"); 4197c478bd9Sstevel@tonic-gate } 4207c478bd9Sstevel@tonic-gate 4217c478bd9Sstevel@tonic-gate static void 4227c478bd9Sstevel@tonic-gate newae(struct aelist *ael, const char *arg) 4237c478bd9Sstevel@tonic-gate { 4247c478bd9Sstevel@tonic-gate struct ae *ae; 4257c478bd9Sstevel@tonic-gate 42680ab886dSwesolows if ((ae = calloc(sizeof (*ae), 1)) == NULL) 42780ab886dSwesolows nomem(); 4287c478bd9Sstevel@tonic-gate ae->ae_arg = strdup(arg); 4297c478bd9Sstevel@tonic-gate if (ael->ael_tail == NULL) 4307c478bd9Sstevel@tonic-gate ael->ael_head = ae; 4317c478bd9Sstevel@tonic-gate else 4327c478bd9Sstevel@tonic-gate ael->ael_tail->ae_next = ae; 4337c478bd9Sstevel@tonic-gate ael->ael_tail = ae; 43480ab886dSwesolows ael->ael_argc++; 43580ab886dSwesolows } 43680ab886dSwesolows 43780ab886dSwesolows static cw_ictx_t * 43880ab886dSwesolows newictx(void) 43980ab886dSwesolows { 44080ab886dSwesolows cw_ictx_t *ctx = calloc(sizeof (cw_ictx_t), 1); 44180ab886dSwesolows if (ctx) 44280ab886dSwesolows if ((ctx->i_ae = calloc(sizeof (struct aelist), 1)) == NULL) { 44380ab886dSwesolows free(ctx); 44480ab886dSwesolows return (NULL); 44580ab886dSwesolows } 44680ab886dSwesolows 44780ab886dSwesolows return (ctx); 4487c478bd9Sstevel@tonic-gate } 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate static void 4517c478bd9Sstevel@tonic-gate error(const char *arg) 4527c478bd9Sstevel@tonic-gate { 453aa9ef484SJohn Levon errx(2, "error: mapping failed at or near arg '%s'", arg); 4547c478bd9Sstevel@tonic-gate } 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate /* 4577c478bd9Sstevel@tonic-gate * Add the current favourite set of warnings to the gcc invocation. 4587c478bd9Sstevel@tonic-gate */ 4597c478bd9Sstevel@tonic-gate static void 4607c478bd9Sstevel@tonic-gate warnings(struct aelist *h) 4617c478bd9Sstevel@tonic-gate { 4627c478bd9Sstevel@tonic-gate static int warningsonce; 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate if (warningsonce++) 4657c478bd9Sstevel@tonic-gate return; 4667c478bd9Sstevel@tonic-gate 4677014882cSRichard Lowe /* 4687014882cSRichard Lowe * Enable as many warnings as exist, then disable those that we never 4697014882cSRichard Lowe * ever want. 4707014882cSRichard Lowe */ 4717c478bd9Sstevel@tonic-gate newae(h, "-Wall"); 4727014882cSRichard Lowe newae(h, "-Wextra"); 4737c478bd9Sstevel@tonic-gate } 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate static void 4767c478bd9Sstevel@tonic-gate optim_disable(struct aelist *h, int level) 4777c478bd9Sstevel@tonic-gate { 4787c478bd9Sstevel@tonic-gate if (level >= 2) { 4797c478bd9Sstevel@tonic-gate newae(h, "-fno-strict-aliasing"); 4807c478bd9Sstevel@tonic-gate newae(h, "-fno-unit-at-a-time"); 4817c478bd9Sstevel@tonic-gate newae(h, "-fno-optimize-sibling-calls"); 4827c478bd9Sstevel@tonic-gate } 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate static void 4867c478bd9Sstevel@tonic-gate Xsmode(struct aelist *h) 4877c478bd9Sstevel@tonic-gate { 4887c478bd9Sstevel@tonic-gate static int xsonce; 4897c478bd9Sstevel@tonic-gate 4907c478bd9Sstevel@tonic-gate if (xsonce++) 4917c478bd9Sstevel@tonic-gate return; 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate newae(h, "-traditional"); 4947c478bd9Sstevel@tonic-gate newae(h, "-traditional-cpp"); 4957c478bd9Sstevel@tonic-gate } 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate static void 4987c478bd9Sstevel@tonic-gate usage() 4997c478bd9Sstevel@tonic-gate { 500aa9ef484SJohn Levon extern char *__progname; 5017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 502aa9ef484SJohn Levon "usage: %s [-C] [--versions] --primary <compiler> " 503aa9ef484SJohn Levon "[--shadow <compiler>]... -- cflags...\n", 504aa9ef484SJohn Levon __progname); 505aa9ef484SJohn Levon (void) fprintf(stderr, "compilers take the form: name,path,style\n" 506aa9ef484SJohn Levon " - name: a unique name usable in flag specifiers\n" 507aa9ef484SJohn Levon " - path: path to the compiler binary\n" 508aa9ef484SJohn Levon " - style: the style of flags expected: either sun or gnu\n"); 5097c478bd9Sstevel@tonic-gate exit(2); 5107c478bd9Sstevel@tonic-gate } 5117c478bd9Sstevel@tonic-gate 512e521259dSpetede static int 513e521259dSpetede xlate_xtb(struct aelist *h, const char *xarg) 514e521259dSpetede { 515e521259dSpetede int i, j; 516e521259dSpetede 517e521259dSpetede for (i = 0; i < xtbl_size; i++) { 518e521259dSpetede if (strcmp(xtbl[i].x_arg, xarg) == 0) 519e521259dSpetede break; 520e521259dSpetede } 521e521259dSpetede 522e521259dSpetede /* 523e521259dSpetede * At the end of the table and so no matching "arg" entry 524e521259dSpetede * found and so this must be a bad -xarch= flag. 525e521259dSpetede */ 526e521259dSpetede if (i == xtbl_size) 527e521259dSpetede error(xarg); 528e521259dSpetede 529e521259dSpetede for (j = 0; j < TRANS_ENTRY; j++) { 530e521259dSpetede if (xtbl[i].x_trans[j] != NULL) 531e521259dSpetede newae(h, xtbl[i].x_trans[j]); 532e521259dSpetede else 533e521259dSpetede break; 534e521259dSpetede } 535e521259dSpetede return (xtbl[i].x_flags); 536e521259dSpetede 537e521259dSpetede } 538e521259dSpetede 5397c478bd9Sstevel@tonic-gate static void 5407c478bd9Sstevel@tonic-gate xlate(struct aelist *h, const char *xarg, const char **table) 5417c478bd9Sstevel@tonic-gate { 5427c478bd9Sstevel@tonic-gate while (*table != NULL && strcmp(xarg, *table) != 0) { 5437c478bd9Sstevel@tonic-gate while (*table != NULL) 5447c478bd9Sstevel@tonic-gate table++; 5457c478bd9Sstevel@tonic-gate table++; 5467c478bd9Sstevel@tonic-gate } 5477c478bd9Sstevel@tonic-gate 5487c478bd9Sstevel@tonic-gate if (*table == NULL) 5497c478bd9Sstevel@tonic-gate error(xarg); 5507c478bd9Sstevel@tonic-gate 5517c478bd9Sstevel@tonic-gate table++; 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate while (*table != NULL) { 5547c478bd9Sstevel@tonic-gate newae(h, *table); 5557c478bd9Sstevel@tonic-gate table++; 5567c478bd9Sstevel@tonic-gate } 5577c478bd9Sstevel@tonic-gate } 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate static void 56080ab886dSwesolows do_gcc(cw_ictx_t *ctx) 5617c478bd9Sstevel@tonic-gate { 5627c478bd9Sstevel@tonic-gate int c; 563e1bf37b1SRichard Lowe int nolibc = 0; 56480ab886dSwesolows int in_output = 0, seen_o = 0, c_files = 0; 56580ab886dSwesolows cw_op_t op = CW_O_LINK; 5667c478bd9Sstevel@tonic-gate char *model = NULL; 567aa9ef484SJohn Levon char *nameflag; 56867deef8cSDan McDonald int mflag = 0; 5697c478bd9Sstevel@tonic-gate 5701912d2c4Swesolows if (ctx->i_flags & CW_F_PROG) { 5711912d2c4Swesolows newae(ctx->i_ae, "--version"); 5721912d2c4Swesolows return; 5731912d2c4Swesolows } 5741912d2c4Swesolows 57580ab886dSwesolows newae(ctx->i_ae, "-fident"); 57680ab886dSwesolows newae(ctx->i_ae, "-finline"); 57780ab886dSwesolows newae(ctx->i_ae, "-fno-inline-functions"); 57880ab886dSwesolows newae(ctx->i_ae, "-fno-builtin"); 57980ab886dSwesolows newae(ctx->i_ae, "-fno-asm"); 5807014882cSRichard Lowe newae(ctx->i_ae, "-fdiagnostics-show-option"); 58180ab886dSwesolows newae(ctx->i_ae, "-nodefaultlibs"); 5827c478bd9Sstevel@tonic-gate 58302e56f3fSwesolows #if defined(__sparc) 58402e56f3fSwesolows /* 58502e56f3fSwesolows * The SPARC ldd and std instructions require 8-byte alignment of 58602e56f3fSwesolows * their address operand. gcc correctly uses them only when the 58702e56f3fSwesolows * ABI requires 8-byte alignment; unfortunately we have a number of 58802e56f3fSwesolows * pieces of buggy code that doesn't conform to the ABI. This 58902e56f3fSwesolows * flag makes gcc work more like Studio with -xmemalign=4. 59002e56f3fSwesolows */ 59180ab886dSwesolows newae(ctx->i_ae, "-mno-integer-ldd-std"); 59202e56f3fSwesolows #endif 59302e56f3fSwesolows 5947c478bd9Sstevel@tonic-gate /* 5957c478bd9Sstevel@tonic-gate * This is needed because 'u' is defined 5967c478bd9Sstevel@tonic-gate * under a conditional on 'sun'. Should 5977c478bd9Sstevel@tonic-gate * probably just remove the conditional, 5987c478bd9Sstevel@tonic-gate * or make it be dependent on '__sun'. 5997c478bd9Sstevel@tonic-gate * 6007c478bd9Sstevel@tonic-gate * -Dunix is also missing in enhanced ANSI mode 6017c478bd9Sstevel@tonic-gate */ 60280ab886dSwesolows newae(ctx->i_ae, "-D__sun"); 6037c478bd9Sstevel@tonic-gate 604aa9ef484SJohn Levon if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1) 605aa9ef484SJohn Levon nomem(); 606aa9ef484SJohn Levon 6077c478bd9Sstevel@tonic-gate /* 6087c478bd9Sstevel@tonic-gate * Walk the argument list, translating as we go .. 6097c478bd9Sstevel@tonic-gate */ 61080ab886dSwesolows while (--ctx->i_oldargc > 0) { 61180ab886dSwesolows char *arg = *++ctx->i_oldargv; 6127c478bd9Sstevel@tonic-gate size_t arglen = strlen(arg); 6137c478bd9Sstevel@tonic-gate 61480ab886dSwesolows if (*arg == '-') { 6157c478bd9Sstevel@tonic-gate arglen--; 61680ab886dSwesolows } else { 6177c478bd9Sstevel@tonic-gate /* 6187c478bd9Sstevel@tonic-gate * Discard inline files that gcc doesn't grok 6197c478bd9Sstevel@tonic-gate */ 62080ab886dSwesolows if (!in_output && arglen > 3 && 6217c478bd9Sstevel@tonic-gate strcmp(arg + arglen - 3, ".il") == 0) 6227c478bd9Sstevel@tonic-gate continue; 6237c478bd9Sstevel@tonic-gate 62480ab886dSwesolows if (!in_output && arglen > 2 && 62580ab886dSwesolows arg[arglen - 2] == '.' && 62680ab886dSwesolows (arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' || 62780ab886dSwesolows arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i')) 62880ab886dSwesolows c_files++; 62980ab886dSwesolows 6307c478bd9Sstevel@tonic-gate /* 63180ab886dSwesolows * Otherwise, filenames and partial arguments 63280ab886dSwesolows * are passed through for gcc to chew on. However, 63380ab886dSwesolows * output is always discarded for the secondary 63480ab886dSwesolows * compiler. 6357c478bd9Sstevel@tonic-gate */ 63667deef8cSDan McDonald if ((ctx->i_flags & CW_F_SHADOW) && in_output) 63780ab886dSwesolows newae(ctx->i_ae, ctx->i_discard); 63867deef8cSDan McDonald else 63980ab886dSwesolows newae(ctx->i_ae, arg); 64080ab886dSwesolows in_output = 0; 6417c478bd9Sstevel@tonic-gate continue; 6427c478bd9Sstevel@tonic-gate } 6437c478bd9Sstevel@tonic-gate 64480ab886dSwesolows if (ctx->i_flags & CW_F_CXX) { 645aa9ef484SJohn Levon if (strncmp(arg, "-_g++=", 6) == 0) { 646aa9ef484SJohn Levon newae(ctx->i_ae, strchr(arg, '=') + 1); 647aa9ef484SJohn Levon continue; 648aa9ef484SJohn Levon } 6497c478bd9Sstevel@tonic-gate if (strncmp(arg, "-compat=", 8) == 0) { 6507c478bd9Sstevel@tonic-gate /* discard -compat=4 and -compat=5 */ 6517c478bd9Sstevel@tonic-gate continue; 6527c478bd9Sstevel@tonic-gate } 6537c478bd9Sstevel@tonic-gate if (strcmp(arg, "-Qoption") == 0) { 6547c478bd9Sstevel@tonic-gate /* discard -Qoption and its two arguments */ 65580ab886dSwesolows if (ctx->i_oldargc < 3) 6567c478bd9Sstevel@tonic-gate error(arg); 65780ab886dSwesolows ctx->i_oldargc -= 2; 65880ab886dSwesolows ctx->i_oldargv += 2; 6597c478bd9Sstevel@tonic-gate continue; 6607c478bd9Sstevel@tonic-gate } 6617c478bd9Sstevel@tonic-gate if (strcmp(arg, "-xwe") == 0) { 6627c478bd9Sstevel@tonic-gate /* turn warnings into errors */ 66380ab886dSwesolows newae(ctx->i_ae, "-Werror"); 6647c478bd9Sstevel@tonic-gate continue; 6657c478bd9Sstevel@tonic-gate } 6667c478bd9Sstevel@tonic-gate if (strcmp(arg, "-norunpath") == 0) { 6677c478bd9Sstevel@tonic-gate /* gcc has no corresponding option */ 6687c478bd9Sstevel@tonic-gate continue; 6697c478bd9Sstevel@tonic-gate } 6707c478bd9Sstevel@tonic-gate if (strcmp(arg, "-nolib") == 0) { 6717c478bd9Sstevel@tonic-gate /* -nodefaultlibs is on by default */ 6727c478bd9Sstevel@tonic-gate nolibc = 1; 6737c478bd9Sstevel@tonic-gate continue; 6747c478bd9Sstevel@tonic-gate } 6757c478bd9Sstevel@tonic-gate #if defined(__sparc) 6767c478bd9Sstevel@tonic-gate if (strcmp(arg, "-cg92") == 0) { 677e521259dSpetede mflag |= xlate_xtb(ctx->i_ae, "v8"); 67880ab886dSwesolows xlate(ctx->i_ae, "super", xchip_tbl); 6797c478bd9Sstevel@tonic-gate continue; 6807c478bd9Sstevel@tonic-gate } 6817c478bd9Sstevel@tonic-gate #endif /* __sparc */ 6827c478bd9Sstevel@tonic-gate } 6837c478bd9Sstevel@tonic-gate 6847c478bd9Sstevel@tonic-gate switch ((c = arg[1])) { 6857c478bd9Sstevel@tonic-gate case '_': 686aa9ef484SJohn Levon if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) || 687aa9ef484SJohn Levon (strncmp(arg, "-_gcc=", 6) == 0) || 688aa9ef484SJohn Levon (strncmp(arg, "-_gnu=", 6) == 0)) { 689aa9ef484SJohn Levon newae(ctx->i_ae, strchr(arg, '=') + 1); 690aa9ef484SJohn Levon } 6917c478bd9Sstevel@tonic-gate break; 6927c478bd9Sstevel@tonic-gate case '#': 6937c478bd9Sstevel@tonic-gate if (arglen == 1) { 69480ab886dSwesolows newae(ctx->i_ae, "-v"); 6957c478bd9Sstevel@tonic-gate break; 6967c478bd9Sstevel@tonic-gate } 6977c478bd9Sstevel@tonic-gate error(arg); 6987c478bd9Sstevel@tonic-gate break; 699e1bf37b1SRichard Lowe case 'f': 700e1bf37b1SRichard Lowe if ((strcmp(arg, "-fpic") == 0) || 701e1bf37b1SRichard Lowe (strcmp(arg, "-fPIC") == 0)) { 702e1bf37b1SRichard Lowe newae(ctx->i_ae, arg); 703e1bf37b1SRichard Lowe break; 704e1bf37b1SRichard Lowe } 705e1bf37b1SRichard Lowe error(arg); 706e1bf37b1SRichard Lowe break; 7077c478bd9Sstevel@tonic-gate case 'g': 70880ab886dSwesolows newae(ctx->i_ae, "-gdwarf-2"); 7097c478bd9Sstevel@tonic-gate break; 7107c478bd9Sstevel@tonic-gate case 'E': 7117c478bd9Sstevel@tonic-gate if (arglen == 1) { 71280ab886dSwesolows newae(ctx->i_ae, "-xc"); 71380ab886dSwesolows newae(ctx->i_ae, arg); 71480ab886dSwesolows op = CW_O_PREPROCESS; 7157c478bd9Sstevel@tonic-gate nolibc = 1; 7167c478bd9Sstevel@tonic-gate break; 7177c478bd9Sstevel@tonic-gate } 7187c478bd9Sstevel@tonic-gate error(arg); 7197c478bd9Sstevel@tonic-gate break; 7207c478bd9Sstevel@tonic-gate case 'c': 7217c478bd9Sstevel@tonic-gate case 'S': 72280ab886dSwesolows if (arglen == 1) { 72380ab886dSwesolows op = CW_O_COMPILE; 7247c478bd9Sstevel@tonic-gate nolibc = 1; 72580ab886dSwesolows } 7267c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 7277c478bd9Sstevel@tonic-gate case 'C': 7287c478bd9Sstevel@tonic-gate case 'H': 7297c478bd9Sstevel@tonic-gate case 'p': 7307c478bd9Sstevel@tonic-gate if (arglen == 1) { 73180ab886dSwesolows newae(ctx->i_ae, arg); 7327c478bd9Sstevel@tonic-gate break; 7337c478bd9Sstevel@tonic-gate } 7347c478bd9Sstevel@tonic-gate error(arg); 7357c478bd9Sstevel@tonic-gate break; 7367c478bd9Sstevel@tonic-gate case 'A': 7377c478bd9Sstevel@tonic-gate case 'h': 7387c478bd9Sstevel@tonic-gate case 'I': 7397c478bd9Sstevel@tonic-gate case 'i': 7407c478bd9Sstevel@tonic-gate case 'L': 7417c478bd9Sstevel@tonic-gate case 'l': 7427c478bd9Sstevel@tonic-gate case 'R': 7437c478bd9Sstevel@tonic-gate case 'U': 7447c478bd9Sstevel@tonic-gate case 'u': 7457c478bd9Sstevel@tonic-gate case 'w': 74680ab886dSwesolows newae(ctx->i_ae, arg); 74780ab886dSwesolows break; 74880ab886dSwesolows case 'o': 74980ab886dSwesolows seen_o = 1; 75080ab886dSwesolows if (arglen == 1) { 75180ab886dSwesolows in_output = 1; 75280ab886dSwesolows newae(ctx->i_ae, arg); 75380ab886dSwesolows } else if (ctx->i_flags & CW_F_SHADOW) { 75480ab886dSwesolows newae(ctx->i_ae, "-o"); 75580ab886dSwesolows newae(ctx->i_ae, ctx->i_discard); 75680ab886dSwesolows } else { 75780ab886dSwesolows newae(ctx->i_ae, arg); 75880ab886dSwesolows } 7597c478bd9Sstevel@tonic-gate break; 7607c478bd9Sstevel@tonic-gate case 'D': 76180ab886dSwesolows newae(ctx->i_ae, arg); 7627c478bd9Sstevel@tonic-gate /* 7637c478bd9Sstevel@tonic-gate * XXX Clearly a hack ... do we need _KADB too? 7647c478bd9Sstevel@tonic-gate */ 7657c478bd9Sstevel@tonic-gate if (strcmp(arg, "-D_KERNEL") == 0 || 7667c478bd9Sstevel@tonic-gate strcmp(arg, "-D_BOOT") == 0) 76780ab886dSwesolows newae(ctx->i_ae, "-ffreestanding"); 7687c478bd9Sstevel@tonic-gate break; 7697c478bd9Sstevel@tonic-gate case 'd': 7707c478bd9Sstevel@tonic-gate if (arglen == 2) { 7717c478bd9Sstevel@tonic-gate if (strcmp(arg, "-dy") == 0) { 77280ab886dSwesolows newae(ctx->i_ae, "-Wl,-dy"); 7737c478bd9Sstevel@tonic-gate break; 7747c478bd9Sstevel@tonic-gate } 7757c478bd9Sstevel@tonic-gate if (strcmp(arg, "-dn") == 0) { 77680ab886dSwesolows newae(ctx->i_ae, "-Wl,-dn"); 7777c478bd9Sstevel@tonic-gate break; 7787c478bd9Sstevel@tonic-gate } 7797c478bd9Sstevel@tonic-gate } 7807c478bd9Sstevel@tonic-gate if (strcmp(arg, "-dalign") == 0) { 7817c478bd9Sstevel@tonic-gate /* 7827c478bd9Sstevel@tonic-gate * -dalign forces alignment in some cases; 7837c478bd9Sstevel@tonic-gate * gcc does not need any flag to do this. 7847c478bd9Sstevel@tonic-gate */ 7857c478bd9Sstevel@tonic-gate break; 7867c478bd9Sstevel@tonic-gate } 7877c478bd9Sstevel@tonic-gate error(arg); 7887c478bd9Sstevel@tonic-gate break; 7897c478bd9Sstevel@tonic-gate case 'e': 7907c478bd9Sstevel@tonic-gate if (strcmp(arg, 7917c478bd9Sstevel@tonic-gate "-erroff=E_EMPTY_TRANSLATION_UNIT") == 0) { 7927c478bd9Sstevel@tonic-gate /* 7937c478bd9Sstevel@tonic-gate * Accept but ignore this -- gcc doesn't 7947c478bd9Sstevel@tonic-gate * seem to complain about empty translation 7957c478bd9Sstevel@tonic-gate * units 7967c478bd9Sstevel@tonic-gate */ 7977c478bd9Sstevel@tonic-gate break; 7987c478bd9Sstevel@tonic-gate } 7997c478bd9Sstevel@tonic-gate /* XX64 -- ignore all -erroff= options, for now */ 8007c478bd9Sstevel@tonic-gate if (strncmp(arg, "-erroff=", 8) == 0) 8017c478bd9Sstevel@tonic-gate break; 8027c478bd9Sstevel@tonic-gate if (strcmp(arg, "-errtags=yes") == 0) { 80380ab886dSwesolows warnings(ctx->i_ae); 8047c478bd9Sstevel@tonic-gate break; 8057c478bd9Sstevel@tonic-gate } 8067c478bd9Sstevel@tonic-gate if (strcmp(arg, "-errwarn=%all") == 0) { 80780ab886dSwesolows newae(ctx->i_ae, "-Werror"); 8087c478bd9Sstevel@tonic-gate break; 8097c478bd9Sstevel@tonic-gate } 8107c478bd9Sstevel@tonic-gate error(arg); 8117c478bd9Sstevel@tonic-gate break; 8127c478bd9Sstevel@tonic-gate case 'G': 81380ab886dSwesolows newae(ctx->i_ae, "-shared"); 8147c478bd9Sstevel@tonic-gate nolibc = 1; 8157c478bd9Sstevel@tonic-gate break; 8167c478bd9Sstevel@tonic-gate case 'k': 8177c478bd9Sstevel@tonic-gate if (strcmp(arg, "-keeptmp") == 0) { 81880ab886dSwesolows newae(ctx->i_ae, "-save-temps"); 8197c478bd9Sstevel@tonic-gate break; 8207c478bd9Sstevel@tonic-gate } 8217c478bd9Sstevel@tonic-gate error(arg); 8227c478bd9Sstevel@tonic-gate break; 8237c478bd9Sstevel@tonic-gate case 'm': 8247c478bd9Sstevel@tonic-gate if (strcmp(arg, "-mt") == 0) { 82580ab886dSwesolows newae(ctx->i_ae, "-D_REENTRANT"); 8267c478bd9Sstevel@tonic-gate break; 8277c478bd9Sstevel@tonic-gate } 828e521259dSpetede if (strcmp(arg, "-m64") == 0) { 829e521259dSpetede newae(ctx->i_ae, "-m64"); 830e521259dSpetede #if defined(__x86) 831e521259dSpetede newae(ctx->i_ae, "-mtune=opteron"); 832e521259dSpetede #endif 833e521259dSpetede mflag |= M64; 834e521259dSpetede break; 835e521259dSpetede } 836e521259dSpetede if (strcmp(arg, "-m32") == 0) { 837e521259dSpetede newae(ctx->i_ae, "-m32"); 838e521259dSpetede mflag |= M32; 839e521259dSpetede break; 840e521259dSpetede } 8417c478bd9Sstevel@tonic-gate error(arg); 8427c478bd9Sstevel@tonic-gate break; 8437c478bd9Sstevel@tonic-gate case 'B': /* linker options */ 8447c478bd9Sstevel@tonic-gate case 'M': 8457c478bd9Sstevel@tonic-gate case 'z': 8467c478bd9Sstevel@tonic-gate { 8477c478bd9Sstevel@tonic-gate char *opt; 8487c478bd9Sstevel@tonic-gate size_t len; 8497c478bd9Sstevel@tonic-gate char *s; 8507c478bd9Sstevel@tonic-gate 8517c478bd9Sstevel@tonic-gate if (arglen == 1) { 85280ab886dSwesolows opt = *++ctx->i_oldargv; 8537c478bd9Sstevel@tonic-gate if (opt == NULL || *opt == '\0') 8547c478bd9Sstevel@tonic-gate error(arg); 85580ab886dSwesolows ctx->i_oldargc--; 8567c478bd9Sstevel@tonic-gate } else { 8577c478bd9Sstevel@tonic-gate opt = arg + 2; 8587c478bd9Sstevel@tonic-gate } 8597c478bd9Sstevel@tonic-gate len = strlen(opt) + 7; 86080ab886dSwesolows if ((s = malloc(len)) == NULL) 86180ab886dSwesolows nomem(); 8627c478bd9Sstevel@tonic-gate (void) snprintf(s, len, "-Wl,-%c%s", c, opt); 86380ab886dSwesolows newae(ctx->i_ae, s); 8647c478bd9Sstevel@tonic-gate free(s); 8657c478bd9Sstevel@tonic-gate } 8667c478bd9Sstevel@tonic-gate break; 8677c478bd9Sstevel@tonic-gate case 'O': 8687c478bd9Sstevel@tonic-gate if (arglen == 1) { 86980ab886dSwesolows newae(ctx->i_ae, "-O"); 8707c478bd9Sstevel@tonic-gate break; 8717c478bd9Sstevel@tonic-gate } 8727c478bd9Sstevel@tonic-gate error(arg); 8737c478bd9Sstevel@tonic-gate break; 8747c478bd9Sstevel@tonic-gate case 'P': 8757c478bd9Sstevel@tonic-gate /* 8767c478bd9Sstevel@tonic-gate * We could do '-E -o filename.i', but that's hard, 8777c478bd9Sstevel@tonic-gate * and we don't need it for the case that's triggering 8787c478bd9Sstevel@tonic-gate * this addition. We'll require the user to specify 8797c478bd9Sstevel@tonic-gate * -o in the Makefile. If they don't they'll find out 8807c478bd9Sstevel@tonic-gate * in a hurry. 8817c478bd9Sstevel@tonic-gate */ 88280ab886dSwesolows newae(ctx->i_ae, "-E"); 88380ab886dSwesolows op = CW_O_PREPROCESS; 8847c478bd9Sstevel@tonic-gate nolibc = 1; 8857c478bd9Sstevel@tonic-gate break; 8867c478bd9Sstevel@tonic-gate case 's': 8877c478bd9Sstevel@tonic-gate if (arglen == 1) { 88880ab886dSwesolows newae(ctx->i_ae, "-Wl,-s"); 8897c478bd9Sstevel@tonic-gate break; 8907c478bd9Sstevel@tonic-gate } 8917c478bd9Sstevel@tonic-gate error(arg); 8927c478bd9Sstevel@tonic-gate break; 893159cf8a6Swesolows case 't': 894159cf8a6Swesolows if (arglen == 1) { 89580ab886dSwesolows newae(ctx->i_ae, "-Wl,-t"); 896159cf8a6Swesolows break; 897159cf8a6Swesolows } 898159cf8a6Swesolows error(arg); 899159cf8a6Swesolows break; 9007c478bd9Sstevel@tonic-gate case 'V': 9017c478bd9Sstevel@tonic-gate if (arglen == 1) { 90280ab886dSwesolows ctx->i_flags &= ~CW_F_ECHO; 90380ab886dSwesolows newae(ctx->i_ae, "--version"); 9047c478bd9Sstevel@tonic-gate break; 9057c478bd9Sstevel@tonic-gate } 9067c478bd9Sstevel@tonic-gate error(arg); 9077c478bd9Sstevel@tonic-gate break; 9087c478bd9Sstevel@tonic-gate case 'v': 9097c478bd9Sstevel@tonic-gate if (arglen == 1) { 91080ab886dSwesolows warnings(ctx->i_ae); 9117c478bd9Sstevel@tonic-gate break; 9127c478bd9Sstevel@tonic-gate } 9137c478bd9Sstevel@tonic-gate error(arg); 9147c478bd9Sstevel@tonic-gate break; 9157c478bd9Sstevel@tonic-gate case 'W': 9167c478bd9Sstevel@tonic-gate if (strncmp(arg, "-Wp,-xc99", 9) == 0) { 9177c478bd9Sstevel@tonic-gate /* 9187c478bd9Sstevel@tonic-gate * gcc's preprocessor will accept c99 9197c478bd9Sstevel@tonic-gate * regardless, so accept and ignore. 9207c478bd9Sstevel@tonic-gate */ 9217c478bd9Sstevel@tonic-gate break; 9227c478bd9Sstevel@tonic-gate } 9237c478bd9Sstevel@tonic-gate if (strncmp(arg, "-Wa,", 4) == 0 || 9247c478bd9Sstevel@tonic-gate strncmp(arg, "-Wp,", 4) == 0 || 9257c478bd9Sstevel@tonic-gate strncmp(arg, "-Wl,", 4) == 0) { 92680ab886dSwesolows newae(ctx->i_ae, arg); 9277c478bd9Sstevel@tonic-gate break; 9287c478bd9Sstevel@tonic-gate } 9294afe1ab8Sesaxe if (strcmp(arg, "-W0,-noglobal") == 0 || 9304afe1ab8Sesaxe strcmp(arg, "-W0,-xglobalstatic") == 0) { 9317c478bd9Sstevel@tonic-gate /* 9327c478bd9Sstevel@tonic-gate * gcc doesn't prefix local symbols 9337c478bd9Sstevel@tonic-gate * in debug mode, so this is not needed. 9347c478bd9Sstevel@tonic-gate */ 9357c478bd9Sstevel@tonic-gate break; 9367c478bd9Sstevel@tonic-gate } 9377c478bd9Sstevel@tonic-gate if (strcmp(arg, "-W0,-Lt") == 0) { 9387c478bd9Sstevel@tonic-gate /* 9397c478bd9Sstevel@tonic-gate * Generate tests at the top of loops. 9407c478bd9Sstevel@tonic-gate * There is no direct gcc equivalent, ignore. 9417c478bd9Sstevel@tonic-gate */ 9427c478bd9Sstevel@tonic-gate break; 9437c478bd9Sstevel@tonic-gate } 9448c1a3716Ssherrym if (strcmp(arg, "-W0,-xdbggen=no%usedonly") == 0) { 94580ab886dSwesolows newae(ctx->i_ae, 94680ab886dSwesolows "-fno-eliminate-unused-debug-symbols"); 94780ab886dSwesolows newae(ctx->i_ae, 94880ab886dSwesolows "-fno-eliminate-unused-debug-types"); 9498c1a3716Ssherrym break; 9508c1a3716Ssherrym } 9519dd82889Spetede if (strcmp(arg, "-W2,-xwrap_int") == 0) { 9529dd82889Spetede /* 9539dd82889Spetede * Use the legacy behaviour (pre-SS11) 9549dd82889Spetede * for integer wrapping. 9559dd82889Spetede * gcc does not need this. 9569dd82889Spetede */ 9579dd82889Spetede break; 9589dd82889Spetede } 9597c478bd9Sstevel@tonic-gate if (strcmp(arg, "-Wd,-xsafe=unboundsym") == 0) { 9607c478bd9Sstevel@tonic-gate /* 9617c478bd9Sstevel@tonic-gate * Prevents optimizing away checks for 9627c478bd9Sstevel@tonic-gate * unbound weak symbol addresses. gcc does 9637c478bd9Sstevel@tonic-gate * not do this, so it's not needed. 9647c478bd9Sstevel@tonic-gate */ 9657c478bd9Sstevel@tonic-gate break; 9667c478bd9Sstevel@tonic-gate } 9677c478bd9Sstevel@tonic-gate if (strncmp(arg, "-Wc,-xcode=", 11) == 0) { 96880ab886dSwesolows xlate(ctx->i_ae, arg + 11, xcode_tbl); 9697c478bd9Sstevel@tonic-gate break; 9707c478bd9Sstevel@tonic-gate } 9717c478bd9Sstevel@tonic-gate if (strncmp(arg, "-Wc,-Qiselect", 13) == 0) { 9727c478bd9Sstevel@tonic-gate /* 9737c478bd9Sstevel@tonic-gate * Prevents insertion of register symbols. 9747c478bd9Sstevel@tonic-gate * gcc doesn't do this, so ignore it. 9757c478bd9Sstevel@tonic-gate */ 9767c478bd9Sstevel@tonic-gate break; 9777c478bd9Sstevel@tonic-gate } 9789dd82889Spetede if (strcmp(arg, "-Wc,-Qassembler-ounrefsym=0") == 0) { 9799dd82889Spetede /* 9809dd82889Spetede * Prevents optimizing away of static variables. 9819dd82889Spetede * gcc does not do this, so it's not needed. 9829dd82889Spetede */ 9839dd82889Spetede break; 9849dd82889Spetede } 9857c478bd9Sstevel@tonic-gate #if defined(__x86) 986d430274bSsherrym if (strcmp(arg, "-Wu,-save_args") == 0) { 98780ab886dSwesolows newae(ctx->i_ae, "-msave-args"); 988d430274bSsherrym break; 989d430274bSsherrym } 9907c478bd9Sstevel@tonic-gate #endif /* __x86 */ 9917c478bd9Sstevel@tonic-gate error(arg); 9927c478bd9Sstevel@tonic-gate break; 9937c478bd9Sstevel@tonic-gate case 'X': 9947c478bd9Sstevel@tonic-gate if (strcmp(arg, "-Xa") == 0 || 9957c478bd9Sstevel@tonic-gate strcmp(arg, "-Xt") == 0) { 9967c478bd9Sstevel@tonic-gate break; 9977c478bd9Sstevel@tonic-gate } 9987c478bd9Sstevel@tonic-gate if (strcmp(arg, "-Xs") == 0) { 99980ab886dSwesolows Xsmode(ctx->i_ae); 10007c478bd9Sstevel@tonic-gate break; 10017c478bd9Sstevel@tonic-gate } 10027c478bd9Sstevel@tonic-gate error(arg); 10037c478bd9Sstevel@tonic-gate break; 10047c478bd9Sstevel@tonic-gate case 'x': 10057c478bd9Sstevel@tonic-gate if (arglen == 1) 10067c478bd9Sstevel@tonic-gate error(arg); 10077c478bd9Sstevel@tonic-gate switch (arg[2]) { 10087c478bd9Sstevel@tonic-gate case 'a': 10097c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xarch=", 7) == 0) { 1010e521259dSpetede mflag |= xlate_xtb(ctx->i_ae, arg + 7); 10117c478bd9Sstevel@tonic-gate break; 10127c478bd9Sstevel@tonic-gate } 10137c478bd9Sstevel@tonic-gate error(arg); 10147c478bd9Sstevel@tonic-gate break; 101502e56f3fSwesolows case 'b': 101602e56f3fSwesolows if (strncmp(arg, "-xbuiltin=", 10) == 0) { 101702e56f3fSwesolows if (strcmp(arg + 10, "%all")) 101880ab886dSwesolows newae(ctx->i_ae, "-fbuiltin"); 101902e56f3fSwesolows break; 102002e56f3fSwesolows } 102102e56f3fSwesolows error(arg); 102202e56f3fSwesolows break; 10237c478bd9Sstevel@tonic-gate case 'C': 10247c478bd9Sstevel@tonic-gate /* Accept C++ style comments -- ignore */ 10257c478bd9Sstevel@tonic-gate if (strcmp(arg, "-xCC") == 0) 10267c478bd9Sstevel@tonic-gate break; 10277c478bd9Sstevel@tonic-gate error(arg); 10287c478bd9Sstevel@tonic-gate break; 10297c478bd9Sstevel@tonic-gate case 'c': 10307c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xc99=%all", 10) == 0) { 103180ab886dSwesolows newae(ctx->i_ae, "-std=gnu99"); 10327c478bd9Sstevel@tonic-gate break; 10337c478bd9Sstevel@tonic-gate } 10347c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xc99=%none", 11) == 0) { 103580ab886dSwesolows newae(ctx->i_ae, "-std=gnu89"); 10367c478bd9Sstevel@tonic-gate break; 10377c478bd9Sstevel@tonic-gate } 10387c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xchip=", 7) == 0) { 103980ab886dSwesolows xlate(ctx->i_ae, arg + 7, xchip_tbl); 10407c478bd9Sstevel@tonic-gate break; 10417c478bd9Sstevel@tonic-gate } 10427c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xcode=", 7) == 0) { 104380ab886dSwesolows xlate(ctx->i_ae, arg + 7, xcode_tbl); 10447c478bd9Sstevel@tonic-gate break; 10457c478bd9Sstevel@tonic-gate } 1046159cf8a6Swesolows if (strncmp(arg, "-xcrossfile", 11) == 0) 1047159cf8a6Swesolows break; 10487c478bd9Sstevel@tonic-gate error(arg); 10497c478bd9Sstevel@tonic-gate break; 10507c478bd9Sstevel@tonic-gate case 'd': 10518c1a3716Ssherrym if (strncmp(arg, "-xdebugformat=", 14) == 0) 10528c1a3716Ssherrym break; 10537c478bd9Sstevel@tonic-gate error(arg); 10547c478bd9Sstevel@tonic-gate break; 10557c478bd9Sstevel@tonic-gate case 'F': 1056f795e658Srie /* 1057f795e658Srie * Compile for mapfile reordering, or unused 1058f795e658Srie * section elimination, syntax can be -xF or 1059f795e658Srie * more complex, like -xF=%all -- ignore. 1060f795e658Srie */ 1061f795e658Srie if (strncmp(arg, "-xF", 3) == 0) 10627c478bd9Sstevel@tonic-gate break; 10637c478bd9Sstevel@tonic-gate error(arg); 10647c478bd9Sstevel@tonic-gate break; 10657c478bd9Sstevel@tonic-gate case 'i': 10667c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xinline", 8) == 0) 10677c478bd9Sstevel@tonic-gate /* No inlining; ignore */ 10687c478bd9Sstevel@tonic-gate break; 10697c478bd9Sstevel@tonic-gate if (strcmp(arg, "-xildon") == 0 || 10707c478bd9Sstevel@tonic-gate strcmp(arg, "-xildoff") == 0) 10717c478bd9Sstevel@tonic-gate /* No incremental linking; ignore */ 10727c478bd9Sstevel@tonic-gate break; 10737c478bd9Sstevel@tonic-gate error(arg); 10747c478bd9Sstevel@tonic-gate break; 107554836668Spetede #if defined(__x86) 107654836668Spetede case 'm': 107754836668Spetede if (strcmp(arg, "-xmodel=kernel") == 0) { 107854836668Spetede newae(ctx->i_ae, "-ffreestanding"); 107954836668Spetede newae(ctx->i_ae, "-mno-red-zone"); 108054836668Spetede model = "-mcmodel=kernel"; 108154836668Spetede nolibc = 1; 108254836668Spetede break; 108354836668Spetede } 108454836668Spetede error(arg); 108554836668Spetede break; 108654836668Spetede #endif /* __x86 */ 10877c478bd9Sstevel@tonic-gate case 'O': 10887c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xO", 3) == 0) { 10897c478bd9Sstevel@tonic-gate size_t len = strlen(arg); 1090538ff303SToomas Soome char *s = NULL; 10917c478bd9Sstevel@tonic-gate int c = *(arg + 3); 10927c478bd9Sstevel@tonic-gate int level; 10937c478bd9Sstevel@tonic-gate 10947c478bd9Sstevel@tonic-gate if (len != 4 || !isdigit(c)) 10957c478bd9Sstevel@tonic-gate error(arg); 10967c478bd9Sstevel@tonic-gate 10977c478bd9Sstevel@tonic-gate level = atoi(arg + 3); 10987c478bd9Sstevel@tonic-gate if (level > 5) 10997c478bd9Sstevel@tonic-gate error(arg); 11007c478bd9Sstevel@tonic-gate if (level >= 2) { 11017c478bd9Sstevel@tonic-gate /* 11027c478bd9Sstevel@tonic-gate * For gcc-3.4.x at -O2 we 11037c478bd9Sstevel@tonic-gate * need to disable optimizations 11047c478bd9Sstevel@tonic-gate * that break ON. 11057c478bd9Sstevel@tonic-gate */ 110680ab886dSwesolows optim_disable(ctx->i_ae, level); 11077c478bd9Sstevel@tonic-gate /* 11087c478bd9Sstevel@tonic-gate * limit -xO3 to -O2 as well. 11097c478bd9Sstevel@tonic-gate */ 11107c478bd9Sstevel@tonic-gate level = 2; 11117c478bd9Sstevel@tonic-gate } 1112538ff303SToomas Soome if (asprintf(&s, "-O%d", level) == -1) 1113538ff303SToomas Soome nomem(); 111480ab886dSwesolows newae(ctx->i_ae, s); 11157c478bd9Sstevel@tonic-gate free(s); 11167c478bd9Sstevel@tonic-gate break; 11177c478bd9Sstevel@tonic-gate } 11187c478bd9Sstevel@tonic-gate error(arg); 11197c478bd9Sstevel@tonic-gate break; 11207c478bd9Sstevel@tonic-gate case 'r': 11217c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xregs=", 7) == 0) { 112280ab886dSwesolows xlate(ctx->i_ae, arg + 7, xregs_tbl); 11237c478bd9Sstevel@tonic-gate break; 11247c478bd9Sstevel@tonic-gate } 11257c478bd9Sstevel@tonic-gate error(arg); 11267c478bd9Sstevel@tonic-gate break; 11277c478bd9Sstevel@tonic-gate case 's': 11287c478bd9Sstevel@tonic-gate if (strcmp(arg, "-xs") == 0 || 11297c478bd9Sstevel@tonic-gate strcmp(arg, "-xspace") == 0 || 11307c478bd9Sstevel@tonic-gate strcmp(arg, "-xstrconst") == 0) 11317c478bd9Sstevel@tonic-gate break; 11327c478bd9Sstevel@tonic-gate error(arg); 11337c478bd9Sstevel@tonic-gate break; 11347c478bd9Sstevel@tonic-gate case 't': 11357c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xtarget=", 9) == 0) { 113680ab886dSwesolows xlate(ctx->i_ae, arg + 9, xtarget_tbl); 11377c478bd9Sstevel@tonic-gate break; 11387c478bd9Sstevel@tonic-gate } 11397c478bd9Sstevel@tonic-gate error(arg); 11407c478bd9Sstevel@tonic-gate break; 11417c478bd9Sstevel@tonic-gate case 'e': 11427c478bd9Sstevel@tonic-gate case 'h': 11437c478bd9Sstevel@tonic-gate case 'l': 11447c478bd9Sstevel@tonic-gate default: 11457c478bd9Sstevel@tonic-gate error(arg); 11467c478bd9Sstevel@tonic-gate break; 11477c478bd9Sstevel@tonic-gate } 11487c478bd9Sstevel@tonic-gate break; 11497c478bd9Sstevel@tonic-gate case 'Y': 11507c478bd9Sstevel@tonic-gate if (arglen == 1) { 115180ab886dSwesolows if ((arg = *++ctx->i_oldargv) == NULL || 115280ab886dSwesolows *arg == '\0') 11537c478bd9Sstevel@tonic-gate error("-Y"); 115480ab886dSwesolows ctx->i_oldargc--; 11557c478bd9Sstevel@tonic-gate arglen = strlen(arg + 1); 11567c478bd9Sstevel@tonic-gate } else { 11577c478bd9Sstevel@tonic-gate arg += 2; 11587c478bd9Sstevel@tonic-gate } 11597c478bd9Sstevel@tonic-gate /* Just ignore -YS,... for now */ 11607c478bd9Sstevel@tonic-gate if (strncmp(arg, "S,", 2) == 0) 11617c478bd9Sstevel@tonic-gate break; 11627c478bd9Sstevel@tonic-gate if (strncmp(arg, "l,", 2) == 0) { 11637c478bd9Sstevel@tonic-gate char *s = strdup(arg); 11647c478bd9Sstevel@tonic-gate s[0] = '-'; 11657c478bd9Sstevel@tonic-gate s[1] = 'B'; 116680ab886dSwesolows newae(ctx->i_ae, s); 11677c478bd9Sstevel@tonic-gate free(s); 11687c478bd9Sstevel@tonic-gate break; 11697c478bd9Sstevel@tonic-gate } 11707c478bd9Sstevel@tonic-gate if (strncmp(arg, "I,", 2) == 0) { 11717c478bd9Sstevel@tonic-gate char *s = strdup(arg); 11727c478bd9Sstevel@tonic-gate s[0] = '-'; 11737c478bd9Sstevel@tonic-gate s[1] = 'I'; 117480ab886dSwesolows newae(ctx->i_ae, "-nostdinc"); 117580ab886dSwesolows newae(ctx->i_ae, s); 11767c478bd9Sstevel@tonic-gate free(s); 11777c478bd9Sstevel@tonic-gate break; 11787c478bd9Sstevel@tonic-gate } 11797c478bd9Sstevel@tonic-gate error(arg); 11807c478bd9Sstevel@tonic-gate break; 11817c478bd9Sstevel@tonic-gate case 'Q': 11827c478bd9Sstevel@tonic-gate /* 11837c478bd9Sstevel@tonic-gate * We could map -Qy into -Wl,-Qy etc. 11847c478bd9Sstevel@tonic-gate */ 11857c478bd9Sstevel@tonic-gate default: 11867c478bd9Sstevel@tonic-gate error(arg); 11877c478bd9Sstevel@tonic-gate break; 11887c478bd9Sstevel@tonic-gate } 11897c478bd9Sstevel@tonic-gate } 11907c478bd9Sstevel@tonic-gate 1191aa9ef484SJohn Levon free(nameflag); 1192aa9ef484SJohn Levon 119367deef8cSDan McDonald if (c_files > 1 && (ctx->i_flags & CW_F_SHADOW) && 119467deef8cSDan McDonald op != CW_O_PREPROCESS) { 1195aa9ef484SJohn Levon errx(2, "multiple source files are " 1196aa9ef484SJohn Levon "allowed only with -E or -P"); 119780ab886dSwesolows } 1198e521259dSpetede 1199e521259dSpetede /* 1200e521259dSpetede * Make sure that we do not have any unintended interactions between 1201e521259dSpetede * the xarch options passed in and the version of the Studio compiler 1202e521259dSpetede * used. 1203e521259dSpetede */ 1204e521259dSpetede if ((mflag & (SS11|SS12)) == (SS11|SS12)) { 1205aa9ef484SJohn Levon errx(2, 1206e521259dSpetede "Conflicting \"-xarch=\" flags (both Studio 11 and 12)\n"); 1207e521259dSpetede } 1208e521259dSpetede 1209e521259dSpetede switch (mflag) { 1210e521259dSpetede case 0: 1211e521259dSpetede /* FALLTHROUGH */ 1212e521259dSpetede case M32: 1213e521259dSpetede #if defined(__sparc) 1214e521259dSpetede /* 1215e521259dSpetede * Only -m32 is defined and so put in the missing xarch 1216e521259dSpetede * translation. 1217e521259dSpetede */ 1218e521259dSpetede newae(ctx->i_ae, "-mcpu=v8"); 1219e521259dSpetede newae(ctx->i_ae, "-mno-v8plus"); 1220e521259dSpetede #endif 1221e521259dSpetede break; 1222e521259dSpetede case M64: 1223e521259dSpetede #if defined(__sparc) 1224e521259dSpetede /* 1225e521259dSpetede * Only -m64 is defined and so put in the missing xarch 1226e521259dSpetede * translation. 1227e521259dSpetede */ 1228e521259dSpetede newae(ctx->i_ae, "-mcpu=v9"); 1229e521259dSpetede #endif 1230e521259dSpetede break; 1231e521259dSpetede case SS12: 1232e521259dSpetede #if defined(__sparc) 1233e521259dSpetede /* no -m32/-m64 flag used - this is an error for sparc builds */ 1234e521259dSpetede (void) fprintf(stderr, "No -m32/-m64 flag defined\n"); 1235e521259dSpetede exit(2); 1236e521259dSpetede #endif 1237e521259dSpetede break; 1238e521259dSpetede case SS11: 1239e521259dSpetede /* FALLTHROUGH */ 1240e521259dSpetede case (SS11|M32): 1241e521259dSpetede case (SS11|M64): 1242e521259dSpetede break; 1243e521259dSpetede case (SS12|M32): 1244e521259dSpetede #if defined(__sparc) 1245e521259dSpetede /* 1246e521259dSpetede * Need to add in further 32 bit options because with SS12 1247e521259dSpetede * the xarch=sparcvis option can be applied to 32 or 64 1248e521259dSpetede * bit, and so the translatation table (xtbl) cannot handle 1249e521259dSpetede * that. 1250e521259dSpetede */ 1251e521259dSpetede newae(ctx->i_ae, "-mv8plus"); 1252e521259dSpetede #endif 1253e521259dSpetede break; 1254e521259dSpetede case (SS12|M64): 1255e521259dSpetede break; 1256e521259dSpetede default: 1257e521259dSpetede (void) fprintf(stderr, 12587a6460b6Spetede "Incompatible -xarch= and/or -m32/-m64 options used.\n"); 1259e521259dSpetede exit(2); 1260e521259dSpetede } 1261aa9ef484SJohn Levon 126267deef8cSDan McDonald if ((op == CW_O_LINK || op == CW_O_PREPROCESS) && 126367deef8cSDan McDonald (ctx->i_flags & CW_F_SHADOW)) 126467deef8cSDan McDonald exit(0); 126580ab886dSwesolows 1266e1bf37b1SRichard Lowe if (model != NULL) 126780ab886dSwesolows newae(ctx->i_ae, model); 12687c478bd9Sstevel@tonic-gate if (!nolibc) 126980ab886dSwesolows newae(ctx->i_ae, "-lc"); 127080ab886dSwesolows if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) { 127180ab886dSwesolows newae(ctx->i_ae, "-o"); 127280ab886dSwesolows newae(ctx->i_ae, ctx->i_discard); 127380ab886dSwesolows } 127480ab886dSwesolows } 127580ab886dSwesolows 1276*1f5207b7SJohn Levon static void 1277*1f5207b7SJohn Levon do_smatch(cw_ictx_t *ctx) 1278*1f5207b7SJohn Levon { 1279*1f5207b7SJohn Levon if (ctx->i_flags & CW_F_PROG) { 1280*1f5207b7SJohn Levon newae(ctx->i_ae, "--version"); 1281*1f5207b7SJohn Levon return; 1282*1f5207b7SJohn Levon } 1283*1f5207b7SJohn Levon 1284*1f5207b7SJohn Levon /* 1285*1f5207b7SJohn Levon * Some sources shouldn't run smatch at all. 1286*1f5207b7SJohn Levon */ 1287*1f5207b7SJohn Levon for (int i = 0; i < ctx->i_oldargc; i++) { 1288*1f5207b7SJohn Levon char *arg = ctx->i_oldargv[i]; 1289*1f5207b7SJohn Levon 1290*1f5207b7SJohn Levon if (strcmp(arg, "-_smatch=off") == 0) { 1291*1f5207b7SJohn Levon ctx->i_flags &= ~ (CW_F_EXEC | CW_F_ECHO); 1292*1f5207b7SJohn Levon return; 1293*1f5207b7SJohn Levon } 1294*1f5207b7SJohn Levon } 1295*1f5207b7SJohn Levon 1296*1f5207b7SJohn Levon /* 1297*1f5207b7SJohn Levon * smatch can handle gcc's options. 1298*1f5207b7SJohn Levon */ 1299*1f5207b7SJohn Levon do_gcc(ctx); 1300*1f5207b7SJohn Levon } 1301*1f5207b7SJohn Levon 130280ab886dSwesolows static void 130380ab886dSwesolows do_cc(cw_ictx_t *ctx) 130480ab886dSwesolows { 130567deef8cSDan McDonald int in_output = 0, seen_o = 0; 130680ab886dSwesolows cw_op_t op = CW_O_LINK; 1307aa9ef484SJohn Levon char *nameflag; 130880ab886dSwesolows 13091912d2c4Swesolows if (ctx->i_flags & CW_F_PROG) { 13101912d2c4Swesolows newae(ctx->i_ae, "-V"); 13111912d2c4Swesolows return; 13121912d2c4Swesolows } 13131912d2c4Swesolows 1314aa9ef484SJohn Levon if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1) 1315aa9ef484SJohn Levon nomem(); 1316aa9ef484SJohn Levon 131780ab886dSwesolows while (--ctx->i_oldargc > 0) { 131880ab886dSwesolows char *arg = *++ctx->i_oldargv; 131980ab886dSwesolows 1320aa9ef484SJohn Levon if (strncmp(arg, "-_CC=", 5) == 0) { 1321aa9ef484SJohn Levon newae(ctx->i_ae, strchr(arg, '=') + 1); 1322aa9ef484SJohn Levon continue; 1323aa9ef484SJohn Levon } 1324aa9ef484SJohn Levon 132580ab886dSwesolows if (*arg != '-') { 132680ab886dSwesolows if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) { 132780ab886dSwesolows newae(ctx->i_ae, arg); 132880ab886dSwesolows } else { 132980ab886dSwesolows in_output = 0; 133080ab886dSwesolows newae(ctx->i_ae, ctx->i_discard); 133180ab886dSwesolows } 133280ab886dSwesolows continue; 133380ab886dSwesolows } 133480ab886dSwesolows switch (*(arg + 1)) { 133580ab886dSwesolows case '_': 1336aa9ef484SJohn Levon if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) || 1337aa9ef484SJohn Levon (strncmp(arg, "-_cc=", 5) == 0) || 1338aa9ef484SJohn Levon (strncmp(arg, "-_sun=", 6) == 0)) { 1339aa9ef484SJohn Levon newae(ctx->i_ae, strchr(arg, '=') + 1); 134080ab886dSwesolows } 134180ab886dSwesolows break; 1342aa9ef484SJohn Levon 134380ab886dSwesolows case 'V': 134480ab886dSwesolows ctx->i_flags &= ~CW_F_ECHO; 134580ab886dSwesolows newae(ctx->i_ae, arg); 134680ab886dSwesolows break; 134780ab886dSwesolows case 'o': 134880ab886dSwesolows seen_o = 1; 134980ab886dSwesolows if (strlen(arg) == 2) { 135080ab886dSwesolows in_output = 1; 135180ab886dSwesolows newae(ctx->i_ae, arg); 135280ab886dSwesolows } else if (ctx->i_flags & CW_F_SHADOW) { 135380ab886dSwesolows newae(ctx->i_ae, "-o"); 135480ab886dSwesolows newae(ctx->i_ae, ctx->i_discard); 135580ab886dSwesolows } else { 135680ab886dSwesolows newae(ctx->i_ae, arg); 135780ab886dSwesolows } 135880ab886dSwesolows break; 135980ab886dSwesolows case 'c': 136080ab886dSwesolows case 'S': 13610bb3415fSrie if (strlen(arg) == 2) 13620bb3415fSrie op = CW_O_COMPILE; 136380ab886dSwesolows newae(ctx->i_ae, arg); 136480ab886dSwesolows break; 136580ab886dSwesolows case 'E': 136680ab886dSwesolows case 'P': 13670bb3415fSrie if (strlen(arg) == 2) 13680bb3415fSrie op = CW_O_PREPROCESS; 136980ab886dSwesolows /*FALLTHROUGH*/ 137080ab886dSwesolows default: 137180ab886dSwesolows newae(ctx->i_ae, arg); 137280ab886dSwesolows } 137380ab886dSwesolows } 137480ab886dSwesolows 1375aa9ef484SJohn Levon free(nameflag); 1376aa9ef484SJohn Levon 137767deef8cSDan McDonald if ((op == CW_O_LINK || op == CW_O_PREPROCESS) && 137867deef8cSDan McDonald (ctx->i_flags & CW_F_SHADOW)) 137967deef8cSDan McDonald exit(0); 138080ab886dSwesolows 138180ab886dSwesolows if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) { 138280ab886dSwesolows newae(ctx->i_ae, "-o"); 138380ab886dSwesolows newae(ctx->i_ae, ctx->i_discard); 138480ab886dSwesolows } 13857c478bd9Sstevel@tonic-gate } 13867c478bd9Sstevel@tonic-gate 13877c478bd9Sstevel@tonic-gate static void 138880ab886dSwesolows prepctx(cw_ictx_t *ctx) 13897c478bd9Sstevel@tonic-gate { 1390aa9ef484SJohn Levon newae(ctx->i_ae, ctx->i_compiler->c_path); 139180ab886dSwesolows 13921912d2c4Swesolows if (ctx->i_flags & CW_F_PROG) { 13931912d2c4Swesolows (void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ? 1394aa9ef484SJohn Levon "shadow" : "primary", ctx->i_compiler->c_path); 13951912d2c4Swesolows (void) fflush(stdout); 13961912d2c4Swesolows } 13971912d2c4Swesolows 139880ab886dSwesolows if (!(ctx->i_flags & CW_F_XLATE)) 139980ab886dSwesolows return; 140080ab886dSwesolows 1401aa9ef484SJohn Levon switch (ctx->i_compiler->c_style) { 1402aa9ef484SJohn Levon case SUN: 140380ab886dSwesolows do_cc(ctx); 140480ab886dSwesolows break; 1405aa9ef484SJohn Levon case GNU: 140680ab886dSwesolows do_gcc(ctx); 140780ab886dSwesolows break; 1408*1f5207b7SJohn Levon case SMATCH: 1409*1f5207b7SJohn Levon do_smatch(ctx); 1410*1f5207b7SJohn Levon break; 141180ab886dSwesolows } 141280ab886dSwesolows } 141380ab886dSwesolows 141480ab886dSwesolows static int 141580ab886dSwesolows invoke(cw_ictx_t *ctx) 141680ab886dSwesolows { 141780ab886dSwesolows char **newargv; 141880ab886dSwesolows int ac; 141980ab886dSwesolows struct ae *a; 142080ab886dSwesolows 142180ab886dSwesolows if ((newargv = calloc(sizeof (*newargv), ctx->i_ae->ael_argc + 1)) == 142280ab886dSwesolows NULL) 142380ab886dSwesolows nomem(); 142480ab886dSwesolows 142580ab886dSwesolows if (ctx->i_flags & CW_F_ECHO) 142680ab886dSwesolows (void) fprintf(stderr, "+ "); 142780ab886dSwesolows 142880ab886dSwesolows for (ac = 0, a = ctx->i_ae->ael_head; a; a = a->ae_next, ac++) { 142980ab886dSwesolows newargv[ac] = a->ae_arg; 143080ab886dSwesolows if (ctx->i_flags & CW_F_ECHO) 143180ab886dSwesolows (void) fprintf(stderr, "%s ", a->ae_arg); 143280ab886dSwesolows if (a == ctx->i_ae->ael_tail) 143380ab886dSwesolows break; 143480ab886dSwesolows } 143580ab886dSwesolows 143680ab886dSwesolows if (ctx->i_flags & CW_F_ECHO) { 143780ab886dSwesolows (void) fprintf(stderr, "\n"); 143880ab886dSwesolows (void) fflush(stderr); 143980ab886dSwesolows } 144080ab886dSwesolows 144180ab886dSwesolows if (!(ctx->i_flags & CW_F_EXEC)) 144280ab886dSwesolows return (0); 144380ab886dSwesolows 14447c478bd9Sstevel@tonic-gate /* 1445aa9ef484SJohn Levon * We must fix up the environment here so that the dependency files are 1446aa9ef484SJohn Levon * not trampled by the shadow compiler. Also take care of GCC 1447aa9ef484SJohn Levon * environment variables that will throw off gcc. This assumes a primary 1448aa9ef484SJohn Levon * gcc. 14497c478bd9Sstevel@tonic-gate */ 145080ab886dSwesolows if ((ctx->i_flags & CW_F_SHADOW) && 145180ab886dSwesolows (unsetenv("SUNPRO_DEPENDENCIES") != 0 || 1452aa9ef484SJohn Levon unsetenv("DEPENDENCIES_OUTPUT") != 0 || 1453aa9ef484SJohn Levon unsetenv("GCC_ROOT") != 0)) { 145480ab886dSwesolows (void) fprintf(stderr, "error: environment setup failed: %s\n", 145580ab886dSwesolows strerror(errno)); 145680ab886dSwesolows return (-1); 145780ab886dSwesolows } 14587c478bd9Sstevel@tonic-gate 145980ab886dSwesolows (void) execv(newargv[0], newargv); 1460aa9ef484SJohn Levon warn("couldn't run %s", newargv[0]); 14617c478bd9Sstevel@tonic-gate 146280ab886dSwesolows return (-1); 146380ab886dSwesolows } 146480ab886dSwesolows 146580ab886dSwesolows static int 146680ab886dSwesolows reap(cw_ictx_t *ctx) 146780ab886dSwesolows { 14681912d2c4Swesolows int status, ret = 0; 146980ab886dSwesolows char buf[1024]; 147080ab886dSwesolows struct stat s; 147180ab886dSwesolows 1472c3f177eaSPeter Dennis - Sustaining Engineer /* 1473c3f177eaSPeter Dennis - Sustaining Engineer * Only wait for one specific child. 1474c3f177eaSPeter Dennis - Sustaining Engineer */ 1475c3f177eaSPeter Dennis - Sustaining Engineer if (ctx->i_pid <= 0) 1476c3f177eaSPeter Dennis - Sustaining Engineer return (-1); 1477c3f177eaSPeter Dennis - Sustaining Engineer 147880ab886dSwesolows do { 1479c3f177eaSPeter Dennis - Sustaining Engineer if (waitpid(ctx->i_pid, &status, 0) < 0) { 1480aa9ef484SJohn Levon warn("cannot reap child"); 1481c3f177eaSPeter Dennis - Sustaining Engineer return (-1); 1482c3f177eaSPeter Dennis - Sustaining Engineer } 14831912d2c4Swesolows if (status != 0) { 14841912d2c4Swesolows if (WIFSIGNALED(status)) { 14851912d2c4Swesolows ret = -WTERMSIG(status); 148680ab886dSwesolows break; 14871912d2c4Swesolows } else if (WIFEXITED(status)) { 14881912d2c4Swesolows ret = WEXITSTATUS(status); 148980ab886dSwesolows break; 149080ab886dSwesolows } 14917c478bd9Sstevel@tonic-gate } 14921912d2c4Swesolows } while (!WIFEXITED(status) && !WIFSIGNALED(status)); 149380ab886dSwesolows 149480ab886dSwesolows (void) unlink(ctx->i_discard); 149580ab886dSwesolows 14961912d2c4Swesolows if (stat(ctx->i_stderr, &s) < 0) { 1497aa9ef484SJohn Levon warn("stat failed on child cleanup"); 149880ab886dSwesolows return (-1); 14997c478bd9Sstevel@tonic-gate } 150080ab886dSwesolows if (s.st_size != 0) { 15011912d2c4Swesolows FILE *f; 150280ab886dSwesolows 15031912d2c4Swesolows if ((f = fopen(ctx->i_stderr, "r")) != NULL) { 15041912d2c4Swesolows while (fgets(buf, sizeof (buf), f)) 15051912d2c4Swesolows (void) fprintf(stderr, "%s", buf); 15061912d2c4Swesolows (void) fflush(stderr); 15071912d2c4Swesolows (void) fclose(f); 15081912d2c4Swesolows } 150980ab886dSwesolows } 15101912d2c4Swesolows (void) unlink(ctx->i_stderr); 15111912d2c4Swesolows free(ctx->i_stderr); 15121912d2c4Swesolows 15131912d2c4Swesolows /* 15141912d2c4Swesolows * cc returns an error code when given -V; we want that to succeed. 15151912d2c4Swesolows */ 15161912d2c4Swesolows if (ctx->i_flags & CW_F_PROG) 15171912d2c4Swesolows return (0); 151880ab886dSwesolows 151980ab886dSwesolows return (ret); 152080ab886dSwesolows } 152180ab886dSwesolows 152280ab886dSwesolows static int 152380ab886dSwesolows exec_ctx(cw_ictx_t *ctx, int block) 152480ab886dSwesolows { 152567deef8cSDan McDonald char *file; 152667deef8cSDan McDonald 152767deef8cSDan McDonald /* 152867deef8cSDan McDonald * To avoid offending cc's sensibilities, the name of its output 152967deef8cSDan McDonald * file must end in '.o'. 153067deef8cSDan McDonald */ 153167deef8cSDan McDonald if ((file = tempnam(NULL, ".cw")) == NULL) { 153267deef8cSDan McDonald nomem(); 153367deef8cSDan McDonald return (-1); 153467deef8cSDan McDonald } 153567deef8cSDan McDonald (void) strlcpy(ctx->i_discard, file, MAXPATHLEN); 153667deef8cSDan McDonald (void) strlcat(ctx->i_discard, ".o", MAXPATHLEN); 153767deef8cSDan McDonald free(file); 153867deef8cSDan McDonald 15391912d2c4Swesolows if ((ctx->i_stderr = tempnam(NULL, ".cw")) == NULL) { 15401912d2c4Swesolows nomem(); 154180ab886dSwesolows return (-1); 154280ab886dSwesolows } 154380ab886dSwesolows 154480ab886dSwesolows if ((ctx->i_pid = fork()) == 0) { 15451912d2c4Swesolows int fd; 15461912d2c4Swesolows 154780ab886dSwesolows (void) fclose(stderr); 15481912d2c4Swesolows if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL, 15491912d2c4Swesolows 0666)) < 0) { 1550aa9ef484SJohn Levon err(1, "open failed for standard error"); 15511912d2c4Swesolows } 15521912d2c4Swesolows if (dup2(fd, 2) < 0) { 1553aa9ef484SJohn Levon err(1, "dup2 failed for standard error"); 155480ab886dSwesolows } 15551912d2c4Swesolows if (fd != 2) 15561912d2c4Swesolows (void) close(fd); 155780ab886dSwesolows if (freopen("/dev/fd/2", "w", stderr) == NULL) { 1558aa9ef484SJohn Levon err(1, "freopen failed for /dev/fd/2"); 155980ab886dSwesolows } 1560aa9ef484SJohn Levon 156180ab886dSwesolows prepctx(ctx); 156280ab886dSwesolows exit(invoke(ctx)); 156380ab886dSwesolows } 156480ab886dSwesolows 156580ab886dSwesolows if (ctx->i_pid < 0) { 1566aa9ef484SJohn Levon err(1, "fork failed"); 156780ab886dSwesolows } 156880ab886dSwesolows 156980ab886dSwesolows if (block) 157080ab886dSwesolows return (reap(ctx)); 157180ab886dSwesolows 157280ab886dSwesolows return (0); 15737c478bd9Sstevel@tonic-gate } 15747c478bd9Sstevel@tonic-gate 1575aa9ef484SJohn Levon static void 1576aa9ef484SJohn Levon parse_compiler(const char *spec, cw_compiler_t *compiler) 15777c478bd9Sstevel@tonic-gate { 1578aa9ef484SJohn Levon char *tspec, *token; 15797c478bd9Sstevel@tonic-gate 1580aa9ef484SJohn Levon if ((tspec = strdup(spec)) == NULL) 158180ab886dSwesolows nomem(); 158280ab886dSwesolows 1583aa9ef484SJohn Levon if ((token = strsep(&tspec, ",")) == NULL) 1584aa9ef484SJohn Levon errx(1, "Compiler is missing a name: %s", spec); 1585aa9ef484SJohn Levon compiler->c_name = token; 15867c478bd9Sstevel@tonic-gate 1587aa9ef484SJohn Levon if ((token = strsep(&tspec, ",")) == NULL) 1588aa9ef484SJohn Levon errx(1, "Compiler is missing a path: %s", spec); 1589aa9ef484SJohn Levon compiler->c_path = token; 15907c478bd9Sstevel@tonic-gate 1591aa9ef484SJohn Levon if ((token = strsep(&tspec, ",")) == NULL) 1592aa9ef484SJohn Levon errx(1, "Compiler is missing a style: %s", spec); 15937c478bd9Sstevel@tonic-gate 1594aa9ef484SJohn Levon if ((strcasecmp(token, "gnu") == 0) || 1595*1f5207b7SJohn Levon (strcasecmp(token, "gcc") == 0)) { 1596aa9ef484SJohn Levon compiler->c_style = GNU; 1597*1f5207b7SJohn Levon } else if ((strcasecmp(token, "sun") == 0) || 1598*1f5207b7SJohn Levon (strcasecmp(token, "cc") == 0)) { 1599aa9ef484SJohn Levon compiler->c_style = SUN; 1600*1f5207b7SJohn Levon } else if ((strcasecmp(token, "smatch") == 0)) { 1601*1f5207b7SJohn Levon compiler->c_style = SMATCH; 1602*1f5207b7SJohn Levon } else { 1603aa9ef484SJohn Levon errx(1, "unknown compiler style: %s", token); 1604*1f5207b7SJohn Levon } 160580ab886dSwesolows 1606aa9ef484SJohn Levon if (tspec != NULL) 1607aa9ef484SJohn Levon errx(1, "Excess tokens in compiler: %s", spec); 1608aa9ef484SJohn Levon } 16097c478bd9Sstevel@tonic-gate 1610aa9ef484SJohn Levon int 1611aa9ef484SJohn Levon main(int argc, char **argv) 1612aa9ef484SJohn Levon { 1613aa9ef484SJohn Levon int ch; 1614aa9ef484SJohn Levon cw_compiler_t primary = { NULL, NULL, 0 }; 1615aa9ef484SJohn Levon cw_compiler_t shadows[10]; 1616aa9ef484SJohn Levon int nshadows = 0; 1617aa9ef484SJohn Levon int ret = 0; 1618aa9ef484SJohn Levon boolean_t do_serial = B_FALSE; 1619aa9ef484SJohn Levon boolean_t do_exec = B_FALSE; 1620aa9ef484SJohn Levon boolean_t vflg = B_FALSE; 1621aa9ef484SJohn Levon boolean_t Cflg = B_FALSE; 1622aa9ef484SJohn Levon boolean_t cflg = B_FALSE; 1623aa9ef484SJohn Levon boolean_t nflg = B_FALSE; 1624aa9ef484SJohn Levon 1625aa9ef484SJohn Levon cw_ictx_t *main_ctx; 1626aa9ef484SJohn Levon 1627aa9ef484SJohn Levon static struct option longopts[] = { 1628aa9ef484SJohn Levon { "compiler", no_argument, NULL, 'c' }, 1629aa9ef484SJohn Levon { "noecho", no_argument, NULL, 'n' }, 1630aa9ef484SJohn Levon { "primary", required_argument, NULL, 'p' }, 1631aa9ef484SJohn Levon { "shadow", required_argument, NULL, 's' }, 1632aa9ef484SJohn Levon { "versions", no_argument, NULL, 'v' }, 1633aa9ef484SJohn Levon { NULL, 0, NULL, 0 }, 1634aa9ef484SJohn Levon }; 1635aa9ef484SJohn Levon 1636aa9ef484SJohn Levon 1637aa9ef484SJohn Levon if ((main_ctx = newictx()) == NULL) 1638aa9ef484SJohn Levon nomem(); 1639aa9ef484SJohn Levon 1640aa9ef484SJohn Levon while ((ch = getopt_long(argc, argv, "C", longopts, NULL)) != -1) { 1641aa9ef484SJohn Levon switch (ch) { 1642aa9ef484SJohn Levon case 'c': 1643aa9ef484SJohn Levon cflg = B_TRUE; 1644aa9ef484SJohn Levon break; 1645aa9ef484SJohn Levon case 'C': 1646aa9ef484SJohn Levon Cflg = B_TRUE; 1647aa9ef484SJohn Levon break; 1648aa9ef484SJohn Levon case 'n': 1649aa9ef484SJohn Levon nflg = B_TRUE; 1650aa9ef484SJohn Levon break; 1651aa9ef484SJohn Levon case 'p': 1652aa9ef484SJohn Levon if (primary.c_path != NULL) { 1653aa9ef484SJohn Levon warnx("Only one primary compiler may " 1654aa9ef484SJohn Levon "be specified"); 1655aa9ef484SJohn Levon usage(); 1656aa9ef484SJohn Levon } 1657aa9ef484SJohn Levon 1658aa9ef484SJohn Levon parse_compiler(optarg, &primary); 1659aa9ef484SJohn Levon break; 1660aa9ef484SJohn Levon case 's': 1661aa9ef484SJohn Levon if (nshadows >= 10) 1662aa9ef484SJohn Levon errx(1, "May only use 10 shadows at " 1663aa9ef484SJohn Levon "the moment"); 1664aa9ef484SJohn Levon parse_compiler(optarg, &shadows[nshadows]); 1665aa9ef484SJohn Levon nshadows++; 1666aa9ef484SJohn Levon break; 1667aa9ef484SJohn Levon case 'v': 1668aa9ef484SJohn Levon vflg = B_TRUE; 1669aa9ef484SJohn Levon break; 1670aa9ef484SJohn Levon default: 1671aa9ef484SJohn Levon (void) fprintf(stderr, "Did you forget '--'?\n"); 1672aa9ef484SJohn Levon usage(); 1673aa9ef484SJohn Levon } 1674aa9ef484SJohn Levon } 1675aa9ef484SJohn Levon 1676aa9ef484SJohn Levon if (primary.c_path == NULL) { 1677aa9ef484SJohn Levon warnx("A primary compiler must be specified"); 16787c478bd9Sstevel@tonic-gate usage(); 16797c478bd9Sstevel@tonic-gate } 16807c478bd9Sstevel@tonic-gate 1681aa9ef484SJohn Levon do_serial = (getenv("CW_SHADOW_SERIAL") == NULL) ? B_FALSE : B_TRUE; 1682aa9ef484SJohn Levon do_exec = (getenv("CW_NO_EXEC") == NULL) ? B_TRUE : B_FALSE; 1683aa9ef484SJohn Levon 1684aa9ef484SJohn Levon /* Leave room for argv[0] */ 1685aa9ef484SJohn Levon argc -= (optind - 1); 1686aa9ef484SJohn Levon argv += (optind - 1); 1687aa9ef484SJohn Levon 1688aa9ef484SJohn Levon main_ctx->i_oldargc = argc; 1689aa9ef484SJohn Levon main_ctx->i_oldargv = argv; 1690aa9ef484SJohn Levon main_ctx->i_flags = CW_F_XLATE; 1691aa9ef484SJohn Levon if (nflg == 0) 1692aa9ef484SJohn Levon main_ctx->i_flags |= CW_F_ECHO; 1693aa9ef484SJohn Levon if (do_exec) 1694aa9ef484SJohn Levon main_ctx->i_flags |= CW_F_EXEC; 1695aa9ef484SJohn Levon if (Cflg) 1696aa9ef484SJohn Levon main_ctx->i_flags |= CW_F_CXX; 1697aa9ef484SJohn Levon main_ctx->i_compiler = &primary; 1698aa9ef484SJohn Levon 1699aa9ef484SJohn Levon if (cflg) { 1700aa9ef484SJohn Levon (void) fputs(primary.c_path, stdout); 17017c478bd9Sstevel@tonic-gate } 17027c478bd9Sstevel@tonic-gate 1703aa9ef484SJohn Levon if (vflg) { 1704aa9ef484SJohn Levon (void) printf("cw version %s\n", CW_VERSION); 17051912d2c4Swesolows (void) fflush(stdout); 1706aa9ef484SJohn Levon main_ctx->i_flags &= ~CW_F_ECHO; 1707aa9ef484SJohn Levon main_ctx->i_flags |= CW_F_PROG | CW_F_EXEC; 17081912d2c4Swesolows do_serial = 1; 17091912d2c4Swesolows } 17101912d2c4Swesolows 1711aa9ef484SJohn Levon ret |= exec_ctx(main_ctx, do_serial); 17121912d2c4Swesolows 1713aa9ef484SJohn Levon for (int i = 0; i < nshadows; i++) { 1714aa9ef484SJohn Levon int r; 1715aa9ef484SJohn Levon cw_ictx_t *shadow_ctx; 171680ab886dSwesolows 1717aa9ef484SJohn Levon if ((shadow_ctx = newictx()) == NULL) 1718aa9ef484SJohn Levon nomem(); 1719aa9ef484SJohn Levon 1720aa9ef484SJohn Levon memcpy(shadow_ctx, main_ctx, sizeof (cw_ictx_t)); 1721aa9ef484SJohn Levon 1722aa9ef484SJohn Levon shadow_ctx->i_flags |= CW_F_SHADOW; 1723aa9ef484SJohn Levon shadow_ctx->i_compiler = &shadows[i]; 1724aa9ef484SJohn Levon 1725aa9ef484SJohn Levon r = exec_ctx(shadow_ctx, do_serial); 1726aa9ef484SJohn Levon if (r == 0) { 1727aa9ef484SJohn Levon shadow_ctx->i_next = main_ctx->i_next; 1728aa9ef484SJohn Levon main_ctx->i_next = shadow_ctx; 1729aa9ef484SJohn Levon } 1730aa9ef484SJohn Levon ret |= r; 17317c478bd9Sstevel@tonic-gate } 17327c478bd9Sstevel@tonic-gate 1733aa9ef484SJohn Levon if (!do_serial) { 1734aa9ef484SJohn Levon cw_ictx_t *next = main_ctx; 1735aa9ef484SJohn Levon while (next != NULL) { 1736aa9ef484SJohn Levon cw_ictx_t *toreap = next; 1737aa9ef484SJohn Levon next = next->i_next; 1738aa9ef484SJohn Levon ret |= reap(toreap); 1739aa9ef484SJohn Levon } 1740aa9ef484SJohn Levon } 17417c478bd9Sstevel@tonic-gate 174280ab886dSwesolows return (ret); 17437c478bd9Sstevel@tonic-gate } 1744