xref: /illumos-gate/usr/src/tools/cw/cw.c (revision 88e61e85)
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 */
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>
265*88e61e85SRichard Lowe #include <dirent.h>
266aa9ef484SJohn Levon 
2677c478bd9Sstevel@tonic-gate #include <sys/param.h>
26880ab886dSwesolows #include <sys/stat.h>
269aa9ef484SJohn Levon #include <sys/types.h>
270aa9ef484SJohn Levon #include <sys/utsname.h>
271aa9ef484SJohn Levon #include <sys/wait.h>
27280ab886dSwesolows 
27380ab886dSwesolows #define	CW_F_CXX	0x01
27480ab886dSwesolows #define	CW_F_SHADOW	0x02
27580ab886dSwesolows #define	CW_F_EXEC	0x04
27680ab886dSwesolows #define	CW_F_ECHO	0x08
27780ab886dSwesolows #define	CW_F_XLATE	0x10
2781912d2c4Swesolows #define	CW_F_PROG	0x20
27980ab886dSwesolows 
28080ab886dSwesolows typedef enum cw_op {
28180ab886dSwesolows 	CW_O_NONE = 0,
28280ab886dSwesolows 	CW_O_PREPROCESS,
28380ab886dSwesolows 	CW_O_COMPILE,
28480ab886dSwesolows 	CW_O_LINK
28580ab886dSwesolows } cw_op_t;
28680ab886dSwesolows 
28780ab886dSwesolows struct aelist {
28880ab886dSwesolows 	struct ae {
28980ab886dSwesolows 		struct ae *ae_next;
29080ab886dSwesolows 		char *ae_arg;
29180ab886dSwesolows 	} *ael_head, *ael_tail;
29280ab886dSwesolows 	int ael_argc;
29380ab886dSwesolows };
29480ab886dSwesolows 
295aa9ef484SJohn Levon typedef enum {
296aa9ef484SJohn Levon 	GNU,
2971f5207b7SJohn Levon 	SUN,
2981f5207b7SJohn Levon 	SMATCH
299aa9ef484SJohn Levon } compiler_style_t;
300aa9ef484SJohn Levon 
301aa9ef484SJohn Levon typedef struct {
302aa9ef484SJohn Levon 	char *c_name;
303aa9ef484SJohn Levon 	char *c_path;
304aa9ef484SJohn Levon 	compiler_style_t c_style;
305aa9ef484SJohn Levon } cw_compiler_t;
306aa9ef484SJohn Levon 
30780ab886dSwesolows typedef struct cw_ictx {
308aa9ef484SJohn Levon 	struct cw_ictx	*i_next;
309aa9ef484SJohn Levon 	cw_compiler_t	*i_compiler;
31080ab886dSwesolows 	struct aelist	*i_ae;
31180ab886dSwesolows 	uint32_t	i_flags;
31280ab886dSwesolows 	int		i_oldargc;
31380ab886dSwesolows 	char		**i_oldargv;
31480ab886dSwesolows 	pid_t		i_pid;
315*88e61e85SRichard Lowe 	char		*i_tmpdir;
3161912d2c4Swesolows 	char		*i_stderr;
31780ab886dSwesolows } cw_ictx_t;
31880ab886dSwesolows 
319e521259dSpetede /*
320e521259dSpetede  * Status values to indicate which Studio compiler and associated
321e521259dSpetede  * flags are being used.
322e521259dSpetede  */
323e521259dSpetede #define	M32		0x01	/* -m32 - only on Studio 12 */
324e521259dSpetede #define	M64		0x02	/* -m64 - only on Studio 12 */
325e521259dSpetede #define	SS11		0x100	/* Studio 11 */
326e521259dSpetede #define	SS12		0x200	/* Studio 12 */
3277c478bd9Sstevel@tonic-gate 
328e521259dSpetede #define	TRANS_ENTRY	5
329e521259dSpetede /*
330e521259dSpetede  * Translation table definition for the -xarch= flag. The "x_arg"
331e521259dSpetede  * value is translated into the appropriate gcc flags according
332e521259dSpetede  * to the values in x_trans[n]. The x_flags indicates what compiler
333e521259dSpetede  * is being used and what flags have been set via the use of
334e521259dSpetede  * "x_arg".
335e521259dSpetede  */
336e521259dSpetede typedef struct xarch_table {
337e521259dSpetede 	char	*x_arg;
338e521259dSpetede 	int	x_flags;
339e521259dSpetede 	char	*x_trans[TRANS_ENTRY];
340e521259dSpetede } xarch_table_t;
341e521259dSpetede 
342e521259dSpetede /*
343e521259dSpetede  * The translation table for the -xarch= flag used in the Studio compilers.
344e521259dSpetede  */
345e521259dSpetede static const xarch_table_t xtbl[] = {
3467c478bd9Sstevel@tonic-gate #if defined(__x86)
347aa9ef484SJohn Levon 	{ "generic",	SS11, {NULL} },
3487a6460b6Spetede 	{ "generic64",	(SS11|M64), { "-m64", "-mtune=opteron" } },
3497a6460b6Spetede 	{ "amd64",	(SS11|M64), { "-m64", "-mtune=opteron" } },
3507a6460b6Spetede 	{ "386",	SS11,	{ "-march=i386" } },
3517a6460b6Spetede 	{ "pentium_pro", SS11,	{ "-march=pentiumpro" } },
35225c28e83SPiotr Jasiukajtis 	{ "sse",	SS11, { "-msse", "-mfpmath=sse" } },
35325c28e83SPiotr Jasiukajtis 	{ "sse2",	SS11, { "-msse2", "-mfpmath=sse" } },
3547c478bd9Sstevel@tonic-gate #elif defined(__sparc)
3557a6460b6Spetede 	{ "generic",	(SS11|M32), { "-m32", "-mcpu=v8" } },
3567a6460b6Spetede 	{ "generic64",	(SS11|M64), { "-m64", "-mcpu=v9" } },
3577a6460b6Spetede 	{ "v8",		(SS11|M32), { "-m32", "-mcpu=v8", "-mno-v8plus" } },
3587a6460b6Spetede 	{ "v8plus",	(SS11|M32), { "-m32", "-mcpu=v9", "-mv8plus" } },
3597a6460b6Spetede 	{ "v8plusa",	(SS11|M32), { "-m32", "-mcpu=ultrasparc", "-mv8plus",
3607a6460b6Spetede 			"-mvis" } },
3617a6460b6Spetede 	{ "v8plusb",	(SS11|M32), { "-m32", "-mcpu=ultrasparc3", "-mv8plus",
3627a6460b6Spetede 			"-mvis" } },
3637a6460b6Spetede 	{ "v9",		(SS11|M64), { "-m64", "-mcpu=v9" } },
3647a6460b6Spetede 	{ "v9a",	(SS11|M64), { "-m64", "-mcpu=ultrasparc", "-mvis" } },
3657a6460b6Spetede 	{ "v9b",	(SS11|M64), { "-m64", "-mcpu=ultrasparc3", "-mvis" } },
3667a6460b6Spetede 	{ "sparc",	SS12, { "-mcpu=v9", "-mv8plus" } },
3677a6460b6Spetede 	{ "sparcvis",	SS12, { "-mcpu=ultrasparc", "-mvis" } },
3687a6460b6Spetede 	{ "sparcvis2",	SS12, { "-mcpu=ultrasparc3", "-mvis" } }
3697c478bd9Sstevel@tonic-gate #endif
3707c478bd9Sstevel@tonic-gate };
3717c478bd9Sstevel@tonic-gate 
372e521259dSpetede static int xtbl_size = sizeof (xtbl) / sizeof (xarch_table_t);
373e521259dSpetede 
3747c478bd9Sstevel@tonic-gate static const char *xchip_tbl[] = {
3757c478bd9Sstevel@tonic-gate #if defined(__x86)
3767c478bd9Sstevel@tonic-gate 	"386",		"-mtune=i386", NULL,
3777c478bd9Sstevel@tonic-gate 	"486",		"-mtune=i486", NULL,
3787c478bd9Sstevel@tonic-gate 	"pentium",	"-mtune=pentium", NULL,
3797c478bd9Sstevel@tonic-gate 	"pentium_pro",  "-mtune=pentiumpro", NULL,
3807c478bd9Sstevel@tonic-gate #elif defined(__sparc)
3817c478bd9Sstevel@tonic-gate 	"super",	"-mtune=supersparc", NULL,
3827c478bd9Sstevel@tonic-gate 	"ultra",	"-mtune=ultrasparc", NULL,
3837c478bd9Sstevel@tonic-gate 	"ultra3",	"-mtune=ultrasparc3", NULL,
3847c478bd9Sstevel@tonic-gate #endif
3857c478bd9Sstevel@tonic-gate 	NULL,		NULL
3867c478bd9Sstevel@tonic-gate };
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate static const char *xcode_tbl[] = {
3897c478bd9Sstevel@tonic-gate #if defined(__sparc)
3907c478bd9Sstevel@tonic-gate 	"abs32",	"-fno-pic", "-mcmodel=medlow", NULL,
3917c478bd9Sstevel@tonic-gate 	"abs44",	"-fno-pic", "-mcmodel=medmid", NULL,
3927c478bd9Sstevel@tonic-gate 	"abs64",	"-fno-pic", "-mcmodel=medany", NULL,
3937c478bd9Sstevel@tonic-gate 	"pic13",	"-fpic", NULL,
3947c478bd9Sstevel@tonic-gate 	"pic32",	"-fPIC", NULL,
3957c478bd9Sstevel@tonic-gate #endif
3967c478bd9Sstevel@tonic-gate 	NULL,		NULL
3977c478bd9Sstevel@tonic-gate };
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate static const char *xtarget_tbl[] = {
4007c478bd9Sstevel@tonic-gate #if defined(__x86)
4017c478bd9Sstevel@tonic-gate 	"pentium_pro",	"-march=pentiumpro", NULL,
4027c478bd9Sstevel@tonic-gate #endif	/* __x86 */
4037c478bd9Sstevel@tonic-gate 	NULL,		NULL
4047c478bd9Sstevel@tonic-gate };
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate static const char *xregs_tbl[] = {
4077c478bd9Sstevel@tonic-gate #if defined(__sparc)
4087c478bd9Sstevel@tonic-gate 	"appl",		"-mapp-regs", NULL,
4097c478bd9Sstevel@tonic-gate 	"no%appl",	"-mno-app-regs", NULL,
4107c478bd9Sstevel@tonic-gate 	"float",	"-mfpu", NULL,
4117c478bd9Sstevel@tonic-gate 	"no%float",	"-mno-fpu", NULL,
4127c478bd9Sstevel@tonic-gate #endif	/* __sparc */
4137c478bd9Sstevel@tonic-gate 	NULL,		NULL
4147c478bd9Sstevel@tonic-gate };
4157c478bd9Sstevel@tonic-gate 
41680ab886dSwesolows static void
41780ab886dSwesolows nomem(void)
41880ab886dSwesolows {
419aa9ef484SJohn Levon 	errx(1, "out of memory");
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate static void
4237c478bd9Sstevel@tonic-gate newae(struct aelist *ael, const char *arg)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	struct ae *ae;
4267c478bd9Sstevel@tonic-gate 
42780ab886dSwesolows 	if ((ae = calloc(sizeof (*ae), 1)) == NULL)
42880ab886dSwesolows 		nomem();
4297c478bd9Sstevel@tonic-gate 	ae->ae_arg = strdup(arg);
4307c478bd9Sstevel@tonic-gate 	if (ael->ael_tail == NULL)
4317c478bd9Sstevel@tonic-gate 		ael->ael_head = ae;
4327c478bd9Sstevel@tonic-gate 	else
4337c478bd9Sstevel@tonic-gate 		ael->ael_tail->ae_next = ae;
4347c478bd9Sstevel@tonic-gate 	ael->ael_tail = ae;
43580ab886dSwesolows 	ael->ael_argc++;
43680ab886dSwesolows }
43780ab886dSwesolows 
43880ab886dSwesolows static cw_ictx_t *
43980ab886dSwesolows newictx(void)
44080ab886dSwesolows {
44180ab886dSwesolows 	cw_ictx_t *ctx = calloc(sizeof (cw_ictx_t), 1);
44280ab886dSwesolows 	if (ctx)
44380ab886dSwesolows 		if ((ctx->i_ae = calloc(sizeof (struct aelist), 1)) == NULL) {
44480ab886dSwesolows 			free(ctx);
44580ab886dSwesolows 			return (NULL);
44680ab886dSwesolows 		}
44780ab886dSwesolows 
44880ab886dSwesolows 	return (ctx);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate static void
4527c478bd9Sstevel@tonic-gate error(const char *arg)
4537c478bd9Sstevel@tonic-gate {
454aa9ef484SJohn Levon 	errx(2, "error: mapping failed at or near arg '%s'", arg);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate /*
4587c478bd9Sstevel@tonic-gate  * Add the current favourite set of warnings to the gcc invocation.
4597c478bd9Sstevel@tonic-gate  */
4607c478bd9Sstevel@tonic-gate static void
4617c478bd9Sstevel@tonic-gate warnings(struct aelist *h)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate 	static int warningsonce;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if (warningsonce++)
4667c478bd9Sstevel@tonic-gate 		return;
4677c478bd9Sstevel@tonic-gate 
4687014882cSRichard Lowe 	/*
4697014882cSRichard Lowe 	 * Enable as many warnings as exist, then disable those that we never
4707014882cSRichard Lowe 	 * ever want.
4717014882cSRichard Lowe 	 */
4727c478bd9Sstevel@tonic-gate 	newae(h, "-Wall");
4737014882cSRichard Lowe 	newae(h, "-Wextra");
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate static void
4777c478bd9Sstevel@tonic-gate optim_disable(struct aelist *h, int level)
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate 	if (level >= 2) {
4807c478bd9Sstevel@tonic-gate 		newae(h, "-fno-strict-aliasing");
4817c478bd9Sstevel@tonic-gate 		newae(h, "-fno-unit-at-a-time");
4827c478bd9Sstevel@tonic-gate 		newae(h, "-fno-optimize-sibling-calls");
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate static void
4877c478bd9Sstevel@tonic-gate Xsmode(struct aelist *h)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	static int xsonce;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	if (xsonce++)
4927c478bd9Sstevel@tonic-gate 		return;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	newae(h, "-traditional");
4957c478bd9Sstevel@tonic-gate 	newae(h, "-traditional-cpp");
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate static void
4997c478bd9Sstevel@tonic-gate usage()
5007c478bd9Sstevel@tonic-gate {
501aa9ef484SJohn Levon 	extern char *__progname;
5027c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
503aa9ef484SJohn Levon 	    "usage: %s [-C] [--versions] --primary <compiler> "
504aa9ef484SJohn Levon 	    "[--shadow <compiler>]... -- cflags...\n",
505aa9ef484SJohn Levon 	    __progname);
506aa9ef484SJohn Levon 	(void) fprintf(stderr, "compilers take the form: name,path,style\n"
507aa9ef484SJohn Levon 	    " - name: a unique name usable in flag specifiers\n"
508aa9ef484SJohn Levon 	    " - path: path to the compiler binary\n"
509aa9ef484SJohn Levon 	    " - style: the style of flags expected: either sun or gnu\n");
5107c478bd9Sstevel@tonic-gate 	exit(2);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
513e521259dSpetede static int
514e521259dSpetede xlate_xtb(struct aelist *h, const char *xarg)
515e521259dSpetede {
516e521259dSpetede 	int	i, j;
517e521259dSpetede 
518e521259dSpetede 	for (i = 0; i < xtbl_size; i++) {
519e521259dSpetede 		if (strcmp(xtbl[i].x_arg, xarg) == 0)
520e521259dSpetede 			break;
521e521259dSpetede 	}
522e521259dSpetede 
523e521259dSpetede 	/*
524e521259dSpetede 	 * At the end of the table and so no matching "arg" entry
525e521259dSpetede 	 * found and so this must be a bad -xarch= flag.
526e521259dSpetede 	 */
527e521259dSpetede 	if (i == xtbl_size)
528e521259dSpetede 		error(xarg);
529e521259dSpetede 
530e521259dSpetede 	for (j = 0; j < TRANS_ENTRY; j++) {
531e521259dSpetede 		if (xtbl[i].x_trans[j] != NULL)
532e521259dSpetede 			newae(h, xtbl[i].x_trans[j]);
533e521259dSpetede 		else
534e521259dSpetede 			break;
535e521259dSpetede 	}
536e521259dSpetede 	return (xtbl[i].x_flags);
537e521259dSpetede 
538e521259dSpetede }
539e521259dSpetede 
5407c478bd9Sstevel@tonic-gate static void
5417c478bd9Sstevel@tonic-gate xlate(struct aelist *h, const char *xarg, const char **table)
5427c478bd9Sstevel@tonic-gate {
5437c478bd9Sstevel@tonic-gate 	while (*table != NULL && strcmp(xarg, *table) != 0) {
5447c478bd9Sstevel@tonic-gate 		while (*table != NULL)
5457c478bd9Sstevel@tonic-gate 			table++;
5467c478bd9Sstevel@tonic-gate 		table++;
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	if (*table == NULL)
5507c478bd9Sstevel@tonic-gate 		error(xarg);
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	table++;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	while (*table != NULL) {
5557c478bd9Sstevel@tonic-gate 		newae(h, *table);
5567c478bd9Sstevel@tonic-gate 		table++;
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate 
560*88e61e85SRichard Lowe /*
561*88e61e85SRichard Lowe  * The compiler wants the output file to end in appropriate extension.  If
562*88e61e85SRichard Lowe  * we're generating a name from whole cloth (path == NULL), we assume that
563*88e61e85SRichard Lowe  * extension to be .o, otherwise we match the extension of the caller.
564*88e61e85SRichard Lowe  */
565*88e61e85SRichard Lowe static char *
566*88e61e85SRichard Lowe discard_file_name(cw_ictx_t *ctx, const char *path)
567*88e61e85SRichard Lowe {
568*88e61e85SRichard Lowe 	char *ret, *ext;
569*88e61e85SRichard Lowe 	char tmpl[] = "cwXXXXXX";
570*88e61e85SRichard Lowe 
571*88e61e85SRichard Lowe 	if (path == NULL) {
572*88e61e85SRichard Lowe 		ext = ".o";
573*88e61e85SRichard Lowe 	} else {
574*88e61e85SRichard Lowe 		ext = strrchr(path, '.');
575*88e61e85SRichard Lowe 	}
576*88e61e85SRichard Lowe 
577*88e61e85SRichard Lowe 	/*
578*88e61e85SRichard Lowe 	 * We need absolute control over where the temporary file goes, since
579*88e61e85SRichard Lowe 	 * we rely on it for cleanup so tempnam(3C) and tmpnam(3C) are
580*88e61e85SRichard Lowe 	 * inappropriate (they use TMPDIR, preferentially).
581*88e61e85SRichard Lowe 	 *
582*88e61e85SRichard Lowe 	 * mkstemp(3C) doesn't actually help us, since the temporary file
583*88e61e85SRichard Lowe 	 * isn't used by us, only its name.
584*88e61e85SRichard Lowe 	 */
585*88e61e85SRichard Lowe 	if (mktemp(tmpl) == NULL)
586*88e61e85SRichard Lowe 		nomem();
587*88e61e85SRichard Lowe 
588*88e61e85SRichard Lowe 	(void) asprintf(&ret, "%s/%s%s", ctx->i_tmpdir, tmpl,
589*88e61e85SRichard Lowe 	    (ext != NULL) ? ext : "");
590*88e61e85SRichard Lowe 
591*88e61e85SRichard Lowe 	if (ret == NULL)
592*88e61e85SRichard Lowe 		nomem();
593*88e61e85SRichard Lowe 
594*88e61e85SRichard Lowe 	return (ret);
595*88e61e85SRichard Lowe }
596*88e61e85SRichard Lowe 
597*88e61e85SRichard Lowe static boolean_t
598*88e61e85SRichard Lowe is_source_file(const char *path)
599*88e61e85SRichard Lowe {
600*88e61e85SRichard Lowe 	char *ext = strrchr(path, '.');
601*88e61e85SRichard Lowe 
602*88e61e85SRichard Lowe 	if ((ext == NULL) || ((ext + 1) == '\0'))
603*88e61e85SRichard Lowe 		return (B_FALSE);
604*88e61e85SRichard Lowe 
605*88e61e85SRichard Lowe 	ext += 1;
606*88e61e85SRichard Lowe 
607*88e61e85SRichard Lowe 	if ((strcasecmp(ext, "c") == 0) ||
608*88e61e85SRichard Lowe 	    (strcmp(ext, "cc") == 0) ||
609*88e61e85SRichard Lowe 	    (strcmp(ext, "i") == 0) ||
610*88e61e85SRichard Lowe 	    (strcasecmp(ext, "s") == 0) ||
611*88e61e85SRichard Lowe 	    (strcmp(ext, "cpp") == 0)) {
612*88e61e85SRichard Lowe 		return (B_TRUE);
613*88e61e85SRichard Lowe 	}
614*88e61e85SRichard Lowe 
615*88e61e85SRichard Lowe 	return (B_FALSE);
616*88e61e85SRichard Lowe }
617*88e61e85SRichard Lowe 
618*88e61e85SRichard Lowe 
6197c478bd9Sstevel@tonic-gate static void
62080ab886dSwesolows do_gcc(cw_ictx_t *ctx)
6217c478bd9Sstevel@tonic-gate {
6227c478bd9Sstevel@tonic-gate 	int c;
623e1bf37b1SRichard Lowe 	int nolibc = 0;
62480ab886dSwesolows 	int in_output = 0, seen_o = 0, c_files = 0;
62580ab886dSwesolows 	cw_op_t op = CW_O_LINK;
6267c478bd9Sstevel@tonic-gate 	char *model = NULL;
627aa9ef484SJohn Levon 	char *nameflag;
628*88e61e85SRichard Lowe 	int mflag = 0;
6297c478bd9Sstevel@tonic-gate 
6301912d2c4Swesolows 	if (ctx->i_flags & CW_F_PROG) {
6311912d2c4Swesolows 		newae(ctx->i_ae, "--version");
6321912d2c4Swesolows 		return;
6331912d2c4Swesolows 	}
6341912d2c4Swesolows 
63580ab886dSwesolows 	newae(ctx->i_ae, "-fident");
63680ab886dSwesolows 	newae(ctx->i_ae, "-finline");
63780ab886dSwesolows 	newae(ctx->i_ae, "-fno-inline-functions");
63880ab886dSwesolows 	newae(ctx->i_ae, "-fno-builtin");
63980ab886dSwesolows 	newae(ctx->i_ae, "-fno-asm");
6407014882cSRichard Lowe 	newae(ctx->i_ae, "-fdiagnostics-show-option");
64180ab886dSwesolows 	newae(ctx->i_ae, "-nodefaultlibs");
6427c478bd9Sstevel@tonic-gate 
64302e56f3fSwesolows #if defined(__sparc)
64402e56f3fSwesolows 	/*
64502e56f3fSwesolows 	 * The SPARC ldd and std instructions require 8-byte alignment of
64602e56f3fSwesolows 	 * their address operand.  gcc correctly uses them only when the
64702e56f3fSwesolows 	 * ABI requires 8-byte alignment; unfortunately we have a number of
64802e56f3fSwesolows 	 * pieces of buggy code that doesn't conform to the ABI.  This
64902e56f3fSwesolows 	 * flag makes gcc work more like Studio with -xmemalign=4.
65002e56f3fSwesolows 	 */
65180ab886dSwesolows 	newae(ctx->i_ae, "-mno-integer-ldd-std");
65202e56f3fSwesolows #endif
65302e56f3fSwesolows 
6547c478bd9Sstevel@tonic-gate 	/*
6557c478bd9Sstevel@tonic-gate 	 * This is needed because 'u' is defined
6567c478bd9Sstevel@tonic-gate 	 * under a conditional on 'sun'.  Should
6577c478bd9Sstevel@tonic-gate 	 * probably just remove the conditional,
6587c478bd9Sstevel@tonic-gate 	 * or make it be dependent on '__sun'.
6597c478bd9Sstevel@tonic-gate 	 *
6607c478bd9Sstevel@tonic-gate 	 * -Dunix is also missing in enhanced ANSI mode
6617c478bd9Sstevel@tonic-gate 	 */
66280ab886dSwesolows 	newae(ctx->i_ae, "-D__sun");
6637c478bd9Sstevel@tonic-gate 
664aa9ef484SJohn Levon 	if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1)
665aa9ef484SJohn Levon 		nomem();
666aa9ef484SJohn Levon 
6677c478bd9Sstevel@tonic-gate 	/*
6687c478bd9Sstevel@tonic-gate 	 * Walk the argument list, translating as we go ..
6697c478bd9Sstevel@tonic-gate 	 */
67080ab886dSwesolows 	while (--ctx->i_oldargc > 0) {
67180ab886dSwesolows 		char *arg = *++ctx->i_oldargv;
6727c478bd9Sstevel@tonic-gate 		size_t arglen = strlen(arg);
6737c478bd9Sstevel@tonic-gate 
67480ab886dSwesolows 		if (*arg == '-') {
6757c478bd9Sstevel@tonic-gate 			arglen--;
67680ab886dSwesolows 		} else {
6777c478bd9Sstevel@tonic-gate 			/*
6787c478bd9Sstevel@tonic-gate 			 * Discard inline files that gcc doesn't grok
6797c478bd9Sstevel@tonic-gate 			 */
68080ab886dSwesolows 			if (!in_output && arglen > 3 &&
6817c478bd9Sstevel@tonic-gate 			    strcmp(arg + arglen - 3, ".il") == 0)
6827c478bd9Sstevel@tonic-gate 				continue;
6837c478bd9Sstevel@tonic-gate 
684*88e61e85SRichard Lowe 			if (!in_output && is_source_file(arg))
68580ab886dSwesolows 				c_files++;
68680ab886dSwesolows 
6877c478bd9Sstevel@tonic-gate 			/*
68880ab886dSwesolows 			 * Otherwise, filenames and partial arguments
68980ab886dSwesolows 			 * are passed through for gcc to chew on.  However,
69080ab886dSwesolows 			 * output is always discarded for the secondary
69180ab886dSwesolows 			 * compiler.
6927c478bd9Sstevel@tonic-gate 			 */
693*88e61e85SRichard Lowe 			if ((ctx->i_flags & CW_F_SHADOW) && in_output) {
694*88e61e85SRichard Lowe 				newae(ctx->i_ae, discard_file_name(ctx, arg));
695*88e61e85SRichard Lowe 			} else {
69680ab886dSwesolows 				newae(ctx->i_ae, arg);
697*88e61e85SRichard Lowe 			}
69880ab886dSwesolows 			in_output = 0;
6997c478bd9Sstevel@tonic-gate 			continue;
7007c478bd9Sstevel@tonic-gate 		}
7017c478bd9Sstevel@tonic-gate 
70280ab886dSwesolows 		if (ctx->i_flags & CW_F_CXX) {
703aa9ef484SJohn Levon 			if (strncmp(arg, "-_g++=", 6) == 0) {
704aa9ef484SJohn Levon 				newae(ctx->i_ae, strchr(arg, '=') + 1);
705aa9ef484SJohn Levon 				continue;
706aa9ef484SJohn Levon 			}
7077c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "-compat=", 8) == 0) {
7087c478bd9Sstevel@tonic-gate 				/* discard -compat=4 and -compat=5 */
7097c478bd9Sstevel@tonic-gate 				continue;
7107c478bd9Sstevel@tonic-gate 			}
7117c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-Qoption") == 0) {
7127c478bd9Sstevel@tonic-gate 				/* discard -Qoption and its two arguments */
71380ab886dSwesolows 				if (ctx->i_oldargc < 3)
7147c478bd9Sstevel@tonic-gate 					error(arg);
71580ab886dSwesolows 				ctx->i_oldargc -= 2;
71680ab886dSwesolows 				ctx->i_oldargv += 2;
7177c478bd9Sstevel@tonic-gate 				continue;
7187c478bd9Sstevel@tonic-gate 			}
7197c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-xwe") == 0) {
7207c478bd9Sstevel@tonic-gate 				/* turn warnings into errors */
72180ab886dSwesolows 				newae(ctx->i_ae, "-Werror");
7227c478bd9Sstevel@tonic-gate 				continue;
7237c478bd9Sstevel@tonic-gate 			}
7247c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-norunpath") == 0) {
7257c478bd9Sstevel@tonic-gate 				/* gcc has no corresponding option */
7267c478bd9Sstevel@tonic-gate 				continue;
7277c478bd9Sstevel@tonic-gate 			}
7287c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-nolib") == 0) {
7297c478bd9Sstevel@tonic-gate 				/* -nodefaultlibs is on by default */
7307c478bd9Sstevel@tonic-gate 				nolibc = 1;
7317c478bd9Sstevel@tonic-gate 				continue;
7327c478bd9Sstevel@tonic-gate 			}
7337c478bd9Sstevel@tonic-gate #if defined(__sparc)
7347c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-cg92") == 0) {
735e521259dSpetede 				mflag |= xlate_xtb(ctx->i_ae, "v8");
73680ab886dSwesolows 				xlate(ctx->i_ae, "super", xchip_tbl);
7377c478bd9Sstevel@tonic-gate 				continue;
7387c478bd9Sstevel@tonic-gate 			}
7397c478bd9Sstevel@tonic-gate #endif	/* __sparc */
7407c478bd9Sstevel@tonic-gate 		}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		switch ((c = arg[1])) {
7437c478bd9Sstevel@tonic-gate 		case '_':
744aa9ef484SJohn Levon 			if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) ||
745aa9ef484SJohn Levon 			    (strncmp(arg, "-_gcc=", 6) == 0) ||
746aa9ef484SJohn Levon 			    (strncmp(arg, "-_gnu=", 6) == 0)) {
747aa9ef484SJohn Levon 				newae(ctx->i_ae, strchr(arg, '=') + 1);
748aa9ef484SJohn Levon 			}
7497c478bd9Sstevel@tonic-gate 			break;
7507c478bd9Sstevel@tonic-gate 		case '#':
7517c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
75280ab886dSwesolows 				newae(ctx->i_ae, "-v");
7537c478bd9Sstevel@tonic-gate 				break;
7547c478bd9Sstevel@tonic-gate 			}
7557c478bd9Sstevel@tonic-gate 			error(arg);
7567c478bd9Sstevel@tonic-gate 			break;
757e1bf37b1SRichard Lowe 		case 'f':
758e1bf37b1SRichard Lowe 			if ((strcmp(arg, "-fpic") == 0) ||
759e1bf37b1SRichard Lowe 			    (strcmp(arg, "-fPIC") == 0)) {
760e1bf37b1SRichard Lowe 				newae(ctx->i_ae, arg);
761e1bf37b1SRichard Lowe 				break;
762e1bf37b1SRichard Lowe 			}
763e1bf37b1SRichard Lowe 			error(arg);
764e1bf37b1SRichard Lowe 			break;
7657c478bd9Sstevel@tonic-gate 		case 'g':
76680ab886dSwesolows 			newae(ctx->i_ae, "-gdwarf-2");
7677c478bd9Sstevel@tonic-gate 			break;
7687c478bd9Sstevel@tonic-gate 		case 'E':
7697c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
77080ab886dSwesolows 				newae(ctx->i_ae, "-xc");
77180ab886dSwesolows 				newae(ctx->i_ae, arg);
77280ab886dSwesolows 				op = CW_O_PREPROCESS;
7737c478bd9Sstevel@tonic-gate 				nolibc = 1;
7747c478bd9Sstevel@tonic-gate 				break;
7757c478bd9Sstevel@tonic-gate 			}
7767c478bd9Sstevel@tonic-gate 			error(arg);
7777c478bd9Sstevel@tonic-gate 			break;
7787c478bd9Sstevel@tonic-gate 		case 'c':
7797c478bd9Sstevel@tonic-gate 		case 'S':
78080ab886dSwesolows 			if (arglen == 1) {
78180ab886dSwesolows 				op = CW_O_COMPILE;
7827c478bd9Sstevel@tonic-gate 				nolibc = 1;
78380ab886dSwesolows 			}
7847c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
7857c478bd9Sstevel@tonic-gate 		case 'C':
7867c478bd9Sstevel@tonic-gate 		case 'H':
7877c478bd9Sstevel@tonic-gate 		case 'p':
7887c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
78980ab886dSwesolows 				newae(ctx->i_ae, arg);
7907c478bd9Sstevel@tonic-gate 				break;
7917c478bd9Sstevel@tonic-gate 			}
7927c478bd9Sstevel@tonic-gate 			error(arg);
7937c478bd9Sstevel@tonic-gate 			break;
7947c478bd9Sstevel@tonic-gate 		case 'A':
7957c478bd9Sstevel@tonic-gate 		case 'h':
7967c478bd9Sstevel@tonic-gate 		case 'I':
7977c478bd9Sstevel@tonic-gate 		case 'i':
7987c478bd9Sstevel@tonic-gate 		case 'L':
7997c478bd9Sstevel@tonic-gate 		case 'l':
8007c478bd9Sstevel@tonic-gate 		case 'R':
8017c478bd9Sstevel@tonic-gate 		case 'U':
8027c478bd9Sstevel@tonic-gate 		case 'u':
8037c478bd9Sstevel@tonic-gate 		case 'w':
80480ab886dSwesolows 			newae(ctx->i_ae, arg);
80580ab886dSwesolows 			break;
80680ab886dSwesolows 		case 'o':
80780ab886dSwesolows 			seen_o = 1;
80880ab886dSwesolows 			if (arglen == 1) {
80980ab886dSwesolows 				in_output = 1;
81080ab886dSwesolows 				newae(ctx->i_ae, arg);
81180ab886dSwesolows 			} else if (ctx->i_flags & CW_F_SHADOW) {
81280ab886dSwesolows 				newae(ctx->i_ae, "-o");
813*88e61e85SRichard Lowe 				newae(ctx->i_ae, discard_file_name(ctx, arg));
81480ab886dSwesolows 			} else {
81580ab886dSwesolows 				newae(ctx->i_ae, arg);
81680ab886dSwesolows 			}
8177c478bd9Sstevel@tonic-gate 			break;
8187c478bd9Sstevel@tonic-gate 		case 'D':
81980ab886dSwesolows 			newae(ctx->i_ae, arg);
8207c478bd9Sstevel@tonic-gate 			/*
8217c478bd9Sstevel@tonic-gate 			 * XXX	Clearly a hack ... do we need _KADB too?
8227c478bd9Sstevel@tonic-gate 			 */
8237c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-D_KERNEL") == 0 ||
8247c478bd9Sstevel@tonic-gate 			    strcmp(arg, "-D_BOOT") == 0)
82580ab886dSwesolows 				newae(ctx->i_ae, "-ffreestanding");
8267c478bd9Sstevel@tonic-gate 			break;
8277c478bd9Sstevel@tonic-gate 		case 'd':
8287c478bd9Sstevel@tonic-gate 			if (arglen == 2) {
8297c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-dy") == 0) {
83080ab886dSwesolows 					newae(ctx->i_ae, "-Wl,-dy");
8317c478bd9Sstevel@tonic-gate 					break;
8327c478bd9Sstevel@tonic-gate 				}
8337c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-dn") == 0) {
83480ab886dSwesolows 					newae(ctx->i_ae, "-Wl,-dn");
8357c478bd9Sstevel@tonic-gate 					break;
8367c478bd9Sstevel@tonic-gate 				}
8377c478bd9Sstevel@tonic-gate 			}
8387c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-dalign") == 0) {
8397c478bd9Sstevel@tonic-gate 				/*
8407c478bd9Sstevel@tonic-gate 				 * -dalign forces alignment in some cases;
8417c478bd9Sstevel@tonic-gate 				 * gcc does not need any flag to do this.
8427c478bd9Sstevel@tonic-gate 				 */
8437c478bd9Sstevel@tonic-gate 				break;
8447c478bd9Sstevel@tonic-gate 			}
8457c478bd9Sstevel@tonic-gate 			error(arg);
8467c478bd9Sstevel@tonic-gate 			break;
8477c478bd9Sstevel@tonic-gate 		case 'e':
8487c478bd9Sstevel@tonic-gate 			if (strcmp(arg,
8497c478bd9Sstevel@tonic-gate 			    "-erroff=E_EMPTY_TRANSLATION_UNIT") == 0) {
8507c478bd9Sstevel@tonic-gate 				/*
8517c478bd9Sstevel@tonic-gate 				 * Accept but ignore this -- gcc doesn't
8527c478bd9Sstevel@tonic-gate 				 * seem to complain about empty translation
8537c478bd9Sstevel@tonic-gate 				 * units
8547c478bd9Sstevel@tonic-gate 				 */
8557c478bd9Sstevel@tonic-gate 				break;
8567c478bd9Sstevel@tonic-gate 			}
8577c478bd9Sstevel@tonic-gate 			/* XX64 -- ignore all -erroff= options, for now */
8587c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "-erroff=", 8) == 0)
8597c478bd9Sstevel@tonic-gate 				break;
8607c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-errtags=yes") == 0) {
86180ab886dSwesolows 				warnings(ctx->i_ae);
8627c478bd9Sstevel@tonic-gate 				break;
8637c478bd9Sstevel@tonic-gate 			}
8647c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-errwarn=%all") == 0) {
86580ab886dSwesolows 				newae(ctx->i_ae, "-Werror");
8667c478bd9Sstevel@tonic-gate 				break;
8677c478bd9Sstevel@tonic-gate 			}
8687c478bd9Sstevel@tonic-gate 			error(arg);
8697c478bd9Sstevel@tonic-gate 			break;
8707c478bd9Sstevel@tonic-gate 		case 'G':
87180ab886dSwesolows 			newae(ctx->i_ae, "-shared");
8727c478bd9Sstevel@tonic-gate 			nolibc = 1;
8737c478bd9Sstevel@tonic-gate 			break;
8747c478bd9Sstevel@tonic-gate 		case 'k':
8757c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-keeptmp") == 0) {
87680ab886dSwesolows 				newae(ctx->i_ae, "-save-temps");
8777c478bd9Sstevel@tonic-gate 				break;
8787c478bd9Sstevel@tonic-gate 			}
8797c478bd9Sstevel@tonic-gate 			error(arg);
8807c478bd9Sstevel@tonic-gate 			break;
8817c478bd9Sstevel@tonic-gate 		case 'm':
8827c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-mt") == 0) {
88380ab886dSwesolows 				newae(ctx->i_ae, "-D_REENTRANT");
8847c478bd9Sstevel@tonic-gate 				break;
8857c478bd9Sstevel@tonic-gate 			}
886e521259dSpetede 			if (strcmp(arg, "-m64") == 0) {
887e521259dSpetede 				newae(ctx->i_ae, "-m64");
888e521259dSpetede #if defined(__x86)
889e521259dSpetede 				newae(ctx->i_ae, "-mtune=opteron");
890e521259dSpetede #endif
891e521259dSpetede 				mflag |= M64;
892e521259dSpetede 				break;
893e521259dSpetede 			}
894e521259dSpetede 			if (strcmp(arg, "-m32") == 0) {
895e521259dSpetede 				newae(ctx->i_ae, "-m32");
896e521259dSpetede 				mflag |= M32;
897e521259dSpetede 				break;
898e521259dSpetede 			}
8997c478bd9Sstevel@tonic-gate 			error(arg);
9007c478bd9Sstevel@tonic-gate 			break;
9017c478bd9Sstevel@tonic-gate 		case 'B':	/* linker options */
9027c478bd9Sstevel@tonic-gate 		case 'M':
9037c478bd9Sstevel@tonic-gate 		case 'z':
9047c478bd9Sstevel@tonic-gate 			{
9057c478bd9Sstevel@tonic-gate 				char *opt;
9067c478bd9Sstevel@tonic-gate 				size_t len;
9077c478bd9Sstevel@tonic-gate 				char *s;
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 				if (arglen == 1) {
91080ab886dSwesolows 					opt = *++ctx->i_oldargv;
9117c478bd9Sstevel@tonic-gate 					if (opt == NULL || *opt == '\0')
9127c478bd9Sstevel@tonic-gate 						error(arg);
91380ab886dSwesolows 					ctx->i_oldargc--;
9147c478bd9Sstevel@tonic-gate 				} else {
9157c478bd9Sstevel@tonic-gate 					opt = arg + 2;
9167c478bd9Sstevel@tonic-gate 				}
9177c478bd9Sstevel@tonic-gate 				len = strlen(opt) + 7;
91880ab886dSwesolows 				if ((s = malloc(len)) == NULL)
91980ab886dSwesolows 					nomem();
9207c478bd9Sstevel@tonic-gate 				(void) snprintf(s, len, "-Wl,-%c%s", c, opt);
92180ab886dSwesolows 				newae(ctx->i_ae, s);
9227c478bd9Sstevel@tonic-gate 				free(s);
9237c478bd9Sstevel@tonic-gate 			}
9247c478bd9Sstevel@tonic-gate 			break;
9257c478bd9Sstevel@tonic-gate 		case 'O':
9267c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
92780ab886dSwesolows 				newae(ctx->i_ae, "-O");
9287c478bd9Sstevel@tonic-gate 				break;
9297c478bd9Sstevel@tonic-gate 			}
9307c478bd9Sstevel@tonic-gate 			error(arg);
9317c478bd9Sstevel@tonic-gate 			break;
9327c478bd9Sstevel@tonic-gate 		case 'P':
9337c478bd9Sstevel@tonic-gate 			/*
9347c478bd9Sstevel@tonic-gate 			 * We could do '-E -o filename.i', but that's hard,
9357c478bd9Sstevel@tonic-gate 			 * and we don't need it for the case that's triggering
9367c478bd9Sstevel@tonic-gate 			 * this addition.  We'll require the user to specify
9377c478bd9Sstevel@tonic-gate 			 * -o in the Makefile.  If they don't they'll find out
9387c478bd9Sstevel@tonic-gate 			 * in a hurry.
9397c478bd9Sstevel@tonic-gate 			 */
94080ab886dSwesolows 			newae(ctx->i_ae, "-E");
94180ab886dSwesolows 			op = CW_O_PREPROCESS;
9427c478bd9Sstevel@tonic-gate 			nolibc = 1;
9437c478bd9Sstevel@tonic-gate 			break;
9447c478bd9Sstevel@tonic-gate 		case 's':
9457c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
94680ab886dSwesolows 				newae(ctx->i_ae, "-Wl,-s");
9477c478bd9Sstevel@tonic-gate 				break;
9487c478bd9Sstevel@tonic-gate 			}
9497c478bd9Sstevel@tonic-gate 			error(arg);
9507c478bd9Sstevel@tonic-gate 			break;
951159cf8a6Swesolows 		case 't':
952159cf8a6Swesolows 			if (arglen == 1) {
95380ab886dSwesolows 				newae(ctx->i_ae, "-Wl,-t");
954159cf8a6Swesolows 				break;
955159cf8a6Swesolows 			}
956159cf8a6Swesolows 			error(arg);
957159cf8a6Swesolows 			break;
9587c478bd9Sstevel@tonic-gate 		case 'V':
9597c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
96080ab886dSwesolows 				ctx->i_flags &= ~CW_F_ECHO;
96180ab886dSwesolows 				newae(ctx->i_ae, "--version");
9627c478bd9Sstevel@tonic-gate 				break;
9637c478bd9Sstevel@tonic-gate 			}
9647c478bd9Sstevel@tonic-gate 			error(arg);
9657c478bd9Sstevel@tonic-gate 			break;
9667c478bd9Sstevel@tonic-gate 		case 'v':
9677c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
96880ab886dSwesolows 				warnings(ctx->i_ae);
9697c478bd9Sstevel@tonic-gate 				break;
9707c478bd9Sstevel@tonic-gate 			}
9717c478bd9Sstevel@tonic-gate 			error(arg);
9727c478bd9Sstevel@tonic-gate 			break;
9737c478bd9Sstevel@tonic-gate 		case 'W':
9747c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "-Wp,-xc99", 9) == 0) {
9757c478bd9Sstevel@tonic-gate 				/*
9767c478bd9Sstevel@tonic-gate 				 * gcc's preprocessor will accept c99
9777c478bd9Sstevel@tonic-gate 				 * regardless, so accept and ignore.
9787c478bd9Sstevel@tonic-gate 				 */
9797c478bd9Sstevel@tonic-gate 				break;
9807c478bd9Sstevel@tonic-gate 			}
9817c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "-Wa,", 4) == 0 ||
9827c478bd9Sstevel@tonic-gate 			    strncmp(arg, "-Wp,", 4) == 0 ||
9837c478bd9Sstevel@tonic-gate 			    strncmp(arg, "-Wl,", 4) == 0) {
98480ab886dSwesolows 				newae(ctx->i_ae, arg);
9857c478bd9Sstevel@tonic-gate 				break;
9867c478bd9Sstevel@tonic-gate 			}
9874afe1ab8Sesaxe 			if (strcmp(arg, "-W0,-noglobal") == 0 ||
9884afe1ab8Sesaxe 			    strcmp(arg, "-W0,-xglobalstatic") == 0) {
9897c478bd9Sstevel@tonic-gate 				/*
9907c478bd9Sstevel@tonic-gate 				 * gcc doesn't prefix local symbols
9917c478bd9Sstevel@tonic-gate 				 * in debug mode, so this is not needed.
9927c478bd9Sstevel@tonic-gate 				 */
9937c478bd9Sstevel@tonic-gate 				break;
9947c478bd9Sstevel@tonic-gate 			}
9957c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-W0,-Lt") == 0) {
9967c478bd9Sstevel@tonic-gate 				/*
9977c478bd9Sstevel@tonic-gate 				 * Generate tests at the top of loops.
9987c478bd9Sstevel@tonic-gate 				 * There is no direct gcc equivalent, ignore.
9997c478bd9Sstevel@tonic-gate 				 */
10007c478bd9Sstevel@tonic-gate 				break;
10017c478bd9Sstevel@tonic-gate 			}
10028c1a3716Ssherrym 			if (strcmp(arg, "-W0,-xdbggen=no%usedonly") == 0) {
100380ab886dSwesolows 				newae(ctx->i_ae,
100480ab886dSwesolows 				    "-fno-eliminate-unused-debug-symbols");
100580ab886dSwesolows 				newae(ctx->i_ae,
100680ab886dSwesolows 				    "-fno-eliminate-unused-debug-types");
10078c1a3716Ssherrym 				break;
10088c1a3716Ssherrym 			}
10099dd82889Spetede 			if (strcmp(arg, "-W2,-xwrap_int") == 0) {
10109dd82889Spetede 				/*
10119dd82889Spetede 				 * Use the legacy behaviour (pre-SS11)
10129dd82889Spetede 				 * for integer wrapping.
10139dd82889Spetede 				 * gcc does not need this.
10149dd82889Spetede 				 */
10159dd82889Spetede 				break;
10169dd82889Spetede 			}
10177c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-Wd,-xsafe=unboundsym") == 0) {
10187c478bd9Sstevel@tonic-gate 				/*
10197c478bd9Sstevel@tonic-gate 				 * Prevents optimizing away checks for
10207c478bd9Sstevel@tonic-gate 				 * unbound weak symbol addresses.  gcc does
10217c478bd9Sstevel@tonic-gate 				 * not do this, so it's not needed.
10227c478bd9Sstevel@tonic-gate 				 */
10237c478bd9Sstevel@tonic-gate 				break;
10247c478bd9Sstevel@tonic-gate 			}
10257c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "-Wc,-xcode=", 11) == 0) {
102680ab886dSwesolows 				xlate(ctx->i_ae, arg + 11, xcode_tbl);
10277c478bd9Sstevel@tonic-gate 				break;
10287c478bd9Sstevel@tonic-gate 			}
10297c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "-Wc,-Qiselect", 13) == 0) {
10307c478bd9Sstevel@tonic-gate 				/*
10317c478bd9Sstevel@tonic-gate 				 * Prevents insertion of register symbols.
10327c478bd9Sstevel@tonic-gate 				 * gcc doesn't do this, so ignore it.
10337c478bd9Sstevel@tonic-gate 				 */
10347c478bd9Sstevel@tonic-gate 				break;
10357c478bd9Sstevel@tonic-gate 			}
10369dd82889Spetede 			if (strcmp(arg, "-Wc,-Qassembler-ounrefsym=0") == 0) {
10379dd82889Spetede 				/*
10389dd82889Spetede 				 * Prevents optimizing away of static variables.
10399dd82889Spetede 				 * gcc does not do this, so it's not needed.
10409dd82889Spetede 				 */
10419dd82889Spetede 				break;
10429dd82889Spetede 			}
10437c478bd9Sstevel@tonic-gate #if defined(__x86)
1044d430274bSsherrym 			if (strcmp(arg, "-Wu,-save_args") == 0) {
104580ab886dSwesolows 				newae(ctx->i_ae, "-msave-args");
1046d430274bSsherrym 				break;
1047d430274bSsherrym 			}
10487c478bd9Sstevel@tonic-gate #endif	/* __x86 */
10497c478bd9Sstevel@tonic-gate 			error(arg);
10507c478bd9Sstevel@tonic-gate 			break;
10517c478bd9Sstevel@tonic-gate 		case 'X':
10527c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-Xa") == 0 ||
10537c478bd9Sstevel@tonic-gate 			    strcmp(arg, "-Xt") == 0) {
10547c478bd9Sstevel@tonic-gate 				break;
10557c478bd9Sstevel@tonic-gate 			}
10567c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-Xs") == 0) {
105780ab886dSwesolows 				Xsmode(ctx->i_ae);
10587c478bd9Sstevel@tonic-gate 				break;
10597c478bd9Sstevel@tonic-gate 			}
10607c478bd9Sstevel@tonic-gate 			error(arg);
10617c478bd9Sstevel@tonic-gate 			break;
10627c478bd9Sstevel@tonic-gate 		case 'x':
10637c478bd9Sstevel@tonic-gate 			if (arglen == 1)
10647c478bd9Sstevel@tonic-gate 				error(arg);
10657c478bd9Sstevel@tonic-gate 			switch (arg[2]) {
10667c478bd9Sstevel@tonic-gate 			case 'a':
10677c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xarch=", 7) == 0) {
1068e521259dSpetede 					mflag |= xlate_xtb(ctx->i_ae, arg + 7);
10697c478bd9Sstevel@tonic-gate 					break;
10707c478bd9Sstevel@tonic-gate 				}
10717c478bd9Sstevel@tonic-gate 				error(arg);
10727c478bd9Sstevel@tonic-gate 				break;
107302e56f3fSwesolows 			case 'b':
107402e56f3fSwesolows 				if (strncmp(arg, "-xbuiltin=", 10) == 0) {
107502e56f3fSwesolows 					if (strcmp(arg + 10, "%all"))
107680ab886dSwesolows 						newae(ctx->i_ae, "-fbuiltin");
107702e56f3fSwesolows 					break;
107802e56f3fSwesolows 				}
107902e56f3fSwesolows 				error(arg);
108002e56f3fSwesolows 				break;
10817c478bd9Sstevel@tonic-gate 			case 'C':
10827c478bd9Sstevel@tonic-gate 				/* Accept C++ style comments -- ignore */
10837c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-xCC") == 0)
10847c478bd9Sstevel@tonic-gate 					break;
10857c478bd9Sstevel@tonic-gate 				error(arg);
10867c478bd9Sstevel@tonic-gate 				break;
10877c478bd9Sstevel@tonic-gate 			case 'c':
10887c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xc99=%all", 10) == 0) {
108980ab886dSwesolows 					newae(ctx->i_ae, "-std=gnu99");
10907c478bd9Sstevel@tonic-gate 					break;
10917c478bd9Sstevel@tonic-gate 				}
10927c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xc99=%none", 11) == 0) {
109380ab886dSwesolows 					newae(ctx->i_ae, "-std=gnu89");
10947c478bd9Sstevel@tonic-gate 					break;
10957c478bd9Sstevel@tonic-gate 				}
10967c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xchip=", 7) == 0) {
109780ab886dSwesolows 					xlate(ctx->i_ae, arg + 7, xchip_tbl);
10987c478bd9Sstevel@tonic-gate 					break;
10997c478bd9Sstevel@tonic-gate 				}
11007c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xcode=", 7) == 0) {
110180ab886dSwesolows 					xlate(ctx->i_ae, arg + 7, xcode_tbl);
11027c478bd9Sstevel@tonic-gate 					break;
11037c478bd9Sstevel@tonic-gate 				}
1104159cf8a6Swesolows 				if (strncmp(arg, "-xcrossfile", 11) == 0)
1105159cf8a6Swesolows 					break;
11067c478bd9Sstevel@tonic-gate 				error(arg);
11077c478bd9Sstevel@tonic-gate 				break;
11087c478bd9Sstevel@tonic-gate 			case 'd':
11098c1a3716Ssherrym 				if (strncmp(arg, "-xdebugformat=", 14) == 0)
11108c1a3716Ssherrym 					break;
11117c478bd9Sstevel@tonic-gate 				error(arg);
11127c478bd9Sstevel@tonic-gate 				break;
11137c478bd9Sstevel@tonic-gate 			case 'F':
1114f795e658Srie 				/*
1115f795e658Srie 				 * Compile for mapfile reordering, or unused
1116f795e658Srie 				 * section elimination, syntax can be -xF or
1117f795e658Srie 				 * more complex, like -xF=%all -- ignore.
1118f795e658Srie 				 */
1119f795e658Srie 				if (strncmp(arg, "-xF", 3) == 0)
11207c478bd9Sstevel@tonic-gate 					break;
11217c478bd9Sstevel@tonic-gate 				error(arg);
11227c478bd9Sstevel@tonic-gate 				break;
11237c478bd9Sstevel@tonic-gate 			case 'i':
11247c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xinline", 8) == 0)
11257c478bd9Sstevel@tonic-gate 					/* No inlining; ignore */
11267c478bd9Sstevel@tonic-gate 					break;
11277c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-xildon") == 0 ||
11287c478bd9Sstevel@tonic-gate 				    strcmp(arg, "-xildoff") == 0)
11297c478bd9Sstevel@tonic-gate 					/* No incremental linking; ignore */
11307c478bd9Sstevel@tonic-gate 					break;
11317c478bd9Sstevel@tonic-gate 				error(arg);
11327c478bd9Sstevel@tonic-gate 				break;
113354836668Spetede #if defined(__x86)
113454836668Spetede 			case 'm':
113554836668Spetede 				if (strcmp(arg, "-xmodel=kernel") == 0) {
113654836668Spetede 					newae(ctx->i_ae, "-ffreestanding");
113754836668Spetede 					newae(ctx->i_ae, "-mno-red-zone");
113854836668Spetede 					model = "-mcmodel=kernel";
113954836668Spetede 					nolibc = 1;
114054836668Spetede 					break;
114154836668Spetede 				}
114254836668Spetede 				error(arg);
114354836668Spetede 				break;
114454836668Spetede #endif	/* __x86 */
11457c478bd9Sstevel@tonic-gate 			case 'O':
11467c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xO", 3) == 0) {
11477c478bd9Sstevel@tonic-gate 					size_t len = strlen(arg);
1148538ff303SToomas Soome 					char *s = NULL;
11497c478bd9Sstevel@tonic-gate 					int c = *(arg + 3);
11507c478bd9Sstevel@tonic-gate 					int level;
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 					if (len != 4 || !isdigit(c))
11537c478bd9Sstevel@tonic-gate 						error(arg);
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 					level = atoi(arg + 3);
11567c478bd9Sstevel@tonic-gate 					if (level > 5)
11577c478bd9Sstevel@tonic-gate 						error(arg);
11587c478bd9Sstevel@tonic-gate 					if (level >= 2) {
11597c478bd9Sstevel@tonic-gate 						/*
11607c478bd9Sstevel@tonic-gate 						 * For gcc-3.4.x at -O2 we
11617c478bd9Sstevel@tonic-gate 						 * need to disable optimizations
11627c478bd9Sstevel@tonic-gate 						 * that break ON.
11637c478bd9Sstevel@tonic-gate 						 */
116480ab886dSwesolows 						optim_disable(ctx->i_ae, level);
11657c478bd9Sstevel@tonic-gate 						/*
11667c478bd9Sstevel@tonic-gate 						 * limit -xO3 to -O2 as well.
11677c478bd9Sstevel@tonic-gate 						 */
11687c478bd9Sstevel@tonic-gate 						level = 2;
11697c478bd9Sstevel@tonic-gate 					}
1170538ff303SToomas Soome 					if (asprintf(&s, "-O%d", level) == -1)
1171538ff303SToomas Soome 						nomem();
117280ab886dSwesolows 					newae(ctx->i_ae, s);
11737c478bd9Sstevel@tonic-gate 					free(s);
11747c478bd9Sstevel@tonic-gate 					break;
11757c478bd9Sstevel@tonic-gate 				}
11767c478bd9Sstevel@tonic-gate 				error(arg);
11777c478bd9Sstevel@tonic-gate 				break;
11787c478bd9Sstevel@tonic-gate 			case 'r':
11797c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xregs=", 7) == 0) {
118080ab886dSwesolows 					xlate(ctx->i_ae, arg + 7, xregs_tbl);
11817c478bd9Sstevel@tonic-gate 					break;
11827c478bd9Sstevel@tonic-gate 				}
11837c478bd9Sstevel@tonic-gate 				error(arg);
11847c478bd9Sstevel@tonic-gate 				break;
11857c478bd9Sstevel@tonic-gate 			case 's':
11867c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-xs") == 0 ||
11877c478bd9Sstevel@tonic-gate 				    strcmp(arg, "-xspace") == 0 ||
11887c478bd9Sstevel@tonic-gate 				    strcmp(arg, "-xstrconst") == 0)
11897c478bd9Sstevel@tonic-gate 					break;
11907c478bd9Sstevel@tonic-gate 				error(arg);
11917c478bd9Sstevel@tonic-gate 				break;
11927c478bd9Sstevel@tonic-gate 			case 't':
11937c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xtarget=", 9) == 0) {
119480ab886dSwesolows 					xlate(ctx->i_ae, arg + 9, xtarget_tbl);
11957c478bd9Sstevel@tonic-gate 					break;
11967c478bd9Sstevel@tonic-gate 				}
11977c478bd9Sstevel@tonic-gate 				error(arg);
11987c478bd9Sstevel@tonic-gate 				break;
11997c478bd9Sstevel@tonic-gate 			case 'e':
12007c478bd9Sstevel@tonic-gate 			case 'h':
12017c478bd9Sstevel@tonic-gate 			case 'l':
12027c478bd9Sstevel@tonic-gate 			default:
12037c478bd9Sstevel@tonic-gate 				error(arg);
12047c478bd9Sstevel@tonic-gate 				break;
12057c478bd9Sstevel@tonic-gate 			}
12067c478bd9Sstevel@tonic-gate 			break;
12077c478bd9Sstevel@tonic-gate 		case 'Y':
12087c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
120980ab886dSwesolows 				if ((arg = *++ctx->i_oldargv) == NULL ||
121080ab886dSwesolows 				    *arg == '\0')
12117c478bd9Sstevel@tonic-gate 					error("-Y");
121280ab886dSwesolows 				ctx->i_oldargc--;
12137c478bd9Sstevel@tonic-gate 				arglen = strlen(arg + 1);
12147c478bd9Sstevel@tonic-gate 			} else {
12157c478bd9Sstevel@tonic-gate 				arg += 2;
12167c478bd9Sstevel@tonic-gate 			}
12177c478bd9Sstevel@tonic-gate 			/* Just ignore -YS,... for now */
12187c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "S,", 2) == 0)
12197c478bd9Sstevel@tonic-gate 				break;
12207c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "l,", 2) == 0) {
12217c478bd9Sstevel@tonic-gate 				char *s = strdup(arg);
12227c478bd9Sstevel@tonic-gate 				s[0] = '-';
12237c478bd9Sstevel@tonic-gate 				s[1] = 'B';
122480ab886dSwesolows 				newae(ctx->i_ae, s);
12257c478bd9Sstevel@tonic-gate 				free(s);
12267c478bd9Sstevel@tonic-gate 				break;
12277c478bd9Sstevel@tonic-gate 			}
12287c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "I,", 2) == 0) {
12297c478bd9Sstevel@tonic-gate 				char *s = strdup(arg);
12307c478bd9Sstevel@tonic-gate 				s[0] = '-';
12317c478bd9Sstevel@tonic-gate 				s[1] = 'I';
123280ab886dSwesolows 				newae(ctx->i_ae, "-nostdinc");
123380ab886dSwesolows 				newae(ctx->i_ae, s);
12347c478bd9Sstevel@tonic-gate 				free(s);
12357c478bd9Sstevel@tonic-gate 				break;
12367c478bd9Sstevel@tonic-gate 			}
12377c478bd9Sstevel@tonic-gate 			error(arg);
12387c478bd9Sstevel@tonic-gate 			break;
12397c478bd9Sstevel@tonic-gate 		case 'Q':
12407c478bd9Sstevel@tonic-gate 			/*
12417c478bd9Sstevel@tonic-gate 			 * We could map -Qy into -Wl,-Qy etc.
12427c478bd9Sstevel@tonic-gate 			 */
12437c478bd9Sstevel@tonic-gate 		default:
12447c478bd9Sstevel@tonic-gate 			error(arg);
12457c478bd9Sstevel@tonic-gate 			break;
12467c478bd9Sstevel@tonic-gate 		}
12477c478bd9Sstevel@tonic-gate 	}
12487c478bd9Sstevel@tonic-gate 
1249aa9ef484SJohn Levon 	free(nameflag);
1250aa9ef484SJohn Levon 
1251*88e61e85SRichard Lowe 	/*
1252*88e61e85SRichard Lowe 	 * When compiling multiple source files in a single invocation some
1253*88e61e85SRichard Lowe 	 * compilers output objects into the current directory with
1254*88e61e85SRichard Lowe 	 * predictable and conventional names.
1255*88e61e85SRichard Lowe 	 *
1256*88e61e85SRichard Lowe 	 * We prevent any attempt to compile multiple files at once so that
1257*88e61e85SRichard Lowe 	 * any such objects created by a shadow can't escape into a later
1258*88e61e85SRichard Lowe 	 * link-edit.
1259*88e61e85SRichard Lowe 	 */
1260*88e61e85SRichard Lowe 	if (c_files > 1 && op != CW_O_PREPROCESS) {
1261aa9ef484SJohn Levon 		errx(2, "multiple source files are "
1262aa9ef484SJohn Levon 		    "allowed only with -E or -P");
126380ab886dSwesolows 	}
1264e521259dSpetede 
1265e521259dSpetede 	/*
1266e521259dSpetede 	 * Make sure that we do not have any unintended interactions between
1267e521259dSpetede 	 * the xarch options passed in and the version of the Studio compiler
1268e521259dSpetede 	 * used.
1269e521259dSpetede 	 */
1270e521259dSpetede 	if ((mflag & (SS11|SS12)) == (SS11|SS12)) {
1271aa9ef484SJohn Levon 		errx(2,
1272e521259dSpetede 		    "Conflicting \"-xarch=\" flags (both Studio 11 and 12)\n");
1273e521259dSpetede 	}
1274e521259dSpetede 
1275e521259dSpetede 	switch (mflag) {
1276e521259dSpetede 	case 0:
1277e521259dSpetede 		/* FALLTHROUGH */
1278e521259dSpetede 	case M32:
1279e521259dSpetede #if defined(__sparc)
1280e521259dSpetede 		/*
1281e521259dSpetede 		 * Only -m32 is defined and so put in the missing xarch
1282e521259dSpetede 		 * translation.
1283e521259dSpetede 		 */
1284e521259dSpetede 		newae(ctx->i_ae, "-mcpu=v8");
1285e521259dSpetede 		newae(ctx->i_ae, "-mno-v8plus");
1286e521259dSpetede #endif
1287e521259dSpetede 		break;
1288e521259dSpetede 	case M64:
1289e521259dSpetede #if defined(__sparc)
1290e521259dSpetede 		/*
1291e521259dSpetede 		 * Only -m64 is defined and so put in the missing xarch
1292e521259dSpetede 		 * translation.
1293e521259dSpetede 		 */
1294e521259dSpetede 		newae(ctx->i_ae, "-mcpu=v9");
1295e521259dSpetede #endif
1296e521259dSpetede 		break;
1297e521259dSpetede 	case SS12:
1298e521259dSpetede #if defined(__sparc)
1299e521259dSpetede 		/* no -m32/-m64 flag used - this is an error for sparc builds */
1300e521259dSpetede 		(void) fprintf(stderr, "No -m32/-m64 flag defined\n");
1301e521259dSpetede 		exit(2);
1302e521259dSpetede #endif
1303e521259dSpetede 		break;
1304e521259dSpetede 	case SS11:
1305e521259dSpetede 		/* FALLTHROUGH */
1306e521259dSpetede 	case (SS11|M32):
1307e521259dSpetede 	case (SS11|M64):
1308e521259dSpetede 		break;
1309e521259dSpetede 	case (SS12|M32):
1310e521259dSpetede #if defined(__sparc)
1311e521259dSpetede 		/*
1312e521259dSpetede 		 * Need to add in further 32 bit options because with SS12
1313e521259dSpetede 		 * the xarch=sparcvis option can be applied to 32 or 64
1314e521259dSpetede 		 * bit, and so the translatation table (xtbl) cannot handle
1315e521259dSpetede 		 * that.
1316e521259dSpetede 		 */
1317e521259dSpetede 		newae(ctx->i_ae, "-mv8plus");
1318e521259dSpetede #endif
1319e521259dSpetede 		break;
1320e521259dSpetede 	case (SS12|M64):
1321e521259dSpetede 		break;
1322e521259dSpetede 	default:
1323e521259dSpetede 		(void) fprintf(stderr,
13247a6460b6Spetede 		    "Incompatible -xarch= and/or -m32/-m64 options used.\n");
1325e521259dSpetede 		exit(2);
1326e521259dSpetede 	}
1327aa9ef484SJohn Levon 
1328*88e61e85SRichard Lowe 	if (ctx->i_flags & CW_F_SHADOW) {
1329*88e61e85SRichard Lowe 		if (op == CW_O_PREPROCESS)
1330*88e61e85SRichard Lowe 			exit(0);
1331*88e61e85SRichard Lowe 		else if (op == CW_O_LINK && c_files == 0)
1332*88e61e85SRichard Lowe 			exit(0);
1333*88e61e85SRichard Lowe 	}
133480ab886dSwesolows 
1335e1bf37b1SRichard Lowe 	if (model != NULL)
133680ab886dSwesolows 		newae(ctx->i_ae, model);
13377c478bd9Sstevel@tonic-gate 	if (!nolibc)
133880ab886dSwesolows 		newae(ctx->i_ae, "-lc");
133980ab886dSwesolows 	if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
134080ab886dSwesolows 		newae(ctx->i_ae, "-o");
1341*88e61e85SRichard Lowe 		newae(ctx->i_ae, discard_file_name(ctx, NULL));
134280ab886dSwesolows 	}
134380ab886dSwesolows }
134480ab886dSwesolows 
13451f5207b7SJohn Levon static void
13461f5207b7SJohn Levon do_smatch(cw_ictx_t *ctx)
13471f5207b7SJohn Levon {
13481f5207b7SJohn Levon 	if (ctx->i_flags & CW_F_PROG) {
13491f5207b7SJohn Levon 		newae(ctx->i_ae, "--version");
13501f5207b7SJohn Levon 		return;
13511f5207b7SJohn Levon 	}
13521f5207b7SJohn Levon 
13531f5207b7SJohn Levon 	/*
13541f5207b7SJohn Levon 	 * Some sources shouldn't run smatch at all.
13551f5207b7SJohn Levon 	 */
13561f5207b7SJohn Levon 	for (int i = 0; i < ctx->i_oldargc; i++) {
13571f5207b7SJohn Levon 		char *arg = ctx->i_oldargv[i];
13581f5207b7SJohn Levon 
13591f5207b7SJohn Levon 		if (strcmp(arg, "-_smatch=off") == 0) {
13601f5207b7SJohn Levon 			ctx->i_flags &= ~ (CW_F_EXEC | CW_F_ECHO);
13611f5207b7SJohn Levon 			return;
13621f5207b7SJohn Levon 		}
13631f5207b7SJohn Levon 	}
13641f5207b7SJohn Levon 
13651f5207b7SJohn Levon 	/*
13661f5207b7SJohn Levon 	 * smatch can handle gcc's options.
13671f5207b7SJohn Levon 	 */
13681f5207b7SJohn Levon 	do_gcc(ctx);
13691f5207b7SJohn Levon }
13701f5207b7SJohn Levon 
137180ab886dSwesolows static void
137280ab886dSwesolows do_cc(cw_ictx_t *ctx)
137380ab886dSwesolows {
1374*88e61e85SRichard Lowe 	int in_output = 0, seen_o = 0, c_files = 0;
137580ab886dSwesolows 	cw_op_t op = CW_O_LINK;
1376aa9ef484SJohn Levon 	char *nameflag;
137780ab886dSwesolows 
13781912d2c4Swesolows 	if (ctx->i_flags & CW_F_PROG) {
13791912d2c4Swesolows 		newae(ctx->i_ae, "-V");
13801912d2c4Swesolows 		return;
13811912d2c4Swesolows 	}
13821912d2c4Swesolows 
1383aa9ef484SJohn Levon 	if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1)
1384aa9ef484SJohn Levon 		nomem();
1385aa9ef484SJohn Levon 
138680ab886dSwesolows 	while (--ctx->i_oldargc > 0) {
138780ab886dSwesolows 		char *arg = *++ctx->i_oldargv;
138880ab886dSwesolows 
1389aa9ef484SJohn Levon 		if (strncmp(arg, "-_CC=", 5) == 0) {
1390aa9ef484SJohn Levon 			newae(ctx->i_ae, strchr(arg, '=') + 1);
1391aa9ef484SJohn Levon 			continue;
1392aa9ef484SJohn Levon 		}
1393aa9ef484SJohn Levon 
139480ab886dSwesolows 		if (*arg != '-') {
1395*88e61e85SRichard Lowe 			if (!in_output && is_source_file(arg))
1396*88e61e85SRichard Lowe 				c_files++;
1397*88e61e85SRichard Lowe 
139880ab886dSwesolows 			if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) {
139980ab886dSwesolows 				newae(ctx->i_ae, arg);
140080ab886dSwesolows 			} else {
140180ab886dSwesolows 				in_output = 0;
1402*88e61e85SRichard Lowe 				newae(ctx->i_ae, discard_file_name(ctx, arg));
140380ab886dSwesolows 			}
140480ab886dSwesolows 			continue;
140580ab886dSwesolows 		}
140680ab886dSwesolows 		switch (*(arg + 1)) {
140780ab886dSwesolows 		case '_':
1408aa9ef484SJohn Levon 			if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) ||
1409aa9ef484SJohn Levon 			    (strncmp(arg, "-_cc=", 5) == 0) ||
1410aa9ef484SJohn Levon 			    (strncmp(arg, "-_sun=", 6) == 0)) {
1411aa9ef484SJohn Levon 				newae(ctx->i_ae, strchr(arg, '=') + 1);
141280ab886dSwesolows 			}
141380ab886dSwesolows 			break;
1414aa9ef484SJohn Levon 
141580ab886dSwesolows 		case 'V':
141680ab886dSwesolows 			ctx->i_flags &= ~CW_F_ECHO;
141780ab886dSwesolows 			newae(ctx->i_ae, arg);
141880ab886dSwesolows 			break;
141980ab886dSwesolows 		case 'o':
142080ab886dSwesolows 			seen_o = 1;
142180ab886dSwesolows 			if (strlen(arg) == 2) {
142280ab886dSwesolows 				in_output = 1;
142380ab886dSwesolows 				newae(ctx->i_ae, arg);
142480ab886dSwesolows 			} else if (ctx->i_flags & CW_F_SHADOW) {
142580ab886dSwesolows 				newae(ctx->i_ae, "-o");
1426*88e61e85SRichard Lowe 				newae(ctx->i_ae, discard_file_name(ctx, arg));
142780ab886dSwesolows 			} else {
142880ab886dSwesolows 				newae(ctx->i_ae, arg);
142980ab886dSwesolows 			}
143080ab886dSwesolows 			break;
143180ab886dSwesolows 		case 'c':
143280ab886dSwesolows 		case 'S':
14330bb3415fSrie 			if (strlen(arg) == 2)
14340bb3415fSrie 				op = CW_O_COMPILE;
143580ab886dSwesolows 			newae(ctx->i_ae, arg);
143680ab886dSwesolows 			break;
143780ab886dSwesolows 		case 'E':
143880ab886dSwesolows 		case 'P':
14390bb3415fSrie 			if (strlen(arg) == 2)
14400bb3415fSrie 				op = CW_O_PREPROCESS;
144180ab886dSwesolows 		/*FALLTHROUGH*/
144280ab886dSwesolows 		default:
144380ab886dSwesolows 			newae(ctx->i_ae, arg);
144480ab886dSwesolows 		}
144580ab886dSwesolows 	}
144680ab886dSwesolows 
1447aa9ef484SJohn Levon 	free(nameflag);
1448aa9ef484SJohn Levon 
1449*88e61e85SRichard Lowe 	/* See the comment on this same code in do_gcc() */
1450*88e61e85SRichard Lowe 	if (c_files > 1 && op != CW_O_PREPROCESS) {
1451*88e61e85SRichard Lowe 		errx(2, "multiple source files are "
1452*88e61e85SRichard Lowe 		    "allowed only with -E or -P");
1453*88e61e85SRichard Lowe 	}
1454*88e61e85SRichard Lowe 
1455*88e61e85SRichard Lowe 	if (ctx->i_flags & CW_F_SHADOW) {
1456*88e61e85SRichard Lowe 		if (op == CW_O_PREPROCESS)
1457*88e61e85SRichard Lowe 			exit(0);
1458*88e61e85SRichard Lowe 		else if (op == CW_O_LINK && c_files == 0)
1459*88e61e85SRichard Lowe 			exit(0);
1460*88e61e85SRichard Lowe 	}
146180ab886dSwesolows 
146280ab886dSwesolows 	if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
146380ab886dSwesolows 		newae(ctx->i_ae, "-o");
1464*88e61e85SRichard Lowe 		newae(ctx->i_ae, discard_file_name(ctx, NULL));
146580ab886dSwesolows 	}
14667c478bd9Sstevel@tonic-gate }
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate static void
146980ab886dSwesolows prepctx(cw_ictx_t *ctx)
14707c478bd9Sstevel@tonic-gate {
1471aa9ef484SJohn Levon 	newae(ctx->i_ae, ctx->i_compiler->c_path);
147280ab886dSwesolows 
14731912d2c4Swesolows 	if (ctx->i_flags & CW_F_PROG) {
14741912d2c4Swesolows 		(void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ?
1475aa9ef484SJohn Levon 		    "shadow" : "primary", ctx->i_compiler->c_path);
14761912d2c4Swesolows 		(void) fflush(stdout);
14771912d2c4Swesolows 	}
14781912d2c4Swesolows 
147980ab886dSwesolows 	if (!(ctx->i_flags & CW_F_XLATE))
148080ab886dSwesolows 		return;
148180ab886dSwesolows 
1482aa9ef484SJohn Levon 	switch (ctx->i_compiler->c_style) {
1483aa9ef484SJohn Levon 	case SUN:
148480ab886dSwesolows 		do_cc(ctx);
148580ab886dSwesolows 		break;
1486aa9ef484SJohn Levon 	case GNU:
148780ab886dSwesolows 		do_gcc(ctx);
148880ab886dSwesolows 		break;
14891f5207b7SJohn Levon 	case SMATCH:
14901f5207b7SJohn Levon 		do_smatch(ctx);
14911f5207b7SJohn Levon 		break;
149280ab886dSwesolows 	}
149380ab886dSwesolows }
149480ab886dSwesolows 
149580ab886dSwesolows static int
149680ab886dSwesolows invoke(cw_ictx_t *ctx)
149780ab886dSwesolows {
149880ab886dSwesolows 	char **newargv;
149980ab886dSwesolows 	int ac;
150080ab886dSwesolows 	struct ae *a;
150180ab886dSwesolows 
150280ab886dSwesolows 	if ((newargv = calloc(sizeof (*newargv), ctx->i_ae->ael_argc + 1)) ==
150380ab886dSwesolows 	    NULL)
150480ab886dSwesolows 		nomem();
150580ab886dSwesolows 
150680ab886dSwesolows 	if (ctx->i_flags & CW_F_ECHO)
150780ab886dSwesolows 		(void) fprintf(stderr, "+ ");
150880ab886dSwesolows 
150980ab886dSwesolows 	for (ac = 0, a = ctx->i_ae->ael_head; a; a = a->ae_next, ac++) {
151080ab886dSwesolows 		newargv[ac] = a->ae_arg;
151180ab886dSwesolows 		if (ctx->i_flags & CW_F_ECHO)
151280ab886dSwesolows 			(void) fprintf(stderr, "%s ", a->ae_arg);
151380ab886dSwesolows 		if (a == ctx->i_ae->ael_tail)
151480ab886dSwesolows 			break;
151580ab886dSwesolows 	}
151680ab886dSwesolows 
151780ab886dSwesolows 	if (ctx->i_flags & CW_F_ECHO) {
151880ab886dSwesolows 		(void) fprintf(stderr, "\n");
151980ab886dSwesolows 		(void) fflush(stderr);
152080ab886dSwesolows 	}
152180ab886dSwesolows 
152280ab886dSwesolows 	if (!(ctx->i_flags & CW_F_EXEC))
152380ab886dSwesolows 		return (0);
152480ab886dSwesolows 
15257c478bd9Sstevel@tonic-gate 	/*
1526aa9ef484SJohn Levon 	 * We must fix up the environment here so that the dependency files are
1527aa9ef484SJohn Levon 	 * not trampled by the shadow compiler. Also take care of GCC
1528aa9ef484SJohn Levon 	 * environment variables that will throw off gcc. This assumes a primary
1529aa9ef484SJohn Levon 	 * gcc.
15307c478bd9Sstevel@tonic-gate 	 */
153180ab886dSwesolows 	if ((ctx->i_flags & CW_F_SHADOW) &&
153280ab886dSwesolows 	    (unsetenv("SUNPRO_DEPENDENCIES") != 0 ||
1533aa9ef484SJohn Levon 	    unsetenv("DEPENDENCIES_OUTPUT") != 0 ||
1534aa9ef484SJohn Levon 	    unsetenv("GCC_ROOT") != 0)) {
153580ab886dSwesolows 		(void) fprintf(stderr, "error: environment setup failed: %s\n",
153680ab886dSwesolows 		    strerror(errno));
153780ab886dSwesolows 		return (-1);
153880ab886dSwesolows 	}
15397c478bd9Sstevel@tonic-gate 
154080ab886dSwesolows 	(void) execv(newargv[0], newargv);
1541aa9ef484SJohn Levon 	warn("couldn't run %s", newargv[0]);
15427c478bd9Sstevel@tonic-gate 
154380ab886dSwesolows 	return (-1);
154480ab886dSwesolows }
154580ab886dSwesolows 
154680ab886dSwesolows static int
154780ab886dSwesolows reap(cw_ictx_t *ctx)
154880ab886dSwesolows {
15491912d2c4Swesolows 	int status, ret = 0;
155080ab886dSwesolows 	char buf[1024];
155180ab886dSwesolows 	struct stat s;
155280ab886dSwesolows 
1553c3f177eaSPeter Dennis - Sustaining Engineer 	/*
1554c3f177eaSPeter Dennis - Sustaining Engineer 	 * Only wait for one specific child.
1555c3f177eaSPeter Dennis - Sustaining Engineer 	 */
1556c3f177eaSPeter Dennis - Sustaining Engineer 	if (ctx->i_pid <= 0)
1557c3f177eaSPeter Dennis - Sustaining Engineer 		return (-1);
1558c3f177eaSPeter Dennis - Sustaining Engineer 
155980ab886dSwesolows 	do {
1560c3f177eaSPeter Dennis - Sustaining Engineer 		if (waitpid(ctx->i_pid, &status, 0) < 0) {
1561aa9ef484SJohn Levon 			warn("cannot reap child");
1562c3f177eaSPeter Dennis - Sustaining Engineer 			return (-1);
1563c3f177eaSPeter Dennis - Sustaining Engineer 		}
15641912d2c4Swesolows 		if (status != 0) {
15651912d2c4Swesolows 			if (WIFSIGNALED(status)) {
15661912d2c4Swesolows 				ret = -WTERMSIG(status);
156780ab886dSwesolows 				break;
15681912d2c4Swesolows 			} else if (WIFEXITED(status)) {
15691912d2c4Swesolows 				ret = WEXITSTATUS(status);
157080ab886dSwesolows 				break;
157180ab886dSwesolows 			}
15727c478bd9Sstevel@tonic-gate 		}
15731912d2c4Swesolows 	} while (!WIFEXITED(status) && !WIFSIGNALED(status));
157480ab886dSwesolows 
15751912d2c4Swesolows 	if (stat(ctx->i_stderr, &s) < 0) {
1576aa9ef484SJohn Levon 		warn("stat failed on child cleanup");
157780ab886dSwesolows 		return (-1);
15787c478bd9Sstevel@tonic-gate 	}
157980ab886dSwesolows 	if (s.st_size != 0) {
15801912d2c4Swesolows 		FILE *f;
158180ab886dSwesolows 
15821912d2c4Swesolows 		if ((f = fopen(ctx->i_stderr, "r")) != NULL) {
15831912d2c4Swesolows 			while (fgets(buf, sizeof (buf), f))
15841912d2c4Swesolows 				(void) fprintf(stderr, "%s", buf);
15851912d2c4Swesolows 			(void) fflush(stderr);
15861912d2c4Swesolows 			(void) fclose(f);
15871912d2c4Swesolows 		}
158880ab886dSwesolows 	}
15891912d2c4Swesolows 	(void) unlink(ctx->i_stderr);
15901912d2c4Swesolows 	free(ctx->i_stderr);
15911912d2c4Swesolows 
15921912d2c4Swesolows 	/*
15931912d2c4Swesolows 	 * cc returns an error code when given -V; we want that to succeed.
15941912d2c4Swesolows 	 */
15951912d2c4Swesolows 	if (ctx->i_flags & CW_F_PROG)
15961912d2c4Swesolows 		return (0);
159780ab886dSwesolows 
159880ab886dSwesolows 	return (ret);
159980ab886dSwesolows }
160080ab886dSwesolows 
160180ab886dSwesolows static int
160280ab886dSwesolows exec_ctx(cw_ictx_t *ctx, int block)
160380ab886dSwesolows {
1604*88e61e85SRichard Lowe 	if ((ctx->i_stderr = tempnam(ctx->i_tmpdir, "cw")) == NULL) {
16051912d2c4Swesolows 		nomem();
160680ab886dSwesolows 		return (-1);
160780ab886dSwesolows 	}
160880ab886dSwesolows 
160980ab886dSwesolows 	if ((ctx->i_pid = fork()) == 0) {
16101912d2c4Swesolows 		int fd;
16111912d2c4Swesolows 
161280ab886dSwesolows 		(void) fclose(stderr);
16131912d2c4Swesolows 		if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL,
16141912d2c4Swesolows 		    0666)) < 0) {
1615aa9ef484SJohn Levon 			err(1, "open failed for standard error");
16161912d2c4Swesolows 		}
16171912d2c4Swesolows 		if (dup2(fd, 2) < 0) {
1618aa9ef484SJohn Levon 			err(1, "dup2 failed for standard error");
161980ab886dSwesolows 		}
16201912d2c4Swesolows 		if (fd != 2)
16211912d2c4Swesolows 			(void) close(fd);
162280ab886dSwesolows 		if (freopen("/dev/fd/2", "w", stderr) == NULL) {
1623aa9ef484SJohn Levon 			err(1, "freopen failed for /dev/fd/2");
162480ab886dSwesolows 		}
1625aa9ef484SJohn Levon 
162680ab886dSwesolows 		prepctx(ctx);
162780ab886dSwesolows 		exit(invoke(ctx));
162880ab886dSwesolows 	}
162980ab886dSwesolows 
163080ab886dSwesolows 	if (ctx->i_pid < 0) {
1631aa9ef484SJohn Levon 		err(1, "fork failed");
163280ab886dSwesolows 	}
163380ab886dSwesolows 
163480ab886dSwesolows 	if (block)
163580ab886dSwesolows 		return (reap(ctx));
163680ab886dSwesolows 
163780ab886dSwesolows 	return (0);
16387c478bd9Sstevel@tonic-gate }
16397c478bd9Sstevel@tonic-gate 
1640aa9ef484SJohn Levon static void
1641aa9ef484SJohn Levon parse_compiler(const char *spec, cw_compiler_t *compiler)
16427c478bd9Sstevel@tonic-gate {
1643aa9ef484SJohn Levon 	char *tspec, *token;
16447c478bd9Sstevel@tonic-gate 
1645aa9ef484SJohn Levon 	if ((tspec = strdup(spec)) == NULL)
164680ab886dSwesolows 		nomem();
164780ab886dSwesolows 
1648aa9ef484SJohn Levon 	if ((token = strsep(&tspec, ",")) == NULL)
1649aa9ef484SJohn Levon 		errx(1, "Compiler is missing a name: %s", spec);
1650aa9ef484SJohn Levon 	compiler->c_name = token;
16517c478bd9Sstevel@tonic-gate 
1652aa9ef484SJohn Levon 	if ((token = strsep(&tspec, ",")) == NULL)
1653aa9ef484SJohn Levon 		errx(1, "Compiler is missing a path: %s", spec);
1654aa9ef484SJohn Levon 	compiler->c_path = token;
16557c478bd9Sstevel@tonic-gate 
1656aa9ef484SJohn Levon 	if ((token = strsep(&tspec, ",")) == NULL)
1657aa9ef484SJohn Levon 		errx(1, "Compiler is missing a style: %s", spec);
16587c478bd9Sstevel@tonic-gate 
1659aa9ef484SJohn Levon 	if ((strcasecmp(token, "gnu") == 0) ||
16601f5207b7SJohn Levon 	    (strcasecmp(token, "gcc") == 0)) {
1661aa9ef484SJohn Levon 		compiler->c_style = GNU;
16621f5207b7SJohn Levon 	} else if ((strcasecmp(token, "sun") == 0) ||
16631f5207b7SJohn Levon 	    (strcasecmp(token, "cc") == 0)) {
1664aa9ef484SJohn Levon 		compiler->c_style = SUN;
16651f5207b7SJohn Levon 	} else if ((strcasecmp(token, "smatch") == 0)) {
16661f5207b7SJohn Levon 		compiler->c_style = SMATCH;
16671f5207b7SJohn Levon 	} else {
1668aa9ef484SJohn Levon 		errx(1, "unknown compiler style: %s", token);
16691f5207b7SJohn Levon 	}
167080ab886dSwesolows 
1671aa9ef484SJohn Levon 	if (tspec != NULL)
1672aa9ef484SJohn Levon 		errx(1, "Excess tokens in compiler: %s", spec);
1673aa9ef484SJohn Levon }
16747c478bd9Sstevel@tonic-gate 
1675*88e61e85SRichard Lowe static void
1676*88e61e85SRichard Lowe cleanup(cw_ictx_t *ctx)
1677*88e61e85SRichard Lowe {
1678*88e61e85SRichard Lowe 	DIR *dirp;
1679*88e61e85SRichard Lowe 	struct dirent *dp;
1680*88e61e85SRichard Lowe 	char buf[MAXPATHLEN];
1681*88e61e85SRichard Lowe 
1682*88e61e85SRichard Lowe 	if ((dirp = opendir(ctx->i_tmpdir)) == NULL) {
1683*88e61e85SRichard Lowe 		if (errno != ENOENT) {
1684*88e61e85SRichard Lowe 			err(1, "couldn't open temp directory: %s",
1685*88e61e85SRichard Lowe 			    ctx->i_tmpdir);
1686*88e61e85SRichard Lowe 		} else {
1687*88e61e85SRichard Lowe 			return;
1688*88e61e85SRichard Lowe 		}
1689*88e61e85SRichard Lowe 	}
1690*88e61e85SRichard Lowe 
1691*88e61e85SRichard Lowe 	errno = 0;
1692*88e61e85SRichard Lowe 	while ((dp = readdir(dirp)) != NULL) {
1693*88e61e85SRichard Lowe 		(void) snprintf(buf, MAXPATHLEN, "%s/%s", ctx->i_tmpdir,
1694*88e61e85SRichard Lowe 		    dp->d_name);
1695*88e61e85SRichard Lowe 
1696*88e61e85SRichard Lowe 		if (strcmp(dp->d_name, ".") == 0 ||
1697*88e61e85SRichard Lowe 		    strcmp(dp->d_name, "..") == 0) {
1698*88e61e85SRichard Lowe 			continue;
1699*88e61e85SRichard Lowe 		}
1700*88e61e85SRichard Lowe 
1701*88e61e85SRichard Lowe 		if (unlink(buf) == -1)
1702*88e61e85SRichard Lowe 			err(1, "failed to unlink temp file: %s", dp->d_name);
1703*88e61e85SRichard Lowe 		errno = 0;
1704*88e61e85SRichard Lowe 	}
1705*88e61e85SRichard Lowe 
1706*88e61e85SRichard Lowe 	if (errno != 0) {
1707*88e61e85SRichard Lowe 		err(1, "failed to read temporary directory: %s",
1708*88e61e85SRichard Lowe 		    ctx->i_tmpdir);
1709*88e61e85SRichard Lowe 	}
1710*88e61e85SRichard Lowe 
1711*88e61e85SRichard Lowe 	(void) closedir(dirp);
1712*88e61e85SRichard Lowe 	if (rmdir(ctx->i_tmpdir) != 0) {
1713*88e61e85SRichard Lowe 		err(1, "failed to unlink temporary directory: %s",
1714*88e61e85SRichard Lowe 		    ctx->i_tmpdir);
1715*88e61e85SRichard Lowe 	}
1716*88e61e85SRichard Lowe }
1717*88e61e85SRichard Lowe 
1718aa9ef484SJohn Levon int
1719aa9ef484SJohn Levon main(int argc, char **argv)
1720aa9ef484SJohn Levon {
1721aa9ef484SJohn Levon 	int ch;
1722aa9ef484SJohn Levon 	cw_compiler_t primary = { NULL, NULL, 0 };
1723aa9ef484SJohn Levon 	cw_compiler_t shadows[10];
1724aa9ef484SJohn Levon 	int nshadows = 0;
1725aa9ef484SJohn Levon 	int ret = 0;
1726aa9ef484SJohn Levon 	boolean_t do_serial = B_FALSE;
1727aa9ef484SJohn Levon 	boolean_t do_exec = B_FALSE;
1728aa9ef484SJohn Levon 	boolean_t vflg = B_FALSE;
1729aa9ef484SJohn Levon 	boolean_t Cflg = B_FALSE;
1730aa9ef484SJohn Levon 	boolean_t cflg = B_FALSE;
1731aa9ef484SJohn Levon 	boolean_t nflg = B_FALSE;
1732*88e61e85SRichard Lowe 	char *tmpdir;
1733aa9ef484SJohn Levon 
1734aa9ef484SJohn Levon 	cw_ictx_t *main_ctx;
1735aa9ef484SJohn Levon 
1736aa9ef484SJohn Levon 	static struct option longopts[] = {
1737aa9ef484SJohn Levon 		{ "compiler", no_argument, NULL, 'c' },
1738aa9ef484SJohn Levon 		{ "noecho", no_argument, NULL, 'n' },
1739aa9ef484SJohn Levon 		{ "primary", required_argument, NULL, 'p' },
1740aa9ef484SJohn Levon 		{ "shadow", required_argument, NULL, 's' },
1741aa9ef484SJohn Levon 		{ "versions", no_argument, NULL, 'v' },
1742aa9ef484SJohn Levon 		{ NULL, 0, NULL, 0 },
1743aa9ef484SJohn Levon 	};
1744aa9ef484SJohn Levon 
1745aa9ef484SJohn Levon 
1746aa9ef484SJohn Levon 	if ((main_ctx = newictx()) == NULL)
1747aa9ef484SJohn Levon 		nomem();
1748aa9ef484SJohn Levon 
1749aa9ef484SJohn Levon 	while ((ch = getopt_long(argc, argv, "C", longopts, NULL)) != -1) {
1750aa9ef484SJohn Levon 		switch (ch) {
1751aa9ef484SJohn Levon 		case 'c':
1752aa9ef484SJohn Levon 			cflg = B_TRUE;
1753aa9ef484SJohn Levon 			break;
1754aa9ef484SJohn Levon 		case 'C':
1755aa9ef484SJohn Levon 			Cflg = B_TRUE;
1756aa9ef484SJohn Levon 			break;
1757aa9ef484SJohn Levon 		case 'n':
1758aa9ef484SJohn Levon 			nflg = B_TRUE;
1759aa9ef484SJohn Levon 			break;
1760aa9ef484SJohn Levon 		case 'p':
1761aa9ef484SJohn Levon 			if (primary.c_path != NULL) {
1762aa9ef484SJohn Levon 				warnx("Only one primary compiler may "
1763aa9ef484SJohn Levon 				    "be specified");
1764aa9ef484SJohn Levon 				usage();
1765aa9ef484SJohn Levon 			}
1766aa9ef484SJohn Levon 
1767aa9ef484SJohn Levon 			parse_compiler(optarg, &primary);
1768aa9ef484SJohn Levon 			break;
1769aa9ef484SJohn Levon 		case 's':
1770aa9ef484SJohn Levon 			if (nshadows >= 10)
1771aa9ef484SJohn Levon 				errx(1, "May only use 10 shadows at "
1772aa9ef484SJohn Levon 				    "the moment");
1773aa9ef484SJohn Levon 			parse_compiler(optarg, &shadows[nshadows]);
1774aa9ef484SJohn Levon 			nshadows++;
1775aa9ef484SJohn Levon 			break;
1776aa9ef484SJohn Levon 		case 'v':
1777aa9ef484SJohn Levon 			vflg = B_TRUE;
1778aa9ef484SJohn Levon 			break;
1779aa9ef484SJohn Levon 		default:
1780aa9ef484SJohn Levon 			(void) fprintf(stderr, "Did you forget '--'?\n");
1781aa9ef484SJohn Levon 			usage();
1782aa9ef484SJohn Levon 		}
1783aa9ef484SJohn Levon 	}
1784aa9ef484SJohn Levon 
1785aa9ef484SJohn Levon 	if (primary.c_path == NULL) {
1786aa9ef484SJohn Levon 		warnx("A primary compiler must be specified");
17877c478bd9Sstevel@tonic-gate 		usage();
17887c478bd9Sstevel@tonic-gate 	}
17897c478bd9Sstevel@tonic-gate 
1790aa9ef484SJohn Levon 	do_serial = (getenv("CW_SHADOW_SERIAL") == NULL) ? B_FALSE : B_TRUE;
1791aa9ef484SJohn Levon 	do_exec = (getenv("CW_NO_EXEC") == NULL) ? B_TRUE : B_FALSE;
1792aa9ef484SJohn Levon 
1793aa9ef484SJohn Levon 	/* Leave room for argv[0] */
1794aa9ef484SJohn Levon 	argc -= (optind - 1);
1795aa9ef484SJohn Levon 	argv += (optind - 1);
1796aa9ef484SJohn Levon 
1797aa9ef484SJohn Levon 	main_ctx->i_oldargc = argc;
1798aa9ef484SJohn Levon 	main_ctx->i_oldargv = argv;
1799aa9ef484SJohn Levon 	main_ctx->i_flags = CW_F_XLATE;
1800aa9ef484SJohn Levon 	if (nflg == 0)
1801aa9ef484SJohn Levon 		main_ctx->i_flags |= CW_F_ECHO;
1802aa9ef484SJohn Levon 	if (do_exec)
1803aa9ef484SJohn Levon 		main_ctx->i_flags |= CW_F_EXEC;
1804aa9ef484SJohn Levon 	if (Cflg)
1805aa9ef484SJohn Levon 		main_ctx->i_flags |= CW_F_CXX;
1806aa9ef484SJohn Levon 	main_ctx->i_compiler = &primary;
1807aa9ef484SJohn Levon 
1808aa9ef484SJohn Levon 	if (cflg) {
1809aa9ef484SJohn Levon 		(void) fputs(primary.c_path, stdout);
18107c478bd9Sstevel@tonic-gate 	}
18117c478bd9Sstevel@tonic-gate 
1812aa9ef484SJohn Levon 	if (vflg) {
1813aa9ef484SJohn Levon 		(void) printf("cw version %s\n", CW_VERSION);
18141912d2c4Swesolows 		(void) fflush(stdout);
1815aa9ef484SJohn Levon 		main_ctx->i_flags &= ~CW_F_ECHO;
1816aa9ef484SJohn Levon 		main_ctx->i_flags |= CW_F_PROG | CW_F_EXEC;
18171912d2c4Swesolows 		do_serial = 1;
18181912d2c4Swesolows 	}
18191912d2c4Swesolows 
1820*88e61e85SRichard Lowe 	tmpdir = getenv("TMPDIR");
1821*88e61e85SRichard Lowe 	if (tmpdir == NULL)
1822*88e61e85SRichard Lowe 		tmpdir = "/tmp";
1823*88e61e85SRichard Lowe 
1824*88e61e85SRichard Lowe 	if (asprintf(&main_ctx->i_tmpdir, "%s/cw.XXXXXX", tmpdir) == -1)
1825*88e61e85SRichard Lowe 		nomem();
1826*88e61e85SRichard Lowe 
1827*88e61e85SRichard Lowe 	if ((main_ctx->i_tmpdir = mkdtemp(main_ctx->i_tmpdir)) == NULL)
1828*88e61e85SRichard Lowe 		errx(1, "failed to create temporary directory");
1829*88e61e85SRichard Lowe 
1830aa9ef484SJohn Levon 	ret |= exec_ctx(main_ctx, do_serial);
18311912d2c4Swesolows 
1832aa9ef484SJohn Levon 	for (int i = 0; i < nshadows; i++) {
1833aa9ef484SJohn Levon 		int r;
1834aa9ef484SJohn Levon 		cw_ictx_t *shadow_ctx;
183580ab886dSwesolows 
1836aa9ef484SJohn Levon 		if ((shadow_ctx = newictx()) == NULL)
1837aa9ef484SJohn Levon 			nomem();
1838aa9ef484SJohn Levon 
1839*88e61e85SRichard Lowe 		(void) memcpy(shadow_ctx, main_ctx, sizeof (cw_ictx_t));
1840aa9ef484SJohn Levon 
1841aa9ef484SJohn Levon 		shadow_ctx->i_flags |= CW_F_SHADOW;
1842aa9ef484SJohn Levon 		shadow_ctx->i_compiler = &shadows[i];
1843aa9ef484SJohn Levon 
1844aa9ef484SJohn Levon 		r = exec_ctx(shadow_ctx, do_serial);
1845aa9ef484SJohn Levon 		if (r == 0) {
1846aa9ef484SJohn Levon 			shadow_ctx->i_next = main_ctx->i_next;
1847aa9ef484SJohn Levon 			main_ctx->i_next = shadow_ctx;
1848aa9ef484SJohn Levon 		}
1849aa9ef484SJohn Levon 		ret |= r;
18507c478bd9Sstevel@tonic-gate 	}
18517c478bd9Sstevel@tonic-gate 
1852aa9ef484SJohn Levon 	if (!do_serial) {
1853aa9ef484SJohn Levon 		cw_ictx_t *next = main_ctx;
1854aa9ef484SJohn Levon 		while (next != NULL) {
1855aa9ef484SJohn Levon 			cw_ictx_t *toreap = next;
1856aa9ef484SJohn Levon 			next = next->i_next;
1857aa9ef484SJohn Levon 			ret |= reap(toreap);
1858aa9ef484SJohn Levon 		}
1859aa9ef484SJohn Levon 	}
18607c478bd9Sstevel@tonic-gate 
1861*88e61e85SRichard Lowe 	cleanup(main_ctx);
186280ab886dSwesolows 	return (ret);
18637c478bd9Sstevel@tonic-gate }
1864