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