xref: /illumos-gate/usr/src/tools/cw/cw.c (revision 3cac7b0d)
1aa9ef484SJohn Levon 
27c478bd9Sstevel@tonic-gate /*
37c478bd9Sstevel@tonic-gate  * CDDL HEADER START
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
602e56f3fSwesolows  * Common Development and Distribution License (the "License").
702e56f3fSwesolows  * You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
2325c28e83SPiotr Jasiukajtis /*
24aa9ef484SJohn Levon  * Copyright 2018, Richard Lowe.
2525c28e83SPiotr Jasiukajtis  */
267c478bd9Sstevel@tonic-gate /*
277fbf8d03SScott Rotondo  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
291f5207b7SJohn Levon  *
301f5207b7SJohn Levon  * Copyright 2019 Joyent, Inc.
317c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * Wrapper for the GNU C compiler to make it accept the Sun C compiler
357c478bd9Sstevel@tonic-gate  * arguments where possible.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * Since the translation is inexact, this is something of a work-in-progress.
389a70fc3bSMark J. Nelson  *
397c478bd9Sstevel@tonic-gate  */
419a70fc3bSMark J. Nelson /* If you modify this file, you must increment CW_VERSION */
42*22a8b493SToomas Soome #define	CW_VERSION	"5.1"
439a70fc3bSMark J. Nelson 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * -#		Verbose mode
467c478bd9Sstevel@tonic-gate  * -###		Show compiler commands built by driver, no compilation
477c478bd9Sstevel@tonic-gate  * -A<name[(tokens)]>	Preprocessor predicate assertion
487c478bd9Sstevel@tonic-gate  * -C		Prevent preprocessor from removing comments
497c478bd9Sstevel@tonic-gate  * -c		Compile only - produce .o files, suppress linking
507c478bd9Sstevel@tonic-gate  * -cg92	Alias for -xtarget=ss1000
517c478bd9Sstevel@tonic-gate  * -D<name[=token]>	Associate name with token as if by #define
527c478bd9Sstevel@tonic-gate  * -d[y|n]	dynamic [-dy] or static [-dn] option to linker
537c478bd9Sstevel@tonic-gate  * -E		Compile source through preprocessor only, output to stdout
547c478bd9Sstevel@tonic-gate  * -erroff=<t>	Suppress warnings specified by tags t(%none, %all, <tag list>)
557c478bd9Sstevel@tonic-gate  * -errtags=<a>	Display messages with tags a(no, yes)
567c478bd9Sstevel@tonic-gate  * -errwarn=<t>	Treats warnings specified by tags t(%none, %all, <tag list>)
577c478bd9Sstevel@tonic-gate  *		as errors
587c478bd9Sstevel@tonic-gate  * -fast	Optimize using a selection of options
597c478bd9Sstevel@tonic-gate  * -fd		Report old-style function definitions and declarations
607c478bd9Sstevel@tonic-gate  * -fnonstd	Initialize floating-point hardware to non-standard preferences
617c478bd9Sstevel@tonic-gate  * -fns[=<yes|no>] Select non-standard floating point mode
627c478bd9Sstevel@tonic-gate  * -fprecision=<p> Set FP rounding precision mode p(single, double, extended)
637c478bd9Sstevel@tonic-gate  * -fround=<r>	Select the IEEE rounding mode in effect at startup
647c478bd9Sstevel@tonic-gate  * -fsimple[=<n>] Select floating-point optimization preferences <n>
657c478bd9Sstevel@tonic-gate  * -fsingle	Use single-precision arithmetic (-Xt and -Xs modes only)
667c478bd9Sstevel@tonic-gate  * -ftrap=<t>	Select floating-point trapping mode in effect at startup
677c478bd9Sstevel@tonic-gate  * -fstore	force floating pt. values to target precision on assignment
687c478bd9Sstevel@tonic-gate  * -g		Compile for debugging
697c478bd9Sstevel@tonic-gate  * -H		Print path name of each file included during compilation
707c478bd9Sstevel@tonic-gate  * -h <name>	Assign <name> to generated dynamic shared library
717c478bd9Sstevel@tonic-gate  * -I<dir>	Add <dir> to preprocessor #include file search path
727c478bd9Sstevel@tonic-gate  * -i		Passed to linker to ignore any LD_LIBRARY_PATH setting
737c478bd9Sstevel@tonic-gate  * -keeptmp	Keep temporary files created during compilation
747c478bd9Sstevel@tonic-gate  * -L<dir>	Pass to linker to add <dir> to the library search path
757c478bd9Sstevel@tonic-gate  * -l<name>	Link with library lib<name>.a or lib<name>.so
767c478bd9Sstevel@tonic-gate  * -mc		Remove duplicate strings from .comment section of output files
777c478bd9Sstevel@tonic-gate  * -mr		Remove all strings from .comment section of output files
787c478bd9Sstevel@tonic-gate  * -mr,"string"	Remove all strings and append "string" to .comment section
797c478bd9Sstevel@tonic-gate  * -mt		Specify options needed when compiling multi-threaded code
807c478bd9Sstevel@tonic-gate  * -native	Find available processor, generate code accordingly
817c478bd9Sstevel@tonic-gate  * -nofstore	Do not force floating pt. values to target precision
827c478bd9Sstevel@tonic-gate  *		on assignment
837c478bd9Sstevel@tonic-gate  * -norunpath	Do not build in a runtime path for shared libraries
84662492f5Ssherrym  * -O		Use default optimization level (-xO2 or -xO3. Check man page.)
857c478bd9Sstevel@tonic-gate  * -o <outputfile> Set name of output file to <outputfile>
867c478bd9Sstevel@tonic-gate  * -P		Compile source through preprocessor only, output to .i  file
877c478bd9Sstevel@tonic-gate  * -p		Compile for profiling with prof
887c478bd9Sstevel@tonic-gate  * -Q[y|n]	Emit/don't emit identification info to output file
897c478bd9Sstevel@tonic-gate  * -R<dir[:dir]> Build runtime search path list into executable
907c478bd9Sstevel@tonic-gate  * -S		Compile and only generate assembly code (.s)
917c478bd9Sstevel@tonic-gate  * -s		Strip symbol table from the executable file
92159cf8a6Swesolows  * -t		Turn off duplicate symbol warnings when linking
937c478bd9Sstevel@tonic-gate  * -U<name>	Delete initial definition of preprocessor symbol <name>
947c478bd9Sstevel@tonic-gate  * -V		Report version number of each compilation phase
957c478bd9Sstevel@tonic-gate  * -v		Do stricter semantic checking
967c478bd9Sstevel@tonic-gate  * -W<c>,<arg>	Pass <arg> to specified component <c> (a,l,m,p,0,2,h,i,u)
977c478bd9Sstevel@tonic-gate  * -w		Suppress compiler warning messages
987c478bd9Sstevel@tonic-gate  * -Xa		Compile assuming ANSI C conformance, allow K & R extensions
997c478bd9Sstevel@tonic-gate  *		(default mode)
1007c478bd9Sstevel@tonic-gate  * -Xs		Compile assuming (pre-ANSI) K & R C style code
1017c478bd9Sstevel@tonic-gate  * -Xt		Compile assuming K & R conformance, allow ANSI C
1027c478bd9Sstevel@tonic-gate  * -xarch=<a>	Specify target architecture instruction set
1037c478bd9Sstevel@tonic-gate  * -xbuiltin[=<b>] When profitable inline, or substitute intrinisic functions
1047c478bd9Sstevel@tonic-gate  *		for system functions, b={%all,%none}
1057c478bd9Sstevel@tonic-gate  * -xCC		Accept C++ style comments
1067c478bd9Sstevel@tonic-gate  * -xchip=<c>	Specify the target processor for use by the optimizer
1077c478bd9Sstevel@tonic-gate  * -xcode=<c>	Generate different code for forming addresses
1087c478bd9Sstevel@tonic-gate  * -xcrossfile[=<n>] Enable optimization and inlining across source files,
1097c478bd9Sstevel@tonic-gate  *		n={0|1}
1107c478bd9Sstevel@tonic-gate  * -xe		Perform only syntax/semantic checking, no code generation
111f795e658Srie  * -xF		Compile for later mapfile reordering or unused section
112f795e658Srie  *		elimination
1137c478bd9Sstevel@tonic-gate  * -xhelp=<f>	Display on-line help information f(flags, readme, errors)
1147c478bd9Sstevel@tonic-gate  * -xildoff	Cancel -xildon
1157c478bd9Sstevel@tonic-gate  * -xildon	Enable use of the incremental linker, ild
1167c478bd9Sstevel@tonic-gate  * -xinline=[<a>,...,<a>]  Attempt inlining of specified user routines,
1177c478bd9Sstevel@tonic-gate  *		<a>={%auto,func,no%func}
1187c478bd9Sstevel@tonic-gate  * -xlibmieee	Force IEEE 754 return values for math routines in
1197c478bd9Sstevel@tonic-gate  *		exceptional cases
1207c478bd9Sstevel@tonic-gate  * -xlibmil	Inline selected libm math routines for optimization
1217c478bd9Sstevel@tonic-gate  * -xlic_lib=sunperf	Link in the Sun supplied performance libraries
1227c478bd9Sstevel@tonic-gate  * -xlicinfo	Show license server information
1237c478bd9Sstevel@tonic-gate  * -xmaxopt=[off,1,2,3,4,5] maximum optimization level allowed on #pragma opt
1247c478bd9Sstevel@tonic-gate  * -xO<n>	Generate optimized code (n={1|2|3|4|5})
1257c478bd9Sstevel@tonic-gate  * -xP		Print prototypes for function definitions
1267c478bd9Sstevel@tonic-gate  * -xprofile=<p> Collect data for a profile or use a profile to optimize
1277c478bd9Sstevel@tonic-gate  *		<p>={{collect,use}[:<path>],tcov}
1287c478bd9Sstevel@tonic-gate  * -xregs=<r>	Control register allocation
1297c478bd9Sstevel@tonic-gate  * -xs		Allow debugging without object (.o) files
1307c478bd9Sstevel@tonic-gate  * -xsb		Compile for use with the WorkShop source browser
1317c478bd9Sstevel@tonic-gate  * -xsbfast	Generate only WorkShop source browser info, no compilation
1327c478bd9Sstevel@tonic-gate  * -xsfpconst	Represent unsuffixed floating point constants as single
1337c478bd9Sstevel@tonic-gate  *		precision
1347c478bd9Sstevel@tonic-gate  * -xspace	Do not do optimizations that increase code size
1357c478bd9Sstevel@tonic-gate  * -xstrconst	Place string literals into read-only data segment
1367c478bd9Sstevel@tonic-gate  * -xtarget=<t>	Specify target system for optimization
1377c478bd9Sstevel@tonic-gate  * -xtemp=<dir>	Set directory for temporary files to <dir>
1387c478bd9Sstevel@tonic-gate  * -xtime	Report the execution time for each compilation phase
1397c478bd9Sstevel@tonic-gate  * -xunroll=n	Enable unrolling loops n times where possible
1407c478bd9Sstevel@tonic-gate  * -Y<c>,<dir>	Specify <dir> for location of component <c> (a,l,m,p,0,h,i,u)
1417c478bd9Sstevel@tonic-gate  * -YA,<dir>	Change default directory searched for components
1427c478bd9Sstevel@tonic-gate  * -YI,<dir>	Change default directory searched for include files
1437c478bd9Sstevel@tonic-gate  * -YP,<dir>	Change default directory for finding libraries files
1447c478bd9Sstevel@tonic-gate  * -YS,<dir>	Change default directory for startup object files
1457c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * Translation table:
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * -#				-v
1527c478bd9Sstevel@tonic-gate  * -###				error
1537c478bd9Sstevel@tonic-gate  * -A<name[(tokens)]>		pass-thru
1547c478bd9Sstevel@tonic-gate  * -B<[static|dynamic]>		pass-thru (syntax error for anything else)
1557c478bd9Sstevel@tonic-gate  * -C				pass-thru
1567c478bd9Sstevel@tonic-gate  * -c				pass-thru
1577c478bd9Sstevel@tonic-gate  * -cg92			-m32 -mcpu=v8 -mtune=supersparc (SPARC only)
1587c478bd9Sstevel@tonic-gate  * -D<name[=token]>		pass-thru
1597c478bd9Sstevel@tonic-gate  * -E				pass-thru
1607c478bd9Sstevel@tonic-gate  * -erroff=E_EMPTY_TRANSLATION_UNIT ignore
1617c478bd9Sstevel@tonic-gate  * -errtags=%all		-Wall
1627c478bd9Sstevel@tonic-gate  * -errwarn=%all		-Werror else -Wno-error
1637c478bd9Sstevel@tonic-gate  * -fast			error
1647c478bd9Sstevel@tonic-gate  * -fd				error
1657c478bd9Sstevel@tonic-gate  * -fnonstd			error
1667c478bd9Sstevel@tonic-gate  * -fns[=<yes|no>]		error
1677c478bd9Sstevel@tonic-gate  * -fprecision=<p>		error
1687c478bd9Sstevel@tonic-gate  * -fround=<r>			error
1697c478bd9Sstevel@tonic-gate  * -fsimple[=<n>]		error
1707c478bd9Sstevel@tonic-gate  * -fsingle[=<n>]		error
1717c478bd9Sstevel@tonic-gate  * -ftrap=<t>			error
1727c478bd9Sstevel@tonic-gate  * -fstore			error
1737c478bd9Sstevel@tonic-gate  * -g				pass-thru
1747c478bd9Sstevel@tonic-gate  * -H				pass-thru
1757c478bd9Sstevel@tonic-gate  * -h <name>			pass-thru
1767c478bd9Sstevel@tonic-gate  * -I<dir>			pass-thru
1777c478bd9Sstevel@tonic-gate  * -i				pass-thru
1787c478bd9Sstevel@tonic-gate  * -keeptmp			-save-temps
1797c478bd9Sstevel@tonic-gate  * -L<dir>			pass-thru
1807c478bd9Sstevel@tonic-gate  * -l<name>			pass-thru
1817c478bd9Sstevel@tonic-gate  * -mc				error
1827c478bd9Sstevel@tonic-gate  * -mr				error
1837c478bd9Sstevel@tonic-gate  * -mr,"string"			error
1847c478bd9Sstevel@tonic-gate  * -mt				-D_REENTRANT
1857c478bd9Sstevel@tonic-gate  * -native			error
1867c478bd9Sstevel@tonic-gate  * -nofstore			error
1877c478bd9Sstevel@tonic-gate  * -nolib			-nodefaultlibs
1887c478bd9Sstevel@tonic-gate  * -norunpath			ignore
189662492f5Ssherrym  * -O				-O1 (Check the man page to be certain)
1907c478bd9Sstevel@tonic-gate  * -o <outputfile>		pass-thru
1917c478bd9Sstevel@tonic-gate  * -P				-E -o filename.i (or error)
1927c478bd9Sstevel@tonic-gate  * -p				pass-thru
1937c478bd9Sstevel@tonic-gate  * -Q[y|n]			error
1947c478bd9Sstevel@tonic-gate  * -R<dir[:dir]>		pass-thru
1957c478bd9Sstevel@tonic-gate  * -S				pass-thru
1967c478bd9Sstevel@tonic-gate  * -U<name>			pass-thru
1977c478bd9Sstevel@tonic-gate  * -V				--version
1987c478bd9Sstevel@tonic-gate  * -v				-Wall
1997c478bd9Sstevel@tonic-gate  * -Wa,<arg>			pass-thru
2007c478bd9Sstevel@tonic-gate  * -Wp,<arg>			pass-thru except -xc99=<a>
2017c478bd9Sstevel@tonic-gate  * -Wl,<arg>			pass-thru
2027c478bd9Sstevel@tonic-gate  * -W{m,0,2,h,i,u>		error/ignore
20354836668Spetede  * -xmodel=kernel		-ffreestanding -mcmodel=kernel -mno-red-zone
204d430274bSsherrym  * -Wu,-save_args		-msave-args
2057c478bd9Sstevel@tonic-gate  * -w				pass-thru
2067c478bd9Sstevel@tonic-gate  * -Xa				-std=iso9899:199409 or -ansi
2077c478bd9Sstevel@tonic-gate  * -Xt				error
2087c478bd9Sstevel@tonic-gate  * -Xs				-traditional -std=c89
2097c478bd9Sstevel@tonic-gate  * -xarch=<a>			table
21002e56f3fSwesolows  * -xbuiltin[=<b>]		-fbuiltin (-fno-builtin otherwise)
2117c478bd9Sstevel@tonic-gate  * -xCC				ignore
2127c478bd9Sstevel@tonic-gate  * -xchip=<c>			table
2137c478bd9Sstevel@tonic-gate  * -xcode=<c>			table
214159cf8a6Swesolows  * -xcrossfile[=<n>]		ignore
2157c478bd9Sstevel@tonic-gate  * -xe				error
2167c478bd9Sstevel@tonic-gate  * -xF				error
2177c478bd9Sstevel@tonic-gate  * -xhelp=<f>			error
2187c478bd9Sstevel@tonic-gate  * -xildoff			ignore
2197c478bd9Sstevel@tonic-gate  * -xildon			ignore
2207c478bd9Sstevel@tonic-gate  * -xinline			ignore
2217c478bd9Sstevel@tonic-gate  * -xlibmieee			error
2227c478bd9Sstevel@tonic-gate  * -xlibmil			error
2237c478bd9Sstevel@tonic-gate  * -xlic_lib=sunperf		error
2247c478bd9Sstevel@tonic-gate  * -xmaxopt=[...]		error
2257c478bd9Sstevel@tonic-gate  * -xO<n>			-O<n>
2267c478bd9Sstevel@tonic-gate  * -xP				error
2277c478bd9Sstevel@tonic-gate  * -xprofile=<p>		error
2287c478bd9Sstevel@tonic-gate  * -xregs=<r>			table
2297c478bd9Sstevel@tonic-gate  * -xs				error
2307c478bd9Sstevel@tonic-gate  * -xsb				error
2317c478bd9Sstevel@tonic-gate  * -xsbfast			error
2327c478bd9Sstevel@tonic-gate  * -xsfpconst			error
2337c478bd9Sstevel@tonic-gate  * -xspace			ignore (-not -Os)
2347c478bd9Sstevel@tonic-gate  * -xstrconst			ignore
2357c478bd9Sstevel@tonic-gate  * -xtarget=<t>			table
2367c478bd9Sstevel@tonic-gate  * -xtemp=<dir>			error
2377c478bd9Sstevel@tonic-gate  * -xtime			error
2387c478bd9Sstevel@tonic-gate  * -xtransition			-Wtransition
2397c478bd9Sstevel@tonic-gate  * -xunroll=n			error
2407c478bd9Sstevel@tonic-gate  * -Y<c>,<dir>			error
2417c478bd9Sstevel@tonic-gate  * -YA,<dir>			error
2427c478bd9Sstevel@tonic-gate  * -YI,<dir>			-nostdinc -I<dir>
2437c478bd9Sstevel@tonic-gate  * -YP,<dir>			error
2447c478bd9Sstevel@tonic-gate  * -YS,<dir>			error
2457c478bd9Sstevel@tonic-gate  */
2477c478bd9Sstevel@tonic-gate #include <ctype.h>
248aa9ef484SJohn Levon #include <err.h>
24980ab886dSwesolows #include <errno.h>
250aa9ef484SJohn Levon #include <fcntl.h>
251aa9ef484SJohn Levon #include <getopt.h>
252aa9ef484SJohn Levon #include <stdio.h>
253aa9ef484SJohn Levon #include <stdlib.h>
254*22a8b493SToomas Soome #include <stdbool.h>
255aa9ef484SJohn Levon #include <string.h>
256aa9ef484SJohn Levon #include <unistd.h>
25788e61e85SRichard Lowe #include <dirent.h>
258aa9ef484SJohn Levon 
2597c478bd9Sstevel@tonic-gate #include <sys/param.h>
26080ab886dSwesolows #include <sys/stat.h>
261aa9ef484SJohn Levon #include <sys/types.h>
262aa9ef484SJohn Levon #include <sys/utsname.h>
263aa9ef484SJohn Levon #include <sys/wait.h>
26580ab886dSwesolows #define	CW_F_CXX	0x01
26680ab886dSwesolows #define	CW_F_SHADOW	0x02
26780ab886dSwesolows #define	CW_F_EXEC	0x04
26880ab886dSwesolows #define	CW_F_ECHO	0x08
26980ab886dSwesolows #define	CW_F_XLATE	0x10
2701912d2c4Swesolows #define	CW_F_PROG	0x20
27280ab886dSwesolows typedef enum cw_op {
27380ab886dSwesolows 	CW_O_NONE = 0,
27480ab886dSwesolows 	CW_O_PREPROCESS,
27580ab886dSwesolows 	CW_O_COMPILE,
27680ab886dSwesolows 	CW_O_LINK
27780ab886dSwesolows } cw_op_t;
27980ab886dSwesolows struct aelist {
28080ab886dSwesolows 	struct ae {
28180ab886dSwesolows 		struct ae *ae_next;
28280ab886dSwesolows 		char *ae_arg;
28380ab886dSwesolows 	} *ael_head, *ael_tail;
28480ab886dSwesolows 	int ael_argc;
28580ab886dSwesolows };
287aa9ef484SJohn Levon typedef enum {
288aa9ef484SJohn Levon 	GNU,
2891f5207b7SJohn Levon 	SUN,
2901f5207b7SJohn Levon 	SMATCH
291aa9ef484SJohn Levon } compiler_style_t;
292aa9ef484SJohn Levon 
293aa9ef484SJohn Levon typedef struct {
294aa9ef484SJohn Levon 	char *c_name;
295aa9ef484SJohn Levon 	char *c_path;
296aa9ef484SJohn Levon 	compiler_style_t c_style;
297aa9ef484SJohn Levon } cw_compiler_t;
298aa9ef484SJohn Levon 
29980ab886dSwesolows typedef struct cw_ictx {
300aa9ef484SJohn Levon 	struct cw_ictx	*i_next;
301aa9ef484SJohn Levon 	cw_compiler_t	*i_compiler;
30269b1fd3fSRichard Lowe 	char		*i_linker;
30380ab886dSwesolows 	struct aelist	*i_ae;
30480ab886dSwesolows 	uint32_t	i_flags;
30580ab886dSwesolows 	int		i_oldargc;
30680ab886dSwesolows 	char		**i_oldargv;
30780ab886dSwesolows 	pid_t		i_pid;
30888e61e85SRichard Lowe 	char		*i_tmpdir;
3091912d2c4Swesolows 	char		*i_stderr;
31080ab886dSwesolows } cw_ictx_t;
312e521259dSpetede /*
313e521259dSpetede  * Status values to indicate which Studio compiler and associated
314e521259dSpetede  * flags are being used.
315e521259dSpetede  */
316e521259dSpetede #define	M32		0x01	/* -m32 - only on Studio 12 */
317e521259dSpetede #define	M64		0x02	/* -m64 - only on Studio 12 */
318e521259dSpetede #define	SS11		0x100	/* Studio 11 */
319e521259dSpetede #define	SS12		0x200	/* Studio 12 */
321e521259dSpetede #define	TRANS_ENTRY	5
322e521259dSpetede /*
323e521259dSpetede  * Translation table definition for the -xarch= flag. The "x_arg"
324e521259dSpetede  * value is translated into the appropriate gcc flags according
325e521259dSpetede  * to the values in x_trans[n]. The x_flags indicates what compiler
326e521259dSpetede  * is being used and what flags have been set via the use of
327e521259dSpetede  * "x_arg".
328e521259dSpetede  */
329e521259dSpetede typedef struct xarch_table {
330e521259dSpetede 	char	*x_arg;
331e521259dSpetede 	int	x_flags;
332e521259dSpetede 	char	*x_trans[TRANS_ENTRY];
333e521259dSpetede } xarch_table_t;
335e521259dSpetede /*
336e521259dSpetede  * The translation table for the -xarch= flag used in the Studio compilers.
337e521259dSpetede  */
338e521259dSpetede static const xarch_table_t xtbl[] = {
3397c478bd9Sstevel@tonic-gate #if defined(__x86)
340aa9ef484SJohn Levon 	{ "generic",	SS11, {NULL} },
3417a6460b6Spetede 	{ "generic64",	(SS11|M64), { "-m64", "-mtune=opteron" } },
3427a6460b6Spetede 	{ "amd64",	(SS11|M64), { "-m64", "-mtune=opteron" } },
3437a6460b6Spetede 	{ "386",	SS11,	{ "-march=i386" } },
3447a6460b6Spetede 	{ "pentium_pro", SS11,	{ "-march=pentiumpro" } },
34525c28e83SPiotr Jasiukajtis 	{ "sse",	SS11, { "-msse", "-mfpmath=sse" } },
34625c28e83SPiotr Jasiukajtis 	{ "sse2",	SS11, { "-msse2", "-mfpmath=sse" } },
3477c478bd9Sstevel@tonic-gate #elif defined(__sparc)
3487a6460b6Spetede 	{ "generic",	(SS11|M32), { "-m32", "-mcpu=v8" } },
3497a6460b6Spetede 	{ "generic64",	(SS11|M64), { "-m64", "-mcpu=v9" } },
3507a6460b6Spetede 	{ "v8",		(SS11|M32), { "-m32", "-mcpu=v8", "-mno-v8plus" } },
3517a6460b6Spetede 	{ "v8plus",	(SS11|M32), { "-m32", "-mcpu=v9", "-mv8plus" } },
3527a6460b6Spetede 	{ "v8plusa",	(SS11|M32), { "-m32", "-mcpu=ultrasparc", "-mv8plus",
3537a6460b6Spetede 			"-mvis" } },
3547a6460b6Spetede 	{ "v8plusb",	(SS11|M32), { "-m32", "-mcpu=ultrasparc3", "-mv8plus",
3557a6460b6Spetede 			"-mvis" } },
3567a6460b6Spetede 	{ "v9",		(SS11|M64), { "-m64", "-mcpu=v9" } },
3577a6460b6Spetede 	{ "v9a",	(SS11|M64), { "-m64", "-mcpu=ultrasparc", "-mvis" } },
3587a6460b6Spetede 	{ "v9b",	(SS11|M64), { "-m64", "-mcpu=ultrasparc3", "-mvis" } },
3597a6460b6Spetede 	{ "sparc",	SS12, { "-mcpu=v9", "-mv8plus" } },
3607a6460b6Spetede 	{ "sparcvis",	SS12, { "-mcpu=ultrasparc", "-mvis" } },
3617a6460b6Spetede 	{ "sparcvis2",	SS12, { "-mcpu=ultrasparc3", "-mvis" } }
3627c478bd9Sstevel@tonic-gate #endif
3637c478bd9Sstevel@tonic-gate };
365e521259dSpetede static int xtbl_size = sizeof (xtbl) / sizeof (xarch_table_t);
3677c478bd9Sstevel@tonic-gate static const char *xchip_tbl[] = {
3687c478bd9Sstevel@tonic-gate #if defined(__x86)
3697c478bd9Sstevel@tonic-gate 	"386",		"-mtune=i386", NULL,
3707c478bd9Sstevel@tonic-gate 	"486",		"-mtune=i486", NULL,
3717c478bd9Sstevel@tonic-gate 	"pentium",	"-mtune=pentium", NULL,
3727c478bd9Sstevel@tonic-gate 	"pentium_pro",  "-mtune=pentiumpro", NULL,
3737c478bd9Sstevel@tonic-gate #elif defined(__sparc)
3747c478bd9Sstevel@tonic-gate 	"super",	"-mtune=supersparc", NULL,
3757c478bd9Sstevel@tonic-gate 	"ultra",	"-mtune=ultrasparc", NULL,
3767c478bd9Sstevel@tonic-gate 	"ultra3",	"-mtune=ultrasparc3", NULL,
3777c478bd9Sstevel@tonic-gate #endif
3787c478bd9Sstevel@tonic-gate 	NULL,		NULL
3797c478bd9Sstevel@tonic-gate };
3817c478bd9Sstevel@tonic-gate static const char *xcode_tbl[] = {
3827c478bd9Sstevel@tonic-gate #if defined(__sparc)
3837c478bd9Sstevel@tonic-gate 	"abs32",	"-fno-pic", "-mcmodel=medlow", NULL,
3847c478bd9Sstevel@tonic-gate 	"abs44",	"-fno-pic", "-mcmodel=medmid", NULL,
3857c478bd9Sstevel@tonic-gate 	"abs64",	"-fno-pic", "-mcmodel=medany", NULL,
3867c478bd9Sstevel@tonic-gate 	"pic13",	"-fpic", NULL,
3877c478bd9Sstevel@tonic-gate 	"pic32",	"-fPIC", NULL,
3887c478bd9Sstevel@tonic-gate #endif
3897c478bd9Sstevel@tonic-gate 	NULL,		NULL
3907c478bd9Sstevel@tonic-gate };
3927c478bd9Sstevel@tonic-gate static const char *xtarget_tbl[] = {
3937c478bd9Sstevel@tonic-gate #if defined(__x86)
3947c478bd9Sstevel@tonic-gate 	"pentium_pro",	"-march=pentiumpro", NULL,
3957c478bd9Sstevel@tonic-gate #endif	/* __x86 */
3967c478bd9Sstevel@tonic-gate 	NULL,		NULL
3977c478bd9Sstevel@tonic-gate };
3997c478bd9Sstevel@tonic-gate static const char *xregs_tbl[] = {
4007c478bd9Sstevel@tonic-gate #if defined(__sparc)
4017c478bd9Sstevel@tonic-gate 	"appl",		"-mapp-regs", NULL,
4027c478bd9Sstevel@tonic-gate 	"no%appl",	"-mno-app-regs", NULL,
4037c478bd9Sstevel@tonic-gate 	"float",	"-mfpu", NULL,
4047c478bd9Sstevel@tonic-gate 	"no%float",	"-mno-fpu", NULL,
4057c478bd9Sstevel@tonic-gate #endif	/* __sparc */
4067c478bd9Sstevel@tonic-gate 	NULL,		NULL
4077c478bd9Sstevel@tonic-gate };
40980ab886dSwesolows static void
nomem(void)41080ab886dSwesolows nomem(void)
41180ab886dSwesolows {
412aa9ef484SJohn Levon 	errx(1, "out of memory");
4137c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate static void
newae(struct aelist * ael,const char * arg)4167c478bd9Sstevel@tonic-gate newae(struct aelist *ael, const char *arg)
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	struct ae *ae;
42069b1fd3fSRichard Lowe 	if ((ae = calloc(1, sizeof (*ae))) == NULL)
42180ab886dSwesolows 		nomem();
4227c478bd9Sstevel@tonic-gate 	ae->ae_arg = strdup(arg);
4237c478bd9Sstevel@tonic-gate 	if (ael->ael_tail == NULL)
4247c478bd9Sstevel@tonic-gate 		ael->ael_head = ae;
4257c478bd9Sstevel@tonic-gate 	else
4267c478bd9Sstevel@tonic-gate 		ael->ael_tail->ae_next = ae;
4277c478bd9Sstevel@tonic-gate 	ael->ael_tail = ae;
42880ab886dSwesolows 	ael->ael_argc++;
42980ab886dSwesolows }
43180ab886dSwesolows static cw_ictx_t *
newictx(void)43280ab886dSwesolows newictx(void)
43380ab886dSwesolows {
43469b1fd3fSRichard Lowe 	cw_ictx_t *ctx = calloc(1, sizeof (cw_ictx_t));
43580ab886dSwesolows 	if (ctx)
43669b1fd3fSRichard Lowe 		if ((ctx->i_ae = calloc(1, sizeof (struct aelist))) == NULL) {
43780ab886dSwesolows 			free(ctx);
43880ab886dSwesolows 			return (NULL);
43980ab886dSwesolows 		}
44180ab886dSwesolows 	return (ctx);
4427c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate static void
error(const char * arg)4457c478bd9Sstevel@tonic-gate error(const char *arg)
4467c478bd9Sstevel@tonic-gate {
447aa9ef484SJohn Levon 	errx(2, "error: mapping failed at or near arg '%s'", arg);
4487c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate /*
4517c478bd9Sstevel@tonic-gate  * Add the current favourite set of warnings to the gcc invocation.
4527c478bd9Sstevel@tonic-gate  */
4537c478bd9Sstevel@tonic-gate static void
warnings(struct aelist * h)4547c478bd9Sstevel@tonic-gate warnings(struct aelist *h)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	static int warningsonce;
4587c478bd9Sstevel@tonic-gate 	if (warningsonce++)
4597c478bd9Sstevel@tonic-gate 		return;
4617014882cSRichard Lowe 	/*
4627014882cSRichard Lowe 	 * Enable as many warnings as exist, then disable those that we never
4637014882cSRichard Lowe 	 * ever want.
4647014882cSRichard Lowe 	 */
4657c478bd9Sstevel@tonic-gate 	newae(h, "-Wall");
4667014882cSRichard Lowe 	newae(h, "-Wextra");
4677c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate static void
optim_disable(struct aelist * h,int level)4707c478bd9Sstevel@tonic-gate optim_disable(struct aelist *h, int level)
4717c478bd9Sstevel@tonic-gate {
4727c478bd9Sstevel@tonic-gate 	if (level >= 2) {
4737c478bd9Sstevel@tonic-gate 		newae(h, "-fno-strict-aliasing");
4747c478bd9Sstevel@tonic-gate 		newae(h, "-fno-unit-at-a-time");
4757c478bd9Sstevel@tonic-gate 		newae(h, "-fno-optimize-sibling-calls");
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate static void
Xsmode(struct aelist * h)4807c478bd9Sstevel@tonic-gate Xsmode(struct aelist *h)
4817c478bd9Sstevel@tonic-gate {
4827c478bd9Sstevel@tonic-gate 	static int xsonce;
4847c478bd9Sstevel@tonic-gate 	if (xsonce++)
4857c478bd9Sstevel@tonic-gate 		return;
4877c478bd9Sstevel@tonic-gate 	newae(h, "-traditional");
4887c478bd9Sstevel@tonic-gate 	newae(h, "-traditional-cpp");
4897c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate static void
usage(void)49269b1fd3fSRichard Lowe usage(void)
4937c478bd9Sstevel@tonic-gate {
494aa9ef484SJohn Levon 	extern char *__progname;
4957c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
496aa9ef484SJohn Levon 	    "usage: %s [-C] [--versions] --primary <compiler> "
497aa9ef484SJohn Levon 	    "[--shadow <compiler>]... -- cflags...\n",
498aa9ef484SJohn Levon 	    __progname);
499aa9ef484SJohn Levon 	(void) fprintf(stderr, "compilers take the form: name,path,style\n"
500aa9ef484SJohn Levon 	    " - name: a unique name usable in flag specifiers\n"
501aa9ef484SJohn Levon 	    " - path: path to the compiler binary\n"
502aa9ef484SJohn Levon 	    " - style: the style of flags expected: either sun or gnu\n");
5037c478bd9Sstevel@tonic-gate 	exit(2);
5047c478bd9Sstevel@tonic-gate }
506e521259dSpetede static int
xlate_xtb(struct aelist * h,const char * xarg)507e521259dSpetede xlate_xtb(struct aelist *h, const char *xarg)
508e521259dSpetede {
509e521259dSpetede 	int	i, j;
511e521259dSpetede 	for (i = 0; i < xtbl_size; i++) {
512e521259dSpetede 		if (strcmp(xtbl[i].x_arg, xarg) == 0)
513e521259dSpetede 			break;
514e521259dSpetede 	}
516e521259dSpetede 	/*
517e521259dSpetede 	 * At the end of the table and so no matching "arg" entry
518e521259dSpetede 	 * found and so this must be a bad -xarch= flag.
519e521259dSpetede 	 */
520e521259dSpetede 	if (i == xtbl_size)
521e521259dSpetede 		error(xarg);
523e521259dSpetede 	for (j = 0; j < TRANS_ENTRY; j++) {
524e521259dSpetede 		if (xtbl[i].x_trans[j] != NULL)
525e521259dSpetede 			newae(h, xtbl[i].x_trans[j]);
526e521259dSpetede 		else
527e521259dSpetede 			break;
528e521259dSpetede 	}
529e521259dSpetede 	return (xtbl[i].x_flags);
531e521259dSpetede }
5337c478bd9Sstevel@tonic-gate static void
xlate(struct aelist * h,const char * xarg,const char ** table)5347c478bd9Sstevel@tonic-gate xlate(struct aelist *h, const char *xarg, const char **table)
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate 	while (*table != NULL && strcmp(xarg, *table) != 0) {
5377c478bd9Sstevel@tonic-gate 		while (*table != NULL)
5387c478bd9Sstevel@tonic-gate 			table++;
5397c478bd9Sstevel@tonic-gate 		table++;
5407c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 	if (*table == NULL)
5437c478bd9Sstevel@tonic-gate 		error(xarg);
5457c478bd9Sstevel@tonic-gate 	table++;
5477c478bd9Sstevel@tonic-gate 	while (*table != NULL) {
5487c478bd9Sstevel@tonic-gate 		newae(h, *table);
5497c478bd9Sstevel@tonic-gate 		table++;
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate }
55388e61e85SRichard Lowe /*
55488e61e85SRichard Lowe  * The compiler wants the output file to end in appropriate extension.  If
55588e61e85SRichard Lowe  * we're generating a name from whole cloth (path == NULL), we assume that
55688e61e85SRichard Lowe  * extension to be .o, otherwise we match the extension of the caller.
55788e61e85SRichard Lowe  */
55888e61e85SRichard Lowe static char *
discard_file_name(cw_ictx_t * ctx,const char * path)55988e61e85SRichard Lowe discard_file_name(cw_ictx_t *ctx, const char *path)
56088e61e85SRichard Lowe {
56188e61e85SRichard Lowe 	char *ret, *ext;
56288e61e85SRichard Lowe 	char tmpl[] = "cwXXXXXX";
56388e61e85SRichard Lowe 
56488e61e85SRichard Lowe 	if (path == NULL) {
56588e61e85SRichard Lowe 		ext = ".o";
56688e61e85SRichard Lowe 	} else {
56788e61e85SRichard Lowe 		ext = strrchr(path, '.');
56888e61e85SRichard Lowe 	}
56988e61e85SRichard Lowe 
57088e61e85SRichard Lowe 	/*
57188e61e85SRichard Lowe 	 * We need absolute control over where the temporary file goes, since
57288e61e85SRichard Lowe 	 * we rely on it for cleanup so tempnam(3C) and tmpnam(3C) are
57388e61e85SRichard Lowe 	 * inappropriate (they use TMPDIR, preferentially).
57488e61e85SRichard Lowe 	 *
57588e61e85SRichard Lowe 	 * mkstemp(3C) doesn't actually help us, since the temporary file
57688e61e85SRichard Lowe 	 * isn't used by us, only its name.
57788e61e85SRichard Lowe 	 */
57888e61e85SRichard Lowe 	if (mktemp(tmpl) == NULL)
57988e61e85SRichard Lowe 		nomem();
58088e61e85SRichard Lowe 
58188e61e85SRichard Lowe 	(void) asprintf(&ret, "%s/%s%s", ctx->i_tmpdir, tmpl,
58288e61e85SRichard Lowe 	    (ext != NULL) ? ext : "");
58388e61e85SRichard Lowe 
58488e61e85SRichard Lowe 	if (ret == NULL)
58588e61e85SRichard Lowe 		nomem();
58688e61e85SRichard Lowe 
58788e61e85SRichard Lowe 	return (ret);
58888e61e85SRichard Lowe }
58988e61e85SRichard Lowe 
590*22a8b493SToomas Soome static bool
is_source_file(const char * path)59188e61e85SRichard Lowe is_source_file(const char *path)
59288e61e85SRichard Lowe {
59388e61e85SRichard Lowe 	char *ext = strrchr(path, '.');
59488e61e85SRichard Lowe 
595406d4d29SJohn Levon 	if ((ext == NULL) || (*(ext + 1) == '\0'))
596*22a8b493SToomas Soome 		return (false);
59788e61e85SRichard Lowe 
59888e61e85SRichard Lowe 	ext += 1;
59988e61e85SRichard Lowe 
60088e61e85SRichard Lowe 	if ((strcasecmp(ext, "c") == 0) ||
60188e61e85SRichard Lowe 	    (strcmp(ext, "cc") == 0) ||
60288e61e85SRichard Lowe 	    (strcmp(ext, "i") == 0) ||
60388e61e85SRichard Lowe 	    (strcasecmp(ext, "s") == 0) ||
60488e61e85SRichard Lowe 	    (strcmp(ext, "cpp") == 0)) {
605*22a8b493SToomas Soome 		return (true);
60688e61e85SRichard Lowe 	}
60788e61e85SRichard Lowe 
608*22a8b493SToomas Soome 	return (false);
60988e61e85SRichard Lowe }
61088e61e85SRichard Lowe 
61188e61e85SRichard Lowe 
6127c478bd9Sstevel@tonic-gate static void
do_gcc(cw_ictx_t * ctx)61380ab886dSwesolows do_gcc(cw_ictx_t *ctx)
6147c478bd9Sstevel@tonic-gate {
6157c478bd9Sstevel@tonic-gate 	int c;
616e1bf37b1SRichard Lowe 	int nolibc = 0;
61780ab886dSwesolows 	int in_output = 0, seen_o = 0, c_files = 0;
61880ab886dSwesolows 	cw_op_t op = CW_O_LINK;
6197c478bd9Sstevel@tonic-gate 	char *model = NULL;
620aa9ef484SJohn Levon 	char *nameflag;
62188e61e85SRichard Lowe 	int mflag = 0;
6231912d2c4Swesolows 	if (ctx->i_flags & CW_F_PROG) {
6241912d2c4Swesolows 		newae(ctx->i_ae, "--version");
6251912d2c4Swesolows 		return;
6261912d2c4Swesolows 	}
62880ab886dSwesolows 	newae(ctx->i_ae, "-fident");
62980ab886dSwesolows 	newae(ctx->i_ae, "-finline");
63080ab886dSwesolows 	newae(ctx->i_ae, "-fno-inline-functions");
63180ab886dSwesolows 	newae(ctx->i_ae, "-fno-builtin");
63280ab886dSwesolows 	newae(ctx->i_ae, "-fno-asm");
6337014882cSRichard Lowe 	newae(ctx->i_ae, "-fdiagnostics-show-option");
63480ab886dSwesolows 	newae(ctx->i_ae, "-nodefaultlibs");
63602e56f3fSwesolows #if defined(__sparc)
63702e56f3fSwesolows 	/*
63802e56f3fSwesolows 	 * The SPARC ldd and std instructions require 8-byte alignment of
63902e56f3fSwesolows 	 * their address operand.  gcc correctly uses them only when the
64002e56f3fSwesolows 	 * ABI requires 8-byte alignment; unfortunately we have a number of
64102e56f3fSwesolows 	 * pieces of buggy code that doesn't conform to the ABI.  This
64202e56f3fSwesolows 	 * flag makes gcc work more like Studio with -xmemalign=4.
64302e56f3fSwesolows 	 */
64480ab886dSwesolows 	newae(ctx->i_ae, "-mno-integer-ldd-std");
64502e56f3fSwesolows #endif
6477c478bd9Sstevel@tonic-gate 	/*
6487c478bd9Sstevel@tonic-gate 	 * This is needed because 'u' is defined
6497c478bd9Sstevel@tonic-gate 	 * under a conditional on 'sun'.  Should
6507c478bd9Sstevel@tonic-gate 	 * probably just remove the conditional,
6517c478bd9Sstevel@tonic-gate 	 * or make it be dependent on '__sun'.
6527c478bd9Sstevel@tonic-gate 	 *
6537c478bd9Sstevel@tonic-gate 	 * -Dunix is also missing in enhanced ANSI mode
6547c478bd9Sstevel@tonic-gate 	 */
65580ab886dSwesolows 	newae(ctx->i_ae, "-D__sun");
657aa9ef484SJohn Levon 	if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1)
658aa9ef484SJohn Levon 		nomem();
659aa9ef484SJohn Levon 
6607c478bd9Sstevel@tonic-gate 	/*
6617c478bd9Sstevel@tonic-gate 	 * Walk the argument list, translating as we go ..
6627c478bd9Sstevel@tonic-gate 	 */
66380ab886dSwesolows 	while (--ctx->i_oldargc > 0) {
66480ab886dSwesolows 		char *arg = *++ctx->i_oldargv;
6657c478bd9Sstevel@tonic-gate 		size_t arglen = strlen(arg);
66780ab886dSwesolows 		if (*arg == '-') {
6687c478bd9Sstevel@tonic-gate 			arglen--;
66980ab886dSwesolows 		} else {
6707c478bd9Sstevel@tonic-gate 			/*
6717c478bd9Sstevel@tonic-gate 			 * Discard inline files that gcc doesn't grok
6727c478bd9Sstevel@tonic-gate 			 */
67380ab886dSwesolows 			if (!in_output && arglen > 3 &&
6747c478bd9Sstevel@tonic-gate 			    strcmp(arg + arglen - 3, ".il") == 0)
6757c478bd9Sstevel@tonic-gate 				continue;
67788e61e85SRichard Lowe 			if (!in_output && is_source_file(arg))
67880ab886dSwesolows 				c_files++;
6807c478bd9Sstevel@tonic-gate 			/*
68180ab886dSwesolows 			 * Otherwise, filenames and partial arguments
68280ab886dSwesolows 			 * are passed through for gcc to chew on.  However,
68380ab886dSwesolows 			 * output is always discarded for the secondary
68480ab886dSwesolows 			 * compiler.
6857c478bd9Sstevel@tonic-gate 			 */
68688e61e85SRichard Lowe 			if ((ctx->i_flags & CW_F_SHADOW) && in_output) {
68788e61e85SRichard Lowe 				newae(ctx->i_ae, discard_file_name(ctx, arg));
68888e61e85SRichard Lowe 			} else {
68980ab886dSwesolows 				newae(ctx->i_ae, arg);
69088e61e85SRichard Lowe 			}
69180ab886dSwesolows 			in_output = 0;
6927c478bd9Sstevel@tonic-gate 			continue;
6937c478bd9Sstevel@tonic-gate 		}
69580ab886dSwesolows 		if (ctx->i_flags & CW_F_CXX) {
696aa9ef484SJohn Levon 			if (strncmp(arg, "-_g++=", 6) == 0) {
697aa9ef484SJohn Levon 				newae(ctx->i_ae, strchr(arg, '=') + 1);
698aa9ef484SJohn Levon 				continue;
699aa9ef484SJohn Levon 			}
7007c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "-compat=", 8) == 0) {
7017c478bd9Sstevel@tonic-gate 				/* discard -compat=4 and -compat=5 */
7027c478bd9Sstevel@tonic-gate 				continue;
7037c478bd9Sstevel@tonic-gate 			}
7047c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-Qoption") == 0) {
7057c478bd9Sstevel@tonic-gate 				/* discard -Qoption and its two arguments */
70680ab886dSwesolows 				if (ctx->i_oldargc < 3)
7077c478bd9Sstevel@tonic-gate 					error(arg);
70880ab886dSwesolows 				ctx->i_oldargc -= 2;
70980ab886dSwesolows 				ctx->i_oldargv += 2;
7107c478bd9Sstevel@tonic-gate 				continue;
7117c478bd9Sstevel@tonic-gate 			}
7127c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-xwe") == 0) {
7137c478bd9Sstevel@tonic-gate 				/* turn warnings into errors */
71480ab886dSwesolows 				newae(ctx->i_ae, "-Werror");
7157c478bd9Sstevel@tonic-gate 				continue;
7167c478bd9Sstevel@tonic-gate 			}
7177c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-norunpath") == 0) {
7187c478bd9Sstevel@tonic-gate 				/* gcc has no corresponding option */
7197c478bd9Sstevel@tonic-gate 				continue;
7207c478bd9Sstevel@tonic-gate 			}
7217c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-nolib") == 0) {
7227c478bd9Sstevel@tonic-gate 				/* -nodefaultlibs is on by default */
7237c478bd9Sstevel@tonic-gate 				nolibc = 1;
7247c478bd9Sstevel@tonic-gate 				continue;
7257c478bd9Sstevel@tonic-gate 			}
7267c478bd9Sstevel@tonic-gate #if defined(__sparc)
7277c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-cg92") == 0) {
728e521259dSpetede 				mflag |= xlate_xtb(ctx->i_ae, "v8");
72980ab886dSwesolows 				xlate(ctx->i_ae, "super", xchip_tbl);
7307c478bd9Sstevel@tonic-gate 				continue;
7317c478bd9Sstevel@tonic-gate 			}
7327c478bd9Sstevel@tonic-gate #endif	/* __sparc */
7337c478bd9Sstevel@tonic-gate 		}
7357c478bd9Sstevel@tonic-gate 		switch ((c = arg[1])) {
7367c478bd9Sstevel@tonic-gate 		case '_':
737aa9ef484SJohn Levon 			if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) ||
738aa9ef484SJohn Levon 			    (strncmp(arg, "-_gcc=", 6) == 0) ||
739aa9ef484SJohn Levon 			    (strncmp(arg, "-_gnu=", 6) == 0)) {
740aa9ef484SJohn Levon 				newae(ctx->i_ae, strchr(arg, '=') + 1);
741aa9ef484SJohn Levon 			}
7427c478bd9Sstevel@tonic-gate 			break;
7437c478bd9Sstevel@tonic-gate 		case '#':
7447c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
74580ab886dSwesolows 				newae(ctx->i_ae, "-v");
7467c478bd9Sstevel@tonic-gate 				break;
7477c478bd9Sstevel@tonic-gate 			}
7487c478bd9Sstevel@tonic-gate 			error(arg);
7497c478bd9Sstevel@tonic-gate 			break;
750e1bf37b1SRichard Lowe 		case 'f':
751e1bf37b1SRichard Lowe 			if ((strcmp(arg, "-fpic") == 0) ||
752e1bf37b1SRichard Lowe 			    (strcmp(arg, "-fPIC") == 0)) {
753e1bf37b1SRichard Lowe 				newae(ctx->i_ae, arg);
754e1bf37b1SRichard Lowe 				break;
755e1bf37b1SRichard Lowe 			}
756e1bf37b1SRichard Lowe 			error(arg);
757e1bf37b1SRichard Lowe 			break;
7587c478bd9Sstevel@tonic-gate 		case 'E':
7597c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
76080ab886dSwesolows 				newae(ctx->i_ae, "-xc");
76180ab886dSwesolows 				newae(ctx->i_ae, arg);
76280ab886dSwesolows 				op = CW_O_PREPROCESS;
7637c478bd9Sstevel@tonic-gate 				nolibc = 1;
7647c478bd9Sstevel@tonic-gate 				break;
7657c478bd9Sstevel@tonic-gate 			}
7667c478bd9Sstevel@tonic-gate 			error(arg);
7677c478bd9Sstevel@tonic-gate 			break;
7687c478bd9Sstevel@tonic-gate 		case 'c':
7697c478bd9Sstevel@tonic-gate 		case 'S':
77080ab886dSwesolows 			if (arglen == 1) {
77180ab886dSwesolows 				op = CW_O_COMPILE;
7727c478bd9Sstevel@tonic-gate 				nolibc = 1;
77380ab886dSwesolows 			}
7747c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
7757c478bd9Sstevel@tonic-gate 		case 'C':
7767c478bd9Sstevel@tonic-gate 		case 'H':
7777c478bd9Sstevel@tonic-gate 		case 'p':
7787c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
77980ab886dSwesolows 				newae(ctx->i_ae, arg);
7807c478bd9Sstevel@tonic-gate 				break;
7817c478bd9Sstevel@tonic-gate 			}
7827c478bd9Sstevel@tonic-gate 			error(arg);
7837c478bd9Sstevel@tonic-gate 			break;
7847c478bd9Sstevel@tonic-gate 		case 'A':
785299e09deSRichard Lowe 		case 'g':
7867c478bd9Sstevel@tonic-gate 		case 'h':
7877c478bd9Sstevel@tonic-gate 		case 'I':
7887c478bd9Sstevel@tonic-gate 		case 'i':
7897c478bd9Sstevel@tonic-gate 		case 'L':
7907c478bd9Sstevel@tonic-gate 		case 'l':
7917c478bd9Sstevel@tonic-gate 		case 'R':
7927c478bd9Sstevel@tonic-gate 		case 'U':
7937c478bd9Sstevel@tonic-gate 		case 'u':
7947c478bd9Sstevel@tonic-gate 		case 'w':
79580ab886dSwesolows 			newae(ctx->i_ae, arg);
79680ab886dSwesolows 			break;
79780ab886dSwesolows 		case 'o':
79880ab886dSwesolows 			seen_o = 1;
79980ab886dSwesolows 			if (arglen == 1) {
80080ab886dSwesolows 				in_output = 1;
80180ab886dSwesolows 				newae(ctx->i_ae, arg);
80280ab886dSwesolows 			} else if (ctx->i_flags & CW_F_SHADOW) {
80380ab886dSwesolows 				newae(ctx->i_ae, "-o");
80488e61e85SRichard Lowe 				newae(ctx->i_ae, discard_file_name(ctx, arg));
80580ab886dSwesolows 			} else {
80680ab886dSwesolows 				newae(ctx->i_ae, arg);
80780ab886dSwesolows 			}
8087c478bd9Sstevel@tonic-gate 			break;
8097c478bd9Sstevel@tonic-gate 		case 'D':
81080ab886dSwesolows 			newae(ctx->i_ae, arg);
8117c478bd9Sstevel@tonic-gate 			/*
8127c478bd9Sstevel@tonic-gate 			 * XXX	Clearly a hack ... do we need _KADB too?
8137c478bd9Sstevel@tonic-gate 			 */
8147c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-D_KERNEL") == 0 ||
8157c478bd9Sstevel@tonic-gate 			    strcmp(arg, "-D_BOOT") == 0)
81680ab886dSwesolows 				newae(ctx->i_ae, "-ffreestanding");
8177c478bd9Sstevel@tonic-gate 			break;
8187c478bd9Sstevel@tonic-gate 		case 'd':
8197c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-dalign") == 0) {
8207c478bd9Sstevel@tonic-gate 				/*
8217c478bd9Sstevel@tonic-gate 				 * -dalign forces alignment in some cases;
8227c478bd9Sstevel@tonic-gate 				 * gcc does not need any flag to do this.
8237c478bd9Sstevel@tonic-gate 				 */
8247c478bd9Sstevel@tonic-gate 				break;
8257c478bd9Sstevel@tonic-gate 			}
8267c478bd9Sstevel@tonic-gate 			error(arg);
8277c478bd9Sstevel@tonic-gate 			break;
8287c478bd9Sstevel@tonic-gate 		case 'e':
8297c478bd9Sstevel@tonic-gate 			if (strcmp(arg,
8307c478bd9Sstevel@tonic-gate 			    "-erroff=E_EMPTY_TRANSLATION_UNIT") == 0) {
8317c478bd9Sstevel@tonic-gate 				/*
8327c478bd9Sstevel@tonic-gate 				 * Accept but ignore this -- gcc doesn't
8337c478bd9Sstevel@tonic-gate 				 * seem to complain about empty translation
8347c478bd9Sstevel@tonic-gate 				 * units
8357c478bd9Sstevel@tonic-gate 				 */
8367c478bd9Sstevel@tonic-gate 				break;
8377c478bd9Sstevel@tonic-gate 			}
8387c478bd9Sstevel@tonic-gate 			/* XX64 -- ignore all -erroff= options, for now */
8397c478bd9Sstevel@tonic-gate 			if (strncmp(arg, "-erroff=", 8) == 0)
8407c478bd9Sstevel@tonic-gate 				break;
8417c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-errtags=yes") == 0) {
84280ab886dSwesolows 				warnings(ctx->i_ae);
8437c478bd9Sstevel@tonic-gate 				break;
8447c478bd9Sstevel@tonic-gate 			}
8457c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-errwarn=%all") == 0) {
84680ab886dSwesolows 				newae(ctx->i_ae, "-Werror");
8477c478bd9Sstevel@tonic-gate 				break;
8487c478bd9Sstevel@tonic-gate 			}
8497c478bd9Sstevel@tonic-gate 			error(arg);
8507c478bd9Sstevel@tonic-gate 			break;
8517c478bd9Sstevel@tonic-gate 		case 'k':
8527c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-keeptmp") == 0) {
85380ab886dSwesolows 				newae(ctx->i_ae, "-save-temps");
8547c478bd9Sstevel@tonic-gate 				break;
8557c478bd9Sstevel@tonic-gate 			}
8567c478bd9Sstevel@tonic-gate 			error(arg);
8577c478bd9Sstevel@tonic-gate 			break;
8587c478bd9Sstevel@tonic-gate 		case 'm':
8597c478bd9Sstevel@tonic-gate 			if (strcmp(arg, "-mt") == 0) {
86080ab886dSwesolows 				newae(ctx->i_ae, "-D_REENTRANT");
8617c478bd9Sstevel@tonic-gate 				break;
8627c478bd9Sstevel@tonic-gate 			}
863e521259dSpetede 			if (strcmp(arg, "-m64") == 0) {
864e521259dSpetede 				newae(ctx->i_ae, "-m64");
865e521259dSpetede #if defined(__x86)
866e521259dSpetede 				newae(ctx->i_ae, "-mtune=opteron");
867e521259dSpetede #endif
868e521259dSpetede 				mflag |= M64;
869e521259dSpetede 				break;
870e521259dSpetede 			}
871e521259dSpetede 			if (strcmp(arg, "-m32") == 0) {
872e521259dSpetede 				newae(ctx->i_ae, "-m32");
873e521259dSpetede 				mflag |= M32;
874e521259dSpetede 				break;
875e521259dSpetede 			}
8767c478bd9Sstevel@tonic-gate 			error(arg);
8777c478bd9Sstevel@tonic-gate 			break;
8787c478bd9Sstevel@tonic-gate 		case 'O':
8797c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
88080ab886dSwesolows 				newae(ctx->i_ae, "-O");
8817c478bd9Sstevel@tonic-gate 				break;
8827c478bd9Sstevel@tonic-gate 			}
8837c478bd9Sstevel@tonic-gate 			error(arg);
8847c478bd9Sstevel@tonic-gate 			break;
8857c478bd9Sstevel@tonic-gate 		case 'P':
8867c478bd9Sstevel@tonic-gate 			/*
8877c478bd9Sstevel@tonic-gate 			 * We could do '-E -o filename.i', but that's hard,
8887c478bd9Sstevel@tonic-gate 			 * and we don't need it for the case that's triggering
8897c478bd9Sstevel@tonic-gate 			 * this addition.  We'll require the user to specify
8907c478bd9Sstevel@tonic-gate 			 * -o in the Makefile.  If they don't they'll find out
8917c478bd9Sstevel@tonic-gate 			 * in a hurry.
8927c478bd9Sstevel@tonic-gate 			 */
89380ab886dSwesolows 			newae(ctx->i_ae, "-E");
89480ab886dSwesolows 			op = CW_O_PREPROCESS;
8957c478bd9Sstevel@tonic-gate 			nolibc = 1;
8967c478bd9Sstevel@tonic-gate 			break;
8977c478bd9Sstevel@tonic-gate 		case 's':
89885f4cb87SRichard Lowe 			if (strcmp(arg, "-shared") == 0) {
89985f4cb87SRichard Lowe 				newae(ctx->i_ae, "-shared");
90085f4cb87SRichard Lowe 				nolibc = 1;
901159cf8a6Swesolows 				break;
902159cf8a6Swesolows 			}
903159cf8a6Swesolows 			error(arg);
904159cf8a6Swesolows 			break;
90585f4cb87SRichard Lowe 
9067c478bd9Sstevel@tonic-gate 		case 'V':
9077c478bd9Sstevel@tonic-gate 			if (arglen == 1) {
90880ab886dSwesolows 				ctx->i_flags &= ~CW_F_ECHO;
90980ab886dSwesolows 				newae(ctx->i_ae, "--version");
9107c478bd9Sstevel@tonic-gate 				break;
9117c478bd9Sstevel@tonic-gate 			}
9127c478bd9Sstevel@tonic-gate 			error(arg);
9137c478bd9Sstevel@tonic-gate 			break;
9147c478bd9Sstevel@tonic-gate 		case 'v':
9157c478bd9Sstevel@tonic-gate 			if (arglen == 1) {