forked from haiku/buildtools
/
fatelf-gcc.c
800 lines (660 loc) · 26.1 KB
/
fatelf-gcc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*
* Copyright 2012, Landon Fuller <landonf@bikemonkey.org>.
* All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Implements a front-end arch-aware driver for gcc(1).
* Models the functionality of Apple's 'driverdriver.c' in gcc-5666.3
*/
#define FATELF_UTILS 1
#include "fatelf-utils.h"
#include <errno.h>
#include <stdbool.h>
#include <unistd.h>
static const char xarch_flag[] = "-Xarch_";
static const char default_output[] = "a.out";
static const char cmd_prefix[] = "fatelf-";
static const char fatelf_glue_cmd[] = "fatelf-glue";
#define MAX_FILE_DEPTH 10
typedef struct arg_table {
int argc;
char **argv;
size_t argv_length;
} arg_table;
typedef struct compiler {
char *fat_arch;
arg_table args;
} compiler;
typedef struct compiler_set {
/* Default arguments applied to all newly added compilers */
arg_table default_args;
/* All compilers */
compiler **compilers;
/* Number of compilers */
size_t count;
} compiler_set;
typedef struct cc_flag {
/* Name of flag */
const char *opt;
/* Flag accepts an argument */
const bool accepts_arg;
/* Flag is interpreted by the driver */
const bool driver_flag;
/* Flag is not passed to the compiler */
const bool driver_only;
/* May not be used when compiling fat */
const bool fat_nocompat;
/* May only be used with -Xarch_* */
const bool xarch_only;
/* If used, will result in output going to stdout by default. This is
* inherently incompatible with fat building. Currently -E is the only
* known example of this. The driver must be aware of this argument. */
const bool enables_stdout_output;
} cc_flag;
typedef struct arch_cc_entry {
const char *arch_flag[10];
const char *cc_arch[10];
} arch_cc_entry;
typedef struct arch_cc_march_entry {
const char *arch_flag;
const char *cc_data_model;
const char *cc_march_flag[3];
} arch_cc_march_entry;
// This table is used to perform interpretation of the gcc arguments
// before passing through to GCC. The multi-argument option list is
// needed to correctly interpret the GCC flags.
//
// If this list is out-of-sync with GCC, there's a *small* possibility of
// collision between GCC flags and their arguments (eg, '-flag -o' would
// interpret the '-o' as a flag, not an argument). The likelihood of
// this occurring is low, and we have the advantage of being able to
// update GCC and fatelf utils in lockstep.
static const cc_flag cc_flags[] = {
/* opt accepts_arg driver_flag driver_only fat_nocompat xarch_only enables_stdout */
// arguments the driver must be aware of
{ "-o", true, true, true, false, false, false },
{ "-c", false, false, false, false, false, false },
// driver-specific arguments
{ "-arch", true, true, true, false, false, false },
// fat-incompatible arguments
{ "-S", false, false, false, true, false, false },
{ "-E", false, false, false, true, false, true },
{ "-MD", false, false, false, true, false, false },
{ "-MMD", false, false, false, true, false, false },
{ "-m32", false, false, false, true, false, false },
{ "-m64", false, false, false, true, false, false },
{ "-mcpu=", false, false, false, false, true, false },
{ "-march=", false, false, false, false, true, false },
// multi-argument options
{ "-D", true, false, false, false, false, false },
{ "-U", true, false, false, false, false, false },
{ "-e", true, false, false, false, false, false },
{ "-T", true, false, false, false, false, false },
{ "-u", true, false, false, false, false, false },
{ "-I", true, false, false, false, false, false },
{ "-m", true, false, false, false, false, false },
{ "-x", true, false, false, false, false, false },
{ "-L", true, false, false, false, false, false },
{ "-A", true, false, false, false, false, false },
{ "-V", true, false, false, false, false, false },
{ "-Tdata", true, false, false, false, false, false },
{ "-Ttext", true, false, false, false, false, false },
{ "-Tbss", true, false, false, false, false, false },
{ "-include", true, false, false, false, false, false },
{ "-imacros", true, false, false, false, false, false },
{ "-aux-info",
true, false, false, false, false, false },
{ "-idirafter",
true, false, false, false, false, false },
{ "-iprefix", true, false, false, false, false, false },
{ "-iwithprefix",
true, false, false, false, false, false },
{ "-iwithprefixbefore",
true, false, false, false, false, false },
{ "-iwithprefix",
true, false, false, false, false, false },
{ "-iquote", true, false, false, false, false, false },
{ "-isystem", true, false, false, false, false, false },
{ "-isysroot",true, false, false, false, false, false },
{ "-specs", true, false, false, false, false, false },
};
// Map -arch flags to compiler architecture names.
// FIXME: These should be made non-specific to Haiku.
static const arch_cc_entry arch_cc_map[] = {
{
{ "x86_64", "i686", "i586", "i486", "i386", NULL },
{ "x86_64-unknown-haiku", "i586-pc-haiku", NULL }
},
{
{ "arm", "armv4t", "xscale", "armv5", "armv6", "armv7", NULL },
{ "arm-unknown-haiku", NULL }
},
{
{ "ppc", "ppc64", NULL },
{ "powerpc-apple-haiku", NULL }
},
{
{ "m68k", NULL },
{ "m68k-unknown-haiku", NULL }
},
};
// Map -arch flags to compiler -m(32|64) and (-march=|-mcpu=) flags
static const arch_cc_march_entry arch_cc_march_map[] = {
{ "i386", "-m32", { NULL } },
{ "i486", "-m32", { "-march=i486", NULL} },
{ "i586", "-m32", { "-march=i586", NULL} },
{ "i686", "-m32", { "-march=i686", NULL} },
{ "x86_64", "-m64", { NULL } },
{ "arm", NULL, { "-march=armv4t", NULL } },
{ "armv4t", NULL, { "-march=armv4t", NULL } },
{ "armv5", NULL, { "-march=armv5tej", NULL } },
{ "xscale", NULL, { "-march=xscale", NULL } },
{ "armv6", NULL, { "-march=armv6k", NULL } },
{ "armv7", NULL, { "-march=armv7a", NULL } },
{ "ppc601", "-m32", { "-mcpu=601", NULL } },
{ "ppc603", "-m32", { "-mcpu=603", NULL } },
{ "ppc604", "-m32", { "-mcpu=604", NULL } },
{ "ppc604e", "-m32", { "-mcpu=604e", NULL } },
{ "ppc750", "-m32", { "-mcpu=750", NULL } },
{ "ppc7400", "-m32", { "-mcpu=7400", NULL } },
{ "ppc7450", "-m32", { "-mcpu=7450", NULL } },
{ "ppc970", "-m32", { "-mcpu=970", NULL } },
{ "ppc64", "-m64", { NULL} },
};
static void parse_arguments (arg_table *input_args, arg_table *driver_args,
arg_table *nofat_args, compiler_set *compilers, bool *stdout_output,
int depth);
/* Look up the given option in the cc_flags table. */
static const cc_flag *find_flag (const char *opt)
{
int i;
for (i = 0; i < sizeof(cc_flags) / sizeof(cc_flags[0]); i++) {
const cc_flag *flag = &cc_flags[i];
size_t flen = strlen(flag->opt);
/* Special case flags that use '=' to combine the flag and
* argument. */
if (flen > 0 && flag->opt[flen-1] == '=') {
if (strncmp(opt, flag->opt, flen) == 0)
return flag;
} else if (strcmp(opt, flag->opt) == 0) {
return flag;
}
}
return NULL;
}
/* Append an argument to the provided args table. The caller is responsible
* for free()'ing the argument added to the table. */
static void append_argument (arg_table *args, const char *argument) {
/* Ensure adequate space in the buffer. We grow in blocks of
* 10 -- this number was arbitrarily chosen. */
if (args->argv == NULL) {
args->argv_length = 10;
args->argv = xmalloc(sizeof(char *) * args->argv_length);
} else if (args->argc+1 >= args->argv_length) {
args->argv_length += 10;
args->argv = xrealloc(args->argv, sizeof(char *) * args->argv_length);
}
args->argv[args->argc] = xstrdup(argument);
args->argc++;
args->argv[args->argc] = NULL;
}
/* Find a compiler matching the given arch, or return NULL */
static compiler *find_compiler (compiler_set *compilers, const char *arch) {
int i;
for (i = 0; i < compilers->count; i++) {
compiler *c = compilers->compilers[i];
if (strcmp(c->fat_arch, arch) == 0)
return c;
}
return NULL;
}
/* Append and return a new compiler to the compiler set for the given arch. If
* the architecture is already available from the compiler set, the existing
* compiler is returned. */
static compiler *append_compiler (compiler_set *compilers, const char *arch) {
compiler *c;
int i;
// Prefer an existing compiler
c = find_compiler(compilers, arch);
if (c != NULL)
return c;
// Initialize a new compiler
c = xmalloc(sizeof(*c));
memset(c, 0, sizeof(*c));
c->fat_arch = strdup(arch);
append_argument(&c->args, "gcc");
// Copy in all previously parsed arguments
for (i = 0; i < compilers->default_args.argc; i++)
append_argument(&c->args, compilers->default_args.argv[i]);
// Append to the compiler set
compilers->count++;
compilers->compilers = xrealloc(compilers->compilers, compilers->count * sizeof(*c));
compilers->compilers[compilers->count - 1] = c;
return c;
}
/* Given a fully populated compiler instance, scan the existing flags and populate
* the arch flags accordingly. */
static void append_compiler_march_flags (compiler *compiler) {
int i;
bool has_data_model = false;
bool has_march = false;
/* Determine the flags that have already been set; we don't want to override
* these */
for (i = 0; i < compiler->args.argc; i++) {
const char *arg = compiler->args.argv[i];
if (strcmp(arg, "-m32") == 0 || strcmp(arg, "-m64") == 0) {
has_data_model = true;
} else if (strstr(arg, "-march=") == arg || strstr(arg, "-mcpu=") == 0) {
has_march = true;
}
}
// Add any -m(32|64) and -march flags
for (i = 0; i < sizeof(arch_cc_march_map) / sizeof(arch_cc_march_map[0]); i++) {
const arch_cc_march_entry *entry = &arch_cc_march_map[i];
if (strcmp(compiler->fat_arch, entry->arch_flag) != 0)
continue;
if (!has_data_model && entry->cc_data_model != NULL)
append_argument(&compiler->args, entry->cc_data_model);
if (!has_march) {
int j;
for (j = 0; entry->cc_march_flag[j] != NULL; j++)
append_argument(&compiler->args, entry->cc_march_flag[j]);
}
break;
}
}
/* Append a compiler argument to the compiler set. If arch_only is non-NULL,
* the argument will only be applied to a matching compiler. New compilers
* will be automatically initialized. */
static void append_compiler_argument (compiler_set *compilers,
const char *argument,
const char *arch_only)
{
int i;
// Handle non-architecture-specific arguments
if (arch_only == NULL) {
append_argument(&compilers->default_args, argument);
for (i = 0; i < compilers->count; i++) {
append_argument(&compilers->compilers[i]->args, argument);
}
return;
}
// Fetch (or create) the compiler entry
compiler *c = NULL;
for (i = 0; i < compilers->count; i++) {
if (strcmp(compilers->compilers[i]->fat_arch, arch_only) == 0) {
c = compilers->compilers[i];
break;
}
}
if (c == NULL)
c = append_compiler(compilers, arch_only);
// Apply the compiler-specific argument
append_argument(&c->args, argument);
}
/* Parse an gcc(1) @file, which contains command line arguments, separated
* by whitespace. */
static void parse_argument_file (const char *fname, arg_table *driver_args,
arg_table *nofat_args, compiler_set *compilers, bool *stdout_output,
int depth)
{
arg_table file_args;
char optbuf[8192];
int scancount;
FILE *file;
int fd;
int i;
/* Protect against infinite recursion */
if (depth >= MAX_FILE_DEPTH)
xfail("Exceeded maximum number of supported @FILE includes in '%s'",
fname);
/* Open the input file */
fd = xopen(fname, O_RDONLY, 0);
file = fdopen(fd, "r");
/* Configure table to hold parsed arguments */
memset(&file_args, 0, sizeof(file_args));
do {
optbuf[sizeof(optbuf)-1] = '\0';
scancount = fscanf(file, "%8192s", optbuf);
if (scancount == EOF)
break;
if (scancount == 0)
xfail("Failed to parse input file: %s", fname);
if (optbuf[sizeof(optbuf)-1] != '\0')
xfail("Unable to handle options larger than 8192");
append_argument(&file_args, optbuf);
} while (1);
/* Recursively parse the input arguments */
parse_arguments(&file_args, driver_args, nofat_args, compilers,
stdout_output, depth);
/* Clean up */
xclose(fname, fd);
for (i = 0; i < file_args.argc; i++)
free(file_args.argv[i]);
}
/* Parse all arguments from input_args, populating compilers and fat_args. */
static void parse_arguments (arg_table *input_args, arg_table *driver_args,
arg_table *nofat_args, compiler_set *compilers, bool *stdout_output,
int depth)
{
int i;
*stdout_output = false;
for (i = 0; i < input_args->argc; i++) {
const char *arg = input_args->argv[i];
// Handle -Xarch_*
const char *arch_only = NULL;
if (strncmp(arg, xarch_flag, strlen(xarch_flag)) == 0) {
// Extract the target architecture
const char *p = arg + strlen(xarch_flag);
if (*p != '\0') {
arch_only = p;
// Advance to the actual flag
i++;
if (i == input_args->argc)
break;
arg = input_args->argv[i];
}
}
// Handle @file
if (arg[0] == '@' && arg[1] != '\0') {
bool stdout_result;
parse_argument_file(arg+1, driver_args, nofat_args, compilers,
&stdout_result, depth + 1);
if (stdout_result)
*stdout_output = true;
continue;
}
const cc_flag *flag = find_flag(arg);
if (flag == NULL) {
append_compiler_argument(compilers, arg, arch_only);
} else {
if (flag->driver_flag)
append_argument(driver_args, arg);
if (!flag->driver_only)
append_compiler_argument(compilers, arg, arch_only);
if (flag->enables_stdout_output)
*stdout_output = true;
if (flag->fat_nocompat)
append_argument(nofat_args, arg);
if (flag->xarch_only && arch_only == NULL)
append_argument(nofat_args, arg);
if (flag->accepts_arg) {
i++;
if (i >= input_args->argc)
xfail("argument to '%s' is missing (expected 1 value)", arg);
arg = input_args->argv[i];
if (flag->driver_flag)
append_argument(driver_args, arg);
if (!flag->driver_only)
append_compiler_argument(compilers, arg, arch_only);
}
}
}
}
/* Find a compiler/tool binary for the given arch. If arch is NULL, a
* non-arch-specific binary will be found. */
static char *find_tool_bin (const char *prefix, const char *arch,
const char *cmdname) {
int i, j, k;
if (arch == NULL) {
char *path = xmalloc(strlen(prefix) + strlen(cmdname) + 2);
strcpy(path, prefix);
strcat(path, "/");
strcat(path, cmdname);
return path;
}
for (i = 0; i < sizeof(arch_cc_map)/ sizeof(arch_cc_map[0]); i++) {
const arch_cc_entry *cc_arch = &arch_cc_map[i];
for (j = 0; cc_arch->arch_flag[j] != NULL; j++) {
if (strcmp(cc_arch->arch_flag[j], arch) == 0) {
for (k = 0; cc_arch->cc_arch[k] != NULL; k++) {
const char *cc = cc_arch->cc_arch[k];
char *path = xmalloc(strlen(prefix) +
strlen(cc) + strlen(cmdname) + 3);
strcpy(path, prefix);
strcat(path, "/");
strcat(path, cc);
strcat(path, "-");
strcat(path, cmdname);
if (access(path, X_OK) == 0)
return path;
}
}
}
}
return NULL;
}
/* Free all argument elements in the provided argument table */
static void clear_arg_table (arg_table *args) {
int i;
for (i = 0; i < args->argc; i++) {
free(args->argv[i]);
args->argv[i] = NULL;
}
args->argc = 0;
}
/* Unlink all arguments in output_files */
static void clean_output_files (arg_table *output_files) {
int i;
for (i = 0; i < output_files->argc; i++)
unlink(output_files->argv[i]);
}
/* Execute a command */
static bool exec_command (arg_table *args, int *stat_loc) {
int ret;
pid_t pid;
#if DEBUG_EXEC
int i;
for (i = 0; i < args->argc; i++)
fprintf(stderr, "%s ", args->argv[i]);
fprintf(stderr, "\n");
#endif
pid = fork();
if (pid == 0) {
execv(args->argv[0], args->argv);
xfail("exec failed: %s", strerror(errno));
}
while ((ret = wait4(pid, stat_loc, 0, NULL)) == -1) {
if (errno != EINTR)
break;
}
if (ret == -1) {
fprintf(stderr, "wait4() failed: %s\n", strerror(errno));
return false;
}
return true;
}
int main(int argc, const char **argv)
{
arg_table input_args;
arg_table driver_args;
arg_table nofat_args;
arg_table fatelf_glue_args;
arg_table temp_output_args;
char *fatelf_glue_path;
bool all_compiles_succeeded;
int stat_loc, exit_code;
compiler_set compilers;
int i;
if (argc < 1)
return 1;
/* Determine the install prefix of our binary, along with the target
* command name (gcc/g++/etc). */
char *prefix = xgetexecname(argv[0]);
char *cmdname;
{
char *prefix_tail = rindex(prefix, '/');
if (prefix_tail == NULL)
xfail("Could not find enclosing directory of path %s", prefix);
/* Strip the cmd_prefix from the command, in the case where the
* command is executed as 'fatelf-gcc' or 'fatelf-g++'. */
if (strncmp(prefix_tail+1, cmd_prefix, strlen(cmd_prefix)) == 0) {
cmdname = strdup(prefix_tail+1 + strlen(cmd_prefix));
} else {
cmdname = strdup(prefix_tail+1);
}
/* NULL terminate the prefix path */
*prefix_tail = '\0';
}
/* Initialize our input/output argument tables */
memset(&compilers, 0, sizeof(compilers));
memset(&driver_args, 0, sizeof(driver_args));
memset(&nofat_args, 0, sizeof(nofat_args));
memset(&temp_output_args, 0, sizeof(temp_output_args));
memset(&fatelf_glue_args, 0, sizeof(fatelf_glue_args));
input_args.argc = argc - 1;
input_args.argv_length = input_args.argc;
input_args.argv = (char **) argv + 1;
/* Find required tools */
fatelf_glue_path = find_tool_bin(prefix, NULL, fatelf_glue_cmd);
if (fatelf_glue_path == NULL)
xfail("Could not find %s", fatelf_glue_cmd);
/* Parse all input arguments */
bool use_stdout;
parse_arguments(&input_args, &driver_args, &nofat_args, &compilers,
&use_stdout, 0);
/* Handle any driver-specific arguments. Note that the existence
* of required flags has already been verified. */
const char *output_file = NULL;
for (i = 0; i < driver_args.argc; i++) {
const char *arg = driver_args.argv[i];
if (strcmp(arg, "-arch") == 0) {
i++;
append_compiler(&compilers, driver_args.argv[i]);
} else if (strcmp(arg, "-o") == 0) {
i++;
output_file = driver_args.argv[i];
use_stdout = false;
}
}
/* Set a default output file if none was configured above */
if (output_file == NULL && use_stdout)
output_file = "-";
else if (output_file == NULL)
output_file = default_output;
/* Report any arguments incompatible with multi-arch execution */
if (compilers.count > 1 && nofat_args.argc > 0) {
for (i = 0; i < nofat_args.argc; i++) {
const char *arg = nofat_args.argv[i];
fprintf(stderr, "%s is not supported with multiple -arch flags\n",
arg);
}
exit(1);
}
if (compilers.count > 1 && use_stdout) {
fprintf(stderr,
"Writing to stdout is not supported with multiple -arch flags\n");
exit(1);
}
/* If no FAT compilers found, add default */
if (compilers.count == 0) {
const fatelf_machine_info *machine = get_machine_from_host();
append_compiler(&compilers, machine->name);
}
/* Apply march flags */
for (i = 0; i < compilers.count; i++)
append_compiler_march_flags(compilers.compilers[i]);
/* Generate the output file template */
char *output_template = NULL;
if (!use_stdout) {
static const char temp_suffix[] = ".XXXXXX";
char *output_dir = strdup(output_file);
char *fname = NULL;
/* Find the directory containing the file, and the file name. */
char *tail = rindex(output_dir, '/');
if (tail != NULL) {
tail++;
fname = strdup(tail);
*tail = '\0';
}
if (fname != NULL) {
output_template = xmalloc(strlen(output_dir) +
strlen(fname) + strlen(temp_suffix) + 2);
strcpy(output_template, output_dir);
strcat(output_template, ".");
strcat(output_template, fname);
strcat(output_template, temp_suffix);
} else {
output_template = xmalloc(strlen(output_file) +
strlen(temp_suffix) + 2);
strcpy(output_template, ".");
strcat(output_template, output_file);
strcat(output_template, temp_suffix);
}
free(fname);
free(output_dir);
}
/* Perform initial compilation */
all_compiles_succeeded = true;
exit_code = 0;
for (i = 0; i < compilers.count; i++) {
compiler *c = compilers.compilers[i];
char *temp_out = NULL;
/* Configure compiler output path */
append_argument(&c->args, "-o");
if (compilers.count == 1) {
/* If building non-FAT, write to the output file directly */
append_argument(&c->args, output_file);
} else {
temp_out = strdup(output_template);
if (mkstemp(temp_out) == -1) {
clean_output_files(&temp_output_args);
xfail("Could not create temporary output file '%s': %s",
temp_out, strerror(errno));
}
append_argument(&c->args, temp_out);
append_argument(&temp_output_args, temp_out);
}
/* Find compiler */
free(c->args.argv[0]);
c->args.argv[0] = find_tool_bin(prefix, c->fat_arch, cmdname);
if (c->args.argv[0] == NULL) {
clean_output_files(&temp_output_args);
xfail("Could not find compiler for %s in %s",c->fat_arch, prefix);
}
if (!exec_command(&c->args, &stat_loc)) {
clean_output_files(&temp_output_args);
return 1;
}
if (WIFEXITED(stat_loc) && WEXITSTATUS(stat_loc) != 0) {
clean_output_files(&temp_output_args);
all_compiles_succeeded = false;
exit_code = WEXITSTATUS(stat_loc);
} else if (WIFSIGNALED(stat_loc)) {
clean_output_files(&temp_output_args);
all_compiles_succeeded = false;
exit_code = 1;
}
if (temp_out != NULL)
free(temp_out);
}
/* Glue the results */
if (compilers.count > 1 && all_compiles_succeeded) {
append_argument(&fatelf_glue_args, fatelf_glue_path);
append_argument(&fatelf_glue_args, output_file);
for (i = 0; i < temp_output_args.argc; i++)
append_argument(&fatelf_glue_args, temp_output_args.argv[i]);
if (!exec_command(&fatelf_glue_args, &stat_loc)) {
clean_output_files(&temp_output_args);
return 1;
}
}
/* Clean up */
clean_output_files(&temp_output_args);
clear_arg_table(&driver_args);
clear_arg_table(&nofat_args);
clear_arg_table(&fatelf_glue_args);
clear_arg_table(&temp_output_args);
for (i = 0; i < compilers.count; i++) {
compiler *c = compilers.compilers[i];
clear_arg_table(&c->args);
free(c->fat_arch);
}
clear_arg_table(&compilers.default_args);
free(fatelf_glue_path);
free(output_template);
free(prefix);
free(cmdname);
/* Return the result of the last failed compilation (or 0). */
return exit_code;
}