Print this page
9899 cw(1onbld) should shadow more compilation
9888 cw shouldn't use __unused
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[MAXPATHLEN];
311 + char *i_discard;
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
↓ open down ↓ |
150 lines elided |
↑ open up ↑ |
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 -/* ARGSUSED */
483 -static void
484 -Xamode(struct aelist __unused *h)
485 -{
486 -}
487 -
488 482 static void
489 483 Xsmode(struct aelist *h)
490 484 {
491 485 static int xsonce;
492 486
493 487 if (xsonce++)
494 488 return;
495 489
496 490 newae(h, "-traditional");
497 491 newae(h, "-traditional-cpp");
498 492 }
499 493
500 494 static void
501 495 usage()
502 496 {
503 497 extern char *__progname;
504 498 (void) fprintf(stderr,
505 499 "usage: %s [-C] [--versions] --primary <compiler> "
506 500 "[--shadow <compiler>]... -- cflags...\n",
507 501 __progname);
508 502 (void) fprintf(stderr, "compilers take the form: name,path,style\n"
509 503 " - name: a unique name usable in flag specifiers\n"
510 504 " - path: path to the compiler binary\n"
511 505 " - style: the style of flags expected: either sun or gnu\n");
512 506 exit(2);
513 507 }
514 508
515 509 static int
516 510 xlate_xtb(struct aelist *h, const char *xarg)
517 511 {
518 512 int i, j;
519 513
520 514 for (i = 0; i < xtbl_size; i++) {
521 515 if (strcmp(xtbl[i].x_arg, xarg) == 0)
522 516 break;
523 517 }
524 518
525 519 /*
526 520 * At the end of the table and so no matching "arg" entry
527 521 * found and so this must be a bad -xarch= flag.
528 522 */
529 523 if (i == xtbl_size)
530 524 error(xarg);
531 525
532 526 for (j = 0; j < TRANS_ENTRY; j++) {
533 527 if (xtbl[i].x_trans[j] != NULL)
534 528 newae(h, xtbl[i].x_trans[j]);
535 529 else
536 530 break;
537 531 }
538 532 return (xtbl[i].x_flags);
539 533
540 534 }
541 535
542 536 static void
543 537 xlate(struct aelist *h, const char *xarg, const char **table)
544 538 {
545 539 while (*table != NULL && strcmp(xarg, *table) != 0) {
546 540 while (*table != NULL)
547 541 table++;
548 542 table++;
549 543 }
550 544
551 545 if (*table == NULL)
↓ open down ↓ |
54 lines elided |
↑ open up ↑ |
552 546 error(xarg);
553 547
554 548 table++;
555 549
556 550 while (*table != NULL) {
557 551 newae(h, *table);
558 552 table++;
559 553 }
560 554 }
561 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 +
562 585 static void
563 586 do_gcc(cw_ictx_t *ctx)
564 587 {
565 588 int c;
566 589 int nolibc = 0;
567 590 int in_output = 0, seen_o = 0, c_files = 0;
568 591 cw_op_t op = CW_O_LINK;
569 592 char *model = NULL;
570 593 char *nameflag;
571 - int mflag = 0;
594 + int mflag = 0;
572 595
573 596 if (ctx->i_flags & CW_F_PROG) {
574 597 newae(ctx->i_ae, "--version");
575 598 return;
576 599 }
577 600
578 601 newae(ctx->i_ae, "-fident");
579 602 newae(ctx->i_ae, "-finline");
580 603 newae(ctx->i_ae, "-fno-inline-functions");
581 604 newae(ctx->i_ae, "-fno-builtin");
582 605 newae(ctx->i_ae, "-fno-asm");
583 606 newae(ctx->i_ae, "-fdiagnostics-show-option");
584 607 newae(ctx->i_ae, "-nodefaultlibs");
585 608
586 609 #if defined(__sparc)
587 610 /*
588 611 * The SPARC ldd and std instructions require 8-byte alignment of
589 612 * their address operand. gcc correctly uses them only when the
590 613 * ABI requires 8-byte alignment; unfortunately we have a number of
591 614 * pieces of buggy code that doesn't conform to the ABI. This
592 615 * flag makes gcc work more like Studio with -xmemalign=4.
593 616 */
594 617 newae(ctx->i_ae, "-mno-integer-ldd-std");
595 618 #endif
596 619
597 620 /*
598 621 * This is needed because 'u' is defined
599 622 * under a conditional on 'sun'. Should
600 623 * probably just remove the conditional,
601 624 * or make it be dependent on '__sun'.
602 625 *
603 626 * -Dunix is also missing in enhanced ANSI mode
604 627 */
605 628 newae(ctx->i_ae, "-D__sun");
606 629
607 630 if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1)
608 631 nomem();
609 632
610 633 /*
611 634 * Walk the argument list, translating as we go ..
612 635 */
613 636 while (--ctx->i_oldargc > 0) {
614 637 char *arg = *++ctx->i_oldargv;
615 638 size_t arglen = strlen(arg);
616 639
617 640 if (*arg == '-') {
618 641 arglen--;
619 642 } else {
620 643 /*
621 644 * Discard inline files that gcc doesn't grok
622 645 */
623 646 if (!in_output && arglen > 3 &&
624 647 strcmp(arg + arglen - 3, ".il") == 0)
625 648 continue;
626 649
627 650 if (!in_output && arglen > 2 &&
628 651 arg[arglen - 2] == '.' &&
↓ open down ↓ |
47 lines elided |
↑ open up ↑ |
629 652 (arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' ||
630 653 arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i'))
631 654 c_files++;
632 655
633 656 /*
634 657 * Otherwise, filenames and partial arguments
635 658 * are passed through for gcc to chew on. However,
636 659 * output is always discarded for the secondary
637 660 * compiler.
638 661 */
639 - if ((ctx->i_flags & CW_F_SHADOW) && in_output)
662 + if ((ctx->i_flags & CW_F_SHADOW) && in_output) {
663 + ctx->i_discard = discard_file_name(arg);
664 +
665 + if (ctx->i_discard == NULL)
666 + nomem();
640 667 newae(ctx->i_ae, ctx->i_discard);
641 - else
668 + } else {
642 669 newae(ctx->i_ae, arg);
670 + }
643 671 in_output = 0;
644 672 continue;
645 673 }
646 674
647 675 if (ctx->i_flags & CW_F_CXX) {
648 676 if (strncmp(arg, "-_g++=", 6) == 0) {
649 677 newae(ctx->i_ae, strchr(arg, '=') + 1);
650 678 continue;
651 679 }
652 680 if (strncmp(arg, "-compat=", 8) == 0) {
653 681 /* discard -compat=4 and -compat=5 */
654 682 continue;
655 683 }
656 684 if (strcmp(arg, "-Qoption") == 0) {
657 685 /* discard -Qoption and its two arguments */
658 686 if (ctx->i_oldargc < 3)
659 687 error(arg);
660 688 ctx->i_oldargc -= 2;
661 689 ctx->i_oldargv += 2;
662 690 continue;
663 691 }
664 692 if (strcmp(arg, "-xwe") == 0) {
665 693 /* turn warnings into errors */
666 694 newae(ctx->i_ae, "-Werror");
667 695 continue;
668 696 }
669 697 if (strcmp(arg, "-norunpath") == 0) {
670 698 /* gcc has no corresponding option */
671 699 continue;
672 700 }
673 701 if (strcmp(arg, "-nolib") == 0) {
674 702 /* -nodefaultlibs is on by default */
675 703 nolibc = 1;
676 704 continue;
677 705 }
678 706 #if defined(__sparc)
679 707 if (strcmp(arg, "-cg92") == 0) {
680 708 mflag |= xlate_xtb(ctx->i_ae, "v8");
681 709 xlate(ctx->i_ae, "super", xchip_tbl);
682 710 continue;
683 711 }
684 712 #endif /* __sparc */
685 713 }
686 714
687 715 switch ((c = arg[1])) {
688 716 case '_':
689 717 if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) ||
690 718 (strncmp(arg, "-_gcc=", 6) == 0) ||
691 719 (strncmp(arg, "-_gnu=", 6) == 0)) {
692 720 newae(ctx->i_ae, strchr(arg, '=') + 1);
693 721 }
694 722 break;
695 723 case '#':
696 724 if (arglen == 1) {
697 725 newae(ctx->i_ae, "-v");
698 726 break;
699 727 }
700 728 error(arg);
701 729 break;
702 730 case 'f':
703 731 if ((strcmp(arg, "-fpic") == 0) ||
704 732 (strcmp(arg, "-fPIC") == 0)) {
705 733 newae(ctx->i_ae, arg);
706 734 break;
707 735 }
708 736 error(arg);
709 737 break;
710 738 case 'g':
711 739 newae(ctx->i_ae, "-gdwarf-2");
712 740 break;
713 741 case 'E':
714 742 if (arglen == 1) {
715 743 newae(ctx->i_ae, "-xc");
716 744 newae(ctx->i_ae, arg);
717 745 op = CW_O_PREPROCESS;
718 746 nolibc = 1;
719 747 break;
720 748 }
721 749 error(arg);
722 750 break;
723 751 case 'c':
724 752 case 'S':
725 753 if (arglen == 1) {
726 754 op = CW_O_COMPILE;
727 755 nolibc = 1;
728 756 }
729 757 /* FALLTHROUGH */
730 758 case 'C':
731 759 case 'H':
732 760 case 'p':
733 761 if (arglen == 1) {
734 762 newae(ctx->i_ae, arg);
735 763 break;
736 764 }
737 765 error(arg);
738 766 break;
739 767 case 'A':
740 768 case 'h':
741 769 case 'I':
742 770 case 'i':
743 771 case 'L':
744 772 case 'l':
745 773 case 'R':
746 774 case 'U':
747 775 case 'u':
↓ open down ↓ |
95 lines elided |
↑ open up ↑ |
748 776 case 'w':
749 777 newae(ctx->i_ae, arg);
750 778 break;
751 779 case 'o':
752 780 seen_o = 1;
753 781 if (arglen == 1) {
754 782 in_output = 1;
755 783 newae(ctx->i_ae, arg);
756 784 } else if (ctx->i_flags & CW_F_SHADOW) {
757 785 newae(ctx->i_ae, "-o");
786 + ctx->i_discard = discard_file_name(arg);
787 + if (ctx->i_discard == NULL)
788 + nomem();
789 +
758 790 newae(ctx->i_ae, ctx->i_discard);
759 791 } else {
760 792 newae(ctx->i_ae, arg);
761 793 }
762 794 break;
763 795 case 'D':
764 796 newae(ctx->i_ae, arg);
765 797 /*
766 798 * XXX Clearly a hack ... do we need _KADB too?
767 799 */
768 800 if (strcmp(arg, "-D_KERNEL") == 0 ||
769 801 strcmp(arg, "-D_BOOT") == 0)
770 802 newae(ctx->i_ae, "-ffreestanding");
771 803 break;
772 804 case 'd':
773 805 if (arglen == 2) {
774 806 if (strcmp(arg, "-dy") == 0) {
775 807 newae(ctx->i_ae, "-Wl,-dy");
776 808 break;
777 809 }
778 810 if (strcmp(arg, "-dn") == 0) {
779 811 newae(ctx->i_ae, "-Wl,-dn");
780 812 break;
781 813 }
782 814 }
783 815 if (strcmp(arg, "-dalign") == 0) {
784 816 /*
785 817 * -dalign forces alignment in some cases;
786 818 * gcc does not need any flag to do this.
787 819 */
788 820 break;
789 821 }
790 822 error(arg);
791 823 break;
792 824 case 'e':
793 825 if (strcmp(arg,
794 826 "-erroff=E_EMPTY_TRANSLATION_UNIT") == 0) {
795 827 /*
796 828 * Accept but ignore this -- gcc doesn't
797 829 * seem to complain about empty translation
798 830 * units
799 831 */
800 832 break;
801 833 }
802 834 /* XX64 -- ignore all -erroff= options, for now */
803 835 if (strncmp(arg, "-erroff=", 8) == 0)
804 836 break;
805 837 if (strcmp(arg, "-errtags=yes") == 0) {
806 838 warnings(ctx->i_ae);
807 839 break;
808 840 }
809 841 if (strcmp(arg, "-errwarn=%all") == 0) {
810 842 newae(ctx->i_ae, "-Werror");
811 843 break;
812 844 }
813 845 error(arg);
814 846 break;
815 847 case 'G':
816 848 newae(ctx->i_ae, "-shared");
817 849 nolibc = 1;
818 850 break;
819 851 case 'k':
820 852 if (strcmp(arg, "-keeptmp") == 0) {
821 853 newae(ctx->i_ae, "-save-temps");
822 854 break;
823 855 }
824 856 error(arg);
825 857 break;
826 858 case 'm':
827 859 if (strcmp(arg, "-mt") == 0) {
828 860 newae(ctx->i_ae, "-D_REENTRANT");
829 861 break;
830 862 }
831 863 if (strcmp(arg, "-m64") == 0) {
832 864 newae(ctx->i_ae, "-m64");
833 865 #if defined(__x86)
834 866 newae(ctx->i_ae, "-mtune=opteron");
835 867 #endif
836 868 mflag |= M64;
837 869 break;
838 870 }
839 871 if (strcmp(arg, "-m32") == 0) {
840 872 newae(ctx->i_ae, "-m32");
841 873 mflag |= M32;
842 874 break;
843 875 }
844 876 error(arg);
845 877 break;
846 878 case 'B': /* linker options */
847 879 case 'M':
848 880 case 'z':
849 881 {
850 882 char *opt;
851 883 size_t len;
852 884 char *s;
853 885
854 886 if (arglen == 1) {
855 887 opt = *++ctx->i_oldargv;
856 888 if (opt == NULL || *opt == '\0')
857 889 error(arg);
858 890 ctx->i_oldargc--;
859 891 } else {
860 892 opt = arg + 2;
861 893 }
862 894 len = strlen(opt) + 7;
863 895 if ((s = malloc(len)) == NULL)
864 896 nomem();
865 897 (void) snprintf(s, len, "-Wl,-%c%s", c, opt);
866 898 newae(ctx->i_ae, s);
867 899 free(s);
868 900 }
869 901 break;
870 902 case 'O':
871 903 if (arglen == 1) {
872 904 newae(ctx->i_ae, "-O");
873 905 break;
874 906 }
875 907 error(arg);
876 908 break;
877 909 case 'P':
878 910 /*
879 911 * We could do '-E -o filename.i', but that's hard,
880 912 * and we don't need it for the case that's triggering
881 913 * this addition. We'll require the user to specify
882 914 * -o in the Makefile. If they don't they'll find out
883 915 * in a hurry.
884 916 */
885 917 newae(ctx->i_ae, "-E");
886 918 op = CW_O_PREPROCESS;
887 919 nolibc = 1;
888 920 break;
889 921 case 's':
890 922 if (arglen == 1) {
891 923 newae(ctx->i_ae, "-Wl,-s");
892 924 break;
893 925 }
894 926 error(arg);
895 927 break;
896 928 case 't':
897 929 if (arglen == 1) {
898 930 newae(ctx->i_ae, "-Wl,-t");
899 931 break;
900 932 }
901 933 error(arg);
902 934 break;
903 935 case 'V':
904 936 if (arglen == 1) {
905 937 ctx->i_flags &= ~CW_F_ECHO;
906 938 newae(ctx->i_ae, "--version");
907 939 break;
908 940 }
909 941 error(arg);
910 942 break;
911 943 case 'v':
912 944 if (arglen == 1) {
913 945 warnings(ctx->i_ae);
914 946 break;
915 947 }
916 948 error(arg);
917 949 break;
918 950 case 'W':
919 951 if (strncmp(arg, "-Wp,-xc99", 9) == 0) {
920 952 /*
921 953 * gcc's preprocessor will accept c99
922 954 * regardless, so accept and ignore.
923 955 */
924 956 break;
925 957 }
926 958 if (strncmp(arg, "-Wa,", 4) == 0 ||
927 959 strncmp(arg, "-Wp,", 4) == 0 ||
928 960 strncmp(arg, "-Wl,", 4) == 0) {
929 961 newae(ctx->i_ae, arg);
930 962 break;
931 963 }
932 964 if (strcmp(arg, "-W0,-noglobal") == 0 ||
933 965 strcmp(arg, "-W0,-xglobalstatic") == 0) {
934 966 /*
935 967 * gcc doesn't prefix local symbols
936 968 * in debug mode, so this is not needed.
937 969 */
938 970 break;
939 971 }
940 972 if (strcmp(arg, "-W0,-Lt") == 0) {
941 973 /*
942 974 * Generate tests at the top of loops.
943 975 * There is no direct gcc equivalent, ignore.
944 976 */
945 977 break;
946 978 }
947 979 if (strcmp(arg, "-W0,-xdbggen=no%usedonly") == 0) {
948 980 newae(ctx->i_ae,
949 981 "-fno-eliminate-unused-debug-symbols");
950 982 newae(ctx->i_ae,
951 983 "-fno-eliminate-unused-debug-types");
952 984 break;
953 985 }
954 986 if (strcmp(arg, "-W2,-xwrap_int") == 0) {
955 987 /*
956 988 * Use the legacy behaviour (pre-SS11)
957 989 * for integer wrapping.
958 990 * gcc does not need this.
959 991 */
960 992 break;
961 993 }
962 994 if (strcmp(arg, "-Wd,-xsafe=unboundsym") == 0) {
963 995 /*
964 996 * Prevents optimizing away checks for
965 997 * unbound weak symbol addresses. gcc does
966 998 * not do this, so it's not needed.
967 999 */
968 1000 break;
969 1001 }
970 1002 if (strncmp(arg, "-Wc,-xcode=", 11) == 0) {
971 1003 xlate(ctx->i_ae, arg + 11, xcode_tbl);
972 1004 break;
973 1005 }
974 1006 if (strncmp(arg, "-Wc,-Qiselect", 13) == 0) {
975 1007 /*
976 1008 * Prevents insertion of register symbols.
977 1009 * gcc doesn't do this, so ignore it.
978 1010 */
979 1011 break;
980 1012 }
981 1013 if (strcmp(arg, "-Wc,-Qassembler-ounrefsym=0") == 0) {
982 1014 /*
983 1015 * Prevents optimizing away of static variables.
984 1016 * gcc does not do this, so it's not needed.
985 1017 */
986 1018 break;
987 1019 }
988 1020 #if defined(__x86)
↓ open down ↓ |
221 lines elided |
↑ open up ↑ |
989 1021 if (strcmp(arg, "-Wu,-save_args") == 0) {
990 1022 newae(ctx->i_ae, "-msave-args");
991 1023 break;
992 1024 }
993 1025 #endif /* __x86 */
994 1026 error(arg);
995 1027 break;
996 1028 case 'X':
997 1029 if (strcmp(arg, "-Xa") == 0 ||
998 1030 strcmp(arg, "-Xt") == 0) {
999 - Xamode(ctx->i_ae);
1000 1031 break;
1001 1032 }
1002 1033 if (strcmp(arg, "-Xs") == 0) {
1003 1034 Xsmode(ctx->i_ae);
1004 1035 break;
1005 1036 }
1006 1037 error(arg);
1007 1038 break;
1008 1039 case 'x':
1009 1040 if (arglen == 1)
1010 1041 error(arg);
1011 1042 switch (arg[2]) {
1012 1043 case 'a':
1013 1044 if (strncmp(arg, "-xarch=", 7) == 0) {
1014 1045 mflag |= xlate_xtb(ctx->i_ae, arg + 7);
1015 1046 break;
1016 1047 }
1017 1048 error(arg);
1018 1049 break;
1019 1050 case 'b':
1020 1051 if (strncmp(arg, "-xbuiltin=", 10) == 0) {
1021 1052 if (strcmp(arg + 10, "%all"))
1022 1053 newae(ctx->i_ae, "-fbuiltin");
1023 1054 break;
1024 1055 }
1025 1056 error(arg);
1026 1057 break;
1027 1058 case 'C':
1028 1059 /* Accept C++ style comments -- ignore */
1029 1060 if (strcmp(arg, "-xCC") == 0)
1030 1061 break;
1031 1062 error(arg);
1032 1063 break;
1033 1064 case 'c':
1034 1065 if (strncmp(arg, "-xc99=%all", 10) == 0) {
1035 1066 newae(ctx->i_ae, "-std=gnu99");
1036 1067 break;
1037 1068 }
1038 1069 if (strncmp(arg, "-xc99=%none", 11) == 0) {
1039 1070 newae(ctx->i_ae, "-std=gnu89");
1040 1071 break;
1041 1072 }
1042 1073 if (strncmp(arg, "-xchip=", 7) == 0) {
1043 1074 xlate(ctx->i_ae, arg + 7, xchip_tbl);
1044 1075 break;
1045 1076 }
1046 1077 if (strncmp(arg, "-xcode=", 7) == 0) {
1047 1078 xlate(ctx->i_ae, arg + 7, xcode_tbl);
1048 1079 break;
1049 1080 }
1050 1081 if (strncmp(arg, "-xcrossfile", 11) == 0)
1051 1082 break;
1052 1083 error(arg);
1053 1084 break;
1054 1085 case 'd':
1055 1086 if (strncmp(arg, "-xdebugformat=", 14) == 0)
1056 1087 break;
1057 1088 error(arg);
1058 1089 break;
1059 1090 case 'F':
1060 1091 /*
1061 1092 * Compile for mapfile reordering, or unused
1062 1093 * section elimination, syntax can be -xF or
1063 1094 * more complex, like -xF=%all -- ignore.
1064 1095 */
1065 1096 if (strncmp(arg, "-xF", 3) == 0)
1066 1097 break;
1067 1098 error(arg);
1068 1099 break;
1069 1100 case 'i':
1070 1101 if (strncmp(arg, "-xinline", 8) == 0)
1071 1102 /* No inlining; ignore */
1072 1103 break;
1073 1104 if (strcmp(arg, "-xildon") == 0 ||
1074 1105 strcmp(arg, "-xildoff") == 0)
1075 1106 /* No incremental linking; ignore */
1076 1107 break;
1077 1108 error(arg);
1078 1109 break;
1079 1110 #if defined(__x86)
1080 1111 case 'm':
1081 1112 if (strcmp(arg, "-xmodel=kernel") == 0) {
1082 1113 newae(ctx->i_ae, "-ffreestanding");
1083 1114 newae(ctx->i_ae, "-mno-red-zone");
1084 1115 model = "-mcmodel=kernel";
1085 1116 nolibc = 1;
1086 1117 break;
1087 1118 }
1088 1119 error(arg);
1089 1120 break;
1090 1121 #endif /* __x86 */
1091 1122 case 'O':
1092 1123 if (strncmp(arg, "-xO", 3) == 0) {
1093 1124 size_t len = strlen(arg);
1094 1125 char *s = NULL;
1095 1126 int c = *(arg + 3);
1096 1127 int level;
1097 1128
1098 1129 if (len != 4 || !isdigit(c))
1099 1130 error(arg);
1100 1131
1101 1132 level = atoi(arg + 3);
1102 1133 if (level > 5)
1103 1134 error(arg);
1104 1135 if (level >= 2) {
1105 1136 /*
1106 1137 * For gcc-3.4.x at -O2 we
1107 1138 * need to disable optimizations
1108 1139 * that break ON.
1109 1140 */
1110 1141 optim_disable(ctx->i_ae, level);
1111 1142 /*
1112 1143 * limit -xO3 to -O2 as well.
1113 1144 */
1114 1145 level = 2;
1115 1146 }
1116 1147 if (asprintf(&s, "-O%d", level) == -1)
1117 1148 nomem();
1118 1149 newae(ctx->i_ae, s);
1119 1150 free(s);
1120 1151 break;
1121 1152 }
1122 1153 error(arg);
1123 1154 break;
1124 1155 case 'r':
1125 1156 if (strncmp(arg, "-xregs=", 7) == 0) {
1126 1157 xlate(ctx->i_ae, arg + 7, xregs_tbl);
1127 1158 break;
1128 1159 }
1129 1160 error(arg);
1130 1161 break;
1131 1162 case 's':
1132 1163 if (strcmp(arg, "-xs") == 0 ||
1133 1164 strcmp(arg, "-xspace") == 0 ||
1134 1165 strcmp(arg, "-xstrconst") == 0)
1135 1166 break;
1136 1167 error(arg);
1137 1168 break;
1138 1169 case 't':
1139 1170 if (strncmp(arg, "-xtarget=", 9) == 0) {
1140 1171 xlate(ctx->i_ae, arg + 9, xtarget_tbl);
1141 1172 break;
1142 1173 }
1143 1174 error(arg);
1144 1175 break;
1145 1176 case 'e':
1146 1177 case 'h':
1147 1178 case 'l':
1148 1179 default:
1149 1180 error(arg);
1150 1181 break;
1151 1182 }
1152 1183 break;
1153 1184 case 'Y':
1154 1185 if (arglen == 1) {
1155 1186 if ((arg = *++ctx->i_oldargv) == NULL ||
1156 1187 *arg == '\0')
1157 1188 error("-Y");
1158 1189 ctx->i_oldargc--;
1159 1190 arglen = strlen(arg + 1);
1160 1191 } else {
1161 1192 arg += 2;
1162 1193 }
1163 1194 /* Just ignore -YS,... for now */
1164 1195 if (strncmp(arg, "S,", 2) == 0)
1165 1196 break;
1166 1197 if (strncmp(arg, "l,", 2) == 0) {
1167 1198 char *s = strdup(arg);
1168 1199 s[0] = '-';
1169 1200 s[1] = 'B';
1170 1201 newae(ctx->i_ae, s);
1171 1202 free(s);
1172 1203 break;
1173 1204 }
1174 1205 if (strncmp(arg, "I,", 2) == 0) {
1175 1206 char *s = strdup(arg);
1176 1207 s[0] = '-';
1177 1208 s[1] = 'I';
1178 1209 newae(ctx->i_ae, "-nostdinc");
1179 1210 newae(ctx->i_ae, s);
1180 1211 free(s);
1181 1212 break;
1182 1213 }
1183 1214 error(arg);
1184 1215 break;
1185 1216 case 'Q':
1186 1217 /*
↓ open down ↓ |
177 lines elided |
↑ open up ↑ |
1187 1218 * We could map -Qy into -Wl,-Qy etc.
1188 1219 */
1189 1220 default:
1190 1221 error(arg);
1191 1222 break;
1192 1223 }
1193 1224 }
1194 1225
1195 1226 free(nameflag);
1196 1227
1197 - if (c_files > 1 && (ctx->i_flags & CW_F_SHADOW) &&
1198 - op != CW_O_PREPROCESS) {
1228 + /*
1229 + * When compiling multiple source files in a single invocation some
1230 + * compilers output objects into the current directory with
1231 + * predictable and conventional names.
1232 + *
1233 + * We prevent any attempt to compile multiple files at once so that
1234 + * any such objects created by a shadow can't escape into a later
1235 + * link-edit.
1236 + */
1237 + if (c_files > 1 && op != CW_O_PREPROCESS) {
1199 1238 errx(2, "multiple source files are "
1200 1239 "allowed only with -E or -P");
1201 1240 }
1202 1241
1203 1242 /*
1204 1243 * Make sure that we do not have any unintended interactions between
1205 1244 * the xarch options passed in and the version of the Studio compiler
1206 1245 * used.
1207 1246 */
1208 1247 if ((mflag & (SS11|SS12)) == (SS11|SS12)) {
1209 1248 errx(2,
1210 1249 "Conflicting \"-xarch=\" flags (both Studio 11 and 12)\n");
1211 1250 }
1212 1251
1213 1252 switch (mflag) {
1214 1253 case 0:
1215 1254 /* FALLTHROUGH */
1216 1255 case M32:
1217 1256 #if defined(__sparc)
1218 1257 /*
1219 1258 * Only -m32 is defined and so put in the missing xarch
1220 1259 * translation.
1221 1260 */
1222 1261 newae(ctx->i_ae, "-mcpu=v8");
1223 1262 newae(ctx->i_ae, "-mno-v8plus");
1224 1263 #endif
1225 1264 break;
1226 1265 case M64:
1227 1266 #if defined(__sparc)
1228 1267 /*
1229 1268 * Only -m64 is defined and so put in the missing xarch
1230 1269 * translation.
1231 1270 */
1232 1271 newae(ctx->i_ae, "-mcpu=v9");
1233 1272 #endif
1234 1273 break;
1235 1274 case SS12:
1236 1275 #if defined(__sparc)
1237 1276 /* no -m32/-m64 flag used - this is an error for sparc builds */
1238 1277 (void) fprintf(stderr, "No -m32/-m64 flag defined\n");
1239 1278 exit(2);
1240 1279 #endif
1241 1280 break;
1242 1281 case SS11:
1243 1282 /* FALLTHROUGH */
1244 1283 case (SS11|M32):
1245 1284 case (SS11|M64):
1246 1285 break;
1247 1286 case (SS12|M32):
1248 1287 #if defined(__sparc)
1249 1288 /*
1250 1289 * Need to add in further 32 bit options because with SS12
1251 1290 * the xarch=sparcvis option can be applied to 32 or 64
1252 1291 * bit, and so the translatation table (xtbl) cannot handle
1253 1292 * that.
1254 1293 */
1255 1294 newae(ctx->i_ae, "-mv8plus");
↓ open down ↓ |
47 lines elided |
↑ open up ↑ |
1256 1295 #endif
1257 1296 break;
1258 1297 case (SS12|M64):
1259 1298 break;
1260 1299 default:
1261 1300 (void) fprintf(stderr,
1262 1301 "Incompatible -xarch= and/or -m32/-m64 options used.\n");
1263 1302 exit(2);
1264 1303 }
1265 1304
1266 - if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
1267 - (ctx->i_flags & CW_F_SHADOW))
1268 - exit(0);
1305 + if (ctx->i_flags & CW_F_SHADOW) {
1306 + if (op == CW_O_PREPROCESS)
1307 + exit(0);
1308 + else if (op == CW_O_LINK && c_files == 0)
1309 + exit(0);
1310 + }
1269 1311
1270 1312 if (model != NULL)
1271 1313 newae(ctx->i_ae, model);
1272 1314 if (!nolibc)
1273 1315 newae(ctx->i_ae, "-lc");
1274 1316 if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1317 + ctx->i_discard = discard_file_name(NULL);
1318 +
1319 + if (ctx->i_discard == NULL)
1320 + nomem();
1275 1321 newae(ctx->i_ae, "-o");
1276 1322 newae(ctx->i_ae, ctx->i_discard);
1277 1323 }
1278 1324 }
1279 1325
1280 1326 static void
1281 1327 do_cc(cw_ictx_t *ctx)
1282 1328 {
1283 - int in_output = 0, seen_o = 0;
1329 + int in_output = 0, seen_o = 0, c_files = 0;
1284 1330 cw_op_t op = CW_O_LINK;
1285 1331 char *nameflag;
1286 1332
1287 1333 if (ctx->i_flags & CW_F_PROG) {
1288 1334 newae(ctx->i_ae, "-V");
1289 1335 return;
1290 1336 }
1291 1337
1292 1338 if (asprintf(&nameflag, "-_%s=", ctx->i_compiler->c_name) == -1)
1293 1339 nomem();
1294 1340
1295 1341 while (--ctx->i_oldargc > 0) {
1296 1342 char *arg = *++ctx->i_oldargv;
1343 + size_t arglen = strlen(arg);
1297 1344
1298 1345 if (strncmp(arg, "-_CC=", 5) == 0) {
1299 1346 newae(ctx->i_ae, strchr(arg, '=') + 1);
1300 1347 continue;
1301 1348 }
1302 1349
1303 1350 if (*arg != '-') {
1351 + if (!in_output && arglen > 2 &&
1352 + arg[arglen - 2] == '.' &&
1353 + (arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' ||
1354 + arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i'))
1355 + c_files++;
1356 +
1304 1357 if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) {
1305 1358 newae(ctx->i_ae, arg);
1306 1359 } else {
1307 1360 in_output = 0;
1361 + ctx->i_discard = discard_file_name(arg);
1362 + if (ctx->i_discard == NULL)
1363 + nomem();
1308 1364 newae(ctx->i_ae, ctx->i_discard);
1309 1365 }
1310 1366 continue;
1311 1367 }
1312 1368 switch (*(arg + 1)) {
1313 1369 case '_':
1314 1370 if ((strncmp(arg, nameflag, strlen(nameflag)) == 0) ||
1315 1371 (strncmp(arg, "-_cc=", 5) == 0) ||
1316 1372 (strncmp(arg, "-_sun=", 6) == 0)) {
1317 1373 newae(ctx->i_ae, strchr(arg, '=') + 1);
1318 1374 }
1319 1375 break;
1320 1376
1321 1377 case 'V':
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
1322 1378 ctx->i_flags &= ~CW_F_ECHO;
1323 1379 newae(ctx->i_ae, arg);
1324 1380 break;
1325 1381 case 'o':
1326 1382 seen_o = 1;
1327 1383 if (strlen(arg) == 2) {
1328 1384 in_output = 1;
1329 1385 newae(ctx->i_ae, arg);
1330 1386 } else if (ctx->i_flags & CW_F_SHADOW) {
1331 1387 newae(ctx->i_ae, "-o");
1388 + ctx->i_discard = discard_file_name(arg);
1389 + if (ctx->i_discard == NULL)
1390 + nomem();
1332 1391 newae(ctx->i_ae, ctx->i_discard);
1333 1392 } else {
1334 1393 newae(ctx->i_ae, arg);
1335 1394 }
1336 1395 break;
1337 1396 case 'c':
1338 1397 case 'S':
1339 1398 if (strlen(arg) == 2)
1340 1399 op = CW_O_COMPILE;
1341 1400 newae(ctx->i_ae, arg);
1342 1401 break;
1343 1402 case 'E':
1344 1403 case 'P':
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
1345 1404 if (strlen(arg) == 2)
1346 1405 op = CW_O_PREPROCESS;
1347 1406 /*FALLTHROUGH*/
1348 1407 default:
1349 1408 newae(ctx->i_ae, arg);
1350 1409 }
1351 1410 }
1352 1411
1353 1412 free(nameflag);
1354 1413
1355 - if ((op == CW_O_LINK || op == CW_O_PREPROCESS) &&
1356 - (ctx->i_flags & CW_F_SHADOW))
1357 - exit(0);
1414 + /* See the comment on this same code in do_gcc() */
1415 + if (c_files > 1 && op != CW_O_PREPROCESS) {
1416 + errx(2, "multiple source files are "
1417 + "allowed only with -E or -P");
1418 + }
1419 +
1420 + if (ctx->i_flags & CW_F_SHADOW) {
1421 + if (op == CW_O_PREPROCESS)
1422 + exit(0);
1423 + else if (op == CW_O_LINK && c_files == 0)
1424 + exit(0);
1425 + }
1358 1426
1359 1427 if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
1360 1428 newae(ctx->i_ae, "-o");
1429 + ctx->i_discard = discard_file_name(NULL);
1430 +
1431 + if (ctx->i_discard == NULL)
1432 + nomem();
1361 1433 newae(ctx->i_ae, ctx->i_discard);
1362 1434 }
1363 1435 }
1364 1436
1365 1437 static void
1366 1438 prepctx(cw_ictx_t *ctx)
1367 1439 {
1368 1440 newae(ctx->i_ae, ctx->i_compiler->c_path);
1369 1441
1370 1442 if (ctx->i_flags & CW_F_PROG) {
1371 1443 (void) printf("%s: %s\n", (ctx->i_flags & CW_F_SHADOW) ?
1372 1444 "shadow" : "primary", ctx->i_compiler->c_path);
1373 1445 (void) fflush(stdout);
1374 1446 }
1375 1447
1376 1448 if (!(ctx->i_flags & CW_F_XLATE))
1377 1449 return;
1378 1450
1379 1451 switch (ctx->i_compiler->c_style) {
1380 1452 case SUN:
1381 1453 do_cc(ctx);
1382 1454 break;
1383 1455 case GNU:
1384 1456 do_gcc(ctx);
1385 1457 break;
1386 1458 }
1387 1459 }
1388 1460
1389 1461 static int
1390 1462 invoke(cw_ictx_t *ctx)
1391 1463 {
1392 1464 char **newargv;
1393 1465 int ac;
1394 1466 struct ae *a;
1395 1467
1396 1468 if ((newargv = calloc(sizeof (*newargv), ctx->i_ae->ael_argc + 1)) ==
1397 1469 NULL)
1398 1470 nomem();
1399 1471
1400 1472 if (ctx->i_flags & CW_F_ECHO)
1401 1473 (void) fprintf(stderr, "+ ");
1402 1474
1403 1475 for (ac = 0, a = ctx->i_ae->ael_head; a; a = a->ae_next, ac++) {
1404 1476 newargv[ac] = a->ae_arg;
1405 1477 if (ctx->i_flags & CW_F_ECHO)
1406 1478 (void) fprintf(stderr, "%s ", a->ae_arg);
1407 1479 if (a == ctx->i_ae->ael_tail)
1408 1480 break;
1409 1481 }
1410 1482
1411 1483 if (ctx->i_flags & CW_F_ECHO) {
1412 1484 (void) fprintf(stderr, "\n");
1413 1485 (void) fflush(stderr);
1414 1486 }
1415 1487
1416 1488 if (!(ctx->i_flags & CW_F_EXEC))
1417 1489 return (0);
1418 1490
1419 1491 /*
1420 1492 * We must fix up the environment here so that the dependency files are
1421 1493 * not trampled by the shadow compiler. Also take care of GCC
1422 1494 * environment variables that will throw off gcc. This assumes a primary
1423 1495 * gcc.
1424 1496 */
1425 1497 if ((ctx->i_flags & CW_F_SHADOW) &&
1426 1498 (unsetenv("SUNPRO_DEPENDENCIES") != 0 ||
1427 1499 unsetenv("DEPENDENCIES_OUTPUT") != 0 ||
1428 1500 unsetenv("GCC_ROOT") != 0)) {
1429 1501 (void) fprintf(stderr, "error: environment setup failed: %s\n",
1430 1502 strerror(errno));
1431 1503 return (-1);
1432 1504 }
1433 1505
1434 1506 (void) execv(newargv[0], newargv);
1435 1507 warn("couldn't run %s", newargv[0]);
1436 1508
1437 1509 return (-1);
1438 1510 }
1439 1511
1440 1512 static int
1441 1513 reap(cw_ictx_t *ctx)
1442 1514 {
1443 1515 int status, ret = 0;
1444 1516 char buf[1024];
1445 1517 struct stat s;
1446 1518
1447 1519 /*
1448 1520 * Only wait for one specific child.
1449 1521 */
1450 1522 if (ctx->i_pid <= 0)
1451 1523 return (-1);
1452 1524
1453 1525 do {
1454 1526 if (waitpid(ctx->i_pid, &status, 0) < 0) {
1455 1527 warn("cannot reap child");
1456 1528 return (-1);
1457 1529 }
1458 1530 if (status != 0) {
1459 1531 if (WIFSIGNALED(status)) {
↓ open down ↓ |
89 lines elided |
↑ open up ↑ |
1460 1532 ret = -WTERMSIG(status);
1461 1533 break;
1462 1534 } else if (WIFEXITED(status)) {
1463 1535 ret = WEXITSTATUS(status);
1464 1536 break;
1465 1537 }
1466 1538 }
1467 1539 } while (!WIFEXITED(status) && !WIFSIGNALED(status));
1468 1540
1469 1541 (void) unlink(ctx->i_discard);
1542 + free(ctx->i_discard);
1470 1543
1471 1544 if (stat(ctx->i_stderr, &s) < 0) {
1472 1545 warn("stat failed on child cleanup");
1473 1546 return (-1);
1474 1547 }
1475 1548 if (s.st_size != 0) {
1476 1549 FILE *f;
1477 1550
1478 1551 if ((f = fopen(ctx->i_stderr, "r")) != NULL) {
1479 1552 while (fgets(buf, sizeof (buf), f))
1480 1553 (void) fprintf(stderr, "%s", buf);
1481 1554 (void) fflush(stderr);
1482 1555 (void) fclose(f);
1483 1556 }
1484 1557 }
1485 1558 (void) unlink(ctx->i_stderr);
1486 1559 free(ctx->i_stderr);
1487 1560
1488 1561 /*
1489 1562 * cc returns an error code when given -V; we want that to succeed.
↓ open down ↓ |
10 lines elided |
↑ open up ↑ |
1490 1563 */
1491 1564 if (ctx->i_flags & CW_F_PROG)
1492 1565 return (0);
1493 1566
1494 1567 return (ret);
1495 1568 }
1496 1569
1497 1570 static int
1498 1571 exec_ctx(cw_ictx_t *ctx, int block)
1499 1572 {
1500 - char *file;
1501 -
1502 - /*
1503 - * To avoid offending cc's sensibilities, the name of its output
1504 - * file must end in '.o'.
1505 - */
1506 - if ((file = tempnam(NULL, ".cw")) == NULL) {
1507 - nomem();
1508 - return (-1);
1509 - }
1510 - (void) strlcpy(ctx->i_discard, file, MAXPATHLEN);
1511 - (void) strlcat(ctx->i_discard, ".o", MAXPATHLEN);
1512 - free(file);
1513 -
1514 1573 if ((ctx->i_stderr = tempnam(NULL, ".cw")) == NULL) {
1515 1574 nomem();
1516 1575 return (-1);
1517 1576 }
1518 1577
1519 1578 if ((ctx->i_pid = fork()) == 0) {
1520 1579 int fd;
1521 1580
1522 1581 (void) fclose(stderr);
1523 1582 if ((fd = open(ctx->i_stderr, O_WRONLY | O_CREAT | O_EXCL,
1524 1583 0666)) < 0) {
1525 1584 err(1, "open failed for standard error");
1526 1585 }
1527 1586 if (dup2(fd, 2) < 0) {
1528 1587 err(1, "dup2 failed for standard error");
1529 1588 }
1530 1589 if (fd != 2)
1531 1590 (void) close(fd);
1532 1591 if (freopen("/dev/fd/2", "w", stderr) == NULL) {
1533 1592 err(1, "freopen failed for /dev/fd/2");
1534 1593 }
1535 1594
1536 1595 prepctx(ctx);
1537 1596 exit(invoke(ctx));
1538 1597 }
1539 1598
1540 1599 if (ctx->i_pid < 0) {
1541 1600 err(1, "fork failed");
1542 1601 }
1543 1602
1544 1603 if (block)
1545 1604 return (reap(ctx));
1546 1605
1547 1606 return (0);
1548 1607 }
1549 1608
1550 1609 static void
1551 1610 parse_compiler(const char *spec, cw_compiler_t *compiler)
1552 1611 {
1553 1612 char *tspec, *token;
1554 1613
1555 1614 if ((tspec = strdup(spec)) == NULL)
1556 1615 nomem();
1557 1616
1558 1617 if ((token = strsep(&tspec, ",")) == NULL)
1559 1618 errx(1, "Compiler is missing a name: %s", spec);
1560 1619 compiler->c_name = token;
1561 1620
1562 1621 if ((token = strsep(&tspec, ",")) == NULL)
1563 1622 errx(1, "Compiler is missing a path: %s", spec);
1564 1623 compiler->c_path = token;
1565 1624
1566 1625 if ((token = strsep(&tspec, ",")) == NULL)
1567 1626 errx(1, "Compiler is missing a style: %s", spec);
1568 1627
1569 1628 if ((strcasecmp(token, "gnu") == 0) ||
1570 1629 (strcasecmp(token, "gcc") == 0))
1571 1630 compiler->c_style = GNU;
1572 1631 else if ((strcasecmp(token, "sun") == 0) ||
1573 1632 (strcasecmp(token, "cc") == 0))
1574 1633 compiler->c_style = SUN;
1575 1634 else
1576 1635 errx(1, "unknown compiler style: %s", token);
1577 1636
1578 1637 if (tspec != NULL)
1579 1638 errx(1, "Excess tokens in compiler: %s", spec);
1580 1639 }
1581 1640
1582 1641 int
1583 1642 main(int argc, char **argv)
1584 1643 {
1585 1644 int ch;
1586 1645 cw_compiler_t primary = { NULL, NULL, 0 };
1587 1646 cw_compiler_t shadows[10];
1588 1647 int nshadows = 0;
1589 1648 int ret = 0;
1590 1649 boolean_t do_serial = B_FALSE;
1591 1650 boolean_t do_exec = B_FALSE;
1592 1651 boolean_t vflg = B_FALSE;
1593 1652 boolean_t Cflg = B_FALSE;
1594 1653 boolean_t cflg = B_FALSE;
1595 1654 boolean_t nflg = B_FALSE;
1596 1655
1597 1656 cw_ictx_t *main_ctx;
1598 1657
1599 1658 static struct option longopts[] = {
1600 1659 { "compiler", no_argument, NULL, 'c' },
1601 1660 { "noecho", no_argument, NULL, 'n' },
1602 1661 { "primary", required_argument, NULL, 'p' },
1603 1662 { "shadow", required_argument, NULL, 's' },
1604 1663 { "versions", no_argument, NULL, 'v' },
1605 1664 { NULL, 0, NULL, 0 },
1606 1665 };
1607 1666
1608 1667
1609 1668 if ((main_ctx = newictx()) == NULL)
1610 1669 nomem();
1611 1670
1612 1671 while ((ch = getopt_long(argc, argv, "C", longopts, NULL)) != -1) {
1613 1672 switch (ch) {
1614 1673 case 'c':
1615 1674 cflg = B_TRUE;
1616 1675 break;
1617 1676 case 'C':
1618 1677 Cflg = B_TRUE;
1619 1678 break;
1620 1679 case 'n':
1621 1680 nflg = B_TRUE;
1622 1681 break;
1623 1682 case 'p':
1624 1683 if (primary.c_path != NULL) {
1625 1684 warnx("Only one primary compiler may "
1626 1685 "be specified");
1627 1686 usage();
1628 1687 }
1629 1688
1630 1689 parse_compiler(optarg, &primary);
1631 1690 break;
1632 1691 case 's':
1633 1692 if (nshadows >= 10)
1634 1693 errx(1, "May only use 10 shadows at "
1635 1694 "the moment");
1636 1695 parse_compiler(optarg, &shadows[nshadows]);
1637 1696 nshadows++;
1638 1697 break;
1639 1698 case 'v':
1640 1699 vflg = B_TRUE;
1641 1700 break;
1642 1701 default:
1643 1702 (void) fprintf(stderr, "Did you forget '--'?\n");
1644 1703 usage();
1645 1704 }
1646 1705 }
1647 1706
1648 1707 if (primary.c_path == NULL) {
1649 1708 warnx("A primary compiler must be specified");
1650 1709 usage();
1651 1710 }
1652 1711
1653 1712 do_serial = (getenv("CW_SHADOW_SERIAL") == NULL) ? B_FALSE : B_TRUE;
1654 1713 do_exec = (getenv("CW_NO_EXEC") == NULL) ? B_TRUE : B_FALSE;
1655 1714
1656 1715 /* Leave room for argv[0] */
1657 1716 argc -= (optind - 1);
1658 1717 argv += (optind - 1);
1659 1718
1660 1719 main_ctx->i_oldargc = argc;
1661 1720 main_ctx->i_oldargv = argv;
1662 1721 main_ctx->i_flags = CW_F_XLATE;
1663 1722 if (nflg == 0)
1664 1723 main_ctx->i_flags |= CW_F_ECHO;
1665 1724 if (do_exec)
1666 1725 main_ctx->i_flags |= CW_F_EXEC;
1667 1726 if (Cflg)
1668 1727 main_ctx->i_flags |= CW_F_CXX;
1669 1728 main_ctx->i_compiler = &primary;
1670 1729
1671 1730 if (cflg) {
1672 1731 (void) fputs(primary.c_path, stdout);
1673 1732 }
1674 1733
1675 1734 if (vflg) {
1676 1735 (void) printf("cw version %s\n", CW_VERSION);
1677 1736 (void) fflush(stdout);
1678 1737 main_ctx->i_flags &= ~CW_F_ECHO;
1679 1738 main_ctx->i_flags |= CW_F_PROG | CW_F_EXEC;
1680 1739 do_serial = 1;
1681 1740 }
1682 1741
1683 1742 ret |= exec_ctx(main_ctx, do_serial);
1684 1743
1685 1744 for (int i = 0; i < nshadows; i++) {
1686 1745 int r;
1687 1746 cw_ictx_t *shadow_ctx;
1688 1747
1689 1748 if ((shadow_ctx = newictx()) == NULL)
1690 1749 nomem();
1691 1750
1692 1751 memcpy(shadow_ctx, main_ctx, sizeof (cw_ictx_t));
1693 1752
1694 1753 shadow_ctx->i_flags |= CW_F_SHADOW;
1695 1754 shadow_ctx->i_compiler = &shadows[i];
1696 1755
1697 1756 r = exec_ctx(shadow_ctx, do_serial);
1698 1757 if (r == 0) {
1699 1758 shadow_ctx->i_next = main_ctx->i_next;
1700 1759 main_ctx->i_next = shadow_ctx;
1701 1760 }
1702 1761 ret |= r;
1703 1762 }
1704 1763
1705 1764 if (!do_serial) {
1706 1765 cw_ictx_t *next = main_ctx;
1707 1766 while (next != NULL) {
1708 1767 cw_ictx_t *toreap = next;
1709 1768 next = next->i_next;
1710 1769 ret |= reap(toreap);
1711 1770 }
1712 1771 }
1713 1772
1714 1773 return (ret);
1715 1774 }
↓ open down ↓ |
192 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX