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. 291f5207b7SJohn Levon * 301f5207b7SJohn 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 */ 4269b1fd3fSRichard Lowe #define CW_VERSION "5.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 * -C Prevent preprocessor from removing comments 497c478bd9Sstevel@tonic-gate * -c Compile only - produce .o files, suppress linking 507c478bd9Sstevel@tonic-gate * -cg92 Alias for -xtarget=ss1000 517c478bd9Sstevel@tonic-gate * -D<name[=token]> Associate name with token as if by #define 527c478bd9Sstevel@tonic-gate * -d[y|n] dynamic [-dy] or static [-dn] option to linker 537c478bd9Sstevel@tonic-gate * -E Compile source through preprocessor only, output to stdout 547c478bd9Sstevel@tonic-gate * -erroff=<t> Suppress warnings specified by tags t(%none, %all, <tag list>) 557c478bd9Sstevel@tonic-gate * -errtags=<a> Display messages with tags a(no, yes) 567c478bd9Sstevel@tonic-gate * -errwarn=<t> Treats warnings specified by tags t(%none, %all, <tag list>) 577c478bd9Sstevel@tonic-gate * as errors 587c478bd9Sstevel@tonic-gate * -fast Optimize using a selection of options 597c478bd9Sstevel@tonic-gate * -fd Report old-style function definitions and declarations 607c478bd9Sstevel@tonic-gate * -fnonstd Initialize floating-point hardware to non-standard preferences 617c478bd9Sstevel@tonic-gate * -fns[=<yes|no>] Select non-standard floating point mode 627c478bd9Sstevel@tonic-gate * -fprecision=<p> Set FP rounding precision mode p(single, double, extended) 637c478bd9Sstevel@tonic-gate * -fround=<r> Select the IEEE rounding mode in effect at startup 647c478bd9Sstevel@tonic-gate * -fsimple[=<n>] Select floating-point optimization preferences <n> 657c478bd9Sstevel@tonic-gate * -fsingle Use single-precision arithmetic (-Xt and -Xs modes only) 667c478bd9Sstevel@tonic-gate * -ftrap=<t> Select floating-point trapping mode in effect at startup 677c478bd9Sstevel@tonic-gate * -fstore force floating pt. values to target precision on assignment 687c478bd9Sstevel@tonic-gate * -g Compile for debugging 697c478bd9Sstevel@tonic-gate * -H Print path name of each file included during compilation 707c478bd9Sstevel@tonic-gate * -h <name> Assign <name> to generated dynamic shared library 717c478bd9Sstevel@tonic-gate * -I<dir> Add <dir> to preprocessor #include file search path 727c478bd9Sstevel@tonic-gate * -i Passed to linker to ignore any LD_LIBRARY_PATH setting 737c478bd9Sstevel@tonic-gate * -keeptmp Keep temporary files created during compilation 747c478bd9Sstevel@tonic-gate * -L<dir> Pass to linker to add <dir> to the library search path 757c478bd9Sstevel@tonic-gate * -l<name> Link with library lib<name>.a or lib<name>.so 767c478bd9Sstevel@tonic-gate * -mc Remove duplicate strings from .comment section of output files 777c478bd9Sstevel@tonic-gate * -mr Remove all strings from .comment section of output files 787c478bd9Sstevel@tonic-gate * -mr,"string" Remove all strings and append "string" to .comment section 797c478bd9Sstevel@tonic-gate * -mt Specify options needed when compiling multi-threaded code 807c478bd9Sstevel@tonic-gate * -native Find available processor, generate code accordingly 817c478bd9Sstevel@tonic-gate * -nofstore Do not force floating pt. values to target precision 827c478bd9Sstevel@tonic-gate * on assignment 837c478bd9Sstevel@tonic-gate * -norunpath Do not build in a runtime path for shared libraries 84662492f5Ssherrym * -O Use default optimization level (-xO2 or -xO3. Check man page.) 857c478bd9Sstevel@tonic-gate * -o <outputfile> Set name of output file to <outputfile> 867c478bd9Sstevel@tonic-gate * -P Compile source through preprocessor only, output to .i file 877c478bd9Sstevel@tonic-gate * -p Compile for profiling with prof 887c478bd9Sstevel@tonic-gate * -Q[y|n] Emit/don't emit identification info to output file 897c478bd9Sstevel@tonic-gate * -R<dir[:dir]> Build runtime search path list into executable 907c478bd9Sstevel@tonic-gate * -S Compile and only generate assembly code (.s) 917c478bd9Sstevel@tonic-gate * -s Strip symbol table from the executable file 92159cf8a6Swesolows * -t Turn off duplicate symbol warnings when linking 937c478bd9Sstevel@tonic-gate * -U<name> Delete initial definition of preprocessor symbol <name> 947c478bd9Sstevel@tonic-gate * -V Report version number of each compilation phase 957c478bd9Sstevel@tonic-gate * -v Do stricter semantic checking 967c478bd9Sstevel@tonic-gate * -W<c>,<arg> Pass <arg> to specified component <c> (a,l,m,p,0,2,h,i,u) 977c478bd9Sstevel@tonic-gate * -w Suppress compiler warning messages 987c478bd9Sstevel@tonic-gate * -Xa Compile assuming ANSI C conformance, allow K & R extensions 997c478bd9Sstevel@tonic-gate * (default mode) 1007c478bd9Sstevel@tonic-gate * -Xs Compile assuming (pre-ANSI) K & R C style code 1017c478bd9Sstevel@tonic-gate * -Xt Compile assuming K & R conformance, allow ANSI C 1027c478bd9Sstevel@tonic-gate * -xarch=<a> Specify target architecture instruction set 1037c478bd9Sstevel@tonic-gate * -xbuiltin[=<b>] When profitable inline, or substitute intrinisic functions 1047c478bd9Sstevel@tonic-gate * for system functions, b={%all,%none} 1057c478bd9Sstevel@tonic-gate * -xCC Accept C++ style comments 1067c478bd9Sstevel@tonic-gate * -xchip=<c> Specify the target processor for use by the optimizer 1077c478bd9Sstevel@tonic-gate * -xcode=<c> Generate different code for forming addresses 1087c478bd9Sstevel@tonic-gate * -xcrossfile[=<n>] Enable optimization and inlining across source files, 1097c478bd9Sstevel@tonic-gate * n={0|1} 1107c478bd9Sstevel@tonic-gate * -xe Perform only syntax/semantic checking, no code generation 111f795e658Srie * -xF Compile for later mapfile reordering or unused section 112f795e658Srie * elimination 1137c478bd9Sstevel@tonic-gate * -xhelp=<f> Display on-line help information f(flags, readme, errors) 1147c478bd9Sstevel@tonic-gate * -xildoff Cancel -xildon 1157c478bd9Sstevel@tonic-gate * -xildon Enable use of the incremental linker, ild 1167c478bd9Sstevel@tonic-gate * -xinline=[<a>,...,<a>] Attempt inlining of specified user routines, 1177c478bd9Sstevel@tonic-gate * <a>={%auto,func,no%func} 1187c478bd9Sstevel@tonic-gate * -xlibmieee Force IEEE 754 return values for math routines in 1197c478bd9Sstevel@tonic-gate * exceptional cases 1207c478bd9Sstevel@tonic-gate * -xlibmil Inline selected libm math routines for optimization 1217c478bd9Sstevel@tonic-gate * -xlic_lib=sunperf Link in the Sun supplied performance libraries 1227c478bd9Sstevel@tonic-gate * -xlicinfo Show license server information 1237c478bd9Sstevel@tonic-gate * -xmaxopt=[off,1,2,3,4,5] maximum optimization level allowed on #pragma opt 1247c478bd9Sstevel@tonic-gate * -xO<n> Generate optimized code (n={1|2|3|4|5}) 1257c478bd9Sstevel@tonic-gate * -xP Print prototypes for function definitions 1267c478bd9Sstevel@tonic-gate * -xprofile=<p> Collect data for a profile or use a profile to optimize 1277c478bd9Sstevel@tonic-gate * <p>={{collect,use}[:<path>],tcov} 1287c478bd9Sstevel@tonic-gate * -xregs=<r> Control register allocation 1297c478bd9Sstevel@tonic-gate * -xs Allow debugging without object (.o) files 1307c478bd9Sstevel@tonic-gate * -xsb Compile for use with the WorkShop source browser 1317c478bd9Sstevel@tonic-gate * -xsbfast Generate only WorkShop source browser info, no compilation 1327c478bd9Sstevel@tonic-gate * -xsfpconst Represent unsuffixed floating point constants as single 1337c478bd9Sstevel@tonic-gate * precision 1347c478bd9Sstevel@tonic-gate * -xspace Do not do optimizations that increase code size 1357c478bd9Sstevel@tonic-gate * -xstrconst Place string literals into read-only data segment 1367c478bd9Sstevel@tonic-gate * -xtarget=<t> Specify target system for optimization 1377c478bd9Sstevel@tonic-gate * -xtemp=<dir> Set directory for temporary files to <dir> 1387c478bd9Sstevel@tonic-gate * -xtime Report the execution time for each compilation phase 1397c478bd9Sstevel@tonic-gate * -xunroll=n Enable unrolling loops n times where possible 1407c478bd9Sstevel@tonic-gate * -Y<c>,<dir> Specify <dir> for location of component <c> (a,l,m,p,0,h,i,u) 1417c478bd9Sstevel@tonic-gate * -YA,<dir> Change default directory searched for components 1427c478bd9Sstevel@tonic-gate * -YI,<dir> Change default directory searched for include files 1437c478bd9Sstevel@tonic-gate * -YP,<dir> Change default directory for finding libraries files 1447c478bd9Sstevel@tonic-gate * -YS,<dir> Change default directory for startup object files 1457c478bd9Sstevel@tonic-gate */ 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate /* 1487c478bd9Sstevel@tonic-gate * Translation table: 1497c478bd9Sstevel@tonic-gate */ 1507c478bd9Sstevel@tonic-gate /* 1517c478bd9Sstevel@tonic-gate * -# -v 1527c478bd9Sstevel@tonic-gate * -### error 1537c478bd9Sstevel@tonic-gate * -A<name[(tokens)]> pass-thru 1547c478bd9Sstevel@tonic-gate * -B<[static|dynamic]> pass-thru (syntax error for anything else) 1557c478bd9Sstevel@tonic-gate * -C pass-thru 1567c478bd9Sstevel@tonic-gate * -c pass-thru 1577c478bd9Sstevel@tonic-gate * -cg92 -m32 -mcpu=v8 -mtune=supersparc (SPARC only) 1587c478bd9Sstevel@tonic-gate * -D<name[=token]> pass-thru 1597c478bd9Sstevel@tonic-gate * -E pass-thru 1607c478bd9Sstevel@tonic-gate * -erroff=E_EMPTY_TRANSLATION_UNIT ignore 1617c478bd9Sstevel@tonic-gate * -errtags=%all -Wall 1627c478bd9Sstevel@tonic-gate * -errwarn=%all -Werror else -Wno-error 1637c478bd9Sstevel@tonic-gate * -fast error 1647c478bd9Sstevel@tonic-gate * -fd error 1657c478bd9Sstevel@tonic-gate * -fnonstd error 1667c478bd9Sstevel@tonic-gate * -fns[=<yes|no>] error 1677c478bd9Sstevel@tonic-gate * -fprecision=<p> error 1687c478bd9Sstevel@tonic-gate * -fround=<r> error 1697c478bd9Sstevel@tonic-gate * -fsimple[=<n>] error 1707c478bd9Sstevel@tonic-gate * -fsingle[=<n>] error 1717c478bd9Sstevel@tonic-gate * -ftrap=<t> error 1727c478bd9Sstevel@tonic-gate * -fstore error 1737c478bd9Sstevel@tonic-gate * -g pass-thru 1747c478bd9Sstevel@tonic-gate * -H pass-thru 1757c478bd9Sstevel@tonic-gate * -h <name> pass-thru 1767c478bd9Sstevel@tonic-gate * -I<dir> pass-thru 1777c478bd9Sstevel@tonic-gate * -i pass-thru 1787c478bd9Sstevel@tonic-gate * -keeptmp -save-temps 1797c478bd9Sstevel@tonic-gate * -L<dir> pass-thru 1807c478bd9Sstevel@tonic-gate * -l<name> pass-thru 1817c478bd9Sstevel@tonic-gate * -mc error 1827c478bd9Sstevel@tonic-gate * -mr error 1837c478bd9Sstevel@tonic-gate * -mr,"string" error 1847c478bd9Sstevel@tonic-gate * -mt -D_REENTRANT 1857c478bd9Sstevel@tonic-gate * -native error 1867c478bd9Sstevel@tonic-gate * -nofstore error 1877c478bd9Sstevel@tonic-gate * -nolib -nodefaultlibs 1887c478bd9Sstevel@tonic-gate * -norunpath ignore 189662492f5Ssherrym * -O -O1 (Check the man page to be certain) 1907c478bd9Sstevel@tonic-gate * -o <outputfile> pass-thru 1917c478bd9Sstevel@tonic-gate * -P -E -o filename.i (or error) 1927c478bd9Sstevel@tonic-gate * -p pass-thru 1937c478bd9Sstevel@tonic-gate * -Q[y|n] error 1947c478bd9Sstevel@tonic-gate * -R<dir[:dir]> pass-thru 1957c478bd9Sstevel@tonic-gate * -S pass-thru 1967c478bd9Sstevel@tonic-gate * -U<name> pass-thru 1977c478bd9Sstevel@tonic-gate * -V --version 1987c478bd9Sstevel@tonic-gate * -v -Wall 1997c478bd9Sstevel@tonic-gate * -Wa,<arg> pass-thru 2007c478bd9Sstevel@tonic-gate * -Wp,<arg> pass-thru except -xc99=<a> 2017c478bd9Sstevel@tonic-gate * -Wl,<arg> pass-thru 2027c478bd9Sstevel@tonic-gate * -W{m,0,2,h,i,u> error/ignore 20354836668Spetede * -xmodel=kernel -ffreestanding -mcmodel=kernel -mno-red-zone 204d430274bSsherrym * -Wu,-save_args -msave-args 2057c478bd9Sstevel@tonic-gate * -w pass-thru 2067c478bd9Sstevel@tonic-gate * -Xa -std=iso9899:199409 or -ansi 2077c478bd9Sstevel@tonic-gate * -Xt error 2087c478bd9Sstevel@tonic-gate * -Xs -traditional -std=c89 2097c478bd9Sstevel@tonic-gate * -xarch=<a> table 21002e56f3fSwesolows * -xbuiltin[=<b>] -fbuiltin (-fno-builtin otherwise) 2117c478bd9Sstevel@tonic-gate * -xCC ignore 2127c478bd9Sstevel@tonic-gate * -xchip=<c> table 2137c478bd9Sstevel@tonic-gate * -xcode=<c> table 214159cf8a6Swesolows * -xcrossfile[=<n>] ignore 2157c478bd9Sstevel@tonic-gate * -xe error 2167c478bd9Sstevel@tonic-gate * -xF error 2177c478bd9Sstevel@tonic-gate * -xhelp=<f> error 2187c478bd9Sstevel@tonic-gate * -xildoff ignore 2197c478bd9Sstevel@tonic-gate * -xildon ignore 2207c478bd9Sstevel@tonic-gate * -xinline ignore 2217c478bd9Sstevel@tonic-gate * -xlibmieee error 2227c478bd9Sstevel@tonic-gate * -xlibmil error 2237c478bd9Sstevel@tonic-gate * -xlic_lib=sunperf error 2247c478bd9Sstevel@tonic-gate * -xmaxopt=[...] error 2257c478bd9Sstevel@tonic-gate * -xO<n> -O<n> 2267c478bd9Sstevel@tonic-gate * -xP error 2277c478bd9Sstevel@tonic-gate * -xprofile=<p> error 2287c478bd9Sstevel@tonic-gate * -xregs=<r> table 2297c478bd9Sstevel@tonic-gate * -xs error 2307c478bd9Sstevel@tonic-gate * -xsb error 2317c478bd9Sstevel@tonic-gate * -xsbfast error 2327c478bd9Sstevel@tonic-gate * -xsfpconst error 2337c478bd9Sstevel@tonic-gate * -xspace ignore (-not -Os) 2347c478bd9Sstevel@tonic-gate * -xstrconst ignore 2357c478bd9Sstevel@tonic-gate * -xtarget=<t> table 2367c478bd9Sstevel@tonic-gate * -xtemp=<dir> error 2377c478bd9Sstevel@tonic-gate * -xtime error 2387c478bd9Sstevel@tonic-gate * -xtransition -Wtransition 2397c478bd9Sstevel@tonic-gate * -xunroll=n error 2408c1a3716Ssherrym * -W0,-xdbggen=no%usedonly -fno-eliminate-unused-debug-symbols 2418c1a3716Ssherrym * -fno-eliminate-unused-debug-types 2427c478bd9Sstevel@tonic-gate * -Y<c>,<dir> error 2437c478bd9Sstevel@tonic-gate * -YA,<dir> error 2447c478bd9Sstevel@tonic-gate * -YI,<dir> -nostdinc -I<dir> 2457c478bd9Sstevel@tonic-gate * -YP,<dir> error 2467c478bd9Sstevel@tonic-gate * -YS,<dir> error 2477c478bd9Sstevel@tonic-gate */ 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate #include <ctype.h> 250aa9ef484SJohn Levon #include <err.h> 25180ab886dSwesolows #include <errno.h> 252aa9ef484SJohn Levon #include <fcntl.h> 253aa9ef484SJohn Levon #include <getopt.h> 254aa9ef484SJohn Levon #include <stdio.h> 255aa9ef484SJohn Levon #include <stdlib.h> 256aa9ef484SJohn Levon #include <string.h> 257aa9ef484SJohn Levon #include <unistd.h> 25888e61e85SRichard Lowe #include <dirent.h> 259aa9ef484SJohn Levon 2607c478bd9Sstevel@tonic-gate #include <sys/param.h> 26180ab886dSwesolows #include <sys/stat.h> 262aa9ef484SJohn Levon #include <sys/types.h> 263aa9ef484SJohn Levon #include <sys/utsname.h> 264aa9ef484SJohn Levon #include <sys/wait.h> 26580ab886dSwesolows 26680ab886dSwesolows #define CW_F_CXX 0x01 26780ab886dSwesolows #define CW_F_SHADOW 0x02 26880ab886dSwesolows #define CW_F_EXEC 0x04 26980ab886dSwesolows #define CW_F_ECHO 0x08 27080ab886dSwesolows #define CW_F_XLATE 0x10 2711912d2c4Swesolows #define CW_F_PROG 0x20 27280ab886dSwesolows 27380ab886dSwesolows typedef enum cw_op { 27480ab886dSwesolows CW_O_NONE = 0, 27580ab886dSwesolows CW_O_PREPROCESS, 27680ab886dSwesolows CW_O_COMPILE, 27780ab886dSwesolows CW_O_LINK 27880ab886dSwesolows } cw_op_t; 27980ab886dSwesolows 28080ab886dSwesolows struct aelist { 28180ab886dSwesolows struct ae { 28280ab886dSwesolows struct ae *ae_next; 28380ab886dSwesolows char *ae_arg; 28480ab886dSwesolows } *ael_head, *ael_tail; 28580ab886dSwesolows int ael_argc; 28680ab886dSwesolows }; 28780ab886dSwesolows 288aa9ef484SJohn Levon typedef enum { 289aa9ef484SJohn Levon GNU, 2901f5207b7SJohn Levon SUN, 2911f5207b7SJohn Levon SMATCH 292aa9ef484SJohn Levon } compiler_style_t; 293aa9ef484SJohn Levon 294aa9ef484SJohn Levon typedef struct { 295aa9ef484SJohn Levon char *c_name; 296aa9ef484SJohn Levon char *c_path; 297aa9ef484SJohn Levon compiler_style_t c_style; 298aa9ef484SJohn Levon } cw_compiler_t; 299aa9ef484SJohn Levon 30080ab886dSwesolows typedef struct cw_ictx { 301aa9ef484SJohn Levon struct cw_ictx *i_next; 302aa9ef484SJohn Levon cw_compiler_t *i_compiler; 30369b1fd3fSRichard Lowe char *i_linker; 30480ab886dSwesolows struct aelist *i_ae; 30580ab886dSwesolows uint32_t i_flags; 30680ab886dSwesolows int i_oldargc; 30780ab886dSwesolows char **i_oldargv; 30880ab886dSwesolows pid_t i_pid; 30988e61e85SRichard Lowe char *i_tmpdir; 3101912d2c4Swesolows char *i_stderr; 31180ab886dSwesolows } cw_ictx_t; 31280ab886dSwesolows 313e521259dSpetede /* 314e521259dSpetede * Status values to indicate which Studio compiler and associated 315e521259dSpetede * flags are being used. 316e521259dSpetede */ 317e521259dSpetede #define M32 0x01 /* -m32 - only on Studio 12 */ 318e521259dSpetede #define M64 0x02 /* -m64 - only on Studio 12 */ 319e521259dSpetede #define SS11 0x100 /* Studio 11 */ 320e521259dSpetede #define SS12 0x200 /* Studio 12 */ 3217c478bd9Sstevel@tonic-gate 322e521259dSpetede #define TRANS_ENTRY 5 323e521259dSpetede /* 324e521259dSpetede * Translation table definition for the -xarch= flag. The "x_arg" 325e521259dSpetede * value is translated into the appropriate gcc flags according 326e521259dSpetede * to the values in x_trans[n]. The x_flags indicates what compiler 327e521259dSpetede * is being used and what flags have been set via the use of 328e521259dSpetede * "x_arg". 329e521259dSpetede */ 330e521259dSpetede typedef struct xarch_table { 331e521259dSpetede char *x_arg; 332e521259dSpetede int x_flags; 333e521259dSpetede char *x_trans[TRANS_ENTRY]; 334e521259dSpetede } xarch_table_t; 335e521259dSpetede 336e521259dSpetede /* 337e521259dSpetede * The translation table for the -xarch= flag used in the Studio compilers. 338e521259dSpetede */ 339e521259dSpetede static const xarch_table_t xtbl[] = { 3407c478bd9Sstevel@tonic-gate #if defined(__x86) 341aa9ef484SJohn Levon { "generic", SS11, {NULL} }, 3427a6460b6Spetede { "generic64", (SS11|M64), { "-m64", "-mtune=opteron" } }, 3437a6460b6Spetede { "amd64", (SS11|M64), { "-m64", "-mtune=opteron" } }, 3447a6460b6Spetede { "386", SS11, { "-march=i386" } }, 3457a6460b6Spetede { "pentium_pro", SS11, { "-march=pentiumpro" } }, 34625c28e83SPiotr Jasiukajtis { "sse", SS11, { "-msse", "-mfpmath=sse" } }, 34725c28e83SPiotr Jasiukajtis { "sse2", SS11, { "-msse2", "-mfpmath=sse" } }, 3487c478bd9Sstevel@tonic-gate #elif defined(__sparc) 3497a6460b6Spetede { "generic", (SS11|M32), { "-m32", "-mcpu=v8" } }, 3507a6460b6Spetede { "generic64", (SS11|M64), { "-m64", "-mcpu=v9" } }, 3517a6460b6Spetede { "v8", (SS11|M32), { "-m32", "-mcpu=v8", "-mno-v8plus" } }, 3527a6460b6Spetede { "v8plus", (SS11|M32), { "-m32", "-mcpu=v9", "-mv8plus" } }, 3537a6460b6Spetede { "v8plusa", (SS11|M32), { "-m32", "-mcpu=ultrasparc", "-mv8plus", 3547a6460b6Spetede "-mvis" } }, 3557a6460b6Spetede { "v8plusb", (SS11|M32), { "-m32", "-mcpu=ultrasparc3", "-mv8plus", 3567a6460b6Spetede "-mvis" } }, 3577a6460b6Spetede { "v9", (SS11|M64), { "-m64", "-mcpu=v9" } }, 3587a6460b6Spetede { "v9a", (SS11|M64), { "-m64", "-mcpu=ultrasparc", "-mvis" } }, 3597a6460b6Spetede { "v9b", (SS11|M64), { "-m64", "-mcpu=ultrasparc3", "-mvis" } }, 3607a6460b6Spetede { "sparc", SS12, { "-mcpu=v9", "-mv8plus" } }, 3617a6460b6Spetede { "sparcvis", SS12, { "-mcpu=ultrasparc", "-mvis" } }, 3627a6460b6Spetede { "sparcvis2", SS12, { "-mcpu=ultrasparc3", "-mvis" } } 3637c478bd9Sstevel@tonic-gate #endif 3647c478bd9Sstevel@tonic-gate }; 3657c478bd9Sstevel@tonic-gate 366e521259dSpetede static int xtbl_size = sizeof (xtbl) / sizeof (xarch_table_t); 367e521259dSpetede 3687c478bd9Sstevel@tonic-gate static const char *xchip_tbl[] = { 3697c478bd9Sstevel@tonic-gate #if defined(__x86) 3707c478bd9Sstevel@tonic-gate "386", "-mtune=i386", NULL, 3717c478bd9Sstevel@tonic-gate "486", "-mtune=i486", NULL, 3727c478bd9Sstevel@tonic-gate "pentium", "-mtune=pentium", NULL, 3737c478bd9Sstevel@tonic-gate "pentium_pro", "-mtune=pentiumpro", NULL, 3747c478bd9Sstevel@tonic-gate #elif defined(__sparc) 3757c478bd9Sstevel@tonic-gate "super", "-mtune=supersparc", NULL, 3767c478bd9Sstevel@tonic-gate "ultra", "-mtune=ultrasparc", NULL, 3777c478bd9Sstevel@tonic-gate "ultra3", "-mtune=ultrasparc3", NULL, 3787c478bd9Sstevel@tonic-gate #endif 3797c478bd9Sstevel@tonic-gate NULL, NULL 3807c478bd9Sstevel@tonic-gate }; 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate static const char *xcode_tbl[] = { 3837c478bd9Sstevel@tonic-gate #if defined(__sparc) 3847c478bd9Sstevel@tonic-gate "abs32", "-fno-pic", "-mcmodel=medlow", NULL, 3857c478bd9Sstevel@tonic-gate "abs44", "-fno-pic", "-mcmodel=medmid", NULL, 3867c478bd9Sstevel@tonic-gate "abs64", "-fno-pic", "-mcmodel=medany", NULL, 3877c478bd9Sstevel@tonic-gate "pic13", "-fpic", NULL, 3887c478bd9Sstevel@tonic-gate "pic32", "-fPIC", NULL, 3897c478bd9Sstevel@tonic-gate #endif 3907c478bd9Sstevel@tonic-gate NULL, NULL 3917c478bd9Sstevel@tonic-gate }; 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate static const char *xtarget_tbl[] = { 3947c478bd9Sstevel@tonic-gate #if defined(__x86) 3957c478bd9Sstevel@tonic-gate "pentium_pro", "-march=pentiumpro", NULL, 3967c478bd9Sstevel@tonic-gate #endif /* __x86 */ 3977c478bd9Sstevel@tonic-gate NULL, NULL 3987c478bd9Sstevel@tonic-gate }; 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate static const char *xregs_tbl[] = { 4017c478bd9Sstevel@tonic-gate #if defined(__sparc) 4027c478bd9Sstevel@tonic-gate "appl", "-mapp-regs", NULL, 4037c478bd9Sstevel@tonic-gate "no%appl", "-mno-app-regs", NULL, 4047c478bd9Sstevel@tonic-gate "float", "-mfpu", NULL, 4057c478bd9Sstevel@tonic-gate "no%float", "-mno-fpu", NULL, 4067c478bd9Sstevel@tonic-gate #endif /* __sparc */ 4077c478bd9Sstevel@tonic-gate NULL, NULL 4087c478bd9Sstevel@tonic-gate }; 4097c478bd9Sstevel@tonic-gate 41080ab886dSwesolows static void 41180ab886dSwesolows nomem(void) 41280ab886dSwesolows { 413aa9ef484SJohn Levon errx(1, "out of memory"); 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate static void 4177c478bd9Sstevel@tonic-gate newae(struct aelist *ael, const char *arg) 4187c478bd9Sstevel@tonic-gate { 4197c478bd9Sstevel@tonic-gate struct ae *ae; 4207c478bd9Sstevel@tonic-gate 42169b1fd3fSRichard Lowe if ((ae = calloc(1, sizeof (*ae))) == NULL) 42280ab886dSwesolows nomem(); 4237c478bd9Sstevel@tonic-gate ae->ae_arg = strdup(arg); 4247c478bd9Sstevel@tonic-gate if (ael->ael_tail == NULL) 4257c478bd9Sstevel@tonic-gate ael->ael_head = ae; 4267c478bd9Sstevel@tonic-gate else 4277c478bd9Sstevel@tonic-gate ael->ael_tail->ae_next = ae; 4287c478bd9Sstevel@tonic-gate ael->ael_tail = ae; 42980ab886dSwesolows ael->ael_argc++; 43080ab886dSwesolows } 43180ab886dSwesolows 43280ab886dSwesolows static cw_ictx_t * 43380ab886dSwesolows newictx(void) 43480ab886dSwesolows { 43569b1fd3fSRichard Lowe cw_ictx_t *ctx = calloc(1, sizeof (cw_ictx_t)); 43680ab886dSwesolows if (ctx) 43769b1fd3fSRichard Lowe if ((ctx->i_ae = calloc(1, sizeof (struct aelist))) == NULL) { 43880ab886dSwesolows free(ctx); 43980ab886dSwesolows return (NULL); 44080ab886dSwesolows } 44180ab886dSwesolows 44280ab886dSwesolows return (ctx); 4437c478bd9Sstevel@tonic-gate } 4447c478bd9Sstevel@tonic-gate 4457c478bd9Sstevel@tonic-gate static void 4467c478bd9Sstevel@tonic-gate error(const char *arg) 4477c478bd9Sstevel@tonic-gate { 448aa9ef484SJohn Levon errx(2, "error: mapping failed at or near arg '%s'", arg); 4497c478bd9Sstevel@tonic-gate } 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate /* 4527c478bd9Sstevel@tonic-gate * Add the current favourite set of warnings to the gcc invocation. 4537c478bd9Sstevel@tonic-gate */ 4547c478bd9Sstevel@tonic-gate static void 4557c478bd9Sstevel@tonic-gate warnings(struct aelist *h) 4567c478bd9Sstevel@tonic-gate { 4577c478bd9Sstevel@tonic-gate static int warningsonce; 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate if (warningsonce++) 4607c478bd9Sstevel@tonic-gate return; 4617c478bd9Sstevel@tonic-gate 4627014882cSRichard Lowe /* 4637014882cSRichard Lowe * Enable as many warnings as exist, then disable those that we never 4647014882cSRichard Lowe * ever want. 4657014882cSRichard Lowe */ 4667c478bd9Sstevel@tonic-gate newae(h, "-Wall"); 4677014882cSRichard Lowe newae(h, "-Wextra"); 4687c478bd9Sstevel@tonic-gate } 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate static void 4717c478bd9Sstevel@tonic-gate optim_disable(struct aelist *h, int level) 4727c478bd9Sstevel@tonic-gate { 4737c478bd9Sstevel@tonic-gate if (level >= 2) { 4747c478bd9Sstevel@tonic-gate newae(h, "-fno-strict-aliasing"); 4757c478bd9Sstevel@tonic-gate newae(h, "-fno-unit-at-a-time"); 4767c478bd9Sstevel@tonic-gate newae(h, "-fno-optimize-sibling-calls"); 4777c478bd9Sstevel@tonic-gate } 4787c478bd9Sstevel@tonic-gate } 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate static void 4817c478bd9Sstevel@tonic-gate Xsmode(struct aelist *h) 4827c478bd9Sstevel@tonic-gate { 4837c478bd9Sstevel@tonic-gate static int xsonce; 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate if (xsonce++) 4867c478bd9Sstevel@tonic-gate return; 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate newae(h, "-traditional"); 4897c478bd9Sstevel@tonic-gate newae(h, "-traditional-cpp"); 4907c478bd9Sstevel@tonic-gate } 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate static void 49369b1fd3fSRichard Lowe usage(void) 4947c478bd9Sstevel@tonic-gate { 495aa9ef484SJohn Levon extern char *__progname; 4967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 497aa9ef484SJohn Levon "usage: %s [-C] [--versions] --primary <compiler> " 498aa9ef484SJohn Levon "[--shadow <compiler>]... -- cflags...\n", 499aa9ef484SJohn Levon __progname); 500aa9ef484SJohn Levon (void) fprintf(stderr, "compilers take the form: name,path,style\n" 501aa9ef484SJohn Levon " - name: a unique name usable in flag specifiers\n" 502aa9ef484SJohn Levon " - path: path to the compiler binary\n" 503aa9ef484SJohn Levon " - style: the style of flags expected: either sun or gnu\n"); 5047c478bd9Sstevel@tonic-gate exit(2); 5057c478bd9Sstevel@tonic-gate } 5067c478bd9Sstevel@tonic-gate 507e521259dSpetede static int 508e521259dSpetede xlate_xtb(struct aelist *h, const char *xarg) 509e521259dSpetede { 510e521259dSpetede int i, j; 511e521259dSpetede 512e521259dSpetede for (i = 0; i < xtbl_size; i++) { 513e521259dSpetede if (strcmp(xtbl[i].x_arg, xarg) == 0) 514e521259dSpetede break; 515e521259dSpetede } 516e521259dSpetede 517e521259dSpetede /* 518e521259dSpetede * At the end of the table and so no matching "arg" entry 519e521259dSpetede * found and so this must be a bad -xarch= flag. 520e521259dSpetede */ 521e521259dSpetede if (i == xtbl_size) 522e521259dSpetede error(xarg); 523e521259dSpetede 524e521259dSpetede for (j = 0; j < TRANS_ENTRY; j++) { 525e521259dSpetede if (xtbl[i].x_trans[j] != NULL) 526e521259dSpetede newae(h, xtbl[i].x_trans[j]); 527e521259dSpetede else 528e521259dSpetede break; 529e521259dSpetede } 530e521259dSpetede return (xtbl[i].x_flags); 531e521259dSpetede 532e521259dSpetede } 533e521259dSpetede 5347c478bd9Sstevel@tonic-gate static void 5357c478bd9Sstevel@tonic-gate xlate(struct aelist *h, const char *xarg, const char **table) 5367c478bd9Sstevel@tonic-gate { 5377c478bd9Sstevel@tonic-gate while (*table != NULL && strcmp(xarg, *table) != 0) { 5387c478bd9Sstevel@tonic-gate while (*table != NULL) 5397c478bd9Sstevel@tonic-gate table++; 5407c478bd9Sstevel@tonic-gate table++; 5417c478bd9Sstevel@tonic-gate } 5427c478bd9Sstevel@tonic-gate 5437c478bd9Sstevel@tonic-gate if (*table == NULL) 5447c478bd9Sstevel@tonic-gate error(xarg); 5457c478bd9Sstevel@tonic-gate 5467c478bd9Sstevel@tonic-gate table++; 5477c478bd9Sstevel@tonic-gate 5487c478bd9Sstevel@tonic-gate while (*table != NULL) { 5497c478bd9Sstevel@tonic-gate newae(h, *table); 5507c478bd9Sstevel@tonic-gate table++; 5517c478bd9Sstevel@tonic-gate } 5527c478bd9Sstevel@tonic-gate } 5537c478bd9Sstevel@tonic-gate 55488e61e85SRichard Lowe /* 55588e61e85SRichard Lowe * The compiler wants the output file to end in appropriate extension. If 55688e61e85SRichard Lowe * we're generating a name from whole cloth (path == NULL), we assume that 55788e61e85SRichard Lowe * extension to be .o, otherwise we match the extension of the caller. 55888e61e85SRichard Lowe */ 55988e61e85SRichard Lowe static char * 56088e61e85SRichard Lowe discard_file_name(cw_ictx_t *ctx, const char *path) 56188e61e85SRichard Lowe { 56288e61e85SRichard Lowe char *ret, *ext; 56388e61e85SRichard Lowe char tmpl[] = "cwXXXXXX"; 56488e61e85SRichard Lowe 56588e61e85SRichard Lowe if (path == NULL) { 56688e61e85SRichard Lowe ext = ".o"; 56788e61e85SRichard Lowe } else { 56888e61e85SRichard Lowe ext = strrchr(path, '.'); 56988e61e85SRichard Lowe } 57088e61e85SRichard Lowe 57188e61e85SRichard Lowe /* 57288e61e85SRichard Lowe * We need absolute control over where the temporary file goes, since 57388e61e85SRichard Lowe * we rely on it for cleanup so tempnam(3C) and tmpnam(3C) are 57488e61e85SRichard Lowe * inappropriate (they use TMPDIR, preferentially). 57588e61e85SRichard Lowe * 57688e61e85SRichard Lowe * mkstemp(3C) doesn't actually help us, since the temporary file 57788e61e85SRichard Lowe * isn't used by us, only its name. 57888e61e85SRichard Lowe */ 57988e61e85SRichard Lowe if (mktemp(tmpl) == NULL) 58088e61e85SRichard Lowe nomem(); 58188e61e85SRichard Lowe 58288e61e85SRichard Lowe (void) asprintf(&ret, "%s/%s%s", ctx->i_tmpdir, tmpl, 58388e61e85SRichard Lowe (ext != NULL) ? ext : ""); 58488e61e85SRichard Lowe 58588e61e85SRichard Lowe if (ret == NULL) 58688e61e85SRichard Lowe nomem(); 58788e61e85SRichard Lowe 58888e61e85SRichard Lowe return (ret); 58988e61e85SRichard Lowe } 59088e61e85SRichard Lowe 59188e61e85SRichard Lowe static boolean_t 59288e61e85SRichard Lowe is_source_file(const char *path) 59388e61e85SRichard Lowe { 59488e61e85SRichard Lowe char *ext = strrchr(path, '.'); 59588e61e85SRichard Lowe 596406d4d29SJohn Levon if ((ext == NULL) || (*(ext + 1) == '\0')) 59788e61e85SRichard Lowe return (B_FALSE); 59888e61e85SRichard Lowe 59988e61e85SRichard Lowe ext += 1; 60088e61e85SRichard Lowe 60188e61e85SRichard Lowe if ((strcasecmp(ext, "c") == 0) || 60288e61e85SRichard Lowe (strcmp(ext, "cc") == 0) || 60388e61e85SRichard Lowe (strcmp(ext, "i") == 0) || 60488e61e85SRichard Lowe (strcasecmp(ext, "s") == 0) || 60588e61e85SRichard Lowe (strcmp(ext, "cpp") == 0)) { 60688e61e85SRichard Lowe return (B_TRUE); 60788e61e85SRichard Lowe } 60888e61e85SRichard Lowe 60988e61e85SRichard Lowe return (B_FALSE); 61088e61e85SRichard Lowe } 61188e61e85SRichard Lowe 61288e61e85SRichard Lowe 6137c478bd9Sstevel@tonic-gate static void 61480ab886dSwesolows do_gcc(cw_ictx_t *ctx) 6157c478bd9Sstevel@tonic-gate { 6167c478bd9Sstevel@tonic-gate int c; 617e1bf37b1SRichard Lowe int nolibc = 0; 61880ab886dSwesolows int in_output = 0, seen_o = 0, c_files = 0; 61980ab886dSwesolows cw_op_t op = CW_O_LINK; 6207c478bd9Sstevel@tonic-gate char *model = NULL; 621aa9ef484SJohn Levon char *nameflag; 62288e61e85SRichard Lowe int mflag = 0; 6237c478bd9Sstevel@tonic-gate 6241912d2c4Swesolows if (ctx->i_flags & CW_F_PROG) { 6251912d2c4Swesolows newae(ctx->i_ae, "--version"); 6261912d2c4Swesolows return; 6271912d2c4Swesolows } 6281912d2c4Swesolows 62980ab886dSwesolows newae(ctx->i_ae, "-fident"); 63080ab886dSwesolows newae(ctx->i_ae, "-finline"); 63180ab886dSwesolows newae(ctx->i_ae, "-fno-inline-functions"); 63280ab886dSwesolows newae(ctx->i_ae, "-fno-builtin"); 63380ab886dSwesolows newae(ctx->i_ae, "-fno-asm"); 6347014882cSRichard Lowe newae(ctx->i_ae, "-fdiagnostics-show-option"); 63580ab886dSwesolows newae(ctx->i_ae, "-nodefaultlibs"); 6367c478bd9Sstevel@tonic-gate 63702e56f3fSwesolows #if defined(__sparc) 63802e56f3fSwesolows /* 63902e56f3fSwesolows * The SPARC ldd and std instructions require 8-byte alignment of 64002e56f3fSwesolows * their address operand. gcc correctly uses them only when the 64102e56f3fSwesolows * ABI requires 8-byte alignment; unfortunately we have a number of 64202e56f3fSwesolows * pieces of buggy code that doesn't conform to the ABI. This 64302e56f3fSwesolows * flag makes gcc work more like Studio with -xmemalign=4. 64402e56f3fSwesolows */ 64580ab886dSwesolows newae(ctx->i_ae, "-mno-integer-ldd-std"); 64602e56f3fSwesolows #endif 64702e56f3fSwesolows 6487c478bd9Sstevel@tonic-gate /* 6497c478bd9Sstevel@tonic-gate * This is needed because 'u' is defined 6507c478bd9Sstevel@tonic-gate * under a conditional on 'sun'. Should 6517c478bd9Sstevel@tonic-gate * probably just remove the conditional, 6527c478bd9Sstevel@tonic-gate * or make it be dependent on '__sun'. 6537c478bd9Sstevel@tonic-gate * 6547c478bd9Sstevel@tonic-gate * -Dunix is also missing in enhanced ANSI mode 6557c478bd9Sstevel@tonic-gate */ 65680ab886dSwesolows newae(ctx->i_ae, "-D__sun"); 6577c478bd9Sstevel@tonic-gate 658aa9ef484SJohn Levon if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1) 659aa9ef484SJohn Levon nomem(); 660aa9ef484SJohn Levon 6617c478bd9Sstevel@tonic-gate /* 6627c478bd9Sstevel@tonic-gate * Walk the argument list, translating as we go .. 6637c478bd9Sstevel@tonic-gate */ 66480ab886dSwesolows while (--ctx->i_oldargc > 0) { 66580ab886dSwesolows char *arg = *++ctx->i_oldargv; 6667c478bd9Sstevel@tonic-gate size_t arglen = strlen(arg); 6677c478bd9Sstevel@tonic-gate 66880ab886dSwesolows if (*arg == '-') { 6697c478bd9Sstevel@tonic-gate arglen--; 67080ab886dSwesolows } else { 6717c478bd9Sstevel@tonic-gate /* 6727c478bd9Sstevel@tonic-gate * Discard inline files that gcc doesn't grok 6737c478bd9Sstevel@tonic-gate */ 67480ab886dSwesolows if (!in_output && arglen > 3 && 6757c478bd9Sstevel@tonic-gate strcmp(arg + arglen - 3, ".il") == 0) 6767c478bd9Sstevel@tonic-gate continue; 6777c478bd9Sstevel@tonic-gate 67888e61e85SRichard Lowe if (!in_output && is_source_file(arg)) 67980ab886dSwesolows c_files++; 68080ab886dSwesolows 6817c478bd9Sstevel@tonic-gate /* 68280ab886dSwesolows * Otherwise, filenames and partial arguments 68380ab886dSwesolows * are passed through for gcc to chew on. However, 68480ab886dSwesolows * output is always discarded for the secondary 68580ab886dSwesolows * compiler. 6867c478bd9Sstevel@tonic-gate */ 68788e61e85SRichard Lowe if ((ctx->i_flags & CW_F_SHADOW) && in_output) { 68888e61e85SRichard Lowe newae(ctx->i_ae, discard_file_name(ctx, arg)); 68988e61e85SRichard Lowe } else { 69080ab886dSwesolows newae(ctx->i_ae, arg); 69188e61e85SRichard Lowe } 69280ab886dSwesolows in_output = 0; 6937c478bd9Sstevel@tonic-gate continue; 6947c478bd9Sstevel@tonic-gate } 6957c478bd9Sstevel@tonic-gate 69680ab886dSwesolows if (ctx->i_flags & CW_F_CXX) { 697aa9ef484SJohn Levon if (strncmp(arg, "-_g++=", 6) == 0) { 698aa9ef484SJohn Levon newae(ctx->i_ae, strchr(arg, '=') + 1); 699aa9ef484SJohn Levon continue; 700aa9ef484SJohn Levon } 7017c478bd9Sstevel@tonic-gate if (strncmp(arg, "-compat=", 8) == 0) { 7027c478bd9Sstevel@tonic-gate /* discard -compat=4 and -compat=5 */ 7037c478bd9Sstevel@tonic-gate continue; 7047c478bd9Sstevel@tonic-gate } 7057c478bd9Sstevel@tonic-gate if (strcmp(arg, "-Qoption") == 0) { 7067c478bd9Sstevel@tonic-gate /* discard -Qoption and its two arguments */ 70780ab886dSwesolows if (ctx->i_oldargc < 3) 7087c478bd9Sstevel@tonic-gate error(arg); 70980ab886dSwesolows ctx->i_oldargc -= 2; 71080ab886dSwesolows ctx->i_oldargv += 2; 7117c478bd9Sstevel@tonic-gate continue; 7127c478bd9Sstevel@tonic-gate } 7137c478bd9Sstevel@tonic-gate if (strcmp(arg, "-xwe") == 0) { 7147c478bd9Sstevel@tonic-gate /* turn warnings into errors */ 71580ab886dSwesolows newae(ctx->i_ae, "-Werror"); 7167c478bd9Sstevel@tonic-gate continue; 7177c478bd9Sstevel@tonic-gate } 7187c478bd9Sstevel@tonic-gate if (strcmp(arg, "-norunpath") == 0) { 7197c478bd9Sstevel@tonic-gate /* gcc has no corresponding option */ 7207c478bd9Sstevel@tonic-gate continue; 7217c478bd9Sstevel@tonic-gate } 7227c478bd9Sstevel@tonic-gate if (strcmp(arg, "-nolib") == 0) { 7237c478bd9Sstevel@tonic-gate /* -nodefaultlibs is on by default */ 7247c478bd9Sstevel@tonic-gate nolibc = 1; 7257c478bd9Sstevel@tonic-gate continue; 7267c478bd9Sstevel@tonic-gate } 7277c478bd9Sstevel@tonic-gate #if defined(__sparc) 7287c478bd9Sstevel@tonic-gate if (strcmp(arg, "-cg92") == 0) { 729e521259dSpetede mflag |= xlate_xtb(ctx->i_ae, "v8"); 73080ab886dSwesolows xlate(ctx->i_ae, "super", xchip_tbl); 7317c478bd9Sstevel@tonic-gate continue; 7327c478bd9Sstevel@tonic-gate } 7337c478bd9Sstevel@tonic-gate #endif /* __sparc */ 7347c478bd9Sstevel@tonic-gate } 7357c478bd9Sstevel@tonic-gate 7367c478bd9Sstevel@tonic-gate switch ((c = arg[1])) { 7377c478bd9Sstevel@tonic-gate case '_': 738aa9ef484SJohn Levon if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) || 739aa9ef484SJohn Levon (strncmp(arg, "-_gcc=", 6) == 0) || 740aa9ef484SJohn Levon (strncmp(arg, "-_gnu=", 6) == 0)) { 741aa9ef484SJohn Levon newae(ctx->i_ae, strchr(arg, '=') + 1); 742aa9ef484SJohn Levon } 7437c478bd9Sstevel@tonic-gate break; 7447c478bd9Sstevel@tonic-gate case '#': 7457c478bd9Sstevel@tonic-gate if (arglen == 1) { 74680ab886dSwesolows newae(ctx->i_ae, "-v"); 7477c478bd9Sstevel@tonic-gate break; 7487c478bd9Sstevel@tonic-gate } 7497c478bd9Sstevel@tonic-gate error(arg); 7507c478bd9Sstevel@tonic-gate break; 751e1bf37b1SRichard Lowe case 'f': 752e1bf37b1SRichard Lowe if ((strcmp(arg, "-fpic") == 0) || 753e1bf37b1SRichard Lowe (strcmp(arg, "-fPIC") == 0)) { 754e1bf37b1SRichard Lowe newae(ctx->i_ae, arg); 755e1bf37b1SRichard Lowe break; 756e1bf37b1SRichard Lowe } 757e1bf37b1SRichard Lowe error(arg); 758e1bf37b1SRichard Lowe break; 7597c478bd9Sstevel@tonic-gate case 'E': 7607c478bd9Sstevel@tonic-gate if (arglen == 1) { 76180ab886dSwesolows newae(ctx->i_ae, "-xc"); 76280ab886dSwesolows newae(ctx->i_ae, arg); 76380ab886dSwesolows op = CW_O_PREPROCESS; 7647c478bd9Sstevel@tonic-gate nolibc = 1; 7657c478bd9Sstevel@tonic-gate break; 7667c478bd9Sstevel@tonic-gate } 7677c478bd9Sstevel@tonic-gate error(arg); 7687c478bd9Sstevel@tonic-gate break; 7697c478bd9Sstevel@tonic-gate case 'c': 7707c478bd9Sstevel@tonic-gate case 'S': 77180ab886dSwesolows if (arglen == 1) { 77280ab886dSwesolows op = CW_O_COMPILE; 7737c478bd9Sstevel@tonic-gate nolibc = 1; 77480ab886dSwesolows } 7757c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 7767c478bd9Sstevel@tonic-gate case 'C': 7777c478bd9Sstevel@tonic-gate case 'H': 7787c478bd9Sstevel@tonic-gate case 'p': 7797c478bd9Sstevel@tonic-gate if (arglen == 1) { 78080ab886dSwesolows newae(ctx->i_ae, arg); 7817c478bd9Sstevel@tonic-gate break; 7827c478bd9Sstevel@tonic-gate } 7837c478bd9Sstevel@tonic-gate error(arg); 7847c478bd9Sstevel@tonic-gate break; 7857c478bd9Sstevel@tonic-gate case 'A': 786299e09deSRichard Lowe case 'g': 7877c478bd9Sstevel@tonic-gate case 'h': 7887c478bd9Sstevel@tonic-gate case 'I': 7897c478bd9Sstevel@tonic-gate case 'i': 7907c478bd9Sstevel@tonic-gate case 'L': 7917c478bd9Sstevel@tonic-gate case 'l': 7927c478bd9Sstevel@tonic-gate case 'R': 7937c478bd9Sstevel@tonic-gate case 'U': 7947c478bd9Sstevel@tonic-gate case 'u': 7957c478bd9Sstevel@tonic-gate case 'w': 79680ab886dSwesolows newae(ctx->i_ae, arg); 79780ab886dSwesolows break; 79880ab886dSwesolows case 'o': 79980ab886dSwesolows seen_o = 1; 80080ab886dSwesolows if (arglen == 1) { 80180ab886dSwesolows in_output = 1; 80280ab886dSwesolows newae(ctx->i_ae, arg); 80380ab886dSwesolows } else if (ctx->i_flags & CW_F_SHADOW) { 80480ab886dSwesolows newae(ctx->i_ae, "-o"); 80588e61e85SRichard Lowe newae(ctx->i_ae, discard_file_name(ctx, arg)); 80680ab886dSwesolows } else { 80780ab886dSwesolows newae(ctx->i_ae, arg); 80880ab886dSwesolows } 8097c478bd9Sstevel@tonic-gate break; 8107c478bd9Sstevel@tonic-gate case 'D': 81180ab886dSwesolows newae(ctx->i_ae, arg); 8127c478bd9Sstevel@tonic-gate /* 8137c478bd9Sstevel@tonic-gate * XXX Clearly a hack ... do we need _KADB too? 8147c478bd9Sstevel@tonic-gate */ 8157c478bd9Sstevel@tonic-gate if (strcmp(arg, "-D_KERNEL") == 0 || 8167c478bd9Sstevel@tonic-gate strcmp(arg, "-D_BOOT") == 0) 81780ab886dSwesolows newae(ctx->i_ae, "-ffreestanding"); 8187c478bd9Sstevel@tonic-gate break; 8197c478bd9Sstevel@tonic-gate case 'd': 8207c478bd9Sstevel@tonic-gate if (strcmp(arg, "-dalign") == 0) { 8217c478bd9Sstevel@tonic-gate /* 8227c478bd9Sstevel@tonic-gate * -dalign forces alignment in some cases; 8237c478bd9Sstevel@tonic-gate * gcc does not need any flag to do this. 8247c478bd9Sstevel@tonic-gate */ 8257c478bd9Sstevel@tonic-gate break; 8267c478bd9Sstevel@tonic-gate } 8277c478bd9Sstevel@tonic-gate error(arg); 8287c478bd9Sstevel@tonic-gate break; 8297c478bd9Sstevel@tonic-gate case 'e': 8307c478bd9Sstevel@tonic-gate if (strcmp(arg, 8317c478bd9Sstevel@tonic-gate "-erroff=E_EMPTY_TRANSLATION_UNIT") == 0) { 8327c478bd9Sstevel@tonic-gate /* 8337c478bd9Sstevel@tonic-gate * Accept but ignore this -- gcc doesn't 8347c478bd9Sstevel@tonic-gate * seem to complain about empty translation 8357c478bd9Sstevel@tonic-gate * units 8367c478bd9Sstevel@tonic-gate */ 8377c478bd9Sstevel@tonic-gate break; 8387c478bd9Sstevel@tonic-gate } 8397c478bd9Sstevel@tonic-gate /* XX64 -- ignore all -erroff= options, for now */ 8407c478bd9Sstevel@tonic-gate if (strncmp(arg, "-erroff=", 8) == 0) 8417c478bd9Sstevel@tonic-gate break; 8427c478bd9Sstevel@tonic-gate if (strcmp(arg, "-errtags=yes") == 0) { 84380ab886dSwesolows warnings(ctx->i_ae); 8447c478bd9Sstevel@tonic-gate break; 8457c478bd9Sstevel@tonic-gate } 8467c478bd9Sstevel@tonic-gate if (strcmp(arg, "-errwarn=%all") == 0) { 84780ab886dSwesolows newae(ctx->i_ae, "-Werror"); 8487c478bd9Sstevel@tonic-gate break; 8497c478bd9Sstevel@tonic-gate } 8507c478bd9Sstevel@tonic-gate error(arg); 8517c478bd9Sstevel@tonic-gate break; 8527c478bd9Sstevel@tonic-gate case 'k': 8537c478bd9Sstevel@tonic-gate if (strcmp(arg, "-keeptmp") == 0) { 85480ab886dSwesolows newae(ctx->i_ae, "-save-temps"); 8557c478bd9Sstevel@tonic-gate break; 8567c478bd9Sstevel@tonic-gate } 8577c478bd9Sstevel@tonic-gate error(arg); 8587c478bd9Sstevel@tonic-gate break; 8597c478bd9Sstevel@tonic-gate case 'm': 8607c478bd9Sstevel@tonic-gate if (strcmp(arg, "-mt") == 0) { 86180ab886dSwesolows newae(ctx->i_ae, "-D_REENTRANT"); 8627c478bd9Sstevel@tonic-gate break; 8637c478bd9Sstevel@tonic-gate } 864e521259dSpetede if (strcmp(arg, "-m64") == 0) { 865e521259dSpetede newae(ctx->i_ae, "-m64"); 866e521259dSpetede #if defined(__x86) 867e521259dSpetede newae(ctx->i_ae, "-mtune=opteron"); 868e521259dSpetede #endif 869e521259dSpetede mflag |= M64; 870e521259dSpetede break; 871e521259dSpetede } 872e521259dSpetede if (strcmp(arg, "-m32") == 0) { 873e521259dSpetede newae(ctx->i_ae, "-m32"); 874e521259dSpetede mflag |= M32; 875e521259dSpetede break; 876e521259dSpetede } 8777c478bd9Sstevel@tonic-gate error(arg); 8787c478bd9Sstevel@tonic-gate break; 8797c478bd9Sstevel@tonic-gate case 'O': 8807c478bd9Sstevel@tonic-gate if (arglen == 1) { 88180ab886dSwesolows newae(ctx->i_ae, "-O"); 8827c478bd9Sstevel@tonic-gate break; 8837c478bd9Sstevel@tonic-gate } 8847c478bd9Sstevel@tonic-gate error(arg); 8857c478bd9Sstevel@tonic-gate break; 8867c478bd9Sstevel@tonic-gate case 'P': 8877c478bd9Sstevel@tonic-gate /* 8887c478bd9Sstevel@tonic-gate * We could do '-E -o filename.i', but that's hard, 8897c478bd9Sstevel@tonic-gate * and we don't need it for the case that's triggering 8907c478bd9Sstevel@tonic-gate * this addition. We'll require the user to specify 8917c478bd9Sstevel@tonic-gate * -o in the Makefile. If they don't they'll find out 8927c478bd9Sstevel@tonic-gate * in a hurry. 8937c478bd9Sstevel@tonic-gate */ 89480ab886dSwesolows newae(ctx->i_ae, "-E"); 89580ab886dSwesolows op = CW_O_PREPROCESS; 8967c478bd9Sstevel@tonic-gate nolibc = 1; 8977c478bd9Sstevel@tonic-gate break; 8987c478bd9Sstevel@tonic-gate case 's': 899*85f4cb87SRichard Lowe if (strcmp(arg, "-shared") == 0) { 900*85f4cb87SRichard Lowe newae(ctx->i_ae, "-shared"); 901*85f4cb87SRichard Lowe nolibc = 1; 902159cf8a6Swesolows break; 903159cf8a6Swesolows } 904159cf8a6Swesolows error(arg); 905159cf8a6Swesolows break; 906*85f4cb87SRichard Lowe 9077c478bd9Sstevel@tonic-gate case 'V': 9087c478bd9Sstevel@tonic-gate if (arglen == 1) { 90980ab886dSwesolows ctx->i_flags &= ~CW_F_ECHO; 91080ab886dSwesolows newae(ctx->i_ae, "--version"); 9117c478bd9Sstevel@tonic-gate break; 9127c478bd9Sstevel@tonic-gate } 9137c478bd9Sstevel@tonic-gate error(arg); 9147c478bd9Sstevel@tonic-gate break; 9157c478bd9Sstevel@tonic-gate case 'v': 9167c478bd9Sstevel@tonic-gate if (arglen == 1) { 91780ab886dSwesolows warnings(ctx->i_ae); 9187c478bd9Sstevel@tonic-gate break; 9197c478bd9Sstevel@tonic-gate } 9207c478bd9Sstevel@tonic-gate error(arg); 9217c478bd9Sstevel@tonic-gate break; 9227c478bd9Sstevel@tonic-gate case 'W': 9237c478bd9Sstevel@tonic-gate if (strncmp(arg, "-Wp,-xc99", 9) == 0) { 9247c478bd9Sstevel@tonic-gate /* 9257c478bd9Sstevel@tonic-gate * gcc's preprocessor will accept c99 9267c478bd9Sstevel@tonic-gate * regardless, so accept and ignore. 9277c478bd9Sstevel@tonic-gate */ 9287c478bd9Sstevel@tonic-gate break; 9297c478bd9Sstevel@tonic-gate } 9307c478bd9Sstevel@tonic-gate if (strncmp(arg, "-Wa,", 4) == 0 || 9317c478bd9Sstevel@tonic-gate strncmp(arg, "-Wp,", 4) == 0 || 9327c478bd9Sstevel@tonic-gate strncmp(arg, "-Wl,", 4) == 0) { 93380ab886dSwesolows newae(ctx->i_ae, arg); 9347c478bd9Sstevel@tonic-gate break; 9357c478bd9Sstevel@tonic-gate } 9364afe1ab8Sesaxe if (strcmp(arg, "-W0,-noglobal") == 0 || 9374afe1ab8Sesaxe strcmp(arg, "-W0,-xglobalstatic") == 0) { 9387c478bd9Sstevel@tonic-gate /* 9397c478bd9Sstevel@tonic-gate * gcc doesn't prefix local symbols 9407c478bd9Sstevel@tonic-gate * in debug mode, so this is not needed. 9417c478bd9Sstevel@tonic-gate */ 9427c478bd9Sstevel@tonic-gate break; 9437c478bd9Sstevel@tonic-gate } 9447c478bd9Sstevel@tonic-gate if (strcmp(arg, "-W0,-Lt") == 0) { 9457c478bd9Sstevel@tonic-gate /* 9467c478bd9Sstevel@tonic-gate * Generate tests at the top of loops. 9477c478bd9Sstevel@tonic-gate * There is no direct gcc equivalent, ignore. 9487c478bd9Sstevel@tonic-gate */ 9497c478bd9Sstevel@tonic-gate break; 9507c478bd9Sstevel@tonic-gate } 9518c1a3716Ssherrym if (strcmp(arg, "-W0,-xdbggen=no%usedonly") == 0) { 95280ab886dSwesolows newae(ctx->i_ae, 95380ab886dSwesolows "-fno-eliminate-unused-debug-symbols"); 95480ab886dSwesolows newae(ctx->i_ae, 95580ab886dSwesolows "-fno-eliminate-unused-debug-types"); 9568c1a3716Ssherrym break; 9578c1a3716Ssherrym } 9589dd82889Spetede if (strcmp(arg, "-W2,-xwrap_int") == 0) { 9599dd82889Spetede /* 9609dd82889Spetede * Use the legacy behaviour (pre-SS11) 9619dd82889Spetede * for integer wrapping. 9629dd82889Spetede * gcc does not need this. 9639dd82889Spetede */ 9649dd82889Spetede break; 9659dd82889Spetede } 9667c478bd9Sstevel@tonic-gate if (strcmp(arg, "-Wd,-xsafe=unboundsym") == 0) { 9677c478bd9Sstevel@tonic-gate /* 9687c478bd9Sstevel@tonic-gate * Prevents optimizing away checks for 9697c478bd9Sstevel@tonic-gate * unbound weak symbol addresses. gcc does 9707c478bd9Sstevel@tonic-gate * not do this, so it's not needed. 9717c478bd9Sstevel@tonic-gate */ 9727c478bd9Sstevel@tonic-gate break; 9737c478bd9Sstevel@tonic-gate } 9747c478bd9Sstevel@tonic-gate if (strncmp(arg, "-Wc,-xcode=", 11) == 0) { 97580ab886dSwesolows xlate(ctx->i_ae, arg + 11, xcode_tbl); 9767c478bd9Sstevel@tonic-gate break; 9777c478bd9Sstevel@tonic-gate } 9787c478bd9Sstevel@tonic-gate if (strncmp(arg, "-Wc,-Qiselect", 13) == 0) { 9797c478bd9Sstevel@tonic-gate /* 9807c478bd9Sstevel@tonic-gate * Prevents insertion of register symbols. 9817c478bd9Sstevel@tonic-gate * gcc doesn't do this, so ignore it. 9827c478bd9Sstevel@tonic-gate */ 9837c478bd9Sstevel@tonic-gate break; 9847c478bd9Sstevel@tonic-gate } 9859dd82889Spetede if (strcmp(arg, "-Wc,-Qassembler-ounrefsym=0") == 0) { 9869dd82889Spetede /* 9879dd82889Spetede * Prevents optimizing away of static variables. 9889dd82889Spetede * gcc does not do this, so it's not needed. 9899dd82889Spetede */ 9909dd82889Spetede break; 9919dd82889Spetede } 9927c478bd9Sstevel@tonic-gate #if defined(__x86) 993d430274bSsherrym if (strcmp(arg, "-Wu,-save_args") == 0) { 99480ab886dSwesolows newae(ctx->i_ae, "-msave-args"); 995d430274bSsherrym break; 996d430274bSsherrym } 9977c478bd9Sstevel@tonic-gate #endif /* __x86 */ 9987c478bd9Sstevel@tonic-gate error(arg); 9997c478bd9Sstevel@tonic-gate break; 10007c478bd9Sstevel@tonic-gate case 'X': 10017c478bd9Sstevel@tonic-gate if (strcmp(arg, "-Xa") == 0 || 10027c478bd9Sstevel@tonic-gate strcmp(arg, "-Xt") == 0) { 10037c478bd9Sstevel@tonic-gate break; 10047c478bd9Sstevel@tonic-gate } 10057c478bd9Sstevel@tonic-gate if (strcmp(arg, "-Xs") == 0) { 100680ab886dSwesolows Xsmode(ctx->i_ae); 10077c478bd9Sstevel@tonic-gate break; 10087c478bd9Sstevel@tonic-gate } 10097c478bd9Sstevel@tonic-gate error(arg); 10107c478bd9Sstevel@tonic-gate break; 10117c478bd9Sstevel@tonic-gate case 'x': 10127c478bd9Sstevel@tonic-gate if (arglen == 1) 10137c478bd9Sstevel@tonic-gate error(arg); 10147c478bd9Sstevel@tonic-gate switch (arg[2]) { 10157c478bd9Sstevel@tonic-gate case 'a': 10167c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xarch=", 7) == 0) { 1017e521259dSpetede mflag |= xlate_xtb(ctx->i_ae, arg + 7); 10187c478bd9Sstevel@tonic-gate break; 10197c478bd9Sstevel@tonic-gate } 10207c478bd9Sstevel@tonic-gate error(arg); 10217c478bd9Sstevel@tonic-gate break; 102202e56f3fSwesolows case 'b': 102302e56f3fSwesolows if (strncmp(arg, "-xbuiltin=", 10) == 0) { 102402e56f3fSwesolows if (strcmp(arg + 10, "%all")) 102580ab886dSwesolows newae(ctx->i_ae, "-fbuiltin"); 102602e56f3fSwesolows break; 102702e56f3fSwesolows } 102802e56f3fSwesolows error(arg); 102902e56f3fSwesolows break; 10307c478bd9Sstevel@tonic-gate case 'C': 10317c478bd9Sstevel@tonic-gate /* Accept C++ style comments -- ignore */ 10327c478bd9Sstevel@tonic-gate if (strcmp(arg, "-xCC") == 0) 10337c478bd9Sstevel@tonic-gate break; 10347c478bd9Sstevel@tonic-gate error(arg); 10357c478bd9Sstevel@tonic-gate break; 10367c478bd9Sstevel@tonic-gate case 'c': 10377c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xc99=%all", 10) == 0) { 103880ab886dSwesolows newae(ctx->i_ae, "-std=gnu99"); 10397c478bd9Sstevel@tonic-gate break; 10407c478bd9Sstevel@tonic-gate } 10417c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xc99=%none", 11) == 0) { 104280ab886dSwesolows newae(ctx->i_ae, "-std=gnu89"); 10437c478bd9Sstevel@tonic-gate break; 10447c478bd9Sstevel@tonic-gate } 10457c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xchip=", 7) == 0) { 104680ab886dSwesolows xlate(ctx->i_ae, arg + 7, xchip_tbl); 10477c478bd9Sstevel@tonic-gate break; 10487c478bd9Sstevel@tonic-gate } 10497c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xcode=", 7) == 0) { 105080ab886dSwesolows xlate(ctx->i_ae, arg + 7, xcode_tbl); 10517c478bd9Sstevel@tonic-gate break; 10527c478bd9Sstevel@tonic-gate } 1053159cf8a6Swesolows if (strncmp(arg, "-xcrossfile", 11) == 0) 1054159cf8a6Swesolows break; 10557c478bd9Sstevel@tonic-gate error(arg); 10567c478bd9Sstevel@tonic-gate break; 10577c478bd9Sstevel@tonic-gate case 'F': 1058f795e658Srie /* 1059f795e658Srie * Compile for mapfile reordering, or unused 1060f795e658Srie * section elimination, syntax can be -xF or 1061f795e658Srie * more complex, like -xF=%all -- ignore. 1062f795e658Srie */ 1063f795e658Srie if (strncmp(arg, "-xF", 3) == 0) 10647c478bd9Sstevel@tonic-gate break; 10657c478bd9Sstevel@tonic-gate error(arg); 10667c478bd9Sstevel@tonic-gate break; 10677c478bd9Sstevel@tonic-gate case 'i': 10687c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xinline", 8) == 0) 10697c478bd9Sstevel@tonic-gate /* No inlining; ignore */ 10707c478bd9Sstevel@tonic-gate break; 10717c478bd9Sstevel@tonic-gate if (strcmp(arg, "-xildon") == 0 || 10727c478bd9Sstevel@tonic-gate strcmp(arg, "-xildoff") == 0) 10737c478bd9Sstevel@tonic-gate /* No incremental linking; ignore */ 10747c478bd9Sstevel@tonic-gate break; 10757c478bd9Sstevel@tonic-gate error(arg); 10767c478bd9Sstevel@tonic-gate break; 107754836668Spetede #if defined(__x86) 107854836668Spetede case 'm': 107954836668Spetede if (strcmp(arg, "-xmodel=kernel") == 0) { 108054836668Spetede newae(ctx->i_ae, "-ffreestanding"); 108154836668Spetede newae(ctx->i_ae, "-mno-red-zone"); 108254836668Spetede model = "-mcmodel=kernel"; 108354836668Spetede nolibc = 1; 108454836668Spetede break; 108554836668Spetede } 108654836668Spetede error(arg); 108754836668Spetede break; 108854836668Spetede #endif /* __x86 */ 10897c478bd9Sstevel@tonic-gate case 'O': 10907c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xO", 3) == 0) { 10917c478bd9Sstevel@tonic-gate size_t len = strlen(arg); 1092538ff303SToomas Soome char *s = NULL; 10937c478bd9Sstevel@tonic-gate int c = *(arg + 3); 10947c478bd9Sstevel@tonic-gate int level; 10957c478bd9Sstevel@tonic-gate 10967c478bd9Sstevel@tonic-gate if (len != 4 || !isdigit(c)) 10977c478bd9Sstevel@tonic-gate error(arg); 10987c478bd9Sstevel@tonic-gate 10997c478bd9Sstevel@tonic-gate level = atoi(arg + 3); 11007c478bd9Sstevel@tonic-gate if (level > 5) 11017c478bd9Sstevel@tonic-gate error(arg); 11027c478bd9Sstevel@tonic-gate if (level >= 2) { 11037c478bd9Sstevel@tonic-gate /* 11047c478bd9Sstevel@tonic-gate * For gcc-3.4.x at -O2 we 11057c478bd9Sstevel@tonic-gate * need to disable optimizations 11067c478bd9Sstevel@tonic-gate * that break ON. 11077c478bd9Sstevel@tonic-gate */ 110880ab886dSwesolows optim_disable(ctx->i_ae, level); 11097c478bd9Sstevel@tonic-gate /* 11107c478bd9Sstevel@tonic-gate * limit -xO3 to -O2 as well. 11117c478bd9Sstevel@tonic-gate */ 11127c478bd9Sstevel@tonic-gate level = 2; 11137c478bd9Sstevel@tonic-gate } 1114538ff303SToomas Soome if (asprintf(&s, "-O%d", level) == -1) 1115538ff303SToomas Soome nomem(); 111680ab886dSwesolows newae(ctx->i_ae, s); 11177c478bd9Sstevel@tonic-gate free(s); 11187c478bd9Sstevel@tonic-gate break; 11197c478bd9Sstevel@tonic-gate } 11207c478bd9Sstevel@tonic-gate error(arg); 11217c478bd9Sstevel@tonic-gate break; 11227c478bd9Sstevel@tonic-gate case 'r': 11237c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xregs=", 7) == 0) { 112480ab886dSwesolows xlate(ctx->i_ae, arg + 7, xregs_tbl); 11257c478bd9Sstevel@tonic-gate break; 11267c478bd9Sstevel@tonic-gate } 11277c478bd9Sstevel@tonic-gate error(arg); 11287c478bd9Sstevel@tonic-gate break; 11297c478bd9Sstevel@tonic-gate case 's': 11307c478bd9Sstevel@tonic-gate if (strcmp(arg, "-xs") == 0 || 11317c478bd9Sstevel@tonic-gate strcmp(arg, "-xspace") == 0 || 11327c478bd9Sstevel@tonic-gate strcmp(arg, "-xstrconst") == 0) 11337c478bd9Sstevel@tonic-gate break; 11347c478bd9Sstevel@tonic-gate error(arg); 11357c478bd9Sstevel@tonic-gate break; 11367c478bd9Sstevel@tonic-gate case 't': 11377c478bd9Sstevel@tonic-gate if (strncmp(arg, "-xtarget=", 9) == 0) { 113880ab886dSwesolows xlate(ctx->i_ae, arg + 9, xtarget_tbl); 11397c478bd9Sstevel@tonic-gate break; 11407c478bd9Sstevel@tonic-gate } 11417c478bd9Sstevel@tonic-gate error(arg); 11427c478bd9Sstevel@tonic-gate break; 11437c478bd9Sstevel@tonic-gate case 'e': 11447c478bd9Sstevel@tonic-gate case 'h': 11457c478bd9Sstevel@tonic-gate case 'l': 11467c478bd9Sstevel@tonic-gate default: 11477c478bd9Sstevel@tonic-gate error(arg); 11487c478bd9Sstevel@tonic-gate break; 11497c478bd9Sstevel@tonic-gate } 11507c478bd9Sstevel@tonic-gate break; 11517c478bd9Sstevel@tonic-gate case 'Y': 11527c478bd9Sstevel@tonic-gate if (arglen == 1) { 115380ab886dSwesolows if ((arg = *++ctx->i_oldargv) == NULL || 115480ab886dSwesolows *arg == '\0') 11557c478bd9Sstevel@tonic-gate error("-Y"); 115680ab886dSwesolows ctx->i_oldargc--; 11577c478bd9Sstevel@tonic-gate arglen = strlen(arg + 1); 11587c478bd9Sstevel@tonic-gate } else { 11597c478bd9Sstevel@tonic-gate arg += 2; 11607c478bd9Sstevel@tonic-gate } 11617c478bd9Sstevel@tonic-gate /* Just ignore -YS,... for now */ 11627c478bd9Sstevel@tonic-gate if (strncmp(arg, "S,", 2) == 0) 11637c478bd9Sstevel@tonic-gate break; 11647c478bd9Sstevel@tonic-gate if (strncmp(arg, "I,", 2) == 0) { 11657c478bd9Sstevel@tonic-gate char *s = strdup(arg); 11667c478bd9Sstevel@tonic-gate s[0] = '-'; 11677c478bd9Sstevel@tonic-gate s[1] = 'I'; 116880ab886dSwesolows newae(ctx->i_ae, "-nostdinc"); 116980ab886dSwesolows newae(ctx->i_ae, s); 11707c478bd9Sstevel@tonic-gate free(s); 11717c478bd9Sstevel@tonic-gate break; 11727c478bd9Sstevel@tonic-gate } 11737c478bd9Sstevel@tonic-gate error(arg); 11747c478bd9Sstevel@tonic-gate break; 11757c478bd9Sstevel@tonic-gate case 'Q': 11767c478bd9Sstevel@tonic-gate /* 11777c478bd9Sstevel@tonic-gate * We could map -Qy into -Wl,-Qy etc. 11787c478bd9Sstevel@tonic-gate */ 11797c478bd9Sstevel@tonic-gate default: 11807c478bd9Sstevel@tonic-gate error(arg); 11817c478bd9Sstevel@tonic-gate break; 11827c478bd9Sstevel@tonic-gate } 11837c478bd9Sstevel@tonic-gate } 11847c478bd9Sstevel@tonic-gate 1185aa9ef484SJohn Levon free(nameflag); 1186aa9ef484SJohn Levon 118788e61e85SRichard Lowe /* 118888e61e85SRichard Lowe * When compiling multiple source files in a single invocation some 118988e61e85SRichard Lowe * compilers output objects into the current directory with 119088e61e85SRichard Lowe * predictable and conventional names. 119188e61e85SRichard Lowe * 119288e61e85SRichard Lowe * We prevent any attempt to compile multiple files at once so that 119388e61e85SRichard Lowe * any such objects created by a shadow can't escape into a later 119488e61e85SRichard Lowe * link-edit. 119588e61e85SRichard Lowe */ 119688e61e85SRichard Lowe if (c_files > 1 && op != CW_O_PREPROCESS) { 1197aa9ef484SJohn Levon errx(2, "multiple source files are " 1198aa9ef484SJohn Levon "allowed only with -E or -P"); 119980ab886dSwesolows } 1200e521259dSpetede 1201e521259dSpetede /* 1202e521259dSpetede * Make sure that we do not have any unintended interactions between 1203e521259dSpetede * the xarch options passed in and the version of the Studio compiler 1204e521259dSpetede * used. 1205e521259dSpetede */ 1206e521259dSpetede if ((mflag & (SS11|SS12)) == (SS11|SS12)) { 1207aa9ef484SJohn Levon errx(2, 1208e521259dSpetede "Conflicting \"-xarch=\" flags (both Studio 11 and 12)\n"); 1209e521259dSpetede } 1210e521259dSpetede 1211e521259dSpetede switch (mflag) { 1212e521259dSpetede case 0: 1213e521259dSpetede /* FALLTHROUGH */ 1214e521259dSpetede case M32: 1215e521259dSpetede #if defined(__sparc) 1216e521259dSpetede /* 1217e521259dSpetede * Only -m32 is defined and so put in the missing xarch 1218e521259dSpetede * translation. 1219e521259dSpetede */ 1220e521259dSpetede newae(ctx->i_ae, "-mcpu=v8"); 1221e521259dSpetede newae(ctx->i_ae, "-mno-v8plus"); 1222e521259dSpetede #endif 1223e521259dSpetede break; 1224e521259dSpetede case M64: 1225e521259dSpetede #if defined(__sparc) 1226e521259dSpetede /* 1227e521259dSpetede * Only -m64 is defined and so put in the missing xarch 1228e521259dSpetede * translation. 1229e521259dSpetede */ 1230e521259dSpetede newae(ctx->i_ae, "-mcpu=v9"); 1231e521259dSpetede #endif 1232e521259dSpetede break; 1233e521259dSpetede case SS12: 1234e521259dSpetede #if defined(__sparc) 1235e521259dSpetede /* no -m32/-m64 flag used - this is an error for sparc builds */ 1236e521259dSpetede (void) fprintf(stderr, "No -m32/-m64 flag defined\n"); 1237e521259dSpetede exit(2); 1238e521259dSpetede #endif 1239e521259dSpetede break; 1240e521259dSpetede case SS11: 1241e521259dSpetede /* FALLTHROUGH */ 1242e521259dSpetede case (SS11|M32): 1243e521259dSpetede case (SS11|M64): 1244e521259dSpetede break; 1245e521259dSpetede case (SS12|M32): 1246e521259dSpetede #if defined(__sparc) 1247e521259dSpetede /* 1248e521259dSpetede * Need to add in further 32 bit options because with SS12 1249e521259dSpetede * the xarch=sparcvis option can be applied to 32 or 64 1250e521259dSpetede * bit, and so the translatation table (xtbl) cannot handle 1251e521259dSpetede * that. 1252e521259dSpetede */ 1253e521259dSpetede newae(ctx->i_ae, "-mv8plus"); 1254e521259dSpetede #endif 1255e521259dSpetede break; 1256e521259dSpetede case (SS12|M64): 1257e521259dSpetede break; 1258e521259dSpetede default: 1259e521259dSpetede (void) fprintf(stderr, 12607a6460b6Spetede "Incompatible -xarch= and/or -m32/-m64 options used.\n"); 1261e521259dSpetede exit(2); 1262e521259dSpetede } 1263aa9ef484SJohn Levon 126488e61e85SRichard Lowe if (ctx->i_flags & CW_F_SHADOW) { 126588e61e85SRichard Lowe if (op == CW_O_PREPROCESS) 126688e61e85SRichard Lowe exit(0); 126788e61e85SRichard Lowe else if (op == CW_O_LINK && c_files == 0) 126888e61e85SRichard Lowe exit(0); 126988e61e85SRichard Lowe } 127080ab886dSwesolows 1271e1bf37b1SRichard Lowe if (model != NULL) 127280ab886dSwesolows newae(ctx->i_ae, model); 12737c478bd9Sstevel@tonic-gate if (!nolibc) 127480ab886dSwesolows newae(ctx->i_ae, "-lc"); 127580ab886dSwesolows if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) { 127680ab886dSwesolows newae(ctx->i_ae, "-o"); 127788e61e85SRichard Lowe newae(ctx->i_ae, discard_file_name(ctx, NULL)); 127880ab886dSwesolows } 127980ab886dSwesolows } 128080ab886dSwesolows 12811f5207b7SJohn Levon static void 12821f5207b7SJohn Levon do_smatch(cw_ictx_t *ctx) 12831f5207b7SJohn Levon { 12841f5207b7SJohn Levon if (ctx->i_flags & CW_F_PROG) { 12851f5207b7SJohn Levon newae(ctx->i_ae, "--version"); 12861f5207b7SJohn Levon return; 12871f5207b7SJohn Levon } 12881f5207b7SJohn Levon 12891f5207b7SJohn Levon /* 12901f5207b7SJohn Levon * Some sources shouldn't run smatch at all. 12911f5207b7SJohn Levon */ 12921f5207b7SJohn Levon for (int i = 0; i < ctx->i_oldargc; i++) { 12931f5207b7SJohn Levon char *arg = ctx->i_oldargv[i]; 12941f5207b7SJohn Levon 12951f5207b7SJohn Levon if (strcmp(arg, "-_smatch=off") == 0) { 12961f5207b7SJohn Levon ctx->i_flags &= ~ (CW_F_EXEC | CW_F_ECHO); 12971f5207b7SJohn Levon return; 12981f5207b7SJohn Levon } 12991f5207b7SJohn Levon } 13001f5207b7SJohn Levon 13011f5207b7SJohn Levon /* 13021f5207b7SJohn Levon * smatch can handle gcc's options. 13031f5207b7SJohn Levon */ 13041f5207b7SJohn Levon do_gcc(ctx); 13051f5207b7SJohn Levon } 13061f5207b7SJohn Levon 130780ab886dSwesolows static void 130880ab886dSwesolows do_cc(cw_ictx_t *ctx) 130980ab886dSwesolows { 131088e61e85SRichard Lowe int in_output = 0, seen_o = 0, c_files = 0; 131180ab886dSwesolows cw_op_t op = CW_O_LINK; 1312aa9ef484SJohn Levon char *nameflag; 131380ab886dSwesolows 13141912d2c4Swesolows if (ctx->i_flags & CW_F_PROG) { 13151912d2c4Swesolows newae(ctx->i_ae, "-V"); 13161912d2c4Swesolows return; 13171912d2c4Swesolows } 13181912d2c4Swesolows 1319aa9ef484SJohn Levon if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1) 1320aa9ef484SJohn Levon nomem(); 1321aa9ef484SJohn Levon 132280ab886dSwesolows while (--ctx->i_oldargc > 0) { 132380ab886dSwesolows char *arg = *++ctx->i_oldargv; 132480ab886dSwesolows 1325aa9ef484SJohn Levon if (strncmp(arg, "-_CC=", 5) == 0) { 1326aa9ef484SJohn Levon newae(ctx->i_ae, strchr(arg, '=') + 1); 1327aa9ef484SJohn Levon continue; 1328aa9ef484SJohn Levon } 1329aa9ef484SJohn Levon 133080ab886dSwesolows if (*arg != '-') { 133188e61e85SRichard Lowe if (!in_output && is_source_file(arg)) 133288e61e85SRichard Lowe c_files++; 133388e61e85SRichard Lowe 133480ab886dSwesolows if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) { 133580ab886dSwesolows newae(ctx->i_ae, arg); 133680ab886dSwesolows } else { 133780ab886dSwesolows in_output = 0; 133888e61e85SRichard Lowe newae(ctx->i_ae, discard_file_name(ctx, arg)); 133980ab886dSwesolows } 134080ab886dSwesolows continue; 134180ab886dSwesolows } 134280ab886dSwesolows switch (*(arg + 1)) { 134380ab886dSwesolows case '_': 1344aa9ef484SJohn Levon if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) || 1345aa9ef484SJohn Levon (strncmp(arg, "-_cc=", 5) == 0) || 1346aa9ef484SJohn Levon (strncmp(arg, "-_sun=", 6) == 0)) { 1347aa9ef484SJohn Levon newae(ctx->i_ae, strchr(arg, '=') + 1); 134880ab886dSwesolows } 134980ab886dSwesolows break; 1350aa9ef484SJohn Levon 135180ab886dSwesolows case 'V': 135280ab886dSwesolows ctx->i_flags &= ~CW_F_ECHO; 135380ab886dSwesolows newae(ctx->i_ae, arg); 135480ab886dSwesolows break; 135580ab886dSwesolows case 'o': 135680ab886dSwesolows seen_o = 1; 135780ab886dSwesolows if (strlen(arg) == 2) { 135880ab886dSwesolows in_output = 1; 135980ab886dSwesolows newae(ctx->i_ae, arg); 136080ab886dSwesolows } else if (ctx->i_flags & CW_F_SHADOW) { 136180ab886dSwesolows newae(ctx->i_ae, "-o"); 136288e61e85SRichard Lowe newae(ctx->i_ae, discard_file_name(ctx, arg)); 136380ab886dSwesolows } else { 136480ab886dSwesolows newae(ctx->i_ae, arg); 136580ab886dSwesolows } 136680ab886dSwesolows break; 136780ab886dSwesolows case 'c': 136880ab886dSwesolows case 'S': 13690bb3415fSrie if (strlen(arg) == 2) 13700bb3415fSrie op = CW_O_COMPILE; 137180ab886dSwesolows newae(ctx->i_ae, arg); 137280ab886dSwesolows break; 137380ab886dSwesolows case 'E': 137480ab886dSwesolows case 'P': 13750bb3415fSrie if (strlen(arg) == 2) 13760bb3415fSrie op = CW_O_PREPROCESS; 137780ab886dSwesolows /*FALLTHROUGH*/ 137880ab886dSwesolows default: 137980ab886dSwesolows newae(ctx->i_ae, arg); 138080ab886dSwesolows } 138180ab886dSwesolows } 138280ab886dSwesolows 1383aa9ef484SJohn Levon free(nameflag); 1384aa9ef484SJohn Levon 138588e61e85SRichard Lowe /* See the comment on this same code in do_gcc() */ 138688e61e85SRichard Lowe if (c_files > 1 && op != CW_O_PREPROCESS) { 138788e61e85SRichard Lowe errx(2, "multiple source files are " 138888e61e85SRichard Lowe "allowed only with -E or -P"); 138988e61e85SRichard Lowe } 139088e61e85SRichard Lowe 139188e61e85SRichard Lowe if (ctx->i_flags & CW_F_SHADOW) { 139288e61e85SRichard Lowe if (op == CW_O_PREPROCESS) 139388e61e85SRichard Lowe exit(0); 139488e61e85SRichard Lowe else if (op == CW_O_LINK && c_files == 0) 139588e61e85SRichard Lowe exit(0); 139688e61e85SRichard Lowe } 139780ab886dSwesolows 139880ab886dSwesolows if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) { 139980ab886dSwesolows newae(ctx->i_ae, "-o"); 140088e61e85SRichard Lowe newae(ctx->i_ae, discard_file_name(ctx, NULL)); 140180ab886dSwesolows } 14027c478bd9Sstevel@tonic-gate } 14037c478bd9Sstevel@tonic-gate 14047c478bd9Sstevel@tonic-gate static void 140580ab886dSwesolows prepctx(cw_ictx_t *ctx) 14067c478bd9Sstevel@tonic-gate { 1407aa9ef484SJohn Levon newae(ctx->i_ae, ctx->i_compiler->c_path); 140880ab886dSwesolows 14091912d2c4Swesolows if (ctx->i_flags & CW_F_PROG) { 14101912d2c4Swesolows (void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ? 1411aa9ef484SJohn Levon "shadow" : "primary", ctx->i_compiler->c_path); 14121912d2c4Swesolows (void) fflush(stdout); 14131912d2c4Swesolows } 14141912d2c4Swesolows 141569b1fd3fSRichard Lowe /* 141669b1fd3fSRichard Lowe * If LD_ALTEXEC is already set, the expectation would be that that 141769b1fd3fSRichard Lowe * link-editor is run, as such we need to leave it the environment 141869b1fd3fSRichard Lowe * alone and let that happen. 141969b1fd3fSRichard Lowe */ 142069b1fd3fSRichard Lowe if ((ctx->i_linker != NULL) && (getenv("LD_ALTEXEC") == NULL)) 142169b1fd3fSRichard Lowe setenv("LD_ALTEXEC", ctx->i_linker, 1); 142269b1fd3fSRichard Lowe 142380ab886dSwesolows if (!(ctx->i_flags & CW_F_XLATE)) 142480ab886dSwesolows return; 142580ab886dSwesolows 1426aa9ef484SJohn Levon switch (ctx->i_compiler->c_style) { 1427aa9ef484SJohn Levon case SUN: 142880ab886dSwesolows do_cc(ctx); 142980ab886dSwesolows break; 1430aa9ef484SJohn Levon case GNU: 143180ab886dSwesolows do_gcc(ctx); 143280ab886dSwesolows break; 14331f5207b7SJohn Levon case SMATCH: 14341f5207b7SJohn Levon do_smatch(ctx); 14351f5207b7SJohn Levon break; 143680ab886dSwesolows } 143780ab886dSwesolows } 143880ab886dSwesolows 143980ab886dSwesolows static int 144080ab886dSwesolows invoke(cw_ictx_t *ctx) 144180ab886dSwesolows { 144280ab886dSwesolows char **newargv; 144380ab886dSwesolows int ac; 144480ab886dSwesolows struct ae *a; 144580ab886dSwesolows 144680ab886dSwesolows if ((newargv = calloc(sizeof (*newargv), ctx->i_ae->ael_argc + 1)) == 144780ab886dSwesolows NULL) 144880ab886dSwesolows nomem(); 144980ab886dSwesolows 145080ab886dSwesolows if (ctx->i_flags & CW_F_ECHO) 145180ab886dSwesolows (void) fprintf(stderr, "+ "); 145280ab886dSwesolows 145380ab886dSwesolows for (ac = 0, a = ctx->i_ae->ael_head; a; a = a->ae_next, ac++) { 145480ab886dSwesolows newargv[ac] = a->ae_arg; 145580ab886dSwesolows if (ctx->i_flags & CW_F_ECHO) 145680ab886dSwesolows (void) fprintf(stderr, "%s ", a->ae_arg); 145780ab886dSwesolows if (a == ctx->i_ae->ael_tail) 145880ab886dSwesolows break; 145980ab886dSwesolows } 146080ab886dSwesolows 146180ab886dSwesolows if (ctx->i_flags & CW_F_ECHO) { 146280ab886dSwesolows (void) fprintf(stderr, "\n"); 146380ab886dSwesolows (void) fflush(stderr); 146480ab886dSwesolows } 146580ab886dSwesolows 146680ab886dSwesolows if (!(ctx->i_flags & CW_F_EXEC)) 146780ab886dSwesolows return (0); 146880ab886dSwesolows 14697c478bd9Sstevel@tonic-gate /* 1470aa9ef484SJohn Levon * We must fix up the environment here so that the dependency files are 1471aa9ef484SJohn Levon * not trampled by the shadow compiler. Also take care of GCC 1472aa9ef484SJohn Levon * environment variables that will throw off gcc. This assumes a primary 1473aa9ef484SJohn Levon * gcc. 14747c478bd9Sstevel@tonic-gate */ 147580ab886dSwesolows if ((ctx->i_flags & CW_F_SHADOW) && 147680ab886dSwesolows (unsetenv("SUNPRO_DEPENDENCIES") != 0 || 1477aa9ef484SJohn Levon unsetenv("DEPENDENCIES_OUTPUT") != 0 || 1478aa9ef484SJohn Levon unsetenv("GCC_ROOT") != 0)) { 147980ab886dSwesolows (void) fprintf(stderr, "error: environment setup failed: %s\n", 148080ab886dSwesolows strerror(errno)); 148180ab886dSwesolows return (-1); 148280ab886dSwesolows } 14837c478bd9Sstevel@tonic-gate 148480ab886dSwesolows (void) execv(newargv[0], newargv); 1485aa9ef484SJohn Levon warn("couldn't run %s", newargv[0]); 14867c478bd9Sstevel@tonic-gate 148780ab886dSwesolows return (-1); 148880ab886dSwesolows } 148980ab886dSwesolows 149080ab886dSwesolows static int 149180ab886dSwesolows reap(cw_ictx_t *ctx) 149280ab886dSwesolows { 14931912d2c4Swesolows int status, ret = 0; 149480ab886dSwesolows char buf[1024]; 149580ab886dSwesolows struct stat s; 149680ab886dSwesolows 1497c3f177eaSPeter Dennis - Sustaining Engineer /* 1498c3f177eaSPeter Dennis - Sustaining Engineer * Only wait for one specific child. 1499c3f177eaSPeter Dennis - Sustaining Engineer */ 1500c3f177eaSPeter Dennis - Sustaining Engineer if (ctx->i_pid <= 0) 1501c3f177eaSPeter Dennis - Sustaining Engineer return (-1); 1502c3f177eaSPeter Dennis - Sustaining Engineer 150380ab886dSwesolows do { 1504c3f177eaSPeter Dennis - Sustaining Engineer if (waitpid(ctx->i_pid, &status, 0) < 0) { 1505aa9ef484SJohn Levon warn("cannot reap child"); 1506c3f177eaSPeter Dennis - Sustaining Engineer return (-1); 1507c3f177eaSPeter Dennis - Sustaining Engineer } 15081912d2c4Swesolows if (status != 0) { 15091912d2c4Swesolows if (WIFSIGNALED(status)) { 15101912d2c4Swesolows ret = -WTERMSIG(status); 151180ab886dSwesolows break; 15121912d2c4Swesolows } else if (WIFEXITED(status)) { 15131912d2c4Swesolows ret = WEXITSTATUS(status); 151480ab886dSwesolows break; 151580ab886dSwesolows } 15167c478bd9Sstevel@tonic-gate } 15171912d2c4Swesolows } while (!WIFEXITED(status) && !WIFSIGNALED(status)); 151880ab886dSwesolows 15191912d2c4Swesolows if (stat(ctx->i_stderr, &s) < 0) { 1520aa9ef484SJohn Levon warn("stat failed on child cleanup"); 152180ab886dSwesolows return (-1); 15227c478bd9Sstevel@tonic-gate } 152380ab886dSwesolows if (s.st_size != 0) { 15241912d2c4Swesolows FILE *f; 152580ab886dSwesolows 15261912d2c4Swesolows if ((f = fopen(ctx->i_stderr, "r")) != NULL) { 15271912d2c4Swesolows while (fgets(buf, sizeof (buf), f)) 15281912d2c4Swesolows (void) fprintf(stderr, "%s", buf); 15291912d2c4Swesolows (void) fflush(stderr); 15301912d2c4Swesolows (void) fclose(f); 15311912d2c4Swesolows } 153280ab886dSwesolows } 15331912d2c4Swesolows (void) unlink(ctx->i_stderr); 15341912d2c4Swesolows free(ctx->i_stderr); 15351912d2c4Swesolows 15361912d2c4Swesolows /* 15371912d2c4Swesolows * cc returns an error code when given -V; we want that to succeed. 15381912d2c4Swesolows */ 15391912d2c4Swesolows if (ctx->i_flags & CW_F_PROG) 15401912d2c4Swesolows return (0); 154180ab886dSwesolows 154280ab886dSwesolows return (ret); 154380ab886dSwesolows } 154480ab886dSwesolows 154580ab886dSwesolows static int 154680ab886dSwesolows exec_ctx(cw_ictx_t *ctx, int block) 154780ab886dSwesolows { 154888e61e85SRichard Lowe if ((ctx->i_stderr = tempnam(ctx->i_tmpdir, "cw")) == NULL) { 15491912d2c4Swesolows nomem(); 155080ab886dSwesolows return (-1); 155180ab886dSwesolows } 155280ab886dSwesolows 155380ab886dSwesolows if ((ctx->i_pid = fork()) == 0) { 15541912d2c4Swesolows int fd; 15551912d2c4Swesolows 155680ab886dSwesolows (void) fclose(stderr); 15571912d2c4Swesolows if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL, 15581912d2c4Swesolows 0666)) < 0) { 1559aa9ef484SJohn Levon err(1, "open failed for standard error"); 15601912d2c4Swesolows } 15611912d2c4Swesolows if (dup2(fd, 2) < 0) { 1562aa9ef484SJohn Levon err(1, "dup2 failed for standard error"); 156380ab886dSwesolows } 15641912d2c4Swesolows if (fd != 2) 15651912d2c4Swesolows (void) close(fd); 156680ab886dSwesolows if (freopen("/dev/fd/2", "w", stderr) == NULL) { 1567aa9ef484SJohn Levon err(1, "freopen failed for /dev/fd/2"); 156880ab886dSwesolows } 1569aa9ef484SJohn Levon 157080ab886dSwesolows prepctx(ctx); 157180ab886dSwesolows exit(invoke(ctx)); 157280ab886dSwesolows } 157380ab886dSwesolows 157480ab886dSwesolows if (ctx->i_pid < 0) { 1575aa9ef484SJohn Levon err(1, "fork failed"); 157680ab886dSwesolows } 157780ab886dSwesolows 157880ab886dSwesolows if (block) 157980ab886dSwesolows return (reap(ctx)); 158080ab886dSwesolows 158180ab886dSwesolows return (0); 15827c478bd9Sstevel@tonic-gate } 15837c478bd9Sstevel@tonic-gate 1584aa9ef484SJohn Levon static void 1585aa9ef484SJohn Levon parse_compiler(const char *spec, cw_compiler_t *compiler) 15867c478bd9Sstevel@tonic-gate { 1587aa9ef484SJohn Levon char *tspec, *token; 15887c478bd9Sstevel@tonic-gate 1589aa9ef484SJohn Levon if ((tspec = strdup(spec)) == NULL) 159080ab886dSwesolows nomem(); 159180ab886dSwesolows 1592aa9ef484SJohn Levon if ((token = strsep(&tspec, ",")) == NULL) 1593aa9ef484SJohn Levon errx(1, "Compiler is missing a name: %s", spec); 1594aa9ef484SJohn Levon compiler->c_name = token; 15957c478bd9Sstevel@tonic-gate 1596aa9ef484SJohn Levon if ((token = strsep(&tspec, ",")) == NULL) 1597aa9ef484SJohn Levon errx(1, "Compiler is missing a path: %s", spec); 1598aa9ef484SJohn Levon compiler->c_path = token; 15997c478bd9Sstevel@tonic-gate 1600aa9ef484SJohn Levon if ((token = strsep(&tspec, ",")) == NULL) 1601aa9ef484SJohn Levon errx(1, "Compiler is missing a style: %s", spec); 16027c478bd9Sstevel@tonic-gate 1603aa9ef484SJohn Levon if ((strcasecmp(token, "gnu") == 0) || 16041f5207b7SJohn Levon (strcasecmp(token, "gcc") == 0)) { 1605aa9ef484SJohn Levon compiler->c_style = GNU; 16061f5207b7SJohn Levon } else if ((strcasecmp(token, "sun") == 0) || 16071f5207b7SJohn Levon (strcasecmp(token, "cc") == 0)) { 1608aa9ef484SJohn Levon compiler->c_style = SUN; 16091f5207b7SJohn Levon } else if ((strcasecmp(token, "smatch") == 0)) { 16101f5207b7SJohn Levon compiler->c_style = SMATCH; 16111f5207b7SJohn Levon } else { 1612aa9ef484SJohn Levon errx(1, "unknown compiler style: %s", token); 16131f5207b7SJohn Levon } 161480ab886dSwesolows 1615aa9ef484SJohn Levon if (tspec != NULL) 1616aa9ef484SJohn Levon errx(1, "Excess tokens in compiler: %s", spec); 1617aa9ef484SJohn Levon } 16187c478bd9Sstevel@tonic-gate 161988e61e85SRichard Lowe static void 162088e61e85SRichard Lowe cleanup(cw_ictx_t *ctx) 162188e61e85SRichard Lowe { 162288e61e85SRichard Lowe DIR *dirp; 162388e61e85SRichard Lowe struct dirent *dp; 162488e61e85SRichard Lowe char buf[MAXPATHLEN]; 162588e61e85SRichard Lowe 162688e61e85SRichard Lowe if ((dirp = opendir(ctx->i_tmpdir)) == NULL) { 162788e61e85SRichard Lowe if (errno != ENOENT) { 162888e61e85SRichard Lowe err(1, "couldn't open temp directory: %s", 162988e61e85SRichard Lowe ctx->i_tmpdir); 163088e61e85SRichard Lowe } else { 163188e61e85SRichard Lowe return; 163288e61e85SRichard Lowe } 163388e61e85SRichard Lowe } 163488e61e85SRichard Lowe 163588e61e85SRichard Lowe errno = 0; 163688e61e85SRichard Lowe while ((dp = readdir(dirp)) != NULL) { 163788e61e85SRichard Lowe (void) snprintf(buf, MAXPATHLEN, "%s/%s", ctx->i_tmpdir, 163888e61e85SRichard Lowe dp->d_name); 163988e61e85SRichard Lowe 164088e61e85SRichard Lowe if (strcmp(dp->d_name, ".") == 0 || 164188e61e85SRichard Lowe strcmp(dp->d_name, "..") == 0) { 164288e61e85SRichard Lowe continue; 164388e61e85SRichard Lowe } 164488e61e85SRichard Lowe 164588e61e85SRichard Lowe if (unlink(buf) == -1) 164688e61e85SRichard Lowe err(1, "failed to unlink temp file: %s", dp->d_name); 164788e61e85SRichard Lowe errno = 0; 164888e61e85SRichard Lowe } 164988e61e85SRichard Lowe 165088e61e85SRichard Lowe if (errno != 0) { 165188e61e85SRichard Lowe err(1, "failed to read temporary directory: %s", 165288e61e85SRichard Lowe ctx->i_tmpdir); 165388e61e85SRichard Lowe } 165488e61e85SRichard Lowe 165588e61e85SRichard Lowe (void) closedir(dirp); 165688e61e85SRichard Lowe if (rmdir(ctx->i_tmpdir) != 0) { 165788e61e85SRichard Lowe err(1, "failed to unlink temporary directory: %s", 165888e61e85SRichard Lowe ctx->i_tmpdir); 165988e61e85SRichard Lowe } 166088e61e85SRichard Lowe } 166188e61e85SRichard Lowe 1662aa9ef484SJohn Levon int 1663aa9ef484SJohn Levon main(int argc, char **argv) 1664aa9ef484SJohn Levon { 1665aa9ef484SJohn Levon int ch; 1666aa9ef484SJohn Levon cw_compiler_t primary = { NULL, NULL, 0 }; 1667aa9ef484SJohn Levon cw_compiler_t shadows[10]; 1668aa9ef484SJohn Levon int nshadows = 0; 1669aa9ef484SJohn Levon int ret = 0; 1670aa9ef484SJohn Levon boolean_t do_serial = B_FALSE; 1671aa9ef484SJohn Levon boolean_t do_exec = B_FALSE; 1672aa9ef484SJohn Levon boolean_t vflg = B_FALSE; 1673aa9ef484SJohn Levon boolean_t Cflg = B_FALSE; 1674aa9ef484SJohn Levon boolean_t cflg = B_FALSE; 1675aa9ef484SJohn Levon boolean_t nflg = B_FALSE; 167688e61e85SRichard Lowe char *tmpdir; 1677aa9ef484SJohn Levon 1678aa9ef484SJohn Levon cw_ictx_t *main_ctx; 1679aa9ef484SJohn Levon 1680aa9ef484SJohn Levon static struct option longopts[] = { 1681aa9ef484SJohn Levon { "compiler", no_argument, NULL, 'c' }, 168269b1fd3fSRichard Lowe { "linker", required_argument, NULL, 'l' }, 1683aa9ef484SJohn Levon { "noecho", no_argument, NULL, 'n' }, 1684aa9ef484SJohn Levon { "primary", required_argument, NULL, 'p' }, 1685aa9ef484SJohn Levon { "shadow", required_argument, NULL, 's' }, 1686aa9ef484SJohn Levon { "versions", no_argument, NULL, 'v' }, 1687aa9ef484SJohn Levon { NULL, 0, NULL, 0 }, 1688aa9ef484SJohn Levon }; 1689aa9ef484SJohn Levon 1690aa9ef484SJohn Levon 1691aa9ef484SJohn Levon if ((main_ctx = newictx()) == NULL) 1692aa9ef484SJohn Levon nomem(); 1693aa9ef484SJohn Levon 1694aa9ef484SJohn Levon while ((ch = getopt_long(argc, argv, "C", longopts, NULL)) != -1) { 1695aa9ef484SJohn Levon switch (ch) { 1696aa9ef484SJohn Levon case 'c': 1697aa9ef484SJohn Levon cflg = B_TRUE; 1698aa9ef484SJohn Levon break; 1699aa9ef484SJohn Levon case 'C': 1700aa9ef484SJohn Levon Cflg = B_TRUE; 1701aa9ef484SJohn Levon break; 170269b1fd3fSRichard Lowe case 'l': 170369b1fd3fSRichard Lowe if ((main_ctx->i_linker = strdup(optarg)) == NULL) 170469b1fd3fSRichard Lowe nomem(); 170569b1fd3fSRichard Lowe break; 1706aa9ef484SJohn Levon case 'n': 1707aa9ef484SJohn Levon nflg = B_TRUE; 1708aa9ef484SJohn Levon break; 1709aa9ef484SJohn Levon case 'p': 1710aa9ef484SJohn Levon if (primary.c_path != NULL) { 1711aa9ef484SJohn Levon warnx("Only one primary compiler may " 1712aa9ef484SJohn Levon "be specified"); 1713aa9ef484SJohn Levon usage(); 1714aa9ef484SJohn Levon } 1715aa9ef484SJohn Levon 1716aa9ef484SJohn Levon parse_compiler(optarg, &primary); 1717aa9ef484SJohn Levon break; 1718aa9ef484SJohn Levon case 's': 1719aa9ef484SJohn Levon if (nshadows >= 10) 1720aa9ef484SJohn Levon errx(1, "May only use 10 shadows at " 1721aa9ef484SJohn Levon "the moment"); 1722aa9ef484SJohn Levon parse_compiler(optarg, &shadows[nshadows]); 1723aa9ef484SJohn Levon nshadows++; 1724aa9ef484SJohn Levon break; 1725aa9ef484SJohn Levon case 'v': 1726aa9ef484SJohn Levon vflg = B_TRUE; 1727aa9ef484SJohn Levon break; 1728aa9ef484SJohn Levon default: 1729aa9ef484SJohn Levon (void) fprintf(stderr, "Did you forget '--'?\n"); 1730aa9ef484SJohn Levon usage(); 1731aa9ef484SJohn Levon } 1732aa9ef484SJohn Levon } 1733aa9ef484SJohn Levon 1734aa9ef484SJohn Levon if (primary.c_path == NULL) { 1735aa9ef484SJohn Levon warnx("A primary compiler must be specified"); 17367c478bd9Sstevel@tonic-gate usage(); 17377c478bd9Sstevel@tonic-gate } 17387c478bd9Sstevel@tonic-gate 1739aa9ef484SJohn Levon do_serial = (getenv("CW_SHADOW_SERIAL") == NULL) ? B_FALSE : B_TRUE; 1740aa9ef484SJohn Levon do_exec = (getenv("CW_NO_EXEC") == NULL) ? B_TRUE : B_FALSE; 1741aa9ef484SJohn Levon 1742aa9ef484SJohn Levon /* Leave room for argv[0] */ 1743aa9ef484SJohn Levon argc -= (optind - 1); 1744aa9ef484SJohn Levon argv += (optind - 1); 1745aa9ef484SJohn Levon 1746aa9ef484SJohn Levon main_ctx->i_oldargc = argc; 1747aa9ef484SJohn Levon main_ctx->i_oldargv = argv; 1748aa9ef484SJohn Levon main_ctx->i_flags = CW_F_XLATE; 1749aa9ef484SJohn Levon if (nflg == 0) 1750aa9ef484SJohn Levon main_ctx->i_flags |= CW_F_ECHO; 1751aa9ef484SJohn Levon if (do_exec) 1752aa9ef484SJohn Levon main_ctx->i_flags |= CW_F_EXEC; 1753aa9ef484SJohn Levon if (Cflg) 1754aa9ef484SJohn Levon main_ctx->i_flags |= CW_F_CXX; 1755aa9ef484SJohn Levon main_ctx->i_compiler = &primary; 1756aa9ef484SJohn Levon 1757aa9ef484SJohn Levon if (cflg) { 1758aa9ef484SJohn Levon (void) fputs(primary.c_path, stdout); 17597c478bd9Sstevel@tonic-gate } 17607c478bd9Sstevel@tonic-gate 1761aa9ef484SJohn Levon if (vflg) { 1762aa9ef484SJohn Levon (void) printf("cw version %s\n", CW_VERSION); 17631912d2c4Swesolows (void) fflush(stdout); 1764aa9ef484SJohn Levon main_ctx->i_flags &= ~CW_F_ECHO; 1765aa9ef484SJohn Levon main_ctx->i_flags |= CW_F_PROG | CW_F_EXEC; 17661912d2c4Swesolows do_serial = 1; 17671912d2c4Swesolows } 17681912d2c4Swesolows 176988e61e85SRichard Lowe tmpdir = getenv("TMPDIR"); 177088e61e85SRichard Lowe if (tmpdir == NULL) 177188e61e85SRichard Lowe tmpdir = "/tmp"; 177288e61e85SRichard Lowe 177388e61e85SRichard Lowe if (asprintf(&main_ctx->i_tmpdir, "%s/cw.XXXXXX", tmpdir) == -1) 177488e61e85SRichard Lowe nomem(); 177588e61e85SRichard Lowe 177688e61e85SRichard Lowe if ((main_ctx->i_tmpdir = mkdtemp(main_ctx->i_tmpdir)) == NULL) 177788e61e85SRichard Lowe errx(1, "failed to create temporary directory"); 177888e61e85SRichard Lowe 1779aa9ef484SJohn Levon ret |= exec_ctx(main_ctx, do_serial); 17801912d2c4Swesolows 1781aa9ef484SJohn Levon for (int i = 0; i < nshadows; i++) { 1782aa9ef484SJohn Levon int r; 1783aa9ef484SJohn Levon cw_ictx_t *shadow_ctx; 178480ab886dSwesolows 1785aa9ef484SJohn Levon if ((shadow_ctx = newictx()) == NULL) 1786aa9ef484SJohn Levon nomem(); 1787aa9ef484SJohn Levon 178888e61e85SRichard Lowe (void) memcpy(shadow_ctx, main_ctx, sizeof (cw_ictx_t)); 1789aa9ef484SJohn Levon 1790aa9ef484SJohn Levon shadow_ctx->i_flags |= CW_F_SHADOW; 1791aa9ef484SJohn Levon shadow_ctx->i_compiler = &shadows[i]; 1792aa9ef484SJohn Levon 1793aa9ef484SJohn Levon r = exec_ctx(shadow_ctx, do_serial); 1794aa9ef484SJohn Levon if (r == 0) { 1795aa9ef484SJohn Levon shadow_ctx->i_next = main_ctx->i_next; 1796aa9ef484SJohn Levon main_ctx->i_next = shadow_ctx; 1797aa9ef484SJohn Levon } 1798aa9ef484SJohn Levon ret |= r; 17997c478bd9Sstevel@tonic-gate } 18007c478bd9Sstevel@tonic-gate 1801aa9ef484SJohn Levon if (!do_serial) { 1802aa9ef484SJohn Levon cw_ictx_t *next = main_ctx; 1803aa9ef484SJohn Levon while (next != NULL) { 1804aa9ef484SJohn Levon cw_ictx_t *toreap = next; 1805aa9ef484SJohn Levon next = next->i_next; 1806aa9ef484SJohn Levon ret |= reap(toreap); 1807aa9ef484SJohn Levon } 1808aa9ef484SJohn Levon } 18097c478bd9Sstevel@tonic-gate 181088e61e85SRichard Lowe cleanup(main_ctx); 181180ab886dSwesolows return (ret); 18127c478bd9Sstevel@tonic-gate } 1813