-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathccache.c
1389 lines (1195 loc) · 33.8 KB
/
ccache.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
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
/*
a re-implementation of the compilercache scripts in C
The idea is based on the shell-script compilercache by Erik Thiele <[email protected]>
Copyright (C) Andrew Tridgell 2002
Copyright (C) Martin Pool 2003
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "ccache.h"
/* verbose mode */
int ccache_verbose = 0;
/* the base cache directory */
char *cache_dir = NULL;
/* the directory for temporary files */
static char *temp_dir = NULL;
/* the debug logfile name, if set */
char *cache_logfile = NULL;
/* the argument list after processing */
static ARGS *stripped_args;
/* the original argument list */
static ARGS *orig_args;
/* the output filename being compiled to */
static char *output_file;
/* the source file */
static char *input_file;
/* the name of the file containing the cached object code */
static char *hashname;
/* the extension of the file after pre-processing */
static const char *i_extension;
/* the name of the temporary pre-processor file */
static char *i_tmpfile;
/* are we compiling a .i or .ii file directly? */
static int direct_i_file;
/* the name of the cpp stderr file */
static char *cpp_stderr;
/* the name of the statistics file */
char *stats_file = NULL;
/* can we safely use the unification hashing backend? */
static int enable_unify;
/* should we strip -c when running the preprocessor only? */
static int strip_c_option;
/* customisation for using the SWIG compiler */
static int swig;
/* a list of supported file extensions, and the equivalent
extension for code that has been through the pre-processor
*/
static struct {
char *extension;
char *i_extension;
} extensions[] = {
{"c", "i"},
{"C", "ii"},
{"m", "mi"},
{"cc", "ii"},
{"CC", "ii"},
{"cpp", "ii"},
{"CPP", "ii"},
{"cxx", "ii"},
{"CXX", "ii"},
{"c++", "ii"},
{"C++", "ii"},
{"i", "i"},
{"ii", "ii"},
{NULL, NULL}};
/*
something went badly wrong - just execute the real compiler
*/
static void failed(void)
{
char *e;
/* delete intermediate pre-processor file if needed */
if (i_tmpfile) {
if (!direct_i_file) {
unlink(i_tmpfile);
}
free(i_tmpfile);
i_tmpfile = NULL;
}
/* delete the cpp stderr file if necessary */
if (cpp_stderr) {
unlink(cpp_stderr);
free(cpp_stderr);
cpp_stderr = NULL;
}
/* strip any local args */
args_strip(orig_args, "--ccache-");
if ((e=getenv("CCACHE_PREFIX"))) {
char *p = find_executable(e, MYNAME);
if (!p) {
cc_log("could not find executable (%s)\n", e);
perror(e);
exit(1);
}
args_add_prefix(orig_args, p);
}
if (ccache_verbose) {
display_execute_args(orig_args->argv);
}
if (swig) {
putenv("CCACHE_OUTFILES");
}
#ifndef _WIN32
execv(orig_args->argv[0], orig_args->argv);
cc_log("execv returned (%s)!\n", strerror(errno));
perror(orig_args->argv[0]);
exit(1);
#else
/* execv on Windows causes the 'non-regular' testcase to fail, so use Win32 API instead */
{
PROCESS_INFORMATION pinfo;
STARTUPINFO sinfo;
BOOL ret;
DWORD exitcode;
char *args;
ZeroMemory(&pinfo, sizeof(PROCESS_INFORMATION));
ZeroMemory(&sinfo, sizeof(STARTUPINFO));
sinfo.cb = sizeof(STARTUPINFO);
args = argvtos(orig_args->argv);
ret = CreateProcessA(orig_args->argv[0], args, NULL, NULL, TRUE, 0, NULL, NULL,
&sinfo, &pinfo);
if (!ret) {
exitcode = 1;
cc_log("CreateProcessA failed starting %s\n", orig_args->argv[0]);
perror_win32(orig_args->argv[0]);
} else {
WaitForSingleObject(pinfo.hProcess, INFINITE);
GetExitCodeProcess(pinfo.hProcess, &exitcode);
CloseHandle(pinfo.hProcess);
CloseHandle(pinfo.hThread);
}
free(args);
exit(exitcode);
}
#endif
}
/* return a string to be used to distinguish temporary files
this also tries to cope with NFS by adding the local hostname
*/
static const char *tmp_string(void)
{
static char *ret;
if (!ret) {
char hostname[200];
strcpy(hostname, "unknown");
#if HAVE_GETHOSTNAME
gethostname(hostname, sizeof(hostname)-1);
#endif
hostname[sizeof(hostname)-1] = 0;
if (asprintf(&ret, "%s.%u", hostname, (unsigned)getpid()) == -1) {
fatal("could not allocate tmp_string");
}
}
return ret;
}
/* update cached file sizes and count helper function for to_cache() */
static void to_cache_stats_helper(struct stat *pstat, char *cached_filename, char *tmp_outfiles, int *files_size, int *cached_files_count)
{
#if ENABLE_ZLIB
/* do an extra stat on the cache file for the size statistics */
if (stat(cached_filename, pstat) != 0) {
cc_log("failed to stat cache files - %s\n", strerror(errno));
stats_update(STATS_ERROR);
if (tmp_outfiles) {
unlink(tmp_outfiles);
}
failed();
}
#else
(void)cached_filename;
(void)tmp_outfiles;
#endif
(*files_size) += file_size(pstat);
(*cached_files_count)++;
}
/* run the real compiler and put the result in cache */
static void to_cache(ARGS *args)
{
char *path_stderr;
char *tmp_stdout, *tmp_stderr, *tmp_outfiles;
struct stat st1;
int status;
int cached_files_count = 0;
int files_size = 0;
x_asprintf(&tmp_stdout, "%s/tmp.stdout.%s", temp_dir, tmp_string());
x_asprintf(&tmp_stderr, "%s/tmp.stderr.%s", temp_dir, tmp_string());
x_asprintf(&tmp_outfiles, "%s/tmp.outfiles.%s", temp_dir, tmp_string());
if (strip_c_option && !swig) {
args_add(stripped_args, "-c");
}
if (output_file) {
args_add(args, "-o");
args_add(args, output_file);
}
/* Turn off DEPENDENCIES_OUTPUT when running cc1, because
* otherwise it will emit a line like
*
* tmp.stdout.vexed.732.o: /home/mbp/.ccache/tmp.stdout.vexed.732.i
*
* unsetenv() is on BSD and Linux but not portable. */
putenv("DEPENDENCIES_OUTPUT");
/* Give SWIG a filename for it to create and populate with a list of files that it generates */
if (swig) {
char *ccache_outfiles;
x_asprintf(&ccache_outfiles, "CCACHE_OUTFILES=%s", tmp_outfiles);
unlink(tmp_outfiles);
if (getenv("CCACHE_OUTFILES") || putenv(ccache_outfiles) == -1) {
cc_log("CCACHE_OUTFILES env variable already set or could not be set\n");
stats_update(STATS_ERROR);
failed();
}
}
if (getenv("CCACHE_CPP2")) {
args_add(args, input_file);
} else {
if (swig) {
args_add(args, "-nopreprocess");
}
args_add(args, i_tmpfile);
}
status = execute(args->argv, tmp_stdout, tmp_stderr);
args_pop(args, 3);
if (stat(tmp_stdout, &st1) != 0 || st1.st_size != 0) {
cc_log("compiler produced stdout for %s\n", input_file);
stats_update(STATS_STDOUT);
unlink(tmp_stdout);
unlink(tmp_stderr);
unlink(tmp_outfiles);
if (!swig) unlink(output_file);
failed();
}
unlink(tmp_stdout);
if (status != 0) {
int fd;
cc_log("compile of %s gave status = %d\n", input_file, status);
stats_update(STATS_STATUS);
fd = open(tmp_stderr, O_RDONLY | O_BINARY);
if (fd != -1) {
if (cpp_stderr) {
/* we might have some stderr from cpp */
int fd2 = open(cpp_stderr, O_RDONLY | O_BINARY);
if (fd2 != -1) {
copy_fd(fd2, 2);
close(fd2);
unlink(cpp_stderr);
cpp_stderr = NULL;
}
}
/* we can use a quick method of
getting the failed output */
copy_fd(fd, 2);
close(fd);
unlink(tmp_stderr);
if (i_tmpfile && !direct_i_file) {
unlink(i_tmpfile);
}
exit(status);
}
unlink(tmp_stderr);
unlink(tmp_outfiles);
if (!swig) unlink(output_file);
failed();
} else {
int hardlink = (getenv("CCACHE_NOCOMPRESS") != 0) && (getenv("CCACHE_HARDLINK") != 0);
if (swig) {
/* read the list of generated files and copy each of them into the cache */
FILE *file;
file = fopen(tmp_outfiles, "r");
if (file) {
char out_filename[FILENAME_MAX + 1];
char out_filename_cache[FILENAME_MAX + 1];
while (fgets(out_filename, FILENAME_MAX, file)) {
char *linefeed = strchr(out_filename, '\n');
if (linefeed) {
char *potential_cr = linefeed - 1;
if (potential_cr >= out_filename && *potential_cr == '\r')
*potential_cr = 0;
*linefeed = 0;
if (cached_files_count == 0) {
strcpy(out_filename_cache, hashname);
} else {
sprintf(out_filename_cache, "%s.%d", hashname, cached_files_count);
}
if (commit_to_cache(out_filename, out_filename_cache, hardlink) != 0) {
fclose(file);
unlink(tmp_outfiles);
failed();
}
to_cache_stats_helper(&st1, out_filename_cache, tmp_outfiles, &files_size, &cached_files_count);
} else {
cached_files_count = 0;
break;
}
}
fclose(file);
if (cached_files_count == 0) {
cc_log("failed to copy output files to cache - internal error\n");
stats_update(STATS_ERROR);
unlink(tmp_outfiles);
failed();
}
/* also copy the (uncompressed) file containing the list of generated files into the cache */
sprintf(out_filename_cache, "%s.outfiles", hashname);
if (stat(tmp_outfiles, &st1) != 0 ||
safe_rename(tmp_outfiles, out_filename_cache) != 0) {
cc_log("failed to copy outfiles file to cache - %s\n", strerror(errno));
stats_update(STATS_ERROR);
unlink(tmp_outfiles);
failed();
}
to_cache_stats_helper(&st1, out_filename_cache, tmp_outfiles, &files_size, &cached_files_count);
unlink(tmp_outfiles);
} else {
cc_log("failed to open temp outfiles file - %s\n", strerror(errno));
stats_update(STATS_ERROR);
failed();
}
} else {
if (commit_to_cache(output_file, hashname, hardlink) != 0) {
failed();
}
to_cache_stats_helper(&st1, hashname, 0, &files_size, &cached_files_count);
}
}
x_asprintf(&path_stderr, "%s.stderr", hashname);
if (stat(tmp_stderr, &st1) != 0 ||
move_file(tmp_stderr, path_stderr) != 0) {
cc_log("failed to rename tmp files - %s\n", strerror(errno));
stats_update(STATS_ERROR);
failed();
}
to_cache_stats_helper(&st1, path_stderr, 0, &files_size, &cached_files_count);
cc_log("Placed %d files for %s into cache\n", cached_files_count, input_file);
stats_tocache(files_size, cached_files_count);
free(tmp_stderr);
free(tmp_stdout);
free(tmp_outfiles);
free(path_stderr);
}
/* find the hash for a command. The hash includes all argument lists,
plus the output from running the compiler with -E */
static void find_hash(ARGS *args)
{
int i;
char *path_stdout, *path_stderr;
char *hash_dir;
char *s;
struct stat st;
int status;
int nlevels = 2;
char *input_base;
char *tmp;
if ((s = getenv("CCACHE_NLEVELS"))) {
nlevels = atoi(s);
if (nlevels < 1) nlevels = 1;
if (nlevels > 8) nlevels = 8;
}
hash_start();
/* when we are doing the unifying tricks we need to include
the input file name in the hash to get the warnings right */
if (enable_unify || swig) {
hash_string(input_file);
}
if (swig) {
if (output_file) {
hash_string(output_file);
}
} else {
/* we have to hash the extension, as a .i file isn't treated the same
by the compiler as a .ii file */
hash_string(i_extension);
}
/* first the arguments */
for (i=1;i<args->argc;i++) {
/* some arguments don't contribute to the hash. The
theory is that these arguments will change the
output of -E if they are going to have any effect
at all, or they only affect linking */
if (i < args->argc-1) {
if (strcmp(args->argv[i], "-I") == 0 ||
strcmp(args->argv[i], "-include") == 0 ||
strcmp(args->argv[i], "-L") == 0 ||
strcmp(args->argv[i], "-D") == 0 ||
strcmp(args->argv[i], "-idirafter") == 0 ||
strcmp(args->argv[i], "-isystem") == 0) {
i++;
continue;
}
}
if (strncmp(args->argv[i], "-I", 2) == 0 ||
strncmp(args->argv[i], "-L", 2) == 0 ||
strncmp(args->argv[i], "-D", 2) == 0 ||
strncmp(args->argv[i], "-idirafter", 10) == 0 ||
strncmp(args->argv[i], "-isystem", 8) == 0) {
continue;
}
if (strncmp(args->argv[i], "--specs=", 8) == 0 &&
stat(args->argv[i]+8, &st) == 0) {
/* if given a explicit specs file, then hash that file, but
don't include the path to it in the hash */
hash_file(args->argv[i]+8);
continue;
}
/* all other arguments are included in the hash */
hash_string(args->argv[i]);
}
/* the compiler driver size and date. This is a simple minded way
to try and detect compiler upgrades. It is not 100% reliable */
if (stat(args->argv[0], &st) != 0) {
cc_log("Couldn't stat the compiler!? (argv[0]='%s')\n", args->argv[0]);
stats_update(STATS_COMPILER);
failed();
}
/* also include the hash of the compiler name - as some compilers
use hard links and behave differently depending on the real name */
if (st.st_nlink > 1) {
hash_string(str_basename(args->argv[0]));
}
hash_int(st.st_size);
hash_int(st.st_mtime);
/* possibly hash the current working directory */
if (getenv("CCACHE_HASHDIR")) {
char *cwd = gnu_getcwd();
if (cwd) {
hash_string(cwd);
free(cwd);
}
}
/* ~/hello.c -> tmp.hello.123.i
limit the basename to 10
characters in order to cope with filesystem with small
maximum filename length limits */
input_base = str_basename(input_file);
tmp = strchr(input_base, '.');
if (tmp != NULL) {
*tmp = 0;
}
if (strlen(input_base) > 10) {
input_base[10] = 0;
}
/* now the run */
x_asprintf(&path_stdout, "%s/%s.tmp.%s.%s", temp_dir,
input_base, tmp_string(),
i_extension);
x_asprintf(&path_stderr, "%s/tmp.cpp_stderr.%s", temp_dir, tmp_string());
if (!direct_i_file) {
/* run cpp on the input file to obtain the .i */
args_add(args, "-E");
args_add(args, input_file);
status = execute(args->argv, path_stdout, path_stderr);
args_pop(args, 2);
} else {
/* we are compiling a .i or .ii file - that means we
can skip the cpp stage and directly form the
correct i_tmpfile */
path_stdout = x_strdup(input_file);
if (create_empty_file(path_stderr) != 0) {
cc_log("failed to create empty stderr file\n");
stats_update(STATS_ERROR);
failed();
}
status = 0;
}
if (status != 0) {
if (!direct_i_file) {
unlink(path_stdout);
}
unlink(path_stderr);
cc_log("the preprocessor gave %d\n", status);
stats_update(STATS_PREPROCESSOR);
failed();
}
/* if the compilation is with -g then we have to include the whole of the
preprocessor output, which means we are sensitive to line number
information. Otherwise we can discard line number info, which makes
us less sensitive to reformatting changes
Note! I have now disabled the unification code by default
as it gives the wrong line numbers for warnings. Pity.
*/
if (!enable_unify) {
hash_file(path_stdout);
} else {
if (unify_hash(path_stdout) != 0) {
stats_update(STATS_ERROR);
failed();
}
}
hash_file(path_stderr);
i_tmpfile = path_stdout;
if (!getenv("CCACHE_CPP2")) {
/* if we are using the CPP trick then we need to remember this stderr
data and output it just before the main stderr from the compiler
pass */
cpp_stderr = path_stderr;
} else {
unlink(path_stderr);
free(path_stderr);
}
/* we use a N level subdir for the cache path to reduce the impact
on filesystems which are slow for large directories
*/
s = hash_result();
x_asprintf(&hash_dir, "%s/%c", cache_dir, s[0]);
x_asprintf(&stats_file, "%s/stats", hash_dir);
for (i=1; i<nlevels; i++) {
char *p;
if (create_dir(hash_dir) != 0) {
cc_log("failed to create %s\n", hash_dir);
stats_update(STATS_ERROR);
failed();
}
x_asprintf(&p, "%s/%c", hash_dir, s[i]);
free(hash_dir);
hash_dir = p;
}
if (create_dir(hash_dir) != 0) {
cc_log("failed to create %s\n", hash_dir);
stats_update(STATS_ERROR);
failed();
}
x_asprintf(&hashname, "%s/%s", hash_dir, s+nlevels);
free(hash_dir);
}
/*
try to return the compile result from cache. If we can return from
cache then this function exits with the correct status code,
otherwise it returns */
static void from_cache(int first)
{
int fd_stderr, fd_cpp_stderr;
char *stderr_file;
struct stat st;
x_asprintf(&stderr_file, "%s.stderr", hashname);
fd_stderr = open(stderr_file, O_RDONLY | O_BINARY);
if (fd_stderr == -1) {
/* it isn't in cache ... */
free(stderr_file);
return;
}
/* make sure the output is there too */
if (stat(hashname, &st) != 0) {
close(fd_stderr);
unlink(stderr_file);
free(stderr_file);
return;
}
/* the user might be disabling cache hits */
#ifndef ENABLE_ZLIB
/* if the cache file is compressed we must recache */
if ((first && getenv("CCACHE_RECACHE")) ||
test_if_compressed(hashname) == 1)
#else
if (first && getenv("CCACHE_RECACHE"))
#endif
{
close(fd_stderr);
unlink(stderr_file);
free(stderr_file);
return;
}
if (first) {
int hardlink;
int passfail = -1;
/* update timestamps for LRU cleanup
also gives output_file a sensible mtime when hard-linking (for make) */
x_utimes(stderr_file);
hardlink = (getenv("CCACHE_HARDLINK") != 0);
if (swig) {
/* read the list of generated files and copy each of them out of the cache */
FILE *file;
char *outfiles;
x_asprintf(&outfiles, "%s.outfiles", hashname);
file = fopen(outfiles, "r");
if (file) {
char out_filename[FILENAME_MAX + 1];
char out_filename_cache[FILENAME_MAX + 1];
int retrieved_files_count = 0;
x_utimes(outfiles);
while (fgets(out_filename, FILENAME_MAX, file)) {
char *linefeed = strchr(out_filename, '\n');
if (linefeed) {
char *potential_cr = linefeed - 1;
if (potential_cr >= out_filename && *potential_cr == '\r')
*potential_cr = 0;
*linefeed = 0;
if (retrieved_files_count == 0) {
strcpy(out_filename_cache, hashname);
} else {
sprintf(out_filename_cache, "%s.%d", hashname, retrieved_files_count);
}
passfail = retrieve_from_cache(out_filename_cache, out_filename, hardlink);
if (passfail == -1) {
break;
}
retrieved_files_count++;
} else {
cc_log("failed to copy output files from cache - internal error\n");
stats_update(STATS_ERROR);
passfail = -1;
break;
}
}
if (retrieved_files_count == 0) {
cc_log("failed to copy output files from cache - internal error\n");
stats_update(STATS_ERROR);
passfail = -1;
}
fclose(file);
} else {
cc_log("failed to open cached outfiles file - %s\n", strerror(errno));
stats_update(STATS_ERROR);
}
} else {
passfail = retrieve_from_cache(hashname, output_file, hardlink);
}
if (passfail == -1) {
close(fd_stderr);
unlink(stderr_file);
free(stderr_file);
return;
}
free(stderr_file);
}
/* get rid of the intermediate preprocessor file */
if (i_tmpfile) {
if (!direct_i_file) {
unlink(i_tmpfile);
}
free(i_tmpfile);
i_tmpfile = NULL;
}
/* send the cpp stderr, if applicable */
fd_cpp_stderr = open(cpp_stderr, O_RDONLY | O_BINARY);
if (fd_cpp_stderr != -1) {
copy_fd(fd_cpp_stderr, 2);
close(fd_cpp_stderr);
unlink(cpp_stderr);
free(cpp_stderr);
cpp_stderr = NULL;
}
/* send the stderr */
copy_fd(fd_stderr, 2);
close(fd_stderr);
/* and exit with the right status code */
if (first) {
cc_log("got cached result for %s\n", input_file);
stats_update(STATS_CACHED);
}
exit(0);
}
/* find the real compiler. We just search the PATH to find a executable of the
same name that isn't a link to ourselves */
static void find_compiler(int argc, char **argv)
{
char *base;
char *path;
orig_args = args_init(argc, argv);
base = str_basename(argv[0]);
/* we might be being invoked like "ccache gcc -c foo.c" */
if (strcmp(base, MYNAME) == 0) {
args_remove_first(orig_args);
free(base);
if (strchr(argv[1],'/')
#ifdef _WIN32
|| strchr(argv[1],'\\')
#endif
) {
/* a full path was given */
return;
}
base = str_basename(argv[1]);
}
/* support user override of the compiler */
if ((path=getenv("CCACHE_CC"))) {
base = x_strdup(path);
}
orig_args->argv[0] = find_executable(base, MYNAME);
/* can't find the compiler! */
if (!orig_args->argv[0]) {
stats_update(STATS_COMPILER);
cc_log("could not find compiler (%s)\n", base);
perror(base);
exit(1);
}
}
/* check a filename for C/C++ extension. Return the pre-processor
extension */
static const char *check_extension(const char *fname, int *direct_i)
{
int i;
const char *p;
if (direct_i) {
*direct_i = 0;
}
if (swig) return "ii"; /* any file extension is acceptable as input for SWIG */
p = strrchr(fname, '.');
if (!p) return NULL;
p++;
for (i=0; extensions[i].extension; i++) {
if (strcmp(p, extensions[i].extension) == 0) {
if (direct_i && strcmp(p, extensions[i].i_extension) == 0) {
*direct_i = 1;
}
p = getenv("CCACHE_EXTENSION");
if (p) return p;
return extensions[i].i_extension;
}
}
return NULL;
}
/*
process the compiler options to form the correct set of options
for obtaining the preprocessor output
*/
static void process_args(int argc, char **argv)
{
int i;
int found_c_opt = 0;
int found_S_opt = 0;
struct stat st;
char *e;
/* is gcc being asked to output dependencies? */
int generating_dependencies = 0;
/* is the dependency makefile name overridden with -MF? */
int dependency_filename_specified = 0;
/* is the dependency makefile target name specified with -MQ or -MF? */
int dependency_target_specified = 0;
stripped_args = args_init(0, NULL);
args_add(stripped_args, argv[0]);
/* -c not required for SWIG */
if (swig) {
found_c_opt = 1;
}
for (i=1; i<argc; i++) {
/* some options will never work ... */
if (strcmp(argv[i], "-E") == 0) {
failed();
}
/* these are too hard */
if (strcmp(argv[i], "-fbranch-probabilities")==0 ||
strcmp(argv[i], "-fprofile-arcs") == 0 ||
strcmp(argv[i], "-ftest-coverage") == 0 ||
strcmp(argv[i], "--coverage") == 0 ||
strcmp(argv[i], "-M") == 0 ||
strcmp(argv[i], "-MM") == 0 ||
strcmp(argv[i], "-x") == 0) {
cc_log("argument %s is unsupported\n", argv[i]);
stats_update(STATS_UNSUPPORTED);
failed();
continue;
}
/* we must have -c */
if (strcmp(argv[i], "-c") == 0) {
if (!strip_c_option) {
args_add(stripped_args, argv[i]);
}
found_c_opt = 1;
continue;
}
/* -S changes the default extension */
if (strcmp(argv[i], "-S") == 0) {
args_add(stripped_args, argv[i]);
found_S_opt = 1;
continue;
}
/* we need to work out where the output was meant to go */
if (strcmp(argv[i], "-o") == 0) {
if (i == argc-1) {
cc_log("missing argument to %s\n", argv[i]);
stats_update(STATS_ARGS);
failed();
}
output_file = argv[i+1];
i++;
continue;
}
/* alternate form of -o, with no space */
if (!swig) { /* some of SWIG's arguments begin with -o */
if (strncmp(argv[i], "-o", 2) == 0) {
output_file = &argv[i][2];
continue;
}
}
/* debugging is handled specially, so that we know if we
can strip line number info
*/
if (strncmp(argv[i], "-g", 2) == 0) {
args_add(stripped_args, argv[i]);
if (strcmp(argv[i], "-g0") != 0) {
enable_unify = 0;
}
continue;
}
/* The user knows best: just swallow the next arg */
if (strcmp(argv[i], "--ccache-skip") == 0) {
i++;
if (i == argc) {
failed();
}
args_add(stripped_args, argv[i]);
continue;
}
/* These options require special handling, because they
behave differently with gcc -E, when the output
file is not specified. */
if (strcmp(argv[i], "-MD") == 0 || strcmp(argv[i], "-MMD") == 0) {
generating_dependencies = 1;
} else if (strcmp(argv[i], "-MF") == 0) {
dependency_filename_specified = 1;
} else if (strcmp(argv[i], "-MQ") == 0 || strcmp(argv[i], "-MT") == 0) {
dependency_target_specified = 1;
}
/* the input file is already preprocessed */
if (swig && strcmp(argv[i], "-nopreprocess") == 0) {
direct_i_file = 1;
continue;
}
/* options that take an argument */
{
const char *opts[] = {"-I", "-include", "-imacros", "-iprefix",
"-iwithprefix", "-iwithprefixbefore",
"-L", "-D", "-U", "-x", "-MF",
"-MT", "-MQ", "-isystem", "-aux-info",
"--param", "-A", "-Xlinker", "-u",
"-idirafter",
NULL};
int j;
for (j=0;opts[j];j++) {
if (strcmp(argv[i], opts[j]) == 0) {
if (i == argc-1) {
cc_log("missing argument to %s\n",
argv[i]);
stats_update(STATS_ARGS);
failed();
}
args_add(stripped_args, argv[i]);
args_add(stripped_args, argv[i+1]);
i++;
break;
}
}
if (opts[j]) continue;
}
/* other options */
if (argv[i][0] == '-') {
args_add(stripped_args, argv[i]);
continue;
}
/* if an argument isn't a plain file then assume its
an option, not an input file. This allows us to
cope better with unusual compiler options */
if (stat(argv[i], &st) != 0 || !S_ISREG(st.st_mode)) {
args_add(stripped_args, argv[i]);
continue;
}
if (input_file) {
if (check_extension(argv[i], NULL)) {
cc_log("multiple input files (%s and %s)\n",
input_file, argv[i]);
stats_update(STATS_MULTIPLE);
} else if (!found_c_opt) {
cc_log("called for link with %s\n", argv[i]);