Print this page
switch aw & cw to target ARMv7
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/cw/cw.c
+++ new/usr/src/tools/cw/cw.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2011, Richard Lowe.
24 24 */
25 25 /*
26 26 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
27 27 * Use is subject to license terms.
28 28 */
29 29
30 30 /*
31 31 * Wrapper for the GNU C compiler to make it accept the Sun C compiler
32 32 * arguments where possible.
33 33 *
34 34 * Since the translation is inexact, this is something of a work-in-progress.
35 35 *
36 36 */
37 37
38 38 /* If you modify this file, you must increment CW_VERSION */
39 39 #define CW_VERSION "1.30"
40 40
41 41 /*
42 42 * -# Verbose mode
43 43 * -### Show compiler commands built by driver, no compilation
44 44 * -A<name[(tokens)]> Preprocessor predicate assertion
45 45 * -B<[static|dynamic]> Specify dynamic or static binding
46 46 * -C Prevent preprocessor from removing comments
47 47 * -c Compile only - produce .o files, suppress linking
48 48 * -cg92 Alias for -xtarget=ss1000
49 49 * -D<name[=token]> Associate name with token as if by #define
50 50 * -d[y|n] dynamic [-dy] or static [-dn] option to linker
51 51 * -E Compile source through preprocessor only, output to stdout
52 52 * -erroff=<t> Suppress warnings specified by tags t(%none, %all, <tag list>)
53 53 * -errtags=<a> Display messages with tags a(no, yes)
54 54 * -errwarn=<t> Treats warnings specified by tags t(%none, %all, <tag list>)
55 55 * as errors
56 56 * -fast Optimize using a selection of options
57 57 * -fd Report old-style function definitions and declarations
58 58 * -features=zla Allow zero-length arrays
59 59 * -flags Show this summary of compiler options
60 60 * -fnonstd Initialize floating-point hardware to non-standard preferences
61 61 * -fns[=<yes|no>] Select non-standard floating point mode
62 62 * -fprecision=<p> Set FP rounding precision mode p(single, double, extended)
63 63 * -fround=<r> Select the IEEE rounding mode in effect at startup
64 64 * -fsimple[=<n>] Select floating-point optimization preferences <n>
65 65 * -fsingle Use single-precision arithmetic (-Xt and -Xs modes only)
66 66 * -ftrap=<t> Select floating-point trapping mode in effect at startup
67 67 * -fstore force floating pt. values to target precision on assignment
68 68 * -G Build a dynamic shared library
69 69 * -g Compile for debugging
70 70 * -H Print path name of each file included during compilation
71 71 * -h <name> Assign <name> to generated dynamic shared library
72 72 * -I<dir> Add <dir> to preprocessor #include file search path
73 73 * -i Passed to linker to ignore any LD_LIBRARY_PATH setting
74 74 * -keeptmp Keep temporary files created during compilation
75 75 * -KPIC Compile position independent code with 32-bit addresses
76 76 * -Kpic Compile position independent code
77 77 * -L<dir> Pass to linker to add <dir> to the library search path
78 78 * -l<name> Link with library lib<name>.a or lib<name>.so
79 79 * -mc Remove duplicate strings from .comment section of output files
80 80 * -mr Remove all strings from .comment section of output files
81 81 * -mr,"string" Remove all strings and append "string" to .comment section
82 82 * -mt Specify options needed when compiling multi-threaded code
83 83 * -native Find available processor, generate code accordingly
84 84 * -nofstore Do not force floating pt. values to target precision
85 85 * on assignment
86 86 * -nolib Same as -xnolib
87 87 * -noqueue Disable queuing of compiler license requests
88 88 * -norunpath Do not build in a runtime path for shared libraries
89 89 * -O Use default optimization level (-xO2 or -xO3. Check man page.)
90 90 * -o <outputfile> Set name of output file to <outputfile>
91 91 * -P Compile source through preprocessor only, output to .i file
92 92 * -PIC Alias for -KPIC or -xcode=pic32
93 93 * -p Compile for profiling with prof
94 94 * -pic Alias for -Kpic or -xcode=pic13
95 95 * -Q[y|n] Emit/don't emit identification info to output file
96 96 * -qp Compile for profiling with prof
97 97 * -R<dir[:dir]> Build runtime search path list into executable
98 98 * -S Compile and only generate assembly code (.s)
99 99 * -s Strip symbol table from the executable file
100 100 * -t Turn off duplicate symbol warnings when linking
101 101 * -U<name> Delete initial definition of preprocessor symbol <name>
102 102 * -V Report version number of each compilation phase
103 103 * -v Do stricter semantic checking
104 104 * -W<c>,<arg> Pass <arg> to specified component <c> (a,l,m,p,0,2,h,i,u)
105 105 * -w Suppress compiler warning messages
106 106 * -Xa Compile assuming ANSI C conformance, allow K & R extensions
107 107 * (default mode)
108 108 * -Xc Compile assuming strict ANSI C conformance
109 109 * -Xs Compile assuming (pre-ANSI) K & R C style code
110 110 * -Xt Compile assuming K & R conformance, allow ANSI C
111 111 * -x386 Generate code for the 80386 processor
112 112 * -x486 Generate code for the 80486 processor
113 113 * -xarch=<a> Specify target architecture instruction set
114 114 * -xbuiltin[=<b>] When profitable inline, or substitute intrinisic functions
115 115 * for system functions, b={%all,%none}
116 116 * -xCC Accept C++ style comments
117 117 * -xchar_byte_order=<o> Specify multi-char byte order <o> (default, high, low)
118 118 * -xchip=<c> Specify the target processor for use by the optimizer
119 119 * -xcode=<c> Generate different code for forming addresses
120 120 * -xcrossfile[=<n>] Enable optimization and inlining across source files,
121 121 * n={0|1}
122 122 * -xe Perform only syntax/semantic checking, no code generation
123 123 * -xF Compile for later mapfile reordering or unused section
124 124 * elimination
125 125 * -xhelp=<f> Display on-line help information f(flags, readme, errors)
126 126 * -xildoff Cancel -xildon
127 127 * -xildon Enable use of the incremental linker, ild
128 128 * -xinline=[<a>,...,<a>] Attempt inlining of specified user routines,
129 129 * <a>={%auto,func,no%func}
130 130 * -xlibmieee Force IEEE 754 return values for math routines in
131 131 * exceptional cases
132 132 * -xlibmil Inline selected libm math routines for optimization
133 133 * -xlic_lib=sunperf Link in the Sun supplied performance libraries
134 134 * -xlicinfo Show license server information
135 135 * -xM Generate makefile dependencies
136 136 * -xM1 Generate makefile dependencies, but exclude /usr/include
137 137 * -xmaxopt=[off,1,2,3,4,5] maximum optimization level allowed on #pragma opt
138 138 * -xnolib Do not link with default system libraries
139 139 * -xnolibmil Cancel -xlibmil on command line
140 140 * -xO<n> Generate optimized code (n={1|2|3|4|5})
141 141 * -xP Print prototypes for function definitions
142 142 * -xpentium Generate code for the pentium processor
143 143 * -xpg Compile for profiling with gprof
144 144 * -xprofile=<p> Collect data for a profile or use a profile to optimize
145 145 * <p>={{collect,use}[:<path>],tcov}
146 146 * -xregs=<r> Control register allocation
147 147 * -xs Allow debugging without object (.o) files
148 148 * -xsb Compile for use with the WorkShop source browser
149 149 * -xsbfast Generate only WorkShop source browser info, no compilation
150 150 * -xsfpconst Represent unsuffixed floating point constants as single
151 151 * precision
152 152 * -xspace Do not do optimizations that increase code size
153 153 * -xstrconst Place string literals into read-only data segment
154 154 * -xtarget=<t> Specify target system for optimization
155 155 * -xtemp=<dir> Set directory for temporary files to <dir>
156 156 * -xtime Report the execution time for each compilation phase
157 157 * -xtransition Emit warnings for differences between K&R C and ANSI C
158 158 * -xtrigraphs[=<yes|no>] Enable|disable trigraph translation
159 159 * -xunroll=n Enable unrolling loops n times where possible
160 160 * -Y<c>,<dir> Specify <dir> for location of component <c> (a,l,m,p,0,h,i,u)
161 161 * -YA,<dir> Change default directory searched for components
162 162 * -YI,<dir> Change default directory searched for include files
163 163 * -YP,<dir> Change default directory for finding libraries files
164 164 * -YS,<dir> Change default directory for startup object files
165 165 */
166 166
167 167 /*
168 168 * Translation table:
169 169 */
170 170 /*
171 171 * -# -v
172 172 * -### error
173 173 * -A<name[(tokens)]> pass-thru
174 174 * -B<[static|dynamic]> pass-thru (syntax error for anything else)
175 175 * -C pass-thru
176 176 * -c pass-thru
177 177 * -cg92 -m32 -mcpu=v8 -mtune=supersparc (SPARC only)
178 178 * -D<name[=token]> pass-thru
179 179 * -dy or -dn -Wl,-dy or -Wl,-dn
180 180 * -E pass-thru
181 181 * -erroff=E_EMPTY_TRANSLATION_UNIT ignore
182 182 * -errtags=%all -Wall
183 183 * -errwarn=%all -Werror else -Wno-error
184 184 * -fast error
185 185 * -fd error
186 186 * -features=zla ignore
187 187 * -flags --help
188 188 * -fnonstd error
189 189 * -fns[=<yes|no>] error
190 190 * -fprecision=<p> error
191 191 * -fround=<r> error
192 192 * -fsimple[=<n>] error
193 193 * -fsingle[=<n>] error
194 194 * -ftrap=<t> error
195 195 * -fstore error
196 196 * -G pass-thru
197 197 * -g pass-thru
198 198 * -H pass-thru
199 199 * -h <name> pass-thru
200 200 * -I<dir> pass-thru
201 201 * -i pass-thru
202 202 * -keeptmp -save-temps
203 203 * -KPIC -fPIC
204 204 * -Kpic -fpic
205 205 * -L<dir> pass-thru
206 206 * -l<name> pass-thru
207 207 * -mc error
208 208 * -mr error
209 209 * -mr,"string" error
210 210 * -mt -D_REENTRANT
211 211 * -native error
212 212 * -nofstore error
213 213 * -nolib -nodefaultlibs
214 214 * -noqueue ignore
215 215 * -norunpath ignore
216 216 * -O -O1 (Check the man page to be certain)
217 217 * -o <outputfile> pass-thru
218 218 * -P -E -o filename.i (or error)
219 219 * -PIC -fPIC (C++ only)
220 220 * -p pass-thru
221 221 * -pic -fpic (C++ only)
222 222 * -Q[y|n] error
223 223 * -qp -p
224 224 * -R<dir[:dir]> pass-thru
225 225 * -S pass-thru
226 226 * -s -Wl,-s
227 227 * -t -Wl,-t
228 228 * -U<name> pass-thru
229 229 * -V --version
230 230 * -v -Wall
231 231 * -Wa,<arg> pass-thru
232 232 * -Wp,<arg> pass-thru except -xc99=<a>
233 233 * -Wl,<arg> pass-thru
234 234 * -W{m,0,2,h,i,u> error/ignore
235 235 * -Wu,-xmodel=kernel -ffreestanding -mcmodel=kernel -mno-red-zone
236 236 * -xmodel=kernel -ffreestanding -mcmodel=kernel -mno-red-zone
237 237 * -Wu,-save_args -msave-args
238 238 * -w pass-thru
239 239 * -Xa -std=iso9899:199409 or -ansi
240 240 * -Xc -ansi -pedantic
241 241 * -Xt error
242 242 * -Xs -traditional -std=c89
243 243 * -x386 -march=i386 (x86 only)
244 244 * -x486 -march=i486 (x86 only)
245 245 * -xarch=<a> table
246 246 * -xbuiltin[=<b>] -fbuiltin (-fno-builtin otherwise)
247 247 * -xCC ignore
248 248 * -xchar_byte_order=<o> error
249 249 * -xchip=<c> table
250 250 * -xcode=<c> table
251 251 * -xdebugformat=<format> ignore (always use dwarf-2 for gcc)
252 252 * -xcrossfile[=<n>] ignore
253 253 * -xe error
254 254 * -xF error
255 255 * -xhelp=<f> error
256 256 * -xildoff ignore
257 257 * -xildon ignore
258 258 * -xinline ignore
259 259 * -xlibmieee error
260 260 * -xlibmil error
261 261 * -xlic_lib=sunperf error
262 262 * -xM -M
263 263 * -xM1 -MM
264 264 * -xmaxopt=[...] error
265 265 * -xnolib -nodefaultlibs
266 266 * -xnolibmil error
267 267 * -xO<n> -O<n>
268 268 * -xP error
269 269 * -xpentium -march=pentium (x86 only)
270 270 * -xpg error
271 271 * -xprofile=<p> error
272 272 * -xregs=<r> table
273 273 * -xs error
274 274 * -xsb error
275 275 * -xsbfast error
276 276 * -xsfpconst error
277 277 * -xspace ignore (-not -Os)
278 278 * -xstrconst ignore
279 279 * -xtarget=<t> table
280 280 * -xtemp=<dir> error
281 281 * -xtime error
282 282 * -xtransition -Wtransition
283 283 * -xtrigraphs=<yes|no> -trigraphs -notrigraphs
284 284 * -xunroll=n error
285 285 * -W0,-xdbggen=no%usedonly -fno-eliminate-unused-debug-symbols
286 286 * -fno-eliminate-unused-debug-types
287 287 * -Y<c>,<dir> error
288 288 * -YA,<dir> error
289 289 * -YI,<dir> -nostdinc -I<dir>
290 290 * -YP,<dir> error
291 291 * -YS,<dir> error
292 292 */
293 293
294 294 #include <stdio.h>
295 295 #include <sys/types.h>
296 296 #include <unistd.h>
297 297 #include <string.h>
298 298 #include <stdlib.h>
299 299 #include <ctype.h>
300 300 #include <fcntl.h>
301 301 #include <errno.h>
302 302 #include <stdarg.h>
303 303 #include <sys/utsname.h>
304 304 #include <sys/param.h>
305 305 #include <sys/isa_defs.h>
306 306 #include <sys/wait.h>
307 307 #include <sys/stat.h>
308 308
309 309 #define CW_F_CXX 0x01
310 310 #define CW_F_SHADOW 0x02
311 311 #define CW_F_EXEC 0x04
312 312 #define CW_F_ECHO 0x08
313 313 #define CW_F_XLATE 0x10
314 314 #define CW_F_PROG 0x20
315 315
316 316 typedef enum cw_compiler {
317 317 CW_C_CC = 0,
318 318 CW_C_GCC
319 319 } cw_compiler_t;
320 320
321 321 static const char *cmds[] = {
322 322 "cc", "CC",
323 323 "gcc", "g++"
324 324 };
325 325
326 326 static char default_dir[2][MAXPATHLEN] = {
327 327 DEFAULT_CC_DIR,
328 328 DEFAULT_GCC_DIR,
329 329 };
330 330
331 331 #define CC(ctx) \
332 332 (((ctx)->i_flags & CW_F_SHADOW) ? \
333 333 ((ctx)->i_compiler == CW_C_CC ? CW_C_GCC : CW_C_CC) : \
334 334 (ctx)->i_compiler)
335 335
336 336 #define CIDX(compiler, flags) \
337 337 ((int)(compiler) << 1) + ((flags) & CW_F_CXX ? 1 : 0)
338 338
339 339 typedef enum cw_op {
340 340 CW_O_NONE = 0,
341 341 CW_O_PREPROCESS,
342 342 CW_O_COMPILE,
343 343 CW_O_LINK
344 344 } cw_op_t;
345 345
346 346 struct aelist {
347 347 struct ae {
348 348 struct ae *ae_next;
349 349 char *ae_arg;
350 350 } *ael_head, *ael_tail;
351 351 int ael_argc;
352 352 };
353 353
354 354 typedef struct cw_ictx {
355 355 cw_compiler_t i_compiler;
356 356 struct aelist *i_ae;
357 357 uint32_t i_flags;
358 358 int i_oldargc;
359 359 char **i_oldargv;
360 360 pid_t i_pid;
361 361 char i_discard[MAXPATHLEN];
362 362 char *i_stderr;
363 363 } cw_ictx_t;
364 364
365 365 /*
366 366 * Status values to indicate which Studio compiler and associated
367 367 * flags are being used.
368 368 */
369 369 #define M32 0x01 /* -m32 - only on Studio 12 */
370 370 #define M64 0x02 /* -m64 - only on Studio 12 */
371 371 #define SS11 0x100 /* Studio 11 */
372 372 #define SS12 0x200 /* Studio 12 */
373 373
374 374 #define TRANS_ENTRY 5
375 375 /*
376 376 * Translation table definition for the -xarch= flag. The "x_arg"
377 377 * value is translated into the appropriate gcc flags according
378 378 * to the values in x_trans[n]. The x_flags indicates what compiler
379 379 * is being used and what flags have been set via the use of
380 380 * "x_arg".
381 381 */
382 382 typedef struct xarch_table {
383 383 char *x_arg;
384 384 int x_flags;
385 385 char *x_trans[TRANS_ENTRY];
386 386 } xarch_table_t;
387 387
388 388 /*
389 389 * The translation table for the -xarch= flag used in the Studio compilers.
390 390 */
391 391 static const xarch_table_t xtbl[] = {
392 392 #if defined(CW_TARGET_i386)
393 393 { "generic", SS11 },
394 394 { "generic64", (SS11|M64), { "-m64", "-mtune=opteron" } },
395 395 { "amd64", (SS11|M64), { "-m64", "-mtune=opteron" } },
396 396 { "386", SS11, { "-march=i386" } },
397 397 { "pentium_pro", SS11, { "-march=pentiumpro" } },
398 398 { "sse", SS11, { "-msse", "-mfpmath=sse" } },
399 399 { "sse2", SS11, { "-msse2", "-mfpmath=sse" } },
400 400 #elif defined(CW_TARGET_sparc)
401 401 { "generic", (SS11|M32), { "-m32", "-mcpu=v8" } },
402 402 { "generic64", (SS11|M64), { "-m64", "-mcpu=v9" } },
403 403 { "v8", (SS11|M32), { "-m32", "-mcpu=v8", "-mno-v8plus" } },
404 404 { "v8plus", (SS11|M32), { "-m32", "-mcpu=v9", "-mv8plus" } },
405 405 { "v8plusa", (SS11|M32), { "-m32", "-mcpu=ultrasparc", "-mv8plus",
↓ open down ↓ |
405 lines elided |
↑ open up ↑ |
406 406 "-mvis" } },
407 407 { "v8plusb", (SS11|M32), { "-m32", "-mcpu=ultrasparc3", "-mv8plus",
408 408 "-mvis" } },
409 409 { "v9", (SS11|M64), { "-m64", "-mcpu=v9" } },
410 410 { "v9a", (SS11|M64), { "-m64", "-mcpu=ultrasparc", "-mvis" } },
411 411 { "v9b", (SS11|M64), { "-m64", "-mcpu=ultrasparc3", "-mvis" } },
412 412 { "sparc", SS12, { "-mcpu=v9", "-mv8plus" } },
413 413 { "sparcvis", SS12, { "-mcpu=ultrasparc", "-mvis" } },
414 414 { "sparcvis2", SS12, { "-mcpu=ultrasparc3", "-mvis" } }
415 415 #elif defined(CW_TARGET_arm)
416 - { "generic", SS12, { "-march=armv6", "-mfpu=vfp", "-mhard-float" } }
416 + { "generic", SS12, { "-march=armv7-a", "-mfpu=vfpv3-d16", "-mhard-float" } }
417 417 #else
418 418 #error Unknown CW_TARGET
419 419 #endif
420 420 };
421 421
422 422 static int xtbl_size = sizeof (xtbl) / sizeof (xarch_table_t);
423 423
424 424 static const char *progname;
425 425
426 426 static const char *xchip_tbl[] = {
427 427 #if defined(CW_TARGET_i386)
428 428 "386", "-mtune=i386", NULL,
429 429 "486", "-mtune=i486", NULL,
430 430 "pentium", "-mtune=pentium", NULL,
431 431 "pentium_pro", "-mtune=pentiumpro", NULL,
432 432 #elif defined(CW_TARGET_sparc)
433 433 "super", "-mtune=supersparc", NULL,
434 434 "ultra", "-mtune=ultrasparc", NULL,
435 435 "ultra3", "-mtune=ultrasparc3", NULL,
436 436 #endif
437 437 NULL, NULL
438 438 };
439 439
440 440 static const char *xcode_tbl[] = {
441 441 #if defined(CW_TARGET_sparc)
442 442 "abs32", "-fno-pic", "-mcmodel=medlow", NULL,
443 443 "abs44", "-fno-pic", "-mcmodel=medmid", NULL,
444 444 "abs64", "-fno-pic", "-mcmodel=medany", NULL,
445 445 "pic13", "-fpic", NULL,
446 446 "pic32", "-fPIC", NULL,
447 447 #endif
448 448 NULL, NULL
449 449 };
450 450
451 451 static const char *xtarget_tbl[] = {
452 452 #if defined(CW_TARGET_i386)
453 453 "pentium_pro", "-march=pentiumpro", NULL,
454 454 #endif /* i386 */
455 455 NULL, NULL
456 456 };
457 457
458 458 static const char *xregs_tbl[] = {
459 459 #if defined(CW_TARGET_sparc)
460 460 "appl", "-mapp-regs", NULL,
461 461 "no%appl", "-mno-app-regs", NULL,
462 462 "float", "-mfpu", NULL,
463 463 "no%float", "-mno-fpu", NULL,
464 464 #endif /* sparc */
465 465 NULL, NULL
466 466 };
467 467
468 468 static void
469 469 nomem(void)
470 470 {
471 471 (void) fprintf(stderr, "%s: error: out of memory\n", progname);
472 472 exit(1);
473 473 }
474 474
475 475 static void
476 476 cw_perror(const char *fmt, ...)
477 477 {
478 478 va_list ap;
479 479 int saved_errno = errno;
480 480
481 481 (void) fprintf(stderr, "%s: error: ", progname);
482 482
483 483 va_start(ap, fmt);
484 484 (void) vfprintf(stderr, fmt, ap);
485 485 va_end(ap);
486 486
487 487 (void) fprintf(stderr, " (%s)\n", strerror(saved_errno));
488 488 }
489 489
490 490 static void
491 491 newae(struct aelist *ael, const char *arg)
492 492 {
493 493 struct ae *ae;
494 494
495 495 if ((ae = calloc(sizeof (*ae), 1)) == NULL)
496 496 nomem();
497 497 ae->ae_arg = strdup(arg);
498 498 if (ael->ael_tail == NULL)
499 499 ael->ael_head = ae;
500 500 else
501 501 ael->ael_tail->ae_next = ae;
502 502 ael->ael_tail = ae;
503 503 ael->ael_argc++;
504 504 }
505 505
506 506 static cw_ictx_t *
507 507 newictx(void)
508 508 {
509 509 cw_ictx_t *ctx = calloc(sizeof (cw_ictx_t), 1);
510 510 if (ctx)
511 511 if ((ctx->i_ae = calloc(sizeof (struct aelist), 1)) == NULL) {
512 512 free(ctx);
513 513 return (NULL);
514 514 }
515 515
516 516 return (ctx);
517 517 }
518 518
519 519 static void
520 520 error(const char *arg)
521 521 {
522 522 (void) fprintf(stderr,
523 523 "%s: error: mapping failed at or near arg '%s'\n", progname, arg);
524 524 exit(2);
525 525 }
526 526
527 527 /*
528 528 * Add the current favourite set of warnings to the gcc invocation.
529 529 */
530 530 static void
531 531 warnings(struct aelist *h)
532 532 {
533 533 static int warningsonce;
534 534
535 535 if (warningsonce++)
536 536 return;
537 537
538 538 /*
539 539 * Enable as many warnings as exist, then disable those that we never
540 540 * ever want.
541 541 */
542 542 newae(h, "-Wall");
543 543 newae(h, "-Wextra");
544 544 }
545 545
546 546 static void
547 547 optim_disable(struct aelist *h, int level)
548 548 {
549 549 if (level >= 2) {
550 550 newae(h, "-fno-strict-aliasing");
551 551 newae(h, "-fno-unit-at-a-time");
552 552 newae(h, "-fno-optimize-sibling-calls");
553 553 }
554 554 }
555 555
556 556 /* ARGSUSED */
557 557 static void
558 558 Xamode(struct aelist *h)
559 559 {
560 560 }
561 561
562 562 static void
563 563 Xcmode(struct aelist *h)
564 564 {
565 565 static int xconce;
566 566
567 567 if (xconce++)
568 568 return;
569 569
570 570 newae(h, "-ansi");
571 571 newae(h, "-pedantic-errors");
572 572 }
573 573
574 574 static void
575 575 Xsmode(struct aelist *h)
576 576 {
577 577 static int xsonce;
578 578
579 579 if (xsonce++)
580 580 return;
581 581
582 582 newae(h, "-traditional");
583 583 newae(h, "-traditional-cpp");
584 584 }
585 585
586 586 static void
587 587 usage()
588 588 {
589 589 (void) fprintf(stderr,
590 590 "usage: %s { -_cc | -_gcc | -_CC | -_g++ } [ -_compiler | ... ]\n",
591 591 progname);
592 592 exit(2);
593 593 }
594 594
595 595 static int
596 596 xlate_xtb(struct aelist *h, const char *xarg)
597 597 {
598 598 int i, j;
599 599
600 600 for (i = 0; i < xtbl_size; i++) {
601 601 if (strcmp(xtbl[i].x_arg, xarg) == 0)
602 602 break;
603 603 }
604 604
605 605 /*
606 606 * At the end of the table and so no matching "arg" entry
607 607 * found and so this must be a bad -xarch= flag.
608 608 */
609 609 if (i == xtbl_size)
610 610 error(xarg);
611 611
612 612 for (j = 0; j < TRANS_ENTRY; j++) {
613 613 if (xtbl[i].x_trans[j] != NULL)
614 614 newae(h, xtbl[i].x_trans[j]);
615 615 else
616 616 break;
617 617 }
618 618 return (xtbl[i].x_flags);
619 619
620 620 }
621 621
622 622 static void
623 623 xlate(struct aelist *h, const char *xarg, const char **table)
624 624 {
625 625 while (*table != NULL && strcmp(xarg, *table) != 0) {
626 626 while (*table != NULL)
627 627 table++;
628 628 table++;
629 629 }
630 630
631 631 if (*table == NULL)
632 632 error(xarg);
633 633
634 634 table++;
635 635
636 636 while (*table != NULL) {
637 637 newae(h, *table);
638 638 table++;
639 639 }
640 640 }
641 641
642 642 static void
643 643 do_gcc(cw_ictx_t *ctx)
644 644 {
645 645 int c;
646 646 int pic = 0, nolibc = 0;
647 647 int in_output = 0, seen_o = 0, c_files = 0;
648 648 cw_op_t op = CW_O_LINK;
649 649 char *model = NULL;
650 650 int mflag = 0;
651 651
652 652 if (ctx->i_flags & CW_F_PROG) {
653 653 newae(ctx->i_ae, "--version");
654 654 return;
655 655 }
656 656
657 657 newae(ctx->i_ae, "-fident");
658 658 newae(ctx->i_ae, "-finline");
659 659 newae(ctx->i_ae, "-fno-inline-functions");
660 660 newae(ctx->i_ae, "-fno-builtin");
661 661 newae(ctx->i_ae, "-fno-asm");
662 662 newae(ctx->i_ae, "-fdiagnostics-show-option");
663 663 newae(ctx->i_ae, "-nodefaultlibs");
664 664
665 665 #if defined(CW_TARGET_sparc)
666 666 /*
667 667 * The SPARC ldd and std instructions require 8-byte alignment of
668 668 * their address operand. gcc correctly uses them only when the
669 669 * ABI requires 8-byte alignment; unfortunately we have a number of
670 670 * pieces of buggy code that doesn't conform to the ABI. This
671 671 * flag makes gcc work more like Studio with -xmemalign=4.
672 672 */
673 673 newae(ctx->i_ae, "-mno-integer-ldd-std");
674 674 #endif
675 675
676 676 /*
677 677 * This is needed because 'u' is defined
678 678 * under a conditional on 'sun'. Should
679 679 * probably just remove the conditional,
680 680 * or make it be dependent on '__sun'.
681 681 *
682 682 * -Dunix is also missing in enhanced ANSI mode
683 683 */
684 684 newae(ctx->i_ae, "-D__sun");
685 685
686 686 /*
687 687 * Walk the argument list, translating as we go ..
688 688 */
689 689
690 690 while (--ctx->i_oldargc > 0) {
691 691 char *arg = *++ctx->i_oldargv;
692 692 size_t arglen = strlen(arg);
693 693
694 694 if (*arg == '-') {
695 695 arglen--;
696 696 } else {
697 697 /*
698 698 * Discard inline files that gcc doesn't grok
699 699 */
700 700 if (!in_output && arglen > 3 &&
701 701 strcmp(arg + arglen - 3, ".il") == 0)
702 702 continue;
703 703
704 704 if (!in_output && arglen > 2 &&
705 705 arg[arglen - 2] == '.' &&
706 706 (arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' ||
707 707 arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i'))
708 708 c_files++;
709 709
710 710 /*
711 711 * Otherwise, filenames and partial arguments
712 712 * are passed through for gcc to chew on. However,
713 713 * output is always discarded for the secondary
714 714 * compiler.
715 715 */
716 716 if ((ctx->i_flags & CW_F_SHADOW) && in_output)
717 717 newae(ctx->i_ae, ctx->i_discard);
718 718 else
719 719 newae(ctx->i_ae, arg);
720 720 in_output = 0;
721 721 continue;
722 722 }
723 723
724 724 if (ctx->i_flags & CW_F_CXX) {
725 725 if (strncmp(arg, "-compat=", 8) == 0) {
726 726 /* discard -compat=4 and -compat=5 */
727 727 continue;
728 728 }
729 729 if (strcmp(arg, "-Qoption") == 0) {
730 730 /* discard -Qoption and its two arguments */
731 731 if (ctx->i_oldargc < 3)
732 732 error(arg);
733 733 ctx->i_oldargc -= 2;
734 734 ctx->i_oldargv += 2;
735 735 continue;
736 736 }
737 737 if (strcmp(arg, "-xwe") == 0) {
738 738 /* turn warnings into errors */
739 739 newae(ctx->i_ae, "-Werror");
740 740 continue;
741 741 }
742 742 if (strcmp(arg, "-noex") == 0) {
743 743 /* no exceptions */
744 744 newae(ctx->i_ae, "-fno-exceptions");
745 745 /* no run time type descriptor information */
746 746 newae(ctx->i_ae, "-fno-rtti");
747 747 continue;
748 748 }
749 749 if (strcmp(arg, "-pic") == 0) {
750 750 newae(ctx->i_ae, "-fpic");
751 751 pic = 1;
752 752 continue;
753 753 }
754 754 if (strcmp(arg, "-PIC") == 0) {
755 755 newae(ctx->i_ae, "-fPIC");
756 756 pic = 1;
757 757 continue;
758 758 }
759 759 if (strcmp(arg, "-norunpath") == 0) {
760 760 /* gcc has no corresponding option */
761 761 continue;
762 762 }
763 763 if (strcmp(arg, "-nolib") == 0) {
764 764 /* -nodefaultlibs is on by default */
765 765 nolibc = 1;
766 766 continue;
767 767 }
768 768 #if defined(CW_TARGET_sparc)
769 769 if (strcmp(arg, "-cg92") == 0) {
770 770 mflag |= xlate_xtb(ctx->i_ae, "v8");
771 771 xlate(ctx->i_ae, "super", xchip_tbl);
772 772 continue;
773 773 }
774 774 #endif
775 775 }
776 776
777 777 switch ((c = arg[1])) {
778 778 case '_':
779 779 if (strcmp(arg, "-_noecho") == 0)
780 780 ctx->i_flags &= ~CW_F_ECHO;
781 781 else if (strncmp(arg, "-_cc=", 5) == 0 ||
782 782 strncmp(arg, "-_CC=", 5) == 0)
783 783 /* EMPTY */;
784 784 else if (strncmp(arg, "-_gcc=", 6) == 0 ||
785 785 strncmp(arg, "-_g++=", 6) == 0)
786 786 newae(ctx->i_ae, arg + 6);
787 787 else
788 788 error(arg);
789 789 break;
790 790 case '#':
791 791 if (arglen == 1) {
792 792 newae(ctx->i_ae, "-v");
793 793 break;
794 794 }
795 795 error(arg);
796 796 break;
797 797 case 'g':
798 798 newae(ctx->i_ae, "-gdwarf-2");
799 799 break;
800 800 case 'E':
801 801 if (arglen == 1) {
802 802 newae(ctx->i_ae, "-xc");
803 803 newae(ctx->i_ae, arg);
804 804 op = CW_O_PREPROCESS;
805 805 nolibc = 1;
806 806 break;
807 807 }
808 808 error(arg);
809 809 break;
810 810 case 'c':
811 811 case 'S':
812 812 if (arglen == 1) {
813 813 op = CW_O_COMPILE;
814 814 nolibc = 1;
815 815 }
816 816 /* FALLTHROUGH */
817 817 case 'C':
818 818 case 'H':
819 819 case 'p':
820 820 if (arglen == 1) {
821 821 newae(ctx->i_ae, arg);
822 822 break;
823 823 }
824 824 error(arg);
825 825 break;
826 826 case 'A':
827 827 case 'h':
828 828 case 'I':
829 829 case 'i':
830 830 case 'L':
831 831 case 'l':
832 832 case 'R':
833 833 case 'U':
834 834 case 'u':
835 835 case 'w':
836 836 newae(ctx->i_ae, arg);
837 837 break;
838 838 case 'o':
839 839 seen_o = 1;
840 840 if (arglen == 1) {
841 841 in_output = 1;
842 842 newae(ctx->i_ae, arg);
843 843 } else if (ctx->i_flags & CW_F_SHADOW) {
844 844 newae(ctx->i_ae, "-o");
845 845 newae(ctx->i_ae, ctx->i_discard);
846 846 } else {
847 847 newae(ctx->i_ae, arg);
848 848 }
849 849 break;
850 850 case 'D':
851 851 newae(ctx->i_ae, arg);
852 852 /*
853 853 * XXX Clearly a hack ... do we need _KADB too?
854 854 */
855 855 if (strcmp(arg, "-D_KERNEL") == 0 ||
856 856 strcmp(arg, "-D_BOOT") == 0)
857 857 newae(ctx->i_ae, "-ffreestanding");
858 858 break;
859 859 case 'd':
860 860 if (arglen == 2) {
861 861 if (strcmp(arg, "-dy") == 0) {
862 862 newae(ctx->i_ae, "-Wl,-dy");
863 863 break;
864 864 }
865 865 if (strcmp(arg, "-dn") == 0) {
866 866 newae(ctx->i_ae, "-Wl,-dn");
867 867 break;
868 868 }
869 869 }
870 870 if (strcmp(arg, "-dalign") == 0) {
871 871 /*
872 872 * -dalign forces alignment in some cases;
873 873 * gcc does not need any flag to do this.
874 874 */
875 875 break;
876 876 }
877 877 error(arg);
878 878 break;
879 879 case 'e':
880 880 if (strcmp(arg,
881 881 "-erroff=E_EMPTY_TRANSLATION_UNIT") == 0) {
882 882 /*
883 883 * Accept but ignore this -- gcc doesn't
884 884 * seem to complain about empty translation
885 885 * units
886 886 */
887 887 break;
888 888 }
889 889 /* XX64 -- ignore all -erroff= options, for now */
890 890 if (strncmp(arg, "-erroff=", 8) == 0)
891 891 break;
892 892 if (strcmp(arg, "-errtags=yes") == 0) {
893 893 warnings(ctx->i_ae);
894 894 break;
895 895 }
896 896 if (strcmp(arg, "-errwarn=%all") == 0) {
897 897 newae(ctx->i_ae, "-Werror");
898 898 break;
899 899 }
900 900 error(arg);
901 901 break;
902 902 case 'f':
903 903 if (strcmp(arg, "-flags") == 0) {
904 904 newae(ctx->i_ae, "--help");
905 905 break;
906 906 }
907 907 if (strncmp(arg, "-features=zla", 13) == 0) {
908 908 /*
909 909 * Accept but ignore this -- gcc allows
910 910 * zero length arrays.
911 911 */
912 912 break;
913 913 }
914 914 error(arg);
915 915 break;
916 916 case 'G':
917 917 newae(ctx->i_ae, "-shared");
918 918 nolibc = 1;
919 919 break;
920 920 case 'k':
921 921 if (strcmp(arg, "-keeptmp") == 0) {
922 922 newae(ctx->i_ae, "-save-temps");
923 923 break;
924 924 }
925 925 error(arg);
926 926 break;
927 927 case 'K':
928 928 if (arglen == 1) {
929 929 if ((arg = *++ctx->i_oldargv) == NULL ||
930 930 *arg == '\0')
931 931 error("-K");
932 932 ctx->i_oldargc--;
933 933 } else {
934 934 arg += 2;
935 935 }
936 936 if (strcmp(arg, "pic") == 0) {
937 937 newae(ctx->i_ae, "-fpic");
938 938 pic = 1;
939 939 break;
940 940 }
941 941 if (strcmp(arg, "PIC") == 0) {
942 942 newae(ctx->i_ae, "-fPIC");
943 943 pic = 1;
944 944 break;
945 945 }
946 946 error("-K");
947 947 break;
948 948 case 'm':
949 949 if (strcmp(arg, "-mt") == 0) {
950 950 newae(ctx->i_ae, "-D_REENTRANT");
951 951 break;
952 952 }
953 953 if (strcmp(arg, "-m64") == 0) {
954 954 newae(ctx->i_ae, "-m64");
955 955 #if defined(CW_TARGET_i386)
956 956 newae(ctx->i_ae, "-mtune=opteron");
957 957 #endif
958 958 mflag |= M64;
959 959 break;
960 960 }
961 961 if (strcmp(arg, "-m32") == 0) {
962 962 newae(ctx->i_ae, "-m32");
963 963 mflag |= M32;
964 964 break;
965 965 }
966 966 error(arg);
967 967 break;
968 968 case 'B': /* linker options */
969 969 case 'M':
970 970 case 'z':
971 971 {
972 972 char *opt;
973 973 size_t len;
974 974 char *s;
975 975
976 976 if (arglen == 1) {
977 977 opt = *++ctx->i_oldargv;
978 978 if (opt == NULL || *opt == '\0')
979 979 error(arg);
980 980 ctx->i_oldargc--;
981 981 } else {
982 982 opt = arg + 2;
983 983 }
984 984 len = strlen(opt) + 7;
985 985 if ((s = malloc(len)) == NULL)
986 986 nomem();
987 987 (void) snprintf(s, len, "-Wl,-%c%s", c, opt);
988 988 newae(ctx->i_ae, s);
989 989 free(s);
990 990 }
991 991 break;
992 992 case 'n':
993 993 if (strcmp(arg, "-noqueue") == 0) {
994 994 /*
995 995 * Horrid license server stuff - n/a
996 996 */
997 997 break;
998 998 }
999 999 error(arg);
1000 1000 break;
1001 1001 case 'O':
1002 1002 if (arglen == 1) {
1003 1003 newae(ctx->i_ae, "-O");
1004 1004 break;
1005 1005 }
1006 1006 error(arg);
1007 1007 break;
1008 1008 case 'P':
1009 1009 /*
1010 1010 * We could do '-E -o filename.i', but that's hard,
1011 1011 * and we don't need it for the case that's triggering
1012 1012 * this addition. We'll require the user to specify
1013 1013 * -o in the Makefile. If they don't they'll find out
1014 1014 * in a hurry.
1015 1015 */
1016 1016 newae(ctx->i_ae, "-E");
1017 1017 op = CW_O_PREPROCESS;
1018 1018 nolibc = 1;
1019 1019 break;
1020 1020 case 'q':
1021 1021 if (strcmp(arg, "-qp") == 0) {
1022 1022 newae(ctx->i_ae, "-p");
1023 1023 break;
1024 1024 }
1025 1025 error(arg);
1026 1026 break;
1027 1027 case 's':
1028 1028 if (arglen == 1) {
1029 1029 newae(ctx->i_ae, "-Wl,-s");
1030 1030 break;
1031 1031 }
1032 1032 error(arg);
1033 1033 break;
1034 1034 case 't':
1035 1035 if (arglen == 1) {
1036 1036 newae(ctx->i_ae, "-Wl,-t");
1037 1037 break;
1038 1038 }
1039 1039 error(arg);
1040 1040 break;
1041 1041 case 'V':
1042 1042 if (arglen == 1) {
1043 1043 ctx->i_flags &= ~CW_F_ECHO;
1044 1044 newae(ctx->i_ae, "--version");
1045 1045 break;
1046 1046 }
1047 1047 error(arg);
1048 1048 break;
1049 1049 case 'v':
1050 1050 if (arglen == 1) {
1051 1051 warnings(ctx->i_ae);
1052 1052 break;
1053 1053 }
1054 1054 error(arg);
1055 1055 break;
1056 1056 case 'W':
1057 1057 if (strncmp(arg, "-Wp,-xc99", 9) == 0) {
1058 1058 /*
1059 1059 * gcc's preprocessor will accept c99
1060 1060 * regardless, so accept and ignore.
1061 1061 */
1062 1062 break;
1063 1063 }
1064 1064 if (strncmp(arg, "-Wa,", 4) == 0 ||
1065 1065 strncmp(arg, "-Wp,", 4) == 0 ||
1066 1066 strncmp(arg, "-Wl,", 4) == 0) {
1067 1067 newae(ctx->i_ae, arg);
1068 1068 break;
1069 1069 }
1070 1070 if (strcmp(arg, "-W0,-xc99=pragma") == 0) {
1071 1071 /* (undocumented) enables _Pragma */
1072 1072 break;
1073 1073 }
1074 1074 if (strcmp(arg, "-W0,-xc99=%none") == 0) {
1075 1075 /*
1076 1076 * This is a polite way of saying
1077 1077 * "no c99 constructs allowed!"
1078 1078 * For now, just accept and ignore this.
1079 1079 */
1080 1080 break;
1081 1081 }
1082 1082 if (strcmp(arg, "-W0,-noglobal") == 0 ||
1083 1083 strcmp(arg, "-W0,-xglobalstatic") == 0) {
1084 1084 /*
1085 1085 * gcc doesn't prefix local symbols
1086 1086 * in debug mode, so this is not needed.
1087 1087 */
1088 1088 break;
1089 1089 }
1090 1090 if (strcmp(arg, "-W0,-Lt") == 0) {
1091 1091 /*
1092 1092 * Generate tests at the top of loops.
1093 1093 * There is no direct gcc equivalent, ignore.
1094 1094 */
1095 1095 break;
1096 1096 }
1097 1097 if (strcmp(arg, "-W0,-xdbggen=no%usedonly") == 0) {
1098 1098 newae(ctx->i_ae,
1099 1099 "-fno-eliminate-unused-debug-symbols");
1100 1100 newae(ctx->i_ae,
1101 1101 "-fno-eliminate-unused-debug-types");
1102 1102 break;
1103 1103 }
1104 1104 if (strcmp(arg, "-W2,-xwrap_int") == 0) {
1105 1105 /*
1106 1106 * Use the legacy behaviour (pre-SS11)
1107 1107 * for integer wrapping.
1108 1108 * gcc does not need this.
1109 1109 */
1110 1110 break;
1111 1111 }
1112 1112 if (strcmp(arg, "-W2,-Rcond_elim") == 0) {
1113 1113 /*
1114 1114 * Elimination and expansion of conditionals;
1115 1115 * gcc has no direct equivalent.
1116 1116 */
1117 1117 break;
1118 1118 }
1119 1119 if (strcmp(arg, "-Wd,-xsafe=unboundsym") == 0) {
1120 1120 /*
1121 1121 * Prevents optimizing away checks for
1122 1122 * unbound weak symbol addresses. gcc does
1123 1123 * not do this, so it's not needed.
1124 1124 */
1125 1125 break;
1126 1126 }
1127 1127 if (strncmp(arg, "-Wc,-xcode=", 11) == 0) {
1128 1128 xlate(ctx->i_ae, arg + 11, xcode_tbl);
1129 1129 if (strncmp(arg + 11, "pic", 3) == 0)
1130 1130 pic = 1;
1131 1131 break;
1132 1132 }
1133 1133 if (strncmp(arg, "-Wc,-Qiselect", 13) == 0) {
1134 1134 /*
1135 1135 * Prevents insertion of register symbols.
1136 1136 * gcc doesn't do this, so ignore it.
1137 1137 */
1138 1138 break;
1139 1139 }
1140 1140 if (strcmp(arg, "-Wc,-Qassembler-ounrefsym=0") == 0) {
1141 1141 /*
1142 1142 * Prevents optimizing away of static variables.
1143 1143 * gcc does not do this, so it's not needed.
1144 1144 */
1145 1145 break;
1146 1146 }
1147 1147 #if defined(CW_TARGET_i386)
1148 1148 if (strcmp(arg, "-Wu,-xmodel=kernel") == 0) {
1149 1149 newae(ctx->i_ae, "-ffreestanding");
1150 1150 newae(ctx->i_ae, "-mno-red-zone");
1151 1151 model = "-mcmodel=kernel";
1152 1152 nolibc = 1;
1153 1153 break;
1154 1154 }
1155 1155 if (strcmp(arg, "-Wu,-save_args") == 0) {
1156 1156 newae(ctx->i_ae, "-msave-args");
1157 1157 break;
1158 1158 }
1159 1159 #endif /* i386 */
1160 1160 error(arg);
1161 1161 break;
1162 1162 case 'X':
1163 1163 if (strcmp(arg, "-Xa") == 0 ||
1164 1164 strcmp(arg, "-Xt") == 0) {
1165 1165 Xamode(ctx->i_ae);
1166 1166 break;
1167 1167 }
1168 1168 if (strcmp(arg, "-Xc") == 0) {
1169 1169 Xcmode(ctx->i_ae);
1170 1170 break;
1171 1171 }
1172 1172 if (strcmp(arg, "-Xs") == 0) {
1173 1173 Xsmode(ctx->i_ae);
1174 1174 break;
1175 1175 }
1176 1176 error(arg);
1177 1177 break;
1178 1178 case 'x':
1179 1179 if (arglen == 1)
1180 1180 error(arg);
1181 1181 switch (arg[2]) {
1182 1182 #if defined(CW_TARGET_i386)
1183 1183 case '3':
1184 1184 if (strcmp(arg, "-x386") == 0) {
1185 1185 newae(ctx->i_ae, "-march=i386");
1186 1186 break;
1187 1187 }
1188 1188 error(arg);
1189 1189 break;
1190 1190 case '4':
1191 1191 if (strcmp(arg, "-x486") == 0) {
1192 1192 newae(ctx->i_ae, "-march=i486");
1193 1193 break;
1194 1194 }
1195 1195 error(arg);
1196 1196 break;
1197 1197 #endif /* i386 */
1198 1198 case 'a':
1199 1199 if (strncmp(arg, "-xarch=", 7) == 0) {
1200 1200 mflag |= xlate_xtb(ctx->i_ae, arg + 7);
1201 1201 break;
1202 1202 }
1203 1203 error(arg);
1204 1204 break;
1205 1205 case 'b':
1206 1206 if (strncmp(arg, "-xbuiltin=", 10) == 0) {
1207 1207 if (strcmp(arg + 10, "%all"))
1208 1208 newae(ctx->i_ae, "-fbuiltin");
1209 1209 break;
1210 1210 }
1211 1211 error(arg);
1212 1212 break;
1213 1213 case 'C':
1214 1214 /* Accept C++ style comments -- ignore */
1215 1215 if (strcmp(arg, "-xCC") == 0)
1216 1216 break;
1217 1217 error(arg);
1218 1218 break;
1219 1219 case 'c':
1220 1220 if (strncmp(arg, "-xc99=%all", 10) == 0) {
1221 1221 newae(ctx->i_ae, "-std=gnu99");
1222 1222 break;
1223 1223 }
1224 1224 if (strncmp(arg, "-xc99=%none", 11) == 0) {
1225 1225 newae(ctx->i_ae, "-std=gnu89");
1226 1226 break;
1227 1227 }
1228 1228 if (strncmp(arg, "-xchip=", 7) == 0) {
1229 1229 xlate(ctx->i_ae, arg + 7, xchip_tbl);
1230 1230 break;
1231 1231 }
1232 1232 if (strncmp(arg, "-xcode=", 7) == 0) {
1233 1233 xlate(ctx->i_ae, arg + 7, xcode_tbl);
1234 1234 if (strncmp(arg + 7, "pic", 3) == 0)
1235 1235 pic = 1;
1236 1236 break;
1237 1237 }
1238 1238 if (strncmp(arg, "-xcache=", 8) == 0)
1239 1239 break;
1240 1240 if (strncmp(arg, "-xcrossfile", 11) == 0)
1241 1241 break;
1242 1242 error(arg);
1243 1243 break;
1244 1244 case 'd':
1245 1245 if (strcmp(arg, "-xdepend") == 0)
1246 1246 break;
1247 1247 if (strncmp(arg, "-xdebugformat=", 14) == 0)
1248 1248 break;
1249 1249 error(arg);
1250 1250 break;
1251 1251 case 'F':
1252 1252 /*
1253 1253 * Compile for mapfile reordering, or unused
1254 1254 * section elimination, syntax can be -xF or
1255 1255 * more complex, like -xF=%all -- ignore.
1256 1256 */
1257 1257 if (strncmp(arg, "-xF", 3) == 0)
1258 1258 break;
1259 1259 error(arg);
1260 1260 break;
1261 1261 case 'i':
1262 1262 if (strncmp(arg, "-xinline", 8) == 0)
1263 1263 /* No inlining; ignore */
1264 1264 break;
1265 1265 if (strcmp(arg, "-xildon") == 0 ||
1266 1266 strcmp(arg, "-xildoff") == 0)
1267 1267 /* No incremental linking; ignore */
1268 1268 break;
1269 1269 error(arg);
1270 1270 break;
1271 1271 #if defined(CW_TARGET_i386)
1272 1272 case 'm':
1273 1273 if (strcmp(arg, "-xmodel=kernel") == 0) {
1274 1274 newae(ctx->i_ae, "-ffreestanding");
1275 1275 newae(ctx->i_ae, "-mno-red-zone");
1276 1276 model = "-mcmodel=kernel";
1277 1277 nolibc = 1;
1278 1278 break;
1279 1279 }
1280 1280 error(arg);
1281 1281 break;
1282 1282 #endif /* i386 */
1283 1283 case 'M':
1284 1284 if (strcmp(arg, "-xM") == 0) {
1285 1285 newae(ctx->i_ae, "-M");
1286 1286 break;
1287 1287 }
1288 1288 if (strcmp(arg, "-xM1") == 0) {
1289 1289 newae(ctx->i_ae, "-MM");
1290 1290 break;
1291 1291 }
1292 1292 error(arg);
1293 1293 break;
1294 1294 case 'n':
1295 1295 if (strcmp(arg, "-xnolib") == 0) {
1296 1296 nolibc = 1;
1297 1297 break;
1298 1298 }
1299 1299 error(arg);
1300 1300 break;
1301 1301 case 'O':
1302 1302 if (strncmp(arg, "-xO", 3) == 0) {
1303 1303 size_t len = strlen(arg);
1304 1304 char *s;
1305 1305 int c = *(arg + 3);
1306 1306 int level;
1307 1307
1308 1308 if (len != 4 || !isdigit(c))
1309 1309 error(arg);
1310 1310
1311 1311 if ((s = malloc(len)) == NULL)
1312 1312 nomem();
1313 1313
1314 1314 level = atoi(arg + 3);
1315 1315 if (level > 5)
1316 1316 error(arg);
1317 1317 if (level >= 2) {
1318 1318 /*
1319 1319 * For gcc-3.4.x at -O2 we
1320 1320 * need to disable optimizations
1321 1321 * that break ON.
1322 1322 */
1323 1323 optim_disable(ctx->i_ae, level);
1324 1324 /*
1325 1325 * limit -xO3 to -O2 as well.
1326 1326 */
1327 1327 level = 2;
1328 1328 }
1329 1329 (void) snprintf(s, len, "-O%d", level);
1330 1330 newae(ctx->i_ae, s);
1331 1331 free(s);
1332 1332 break;
1333 1333 }
1334 1334 error(arg);
1335 1335 break;
1336 1336 case 'p':
1337 1337 if (strcmp(arg, "-xpentium") == 0) {
1338 1338 newae(ctx->i_ae, "-march=pentium");
1339 1339 break;
1340 1340 }
1341 1341 if (strcmp(arg, "-xpg") == 0) {
1342 1342 newae(ctx->i_ae, "-pg");
1343 1343 break;
1344 1344 }
1345 1345 error(arg);
1346 1346 break;
1347 1347 case 'r':
1348 1348 if (strncmp(arg, "-xregs=", 7) == 0) {
1349 1349 xlate(ctx->i_ae, arg + 7, xregs_tbl);
1350 1350 break;
1351 1351 }
1352 1352 error(arg);
1353 1353 break;
1354 1354 case 's':
1355 1355 if (strcmp(arg, "-xs") == 0 ||
1356 1356 strcmp(arg, "-xspace") == 0 ||
1357 1357 strcmp(arg, "-xstrconst") == 0)
1358 1358 break;
1359 1359 error(arg);
1360 1360 break;
1361 1361 case 't':
1362 1362 if (strcmp(arg, "-xtransition") == 0) {
1363 1363 newae(ctx->i_ae, "-Wtransition");
1364 1364 break;
1365 1365 }
1366 1366 if (strcmp(arg, "-xtrigraphs=yes") == 0) {
1367 1367 newae(ctx->i_ae, "-trigraphs");
1368 1368 break;
1369 1369 }
1370 1370 if (strcmp(arg, "-xtrigraphs=no") == 0) {
1371 1371 newae(ctx->i_ae, "-notrigraphs");
1372 1372 break;
1373 1373 }
1374 1374 if (strncmp(arg, "-xtarget=", 9) == 0) {
1375 1375 xlate(ctx->i_ae, arg + 9, xtarget_tbl);
1376 1376 break;
1377 1377 }
1378 1378 error(arg);
1379 1379 break;
1380 1380 case 'e':
1381 1381 case 'h':
1382 1382 case 'l':
1383 1383 default:
1384 1384 error(arg);
1385 1385 break;
1386 1386 }
1387 1387 break;
1388 1388 case 'Y':
1389 1389 if (arglen == 1) {
1390 1390 if ((arg = *++ctx->i_oldargv) == NULL ||
1391 1391 *arg == '\0')
1392 1392 error("-Y");
1393 1393 ctx->i_oldargc--;
1394 1394 arglen = strlen(arg + 1);
1395 1395 } else {
1396 1396 arg += 2;
1397 1397 }
1398 1398 /* Just ignore -YS,... for now */
1399 1399 if (strncmp(arg, "S,", 2) == 0)
1400 1400 break;
1401 1401 if (strncmp(arg, "l,", 2) == 0) {
1402 1402 char *s = strdup(arg);
1403 1403 s[0] = '-';
1404 1404 s[1] = 'B';
1405 1405 newae(ctx->i_ae, s);
1406 1406 free(s);
1407 1407 break;
1408 1408 }
1409 1409 if (strncmp(arg, "I,", 2) == 0) {
1410 1410 char *s = strdup(arg);
1411 1411 s[0] = '-';
1412 1412 s[1] = 'I';
1413 1413 newae(ctx->i_ae, "-nostdinc");
1414 1414 newae(ctx->i_ae, s);
1415 1415 free(s);
1416 1416 break;
1417 1417 }
1418 1418 error(arg);
1419 1419 break;
1420 1420 case 'Q':
1421 1421 /*
1422 1422 * We could map -Qy into -Wl,-Qy etc.
1423 1423 */
1424 1424 default:
1425 1425 error(arg);
1426 1426 break;
1427 1427 }
1428 1428 }
1429 1429
1430 1430 if (c_files > 1 && (ctx->i_flags & CW_F_SHADOW) &&
1431 1431 op != CW_O_PREPROCESS) {
1432 1432 (void) fprintf(stderr, "%s: error: multiple source files are "
1433 1433 "allowed only with -E or -P\n", progname);
1434 1434 exit(2);
1435 1435 }
1436 1436
1437 1437 /*
1438 1438 * Make sure that we do not have any unintended interactions between
1439 1439 * the xarch options passed in and the version of the Studio compiler
1440 1440 * used.
1441 1441 */
1442 1442 if ((mflag & (SS11|SS12)) == (SS11|SS12)) {
1443 1443 (void) fprintf(stderr,
1444 1444 "Conflicting \"-xarch=\" flags (both Studio 11 and 12)\n");
1445 1445 exit(2);
1446 1446 }
1447 1447
1448 1448 switch (mflag) {
1449 1449 case 0:
1450 1450 /* FALLTHROUGH */
1451 1451 case M32:
1452 1452 #if defined(CW_TARGET_sparc)
1453 1453 /*
1454 1454 * Only -m32 is defined and so put in the missing xarch
1455 1455 * translation.
1456 1456 */
1457 1457 newae(ctx->i_ae, "-mcpu=v8");
1458 1458 newae(ctx->i_ae, "-mno-v8plus");
1459 1459 #endif
1460 1460 break;
1461 1461 case M64:
1462 1462 #if defined(CW_TARGET_sparc)
1463 1463 /*
1464 1464 * Only -m64 is defined and so put in the missing xarch
1465 1465 * translation.
1466 1466 */
1467 1467 newae(ctx->i_ae, "-mcpu=v9");
1468 1468 #endif
1469 1469 break;
1470 1470 case SS12:
1471 1471 #if defined(CW_TARGET_sparc)
1472 1472 /* no -m32/-m64 flag used - this is an error for sparc builds */
1473 1473 (void) fprintf(stderr, "No -m32/-m64 flag defined\n");
1474 1474 exit(2);
1475 1475 #endif
1476 1476 break;
1477 1477 case SS11:
1478 1478 /* FALLTHROUGH */
1479 1479 case (SS11|M32):
1480 1480 case (SS11|M64):
1481 1481 break;
1482 1482 case (SS12|M32):
1483 1483 #if defined(CW_TARGET_sparc)
1484 1484 /*
1485 1485 * Need to add in further 32 bit options because with SS12
1486 1486 * the xarch=sparcvis option can be applied to 32 or 64
1487 1487 * bit, and so the translatation table (xtbl) cannot handle
1488 1488 * that.
1489 1489 */
1490 1490 newae(ctx->i_ae, "-mv8plus");
1491 1491 #endif
1492 1492 break;
1493 1493 case (SS12|M64):
1494 1494 break;
1495 1495 default:
1496 1496 (void) fprintf(stderr,
1497 1497 "Incompatible -xarch= and/or -m32/-m64 options used.\n");
1498 1498 exit(2);
1499 1499 }
1500 1500 if (op == CW_O_LINK && (ctx->i_flags & CW_F_SHADOW))
1501 1501 exit(0);
1502 1502
1503 1503 if (model && !pic)
1504 1504 newae(ctx->i_ae, model);
1505 1505 if (!nolibc)
1506 1506 newae(ctx->i_ae, "-lc");
1507 1507 if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1508 1508 newae(ctx->i_ae, "-o");
1509 1509 newae(ctx->i_ae, ctx->i_discard);
1510 1510 }
1511 1511 }
1512 1512
1513 1513 static void
1514 1514 do_cc(cw_ictx_t *ctx)
1515 1515 {
1516 1516 int in_output = 0, seen_o = 0;
1517 1517 cw_op_t op = CW_O_LINK;
1518 1518
1519 1519 if (ctx->i_flags & CW_F_PROG) {
1520 1520 newae(ctx->i_ae, "-V");
1521 1521 return;
1522 1522 }
1523 1523
1524 1524 while (--ctx->i_oldargc > 0) {
1525 1525 char *arg = *++ctx->i_oldargv;
1526 1526
1527 1527 if (*arg != '-') {
1528 1528 if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) {
1529 1529 newae(ctx->i_ae, arg);
1530 1530 } else {
1531 1531 in_output = 0;
1532 1532 newae(ctx->i_ae, ctx->i_discard);
1533 1533 }
1534 1534 continue;
1535 1535 }
1536 1536 switch (*(arg + 1)) {
1537 1537 case '_':
1538 1538 if (strcmp(arg, "-_noecho") == 0) {
1539 1539 ctx->i_flags &= ~CW_F_ECHO;
1540 1540 } else if (strncmp(arg, "-_cc=", 5) == 0 ||
1541 1541 strncmp(arg, "-_CC=", 5) == 0) {
1542 1542 newae(ctx->i_ae, arg + 5);
1543 1543 } else if (strncmp(arg, "-_gcc=", 6) != 0 &&
1544 1544 strncmp(arg, "-_g++=", 6) != 0) {
1545 1545 (void) fprintf(stderr,
1546 1546 "%s: invalid argument '%s'\n", progname,
1547 1547 arg);
1548 1548 exit(2);
1549 1549 }
1550 1550 break;
1551 1551 case 'V':
1552 1552 ctx->i_flags &= ~CW_F_ECHO;
1553 1553 newae(ctx->i_ae, arg);
1554 1554 break;
1555 1555 case 'o':
1556 1556 seen_o = 1;
1557 1557 if (strlen(arg) == 2) {
1558 1558 in_output = 1;
1559 1559 newae(ctx->i_ae, arg);
1560 1560 } else if (ctx->i_flags & CW_F_SHADOW) {
1561 1561 newae(ctx->i_ae, "-o");
1562 1562 newae(ctx->i_ae, ctx->i_discard);
1563 1563 } else {
1564 1564 newae(ctx->i_ae, arg);
1565 1565 }
1566 1566 break;
1567 1567 case 'c':
1568 1568 case 'S':
1569 1569 if (strlen(arg) == 2)
1570 1570 op = CW_O_COMPILE;
1571 1571 newae(ctx->i_ae, arg);
1572 1572 break;
1573 1573 case 'E':
1574 1574 case 'P':
1575 1575 if (strlen(arg) == 2)
1576 1576 op = CW_O_PREPROCESS;
1577 1577 /*FALLTHROUGH*/
1578 1578 default:
1579 1579 newae(ctx->i_ae, arg);
1580 1580 }
1581 1581 }
1582 1582
1583 1583 if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
1584 1584 (ctx->i_flags & CW_F_SHADOW))
1585 1585 exit(0);
1586 1586
1587 1587 if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1588 1588 newae(ctx->i_ae, "-o");
1589 1589 newae(ctx->i_ae, ctx->i_discard);
1590 1590 }
1591 1591 }
1592 1592
1593 1593 static void
1594 1594 prepctx(cw_ictx_t *ctx)
1595 1595 {
1596 1596 const char *dir = NULL, *cmd;
1597 1597 char *program = NULL;
1598 1598 size_t len;
1599 1599
1600 1600 switch (CIDX(CC(ctx), ctx->i_flags)) {
1601 1601 case CIDX(CW_C_CC, 0):
1602 1602 if ((program = getenv("CW_" CW_TARGET "_CC")) == NULL)
1603 1603 program = getenv("CW_CC");
1604 1604 if ((dir = getenv("CW_" CW_TARGET "_CC_DIR")) == NULL)
1605 1605 dir = getenv("CW_CC_DIR");
1606 1606 break;
1607 1607 case CIDX(CW_C_CC, CW_F_CXX):
1608 1608 if ((program = getenv("CW_" CW_TARGET "_CPLUSPLUS")) ==
1609 1609 NULL)
1610 1610 program = getenv("CW_CPLUSPLUS");
1611 1611 if ((dir = getenv("CW_" CW_TARGET "_CPLUSPLUS_DIR")) ==
1612 1612 NULL)
1613 1613 dir = getenv("CW_CPLUSPLUS_DIR");
1614 1614 break;
1615 1615 case CIDX(CW_C_GCC, 0):
1616 1616 if ((program = getenv("CW_" CW_TARGET "_GCC")) == NULL)
1617 1617 program = getenv("CW_GCC");
1618 1618 if ((dir = getenv("CW_" CW_TARGET "_GCC_DIR")) == NULL)
1619 1619 dir = getenv("CW_GCC_DIR");
1620 1620 break;
1621 1621 case CIDX(CW_C_GCC, CW_F_CXX):
1622 1622 if ((program = getenv("CW_" CW_TARGET "_GPLUSPLUS")) ==
1623 1623 NULL)
1624 1624 program = getenv("CW_GPLUSPLUS");
1625 1625 if ((dir = getenv("CW_" CW_TARGET "_GPLUSPLUS_DIR")) ==
1626 1626 NULL)
1627 1627 dir = getenv("CW_GPLUSPLUS_DIR");
1628 1628 break;
1629 1629 }
1630 1630
1631 1631 if (program == NULL) {
1632 1632 if (dir == NULL)
1633 1633 dir = default_dir[CC(ctx)];
1634 1634 cmd = cmds[CIDX(CC(ctx), ctx->i_flags)];
1635 1635 len = strlen(dir) + strlen(cmd) + 2;
1636 1636 if ((program = malloc(len)) == NULL)
1637 1637 nomem();
1638 1638 (void) snprintf(program, len, "%s/%s", dir, cmd);
1639 1639 }
1640 1640
1641 1641 newae(ctx->i_ae, program);
1642 1642
1643 1643 if (ctx->i_flags & CW_F_PROG) {
1644 1644 (void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ?
1645 1645 "shadow" : "primary", program);
1646 1646 (void) fflush(stdout);
1647 1647 }
1648 1648
1649 1649 if (!(ctx->i_flags & CW_F_XLATE))
1650 1650 return;
1651 1651
1652 1652 switch (CC(ctx)) {
1653 1653 case CW_C_CC:
1654 1654 do_cc(ctx);
1655 1655 break;
1656 1656 case CW_C_GCC:
1657 1657 do_gcc(ctx);
1658 1658 break;
1659 1659 }
1660 1660 }
1661 1661
1662 1662 static int
1663 1663 invoke(cw_ictx_t *ctx)
1664 1664 {
1665 1665 char **newargv;
1666 1666 int ac;
1667 1667 struct ae *a;
1668 1668
1669 1669 if ((newargv = calloc(sizeof (*newargv), ctx->i_ae->ael_argc + 1)) ==
1670 1670 NULL)
1671 1671 nomem();
1672 1672
1673 1673 if (ctx->i_flags & CW_F_ECHO)
1674 1674 (void) fprintf(stderr, "+ ");
1675 1675
1676 1676 for (ac = 0, a = ctx->i_ae->ael_head; a; a = a->ae_next, ac++) {
1677 1677 newargv[ac] = a->ae_arg;
1678 1678 if (ctx->i_flags & CW_F_ECHO)
1679 1679 (void) fprintf(stderr, "%s ", a->ae_arg);
1680 1680 if (a == ctx->i_ae->ael_tail)
1681 1681 break;
1682 1682 }
1683 1683
1684 1684 if (ctx->i_flags & CW_F_ECHO) {
1685 1685 (void) fprintf(stderr, "\n");
1686 1686 (void) fflush(stderr);
1687 1687 }
1688 1688
1689 1689 if (!(ctx->i_flags & CW_F_EXEC))
1690 1690 return (0);
1691 1691
1692 1692 /*
1693 1693 * We must fix up the environment here so that the
1694 1694 * dependency files are not trampled by the shadow compiler.
1695 1695 */
1696 1696 if ((ctx->i_flags & CW_F_SHADOW) &&
1697 1697 (unsetenv("SUNPRO_DEPENDENCIES") != 0 ||
1698 1698 unsetenv("DEPENDENCIES_OUTPUT") != 0)) {
1699 1699 (void) fprintf(stderr, "error: environment setup failed: %s\n",
1700 1700 strerror(errno));
1701 1701 return (-1);
1702 1702 }
1703 1703
1704 1704 (void) execv(newargv[0], newargv);
1705 1705 cw_perror("couldn't run %s", newargv[0]);
1706 1706
1707 1707 return (-1);
1708 1708 }
1709 1709
1710 1710 static int
1711 1711 reap(cw_ictx_t *ctx)
1712 1712 {
1713 1713 int status, ret = 0;
1714 1714 char buf[1024];
1715 1715 struct stat s;
1716 1716
1717 1717 /*
1718 1718 * Only wait for one specific child.
1719 1719 */
1720 1720 if (ctx->i_pid <= 0)
1721 1721 return (-1);
1722 1722
1723 1723 do {
1724 1724 if (waitpid(ctx->i_pid, &status, 0) < 0) {
1725 1725 cw_perror("cannot reap child");
1726 1726 return (-1);
1727 1727 }
1728 1728 if (status != 0) {
1729 1729 if (WIFSIGNALED(status)) {
1730 1730 ret = -WTERMSIG(status);
1731 1731 break;
1732 1732 } else if (WIFEXITED(status)) {
1733 1733 ret = WEXITSTATUS(status);
1734 1734 break;
1735 1735 }
1736 1736 }
1737 1737 } while (!WIFEXITED(status) && !WIFSIGNALED(status));
1738 1738
1739 1739 (void) unlink(ctx->i_discard);
1740 1740
1741 1741 if (stat(ctx->i_stderr, &s) < 0) {
1742 1742 cw_perror("stat failed on child cleanup");
1743 1743 return (-1);
1744 1744 }
1745 1745 if (s.st_size != 0) {
1746 1746 FILE *f;
1747 1747
1748 1748 if ((f = fopen(ctx->i_stderr, "r")) != NULL) {
1749 1749 while (fgets(buf, sizeof (buf), f))
1750 1750 (void) fprintf(stderr, "%s", buf);
1751 1751 (void) fflush(stderr);
1752 1752 (void) fclose(f);
1753 1753 }
1754 1754 }
1755 1755 (void) unlink(ctx->i_stderr);
1756 1756 free(ctx->i_stderr);
1757 1757
1758 1758 /*
1759 1759 * cc returns an error code when given -V; we want that to succeed.
1760 1760 */
1761 1761 if (ctx->i_flags & CW_F_PROG)
1762 1762 return (0);
1763 1763
1764 1764 return (ret);
1765 1765 }
1766 1766
1767 1767 static int
1768 1768 exec_ctx(cw_ictx_t *ctx, int block)
1769 1769 {
1770 1770 char *file;
1771 1771
1772 1772 /*
1773 1773 * To avoid offending cc's sensibilities, the name of its output
1774 1774 * file must end in '.o'.
1775 1775 */
1776 1776 if ((file = tempnam(NULL, ".cw")) == NULL) {
1777 1777 nomem();
1778 1778 return (-1);
1779 1779 }
1780 1780 (void) strlcpy(ctx->i_discard, file, MAXPATHLEN);
1781 1781 (void) strlcat(ctx->i_discard, ".o", MAXPATHLEN);
1782 1782 free(file);
1783 1783
1784 1784 if ((ctx->i_stderr = tempnam(NULL, ".cw")) == NULL) {
1785 1785 nomem();
1786 1786 return (-1);
1787 1787 }
1788 1788
1789 1789 if ((ctx->i_pid = fork()) == 0) {
1790 1790 int fd;
1791 1791
1792 1792 (void) fclose(stderr);
1793 1793 if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL,
1794 1794 0666)) < 0) {
1795 1795 cw_perror("open failed for standard error");
1796 1796 exit(1);
1797 1797 }
1798 1798 if (dup2(fd, 2) < 0) {
1799 1799 cw_perror("dup2 failed for standard error");
1800 1800 exit(1);
1801 1801 }
1802 1802 if (fd != 2)
1803 1803 (void) close(fd);
1804 1804 if (freopen("/dev/fd/2", "w", stderr) == NULL) {
1805 1805 cw_perror("freopen failed for /dev/fd/2");
1806 1806 exit(1);
1807 1807 }
1808 1808 prepctx(ctx);
1809 1809 exit(invoke(ctx));
1810 1810 }
1811 1811
1812 1812 if (ctx->i_pid < 0) {
1813 1813 cw_perror("fork failed");
1814 1814 return (1);
1815 1815 }
1816 1816
1817 1817 if (block)
1818 1818 return (reap(ctx));
1819 1819
1820 1820 return (0);
1821 1821 }
1822 1822
1823 1823 int
1824 1824 main(int argc, char **argv)
1825 1825 {
1826 1826 cw_ictx_t *ctx = newictx();
1827 1827 cw_ictx_t *ctx_shadow = newictx();
1828 1828 const char *dir;
1829 1829 int do_serial, do_shadow;
1830 1830 int ret = 0;
1831 1831
1832 1832 if ((progname = strrchr(argv[0], '/')) == NULL)
1833 1833 progname = argv[0];
1834 1834 else
1835 1835 progname++;
1836 1836
1837 1837 if (ctx == NULL || ctx_shadow == NULL)
1838 1838 nomem();
1839 1839
1840 1840 ctx->i_flags = CW_F_ECHO|CW_F_XLATE;
1841 1841
1842 1842 /*
1843 1843 * Figure out where to get our tools from. This depends on
1844 1844 * the environment variables set at run time.
1845 1845 */
1846 1846 if ((dir = getenv("SPRO_VROOT")) != NULL) {
1847 1847 (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
1848 1848 "%s/bin", dir);
1849 1849 } else if ((dir = getenv("SPRO_ROOT")) != NULL) {
1850 1850 (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
1851 1851 "%s/SS12/bin", dir);
1852 1852 } else if ((dir = getenv("BUILD_TOOLS")) != NULL) {
1853 1853 (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
1854 1854 "%s/SUNWspro/SS12/bin", dir);
1855 1855 }
1856 1856
1857 1857 if ((dir = getenv("GCC_ROOT")) != NULL) {
1858 1858 (void) snprintf(default_dir[CW_C_GCC], MAXPATHLEN,
1859 1859 "%s/bin", dir);
1860 1860 }
1861 1861
1862 1862 do_shadow = (getenv("CW_NO_SHADOW") ? 0 : 1);
1863 1863 do_serial = (getenv("CW_SHADOW_SERIAL") ? 1 : 0);
1864 1864
1865 1865 if (getenv("CW_NO_EXEC") == NULL)
1866 1866 ctx->i_flags |= CW_F_EXEC;
1867 1867
1868 1868 /*
1869 1869 * The first argument must be one of "-_cc", "-_gcc", "-_CC", or "-_g++"
1870 1870 */
1871 1871 if (argc == 1)
1872 1872 usage();
1873 1873 argc--;
1874 1874 argv++;
1875 1875 if (strcmp(argv[0], "-_cc") == 0) {
1876 1876 ctx->i_compiler = CW_C_CC;
1877 1877 } else if (strcmp(argv[0], "-_gcc") == 0) {
1878 1878 ctx->i_compiler = CW_C_GCC;
1879 1879 } else if (strcmp(argv[0], "-_CC") == 0) {
1880 1880 ctx->i_compiler = CW_C_CC;
1881 1881 ctx->i_flags |= CW_F_CXX;
1882 1882 } else if (strcmp(argv[0], "-_g++") == 0) {
1883 1883 ctx->i_compiler = CW_C_GCC;
1884 1884 ctx->i_flags |= CW_F_CXX;
1885 1885 } else {
1886 1886 /* assume "-_gcc" by default */
1887 1887 argc++;
1888 1888 argv--;
1889 1889 ctx->i_compiler = CW_C_GCC;
1890 1890 }
1891 1891
1892 1892 /*
1893 1893 * -_compiler - tell us the path to the primary compiler only
1894 1894 */
1895 1895 if (argc > 1 && strcmp(argv[1], "-_compiler") == 0) {
1896 1896 ctx->i_flags &= ~CW_F_XLATE;
1897 1897 prepctx(ctx);
1898 1898 (void) printf("%s\n", ctx->i_ae->ael_head->ae_arg);
1899 1899 return (0);
1900 1900 }
1901 1901
1902 1902 /*
1903 1903 * -_versions - tell us the cw version, paths to all compilers, and
1904 1904 * ask each for its version if we know how.
1905 1905 */
1906 1906 if (argc > 1 && strcmp(argv[1], "-_versions") == 0) {
1907 1907 (void) printf("cw version %s", CW_VERSION);
1908 1908 if (!do_shadow)
1909 1909 (void) printf(" (SHADOW MODE DISABLED)");
1910 1910 (void) printf("\n");
1911 1911 (void) fflush(stdout);
1912 1912 ctx->i_flags &= ~CW_F_ECHO;
1913 1913 ctx->i_flags |= CW_F_PROG|CW_F_EXEC;
1914 1914 argc--;
1915 1915 argv++;
1916 1916 do_serial = 1;
1917 1917 }
1918 1918
1919 1919 ctx->i_oldargc = argc;
1920 1920 ctx->i_oldargv = argv;
1921 1921
1922 1922 ret |= exec_ctx(ctx, do_serial);
1923 1923
1924 1924 if (do_shadow) {
1925 1925 (void) memcpy(ctx_shadow, ctx, sizeof (cw_ictx_t));
1926 1926 ctx_shadow->i_flags |= CW_F_SHADOW;
1927 1927 ret |= exec_ctx(ctx_shadow, 1);
1928 1928 }
1929 1929
1930 1930 if (!do_serial)
1931 1931 ret |= reap(ctx);
1932 1932
1933 1933 return (ret);
1934 1934 }
↓ open down ↓ |
1508 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX