-
Notifications
You must be signed in to change notification settings - Fork 1
/
ccache.c
2130 lines (1883 loc) · 53.4 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
/*
* ccache -- a fast C/C++ compiler cache
*
* Copyright (C) 2002-2007 Andrew Tridgell
* Copyright (C) 2009-2010 Joel Rosdahl
*
* 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 3 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., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "ccache.h"
#include "compopt.h"
#ifdef HAVE_GETOPT_LONG
#include <getopt.h>
#else
#include "getopt_long.h"
#endif
#include "hashtable.h"
#include "hashtable_itr.h"
#include "hashutil.h"
#include "language.h"
#include "manifest.h"
static const char VERSION_TEXT[] =
MYNAME " version %s\n"
"\n"
"Copyright (C) 2002-2007 Andrew Tridgell\n"
"Copyright (C) 2009-2010 Joel Rosdahl\n"
"\n"
"This program is free software; you can redistribute it and/or modify it under\n"
"the terms of the GNU General Public License as published by the Free Software\n"
"Foundation; either version 3 of the License, or (at your option) any later\n"
"version.\n";
static const char USAGE_TEXT[] =
"Usage:\n"
" " MYNAME " [options]\n"
" " MYNAME " compiler [compiler options]\n"
" compiler [compiler options] (via symbolic link)\n"
"\n"
"Options:\n"
" -c, --cleanup delete old files and recalculate size counters\n"
" (normally not needed as this is done automatically)\n"
" -C, --clear clear the cache completely\n"
" -F, --max-files=N set maximum number of files in cache to N (use 0 for\n"
" no limit)\n"
" -M, --max-size=SIZE set maximum size of cache to SIZE (use 0 for no\n"
" limit; available suffixes: G, M and K; default\n"
" suffix: G)\n"
" -s, --show-stats show statistics summary\n"
" -z, --zero-stats zero statistics counters\n"
"\n"
" -h, --help print this help text\n"
" -V, --version print version and copyright information\n"
"\n"
"See also <http://ccache.samba.org>.\n";
/* current working directory taken from $PWD, or getcwd() if $PWD is bad */
static char *current_working_dir;
/* the base cache directory */
char *cache_dir = NULL;
/* the directory for temporary files */
static char *temp_dir;
/* the debug logfile name, if set */
char *cache_logfile = NULL;
/* base directory (from CCACHE_BASEDIR) */
static char *base_dir;
/* the original argument list */
static struct args *orig_args;
/* the source file */
static char *input_file;
/* The output file being compiled to. */
static char *output_obj;
/* The path to the dependency file (implicit or specified with -MF). */
static char *output_dep;
/*
* Name (represented as a struct file_hash) of the file containing the cached
* object code.
*/
static struct file_hash *cached_obj_hash;
/*
* Full path to the file containing the cached object code
* (cachedir/a/b/cdef[...]-size.o).
*/
static char *cached_obj;
/*
* Full path to the file containing the standard error output
* (cachedir/a/b/cdef[...]-size.stderr).
*/
static char *cached_stderr;
/*
* Full path to the file containing the dependency information
* (cachedir/a/b/cdef[...]-size.d).
*/
static char *cached_dep;
/*
* Full path to the file containing the manifest
* (cachedir/a/b/cdef[...]-size.manifest).
*/
static char *manifest_path;
/*
* Time of compilation. Used to see if include files have changed after
* compilation.
*/
static time_t time_of_compilation;
/* Bitmask of SLOPPY_*. */
unsigned sloppiness = 0;
/*
* Files included by the preprocessor and their hashes/sizes. Key: file path.
* Value: struct file_hash.
*/
static struct hashtable *included_files;
/* is gcc being asked to output dependencies? */
static bool generating_dependencies;
/* the extension of the file (without dot) 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 bool direct_i_file;
/* the name of the cpp stderr file */
static char *cpp_stderr;
/*
* Full path to the statistics file in the subdirectory where the cached result
* belongs (CCACHE_DIR/X/stats).
*/
char *stats_file = NULL;
/* can we safely use the unification hashing backend? */
static bool enable_unify;
/* should we use the direct mode? */
static bool enable_direct = true;
/*
* Whether to enable compression of files stored in the cache. (Manifest files
* are always compressed.)
*/
static bool enable_compression = false;
/* number of levels (1 <= nlevels <= 8) */
static int nlevels = 2;
/*
* Whether we should use the optimization of passing the already existing
* preprocessed source code to the compiler.
*/
static bool compile_preprocessed_source_code;
/* Whether the output is a precompiled header */
static bool output_is_precompiled_header = false;
/*
* Whether we are using a precompiled header (either via -include or #include).
*/
static bool using_precompiled_header = false;
/* How long (in microseconds) to wait before breaking a stale lock. */
unsigned lock_staleness_limit = 2000000;
enum fromcache_call_mode {
FROMCACHE_DIRECT_MODE,
FROMCACHE_CPP_MODE,
FROMCACHE_COMPILED_MODE
};
/*
* This is a string that identifies the current "version" of the hash sum
* computed by ccache. If, for any reason, we want to force the hash sum to be
* different for the same input in a new ccache version, we can just change
* this string. A typical example would be if the format of one of the files
* stored in the cache changes in a backwards-incompatible way.
*/
static const char HASH_PREFIX[] = "3";
/* Something went badly wrong - just execute the real compiler. */
static void
failed(void)
{
char *e;
/* strip any local args */
args_strip(orig_args, "--ccache-");
if ((e = getenv("CCACHE_PREFIX"))) {
char *p = find_executable(e, MYNAME);
if (!p) {
fatal("%s: %s", e, strerror(errno));
}
args_add_prefix(orig_args, p);
}
cc_log("Failed; falling back to running the real compiler");
cc_log_argv("Executing ", orig_args->argv);
exitfn_call();
execv(orig_args->argv[0], orig_args->argv);
fatal("%s: execv returned (%s)", orig_args->argv[0], strerror(errno));
}
static void
clean_up_tmp_files()
{
/* 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;
}
}
/*
* Transform a name to a full path into the cache directory, creating needed
* sublevels if needed. Caller frees.
*/
static char *
get_path_in_cache(const char *name, const char *suffix)
{
int i;
char *path;
char *result;
path = x_strdup(cache_dir);
for (i = 0; i < nlevels; ++i) {
char *p = format("%s/%c", path, name[i]);
free(path);
path = p;
if (create_dir(path) != 0) {
cc_log("Failed to create %s", path);
failed();
}
}
result = format("%s/%s%s", path, name + nlevels, suffix);
free(path);
return result;
}
/*
* This function hashes an include file and stores the path and hash in the
* global included_files variable. If the include file is a PCH, cpp_hash is
* also updated. Takes over ownership of path.
*/
static void
remember_include_file(char *path, size_t path_len, struct mdfour *cpp_hash)
{
struct mdfour fhash;
struct stat st;
char *source = NULL;
size_t size;
int result;
bool is_pch;
if (path_len >= 2 && (path[0] == '<' && path[path_len - 1] == '>')) {
/* Typically <built-in> or <command-line>. */
goto ignore;
}
if (str_eq(path, input_file)) {
/* Don't remember the input file. */
goto ignore;
}
if (hashtable_search(included_files, path)) {
/* Already known include file. */
goto ignore;
}
if (stat(path, &st) != 0) {
cc_log("Failed to stat include file %s", path);
goto failure;
}
if (S_ISDIR(st.st_mode)) {
/* Ignore directory, typically $PWD. */
goto ignore;
}
/* Let's hash the include file. */
if (!(sloppiness & SLOPPY_INCLUDE_FILE_MTIME)
&& st.st_mtime >= time_of_compilation) {
cc_log("Include file %s too new", path);
goto failure;
}
hash_start(&fhash);
is_pch = is_precompiled_header(path);
if (is_pch) {
struct file_hash pch_hash;
if (!hash_file(&fhash, path)) {
goto failure;
}
hash_result_as_bytes(&fhash, pch_hash.hash);
pch_hash.size = fhash.totalN;
hash_delimiter(cpp_hash, "pch_hash");
hash_buffer(cpp_hash, pch_hash.hash, sizeof(pch_hash.hash));
}
if (enable_direct) {
struct file_hash *h;
if (!is_pch) { /* else: the file has already been hashed. */
if (st.st_size > 0) {
if (!read_file(path, st.st_size, &source, &size)) {
goto failure;
}
} else {
source = x_strdup("");
size = 0;
}
result = hash_source_code_string(&fhash, source, size, path);
if (result & HASH_SOURCE_CODE_ERROR
|| result & HASH_SOURCE_CODE_FOUND_TIME) {
goto failure;
}
}
h = x_malloc(sizeof(*h));
hash_result_as_bytes(&fhash, h->hash);
h->size = fhash.totalN;
hashtable_insert(included_files, path, h);
} else {
free(path);
}
free(source);
return;
failure:
cc_log("Disabling direct mode");
enable_direct = false;
/* Fall through. */
ignore:
free(path);
free(source);
}
/*
* Make a relative path from CCACHE_BASEDIR to path. Takes over ownership of
* path. Caller frees.
*/
static char *
make_relative_path(char *path)
{
char *relpath;
if (!base_dir || !str_startswith(path, base_dir)) {
return path;
}
relpath = get_relative_path(current_working_dir, path);
free(path);
return relpath;
}
/*
* This function reads and hashes a file. While doing this, it also does these
* things:
*
* - Makes include file paths whose prefix is CCACHE_BASEDIR relative when
* computing the hash sum.
* - Stores the paths and hashes of included files in the global variable
* included_files.
*/
static bool
process_preprocessed_file(struct mdfour *hash, const char *path)
{
char *data;
char *p, *q, *end;
size_t size;
if (!read_file(path, 0, &data, &size)) {
return false;
}
included_files = create_hashtable(1000, hash_from_string, strings_equal);
/* Bytes between p and q are pending to be hashed. */
end = data + size;
p = data;
q = data;
while (q < end - 7) { /* There must be at least 7 characters (# 1 "x") left
to potentially find an include file path. */
/*
* Check if we look at a line containing the file name of an included file.
* At least the following formats exist (where N is a positive integer):
*
* GCC:
*
* # N "file"
* # N "file" N
* #pragma GCC pch_preprocess "file"
*
* HP's compiler:
*
* #line N "file"
*
* Note that there may be other lines starting with '#' left after
* preprocessing as well, for instance "# pragma".
*/
if (q[0] == '#'
/* GCC: */
&& ((q[1] == ' ' && q[2] >= '0' && q[2] <= '9')
/* GCC precompiled header: */
|| (q[1] == 'p'
&& str_startswith(&q[2], "ragma GCC pch_preprocess "))
/* HP: */
|| (q[1] == 'l' && q[2] == 'i' && q[3] == 'n' && q[4] == 'e'
&& q[5] == ' '))
&& (q == data || q[-1] == '\n')) {
char *path;
while (q < end && *q != '"') {
q++;
}
q++;
if (q >= end) {
cc_log("Failed to parse included file path");
free(data);
return false;
}
/* q points to the beginning of an include file path */
hash_buffer(hash, p, q - p);
p = q;
while (q < end && *q != '"') {
q++;
}
/* p and q span the include file path */
path = x_strndup(p, q - p);
path = make_relative_path(path);
hash_string(hash, path);
remember_include_file(path, q - p, hash);
p = q;
} else {
q++;
}
}
hash_buffer(hash, p, (end - p));
free(data);
return true;
}
/* run the real compiler and put the result in cache */
static void
to_cache(struct args *args)
{
char *tmp_stdout, *tmp_stderr, *tmp_obj;
struct stat st;
int status;
size_t added_bytes = 0;
unsigned added_files = 0;
tmp_stdout = format("%s.tmp.stdout.%s", cached_obj, tmp_string());
tmp_stderr = format("%s.tmp.stderr.%s", cached_obj, tmp_string());
tmp_obj = format("%s.tmp.%s", cached_obj, tmp_string());
args_add(args, "-o");
args_add(args, tmp_obj);
/* 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");
if (compile_preprocessed_source_code) {
args_add(args, i_tmpfile);
} else {
args_add(args, input_file);
}
cc_log("Running real compiler");
status = execute(args->argv, tmp_stdout, tmp_stderr);
args_pop(args, 3);
if (stat(tmp_stdout, &st) != 0 || st.st_size != 0) {
cc_log("Compiler produced stdout");
stats_update(STATS_STDOUT);
unlink(tmp_stdout);
unlink(tmp_stderr);
unlink(tmp_obj);
failed();
}
unlink(tmp_stdout);
/*
* Merge stderr from the preprocessor (if any) and stderr from the real
* compiler into tmp_stderr.
*/
if (cpp_stderr) {
int fd_cpp_stderr;
int fd_real_stderr;
int fd_result;
char *tmp_stderr2;
tmp_stderr2 = format("%s.tmp.stderr2.%s", cached_obj, tmp_string());
if (x_rename(tmp_stderr, tmp_stderr2)) {
cc_log("Failed to rename %s to %s", tmp_stderr, tmp_stderr2);
failed();
}
fd_cpp_stderr = open(cpp_stderr, O_RDONLY | O_BINARY);
if (fd_cpp_stderr == -1) {
cc_log("Failed opening %s", cpp_stderr);
failed();
}
fd_real_stderr = open(tmp_stderr2, O_RDONLY | O_BINARY);
if (fd_real_stderr == -1) {
cc_log("Failed opening %s", tmp_stderr2);
failed();
}
fd_result = open(tmp_stderr, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
if (fd_result == -1) {
cc_log("Failed opening %s", tmp_stderr);
failed();
}
copy_fd(fd_cpp_stderr, fd_result);
copy_fd(fd_real_stderr, fd_result);
close(fd_cpp_stderr);
close(fd_real_stderr);
close(fd_result);
unlink(tmp_stderr2);
free(tmp_stderr2);
}
if (status != 0) {
int fd;
cc_log("Compiler gave exit status %d", status);
stats_update(STATS_STATUS);
fd = open(tmp_stderr, O_RDONLY | O_BINARY);
if (fd != -1) {
if (str_eq(output_obj, "/dev/null")
|| (access(tmp_obj, R_OK) == 0
&& move_file(tmp_obj, output_obj, 0) == 0)
|| errno == ENOENT) {
/* we can use a quick method of getting the failed output */
copy_fd(fd, 2);
close(fd);
unlink(tmp_stderr);
exit(status);
}
}
unlink(tmp_stderr);
unlink(tmp_obj);
failed();
}
if (stat(tmp_obj, &st) != 0) {
cc_log("Compiler didn't produce an object file");
stats_update(STATS_NOOUTPUT);
failed();
}
if (st.st_size == 0) {
cc_log("Compiler produced an empty object file");
stats_update(STATS_EMPTYOUTPUT);
failed();
}
if (stat(tmp_stderr, &st) != 0) {
cc_log("Failed to stat %s", tmp_stderr);
stats_update(STATS_ERROR);
failed();
}
if (st.st_size > 0) {
if (move_uncompressed_file(tmp_stderr, cached_stderr,
enable_compression) != 0) {
cc_log("Failed to move %s to %s", tmp_stderr, cached_stderr);
stats_update(STATS_ERROR);
failed();
}
cc_log("Stored in cache: %s", cached_stderr);
if (enable_compression) {
stat(cached_stderr, &st);
}
added_bytes += file_size(&st);
added_files += 1;
} else {
unlink(tmp_stderr);
}
if (move_uncompressed_file(tmp_obj, cached_obj, enable_compression) != 0) {
cc_log("Failed to move %s to %s", tmp_obj, cached_obj);
stats_update(STATS_ERROR);
failed();
} else {
cc_log("Stored in cache: %s", cached_obj);
stat(cached_obj, &st);
added_bytes += file_size(&st);
added_files += 1;
}
/*
* Do an extra stat on the potentially compressed object file for the
* size statistics.
*/
if (stat(cached_obj, &st) != 0) {
cc_log("Failed to stat %s", strerror(errno));
stats_update(STATS_ERROR);
failed();
}
stats_update_size(STATS_TOCACHE, added_bytes / 1024, added_files);
free(tmp_obj);
free(tmp_stderr);
free(tmp_stdout);
}
/*
* Find the object file name by running the compiler in preprocessor mode.
* Returns the hash as a heap-allocated hex string.
*/
static struct file_hash *
get_object_name_from_cpp(struct args *args, struct mdfour *hash)
{
char *input_base;
char *tmp;
char *path_stdout, *path_stderr;
int status;
struct file_hash *result;
/* ~/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 = basename(input_file);
tmp = strchr(input_base, '.');
if (tmp != NULL) {
*tmp = 0;
}
if (strlen(input_base) > 10) {
input_base[10] = 0;
}
/* now the run */
path_stdout = format("%s/%s.tmp.%s.%s",
temp_dir, input_base, tmp_string(), i_extension);
path_stderr = format("%s/tmp.cpp_stderr.%s", temp_dir, tmp_string());
time_of_compilation = time(NULL);
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 = input_file;
if (create_empty_file(path_stderr) != 0) {
stats_update(STATS_ERROR);
cc_log("Failed to create %s", path_stderr);
failed();
}
status = 0;
}
if (status != 0) {
if (!direct_i_file) {
unlink(path_stdout);
}
unlink(path_stderr);
cc_log("Preprocessor gave exit status %d", status);
stats_update(STATS_PREPROCESSOR);
failed();
}
if (enable_unify) {
/*
* When we are doing the unifying tricks we need to include the
* input file name in the hash to get the warnings right.
*/
hash_delimiter(hash, "unifyfilename");
hash_string(hash, input_file);
hash_delimiter(hash, "unifycpp");
if (unify_hash(hash, path_stdout) != 0) {
stats_update(STATS_ERROR);
unlink(path_stderr);
cc_log("Failed to unify %s", path_stdout);
failed();
}
} else {
hash_delimiter(hash, "cpp");
if (!process_preprocessed_file(hash, path_stdout)) {
stats_update(STATS_ERROR);
unlink(path_stderr);
failed();
}
}
hash_delimiter(hash, "cppstderr");
if (!hash_file(hash, path_stderr)) {
fatal("Failed to open %s", path_stderr);
}
i_tmpfile = path_stdout;
if (compile_preprocessed_source_code) {
/*
* If we are using the CPP trick, 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);
}
result = x_malloc(sizeof(*result));
hash_result_as_bytes(hash, result->hash);
result->size = hash->totalN;
return result;
}
static void
update_cached_result_globals(struct file_hash *hash)
{
char *object_name;
object_name = format_hash_as_string(hash->hash, hash->size);
cached_obj_hash = hash;
cached_obj = get_path_in_cache(object_name, ".o");
cached_stderr = get_path_in_cache(object_name, ".stderr");
cached_dep = get_path_in_cache(object_name, ".d");
stats_file = format("%s/%c/stats", cache_dir, object_name[0]);
free(object_name);
}
/*
* Update a hash sum with information common for the direct and preprocessor
* modes.
*/
static void
calculate_common_hash(struct args *args, struct mdfour *hash)
{
struct stat st;
const char *compilercheck;
char *p;
hash_string(hash, HASH_PREFIX);
/*
* We have to hash the extension, as a .i file isn't treated the same
* by the compiler as a .ii file.
*/
hash_delimiter(hash, "ext");
hash_string(hash, i_extension);
if (stat(args->argv[0], &st) != 0) {
cc_log("Couldn't stat the compiler (%s)", args->argv[0]);
stats_update(STATS_COMPILER);
failed();
}
/*
* Hash information about the compiler.
*/
compilercheck = getenv("CCACHE_COMPILERCHECK");
if (!compilercheck) {
compilercheck = "mtime";
}
if (str_eq(compilercheck, "none")) {
/* Do nothing. */
} else if (str_eq(compilercheck, "content")) {
hash_delimiter(hash, "cc_content");
hash_file(hash, args->argv[0]);
} else if (str_eq(compilercheck, "mtime")) {
hash_delimiter(hash, "cc_mtime");
hash_int(hash, st.st_size);
hash_int(hash, st.st_mtime);
} else { /* command string */
fatal("Win32 does not implement arbritary command for COMPILERCHECK");
}
/*
* Also hash the compiler name as some compilers use hard links and
* behave differently depending on the real name.
*/
hash_delimiter(hash, "cc_name");
hash_string(hash, basename(args->argv[0]));
/* Possibly hash the current working directory. */
if (getenv("CCACHE_HASHDIR")) {
char *cwd = gnu_getcwd();
if (cwd) {
hash_delimiter(hash, "cwd");
hash_string(hash, cwd);
free(cwd);
}
}
p = getenv("CCACHE_EXTRAFILES");
if (p) {
char *path, *q, *saveptr = NULL;
p = x_strdup(p);
q = p;
while ((path = strtok_r(q, PATH_DELIM, &saveptr))) {
cc_log("Hashing extra file %s", path);
hash_delimiter(hash, "extrafile");
if (!hash_file(hash, path)) {
stats_update(STATS_BADEXTRAFILE);
failed();
}
q = NULL;
}
free(p);
}
}
/*
* Update a hash sum with information specific to the direct and preprocessor
* modes and calculate the object hash. Returns the object hash on success,
* otherwise NULL. Caller frees.
*/
static struct file_hash *
calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode)
{
int i;
char *manifest_name;
struct stat st;
int result;
struct file_hash *object_hash = NULL;
/* first the arguments */
for (i = 1; i < args->argc; i++) {
/* -L doesn't affect compilation. */
if (i < args->argc-1 && str_eq(args->argv[i], "-L")) {
i++;
continue;
}
if (str_startswith(args->argv[i], "-L")) {
continue;
}
/* When using the preprocessor, 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. For precompiled headers this might not be the case. */
if (!direct_mode && !output_is_precompiled_header
&& !using_precompiled_header) {
if (compopt_affects_cpp(args->argv[i])) {
i++;
continue;
}
if (compopt_short(compopt_affects_cpp, args->argv[i])) {
continue;
}
}
if (str_startswith(args->argv[i], "--specs=") &&
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_delimiter(hash, "specs");
if (!hash_file(hash, args->argv[i] + 8)) {
failed();
}
continue;
}
/* All other arguments are included in the hash. */
hash_delimiter(hash, "arg");
hash_string(hash, args->argv[i]);
}
if (direct_mode) {
if (!(sloppiness & SLOPPY_FILE_MACRO)) {
/*
* The source code file or an include file may contain
* __FILE__, so make sure that the hash is unique for
* the file name.
*/
hash_delimiter(hash, "inputfile");
hash_string(hash, input_file);
}
hash_delimiter(hash, "sourcecode");
result = hash_source_code_file(hash, input_file);
if (result & HASH_SOURCE_CODE_ERROR) {
failed();
}
if (result & HASH_SOURCE_CODE_FOUND_TIME) {
cc_log("Disabling direct mode");
enable_direct = false;
return NULL;
}
manifest_name = hash_result(hash);
manifest_path = get_path_in_cache(manifest_name, ".manifest");
free(manifest_name);
cc_log("Looking for object file hash in %s", manifest_path);
object_hash = manifest_get(manifest_path);
if (object_hash) {
cc_log("Got object file hash from manifest");
} else {
cc_log("Did not find object file hash in manifest");
}
} else {
object_hash = get_object_name_from_cpp(args, hash);
cc_log("Got object file hash from preprocessor");
if (generating_dependencies) {
cc_log("Preprocessor created %s", output_dep);
}
}
return object_hash;
}
/*
* 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(enum fromcache_call_mode mode, bool put_object_in_manifest)
{
int fd_stderr;
int ret;
struct stat st;
bool produce_dep_file;
/* the user might be disabling cache hits */
if (mode != FROMCACHE_COMPILED_MODE && getenv("CCACHE_RECACHE")) {
return;
}
/* Check if the object file is there. */
if (stat(cached_obj, &st) != 0) {
cc_log("Object file %s not in cache", cached_obj);
return;
}
/*
* (If mode != FROMCACHE_DIRECT_MODE, the dependency file is created by
* gcc.)
*/
produce_dep_file = generating_dependencies && mode == FROMCACHE_DIRECT_MODE;
/* If the dependency file should be in the cache, check that it is. */
if (produce_dep_file && stat(cached_dep, &st) != 0) {
cc_log("Dependency file %s missing in cache", cached_dep);
return;
}
if (str_eq(output_obj, "/dev/null")) {
ret = 0;
} else {
unlink(output_obj);
/* only make a hardlink if the cache file is uncompressed */
if (getenv("CCACHE_HARDLINK") && !file_is_compressed(cached_obj)) {
ret = link(cached_obj, output_obj);
} else {
ret = copy_file(cached_obj, output_obj, 0);
}