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