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