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