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