xref: /titanic_51/usr/src/tools/cw/cw.c (revision 43a291055ab3951f6372241323fd4e2486098fff)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Wrapper for the GNU C compiler to make it accept the Sun C compiler
31  * arguments where possible.
32  *
33  * Since the translation is inexact, this is something of a work-in-progress.
34  */
35 
36 /*
37  * -#		Verbose mode
38  * -###		Show compiler commands built by driver, no compilation
39  * -A<name[(tokens)]>	Preprocessor predicate assertion
40  * -B<[static|dynamic]>	Specify dynamic or static binding
41  * -C		Prevent preprocessor from removing comments
42  * -c		Compile only - produce .o files, suppress linking
43  * -cg92	Alias for -xtarget=ss1000
44  * -D<name[=token]>	Associate name with token as if by #define
45  * -d[y|n]	dynamic [-dy] or static [-dn] option to linker
46  * -E		Compile source through preprocessor only, output to stdout
47  * -erroff=<t>	Suppress warnings specified by tags t(%none, %all, <tag list>)
48  * -errtags=<a>	Display messages with tags a(no, yes)
49  * -errwarn=<t>	Treats warnings specified by tags t(%none, %all, <tag list>)
50  *		as errors
51  * -fast	Optimize using a selection of options
52  * -fd		Report old-style function definitions and declarations
53  * -flags	Show this summary of compiler options
54  * -fnonstd	Initialize floating-point hardware to non-standard preferences
55  * -fns[=<yes|no>] Select non-standard floating point mode
56  * -fprecision=<p> Set FP rounding precision mode p(single, double, extended)
57  * -fround=<r>	Select the IEEE rounding mode in effect at startup
58  * -fsimple[=<n>] Select floating-point optimization preferences <n>
59  * -fsingle	Use single-precision arithmetic (-Xt and -Xs modes only)
60  * -ftrap=<t>	Select floating-point trapping mode in effect at startup
61  * -fstore	force floating pt. values to target precision on assignment
62  * -G		Build a dynamic shared library
63  * -g		Compile for debugging
64  * -H		Print path name of each file included during compilation
65  * -h <name>	Assign <name> to generated dynamic shared library
66  * -I<dir>	Add <dir> to preprocessor #include file search path
67  * -i		Passed to linker to ignore any LD_LIBRARY_PATH setting
68  * -keeptmp	Keep temporary files created during compilation
69  * -KPIC	Compile position independent code with 32-bit addresses
70  * -Kpic	Compile position independent code
71  * -L<dir>	Pass to linker to add <dir> to the library search path
72  * -l<name>	Link with library lib<name>.a or lib<name>.so
73  * -mc		Remove duplicate strings from .comment section of output files
74  * -mr		Remove all strings from .comment section of output files
75  * -mr,"string"	Remove all strings and append "string" to .comment section
76  * -mt		Specify options needed when compiling multi-threaded code
77  * -native	Find available processor, generate code accordingly
78  * -nofstore	Do not force floating pt. values to target precision
79  *		on assignment
80  * -nolib	Same as -xnolib
81  * -noqueue	Disable queuing of compiler license requests
82  * -norunpath	Do not build in a runtime path for shared libraries
83  * -O		Use default optimization level (-xO2 or -xO3. Check man page.)
84  * -o <outputfile> Set name of output file to <outputfile>
85  * -P		Compile source through preprocessor only, output to .i  file
86  * -PIC		Alias for -KPIC or -xcode=pic32
87  * -p		Compile for profiling with prof
88  * -pic		Alias for -Kpic or -xcode=pic13
89  * -Q[y|n]	Emit/don't emit identification info to output file
90  * -qp		Compile for profiling with prof
91  * -R<dir[:dir]> Build runtime search path list into executable
92  * -S		Compile and only generate assembly code (.s)
93  * -s		Strip symbol table from the executable file
94  * -U<name>	Delete initial definition of preprocessor symbol <name>
95  * -V		Report version number of each compilation phase
96  * -v		Do stricter semantic checking
97  * -W<c>,<arg>	Pass <arg> to specified component <c> (a,l,m,p,0,2,h,i,u)
98  * -w		Suppress compiler warning messages
99  * -Xa		Compile assuming ANSI C conformance, allow K & R extensions
100  *		(default mode)
101  * -Xc		Compile assuming strict ANSI C conformance
102  * -Xs		Compile assuming (pre-ANSI) K & R C style code
103  * -Xt		Compile assuming K & R conformance, allow ANSI C
104  * -x386	Generate code for the 80386 processor
105  * -x486	Generate code for the 80486 processor
106  * -xarch=<a>	Specify target architecture instruction set
107  * -xbuiltin[=<b>] When profitable inline, or substitute intrinisic functions
108  *		for system functions, b={%all,%none}
109  * -xCC		Accept C++ style comments
110  * -xchar_byte_order=<o> Specify multi-char byte order <o> (default, high, low)
111  * -xchip=<c>	Specify the target processor for use by the optimizer
112  * -xcode=<c>	Generate different code for forming addresses
113  * -xcrossfile[=<n>] Enable optimization and inlining across source files,
114  *		n={0|1}
115  * -xe		Perform only syntax/semantic checking, no code generation
116  * -xF		Compile for later mapfile reordering
117  * -xhelp=<f>	Display on-line help information f(flags, readme, errors)
118  * -xildoff	Cancel -xildon
119  * -xildon	Enable use of the incremental linker, ild
120  * -xinline=[<a>,...,<a>]  Attempt inlining of specified user routines,
121  *		<a>={%auto,func,no%func}
122  * -xlibmieee	Force IEEE 754 return values for math routines in
123  *		exceptional cases
124  * -xlibmil	Inline selected libm math routines for optimization
125  * -xlic_lib=sunperf	Link in the Sun supplied performance libraries
126  * -xlicinfo	Show license server information
127  * -xM		Generate makefile dependencies
128  * -xM1		Generate makefile dependencies, but exclude /usr/include
129  * -xmaxopt=[off,1,2,3,4,5] maximum optimization level allowed on #pragma opt
130  * -xnolib	Do not link with default system libraries
131  * -xnolibmil	Cancel -xlibmil on command line
132  * -xO<n>	Generate optimized code (n={1|2|3|4|5})
133  * -xP		Print prototypes for function definitions
134  * -xpentium	Generate code for the pentium processor
135  * -xpg		Compile for profiling with gprof
136  * -xprofile=<p> Collect data for a profile or use a profile to optimize
137  *		<p>={{collect,use}[:<path>],tcov}
138  * -xregs=<r>	Control register allocation
139  * -xs		Allow debugging without object (.o) files
140  * -xsb		Compile for use with the WorkShop source browser
141  * -xsbfast	Generate only WorkShop source browser info, no compilation
142  * -xsfpconst	Represent unsuffixed floating point constants as single
143  *		precision
144  * -xspace	Do not do optimizations that increase code size
145  * -xstrconst	Place string literals into read-only data segment
146  * -xtarget=<t>	Specify target system for optimization
147  * -xtemp=<dir>	Set directory for temporary files to <dir>
148  * -xtime	Report the execution time for each compilation phase
149  * -xtransition	Emit warnings for differences between K&R C and ANSI C
150  * -xtrigraphs[=<yes|no>] Enable|disable trigraph translation
151  * -xunroll=n	Enable unrolling loops n times where possible
152  * -Y<c>,<dir>	Specify <dir> for location of component <c> (a,l,m,p,0,h,i,u)
153  * -YA,<dir>	Change default directory searched for components
154  * -YI,<dir>	Change default directory searched for include files
155  * -YP,<dir>	Change default directory for finding libraries files
156  * -YS,<dir>	Change default directory for startup object files
157  */
158 
159 /*
160  * Translation table:
161  */
162 /*
163  * -#				-v
164  * -###				error
165  * -A<name[(tokens)]>		pass-thru
166  * -B<[static|dynamic]>		pass-thru (syntax error for anything else)
167  * -C				pass-thru
168  * -c				pass-thru
169  * -cg92			-m32 -mcpu=v8 -mtune=supersparc (SPARC only)
170  * -D<name[=token]>		pass-thru
171  * -dy or -dn			-Wl,-dy or -Wl,-dn
172  * -E				pass-thru
173  * -erroff=E_EMPTY_TRANSLATION_UNIT ignore
174  * -errtags=%all		-Wall
175  * -errwarn=%all		-Werror else -Wno-error
176  * -fast			error
177  * -fd				error
178  * -flags			--help
179  * -fnonstd			error
180  * -fns[=<yes|no>]		error
181  * -fprecision=<p>		error
182  * -fround=<r>			error
183  * -fsimple[=<n>]		error
184  * -fsingle[=<n>]		error
185  * -ftrap=<t>			error
186  * -fstore			error
187  * -G				pass-thru
188  * -g				pass-thru
189  * -H				pass-thru
190  * -h <name>			pass-thru
191  * -I<dir>			pass-thru
192  * -i				pass-thru
193  * -keeptmp			-save-temps
194  * -KPIC			-fPIC
195  * -Kpic			-fpic
196  * -L<dir>			pass-thru
197  * -l<name>			pass-thru
198  * -mc				error
199  * -mr				error
200  * -mr,"string"			error
201  * -mt				-D_REENTRANT
202  * -native			error
203  * -nofstore			error
204  * -nolib			-nodefaultlibs
205  * -noqueue			ignore
206  * -norunpath			ignore
207  * -O				-O1 (Check the man page to be certain)
208  * -o <outputfile>		pass-thru
209  * -P				-E -o filename.i (or error)
210  * -PIC				-fPIC (C++ only)
211  * -p				pass-thru
212  * -pic				-fpic (C++ only)
213  * -Q[y|n]			error
214  * -qp				-p
215  * -R<dir[:dir]>		pass-thru
216  * -S				pass-thru
217  * -s				-Wl,-s
218  * -U<name>			pass-thru
219  * -V				--version
220  * -v				-Wall
221  * -Wa,<arg>			pass-thru
222  * -Wp,<arg>			pass-thru except -xc99=<a>
223  * -Wl,<arg>			pass-thru
224  * -W{m,0,2,h,i,u>		error/ignore
225  * -Wu,-xmodel=kernel		-ffreestanding -mcmodel=kernel -mno-red-zone
226  * -w				pass-thru
227  * -Xa				-std=iso9899:199409 or -ansi
228  * -Xc				-ansi -pedantic
229  * -Xt				error
230  * -Xs				-traditional -std=c89
231  * -x386			-march=i386 (x86 only)
232  * -x486			-march=i486 (x86 only)
233  * -xarch=<a>			table
234  * -xbuiltin[=<b>]		error
235  * -xCC				ignore
236  * -xchar_byte_order=<o>	error
237  * -xchip=<c>			table
238  * -xcode=<c>			table
239  * -xcrossfile[=<n>]		error
240  * -xe				error
241  * -xF				error
242  * -xhelp=<f>			error
243  * -xildoff			ignore
244  * -xildon			ignore
245  * -xinline			ignore
246  * -xlibmieee			error
247  * -xlibmil			error
248  * -xlic_lib=sunperf		error
249  * -xM				-M
250  * -xM1				-MM
251  * -xmaxopt=[...]		error
252  * -xnolib			-nodefaultlibs
253  * -xnolibmil			error
254  * -xO<n>			-O<n>
255  * -xP				error
256  * -xpentium			-march=pentium (x86 only)
257  * -xpg				error
258  * -xprofile=<p>		error
259  * -xregs=<r>			table
260  * -xs				error
261  * -xsb				error
262  * -xsbfast			error
263  * -xsfpconst			error
264  * -xspace			ignore (-not -Os)
265  * -xstrconst			ignore
266  * -xtarget=<t>			table
267  * -xtemp=<dir>			error
268  * -xtime			error
269  * -xtransition			-Wtransition
270  * -xtrigraphs=<yes|no>		-trigraphs -notrigraphs
271  * -xunroll=n			error
272  * -Y<c>,<dir>			error
273  * -YA,<dir>			error
274  * -YI,<dir>			-nostdinc -I<dir>
275  * -YP,<dir>			error
276  * -YS,<dir>			error
277  */
278 
279 #include <stdio.h>
280 #include <sys/types.h>
281 #include <unistd.h>
282 #include <string.h>
283 #include <stdlib.h>
284 #include <ctype.h>
285 #include <sys/utsname.h>
286 #include <sys/param.h>
287 #include <sys/isa_defs.h>
288 
289 static int echo = 1;
290 static int newargc;
291 static const char *progname;
292 
293 static char *default_cc_dir;
294 static char *default_gcc_dir;
295 
296 static char *default_cplusplus_dir;
297 static char *default_gplusplus_dir;
298 
299 static const char *xarch_tbl[] = {
300 #if defined(__x86)
301 	"generic",	NULL,
302 	"generic64",	"-m64", "-mtune=opteron", NULL,
303 	"amd64",	"-m64", "-mtune=opteron", NULL,
304 	"386",		"-march=i386", NULL,
305 	"pentium_pro",	"-march=pentiumpro", NULL,
306 #elif defined(__sparc)
307 	"generic",	"-m32", "-mcpu=v8", NULL,
308 	"generic64",	"-m64", "-mcpu=v9", NULL,
309 	"v8",		"-m32", "-mcpu=v8", "-mno-v8plus", NULL,
310 	"v8plus",	"-m32", "-mcpu=v9", "-mv8plus", NULL,
311 	"v8plusa",	"-m32", "-mcpu=ultrasparc", "-mv8plus", "-mvis", NULL,
312 	"v8plusb",	"-m32", "-mcpu=ultrasparc3", "-mv8plus", "-mvis", NULL,
313 	"v9",		"-m64", "-mcpu=v9", NULL,
314 	"v9a",		"-m64", "-mcpu=ultrasparc", "-mvis", NULL,
315 	"v9b",		"-m64", "-mcpu=ultrasparc3", "-mvis", NULL,
316 #endif
317 	NULL,		NULL
318 };
319 
320 static const char *xchip_tbl[] = {
321 #if defined(__x86)
322 	"386",		"-mtune=i386", NULL,
323 	"486",		"-mtune=i486", NULL,
324 	"pentium",	"-mtune=pentium", NULL,
325 	"pentium_pro",  "-mtune=pentiumpro", NULL,
326 #elif defined(__sparc)
327 	"super",	"-mtune=supersparc", NULL,
328 	"ultra",	"-mtune=ultrasparc", NULL,
329 	"ultra3",	"-mtune=ultrasparc3", NULL,
330 #endif
331 	NULL,		NULL
332 };
333 
334 static const char *xcode_tbl[] = {
335 #if defined(__sparc)
336 	"abs32",	"-fno-pic", "-mcmodel=medlow", NULL,
337 	"abs44",	"-fno-pic", "-mcmodel=medmid", NULL,
338 	"abs64",	"-fno-pic", "-mcmodel=medany", NULL,
339 	"pic13",	"-fpic", NULL,
340 	"pic32",	"-fPIC", NULL,
341 #endif
342 	NULL,		NULL
343 };
344 
345 static const char *xtarget_tbl[] = {
346 #if defined(__x86)
347 	"pentium_pro",	"-march=pentiumpro", NULL,
348 #endif	/* __x86 */
349 	NULL,		NULL
350 };
351 
352 static const char *xregs_tbl[] = {
353 #if defined(__sparc)
354 	"appl",		"-mapp-regs", NULL,
355 	"no%appl",	"-mno-app-regs", NULL,
356 	"float",	"-mfpu", NULL,
357 	"no%float",	"-mno-fpu", NULL,
358 #endif	/* __sparc */
359 	NULL,		NULL
360 };
361 
362 struct aelist {
363 	struct ae {
364 		struct ae *ae_next;
365 		char *ae_arg;
366 	} *ael_head, *ael_tail;
367 };
368 
369 static struct aelist *
370 newael(void)
371 {
372 	return (calloc(sizeof (struct aelist), 1));
373 }
374 
375 static void
376 newae(struct aelist *ael, const char *arg)
377 {
378 	struct ae *ae;
379 
380 	ae = calloc(sizeof (*ae), 1);
381 	ae->ae_arg = strdup(arg);
382 	if (ael->ael_tail == NULL)
383 		ael->ael_head = ae;
384 	else
385 		ael->ael_tail->ae_next = ae;
386 	ael->ael_tail = ae;
387 	newargc++;
388 }
389 
390 static void
391 error(const char *arg)
392 {
393 	(void) fprintf(stderr,
394 	    "%s: mapping failed at or near arg '%s'\n", progname, arg);
395 	exit(2);
396 }
397 
398 /*
399  * Add the current favourite set of warnings to the gcc invocation.
400  */
401 static void
402 warnings(struct aelist *h)
403 {
404 	static int warningsonce;
405 
406 	if (warningsonce++)
407 		return;
408 
409 	newae(h, "-Wall");
410 	newae(h, "-Wno-unknown-pragmas");
411 	newae(h, "-Wno-missing-braces");
412 	newae(h, "-Wno-sign-compare");
413 	newae(h, "-Wno-parentheses");
414 	newae(h, "-Wno-uninitialized");
415 	newae(h, "-Wno-implicit-function-declaration");
416 	newae(h, "-Wno-unused");
417 	newae(h, "-Wno-trigraphs");
418 	newae(h, "-Wno-char-subscripts");
419 	newae(h, "-Wno-switch");
420 }
421 
422 static void
423 optim_disable(struct aelist *h, int level)
424 {
425 	if (level >= 2) {
426 		newae(h, "-fno-strict-aliasing");
427 		newae(h, "-fno-unit-at-a-time");
428 		newae(h, "-fno-optimize-sibling-calls");
429 	}
430 }
431 
432 /* ARGSUSED */
433 static void
434 Xamode(struct aelist *h)
435 {
436 }
437 
438 static void
439 Xcmode(struct aelist *h)
440 {
441 	static int xconce;
442 
443 	if (xconce++)
444 		return;
445 
446 	newae(h, "-ansi");
447 	newae(h, "-pedantic-errors");
448 }
449 
450 static void
451 Xsmode(struct aelist *h)
452 {
453 	static int xsonce;
454 
455 	if (xsonce++)
456 		return;
457 
458 	newae(h, "-traditional");
459 	newae(h, "-traditional-cpp");
460 }
461 
462 static void
463 usage()
464 {
465 	(void) fprintf(stderr,
466 	    "usage: %s { -_cc | -_gcc | -_CC | -_g++ } [ -_compiler | ... ]\n",
467 	    progname);
468 	exit(2);
469 }
470 
471 static void
472 xlate(struct aelist *h, const char *xarg, const char **table)
473 {
474 	while (*table != NULL && strcmp(xarg, *table) != 0) {
475 		while (*table != NULL)
476 			table++;
477 		table++;
478 	}
479 
480 	if (*table == NULL)
481 		error(xarg);
482 
483 	table++;
484 
485 	while (*table != NULL) {
486 		newae(h, *table);
487 		table++;
488 	}
489 }
490 
491 static void
492 do_gcc(const char *dir, const char *cmd, int argc, char **argv,
493     struct aelist *h, int cplusplus)
494 {
495 	int c;
496 	int pic = 0;
497 	int nolibc = 0;
498 	char *model = NULL;
499 	char *program;
500 	size_t len = strlen(dir) + strlen(cmd) + 2;
501 
502 	program = malloc(len);
503 	(void) snprintf(program, len, "%s/%s", dir, cmd);
504 
505 	/*
506 	 * Basic defaults for ON compilation
507 	 */
508 	newae(h, program);
509 
510 	newae(h, "-fident");
511 	newae(h, "-finline");
512 	newae(h, "-fno-inline-functions");
513 	newae(h, "-fno-builtin");
514 	newae(h, "-fno-asm");
515 	newae(h, "-nodefaultlibs");
516 
517 	/*
518 	 * This is needed because 'u' is defined
519 	 * under a conditional on 'sun'.  Should
520 	 * probably just remove the conditional,
521 	 * or make it be dependent on '__sun'.
522 	 *
523 	 * -Dunix is also missing in enhanced ANSI mode
524 	 */
525 	newae(h, "-D__sun");
526 
527 	/*
528 	 * Walk the argument list, translating as we go ..
529 	 */
530 
531 	while (--argc > 0) {
532 		char *arg = *++argv;
533 		size_t arglen = strlen(arg);
534 
535 		if (*arg == '-')
536 			arglen--;
537 		else {
538 			/*
539 			 * Discard inline files that gcc doesn't grok
540 			 */
541 			if (arglen > 3 &&
542 			    strcmp(arg + arglen - 3, ".il") == 0)
543 				continue;
544 
545 			/*
546 			 * Otherwise, filenames, and partial arguments
547 			 * are simply passed through for gcc to chew on.
548 			 */
549 			newae(h, arg);
550 			continue;
551 		}
552 
553 		if (cplusplus) {
554 			if (strncmp(arg, "-compat=", 8) == 0) {
555 				/* discard -compat=4 and -compat=5 */
556 				continue;
557 			}
558 			if (strcmp(arg, "-Qoption") == 0) {
559 				/* discard -Qoption and its two arguments */
560 				if (argc < 3)
561 					error(arg);
562 				argc -= 2;
563 				argv += 2;
564 				continue;
565 			}
566 			if (strcmp(arg, "-xwe") == 0) {
567 				/* turn warnings into errors */
568 				/* newae(h, "-Werror"); */
569 				continue;
570 			}
571 			if (strcmp(arg, "-noex") == 0) {
572 				/* no exceptions */
573 				newae(h, "-fno-exceptions");
574 				/* no run time type descriptor information */
575 				newae(h, "-fno-rtti");
576 				continue;
577 			}
578 			if (strcmp(arg, "-pic") == 0) {
579 				newae(h, "-fpic");
580 				pic = 1;
581 				continue;
582 			}
583 			if (strcmp(arg, "-PIC") == 0) {
584 				newae(h, "-fPIC");
585 				pic = 1;
586 				continue;
587 			}
588 			if (strcmp(arg, "-norunpath") == 0) {
589 				/* gcc has no corresponding option */
590 				continue;
591 			}
592 			if (strcmp(arg, "-nolib") == 0) {
593 				/* -nodefaultlibs is on by default */
594 				nolibc = 1;
595 				continue;
596 			}
597 #if defined(__sparc)
598 			if (strcmp(arg, "-cg92") == 0) {
599 				xlate(h, "v8", xarch_tbl);
600 				xlate(h, "super", xchip_tbl);
601 				continue;
602 			}
603 #endif	/* __sparc */
604 		}
605 
606 		switch ((c = arg[1])) {
607 		case '_':
608 			if (strcmp(arg, "-_noecho") == 0)
609 				echo = 0;
610 			else if (strncmp(arg, "-_cc=", 5) == 0 ||
611 			    strncmp(arg, "-_CC=", 5) == 0)
612 				/* EMPTY */;
613 			else if (strncmp(arg, "-_gcc=", 6) == 0 ||
614 			    strncmp(arg, "-_g++=", 6) == 0)
615 				newae(h, arg + 6);
616 			else if (strcmp(arg, "-_compiler") == 0) {
617 				(void) printf("%s\n", program);
618 				exit(0);
619 			} else
620 				error(arg);
621 			break;
622 		case '#':
623 			if (arglen == 1) {
624 				newae(h, "-v");
625 				break;
626 			}
627 			error(arg);
628 			break;
629 		case 'g':
630 			newae(h, "-gdwarf-2");
631 			break;
632 		case 'E':
633 			if (arglen == 1) {
634 				newae(h, "-xc");
635 				newae(h, arg);
636 				nolibc = 1;
637 				break;
638 			}
639 			error(arg);
640 			break;
641 		case 'c':
642 		case 'S':
643 			if (arglen == 1)
644 				nolibc = 1;
645 			/* FALLTHROUGH */
646 		case 'C':
647 		case 'H':
648 		case 'p':
649 			if (arglen == 1) {
650 				newae(h, arg);
651 				break;
652 			}
653 			error(arg);
654 			break;
655 		case 'A':
656 		case 'h':
657 		case 'I':
658 		case 'i':
659 		case 'L':
660 		case 'l':
661 		case 'o':
662 		case 'R':
663 		case 'U':
664 		case 'u':
665 		case 'w':
666 			newae(h, arg);
667 			break;
668 		case 'D':
669 			newae(h, arg);
670 			/*
671 			 * XXX	Clearly a hack ... do we need _KADB too?
672 			 */
673 			if (strcmp(arg, "-D_KERNEL") == 0 ||
674 			    strcmp(arg, "-D_BOOT") == 0)
675 				newae(h, "-ffreestanding");
676 			break;
677 		case 'd':
678 			if (arglen == 2) {
679 				if (strcmp(arg, "-dy") == 0) {
680 					newae(h, "-Wl,-dy");
681 					break;
682 				}
683 				if (strcmp(arg, "-dn") == 0) {
684 					newae(h, "-Wl,-dn");
685 					break;
686 				}
687 			}
688 			if (strcmp(arg, "-dalign") == 0) {
689 				/*
690 				 * -dalign forces alignment in some cases;
691 				 * gcc does not need any flag to do this.
692 				 */
693 				break;
694 			}
695 			error(arg);
696 			break;
697 		case 'e':
698 			if (strcmp(arg,
699 			    "-erroff=E_EMPTY_TRANSLATION_UNIT") == 0) {
700 				/*
701 				 * Accept but ignore this -- gcc doesn't
702 				 * seem to complain about empty translation
703 				 * units
704 				 */
705 				break;
706 			}
707 			/* XX64 -- ignore all -erroff= options, for now */
708 			if (strncmp(arg, "-erroff=", 8) == 0)
709 				break;
710 			if (strcmp(arg, "-errtags=yes") == 0) {
711 				warnings(h);
712 				break;
713 			}
714 			if (strcmp(arg, "-errwarn=%all") == 0) {
715 				newae(h, "-Werror");
716 				break;
717 			}
718 			error(arg);
719 			break;
720 		case 'f':
721 			if (strcmp(arg, "-flags") == 0) {
722 				newae(h, "--help");
723 				break;
724 			}
725 			error(arg);
726 			break;
727 		case 'G':
728 			newae(h, "-shared");
729 			nolibc = 1;
730 			break;
731 		case 'k':
732 			if (strcmp(arg, "-keeptmp") == 0) {
733 				newae(h, "-save-temps");
734 				break;
735 			}
736 			error(arg);
737 			break;
738 		case 'K':
739 			if (arglen == 1) {
740 				if ((arg = *++argv) == NULL || *arg == '\0')
741 					error("-K");
742 				argc--;
743 			} else {
744 				arg += 2;
745 			}
746 			if (strcmp(arg, "pic") == 0) {
747 				newae(h, "-fpic");
748 				pic = 1;
749 				break;
750 			}
751 			if (strcmp(arg, "PIC") == 0) {
752 				newae(h, "-fPIC");
753 				pic = 1;
754 				break;
755 			}
756 			error("-K");
757 			break;
758 		case 'm':
759 			if (strcmp(arg, "-mt") == 0) {
760 				newae(h, "-D_REENTRANT");
761 				break;
762 			}
763 			error(arg);
764 			break;
765 		case 'B':	/* linker options */
766 		case 'M':
767 		case 'z':
768 			{
769 				char *opt;
770 				size_t len;
771 				char *s;
772 
773 				if (arglen == 1) {
774 					opt = *++argv;
775 					if (opt == NULL || *opt == '\0')
776 						error(arg);
777 					argc--;
778 				} else {
779 					opt = arg + 2;
780 				}
781 				len = strlen(opt) + 7;
782 				s = malloc(len);
783 				(void) snprintf(s, len, "-Wl,-%c%s", c, opt);
784 				newae(h, s);
785 				free(s);
786 			}
787 			break;
788 		case 'n':
789 			if (strcmp(arg, "-noqueue") == 0) {
790 				/*
791 				 * Horrid license server stuff - n/a
792 				 */
793 				break;
794 			}
795 			error(arg);
796 			break;
797 		case 'O':
798 			if (arglen == 1) {
799 				newae(h, "-O");
800 				break;
801 			}
802 			error(arg);
803 			break;
804 		case 'P':
805 			/*
806 			 * We could do '-E -o filename.i', but that's hard,
807 			 * and we don't need it for the case that's triggering
808 			 * this addition.  We'll require the user to specify
809 			 * -o in the Makefile.  If they don't they'll find out
810 			 * in a hurry.
811 			 */
812 			newae(h, "-E");
813 			nolibc = 1;
814 			break;
815 		case 'q':
816 			if (strcmp(arg, "-qp") == 0) {
817 				newae(h, "-p");
818 				break;
819 			}
820 			error(arg);
821 			break;
822 		case 's':
823 			if (arglen == 1) {
824 				newae(h, "-Wl,-s");
825 				break;
826 			}
827 			error(arg);
828 			break;
829 		case 'V':
830 			if (arglen == 1) {
831 				echo = 0;
832 				newae(h, "--version");
833 				break;
834 			}
835 			error(arg);
836 			break;
837 		case 'v':
838 			if (arglen == 1) {
839 				warnings(h);
840 				break;
841 			}
842 			error(arg);
843 			break;
844 		case 'W':
845 			if (strncmp(arg, "-Wp,-xc99", 9) == 0) {
846 				/*
847 				 * gcc's preprocessor will accept c99
848 				 * regardless, so accept and ignore.
849 				 */
850 				break;
851 			}
852 			if (strncmp(arg, "-Wa,", 4) == 0 ||
853 			    strncmp(arg, "-Wp,", 4) == 0 ||
854 			    strncmp(arg, "-Wl,", 4) == 0) {
855 				newae(h, arg);
856 				break;
857 			}
858 			if (strcmp(arg, "-W0,-xc99=pragma") == 0) {
859 				/* (undocumented) enables _Pragma */
860 				break;
861 			}
862 			if (strcmp(arg, "-W0,-xc99=%none") == 0) {
863 				/*
864 				 * This is a polite way of saying
865 				 * "no c99 constructs allowed!"
866 				 * For now, just accept and ignore this.
867 				 */
868 				break;
869 			}
870 			if (strcmp(arg, "-W0,-noglobal") == 0) {
871 				/*
872 				 * gcc doesn't prefix local symbols
873 				 * in debug mode, so this is not needed.
874 				 */
875 				break;
876 			}
877 			if (strcmp(arg, "-W0,-Lt") == 0) {
878 				/*
879 				 * Generate tests at the top of loops.
880 				 * There is no direct gcc equivalent, ignore.
881 				 */
882 				break;
883 			}
884 			if (strcmp(arg, "-W2,-Rcond_elim") == 0) {
885 				/*
886 				 * Elimination and expansion of conditionals;
887 				 * gcc has no direct equivalent.
888 				 */
889 				break;
890 			}
891 			if (strcmp(arg, "-Wd,-xsafe=unboundsym") == 0) {
892 				/*
893 				 * Prevents optimizing away checks for
894 				 * unbound weak symbol addresses.  gcc does
895 				 * not do this, so it's not needed.
896 				 */
897 				break;
898 			}
899 			if (strncmp(arg, "-Wc,-xcode=", 11) == 0) {
900 				xlate(h, arg + 11, xcode_tbl);
901 				if (strncmp(arg + 11, "pic", 3) == 0)
902 					pic = 1;
903 				break;
904 			}
905 			if (strncmp(arg, "-Wc,-Qiselect", 13) == 0) {
906 				/*
907 				 * Prevents insertion of register symbols.
908 				 * gcc doesn't do this, so ignore it.
909 				 */
910 				break;
911 			}
912 #if defined(__x86)
913 			if (strcmp(arg, "-Wu,-no_got_reloc") == 0) {
914 				/*
915 				 * Don't create any GOT relocations?
916 				 * Well, gcc doesn't have this degree
917 				 * of control over its pic code ...
918 				 */
919 				break;
920 			}
921 			if (strcmp(arg, "-Wu,-xmodel=kernel") == 0) {
922 				newae(h, "-ffreestanding");
923 				newae(h, "-mno-red-zone");
924 				model = "-mcmodel=kernel";
925 				nolibc = 1;
926 				break;
927 			}
928 #endif	/* __x86 */
929 			error(arg);
930 			break;
931 		case 'X':
932 			if (strcmp(arg, "-Xa") == 0 ||
933 			    strcmp(arg, "-Xt") == 0) {
934 				Xamode(h);
935 				break;
936 			}
937 			if (strcmp(arg, "-Xc") == 0) {
938 				Xcmode(h);
939 				break;
940 			}
941 			if (strcmp(arg, "-Xs") == 0) {
942 				Xsmode(h);
943 				break;
944 			}
945 			error(arg);
946 			break;
947 		case 'x':
948 			if (arglen == 1)
949 				error(arg);
950 			switch (arg[2]) {
951 #if defined(__x86)
952 			case '3':
953 				if (strcmp(arg, "-x386") == 0) {
954 					newae(h, "-march=i386");
955 					break;
956 				}
957 				error(arg);
958 				break;
959 			case '4':
960 				if (strcmp(arg, "-x486") == 0) {
961 					newae(h, "-march=i486");
962 					break;
963 				}
964 				error(arg);
965 				break;
966 #endif	/* __x86 */
967 			case 'a':
968 				if (strncmp(arg, "-xarch=", 7) == 0) {
969 					xlate(h, arg + 7, xarch_tbl);
970 					break;
971 				}
972 				error(arg);
973 				break;
974 			case 'C':
975 				/* Accept C++ style comments -- ignore */
976 				if (strcmp(arg, "-xCC") == 0)
977 					break;
978 				error(arg);
979 				break;
980 			case 'c':
981 				if (strncmp(arg, "-xc99=%all", 10) == 0) {
982 					newae(h, "-std=gnu99");
983 					break;
984 				}
985 				if (strncmp(arg, "-xc99=%none", 11) == 0) {
986 					newae(h, "-std=gnu89");
987 					break;
988 				}
989 				if (strncmp(arg, "-xchip=", 7) == 0) {
990 					xlate(h, arg + 7, xchip_tbl);
991 					break;
992 				}
993 				if (strncmp(arg, "-xcode=", 7) == 0) {
994 					xlate(h, arg + 7, xcode_tbl);
995 					if (strncmp(arg + 7, "pic", 3) == 0)
996 						pic = 1;
997 					break;
998 				}
999 				if (strncmp(arg, "-xcache=", 8) == 0)
1000 					break;
1001 				error(arg);
1002 				break;
1003 			case 'd':
1004 				if (strcmp(arg, "-xdepend") == 0)
1005 					break;
1006 				error(arg);
1007 				break;
1008 			case 'F':
1009 				/* compile for mapfile reordering -- ignore */
1010 				if (strcmp(arg, "-xF") == 0)
1011 					break;
1012 				error(arg);
1013 				break;
1014 			case 'i':
1015 				if (strncmp(arg, "-xinline", 8) == 0)
1016 					/* No inlining; ignore */
1017 					break;
1018 				if (strcmp(arg, "-xildon") == 0 ||
1019 				    strcmp(arg, "-xildoff") == 0)
1020 					/* No incremental linking; ignore */
1021 					break;
1022 				error(arg);
1023 				break;
1024 			case 'M':
1025 				if (strcmp(arg, "-xM") == 0) {
1026 					newae(h, "-M");
1027 					break;
1028 				}
1029 				if (strcmp(arg, "-xM1") == 0) {
1030 					newae(h, "-MM");
1031 					break;
1032 				}
1033 				error(arg);
1034 				break;
1035 			case 'n':
1036 				if (strcmp(arg, "-xnolib") == 0) {
1037 					nolibc = 1;
1038 					break;
1039 				}
1040 				error(arg);
1041 				break;
1042 			case 'O':
1043 				if (strncmp(arg, "-xO", 3) == 0) {
1044 					size_t len = strlen(arg);
1045 					char *s = malloc(len);
1046 					int c = *(arg + 3);
1047 					int level;
1048 
1049 					if (len != 4 || !isdigit(c))
1050 						error(arg);
1051 
1052 					level = atoi(arg + 3);
1053 					if (level > 5)
1054 						error(arg);
1055 					if (level >= 2) {
1056 						/*
1057 						 * For gcc-3.4.x at -O2 we
1058 						 * need to disable optimizations
1059 						 * that break ON.
1060 						 */
1061 						optim_disable(h, level);
1062 						/*
1063 						 * limit -xO3 to -O2 as well.
1064 						 */
1065 						level = 2;
1066 					}
1067 					(void) snprintf(s, len, "-O%d", level);
1068 					newae(h, s);
1069 					free(s);
1070 					break;
1071 				}
1072 				error(arg);
1073 				break;
1074 			case 'p':
1075 				if (strcmp(arg, "-xpentium") == 0) {
1076 					newae(h, "-march=pentium");
1077 					break;
1078 				}
1079 				if (strcmp(arg, "-xpg") == 0) {
1080 					newae(h, "-pg");
1081 					break;
1082 				}
1083 				error(arg);
1084 				break;
1085 			case 'r':
1086 				if (strncmp(arg, "-xregs=", 7) == 0) {
1087 					xlate(h, arg + 7, xregs_tbl);
1088 					break;
1089 				}
1090 				error(arg);
1091 				break;
1092 			case 's':
1093 				if (strcmp(arg, "-xs") == 0 ||
1094 				    strcmp(arg, "-xspace") == 0 ||
1095 				    strcmp(arg, "-xstrconst") == 0)
1096 					break;
1097 				error(arg);
1098 				break;
1099 			case 't':
1100 				if (strcmp(arg, "-xtransition") == 0) {
1101 					newae(h, "-Wtransition");
1102 					break;
1103 				}
1104 				if (strcmp(arg, "-xtrigraphs=yes") == 0) {
1105 					newae(h, "-trigraphs");
1106 					break;
1107 				}
1108 				if (strcmp(arg, "-xtrigraphs=no") == 0) {
1109 					newae(h, "-notrigraphs");
1110 					break;
1111 				}
1112 				if (strncmp(arg, "-xtarget=", 9) == 0) {
1113 					xlate(h, arg + 9, xtarget_tbl);
1114 					break;
1115 				}
1116 				error(arg);
1117 				break;
1118 			case 'b':
1119 			case 'e':
1120 			case 'h':
1121 			case 'l':
1122 			default:
1123 				error(arg);
1124 				break;
1125 			}
1126 			break;
1127 		case 'Y':
1128 			if (arglen == 1) {
1129 				if ((arg = *++argv) == NULL || *arg == '\0')
1130 					error("-Y");
1131 				argc--;
1132 				arglen = strlen(arg + 1);
1133 			} else {
1134 				arg += 2;
1135 			}
1136 			/* Just ignore -YS,... for now */
1137 			if (strncmp(arg, "S,", 2) == 0)
1138 				break;
1139 			if (strncmp(arg, "l,", 2) == 0) {
1140 				char *s = strdup(arg);
1141 				s[0] = '-';
1142 				s[1] = 'B';
1143 				newae(h, s);
1144 				free(s);
1145 				break;
1146 			}
1147 			if (strncmp(arg, "I,", 2) == 0) {
1148 				char *s = strdup(arg);
1149 				s[0] = '-';
1150 				s[1] = 'I';
1151 				newae(h, "-nostdinc");
1152 				newae(h, s);
1153 				free(s);
1154 				break;
1155 			}
1156 			error(arg);
1157 			break;
1158 		case 'Q':
1159 			/*
1160 			 * We could map -Qy into -Wl,-Qy etc.
1161 			 */
1162 		default:
1163 			error(arg);
1164 			break;
1165 		}
1166 	}
1167 
1168 	if (model && !pic)
1169 		newae(h, model);
1170 	if (!nolibc)
1171 		newae(h, "-lc");
1172 }
1173 
1174 /* ARGSUSED4 */
1175 static void
1176 do_cc(const char *dir, const char *cmd, int argc, char **argv,
1177     struct aelist *h, int cplusplus)
1178 {
1179 	char *program;
1180 	size_t len = strlen(dir) + strlen(cmd) + 2;
1181 
1182 	program = malloc(len);
1183 	(void) snprintf(program, len, "%s/%s", dir, cmd);
1184 
1185 	/*
1186 	 * This is pretty simple.
1187 	 * We just have to recognize -V, -_noecho, -_compiler, -_cc= and -_gcc=
1188 	 */
1189 	newae(h, program);
1190 
1191 	while (--argc > 0) {
1192 		char *arg = *++argv;
1193 
1194 		if (*arg != '-') {
1195 			newae(h, arg);
1196 		} else if (*(arg + 1) != '_') {
1197 			if (strcmp(arg, "-V") == 0)
1198 				echo = 0;
1199 			newae(h, arg);
1200 		} else if (strcmp(arg, "-_noecho") == 0) {
1201 			echo = 0;
1202 		} else if (strcmp(arg, "-_compiler") == 0) {
1203 			(void) printf("%s\n", program);
1204 			exit(0);
1205 		} else if (strncmp(arg, "-_cc=", 5) == 0 ||
1206 		    strncmp(arg, "-_CC=", 5) == 0) {
1207 			newae(h, arg + 5);
1208 		} else if (strncmp(arg, "-_gcc=", 6) != 0 &&
1209 		    strncmp(arg, "-_g++=", 6) != 0) {
1210 			(void) fprintf(stderr,
1211 			    "%s: invalid argument '%s'\n", progname, arg);
1212 			exit(2);
1213 		}
1214 	}
1215 }
1216 
1217 int
1218 main(int argc, char **argv)
1219 {
1220 	struct aelist *h = newael();
1221 	const char *dir;
1222 	int ac;
1223 	char **newargv;
1224 	struct ae *a;
1225 	char cc_buf[MAXPATHLEN], gcc_buf[MAXPATHLEN];
1226 
1227 	if ((progname = strrchr(argv[0], '/')) == NULL)
1228 		progname = argv[0];
1229 	else
1230 		progname++;
1231 
1232 	default_cc_dir = DEFAULT_CC_DIR;
1233 	default_gcc_dir = DEFAULT_GCC_DIR;
1234 	default_cplusplus_dir = DEFAULT_CPLUSPLUS_DIR;
1235 	default_gplusplus_dir = DEFAULT_GPLUSPLUS_DIR;
1236 
1237 	/*
1238 	 * Figure out where to get our tools from.  This depends on
1239 	 * the environment variables set at run time.
1240 	 */
1241 	if ((dir = getenv("SPRO_VROOT")) != NULL) {
1242 		(void) snprintf(cc_buf, MAXPATHLEN, "%s/bin", dir);
1243 	} else if ((dir = getenv("SPRO_ROOT")) != NULL) {
1244 		(void) snprintf(cc_buf, MAXPATHLEN, "%s/SOS8/bin", dir);
1245 	} else if ((dir = getenv("BUILD_TOOLS")) != NULL) {
1246 		(void) snprintf(cc_buf, MAXPATHLEN,
1247 		    "%s/SUNWspro/SOS8/bin", dir);
1248 	}
1249 	if (dir != NULL)
1250 		default_cc_dir = (char *)cc_buf;
1251 
1252 	if ((dir = getenv("GNU_ROOT")) != NULL) {
1253 		(void) snprintf(gcc_buf, MAXPATHLEN, "%s/bin", dir);
1254 		default_gcc_dir = (char *)gcc_buf;
1255 	}
1256 
1257 	default_cplusplus_dir = default_cc_dir;
1258 	default_gplusplus_dir = default_gcc_dir;
1259 
1260 	/*
1261 	 * The first argument must be one of "-_cc", "-_gcc", "-_CC", or "-_g++"
1262 	 */
1263 	if (argc == 1)
1264 		usage();
1265 	argc--;
1266 	argv++;
1267 	if (strcmp(argv[0], "-_cc") == 0) {
1268 		if ((dir = getenv("CW_CC_DIR")) == NULL)
1269 			dir = default_cc_dir;
1270 		do_cc(dir, "cc", argc, argv, h, 0);
1271 	} else if (strcmp(argv[0], "-_gcc") == 0) {
1272 		if ((dir = getenv("CW_GCC_DIR")) == NULL)
1273 			dir = default_gcc_dir;
1274 		do_gcc(dir, "gcc", argc, argv, h, 0);
1275 	} else if (strcmp(argv[0], "-_CC") == 0) {
1276 		if ((dir = getenv("CW_CPLUSPLUS_DIR")) == NULL)
1277 			dir = default_cplusplus_dir;
1278 		do_cc(dir, "CC", argc, argv, h, 1);
1279 	} else if (strcmp(argv[0], "-_g++") == 0) {
1280 		if ((dir = getenv("CW_GPLUSPLUS_DIR")) == NULL)
1281 			dir = default_gplusplus_dir;
1282 		do_gcc(dir, "g++", argc, argv, h, 1);
1283 	} else {
1284 		/* assume "-_gcc" by default */
1285 		argc++;
1286 		argv--;
1287 		if ((dir = getenv("CW_GCC_DIR")) == NULL)
1288 			dir = default_gcc_dir;
1289 		do_gcc(dir, "gcc", argc, argv, h, 0);
1290 	}
1291 
1292 	newargv = calloc(sizeof (*newargv), newargc + 1);
1293 
1294 	if (echo)
1295 		(void) printf("+ ");
1296 
1297 	for (ac = 0, a = h->ael_head; a; a = a->ae_next, ac++) {
1298 		newargv[ac] = a->ae_arg;
1299 		if (echo)
1300 			(void) printf("%s ", a->ae_arg);
1301 		if (a == h->ael_tail)
1302 			break;
1303 	}
1304 
1305 	if (echo) {
1306 		(void) printf("\n");
1307 		(void) fflush(stdout);
1308 	}
1309 
1310 	/*
1311 	 * Here goes ..
1312 	 */
1313 	(void) execvp(newargv[0], newargv);
1314 
1315 	/*
1316 	 * execvp() returns only on error.
1317 	 */
1318 	perror("execvp");
1319 	(void) fprintf(stderr, "%s: couldn't run %s\n",
1320 	    progname, newargv[0]);
1321 	return (4);
1322 }
1323