xref: /illumos-gate/usr/src/tools/cw/cw.c (revision 25c28e83)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
502e56f3fSwesolows  * Common Development and Distribution License (the "License").
602e56f3fSwesolows  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2102e56f3fSwesolows 
22*25c28e83SPiotr Jasiukajtis /*
23*25c28e83SPiotr Jasiukajtis  * Copyright 2011, Richard Lowe.
24*25c28e83SPiotr Jasiukajtis  */
257c478bd9Sstevel@tonic-gate /*
267fbf8d03SScott Rotondo  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Wrapper for the GNU C compiler to make it accept the Sun C compiler
327c478bd9Sstevel@tonic-gate  * arguments where possible.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * Since the translation is inexact, this is something of a work-in-progress.
359a70fc3bSMark J. Nelson  *
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
389a70fc3bSMark J. Nelson /* If you modify this file, you must increment CW_VERSION */
39*25c28e83SPiotr Jasiukajtis #define	CW_VERSION	"1.30"
409a70fc3bSMark J. Nelson 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * -#		Verbose mode
437c478bd9Sstevel@tonic-gate  * -###		Show compiler commands built by driver, no compilation
447c478bd9Sstevel@tonic-gate  * -A<name[(tokens)]>	Preprocessor predicate assertion
457c478bd9Sstevel@tonic-gate  * -B<[static|dynamic]>	Specify dynamic or static binding
467c478bd9Sstevel@tonic-gate  * -C		Prevent preprocessor from removing comments
477c478bd9Sstevel@tonic-gate  * -c		Compile only - produce .o files, suppress linking
487c478bd9Sstevel@tonic-gate  * -cg92	Alias for -xtarget=ss1000
497c478bd9Sstevel@tonic-gate  * -D<name[=token]>	Associate name with token as if by #define
507c478bd9Sstevel@tonic-gate  * -d[y|n]	dynamic [-dy] or static [-dn] option to linker
517c478bd9Sstevel@tonic-gate  * -E		Compile source through preprocessor only, output to stdout
527c478bd9Sstevel@tonic-gate  * -erroff=<t>	Suppress warnings specified by tags t(%none, %all, <tag list>)
537c478bd9Sstevel@tonic-gate  * -errtags=<a>	Display messages with tags a(no, yes)
547c478bd9Sstevel@tonic-gate  * -errwarn=<t>	Treats warnings specified by tags t(%none, %all, <tag list>)
557c478bd9Sstevel@tonic-gate  *		as errors
567c478bd9Sstevel@tonic-gate  * -fast	Optimize using a selection of options
577c478bd9Sstevel@tonic-gate  * -fd		Report old-style function definitions and declarations
580e62f92eSMark Logan  * -features=zla	Allow zero-length arrays
597c478bd9Sstevel@tonic-gate  * -flags	Show this summary of compiler options
607c478bd9Sstevel@tonic-gate  * -fnonstd	Initialize floating-point hardware to non-standard preferences
617c478bd9Sstevel@tonic-gate  * -fns[=<yes|no>] Select non-standard floating point mode
627c478bd9Sstevel@tonic-gate  * -fprecision=<p> Set FP rounding precision mode p(single, double, extended)
637c478bd9Sstevel@tonic-gate  * -fround=<r>	Select the IEEE rounding mode in effect at startup
647c478bd9Sstevel@tonic-gate  * -fsimple[=<n>] Select floating-point optimization preferences <n>
657c478bd9Sstevel@tonic-gate  * -fsingle	Use single-precision arithmetic (-Xt and -Xs modes only)
667c478bd9Sstevel@tonic-gate  * -ftrap=<t>	Select floating-point trapping mode in effect at startup
677c478bd9Sstevel@tonic-gate  * -fstore	force floating pt. values to target precision on assignment
687c478bd9Sstevel@tonic-gate  * -G		Build a dynamic shared library
697c478bd9Sstevel@tonic-gate  * -g		Compile for debugging
707c478bd9Sstevel@tonic-gate  * -H		Print path name of each file included during compilation
717c478bd9Sstevel@tonic-gate  * -h <name>	Assign <name> to generated dynamic shared library
727c478bd9Sstevel@tonic-gate  * -I<dir>	Add <dir> to preprocessor #include file search path
737c478bd9Sstevel@tonic-gate  * -i		Passed to linker to ignore any LD_LIBRARY_PATH setting
747c478bd9Sstevel@tonic-gate  * -keeptmp	Keep temporary files created during compilation
757c478bd9Sstevel@tonic-gate  * -KPIC	Compile position independent code with 32-bit addresses
767c478bd9Sstevel@tonic-gate  * -Kpic	Compile position independent code
777c478bd9Sstevel@tonic-gate  * -L<dir>	Pass to linker to add <dir> to the library search path
787c478bd9Sstevel@tonic-gate  * -l<name>	Link with library lib<name>.a or lib<name>.so
797c478bd9Sstevel@tonic-gate  * -mc		Remove duplicate strings from .comment section of output files
807c478bd9Sstevel@tonic-gate  * -mr		Remove all strings from .comment section of output files
817c478bd9Sstevel@tonic-gate  * -mr,"string"	Remove all strings and append "string" to .comment section
827c478bd9Sstevel@tonic-gate  * -mt		Specify options needed when compiling multi-threaded code
837c478bd9Sstevel@tonic-gate  * -native	Find available processor, generate code accordingly
847c478bd9Sstevel@tonic-gate  * -nofstore	Do not force floating pt. values to target precision
857c478bd9Sstevel@tonic-gate  *		on assignment
867c478bd9Sstevel@tonic-gate  * -nolib	Same as -xnolib
877c478bd9Sstevel@tonic-gate  * -noqueue	Disable queuing of compiler license requests
887c478bd9Sstevel@tonic-gate  * -norunpath	Do not build in a runtime path for shared libraries
89662492f5Ssherrym  * -O		Use default optimization level (-xO2 or -xO3. Check man page.)
907c478bd9Sstevel@tonic-gate  * -o <outputfile> Set name of output file to <outputfile>
917c478bd9Sstevel@tonic-gate  * -P		Compile source through preprocessor only, output to .i  file
927c478bd9Sstevel@tonic-gate  * -PIC		Alias for -KPIC or -xcode=pic32
937c478bd9Sstevel@tonic-gate  * -p		Compile for profiling with prof
947c478bd9Sstevel@tonic-gate  * -pic		Alias for -Kpic or -xcode=pic13
957c478bd9Sstevel@tonic-gate  * -Q[y|n]	Emit/don't emit identification info to output file
967c478bd9Sstevel@tonic-gate  * -qp		Compile for profiling with prof
977c478bd9Sstevel@tonic-gate  * -R<dir[:dir]> Build runtime search path list into executable
987c478bd9Sstevel@tonic-gate  * -S		Compile and only generate assembly code (.s)
997c478bd9Sstevel@tonic-gate  * -s		Strip symbol table from the executable file
100159cf8a6Swesolows  * -t		Turn off duplicate symbol warnings when linking
1017c478bd9Sstevel@tonic-gate  * -U<name>	Delete initial definition of preprocessor symbol <name>
1027c478bd9Sstevel@tonic-gate  * -V		Report version number of each compilation phase
1037c478bd9Sstevel@tonic-gate  * -v		Do stricter semantic checking
1047c478bd9Sstevel@tonic-gate  * -W<c>,<arg>	Pass <arg> to specified component <c> (a,l,m,p,0,2,h,i,u)
1057c478bd9Sstevel@tonic-gate  * -w		Suppress compiler warning messages
1067c478bd9Sstevel@tonic-gate  * -Xa		Compile assuming ANSI C conformance, allow K & R extensions
1077c478bd9Sstevel@tonic-gate  *		(default mode)
1087c478bd9Sstevel@tonic-gate  * -Xc		Compile assuming strict ANSI C conformance
1097c478bd9Sstevel@tonic-gate  * -Xs		Compile assuming (pre-ANSI) K & R C style code
1107c478bd9Sstevel@tonic-gate  * -Xt		Compile assuming K & R conformance, allow ANSI C
1117c478bd9Sstevel@tonic-gate  * -x386	Generate code for the 80386 processor
1127c478bd9Sstevel@tonic-gate  * -x486	Generate code for the 80486 processor
1137c478bd9Sstevel@tonic-gate  * -xarch=<a>	Specify target architecture instruction set
1147c478bd9Sstevel@tonic-gate  * -xbuiltin[=<b>] When profitable inline, or substitute intrinisic functions
1157c478bd9Sstevel@tonic-gate  *		for system functions, b={%all,%none}
1167c478bd9Sstevel@tonic-gate  * -xCC		Accept C++ style comments
1177c478bd9Sstevel@tonic-gate  * -xchar_byte_order=<o> Specify multi-char byte order <o> (default, high, low)
1187c478bd9Sstevel@tonic-gate  * -xchip=<c>	Specify the target processor for use by the optimizer
1197c478bd9Sstevel@tonic-gate  * -xcode=<c>	Generate different code for forming addresses
1207c478bd9Sstevel@tonic-gate  * -xcrossfile[=<n>] Enable optimization and inlining across source files,
1217c478bd9Sstevel@tonic-gate  *		n={0|1}
1227c478bd9Sstevel@tonic-gate  * -xe		Perform only syntax/semantic checking, no code generation
123f795e658Srie  * -xF		Compile for later mapfile reordering or unused section
124f795e658Srie  *		elimination
1257c478bd9Sstevel@tonic-gate  * -xhelp=<f>	Display on-line help information f(flags, readme, errors)
1267c478bd9Sstevel@tonic-gate  * -xildoff	Cancel -xildon
1277c478bd9Sstevel@tonic-gate  * -xildon	Enable use of the incremental linker, ild
1287c478bd9Sstevel@tonic-gate  * -xinline=[<a>,...,<a>]  Attempt inlining of specified user routines,
1297c478bd9Sstevel@tonic-gate  *		<a>={%auto,func,no%func}
1307c478bd9Sstevel@tonic-gate  * -xlibmieee	Force IEEE 754 return values for math routines in
1317c478bd9Sstevel@tonic-gate  *		exceptional cases
1327c478bd9Sstevel@tonic-gate  * -xlibmil	Inline selected libm math routines for optimization
1337c478bd9Sstevel@tonic-gate  * -xlic_lib=sunperf	Link in the Sun supplied performance libraries
1347c478bd9Sstevel@tonic-gate  * -xlicinfo	Show license server information
1357c478bd9Sstevel@tonic-gate  * -xM		Generate makefile dependencies
1367c478bd9Sstevel@tonic-gate  * -xM1		Generate makefile dependencies, but exclude /usr/include
1377c478bd9Sstevel@tonic-gate  * -xmaxopt=[off,1,2,3,4,5] maximum optimization level allowed on #pragma opt
1387c478bd9Sstevel@tonic-gate  * -xnolib	Do not link with default system libraries
1397c478bd9Sstevel@tonic-gate  * -xnolibmil	Cancel -xlibmil on command line
1407c478bd9Sstevel@tonic-gate  * -xO<n>	Generate optimized code (n={1|2|3|4|5})
1417c478bd9Sstevel@tonic-gate  * -xP		Print prototypes for function definitions
1427c478bd9Sstevel@tonic-gate  * -xpentium	Generate code for the pentium processor
1437c478bd9Sstevel@tonic-gate  * -xpg		Compile for profiling with gprof
1447c478bd9Sstevel@tonic-gate  * -xprofile=<p> Collect data for a profile or use a profile to optimize
1457c478bd9Sstevel@tonic-gate  *		<p>={{collect,use}[:<path>],tcov}
1467c478bd9Sstevel@tonic-gate  * -xregs=<r>	Control register allocation
1477c478bd9Sstevel@tonic-gate  * -xs		Allow debugging without object (.o) files
1487c478bd9Sstevel@tonic-gate  * -xsb		Compile for use with the WorkShop source browser
1497c478bd9Sstevel@tonic-gate  * -xsbfast	Generate only WorkShop source browser info, no compilation
1507c478bd9Sstevel@tonic-gate  * -xsfpconst	Represent unsuffixed floating point constants as single
1517c478bd9Sstevel@tonic-gate  *		precision
1527c478bd9Sstevel@tonic-gate  * -xspace	Do not do optimizations that increase code size
1537c478bd9Sstevel@tonic-gate  * -xstrconst	Place string literals into read-only data segment
1547c478bd9Sstevel@tonic-gate  * -xtarget=<t>	Specify target system for optimization
1557c478bd9Sstevel@tonic-gate  * -xtemp=<dir>	Set directory for temporary files to <dir>
1567c478bd9Sstevel@tonic-gate  * -xtime	Report the execution time for each compilation phase
1577c478bd9Sstevel@tonic-gate  * -xtransition	Emit warnings for differences between K&R C and ANSI C
1587c478bd9Sstevel@tonic-gate  * -xtrigraphs[=<yes|no>] Enable|disable trigraph translation
1597c478bd9Sstevel@tonic-gate  * -xunroll=n	Enable unrolling loops n times where possible
1607c478bd9Sstevel@tonic-gate  * -Y<c>,<dir>	Specify <dir> for location of component <c> (a,l,m,p,0,h,i,u)
1617c478bd9Sstevel@tonic-gate  * -YA,<dir>	Change default directory searched for components
1627c478bd9Sstevel@tonic-gate  * -YI,<dir>	Change default directory searched for include files
1637c478bd9Sstevel@tonic-gate  * -YP,<dir>	Change default directory for finding libraries files
1647c478bd9Sstevel@tonic-gate  * -YS,<dir>	Change default directory for startup object files
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * Translation table:
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * -#				-v
1727c478bd9Sstevel@tonic-gate  * -###				error
1737c478bd9Sstevel@tonic-gate  * -A<name[(tokens)]>		pass-thru
1747c478bd9Sstevel@tonic-gate  * -B<[static|dynamic]>		pass-thru (syntax error for anything else)
1757c478bd9Sstevel@tonic-gate  * -C				pass-thru
1767c478bd9Sstevel@tonic-gate  * -c				pass-thru
1777c478bd9Sstevel@tonic-gate  * -cg92			-m32 -mcpu=v8 -mtune=supersparc (SPARC only)
1787c478bd9Sstevel@tonic-gate  * -D<name[=token]>		pass-thru
1797c478bd9Sstevel@tonic-gate  * -dy or -dn			-Wl,-dy or -Wl,-dn
1807c478bd9Sstevel@tonic-gate  * -E				pass-thru
1817c478bd9Sstevel@tonic-gate  * -erroff=E_EMPTY_TRANSLATION_UNIT ignore
1827c478bd9Sstevel@tonic-gate  * -errtags=%all		-Wall
1837c478bd9Sstevel@tonic-gate  * -errwarn=%all		-Werror else -Wno-error
1847c478bd9Sstevel@tonic-gate  * -fast			error
1857c478bd9Sstevel@tonic-gate  * -fd				error
1860e62f92eSMark Logan  * -features=zla		ignore
1877c478bd9Sstevel@tonic-gate  * -flags			--help
1887c478bd9Sstevel@tonic-gate  * -fnonstd			error
1897c478bd9Sstevel@tonic-gate  * -fns[=<yes|no>]		error
1907c478bd9Sstevel@tonic-gate  * -fprecision=<p>		error
1917c478bd9Sstevel@tonic-gate  * -fround=<r>			error
1927c478bd9Sstevel@tonic-gate  * -fsimple[=<n>]		error
1937c478bd9Sstevel@tonic-gate  * -fsingle[=<n>]		error
1947c478bd9Sstevel@tonic-gate  * -ftrap=<t>			error
1957c478bd9Sstevel@tonic-gate  * -fstore			error
1967c478bd9Sstevel@tonic-gate  * -G				pass-thru
1977c478bd9Sstevel@tonic-gate  * -g				pass-thru
1987c478bd9Sstevel@tonic-gate  * -H				pass-thru
1997c478bd9Sstevel@tonic-gate  * -h <name>			pass-thru
2007c478bd9Sstevel@tonic-gate  * -I<dir>			pass-thru
2017c478bd9Sstevel@tonic-gate  * -i				pass-thru
2027c478bd9Sstevel@tonic-gate  * -keeptmp			-save-temps
2037c478bd9Sstevel@tonic-gate  * -KPIC			-fPIC
2047c478bd9Sstevel@tonic-gate  * -Kpic			-fpic
2057c478bd9Sstevel@tonic-gate  * -L<dir>			pass-thru
2067c478bd9Sstevel@tonic-gate  * -l<name>			pass-thru
2077c478bd9Sstevel@tonic-gate  * -mc				error
2087c478bd9Sstevel@tonic-gate  * -mr				error
2097c478bd9Sstevel@tonic-gate  * -mr,"string"			error
2107c478bd9Sstevel@tonic-gate  * -mt				-D_REENTRANT
2117c478bd9Sstevel@tonic-gate  * -native			error
2127c478bd9Sstevel@tonic-gate  * -nofstore			error
2137c478bd9Sstevel@tonic-gate  * -nolib			-nodefaultlibs
2147c478bd9Sstevel@tonic-gate  * -noqueue			ignore
2157c478bd9Sstevel@tonic-gate  * -norunpath			ignore
216662492f5Ssherrym  * -O				-O1 (Check the man page to be certain)
2177c478bd9Sstevel@tonic-gate  * -o <outputfile>		pass-thru
2187c478bd9Sstevel@tonic-gate  * -P				-E -o filename.i (or error)
2197c478bd9Sstevel@tonic-gate  * -PIC				-fPIC (C++ only)
2207c478bd9Sstevel@tonic-gate  * -p				pass-thru
2217c478bd9Sstevel@tonic-gate  * -pic				-fpic (C++ only)
2227c478bd9Sstevel@tonic-gate  * -Q[y|n]			error
2237c478bd9Sstevel@tonic-gate  * -qp				-p
2247c478bd9Sstevel@tonic-gate  * -R<dir[:dir]>		pass-thru
2257c478bd9Sstevel@tonic-gate  * -S				pass-thru
2267c478bd9Sstevel@tonic-gate  * -s				-Wl,-s
227159cf8a6Swesolows  * -t				-Wl,-t
2287c478bd9Sstevel@tonic-gate  * -U<name>			pass-thru
2297c478bd9Sstevel@tonic-gate  * -V				--version
2307c478bd9Sstevel@tonic-gate  * -v				-Wall
2317c478bd9Sstevel@tonic-gate  * -Wa,<arg>			pass-thru
2327c478bd9Sstevel@tonic-gate  * -Wp,<arg>			pass-thru except -xc99=<a>
2337c478bd9Sstevel@tonic-gate  * -Wl,<arg>			pass-thru
2347c478bd9Sstevel@tonic-gate  * -W{m,0,2,h,i,u>		error/ignore
2357c478bd9Sstevel@tonic-gate  * -Wu,-xmodel=kernel		-ffreestanding -mcmodel=kernel -mno-red-zone
23654836668Spetede  * -xmodel=kernel		-ffreestanding -mcmodel=kernel -mno-red-zone
237d430274bSsherrym  * -Wu,-save_args		-msave-args
2387c478bd9Sstevel@tonic-gate  * -w				pass-thru
2397c478bd9Sstevel@tonic-gate  * -Xa				-std=iso9899:199409 or -ansi
2407c478bd9Sstevel@tonic-gate  * -Xc				-ansi -pedantic
2417c478bd9Sstevel@tonic-gate  * -Xt				error
2427c478bd9Sstevel@tonic-gate  * -Xs				-traditional -std=c89
2437c478bd9Sstevel@tonic-gate  * -x386			-march=i386 (x86 only)
2447c478bd9Sstevel@tonic-gate  * -x486			-march=i486 (x86 only)
2457c478bd9Sstevel@tonic-gate  * -xarch=<a>			table
24602e56f3fSwesolows  * -xbuiltin[=<b>]		-fbuiltin (-fno-builtin otherwise)
2477c478bd9Sstevel@tonic-gate  * -xCC				ignore
2487c478bd9Sstevel@tonic-gate  * -xchar_byte_order=<o>	error
2497c478bd9Sstevel@tonic-gate  * -xchip=<c>			table
2507c478bd9Sstevel@tonic-gate  * -xcode=<c>			table
2518c1a3716Ssherrym  * -xdebugformat=<format>	ignore (always use dwarf-2 for gcc)
252159cf8a6Swesolows  * -xcrossfile[=<n>]		ignore
2537c478bd9Sstevel@tonic-gate  * -xe				error
2547c478bd9Sstevel@tonic-gate  * -xF				error
2557c478bd9Sstevel@tonic-gate  * -xhelp=<f>			error
2567c478bd9Sstevel@tonic-gate  * -xildoff			ignore
2577c478bd9Sstevel@tonic-gate  * -xildon			ignore
2587c478bd9Sstevel@tonic-gate  * -xinline			ignore
2597c478bd9Sstevel@tonic-gate  * -xlibmieee			error
2607c478bd9Sstevel@tonic-gate  * -xlibmil			error
2617c478bd9Sstevel@tonic-gate  * -xlic_lib=sunperf		error
2627c478bd9Sstevel@tonic-gate  * -xM				-M
2637c478bd9Sstevel@tonic-gate  * -xM1				-MM
2647c478bd9Sstevel@tonic-gate  * -xmaxopt=[...]		error
2657c478bd9Sstevel@tonic-gate  * -xnolib			-nodefaultlibs
2667c478bd9Sstevel@tonic-gate  * -xnolibmil			error
2677c478bd9Sstevel@tonic-gate  * -xO<n>			-O<n>
2687c478bd9Sstevel@tonic-gate  * -xP				error
2697c478bd9Sstevel@tonic-gate  * -xpentium			-march=pentium (x86 only)
2707c478bd9Sstevel@tonic-gate  * -xpg				error
2717c478bd9Sstevel@tonic-gate  * -xprofile=<p>		error
2727c478bd9Sstevel@tonic-gate  * -xregs=<r>			table
2737c478bd9Sstevel@tonic-gate  * -xs				error
2747c478bd9Sstevel@tonic-gate  * -xsb				error
2757c478bd9Sstevel@tonic-gate  * -xsbfast			error
2767c478bd9Sstevel@tonic-gate  * -xsfpconst			error
2777c478bd9Sstevel@tonic-gate  * -xspace			ignore (-not -Os)
2787c478bd9Sstevel@tonic-gate  * -xstrconst			ignore
2797c478bd9Sstevel@tonic-gate  * -xtarget=<t>			table
2807c478bd9Sstevel@tonic-gate  * -xtemp=<dir>			error
2817c478bd9Sstevel@tonic-gate  * -xtime			error
2827c478bd9Sstevel@tonic-gate  * -xtransition			-Wtransition
2837c478bd9Sstevel@tonic-gate  * -xtrigraphs=<yes|no>		-trigraphs -notrigraphs
2847c478bd9Sstevel@tonic-gate  * -xunroll=n			error
2858c1a3716Ssherrym  * -W0,-xdbggen=no%usedonly	-fno-eliminate-unused-debug-symbols
2868c1a3716Ssherrym  *				-fno-eliminate-unused-debug-types
2877c478bd9Sstevel@tonic-gate  * -Y<c>,<dir>			error
2887c478bd9Sstevel@tonic-gate  * -YA,<dir>			error
2897c478bd9Sstevel@tonic-gate  * -YI,<dir>			-nostdinc -I<dir>
2907c478bd9Sstevel@tonic-gate  * -YP,<dir>			error
2917c478bd9Sstevel@tonic-gate  * -YS,<dir>			error
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate #include <stdio.h>
2957c478bd9Sstevel@tonic-gate #include <sys/types.h>
2967c478bd9Sstevel@tonic-gate #include <unistd.h>
2977c478bd9Sstevel@tonic-gate #include <string.h>
2987c478bd9Sstevel@tonic-gate #include <stdlib.h>
2997c478bd9Sstevel@tonic-gate #include <ctype.h>
30080ab886dSwesolows #include <fcntl.h>
30180ab886dSwesolows #include <errno.h>
30280ab886dSwesolows #include <stdarg.h>
3037c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
3047c478bd9Sstevel@tonic-gate #include <sys/param.h>
3057c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
30680ab886dSwesolows #include <sys/wait.h>
30780ab886dSwesolows #include <sys/stat.h>
30880ab886dSwesolows 
30980ab886dSwesolows #define	CW_F_CXX	0x01
31080ab886dSwesolows #define	CW_F_SHADOW	0x02
31180ab886dSwesolows #define	CW_F_EXEC	0x04
31280ab886dSwesolows #define	CW_F_ECHO	0x08
31380ab886dSwesolows #define	CW_F_XLATE	0x10
3141912d2c4Swesolows #define	CW_F_PROG	0x20
31580ab886dSwesolows 
31680ab886dSwesolows typedef enum cw_compiler {
31780ab886dSwesolows 	CW_C_CC = 0,
31880ab886dSwesolows 	CW_C_GCC
31980ab886dSwesolows } cw_compiler_t;
32080ab886dSwesolows 
32180ab886dSwesolows static const char *cmds[] = {
32280ab886dSwesolows 	"cc", "CC",
32380ab886dSwesolows 	"gcc", "g++"
32480ab886dSwesolows };
3257c478bd9Sstevel@tonic-gate 
3267fbf8d03SScott Rotondo static char default_dir[2][MAXPATHLEN] = {
3277fbf8d03SScott Rotondo 	DEFAULT_CC_DIR,
3287fbf8d03SScott Rotondo 	DEFAULT_GCC_DIR,
32980ab886dSwesolows };
3307c478bd9Sstevel@tonic-gate 
33180ab886dSwesolows #define	CC(ctx) \
33280ab886dSwesolows 	(((ctx)->i_flags & CW_F_SHADOW) ? \
33380ab886dSwesolows 	    ((ctx)->i_compiler == CW_C_CC ? CW_C_GCC : CW_C_CC) : \
33480ab886dSwesolows 	    (ctx)->i_compiler)
3357c478bd9Sstevel@tonic-gate 
33680ab886dSwesolows #define	CIDX(compiler, flags)	\
33780ab886dSwesolows 	((int)(compiler) << 1) + ((flags) & CW_F_CXX ? 1 : 0)
33880ab886dSwesolows 
33980ab886dSwesolows typedef enum cw_op {
34080ab886dSwesolows 	CW_O_NONE = 0,
34180ab886dSwesolows 	CW_O_PREPROCESS,
34280ab886dSwesolows 	CW_O_COMPILE,
34380ab886dSwesolows 	CW_O_LINK
34480ab886dSwesolows } cw_op_t;
34580ab886dSwesolows 
34680ab886dSwesolows struct aelist {
34780ab886dSwesolows 	struct ae {
34880ab886dSwesolows 		struct ae *ae_next;
34980ab886dSwesolows 		char *ae_arg;
35080ab886dSwesolows 	} *ael_head, *ael_tail;
35180ab886dSwesolows 	int ael_argc;
35280ab886dSwesolows };
35380ab886dSwesolows 
35480ab886dSwesolows typedef struct cw_ictx {
35580ab886dSwesolows 	cw_compiler_t	i_compiler;
35680ab886dSwesolows 	struct aelist	*i_ae;
35780ab886dSwesolows 	uint32_t	i_flags;
35880ab886dSwesolows 	int		i_oldargc;
35980ab886dSwesolows 	char		**i_oldargv;
36080ab886dSwesolows 	pid_t		i_pid;
36180ab886dSwesolows 	char		i_discard[MAXPATHLEN];
3621912d2c4Swesolows 	char		*i_stderr;
36380ab886dSwesolows } cw_ictx_t;
36480ab886dSwesolows 
365e521259dSpetede /*
366e521259dSpetede  * Status values to indicate which Studio compiler and associated
367e521259dSpetede  * flags are being used.
368e521259dSpetede  */
369e521259dSpetede #define	M32		0x01	/* -m32 - only on Studio 12 */
370e521259dSpetede #define	M64		0x02	/* -m64 - only on Studio 12 */
371e521259dSpetede #define	SS11		0x100	/* Studio 11 */
372e521259dSpetede #define	SS12		0x200	/* Studio 12 */
3737c478bd9Sstevel@tonic-gate 
374e521259dSpetede #define	TRANS_ENTRY	5
375e521259dSpetede /*
376e521259dSpetede  * Translation table definition for the -xarch= flag. The "x_arg"
377e521259dSpetede  * value is translated into the appropriate gcc flags according
378e521259dSpetede  * to the values in x_trans[n]. The x_flags indicates what compiler
379e521259dSpetede  * is being used and what flags have been set via the use of
380e521259dSpetede  * "x_arg".
381e521259dSpetede  */
382e521259dSpetede typedef struct xarch_table {
383e521259dSpetede 	char	*x_arg;
384e521259dSpetede 	int	x_flags;
385e521259dSpetede 	char	*x_trans[TRANS_ENTRY];
386e521259dSpetede } xarch_table_t;
387e521259dSpetede 
388e521259dSpetede /*
389e521259dSpetede  * The translation table for the -xarch= flag used in the Studio compilers.
390e521259dSpetede  */
391e521259dSpetede static const xarch_table_t xtbl[] = {
3927c478bd9Sstevel@tonic-gate #if defined(__x86)
393e521259dSpetede 	{ "generic",	SS11 },
3947a6460b6Spetede 	{ "generic64",	(SS11|M64), { "-m64", "-mtune=opteron" } },
3957a6460b6Spetede 	{ "amd64",	(SS11|M64), { "-m64", "-mtune=opteron" } },
3967a6460b6Spetede 	{ "386",	SS11,	{ "-march=i386" } },
3977a6460b6Spetede 	{ "pentium_pro", SS11,	{ "-march=pentiumpro" } },
398*25c28e83SPiotr Jasiukajtis 	{ "sse",	SS11, { "-msse", "-mfpmath=sse" } },
399*25c28e83SPiotr Jasiukajtis 	{ "sse2",	SS11, { "-msse2", "-mfpmath=sse" } },
4007c478bd9Sstevel@tonic-gate #elif defined(__sparc)
4017a6460b6Spetede 	{ "generic",	(SS11|M32), { "-m32", "-mcpu=v8" } },
4027a6460b6Spetede 	{ "generic64",	(SS11|M64), { "-m64", "-mcpu=v9" } },
4037a6460b6Spetede 	{ "v8",		(SS11|M32), { "-m32", "-mcpu=v8", "-mno-v8plus" } },
4047a6460b6Spetede 	{ "v8plus",	(SS11|M32), { "-m32", "-mcpu=v9", "-mv8plus" } },
4057a6460b6Spetede 	{ "v8plusa",	(SS11|M32), { "-m32", "-mcpu=ultrasparc", "-mv8plus",
4067a6460b6Spetede 			"-mvis" } },
4077a6460b6Spetede 	{ "v8plusb",	(SS11|M32), { "-m32", "-mcpu=ultrasparc3", "-mv8plus",
4087a6460b6Spetede 			"-mvis" } },
4097a6460b6Spetede 	{ "v9",		(SS11|M64), { "-m64", "-mcpu=v9" } },
4107a6460b6Spetede 	{ "v9a",	(SS11|M64), { "-m64", "-mcpu=ultrasparc", "-mvis" } },
4117a6460b6Spetede 	{ "v9b",	(SS11|M64), { "-m64", "-mcpu=ultrasparc3", "-mvis" } },
4127a6460b6Spetede 	{ "sparc",	SS12, { "-mcpu=v9", "-mv8plus" } },
4137a6460b6Spetede 	{ "sparcvis",	SS12, { "-mcpu=ultrasparc", "-mvis" } },
4147a6460b6Spetede 	{ "sparcvis2",	SS12, { "-mcpu=ultrasparc3", "-mvis" } }
4157c478bd9Sstevel@tonic-gate #endif
4167c478bd9Sstevel@tonic-gate };
4177c478bd9Sstevel@tonic-gate 
418e521259dSpetede static int xtbl_size = sizeof (xtbl) / sizeof (xarch_table_t);
419e521259dSpetede 
420e521259dSpetede static const char *progname;
421e521259dSpetede 
4227c478bd9Sstevel@tonic-gate static const char *xchip_tbl[] = {
4237c478bd9Sstevel@tonic-gate #if defined(__x86)
4247c478bd9Sstevel@tonic-gate 	"386",		"-mtune=i386", NULL,
4257c478bd9Sstevel@tonic-gate 	"486",		"-mtune=i486", NULL,
4267c478bd9Sstevel@tonic-gate 	"pentium",	"-mtune=pentium", NULL,
4277c478bd9Sstevel@tonic-gate 	"pentium_pro",  "-mtune=pentiumpro", NULL,
4287c478bd9Sstevel@tonic-gate #elif defined(__sparc)
4297c478bd9Sstevel@tonic-gate 	"super",	"-mtune=supersparc", NULL,
4307c478bd9Sstevel@tonic-gate 	"ultra",	"-mtune=ultrasparc", NULL,
4317c478bd9Sstevel@tonic-gate 	"ultra3",	"-mtune=ultrasparc3", NULL,
4327c478bd9Sstevel@tonic-gate #endif
4337c478bd9Sstevel@tonic-gate 	NULL,		NULL
4347c478bd9Sstevel@tonic-gate };
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate static const char *xcode_tbl[] = {
4377c478bd9Sstevel@tonic-gate #if defined(__sparc)
4387c478bd9Sstevel@tonic-gate 	"abs32",	"-fno-pic", "-mcmodel=medlow", NULL,
4397c478bd9Sstevel@tonic-gate 	"abs44",	"-fno-pic", "-mcmodel=medmid", NULL,
4407c478bd9Sstevel@tonic-gate 	"abs64",	"-fno-pic", "-mcmodel=medany", NULL,
4417c478bd9Sstevel@tonic-gate 	"pic13",	"-fpic", NULL,
4427c478bd9Sstevel@tonic-gate 	"pic32",	"-fPIC", NULL,
4437c478bd9Sstevel@tonic-gate #endif
4447c478bd9Sstevel@tonic-gate 	NULL,		NULL
4457c478bd9Sstevel@tonic-gate };
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate static const char *xtarget_tbl[] = {
4487c478bd9Sstevel@tonic-gate #if defined(__x86)
4497c478bd9Sstevel@tonic-gate 	"pentium_pro",	"-march=pentiumpro", NULL,
4507c478bd9Sstevel@tonic-gate #endif	/* __x86 */
4517c478bd9Sstevel@tonic-gate 	NULL,		NULL
4527c478bd9Sstevel@tonic-gate };
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate static const char *xregs_tbl[] = {
4557c478bd9Sstevel@tonic-gate #if defined(__sparc)
4567c478bd9Sstevel@tonic-gate 	"appl",		"-mapp-regs", NULL,
4577c478bd9Sstevel@tonic-gate 	"no%appl",	"-mno-app-regs", NULL,
4587c478bd9Sstevel@tonic-gate 	"float",	"-mfpu", NULL,
4597c478bd9Sstevel@tonic-gate 	"no%float",	"-mno-fpu", NULL,
4607c478bd9Sstevel@tonic-gate #endif	/* __sparc */
4617c478bd9Sstevel@tonic-gate 	NULL,		NULL
4627c478bd9Sstevel@tonic-gate };
4637c478bd9Sstevel@tonic-gate 
46480ab886dSwesolows static void
46580ab886dSwesolows nomem(void)
46680ab886dSwesolows {
46780ab886dSwesolows 	(void) fprintf(stderr, "%s: error: out of memory\n", progname);
46880ab886dSwesolows 	exit(1);
46980ab886dSwesolows }
4707c478bd9Sstevel@tonic-gate 
47180ab886dSwesolows static void
47280ab886dSwesolows cw_perror(const char *fmt, ...)
4737c478bd9Sstevel@tonic-gate {
47480ab886dSwesolows 	va_list ap;
47580ab886dSwesolows 	int saved_errno = errno;
47680ab886dSwesolows 
47780ab886dSwesolows 	(void) fprintf(stderr, "%s: error: ", progname);
47880ab886dSwesolows 
47980ab886dSwesolows 	va_start(ap, fmt);
48080ab886dSwesolows 	(void) vfprintf(stderr, fmt, ap);
48180ab886dSwesolows 	va_end(ap);
48280ab886dSwesolows 
48380ab886dSwesolows 	(void) fprintf(stderr, " (%s)\n", strerror(saved_errno));
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate static void
4877c478bd9Sstevel@tonic-gate newae(struct aelist *ael, const char *arg)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	struct ae *ae;
4907c478bd9Sstevel@tonic-gate 
49180ab886dSwesolows 	if ((ae = calloc(sizeof (*ae), 1)) == NULL)
49280ab886dSwesolows 		nomem();
4937c478bd9Sstevel@tonic-gate 	ae->ae_arg = strdup(arg);
4947c478bd9Sstevel@tonic-gate 	if (ael->ael_tail == NULL)
4957c478bd9Sstevel@tonic-gate 		ael->ael_head = ae;
4967c478bd9Sstevel@tonic-gate 	else
4977c478bd9Sstevel@tonic-gate 		ael->ael_tail->ae_next = ae;
4987c478bd9Sstevel@tonic-gate 	ael->ael_tail = ae;
49980ab886dSwesolows 	ael->ael_argc++;
50080ab886dSwesolows }
50180ab886dSwesolows 
50280ab886dSwesolows static cw_ictx_t *
50380ab886dSwesolows newictx(void)
50480ab886dSwesolows {
50580ab886dSwesolows 	cw_ictx_t *ctx = calloc(sizeof (cw_ictx_t), 1);
50680ab886dSwesolows 	if (ctx)
50780ab886dSwesolows 		if ((ctx->i_ae = calloc(sizeof (struct aelist), 1)) == NULL) {
50880ab886dSwesolows 			free(ctx);
50980ab886dSwesolows 			return (NULL);
51080ab886dSwesolows 		}
51180ab886dSwesolows 
51280ab886dSwesolows 	return (ctx);
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate static void
5167c478bd9Sstevel@tonic-gate error(const char *arg)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
51980ab886dSwesolows 	    "%s: error: mapping failed at or near arg '%s'\n", progname, arg);
5207c478bd9Sstevel@tonic-gate 	exit(2);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate  * Add the current favourite set of warnings to the gcc invocation.
5257c478bd9Sstevel@tonic-gate  */
5267c478bd9Sstevel@tonic-gate static void
5277c478bd9Sstevel@tonic-gate warnings(struct aelist *h)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	static int warningsonce;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	if (warningsonce++)
5327c478bd9Sstevel@tonic-gate 		return;
5337c478bd9Sstevel@tonic-gate 
5347014882cSRichard Lowe 	/*
5357014882cSRichard Lowe 	 * Enable as many warnings as exist, then disable those that we never
5367014882cSRichard Lowe 	 * ever want.
5377014882cSRichard Lowe 	 */
5387c478bd9Sstevel@tonic-gate 	newae(h, "-Wall");
5397014882cSRichard Lowe 	newae(h, "-Wextra");
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate static void
5437c478bd9Sstevel@tonic-gate optim_disable(struct aelist *h, int level)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 	if (level >= 2) {
5467c478bd9Sstevel@tonic-gate 		newae(h, "-fno-strict-aliasing");
5477c478bd9Sstevel@tonic-gate 		newae(h, "-fno-unit-at-a-time");
5487c478bd9Sstevel@tonic-gate 		newae(h, "-fno-optimize-sibling-calls");
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate /* ARGSUSED */
5537c478bd9Sstevel@tonic-gate static void
5547c478bd9Sstevel@tonic-gate Xamode(struct aelist *h)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate static void
5597c478bd9Sstevel@tonic-gate Xcmode(struct aelist *h)
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 	static int xconce;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	if (xconce++)
5647c478bd9Sstevel@tonic-gate 		return;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	newae(h, "-ansi");
5677c478bd9Sstevel@tonic-gate 	newae(h, "-pedantic-errors");
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate static void
5717c478bd9Sstevel@tonic-gate Xsmode(struct aelist *h)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	static int xsonce;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	if (xsonce++)
5767c478bd9Sstevel@tonic-gate 		return;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	newae(h, "-traditional");
5797c478bd9Sstevel@tonic-gate 	newae(h, "-traditional-cpp");
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate static void
5837c478bd9Sstevel@tonic-gate usage()
5847c478bd9Sstevel@tonic-gate {
5857c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
5867c478bd9Sstevel@tonic-gate 	    "usage: %s { -_cc | -_gcc | -_CC | -_g++ } [ -_compiler | ... ]\n",
5877c478bd9Sstevel@tonic-gate 	    progname);
5887c478bd9Sstevel@tonic-gate 	exit(2);
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate 
591e521259dSpetede static int
592e521259dSpetede xlate_xtb(struct aelist *h, const char *xarg)
593e521259dSpetede {
594e521259dSpetede 	int	i, j;
595e521259dSpetede 
596e521259dSpetede 	for (i = 0; i < xtbl_size; i++) {
597e521259dSpetede 		if (strcmp(xtbl[i].x_arg, xarg) == 0)
598e521259dSpetede 			break;
599e521259dSpetede 	}
600e521259dSpetede 
601e521259dSpetede 	/*
602e521259dSpetede 	 * At the end of the table and so no matching "arg" entry
603e521259dSpetede 	 * found and so this must be a bad -xarch= flag.
604e521259dSpetede 	 */
605e521259dSpetede 	if (i == xtbl_size)
606e521259dSpetede 		error(xarg);
607e521259dSpetede 
608e521259dSpetede 	for (j = 0; j < TRANS_ENTRY; j++) {
609e521259dSpetede 		if (xtbl[i].x_trans[j] != NULL)
610e521259dSpetede 			newae(h, xtbl[i].x_trans[j]);
611e521259dSpetede 		else
612e521259dSpetede 			break;
613e521259dSpetede 	}
614e521259dSpetede 	return (xtbl[i].x_flags);
615e521259dSpetede 
616e521259dSpetede }
617e521259dSpetede 
6187c478bd9Sstevel@tonic-gate static void
6197c478bd9Sstevel@tonic-gate xlate(struct aelist *h, const char *xarg, const char **table)
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate 	while (*table != NULL && strcmp(xarg, *table) != 0) {
6227c478bd9Sstevel@tonic-gate 		while (*table != NULL)
6237c478bd9Sstevel@tonic-gate 			table++;
6247c478bd9Sstevel@tonic-gate 		table++;
6257c478bd9Sstevel@tonic-gate 	}
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	if (*table == NULL)
6287c478bd9Sstevel@tonic-gate 		error(xarg);
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	table++;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	while (*table != NULL) {
6337c478bd9Sstevel@tonic-gate 		newae(h, *table);
6347c478bd9Sstevel@tonic-gate 		table++;
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate static void
63980ab886dSwesolows do_gcc(cw_ictx_t *ctx)
6407c478bd9Sstevel@tonic-gate {
6417c478bd9Sstevel@tonic-gate 	int c;
64280ab886dSwesolows 	int pic = 0, nolibc = 0;
64380ab886dSwesolows 	int in_output = 0, seen_o = 0, c_files = 0;
64480ab886dSwesolows 	cw_op_t op = CW_O_LINK;
6457c478bd9Sstevel@tonic-gate 	char *model = NULL;
646e521259dSpetede 	int	mflag = 0;
6477c478bd9Sstevel@tonic-gate 
6481912d2c4Swesolows 	if (ctx->i_flags & CW_F_PROG) {
6491912d2c4Swesolows 		newae(ctx->i_ae, "--version");
6501912d2c4Swesolows 		return;
6511912d2c4Swesolows 	}
6521912d2c4Swesolows 
65380ab886dSwesolows 	newae(ctx->i_ae, "-fident");
65480ab886dSwesolows 	newae(ctx->i_ae, "-finline");
65580ab886dSwesolows 	newae(ctx->i_ae, "-fno-inline-functions");
65680ab886dSwesolows 	newae(ctx->i_ae, "-fno-builtin");
65780ab886dSwesolows 	newae(ctx->i_ae, "-fno-asm");
6587014882cSRichard Lowe 	newae(ctx->i_ae, "-fdiagnostics-show-option");
65980ab886dSwesolows 	newae(ctx->i_ae, "-nodefaultlibs");
6607c478bd9Sstevel@tonic-gate 
66102e56f3fSwesolows #if defined(__sparc)
66202e56f3fSwesolows 	/*
66302e56f3fSwesolows 	 * The SPARC ldd and std instructions require 8-byte alignment of
66402e56f3fSwesolows 	 * their address operand.  gcc correctly uses them only when the
66502e56f3fSwesolows 	 * ABI requires 8-byte alignment; unfortunately we have a number of
66602e56f3fSwesolows 	 * pieces of buggy code that doesn't conform to the ABI.  This
66702e56f3fSwesolows 	 * flag makes gcc work more like Studio with -xmemalign=4.
66802e56f3fSwesolows 	 */
66980ab886dSwesolows 	newae(ctx->i_ae, "-mno-integer-ldd-std");
67002e56f3fSwesolows #endif
67102e56f3fSwesolows 
6727c478bd9Sstevel@tonic-gate 	/*
6737c478bd9Sstevel@tonic-gate 	 * This is needed because 'u' is defined
6747c478bd9Sstevel@tonic-gate 	 * under a conditional on 'sun'.  Should
6757c478bd9Sstevel@tonic-gate 	 * probably just remove the conditional,
6767c478bd9Sstevel@tonic-gate 	 * or make it be dependent on '__sun'.
6777c478bd9Sstevel@tonic-gate 	 *
6787c478bd9Sstevel@tonic-gate 	 * -Dunix is also missing in enhanced ANSI mode
6797c478bd9Sstevel@tonic-gate 	 */
68080ab886dSwesolows 	newae(ctx->i_ae, "-D__sun");
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	/*
6837c478bd9Sstevel@tonic-gate 	 * Walk the argument list, translating as we go ..
6847c478bd9Sstevel@tonic-gate 	 */
6857c478bd9Sstevel@tonic-gate 
68680ab886dSwesolows 	while (--ctx->i_oldargc > 0) {
68780ab886dSwesolows 		char *arg = *++ctx->i_oldargv;
6887c478bd9Sstevel@tonic-gate 		size_t arglen = strlen(arg);
6897c478bd9Sstevel@tonic-gate 
69080ab886dSwesolows 		if (*arg == '-') {
6917c478bd9Sstevel@tonic-gate 			arglen--;
69280ab886dSwesolows 		} else {
6937c478bd9Sstevel@tonic-gate 			/*
6947c478bd9Sstevel@tonic-gate 			 * Discard inline files that gcc doesn't grok
6957c478bd9Sstevel@tonic-gate 			 */
69680ab886dSwesolows 			if (!in_output && arglen > 3 &&
6977c478bd9Sstevel@tonic-gate 			    strcmp(arg + arglen - 3, ".il") == 0)
6987c478bd9Sstevel@tonic-gate 				continue;
6997c478bd9Sstevel@tonic-gate 
70080ab886dSwesolows 			if (!in_output && arglen > 2 &&
70180ab886dSwesolows 			    arg[arglen - 2] == '.' &&
70280ab886dSwesolows 			    (arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' ||
70380ab886dSwesolows 			    arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i'))
70480ab886dSwesolows 				c_files++;
70580ab886dSwesolows 
7067c478bd9Sstevel@tonic-gate 			/*
70780ab886dSwesolows 			 * Otherwise, filenames and partial arguments
70880ab886dSwesolows 			 * are passed through for gcc to chew on.  However,
70980ab886dSwesolows 			 * output is always discarded for the secondary
71080ab886dSwesolows 			 * compiler.
7117c478bd9Sstevel@tonic-gate 			 */
71280ab886dSwesolows 			if ((ctx->i_flags & CW_F_SHADOW) && in_output)
71380ab886dSwesolows 				newae(ctx->i_ae, ctx->i_discard);
71480ab886dSwesolows 			else
71580ab886dSwesolows 				newae(ctx->i_ae, arg);
71680ab886dSwesolows 			in_output = 0;
7177c478bd9Sstevel@tonic-gate 			continue;
7187c478bd9Sstevel@tonic-gate 		}
7197c478bd9Sstevel@tonic-gate 
72080ab886dSwesolows 		if (ctx->i_flags & CW_F_CXX) {
7217c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "-compat=", 8) == 0) {
7227c478bd9Sstevel@tonic-gate 				/* discard -compat=4 and -compat=5 */
7237c478bd9Sstevel@tonic-gate 				continue;
7247c478bd9Sstevel@tonic-gate 			}
7257c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-Qoption") == 0) {
7267c478bd9Sstevel@tonic-gate 				/* discard -Qoption and its two arguments */
72780ab886dSwesolows 				if (ctx->i_oldargc < 3)
7287c478bd9Sstevel@tonic-gate 					error(arg);
72980ab886dSwesolows 				ctx->i_oldargc -= 2;
73080ab886dSwesolows 				ctx->i_oldargv += 2;
7317c478bd9Sstevel@tonic-gate 				continue;
7327c478bd9Sstevel@tonic-gate 			}
7337c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-xwe") == 0) {
7347c478bd9Sstevel@tonic-gate 				/* turn warnings into errors */
73580ab886dSwesolows 				newae(ctx->i_ae, "-Werror");
7367c478bd9Sstevel@tonic-gate 				continue;
7377c478bd9Sstevel@tonic-gate 			}
7387c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-noex") == 0) {
7397c478bd9Sstevel@tonic-gate 				/* no exceptions */
74080ab886dSwesolows 				newae(ctx->i_ae, "-fno-exceptions");
7417c478bd9Sstevel@tonic-gate 				/* no run time type descriptor information */
74280ab886dSwesolows 				newae(ctx->i_ae, "-fno-rtti");
7437c478bd9Sstevel@tonic-gate 				continue;
7447c478bd9Sstevel@tonic-gate 			}
7457c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-pic") == 0) {
74680ab886dSwesolows 				newae(ctx->i_ae, "-fpic");
7477c478bd9Sstevel@tonic-gate 				pic = 1;
7487c478bd9Sstevel@tonic-gate 				continue;
7497c478bd9Sstevel@tonic-gate 			}
7507c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-PIC") == 0) {
75180ab886dSwesolows 				newae(ctx->i_ae, "-fPIC");
7527c478bd9Sstevel@tonic-gate 				pic = 1;
7537c478bd9Sstevel@tonic-gate 				continue;
7547c478bd9Sstevel@tonic-gate 			}
7557c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-norunpath") == 0) {
7567c478bd9Sstevel@tonic-gate 				/* gcc has no corresponding option */
7577c478bd9Sstevel@tonic-gate 				continue;
7587c478bd9Sstevel@tonic-gate 			}
7597c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-nolib") == 0) {
7607c478bd9Sstevel@tonic-gate 				/* -nodefaultlibs is on by default */
7617c478bd9Sstevel@tonic-gate 				nolibc = 1;
7627c478bd9Sstevel@tonic-gate 				continue;
7637c478bd9Sstevel@tonic-gate 			}
7647c478bd9Sstevel@tonic-gate #if defined(__sparc)
7657c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-cg92") == 0) {
766e521259dSpetede 				mflag |= xlate_xtb(ctx->i_ae, "v8");
76780ab886dSwesolows 				xlate(ctx->i_ae, "super", xchip_tbl);
7687c478bd9Sstevel@tonic-gate 				continue;
7697c478bd9Sstevel@tonic-gate 			}
7707c478bd9Sstevel@tonic-gate #endif	/* __sparc */
7717c478bd9Sstevel@tonic-gate 		}
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 		switch ((c = arg[1])) {
7747c478bd9Sstevel@tonic-gate 		case '_':
7757c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-_noecho") == 0)
77680ab886dSwesolows 				ctx->i_flags &= ~CW_F_ECHO;
7777c478bd9Sstevel@tonic-gate 			else if (strncmp(arg, "-_cc=", 5) == 0 ||
7787c478bd9Sstevel@tonic-gate 			    strncmp(arg, "-_CC=", 5) == 0)
7797c478bd9Sstevel@tonic-gate 				/* EMPTY */;
7807c478bd9Sstevel@tonic-gate 			else if (strncmp(arg, "-_gcc=", 6) == 0 ||
7817c478bd9Sstevel@tonic-gate 			    strncmp(arg, "-_g++=", 6) == 0)
78280ab886dSwesolows 				newae(ctx->i_ae, arg + 6);
78380ab886dSwesolows 			else
7847c478bd9Sstevel@tonic-gate 				error(arg);
7857c478bd9Sstevel@tonic-gate 			break;
7867c478bd9Sstevel@tonic-gate 		case '#':
7877c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
78880ab886dSwesolows 				newae(ctx->i_ae, "-v");
7897c478bd9Sstevel@tonic-gate 				break;
7907c478bd9Sstevel@tonic-gate 			}
7917c478bd9Sstevel@tonic-gate 			error(arg);
7927c478bd9Sstevel@tonic-gate 			break;
7937c478bd9Sstevel@tonic-gate 		case 'g':
79480ab886dSwesolows 			newae(ctx->i_ae, "-gdwarf-2");
7957c478bd9Sstevel@tonic-gate 			break;
7967c478bd9Sstevel@tonic-gate 		case 'E':
7977c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
79880ab886dSwesolows 				newae(ctx->i_ae, "-xc");
79980ab886dSwesolows 				newae(ctx->i_ae, arg);
80080ab886dSwesolows 				op = CW_O_PREPROCESS;
8017c478bd9Sstevel@tonic-gate 				nolibc = 1;
8027c478bd9Sstevel@tonic-gate 				break;
8037c478bd9Sstevel@tonic-gate 			}
8047c478bd9Sstevel@tonic-gate 			error(arg);
8057c478bd9Sstevel@tonic-gate 			break;
8067c478bd9Sstevel@tonic-gate 		case 'c':
8077c478bd9Sstevel@tonic-gate 		case 'S':
80880ab886dSwesolows 			if (arglen == 1) {
80980ab886dSwesolows 				op = CW_O_COMPILE;
8107c478bd9Sstevel@tonic-gate 				nolibc = 1;
81180ab886dSwesolows 			}
8127c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
8137c478bd9Sstevel@tonic-gate 		case 'C':
8147c478bd9Sstevel@tonic-gate 		case 'H':
8157c478bd9Sstevel@tonic-gate 		case 'p':
8167c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
81780ab886dSwesolows 				newae(ctx->i_ae, arg);
8187c478bd9Sstevel@tonic-gate 				break;
8197c478bd9Sstevel@tonic-gate 			}
8207c478bd9Sstevel@tonic-gate 			error(arg);
8217c478bd9Sstevel@tonic-gate 			break;
8227c478bd9Sstevel@tonic-gate 		case 'A':
8237c478bd9Sstevel@tonic-gate 		case 'h':
8247c478bd9Sstevel@tonic-gate 		case 'I':
8257c478bd9Sstevel@tonic-gate 		case 'i':
8267c478bd9Sstevel@tonic-gate 		case 'L':
8277c478bd9Sstevel@tonic-gate 		case 'l':
8287c478bd9Sstevel@tonic-gate 		case 'R':
8297c478bd9Sstevel@tonic-gate 		case 'U':
8307c478bd9Sstevel@tonic-gate 		case 'u':
8317c478bd9Sstevel@tonic-gate 		case 'w':
83280ab886dSwesolows 			newae(ctx->i_ae, arg);
83380ab886dSwesolows 			break;
83480ab886dSwesolows 		case 'o':
83580ab886dSwesolows 			seen_o = 1;
83680ab886dSwesolows 			if (arglen == 1) {
83780ab886dSwesolows 				in_output = 1;
83880ab886dSwesolows 				newae(ctx->i_ae, arg);
83980ab886dSwesolows 			} else if (ctx->i_flags & CW_F_SHADOW) {
84080ab886dSwesolows 				newae(ctx->i_ae, "-o");
84180ab886dSwesolows 				newae(ctx->i_ae, ctx->i_discard);
84280ab886dSwesolows 			} else {
84380ab886dSwesolows 				newae(ctx->i_ae, arg);
84480ab886dSwesolows 			}
8457c478bd9Sstevel@tonic-gate 			break;
8467c478bd9Sstevel@tonic-gate 		case 'D':
84780ab886dSwesolows 			newae(ctx->i_ae, arg);
8487c478bd9Sstevel@tonic-gate 			/*
8497c478bd9Sstevel@tonic-gate 			 * XXX	Clearly a hack ... do we need _KADB too?
8507c478bd9Sstevel@tonic-gate 			 */
8517c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-D_KERNEL") == 0 ||
8527c478bd9Sstevel@tonic-gate 			    strcmp(arg, "-D_BOOT") == 0)
85380ab886dSwesolows 				newae(ctx->i_ae, "-ffreestanding");
8547c478bd9Sstevel@tonic-gate 			break;
8557c478bd9Sstevel@tonic-gate 		case 'd':
8567c478bd9Sstevel@tonic-gate 			if (arglen == 2) {
8577c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-dy") == 0) {
85880ab886dSwesolows 					newae(ctx->i_ae, "-Wl,-dy");
8597c478bd9Sstevel@tonic-gate 					break;
8607c478bd9Sstevel@tonic-gate 				}
8617c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-dn") == 0) {
86280ab886dSwesolows 					newae(ctx->i_ae, "-Wl,-dn");
8637c478bd9Sstevel@tonic-gate 					break;
8647c478bd9Sstevel@tonic-gate 				}
8657c478bd9Sstevel@tonic-gate 			}
8667c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-dalign") == 0) {
8677c478bd9Sstevel@tonic-gate 				/*
8687c478bd9Sstevel@tonic-gate 				 * -dalign forces alignment in some cases;
8697c478bd9Sstevel@tonic-gate 				 * gcc does not need any flag to do this.
8707c478bd9Sstevel@tonic-gate 				 */
8717c478bd9Sstevel@tonic-gate 				break;
8727c478bd9Sstevel@tonic-gate 			}
8737c478bd9Sstevel@tonic-gate 			error(arg);
8747c478bd9Sstevel@tonic-gate 			break;
8757c478bd9Sstevel@tonic-gate 		case 'e':
8767c478bd9Sstevel@tonic-gate 			if (strcmp(arg,
8777c478bd9Sstevel@tonic-gate 			    "-erroff=E_EMPTY_TRANSLATION_UNIT") == 0) {
8787c478bd9Sstevel@tonic-gate 				/*
8797c478bd9Sstevel@tonic-gate 				 * Accept but ignore this -- gcc doesn't
8807c478bd9Sstevel@tonic-gate 				 * seem to complain about empty translation
8817c478bd9Sstevel@tonic-gate 				 * units
8827c478bd9Sstevel@tonic-gate 				 */
8837c478bd9Sstevel@tonic-gate 				break;
8847c478bd9Sstevel@tonic-gate 			}
8857c478bd9Sstevel@tonic-gate 			/* XX64 -- ignore all -erroff= options, for now */
8867c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "-erroff=", 8) == 0)
8877c478bd9Sstevel@tonic-gate 				break;
8887c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-errtags=yes") == 0) {
88980ab886dSwesolows 				warnings(ctx->i_ae);
8907c478bd9Sstevel@tonic-gate 				break;
8917c478bd9Sstevel@tonic-gate 			}
8927c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-errwarn=%all") == 0) {
89380ab886dSwesolows 				newae(ctx->i_ae, "-Werror");
8947c478bd9Sstevel@tonic-gate 				break;
8957c478bd9Sstevel@tonic-gate 			}
8967c478bd9Sstevel@tonic-gate 			error(arg);
8977c478bd9Sstevel@tonic-gate 			break;
8987c478bd9Sstevel@tonic-gate 		case 'f':
8997c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-flags") == 0) {
90080ab886dSwesolows 				newae(ctx->i_ae, "--help");
9017c478bd9Sstevel@tonic-gate 				break;
9027c478bd9Sstevel@tonic-gate 			}
9030e62f92eSMark Logan 			if (strncmp(arg, "-features=zla", 13) == 0) {
9040e62f92eSMark Logan 				/*
9050e62f92eSMark Logan 				 * Accept but ignore this -- gcc allows
9060e62f92eSMark Logan 				 * zero length arrays.
9070e62f92eSMark Logan 				 */
9080e62f92eSMark Logan 				break;
9090e62f92eSMark Logan 			}
9107c478bd9Sstevel@tonic-gate 			error(arg);
9117c478bd9Sstevel@tonic-gate 			break;
9127c478bd9Sstevel@tonic-gate 		case 'G':
91380ab886dSwesolows 			newae(ctx->i_ae, "-shared");
9147c478bd9Sstevel@tonic-gate 			nolibc = 1;
9157c478bd9Sstevel@tonic-gate 			break;
9167c478bd9Sstevel@tonic-gate 		case 'k':
9177c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-keeptmp") == 0) {
91880ab886dSwesolows 				newae(ctx->i_ae, "-save-temps");
9197c478bd9Sstevel@tonic-gate 				break;
9207c478bd9Sstevel@tonic-gate 			}
9217c478bd9Sstevel@tonic-gate 			error(arg);
9227c478bd9Sstevel@tonic-gate 			break;
9237c478bd9Sstevel@tonic-gate 		case 'K':
9247c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
92580ab886dSwesolows 				if ((arg = *++ctx->i_oldargv) == NULL ||
92680ab886dSwesolows 				    *arg == '\0')
9277c478bd9Sstevel@tonic-gate 					error("-K");
92880ab886dSwesolows 				ctx->i_oldargc--;
9297c478bd9Sstevel@tonic-gate 			} else {
9307c478bd9Sstevel@tonic-gate 				arg += 2;
9317c478bd9Sstevel@tonic-gate 			}
9327c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "pic") == 0) {
93380ab886dSwesolows 				newae(ctx->i_ae, "-fpic");
9347c478bd9Sstevel@tonic-gate 				pic = 1;
9357c478bd9Sstevel@tonic-gate 				break;
9367c478bd9Sstevel@tonic-gate 			}
9377c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "PIC") == 0) {
93880ab886dSwesolows 				newae(ctx->i_ae, "-fPIC");
9397c478bd9Sstevel@tonic-gate 				pic = 1;
9407c478bd9Sstevel@tonic-gate 				break;
9417c478bd9Sstevel@tonic-gate 			}
9427c478bd9Sstevel@tonic-gate 			error("-K");
9437c478bd9Sstevel@tonic-gate 			break;
9447c478bd9Sstevel@tonic-gate 		case 'm':
9457c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-mt") == 0) {
94680ab886dSwesolows 				newae(ctx->i_ae, "-D_REENTRANT");
9477c478bd9Sstevel@tonic-gate 				break;
9487c478bd9Sstevel@tonic-gate 			}
949e521259dSpetede 			if (strcmp(arg, "-m64") == 0) {
950e521259dSpetede 				newae(ctx->i_ae, "-m64");
951e521259dSpetede #if defined(__x86)
952e521259dSpetede 				newae(ctx->i_ae, "-mtune=opteron");
953e521259dSpetede #endif
954e521259dSpetede 				mflag |= M64;
955e521259dSpetede 				break;
956e521259dSpetede 			}
957e521259dSpetede 			if (strcmp(arg, "-m32") == 0) {
958e521259dSpetede 				newae(ctx->i_ae, "-m32");
959e521259dSpetede 				mflag |= M32;
960e521259dSpetede 				break;
961e521259dSpetede 			}
9627c478bd9Sstevel@tonic-gate 			error(arg);
9637c478bd9Sstevel@tonic-gate 			break;
9647c478bd9Sstevel@tonic-gate 		case 'B':	/* linker options */
9657c478bd9Sstevel@tonic-gate 		case 'M':
9667c478bd9Sstevel@tonic-gate 		case 'z':
9677c478bd9Sstevel@tonic-gate 			{
9687c478bd9Sstevel@tonic-gate 				char *opt;
9697c478bd9Sstevel@tonic-gate 				size_t len;
9707c478bd9Sstevel@tonic-gate 				char *s;
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 				if (arglen == 1) {
97380ab886dSwesolows 					opt = *++ctx->i_oldargv;
9747c478bd9Sstevel@tonic-gate 					if (opt == NULL || *opt == '\0')
9757c478bd9Sstevel@tonic-gate 						error(arg);
97680ab886dSwesolows 					ctx->i_oldargc--;
9777c478bd9Sstevel@tonic-gate 				} else {
9787c478bd9Sstevel@tonic-gate 					opt = arg + 2;
9797c478bd9Sstevel@tonic-gate 				}
9807c478bd9Sstevel@tonic-gate 				len = strlen(opt) + 7;
98180ab886dSwesolows 				if ((s = malloc(len)) == NULL)
98280ab886dSwesolows 					nomem();
9837c478bd9Sstevel@tonic-gate 				(void) snprintf(s, len, "-Wl,-%c%s", c, opt);
98480ab886dSwesolows 				newae(ctx->i_ae, s);
9857c478bd9Sstevel@tonic-gate 				free(s);
9867c478bd9Sstevel@tonic-gate 			}
9877c478bd9Sstevel@tonic-gate 			break;
9887c478bd9Sstevel@tonic-gate 		case 'n':
9897c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-noqueue") == 0) {
9907c478bd9Sstevel@tonic-gate 				/*
9917c478bd9Sstevel@tonic-gate 				 * Horrid license server stuff - n/a
9927c478bd9Sstevel@tonic-gate 				 */
9937c478bd9Sstevel@tonic-gate 				break;
9947c478bd9Sstevel@tonic-gate 			}
9957c478bd9Sstevel@tonic-gate 			error(arg);
9967c478bd9Sstevel@tonic-gate 			break;
9977c478bd9Sstevel@tonic-gate 		case 'O':
9987c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
99980ab886dSwesolows 				newae(ctx->i_ae, "-O");
10007c478bd9Sstevel@tonic-gate 				break;
10017c478bd9Sstevel@tonic-gate 			}
10027c478bd9Sstevel@tonic-gate 			error(arg);
10037c478bd9Sstevel@tonic-gate 			break;
10047c478bd9Sstevel@tonic-gate 		case 'P':
10057c478bd9Sstevel@tonic-gate 			/*
10067c478bd9Sstevel@tonic-gate 			 * We could do '-E -o filename.i', but that's hard,
10077c478bd9Sstevel@tonic-gate 			 * and we don't need it for the case that's triggering
10087c478bd9Sstevel@tonic-gate 			 * this addition.  We'll require the user to specify
10097c478bd9Sstevel@tonic-gate 			 * -o in the Makefile.  If they don't they'll find out
10107c478bd9Sstevel@tonic-gate 			 * in a hurry.
10117c478bd9Sstevel@tonic-gate 			 */
101280ab886dSwesolows 			newae(ctx->i_ae, "-E");
101380ab886dSwesolows 			op = CW_O_PREPROCESS;
10147c478bd9Sstevel@tonic-gate 			nolibc = 1;
10157c478bd9Sstevel@tonic-gate 			break;
10167c478bd9Sstevel@tonic-gate 		case 'q':
10177c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-qp") == 0) {
101880ab886dSwesolows 				newae(ctx->i_ae, "-p");
10197c478bd9Sstevel@tonic-gate 				break;
10207c478bd9Sstevel@tonic-gate 			}
10217c478bd9Sstevel@tonic-gate 			error(arg);
10227c478bd9Sstevel@tonic-gate 			break;
10237c478bd9Sstevel@tonic-gate 		case 's':
10247c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
102580ab886dSwesolows 				newae(ctx->i_ae, "-Wl,-s");
10267c478bd9Sstevel@tonic-gate 				break;
10277c478bd9Sstevel@tonic-gate 			}
10287c478bd9Sstevel@tonic-gate 			error(arg);
10297c478bd9Sstevel@tonic-gate 			break;
1030159cf8a6Swesolows 		case 't':
1031159cf8a6Swesolows 			if (arglen == 1) {
103280ab886dSwesolows 				newae(ctx->i_ae, "-Wl,-t");
1033159cf8a6Swesolows 				break;
1034159cf8a6Swesolows 			}
1035159cf8a6Swesolows 			error(arg);
1036159cf8a6Swesolows 			break;
10377c478bd9Sstevel@tonic-gate 		case 'V':
10387c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
103980ab886dSwesolows 				ctx->i_flags &= ~CW_F_ECHO;
104080ab886dSwesolows 				newae(ctx->i_ae, "--version");
10417c478bd9Sstevel@tonic-gate 				break;
10427c478bd9Sstevel@tonic-gate 			}
10437c478bd9Sstevel@tonic-gate 			error(arg);
10447c478bd9Sstevel@tonic-gate 			break;
10457c478bd9Sstevel@tonic-gate 		case 'v':
10467c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
104780ab886dSwesolows 				warnings(ctx->i_ae);
10487c478bd9Sstevel@tonic-gate 				break;
10497c478bd9Sstevel@tonic-gate 			}
10507c478bd9Sstevel@tonic-gate 			error(arg);
10517c478bd9Sstevel@tonic-gate 			break;
10527c478bd9Sstevel@tonic-gate 		case 'W':
10537c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "-Wp,-xc99", 9) == 0) {
10547c478bd9Sstevel@tonic-gate 				/*
10557c478bd9Sstevel@tonic-gate 				 * gcc's preprocessor will accept c99
10567c478bd9Sstevel@tonic-gate 				 * regardless, so accept and ignore.
10577c478bd9Sstevel@tonic-gate 				 */
10587c478bd9Sstevel@tonic-gate 				break;
10597c478bd9Sstevel@tonic-gate 			}
10607c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "-Wa,", 4) == 0 ||
10617c478bd9Sstevel@tonic-gate 			    strncmp(arg, "-Wp,", 4) == 0 ||
10627c478bd9Sstevel@tonic-gate 			    strncmp(arg, "-Wl,", 4) == 0) {
106380ab886dSwesolows 				newae(ctx->i_ae, arg);
10647c478bd9Sstevel@tonic-gate 				break;
10657c478bd9Sstevel@tonic-gate 			}
10667c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-W0,-xc99=pragma") == 0) {
10677c478bd9Sstevel@tonic-gate 				/* (undocumented) enables _Pragma */
10687c478bd9Sstevel@tonic-gate 				break;
10697c478bd9Sstevel@tonic-gate 			}
10707c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-W0,-xc99=%none") == 0) {
10717c478bd9Sstevel@tonic-gate 				/*
10727c478bd9Sstevel@tonic-gate 				 * This is a polite way of saying
10737c478bd9Sstevel@tonic-gate 				 * "no c99 constructs allowed!"
10747c478bd9Sstevel@tonic-gate 				 * For now, just accept and ignore this.
10757c478bd9Sstevel@tonic-gate 				 */
10767c478bd9Sstevel@tonic-gate 				break;
10777c478bd9Sstevel@tonic-gate 			}
10784afe1ab8Sesaxe 			if (strcmp(arg, "-W0,-noglobal") == 0 ||
10794afe1ab8Sesaxe 			    strcmp(arg, "-W0,-xglobalstatic") == 0) {
10807c478bd9Sstevel@tonic-gate 				/*
10817c478bd9Sstevel@tonic-gate 				 * gcc doesn't prefix local symbols
10827c478bd9Sstevel@tonic-gate 				 * in debug mode, so this is not needed.
10837c478bd9Sstevel@tonic-gate 				 */
10847c478bd9Sstevel@tonic-gate 				break;
10857c478bd9Sstevel@tonic-gate 			}
10867c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-W0,-Lt") == 0) {
10877c478bd9Sstevel@tonic-gate 				/*
10887c478bd9Sstevel@tonic-gate 				 * Generate tests at the top of loops.
10897c478bd9Sstevel@tonic-gate 				 * There is no direct gcc equivalent, ignore.
10907c478bd9Sstevel@tonic-gate 				 */
10917c478bd9Sstevel@tonic-gate 				break;
10927c478bd9Sstevel@tonic-gate 			}
10938c1a3716Ssherrym 			if (strcmp(arg, "-W0,-xdbggen=no%usedonly") == 0) {
109480ab886dSwesolows 				newae(ctx->i_ae,
109580ab886dSwesolows 				    "-fno-eliminate-unused-debug-symbols");
109680ab886dSwesolows 				newae(ctx->i_ae,
109780ab886dSwesolows 				    "-fno-eliminate-unused-debug-types");
10988c1a3716Ssherrym 				break;
10998c1a3716Ssherrym 			}
11009dd82889Spetede 			if (strcmp(arg, "-W2,-xwrap_int") == 0) {
11019dd82889Spetede 				/*
11029dd82889Spetede 				 * Use the legacy behaviour (pre-SS11)
11039dd82889Spetede 				 * for integer wrapping.
11049dd82889Spetede 				 * gcc does not need this.
11059dd82889Spetede 				 */
11069dd82889Spetede 				break;
11079dd82889Spetede 			}
11087c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-W2,-Rcond_elim") == 0) {
11097c478bd9Sstevel@tonic-gate 				/*
11107c478bd9Sstevel@tonic-gate 				 * Elimination and expansion of conditionals;
11117c478bd9Sstevel@tonic-gate 				 * gcc has no direct equivalent.
11127c478bd9Sstevel@tonic-gate 				 */
11137c478bd9Sstevel@tonic-gate 				break;
11147c478bd9Sstevel@tonic-gate 			}
11157c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-Wd,-xsafe=unboundsym") == 0) {
11167c478bd9Sstevel@tonic-gate 				/*
11177c478bd9Sstevel@tonic-gate 				 * Prevents optimizing away checks for
11187c478bd9Sstevel@tonic-gate 				 * unbound weak symbol addresses.  gcc does
11197c478bd9Sstevel@tonic-gate 				 * not do this, so it's not needed.
11207c478bd9Sstevel@tonic-gate 				 */
11217c478bd9Sstevel@tonic-gate 				break;
11227c478bd9Sstevel@tonic-gate 			}
11237c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "-Wc,-xcode=", 11) == 0) {
112480ab886dSwesolows 				xlate(ctx->i_ae, arg + 11, xcode_tbl);
11257c478bd9Sstevel@tonic-gate 				if (strncmp(arg + 11, "pic", 3) == 0)
11267c478bd9Sstevel@tonic-gate 					pic = 1;
11277c478bd9Sstevel@tonic-gate 				break;
11287c478bd9Sstevel@tonic-gate 			}
11297c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "-Wc,-Qiselect", 13) == 0) {
11307c478bd9Sstevel@tonic-gate 				/*
11317c478bd9Sstevel@tonic-gate 				 * Prevents insertion of register symbols.
11327c478bd9Sstevel@tonic-gate 				 * gcc doesn't do this, so ignore it.
11337c478bd9Sstevel@tonic-gate 				 */
11347c478bd9Sstevel@tonic-gate 				break;
11357c478bd9Sstevel@tonic-gate 			}
11369dd82889Spetede 			if (strcmp(arg, "-Wc,-Qassembler-ounrefsym=0") == 0) {
11379dd82889Spetede 				/*
11389dd82889Spetede 				 * Prevents optimizing away of static variables.
11399dd82889Spetede 				 * gcc does not do this, so it's not needed.
11409dd82889Spetede 				 */
11419dd82889Spetede 				break;
11429dd82889Spetede 			}
11437c478bd9Sstevel@tonic-gate #if defined(__x86)
11447c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-Wu,-xmodel=kernel") == 0) {
114580ab886dSwesolows 				newae(ctx->i_ae, "-ffreestanding");
114680ab886dSwesolows 				newae(ctx->i_ae, "-mno-red-zone");
11477c478bd9Sstevel@tonic-gate 				model = "-mcmodel=kernel";
11487c478bd9Sstevel@tonic-gate 				nolibc = 1;
11497c478bd9Sstevel@tonic-gate 				break;
11507c478bd9Sstevel@tonic-gate 			}
1151d430274bSsherrym 			if (strcmp(arg, "-Wu,-save_args") == 0) {
115280ab886dSwesolows 				newae(ctx->i_ae, "-msave-args");
1153d430274bSsherrym 				break;
1154d430274bSsherrym 			}
11557c478bd9Sstevel@tonic-gate #endif	/* __x86 */
11567c478bd9Sstevel@tonic-gate 			error(arg);
11577c478bd9Sstevel@tonic-gate 			break;
11587c478bd9Sstevel@tonic-gate 		case 'X':
11597c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-Xa") == 0 ||
11607c478bd9Sstevel@tonic-gate 			    strcmp(arg, "-Xt") == 0) {
116180ab886dSwesolows 				Xamode(ctx->i_ae);
11627c478bd9Sstevel@tonic-gate 				break;
11637c478bd9Sstevel@tonic-gate 			}
11647c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-Xc") == 0) {
116580ab886dSwesolows 				Xcmode(ctx->i_ae);
11667c478bd9Sstevel@tonic-gate 				break;
11677c478bd9Sstevel@tonic-gate 			}
11687c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-Xs") == 0) {
116980ab886dSwesolows 				Xsmode(ctx->i_ae);
11707c478bd9Sstevel@tonic-gate 				break;
11717c478bd9Sstevel@tonic-gate 			}
11727c478bd9Sstevel@tonic-gate 			error(arg);
11737c478bd9Sstevel@tonic-gate 			break;
11747c478bd9Sstevel@tonic-gate 		case 'x':
11757c478bd9Sstevel@tonic-gate 			if (arglen == 1)
11767c478bd9Sstevel@tonic-gate 				error(arg);
11777c478bd9Sstevel@tonic-gate 			switch (arg[2]) {
11787c478bd9Sstevel@tonic-gate #if defined(__x86)
11797c478bd9Sstevel@tonic-gate 			case '3':
11807c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-x386") == 0) {
118180ab886dSwesolows 					newae(ctx->i_ae, "-march=i386");
11827c478bd9Sstevel@tonic-gate 					break;
11837c478bd9Sstevel@tonic-gate 				}
11847c478bd9Sstevel@tonic-gate 				error(arg);
11857c478bd9Sstevel@tonic-gate 				break;
11867c478bd9Sstevel@tonic-gate 			case '4':
11877c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-x486") == 0) {
118880ab886dSwesolows 					newae(ctx->i_ae, "-march=i486");
11897c478bd9Sstevel@tonic-gate 					break;
11907c478bd9Sstevel@tonic-gate 				}
11917c478bd9Sstevel@tonic-gate 				error(arg);
11927c478bd9Sstevel@tonic-gate 				break;
11937c478bd9Sstevel@tonic-gate #endif	/* __x86 */
11947c478bd9Sstevel@tonic-gate 			case 'a':
11957c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xarch=", 7) == 0) {
1196e521259dSpetede 					mflag |= xlate_xtb(ctx->i_ae, arg + 7);
11977c478bd9Sstevel@tonic-gate 					break;
11987c478bd9Sstevel@tonic-gate 				}
11997c478bd9Sstevel@tonic-gate 				error(arg);
12007c478bd9Sstevel@tonic-gate 				break;
120102e56f3fSwesolows 			case 'b':
120202e56f3fSwesolows 				if (strncmp(arg, "-xbuiltin=", 10) == 0) {
120302e56f3fSwesolows 					if (strcmp(arg + 10, "%all"))
120480ab886dSwesolows 						newae(ctx->i_ae, "-fbuiltin");
120502e56f3fSwesolows 					break;
120602e56f3fSwesolows 				}
120702e56f3fSwesolows 				error(arg);
120802e56f3fSwesolows 				break;
12097c478bd9Sstevel@tonic-gate 			case 'C':
12107c478bd9Sstevel@tonic-gate 				/* Accept C++ style comments -- ignore */
12117c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-xCC") == 0)
12127c478bd9Sstevel@tonic-gate 					break;
12137c478bd9Sstevel@tonic-gate 				error(arg);
12147c478bd9Sstevel@tonic-gate 				break;
12157c478bd9Sstevel@tonic-gate 			case 'c':
12167c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xc99=%all", 10) == 0) {
121780ab886dSwesolows 					newae(ctx->i_ae, "-std=gnu99");
12187c478bd9Sstevel@tonic-gate 					break;
12197c478bd9Sstevel@tonic-gate 				}
12207c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xc99=%none", 11) == 0) {
122180ab886dSwesolows 					newae(ctx->i_ae, "-std=gnu89");
12227c478bd9Sstevel@tonic-gate 					break;
12237c478bd9Sstevel@tonic-gate 				}
12247c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xchip=", 7) == 0) {
122580ab886dSwesolows 					xlate(ctx->i_ae, arg + 7, xchip_tbl);
12267c478bd9Sstevel@tonic-gate 					break;
12277c478bd9Sstevel@tonic-gate 				}
12287c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xcode=", 7) == 0) {
122980ab886dSwesolows 					xlate(ctx->i_ae, arg + 7, xcode_tbl);
12307c478bd9Sstevel@tonic-gate 					if (strncmp(arg + 7, "pic", 3) == 0)
12317c478bd9Sstevel@tonic-gate 						pic = 1;
12327c478bd9Sstevel@tonic-gate 					break;
12337c478bd9Sstevel@tonic-gate 				}
12347c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xcache=", 8) == 0)
12357c478bd9Sstevel@tonic-gate 					break;
1236159cf8a6Swesolows 				if (strncmp(arg, "-xcrossfile", 11) == 0)
1237159cf8a6Swesolows 					break;
12387c478bd9Sstevel@tonic-gate 				error(arg);
12397c478bd9Sstevel@tonic-gate 				break;
12407c478bd9Sstevel@tonic-gate 			case 'd':
12417c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-xdepend") == 0)
12427c478bd9Sstevel@tonic-gate 					break;
12438c1a3716Ssherrym 				if (strncmp(arg, "-xdebugformat=", 14) == 0)
12448c1a3716Ssherrym 					break;
12457c478bd9Sstevel@tonic-gate 				error(arg);
12467c478bd9Sstevel@tonic-gate 				break;
12477c478bd9Sstevel@tonic-gate 			case 'F':
1248f795e658Srie 				/*
1249f795e658Srie 				 * Compile for mapfile reordering, or unused
1250f795e658Srie 				 * section elimination, syntax can be -xF or
1251f795e658Srie 				 * more complex, like -xF=%all -- ignore.
1252f795e658Srie 				 */
1253f795e658Srie 				if (strncmp(arg, "-xF", 3) == 0)
12547c478bd9Sstevel@tonic-gate 					break;
12557c478bd9Sstevel@tonic-gate 				error(arg);
12567c478bd9Sstevel@tonic-gate 				break;
12577c478bd9Sstevel@tonic-gate 			case 'i':
12587c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xinline", 8) == 0)
12597c478bd9Sstevel@tonic-gate 					/* No inlining; ignore */
12607c478bd9Sstevel@tonic-gate 					break;
12617c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-xildon") == 0 ||
12627c478bd9Sstevel@tonic-gate 				    strcmp(arg, "-xildoff") == 0)
12637c478bd9Sstevel@tonic-gate 					/* No incremental linking; ignore */
12647c478bd9Sstevel@tonic-gate 					break;
12657c478bd9Sstevel@tonic-gate 				error(arg);
12667c478bd9Sstevel@tonic-gate 				break;
126754836668Spetede #if defined(__x86)
126854836668Spetede 			case 'm':
126954836668Spetede 				if (strcmp(arg, "-xmodel=kernel") == 0) {
127054836668Spetede 					newae(ctx->i_ae, "-ffreestanding");
127154836668Spetede 					newae(ctx->i_ae, "-mno-red-zone");
127254836668Spetede 					model = "-mcmodel=kernel";
127354836668Spetede 					nolibc = 1;
127454836668Spetede 					break;
127554836668Spetede 				}
127654836668Spetede 				error(arg);
127754836668Spetede 				break;
127854836668Spetede #endif	/* __x86 */
12797c478bd9Sstevel@tonic-gate 			case 'M':
12807c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-xM") == 0) {
128180ab886dSwesolows 					newae(ctx->i_ae, "-M");
12827c478bd9Sstevel@tonic-gate 					break;
12837c478bd9Sstevel@tonic-gate 				}
12847c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-xM1") == 0) {
128580ab886dSwesolows 					newae(ctx->i_ae, "-MM");
12867c478bd9Sstevel@tonic-gate 					break;
12877c478bd9Sstevel@tonic-gate 				}
12887c478bd9Sstevel@tonic-gate 				error(arg);
12897c478bd9Sstevel@tonic-gate 				break;
12907c478bd9Sstevel@tonic-gate 			case 'n':
12917c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-xnolib") == 0) {
12927c478bd9Sstevel@tonic-gate 					nolibc = 1;
12937c478bd9Sstevel@tonic-gate 					break;
12947c478bd9Sstevel@tonic-gate 				}
12957c478bd9Sstevel@tonic-gate 				error(arg);
12967c478bd9Sstevel@tonic-gate 				break;
12977c478bd9Sstevel@tonic-gate 			case 'O':
12987c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xO", 3) == 0) {
12997c478bd9Sstevel@tonic-gate 					size_t len = strlen(arg);
130080ab886dSwesolows 					char *s;
13017c478bd9Sstevel@tonic-gate 					int c = *(arg + 3);
13027c478bd9Sstevel@tonic-gate 					int level;
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 					if (len != 4 || !isdigit(c))
13057c478bd9Sstevel@tonic-gate 						error(arg);
13067c478bd9Sstevel@tonic-gate 
130780ab886dSwesolows 					if ((s = malloc(len)) == NULL)
130880ab886dSwesolows 						nomem();
130980ab886dSwesolows 
13107c478bd9Sstevel@tonic-gate 					level = atoi(arg + 3);
13117c478bd9Sstevel@tonic-gate 					if (level > 5)
13127c478bd9Sstevel@tonic-gate 						error(arg);
13137c478bd9Sstevel@tonic-gate 					if (level >= 2) {
13147c478bd9Sstevel@tonic-gate 						/*
13157c478bd9Sstevel@tonic-gate 						 * For gcc-3.4.x at -O2 we
13167c478bd9Sstevel@tonic-gate 						 * need to disable optimizations
13177c478bd9Sstevel@tonic-gate 						 * that break ON.
13187c478bd9Sstevel@tonic-gate 						 */
131980ab886dSwesolows 						optim_disable(ctx->i_ae, level);
13207c478bd9Sstevel@tonic-gate 						/*
13217c478bd9Sstevel@tonic-gate 						 * limit -xO3 to -O2 as well.
13227c478bd9Sstevel@tonic-gate 						 */
13237c478bd9Sstevel@tonic-gate 						level = 2;
13247c478bd9Sstevel@tonic-gate 					}
13257c478bd9Sstevel@tonic-gate 					(void) snprintf(s, len, "-O%d", level);
132680ab886dSwesolows 					newae(ctx->i_ae, s);
13277c478bd9Sstevel@tonic-gate 					free(s);
13287c478bd9Sstevel@tonic-gate 					break;
13297c478bd9Sstevel@tonic-gate 				}
13307c478bd9Sstevel@tonic-gate 				error(arg);
13317c478bd9Sstevel@tonic-gate 				break;
13327c478bd9Sstevel@tonic-gate 			case 'p':
13337c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-xpentium") == 0) {
133480ab886dSwesolows 					newae(ctx->i_ae, "-march=pentium");
13357c478bd9Sstevel@tonic-gate 					break;
13367c478bd9Sstevel@tonic-gate 				}
13377c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-xpg") == 0) {
133880ab886dSwesolows 					newae(ctx->i_ae, "-pg");
13397c478bd9Sstevel@tonic-gate 					break;
13407c478bd9Sstevel@tonic-gate 				}
13417c478bd9Sstevel@tonic-gate 				error(arg);
13427c478bd9Sstevel@tonic-gate 				break;
13437c478bd9Sstevel@tonic-gate 			case 'r':
13447c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xregs=", 7) == 0) {
134580ab886dSwesolows 					xlate(ctx->i_ae, arg + 7, xregs_tbl);
13467c478bd9Sstevel@tonic-gate 					break;
13477c478bd9Sstevel@tonic-gate 				}
13487c478bd9Sstevel@tonic-gate 				error(arg);
13497c478bd9Sstevel@tonic-gate 				break;
13507c478bd9Sstevel@tonic-gate 			case 's':
13517c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-xs") == 0 ||
13527c478bd9Sstevel@tonic-gate 				    strcmp(arg, "-xspace") == 0 ||
13537c478bd9Sstevel@tonic-gate 				    strcmp(arg, "-xstrconst") == 0)
13547c478bd9Sstevel@tonic-gate 					break;
13557c478bd9Sstevel@tonic-gate 				error(arg);
13567c478bd9Sstevel@tonic-gate 				break;
13577c478bd9Sstevel@tonic-gate 			case 't':
13587c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-xtransition") == 0) {
135980ab886dSwesolows 					newae(ctx->i_ae, "-Wtransition");
13607c478bd9Sstevel@tonic-gate 					break;
13617c478bd9Sstevel@tonic-gate 				}
13627c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-xtrigraphs=yes") == 0) {
136380ab886dSwesolows 					newae(ctx->i_ae, "-trigraphs");
13647c478bd9Sstevel@tonic-gate 					break;
13657c478bd9Sstevel@tonic-gate 				}
13667c478bd9Sstevel@tonic-gate 				if (strcmp(arg, "-xtrigraphs=no") == 0) {
136780ab886dSwesolows 					newae(ctx->i_ae, "-notrigraphs");
13687c478bd9Sstevel@tonic-gate 					break;
13697c478bd9Sstevel@tonic-gate 				}
13707c478bd9Sstevel@tonic-gate 				if (strncmp(arg, "-xtarget=", 9) == 0) {
137180ab886dSwesolows 					xlate(ctx->i_ae, arg + 9, xtarget_tbl);
13727c478bd9Sstevel@tonic-gate 					break;
13737c478bd9Sstevel@tonic-gate 				}
13747c478bd9Sstevel@tonic-gate 				error(arg);
13757c478bd9Sstevel@tonic-gate 				break;
13767c478bd9Sstevel@tonic-gate 			case 'e':
13777c478bd9Sstevel@tonic-gate 			case 'h':
13787c478bd9Sstevel@tonic-gate 			case 'l':
13797c478bd9Sstevel@tonic-gate 			default:
13807c478bd9Sstevel@tonic-gate 				error(arg);
13817c478bd9Sstevel@tonic-gate 				break;
13827c478bd9Sstevel@tonic-gate 			}
13837c478bd9Sstevel@tonic-gate 			break;
13847c478bd9Sstevel@tonic-gate 		case 'Y':
13857c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
138680ab886dSwesolows 				if ((arg = *++ctx->i_oldargv) == NULL ||
138780ab886dSwesolows 				    *arg == '\0')
13887c478bd9Sstevel@tonic-gate 					error("-Y");
138980ab886dSwesolows 				ctx->i_oldargc--;
13907c478bd9Sstevel@tonic-gate 				arglen = strlen(arg + 1);
13917c478bd9Sstevel@tonic-gate 			} else {
13927c478bd9Sstevel@tonic-gate 				arg += 2;
13937c478bd9Sstevel@tonic-gate 			}
13947c478bd9Sstevel@tonic-gate 			/* Just ignore -YS,... for now */
13957c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "S,", 2) == 0)
13967c478bd9Sstevel@tonic-gate 				break;
13977c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "l,", 2) == 0) {
13987c478bd9Sstevel@tonic-gate 				char *s = strdup(arg);
13997c478bd9Sstevel@tonic-gate 				s[0] = '-';
14007c478bd9Sstevel@tonic-gate 				s[1] = 'B';
140180ab886dSwesolows 				newae(ctx->i_ae, s);
14027c478bd9Sstevel@tonic-gate 				free(s);
14037c478bd9Sstevel@tonic-gate 				break;
14047c478bd9Sstevel@tonic-gate 			}
14057c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "I,", 2) == 0) {
14067c478bd9Sstevel@tonic-gate 				char *s = strdup(arg);
14077c478bd9Sstevel@tonic-gate 				s[0] = '-';
14087c478bd9Sstevel@tonic-gate 				s[1] = 'I';
140980ab886dSwesolows 				newae(ctx->i_ae, "-nostdinc");
141080ab886dSwesolows 				newae(ctx->i_ae, s);
14117c478bd9Sstevel@tonic-gate 				free(s);
14127c478bd9Sstevel@tonic-gate 				break;
14137c478bd9Sstevel@tonic-gate 			}
14147c478bd9Sstevel@tonic-gate 			error(arg);
14157c478bd9Sstevel@tonic-gate 			break;
14167c478bd9Sstevel@tonic-gate 		case 'Q':
14177c478bd9Sstevel@tonic-gate 			/*
14187c478bd9Sstevel@tonic-gate 			 * We could map -Qy into -Wl,-Qy etc.
14197c478bd9Sstevel@tonic-gate 			 */
14207c478bd9Sstevel@tonic-gate 		default:
14217c478bd9Sstevel@tonic-gate 			error(arg);
14227c478bd9Sstevel@tonic-gate 			break;
14237c478bd9Sstevel@tonic-gate 		}
14247c478bd9Sstevel@tonic-gate 	}
14257c478bd9Sstevel@tonic-gate 
142680ab886dSwesolows 	if (c_files > 1 && (ctx->i_flags & CW_F_SHADOW) &&
142780ab886dSwesolows 	    op != CW_O_PREPROCESS) {
142880ab886dSwesolows 		(void) fprintf(stderr, "%s: error: multiple source files are "
142980ab886dSwesolows 		    "allowed only with -E or -P\n", progname);
143080ab886dSwesolows 		exit(2);
143180ab886dSwesolows 	}
1432e521259dSpetede 
1433e521259dSpetede 	/*
1434e521259dSpetede 	 * Make sure that we do not have any unintended interactions between
1435e521259dSpetede 	 * the xarch options passed in and the version of the Studio compiler
1436e521259dSpetede 	 * used.
1437e521259dSpetede 	 */
1438e521259dSpetede 	if ((mflag & (SS11|SS12)) == (SS11|SS12)) {
1439e521259dSpetede 		(void) fprintf(stderr,
1440e521259dSpetede 		    "Conflicting \"-xarch=\" flags (both Studio 11 and 12)\n");
1441e521259dSpetede 		exit(2);
1442e521259dSpetede 	}
1443e521259dSpetede 
1444e521259dSpetede 	switch (mflag) {
1445e521259dSpetede 	case 0:
1446e521259dSpetede 		/* FALLTHROUGH */
1447e521259dSpetede 	case M32:
1448e521259dSpetede #if defined(__sparc)
1449e521259dSpetede 		/*
1450e521259dSpetede 		 * Only -m32 is defined and so put in the missing xarch
1451e521259dSpetede 		 * translation.
1452e521259dSpetede 		 */
1453e521259dSpetede 		newae(ctx->i_ae, "-mcpu=v8");
1454e521259dSpetede 		newae(ctx->i_ae, "-mno-v8plus");
1455e521259dSpetede #endif
1456e521259dSpetede 		break;
1457e521259dSpetede 	case M64:
1458e521259dSpetede #if defined(__sparc)
1459e521259dSpetede 		/*
1460e521259dSpetede 		 * Only -m64 is defined and so put in the missing xarch
1461e521259dSpetede 		 * translation.
1462e521259dSpetede 		 */
1463e521259dSpetede 		newae(ctx->i_ae, "-mcpu=v9");
1464e521259dSpetede #endif
1465e521259dSpetede 		break;
1466e521259dSpetede 	case SS12:
1467e521259dSpetede #if defined(__sparc)
1468e521259dSpetede 		/* no -m32/-m64 flag used - this is an error for sparc builds */
1469e521259dSpetede 		(void) fprintf(stderr, "No -m32/-m64 flag defined\n");
1470e521259dSpetede 		exit(2);
1471e521259dSpetede #endif
1472e521259dSpetede 		break;
1473e521259dSpetede 	case SS11:
1474e521259dSpetede 		/* FALLTHROUGH */
1475e521259dSpetede 	case (SS11|M32):
1476e521259dSpetede 	case (SS11|M64):
1477e521259dSpetede 		break;
1478e521259dSpetede 	case (SS12|M32):
1479e521259dSpetede #if defined(__sparc)
1480e521259dSpetede 		/*
1481e521259dSpetede 		 * Need to add in further 32 bit options because with SS12
1482e521259dSpetede 		 * the xarch=sparcvis option can be applied to 32 or 64
1483e521259dSpetede 		 * bit, and so the translatation table (xtbl) cannot handle
1484e521259dSpetede 		 * that.
1485e521259dSpetede 		 */
1486e521259dSpetede 		newae(ctx->i_ae, "-mv8plus");
1487e521259dSpetede #endif
1488e521259dSpetede 		break;
1489e521259dSpetede 	case (SS12|M64):
1490e521259dSpetede 		break;
1491e521259dSpetede 	default:
1492e521259dSpetede 		(void) fprintf(stderr,
14937a6460b6Spetede 		    "Incompatible -xarch= and/or -m32/-m64 options used.\n");
1494e521259dSpetede 		exit(2);
1495e521259dSpetede 	}
149680ab886dSwesolows 	if (op == CW_O_LINK && (ctx->i_flags & CW_F_SHADOW))
149780ab886dSwesolows 		exit(0);
149880ab886dSwesolows 
14997c478bd9Sstevel@tonic-gate 	if (model && !pic)
150080ab886dSwesolows 		newae(ctx->i_ae, model);
15017c478bd9Sstevel@tonic-gate 	if (!nolibc)
150280ab886dSwesolows 		newae(ctx->i_ae, "-lc");
150380ab886dSwesolows 	if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
150480ab886dSwesolows 		newae(ctx->i_ae, "-o");
150580ab886dSwesolows 		newae(ctx->i_ae, ctx->i_discard);
150680ab886dSwesolows 	}
150780ab886dSwesolows }
150880ab886dSwesolows 
150980ab886dSwesolows static void
151080ab886dSwesolows do_cc(cw_ictx_t *ctx)
151180ab886dSwesolows {
151280ab886dSwesolows 	int in_output = 0, seen_o = 0;
151380ab886dSwesolows 	cw_op_t op = CW_O_LINK;
151480ab886dSwesolows 
15151912d2c4Swesolows 	if (ctx->i_flags & CW_F_PROG) {
15161912d2c4Swesolows 		newae(ctx->i_ae, "-V");
15171912d2c4Swesolows 		return;
15181912d2c4Swesolows 	}
15191912d2c4Swesolows 
152080ab886dSwesolows 	while (--ctx->i_oldargc > 0) {
152180ab886dSwesolows 		char *arg = *++ctx->i_oldargv;
152280ab886dSwesolows 
152380ab886dSwesolows 		if (*arg != '-') {
152480ab886dSwesolows 			if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) {
152580ab886dSwesolows 				newae(ctx->i_ae, arg);
152680ab886dSwesolows 			} else {
152780ab886dSwesolows 				in_output = 0;
152880ab886dSwesolows 				newae(ctx->i_ae, ctx->i_discard);
152980ab886dSwesolows 			}
153080ab886dSwesolows 			continue;
153180ab886dSwesolows 		}
153280ab886dSwesolows 		switch (*(arg + 1)) {
153380ab886dSwesolows 		case '_':
153480ab886dSwesolows 			if (strcmp(arg, "-_noecho") == 0) {
153580ab886dSwesolows 				ctx->i_flags &= ~CW_F_ECHO;
153680ab886dSwesolows 			} else if (strncmp(arg, "-_cc=", 5) == 0 ||
153780ab886dSwesolows 			    strncmp(arg, "-_CC=", 5) == 0) {
153880ab886dSwesolows 				newae(ctx->i_ae, arg + 5);
153980ab886dSwesolows 			} else if (strncmp(arg, "-_gcc=", 6) != 0 &&
154080ab886dSwesolows 			    strncmp(arg, "-_g++=", 6) != 0) {
154180ab886dSwesolows 				(void) fprintf(stderr,
154280ab886dSwesolows 				    "%s: invalid argument '%s'\n", progname,
154380ab886dSwesolows 				    arg);
154480ab886dSwesolows 				exit(2);
154580ab886dSwesolows 			}
154680ab886dSwesolows 			break;
154780ab886dSwesolows 		case 'V':
154880ab886dSwesolows 			ctx->i_flags &= ~CW_F_ECHO;
154980ab886dSwesolows 			newae(ctx->i_ae, arg);
155080ab886dSwesolows 			break;
155180ab886dSwesolows 		case 'o':
155280ab886dSwesolows 			seen_o = 1;
155380ab886dSwesolows 			if (strlen(arg) == 2) {
155480ab886dSwesolows 				in_output = 1;
155580ab886dSwesolows 				newae(ctx->i_ae, arg);
155680ab886dSwesolows 			} else if (ctx->i_flags & CW_F_SHADOW) {
155780ab886dSwesolows 				newae(ctx->i_ae, "-o");
155880ab886dSwesolows 				newae(ctx->i_ae, ctx->i_discard);
155980ab886dSwesolows 			} else {
156080ab886dSwesolows 				newae(ctx->i_ae, arg);
156180ab886dSwesolows 			}
156280ab886dSwesolows 			break;
156380ab886dSwesolows 		case 'c':
156480ab886dSwesolows 		case 'S':
15650bb3415fSrie 			if (strlen(arg) == 2)
15660bb3415fSrie 				op = CW_O_COMPILE;
156780ab886dSwesolows 			newae(ctx->i_ae, arg);
156880ab886dSwesolows 			break;
156980ab886dSwesolows 		case 'E':
157080ab886dSwesolows 		case 'P':
15710bb3415fSrie 			if (strlen(arg) == 2)
15720bb3415fSrie 				op = CW_O_PREPROCESS;
157380ab886dSwesolows 		/*FALLTHROUGH*/
157480ab886dSwesolows 		default:
157580ab886dSwesolows 			newae(ctx->i_ae, arg);
157680ab886dSwesolows 		}
157780ab886dSwesolows 	}
157880ab886dSwesolows 
157980ab886dSwesolows 	if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
158080ab886dSwesolows 	    (ctx->i_flags & CW_F_SHADOW))
158180ab886dSwesolows 		exit(0);
158280ab886dSwesolows 
158380ab886dSwesolows 	if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
158480ab886dSwesolows 		newae(ctx->i_ae, "-o");
158580ab886dSwesolows 		newae(ctx->i_ae, ctx->i_discard);
158680ab886dSwesolows 	}
15877c478bd9Sstevel@tonic-gate }
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate static void
159080ab886dSwesolows prepctx(cw_ictx_t *ctx)
15917c478bd9Sstevel@tonic-gate {
15927fbf8d03SScott Rotondo 	const char *dir = NULL, *cmd;
15937fbf8d03SScott Rotondo 	char *program = NULL;
159480ab886dSwesolows 	size_t len;
15957c478bd9Sstevel@tonic-gate 
15967fbf8d03SScott Rotondo 	switch (CIDX(CC(ctx), ctx->i_flags)) {
15977fbf8d03SScott Rotondo 		case CIDX(CW_C_CC, 0):
15987fbf8d03SScott Rotondo 			program = getenv("CW_CC");
15997fbf8d03SScott Rotondo 			dir = getenv("CW_CC_DIR");
16007fbf8d03SScott Rotondo 			break;
16017fbf8d03SScott Rotondo 		case CIDX(CW_C_CC, CW_F_CXX):
16027fbf8d03SScott Rotondo 			program = getenv("CW_CPLUSPLUS");
16037fbf8d03SScott Rotondo 			dir = getenv("CW_CPLUSPLUS_DIR");
16047fbf8d03SScott Rotondo 			break;
16057fbf8d03SScott Rotondo 		case CIDX(CW_C_GCC, 0):
16067fbf8d03SScott Rotondo 			program = getenv("CW_GCC");
16077fbf8d03SScott Rotondo 			dir = getenv("CW_GCC_DIR");
16087fbf8d03SScott Rotondo 			break;
16097fbf8d03SScott Rotondo 		case CIDX(CW_C_GCC, CW_F_CXX):
16107fbf8d03SScott Rotondo 			program = getenv("CW_GPLUSPLUS");
16117fbf8d03SScott Rotondo 			dir = getenv("CW_GPLUSPLUS_DIR");
16127fbf8d03SScott Rotondo 			break;
16137fbf8d03SScott Rotondo 	}
16147fbf8d03SScott Rotondo 
16157fbf8d03SScott Rotondo 	if (program == NULL) {
16167fbf8d03SScott Rotondo 		if (dir == NULL)
16177fbf8d03SScott Rotondo 			dir = default_dir[CC(ctx)];
16187fbf8d03SScott Rotondo 		cmd = cmds[CIDX(CC(ctx), ctx->i_flags)];
16197fbf8d03SScott Rotondo 		len = strlen(dir) + strlen(cmd) + 2;
16207fbf8d03SScott Rotondo 		if ((program = malloc(len)) == NULL)
16217fbf8d03SScott Rotondo 			nomem();
16227fbf8d03SScott Rotondo 		(void) snprintf(program, len, "%s/%s", dir, cmd);
16237fbf8d03SScott Rotondo 	}
16247c478bd9Sstevel@tonic-gate 
162580ab886dSwesolows 	newae(ctx->i_ae, program);
162680ab886dSwesolows 
16271912d2c4Swesolows 	if (ctx->i_flags & CW_F_PROG) {
16281912d2c4Swesolows 		(void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ?
16291912d2c4Swesolows 		    "shadow" : "primary", program);
16301912d2c4Swesolows 		(void) fflush(stdout);
16311912d2c4Swesolows 	}
16321912d2c4Swesolows 
163380ab886dSwesolows 	if (!(ctx->i_flags & CW_F_XLATE))
163480ab886dSwesolows 		return;
163580ab886dSwesolows 
163680ab886dSwesolows 	switch (CC(ctx)) {
163780ab886dSwesolows 	case CW_C_CC:
163880ab886dSwesolows 		do_cc(ctx);
163980ab886dSwesolows 		break;
164080ab886dSwesolows 	case CW_C_GCC:
164180ab886dSwesolows 		do_gcc(ctx);
164280ab886dSwesolows 		break;
164380ab886dSwesolows 	}
164480ab886dSwesolows }
164580ab886dSwesolows 
164680ab886dSwesolows static int
164780ab886dSwesolows invoke(cw_ictx_t *ctx)
164880ab886dSwesolows {
164980ab886dSwesolows 	char **newargv;
165080ab886dSwesolows 	int ac;
165180ab886dSwesolows 	struct ae *a;
165280ab886dSwesolows 
165380ab886dSwesolows 	if ((newargv = calloc(sizeof (*newargv), ctx->i_ae->ael_argc + 1)) ==
165480ab886dSwesolows 	    NULL)
165580ab886dSwesolows 		nomem();
165680ab886dSwesolows 
165780ab886dSwesolows 	if (ctx->i_flags & CW_F_ECHO)
165880ab886dSwesolows 		(void) fprintf(stderr, "+ ");
165980ab886dSwesolows 
166080ab886dSwesolows 	for (ac = 0, a = ctx->i_ae->ael_head; a; a = a->ae_next, ac++) {
166180ab886dSwesolows 		newargv[ac] = a->ae_arg;
166280ab886dSwesolows 		if (ctx->i_flags & CW_F_ECHO)
166380ab886dSwesolows 			(void) fprintf(stderr, "%s ", a->ae_arg);
166480ab886dSwesolows 		if (a == ctx->i_ae->ael_tail)
166580ab886dSwesolows 			break;
166680ab886dSwesolows 	}
166780ab886dSwesolows 
166880ab886dSwesolows 	if (ctx->i_flags & CW_F_ECHO) {
166980ab886dSwesolows 		(void) fprintf(stderr, "\n");
167080ab886dSwesolows 		(void) fflush(stderr);
167180ab886dSwesolows 	}
167280ab886dSwesolows 
167380ab886dSwesolows 	if (!(ctx->i_flags & CW_F_EXEC))
167480ab886dSwesolows 		return (0);
167580ab886dSwesolows 
16767c478bd9Sstevel@tonic-gate 	/*
167780ab886dSwesolows 	 * We must fix up the environment here so that the
167880ab886dSwesolows 	 * dependency files are not trampled by the shadow compiler.
16797c478bd9Sstevel@tonic-gate 	 */
168080ab886dSwesolows 	if ((ctx->i_flags & CW_F_SHADOW) &&
168180ab886dSwesolows 	    (unsetenv("SUNPRO_DEPENDENCIES") != 0 ||
168280ab886dSwesolows 	    unsetenv("DEPENDENCIES_OUTPUT") != 0)) {
168380ab886dSwesolows 		(void) fprintf(stderr, "error: environment setup failed: %s\n",
168480ab886dSwesolows 		    strerror(errno));
168580ab886dSwesolows 		return (-1);
168680ab886dSwesolows 	}
16877c478bd9Sstevel@tonic-gate 
168880ab886dSwesolows 	(void) execv(newargv[0], newargv);
168980ab886dSwesolows 	cw_perror("couldn't run %s", newargv[0]);
16907c478bd9Sstevel@tonic-gate 
169180ab886dSwesolows 	return (-1);
169280ab886dSwesolows }
169380ab886dSwesolows 
169480ab886dSwesolows static int
169580ab886dSwesolows reap(cw_ictx_t *ctx)
169680ab886dSwesolows {
16971912d2c4Swesolows 	int status, ret = 0;
169880ab886dSwesolows 	char buf[1024];
169980ab886dSwesolows 	struct stat s;
170080ab886dSwesolows 
1701c3f177eaSPeter Dennis - Sustaining Engineer 	/*
1702c3f177eaSPeter Dennis - Sustaining Engineer 	 * Only wait for one specific child.
1703c3f177eaSPeter Dennis - Sustaining Engineer 	 */
1704c3f177eaSPeter Dennis - Sustaining Engineer 	if (ctx->i_pid <= 0)
1705c3f177eaSPeter Dennis - Sustaining Engineer 		return (-1);
1706c3f177eaSPeter Dennis - Sustaining Engineer 
170780ab886dSwesolows 	do {
1708c3f177eaSPeter Dennis - Sustaining Engineer 		if (waitpid(ctx->i_pid, &status, 0) < 0) {
1709c3f177eaSPeter Dennis - Sustaining Engineer 			cw_perror("cannot reap child");
1710c3f177eaSPeter Dennis - Sustaining Engineer 			return (-1);
1711c3f177eaSPeter Dennis - Sustaining Engineer 		}
17121912d2c4Swesolows 		if (status != 0) {
17131912d2c4Swesolows 			if (WIFSIGNALED(status)) {
17141912d2c4Swesolows 				ret = -WTERMSIG(status);
171580ab886dSwesolows 				break;
17161912d2c4Swesolows 			} else if (WIFEXITED(status)) {
17171912d2c4Swesolows 				ret = WEXITSTATUS(status);
171880ab886dSwesolows 				break;
171980ab886dSwesolows 			}
17207c478bd9Sstevel@tonic-gate 		}
17211912d2c4Swesolows 	} while (!WIFEXITED(status) && !WIFSIGNALED(status));
172280ab886dSwesolows 
172380ab886dSwesolows 	(void) unlink(ctx->i_discard);
172480ab886dSwesolows 
17251912d2c4Swesolows 	if (stat(ctx->i_stderr, &s) < 0) {
172680ab886dSwesolows 		cw_perror("stat failed on child cleanup");
172780ab886dSwesolows 		return (-1);
17287c478bd9Sstevel@tonic-gate 	}
172980ab886dSwesolows 	if (s.st_size != 0) {
17301912d2c4Swesolows 		FILE *f;
173180ab886dSwesolows 
17321912d2c4Swesolows 		if ((f = fopen(ctx->i_stderr, "r")) != NULL) {
17331912d2c4Swesolows 			while (fgets(buf, sizeof (buf), f))
17341912d2c4Swesolows 				(void) fprintf(stderr, "%s", buf);
17351912d2c4Swesolows 			(void) fflush(stderr);
17361912d2c4Swesolows 			(void) fclose(f);
17371912d2c4Swesolows 		}
173880ab886dSwesolows 	}
17391912d2c4Swesolows 	(void) unlink(ctx->i_stderr);
17401912d2c4Swesolows 	free(ctx->i_stderr);
17411912d2c4Swesolows 
17421912d2c4Swesolows 	/*
17431912d2c4Swesolows 	 * cc returns an error code when given -V; we want that to succeed.
17441912d2c4Swesolows 	 */
17451912d2c4Swesolows 	if (ctx->i_flags & CW_F_PROG)
17461912d2c4Swesolows 		return (0);
174780ab886dSwesolows 
174880ab886dSwesolows 	return (ret);
174980ab886dSwesolows }
175080ab886dSwesolows 
175180ab886dSwesolows static int
175280ab886dSwesolows exec_ctx(cw_ictx_t *ctx, int block)
175380ab886dSwesolows {
175480ab886dSwesolows 	char *file;
175580ab886dSwesolows 
175680ab886dSwesolows 	/*
175780ab886dSwesolows 	 * To avoid offending cc's sensibilities, the name of its output
175880ab886dSwesolows 	 * file must end in '.o'.
175980ab886dSwesolows 	 */
176080ab886dSwesolows 	if ((file = tempnam(NULL, ".cw")) == NULL) {
176180ab886dSwesolows 		nomem();
176280ab886dSwesolows 		return (-1);
176380ab886dSwesolows 	}
176480ab886dSwesolows 	(void) strlcpy(ctx->i_discard, file, MAXPATHLEN);
176580ab886dSwesolows 	(void) strlcat(ctx->i_discard, ".o", MAXPATHLEN);
176680ab886dSwesolows 	free(file);
176780ab886dSwesolows 
17681912d2c4Swesolows 	if ((ctx->i_stderr = tempnam(NULL, ".cw")) == NULL) {
17691912d2c4Swesolows 		nomem();
177080ab886dSwesolows 		return (-1);
177180ab886dSwesolows 	}
177280ab886dSwesolows 
177380ab886dSwesolows 	if ((ctx->i_pid = fork()) == 0) {
17741912d2c4Swesolows 		int fd;
17751912d2c4Swesolows 
177680ab886dSwesolows 		(void) fclose(stderr);
17771912d2c4Swesolows 		if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL,
17781912d2c4Swesolows 		    0666)) < 0) {
17791912d2c4Swesolows 			cw_perror("open failed for standard error");
17801912d2c4Swesolows 			exit(1);
17811912d2c4Swesolows 		}
17821912d2c4Swesolows 		if (dup2(fd, 2) < 0) {
178380ab886dSwesolows 			cw_perror("dup2 failed for standard error");
178480ab886dSwesolows 			exit(1);
178580ab886dSwesolows 		}
17861912d2c4Swesolows 		if (fd != 2)
17871912d2c4Swesolows 			(void) close(fd);
178880ab886dSwesolows 		if (freopen("/dev/fd/2", "w", stderr) == NULL) {
178980ab886dSwesolows 			cw_perror("freopen failed for /dev/fd/2");
179080ab886dSwesolows 			exit(1);
179180ab886dSwesolows 		}
179280ab886dSwesolows 		prepctx(ctx);
179380ab886dSwesolows 		exit(invoke(ctx));
179480ab886dSwesolows 	}
179580ab886dSwesolows 
179680ab886dSwesolows 	if (ctx->i_pid < 0) {
179780ab886dSwesolows 		cw_perror("fork failed");
179880ab886dSwesolows 		return (1);
179980ab886dSwesolows 	}
180080ab886dSwesolows 
180180ab886dSwesolows 	if (block)
180280ab886dSwesolows 		return (reap(ctx));
180380ab886dSwesolows 
180480ab886dSwesolows 	return (0);
18057c478bd9Sstevel@tonic-gate }
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate int
18087c478bd9Sstevel@tonic-gate main(int argc, char **argv)
18097c478bd9Sstevel@tonic-gate {
181080ab886dSwesolows 	cw_ictx_t *ctx = newictx();
181180ab886dSwesolows 	cw_ictx_t *ctx_shadow = newictx();
18127c478bd9Sstevel@tonic-gate 	const char *dir;
181380ab886dSwesolows 	int do_serial, do_shadow;
181480ab886dSwesolows 	int ret = 0;
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	if ((progname = strrchr(argv[0], '/')) == NULL)
18177c478bd9Sstevel@tonic-gate 		progname = argv[0];
18187c478bd9Sstevel@tonic-gate 	else
18197c478bd9Sstevel@tonic-gate 		progname++;
18207c478bd9Sstevel@tonic-gate 
182180ab886dSwesolows 	if (ctx == NULL || ctx_shadow == NULL)
182280ab886dSwesolows 		nomem();
182380ab886dSwesolows 
182480ab886dSwesolows 	ctx->i_flags = CW_F_ECHO|CW_F_XLATE;
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	/*
18277c478bd9Sstevel@tonic-gate 	 * Figure out where to get our tools from.  This depends on
18287c478bd9Sstevel@tonic-gate 	 * the environment variables set at run time.
18297c478bd9Sstevel@tonic-gate 	 */
18307c478bd9Sstevel@tonic-gate 	if ((dir = getenv("SPRO_VROOT")) != NULL) {
18317fbf8d03SScott Rotondo 		(void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
18327fbf8d03SScott Rotondo 		    "%s/bin", dir);
18337c478bd9Sstevel@tonic-gate 	} else if ((dir = getenv("SPRO_ROOT")) != NULL) {
18347fbf8d03SScott Rotondo 		(void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
18357fbf8d03SScott Rotondo 		    "%s/SS12/bin", dir);
18367c478bd9Sstevel@tonic-gate 	} else if ((dir = getenv("BUILD_TOOLS")) != NULL) {
18377fbf8d03SScott Rotondo 		(void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
18380bb07399SNick Todd 		    "%s/SUNWspro/SS12/bin", dir);
18397c478bd9Sstevel@tonic-gate 	}
18407c478bd9Sstevel@tonic-gate 
18416a3e8e86SRichard Lowe 	if ((dir = getenv("GCC_ROOT")) != NULL) {
18427fbf8d03SScott Rotondo 		(void) snprintf(default_dir[CW_C_GCC], MAXPATHLEN,
18437fbf8d03SScott Rotondo 		    "%s/bin", dir);
18447c478bd9Sstevel@tonic-gate 	}
18457c478bd9Sstevel@tonic-gate 
184680ab886dSwesolows 	do_shadow = (getenv("CW_NO_SHADOW") ? 0 : 1);
184780ab886dSwesolows 	do_serial = (getenv("CW_SHADOW_SERIAL") ? 1 : 0);
184880ab886dSwesolows 
184980ab886dSwesolows 	if (getenv("CW_NO_EXEC") == NULL)
185080ab886dSwesolows 		ctx->i_flags |= CW_F_EXEC;
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	/*
18537c478bd9Sstevel@tonic-gate 	 * The first argument must be one of "-_cc", "-_gcc", "-_CC", or "-_g++"
18547c478bd9Sstevel@tonic-gate 	 */
18557c478bd9Sstevel@tonic-gate 	if (argc == 1)
18567c478bd9Sstevel@tonic-gate 		usage();
18577c478bd9Sstevel@tonic-gate 	argc--;
18587c478bd9Sstevel@tonic-gate 	argv++;
18597c478bd9Sstevel@tonic-gate 	if (strcmp(argv[0], "-_cc") == 0) {
186080ab886dSwesolows 		ctx->i_compiler = CW_C_CC;
18617c478bd9Sstevel@tonic-gate 	} else if (strcmp(argv[0], "-_gcc") == 0) {
186280ab886dSwesolows 		ctx->i_compiler = CW_C_GCC;
18637c478bd9Sstevel@tonic-gate 	} else if (strcmp(argv[0], "-_CC") == 0) {
186480ab886dSwesolows 		ctx->i_compiler = CW_C_CC;
186580ab886dSwesolows 		ctx->i_flags |= CW_F_CXX;
18667c478bd9Sstevel@tonic-gate 	} else if (strcmp(argv[0], "-_g++") == 0) {
186780ab886dSwesolows 		ctx->i_compiler = CW_C_GCC;
186880ab886dSwesolows 		ctx->i_flags |= CW_F_CXX;
18697c478bd9Sstevel@tonic-gate 	} else {
18707c478bd9Sstevel@tonic-gate 		/* assume "-_gcc" by default */
18717c478bd9Sstevel@tonic-gate 		argc++;
18727c478bd9Sstevel@tonic-gate 		argv--;
187380ab886dSwesolows 		ctx->i_compiler = CW_C_GCC;
18747c478bd9Sstevel@tonic-gate 	}
18757c478bd9Sstevel@tonic-gate 
18761912d2c4Swesolows 	/*
18771912d2c4Swesolows 	 * -_compiler - tell us the path to the primary compiler only
18781912d2c4Swesolows 	 */
187980ab886dSwesolows 	if (argc > 1 && strcmp(argv[1], "-_compiler") == 0) {
188080ab886dSwesolows 		ctx->i_flags &= ~CW_F_XLATE;
188180ab886dSwesolows 		prepctx(ctx);
188280ab886dSwesolows 		(void) printf("%s\n", ctx->i_ae->ael_head->ae_arg);
188380ab886dSwesolows 		return (0);
18847c478bd9Sstevel@tonic-gate 	}
18857c478bd9Sstevel@tonic-gate 
18861912d2c4Swesolows 	/*
18871912d2c4Swesolows 	 * -_versions - tell us the cw version, paths to all compilers, and
18881912d2c4Swesolows 	 *		ask each for its version if we know how.
18891912d2c4Swesolows 	 */
18901912d2c4Swesolows 	if (argc > 1 && strcmp(argv[1], "-_versions") == 0) {
18919a70fc3bSMark J. Nelson 		(void) printf("cw version %s", CW_VERSION);
18921912d2c4Swesolows 		if (!do_shadow)
18931912d2c4Swesolows 			(void) printf(" (SHADOW MODE DISABLED)");
18941912d2c4Swesolows 		(void) printf("\n");
18951912d2c4Swesolows 		(void) fflush(stdout);
18961912d2c4Swesolows 		ctx->i_flags &= ~CW_F_ECHO;
18971912d2c4Swesolows 		ctx->i_flags |= CW_F_PROG|CW_F_EXEC;
18981912d2c4Swesolows 		argc--;
18991912d2c4Swesolows 		argv++;
19001912d2c4Swesolows 		do_serial = 1;
19011912d2c4Swesolows 	}
19021912d2c4Swesolows 
19031912d2c4Swesolows 	ctx->i_oldargc = argc;
19041912d2c4Swesolows 	ctx->i_oldargv = argv;
19051912d2c4Swesolows 
190680ab886dSwesolows 	ret |= exec_ctx(ctx, do_serial);
190780ab886dSwesolows 
190880ab886dSwesolows 	if (do_shadow) {
190980ab886dSwesolows 		(void) memcpy(ctx_shadow, ctx, sizeof (cw_ictx_t));
191080ab886dSwesolows 		ctx_shadow->i_flags |= CW_F_SHADOW;
191180ab886dSwesolows 		ret |= exec_ctx(ctx_shadow, 1);
19127c478bd9Sstevel@tonic-gate 	}
19137c478bd9Sstevel@tonic-gate 
191480ab886dSwesolows 	if (!do_serial)
191580ab886dSwesolows 		ret |= reap(ctx);
19167c478bd9Sstevel@tonic-gate 
191780ab886dSwesolows 	return (ret);
19187c478bd9Sstevel@tonic-gate }
1919