xref: /titanic_52/usr/src/tools/cw/cw.c (revision d430274b3de06b41cfcaa530b57efe610b45b122)
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  * -Wu,-save_args		-msave-args
227  * -w				pass-thru
228  * -Xa				-std=iso9899:199409 or -ansi
229  * -Xc				-ansi -pedantic
230  * -Xt				error
231  * -Xs				-traditional -std=c89
232  * -x386			-march=i386 (x86 only)
233  * -x486			-march=i486 (x86 only)
234  * -xarch=<a>			table
235  * -xbuiltin[=<b>]		error
236  * -xCC				ignore
237  * -xchar_byte_order=<o>	error
238  * -xchip=<c>			table
239  * -xcode=<c>			table
240  * -xcrossfile[=<n>]		error
241  * -xe				error
242  * -xF				error
243  * -xhelp=<f>			error
244  * -xildoff			ignore
245  * -xildon			ignore
246  * -xinline			ignore
247  * -xlibmieee			error
248  * -xlibmil			error
249  * -xlic_lib=sunperf		error
250  * -xM				-M
251  * -xM1				-MM
252  * -xmaxopt=[...]		error
253  * -xnolib			-nodefaultlibs
254  * -xnolibmil			error
255  * -xO<n>			-O<n>
256  * -xP				error
257  * -xpentium			-march=pentium (x86 only)
258  * -xpg				error
259  * -xprofile=<p>		error
260  * -xregs=<r>			table
261  * -xs				error
262  * -xsb				error
263  * -xsbfast			error
264  * -xsfpconst			error
265  * -xspace			ignore (-not -Os)
266  * -xstrconst			ignore
267  * -xtarget=<t>			table
268  * -xtemp=<dir>			error
269  * -xtime			error
270  * -xtransition			-Wtransition
271  * -xtrigraphs=<yes|no>		-trigraphs -notrigraphs
272  * -xunroll=n			error
273  * -Y<c>,<dir>			error
274  * -YA,<dir>			error
275  * -YI,<dir>			-nostdinc -I<dir>
276  * -YP,<dir>			error
277  * -YS,<dir>			error
278  */
279 
280 #include <stdio.h>
281 #include <sys/types.h>
282 #include <unistd.h>
283 #include <string.h>
284 #include <stdlib.h>
285 #include <ctype.h>
286 #include <sys/utsname.h>
287 #include <sys/param.h>
288 #include <sys/isa_defs.h>
289 
290 static int echo = 1;
291 static int newargc;
292 static const char *progname;
293 
294 static char *default_cc_dir;
295 static char *default_gcc_dir;
296 
297 static char *default_cplusplus_dir;
298 static char *default_gplusplus_dir;
299 
300 static const char *xarch_tbl[] = {
301 #if defined(__x86)
302 	"generic",	NULL,
303 	"generic64",	"-m64", "-mtune=opteron", NULL,
304 	"amd64",	"-m64", "-mtune=opteron", NULL,
305 	"386",		"-march=i386", NULL,
306 	"pentium_pro",	"-march=pentiumpro", NULL,
307 #elif defined(__sparc)
308 	"generic",	"-m32", "-mcpu=v8", NULL,
309 	"generic64",	"-m64", "-mcpu=v9", NULL,
310 	"v8",		"-m32", "-mcpu=v8", "-mno-v8plus", NULL,
311 	"v8plus",	"-m32", "-mcpu=v9", "-mv8plus", NULL,
312 	"v8plusa",	"-m32", "-mcpu=ultrasparc", "-mv8plus", "-mvis", NULL,
313 	"v8plusb",	"-m32", "-mcpu=ultrasparc3", "-mv8plus", "-mvis", NULL,
314 	"v9",		"-m64", "-mcpu=v9", NULL,
315 	"v9a",		"-m64", "-mcpu=ultrasparc", "-mvis", NULL,
316 	"v9b",		"-m64", "-mcpu=ultrasparc3", "-mvis", NULL,
317 #endif
318 	NULL,		NULL
319 };
320 
321 static const char *xchip_tbl[] = {
322 #if defined(__x86)
323 	"386",		"-mtune=i386", NULL,
324 	"486",		"-mtune=i486", NULL,
325 	"pentium",	"-mtune=pentium", NULL,
326 	"pentium_pro",  "-mtune=pentiumpro", NULL,
327 #elif defined(__sparc)
328 	"super",	"-mtune=supersparc", NULL,
329 	"ultra",	"-mtune=ultrasparc", NULL,
330 	"ultra3",	"-mtune=ultrasparc3", NULL,
331 #endif
332 	NULL,		NULL
333 };
334 
335 static const char *xcode_tbl[] = {
336 #if defined(__sparc)
337 	"abs32",	"-fno-pic", "-mcmodel=medlow", NULL,
338 	"abs44",	"-fno-pic", "-mcmodel=medmid", NULL,
339 	"abs64",	"-fno-pic", "-mcmodel=medany", NULL,
340 	"pic13",	"-fpic", NULL,
341 	"pic32",	"-fPIC", NULL,
342 #endif
343 	NULL,		NULL
344 };
345 
346 static const char *xtarget_tbl[] = {
347 #if defined(__x86)
348 	"pentium_pro",	"-march=pentiumpro", NULL,
349 #endif	/* __x86 */
350 	NULL,		NULL
351 };
352 
353 static const char *xregs_tbl[] = {
354 #if defined(__sparc)
355 	"appl",		"-mapp-regs", NULL,
356 	"no%appl",	"-mno-app-regs", NULL,
357 	"float",	"-mfpu", NULL,
358 	"no%float",	"-mno-fpu", NULL,
359 #endif	/* __sparc */
360 	NULL,		NULL
361 };
362 
363 struct aelist {
364 	struct ae {
365 		struct ae *ae_next;
366 		char *ae_arg;
367 	} *ael_head, *ael_tail;
368 };
369 
370 static struct aelist *
371 newael(void)
372 {
373 	return (calloc(sizeof (struct aelist), 1));
374 }
375 
376 static void
377 newae(struct aelist *ael, const char *arg)
378 {
379 	struct ae *ae;
380 
381 	ae = calloc(sizeof (*ae), 1);
382 	ae->ae_arg = strdup(arg);
383 	if (ael->ael_tail == NULL)
384 		ael->ael_head = ae;
385 	else
386 		ael->ael_tail->ae_next = ae;
387 	ael->ael_tail = ae;
388 	newargc++;
389 }
390 
391 static void
392 error(const char *arg)
393 {
394 	(void) fprintf(stderr,
395 	    "%s: mapping failed at or near arg '%s'\n", progname, arg);
396 	exit(2);
397 }
398 
399 /*
400  * Add the current favourite set of warnings to the gcc invocation.
401  */
402 static void
403 warnings(struct aelist *h)
404 {
405 	static int warningsonce;
406 
407 	if (warningsonce++)
408 		return;
409 
410 	newae(h, "-Wall");
411 	newae(h, "-Wno-unknown-pragmas");
412 	newae(h, "-Wno-missing-braces");
413 	newae(h, "-Wno-sign-compare");
414 	newae(h, "-Wno-parentheses");
415 	newae(h, "-Wno-uninitialized");
416 	newae(h, "-Wno-implicit-function-declaration");
417 	newae(h, "-Wno-unused");
418 	newae(h, "-Wno-trigraphs");
419 	newae(h, "-Wno-char-subscripts");
420 	newae(h, "-Wno-switch");
421 }
422 
423 static void
424 optim_disable(struct aelist *h, int level)
425 {
426 	if (level >= 2) {
427 		newae(h, "-fno-strict-aliasing");
428 		newae(h, "-fno-unit-at-a-time");
429 		newae(h, "-fno-optimize-sibling-calls");
430 	}
431 }
432 
433 /* ARGSUSED */
434 static void
435 Xamode(struct aelist *h)
436 {
437 }
438 
439 static void
440 Xcmode(struct aelist *h)
441 {
442 	static int xconce;
443 
444 	if (xconce++)
445 		return;
446 
447 	newae(h, "-ansi");
448 	newae(h, "-pedantic-errors");
449 }
450 
451 static void
452 Xsmode(struct aelist *h)
453 {
454 	static int xsonce;
455 
456 	if (xsonce++)
457 		return;
458 
459 	newae(h, "-traditional");
460 	newae(h, "-traditional-cpp");
461 }
462 
463 static void
464 usage()
465 {
466 	(void) fprintf(stderr,
467 	    "usage: %s { -_cc | -_gcc | -_CC | -_g++ } [ -_compiler | ... ]\n",
468 	    progname);
469 	exit(2);
470 }
471 
472 static void
473 xlate(struct aelist *h, const char *xarg, const char **table)
474 {
475 	while (*table != NULL && strcmp(xarg, *table) != 0) {
476 		while (*table != NULL)
477 			table++;
478 		table++;
479 	}
480 
481 	if (*table == NULL)
482 		error(xarg);
483 
484 	table++;
485 
486 	while (*table != NULL) {
487 		newae(h, *table);
488 		table++;
489 	}
490 }
491 
492 static void
493 do_gcc(const char *dir, const char *cmd, int argc, char **argv,
494     struct aelist *h, int cplusplus)
495 {
496 	int c;
497 	int pic = 0;
498 	int nolibc = 0;
499 	char *model = NULL;
500 	char *program;
501 	size_t len = strlen(dir) + strlen(cmd) + 2;
502 
503 	program = malloc(len);
504 	(void) snprintf(program, len, "%s/%s", dir, cmd);
505 
506 	/*
507 	 * Basic defaults for ON compilation
508 	 */
509 	newae(h, program);
510 
511 	newae(h, "-fident");
512 	newae(h, "-finline");
513 	newae(h, "-fno-inline-functions");
514 	newae(h, "-fno-builtin");
515 	newae(h, "-fno-asm");
516 	newae(h, "-nodefaultlibs");
517 
518 	/*
519 	 * This is needed because 'u' is defined
520 	 * under a conditional on 'sun'.  Should
521 	 * probably just remove the conditional,
522 	 * or make it be dependent on '__sun'.
523 	 *
524 	 * -Dunix is also missing in enhanced ANSI mode
525 	 */
526 	newae(h, "-D__sun");
527 
528 	/*
529 	 * Walk the argument list, translating as we go ..
530 	 */
531 
532 	while (--argc > 0) {
533 		char *arg = *++argv;
534 		size_t arglen = strlen(arg);
535 
536 		if (*arg == '-')
537 			arglen--;
538 		else {
539 			/*
540 			 * Discard inline files that gcc doesn't grok
541 			 */
542 			if (arglen > 3 &&
543 			    strcmp(arg + arglen - 3, ".il") == 0)
544 				continue;
545 
546 			/*
547 			 * Otherwise, filenames, and partial arguments
548 			 * are simply passed through for gcc to chew on.
549 			 */
550 			newae(h, arg);
551 			continue;
552 		}
553 
554 		if (cplusplus) {
555 			if (strncmp(arg, "-compat=", 8) == 0) {
556 				/* discard -compat=4 and -compat=5 */
557 				continue;
558 			}
559 			if (strcmp(arg, "-Qoption") == 0) {
560 				/* discard -Qoption and its two arguments */
561 				if (argc < 3)
562 					error(arg);
563 				argc -= 2;
564 				argv += 2;
565 				continue;
566 			}
567 			if (strcmp(arg, "-xwe") == 0) {
568 				/* turn warnings into errors */
569 				/* newae(h, "-Werror"); */
570 				continue;
571 			}
572 			if (strcmp(arg, "-noex") == 0) {
573 				/* no exceptions */
574 				newae(h, "-fno-exceptions");
575 				/* no run time type descriptor information */
576 				newae(h, "-fno-rtti");
577 				continue;
578 			}
579 			if (strcmp(arg, "-pic") == 0) {
580 				newae(h, "-fpic");
581 				pic = 1;
582 				continue;
583 			}
584 			if (strcmp(arg, "-PIC") == 0) {
585 				newae(h, "-fPIC");
586 				pic = 1;
587 				continue;
588 			}
589 			if (strcmp(arg, "-norunpath") == 0) {
590 				/* gcc has no corresponding option */
591 				continue;
592 			}
593 			if (strcmp(arg, "-nolib") == 0) {
594 				/* -nodefaultlibs is on by default */
595 				nolibc = 1;
596 				continue;
597 			}
598 #if defined(__sparc)
599 			if (strcmp(arg, "-cg92") == 0) {
600 				xlate(h, "v8", xarch_tbl);
601 				xlate(h, "super", xchip_tbl);
602 				continue;
603 			}
604 #endif	/* __sparc */
605 		}
606 
607 		switch ((c = arg[1])) {
608 		case '_':
609 			if (strcmp(arg, "-_noecho") == 0)
610 				echo = 0;
611 			else if (strncmp(arg, "-_cc=", 5) == 0 ||
612 			    strncmp(arg, "-_CC=", 5) == 0)
613 				/* EMPTY */;
614 			else if (strncmp(arg, "-_gcc=", 6) == 0 ||
615 			    strncmp(arg, "-_g++=", 6) == 0)
616 				newae(h, arg + 6);
617 			else if (strcmp(arg, "-_compiler") == 0) {
618 				(void) printf("%s\n", program);
619 				exit(0);
620 			} else
621 				error(arg);
622 			break;
623 		case '#':
624 			if (arglen == 1) {
625 				newae(h, "-v");
626 				break;
627 			}
628 			error(arg);
629 			break;
630 		case 'g':
631 			newae(h, "-gdwarf-2");
632 			break;
633 		case 'E':
634 			if (arglen == 1) {
635 				newae(h, "-xc");
636 				newae(h, arg);
637 				nolibc = 1;
638 				break;
639 			}
640 			error(arg);
641 			break;
642 		case 'c':
643 		case 'S':
644 			if (arglen == 1)
645 				nolibc = 1;
646 			/* FALLTHROUGH */
647 		case 'C':
648 		case 'H':
649 		case 'p':
650 			if (arglen == 1) {
651 				newae(h, arg);
652 				break;
653 			}
654 			error(arg);
655 			break;
656 		case 'A':
657 		case 'h':
658 		case 'I':
659 		case 'i':
660 		case 'L':
661 		case 'l':
662 		case 'o':
663 		case 'R':
664 		case 'U':
665 		case 'u':
666 		case 'w':
667 			newae(h, arg);
668 			break;
669 		case 'D':
670 			newae(h, arg);
671 			/*
672 			 * XXX	Clearly a hack ... do we need _KADB too?
673 			 */
674 			if (strcmp(arg, "-D_KERNEL") == 0 ||
675 			    strcmp(arg, "-D_BOOT") == 0)
676 				newae(h, "-ffreestanding");
677 			break;
678 		case 'd':
679 			if (arglen == 2) {
680 				if (strcmp(arg, "-dy") == 0) {
681 					newae(h, "-Wl,-dy");
682 					break;
683 				}
684 				if (strcmp(arg, "-dn") == 0) {
685 					newae(h, "-Wl,-dn");
686 					break;
687 				}
688 			}
689 			if (strcmp(arg, "-dalign") == 0) {
690 				/*
691 				 * -dalign forces alignment in some cases;
692 				 * gcc does not need any flag to do this.
693 				 */
694 				break;
695 			}
696 			error(arg);
697 			break;
698 		case 'e':
699 			if (strcmp(arg,
700 			    "-erroff=E_EMPTY_TRANSLATION_UNIT") == 0) {
701 				/*
702 				 * Accept but ignore this -- gcc doesn't
703 				 * seem to complain about empty translation
704 				 * units
705 				 */
706 				break;
707 			}
708 			/* XX64 -- ignore all -erroff= options, for now */
709 			if (strncmp(arg, "-erroff=", 8) == 0)
710 				break;
711 			if (strcmp(arg, "-errtags=yes") == 0) {
712 				warnings(h);
713 				break;
714 			}
715 			if (strcmp(arg, "-errwarn=%all") == 0) {
716 				newae(h, "-Werror");
717 				break;
718 			}
719 			error(arg);
720 			break;
721 		case 'f':
722 			if (strcmp(arg, "-flags") == 0) {
723 				newae(h, "--help");
724 				break;
725 			}
726 			error(arg);
727 			break;
728 		case 'G':
729 			newae(h, "-shared");
730 			nolibc = 1;
731 			break;
732 		case 'k':
733 			if (strcmp(arg, "-keeptmp") == 0) {
734 				newae(h, "-save-temps");
735 				break;
736 			}
737 			error(arg);
738 			break;
739 		case 'K':
740 			if (arglen == 1) {
741 				if ((arg = *++argv) == NULL || *arg == '\0')
742 					error("-K");
743 				argc--;
744 			} else {
745 				arg += 2;
746 			}
747 			if (strcmp(arg, "pic") == 0) {
748 				newae(h, "-fpic");
749 				pic = 1;
750 				break;
751 			}
752 			if (strcmp(arg, "PIC") == 0) {
753 				newae(h, "-fPIC");
754 				pic = 1;
755 				break;
756 			}
757 			error("-K");
758 			break;
759 		case 'm':
760 			if (strcmp(arg, "-mt") == 0) {
761 				newae(h, "-D_REENTRANT");
762 				break;
763 			}
764 			error(arg);
765 			break;
766 		case 'B':	/* linker options */
767 		case 'M':
768 		case 'z':
769 			{
770 				char *opt;
771 				size_t len;
772 				char *s;
773 
774 				if (arglen == 1) {
775 					opt = *++argv;
776 					if (opt == NULL || *opt == '\0')
777 						error(arg);
778 					argc--;
779 				} else {
780 					opt = arg + 2;
781 				}
782 				len = strlen(opt) + 7;
783 				s = malloc(len);
784 				(void) snprintf(s, len, "-Wl,-%c%s", c, opt);
785 				newae(h, s);
786 				free(s);
787 			}
788 			break;
789 		case 'n':
790 			if (strcmp(arg, "-noqueue") == 0) {
791 				/*
792 				 * Horrid license server stuff - n/a
793 				 */
794 				break;
795 			}
796 			error(arg);
797 			break;
798 		case 'O':
799 			if (arglen == 1) {
800 				newae(h, "-O");
801 				break;
802 			}
803 			error(arg);
804 			break;
805 		case 'P':
806 			/*
807 			 * We could do '-E -o filename.i', but that's hard,
808 			 * and we don't need it for the case that's triggering
809 			 * this addition.  We'll require the user to specify
810 			 * -o in the Makefile.  If they don't they'll find out
811 			 * in a hurry.
812 			 */
813 			newae(h, "-E");
814 			nolibc = 1;
815 			break;
816 		case 'q':
817 			if (strcmp(arg, "-qp") == 0) {
818 				newae(h, "-p");
819 				break;
820 			}
821 			error(arg);
822 			break;
823 		case 's':
824 			if (arglen == 1) {
825 				newae(h, "-Wl,-s");
826 				break;
827 			}
828 			error(arg);
829 			break;
830 		case 'V':
831 			if (arglen == 1) {
832 				echo = 0;
833 				newae(h, "--version");
834 				break;
835 			}
836 			error(arg);
837 			break;
838 		case 'v':
839 			if (arglen == 1) {
840 				warnings(h);
841 				break;
842 			}
843 			error(arg);
844 			break;
845 		case 'W':
846 			if (strncmp(arg, "-Wp,-xc99", 9) == 0) {
847 				/*
848 				 * gcc's preprocessor will accept c99
849 				 * regardless, so accept and ignore.
850 				 */
851 				break;
852 			}
853 			if (strncmp(arg, "-Wa,", 4) == 0 ||
854 			    strncmp(arg, "-Wp,", 4) == 0 ||
855 			    strncmp(arg, "-Wl,", 4) == 0) {
856 				newae(h, arg);
857 				break;
858 			}
859 			if (strcmp(arg, "-W0,-xc99=pragma") == 0) {
860 				/* (undocumented) enables _Pragma */
861 				break;
862 			}
863 			if (strcmp(arg, "-W0,-xc99=%none") == 0) {
864 				/*
865 				 * This is a polite way of saying
866 				 * "no c99 constructs allowed!"
867 				 * For now, just accept and ignore this.
868 				 */
869 				break;
870 			}
871 			if (strcmp(arg, "-W0,-noglobal") == 0) {
872 				/*
873 				 * gcc doesn't prefix local symbols
874 				 * in debug mode, so this is not needed.
875 				 */
876 				break;
877 			}
878 			if (strcmp(arg, "-W0,-Lt") == 0) {
879 				/*
880 				 * Generate tests at the top of loops.
881 				 * There is no direct gcc equivalent, ignore.
882 				 */
883 				break;
884 			}
885 			if (strcmp(arg, "-W2,-Rcond_elim") == 0) {
886 				/*
887 				 * Elimination and expansion of conditionals;
888 				 * gcc has no direct equivalent.
889 				 */
890 				break;
891 			}
892 			if (strcmp(arg, "-Wd,-xsafe=unboundsym") == 0) {
893 				/*
894 				 * Prevents optimizing away checks for
895 				 * unbound weak symbol addresses.  gcc does
896 				 * not do this, so it's not needed.
897 				 */
898 				break;
899 			}
900 			if (strncmp(arg, "-Wc,-xcode=", 11) == 0) {
901 				xlate(h, arg + 11, xcode_tbl);
902 				if (strncmp(arg + 11, "pic", 3) == 0)
903 					pic = 1;
904 				break;
905 			}
906 			if (strncmp(arg, "-Wc,-Qiselect", 13) == 0) {
907 				/*
908 				 * Prevents insertion of register symbols.
909 				 * gcc doesn't do this, so ignore it.
910 				 */
911 				break;
912 			}
913 #if defined(__x86)
914 			if (strcmp(arg, "-Wu,-no_got_reloc") == 0) {
915 				/*
916 				 * Don't create any GOT relocations?
917 				 * Well, gcc doesn't have this degree
918 				 * of control over its pic code ...
919 				 */
920 				break;
921 			}
922 			if (strcmp(arg, "-Wu,-xmodel=kernel") == 0) {
923 				newae(h, "-ffreestanding");
924 				newae(h, "-mno-red-zone");
925 				model = "-mcmodel=kernel";
926 				nolibc = 1;
927 				break;
928 			}
929 			if (strcmp(arg, "-Wu,-save_args") == 0) {
930 				newae(h, "-msave-args");
931 				break;
932 			}
933 #endif	/* __x86 */
934 			error(arg);
935 			break;
936 		case 'X':
937 			if (strcmp(arg, "-Xa") == 0 ||
938 			    strcmp(arg, "-Xt") == 0) {
939 				Xamode(h);
940 				break;
941 			}
942 			if (strcmp(arg, "-Xc") == 0) {
943 				Xcmode(h);
944 				break;
945 			}
946 			if (strcmp(arg, "-Xs") == 0) {
947 				Xsmode(h);
948 				break;
949 			}
950 			error(arg);
951 			break;
952 		case 'x':
953 			if (arglen == 1)
954 				error(arg);
955 			switch (arg[2]) {
956 #if defined(__x86)
957 			case '3':
958 				if (strcmp(arg, "-x386") == 0) {
959 					newae(h, "-march=i386");
960 					break;
961 				}
962 				error(arg);
963 				break;
964 			case '4':
965 				if (strcmp(arg, "-x486") == 0) {
966 					newae(h, "-march=i486");
967 					break;
968 				}
969 				error(arg);
970 				break;
971 #endif	/* __x86 */
972 			case 'a':
973 				if (strncmp(arg, "-xarch=", 7) == 0) {
974 					xlate(h, arg + 7, xarch_tbl);
975 					break;
976 				}
977 				error(arg);
978 				break;
979 			case 'C':
980 				/* Accept C++ style comments -- ignore */
981 				if (strcmp(arg, "-xCC") == 0)
982 					break;
983 				error(arg);
984 				break;
985 			case 'c':
986 				if (strncmp(arg, "-xc99=%all", 10) == 0) {
987 					newae(h, "-std=gnu99");
988 					break;
989 				}
990 				if (strncmp(arg, "-xc99=%none", 11) == 0) {
991 					newae(h, "-std=gnu89");
992 					break;
993 				}
994 				if (strncmp(arg, "-xchip=", 7) == 0) {
995 					xlate(h, arg + 7, xchip_tbl);
996 					break;
997 				}
998 				if (strncmp(arg, "-xcode=", 7) == 0) {
999 					xlate(h, arg + 7, xcode_tbl);
1000 					if (strncmp(arg + 7, "pic", 3) == 0)
1001 						pic = 1;
1002 					break;
1003 				}
1004 				if (strncmp(arg, "-xcache=", 8) == 0)
1005 					break;
1006 				error(arg);
1007 				break;
1008 			case 'd':
1009 				if (strcmp(arg, "-xdepend") == 0)
1010 					break;
1011 				error(arg);
1012 				break;
1013 			case 'F':
1014 				/* compile for mapfile reordering -- ignore */
1015 				if (strcmp(arg, "-xF") == 0)
1016 					break;
1017 				error(arg);
1018 				break;
1019 			case 'i':
1020 				if (strncmp(arg, "-xinline", 8) == 0)
1021 					/* No inlining; ignore */
1022 					break;
1023 				if (strcmp(arg, "-xildon") == 0 ||
1024 				    strcmp(arg, "-xildoff") == 0)
1025 					/* No incremental linking; ignore */
1026 					break;
1027 				error(arg);
1028 				break;
1029 			case 'M':
1030 				if (strcmp(arg, "-xM") == 0) {
1031 					newae(h, "-M");
1032 					break;
1033 				}
1034 				if (strcmp(arg, "-xM1") == 0) {
1035 					newae(h, "-MM");
1036 					break;
1037 				}
1038 				error(arg);
1039 				break;
1040 			case 'n':
1041 				if (strcmp(arg, "-xnolib") == 0) {
1042 					nolibc = 1;
1043 					break;
1044 				}
1045 				error(arg);
1046 				break;
1047 			case 'O':
1048 				if (strncmp(arg, "-xO", 3) == 0) {
1049 					size_t len = strlen(arg);
1050 					char *s = malloc(len);
1051 					int c = *(arg + 3);
1052 					int level;
1053 
1054 					if (len != 4 || !isdigit(c))
1055 						error(arg);
1056 
1057 					level = atoi(arg + 3);
1058 					if (level > 5)
1059 						error(arg);
1060 					if (level >= 2) {
1061 						/*
1062 						 * For gcc-3.4.x at -O2 we
1063 						 * need to disable optimizations
1064 						 * that break ON.
1065 						 */
1066 						optim_disable(h, level);
1067 						/*
1068 						 * limit -xO3 to -O2 as well.
1069 						 */
1070 						level = 2;
1071 					}
1072 					(void) snprintf(s, len, "-O%d", level);
1073 					newae(h, s);
1074 					free(s);
1075 					break;
1076 				}
1077 				error(arg);
1078 				break;
1079 			case 'p':
1080 				if (strcmp(arg, "-xpentium") == 0) {
1081 					newae(h, "-march=pentium");
1082 					break;
1083 				}
1084 				if (strcmp(arg, "-xpg") == 0) {
1085 					newae(h, "-pg");
1086 					break;
1087 				}
1088 				error(arg);
1089 				break;
1090 			case 'r':
1091 				if (strncmp(arg, "-xregs=", 7) == 0) {
1092 					xlate(h, arg + 7, xregs_tbl);
1093 					break;
1094 				}
1095 				error(arg);
1096 				break;
1097 			case 's':
1098 				if (strcmp(arg, "-xs") == 0 ||
1099 				    strcmp(arg, "-xspace") == 0 ||
1100 				    strcmp(arg, "-xstrconst") == 0)
1101 					break;
1102 				error(arg);
1103 				break;
1104 			case 't':
1105 				if (strcmp(arg, "-xtransition") == 0) {
1106 					newae(h, "-Wtransition");
1107 					break;
1108 				}
1109 				if (strcmp(arg, "-xtrigraphs=yes") == 0) {
1110 					newae(h, "-trigraphs");
1111 					break;
1112 				}
1113 				if (strcmp(arg, "-xtrigraphs=no") == 0) {
1114 					newae(h, "-notrigraphs");
1115 					break;
1116 				}
1117 				if (strncmp(arg, "-xtarget=", 9) == 0) {
1118 					xlate(h, arg + 9, xtarget_tbl);
1119 					break;
1120 				}
1121 				error(arg);
1122 				break;
1123 			case 'b':
1124 			case 'e':
1125 			case 'h':
1126 			case 'l':
1127 			default:
1128 				error(arg);
1129 				break;
1130 			}
1131 			break;
1132 		case 'Y':
1133 			if (arglen == 1) {
1134 				if ((arg = *++argv) == NULL || *arg == '\0')
1135 					error("-Y");
1136 				argc--;
1137 				arglen = strlen(arg + 1);
1138 			} else {
1139 				arg += 2;
1140 			}
1141 			/* Just ignore -YS,... for now */
1142 			if (strncmp(arg, "S,", 2) == 0)
1143 				break;
1144 			if (strncmp(arg, "l,", 2) == 0) {
1145 				char *s = strdup(arg);
1146 				s[0] = '-';
1147 				s[1] = 'B';
1148 				newae(h, s);
1149 				free(s);
1150 				break;
1151 			}
1152 			if (strncmp(arg, "I,", 2) == 0) {
1153 				char *s = strdup(arg);
1154 				s[0] = '-';
1155 				s[1] = 'I';
1156 				newae(h, "-nostdinc");
1157 				newae(h, s);
1158 				free(s);
1159 				break;
1160 			}
1161 			error(arg);
1162 			break;
1163 		case 'Q':
1164 			/*
1165 			 * We could map -Qy into -Wl,-Qy etc.
1166 			 */
1167 		default:
1168 			error(arg);
1169 			break;
1170 		}
1171 	}
1172 
1173 	if (model && !pic)
1174 		newae(h, model);
1175 	if (!nolibc)
1176 		newae(h, "-lc");
1177 }
1178 
1179 /* ARGSUSED4 */
1180 static void
1181 do_cc(const char *dir, const char *cmd, int argc, char **argv,
1182     struct aelist *h, int cplusplus)
1183 {
1184 	char *program;
1185 	size_t len = strlen(dir) + strlen(cmd) + 2;
1186 
1187 	program = malloc(len);
1188 	(void) snprintf(program, len, "%s/%s", dir, cmd);
1189 
1190 	/*
1191 	 * This is pretty simple.
1192 	 * We just have to recognize -V, -_noecho, -_compiler, -_cc= and -_gcc=
1193 	 */
1194 	newae(h, program);
1195 
1196 	while (--argc > 0) {
1197 		char *arg = *++argv;
1198 
1199 		if (*arg != '-') {
1200 			newae(h, arg);
1201 		} else if (*(arg + 1) != '_') {
1202 			if (strcmp(arg, "-V") == 0)
1203 				echo = 0;
1204 			newae(h, arg);
1205 		} else if (strcmp(arg, "-_noecho") == 0) {
1206 			echo = 0;
1207 		} else if (strcmp(arg, "-_compiler") == 0) {
1208 			(void) printf("%s\n", program);
1209 			exit(0);
1210 		} else if (strncmp(arg, "-_cc=", 5) == 0 ||
1211 		    strncmp(arg, "-_CC=", 5) == 0) {
1212 			newae(h, arg + 5);
1213 		} else if (strncmp(arg, "-_gcc=", 6) != 0 &&
1214 		    strncmp(arg, "-_g++=", 6) != 0) {
1215 			(void) fprintf(stderr,
1216 			    "%s: invalid argument '%s'\n", progname, arg);
1217 			exit(2);
1218 		}
1219 	}
1220 }
1221 
1222 int
1223 main(int argc, char **argv)
1224 {
1225 	struct aelist *h = newael();
1226 	const char *dir;
1227 	int ac;
1228 	char **newargv;
1229 	struct ae *a;
1230 	char cc_buf[MAXPATHLEN], gcc_buf[MAXPATHLEN];
1231 
1232 	if ((progname = strrchr(argv[0], '/')) == NULL)
1233 		progname = argv[0];
1234 	else
1235 		progname++;
1236 
1237 	default_cc_dir = DEFAULT_CC_DIR;
1238 	default_gcc_dir = DEFAULT_GCC_DIR;
1239 	default_cplusplus_dir = DEFAULT_CPLUSPLUS_DIR;
1240 	default_gplusplus_dir = DEFAULT_GPLUSPLUS_DIR;
1241 
1242 	/*
1243 	 * Figure out where to get our tools from.  This depends on
1244 	 * the environment variables set at run time.
1245 	 */
1246 	if ((dir = getenv("SPRO_VROOT")) != NULL) {
1247 		(void) snprintf(cc_buf, MAXPATHLEN, "%s/bin", dir);
1248 	} else if ((dir = getenv("SPRO_ROOT")) != NULL) {
1249 		(void) snprintf(cc_buf, MAXPATHLEN, "%s/SOS8/bin", dir);
1250 	} else if ((dir = getenv("BUILD_TOOLS")) != NULL) {
1251 		(void) snprintf(cc_buf, MAXPATHLEN,
1252 		    "%s/SUNWspro/SOS8/bin", dir);
1253 	}
1254 	if (dir != NULL)
1255 		default_cc_dir = (char *)cc_buf;
1256 
1257 	if ((dir = getenv("GNU_ROOT")) != NULL) {
1258 		(void) snprintf(gcc_buf, MAXPATHLEN, "%s/bin", dir);
1259 		default_gcc_dir = (char *)gcc_buf;
1260 	}
1261 
1262 	default_cplusplus_dir = default_cc_dir;
1263 	default_gplusplus_dir = default_gcc_dir;
1264 
1265 	/*
1266 	 * The first argument must be one of "-_cc", "-_gcc", "-_CC", or "-_g++"
1267 	 */
1268 	if (argc == 1)
1269 		usage();
1270 	argc--;
1271 	argv++;
1272 	if (strcmp(argv[0], "-_cc") == 0) {
1273 		if ((dir = getenv("CW_CC_DIR")) == NULL)
1274 			dir = default_cc_dir;
1275 		do_cc(dir, "cc", argc, argv, h, 0);
1276 	} else if (strcmp(argv[0], "-_gcc") == 0) {
1277 		if ((dir = getenv("CW_GCC_DIR")) == NULL)
1278 			dir = default_gcc_dir;
1279 		do_gcc(dir, "gcc", argc, argv, h, 0);
1280 	} else if (strcmp(argv[0], "-_CC") == 0) {
1281 		if ((dir = getenv("CW_CPLUSPLUS_DIR")) == NULL)
1282 			dir = default_cplusplus_dir;
1283 		do_cc(dir, "CC", argc, argv, h, 1);
1284 	} else if (strcmp(argv[0], "-_g++") == 0) {
1285 		if ((dir = getenv("CW_GPLUSPLUS_DIR")) == NULL)
1286 			dir = default_gplusplus_dir;
1287 		do_gcc(dir, "g++", argc, argv, h, 1);
1288 	} else {
1289 		/* assume "-_gcc" by default */
1290 		argc++;
1291 		argv--;
1292 		if ((dir = getenv("CW_GCC_DIR")) == NULL)
1293 			dir = default_gcc_dir;
1294 		do_gcc(dir, "gcc", argc, argv, h, 0);
1295 	}
1296 
1297 	newargv = calloc(sizeof (*newargv), newargc + 1);
1298 
1299 	if (echo)
1300 		(void) printf("+ ");
1301 
1302 	for (ac = 0, a = h->ael_head; a; a = a->ae_next, ac++) {
1303 		newargv[ac] = a->ae_arg;
1304 		if (echo)
1305 			(void) printf("%s ", a->ae_arg);
1306 		if (a == h->ael_tail)
1307 			break;
1308 	}
1309 
1310 	if (echo) {
1311 		(void) printf("\n");
1312 		(void) fflush(stdout);
1313 	}
1314 
1315 	/*
1316 	 * Here goes ..
1317 	 */
1318 	(void) execvp(newargv[0], newargv);
1319 
1320 	/*
1321 	 * execvp() returns only on error.
1322 	 */
1323 	perror("execvp");
1324 	(void) fprintf(stderr, "%s: couldn't run %s\n",
1325 	    progname, newargv[0]);
1326 	return (4);
1327 }
1328