summaryrefslogtreecommitdiff
path: root/usr/src/tools/cw/cw.c
blob: 218d2f9b9b55376a6b6d2776fdfae3520da21993 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Wrapper for the GNU C compiler to make it accept the Sun C compiler
 * arguments where possible.
 *
 * Since the translation is inexact, this is something of a work-in-progress.
 */

/*
 * -#		Verbose mode
 * -###		Show compiler commands built by driver, no compilation
 * -A<name[(tokens)]>	Preprocessor predicate assertion
 * -B<[static|dynamic]>	Specify dynamic or static binding
 * -C		Prevent preprocessor from removing comments
 * -c		Compile only - produce .o files, suppress linking
 * -cg92	Alias for -xtarget=ss1000
 * -D<name[=token]>	Associate name with token as if by #define
 * -d[y|n]	dynamic [-dy] or static [-dn] option to linker
 * -E		Compile source through preprocessor only, output to stdout
 * -erroff=<t>	Suppress warnings specified by tags t(%none, %all, <tag list>)
 * -errtags=<a>	Display messages with tags a(no, yes)
 * -errwarn=<t>	Treats warnings specified by tags t(%none, %all, <tag list>)
 *		as errors
 * -fast	Optimize using a selection of options
 * -fd		Report old-style function definitions and declarations
 * -flags	Show this summary of compiler options
 * -fnonstd	Initialize floating-point hardware to non-standard preferences
 * -fns[=<yes|no>] Select non-standard floating point mode
 * -fprecision=<p> Set FP rounding precision mode p(single, double, extended)
 * -fround=<r>	Select the IEEE rounding mode in effect at startup
 * -fsimple[=<n>] Select floating-point optimization preferences <n>
 * -fsingle	Use single-precision arithmetic (-Xt and -Xs modes only)
 * -ftrap=<t>	Select floating-point trapping mode in effect at startup
 * -fstore	force floating pt. values to target precision on assignment
 * -G		Build a dynamic shared library
 * -g		Compile for debugging
 * -H		Print path name of each file included during compilation
 * -h <name>	Assign <name> to generated dynamic shared library
 * -I<dir>	Add <dir> to preprocessor #include file search path
 * -i		Passed to linker to ignore any LD_LIBRARY_PATH setting
 * -keeptmp	Keep temporary files created during compilation
 * -KPIC	Compile position independent code with 32-bit addresses
 * -Kpic	Compile position independent code
 * -L<dir>	Pass to linker to add <dir> to the library search path
 * -l<name>	Link with library lib<name>.a or lib<name>.so
 * -mc		Remove duplicate strings from .comment section of output files
 * -mr		Remove all strings from .comment section of output files
 * -mr,"string"	Remove all strings and append "string" to .comment section
 * -mt		Specify options needed when compiling multi-threaded code
 * -native	Find available processor, generate code accordingly
 * -nofstore	Do not force floating pt. values to target precision
 *		on assignment
 * -nolib	Same as -xnolib
 * -noqueue	Disable queuing of compiler license requests
 * -norunpath	Do not build in a runtime path for shared libraries
 * -O		Use default optimization level (-xO2 or -xO3. Check man page.)
 * -o <outputfile> Set name of output file to <outputfile>
 * -P		Compile source through preprocessor only, output to .i  file
 * -PIC		Alias for -KPIC or -xcode=pic32
 * -p		Compile for profiling with prof
 * -pic		Alias for -Kpic or -xcode=pic13
 * -Q[y|n]	Emit/don't emit identification info to output file
 * -qp		Compile for profiling with prof
 * -R<dir[:dir]> Build runtime search path list into executable
 * -S		Compile and only generate assembly code (.s)
 * -s		Strip symbol table from the executable file
 * -t		Turn off duplicate symbol warnings when linking
 * -U<name>	Delete initial definition of preprocessor symbol <name>
 * -V		Report version number of each compilation phase
 * -v		Do stricter semantic checking
 * -W<c>,<arg>	Pass <arg> to specified component <c> (a,l,m,p,0,2,h,i,u)
 * -w		Suppress compiler warning messages
 * -Xa		Compile assuming ANSI C conformance, allow K & R extensions
 *		(default mode)
 * -Xc		Compile assuming strict ANSI C conformance
 * -Xs		Compile assuming (pre-ANSI) K & R C style code
 * -Xt		Compile assuming K & R conformance, allow ANSI C
 * -x386	Generate code for the 80386 processor
 * -x486	Generate code for the 80486 processor
 * -xarch=<a>	Specify target architecture instruction set
 * -xbuiltin[=<b>] When profitable inline, or substitute intrinisic functions
 *		for system functions, b={%all,%none}
 * -xCC		Accept C++ style comments
 * -xchar_byte_order=<o> Specify multi-char byte order <o> (default, high, low)
 * -xchip=<c>	Specify the target processor for use by the optimizer
 * -xcode=<c>	Generate different code for forming addresses
 * -xcrossfile[=<n>] Enable optimization and inlining across source files,
 *		n={0|1}
 * -xe		Perform only syntax/semantic checking, no code generation
 * -xF		Compile for later mapfile reordering
 * -xhelp=<f>	Display on-line help information f(flags, readme, errors)
 * -xildoff	Cancel -xildon
 * -xildon	Enable use of the incremental linker, ild
 * -xinline=[<a>,...,<a>]  Attempt inlining of specified user routines,
 *		<a>={%auto,func,no%func}
 * -xlibmieee	Force IEEE 754 return values for math routines in
 *		exceptional cases
 * -xlibmil	Inline selected libm math routines for optimization
 * -xlic_lib=sunperf	Link in the Sun supplied performance libraries
 * -xlicinfo	Show license server information
 * -xM		Generate makefile dependencies
 * -xM1		Generate makefile dependencies, but exclude /usr/include
 * -xmaxopt=[off,1,2,3,4,5] maximum optimization level allowed on #pragma opt
 * -xnolib	Do not link with default system libraries
 * -xnolibmil	Cancel -xlibmil on command line
 * -xO<n>	Generate optimized code (n={1|2|3|4|5})
 * -xP		Print prototypes for function definitions
 * -xpentium	Generate code for the pentium processor
 * -xpg		Compile for profiling with gprof
 * -xprofile=<p> Collect data for a profile or use a profile to optimize
 *		<p>={{collect,use}[:<path>],tcov}
 * -xregs=<r>	Control register allocation
 * -xs		Allow debugging without object (.o) files
 * -xsb		Compile for use with the WorkShop source browser
 * -xsbfast	Generate only WorkShop source browser info, no compilation
 * -xsfpconst	Represent unsuffixed floating point constants as single
 *		precision
 * -xspace	Do not do optimizations that increase code size
 * -xstrconst	Place string literals into read-only data segment
 * -xtarget=<t>	Specify target system for optimization
 * -xtemp=<dir>	Set directory for temporary files to <dir>
 * -xtime	Report the execution time for each compilation phase
 * -xtransition	Emit warnings for differences between K&R C and ANSI C
 * -xtrigraphs[=<yes|no>] Enable|disable trigraph translation
 * -xunroll=n	Enable unrolling loops n times where possible
 * -Y<c>,<dir>	Specify <dir> for location of component <c> (a,l,m,p,0,h,i,u)
 * -YA,<dir>	Change default directory searched for components
 * -YI,<dir>	Change default directory searched for include files
 * -YP,<dir>	Change default directory for finding libraries files
 * -YS,<dir>	Change default directory for startup object files
 */

/*
 * Translation table:
 */
/*
 * -#				-v
 * -###				error
 * -A<name[(tokens)]>		pass-thru
 * -B<[static|dynamic]>		pass-thru (syntax error for anything else)
 * -C				pass-thru
 * -c				pass-thru
 * -cg92			-m32 -mcpu=v8 -mtune=supersparc (SPARC only)
 * -D<name[=token]>		pass-thru
 * -dy or -dn			-Wl,-dy or -Wl,-dn
 * -E				pass-thru
 * -erroff=E_EMPTY_TRANSLATION_UNIT ignore
 * -errtags=%all		-Wall
 * -errwarn=%all		-Werror else -Wno-error
 * -fast			error
 * -fd				error
 * -flags			--help
 * -fnonstd			error
 * -fns[=<yes|no>]		error
 * -fprecision=<p>		error
 * -fround=<r>			error
 * -fsimple[=<n>]		error
 * -fsingle[=<n>]		error
 * -ftrap=<t>			error
 * -fstore			error
 * -G				pass-thru
 * -g				pass-thru
 * -H				pass-thru
 * -h <name>			pass-thru
 * -I<dir>			pass-thru
 * -i				pass-thru
 * -keeptmp			-save-temps
 * -KPIC			-fPIC
 * -Kpic			-fpic
 * -L<dir>			pass-thru
 * -l<name>			pass-thru
 * -mc				error
 * -mr				error
 * -mr,"string"			error
 * -mt				-D_REENTRANT
 * -native			error
 * -nofstore			error
 * -nolib			-nodefaultlibs
 * -noqueue			ignore
 * -norunpath			ignore
 * -O				-O1 (Check the man page to be certain)
 * -o <outputfile>		pass-thru
 * -P				-E -o filename.i (or error)
 * -PIC				-fPIC (C++ only)
 * -p				pass-thru
 * -pic				-fpic (C++ only)
 * -Q[y|n]			error
 * -qp				-p
 * -R<dir[:dir]>		pass-thru
 * -S				pass-thru
 * -s				-Wl,-s
 * -t				-Wl,-t
 * -U<name>			pass-thru
 * -V				--version
 * -v				-Wall
 * -Wa,<arg>			pass-thru
 * -Wp,<arg>			pass-thru except -xc99=<a>
 * -Wl,<arg>			pass-thru
 * -W{m,0,2,h,i,u>		error/ignore
 * -Wu,-xmodel=kernel		-ffreestanding -mcmodel=kernel -mno-red-zone
 * -Wu,-save_args		-msave-args
 * -w				pass-thru
 * -Xa				-std=iso9899:199409 or -ansi
 * -Xc				-ansi -pedantic
 * -Xt				error
 * -Xs				-traditional -std=c89
 * -x386			-march=i386 (x86 only)
 * -x486			-march=i486 (x86 only)
 * -xarch=<a>			table
 * -xbuiltin[=<b>]		-fbuiltin (-fno-builtin otherwise)
 * -xCC				ignore
 * -xchar_byte_order=<o>	error
 * -xchip=<c>			table
 * -xcode=<c>			table
 * -xdebugformat=<format>	ignore (always use dwarf-2 for gcc)
 * -xcrossfile[=<n>]		ignore
 * -xe				error
 * -xF				error
 * -xhelp=<f>			error
 * -xildoff			ignore
 * -xildon			ignore
 * -xinline			ignore
 * -xlibmieee			error
 * -xlibmil			error
 * -xlic_lib=sunperf		error
 * -xM				-M
 * -xM1				-MM
 * -xmaxopt=[...]		error
 * -xnolib			-nodefaultlibs
 * -xnolibmil			error
 * -xO<n>			-O<n>
 * -xP				error
 * -xpentium			-march=pentium (x86 only)
 * -xpg				error
 * -xprofile=<p>		error
 * -xregs=<r>			table
 * -xs				error
 * -xsb				error
 * -xsbfast			error
 * -xsfpconst			error
 * -xspace			ignore (-not -Os)
 * -xstrconst			ignore
 * -xtarget=<t>			table
 * -xtemp=<dir>			error
 * -xtime			error
 * -xtransition			-Wtransition
 * -xtrigraphs=<yes|no>		-trigraphs -notrigraphs
 * -xunroll=n			error
 * -W0,-xdbggen=no%usedonly	-fno-eliminate-unused-debug-symbols
 *				-fno-eliminate-unused-debug-types
 * -Y<c>,<dir>			error
 * -YA,<dir>			error
 * -YI,<dir>			-nostdinc -I<dir>
 * -YP,<dir>			error
 * -YS,<dir>			error
 */

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/utsname.h>
#include <sys/param.h>
#include <sys/isa_defs.h>

static int echo = 1;
static int newargc;
static const char *progname;

static char *default_cc_dir;
static char *default_gcc_dir;

static char *default_cplusplus_dir;
static char *default_gplusplus_dir;

static const char *xarch_tbl[] = {
#if defined(__x86)
	"generic",	NULL,
	"generic64",	"-m64", "-mtune=opteron", NULL,
	"amd64",	"-m64", "-mtune=opteron", NULL,
	"386",		"-march=i386", NULL,
	"pentium_pro",	"-march=pentiumpro", NULL,
#elif defined(__sparc)
	"generic",	"-m32", "-mcpu=v8", NULL,
	"generic64",	"-m64", "-mcpu=v9", NULL,
	"v8",		"-m32", "-mcpu=v8", "-mno-v8plus", NULL,
	"v8plus",	"-m32", "-mcpu=v9", "-mv8plus", NULL,
	"v8plusa",	"-m32", "-mcpu=ultrasparc", "-mv8plus", "-mvis", NULL,
	"v8plusb",	"-m32", "-mcpu=ultrasparc3", "-mv8plus", "-mvis", NULL,
	"v9",		"-m64", "-mcpu=v9", NULL,
	"v9a",		"-m64", "-mcpu=ultrasparc", "-mvis", NULL,
	"v9b",		"-m64", "-mcpu=ultrasparc3", "-mvis", NULL,
#endif
	NULL,		NULL
};

static const char *xchip_tbl[] = {
#if defined(__x86)
	"386",		"-mtune=i386", NULL,
	"486",		"-mtune=i486", NULL,
	"pentium",	"-mtune=pentium", NULL,
	"pentium_pro",  "-mtune=pentiumpro", NULL,
#elif defined(__sparc)
	"super",	"-mtune=supersparc", NULL,
	"ultra",	"-mtune=ultrasparc", NULL,
	"ultra3",	"-mtune=ultrasparc3", NULL,
#endif
	NULL,		NULL
};

static const char *xcode_tbl[] = {
#if defined(__sparc)
	"abs32",	"-fno-pic", "-mcmodel=medlow", NULL,
	"abs44",	"-fno-pic", "-mcmodel=medmid", NULL,
	"abs64",	"-fno-pic", "-mcmodel=medany", NULL,
	"pic13",	"-fpic", NULL,
	"pic32",	"-fPIC", NULL,
#endif
	NULL,		NULL
};

static const char *xtarget_tbl[] = {
#if defined(__x86)
	"pentium_pro",	"-march=pentiumpro", NULL,
#endif	/* __x86 */
	NULL,		NULL
};

static const char *xregs_tbl[] = {
#if defined(__sparc)
	"appl",		"-mapp-regs", NULL,
	"no%appl",	"-mno-app-regs", NULL,
	"float",	"-mfpu", NULL,
	"no%float",	"-mno-fpu", NULL,
#endif	/* __sparc */
	NULL,		NULL
};

struct aelist {
	struct ae {
		struct ae *ae_next;
		char *ae_arg;
	} *ael_head, *ael_tail;
};

static struct aelist *
newael(void)
{
	return (calloc(sizeof (struct aelist), 1));
}

static void
newae(struct aelist *ael, const char *arg)
{
	struct ae *ae;

	ae = calloc(sizeof (*ae), 1);
	ae->ae_arg = strdup(arg);
	if (ael->ael_tail == NULL)
		ael->ael_head = ae;
	else
		ael->ael_tail->ae_next = ae;
	ael->ael_tail = ae;
	newargc++;
}

static void
error(const char *arg)
{
	(void) fprintf(stderr,
	    "%s: mapping failed at or near arg '%s'\n", progname, arg);
	exit(2);
}

/*
 * Add the current favourite set of warnings to the gcc invocation.
 */
static void
warnings(struct aelist *h)
{
	static int warningsonce;

	if (warningsonce++)
		return;

	newae(h, "-Wall");
	newae(h, "-Wno-unknown-pragmas");
	newae(h, "-Wno-missing-braces");
	newae(h, "-Wno-sign-compare");
	newae(h, "-Wno-parentheses");
	newae(h, "-Wno-uninitialized");
	newae(h, "-Wno-implicit-function-declaration");
	newae(h, "-Wno-unused");
	newae(h, "-Wno-trigraphs");
	newae(h, "-Wno-char-subscripts");
	newae(h, "-Wno-switch");
}

static void
optim_disable(struct aelist *h, int level)
{
	if (level >= 2) {
		newae(h, "-fno-strict-aliasing");
		newae(h, "-fno-unit-at-a-time");
		newae(h, "-fno-optimize-sibling-calls");
	}
}

/* ARGSUSED */
static void
Xamode(struct aelist *h)
{
}

static void
Xcmode(struct aelist *h)
{
	static int xconce;

	if (xconce++)
		return;

	newae(h, "-ansi");
	newae(h, "-pedantic-errors");
}

static void
Xsmode(struct aelist *h)
{
	static int xsonce;

	if (xsonce++)
		return;

	newae(h, "-traditional");
	newae(h, "-traditional-cpp");
}

static void
usage()
{
	(void) fprintf(stderr,
	    "usage: %s { -_cc | -_gcc | -_CC | -_g++ } [ -_compiler | ... ]\n",
	    progname);
	exit(2);
}

static void
xlate(struct aelist *h, const char *xarg, const char **table)
{
	while (*table != NULL && strcmp(xarg, *table) != 0) {
		while (*table != NULL)
			table++;
		table++;
	}

	if (*table == NULL)
		error(xarg);

	table++;

	while (*table != NULL) {
		newae(h, *table);
		table++;
	}
}

static void
do_gcc(const char *dir, const char *cmd, int argc, char **argv,
    struct aelist *h, int cplusplus)
{
	int c;
	int pic = 0;
	int nolibc = 0;
	char *model = NULL;
	char *program;
	size_t len = strlen(dir) + strlen(cmd) + 2;

	program = malloc(len);
	(void) snprintf(program, len, "%s/%s", dir, cmd);

	/*
	 * Basic defaults for ON compilation
	 */
	newae(h, program);

	newae(h, "-fident");
	newae(h, "-finline");
	newae(h, "-fno-inline-functions");
	newae(h, "-fno-builtin");
	newae(h, "-fno-asm");
	newae(h, "-nodefaultlibs");

#if defined(__sparc)
	/*
	 * The SPARC ldd and std instructions require 8-byte alignment of
	 * their address operand.  gcc correctly uses them only when the
	 * ABI requires 8-byte alignment; unfortunately we have a number of
	 * pieces of buggy code that doesn't conform to the ABI.  This
	 * flag makes gcc work more like Studio with -xmemalign=4.
	 */
	newae(h, "-mno-integer-ldd-std");
#endif

	/*
	 * This is needed because 'u' is defined
	 * under a conditional on 'sun'.  Should
	 * probably just remove the conditional,
	 * or make it be dependent on '__sun'.
	 *
	 * -Dunix is also missing in enhanced ANSI mode
	 */
	newae(h, "-D__sun");

	/*
	 * Walk the argument list, translating as we go ..
	 */

	while (--argc > 0) {
		char *arg = *++argv;
		size_t arglen = strlen(arg);

		if (*arg == '-')
			arglen--;
		else {
			/*
			 * Discard inline files that gcc doesn't grok
			 */
			if (arglen > 3 &&
			    strcmp(arg + arglen - 3, ".il") == 0)
				continue;

			/*
			 * Otherwise, filenames, and partial arguments
			 * are simply passed through for gcc to chew on.
			 */
			newae(h, arg);
			continue;
		}

		if (cplusplus) {
			if (strncmp(arg, "-compat=", 8) == 0) {
				/* discard -compat=4 and -compat=5 */
				continue;
			}
			if (strcmp(arg, "-Qoption") == 0) {
				/* discard -Qoption and its two arguments */
				if (argc < 3)
					error(arg);
				argc -= 2;
				argv += 2;
				continue;
			}
			if (strcmp(arg, "-xwe") == 0) {
				/* turn warnings into errors */
				/* newae(h, "-Werror"); */
				continue;
			}
			if (strcmp(arg, "-noex") == 0) {
				/* no exceptions */
				newae(h, "-fno-exceptions");
				/* no run time type descriptor information */
				newae(h, "-fno-rtti");
				continue;
			}
			if (strcmp(arg, "-pic") == 0) {
				newae(h, "-fpic");
				pic = 1;
				continue;
			}
			if (strcmp(arg, "-PIC") == 0) {
				newae(h, "-fPIC");
				pic = 1;
				continue;
			}
			if (strcmp(arg, "-norunpath") == 0) {
				/* gcc has no corresponding option */
				continue;
			}
			if (strcmp(arg, "-nolib") == 0) {
				/* -nodefaultlibs is on by default */
				nolibc = 1;
				continue;
			}
#if defined(__sparc)
			if (strcmp(arg, "-cg92") == 0) {
				xlate(h, "v8", xarch_tbl);
				xlate(h, "super", xchip_tbl);
				continue;
			}
#endif	/* __sparc */
		}

		switch ((c = arg[1])) {
		case '_':
			if (strcmp(arg, "-_noecho") == 0)
				echo = 0;
			else if (strncmp(arg, "-_cc=", 5) == 0 ||
			    strncmp(arg, "-_CC=", 5) == 0)
				/* EMPTY */;
			else if (strncmp(arg, "-_gcc=", 6) == 0 ||
			    strncmp(arg, "-_g++=", 6) == 0)
				newae(h, arg + 6);
			else if (strcmp(arg, "-_compiler") == 0) {
				(void) printf("%s\n", program);
				exit(0);
			} else
				error(arg);
			break;
		case '#':
			if (arglen == 1) {
				newae(h, "-v");
				break;
			}
			error(arg);
			break;
		case 'g':
			newae(h, "-gdwarf-2");
			break;
		case 'E':
			if (arglen == 1) {
				newae(h, "-xc");
				newae(h, arg);
				nolibc = 1;
				break;
			}
			error(arg);
			break;
		case 'c':
		case 'S':
			if (arglen == 1)
				nolibc = 1;
			/* FALLTHROUGH */
		case 'C':
		case 'H':
		case 'p':
			if (arglen == 1) {
				newae(h, arg);
				break;
			}
			error(arg);
			break;
		case 'A':
		case 'h':
		case 'I':
		case 'i':
		case 'L':
		case 'l':
		case 'o':
		case 'R':
		case 'U':
		case 'u':
		case 'w':
			newae(h, arg);
			break;
		case 'D':
			newae(h, arg);
			/*
			 * XXX	Clearly a hack ... do we need _KADB too?
			 */
			if (strcmp(arg, "-D_KERNEL") == 0 ||
			    strcmp(arg, "-D_BOOT") == 0)
				newae(h, "-ffreestanding");
			break;
		case 'd':
			if (arglen == 2) {
				if (strcmp(arg, "-dy") == 0) {
					newae(h, "-Wl,-dy");
					break;
				}
				if (strcmp(arg, "-dn") == 0) {
					newae(h, "-Wl,-dn");
					break;
				}
			}
			if (strcmp(arg, "-dalign") == 0) {
				/*
				 * -dalign forces alignment in some cases;
				 * gcc does not need any flag to do this.
				 */
				break;
			}
			error(arg);
			break;
		case 'e':
			if (strcmp(arg,
			    "-erroff=E_EMPTY_TRANSLATION_UNIT") == 0) {
				/*
				 * Accept but ignore this -- gcc doesn't
				 * seem to complain about empty translation
				 * units
				 */
				break;
			}
			/* XX64 -- ignore all -erroff= options, for now */
			if (strncmp(arg, "-erroff=", 8) == 0)
				break;
			if (strcmp(arg, "-errtags=yes") == 0) {
				warnings(h);
				break;
			}
			if (strcmp(arg, "-errwarn=%all") == 0) {
				newae(h, "-Werror");
				break;
			}
			error(arg);
			break;
		case 'f':
			if (strcmp(arg, "-flags") == 0) {
				newae(h, "--help");
				break;
			}
			error(arg);
			break;
		case 'G':
			newae(h, "-shared");
			nolibc = 1;
			break;
		case 'k':
			if (strcmp(arg, "-keeptmp") == 0) {
				newae(h, "-save-temps");
				break;
			}
			error(arg);
			break;
		case 'K':
			if (arglen == 1) {
				if ((arg = *++argv) == NULL || *arg == '\0')
					error("-K");
				argc--;
			} else {
				arg += 2;
			}
			if (strcmp(arg, "pic") == 0) {
				newae(h, "-fpic");
				pic = 1;
				break;
			}
			if (strcmp(arg, "PIC") == 0) {
				newae(h, "-fPIC");
				pic = 1;
				break;
			}
			error("-K");
			break;
		case 'm':
			if (strcmp(arg, "-mt") == 0) {
				newae(h, "-D_REENTRANT");
				break;
			}
			error(arg);
			break;
		case 'B':	/* linker options */
		case 'M':
		case 'z':
			{
				char *opt;
				size_t len;
				char *s;

				if (arglen == 1) {
					opt = *++argv;
					if (opt == NULL || *opt == '\0')
						error(arg);
					argc--;
				} else {
					opt = arg + 2;
				}
				len = strlen(opt) + 7;
				s = malloc(len);
				(void) snprintf(s, len, "-Wl,-%c%s", c, opt);
				newae(h, s);
				free(s);
			}
			break;
		case 'n':
			if (strcmp(arg, "-noqueue") == 0) {
				/*
				 * Horrid license server stuff - n/a
				 */
				break;
			}
			error(arg);
			break;
		case 'O':
			if (arglen == 1) {
				newae(h, "-O");
				break;
			}
			error(arg);
			break;
		case 'P':
			/*
			 * We could do '-E -o filename.i', but that's hard,
			 * and we don't need it for the case that's triggering
			 * this addition.  We'll require the user to specify
			 * -o in the Makefile.  If they don't they'll find out
			 * in a hurry.
			 */
			newae(h, "-E");
			nolibc = 1;
			break;
		case 'q':
			if (strcmp(arg, "-qp") == 0) {
				newae(h, "-p");
				break;
			}
			error(arg);
			break;
		case 's':
			if (arglen == 1) {
				newae(h, "-Wl,-s");
				break;
			}
			error(arg);
			break;
		case 't':
			if (arglen == 1) {
				newae(h, "-Wl,-t");
				break;
			}
			error(arg);
			break;
		case 'V':
			if (arglen == 1) {
				echo = 0;
				newae(h, "--version");
				break;
			}
			error(arg);
			break;
		case 'v':
			if (arglen == 1) {
				warnings(h);
				break;
			}
			error(arg);
			break;
		case 'W':
			if (strncmp(arg, "-Wp,-xc99", 9) == 0) {
				/*
				 * gcc's preprocessor will accept c99
				 * regardless, so accept and ignore.
				 */
				break;
			}
			if (strncmp(arg, "-Wa,", 4) == 0 ||
			    strncmp(arg, "-Wp,", 4) == 0 ||
			    strncmp(arg, "-Wl,", 4) == 0) {
				newae(h, arg);
				break;
			}
			if (strcmp(arg, "-W0,-xc99=pragma") == 0) {
				/* (undocumented) enables _Pragma */
				break;
			}
			if (strcmp(arg, "-W0,-xc99=%none") == 0) {
				/*
				 * This is a polite way of saying
				 * "no c99 constructs allowed!"
				 * For now, just accept and ignore this.
				 */
				break;
			}
			if (strcmp(arg, "-W0,-noglobal") == 0) {
				/*
				 * gcc doesn't prefix local symbols
				 * in debug mode, so this is not needed.
				 */
				break;
			}
			if (strcmp(arg, "-W0,-Lt") == 0) {
				/*
				 * Generate tests at the top of loops.
				 * There is no direct gcc equivalent, ignore.
				 */
				break;
			}
			if (strcmp(arg, "-W0,-xdbggen=no%usedonly") == 0) {
				newae(h, "-fno-eliminate-unused-debug-symbols");
				newae(h, "-fno-eliminate-unused-debug-types");
				break;
			}
			if (strcmp(arg, "-W2,-Rcond_elim") == 0) {
				/*
				 * Elimination and expansion of conditionals;
				 * gcc has no direct equivalent.
				 */
				break;
			}
			if (strcmp(arg, "-Wd,-xsafe=unboundsym") == 0) {
				/*
				 * Prevents optimizing away checks for
				 * unbound weak symbol addresses.  gcc does
				 * not do this, so it's not needed.
				 */
				break;
			}
			if (strncmp(arg, "-Wc,-xcode=", 11) == 0) {
				xlate(h, arg + 11, xcode_tbl);
				if (strncmp(arg + 11, "pic", 3) == 0)
					pic = 1;
				break;
			}
			if (strncmp(arg, "-Wc,-Qiselect", 13) == 0) {
				/*
				 * Prevents insertion of register symbols.
				 * gcc doesn't do this, so ignore it.
				 */
				break;
			}
#if defined(__x86)
			if (strcmp(arg, "-Wu,-no_got_reloc") == 0) {
				newae(h, "-fno-jump-tables");
				newae(h, "-fno-constant-pools");
				break;
			}
			if (strcmp(arg, "-Wu,-xmodel=kernel") == 0) {
				newae(h, "-ffreestanding");
				newae(h, "-mno-red-zone");
				model = "-mcmodel=kernel";
				nolibc = 1;
				break;
			}
			if (strcmp(arg, "-Wu,-save_args") == 0) {
				newae(h, "-msave-args");
				break;
			}
#endif	/* __x86 */
			error(arg);
			break;
		case 'X':
			if (strcmp(arg, "-Xa") == 0 ||
			    strcmp(arg, "-Xt") == 0) {
				Xamode(h);
				break;
			}
			if (strcmp(arg, "-Xc") == 0) {
				Xcmode(h);
				break;
			}
			if (strcmp(arg, "-Xs") == 0) {
				Xsmode(h);
				break;
			}
			error(arg);
			break;
		case 'x':
			if (arglen == 1)
				error(arg);
			switch (arg[2]) {
#if defined(__x86)
			case '3':
				if (strcmp(arg, "-x386") == 0) {
					newae(h, "-march=i386");
					break;
				}
				error(arg);
				break;
			case '4':
				if (strcmp(arg, "-x486") == 0) {
					newae(h, "-march=i486");
					break;
				}
				error(arg);
				break;
#endif	/* __x86 */
			case 'a':
				if (strncmp(arg, "-xarch=", 7) == 0) {
					xlate(h, arg + 7, xarch_tbl);
					break;
				}
				error(arg);
				break;
			case 'b':
				if (strncmp(arg, "-xbuiltin=", 10) == 0) {
					if (strcmp(arg + 10, "%all"))
						newae(h, "-fbuiltin");
					break;
				}
				error(arg);
				break;
			case 'C':
				/* Accept C++ style comments -- ignore */
				if (strcmp(arg, "-xCC") == 0)
					break;
				error(arg);
				break;
			case 'c':
				if (strncmp(arg, "-xc99=%all", 10) == 0) {
					newae(h, "-std=gnu99");
					break;
				}
				if (strncmp(arg, "-xc99=%none", 11) == 0) {
					newae(h, "-std=gnu89");
					break;
				}
				if (strncmp(arg, "-xchip=", 7) == 0) {
					xlate(h, arg + 7, xchip_tbl);
					break;
				}
				if (strncmp(arg, "-xcode=", 7) == 0) {
					xlate(h, arg + 7, xcode_tbl);
					if (strncmp(arg + 7, "pic", 3) == 0)
						pic = 1;
					break;
				}
				if (strncmp(arg, "-xcache=", 8) == 0)
					break;
				if (strncmp(arg, "-xcrossfile", 11) == 0)
					break;
				error(arg);
				break;
			case 'd':
				if (strcmp(arg, "-xdepend") == 0)
					break;
				if (strncmp(arg, "-xdebugformat=", 14) == 0)
					break;
				error(arg);
				break;
			case 'F':
				/* compile for mapfile reordering -- ignore */
				if (strcmp(arg, "-xF") == 0)
					break;
				error(arg);
				break;
			case 'i':
				if (strncmp(arg, "-xinline", 8) == 0)
					/* No inlining; ignore */
					break;
				if (strcmp(arg, "-xildon") == 0 ||
				    strcmp(arg, "-xildoff") == 0)
					/* No incremental linking; ignore */
					break;
				error(arg);
				break;
			case 'M':
				if (strcmp(arg, "-xM") == 0) {
					newae(h, "-M");
					break;
				}
				if (strcmp(arg, "-xM1") == 0) {
					newae(h, "-MM");
					break;
				}
				error(arg);
				break;
			case 'n':
				if (strcmp(arg, "-xnolib") == 0) {
					nolibc = 1;
					break;
				}
				error(arg);
				break;
			case 'O':
				if (strncmp(arg, "-xO", 3) == 0) {
					size_t len = strlen(arg);
					char *s = malloc(len);
					int c = *(arg + 3);
					int level;

					if (len != 4 || !isdigit(c))
						error(arg);

					level = atoi(arg + 3);
					if (level > 5)
						error(arg);
					if (level >= 2) {
						/*
						 * For gcc-3.4.x at -O2 we
						 * need to disable optimizations
						 * that break ON.
						 */
						optim_disable(h, level);
						/*
						 * limit -xO3 to -O2 as well.
						 */
						level = 2;
					}
					(void) snprintf(s, len, "-O%d", level);
					newae(h, s);
					free(s);
					break;
				}
				error(arg);
				break;
			case 'p':
				if (strcmp(arg, "-xpentium") == 0) {
					newae(h, "-march=pentium");
					break;
				}
				if (strcmp(arg, "-xpg") == 0) {
					newae(h, "-pg");
					break;
				}
				error(arg);
				break;
			case 'r':
				if (strncmp(arg, "-xregs=", 7) == 0) {
					xlate(h, arg + 7, xregs_tbl);
					break;
				}
				error(arg);
				break;
			case 's':
				if (strcmp(arg, "-xs") == 0 ||
				    strcmp(arg, "-xspace") == 0 ||
				    strcmp(arg, "-xstrconst") == 0)
					break;
				error(arg);
				break;
			case 't':
				if (strcmp(arg, "-xtransition") == 0) {
					newae(h, "-Wtransition");
					break;
				}
				if (strcmp(arg, "-xtrigraphs=yes") == 0) {
					newae(h, "-trigraphs");
					break;
				}
				if (strcmp(arg, "-xtrigraphs=no") == 0) {
					newae(h, "-notrigraphs");
					break;
				}
				if (strncmp(arg, "-xtarget=", 9) == 0) {
					xlate(h, arg + 9, xtarget_tbl);
					break;
				}
				error(arg);
				break;
			case 'e':
			case 'h':
			case 'l':
			default:
				error(arg);
				break;
			}
			break;
		case 'Y':
			if (arglen == 1) {
				if ((arg = *++argv) == NULL || *arg == '\0')
					error("-Y");
				argc--;
				arglen = strlen(arg + 1);
			} else {
				arg += 2;
			}
			/* Just ignore -YS,... for now */
			if (strncmp(arg, "S,", 2) == 0)
				break;
			if (strncmp(arg, "l,", 2) == 0) {
				char *s = strdup(arg);
				s[0] = '-';
				s[1] = 'B';
				newae(h, s);
				free(s);
				break;
			}
			if (strncmp(arg, "I,", 2) == 0) {
				char *s = strdup(arg);
				s[0] = '-';
				s[1] = 'I';
				newae(h, "-nostdinc");
				newae(h, s);
				free(s);
				break;
			}
			error(arg);
			break;
		case 'Q':
			/*
			 * We could map -Qy into -Wl,-Qy etc.
			 */
		default:
			error(arg);
			break;
		}
	}

	if (model && !pic)
		newae(h, model);
	if (!nolibc)
		newae(h, "-lc");
}

/* ARGSUSED4 */
static void
do_cc(const char *dir, const char *cmd, int argc, char **argv,
    struct aelist *h, int cplusplus)
{
	char *program;
	size_t len = strlen(dir) + strlen(cmd) + 2;

	program = malloc(len);
	(void) snprintf(program, len, "%s/%s", dir, cmd);

	/*
	 * This is pretty simple.
	 * We just have to recognize -V, -_noecho, -_compiler, -_cc= and -_gcc=
	 */
	newae(h, program);

	while (--argc > 0) {
		char *arg = *++argv;

		if (*arg != '-') {
			newae(h, arg);
		} else if (*(arg + 1) != '_') {
			if (strcmp(arg, "-V") == 0)
				echo = 0;
			newae(h, arg);
		} else if (strcmp(arg, "-_noecho") == 0) {
			echo = 0;
		} else if (strcmp(arg, "-_compiler") == 0) {
			(void) printf("%s\n", program);
			exit(0);
		} else if (strncmp(arg, "-_cc=", 5) == 0 ||
		    strncmp(arg, "-_CC=", 5) == 0) {
			newae(h, arg + 5);
		} else if (strncmp(arg, "-_gcc=", 6) != 0 &&
		    strncmp(arg, "-_g++=", 6) != 0) {
			(void) fprintf(stderr,
			    "%s: invalid argument '%s'\n", progname, arg);
			exit(2);
		}
	}
}

int
main(int argc, char **argv)
{
	struct aelist *h = newael();
	const char *dir;
	int ac;
	char **newargv;
	struct ae *a;
	char cc_buf[MAXPATHLEN], gcc_buf[MAXPATHLEN];

	if ((progname = strrchr(argv[0], '/')) == NULL)
		progname = argv[0];
	else
		progname++;

	default_cc_dir = DEFAULT_CC_DIR;
	default_gcc_dir = DEFAULT_GCC_DIR;
	default_cplusplus_dir = DEFAULT_CPLUSPLUS_DIR;
	default_gplusplus_dir = DEFAULT_GPLUSPLUS_DIR;

	/*
	 * Figure out where to get our tools from.  This depends on
	 * the environment variables set at run time.
	 */
	if ((dir = getenv("SPRO_VROOT")) != NULL) {
		(void) snprintf(cc_buf, MAXPATHLEN, "%s/bin", dir);
	} else if ((dir = getenv("SPRO_ROOT")) != NULL) {
		(void) snprintf(cc_buf, MAXPATHLEN, "%s/SOS10/bin", dir);
	} else if ((dir = getenv("BUILD_TOOLS")) != NULL) {
		(void) snprintf(cc_buf, MAXPATHLEN,
		    "%s/SUNWspro/SOS10/bin", dir);
	}
	if (dir != NULL)
		default_cc_dir = (char *)cc_buf;

	if ((dir = getenv("GNU_ROOT")) != NULL) {
		(void) snprintf(gcc_buf, MAXPATHLEN, "%s/bin", dir);
		default_gcc_dir = (char *)gcc_buf;
	}

	default_cplusplus_dir = default_cc_dir;
	default_gplusplus_dir = default_gcc_dir;

	/*
	 * The first argument must be one of "-_cc", "-_gcc", "-_CC", or "-_g++"
	 */
	if (argc == 1)
		usage();
	argc--;
	argv++;
	if (strcmp(argv[0], "-_cc") == 0) {
		if ((dir = getenv("CW_CC_DIR")) == NULL)
			dir = default_cc_dir;
		do_cc(dir, "cc", argc, argv, h, 0);
	} else if (strcmp(argv[0], "-_gcc") == 0) {
		if ((dir = getenv("CW_GCC_DIR")) == NULL)
			dir = default_gcc_dir;
		do_gcc(dir, "gcc", argc, argv, h, 0);
	} else if (strcmp(argv[0], "-_CC") == 0) {
		if ((dir = getenv("CW_CPLUSPLUS_DIR")) == NULL)
			dir = default_cplusplus_dir;
		do_cc(dir, "CC", argc, argv, h, 1);
	} else if (strcmp(argv[0], "-_g++") == 0) {
		if ((dir = getenv("CW_GPLUSPLUS_DIR")) == NULL)
			dir = default_gplusplus_dir;
		do_gcc(dir, "g++", argc, argv, h, 1);
	} else {
		/* assume "-_gcc" by default */
		argc++;
		argv--;
		if ((dir = getenv("CW_GCC_DIR")) == NULL)
			dir = default_gcc_dir;
		do_gcc(dir, "gcc", argc, argv, h, 0);
	}

	newargv = calloc(sizeof (*newargv), newargc + 1);

	if (echo)
		(void) printf("+ ");

	for (ac = 0, a = h->ael_head; a; a = a->ae_next, ac++) {
		newargv[ac] = a->ae_arg;
		if (echo)
			(void) printf("%s ", a->ae_arg);
		if (a == h->ael_tail)
			break;
	}

	if (echo) {
		(void) printf("\n");
		(void) fflush(stdout);
	}

	/*
	 * Here goes ..
	 */
	(void) execvp(newargv[0], newargv);

	/*
	 * execvp() returns only on error.
	 */
	perror("execvp");
	(void) fprintf(stderr, "%s: couldn't run %s\n",
	    progname, newargv[0]);
	return (4);
}