Print this page
cw: give cw(1onbld) a new interface allowing for multiple arbitrary shadows
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 +#define COMPILER_STYLE(comp) (comp->style == SUN ? "cc" : "gcc")
339 +
340 +typedef struct {
341 + char *name;
342 + char *path;
343 + compiler_style_t style;
344 +} cw_compiler_t;
345 +
354 346 typedef struct cw_ictx {
355 - cw_compiler_t i_compiler;
347 + cw_compiler_t *i_compiler;
356 348 struct aelist *i_ae;
357 349 uint32_t i_flags;
358 350 int i_oldargc;
359 351 char **i_oldargv;
360 352 pid_t i_pid;
361 353 char i_discard[MAXPATHLEN];
362 354 char *i_stderr;
363 355 } cw_ictx_t;
364 356
365 357 /*
366 358 * Status values to indicate which Studio compiler and associated
367 359 * flags are being used.
368 360 */
369 361 #define M32 0x01 /* -m32 - only on Studio 12 */
370 362 #define M64 0x02 /* -m64 - only on Studio 12 */
371 363 #define SS11 0x100 /* Studio 11 */
372 364 #define SS12 0x200 /* Studio 12 */
373 365
374 366 #define TRANS_ENTRY 5
375 367 /*
376 368 * Translation table definition for the -xarch= flag. The "x_arg"
377 369 * value is translated into the appropriate gcc flags according
378 370 * to the values in x_trans[n]. The x_flags indicates what compiler
379 371 * is being used and what flags have been set via the use of
380 372 * "x_arg".
381 373 */
382 374 typedef struct xarch_table {
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
383 375 char *x_arg;
384 376 int x_flags;
385 377 char *x_trans[TRANS_ENTRY];
386 378 } xarch_table_t;
387 379
388 380 /*
389 381 * The translation table for the -xarch= flag used in the Studio compilers.
390 382 */
391 383 static const xarch_table_t xtbl[] = {
392 384 #if defined(__x86)
393 - { "generic", SS11 },
385 + { "generic", SS11, {NULL} },
394 386 { "generic64", (SS11|M64), { "-m64", "-mtune=opteron" } },
395 387 { "amd64", (SS11|M64), { "-m64", "-mtune=opteron" } },
396 388 { "386", SS11, { "-march=i386" } },
397 389 { "pentium_pro", SS11, { "-march=pentiumpro" } },
398 390 { "sse", SS11, { "-msse", "-mfpmath=sse" } },
399 391 { "sse2", SS11, { "-msse2", "-mfpmath=sse" } },
400 392 #elif defined(__sparc)
401 393 { "generic", (SS11|M32), { "-m32", "-mcpu=v8" } },
402 394 { "generic64", (SS11|M64), { "-m64", "-mcpu=v9" } },
403 395 { "v8", (SS11|M32), { "-m32", "-mcpu=v8", "-mno-v8plus" } },
404 396 { "v8plus", (SS11|M32), { "-m32", "-mcpu=v9", "-mv8plus" } },
405 397 { "v8plusa", (SS11|M32), { "-m32", "-mcpu=ultrasparc", "-mv8plus",
406 398 "-mvis" } },
407 399 { "v8plusb", (SS11|M32), { "-m32", "-mcpu=ultrasparc3", "-mv8plus",
408 400 "-mvis" } },
409 401 { "v9", (SS11|M64), { "-m64", "-mcpu=v9" } },
↓ open down ↓ |
6 lines elided |
↑ open up ↑ |
410 402 { "v9a", (SS11|M64), { "-m64", "-mcpu=ultrasparc", "-mvis" } },
411 403 { "v9b", (SS11|M64), { "-m64", "-mcpu=ultrasparc3", "-mvis" } },
412 404 { "sparc", SS12, { "-mcpu=v9", "-mv8plus" } },
413 405 { "sparcvis", SS12, { "-mcpu=ultrasparc", "-mvis" } },
414 406 { "sparcvis2", SS12, { "-mcpu=ultrasparc3", "-mvis" } }
415 407 #endif
416 408 };
417 409
418 410 static int xtbl_size = sizeof (xtbl) / sizeof (xarch_table_t);
419 411
420 -static const char *progname;
421 -
422 412 static const char *xchip_tbl[] = {
423 413 #if defined(__x86)
424 414 "386", "-mtune=i386", NULL,
425 415 "486", "-mtune=i486", NULL,
426 416 "pentium", "-mtune=pentium", NULL,
427 417 "pentium_pro", "-mtune=pentiumpro", NULL,
428 418 #elif defined(__sparc)
429 419 "super", "-mtune=supersparc", NULL,
430 420 "ultra", "-mtune=ultrasparc", NULL,
431 421 "ultra3", "-mtune=ultrasparc3", NULL,
432 422 #endif
433 423 NULL, NULL
434 424 };
435 425
436 426 static const char *xcode_tbl[] = {
437 427 #if defined(__sparc)
438 428 "abs32", "-fno-pic", "-mcmodel=medlow", NULL,
439 429 "abs44", "-fno-pic", "-mcmodel=medmid", NULL,
440 430 "abs64", "-fno-pic", "-mcmodel=medany", NULL,
441 431 "pic13", "-fpic", NULL,
442 432 "pic32", "-fPIC", NULL,
443 433 #endif
444 434 NULL, NULL
445 435 };
446 436
447 437 static const char *xtarget_tbl[] = {
448 438 #if defined(__x86)
449 439 "pentium_pro", "-march=pentiumpro", NULL,
450 440 #endif /* __x86 */
451 441 NULL, NULL
452 442 };
453 443
454 444 static const char *xregs_tbl[] = {
455 445 #if defined(__sparc)
456 446 "appl", "-mapp-regs", NULL,
↓ open down ↓ |
25 lines elided |
↑ open up ↑ |
457 447 "no%appl", "-mno-app-regs", NULL,
458 448 "float", "-mfpu", NULL,
459 449 "no%float", "-mno-fpu", NULL,
460 450 #endif /* __sparc */
461 451 NULL, NULL
462 452 };
463 453
464 454 static void
465 455 nomem(void)
466 456 {
467 - (void) fprintf(stderr, "%s: error: out of memory\n", progname);
468 - exit(1);
469 -}
470 -
471 -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));
457 + (void) errx(1, "out of memory");
484 458 }
485 459
486 460 static void
487 461 newae(struct aelist *ael, const char *arg)
488 462 {
489 463 struct ae *ae;
490 464
491 465 if ((ae = calloc(sizeof (*ae), 1)) == NULL)
492 466 nomem();
493 467 ae->ae_arg = strdup(arg);
494 468 if (ael->ael_tail == NULL)
495 469 ael->ael_head = ae;
496 470 else
497 471 ael->ael_tail->ae_next = ae;
498 472 ael->ael_tail = ae;
499 473 ael->ael_argc++;
500 474 }
501 475
502 476 static cw_ictx_t *
503 477 newictx(void)
504 478 {
505 479 cw_ictx_t *ctx = calloc(sizeof (cw_ictx_t), 1);
506 480 if (ctx)
507 481 if ((ctx->i_ae = calloc(sizeof (struct aelist), 1)) == NULL) {
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
508 482 free(ctx);
509 483 return (NULL);
510 484 }
511 485
512 486 return (ctx);
513 487 }
514 488
515 489 static void
516 490 error(const char *arg)
517 491 {
518 - (void) fprintf(stderr,
519 - "%s: error: mapping failed at or near arg '%s'\n", progname, arg);
520 - exit(2);
492 + errx(2, "error: mapping failed at or near arg '%s'\n", arg);
521 493 }
522 494
523 495 /*
524 496 * Add the current favourite set of warnings to the gcc invocation.
525 497 */
526 498 static void
527 499 warnings(struct aelist *h)
528 500 {
529 501 static int warningsonce;
530 502
531 503 if (warningsonce++)
532 504 return;
533 505
534 506 /*
535 507 * Enable as many warnings as exist, then disable those that we never
536 508 * ever want.
537 509 */
538 510 newae(h, "-Wall");
539 511 newae(h, "-Wextra");
540 512 }
541 513
542 514 static void
543 515 optim_disable(struct aelist *h, int level)
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
544 516 {
545 517 if (level >= 2) {
546 518 newae(h, "-fno-strict-aliasing");
547 519 newae(h, "-fno-unit-at-a-time");
548 520 newae(h, "-fno-optimize-sibling-calls");
549 521 }
550 522 }
551 523
552 524 /* ARGSUSED */
553 525 static void
554 -Xamode(struct aelist *h)
526 +Xamode(struct aelist *h __attribute__((__unused__)))
555 527 {
556 528 }
557 529
558 530 static void
559 531 Xcmode(struct aelist *h)
560 532 {
561 533 static int xconce;
562 534
563 535 if (xconce++)
564 536 return;
565 537
566 538 newae(h, "-ansi");
567 539 newae(h, "-pedantic-errors");
568 540 }
569 541
570 542 static void
571 543 Xsmode(struct aelist *h)
572 544 {
573 545 static int xsonce;
574 546
↓ open down ↓ |
10 lines elided |
↑ open up ↑ |
575 547 if (xsonce++)
576 548 return;
577 549
578 550 newae(h, "-traditional");
579 551 newae(h, "-traditional-cpp");
580 552 }
581 553
582 554 static void
583 555 usage()
584 556 {
557 + extern char *__progname;
585 558 (void) fprintf(stderr,
586 - "usage: %s { -_cc | -_gcc | -_CC | -_g++ } [ -_compiler | ... ]\n",
587 - progname);
559 + "usage: %s [-C] [--versions] --primary <compiler> "
560 + "[--shadow <compiler>]... -- cflags...\n",
561 + __progname);
562 + (void) fprintf(stderr, "compilers take the form: name,path,style\n"
563 + " - name: a unique name usable in flag specifiers\n"
564 + " - path: path to the compiler binary\n"
565 + " - style: the style of flags expected: either sun or gnu\n");
588 566 exit(2);
589 567 }
590 568
591 569 static int
592 570 xlate_xtb(struct aelist *h, const char *xarg)
593 571 {
594 572 int i, j;
595 573
596 574 for (i = 0; i < xtbl_size; i++) {
597 575 if (strcmp(xtbl[i].x_arg, xarg) == 0)
598 576 break;
599 577 }
600 578
601 579 /*
602 580 * At the end of the table and so no matching "arg" entry
603 581 * found and so this must be a bad -xarch= flag.
604 582 */
605 583 if (i == xtbl_size)
606 584 error(xarg);
607 585
608 586 for (j = 0; j < TRANS_ENTRY; j++) {
609 587 if (xtbl[i].x_trans[j] != NULL)
610 588 newae(h, xtbl[i].x_trans[j]);
611 589 else
612 590 break;
613 591 }
614 592 return (xtbl[i].x_flags);
615 593
616 594 }
617 595
618 596 static void
619 597 xlate(struct aelist *h, const char *xarg, const char **table)
620 598 {
621 599 while (*table != NULL && strcmp(xarg, *table) != 0) {
622 600 while (*table != NULL)
623 601 table++;
624 602 table++;
625 603 }
626 604
627 605 if (*table == NULL)
628 606 error(xarg);
629 607
630 608 table++;
631 609
632 610 while (*table != NULL) {
633 611 newae(h, *table);
634 612 table++;
635 613 }
↓ open down ↓ |
38 lines elided |
↑ open up ↑ |
636 614 }
637 615
638 616 static void
639 617 do_gcc(cw_ictx_t *ctx)
640 618 {
641 619 int c;
642 620 int pic = 0, nolibc = 0;
643 621 int in_output = 0, seen_o = 0, c_files = 0;
644 622 cw_op_t op = CW_O_LINK;
645 623 char *model = NULL;
624 + char *nameflag;
646 625 int mflag = 0;
647 626
648 627 if (ctx->i_flags & CW_F_PROG) {
649 628 newae(ctx->i_ae, "--version");
650 629 return;
651 630 }
652 631
653 632 newae(ctx->i_ae, "-fident");
654 633 newae(ctx->i_ae, "-finline");
655 634 newae(ctx->i_ae, "-fno-inline-functions");
656 635 newae(ctx->i_ae, "-fno-builtin");
657 636 newae(ctx->i_ae, "-fno-asm");
658 637 newae(ctx->i_ae, "-fdiagnostics-show-option");
659 638 newae(ctx->i_ae, "-nodefaultlibs");
660 639
661 640 #if defined(__sparc)
662 641 /*
663 642 * The SPARC ldd and std instructions require 8-byte alignment of
664 643 * their address operand. gcc correctly uses them only when the
665 644 * ABI requires 8-byte alignment; unfortunately we have a number of
666 645 * pieces of buggy code that doesn't conform to the ABI. This
667 646 * flag makes gcc work more like Studio with -xmemalign=4.
668 647 */
669 648 newae(ctx->i_ae, "-mno-integer-ldd-std");
670 649 #endif
671 650
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
672 651 /*
673 652 * This is needed because 'u' is defined
674 653 * under a conditional on 'sun'. Should
675 654 * probably just remove the conditional,
676 655 * or make it be dependent on '__sun'.
677 656 *
678 657 * -Dunix is also missing in enhanced ANSI mode
679 658 */
680 659 newae(ctx->i_ae, "-D__sun");
681 660
661 + if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->name) == -1)
662 + nomem();
663 +
682 664 /*
683 665 * Walk the argument list, translating as we go ..
684 666 */
685 -
686 667 while (--ctx->i_oldargc > 0) {
687 668 char *arg = *++ctx->i_oldargv;
688 669 size_t arglen = strlen(arg);
689 670
690 671 if (*arg == '-') {
691 672 arglen--;
692 673 } else {
693 674 /*
694 675 * Discard inline files that gcc doesn't grok
695 676 */
696 677 if (!in_output && arglen > 3 &&
697 678 strcmp(arg + arglen - 3, ".il") == 0)
698 679 continue;
699 680
700 681 if (!in_output && arglen > 2 &&
701 682 arg[arglen - 2] == '.' &&
702 683 (arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' ||
703 684 arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i'))
704 685 c_files++;
705 686
706 687 /*
707 688 * Otherwise, filenames and partial arguments
708 689 * are passed through for gcc to chew on. However,
709 690 * output is always discarded for the secondary
710 691 * compiler.
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
711 692 */
712 693 if ((ctx->i_flags & CW_F_SHADOW) && in_output)
713 694 newae(ctx->i_ae, ctx->i_discard);
714 695 else
715 696 newae(ctx->i_ae, arg);
716 697 in_output = 0;
717 698 continue;
718 699 }
719 700
720 701 if (ctx->i_flags & CW_F_CXX) {
702 + if (strncmp(arg, "-_g++=", 6) == 0) {
703 + newae(ctx->i_ae, strchr(arg, '=') + 1);
704 + continue;
705 + }
721 706 if (strncmp(arg, "-compat=", 8) == 0) {
722 707 /* discard -compat=4 and -compat=5 */
723 708 continue;
724 709 }
725 710 if (strcmp(arg, "-Qoption") == 0) {
726 711 /* discard -Qoption and its two arguments */
727 712 if (ctx->i_oldargc < 3)
728 713 error(arg);
729 714 ctx->i_oldargc -= 2;
730 715 ctx->i_oldargv += 2;
731 716 continue;
732 717 }
733 718 if (strcmp(arg, "-xwe") == 0) {
734 719 /* turn warnings into errors */
735 720 newae(ctx->i_ae, "-Werror");
736 721 continue;
737 722 }
738 723 if (strcmp(arg, "-noex") == 0) {
739 724 /* no exceptions */
740 725 newae(ctx->i_ae, "-fno-exceptions");
741 726 /* no run time type descriptor information */
742 727 newae(ctx->i_ae, "-fno-rtti");
743 728 continue;
744 729 }
745 730 if (strcmp(arg, "-pic") == 0) {
746 731 newae(ctx->i_ae, "-fpic");
747 732 pic = 1;
748 733 continue;
749 734 }
750 735 if (strcmp(arg, "-PIC") == 0) {
751 736 newae(ctx->i_ae, "-fPIC");
752 737 pic = 1;
753 738 continue;
754 739 }
755 740 if (strcmp(arg, "-norunpath") == 0) {
756 741 /* gcc has no corresponding option */
757 742 continue;
758 743 }
759 744 if (strcmp(arg, "-nolib") == 0) {
760 745 /* -nodefaultlibs is on by default */
761 746 nolibc = 1;
762 747 continue;
763 748 }
764 749 #if defined(__sparc)
↓ open down ↓ |
34 lines elided |
↑ open up ↑ |
765 750 if (strcmp(arg, "-cg92") == 0) {
766 751 mflag |= xlate_xtb(ctx->i_ae, "v8");
767 752 xlate(ctx->i_ae, "super", xchip_tbl);
768 753 continue;
769 754 }
770 755 #endif /* __sparc */
771 756 }
772 757
773 758 switch ((c = arg[1])) {
774 759 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);
760 + if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) ||
761 + (strncmp(arg, "-_gcc=", 6) == 0) ||
762 + (strncmp(arg, "-_gnu=", 6) == 0)) {
763 + newae(ctx->i_ae, strchr(arg, '=') + 1);
764 + }
785 765 break;
786 766 case '#':
787 767 if (arglen == 1) {
788 768 newae(ctx->i_ae, "-v");
789 769 break;
790 770 }
791 771 error(arg);
792 772 break;
793 773 case 'g':
794 774 newae(ctx->i_ae, "-gdwarf-2");
795 775 break;
796 776 case 'E':
797 777 if (arglen == 1) {
798 778 newae(ctx->i_ae, "-xc");
799 779 newae(ctx->i_ae, arg);
800 780 op = CW_O_PREPROCESS;
801 781 nolibc = 1;
802 782 break;
803 783 }
804 784 error(arg);
805 785 break;
806 786 case 'c':
807 787 case 'S':
808 788 if (arglen == 1) {
809 789 op = CW_O_COMPILE;
810 790 nolibc = 1;
811 791 }
812 792 /* FALLTHROUGH */
813 793 case 'C':
814 794 case 'H':
815 795 case 'p':
816 796 if (arglen == 1) {
817 797 newae(ctx->i_ae, arg);
818 798 break;
819 799 }
820 800 error(arg);
821 801 break;
822 802 case 'A':
823 803 case 'h':
824 804 case 'I':
825 805 case 'i':
826 806 case 'L':
827 807 case 'l':
828 808 case 'R':
829 809 case 'U':
830 810 case 'u':
831 811 case 'w':
832 812 newae(ctx->i_ae, arg);
833 813 break;
834 814 case 'o':
835 815 seen_o = 1;
836 816 if (arglen == 1) {
837 817 in_output = 1;
838 818 newae(ctx->i_ae, arg);
839 819 } else if (ctx->i_flags & CW_F_SHADOW) {
840 820 newae(ctx->i_ae, "-o");
841 821 newae(ctx->i_ae, ctx->i_discard);
842 822 } else {
843 823 newae(ctx->i_ae, arg);
844 824 }
845 825 break;
846 826 case 'D':
847 827 newae(ctx->i_ae, arg);
848 828 /*
849 829 * XXX Clearly a hack ... do we need _KADB too?
850 830 */
851 831 if (strcmp(arg, "-D_KERNEL") == 0 ||
852 832 strcmp(arg, "-D_BOOT") == 0)
853 833 newae(ctx->i_ae, "-ffreestanding");
854 834 break;
855 835 case 'd':
856 836 if (arglen == 2) {
857 837 if (strcmp(arg, "-dy") == 0) {
858 838 newae(ctx->i_ae, "-Wl,-dy");
859 839 break;
860 840 }
861 841 if (strcmp(arg, "-dn") == 0) {
862 842 newae(ctx->i_ae, "-Wl,-dn");
863 843 break;
864 844 }
865 845 }
866 846 if (strcmp(arg, "-dalign") == 0) {
867 847 /*
868 848 * -dalign forces alignment in some cases;
869 849 * gcc does not need any flag to do this.
870 850 */
871 851 break;
872 852 }
873 853 error(arg);
874 854 break;
875 855 case 'e':
876 856 if (strcmp(arg,
877 857 "-erroff=E_EMPTY_TRANSLATION_UNIT") == 0) {
878 858 /*
879 859 * Accept but ignore this -- gcc doesn't
880 860 * seem to complain about empty translation
881 861 * units
882 862 */
883 863 break;
884 864 }
885 865 /* XX64 -- ignore all -erroff= options, for now */
886 866 if (strncmp(arg, "-erroff=", 8) == 0)
887 867 break;
888 868 if (strcmp(arg, "-errtags=yes") == 0) {
889 869 warnings(ctx->i_ae);
890 870 break;
891 871 }
892 872 if (strcmp(arg, "-errwarn=%all") == 0) {
893 873 newae(ctx->i_ae, "-Werror");
894 874 break;
895 875 }
896 876 error(arg);
897 877 break;
898 878 case 'f':
899 879 if (strcmp(arg, "-flags") == 0) {
900 880 newae(ctx->i_ae, "--help");
901 881 break;
902 882 }
903 883 if (strncmp(arg, "-features=zla", 13) == 0) {
904 884 /*
905 885 * Accept but ignore this -- gcc allows
906 886 * zero length arrays.
907 887 */
908 888 break;
909 889 }
910 890 error(arg);
911 891 break;
912 892 case 'G':
913 893 newae(ctx->i_ae, "-shared");
914 894 nolibc = 1;
915 895 break;
916 896 case 'k':
917 897 if (strcmp(arg, "-keeptmp") == 0) {
918 898 newae(ctx->i_ae, "-save-temps");
919 899 break;
920 900 }
921 901 error(arg);
922 902 break;
923 903 case 'K':
924 904 if (arglen == 1) {
925 905 if ((arg = *++ctx->i_oldargv) == NULL ||
926 906 *arg == '\0')
927 907 error("-K");
928 908 ctx->i_oldargc--;
929 909 } else {
930 910 arg += 2;
931 911 }
932 912 if (strcmp(arg, "pic") == 0) {
933 913 newae(ctx->i_ae, "-fpic");
934 914 pic = 1;
935 915 break;
936 916 }
937 917 if (strcmp(arg, "PIC") == 0) {
938 918 newae(ctx->i_ae, "-fPIC");
939 919 pic = 1;
940 920 break;
941 921 }
942 922 error("-K");
943 923 break;
944 924 case 'm':
945 925 if (strcmp(arg, "-mt") == 0) {
946 926 newae(ctx->i_ae, "-D_REENTRANT");
947 927 break;
948 928 }
949 929 if (strcmp(arg, "-m64") == 0) {
950 930 newae(ctx->i_ae, "-m64");
951 931 #if defined(__x86)
952 932 newae(ctx->i_ae, "-mtune=opteron");
953 933 #endif
954 934 mflag |= M64;
955 935 break;
956 936 }
957 937 if (strcmp(arg, "-m32") == 0) {
958 938 newae(ctx->i_ae, "-m32");
959 939 mflag |= M32;
960 940 break;
961 941 }
962 942 error(arg);
963 943 break;
964 944 case 'B': /* linker options */
965 945 case 'M':
966 946 case 'z':
967 947 {
968 948 char *opt;
969 949 size_t len;
970 950 char *s;
971 951
972 952 if (arglen == 1) {
973 953 opt = *++ctx->i_oldargv;
974 954 if (opt == NULL || *opt == '\0')
975 955 error(arg);
976 956 ctx->i_oldargc--;
977 957 } else {
978 958 opt = arg + 2;
979 959 }
980 960 len = strlen(opt) + 7;
981 961 if ((s = malloc(len)) == NULL)
982 962 nomem();
983 963 (void) snprintf(s, len, "-Wl,-%c%s", c, opt);
984 964 newae(ctx->i_ae, s);
985 965 free(s);
986 966 }
987 967 break;
988 968 case 'n':
989 969 if (strcmp(arg, "-noqueue") == 0) {
990 970 /*
991 971 * Horrid license server stuff - n/a
992 972 */
993 973 break;
994 974 }
995 975 error(arg);
996 976 break;
997 977 case 'O':
998 978 if (arglen == 1) {
999 979 newae(ctx->i_ae, "-O");
1000 980 break;
1001 981 }
1002 982 error(arg);
1003 983 break;
1004 984 case 'P':
1005 985 /*
1006 986 * We could do '-E -o filename.i', but that's hard,
1007 987 * and we don't need it for the case that's triggering
1008 988 * this addition. We'll require the user to specify
1009 989 * -o in the Makefile. If they don't they'll find out
1010 990 * in a hurry.
1011 991 */
1012 992 newae(ctx->i_ae, "-E");
1013 993 op = CW_O_PREPROCESS;
1014 994 nolibc = 1;
1015 995 break;
1016 996 case 'q':
1017 997 if (strcmp(arg, "-qp") == 0) {
1018 998 newae(ctx->i_ae, "-p");
1019 999 break;
1020 1000 }
1021 1001 error(arg);
1022 1002 break;
1023 1003 case 's':
1024 1004 if (arglen == 1) {
1025 1005 newae(ctx->i_ae, "-Wl,-s");
1026 1006 break;
1027 1007 }
1028 1008 error(arg);
1029 1009 break;
1030 1010 case 't':
1031 1011 if (arglen == 1) {
1032 1012 newae(ctx->i_ae, "-Wl,-t");
1033 1013 break;
1034 1014 }
1035 1015 error(arg);
1036 1016 break;
1037 1017 case 'V':
1038 1018 if (arglen == 1) {
1039 1019 ctx->i_flags &= ~CW_F_ECHO;
1040 1020 newae(ctx->i_ae, "--version");
1041 1021 break;
1042 1022 }
1043 1023 error(arg);
1044 1024 break;
1045 1025 case 'v':
1046 1026 if (arglen == 1) {
1047 1027 warnings(ctx->i_ae);
1048 1028 break;
1049 1029 }
1050 1030 error(arg);
1051 1031 break;
1052 1032 case 'W':
1053 1033 if (strncmp(arg, "-Wp,-xc99", 9) == 0) {
1054 1034 /*
1055 1035 * gcc's preprocessor will accept c99
1056 1036 * regardless, so accept and ignore.
1057 1037 */
1058 1038 break;
1059 1039 }
1060 1040 if (strncmp(arg, "-Wa,", 4) == 0 ||
1061 1041 strncmp(arg, "-Wp,", 4) == 0 ||
1062 1042 strncmp(arg, "-Wl,", 4) == 0) {
1063 1043 newae(ctx->i_ae, arg);
1064 1044 break;
1065 1045 }
1066 1046 if (strcmp(arg, "-W0,-xc99=pragma") == 0) {
1067 1047 /* (undocumented) enables _Pragma */
1068 1048 break;
1069 1049 }
1070 1050 if (strcmp(arg, "-W0,-xc99=%none") == 0) {
1071 1051 /*
1072 1052 * This is a polite way of saying
1073 1053 * "no c99 constructs allowed!"
1074 1054 * For now, just accept and ignore this.
1075 1055 */
1076 1056 break;
1077 1057 }
1078 1058 if (strcmp(arg, "-W0,-noglobal") == 0 ||
1079 1059 strcmp(arg, "-W0,-xglobalstatic") == 0) {
1080 1060 /*
1081 1061 * gcc doesn't prefix local symbols
1082 1062 * in debug mode, so this is not needed.
1083 1063 */
1084 1064 break;
1085 1065 }
1086 1066 if (strcmp(arg, "-W0,-Lt") == 0) {
1087 1067 /*
1088 1068 * Generate tests at the top of loops.
1089 1069 * There is no direct gcc equivalent, ignore.
1090 1070 */
1091 1071 break;
1092 1072 }
1093 1073 if (strcmp(arg, "-W0,-xdbggen=no%usedonly") == 0) {
1094 1074 newae(ctx->i_ae,
1095 1075 "-fno-eliminate-unused-debug-symbols");
1096 1076 newae(ctx->i_ae,
1097 1077 "-fno-eliminate-unused-debug-types");
1098 1078 break;
1099 1079 }
1100 1080 if (strcmp(arg, "-W2,-xwrap_int") == 0) {
1101 1081 /*
1102 1082 * Use the legacy behaviour (pre-SS11)
1103 1083 * for integer wrapping.
1104 1084 * gcc does not need this.
1105 1085 */
1106 1086 break;
1107 1087 }
1108 1088 if (strcmp(arg, "-W2,-Rcond_elim") == 0) {
1109 1089 /*
1110 1090 * Elimination and expansion of conditionals;
1111 1091 * gcc has no direct equivalent.
1112 1092 */
1113 1093 break;
1114 1094 }
1115 1095 if (strcmp(arg, "-Wd,-xsafe=unboundsym") == 0) {
1116 1096 /*
1117 1097 * Prevents optimizing away checks for
1118 1098 * unbound weak symbol addresses. gcc does
1119 1099 * not do this, so it's not needed.
1120 1100 */
1121 1101 break;
1122 1102 }
1123 1103 if (strncmp(arg, "-Wc,-xcode=", 11) == 0) {
1124 1104 xlate(ctx->i_ae, arg + 11, xcode_tbl);
1125 1105 if (strncmp(arg + 11, "pic", 3) == 0)
1126 1106 pic = 1;
1127 1107 break;
1128 1108 }
1129 1109 if (strncmp(arg, "-Wc,-Qiselect", 13) == 0) {
1130 1110 /*
1131 1111 * Prevents insertion of register symbols.
1132 1112 * gcc doesn't do this, so ignore it.
1133 1113 */
1134 1114 break;
1135 1115 }
1136 1116 if (strcmp(arg, "-Wc,-Qassembler-ounrefsym=0") == 0) {
1137 1117 /*
1138 1118 * Prevents optimizing away of static variables.
1139 1119 * gcc does not do this, so it's not needed.
1140 1120 */
1141 1121 break;
1142 1122 }
1143 1123 #if defined(__x86)
1144 1124 if (strcmp(arg, "-Wu,-xmodel=kernel") == 0) {
1145 1125 newae(ctx->i_ae, "-ffreestanding");
1146 1126 newae(ctx->i_ae, "-mno-red-zone");
1147 1127 model = "-mcmodel=kernel";
1148 1128 nolibc = 1;
1149 1129 break;
1150 1130 }
1151 1131 if (strcmp(arg, "-Wu,-save_args") == 0) {
1152 1132 newae(ctx->i_ae, "-msave-args");
1153 1133 break;
1154 1134 }
1155 1135 #endif /* __x86 */
1156 1136 error(arg);
1157 1137 break;
1158 1138 case 'X':
1159 1139 if (strcmp(arg, "-Xa") == 0 ||
1160 1140 strcmp(arg, "-Xt") == 0) {
1161 1141 Xamode(ctx->i_ae);
1162 1142 break;
1163 1143 }
1164 1144 if (strcmp(arg, "-Xc") == 0) {
1165 1145 Xcmode(ctx->i_ae);
1166 1146 break;
1167 1147 }
1168 1148 if (strcmp(arg, "-Xs") == 0) {
1169 1149 Xsmode(ctx->i_ae);
1170 1150 break;
1171 1151 }
1172 1152 error(arg);
1173 1153 break;
1174 1154 case 'x':
1175 1155 if (arglen == 1)
1176 1156 error(arg);
1177 1157 switch (arg[2]) {
1178 1158 #if defined(__x86)
1179 1159 case '3':
1180 1160 if (strcmp(arg, "-x386") == 0) {
1181 1161 newae(ctx->i_ae, "-march=i386");
1182 1162 break;
1183 1163 }
1184 1164 error(arg);
1185 1165 break;
1186 1166 case '4':
1187 1167 if (strcmp(arg, "-x486") == 0) {
1188 1168 newae(ctx->i_ae, "-march=i486");
1189 1169 break;
1190 1170 }
1191 1171 error(arg);
1192 1172 break;
1193 1173 #endif /* __x86 */
1194 1174 case 'a':
1195 1175 if (strncmp(arg, "-xarch=", 7) == 0) {
1196 1176 mflag |= xlate_xtb(ctx->i_ae, arg + 7);
1197 1177 break;
1198 1178 }
1199 1179 error(arg);
1200 1180 break;
1201 1181 case 'b':
1202 1182 if (strncmp(arg, "-xbuiltin=", 10) == 0) {
1203 1183 if (strcmp(arg + 10, "%all"))
1204 1184 newae(ctx->i_ae, "-fbuiltin");
1205 1185 break;
1206 1186 }
1207 1187 error(arg);
1208 1188 break;
1209 1189 case 'C':
1210 1190 /* Accept C++ style comments -- ignore */
1211 1191 if (strcmp(arg, "-xCC") == 0)
1212 1192 break;
1213 1193 error(arg);
1214 1194 break;
1215 1195 case 'c':
1216 1196 if (strncmp(arg, "-xc99=%all", 10) == 0) {
1217 1197 newae(ctx->i_ae, "-std=gnu99");
1218 1198 break;
1219 1199 }
1220 1200 if (strncmp(arg, "-xc99=%none", 11) == 0) {
1221 1201 newae(ctx->i_ae, "-std=gnu89");
1222 1202 break;
1223 1203 }
1224 1204 if (strncmp(arg, "-xchip=", 7) == 0) {
1225 1205 xlate(ctx->i_ae, arg + 7, xchip_tbl);
1226 1206 break;
1227 1207 }
1228 1208 if (strncmp(arg, "-xcode=", 7) == 0) {
1229 1209 xlate(ctx->i_ae, arg + 7, xcode_tbl);
1230 1210 if (strncmp(arg + 7, "pic", 3) == 0)
1231 1211 pic = 1;
1232 1212 break;
1233 1213 }
1234 1214 if (strncmp(arg, "-xcache=", 8) == 0)
1235 1215 break;
1236 1216 if (strncmp(arg, "-xcrossfile", 11) == 0)
1237 1217 break;
1238 1218 error(arg);
1239 1219 break;
1240 1220 case 'd':
1241 1221 if (strcmp(arg, "-xdepend") == 0)
1242 1222 break;
1243 1223 if (strncmp(arg, "-xdebugformat=", 14) == 0)
1244 1224 break;
1245 1225 error(arg);
1246 1226 break;
1247 1227 case 'F':
1248 1228 /*
1249 1229 * Compile for mapfile reordering, or unused
1250 1230 * section elimination, syntax can be -xF or
1251 1231 * more complex, like -xF=%all -- ignore.
1252 1232 */
1253 1233 if (strncmp(arg, "-xF", 3) == 0)
1254 1234 break;
1255 1235 error(arg);
1256 1236 break;
1257 1237 case 'i':
1258 1238 if (strncmp(arg, "-xinline", 8) == 0)
1259 1239 /* No inlining; ignore */
1260 1240 break;
1261 1241 if (strcmp(arg, "-xildon") == 0 ||
1262 1242 strcmp(arg, "-xildoff") == 0)
1263 1243 /* No incremental linking; ignore */
1264 1244 break;
1265 1245 error(arg);
1266 1246 break;
1267 1247 #if defined(__x86)
1268 1248 case 'm':
1269 1249 if (strcmp(arg, "-xmodel=kernel") == 0) {
1270 1250 newae(ctx->i_ae, "-ffreestanding");
1271 1251 newae(ctx->i_ae, "-mno-red-zone");
1272 1252 model = "-mcmodel=kernel";
1273 1253 nolibc = 1;
1274 1254 break;
1275 1255 }
1276 1256 error(arg);
1277 1257 break;
1278 1258 #endif /* __x86 */
1279 1259 case 'M':
1280 1260 if (strcmp(arg, "-xM") == 0) {
1281 1261 newae(ctx->i_ae, "-M");
1282 1262 break;
1283 1263 }
1284 1264 if (strcmp(arg, "-xM1") == 0) {
1285 1265 newae(ctx->i_ae, "-MM");
1286 1266 break;
1287 1267 }
1288 1268 error(arg);
1289 1269 break;
1290 1270 case 'n':
1291 1271 if (strcmp(arg, "-xnolib") == 0) {
1292 1272 nolibc = 1;
1293 1273 break;
1294 1274 }
1295 1275 error(arg);
1296 1276 break;
1297 1277 case 'O':
1298 1278 if (strncmp(arg, "-xO", 3) == 0) {
1299 1279 size_t len = strlen(arg);
1300 1280 char *s;
1301 1281 int c = *(arg + 3);
1302 1282 int level;
1303 1283
1304 1284 if (len != 4 || !isdigit(c))
1305 1285 error(arg);
1306 1286
1307 1287 if ((s = malloc(len)) == NULL)
1308 1288 nomem();
1309 1289
1310 1290 level = atoi(arg + 3);
1311 1291 if (level > 5)
1312 1292 error(arg);
1313 1293 if (level >= 2) {
1314 1294 /*
1315 1295 * For gcc-3.4.x at -O2 we
1316 1296 * need to disable optimizations
1317 1297 * that break ON.
1318 1298 */
1319 1299 optim_disable(ctx->i_ae, level);
1320 1300 /*
1321 1301 * limit -xO3 to -O2 as well.
1322 1302 */
1323 1303 level = 2;
1324 1304 }
1325 1305 (void) snprintf(s, len, "-O%d", level);
1326 1306 newae(ctx->i_ae, s);
1327 1307 free(s);
1328 1308 break;
1329 1309 }
1330 1310 error(arg);
1331 1311 break;
1332 1312 case 'p':
1333 1313 if (strcmp(arg, "-xpentium") == 0) {
1334 1314 newae(ctx->i_ae, "-march=pentium");
1335 1315 break;
1336 1316 }
1337 1317 if (strcmp(arg, "-xpg") == 0) {
1338 1318 newae(ctx->i_ae, "-pg");
1339 1319 break;
1340 1320 }
1341 1321 error(arg);
1342 1322 break;
1343 1323 case 'r':
1344 1324 if (strncmp(arg, "-xregs=", 7) == 0) {
1345 1325 xlate(ctx->i_ae, arg + 7, xregs_tbl);
1346 1326 break;
1347 1327 }
1348 1328 error(arg);
1349 1329 break;
1350 1330 case 's':
1351 1331 if (strcmp(arg, "-xs") == 0 ||
1352 1332 strcmp(arg, "-xspace") == 0 ||
1353 1333 strcmp(arg, "-xstrconst") == 0)
1354 1334 break;
1355 1335 error(arg);
1356 1336 break;
1357 1337 case 't':
1358 1338 if (strcmp(arg, "-xtransition") == 0) {
1359 1339 newae(ctx->i_ae, "-Wtransition");
1360 1340 break;
1361 1341 }
1362 1342 if (strcmp(arg, "-xtrigraphs=yes") == 0) {
1363 1343 newae(ctx->i_ae, "-trigraphs");
1364 1344 break;
1365 1345 }
1366 1346 if (strcmp(arg, "-xtrigraphs=no") == 0) {
1367 1347 newae(ctx->i_ae, "-notrigraphs");
1368 1348 break;
1369 1349 }
1370 1350 if (strncmp(arg, "-xtarget=", 9) == 0) {
1371 1351 xlate(ctx->i_ae, arg + 9, xtarget_tbl);
1372 1352 break;
1373 1353 }
1374 1354 error(arg);
1375 1355 break;
1376 1356 case 'e':
1377 1357 case 'h':
1378 1358 case 'l':
1379 1359 default:
1380 1360 error(arg);
1381 1361 break;
1382 1362 }
1383 1363 break;
1384 1364 case 'Y':
1385 1365 if (arglen == 1) {
1386 1366 if ((arg = *++ctx->i_oldargv) == NULL ||
1387 1367 *arg == '\0')
1388 1368 error("-Y");
1389 1369 ctx->i_oldargc--;
1390 1370 arglen = strlen(arg + 1);
1391 1371 } else {
1392 1372 arg += 2;
1393 1373 }
1394 1374 /* Just ignore -YS,... for now */
1395 1375 if (strncmp(arg, "S,", 2) == 0)
1396 1376 break;
1397 1377 if (strncmp(arg, "l,", 2) == 0) {
1398 1378 char *s = strdup(arg);
1399 1379 s[0] = '-';
1400 1380 s[1] = 'B';
1401 1381 newae(ctx->i_ae, s);
1402 1382 free(s);
1403 1383 break;
1404 1384 }
1405 1385 if (strncmp(arg, "I,", 2) == 0) {
1406 1386 char *s = strdup(arg);
1407 1387 s[0] = '-';
1408 1388 s[1] = 'I';
1409 1389 newae(ctx->i_ae, "-nostdinc");
1410 1390 newae(ctx->i_ae, s);
1411 1391 free(s);
1412 1392 break;
1413 1393 }
1414 1394 error(arg);
1415 1395 break;
↓ open down ↓ |
621 lines elided |
↑ open up ↑ |
1416 1396 case 'Q':
1417 1397 /*
1418 1398 * We could map -Qy into -Wl,-Qy etc.
1419 1399 */
1420 1400 default:
1421 1401 error(arg);
1422 1402 break;
1423 1403 }
1424 1404 }
1425 1405
1406 + free(nameflag);
1407 +
1426 1408 if (c_files > 1 && (ctx->i_flags & CW_F_SHADOW) &&
1427 1409 op != CW_O_PREPROCESS) {
1428 - (void) fprintf(stderr, "%s: error: multiple source files are "
1429 - "allowed only with -E or -P\n", progname);
1430 - exit(2);
1410 + (void) errx(2, "multiple source files are "
1411 + "allowed only with -E or -P");
1431 1412 }
1432 1413
1433 1414 /*
1434 1415 * Make sure that we do not have any unintended interactions between
1435 1416 * the xarch options passed in and the version of the Studio compiler
1436 1417 * used.
1437 1418 */
1438 1419 if ((mflag & (SS11|SS12)) == (SS11|SS12)) {
1439 - (void) fprintf(stderr,
1420 + (void) errx(2,
1440 1421 "Conflicting \"-xarch=\" flags (both Studio 11 and 12)\n");
1441 - exit(2);
1442 1422 }
1443 1423
1444 1424 switch (mflag) {
1445 1425 case 0:
1446 1426 /* FALLTHROUGH */
1447 1427 case M32:
1448 1428 #if defined(__sparc)
1449 1429 /*
1450 1430 * Only -m32 is defined and so put in the missing xarch
1451 1431 * translation.
1452 1432 */
1453 1433 newae(ctx->i_ae, "-mcpu=v8");
1454 1434 newae(ctx->i_ae, "-mno-v8plus");
1455 1435 #endif
1456 1436 break;
1457 1437 case M64:
1458 1438 #if defined(__sparc)
1459 1439 /*
1460 1440 * Only -m64 is defined and so put in the missing xarch
1461 1441 * translation.
1462 1442 */
1463 1443 newae(ctx->i_ae, "-mcpu=v9");
1464 1444 #endif
1465 1445 break;
1466 1446 case SS12:
1467 1447 #if defined(__sparc)
1468 1448 /* no -m32/-m64 flag used - this is an error for sparc builds */
1469 1449 (void) fprintf(stderr, "No -m32/-m64 flag defined\n");
1470 1450 exit(2);
1471 1451 #endif
1472 1452 break;
1473 1453 case SS11:
1474 1454 /* FALLTHROUGH */
1475 1455 case (SS11|M32):
1476 1456 case (SS11|M64):
1477 1457 break;
1478 1458 case (SS12|M32):
1479 1459 #if defined(__sparc)
1480 1460 /*
1481 1461 * Need to add in further 32 bit options because with SS12
1482 1462 * the xarch=sparcvis option can be applied to 32 or 64
1483 1463 * bit, and so the translatation table (xtbl) cannot handle
1484 1464 * that.
1485 1465 */
1486 1466 newae(ctx->i_ae, "-mv8plus");
1487 1467 #endif
1488 1468 break;
1489 1469 case (SS12|M64):
1490 1470 break;
1491 1471 default:
1492 1472 (void) fprintf(stderr,
1493 1473 "Incompatible -xarch= and/or -m32/-m64 options used.\n");
1494 1474 exit(2);
1495 1475 }
1496 1476 if (op == CW_O_LINK && (ctx->i_flags & CW_F_SHADOW))
1497 1477 exit(0);
1498 1478
1499 1479 if (model && !pic)
1500 1480 newae(ctx->i_ae, model);
1501 1481 if (!nolibc)
1502 1482 newae(ctx->i_ae, "-lc");
1503 1483 if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
↓ open down ↓ |
52 lines elided |
↑ open up ↑ |
1504 1484 newae(ctx->i_ae, "-o");
1505 1485 newae(ctx->i_ae, ctx->i_discard);
1506 1486 }
1507 1487 }
1508 1488
1509 1489 static void
1510 1490 do_cc(cw_ictx_t *ctx)
1511 1491 {
1512 1492 int in_output = 0, seen_o = 0;
1513 1493 cw_op_t op = CW_O_LINK;
1494 + char *nameflag;
1514 1495
1515 1496 if (ctx->i_flags & CW_F_PROG) {
1516 1497 newae(ctx->i_ae, "-V");
1517 1498 return;
1518 1499 }
1519 1500
1501 + if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->name) == -1)
1502 + nomem();
1503 +
1520 1504 while (--ctx->i_oldargc > 0) {
1521 1505 char *arg = *++ctx->i_oldargv;
1522 1506
1507 + if (strncmp(arg, "-_CC=", 5) == 0) {
1508 + newae(ctx->i_ae, strchr(arg, '=') + 1);
1509 + continue;
1510 + }
1511 +
1523 1512 if (*arg != '-') {
1524 1513 if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) {
1525 1514 newae(ctx->i_ae, arg);
1526 1515 } else {
1527 1516 in_output = 0;
1528 1517 newae(ctx->i_ae, ctx->i_discard);
1529 1518 }
1530 1519 continue;
1531 1520 }
1532 1521 switch (*(arg + 1)) {
1533 1522 case '_':
1534 - if (strcmp(arg, "-_noecho") == 0) {
1535 - ctx->i_flags &= ~CW_F_ECHO;
1536 - } else if (strncmp(arg, "-_cc=", 5) == 0 ||
1537 - strncmp(arg, "-_CC=", 5) == 0) {
1538 - newae(ctx->i_ae, arg + 5);
1539 - } else if (strncmp(arg, "-_gcc=", 6) != 0 &&
1540 - strncmp(arg, "-_g++=", 6) != 0) {
1541 - (void) fprintf(stderr,
1542 - "%s: invalid argument '%s'\n", progname,
1543 - arg);
1544 - exit(2);
1523 + if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) ||
1524 + (strncmp(arg, "-_cc=", 5) == 0) ||
1525 + (strncmp(arg, "-_sun=", 6) == 0)) {
1526 + newae(ctx->i_ae, strchr(arg, '=') + 1);
1545 1527 }
1546 1528 break;
1529 +
1547 1530 case 'V':
1548 1531 ctx->i_flags &= ~CW_F_ECHO;
1549 1532 newae(ctx->i_ae, arg);
1550 1533 break;
1551 1534 case 'o':
1552 1535 seen_o = 1;
1553 1536 if (strlen(arg) == 2) {
1554 1537 in_output = 1;
1555 1538 newae(ctx->i_ae, arg);
1556 1539 } else if (ctx->i_flags & CW_F_SHADOW) {
1557 1540 newae(ctx->i_ae, "-o");
1558 1541 newae(ctx->i_ae, ctx->i_discard);
1559 1542 } else {
1560 1543 newae(ctx->i_ae, arg);
1561 1544 }
1562 1545 break;
1563 1546 case 'c':
1564 1547 case 'S':
1565 1548 if (strlen(arg) == 2)
1566 1549 op = CW_O_COMPILE;
1567 1550 newae(ctx->i_ae, arg);
1568 1551 break;
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
1569 1552 case 'E':
1570 1553 case 'P':
1571 1554 if (strlen(arg) == 2)
1572 1555 op = CW_O_PREPROCESS;
1573 1556 /*FALLTHROUGH*/
1574 1557 default:
1575 1558 newae(ctx->i_ae, arg);
1576 1559 }
1577 1560 }
1578 1561
1562 + free(nameflag);
1563 +
1579 1564 if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
1580 1565 (ctx->i_flags & CW_F_SHADOW))
1581 1566 exit(0);
1582 1567
1583 1568 if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1584 1569 newae(ctx->i_ae, "-o");
1585 1570 newae(ctx->i_ae, ctx->i_discard);
1586 1571 }
1587 1572 }
1588 1573
1589 1574 static void
1590 1575 prepctx(cw_ictx_t *ctx)
1591 1576 {
1592 - const char *dir = NULL, *cmd;
1593 - char *program = NULL;
1594 - size_t len;
1595 -
1596 - switch (CIDX(CC(ctx), ctx->i_flags)) {
1597 - case CIDX(CW_C_CC, 0):
1598 - program = getenv("CW_CC");
1599 - dir = getenv("CW_CC_DIR");
1600 - break;
1601 - case CIDX(CW_C_CC, CW_F_CXX):
1602 - program = getenv("CW_CPLUSPLUS");
1603 - dir = getenv("CW_CPLUSPLUS_DIR");
1604 - break;
1605 - case CIDX(CW_C_GCC, 0):
1606 - program = getenv("CW_GCC");
1607 - dir = getenv("CW_GCC_DIR");
1608 - break;
1609 - case CIDX(CW_C_GCC, CW_F_CXX):
1610 - program = getenv("CW_GPLUSPLUS");
1611 - dir = getenv("CW_GPLUSPLUS_DIR");
1612 - break;
1613 - }
1614 -
1615 - if (program == NULL) {
1616 - if (dir == NULL)
1617 - dir = default_dir[CC(ctx)];
1618 - cmd = cmds[CIDX(CC(ctx), ctx->i_flags)];
1619 - len = strlen(dir) + strlen(cmd) + 2;
1620 - if ((program = malloc(len)) == NULL)
1621 - nomem();
1622 - (void) snprintf(program, len, "%s/%s", dir, cmd);
1623 - }
1624 -
1625 - newae(ctx->i_ae, program);
1577 + newae(ctx->i_ae, ctx->i_compiler->path);
1626 1578
1627 1579 if (ctx->i_flags & CW_F_PROG) {
1628 1580 (void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ?
1629 - "shadow" : "primary", program);
1581 + "shadow" : "primary", ctx->i_compiler->path);
1630 1582 (void) fflush(stdout);
1631 1583 }
1632 1584
1633 1585 if (!(ctx->i_flags & CW_F_XLATE))
1634 1586 return;
1635 1587
1636 - switch (CC(ctx)) {
1637 - case CW_C_CC:
1588 + switch (ctx->i_compiler->style) {
1589 + case SUN:
1638 1590 do_cc(ctx);
1639 1591 break;
1640 - case CW_C_GCC:
1592 + case GNU:
1641 1593 do_gcc(ctx);
1642 1594 break;
1643 1595 }
1644 1596 }
1645 1597
1646 1598 static int
1647 1599 invoke(cw_ictx_t *ctx)
1648 1600 {
1649 1601 char **newargv;
1650 1602 int ac;
1651 1603 struct ae *a;
1652 1604
1653 1605 if ((newargv = calloc(sizeof (*newargv), ctx->i_ae->ael_argc + 1)) ==
1654 1606 NULL)
1655 1607 nomem();
1656 1608
1657 1609 if (ctx->i_flags & CW_F_ECHO)
1658 1610 (void) fprintf(stderr, "+ ");
1659 1611
1660 1612 for (ac = 0, a = ctx->i_ae->ael_head; a; a = a->ae_next, ac++) {
1661 1613 newargv[ac] = a->ae_arg;
1662 1614 if (ctx->i_flags & CW_F_ECHO)
1663 1615 (void) fprintf(stderr, "%s ", a->ae_arg);
1664 1616 if (a == ctx->i_ae->ael_tail)
1665 1617 break;
1666 1618 }
1667 1619
1668 1620 if (ctx->i_flags & CW_F_ECHO) {
1669 1621 (void) fprintf(stderr, "\n");
1670 1622 (void) fflush(stderr);
1671 1623 }
1672 1624
1673 1625 if (!(ctx->i_flags & CW_F_EXEC))
1674 1626 return (0);
1675 1627
1676 1628 /*
1677 1629 * We must fix up the environment here so that the
1678 1630 * dependency files are not trampled by the shadow compiler.
↓ open down ↓ |
28 lines elided |
↑ open up ↑ |
1679 1631 */
1680 1632 if ((ctx->i_flags & CW_F_SHADOW) &&
1681 1633 (unsetenv("SUNPRO_DEPENDENCIES") != 0 ||
1682 1634 unsetenv("DEPENDENCIES_OUTPUT") != 0)) {
1683 1635 (void) fprintf(stderr, "error: environment setup failed: %s\n",
1684 1636 strerror(errno));
1685 1637 return (-1);
1686 1638 }
1687 1639
1688 1640 (void) execv(newargv[0], newargv);
1689 - cw_perror("couldn't run %s", newargv[0]);
1641 + warn("couldn't run %s", newargv[0]);
1690 1642
1691 1643 return (-1);
1692 1644 }
1693 1645
1694 1646 static int
1695 1647 reap(cw_ictx_t *ctx)
1696 1648 {
1697 1649 int status, ret = 0;
1698 1650 char buf[1024];
1699 1651 struct stat s;
1700 1652
1701 1653 /*
1702 1654 * Only wait for one specific child.
1703 1655 */
1704 1656 if (ctx->i_pid <= 0)
1705 1657 return (-1);
1706 1658
1707 1659 do {
1708 1660 if (waitpid(ctx->i_pid, &status, 0) < 0) {
1709 - cw_perror("cannot reap child");
1661 + warn("cannot reap child");
1710 1662 return (-1);
1711 1663 }
1712 1664 if (status != 0) {
1713 1665 if (WIFSIGNALED(status)) {
1714 1666 ret = -WTERMSIG(status);
1715 1667 break;
1716 1668 } else if (WIFEXITED(status)) {
1717 1669 ret = WEXITSTATUS(status);
1718 1670 break;
1719 1671 }
1720 1672 }
1721 1673 } while (!WIFEXITED(status) && !WIFSIGNALED(status));
1722 1674
1723 1675 (void) unlink(ctx->i_discard);
1724 1676
1725 1677 if (stat(ctx->i_stderr, &s) < 0) {
1726 - cw_perror("stat failed on child cleanup");
1678 + warn("stat failed on child cleanup");
1727 1679 return (-1);
1728 1680 }
1729 1681 if (s.st_size != 0) {
1730 1682 FILE *f;
1731 1683
1732 1684 if ((f = fopen(ctx->i_stderr, "r")) != NULL) {
1733 1685 while (fgets(buf, sizeof (buf), f))
1734 1686 (void) fprintf(stderr, "%s", buf);
1735 1687 (void) fflush(stderr);
1736 1688 (void) fclose(f);
1737 1689 }
1738 1690 }
1739 1691 (void) unlink(ctx->i_stderr);
1740 1692 free(ctx->i_stderr);
1741 1693
1742 1694 /*
1743 1695 * cc returns an error code when given -V; we want that to succeed.
1744 1696 */
1745 1697 if (ctx->i_flags & CW_F_PROG)
1746 1698 return (0);
1747 1699
1748 1700 return (ret);
1749 1701 }
1750 1702
1751 1703 static int
1752 1704 exec_ctx(cw_ictx_t *ctx, int block)
1753 1705 {
1754 1706 char *file;
1755 1707
1756 1708 /*
1757 1709 * To avoid offending cc's sensibilities, the name of its output
1758 1710 * file must end in '.o'.
1759 1711 */
1760 1712 if ((file = tempnam(NULL, ".cw")) == NULL) {
1761 1713 nomem();
1762 1714 return (-1);
1763 1715 }
1764 1716 (void) strlcpy(ctx->i_discard, file, MAXPATHLEN);
1765 1717 (void) strlcat(ctx->i_discard, ".o", MAXPATHLEN);
1766 1718 free(file);
1767 1719
1768 1720 if ((ctx->i_stderr = tempnam(NULL, ".cw")) == NULL) {
↓ open down ↓ |
32 lines elided |
↑ open up ↑ |
1769 1721 nomem();
1770 1722 return (-1);
1771 1723 }
1772 1724
1773 1725 if ((ctx->i_pid = fork()) == 0) {
1774 1726 int fd;
1775 1727
1776 1728 (void) fclose(stderr);
1777 1729 if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL,
1778 1730 0666)) < 0) {
1779 - cw_perror("open failed for standard error");
1780 - exit(1);
1731 + err(1, "open failed for standard error");
1781 1732 }
1782 1733 if (dup2(fd, 2) < 0) {
1783 - cw_perror("dup2 failed for standard error");
1784 - exit(1);
1734 + err(1, "dup2 failed for standard error");
1785 1735 }
1786 1736 if (fd != 2)
1787 1737 (void) close(fd);
1788 1738 if (freopen("/dev/fd/2", "w", stderr) == NULL) {
1789 - cw_perror("freopen failed for /dev/fd/2");
1790 - exit(1);
1739 + err(1, "freopen failed for /dev/fd/2");
1791 1740 }
1792 1741 prepctx(ctx);
1793 1742 exit(invoke(ctx));
1794 1743 }
1795 1744
1796 1745 if (ctx->i_pid < 0) {
1797 - cw_perror("fork failed");
1798 - return (1);
1746 + err(1, "fork failed");
1799 1747 }
1800 1748
1801 1749 if (block)
1802 1750 return (reap(ctx));
1803 1751
1804 1752 return (0);
1805 1753 }
1806 1754
1807 -int
1808 -main(int argc, char **argv)
1755 +static int
1756 +parse_compiler(const char *spec, cw_compiler_t *compiler)
1809 1757 {
1810 - cw_ictx_t *ctx = newictx();
1811 - cw_ictx_t *ctx_shadow = newictx();
1812 - const char *dir;
1813 - int do_serial, do_shadow;
1814 - int ret = 0;
1758 + char *tspec, *token;
1759 +
1760 + if ((tspec = strdup(spec)) == NULL)
1761 + err(1, "out of memory");
1815 1762
1816 - if ((progname = strrchr(argv[0], '/')) == NULL)
1817 - progname = argv[0];
1763 + if ((token = strsep(&tspec, ",")) == NULL)
1764 + errx(1, "Compiler is missing a name: %s", spec);
1765 + compiler->name = token;
1766 +
1767 + if ((token = strsep(&tspec, ",")) == NULL)
1768 + errx(1, "Compiler is missing a path: %s", spec);
1769 + compiler->path = token;
1770 +
1771 + if ((token = strsep(&tspec, ",")) == NULL)
1772 + errx(1, "Compiler is missing a style: %s", spec);
1773 +
1774 + if ((strcasecmp(token, "gnu") == 0) ||
1775 + (strcasecmp(token, "gcc") == 0))
1776 + compiler->style = GNU;
1777 + else if ((strcasecmp(token, "sun") == 0) ||
1778 + (strcasecmp(token, "cc") == 0))
1779 + compiler->style = SUN;
1818 1780 else
1819 - progname++;
1781 + errx(1, "unknown compiler style: %s", token);
1820 1782
1821 - if (ctx == NULL || ctx_shadow == NULL)
1822 - nomem();
1783 + if (tspec != NULL)
1784 + errx(1, "Excess tokens in compiler: %s", spec);
1823 1785
1824 - ctx->i_flags = CW_F_ECHO|CW_F_XLATE;
1786 + return (0);
1787 +}
1825 1788
1826 - /*
1827 - * Figure out where to get our tools from. This depends on
1828 - * the environment variables set at run time.
1829 - */
1830 - if ((dir = getenv("SPRO_VROOT")) != NULL) {
1831 - (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
1832 - "%s/bin", dir);
1833 - } else if ((dir = getenv("SPRO_ROOT")) != NULL) {
1834 - (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
1835 - "%s/SS12/bin", dir);
1836 - } else if ((dir = getenv("BUILD_TOOLS")) != NULL) {
1837 - (void) snprintf(default_dir[CW_C_CC], MAXPATHLEN,
1838 - "%s/SUNWspro/SS12/bin", dir);
1839 - }
1840 -
1841 - if ((dir = getenv("GCC_ROOT")) != NULL) {
1842 - (void) snprintf(default_dir[CW_C_GCC], MAXPATHLEN,
1843 - "%s/bin", dir);
1844 - }
1789 +int
1790 +main(int argc, char **argv)
1791 +{
1792 + int ch;
1793 + cw_compiler_t primary = { NULL, NULL, 0 };
1794 + cw_compiler_t shadows[10];
1795 + int nshadows = 0;
1796 + int ret = 0;
1797 + boolean_t do_serial = B_FALSE;
1798 + boolean_t do_exec = B_FALSE;
1799 + boolean_t vflg = B_FALSE;
1800 + boolean_t Cflg = B_FALSE;
1801 + boolean_t cflg = B_FALSE;
1802 + boolean_t nflg = B_FALSE;
1803 +
1804 + cw_ictx_t *main_ctx = newictx();
1805 +
1806 + static struct option longopts[] = {
1807 + { "compiler", no_argument, NULL, 'c' },
1808 + { "noecho", no_argument, NULL ,'n' },
1809 + { "primary", required_argument, NULL, 'p' },
1810 + { "shadow", required_argument, NULL, 's' },
1811 + { "versions", no_argument, NULL, 'v' },
1812 + { NULL, 0, NULL, 0 },
1813 + };
1845 1814
1846 - do_shadow = (getenv("CW_NO_SHADOW") ? 0 : 1);
1847 - do_serial = (getenv("CW_SHADOW_SERIAL") ? 1 : 0);
1815 + while ((ch = getopt_long(argc, argv, "C", longopts, NULL)) != -1) {
1816 + switch (ch) {
1817 + case 'c':
1818 + cflg = B_TRUE;
1819 + break;
1820 + case 'C':
1821 + Cflg = B_TRUE;
1822 + break;
1823 + case 'n':
1824 + nflg = B_TRUE;
1825 + break;
1826 + case 'p':
1827 + if (primary.path != NULL) {
1828 + warnx("Only one primary compiler may be specified");
1829 + usage();
1830 + }
1848 1831
1849 - if (getenv("CW_NO_EXEC") == NULL)
1850 - ctx->i_flags |= CW_F_EXEC;
1832 + if (parse_compiler(optarg, &primary) != 0)
1833 + errx(1, "Couldn't parse %s as a compiler spec", optarg);
1834 + break;
1835 + case 's':
1836 + if (nshadows >= 10)
1837 + errx(1, "May only use 10 shadows at the moment");
1838 + if (parse_compiler(optarg, &shadows[nshadows]) != 0)
1839 + errx(1, "Couldn't parse %s as a compiler spec", optarg);
1840 + nshadows++;
1841 + break;
1842 + case 'v':
1843 + vflg = B_TRUE;
1844 + break;
1845 + default:
1846 + fprintf(stderr, "Did you forget '--'?\n");
1847 + usage();
1848 + }
1849 + }
1851 1850
1852 - /*
1853 - * The first argument must be one of "-_cc", "-_gcc", "-_CC", or "-_g++"
1854 - */
1855 - if (argc == 1)
1851 + if (primary.path == NULL) {
1852 + warnx("A primary compiler must be specified");
1856 1853 usage();
1857 - argc--;
1858 - argv++;
1859 - if (strcmp(argv[0], "-_cc") == 0) {
1860 - ctx->i_compiler = CW_C_CC;
1861 - } else if (strcmp(argv[0], "-_gcc") == 0) {
1862 - ctx->i_compiler = CW_C_GCC;
1863 - } else if (strcmp(argv[0], "-_CC") == 0) {
1864 - ctx->i_compiler = CW_C_CC;
1865 - ctx->i_flags |= CW_F_CXX;
1866 - } else if (strcmp(argv[0], "-_g++") == 0) {
1867 - ctx->i_compiler = CW_C_GCC;
1868 - ctx->i_flags |= CW_F_CXX;
1869 - } else {
1870 - /* assume "-_gcc" by default */
1871 - argc++;
1872 - argv--;
1873 - ctx->i_compiler = CW_C_GCC;
1874 1854 }
1875 1855
1876 - /*
1877 - * -_compiler - tell us the path to the primary compiler only
1878 - */
1879 - if (argc > 1 && strcmp(argv[1], "-_compiler") == 0) {
1880 - ctx->i_flags &= ~CW_F_XLATE;
1881 - prepctx(ctx);
1882 - (void) printf("%s\n", ctx->i_ae->ael_head->ae_arg);
1883 - return (0);
1856 + do_serial = (getenv("CW_SHADOW_SERIAL") == NULL) ? B_FALSE : B_TRUE;
1857 + do_exec = (getenv("CW_NO_EXEC") == NULL) ? B_TRUE : B_FALSE;
1858 +
1859 + /* Leave room for argv[0] */
1860 + argc -= (optind - 1);
1861 + argv += (optind - 1);
1862 +
1863 + if (main_ctx == NULL)
1864 + nomem();
1865 +
1866 + main_ctx->i_oldargc = argc;
1867 + main_ctx->i_oldargv = argv;
1868 + main_ctx->i_flags = CW_F_XLATE;
1869 + if (nflg == 0)
1870 + main_ctx->i_flags |= CW_F_ECHO;
1871 + if (do_exec)
1872 + main_ctx->i_flags |= CW_F_EXEC;
1873 + if (Cflg)
1874 + main_ctx->i_flags |= CW_F_CXX;
1875 + main_ctx->i_compiler = &primary;
1876 +
1877 + if (cflg) {
1878 + fputs(primary.path, stdout);
1884 1879 }
1885 1880
1886 - /*
1887 - * -_versions - tell us the cw version, paths to all compilers, and
1888 - * ask each for its version if we know how.
1889 - */
1890 - if (argc > 1 && strcmp(argv[1], "-_versions") == 0) {
1891 - (void) printf("cw version %s", CW_VERSION);
1892 - if (!do_shadow)
1893 - (void) printf(" (SHADOW MODE DISABLED)");
1894 - (void) printf("\n");
1881 + if (vflg) {
1882 + (void) printf("cw version %s\n", CW_VERSION);
1895 1883 (void) fflush(stdout);
1896 - ctx->i_flags &= ~CW_F_ECHO;
1897 - ctx->i_flags |= CW_F_PROG|CW_F_EXEC;
1898 - argc--;
1899 - argv++;
1884 + main_ctx->i_flags &= ~CW_F_ECHO;
1885 + main_ctx->i_flags |= CW_F_PROG|CW_F_EXEC;
1900 1886 do_serial = 1;
1901 1887 }
1902 1888
1903 - ctx->i_oldargc = argc;
1904 - ctx->i_oldargv = argv;
1889 + ret |= exec_ctx(main_ctx, do_serial);
1890 +
1891 + for (int i = 0; i < nshadows; i++) {
1892 + cw_ictx_t *shadow_ctx = newictx();
1893 +
1894 + if (shadow_ctx == NULL)
1895 + nomem();
1896 +
1897 + memcpy(shadow_ctx, main_ctx, sizeof (cw_ictx_t));
1905 1898
1906 - ret |= exec_ctx(ctx, do_serial);
1899 + shadow_ctx->i_flags |= CW_F_SHADOW;
1900 + shadow_ctx->i_compiler = &shadows[i];
1907 1901
1908 - if (do_shadow) {
1909 - (void) memcpy(ctx_shadow, ctx, sizeof (cw_ictx_t));
1910 - ctx_shadow->i_flags |= CW_F_SHADOW;
1911 - ret |= exec_ctx(ctx_shadow, 1);
1902 + /* XXX: Would be nice to run these parallel, too */
1903 + ret |= exec_ctx(shadow_ctx, 1);
1912 1904 }
1913 1905
1914 1906 if (!do_serial)
1915 - ret |= reap(ctx);
1907 + ret |= reap(main_ctx);
1916 1908
1917 1909 return (ret);
1918 1910 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX