forked from fuhsnn/slimcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
1502 lines (1268 loc) · 35.6 KB
/
main.c
File metadata and controls
1502 lines (1268 loc) · 35.6 KB
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "slimcc.h"
typedef enum {
FILE_NONE = 0,
FILE_C,
FILE_ASM,
FILE_PP_ASM,
FILE_LDARG,
} FileType;
typedef enum {
PR_NORMAL = 0,
PR_NOFORK,
PR_PIPE,
PR_HHH,
} ProcMode;
typedef struct {
const char *arg;
bool is_def;
} MacroChange;
typedef struct {
MacroChange *data;
int capacity;
int len;
} MacroChangeArr;
StringArray include_paths;
StringArray iquote_paths;
StringArray display_files;
bool opt_fcommon;
int opt_fpic;
int opt_fpie;
bool opt_femulated_tls;
int opt_fn_align = 1;
bool opt_use_plt = true;
bool opt_optimize = true;
bool opt_reuse_stack = true;
bool opt_g;
bool opt_func_sections;
bool opt_data_sections;
bool opt_werror;
bool opt_cc1_asm_pp;
const char *opt_visibility;
StdVer opt_std = STD_C17;
bool is_iso_std;
bool opt_fdefer_ts;
bool opt_short_enums;
bool opt_gnu_keywords;
bool opt_gnu89_inline;
bool opt_ms_anon_struct;
bool opt_disable_visibility;
bool opt_fake_always_inline;
static StringArray opt_imacros;
static StringArray opt_include;
bool opt_E;
bool opt_dM;
static bool opt_P;
static bool opt_M;
static bool opt_MM;
static bool opt_MD;
static bool opt_MMD;
static bool opt_MG;
static bool opt_MP;
static bool opt_S;
static bool opt_c;
static bool opt_verbose;
bool opt_pie;
bool opt_nopie;
bool opt_pthread;
bool opt_r;
bool opt_rdynamic;
bool opt_static;
bool opt_static_pie;
bool opt_static_libgcc;
bool opt_shared;
bool opt_s;
bool opt_nostartfiles;
bool opt_nodefaultlibs;
bool opt_nolibc;
const char *default_ld = "ld";
const char *default_as = "as";
const char *dumpmachine_str;
static const char *opt_use_ld;
static const char *opt_use_as;
static const char *opt_MF;
static const char *opt_MT;
static const char *opt_o;
static StringArray ld_paths;
static StringArray input_args;
static StringArray sysincl_paths;
static StringArray dep_files;
static StringArray tmpfiles;
static const char *tmp_folder;
static StringArray as_args;
static MacroChangeArr macrodefs;
static int incl_cnt;
static ProcMode proc_mode;
static bool is_fork_child;
char *argv0;
static void cc1(const char *input_file, const char *output, bool is_asm_pp);
#if defined(USE_ASAN)
__attribute__((visibility("default"))) const char *__asan_default_options(void) {
return "detect_leaks=0";
}
#endif
#if defined(USE_UBSAN)
__attribute__((visibility("default"))) const char *__ubsan_default_options(void) {
return "print_stacktrace=1";
}
#endif
static void version(void) {
puts("slimcc version 0.0");
}
static bool startswith(const char *arg, const char **p, const char *str) {
size_t len = strlen(str);
if (!strncmp(arg, str, len)) {
*p = arg + len;
return true;
}
return false;
}
static bool take_arg(char **argv, int *i, const char **arg, const char *opt) {
if (strcmp(argv[*i], opt))
return false;
*i += 1;
if (argv[*i]) {
*arg = argv[*i];
return true;
}
fprintf(stderr, "missing argument to %s\n", opt);
exit(1);
}
static bool take_arg_s(char **argv, int *i, const char **p, const char *str) {
return take_arg(argv, i, p, str) || startswith(argv[*i], p, str);
}
static bool comma_arg(const char *arg, StringArray *arr, const char *str) {
if (startswith(arg, &arg, str)) {
arg = strtok(strdup(arg), ",");
while (arg) {
strarray_push(arr, arg);
arg = strtok(NULL, ",");
}
return true;
}
return false;
}
void add_include_path(StringArray *arr, const char *path) {
size_t orig_len = strlen(path);
size_t len = orig_len;
while (len > 1 && path[len - 1] == '/')
len--;
for (int i = 0; i < arr->len; i++) {
const char *s2 = arr->data[i];
if (!strncmp(s2, path, len) && s2[len] == '\0')
return;
}
if (len != orig_len)
path = strndup(path, len);
strarray_push(arr, path);
}
static FileType parse_opt_x(const char *s) {
if (!strcmp(s, "c"))
return FILE_C;
if (!strcmp(s, "assembler"))
return FILE_ASM;
if (!strcmp(s, "assembler-with-cpp"))
return FILE_PP_ASM;
if (!strcmp(s, "none"))
return FILE_NONE;
error("<command line>: unknown argument for -x: %s", s);
}
static bool set_bool(const char *p, bool val, const char *str, bool *opt) {
if (!strcmp(p, str)) {
*opt = val;
return true;
}
return false;
}
static bool set_true(const char *p, const char *str, bool *opt) {
return set_bool(p, true, str, opt);
}
static void set_std(bool is_iso, const char *arg) {
char *end;
int val = strtoul(arg, &end, 10);
if (end - arg == 2) {
is_iso_std = is_iso;
switch (val) {
case 89:
case 90: opt_std = STD_C89; return;
case 99: opt_std = STD_C99; return;
case 11: opt_std = STD_C11; return;
case 17:
case 18: opt_std = STD_C17; return;
case 23: opt_std = STD_C23; return;
}
}
error("unknown c standard");
}
static void set_std_iso(const char *arg) {
char *end;
int val = strtoul(arg, &end, 10);
if (end != arg) {
is_iso_std = true;
switch (val) {
case 1990: opt_std = STD_C89; return;
case 199409: opt_std = STD_C94; return;
case 1999: opt_std = STD_C99; return;
case 2011: opt_std = STD_C11; return;
case 2017:
case 2018: opt_std = STD_C17; return;
case 2024: opt_std = STD_C23; return;
}
}
error("unknown c standard");
}
static void macrochange_push(MacroChangeArr *arr, const char *arg, bool is_def) {
if (arr->len == arr->capacity) {
arr->capacity += 8;
arr->data = realloc(arr->data, sizeof(MacroChange) * arr->capacity);
}
MacroChange *m = &arr->data[arr->len++];
m->arg = arg;
m->is_def = is_def;
}
static void cli_macros(bool is_asm_pp) {
if (is_asm_pp) {
define_macro("__ASSEMBLER__", "1");
} else {
if (is_iso_std)
define_macro("__STRICT_ANSI__", "1");
switch (opt_std) {
case STD_C94: define_macro("__STDC_VERSION__", "199409L"); break;
case STD_C99: define_macro("__STDC_VERSION__", "199901L"); break;
case STD_C11: define_macro("__STDC_VERSION__", "201112L"); break;
case STD_C17: define_macro("__STDC_VERSION__", "201710L"); break;
case STD_C23: define_macro("__STDC_VERSION__", "202311L"); break;
}
if (opt_std >= STD_C99)
define_macro("__GNUC_STDC_INLINE__", "1");
define_macro("__STDC_UTF_16__", "1");
define_macro("__STDC_UTF_32__", "1");
define_macro("__STDC_DEFER_TS25755__", opt_fdefer_ts ? "2" : "1");
}
if (opt_pthread)
define_macro("_REENTRANT", "1");
if (opt_fpic == 1) {
define_macro("__pic__", "1");
define_macro("__PIC__", "1");
} else if (opt_fpic == 2) {
define_macro("__pic__", "2");
define_macro("__PIC__", "2");
} else if (opt_fpie == 1) {
define_macro("__pic__", "1");
define_macro("__PIC__", "1");
define_macro("__pie__", "1");
define_macro("__PIE__", "1");
} else if (opt_fpie == 2) {
define_macro("__pic__", "2");
define_macro("__PIC__", "2");
define_macro("__pie__", "2");
define_macro("__PIE__", "2");
}
for (int i = 0; i < macrodefs.len; i++) {
MacroChange *m = ¯odefs.data[i];
if (m->is_def)
define_macro_cli(m->arg);
else
undef_macro(m->arg);
}
}
static char *quote_makefile(const char *s) {
char *buf = calloc(1, strlen(s) * 2 + 1);
for (int i = 0, j = 0; s[i]; i++) {
switch (s[i]) {
case '$':
buf[j++] = '$';
buf[j++] = '$';
break;
case '#':
buf[j++] = '\\';
buf[j++] = '#';
break;
case ' ':
case '\t':
for (int k = i - 1; k >= 0 && s[k] == '\\'; k--)
buf[j++] = '\\';
buf[j++] = '\\';
buf[j++] = s[i];
break;
default: {
buf[j++] = s[i];
break;
}
}
}
return buf;
}
static void build_incl_paths(const char *opt_B, bool opt_nostdinc, StringArray *isystem,
StringArray *idirafter) {
if (opt_B)
add_include_path(&sysincl_paths, opt_B);
for (int i = 0; i < isystem->len; i++)
add_include_path(&sysincl_paths, isystem->data[i]);
if (!opt_nostdinc)
platform_stdinc_paths(&sysincl_paths);
for (int i = 0; i < idirafter->len; i++)
add_include_path(&sysincl_paths, idirafter->data[i]);
// Filter system directories passed as -I
for (int i = 0; i < include_paths.len; i++) {
bool match = false;
for (int j = 0; j < sysincl_paths.len; j++)
if ((match = !strcmp(sysincl_paths.data[j], include_paths.data[i])))
break;
if (!match)
include_paths.data[incl_cnt++] = include_paths.data[i];
}
include_paths.len = incl_cnt;
for (int i = 0; i < sysincl_paths.len; i++)
strarray_push(&include_paths, sysincl_paths.data[i]);
}
static void build_ld_paths(const char *opt_B, StringArray *paths) {
if (opt_B)
strarray_push(&ld_paths, opt_B);
for (int i = 0; i < paths->len; i++)
strarray_push(&ld_paths, paths->data[i]);
platform_search_dirs(&ld_paths);
}
static void parse_args(int argc, char **argv, bool *run_ld) {
const char *arg;
int input_cnt = 0;
const char *opt_B = NULL;
bool opt_pipe = false;
bool opt_hash_hash_hash = false;
bool has_wl = false;
bool has_gnu_keywords_option = false;
bool opt_nostdinc = false;
StringArray libpaths = {0};
StringArray isystem = {0};
StringArray idirafter = {0};
for (int i = 1; i < argc; i++) {
if (*argv[i] == '\0')
continue;
if (*argv[i] != '-' || argv[i][1] == '\0') {
strarray_push(&input_args, argv[i]);
input_cnt++;
continue;
}
if (!strcmp(argv[i], "-###")) {
opt_hash_hash_hash = true;
continue;
}
if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose")) {
opt_verbose = true;
continue;
}
if (!strcmp(argv[i], "--help")) {
puts("slimcc [ -o <path> ] <file>");
exit(0);
}
if (!strcmp(argv[i], "--version")) {
version();
exit(0);
}
if (!strcmp(argv[i], "-dumpmachine")) {
if (!dumpmachine_str)
error("'-dumpmachine' not configured");
puts(dumpmachine_str);
exit(0);
}
if (!strcmp(argv[i], "-print-search-dirs") ||
!strcmp(argv[i], "--print-search-dirs")) {
StringArray dirs = {0};
platform_search_dirs(&dirs);
printf("libraries: =");
for (int i = 0; i < dirs.len; i++)
printf("%s%s", dirs.data[i], (i + 1 != dirs.len) ? ":" : "\n");
exit(0);
}
if (!strcmp(argv[i], "-hashmap-test")) {
hashmap_test();
exit(0);
}
if (take_arg_s(argv, &i, &arg, "-o")) {
opt_o = arg;
continue;
}
if (!strcmp(argv[i], "-S")) {
opt_S = true;
continue;
}
if (!strcmp(argv[i], "-c")) {
opt_c = true;
continue;
}
if (!strcmp(argv[i], "-E")) {
opt_E = true;
continue;
}
if (!strcmp(argv[i], "-P")) {
opt_P = true;
continue;
}
if (!strcmp(argv[i], "-dM")) {
opt_dM = true;
continue;
}
if (take_arg_s(argv, &i, &opt_B, "-B"))
continue;
if (take_arg_s(argv, &i, &arg, "-I")) {
add_include_path(&include_paths, arg);
continue;
}
if (take_arg_s(argv, &i, &arg, "-isystem")) {
strarray_push(&isystem, arg);
continue;
}
if (take_arg_s(argv, &i, &arg, "-idirafter")) {
strarray_push(&idirafter, arg);
continue;
}
if (take_arg_s(argv, &i, &arg, "-iquote")) {
add_include_path(&iquote_paths, arg);
continue;
}
if (take_arg_s(argv, &i, &arg, "-D")) {
macrochange_push(¯odefs, arg, true);
continue;
}
if (take_arg_s(argv, &i, &arg, "-U")) {
macrochange_push(¯odefs, arg, false);
continue;
}
if (take_arg_s(argv, &i, &arg, "-imacros")) {
strarray_push(&opt_imacros, arg);
continue;
}
if (take_arg_s(argv, &i, &arg, "-include")) {
strarray_push(&opt_include, arg);
continue;
}
if (take_arg_s(argv, &i, &arg, "-x")) {
strarray_push(&input_args, "-x");
strarray_push(&input_args, arg);
continue;
}
if (take_arg_s(argv, &i, &arg, "-L")) {
strarray_push(&libpaths, arg);
continue;
}
if (comma_arg(argv[i], &as_args, "-Wa,"))
continue;
if (startswith(argv[i], &arg, "-Wl,")) {
strarray_push(&input_args, argv[i]);
has_wl = true;
continue;
}
if (take_arg_s(argv, &i, &arg, "-l")) {
strarray_push(&input_args, format("-Wl,-l%s", arg));
has_wl = true;
continue;
}
if (take_arg(argv, &i, &arg, "-Xlinker")) {
strarray_push(&input_args, format("-Wl,%s", arg));
has_wl = true;
continue;
}
if (take_arg_s(argv, &i, &arg, "-z")) {
strarray_push(&input_args, format("-Wl,-z,%s", arg));
has_wl = true;
continue;
}
if (!strcmp(argv[i], "-s")) {
opt_s = true;
continue;
}
if (!strcmp(argv[i], "-M")) {
opt_M = true;
continue;
}
if (!strcmp(argv[i], "-MM")) {
opt_M = opt_MM = true;
continue;
}
if (!strcmp(argv[i], "-MD")) {
opt_MD = true;
continue;
}
if (!strcmp(argv[i], "-MMD")) {
opt_MD = opt_MMD = true;
continue;
}
if (startswith(argv[i], &arg, "-Wp,-MD,")) {
opt_MD = true;
opt_MF = arg;
continue;
}
if (startswith(argv[i], &arg, "-Wp,-MMD,")) {
opt_MD = opt_MMD = true;
opt_MF = arg;
continue;
}
if (!strcmp(argv[i], "-MG")) {
opt_MG = true;
continue;
}
if (!strcmp(argv[i], "-MP")) {
opt_MP = true;
continue;
}
if (take_arg_s(argv, &i, &arg, "-MF")) {
opt_MF = arg;
continue;
}
if (take_arg_s(argv, &i, &arg, "-MT") || startswith(argv[i], &arg, "-Wp,-MT,")) {
if (opt_MT == NULL)
opt_MT = arg;
else
opt_MT = format("%s %s", opt_MT, arg);
continue;
}
if (take_arg_s(argv, &i, &arg, "-MQ") || startswith(argv[i], &arg, "-Wp,-MQ,")) {
if (opt_MT == NULL)
opt_MT = quote_makefile(arg);
else
opt_MT = format("%s %s", opt_MT, quote_makefile(arg));
continue;
}
if (startswith(argv[i], &arg, "-g")) {
opt_g = strcmp(arg, "0");
continue;
}
if (startswith(argv[i], &arg, "-O")) {
switch (*arg) {
case 's':
case 'z': {
opt_fn_align = 1;
break;
}
case '2':
case '3':
if (opt_fn_align == 1)
opt_fn_align = 16;
break;
}
continue;
}
if (!strcmp(argv[i], "-ansi")) {
set_std(true, "89");
continue;
}
if (startswith(argv[i], &arg, "-std=") ||
startswith(argv[i], &arg, "--std=") ||
take_arg(argv, &i, &arg, "--std")) {
if (startswith(arg, &arg, "c"))
set_std(true, arg);
else if (startswith(arg, &arg, "gnu"))
set_std(false, arg);
else if (startswith(arg, &arg, "iso9899:"))
set_std_iso(arg);
else
error("unknown c standard");
continue;
}
if (startswith(argv[i], &arg, "-f")) {
bool b = !startswith(arg, &arg, "no-");
if (set_bool(arg, b, "common", &opt_fcommon) ||
set_bool(arg, b, "plt", &opt_use_plt) ||
set_bool(arg, b, "function-sections", &opt_func_sections) ||
set_bool(arg, b, "data-sections", &opt_data_sections) ||
set_bool(arg, b, "emulated-tls", &opt_femulated_tls) ||
set_bool(arg, b, "short-enums", &opt_short_enums) ||
set_bool(arg, b, "gnu89-inline", &opt_gnu89_inline))
continue;
if (set_bool(arg, b, "ms-anon-struct", &opt_ms_anon_struct))
continue;
if (!strcmp(arg, "asm") || !strcmp(arg, "gnu-keywords")) {
opt_gnu_keywords = b;
has_gnu_keywords_option = true;
continue;
}
if (!strncmp(arg, "align-functions", 15)) {
arg += 15;
if (*arg == '\0') {
opt_fn_align = b ? 16 : 1;
continue;
}
if (b && *arg == '=') {
char *end;
if (!(opt_fn_align = strtoul(arg + 1, &end, 10)))
opt_fn_align = 16;
if (*end == '\0' && is_pow_of_two(opt_fn_align))
continue;
}
error("unsupported form of '-falign-functions'");
}
if (!strcmp(arg, "pic")) {
opt_fpic = 1 * b, opt_fpie = 0;
continue;
}
if (!strcmp(arg, "PIC")) {
opt_fpic = 2 * b, opt_fpie = 0;
continue;
}
if (!strcmp(arg, "pie")) {
opt_fpic = 0, opt_fpie = 1 * b;
continue;
}
if (!strcmp(arg, "PIE")) {
opt_fpic = 0, opt_fpie = 2 * b;
continue;
}
// -f only options
if (b) {
if (set_true(arg, "defer-ts", &opt_fdefer_ts))
continue;
if (set_bool(arg, false, "signed-char", &ty_pchar->is_unsigned) ||
set_bool(arg, true, "unsigned-char", &ty_pchar->is_unsigned))
continue;
if (startswith(arg, &arg, "stack-reuse=")) {
opt_reuse_stack = !strcmp(arg, "all");
continue;
}
if (set_true(arg, "disable-visibility", &opt_disable_visibility))
continue;
if (set_true(arg, "fake-always-inline", &opt_fake_always_inline))
continue;
if (startswith(arg, &opt_visibility, "visibility=") ||
startswith(arg, &opt_use_as, "use-as="))
continue;
if (startswith(arg, &arg, "use-ld=")) {
if (!strcmp(arg, "lld")) {
opt_use_ld = "ld.lld";
continue;
}
opt_use_ld = arg;
continue;
}
}
}
if (argv[i][0] == '-') {
arg = (argv[i][1] == '-') ? &argv[i][2] : &argv[i][1];
if (set_true(arg, "r", &opt_r) ||
set_true(arg, "rdynamic", &opt_rdynamic) ||
set_true(arg, "static", &opt_static) ||
set_true(arg, "static-pie", &opt_static_pie) ||
set_true(arg, "static-libgcc", &opt_static_libgcc) ||
set_true(arg, "shared", &opt_shared) ||
set_true(arg, "pie", &opt_pie) ||
set_true(arg, "nopie", &opt_nopie) ||
set_true(arg, "pthread", &opt_pthread) ||
set_true(arg, "pipe", &opt_pipe))
continue;
if (!strcmp(arg, "no-pie")) {
opt_pie = false;
opt_nopie = true;
continue;
}
}
if (!strcmp(argv[i], "-nostdinc")) {
opt_nostdinc = true;
continue;
}
if (set_true(argv[i], "-nostartfiles", &opt_nostartfiles) ||
set_true(argv[i], "-nodefaultlibs", &opt_nodefaultlibs) ||
set_true(argv[i], "-nolibc", &opt_nolibc))
continue;
if (!strcmp(argv[i], "-nostdlib")) {
opt_nostartfiles = opt_nodefaultlibs = true;
continue;
}
if (set_bool(argv[i], true, "-Werror", &opt_werror) ||
set_bool(argv[i], false, "-Wno-error", &opt_werror))
continue;
if (startswith(argv[i], &arg, "-W")) {
if (strchr(arg, ','))
error("unknown argument: %s", argv[i]);
continue;
}
// These options are ignored for now.
if (startswith(argv[i], &arg, "-march=") ||
!strcmp(argv[i], "-fdollars-in-identifiers") ||
!strcmp(argv[i], "-ffreestanding") ||
!strcmp(argv[i], "-ffp-contract=off") ||
!strcmp(argv[i], "-fno-builtin") ||
!strcmp(argv[i], "-fno-fast-math") ||
!strcmp(argv[i], "-fno-lto") ||
!strcmp(argv[i], "-fno-asynchronous-unwind-tables") ||
!strcmp(argv[i], "-fno-delete-null-pointer-checks") ||
!strcmp(argv[i], "-fno-exceptions") ||
!strcmp(argv[i], "-fno-omit-frame-pointer") ||
!strcmp(argv[i], "-fno-stack-protector") ||
!strcmp(argv[i], "-fno-strict-aliasing") ||
!strcmp(argv[i], "-fno-strict-overflow") ||
!strcmp(argv[i], "-fwrapv") ||
!strcmp(argv[i], "-m64") ||
!strcmp(argv[i], "-malign-double") ||
!strcmp(argv[i], "-mfpmath=sse") ||
!strcmp(argv[i], "-mno-red-zone") ||
!strcmp(argv[i], "-pedantic") ||
!strcmp(argv[i], "--pedantic") ||
!strcmp(argv[i], "-pedantic-errors") ||
!strcmp(argv[i], "--pedantic-errors") ||
!strcmp(argv[i], "-w"))
continue;
error("unknown argument: %s", argv[i]);
}
if (!opt_E && opt_dM)
error("option -dM without -E not supported");
if (opt_MG && !opt_M)
error("option -MG must be used with -M or -MM");
if (opt_disable_visibility && opt_visibility)
error("-fvisibility disabled with -fdisable-visibility");
if (!has_gnu_keywords_option)
opt_gnu_keywords = !is_iso_std;
if (opt_B) {
char *as_b = format("%s/%s", opt_B, default_as);
char *ld_b = format("%s/%s", opt_B, default_ld);
if (file_exists(as_b))
default_as = as_b;
if (file_exists(ld_b))
default_ld = ld_b;
}
build_incl_paths(opt_B, opt_nostdinc, &isystem, &idirafter);
build_ld_paths(opt_B, &libpaths);
bool no_input = !input_cnt && !has_wl;
if (opt_hash_hash_hash || opt_verbose) {
version();
if (no_input)
exit(0);
}
if (no_input)
error("no input files");
if (opt_hash_hash_hash)
proc_mode = PR_HHH;
else if (opt_pipe)
proc_mode = PR_PIPE;
else if (input_cnt == 1)
proc_mode = PR_NOFORK;
*run_ld = has_wl && !(opt_c || opt_S || opt_E);
}
static FILE *open_file(const char *path) {
if (!path || strcmp(path, "-") == 0)
return stdout;
FILE *out = fopen(path, "w");
if (!out)
error("cannot open output file: %s: %s", path, strerror(errno));
return out;
}
static void close_file(FILE *file) {
if (file == stdout)
fflush(file);
else
fclose(file);
}
static bool endswith(const char *p, const char *q) {
int len1 = strlen(p);
int len2 = strlen(q);
return (len1 >= len2) && !strcmp(p + len1 - len2, q);
}
// Replace file extension
static const char *replace_extn(const char *tmpl, const char *extn) {
char *filename = basename(strdup(tmpl));
char *dot = strrchr(filename, '.');
if (dot)
*dot = '\0';
return format("%s%s", filename, extn);
}
static void cleanup(void) {
for (int i = 0; i < tmpfiles.len; i++)
unlink(tmpfiles.data[i]);
if (tmp_folder)
rmdir(tmp_folder);
}
void cleanup_exit(int status) {
if (is_fork_child)
_exit(status);
cleanup();
exit(status);
}
static char *create_tmpfile(void) {
if (!tmp_folder) {
tmp_folder = mkdtemp(strdup("/tmp/slimcc-XXXXXX"));
if (!tmp_folder)
error("mkdtemp failed: %s", strerror(errno));
}
static int64_t i;
char *path = format("%s/%" PRIi64, tmp_folder, i++);
strarray_push(&tmpfiles, path);
return path;
}
static char *create_pipefile(void) {
char *path = create_tmpfile();
if (mkfifo(path, (S_IRUSR | S_IWUSR)) == -1)
error("mkfifo failed: %s", strerror(errno));
return path;
}
void run_subprocess(const char **argv) {
if (opt_verbose || proc_mode == PR_HHH) {
fprintf(stderr, "\"%s\"", argv[0]);
for (int i = 1; argv[i]; i++)
fprintf(stderr, " \"%s\"", argv[i]);
fprintf(stderr, "\n");
if (proc_mode == PR_HHH)
return;
}
extern char **environ;
pid_t id;
int status;
if (posix_spawnp(&id, argv[0], NULL, NULL, (char *const *)argv, environ) ||
waitpid(id, &status, 0) <= 0 ||
status != 0) {
fprintf(stderr, "exec failed: %s\n", argv[0]);
cleanup_exit(1);
}
}
static int fork_cc1(const char *input, const char *output, bool is_asm_pp) {
pid_t id = fork();
if (id == 0) {
is_fork_child = true;
cc1(input, output, is_asm_pp);
_exit(0);
}
return id;
}
static void run_cc1(const char *input, const char *output, bool is_asm_pp) {
if (proc_mode == PR_HHH)
return;
if (proc_mode == PR_NOFORK) {
cc1(input, output, is_asm_pp);
return;
}
int id = fork_cc1(input, output, is_asm_pp);
int status;
if (waitpid(id, &status, 0) <= 0 || status != 0)
cleanup_exit(1);
}
static void run_cc1_as(const char *input, const char *output, bool is_asm_pp) {
if (proc_mode == PR_HHH) {
char *tmp = create_tmpfile();
run_assembler(&as_args, tmp, output);
return;
}
if (proc_mode == PR_PIPE) {
char *tmp = create_pipefile();
int id = fork_cc1(input, tmp, is_asm_pp);
run_assembler(&as_args, tmp, output);
int status;
if (waitpid(id, &status, 0) <= 0 || status != 0)
cleanup_exit(1);
return;
}