resetting manifest requested domain to floor
[platform/upstream/ccache.git] / ccache.c
1 /*
2  * ccache -- a fast C/C++ compiler cache
3  *
4  * Copyright (C) 2002-2007 Andrew Tridgell
5  * Copyright (C) 2009-2011 Joel Rosdahl
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 3 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc., 51
19  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "ccache.h"
23 #include "compopt.h"
24 #ifdef HAVE_GETOPT_LONG
25 #include <getopt.h>
26 #else
27 #include "getopt_long.h"
28 #endif
29 #include "hashtable.h"
30 #include "hashtable_itr.h"
31 #include "hashutil.h"
32 #include "language.h"
33 #include "manifest.h"
34
35 static const char VERSION_TEXT[] =
36 MYNAME " version %s\n"
37 "\n"
38 "Copyright (C) 2002-2007 Andrew Tridgell\n"
39 "Copyright (C) 2009-2011 Joel Rosdahl\n"
40 "\n"
41 "This program is free software; you can redistribute it and/or modify it under\n"
42 "the terms of the GNU General Public License as published by the Free Software\n"
43 "Foundation; either version 3 of the License, or (at your option) any later\n"
44 "version.\n";
45
46 static const char USAGE_TEXT[] =
47 "Usage:\n"
48 "    " MYNAME " [options]\n"
49 "    " MYNAME " compiler [compiler options]\n"
50 "    compiler [compiler options]          (via symbolic link)\n"
51 "\n"
52 "Options:\n"
53 "    -c, --cleanup         delete old files and recalculate size counters\n"
54 "                          (normally not needed as this is done automatically)\n"
55 "    -C, --clear           clear the cache completely\n"
56 "    -F, --max-files=N     set maximum number of files in cache to N (use 0 for\n"
57 "                          no limit)\n"
58 "    -M, --max-size=SIZE   set maximum size of cache to SIZE (use 0 for no\n"
59 "                          limit; available suffixes: G, M and K; default\n"
60 "                          suffix: G)\n"
61 "    -s, --show-stats      show statistics summary\n"
62 "    -z, --zero-stats      zero statistics counters\n"
63 "\n"
64 "    -h, --help            print this help text\n"
65 "    -V, --version         print version and copyright information\n"
66 "\n"
67 "See also <http://ccache.samba.org>.\n";
68
69 /* current working directory taken from $PWD, or getcwd() if $PWD is bad */
70 char *current_working_dir = NULL;
71
72 /* the base cache directory */
73 char *cache_dir = NULL;
74
75 /* the directory for temporary files */
76 static char *temp_dir;
77
78 /* the debug logfile name, if set */
79 char *cache_logfile = NULL;
80
81 /* base directory (from CCACHE_BASEDIR) */
82 char *base_dir = NULL;
83
84 /* the original argument list */
85 static struct args *orig_args;
86
87 /* the source file */
88 static char *input_file;
89
90 /* The output file being compiled to. */
91 static char *output_obj;
92
93 /* The path to the dependency file (implicit or specified with -MF). */
94 static char *output_dep;
95
96 /*
97  * Name (represented as a struct file_hash) of the file containing the cached
98  * object code.
99  */
100 static struct file_hash *cached_obj_hash;
101
102 /*
103  * Full path to the file containing the cached object code
104  * (cachedir/a/b/cdef[...]-size.o).
105  */
106 static char *cached_obj;
107
108 /*
109  * Full path to the file containing the standard error output
110  * (cachedir/a/b/cdef[...]-size.stderr).
111  */
112 static char *cached_stderr;
113
114 /*
115  * Full path to the file containing the dependency information
116  * (cachedir/a/b/cdef[...]-size.d).
117  */
118 static char *cached_dep;
119
120 /*
121  * Full path to the file containing the manifest
122  * (cachedir/a/b/cdef[...]-size.manifest).
123  */
124 static char *manifest_path;
125
126 /*
127  * Time of compilation. Used to see if include files have changed after
128  * compilation.
129  */
130 static time_t time_of_compilation;
131
132 /* Bitmask of SLOPPY_*. */
133 unsigned sloppiness = 0;
134
135 /*
136  * Files included by the preprocessor and their hashes/sizes. Key: file path.
137  * Value: struct file_hash.
138  */
139 static struct hashtable *included_files;
140
141 /* is gcc being asked to output dependencies? */
142 static bool generating_dependencies;
143
144 /* the extension of the file (without dot) after pre-processing */
145 static const char *i_extension;
146
147 /* the name of the temporary pre-processor file */
148 static char *i_tmpfile;
149
150 /* are we compiling a .i or .ii file directly? */
151 static bool direct_i_file;
152
153 /* the name of the cpp stderr file */
154 static char *cpp_stderr;
155
156 /*
157  * Full path to the statistics file in the subdirectory where the cached result
158  * belongs (CCACHE_DIR/X/stats).
159  */
160 char *stats_file = NULL;
161
162 /* can we safely use the unification hashing backend? */
163 static bool enable_unify;
164
165 /* should we use the direct mode? */
166 static bool enable_direct = true;
167
168 /*
169  * Whether to enable compression of files stored in the cache. (Manifest files
170  * are always compressed.)
171  */
172 static bool enable_compression = false;
173
174 /* number of levels (1 <= nlevels <= 8) */
175 static int nlevels = 2;
176
177 /*
178  * Whether we should use the optimization of passing the already existing
179  * preprocessed source code to the compiler.
180  */
181 static bool compile_preprocessed_source_code;
182
183 /* Whether the output is a precompiled header */
184 static bool output_is_precompiled_header = false;
185
186 /*
187  * Whether we are using a precompiled header (either via -include or #include).
188  */
189 static bool using_precompiled_header = false;
190
191 /* How long (in microseconds) to wait before breaking a stale lock. */
192 unsigned lock_staleness_limit = 2000000;
193
194 enum fromcache_call_mode {
195         FROMCACHE_DIRECT_MODE,
196         FROMCACHE_CPP_MODE,
197         FROMCACHE_COMPILED_MODE
198 };
199
200 /*
201  * This is a string that identifies the current "version" of the hash sum
202  * computed by ccache. If, for any reason, we want to force the hash sum to be
203  * different for the same input in a new ccache version, we can just change
204  * this string. A typical example would be if the format of one of the files
205  * stored in the cache changes in a backwards-incompatible way.
206  */
207 static const char HASH_PREFIX[] = "3";
208
209 /* Something went badly wrong - just execute the real compiler. */
210 static void
211 failed(void)
212 {
213         char *e;
214
215         /* strip any local args */
216         args_strip(orig_args, "--ccache-");
217
218         if ((e = getenv("CCACHE_PREFIX"))) {
219                 char *p = find_executable(e, MYNAME);
220                 if (!p) {
221                         fatal("%s: %s", e, strerror(errno));
222                 }
223                 args_add_prefix(orig_args, p);
224         }
225
226         cc_log("Failed; falling back to running the real compiler");
227         cc_log_argv("Executing ", orig_args->argv);
228         exitfn_call();
229         execv(orig_args->argv[0], orig_args->argv);
230         fatal("%s: execv returned (%s)", orig_args->argv[0], strerror(errno));
231 }
232
233 static void
234 clean_up_tmp_files()
235 {
236         /* delete intermediate pre-processor file if needed */
237         if (i_tmpfile) {
238                 if (!direct_i_file) {
239                         tmp_unlink(i_tmpfile);
240                 }
241                 free(i_tmpfile);
242                 i_tmpfile = NULL;
243         }
244
245         /* delete the cpp stderr file if necessary */
246         if (cpp_stderr) {
247                 tmp_unlink(cpp_stderr);
248                 free(cpp_stderr);
249                 cpp_stderr = NULL;
250         }
251 }
252
253 /*
254  * Transform a name to a full path into the cache directory, creating needed
255  * sublevels if needed. Caller frees.
256  */
257 static char *
258 get_path_in_cache(const char *name, const char *suffix)
259 {
260         int i;
261         char *path;
262         char *result;
263
264         path = x_strdup(cache_dir);
265         for (i = 0; i < nlevels; ++i) {
266                 char *p = format("%s/%c", path, name[i]);
267                 free(path);
268                 path = p;
269                 if (create_dir(path) != 0) {
270                         cc_log("Failed to create %s: %s", path, strerror(errno));
271                         failed();
272                 }
273         }
274         result = format("%s/%s%s", path, name + nlevels, suffix);
275         free(path);
276         return result;
277 }
278
279 /*
280  * This function hashes an include file and stores the path and hash in the
281  * global included_files variable. If the include file is a PCH, cpp_hash is
282  * also updated. Takes over ownership of path.
283  */
284 static void
285 remember_include_file(char *path, size_t path_len, struct mdfour *cpp_hash)
286 {
287         struct mdfour fhash;
288         struct stat st;
289         char *source = NULL;
290         size_t size;
291         int result;
292         bool is_pch;
293
294         if (path_len >= 2 && (path[0] == '<' && path[path_len - 1] == '>')) {
295                 /* Typically <built-in> or <command-line>. */
296                 goto ignore;
297         }
298
299         if (str_eq(path, input_file)) {
300                 /* Don't remember the input file. */
301                 goto ignore;
302         }
303
304         if (hashtable_search(included_files, path)) {
305                 /* Already known include file. */
306                 goto ignore;
307         }
308
309         if (stat(path, &st) != 0) {
310                 cc_log("Failed to stat include file %s: %s", path, strerror(errno));
311                 goto failure;
312         }
313         if (S_ISDIR(st.st_mode)) {
314                 /* Ignore directory, typically $PWD. */
315                 goto ignore;
316         }
317         if (!S_ISREG(st.st_mode)) {
318                 /* Device, pipe, socket or other strange creature. */
319                 cc_log("Non-regular include file %s", path);
320                 goto failure;
321         }
322
323         /* Let's hash the include file. */
324         if (!(sloppiness & SLOPPY_INCLUDE_FILE_MTIME)
325             && st.st_mtime >= time_of_compilation) {
326                 cc_log("Include file %s too new", path);
327                 goto failure;
328         }
329
330         hash_start(&fhash);
331
332         is_pch = is_precompiled_header(path);
333         if (is_pch) {
334                 struct file_hash pch_hash;
335                 if (!hash_file(&fhash, path)) {
336                         goto failure;
337                 }
338                 hash_result_as_bytes(&fhash, pch_hash.hash);
339                 pch_hash.size = fhash.totalN;
340                 hash_delimiter(cpp_hash, "pch_hash");
341                 hash_buffer(cpp_hash, pch_hash.hash, sizeof(pch_hash.hash));
342         }
343         if (enable_direct) {
344                 struct file_hash *h;
345
346                 if (!is_pch) { /* else: the file has already been hashed. */
347                         if (st.st_size > 0) {
348                                 if (!read_file(path, st.st_size, &source, &size)) {
349                                         goto failure;
350                                 }
351                         } else {
352                                 source = x_strdup("");
353                                 size = 0;
354                         }
355
356                         result = hash_source_code_string(&fhash, source, size, path);
357                         if (result & HASH_SOURCE_CODE_ERROR
358                             || result & HASH_SOURCE_CODE_FOUND_TIME) {
359                                 goto failure;
360                         }
361                 }
362
363                 h = x_malloc(sizeof(*h));
364                 hash_result_as_bytes(&fhash, h->hash);
365                 h->size = fhash.totalN;
366                 hashtable_insert(included_files, path, h);
367         } else {
368                 free(path);
369         }
370
371         free(source);
372         return;
373
374 failure:
375         cc_log("Disabling direct mode");
376         enable_direct = false;
377         /* Fall through. */
378 ignore:
379         free(path);
380         free(source);
381 }
382
383 /*
384  * Make a relative path from CCACHE_BASEDIR to path. Takes over ownership of
385  * path. Caller frees.
386  */
387 static char *
388 make_relative_path(char *path)
389 {
390         char *relpath;
391
392         if (!base_dir || !str_startswith(path, base_dir)) {
393                 return path;
394         }
395
396         relpath = get_relative_path(current_working_dir, path);
397         free(path);
398         return relpath;
399 }
400
401 /*
402  * This function reads and hashes a file. While doing this, it also does these
403  * things:
404  *
405  * - Makes include file paths whose prefix is CCACHE_BASEDIR relative when
406  *   computing the hash sum.
407  * - Stores the paths and hashes of included files in the global variable
408  *   included_files.
409  */
410 static bool
411 process_preprocessed_file(struct mdfour *hash, const char *path)
412 {
413         char *data;
414         char *p, *q, *end;
415         size_t size;
416
417         if (!read_file(path, 0, &data, &size)) {
418                 return false;
419         }
420
421         included_files = create_hashtable(1000, hash_from_string, strings_equal);
422
423         /* Bytes between p and q are pending to be hashed. */
424         end = data + size;
425         p = data;
426         q = data;
427         while (q < end - 7) { /* There must be at least 7 characters (# 1 "x") left
428                                  to potentially find an include file path. */
429                 /*
430                  * Check if we look at a line containing the file name of an included file.
431                  * At least the following formats exist (where N is a positive integer):
432                  *
433                  * GCC:
434                  *
435                  *   # N "file"
436                  *   # N "file" N
437                  *   #pragma GCC pch_preprocess "file"
438                  *
439                  * HP's compiler:
440                  *
441                  *   #line N "file"
442                  *
443                  * AIX's compiler:
444                  *
445                  *   #line N "file"
446                  *   #line N
447                  *
448                  * Note that there may be other lines starting with '#' left after
449                  * preprocessing as well, for instance "#    pragma".
450                  */
451                 if (q[0] == '#'
452                         /* GCC: */
453                     && ((q[1] == ' ' && q[2] >= '0' && q[2] <= '9')
454                         /* GCC precompiled header: */
455                         || (q[1] == 'p'
456                             && str_startswith(&q[2], "ragma GCC pch_preprocess "))
457                         /* HP/AIX: */
458                         || (q[1] == 'l' && q[2] == 'i' && q[3] == 'n' && q[4] == 'e'
459                             && q[5] == ' '))
460                     && (q == data || q[-1] == '\n')) {
461                         char *path;
462
463                         while (q < end && *q != '"' && *q != '\n') {
464                                 q++;
465                         }
466                         if (q < end && *q == '\n') {
467                                 /* A newline before the quotation mark -> no match. */
468                                 continue;
469                         }
470                         q++;
471                         if (q >= end) {
472                                 cc_log("Failed to parse included file path");
473                                 free(data);
474                                 return false;
475                         }
476                         /* q points to the beginning of an include file path */
477                         hash_buffer(hash, p, q - p);
478                         p = q;
479                         while (q < end && *q != '"') {
480                                 q++;
481                         }
482                         /* p and q span the include file path */
483                         path = x_strndup(p, q - p);
484                         path = make_relative_path(path);
485                         hash_string(hash, path);
486                         remember_include_file(path, q - p, hash);
487                         p = q;
488                 } else {
489                         q++;
490                 }
491         }
492
493         hash_buffer(hash, p, (end - p));
494         free(data);
495         return true;
496 }
497
498 /* run the real compiler and put the result in cache */
499 static void
500 to_cache(struct args *args)
501 {
502         char *tmp_stdout, *tmp_stderr, *tmp_obj;
503         struct stat st;
504         int status;
505         size_t added_bytes = 0;
506         unsigned added_files = 0;
507
508         tmp_stdout = format("%s.tmp.stdout.%s", cached_obj, tmp_string());
509         tmp_stderr = format("%s.tmp.stderr.%s", cached_obj, tmp_string());
510         tmp_obj = format("%s.tmp.%s", cached_obj, tmp_string());
511
512         args_add(args, "-o");
513         args_add(args, tmp_obj);
514
515         /* Turn off DEPENDENCIES_OUTPUT when running cc1, because
516          * otherwise it will emit a line like
517          *
518          *  tmp.stdout.vexed.732.o: /home/mbp/.ccache/tmp.stdout.vexed.732.i
519          *
520          * unsetenv() is on BSD and Linux but not portable. */
521         putenv("DEPENDENCIES_OUTPUT");
522
523         if (compile_preprocessed_source_code) {
524                 args_add(args, i_tmpfile);
525         } else {
526                 args_add(args, input_file);
527         }
528
529         cc_log("Running real compiler");
530         status = execute(args->argv, tmp_stdout, tmp_stderr);
531         args_pop(args, 3);
532
533         if (stat(tmp_stdout, &st) != 0 || st.st_size != 0) {
534                 cc_log("Compiler produced stdout");
535                 stats_update(STATS_STDOUT);
536                 tmp_unlink(tmp_stdout);
537                 tmp_unlink(tmp_stderr);
538                 tmp_unlink(tmp_obj);
539                 failed();
540         }
541         tmp_unlink(tmp_stdout);
542
543         /*
544          * Merge stderr from the preprocessor (if any) and stderr from the real
545          * compiler into tmp_stderr.
546          */
547         if (cpp_stderr) {
548                 int fd_cpp_stderr;
549                 int fd_real_stderr;
550                 int fd_result;
551                 char *tmp_stderr2;
552
553                 tmp_stderr2 = format("%s.tmp.stderr2.%s", cached_obj, tmp_string());
554                 if (x_rename(tmp_stderr, tmp_stderr2)) {
555                         cc_log("Failed to rename %s to %s: %s", tmp_stderr, tmp_stderr2,
556                                strerror(errno));
557                         failed();
558                 }
559                 fd_cpp_stderr = open(cpp_stderr, O_RDONLY | O_BINARY);
560                 if (fd_cpp_stderr == -1) {
561                         cc_log("Failed opening %s: %s", cpp_stderr, strerror(errno));
562                         failed();
563                 }
564                 fd_real_stderr = open(tmp_stderr2, O_RDONLY | O_BINARY);
565                 if (fd_real_stderr == -1) {
566                         cc_log("Failed opening %s: %s", tmp_stderr2, strerror(errno));
567                         failed();
568                 }
569                 fd_result = open(tmp_stderr, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
570                 if (fd_result == -1) {
571                         cc_log("Failed opening %s: %s", tmp_stderr, strerror(errno));
572                         failed();
573                 }
574                 copy_fd(fd_cpp_stderr, fd_result);
575                 copy_fd(fd_real_stderr, fd_result);
576                 close(fd_cpp_stderr);
577                 close(fd_real_stderr);
578                 close(fd_result);
579                 tmp_unlink(tmp_stderr2);
580                 free(tmp_stderr2);
581         }
582
583         if (status != 0) {
584                 int fd;
585                 cc_log("Compiler gave exit status %d", status);
586                 stats_update(STATS_STATUS);
587
588                 fd = open(tmp_stderr, O_RDONLY | O_BINARY);
589                 if (fd != -1) {
590                         if (str_eq(output_obj, "/dev/null")
591                             || (access(tmp_obj, R_OK) == 0
592                                 && move_file(tmp_obj, output_obj, 0) == 0)
593                             || errno == ENOENT) {
594                                 /* we can use a quick method of getting the failed output */
595                                 copy_fd(fd, 2);
596                                 close(fd);
597                                 tmp_unlink(tmp_stderr);
598                                 exit(status);
599                         }
600                 }
601
602                 tmp_unlink(tmp_stderr);
603                 tmp_unlink(tmp_obj);
604                 failed();
605         }
606
607         if (stat(tmp_obj, &st) != 0) {
608                 cc_log("Compiler didn't produce an object file");
609                 stats_update(STATS_NOOUTPUT);
610                 failed();
611         }
612         if (st.st_size == 0) {
613                 cc_log("Compiler produced an empty object file");
614                 stats_update(STATS_EMPTYOUTPUT);
615                 failed();
616         }
617
618         if (stat(tmp_stderr, &st) != 0) {
619                 cc_log("Failed to stat %s: %s", tmp_stderr, strerror(errno));
620                 stats_update(STATS_ERROR);
621                 failed();
622         }
623         if (st.st_size > 0) {
624                 if (move_uncompressed_file(tmp_stderr, cached_stderr,
625                                            enable_compression) != 0) {
626                         cc_log("Failed to move %s to %s: %s", tmp_stderr, cached_stderr,
627                                strerror(errno));
628                         stats_update(STATS_ERROR);
629                         failed();
630                 }
631                 cc_log("Stored in cache: %s", cached_stderr);
632                 if (enable_compression) {
633                         stat(cached_stderr, &st);
634                 }
635                 added_bytes += file_size(&st);
636                 added_files += 1;
637         } else {
638                 tmp_unlink(tmp_stderr);
639         }
640         if (move_uncompressed_file(tmp_obj, cached_obj, enable_compression) != 0) {
641                 cc_log("Failed to move %s to %s: %s", tmp_obj, cached_obj, strerror(errno));
642                 stats_update(STATS_ERROR);
643                 failed();
644         } else {
645                 cc_log("Stored in cache: %s", cached_obj);
646                 stat(cached_obj, &st);
647                 added_bytes += file_size(&st);
648                 added_files += 1;
649         }
650
651         /*
652          * Do an extra stat on the potentially compressed object file for the
653          * size statistics.
654          */
655         if (stat(cached_obj, &st) != 0) {
656                 cc_log("Failed to stat %s: %s", cached_obj, strerror(errno));
657                 stats_update(STATS_ERROR);
658                 failed();
659         }
660
661         stats_update_size(STATS_TOCACHE, added_bytes / 1024, added_files);
662
663         free(tmp_obj);
664         free(tmp_stderr);
665         free(tmp_stdout);
666 }
667
668 /*
669  * Find the object file name by running the compiler in preprocessor mode.
670  * Returns the hash as a heap-allocated hex string.
671  */
672 static struct file_hash *
673 get_object_name_from_cpp(struct args *args, struct mdfour *hash)
674 {
675         char *input_base;
676         char *tmp;
677         char *path_stdout, *path_stderr;
678         int status;
679         struct file_hash *result;
680
681         /* ~/hello.c -> tmp.hello.123.i
682            limit the basename to 10
683            characters in order to cope with filesystem with small
684            maximum filename length limits */
685         input_base = basename(input_file);
686         tmp = strchr(input_base, '.');
687         if (tmp != NULL) {
688                 *tmp = 0;
689         }
690         if (strlen(input_base) > 10) {
691                 input_base[10] = 0;
692         }
693
694         /* now the run */
695         path_stdout = format("%s/%s.tmp.%s.%s",
696                              temp_dir, input_base, tmp_string(), i_extension);
697         path_stderr = format("%s/tmp.cpp_stderr.%s", temp_dir, tmp_string());
698
699         time_of_compilation = time(NULL);
700
701         if (!direct_i_file) {
702                 /* run cpp on the input file to obtain the .i */
703                 args_add(args, "-E");
704                 args_add(args, input_file);
705                 status = execute(args->argv, path_stdout, path_stderr);
706                 args_pop(args, 2);
707         } else {
708                 /* we are compiling a .i or .ii file - that means we
709                    can skip the cpp stage and directly form the
710                    correct i_tmpfile */
711                 path_stdout = input_file;
712                 if (create_empty_file(path_stderr) != 0) {
713                         cc_log("Failed to create %s: %s", path_stderr, strerror(errno));
714                         stats_update(STATS_ERROR);
715                         failed();
716                 }
717                 status = 0;
718         }
719
720         if (status != 0) {
721                 if (!direct_i_file) {
722                         tmp_unlink(path_stdout);
723                 }
724                 tmp_unlink(path_stderr);
725                 cc_log("Preprocessor gave exit status %d", status);
726                 stats_update(STATS_PREPROCESSOR);
727                 failed();
728         }
729
730         if (enable_unify) {
731                 /*
732                  * When we are doing the unifying tricks we need to include the
733                  * input file name in the hash to get the warnings right.
734                  */
735                 hash_delimiter(hash, "unifyfilename");
736                 hash_string(hash, input_file);
737
738                 hash_delimiter(hash, "unifycpp");
739                 if (unify_hash(hash, path_stdout) != 0) {
740                         stats_update(STATS_ERROR);
741                         tmp_unlink(path_stderr);
742                         cc_log("Failed to unify %s", path_stdout);
743                         failed();
744                 }
745         } else {
746                 hash_delimiter(hash, "cpp");
747                 if (!process_preprocessed_file(hash, path_stdout)) {
748                         stats_update(STATS_ERROR);
749                         tmp_unlink(path_stderr);
750                         failed();
751                 }
752         }
753
754         hash_delimiter(hash, "cppstderr");
755         if (!hash_file(hash, path_stderr)) {
756                 fatal("Failed to open %s: %s", path_stderr, strerror(errno));
757         }
758
759         i_tmpfile = path_stdout;
760
761         if (compile_preprocessed_source_code) {
762                 /*
763                  * If we are using the CPP trick, we need to remember this
764                  * stderr data and output it just before the main stderr from
765                  * the compiler pass.
766                  */
767                 cpp_stderr = path_stderr;
768         } else {
769                 tmp_unlink(path_stderr);
770                 free(path_stderr);
771         }
772
773         result = x_malloc(sizeof(*result));
774         hash_result_as_bytes(hash, result->hash);
775         result->size = hash->totalN;
776         return result;
777 }
778
779 static void
780 update_cached_result_globals(struct file_hash *hash)
781 {
782         char *object_name;
783
784         object_name = format_hash_as_string(hash->hash, hash->size);
785         cached_obj_hash = hash;
786         cached_obj = get_path_in_cache(object_name, ".o");
787         cached_stderr = get_path_in_cache(object_name, ".stderr");
788         cached_dep = get_path_in_cache(object_name, ".d");
789         stats_file = format("%s/%c/stats", cache_dir, object_name[0]);
790         free(object_name);
791 }
792
793 /*
794  * Update a hash sum with information common for the direct and preprocessor
795  * modes.
796  */
797 static void
798 calculate_common_hash(struct args *args, struct mdfour *hash)
799 {
800         struct stat st;
801         const char *compilercheck;
802         char *p;
803
804         hash_string(hash, HASH_PREFIX);
805
806         /*
807          * We have to hash the extension, as a .i file isn't treated the same
808          * by the compiler as a .ii file.
809          */
810         hash_delimiter(hash, "ext");
811         hash_string(hash, i_extension);
812
813         if (stat(args->argv[0], &st) != 0) {
814                 cc_log("Couldn't stat compiler %s: %s", args->argv[0], strerror(errno));
815                 stats_update(STATS_COMPILER);
816                 failed();
817         }
818
819         /*
820          * Hash information about the compiler.
821          */
822         compilercheck = getenv("CCACHE_COMPILERCHECK");
823         if (!compilercheck) {
824                 compilercheck = "mtime";
825         }
826         if (str_eq(compilercheck, "none")) {
827                 /* Do nothing. */
828         } else if (str_eq(compilercheck, "content")) {
829                 hash_delimiter(hash, "cc_content");
830                 hash_file(hash, args->argv[0]);
831         } else if (str_eq(compilercheck, "mtime")) {
832                 hash_delimiter(hash, "cc_mtime");
833                 hash_int(hash, st.st_size);
834                 hash_int(hash, st.st_mtime);
835         } else { /* command string */
836                 if (!hash_multicommand_output(hash, compilercheck, orig_args->argv[0])) {
837                         fatal("Failure running compiler check command: %s", compilercheck);
838                 }
839         }
840
841         /*
842          * Also hash the compiler name as some compilers use hard links and
843          * behave differently depending on the real name.
844          */
845         hash_delimiter(hash, "cc_name");
846         hash_string(hash, basename(args->argv[0]));
847
848         /* Possibly hash the current working directory. */
849         if (getenv("CCACHE_HASHDIR")) {
850                 char *cwd = gnu_getcwd();
851                 if (cwd) {
852                         hash_delimiter(hash, "cwd");
853                         hash_string(hash, cwd);
854                         free(cwd);
855                 }
856         }
857
858         p = getenv("CCACHE_EXTRAFILES");
859         if (p) {
860                 char *path, *q, *saveptr = NULL;
861                 p = x_strdup(p);
862                 q = p;
863                 while ((path = strtok_r(q, PATH_DELIM, &saveptr))) {
864                         cc_log("Hashing extra file %s", path);
865                         hash_delimiter(hash, "extrafile");
866                         if (!hash_file(hash, path)) {
867                                 stats_update(STATS_BADEXTRAFILE);
868                                 failed();
869                         }
870                         q = NULL;
871                 }
872                 free(p);
873         }
874 }
875
876 /*
877  * Update a hash sum with information specific to the direct and preprocessor
878  * modes and calculate the object hash. Returns the object hash on success,
879  * otherwise NULL. Caller frees.
880  */
881 static struct file_hash *
882 calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode)
883 {
884         int i;
885         char *manifest_name;
886         struct stat st;
887         int result;
888         struct file_hash *object_hash = NULL;
889
890         /* first the arguments */
891         for (i = 1; i < args->argc; i++) {
892                 /* -L doesn't affect compilation. */
893                 if (i < args->argc-1 && str_eq(args->argv[i], "-L")) {
894                         i++;
895                         continue;
896                 }
897                 if (str_startswith(args->argv[i], "-L")) {
898                         continue;
899                 }
900
901                 /* When using the preprocessor, some arguments don't contribute
902                    to the hash. The theory is that these arguments will change
903                    the output of -E if they are going to have any effect at
904                    all. For precompiled headers this might not be the case. */
905                 if (!direct_mode && !output_is_precompiled_header
906                     && !using_precompiled_header) {
907                         if (compopt_affects_cpp(args->argv[i])) {
908                                 i++;
909                                 continue;
910                         }
911                         if (compopt_short(compopt_affects_cpp, args->argv[i])) {
912                                 continue;
913                         }
914                 }
915
916                 if (str_startswith(args->argv[i], "--specs=") &&
917                     stat(args->argv[i] + 8, &st) == 0) {
918                         /* If given a explicit specs file, then hash that file,
919                            but don't include the path to it in the hash. */
920                         hash_delimiter(hash, "specs");
921                         if (!hash_file(hash, args->argv[i] + 8)) {
922                                 failed();
923                         }
924                         continue;
925                 }
926
927                 /* All other arguments are included in the hash. */
928                 hash_delimiter(hash, "arg");
929                 hash_string(hash, args->argv[i]);
930         }
931
932         if (direct_mode) {
933                 if (!(sloppiness & SLOPPY_FILE_MACRO)) {
934                         /*
935                          * The source code file or an include file may contain
936                          * __FILE__, so make sure that the hash is unique for
937                          * the file name.
938                          */
939                         hash_delimiter(hash, "inputfile");
940                         hash_string(hash, input_file);
941                 }
942
943                 hash_delimiter(hash, "sourcecode");
944                 result = hash_source_code_file(hash, input_file);
945                 if (result & HASH_SOURCE_CODE_ERROR) {
946                         failed();
947                 }
948                 if (result & HASH_SOURCE_CODE_FOUND_TIME) {
949                         cc_log("Disabling direct mode");
950                         enable_direct = false;
951                         return NULL;
952                 }
953                 manifest_name = hash_result(hash);
954                 manifest_path = get_path_in_cache(manifest_name, ".manifest");
955                 free(manifest_name);
956                 cc_log("Looking for object file hash in %s", manifest_path);
957                 object_hash = manifest_get(manifest_path);
958                 if (object_hash) {
959                         cc_log("Got object file hash from manifest");
960                 } else {
961                         cc_log("Did not find object file hash in manifest");
962                 }
963         } else {
964                 object_hash = get_object_name_from_cpp(args, hash);
965                 cc_log("Got object file hash from preprocessor");
966                 if (generating_dependencies) {
967                         cc_log("Preprocessor created %s", output_dep);
968                 }
969         }
970
971         return object_hash;
972 }
973
974 /*
975  * Try to return the compile result from cache. If we can return from cache
976  * then this function exits with the correct status code, otherwise it returns.
977  */
978 static void
979 from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest)
980 {
981         int fd_stderr;
982         int ret;
983         struct stat st;
984         bool produce_dep_file;
985
986         /* the user might be disabling cache hits */
987         if (mode != FROMCACHE_COMPILED_MODE && getenv("CCACHE_RECACHE")) {
988                 return;
989         }
990
991         /* Check if the object file is there. */
992         if (stat(cached_obj, &st) != 0) {
993                 cc_log("Object file %s not in cache", cached_obj);
994                 return;
995         }
996
997         /*
998          * (If mode != FROMCACHE_DIRECT_MODE, the dependency file is created by
999          * gcc.)
1000          */
1001         produce_dep_file = generating_dependencies && mode == FROMCACHE_DIRECT_MODE;
1002
1003         /* If the dependency file should be in the cache, check that it is. */
1004         if (produce_dep_file && stat(cached_dep, &st) != 0) {
1005                 cc_log("Dependency file %s missing in cache", cached_dep);
1006                 return;
1007         }
1008
1009         if (str_eq(output_obj, "/dev/null")) {
1010                 ret = 0;
1011         } else {
1012                 x_unlink(output_obj);
1013                 /* only make a hardlink if the cache file is uncompressed */
1014                 if (getenv("CCACHE_HARDLINK") && !file_is_compressed(cached_obj)) {
1015                         ret = link(cached_obj, output_obj);
1016                 } else {
1017                         ret = copy_file(cached_obj, output_obj, 0);
1018                 }
1019         }
1020
1021         if (ret == -1) {
1022                 if (errno == ENOENT) {
1023                         /* Someone removed the file just before we began copying? */
1024                         cc_log("Object file %s just disappeared from cache", cached_obj);
1025                         stats_update(STATS_MISSING);
1026                 } else {
1027                         cc_log("Failed to copy/link %s to %s: %s",
1028                                cached_obj, output_obj, strerror(errno));
1029                         stats_update(STATS_ERROR);
1030                         failed();
1031                 }
1032                 x_unlink(output_obj);
1033                 x_unlink(cached_stderr);
1034                 x_unlink(cached_obj);
1035                 x_unlink(cached_dep);
1036                 return;
1037         } else {
1038                 cc_log("Created %s from %s", output_obj, cached_obj);
1039         }
1040
1041         if (produce_dep_file) {
1042                 x_unlink(output_dep);
1043                 /* only make a hardlink if the cache file is uncompressed */
1044                 if (getenv("CCACHE_HARDLINK") && !file_is_compressed(cached_dep)) {
1045                         ret = link(cached_dep, output_dep);
1046                 } else {
1047                         ret = copy_file(cached_dep, output_dep, 0);
1048                 }
1049                 if (ret == -1) {
1050                         if (errno == ENOENT) {
1051                                 /*
1052                                  * Someone removed the file just before we
1053                                  * began copying?
1054                                  */
1055                                 cc_log("Dependency file %s just disappeared from cache", output_obj);
1056                                 stats_update(STATS_MISSING);
1057                         } else {
1058                                 cc_log("Failed to copy/link %s to %s: %s",
1059                                        cached_dep, output_dep, strerror(errno));
1060                                 stats_update(STATS_ERROR);
1061                                 failed();
1062                         }
1063                         x_unlink(output_obj);
1064                         x_unlink(output_dep);
1065                         x_unlink(cached_stderr);
1066                         x_unlink(cached_obj);
1067                         x_unlink(cached_dep);
1068                         return;
1069                 } else {
1070                         cc_log("Created %s from %s", output_dep, cached_dep);
1071                 }
1072         }
1073
1074         /* Update modification timestamps to save files from LRU cleanup.
1075            Also gives files a sensible mtime when hard-linking. */
1076         update_mtime(cached_obj);
1077         update_mtime(cached_stderr);
1078         if (produce_dep_file) {
1079                 update_mtime(cached_dep);
1080         }
1081
1082         if (generating_dependencies && mode != FROMCACHE_DIRECT_MODE) {
1083                 /* Store the dependency file in the cache. */
1084                 ret = copy_file(output_dep, cached_dep, enable_compression);
1085                 if (ret == -1) {
1086                         cc_log("Failed to copy %s to %s: %s", output_dep, cached_dep,
1087                                strerror(errno));
1088                         /* Continue despite the error. */
1089                 } else {
1090                         cc_log("Stored in cache: %s", cached_dep);
1091                         stat(cached_dep, &st);
1092                         stats_update_size(STATS_NONE, file_size(&st) / 1024, 1);
1093                 }
1094         }
1095
1096         /* Send the stderr, if any. */
1097         fd_stderr = open(cached_stderr, O_RDONLY | O_BINARY);
1098         if (fd_stderr != -1) {
1099                 copy_fd(fd_stderr, 2);
1100                 close(fd_stderr);
1101         }
1102
1103         /* Create or update the manifest file. */
1104         if (enable_direct
1105             && put_object_in_manifest
1106             && included_files
1107             && !getenv("CCACHE_READONLY")) {
1108                 struct stat st;
1109                 size_t old_size = 0; /* in bytes */
1110                 if (stat(manifest_path, &st) == 0) {
1111                         old_size = file_size(&st);
1112                 }
1113                 if (manifest_put(manifest_path, cached_obj_hash, included_files)) {
1114                         cc_log("Added object file hash to %s", manifest_path);
1115                         update_mtime(manifest_path);
1116                         stat(manifest_path, &st);
1117                         stats_update_size(STATS_NONE,
1118                                           (file_size(&st) - old_size) / 1024,
1119                                           old_size == 0 ? 1 : 0);
1120                 } else {
1121                         cc_log("Failed to add object file hash to %s", manifest_path);
1122                 }
1123         }
1124
1125         /* log the cache hit */
1126         switch (mode) {
1127         case FROMCACHE_DIRECT_MODE:
1128                 cc_log("Succeded getting cached result");
1129                 stats_update(STATS_CACHEHIT_DIR);
1130                 break;
1131
1132         case FROMCACHE_CPP_MODE:
1133                 cc_log("Succeded getting cached result");
1134                 stats_update(STATS_CACHEHIT_CPP);
1135                 break;
1136
1137         case FROMCACHE_COMPILED_MODE:
1138                 /* Stats already updated in to_cache(). */
1139                 break;
1140         }
1141
1142         /* and exit with the right status code */
1143         exit(0);
1144 }
1145
1146 /* find the real compiler. We just search the PATH to find a executable of the
1147    same name that isn't a link to ourselves */
1148 static void
1149 find_compiler(int argc, char **argv)
1150 {
1151         char *base;
1152         char *path;
1153         char *compiler;
1154
1155         orig_args = args_init(argc, argv);
1156
1157         base = basename(argv[0]);
1158
1159         /* we might be being invoked like "ccache gcc -c foo.c" */
1160         if (same_executable_name(base, MYNAME)) {
1161                 args_remove_first(orig_args);
1162                 free(base);
1163                 if (is_full_path(argv[1])) {
1164                         /* a full path was given */
1165                         return;
1166                 }
1167                 base = basename(argv[1]);
1168         }
1169
1170         /* support user override of the compiler */
1171         if ((path = getenv("CCACHE_CC"))) {
1172                 base = x_strdup(path);
1173         }
1174
1175         compiler = find_executable(base, MYNAME);
1176
1177         /* can't find the compiler! */
1178         if (!compiler) {
1179                 stats_update(STATS_COMPILER);
1180                 fatal("Could not find compiler \"%s\" in PATH", base);
1181         }
1182         if (str_eq(compiler, argv[0])) {
1183                 fatal("Recursive invocation (the name of the ccache binary must be \"%s\")",
1184                       MYNAME);
1185         }
1186         orig_args->argv[0] = compiler;
1187 }
1188
1189 bool
1190 is_precompiled_header(const char *path)
1191 {
1192         return str_eq(get_extension(path), ".gch");
1193 }
1194
1195 /*
1196  * Process the compiler options into options suitable for passing to the
1197  * preprocessor and the real compiler. The preprocessor options don't include
1198  * -E; this is added later. Returns true on success, otherwise false.
1199  */
1200 bool
1201 cc_process_args(struct args *orig_args, struct args **preprocessor_args,
1202                 struct args **compiler_args)
1203 {
1204         int i;
1205         bool found_c_opt = false;
1206         bool found_S_opt = false;
1207         bool found_arch_opt = false;
1208         bool found_pch = false;
1209         bool found_fpch_preprocess = false;
1210         const char *explicit_language = NULL; /* As specified with -x. */
1211         const char *file_language;            /* As deduced from file extension. */
1212         const char *actual_language;          /* Language to actually use. */
1213         const char *input_charset = NULL;
1214         struct stat st;
1215         /* is the dependency makefile name overridden with -MF? */
1216         bool dependency_filename_specified = false;
1217         /* is the dependency makefile target name specified with -MT or -MQ? */
1218         bool dependency_target_specified = false;
1219         struct args *stripped_args = NULL, *dep_args = NULL;
1220         int argc = orig_args->argc;
1221         char **argv = orig_args->argv;
1222         bool result = true;
1223
1224         stripped_args = args_init(0, NULL);
1225         dep_args = args_init(0, NULL);
1226
1227         args_add(stripped_args, argv[0]);
1228
1229         for (i = 1; i < argc; i++) {
1230                 /* The user knows best: just swallow the next arg */
1231                 if (str_eq(argv[i], "--ccache-skip")) {
1232                         i++;
1233                         if (i == argc) {
1234                                 cc_log("--ccache-skip lacks an argument");
1235                                 result = false;
1236                                 goto out;
1237                         }
1238                         args_add(stripped_args, argv[i]);
1239                         continue;
1240                 }
1241
1242                 /* Special case for -E. */
1243                 if (str_eq(argv[i], "-E")) {
1244                         stats_update(STATS_PREPROCESSING);
1245                         result = false;
1246                         goto out;
1247                 }
1248
1249                 /* These are always too hard. */
1250                 if (compopt_too_hard(argv[i])
1251                     || str_startswith(argv[i], "@")
1252                     || str_startswith(argv[i], "-fdump-")) {
1253                         cc_log("Compiler option %s is unsupported", argv[i]);
1254                         stats_update(STATS_UNSUPPORTED);
1255                         result = false;
1256                         goto out;
1257                 }
1258
1259                 /* These are too hard in direct mode. */
1260                 if (enable_direct) {
1261                         if (compopt_too_hard_for_direct_mode(argv[i])) {
1262                                 cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
1263                                 enable_direct = false;
1264                         }
1265                 }
1266
1267                 /* Multiple -arch options are too hard. */
1268                 if (str_eq(argv[i], "-arch")) {
1269                         if (found_arch_opt) {
1270                                 cc_log("More than one -arch compiler option is unsupported");
1271                                 stats_update(STATS_UNSUPPORTED);
1272                                 result = false;
1273                                 goto out;
1274                         } else {
1275                                 found_arch_opt = true;
1276                         }
1277                 }
1278
1279                 if (str_eq(argv[i], "-fpch-preprocess")) {
1280                         found_fpch_preprocess = true;
1281                 }
1282
1283                 /* we must have -c */
1284                 if (str_eq(argv[i], "-c")) {
1285                         args_add(stripped_args, argv[i]);
1286                         found_c_opt = true;
1287                         continue;
1288                 }
1289
1290                 /* -S changes the default extension */
1291                 if (str_eq(argv[i], "-S")) {
1292                         args_add(stripped_args, argv[i]);
1293                         found_S_opt = true;
1294                         continue;
1295                 }
1296
1297                 /*
1298                  * Special handling for -x: remember the last specified language before the
1299                  * input file and strip all -x options from the arguments.
1300                  */
1301                 if (str_eq(argv[i], "-x")) {
1302                         if (i == argc-1) {
1303                                 cc_log("Missing argument to %s", argv[i]);
1304                                 stats_update(STATS_ARGS);
1305                                 result = false;
1306                                 goto out;
1307                         }
1308                         if (!input_file) {
1309                                 explicit_language = argv[i+1];
1310                         }
1311                         i++;
1312                         continue;
1313                 }
1314                 if (str_startswith(argv[i], "-x")) {
1315                         if (!input_file) {
1316                                 explicit_language = &argv[i][2];
1317                         }
1318                         continue;
1319                 }
1320
1321                 /* we need to work out where the output was meant to go */
1322                 if (str_eq(argv[i], "-o")) {
1323                         if (i == argc-1) {
1324                                 cc_log("Missing argument to %s", argv[i]);
1325                                 stats_update(STATS_ARGS);
1326                                 result = false;
1327                                 goto out;
1328                         }
1329                         output_obj = argv[i+1];
1330                         i++;
1331                         continue;
1332                 }
1333
1334                 /* alternate form of -o, with no space */
1335                 if (str_startswith(argv[i], "-o")) {
1336                         output_obj = &argv[i][2];
1337                         continue;
1338                 }
1339
1340                 /* debugging is handled specially, so that we know if we
1341                    can strip line number info
1342                 */
1343                 if (str_startswith(argv[i], "-g")) {
1344                         args_add(stripped_args, argv[i]);
1345                         if (enable_unify && !str_eq(argv[i], "-g0")) {
1346                                 cc_log("%s used; disabling unify mode", argv[i]);
1347                                 enable_unify = false;
1348                         }
1349                         if (str_eq(argv[i], "-g3")) {
1350                                 /*
1351                                  * Fix for bug 7190 ("commandline macros (-D)
1352                                  * have non-zero lineno when using -g3").
1353                                  */
1354                                 cc_log("%s used; not compiling preprocessed code", argv[i]);
1355                                 compile_preprocessed_source_code = false;
1356                         }
1357                         continue;
1358                 }
1359
1360                 /* These options require special handling, because they
1361                    behave differently with gcc -E, when the output
1362                    file is not specified. */
1363                 if (str_eq(argv[i], "-MD") || str_eq(argv[i], "-MMD")) {
1364                         generating_dependencies = true;
1365                         args_add(dep_args, argv[i]);
1366                         continue;
1367                 }
1368                 if (str_startswith(argv[i], "-MF")) {
1369                         char *arg;
1370                         dependency_filename_specified = true;
1371                         free(output_dep);
1372                         args_add(dep_args, argv[i]);
1373                         if (strlen(argv[i]) == 3) {
1374                                 /* -MF arg */
1375                                 if (i >= argc - 1) {
1376                                         cc_log("Missing argument to %s", argv[i]);
1377                                         stats_update(STATS_ARGS);
1378                                         result = false;
1379                                         goto out;
1380                                 }
1381                                 arg = argv[i + 1];
1382                                 args_add(dep_args, argv[i + 1]);
1383                                 i++;
1384                         } else {
1385                                 /* -MFarg */
1386                                 arg = &argv[i][3];
1387                         }
1388                         output_dep = make_relative_path(x_strdup(arg));
1389                         continue;
1390                 }
1391                 if (str_startswith(argv[i], "-MQ") || str_startswith(argv[i], "-MT")) {
1392                         args_add(dep_args, argv[i]);
1393                         if (strlen(argv[i]) == 3) {
1394                                 /* -MQ arg or -MT arg */
1395                                 if (i >= argc - 1) {
1396                                         cc_log("Missing argument to %s", argv[i]);
1397                                         stats_update(STATS_ARGS);
1398                                         result = false;
1399                                         goto out;
1400                                 }
1401                                 args_add(dep_args, argv[i + 1]);
1402                                 i++;
1403                                 /*
1404                                  * Yes, that's right. It's strange, but apparently, GCC behaves
1405                                  * differently for -MT arg and -MTarg (and similar for -MQ): in the
1406                                  * latter case, but not in the former, an implicit dependency for the
1407                                  * object file is added to the dependency file.
1408                                  */
1409                                 dependency_target_specified = true;
1410                         }
1411                         continue;
1412                 }
1413                 if (str_startswith(argv[i], "--sysroot=")) {
1414                         char *relpath = make_relative_path(x_strdup(argv[i] + 10));
1415                         char *option = format("--sysroot=%s", relpath);
1416                         args_add(stripped_args, option);
1417                         free(relpath);
1418                         free(option);
1419                         continue;
1420                 }
1421                 if (str_startswith(argv[i], "-Wp,")) {
1422                         if (str_startswith(argv[i], "-Wp,-MD,") && !strchr(argv[i] + 8, ',')) {
1423                                 generating_dependencies = true;
1424                                 dependency_filename_specified = true;
1425                                 free(output_dep);
1426                                 output_dep = make_relative_path(x_strdup(argv[i] + 8));
1427                                 args_add(dep_args, argv[i]);
1428                                 continue;
1429                         } else if (str_startswith(argv[i], "-Wp,-MMD,")
1430                                    && !strchr(argv[i] + 9, ',')) {
1431                                 generating_dependencies = true;
1432                                 dependency_filename_specified = true;
1433                                 free(output_dep);
1434                                 output_dep = make_relative_path(x_strdup(argv[i] + 9));
1435                                 args_add(dep_args, argv[i]);
1436                                 continue;
1437                         } else if (enable_direct) {
1438                                 /*
1439                                  * -Wp, can be used to pass too hard options to
1440                                  * the preprocessor. Hence, disable direct
1441                                  * mode.
1442                                  */
1443                                 cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
1444                                 enable_direct = false;
1445                         }
1446                 }
1447                 if (str_eq(argv[i], "-MP")) {
1448                         args_add(dep_args, argv[i]);
1449                         continue;
1450                 }
1451
1452                 /* Input charset needs to be handled specially. */
1453                 if (str_startswith(argv[i], "-finput-charset=")) {
1454                         input_charset = argv[i];
1455                         continue;
1456                 }
1457
1458                 /*
1459                  * Options taking an argument that that we may want to rewrite
1460                  * to relative paths to get better hit rate. A secondary effect
1461                  * is that paths in the standard error output produced by the
1462                  * compiler will be normalized.
1463                  */
1464                 if (compopt_takes_path(argv[i])) {
1465                         char *relpath;
1466                         char *pchpath;
1467                         if (i == argc-1) {
1468                                 cc_log("Missing argument to %s", argv[i]);
1469                                 stats_update(STATS_ARGS);
1470                                 result = false;
1471                                 goto out;
1472                         }
1473
1474                         args_add(stripped_args, argv[i]);
1475                         relpath = make_relative_path(x_strdup(argv[i+1]));
1476                         args_add(stripped_args, relpath);
1477
1478                         /* Try to be smart about detecting precompiled headers */
1479                         pchpath = format("%s.gch", argv[i+1]);
1480                         if (stat(pchpath, &st) == 0) {
1481                                 cc_log("Detected use of precompiled header: %s", pchpath);
1482                                 found_pch = true;
1483                         }
1484
1485                         free(pchpath);
1486                         free(relpath);
1487                         i++;
1488                         continue;
1489                 }
1490
1491                 /* Same as above but options with concatenated argument. */
1492                 if (compopt_short(compopt_takes_path, argv[i])) {
1493                         char *relpath;
1494                         char *option;
1495                         relpath = make_relative_path(x_strdup(argv[i] + 2));
1496                         option = format("-%c%s", argv[i][1], relpath);
1497                         args_add(stripped_args, option);
1498                         free(relpath);
1499                         free(option);
1500                         continue;
1501                 }
1502
1503                 /* options that take an argument */
1504                 if (compopt_takes_arg(argv[i])) {
1505                         if (i == argc-1) {
1506                                 cc_log("Missing argument to %s", argv[i]);
1507                                 stats_update(STATS_ARGS);
1508                                 result = false;
1509                                 goto out;
1510                         }
1511                         args_add(stripped_args, argv[i]);
1512                         args_add(stripped_args, argv[i+1]);
1513                         i++;
1514                         continue;
1515                 }
1516
1517                 /* other options */
1518                 if (argv[i][0] == '-') {
1519                         args_add(stripped_args, argv[i]);
1520                         continue;
1521                 }
1522
1523                 /* if an argument isn't a plain file then assume its
1524                    an option, not an input file. This allows us to
1525                    cope better with unusual compiler options */
1526                 if (stat(argv[i], &st) != 0 || !S_ISREG(st.st_mode)) {
1527                         cc_log("%s is not a regular file, not considering as input file",
1528                                argv[i]);
1529                         args_add(stripped_args, argv[i]);
1530                         continue;
1531                 }
1532
1533                 if (input_file) {
1534                         if (language_for_file(argv[i])) {
1535                                 cc_log("Multiple input files: %s and %s", input_file, argv[i]);
1536                                 stats_update(STATS_MULTIPLE);
1537                         } else if (!found_c_opt) {
1538                                 cc_log("Called for link with %s", argv[i]);
1539                                 if (strstr(argv[i], "conftest.")) {
1540                                         stats_update(STATS_CONFTEST);
1541                                 } else {
1542                                         stats_update(STATS_LINK);
1543                                 }
1544                         } else {
1545                                 cc_log("Unsupported source extension: %s", argv[i]);
1546                                 stats_update(STATS_SOURCELANG);
1547                         }
1548                         result = false;
1549                         goto out;
1550                 }
1551
1552                 /* Rewrite to relative to increase hit rate. */
1553                 input_file = make_relative_path(x_strdup(argv[i]));
1554         }
1555
1556         if (!input_file) {
1557                 cc_log("No input file found");
1558                 stats_update(STATS_NOINPUT);
1559                 result = false;
1560                 goto out;
1561         }
1562
1563         if (found_pch || found_fpch_preprocess) {
1564                 using_precompiled_header = true;
1565                 if (!(sloppiness & SLOPPY_TIME_MACROS)) {
1566                         cc_log("You have to specify \"time_macros\" sloppiness when using"
1567                                " precompiled headers to get direct hits");
1568                         cc_log("Disabling direct mode");
1569                         stats_update(STATS_CANTUSEPCH);
1570                         result = false;
1571                         goto out;
1572                 }
1573         }
1574
1575         if (explicit_language && str_eq(explicit_language, "none")) {
1576                 explicit_language = NULL;
1577         }
1578         file_language = language_for_file(input_file);
1579         if (explicit_language) {
1580                 if (!language_is_supported(explicit_language)) {
1581                         cc_log("Unsupported language: %s", explicit_language);
1582                         stats_update(STATS_SOURCELANG);
1583                         result = false;
1584                         goto out;
1585                 }
1586                 actual_language = explicit_language;
1587         } else {
1588                 actual_language = file_language;
1589         }
1590
1591         output_is_precompiled_header =
1592                 actual_language && strstr(actual_language, "-header") != NULL;
1593
1594         if (!found_c_opt && !output_is_precompiled_header) {
1595                 cc_log("No -c option found");
1596                 /* I find that having a separate statistic for autoconf tests is useful,
1597                    as they are the dominant form of "called for link" in many cases */
1598                 if (strstr(input_file, "conftest.")) {
1599                         stats_update(STATS_CONFTEST);
1600                 } else {
1601                         stats_update(STATS_LINK);
1602                 }
1603                 result = false;
1604                 goto out;
1605         }
1606
1607         if (!actual_language) {
1608                 cc_log("Unsupported source extension: %s", input_file);
1609                 stats_update(STATS_SOURCELANG);
1610                 result = false;
1611                 goto out;
1612         }
1613
1614         direct_i_file = language_is_preprocessed(actual_language);
1615
1616         if (output_is_precompiled_header) {
1617                 /* It doesn't work to create the .gch from preprocessed source. */
1618                 cc_log("Creating precompiled header; not compiling preprocessed code");
1619                 compile_preprocessed_source_code = false;
1620         }
1621
1622         i_extension = getenv("CCACHE_EXTENSION");
1623         if (!i_extension) {
1624                 const char *p_language = p_language_for_language(actual_language);
1625                 i_extension = extension_for_language(p_language) + 1;
1626         }
1627
1628         /* don't try to second guess the compilers heuristics for stdout handling */
1629         if (output_obj && str_eq(output_obj, "-")) {
1630                 stats_update(STATS_OUTSTDOUT);
1631                 cc_log("Output file is -");
1632                 result = false;
1633                 goto out;
1634         }
1635
1636         if (!output_obj) {
1637                 if (output_is_precompiled_header) {
1638                         output_obj = format("%s.gch", input_file);
1639                 } else {
1640                         char *p;
1641                         output_obj = x_strdup(input_file);
1642                         if ((p = strrchr(output_obj, '/'))) {
1643                                 output_obj = p+1;
1644                         }
1645                         p = strrchr(output_obj, '.');
1646                         if (!p || !p[1]) {
1647                                 cc_log("Badly formed object filename");
1648                                 stats_update(STATS_ARGS);
1649                                 result = false;
1650                                 goto out;
1651                         }
1652                         p[1] = found_S_opt ? 's' : 'o';
1653                         p[2] = 0;
1654                 }
1655         }
1656
1657         /* cope with -o /dev/null */
1658         if (!str_eq(output_obj,"/dev/null")
1659             && stat(output_obj, &st) == 0
1660             && !S_ISREG(st.st_mode)) {
1661                 cc_log("Not a regular file: %s", output_obj);
1662                 stats_update(STATS_DEVICE);
1663                 result = false;
1664                 goto out;
1665         }
1666
1667         /*
1668          * Some options shouldn't be passed to the real compiler when it compiles
1669          * preprocessed code:
1670          *
1671          * -finput-charset=XXX (otherwise conversion happens twice)
1672          * -x XXX (otherwise the wrong language is selected)
1673          */
1674         *preprocessor_args = args_copy(stripped_args);
1675         if (input_charset) {
1676                 args_add(*preprocessor_args, input_charset);
1677         }
1678         if (found_pch) {
1679                 args_add(*preprocessor_args, "-fpch-preprocess");
1680         }
1681         if (explicit_language) {
1682                 args_add(*preprocessor_args, "-x");
1683                 args_add(*preprocessor_args, explicit_language);
1684         }
1685
1686         /*
1687          * Add flags for dependency generation only to the preprocessor command line.
1688          */
1689         if (generating_dependencies) {
1690                 if (!dependency_filename_specified) {
1691                         char *default_depfile_name;
1692                         char *base_name;
1693
1694                         base_name = remove_extension(output_obj);
1695                         default_depfile_name = format("%s.d", base_name);
1696                         free(base_name);
1697                         args_add(dep_args, "-MF");
1698                         args_add(dep_args, default_depfile_name);
1699                         output_dep = make_relative_path(x_strdup(default_depfile_name));
1700                 }
1701
1702                 if (!dependency_target_specified) {
1703                         args_add(dep_args, "-MQ");
1704                         args_add(dep_args, output_obj);
1705                 }
1706         }
1707
1708         if (compile_preprocessed_source_code) {
1709                 *compiler_args = args_copy(stripped_args);
1710                 if (explicit_language) {
1711                         /*
1712                          * Workaround for a bug in Apple's patched distcc -- it doesn't properly
1713                          * reset the language specified with -x, so if -x is given, we have to
1714                          * specify the preprocessed language explicitly.
1715                          */
1716                         args_add(*compiler_args, "-x");
1717                         args_add(*compiler_args, p_language_for_language(explicit_language));
1718                 }
1719         } else {
1720                 *compiler_args = args_copy(*preprocessor_args);
1721         }
1722
1723         /*
1724          * Only pass dependency arguments to the preprocesor since Intel's C++
1725          * compiler doesn't produce a correct .d file when compiling preprocessed
1726          * source.
1727          */
1728         args_extend(*preprocessor_args, dep_args);
1729
1730 out:
1731         args_free(stripped_args);
1732         args_free(dep_args);
1733         return result;
1734 }
1735
1736 /* Reset the global state. Used by the test suite. */
1737 void
1738 cc_reset(void)
1739 {
1740         free(current_working_dir); current_working_dir = NULL;
1741         free(cache_dir); cache_dir = NULL;
1742         cache_logfile = NULL;
1743         base_dir = NULL;
1744         args_free(orig_args); orig_args = NULL;
1745         free(input_file); input_file = NULL;
1746         output_obj = NULL;
1747         free(output_dep); output_dep = NULL;
1748         free(cached_obj_hash); cached_obj_hash = NULL;
1749         free(cached_obj); cached_obj = NULL;
1750         free(cached_stderr); cached_stderr = NULL;
1751         free(cached_dep); cached_dep = NULL;
1752         free(manifest_path); manifest_path = NULL;
1753         time_of_compilation = 0;
1754         sloppiness = false;
1755         if (included_files) {
1756                 hashtable_destroy(included_files, 1); included_files = NULL;
1757         }
1758         generating_dependencies = false;
1759         i_extension = NULL;
1760         i_tmpfile = NULL;
1761         direct_i_file = false;
1762         free(cpp_stderr); cpp_stderr = NULL;
1763         free(stats_file); stats_file = NULL;
1764         enable_unify = false;
1765         enable_direct = true;
1766         enable_compression = false;
1767         nlevels = 2;
1768         compile_preprocessed_source_code = false;
1769         output_is_precompiled_header = false;
1770 }
1771
1772 static unsigned
1773 parse_sloppiness(char *p)
1774 {
1775         unsigned result = 0;
1776         char *word, *q, *saveptr = NULL;
1777
1778         if (!p) {
1779                 return result;
1780         }
1781         p = x_strdup(p);
1782         q = p;
1783         while ((word = strtok_r(q, ", ", &saveptr))) {
1784                 if (str_eq(word, "file_macro")) {
1785                         cc_log("Being sloppy about __FILE__");
1786                         result |= SLOPPY_FILE_MACRO;
1787                 }
1788                 if (str_eq(word, "include_file_mtime")) {
1789                         cc_log("Being sloppy about include file mtime");
1790                         result |= SLOPPY_INCLUDE_FILE_MTIME;
1791                 }
1792                 if (str_eq(word, "time_macros")) {
1793                         cc_log("Being sloppy about __DATE__ and __TIME__");
1794                         result |= SLOPPY_TIME_MACROS;
1795                 }
1796                 q = NULL;
1797         }
1798         free(p);
1799         return result;
1800 }
1801
1802 /* the main ccache driver function */
1803 static void
1804 ccache(int argc, char *argv[])
1805 {
1806         bool put_object_in_manifest = false;
1807         struct file_hash *object_hash;
1808         struct file_hash *object_hash_from_manifest = NULL;
1809         char *env;
1810         struct mdfour common_hash;
1811         struct mdfour direct_hash;
1812         struct mdfour cpp_hash;
1813
1814         /* Arguments (except -E) to send to the preprocessor. */
1815         struct args *preprocessor_args;
1816
1817         /* Arguments to send to the real compiler. */
1818         struct args *compiler_args;
1819
1820         find_compiler(argc, argv);
1821
1822         if (getenv("CCACHE_DISABLE")) {
1823                 cc_log("ccache is disabled");
1824                 failed();
1825         }
1826
1827         sloppiness = parse_sloppiness(getenv("CCACHE_SLOPPINESS"));
1828
1829         cc_log_argv("Command line: ", argv);
1830         cc_log("Hostname: %s", get_hostname());
1831         cc_log("Working directory: %s", current_working_dir);
1832
1833         if (base_dir) {
1834                 cc_log("Base directory: %s", base_dir);
1835         }
1836
1837         if (getenv("CCACHE_UNIFY")) {
1838                 cc_log("Unify mode disabled");
1839                 enable_unify = true;
1840         }
1841
1842         if (getenv("CCACHE_NODIRECT") || enable_unify) {
1843                 cc_log("Direct mode disabled");
1844                 enable_direct = false;
1845         }
1846
1847         if (getenv("CCACHE_COMPRESS")) {
1848                 cc_log("Compression enabled");
1849                 enable_compression = true;
1850         }
1851
1852         if ((env = getenv("CCACHE_NLEVELS"))) {
1853                 nlevels = atoi(env);
1854                 if (nlevels < 1) nlevels = 1;
1855                 if (nlevels > 8) nlevels = 8;
1856         }
1857
1858         if (!cc_process_args(orig_args, &preprocessor_args, &compiler_args)) {
1859                 failed();
1860         }
1861
1862         cc_log("Source file: %s", input_file);
1863         if (generating_dependencies) {
1864                 cc_log("Dependency file: %s", output_dep);
1865         }
1866         cc_log("Object file: %s", output_obj);
1867
1868         hash_start(&common_hash);
1869         calculate_common_hash(preprocessor_args, &common_hash);
1870
1871         /* try to find the hash using the manifest */
1872         direct_hash = common_hash;
1873         if (enable_direct) {
1874                 cc_log("Trying direct lookup");
1875                 object_hash = calculate_object_hash(preprocessor_args, &direct_hash, 1);
1876                 if (object_hash) {
1877                         update_cached_result_globals(object_hash);
1878
1879                         /*
1880                          * If we can return from cache at this point then do
1881                          * so.
1882                          */
1883                         from_cache(FROMCACHE_DIRECT_MODE, 0);
1884
1885                         /*
1886                          * Wasn't able to return from cache at this point.
1887                          * However, the object was already found in manifest,
1888                          * so don't readd it later.
1889                          */
1890                         put_object_in_manifest = false;
1891
1892                         object_hash_from_manifest = object_hash;
1893                 } else {
1894                         /* Add object to manifest later. */
1895                         put_object_in_manifest = true;
1896                 }
1897         }
1898
1899         /*
1900          * Find the hash using the preprocessed output. Also updates
1901          * included_files.
1902          */
1903         cpp_hash = common_hash;
1904         cc_log("Running preprocessor");
1905         object_hash = calculate_object_hash(preprocessor_args, &cpp_hash, 0);
1906         if (!object_hash) {
1907                 fatal("internal error: object hash from cpp returned NULL");
1908         }
1909         update_cached_result_globals(object_hash);
1910
1911         if (object_hash_from_manifest
1912             && !file_hashes_equal(object_hash_from_manifest, object_hash)) {
1913                 /*
1914                  * The hash from manifest differs from the hash of the
1915                  * preprocessor output. This could be because:
1916                  *
1917                  * - The preprocessor produces different output for the same
1918                  *   input (not likely).
1919                  * - There's a bug in ccache (maybe incorrect handling of
1920                  *   compiler arguments).
1921                  * - The user has used a different CCACHE_BASEDIR (most
1922                  *   likely).
1923                  *
1924                  * The best thing here would probably be to remove the hash
1925                  * entry from the manifest. For now, we use a simpler method:
1926                  * just remove the manifest file.
1927                  */
1928                 cc_log("Hash from manifest doesn't match preprocessor output");
1929                 cc_log("Likely reason: different CCACHE_BASEDIRs used");
1930                 cc_log("Removing manifest as a safety measure");
1931                 x_unlink(manifest_path);
1932
1933                 put_object_in_manifest = true;
1934         }
1935
1936         /* if we can return from cache at this point then do */
1937         from_cache(FROMCACHE_CPP_MODE, put_object_in_manifest);
1938
1939         if (getenv("CCACHE_READONLY")) {
1940                 cc_log("Read-only mode; running real compiler");
1941                 failed();
1942         }
1943
1944         env = getenv("CCACHE_PREFIX");
1945         if (env) {
1946                 char *p = find_executable(env, MYNAME);
1947                 if (!p) {
1948                         fatal("%s: %s", env, strerror(errno));
1949                 }
1950                 cc_log("Using command-line prefix %s", env);
1951                 args_add_prefix(compiler_args, p);
1952         }
1953
1954         /* run real compiler, sending output to cache */
1955         to_cache(compiler_args);
1956
1957         /* return from cache */
1958         from_cache(FROMCACHE_COMPILED_MODE, put_object_in_manifest);
1959
1960         /* oh oh! */
1961         cc_log("Secondary from_cache failed");
1962         stats_update(STATS_ERROR);
1963         failed();
1964 }
1965
1966 static void
1967 check_cache_dir(void)
1968 {
1969         if (!cache_dir) {
1970                 fatal("Unable to determine cache directory");
1971         }
1972 }
1973
1974 /* the main program when not doing a compile */
1975 static int
1976 ccache_main_options(int argc, char *argv[])
1977 {
1978         int c;
1979         size_t v;
1980
1981         static const struct option options[] = {
1982                 {"show-stats", no_argument,       0, 's'},
1983                 {"zero-stats", no_argument,       0, 'z'},
1984                 {"cleanup",    no_argument,       0, 'c'},
1985                 {"clear",      no_argument,       0, 'C'},
1986                 {"max-files",  required_argument, 0, 'F'},
1987                 {"max-size",   required_argument, 0, 'M'},
1988                 {"help",       no_argument,       0, 'h'},
1989                 {"version",    no_argument,       0, 'V'},
1990                 {0, 0, 0, 0}
1991         };
1992
1993         while ((c = getopt_long(argc, argv, "hszcCF:M:V", options, NULL)) != -1) {
1994                 switch (c) {
1995                 case 'V':
1996                         fprintf(stdout, VERSION_TEXT, CCACHE_VERSION);
1997                         exit(0);
1998
1999                 case 'h':
2000                         fputs(USAGE_TEXT, stdout);
2001                         exit(0);
2002
2003                 case 's':
2004                         check_cache_dir();
2005                         stats_summary();
2006                         break;
2007
2008                 case 'c':
2009                         check_cache_dir();
2010                         cleanup_all(cache_dir);
2011                         printf("Cleaned cache\n");
2012                         break;
2013
2014                 case 'C':
2015                         check_cache_dir();
2016                         wipe_all(cache_dir);
2017                         printf("Cleared cache\n");
2018                         break;
2019
2020                 case 'z':
2021                         check_cache_dir();
2022                         stats_zero();
2023                         printf("Statistics cleared\n");
2024                         break;
2025
2026                 case 'F':
2027                         check_cache_dir();
2028                         v = atoi(optarg);
2029                         if (stats_set_limits(v, -1) == 0) {
2030                                 if (v == 0) {
2031                                         printf("Unset cache file limit\n");
2032                                 } else {
2033                                         printf("Set cache file limit to %u\n", (unsigned)v);
2034                                 }
2035                         } else {
2036                                 printf("Could not set cache file limit.\n");
2037                                 exit(1);
2038                         }
2039                         break;
2040
2041                 case 'M':
2042                         check_cache_dir();
2043                         v = value_units(optarg);
2044                         if (stats_set_limits(-1, v) == 0) {
2045                                 if (v == 0) {
2046                                         printf("Unset cache size limit\n");
2047                                 } else {
2048                                         char *s = format_size(v);
2049                                         printf("Set cache size limit to %s\n", s);
2050                                         free(s);
2051                                 }
2052                         } else {
2053                                 printf("Could not set cache size limit.\n");
2054                                 exit(1);
2055                         }
2056                         break;
2057
2058                 default:
2059                         fputs(USAGE_TEXT, stderr);
2060                         exit(1);
2061                 }
2062         }
2063
2064         return 0;
2065 }
2066
2067
2068 /* Make a copy of stderr that will not be cached, so things like
2069    distcc can send networking errors to it. */
2070 static void
2071 setup_uncached_err(void)
2072 {
2073         char *buf;
2074         int uncached_fd;
2075
2076         uncached_fd = dup(2);
2077         if (uncached_fd == -1) {
2078                 cc_log("dup(2) failed: %s", strerror(errno));
2079                 failed();
2080         }
2081
2082         /* leak a pointer to the environment */
2083         buf = format("UNCACHED_ERR_FD=%d", uncached_fd);
2084
2085         if (putenv(buf) == -1) {
2086                 cc_log("putenv failed: %s", strerror(errno));
2087                 failed();
2088         }
2089 }
2090
2091 int
2092 ccache_main(int argc, char *argv[])
2093 {
2094         char *p;
2095         char *program_name;
2096
2097         exitfn_init();
2098         exitfn_add_nullary(stats_flush);
2099         exitfn_add_nullary(clean_up_tmp_files);
2100
2101         /* check for logging early so cc_log messages start working ASAP */
2102         cache_logfile = getenv("CCACHE_LOGFILE");
2103         cc_log("=== CCACHE STARTED =========================================");
2104
2105         /* the user might have set CCACHE_UMASK */
2106         p = getenv("CCACHE_UMASK");
2107         if (p) {
2108                 mode_t mask;
2109                 errno = 0;
2110                 mask = strtol(p, NULL, 8);
2111                 if (errno == 0) {
2112                         umask(mask);
2113                 }
2114         }
2115
2116         current_working_dir = get_cwd();
2117         if (!current_working_dir) {
2118                 cc_log("Could not determine current working directory");
2119                 failed();
2120         }
2121         cache_dir = getenv("CCACHE_DIR");
2122         if (cache_dir) {
2123                 cache_dir = x_strdup(cache_dir);
2124         } else {
2125                 const char *home_directory = get_home_directory();
2126                 if (home_directory) {
2127                         cache_dir = format("%s/.ccache", home_directory);
2128                 }
2129         }
2130
2131         /* check if we are being invoked as "ccache" */
2132         program_name = basename(argv[0]);
2133         if (same_executable_name(program_name, MYNAME)) {
2134                 if (argc < 2) {
2135                         fputs(USAGE_TEXT, stderr);
2136                         exit(1);
2137                 }
2138                 /* if the first argument isn't an option, then assume we are
2139                    being passed a compiler name and options */
2140                 if (argv[1][0] == '-') {
2141                         return ccache_main_options(argc, argv);
2142                 }
2143         }
2144         free(program_name);
2145
2146         check_cache_dir();
2147
2148         temp_dir = getenv("CCACHE_TEMPDIR");
2149         if (!temp_dir) {
2150                 temp_dir = format("%s/tmp", cache_dir);
2151         }
2152
2153         base_dir = getenv("CCACHE_BASEDIR");
2154         if (base_dir && base_dir[0] != '/') {
2155                 cc_log("Ignoring non-absolute base directory %s", base_dir);
2156                 base_dir = NULL;
2157         }
2158
2159         compile_preprocessed_source_code = !getenv("CCACHE_CPP2");
2160
2161         setup_uncached_err();
2162
2163         /* make sure the cache dir exists */
2164         if (create_dir(cache_dir) != 0) {
2165                 fprintf(stderr,
2166                         "ccache: failed to create %s (%s)\n",
2167                         cache_dir, strerror(errno));
2168                 exit(1);
2169         }
2170
2171         /* make sure the temp dir exists */
2172         if (create_dir(temp_dir) != 0) {
2173                 fprintf(stderr,
2174                         "ccache: failed to create %s (%s)\n",
2175                         temp_dir, strerror(errno));
2176                 exit(1);
2177         }
2178
2179         if (!getenv("CCACHE_READONLY")) {
2180                 if (create_cachedirtag(cache_dir) != 0) {
2181                         fprintf(stderr,
2182                                 "ccache: failed to create %s/CACHEDIR.TAG (%s)\n",
2183                                 cache_dir, strerror(errno));
2184                         exit(1);
2185                 }
2186         }
2187
2188         ccache(argc, argv);
2189         return 1;
2190 }