c69745f5dec54e0dfc4d030f9c2c4296dc8264ef
[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-2015 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 #define STRINGIFY(x) #x
36 #define TO_STRING(x) STRINGIFY(x)
37
38 static const char VERSION_TEXT[] =
39   MYNAME " version %s\n"
40   "\n"
41   "Copyright (C) 2002-2007 Andrew Tridgell\n"
42   "Copyright (C) 2009-2015 Joel Rosdahl\n"
43   "\n"
44   "This program is free software; you can redistribute it and/or modify it under\n"
45   "the terms of the GNU General Public License as published by the Free Software\n"
46   "Foundation; either version 3 of the License, or (at your option) any later\n"
47   "version.\n";
48
49 static const char USAGE_TEXT[] =
50   "Usage:\n"
51   "    " MYNAME " [options]\n"
52   "    " MYNAME " compiler [compiler options]\n"
53   "    compiler [compiler options]          (via symbolic link)\n"
54   "\n"
55   "Options:\n"
56   "    -c, --cleanup         delete old files and recalculate size counters\n"
57   "                          (normally not needed as this is done automatically)\n"
58   "    -C, --clear           clear the cache completely (except configuration)\n"
59   "    -F, --max-files=N     set maximum number of files in cache to N (use 0 for\n"
60   "                          no limit)\n"
61   "    -M, --max-size=SIZE   set maximum size of cache to SIZE (use 0 for no\n"
62   "                          limit); available suffixes: k, M, G, T (decimal) and\n"
63   "                          Ki, Mi, Gi, Ti (binary); default suffix: G\n"
64   "    -o, --set-config=K=V  set configuration key K to value V\n"
65   "    -p, --print-config    print current configuration options\n"
66   "    -s, --show-stats      show statistics summary\n"
67   "    -z, --zero-stats      zero statistics counters\n"
68   "\n"
69   "    -h, --help            print this help text\n"
70   "    -V, --version         print version and copyright information\n"
71   "\n"
72   "See also <http://ccache.samba.org>.\n";
73
74 /* Global configuration data. */
75 struct conf *conf = NULL;
76
77 /* Where to write configuration changes. */
78 char *primary_config_path = NULL;
79
80 /* Secondary, read-only configuration file (if any). */
81 char *secondary_config_path = NULL;
82
83 /* current working directory taken from $PWD, or getcwd() if $PWD is bad */
84 char *current_working_dir = NULL;
85
86 /* the original argument list */
87 static struct args *orig_args;
88
89 /* the source file */
90 static char *input_file;
91
92 /* The output file being compiled to. */
93 static char *output_obj;
94
95 /* The path to the dependency file (implicit or specified with -MF). */
96 static char *output_dep;
97
98 /* The path to the coverage file (implicit when using -ftest-coverage). */
99 static char *output_cov;
100
101 /* Diagnostic generation information (clang). Contains pathname if not
102  * NULL. */
103 static char *output_dia = NULL;
104
105 /* -gsplit-dwarf support: Split dwarf information (GCC 4.8 and
106  *  up). Contains pathname if not NULL. */
107 static char *output_dwo = NULL;
108
109 /*
110  * Name (represented as a struct file_hash) of the file containing the cached
111  * object code.
112  */
113 static struct file_hash *cached_obj_hash;
114
115 /*
116  * Full path to the file containing the cached object code
117  * (cachedir/a/b/cdef[...]-size.o).
118  */
119 static char *cached_obj;
120
121 /*
122  * Full path to the file containing the standard error output
123  * (cachedir/a/b/cdef[...]-size.stderr).
124  */
125 static char *cached_stderr;
126
127 /*
128  * Full path to the file containing the dependency information
129  * (cachedir/a/b/cdef[...]-size.d).
130  */
131 static char *cached_dep;
132
133 /*
134  * Full path to the file containing the coverage information
135  * (cachedir/a/b/cdef[...]-size.gcno).
136  */
137 static char *cached_cov;
138
139 /*
140  * Full path to the file containing the diagnostic information (for clang)
141  * (cachedir/a/b/cdef[...]-size.dia).
142  */
143 static char *cached_dia;
144
145 /*
146  * -gsplit-dwarf support:
147  * Full path to the file containing the split dwarf (for GCC 4.8 and
148  * above)
149  * (cachedir/a/b/cdef[...]-size.dwo).
150  *
151  * contains NULL if -gsplit-dwarf is not given.
152  */
153 static char *cached_dwo;
154
155 /*
156  * -gsplit-dwarf support:
157  * using_split_dwarf is true if "-gsplit-dwarf" is given to the
158  * compiler (GCC 4.8 and up).
159  */
160 bool using_split_dwarf = false;
161
162 /*
163  * Full path to the file containing the manifest
164  * (cachedir/a/b/cdef[...]-size.manifest).
165  */
166 static char *manifest_path;
167
168 /*
169  * Time of compilation. Used to see if include files have changed after
170  * compilation.
171  */
172 time_t time_of_compilation;
173
174 /*
175  * Files included by the preprocessor and their hashes/sizes. Key: file path.
176  * Value: struct file_hash.
177  */
178 static struct hashtable *included_files;
179
180 /* is gcc being asked to output dependencies? */
181 static bool generating_dependencies;
182
183 /* is gcc being asked to output coverage? */
184 static bool generating_coverage;
185
186 /* is gcc being asked to output coverage data (.gcda) at runtime? */
187 static bool profile_arcs;
188
189 /* name of the custom profile directory (default: object dirname) */
190 static char *profile_dir;
191
192 /* the name of the temporary pre-processor file */
193 static char *i_tmpfile;
194
195 /* are we compiling a .i or .ii file directly? */
196 static bool direct_i_file;
197
198 /* the name of the cpp stderr file */
199 static char *cpp_stderr;
200
201 /*
202  * Full path to the statistics file in the subdirectory where the cached result
203  * belongs (<cache_dir>/<x>/stats).
204  */
205 char *stats_file = NULL;
206
207 /* Whether the output is a precompiled header */
208 static bool output_is_precompiled_header = false;
209
210 /* Profile generation / usage information */
211 static char *profile_dir = NULL;
212 static bool profile_use = false;
213 static bool profile_generate = false;
214
215 /*
216  * Whether we are using a precompiled header (either via -include, #include or
217  * clang's -include-pch or -include-pth).
218  */
219 static bool using_precompiled_header = false;
220
221 /*
222  * The .gch/.pch/.pth file used for compilation.
223  */
224 static char *included_pch_file = NULL;
225
226 /* How long (in microseconds) to wait before breaking a stale lock. */
227 unsigned lock_staleness_limit = 2000000;
228
229 enum fromcache_call_mode {
230         FROMCACHE_DIRECT_MODE,
231         FROMCACHE_CPP_MODE
232 };
233
234 struct pending_tmp_file {
235         char *path;
236         struct pending_tmp_file *next;
237 };
238
239 /* Temporary files to remove at program exit. */
240 static struct pending_tmp_file *pending_tmp_files = NULL;
241
242 /*
243  * This is a string that identifies the current "version" of the hash sum
244  * computed by ccache. If, for any reason, we want to force the hash sum to be
245  * different for the same input in a new ccache version, we can just change
246  * this string. A typical example would be if the format of one of the files
247  * stored in the cache changes in a backwards-incompatible way.
248  */
249 static const char HASH_PREFIX[] = "3";
250
251 static void
252 add_prefix(struct args *args)
253 {
254         char *e;
255         char *tok, *saveptr = NULL;
256         struct args *prefix;
257         int i;
258
259         if (str_eq(conf->prefix_command, "")) {
260                 return;
261         }
262
263         prefix = args_init(0, NULL);
264         e = x_strdup(conf->prefix_command);
265         for (tok = strtok_r(e, " ", &saveptr);
266              tok;
267              tok = strtok_r(NULL, " ", &saveptr)) {
268                 char *p;
269
270                 p = find_executable(tok, MYNAME);
271                 if (!p) {
272                         fatal("%s: %s", tok, strerror(errno));
273                 }
274
275                 args_add(prefix, p);
276                 free(p);
277         }
278         free(e);
279
280         cc_log("Using command-line prefix %s", conf->prefix_command);
281         for (i = prefix->argc; i != 0; i--) {
282                 args_add_prefix(args, prefix->argv[i-1]);
283         }
284         args_free(prefix);
285 }
286
287 /* Something went badly wrong - just execute the real compiler. */
288 static void
289 failed(void)
290 {
291         assert(orig_args);
292
293         args_strip(orig_args, "--ccache-");
294         add_prefix(orig_args);
295
296         cc_log("Failed; falling back to running the real compiler");
297         cc_log_argv("Executing ", orig_args->argv);
298         exitfn_call();
299         execv(orig_args->argv[0], orig_args->argv);
300         fatal("execv of %s failed: %s", orig_args->argv[0], strerror(errno));
301 }
302
303 static const char *
304 temp_dir()
305 {
306         static char *path = NULL;
307         if (path) {
308                 return path; /* Memoize */
309         }
310         path = conf->temporary_dir;
311         if (str_eq(path, "")) {
312                 path = format("%s/tmp", conf->cache_dir);
313         }
314         return path;
315 }
316
317 static void
318 add_pending_tmp_file(const char *path)
319 {
320         struct pending_tmp_file *e = x_malloc(sizeof(*e));
321         e->path = x_strdup(path);
322         e->next = pending_tmp_files;
323         pending_tmp_files = e;
324 }
325
326 static void
327 clean_up_pending_tmp_files(void)
328 {
329         struct pending_tmp_file *p = pending_tmp_files;
330         while (p) {
331                 /* Can't call tmp_unlink here since its cc_log calls aren't signal safe. */
332                 unlink(p->path);
333                 p = p->next;
334                 /* Leak p->path and p here because clean_up_pending_tmp_files needs to be
335                  * signal safe. */
336         }
337 }
338
339 static void
340 signal_handler(int signo)
341 {
342         (void)signo;
343         clean_up_pending_tmp_files();
344         _exit(1);
345 }
346
347 static void
348 clean_up_internal_tempdir(void)
349 {
350         DIR *dir;
351         struct dirent *entry;
352         struct stat st;
353         time_t now = time(NULL);
354
355         if (x_stat(conf->cache_dir, &st) != 0 || st.st_mtime + 3600 >= now) {
356                 /* No cleanup needed. */
357                 return;
358         }
359
360         update_mtime(conf->cache_dir);
361
362         dir = opendir(temp_dir());
363         if (!dir) {
364                 return;
365         }
366
367         while ((entry = readdir(dir))) {
368                 char *path;
369
370                 if (str_eq(entry->d_name, ".") || str_eq(entry->d_name, "..")) {
371                         continue;
372                 }
373
374                 path = format("%s/%s", temp_dir(), entry->d_name);
375                 if (x_lstat(path, &st) == 0 && st.st_mtime + 3600 < now) {
376                         tmp_unlink(path);
377                 }
378                 free(path);
379         }
380
381         closedir(dir);
382 }
383
384 static char *
385 get_current_working_dir(void)
386 {
387         if (!current_working_dir) {
388                 char *cwd = get_cwd();
389                 if (cwd) {
390                         current_working_dir = x_realpath(cwd);
391                         free(cwd);
392                 }
393                 if (!current_working_dir) {
394                         cc_log("Unable to determine current working directory: %s",
395                                strerror(errno));
396                         failed();
397                 }
398         }
399         return current_working_dir;
400 }
401
402 /*
403  * Transform a name to a full path into the cache directory, creating needed
404  * sublevels if needed. Caller frees.
405  */
406 static char *
407 get_path_in_cache(const char *name, const char *suffix)
408 {
409         unsigned i;
410         char *path;
411         char *result;
412
413         path = x_strdup(conf->cache_dir);
414         for (i = 0; i < conf->cache_dir_levels; ++i) {
415                 char *p = format("%s/%c", path, name[i]);
416                 free(path);
417                 path = p;
418         }
419
420         result = format("%s/%s%s", path, name + conf->cache_dir_levels, suffix);
421         free(path);
422         return result;
423 }
424
425 /*
426  * This function hashes an include file and stores the path and hash in the
427  * global included_files variable. If the include file is a PCH, cpp_hash is
428  * also updated. Takes over ownership of path.
429  */
430 static void
431 remember_include_file(char *path, struct mdfour *cpp_hash)
432 {
433 #ifdef _WIN32
434         DWORD attributes;
435 #endif
436         struct mdfour fhash;
437         struct stat st;
438         char *source = NULL;
439         size_t size;
440         bool is_pch;
441         size_t path_len = strlen(path);
442
443         if (path_len >= 2 && (path[0] == '<' && path[path_len - 1] == '>')) {
444                 /* Typically <built-in> or <command-line>. */
445                 goto ignore;
446         }
447
448         if (str_eq(path, input_file)) {
449                 /* Don't remember the input file. */
450                 goto ignore;
451         }
452
453         if (hashtable_search(included_files, path)) {
454                 /* Already known include file. */
455                 goto ignore;
456         }
457
458 #ifdef _WIN32
459         /* stat fails on directories on win32 */
460         attributes = GetFileAttributes(path);
461         if (attributes != INVALID_FILE_ATTRIBUTES &&
462             attributes & FILE_ATTRIBUTE_DIRECTORY)
463                 goto ignore;
464 #endif
465
466         if (x_stat(path, &st) != 0) {
467                 goto failure;
468         }
469         if (S_ISDIR(st.st_mode)) {
470                 /* Ignore directory, typically $PWD. */
471                 goto ignore;
472         }
473         if (!S_ISREG(st.st_mode)) {
474                 /* Device, pipe, socket or other strange creature. */
475                 cc_log("Non-regular include file %s", path);
476                 goto failure;
477         }
478
479         /* Let's hash the include file. */
480         if (!(conf->sloppiness & SLOPPY_INCLUDE_FILE_MTIME)
481             && st.st_mtime >= time_of_compilation) {
482                 cc_log("Include file %s too new", path);
483                 goto failure;
484         }
485
486         if (!(conf->sloppiness & SLOPPY_INCLUDE_FILE_CTIME)
487             && st.st_ctime >= time_of_compilation) {
488                 cc_log("Include file %s ctime too new", path);
489                 goto failure;
490         }
491
492         hash_start(&fhash);
493
494         is_pch = is_precompiled_header(path);
495         if (is_pch) {
496                 struct file_hash pch_hash;
497                 if (!hash_file(&fhash, path)) {
498                         goto failure;
499                 }
500                 hash_result_as_bytes(&fhash, pch_hash.hash);
501                 pch_hash.size = fhash.totalN;
502                 hash_delimiter(cpp_hash, "pch_hash");
503                 hash_buffer(cpp_hash, pch_hash.hash, sizeof(pch_hash.hash));
504         }
505         if (conf->direct_mode) {
506                 struct file_hash *h;
507
508                 if (!is_pch) { /* else: the file has already been hashed. */
509                         int result;
510
511                         if (st.st_size > 0) {
512                                 if (!read_file(path, st.st_size, &source, &size)) {
513                                         goto failure;
514                                 }
515                         } else {
516                                 source = x_strdup("");
517                                 size = 0;
518                         }
519
520                         result = hash_source_code_string(conf, &fhash, source, size, path);
521                         if (result & HASH_SOURCE_CODE_ERROR
522                             || result & HASH_SOURCE_CODE_FOUND_TIME) {
523                                 goto failure;
524                         }
525                 }
526
527                 h = x_malloc(sizeof(*h));
528                 hash_result_as_bytes(&fhash, h->hash);
529                 h->size = fhash.totalN;
530                 hashtable_insert(included_files, path, h);
531         } else {
532                 free(path);
533         }
534
535         free(source);
536         return;
537
538 failure:
539         if (conf->direct_mode) {
540                 cc_log("Disabling direct mode");
541                 conf->direct_mode = false;
542         }
543         /* Fall through. */
544 ignore:
545         free(path);
546         free(source);
547 }
548
549 /*
550  * Make a relative path from current working directory to path if path is under
551  * the base directory. Takes over ownership of path. Caller frees.
552  */
553 static char *
554 make_relative_path(char *path)
555 {
556         char *relpath, *canon_path, *path_suffix = NULL;
557         struct stat st;
558
559         if (str_eq(conf->base_dir, "") || !str_startswith(path, conf->base_dir)) {
560                 return path;
561         }
562
563         /* x_realpath only works for existing paths, so if path doesn't exist, try
564          * dirname(path) and assemble the path afterwards. We only bother to try
565          * canonicalizing one of these two paths since a compiler path argument
566          * typically only makes sense if path or dirname(path) exists. */
567         if (stat(path, &st) != 0) {
568                 /* path doesn't exist. */
569                 char *dir, *p;
570                 dir = dirname(path);
571                 if (stat(dir, &st) != 0) {
572                         /* And neither does its parent directory, so no action to take. */
573                         free(dir);
574                         return path;
575                 }
576                 path_suffix = basename(path);
577                 p = path;
578                 path = dirname(path);
579                 free(p);
580         }
581
582         canon_path = x_realpath(path);
583         if (canon_path) {
584                 free(path);
585                 relpath = get_relative_path(get_current_working_dir(), canon_path);
586                 free(canon_path);
587                 if (path_suffix) {
588                         path = format("%s/%s", relpath, path_suffix);
589                         free(relpath);
590                         free(path_suffix);
591                         return path;
592                 } else {
593                         return relpath;
594                 }
595         } else {
596                 /* path doesn't exist, so leave it as it is. */
597                 free(path_suffix);
598                 return path;
599         }
600 }
601
602 /*
603  * This function reads and hashes a file. While doing this, it also does these
604  * things:
605  *
606  * - Makes include file paths for which the base directory is a prefix relative
607  *   when computing the hash sum.
608  * - Stores the paths and hashes of included files in the global variable
609  *   included_files.
610  */
611 static bool
612 process_preprocessed_file(struct mdfour *hash, const char *path)
613 {
614         char *data;
615         char *p, *q, *end;
616         size_t size;
617
618         if (!read_file(path, 0, &data, &size)) {
619                 return false;
620         }
621
622         included_files = create_hashtable(1000, hash_from_string, strings_equal);
623
624         /* Bytes between p and q are pending to be hashed. */
625         end = data + size;
626         p = data;
627         q = data;
628         /* There must be at least 7 characters (# 1 "x") left to potentially find an
629          * include file path. */
630         while (q < end - 7) {
631                 /*
632                  * Check if we look at a line containing the file name of an included file.
633                  * At least the following formats exist (where N is a positive integer):
634                  *
635                  * GCC:
636                  *
637                  *   # N "file"
638                  *   # N "file" N
639                  *   #pragma GCC pch_preprocess "file"
640                  *
641                  * HP's compiler:
642                  *
643                  *   #line N "file"
644                  *
645                  * AIX's compiler:
646                  *
647                  *   #line N "file"
648                  *   #line N
649                  *
650                  * Note that there may be other lines starting with '#' left after
651                  * preprocessing as well, for instance "#    pragma".
652                  */
653                 if (q[0] == '#'
654                     /* GCC: */
655                     && ((q[1] == ' ' && q[2] >= '0' && q[2] <= '9')
656                         /* GCC precompiled header: */
657                         || (q[1] == 'p'
658                             && str_startswith(&q[2], "ragma GCC pch_preprocess "))
659                         /* HP/AIX: */
660                         || (q[1] == 'l' && q[2] == 'i' && q[3] == 'n' && q[4] == 'e'
661                             && q[5] == ' '))
662                     && (q == data || q[-1] == '\n')) {
663                         char *path;
664
665                         while (q < end && *q != '"' && *q != '\n') {
666                                 q++;
667                         }
668                         if (q < end && *q == '\n') {
669                                 /* A newline before the quotation mark -> no match. */
670                                 continue;
671                         }
672                         q++;
673                         if (q >= end) {
674                                 cc_log("Failed to parse included file path");
675                                 free(data);
676                                 return false;
677                         }
678                         /* q points to the beginning of an include file path */
679                         hash_buffer(hash, p, q - p);
680                         p = q;
681                         while (q < end && *q != '"') {
682                                 q++;
683                         }
684                         /* p and q span the include file path */
685                         path = x_strndup(p, q - p);
686                         path = make_relative_path(path);
687                         hash_string(hash, path);
688                         remember_include_file(path, hash);
689                         p = q;
690                 } else {
691                         q++;
692                 }
693         }
694
695         hash_buffer(hash, p, (end - p));
696         free(data);
697
698         /* Explicitly check the .gch/.pch/.pth file, Clang does not include any
699          * mention of it in the preprocessed output. */
700         if (included_pch_file) {
701                 char *path = x_strdup(included_pch_file);
702                 path = make_relative_path(path);
703                 hash_string(hash, path);
704                 remember_include_file(path, hash);
705         }
706
707         return true;
708 }
709
710 /* Copy or link a file to the cache. */
711 static void
712 put_file_in_cache(const char *source, const char *dest)
713 {
714         int ret;
715         struct stat st;
716         bool do_link = conf->hard_link && !conf->compression;
717
718         if (do_link) {
719                 x_unlink(dest);
720                 ret = link(source, dest);
721         } else {
722                 ret = copy_file(source, dest, conf->compression);
723         }
724         if (ret != 0) {
725                 cc_log("Failed to %s %s to %s: %s",
726                        do_link ? "link" : "copy",
727                        source,
728                        dest,
729                        strerror(errno));
730                 stats_update(STATS_ERROR);
731                 failed();
732         }
733         cc_log("Stored in cache: %s -> %s", source, dest);
734         if (x_stat(dest, &st) != 0) {
735                 stats_update(STATS_ERROR);
736                 failed();
737         }
738         stats_update_size(file_size(&st), 1);
739 }
740
741 /* Copy or link a file from the cache. */
742 static void
743 get_file_from_cache(const char *source, const char *dest)
744 {
745         int ret;
746         bool do_link = conf->hard_link && !file_is_compressed(source);
747
748         if (do_link) {
749                 x_unlink(dest);
750                 ret = link(source, dest);
751         } else {
752                 ret = copy_file(source, dest, 0);
753         }
754
755         if (ret == -1) {
756                 if (errno == ENOENT) {
757                         /* Someone removed the file just before we began copying? */
758                         cc_log("Cache file %s just disappeared from cache", source);
759                         stats_update(STATS_MISSING);
760                 } else {
761                         cc_log("Failed to %s %s to %s: %s",
762                                do_link ? "link" : "copy",
763                                source,
764                                dest,
765                                strerror(errno));
766                         stats_update(STATS_ERROR);
767                 }
768
769                 /* If there was trouble getting a file from the cached result, wipe the
770                  * whole cached result for consistency. */
771                 x_unlink(cached_stderr);
772                 x_unlink(cached_obj);
773                 x_unlink(cached_dep);
774                 x_unlink(cached_dia);
775
776                 failed();
777         }
778
779         cc_log("Created from cache: %s -> %s", source, dest);
780 }
781
782 /* Send cached stderr, if any, to stderr. */
783 static void
784 send_cached_stderr(void)
785 {
786         int fd_stderr = open(cached_stderr, O_RDONLY | O_BINARY);
787         if (fd_stderr != -1) {
788                 copy_fd(fd_stderr, 2);
789                 close(fd_stderr);
790         }
791 }
792
793 /* Create or update the manifest file. */
794 void update_manifest_file(void)
795 {
796         struct stat st;
797         size_t old_size = 0; /* in bytes */
798
799         if (!conf->direct_mode
800             || !included_files
801             || conf->read_only
802             || conf->read_only_direct) {
803                 return;
804         }
805
806         if (stat(manifest_path, &st) == 0) {
807                 old_size = file_size(&st);
808         }
809         if (manifest_put(manifest_path, cached_obj_hash, included_files)) {
810                 cc_log("Added object file hash to %s", manifest_path);
811                 update_mtime(manifest_path);
812                 if (x_stat(manifest_path, &st) == 0) {
813                         stats_update_size(file_size(&st) - old_size, old_size == 0 ? 1 : 0);
814                 }
815         } else {
816                 cc_log("Failed to add object file hash to %s", manifest_path);
817         }
818 }
819
820 /* run the real compiler and put the result in cache */
821 static void
822 to_cache(struct args *args)
823 {
824         char *tmp_stdout, *tmp_stderr, *tmp_aux, *tmp_cov;
825         char *tmp_dwo = NULL;
826         struct stat st;
827         int status, tmp_stdout_fd, tmp_stderr_fd;
828         FILE *f;
829
830         tmp_stdout = format("%s.tmp.stdout", cached_obj);
831         tmp_stdout_fd = create_tmp_fd(&tmp_stdout);
832         tmp_stderr = format("%s.tmp.stderr", cached_obj);
833         tmp_stderr_fd = create_tmp_fd(&tmp_stderr);
834
835         if (generating_coverage) {
836                 /* gcc has some funny rule about max extension length */
837                 if (strlen(get_extension(output_obj)) < 6) {
838                         tmp_aux = remove_extension(output_obj);
839                 } else {
840                         tmp_aux = x_strdup(output_obj);
841                 }
842                 tmp_cov = format("%s.gcno", tmp_aux);
843                 free(tmp_aux);
844         } else {
845                 tmp_cov = NULL;
846         }
847
848         /* GCC (at least 4.8 and 4.9) forms the .dwo file name by removing everything
849          * after (and including) the last "." from the object file name and then
850          * appending ".dwo".
851          */
852         if (using_split_dwarf) {
853                 char *base_name = remove_extension(output_obj);
854                 tmp_dwo = format("%s.dwo", base_name);
855                 free(base_name);
856         }
857
858         args_add(args, "-o");
859         args_add(args, output_obj);
860
861         if (output_dia) {
862                 args_add(args, "--serialize-diagnostics");
863                 args_add(args, output_dia);
864         }
865
866         /* Turn off DEPENDENCIES_OUTPUT when running cc1, because
867          * otherwise it will emit a line like
868          *
869          *  tmp.stdout.vexed.732.o: /home/mbp/.ccache/tmp.stdout.vexed.732.i
870          */
871         x_unsetenv("DEPENDENCIES_OUTPUT");
872
873         if (conf->run_second_cpp) {
874                 args_add(args, input_file);
875         } else {
876                 args_add(args, i_tmpfile);
877         }
878
879         cc_log("Running real compiler");
880         status = execute(args->argv, tmp_stdout_fd, tmp_stderr_fd);
881         args_pop(args, 3);
882
883         if (x_stat(tmp_stdout, &st) != 0) {
884                 /* The stdout file was removed - cleanup in progress? Better bail out. */
885                 stats_update(STATS_MISSING);
886                 tmp_unlink(tmp_stdout);
887                 tmp_unlink(tmp_stderr);
888                 if (tmp_cov) {
889                         tmp_unlink(tmp_cov);
890                 }
891                 tmp_unlink(tmp_dwo);
892                 failed();
893         }
894         if (st.st_size != 0) {
895                 cc_log("Compiler produced stdout");
896                 stats_update(STATS_STDOUT);
897                 tmp_unlink(tmp_stdout);
898                 tmp_unlink(tmp_stderr);
899                 if (tmp_cov) {
900                         tmp_unlink(tmp_cov);
901                 }
902                 tmp_unlink(tmp_dwo);
903                 failed();
904         }
905         tmp_unlink(tmp_stdout);
906
907         /*
908          * Merge stderr from the preprocessor (if any) and stderr from the real
909          * compiler into tmp_stderr.
910          */
911         if (cpp_stderr) {
912                 int fd_cpp_stderr;
913                 int fd_real_stderr;
914                 int fd_result;
915                 char *tmp_stderr2;
916
917                 tmp_stderr2 = format("%s.2", tmp_stderr);
918                 if (x_rename(tmp_stderr, tmp_stderr2)) {
919                         cc_log("Failed to rename %s to %s: %s", tmp_stderr, tmp_stderr2,
920                                strerror(errno));
921                         failed();
922                 }
923                 fd_cpp_stderr = open(cpp_stderr, O_RDONLY | O_BINARY);
924                 if (fd_cpp_stderr == -1) {
925                         cc_log("Failed opening %s: %s", cpp_stderr, strerror(errno));
926                         failed();
927                 }
928                 fd_real_stderr = open(tmp_stderr2, O_RDONLY | O_BINARY);
929                 if (fd_real_stderr == -1) {
930                         cc_log("Failed opening %s: %s", tmp_stderr2, strerror(errno));
931                         failed();
932                 }
933                 fd_result = open(tmp_stderr, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
934                 if (fd_result == -1) {
935                         cc_log("Failed opening %s: %s", tmp_stderr, strerror(errno));
936                         failed();
937                 }
938                 copy_fd(fd_cpp_stderr, fd_result);
939                 copy_fd(fd_real_stderr, fd_result);
940                 close(fd_cpp_stderr);
941                 close(fd_real_stderr);
942                 close(fd_result);
943                 tmp_unlink(tmp_stderr2);
944                 free(tmp_stderr2);
945         }
946
947         if (status != 0) {
948                 int fd;
949                 cc_log("Compiler gave exit status %d", status);
950                 stats_update(STATS_STATUS);
951
952                 fd = open(tmp_stderr, O_RDONLY | O_BINARY);
953                 if (fd != -1) {
954                         /* We can output stderr immediately instead of rerunning the compiler. */
955                         copy_fd(fd, 2);
956                         close(fd);
957                         tmp_unlink(tmp_stderr);
958
959                         x_exit(status);
960                 }
961
962                 tmp_unlink(tmp_stderr);
963                 if (tmp_cov) {
964                         tmp_unlink(tmp_cov);
965                 }
966                 tmp_unlink(tmp_dwo);
967
968                 failed();
969         }
970
971         if (stat(output_obj, &st) != 0) {
972                 cc_log("Compiler didn't produce an object file");
973                 stats_update(STATS_NOOUTPUT);
974                 failed();
975         }
976         if (st.st_size == 0) {
977                 cc_log("Compiler produced an empty object file");
978                 stats_update(STATS_EMPTYOUTPUT);
979                 failed();
980         }
981
982         if (using_split_dwarf) {
983                 if (stat(tmp_dwo, &st) != 0) {
984                         cc_log("Compiler didn't produce a split dwarf file");
985                         stats_update(STATS_NOOUTPUT);
986                         failed();
987                 }
988                 if (st.st_size == 0) {
989                         cc_log("Compiler produced an empty split dwarf file");
990                         stats_update(STATS_EMPTYOUTPUT);
991                         failed();
992                 }
993         }
994
995         if (x_stat(tmp_stderr, &st) != 0) {
996                 stats_update(STATS_ERROR);
997                 failed();
998         }
999         if (st.st_size > 0) {
1000                 if (move_uncompressed_file(
1001                       tmp_stderr, cached_stderr,
1002                       conf->compression ? conf->compression_level : 0) != 0) {
1003                         cc_log("Failed to move %s to %s: %s", tmp_stderr, cached_stderr,
1004                                strerror(errno));
1005                         stats_update(STATS_ERROR);
1006                         failed();
1007                 }
1008                 cc_log("Stored in cache: %s", cached_stderr);
1009                 if (!conf->compression
1010                     /* If the file was compressed, obtain the size again: */
1011                     || (conf->compression && x_stat(cached_stderr, &st) == 0)) {
1012                         stats_update_size(file_size(&st), 1);
1013                 }
1014         } else {
1015                 tmp_unlink(tmp_stderr);
1016                 if (conf->recache) {
1017                         /* If recaching, we need to remove any previous .stderr. */
1018                         x_unlink(cached_stderr);
1019                 }
1020         }
1021
1022         if (generating_coverage) {
1023                 /* gcc won't generate notes if there is no code */
1024                 if (stat(tmp_cov, &st) != 0 && errno == ENOENT) {
1025                         cc_log("Creating placeholder: %s", cached_cov);
1026
1027                         f = fopen(cached_cov, "wb");
1028                         if (!f) {
1029                                 cc_log("Failed to create %s: %s", cached_cov, strerror(errno));
1030                                 stats_update(STATS_ERROR);
1031                                 failed();
1032                         }
1033                         fclose(f);
1034                         stats_update_size(0, 1);
1035                 } else {
1036                         put_file_in_cache(tmp_cov, cached_cov);
1037                 }
1038         }
1039
1040         if (output_dia) {
1041                 if (x_stat(output_dia, &st) != 0) {
1042                         stats_update(STATS_ERROR);
1043                         failed();
1044                 }
1045                 if (st.st_size > 0) {
1046                         put_file_in_cache(output_dia, cached_dia);
1047                 }
1048         }
1049
1050         put_file_in_cache(output_obj, cached_obj);
1051
1052         if (using_split_dwarf) {
1053                 assert(tmp_dwo);
1054                 assert(cached_dwo);
1055                 put_file_in_cache(tmp_dwo, cached_dwo);
1056         }
1057
1058         if (generating_dependencies) {
1059                 put_file_in_cache(output_dep, cached_dep);
1060         }
1061         stats_update(STATS_TOCACHE);
1062
1063         /* Make sure we have a CACHEDIR.TAG in the cache part of cache_dir. This can
1064          * be done almost anywhere, but we might as well do it near the end as we
1065          * save the stat call if we exit early.
1066          */
1067         {
1068                 char *first_level_dir = dirname(stats_file);
1069                 if (create_cachedirtag(first_level_dir) != 0) {
1070                         cc_log("Failed to create %s/CACHEDIR.TAG (%s)\n",
1071                                first_level_dir, strerror(errno));
1072                         stats_update(STATS_ERROR);
1073                         failed();
1074                 }
1075                 free(first_level_dir);
1076
1077                 /* Remove any CACHEDIR.TAG on the cache_dir level where it was located in
1078                  * previous ccache versions. */
1079                 if (getpid() % 1000 == 0) {
1080                         char *path = format("%s/CACHEDIR.TAG", conf->cache_dir);
1081                         x_unlink(path);
1082                         free(path);
1083                 }
1084         }
1085
1086         /* Everything OK. */
1087         send_cached_stderr();
1088         update_manifest_file();
1089
1090         free(tmp_stderr);
1091         free(tmp_stdout);
1092         free(tmp_cov);
1093         free(tmp_dwo);
1094 }
1095
1096 /*
1097  * Find the object file name by running the compiler in preprocessor mode.
1098  * Returns the hash as a heap-allocated hex string.
1099  */
1100 static struct file_hash *
1101 get_object_name_from_cpp(struct args *args, struct mdfour *hash)
1102 {
1103         char *input_base;
1104         char *tmp;
1105         char *path_stdout, *path_stderr;
1106         int status, path_stderr_fd;
1107         struct file_hash *result;
1108
1109         /* ~/hello.c -> tmp.hello.123.i
1110          * limit the basename to 10
1111          * characters in order to cope with filesystem with small
1112          * maximum filename length limits */
1113         input_base = basename(input_file);
1114         tmp = strchr(input_base, '.');
1115         if (tmp) {
1116                 *tmp = 0;
1117         }
1118         if (strlen(input_base) > 10) {
1119                 input_base[10] = 0;
1120         }
1121
1122         path_stderr = format("%s/tmp.cpp_stderr", temp_dir());
1123         path_stderr_fd = create_tmp_fd(&path_stderr);
1124         add_pending_tmp_file(path_stderr);
1125
1126         time_of_compilation = time(NULL);
1127
1128         if (direct_i_file) {
1129                 /* We are compiling a .i or .ii file - that means we can skip the cpp stage
1130                  * and directly form the correct i_tmpfile. */
1131                 path_stdout = input_file;
1132                 status = 0;
1133         } else {
1134                 /* Run cpp on the input file to obtain the .i. */
1135                 int path_stdout_fd;
1136                 path_stdout = format("%s/%s.stdout", temp_dir(), input_base);
1137                 path_stdout_fd = create_tmp_fd(&path_stdout);
1138                 add_pending_tmp_file(path_stdout);
1139
1140                 args_add(args, "-E");
1141                 args_add(args, input_file);
1142                 cc_log("Running preprocessor");
1143                 status = execute(args->argv, path_stdout_fd, path_stderr_fd);
1144                 args_pop(args, 2);
1145         }
1146
1147         if (status != 0) {
1148                 cc_log("Preprocessor gave exit status %d", status);
1149                 stats_update(STATS_PREPROCESSOR);
1150                 failed();
1151         }
1152
1153         if (conf->unify) {
1154                 /* When we are doing the unifying tricks we need to include the input file
1155                  * name in the hash to get the warnings right. */
1156                 hash_delimiter(hash, "unifyfilename");
1157                 hash_string(hash, input_file);
1158
1159                 hash_delimiter(hash, "unifycpp");
1160                 if (unify_hash(hash, path_stdout) != 0) {
1161                         stats_update(STATS_ERROR);
1162                         cc_log("Failed to unify %s", path_stdout);
1163                         failed();
1164                 }
1165         } else {
1166                 hash_delimiter(hash, "cpp");
1167                 if (!process_preprocessed_file(hash, path_stdout)) {
1168                         stats_update(STATS_ERROR);
1169                         failed();
1170                 }
1171         }
1172
1173         hash_delimiter(hash, "cppstderr");
1174         if (!hash_file(hash, path_stderr)) {
1175                 fatal("Failed to open %s: %s", path_stderr, strerror(errno));
1176         }
1177
1178         if (direct_i_file) {
1179                 i_tmpfile = input_file;
1180         } else {
1181                 /* i_tmpfile needs the proper cpp_extension for the compiler to do its
1182                  * thing correctly. */
1183                 i_tmpfile = format("%s.%s", path_stdout, conf->cpp_extension);
1184                 x_rename(path_stdout, i_tmpfile);
1185                 add_pending_tmp_file(i_tmpfile);
1186         }
1187
1188         if (conf->run_second_cpp) {
1189                 free(path_stderr);
1190         } else {
1191                 /*
1192                  * If we are using the CPP trick, we need to remember this
1193                  * stderr data and output it just before the main stderr from
1194                  * the compiler pass.
1195                  */
1196                 cpp_stderr = path_stderr;
1197                 hash_delimiter(hash, "runsecondcpp");
1198                 hash_string(hash, "false");
1199         }
1200
1201         result = x_malloc(sizeof(*result));
1202         hash_result_as_bytes(hash, result->hash);
1203         result->size = hash->totalN;
1204         return result;
1205 }
1206
1207 static void
1208 update_cached_result_globals(struct file_hash *hash)
1209 {
1210         char *object_name;
1211         object_name = format_hash_as_string(hash->hash, hash->size);
1212         cached_obj_hash = hash;
1213         cached_obj = get_path_in_cache(object_name, ".o");
1214         cached_stderr = get_path_in_cache(object_name, ".stderr");
1215         cached_dep = get_path_in_cache(object_name, ".d");
1216         cached_cov = get_path_in_cache(object_name, ".gcno");
1217         cached_dia = get_path_in_cache(object_name, ".dia");
1218
1219         if (using_split_dwarf) {
1220                 cached_dwo = get_path_in_cache(object_name, ".dwo");
1221         } else {
1222                 cached_dwo = NULL;
1223         }
1224
1225         stats_file = format("%s/%c/stats", conf->cache_dir, object_name[0]);
1226         free(object_name);
1227 }
1228
1229 /*
1230  * Hash mtime or content of a file, or the output of a command, according to
1231  * the CCACHE_COMPILERCHECK setting.
1232  */
1233 static void
1234 hash_compiler(struct mdfour *hash, struct stat *st, const char *path,
1235               bool allow_command)
1236 {
1237         if (str_eq(conf->compiler_check, "none")) {
1238                 /* Do nothing. */
1239         } else if (str_eq(conf->compiler_check, "mtime")) {
1240                 hash_delimiter(hash, "cc_mtime");
1241                 hash_int(hash, st->st_size);
1242                 hash_int(hash, st->st_mtime);
1243         } else if (str_startswith(conf->compiler_check, "string:")) {
1244                 hash_delimiter(hash, "cc_hash");
1245                 hash_string(hash, conf->compiler_check + strlen("string:"));
1246         } else if (str_eq(conf->compiler_check, "content") || !allow_command) {
1247                 hash_delimiter(hash, "cc_content");
1248                 hash_file(hash, path);
1249         } else { /* command string */
1250                 if (!hash_multicommand_output(
1251                       hash, conf->compiler_check, orig_args->argv[0])) {
1252                         fatal("Failure running compiler check command: %s", conf->compiler_check);
1253                 }
1254         }
1255 }
1256
1257 /*
1258  * Note that these compiler checks are unreliable, so nothing should
1259  * hard-depend on them.
1260  */
1261
1262 static bool
1263 compiler_is_clang(struct args *args)
1264 {
1265         char *name = basename(args->argv[0]);
1266         bool is = strstr(name, "clang");
1267         free(name);
1268         return is;
1269 }
1270
1271 static bool
1272 compiler_is_gcc(struct args *args)
1273 {
1274         char *name = basename(args->argv[0]);
1275         bool is = strstr(name, "gcc") || strstr(name, "g++");
1276         free(name);
1277         return is;
1278 }
1279
1280 /*
1281  * Update a hash sum with information common for the direct and preprocessor
1282  * modes.
1283  */
1284 static void
1285 calculate_common_hash(struct args *args, struct mdfour *hash)
1286 {
1287         struct stat st;
1288         char *p;
1289         const char *full_path = args->argv[0];
1290 #ifdef _WIN32
1291         const char *ext;
1292         char full_path_win_ext[MAX_PATH + 1] = {0};
1293 #endif
1294
1295         hash_string(hash, HASH_PREFIX);
1296
1297         /*
1298          * We have to hash the extension, as a .i file isn't treated the same
1299          * by the compiler as a .ii file.
1300          */
1301         hash_delimiter(hash, "ext");
1302         hash_string(hash, conf->cpp_extension);
1303
1304 #ifdef _WIN32
1305         ext = strrchr(args->argv[0], '.');
1306         add_exe_ext_if_no_to_fullpath(full_path_win_ext, MAX_PATH, ext,
1307                                       args->argv[0]);
1308         full_path = full_path_win_ext;
1309 #endif
1310
1311         if (x_stat(full_path, &st) != 0) {
1312                 stats_update(STATS_COMPILER);
1313                 failed();
1314         }
1315
1316         /*
1317          * Hash information about the compiler.
1318          */
1319         hash_compiler(hash, &st, args->argv[0], true);
1320
1321         /*
1322          * Also hash the compiler name as some compilers use hard links and
1323          * behave differently depending on the real name.
1324          */
1325         hash_delimiter(hash, "cc_name");
1326         p = basename(args->argv[0]);
1327         hash_string(hash, p);
1328         free(p);
1329
1330         /* Possibly hash the current working directory. */
1331         if (conf->hash_dir) {
1332                 char *cwd = gnu_getcwd();
1333                 if (cwd) {
1334                         hash_delimiter(hash, "cwd");
1335                         hash_string(hash, cwd);
1336                         free(cwd);
1337                 }
1338         }
1339
1340         /* Possibly hash the coverage data file path. */
1341         if (generating_coverage && profile_arcs) {
1342                 char *gcda_path;
1343                 char *dir = dirname(output_obj);
1344                 if (profile_dir) {
1345                         dir = x_strdup(profile_dir);
1346                 } else {
1347                         char *real_dir = x_realpath(dir);
1348                         free(dir);
1349                         dir = real_dir;
1350                 }
1351                 if (dir) {
1352                         char *base_name = basename(output_obj);
1353                         p = remove_extension(base_name);
1354                         free(base_name);
1355                         gcda_path = format("%s/%s.gcda", dir, p);
1356                         cc_log("Hashing coverage path %s", gcda_path);
1357                         free(p);
1358                         hash_delimiter(hash, "gcda");
1359                         hash_string(hash, gcda_path);
1360                         free(dir);
1361                 }
1362         }
1363
1364         if (!str_eq(conf->extra_files_to_hash, "")) {
1365                 char *path, *p, *q, *saveptr = NULL;
1366                 p = x_strdup(conf->extra_files_to_hash);
1367                 q = p;
1368                 while ((path = strtok_r(q, PATH_DELIM, &saveptr))) {
1369                         cc_log("Hashing extra file %s", path);
1370                         hash_delimiter(hash, "extrafile");
1371                         if (!hash_file(hash, path)) {
1372                                 stats_update(STATS_BADEXTRAFILE);
1373                                 failed();
1374                         }
1375                         q = NULL;
1376                 }
1377                 free(p);
1378         }
1379
1380         /* Possibly hash GCC_COLORS (for color diagnostics). */
1381         if (compiler_is_gcc(args)) {
1382                 const char *gcc_colors = getenv("GCC_COLORS");
1383                 if (gcc_colors) {
1384                         hash_delimiter(hash, "gcccolors");
1385                         hash_string(hash, gcc_colors);
1386                 }
1387         }
1388 }
1389
1390 /*
1391  * Update a hash sum with information specific to the direct and preprocessor
1392  * modes and calculate the object hash. Returns the object hash on success,
1393  * otherwise NULL. Caller frees.
1394  */
1395 static struct file_hash *
1396 calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode)
1397 {
1398         int i;
1399         char *manifest_name;
1400         struct stat st;
1401         int result;
1402         struct file_hash *object_hash = NULL;
1403         char *p;
1404
1405         if (direct_mode) {
1406                 hash_delimiter(hash, "manifest version");
1407                 hash_int(hash, MANIFEST_VERSION);
1408         }
1409
1410         /* first the arguments */
1411         for (i = 1; i < args->argc; i++) {
1412                 /* -L doesn't affect compilation. */
1413                 if (i < args->argc-1 && str_eq(args->argv[i], "-L")) {
1414                         i++;
1415                         continue;
1416                 }
1417                 if (str_startswith(args->argv[i], "-L")) {
1418                         continue;
1419                 }
1420
1421                 /* -Wl,... doesn't affect compilation. */
1422                 if (str_startswith(args->argv[i], "-Wl,")) {
1423                         continue;
1424                 }
1425
1426                 /* The -fdebug-prefix-map option may be used in combination with
1427                  * CCACHE_BASEDIR to reuse results across different directories. Skip it
1428                  * from hashing. */
1429                 if (str_startswith(args->argv[i], "-fdebug-prefix-map=")) {
1430                         continue;
1431                 }
1432
1433                 /* When using the preprocessor, some arguments don't contribute
1434                  * to the hash. The theory is that these arguments will change
1435                  * the output of -E if they are going to have any effect at
1436                  * all. For precompiled headers this might not be the case. */
1437                 if (!direct_mode && !output_is_precompiled_header
1438                     && !using_precompiled_header) {
1439                         if (compopt_affects_cpp(args->argv[i])) {
1440                                 i++;
1441                                 continue;
1442                         }
1443                         if (compopt_short(compopt_affects_cpp, args->argv[i])) {
1444                                 continue;
1445                         }
1446                 }
1447
1448                 /* If we're generating dependencies, we make sure to skip the
1449                  * filename of the dependency file, since it doesn't impact the
1450                  * output.
1451                  */
1452                 if (generating_dependencies) {
1453                         if (str_startswith(args->argv[i], "-Wp,")) {
1454                                 if (str_startswith(args->argv[i], "-Wp,-MD,")
1455                                     && !strchr(args->argv[i] + 8, ',')) {
1456                                         hash_string_length(hash, args->argv[i], 8);
1457                                         continue;
1458                                 } else if (str_startswith(args->argv[i], "-Wp,-MMD,")
1459                                            && !strchr(args->argv[i] + 9, ',')) {
1460                                         hash_string_length(hash, args->argv[i], 9);
1461                                         continue;
1462                                 }
1463                         } else if (str_startswith(args->argv[i], "-MF")) {
1464                                 bool separate_argument = (strlen(args->argv[i]) == 3);
1465
1466                                 /* In either case, hash the "-MF" part. */
1467                                 hash_string_length(hash, args->argv[i], 3);
1468
1469                                 if (separate_argument) {
1470                                         /* Next argument is dependency name, so
1471                                          * skip it. */
1472                                         i++;
1473                                 }
1474                                 continue;
1475                         }
1476                 }
1477
1478                 p = NULL;
1479                 if (str_startswith(args->argv[i], "-specs=")) {
1480                         p = args->argv[i] + 7;
1481                 } else if (str_startswith(args->argv[i], "--specs=")) {
1482                         p = args->argv[i] + 8;
1483                 }
1484                 if (p && x_stat(p, &st) == 0) {
1485                         /* If given an explicit specs file, then hash that file,
1486                          * but don't include the path to it in the hash. */
1487                         hash_delimiter(hash, "specs");
1488                         hash_compiler(hash, &st, p, false);
1489                         continue;
1490                 }
1491
1492                 if (str_startswith(args->argv[i], "-fplugin=")
1493                     && x_stat(args->argv[i] + 9, &st) == 0) {
1494                         hash_delimiter(hash, "plugin");
1495                         hash_compiler(hash, &st, args->argv[i] + 9, false);
1496                         continue;
1497                 }
1498
1499                 if (str_eq(args->argv[i], "-Xclang")
1500                     && i + 3 < args->argc
1501                     && str_eq(args->argv[i+1], "-load")
1502                     && str_eq(args->argv[i+2], "-Xclang")
1503                     && x_stat(args->argv[i+3], &st) == 0) {
1504                         hash_delimiter(hash, "plugin");
1505                         hash_compiler(hash, &st, args->argv[i+3], false);
1506                         continue;
1507                 }
1508
1509                 /* All other arguments are included in the hash. */
1510                 hash_delimiter(hash, "arg");
1511                 hash_string(hash, args->argv[i]);
1512         }
1513
1514         /*
1515          * For profile generation (-fprofile-arcs, -fprofile-generate):
1516          * - hash profile directory
1517          * - output to the real file first
1518          *
1519          * For profile usage (-fprofile-use):
1520          * - hash profile data
1521          *
1522          * -fbranch-probabilities and -fvpt usage is covered by
1523          * -fprofile-generate/-fprofile-use.
1524          *
1525          * The profile directory can be specified as an argument to
1526          * -fprofile-generate=, -fprofile-use=, or -fprofile-dir=.
1527          */
1528
1529         /*
1530          * We need to output to the real object first here, otherwise runtime
1531          * artifacts will be produced in the wrong place.
1532          */
1533         if (profile_generate) {
1534                 if (!profile_dir) {
1535                         profile_dir = get_cwd();
1536                 }
1537                 cc_log("Adding profile directory %s to our hash", profile_dir);
1538                 hash_delimiter(hash, "-fprofile-dir");
1539                 hash_string(hash, profile_dir);
1540         }
1541
1542         if (profile_use) {
1543                 /* Calculate gcda name */
1544                 char *gcda_name;
1545                 char *base_name;
1546                 base_name = remove_extension(output_obj);
1547                 if (!profile_dir) {
1548                         profile_dir = get_cwd();
1549                 }
1550                 gcda_name = format("%s/%s.gcda", profile_dir, base_name);
1551                 cc_log("Adding profile data %s to our hash", gcda_name);
1552                 /* Add the gcda to our hash */
1553                 hash_delimiter(hash, "-fprofile-use");
1554                 hash_file(hash, gcda_name);
1555                 free(base_name);
1556                 free(gcda_name);
1557         }
1558
1559         if (direct_mode) {
1560                 /* Hash environment variables that affect the preprocessor output. */
1561                 const char **p;
1562                 const char *envvars[] = {
1563                         "CPATH",
1564                         "C_INCLUDE_PATH",
1565                         "CPLUS_INCLUDE_PATH",
1566                         "OBJC_INCLUDE_PATH",
1567                         "OBJCPLUS_INCLUDE_PATH", /* clang */
1568                         NULL
1569                 };
1570                 for (p = envvars; *p; ++p) {
1571                         char *v = getenv(*p);
1572                         if (v) {
1573                                 hash_delimiter(hash, *p);
1574                                 hash_string(hash, v);
1575                         }
1576                 }
1577
1578                 if (!(conf->sloppiness & SLOPPY_FILE_MACRO)) {
1579                         /*
1580                          * The source code file or an include file may contain
1581                          * __FILE__, so make sure that the hash is unique for
1582                          * the file name.
1583                          */
1584                         hash_delimiter(hash, "inputfile");
1585                         hash_string(hash, input_file);
1586                 }
1587
1588                 hash_delimiter(hash, "sourcecode");
1589                 result = hash_source_code_file(conf, hash, input_file);
1590                 if (result & HASH_SOURCE_CODE_ERROR) {
1591                         failed();
1592                 }
1593                 if (result & HASH_SOURCE_CODE_FOUND_TIME) {
1594                         cc_log("Disabling direct mode");
1595                         conf->direct_mode = false;
1596                         return NULL;
1597                 }
1598                 manifest_name = hash_result(hash);
1599                 manifest_path = get_path_in_cache(manifest_name, ".manifest");
1600                 free(manifest_name);
1601                 cc_log("Looking for object file hash in %s", manifest_path);
1602                 object_hash = manifest_get(conf, manifest_path);
1603                 if (object_hash) {
1604                         cc_log("Got object file hash from manifest");
1605                 } else {
1606                         cc_log("Did not find object file hash in manifest");
1607                 }
1608         } else {
1609                 object_hash = get_object_name_from_cpp(args, hash);
1610                 cc_log("Got object file hash from preprocessor");
1611                 if (generating_dependencies) {
1612                         cc_log("Preprocessor created %s", output_dep);
1613                 }
1614         }
1615
1616         return object_hash;
1617 }
1618
1619 /*
1620  * Try to return the compile result from cache. If we can return from cache
1621  * then this function exits with the correct status code, otherwise it returns.
1622  */
1623 static void
1624 from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest)
1625 {
1626         struct stat st;
1627         bool produce_dep_file = false;
1628
1629         /* the user might be disabling cache hits */
1630         if (conf->recache) {
1631                 return;
1632         }
1633
1634         if (stat(cached_obj, &st) != 0) {
1635                 cc_log("Object file %s not in cache", cached_obj);
1636                 return;
1637         }
1638
1639         /* Check if the diagnostic file is there. */
1640         if (output_dia && stat(cached_dia, &st) != 0) {
1641                 cc_log("Diagnostic file %s not in cache", cached_dia);
1642                 return;
1643         }
1644
1645         /*
1646          * Occasionally, e.g. on hard reset, our cache ends up as just filesystem
1647          * meta-data with no content. Catch an easy case of this.
1648          */
1649         if (st.st_size == 0) {
1650                 cc_log("Invalid (empty) object file %s in cache", cached_obj);
1651                 x_unlink(cached_obj);
1652                 return;
1653         }
1654
1655         if (using_split_dwarf && !generating_dependencies) {
1656                 assert(output_dwo);
1657         }
1658         if (output_dwo) {
1659                 assert(cached_dwo);
1660                 if (stat(cached_dwo, &st) != 0) {
1661                         cc_log("Split dwarf file %s not in cache", cached_dwo);
1662                         return;
1663                 }
1664                 if (st.st_size == 0) {
1665                         cc_log("Invalid (empty) dwo file %s in cache", cached_dwo);
1666                         x_unlink(cached_dwo);
1667                         x_unlink(cached_obj); /* to really invalidate */
1668                         return;
1669                 }
1670         }
1671
1672         /*
1673          * (If mode != FROMCACHE_DIRECT_MODE, the dependency file is created by
1674          * gcc.)
1675          */
1676         produce_dep_file = generating_dependencies && mode == FROMCACHE_DIRECT_MODE;
1677
1678         /* If the dependency file should be in the cache, check that it is. */
1679         if (produce_dep_file && stat(cached_dep, &st) != 0) {
1680                 cc_log("Dependency file %s missing in cache", cached_dep);
1681                 return;
1682         }
1683
1684         /*
1685          * Copy object file from cache. Do so also for FissionDwarf file, cached_dwo,
1686          * when -gsplit-dwarf is specified.
1687          */
1688         if (!str_eq(output_obj, "/dev/null")) {
1689                 get_file_from_cache(cached_obj, output_obj);
1690                 if (using_split_dwarf) {
1691                         assert(output_dwo);
1692                         get_file_from_cache(cached_dwo, output_dwo);
1693                 }
1694         }
1695         if (produce_dep_file) {
1696                 get_file_from_cache(cached_dep, output_dep);
1697         }
1698         if (generating_coverage && stat(cached_cov, &st) == 0 && st.st_size > 0) {
1699                 /* gcc won't generate notes if there is no code */
1700                 get_file_from_cache(cached_cov, output_cov);
1701         }
1702         if (output_dia) {
1703                 get_file_from_cache(cached_dia, output_dia);
1704         }
1705
1706         /* Update modification timestamps to save files from LRU cleanup.
1707          * Also gives files a sensible mtime when hard-linking. */
1708         update_mtime(cached_obj);
1709         update_mtime(cached_stderr);
1710         if (produce_dep_file) {
1711                 update_mtime(cached_dep);
1712         }
1713         if (generating_coverage) {
1714                 update_mtime(cached_cov);
1715         }
1716         if (output_dia) {
1717                 update_mtime(cached_dia);
1718         }
1719         if (cached_dwo) {
1720                 update_mtime(cached_dwo);
1721         }
1722
1723         if (generating_dependencies && mode == FROMCACHE_CPP_MODE) {
1724                 put_file_in_cache(output_dep, cached_dep);
1725         }
1726
1727         send_cached_stderr();
1728
1729         if (put_object_in_manifest) {
1730                 update_manifest_file();
1731         }
1732
1733         /* log the cache hit */
1734         switch (mode) {
1735         case FROMCACHE_DIRECT_MODE:
1736                 cc_log("Succeeded getting cached result");
1737                 stats_update(STATS_CACHEHIT_DIR);
1738                 break;
1739
1740         case FROMCACHE_CPP_MODE:
1741                 cc_log("Succeeded getting cached result");
1742                 stats_update(STATS_CACHEHIT_CPP);
1743                 break;
1744         }
1745
1746         /* and exit with the right status code */
1747         x_exit(0);
1748 }
1749
1750 /* find the real compiler. We just search the PATH to find a executable of the
1751  * same name that isn't a link to ourselves */
1752 static void
1753 find_compiler(char **argv)
1754 {
1755         char *base;
1756         char *compiler;
1757
1758         base = basename(argv[0]);
1759
1760         /* we might be being invoked like "ccache gcc -c foo.c" */
1761         if (same_executable_name(base, MYNAME)) {
1762                 args_remove_first(orig_args);
1763                 free(base);
1764                 if (is_full_path(orig_args->argv[0])) {
1765                         /* a full path was given */
1766                         return;
1767                 }
1768                 base = basename(orig_args->argv[0]);
1769         }
1770
1771         /* support user override of the compiler */
1772         if (!str_eq(conf->compiler, "")) {
1773                 base = conf->compiler;
1774         }
1775
1776         compiler = find_executable(base, MYNAME);
1777
1778         /* can't find the compiler! */
1779         if (!compiler) {
1780                 stats_update(STATS_COMPILER);
1781                 fatal("Could not find compiler \"%s\" in PATH", base);
1782         }
1783         if (str_eq(compiler, argv[0])) {
1784                 fatal("Recursive invocation (the name of the ccache binary must be \"%s\")",
1785                       MYNAME);
1786         }
1787         orig_args->argv[0] = compiler;
1788 }
1789
1790 bool
1791 is_precompiled_header(const char *path)
1792 {
1793         return str_eq(get_extension(path), ".gch")
1794                || str_eq(get_extension(path), ".pch")
1795                || str_eq(get_extension(path), ".pth");
1796 }
1797
1798 static bool
1799 color_output_possible(void)
1800 {
1801         const char *term_env = getenv("TERM");
1802         return isatty(STDERR_FILENO) && term_env && strcasecmp(term_env, "DUMB") != 0;
1803 }
1804
1805 /*
1806  * Process the compiler options into options suitable for passing to the
1807  * preprocessor and the real compiler. The preprocessor options don't include
1808  * -E; this is added later. Returns true on success, otherwise false.
1809  */
1810 bool
1811 cc_process_args(struct args *args, struct args **preprocessor_args,
1812                 struct args **compiler_args)
1813 {
1814         int i;
1815         bool found_c_opt = false;
1816         bool found_S_opt = false;
1817         bool found_arch_opt = false;
1818         bool found_pch = false;
1819         bool found_fpch_preprocess = false;
1820         const char *explicit_language = NULL; /* As specified with -x. */
1821         const char *file_language;            /* As deduced from file extension. */
1822         const char *actual_language;          /* Language to actually use. */
1823         const char *input_charset = NULL;
1824         struct stat st;
1825         /* is the dependency makefile name overridden with -MF? */
1826         bool dependency_filename_specified = false;
1827         /* is the dependency makefile target name specified with -MT or -MQ? */
1828         bool dependency_target_specified = false;
1829         struct args *expanded_args, *stripped_args, *dep_args, *cpp_args;
1830         int argc;
1831         char **argv;
1832         bool result = true;
1833         bool found_color_diagnostics = false;
1834
1835         expanded_args = args_copy(args);
1836         stripped_args = args_init(0, NULL);
1837         dep_args = args_init(0, NULL);
1838         cpp_args = args_init(0, NULL);
1839
1840         argc = expanded_args->argc;
1841         argv = expanded_args->argv;
1842
1843         args_add(stripped_args, argv[0]);
1844
1845         for (i = 1; i < argc; i++) {
1846                 /* The user knows best: just swallow the next arg */
1847                 if (str_eq(argv[i], "--ccache-skip")) {
1848                         i++;
1849                         if (i == argc) {
1850                                 cc_log("--ccache-skip lacks an argument");
1851                                 result = false;
1852                                 goto out;
1853                         }
1854                         args_add(stripped_args, argv[i]);
1855                         continue;
1856                 }
1857
1858                 /* Special case for -E. */
1859                 if (str_eq(argv[i], "-E")) {
1860                         stats_update(STATS_PREPROCESSING);
1861                         result = false;
1862                         goto out;
1863                 }
1864
1865                 /* Handle "@file" argument. */
1866                 if (str_startswith(argv[i], "@") || str_startswith(argv[i], "-@")) {
1867                         char *argpath = argv[i] + 1;
1868                         struct args *file_args;
1869
1870                         if (argpath[-1] == '-') {
1871                                 ++argpath;
1872                         }
1873                         file_args = args_init_from_gcc_atfile(argpath);
1874                         if (!file_args) {
1875                                 cc_log("Couldn't read arg file %s", argpath);
1876                                 stats_update(STATS_ARGS);
1877                                 result = false;
1878                                 goto out;
1879                         }
1880
1881                         args_insert(expanded_args, i, file_args, true);
1882                         argc = expanded_args->argc;
1883                         argv = expanded_args->argv;
1884                         i--;
1885                         continue;
1886                 }
1887
1888                 /* These are always too hard. */
1889                 if (compopt_too_hard(argv[i])
1890                     || str_startswith(argv[i], "-fdump-")) {
1891                         cc_log("Compiler option %s is unsupported", argv[i]);
1892                         stats_update(STATS_UNSUPPORTED);
1893                         result = false;
1894                         goto out;
1895                 }
1896
1897                 /* These are too hard in direct mode. */
1898                 if (conf->direct_mode) {
1899                         if (compopt_too_hard_for_direct_mode(argv[i])) {
1900                                 cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
1901                                 conf->direct_mode = false;
1902                         }
1903                 }
1904
1905                 /* Multiple -arch options are too hard. */
1906                 if (str_eq(argv[i], "-arch")) {
1907                         if (found_arch_opt) {
1908                                 cc_log("More than one -arch compiler option is unsupported");
1909                                 stats_update(STATS_UNSUPPORTED);
1910                                 result = false;
1911                                 goto out;
1912                         } else {
1913                                 found_arch_opt = true;
1914                         }
1915                 }
1916
1917                 if (str_eq(argv[i], "-fpch-preprocess")
1918                     || str_eq(argv[i], "-emit-pch")
1919                     || str_eq(argv[i], "-emit-pth")) {
1920                         found_fpch_preprocess = true;
1921                 }
1922
1923                 /* we must have -c */
1924                 if (str_eq(argv[i], "-c")) {
1925                         found_c_opt = true;
1926                         continue;
1927                 }
1928
1929                 /* -S changes the default extension */
1930                 if (str_eq(argv[i], "-S")) {
1931                         args_add(stripped_args, argv[i]);
1932                         found_S_opt = true;
1933                         continue;
1934                 }
1935
1936                 /*
1937                  * Special handling for -x: remember the last specified language before the
1938                  * input file and strip all -x options from the arguments.
1939                  */
1940                 if (str_eq(argv[i], "-x")) {
1941                         if (i == argc-1) {
1942                                 cc_log("Missing argument to %s", argv[i]);
1943                                 stats_update(STATS_ARGS);
1944                                 result = false;
1945                                 goto out;
1946                         }
1947                         if (!input_file) {
1948                                 explicit_language = argv[i+1];
1949                         }
1950                         i++;
1951                         continue;
1952                 }
1953                 if (str_startswith(argv[i], "-x")) {
1954                         if (!input_file) {
1955                                 explicit_language = &argv[i][2];
1956                         }
1957                         continue;
1958                 }
1959
1960                 /* we need to work out where the output was meant to go */
1961                 if (str_eq(argv[i], "-o")) {
1962                         if (i == argc-1) {
1963                                 cc_log("Missing argument to %s", argv[i]);
1964                                 stats_update(STATS_ARGS);
1965                                 result = false;
1966                                 goto out;
1967                         }
1968                         output_obj = make_relative_path(x_strdup(argv[i+1]));
1969                         i++;
1970                         continue;
1971                 }
1972
1973                 /* alternate form of -o, with no space */
1974                 if (str_startswith(argv[i], "-o")) {
1975                         output_obj = make_relative_path(x_strdup(&argv[i][2]));
1976                         continue;
1977                 }
1978
1979                 if (str_eq(argv[i], "-gsplit-dwarf")) {
1980                         cc_log("Enabling caching of dwarf files since -gsplit-dwarf is used");
1981                         using_split_dwarf = true;
1982                         args_add(stripped_args, argv[i]);
1983                         continue;
1984                 }
1985
1986                 /* Debugging is handled specially, so that we know if we can strip line
1987                  * number info. */
1988                 if (str_startswith(argv[i], "-g")) {
1989                         args_add(stripped_args, argv[i]);
1990                         if (conf->unify && !str_eq(argv[i], "-g0")) {
1991                                 cc_log("%s used; disabling unify mode", argv[i]);
1992                                 conf->unify = false;
1993                         }
1994                         if (str_eq(argv[i], "-g3")) {
1995                                 /*
1996                                  * Fix for bug 7190 ("commandline macros (-D)
1997                                  * have non-zero lineno when using -g3").
1998                                  */
1999                                 cc_log("%s used; not compiling preprocessed code", argv[i]);
2000                                 conf->run_second_cpp = true;
2001                         }
2002                         continue;
2003                 }
2004
2005                 /* These options require special handling, because they
2006                  * behave differently with gcc -E, when the output
2007                  * file is not specified. */
2008                 if (str_eq(argv[i], "-MD") || str_eq(argv[i], "-MMD")) {
2009                         generating_dependencies = true;
2010                         args_add(dep_args, argv[i]);
2011                         continue;
2012                 }
2013                 if (str_startswith(argv[i], "-MF")) {
2014                         char *arg;
2015                         bool separate_argument = (strlen(argv[i]) == 3);
2016                         dependency_filename_specified = true;
2017                         free(output_dep);
2018                         if (separate_argument) {
2019                                 /* -MF arg */
2020                                 if (i >= argc - 1) {
2021                                         cc_log("Missing argument to %s", argv[i]);
2022                                         stats_update(STATS_ARGS);
2023                                         result = false;
2024                                         goto out;
2025                                 }
2026                                 arg = argv[i + 1];
2027                                 i++;
2028                         } else {
2029                                 /* -MFarg */
2030                                 arg = &argv[i][3];
2031                         }
2032                         output_dep = make_relative_path(x_strdup(arg));
2033                         /* Keep the format of the args the same */
2034                         if (separate_argument) {
2035                                 args_add(dep_args, "-MF");
2036                                 args_add(dep_args, output_dep);
2037                         } else {
2038                                 char *option = format("-MF%s", output_dep);
2039                                 args_add(dep_args, option);
2040                                 free(option);
2041                         }
2042                         continue;
2043                 }
2044                 if (str_startswith(argv[i], "-MQ") || str_startswith(argv[i], "-MT")) {
2045                         char *relpath;
2046                         dependency_target_specified = true;
2047                         if (strlen(argv[i]) == 3) {
2048                                 /* -MQ arg or -MT arg */
2049                                 if (i >= argc - 1) {
2050                                         cc_log("Missing argument to %s", argv[i]);
2051                                         stats_update(STATS_ARGS);
2052                                         result = false;
2053                                         goto out;
2054                                 }
2055                                 args_add(dep_args, argv[i]);
2056                                 relpath = make_relative_path(x_strdup(argv[i + 1]));
2057                                 args_add(dep_args, relpath);
2058                                 free(relpath);
2059                                 i++;
2060                         } else {
2061                                 char *arg_opt;
2062                                 char *option;
2063                                 arg_opt = x_strndup(argv[i], 3);
2064                                 relpath = make_relative_path(x_strdup(argv[i] + 3));
2065                                 option = format("%s%s", arg_opt, relpath);
2066                                 args_add(dep_args, option);
2067                                 free(arg_opt);
2068                                 free(relpath);
2069                                 free(option);
2070                         }
2071                         continue;
2072                 }
2073                 if (str_eq(argv[i], "-fprofile-arcs")) {
2074                         profile_arcs = true;
2075                         args_add(stripped_args, argv[i]);
2076                         continue;
2077                 }
2078                 if (str_eq(argv[i], "-ftest-coverage")) {
2079                         generating_coverage = true;
2080                         args_add(stripped_args, argv[i]);
2081                         continue;
2082                 }
2083                 if (str_eq(argv[i], "--coverage")) { /* = -fprofile-arcs -ftest-coverage */
2084                         profile_arcs = true;
2085                         generating_coverage = true;
2086                         args_add(stripped_args, argv[i]);
2087                         continue;
2088                 }
2089                 if (str_startswith(argv[i], "-fprofile-dir=")) {
2090                         profile_dir = x_strdup(argv[i] + 14);
2091                         args_add(stripped_args, argv[i]);
2092                         continue;
2093                 }
2094                 if (str_startswith(argv[i], "--sysroot=")) {
2095                         char *relpath = make_relative_path(x_strdup(argv[i] + 10));
2096                         char *option = format("--sysroot=%s", relpath);
2097                         args_add(stripped_args, option);
2098                         free(relpath);
2099                         free(option);
2100                         continue;
2101                 }
2102                 if (str_startswith(argv[i], "-Wp,")) {
2103                         if (str_eq(argv[i], "-Wp,-P") || strstr(argv[i], ",-P,")) {
2104                                 /* -P removes preprocessor information in such a way that the object
2105                                  * file from compiling the preprocessed file will not be equal to the
2106                                  * object file produced when compiling without ccache. */
2107                                 cc_log("Too hard option -Wp,-P detected");
2108                                 stats_update(STATS_UNSUPPORTED);
2109                                 failed();
2110                         } else if (str_startswith(argv[i], "-Wp,-MD,")
2111                                    && !strchr(argv[i] + 8, ',')) {
2112                                 generating_dependencies = true;
2113                                 dependency_filename_specified = true;
2114                                 free(output_dep);
2115                                 output_dep = make_relative_path(x_strdup(argv[i] + 8));
2116                                 args_add(dep_args, argv[i]);
2117                                 continue;
2118                         } else if (str_startswith(argv[i], "-Wp,-MMD,")
2119                                    && !strchr(argv[i] + 9, ',')) {
2120                                 generating_dependencies = true;
2121                                 dependency_filename_specified = true;
2122                                 free(output_dep);
2123                                 output_dep = make_relative_path(x_strdup(argv[i] + 9));
2124                                 args_add(dep_args, argv[i]);
2125                                 continue;
2126                         } else if (conf->direct_mode) {
2127                                 /*
2128                                  * -Wp, can be used to pass too hard options to
2129                                  * the preprocessor. Hence, disable direct
2130                                  * mode.
2131                                  */
2132                                 cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
2133                                 conf->direct_mode = false;
2134                         }
2135                 }
2136                 if (str_eq(argv[i], "-MP")) {
2137                         args_add(dep_args, argv[i]);
2138                         continue;
2139                 }
2140
2141                 /* Input charset needs to be handled specially. */
2142                 if (str_startswith(argv[i], "-finput-charset=")) {
2143                         input_charset = argv[i];
2144                         continue;
2145                 }
2146
2147                 if (str_eq(argv[i], "--serialize-diagnostics")) {
2148                         if (i >= argc - 1) {
2149                                 cc_log("Missing argument to %s", argv[i]);
2150                                 stats_update(STATS_ARGS);
2151                                 result = false;
2152                                 goto out;
2153                         }
2154                         output_dia = make_relative_path(x_strdup(argv[i+1]));
2155                         i++;
2156                         continue;
2157                 }
2158
2159                 if (str_startswith(argv[i], "-fprofile-")) {
2160                         const char *arg_profile_dir = strchr(argv[i], '=');
2161                         char *arg = x_strdup(argv[i]);
2162                         bool supported_profile_option = false;
2163
2164                         if (arg_profile_dir) {
2165                                 char *option = x_strndup(argv[i], arg_profile_dir - argv[i]);
2166                                 char *dir;
2167
2168                                 /* Convert to absolute path. */
2169                                 dir = x_realpath(arg_profile_dir + 1);
2170                                 if (!dir) {
2171                                         /* Directory doesn't exist. */
2172                                         dir = x_strdup(arg_profile_dir + 1);
2173                                 }
2174
2175                                 /* We can get a better hit rate by using the real path here. */
2176                                 free(arg);
2177                                 arg = format("%s=%s", option, dir);
2178                                 cc_log("Rewriting %s to %s", argv[i], arg);
2179                                 free(option);
2180                                 free(dir);
2181                         }
2182
2183                         if (str_startswith(argv[i], "-fprofile-generate")
2184                             || str_eq(argv[i], "-fprofile-arcs")) {
2185                                 profile_generate = true;
2186                                 supported_profile_option = true;
2187                         } else if (str_startswith(argv[i], "-fprofile-use")
2188                                    || str_eq(argv[i], "-fbranch-probabilities")) {
2189                                 profile_use = true;
2190                                 supported_profile_option = true;
2191                         } else if (str_eq(argv[i], "-fprofile-dir")) {
2192                                 supported_profile_option = true;
2193                         }
2194
2195                         if (supported_profile_option) {
2196                                 args_add(stripped_args, arg);
2197                                 free(arg);
2198
2199                                 /*
2200                                  * If the profile directory has already been set, give up... Hard to
2201                                  * know what the user means, and what the compiler will do.
2202                                  */
2203                                 if (arg_profile_dir && profile_dir) {
2204                                         cc_log("Profile directory already set; giving up");
2205                                         result = false;
2206                                         goto out;
2207                                 } else if (arg_profile_dir) {
2208                                         cc_log("Setting profile directory to %s", profile_dir);
2209                                         profile_dir = x_strdup(arg_profile_dir);
2210                                 }
2211                                 continue;
2212                         }
2213                         cc_log("Unknown profile option: %s", argv[i]);
2214                         free(arg);
2215                 }
2216
2217                 if (str_eq(argv[i], "-fcolor-diagnostics")
2218                     || str_eq(argv[i], "-fno-color-diagnostics")
2219                     || str_eq(argv[i], "-fdiagnostics-color")
2220                     || str_eq(argv[i], "-fdiagnostics-color=always")
2221                     || str_eq(argv[i], "-fno-diagnostics-color")
2222                     || str_eq(argv[i], "-fdiagnostics-color=never")) {
2223                         args_add(stripped_args, argv[i]);
2224                         found_color_diagnostics = true;
2225                         continue;
2226                 }
2227                 if (str_eq(argv[i], "-fdiagnostics-color=auto")) {
2228                         if (color_output_possible()) {
2229                                 /* Output is redirected, so color output must be forced. */
2230                                 args_add(stripped_args, "-fdiagnostics-color=always");
2231                                 cc_log("Automatically forcing colors");
2232                         } else {
2233                                 args_add(stripped_args, argv[i]);
2234                         }
2235                         found_color_diagnostics = true;
2236                         continue;
2237                 }
2238
2239                 /*
2240                  * Options taking an argument that we may want to rewrite to relative paths
2241                  * to get better hit rate. A secondary effect is that paths in the standard
2242                  * error output produced by the compiler will be normalized.
2243                  */
2244                 if (compopt_takes_path(argv[i])) {
2245                         char *relpath;
2246                         char *pch_file = NULL;
2247                         if (i == argc-1) {
2248                                 cc_log("Missing argument to %s", argv[i]);
2249                                 stats_update(STATS_ARGS);
2250                                 result = false;
2251                                 goto out;
2252                         }
2253
2254                         relpath = make_relative_path(x_strdup(argv[i+1]));
2255                         if (compopt_affects_cpp(argv[i])) {
2256                                 args_add(cpp_args, argv[i]);
2257                                 args_add(cpp_args, relpath);
2258                         } else {
2259                                 args_add(stripped_args, argv[i]);
2260                                 args_add(stripped_args, relpath);
2261                         }
2262
2263                         /* Try to be smart about detecting precompiled headers */
2264                         if (str_eq(argv[i], "-include-pch")
2265                             || str_eq(argv[i], "-include-pth")) {
2266                                 if (stat(argv[i+1], &st) == 0) {
2267                                         cc_log("Detected use of precompiled header: %s", argv[i+1]);
2268                                         found_pch = true;
2269                                         pch_file = x_strdup(argv[i+1]);
2270                                 }
2271                         } else {
2272                                 char *gchpath = format("%s.gch", argv[i+1]);
2273                                 if (stat(gchpath, &st) == 0) {
2274                                         cc_log("Detected use of precompiled header: %s", gchpath);
2275                                         found_pch = true;
2276                                         pch_file = x_strdup(gchpath);
2277                                 } else {
2278                                         char *pchpath = format("%s.pch", argv[i+1]);
2279                                         if (stat(pchpath, &st) == 0) {
2280                                                 cc_log("Detected use of precompiled header: %s", pchpath);
2281                                                 found_pch = true;
2282                                                 pch_file = x_strdup(pchpath);
2283                                         } else {
2284                                                 /* clang may use pretokenized headers */
2285                                                 char *pthpath = format("%s.pth", argv[i+1]);
2286                                                 if (stat(pthpath, &st) == 0) {
2287                                                         cc_log("Detected use of pretokenized header: %s", pthpath);
2288                                                         found_pch = true;
2289                                                         pch_file = x_strdup(pthpath);
2290                                                 }
2291                                                 free(pthpath);
2292                                         }
2293                                         free(pchpath);
2294                                 }
2295                                 free(gchpath);
2296                         }
2297
2298                         if (pch_file) {
2299                                 if (included_pch_file) {
2300                                         cc_log("Multiple precompiled headers used: %s and %s\n",
2301                                                included_pch_file, pch_file);
2302                                         stats_update(STATS_ARGS);
2303                                         result = false;
2304                                         goto out;
2305                                 }
2306                                 included_pch_file = pch_file;
2307                         }
2308
2309                         free(relpath);
2310                         i++;
2311                         continue;
2312                 }
2313
2314                 /* Same as above but options with concatenated argument. */
2315                 if (compopt_short(compopt_takes_path, argv[i])) {
2316                         char *relpath;
2317                         char *option;
2318                         relpath = make_relative_path(x_strdup(argv[i] + 2));
2319                         option = format("-%c%s", argv[i][1], relpath);
2320
2321                         if (compopt_short(compopt_affects_cpp, argv[i])) {
2322                                 args_add(cpp_args, option);
2323                         } else {
2324                                 args_add(stripped_args, option);
2325                         }
2326
2327                         free(relpath);
2328                         free(option);
2329                         continue;
2330                 }
2331
2332                 /* options that take an argument */
2333                 if (compopt_takes_arg(argv[i])) {
2334                         if (i == argc-1) {
2335                                 cc_log("Missing argument to %s", argv[i]);
2336                                 stats_update(STATS_ARGS);
2337                                 result = false;
2338                                 goto out;
2339                         }
2340
2341                         if (compopt_affects_cpp(argv[i])) {
2342                                 args_add(cpp_args, argv[i]);
2343                                 args_add(cpp_args, argv[i+1]);
2344                         } else {
2345                                 args_add(stripped_args, argv[i]);
2346                                 args_add(stripped_args, argv[i+1]);
2347                         }
2348
2349                         i++;
2350                         continue;
2351                 }
2352
2353                 /* other options */
2354                 if (argv[i][0] == '-') {
2355                         if (compopt_affects_cpp(argv[i])
2356                             || compopt_prefix_affects_cpp(argv[i])) {
2357                                 args_add(cpp_args, argv[i]);
2358                         } else {
2359                                 args_add(stripped_args, argv[i]);
2360                         }
2361                         continue;
2362                 }
2363
2364                 /* if an argument isn't a plain file then assume its
2365                  * an option, not an input file. This allows us to
2366                  * cope better with unusual compiler options */
2367                 if (stat(argv[i], &st) != 0 || !S_ISREG(st.st_mode)) {
2368                         cc_log("%s is not a regular file, not considering as input file",
2369                                argv[i]);
2370                         args_add(stripped_args, argv[i]);
2371                         continue;
2372                 }
2373
2374                 if (input_file) {
2375                         if (language_for_file(argv[i])) {
2376                                 cc_log("Multiple input files: %s and %s", input_file, argv[i]);
2377                                 stats_update(STATS_MULTIPLE);
2378                         } else if (!found_c_opt) {
2379                                 cc_log("Called for link with %s", argv[i]);
2380                                 if (strstr(argv[i], "conftest.")) {
2381                                         stats_update(STATS_CONFTEST);
2382                                 } else {
2383                                         stats_update(STATS_LINK);
2384                                 }
2385                         } else {
2386                                 cc_log("Unsupported source extension: %s", argv[i]);
2387                                 stats_update(STATS_SOURCELANG);
2388                         }
2389                         result = false;
2390                         goto out;
2391                 }
2392
2393                 /* The source code file path gets put into the notes */
2394                 if (generating_coverage) {
2395                         input_file = x_strdup(argv[i]);
2396                         continue;
2397                 }
2398
2399                 /* Rewrite to relative to increase hit rate. */
2400                 input_file = make_relative_path(x_strdup(argv[i]));
2401         } /* for */
2402
2403         if (found_S_opt) {
2404                 /* Even if -gsplit-dwarf is given, the .dwo file is not generated when -S
2405                  * is also given.
2406                  */
2407                 using_split_dwarf = false;
2408                 cc_log("Disabling caching of dwarf files since -S is used");
2409         }
2410
2411         if (!input_file) {
2412                 cc_log("No input file found");
2413                 stats_update(STATS_NOINPUT);
2414                 result = false;
2415                 goto out;
2416         }
2417
2418         if (found_pch || found_fpch_preprocess) {
2419                 using_precompiled_header = true;
2420                 if (!(conf->sloppiness & SLOPPY_TIME_MACROS)) {
2421                         cc_log("You have to specify \"time_macros\" sloppiness when using"
2422                                " precompiled headers to get direct hits");
2423                         cc_log("Disabling direct mode");
2424                         stats_update(STATS_CANTUSEPCH);
2425                         result = false;
2426                         goto out;
2427                 }
2428         }
2429
2430         if (explicit_language && str_eq(explicit_language, "none")) {
2431                 explicit_language = NULL;
2432         }
2433         file_language = language_for_file(input_file);
2434         if (explicit_language) {
2435                 if (!language_is_supported(explicit_language)) {
2436                         cc_log("Unsupported language: %s", explicit_language);
2437                         stats_update(STATS_SOURCELANG);
2438                         result = false;
2439                         goto out;
2440                 }
2441                 actual_language = explicit_language;
2442         } else {
2443                 actual_language = file_language;
2444         }
2445
2446         output_is_precompiled_header =
2447           actual_language && strstr(actual_language, "-header");
2448
2449         if (output_is_precompiled_header
2450             && !(conf->sloppiness & SLOPPY_PCH_DEFINES)) {
2451                 cc_log("You have to specify \"pch_defines,time_macros\" sloppiness when"
2452                        " creating precompiled headers");
2453                 stats_update(STATS_CANTUSEPCH);
2454                 result = false;
2455                 goto out;
2456         }
2457
2458         if (!found_c_opt) {
2459                 if (output_is_precompiled_header) {
2460                         args_add(stripped_args, "-c");
2461                 } else {
2462                         cc_log("No -c option found");
2463                         /* I find that having a separate statistic for autoconf tests is useful,
2464                          * as they are the dominant form of "called for link" in many cases */
2465                         if (strstr(input_file, "conftest.")) {
2466                                 stats_update(STATS_CONFTEST);
2467                         } else {
2468                                 stats_update(STATS_LINK);
2469                         }
2470                         result = false;
2471                         goto out;
2472                 }
2473         }
2474
2475         if (!actual_language) {
2476                 cc_log("Unsupported source extension: %s", input_file);
2477                 stats_update(STATS_SOURCELANG);
2478                 result = false;
2479                 goto out;
2480         }
2481
2482         direct_i_file = language_is_preprocessed(actual_language);
2483
2484         if (output_is_precompiled_header) {
2485                 /* It doesn't work to create the .gch from preprocessed source. */
2486                 cc_log("Creating precompiled header; not compiling preprocessed code");
2487                 conf->run_second_cpp = true;
2488         }
2489
2490         if (str_eq(conf->cpp_extension, "")) {
2491                 const char *p_language = p_language_for_language(actual_language);
2492                 free(conf->cpp_extension);
2493                 conf->cpp_extension = x_strdup(extension_for_language(p_language) + 1);
2494         }
2495
2496         /* don't try to second guess the compilers heuristics for stdout handling */
2497         if (output_obj && str_eq(output_obj, "-")) {
2498                 stats_update(STATS_OUTSTDOUT);
2499                 cc_log("Output file is -");
2500                 result = false;
2501                 goto out;
2502         }
2503
2504         if (!output_obj) {
2505                 if (output_is_precompiled_header) {
2506                         output_obj = format("%s.gch", input_file);
2507                 } else {
2508                         char *p;
2509                         output_obj = basename(input_file);
2510                         p = strrchr(output_obj, '.');
2511                         if (!p || !p[1]) {
2512                                 cc_log("Badly formed object filename");
2513                                 stats_update(STATS_ARGS);
2514                                 result = false;
2515                                 goto out;
2516                         }
2517                         p[1] = found_S_opt ? 's' : 'o';
2518                         p[2] = 0;
2519                 }
2520         }
2521
2522         if (using_split_dwarf) {
2523                 char *p;
2524                 p = strrchr(output_obj, '.');
2525                 if (!p || !p[1]) {
2526                         cc_log("Badly formed object filename");
2527                         stats_update(STATS_ARGS);
2528                         result = false;
2529                         goto out;
2530                 }
2531                 {
2532                         char *base_name = remove_extension(output_obj);
2533                         output_dwo = format("%s.dwo", base_name);
2534                         free(base_name);
2535                 }
2536         }
2537
2538         /* cope with -o /dev/null */
2539         if (!str_eq(output_obj, "/dev/null")
2540             && stat(output_obj, &st) == 0
2541             && !S_ISREG(st.st_mode)) {
2542                 cc_log("Not a regular file: %s", output_obj);
2543                 stats_update(STATS_DEVICE);
2544                 result = false;
2545                 goto out;
2546         }
2547
2548         /*
2549          * Some options shouldn't be passed to the real compiler when it compiles
2550          * preprocessed code:
2551          *
2552          * -finput-charset=XXX (otherwise conversion happens twice)
2553          * -x XXX (otherwise the wrong language is selected)
2554          */
2555         if (input_charset) {
2556                 args_add(cpp_args, input_charset);
2557         }
2558         if (found_pch) {
2559                 args_add(cpp_args, "-fpch-preprocess");
2560         }
2561         if (explicit_language) {
2562                 args_add(cpp_args, "-x");
2563                 args_add(cpp_args, explicit_language);
2564         }
2565
2566         /*
2567          * Since output is redirected, compilers will not color their output by
2568          * default, so force it explicitly if it would be otherwise done.
2569          */
2570         if (!found_color_diagnostics && color_output_possible()) {
2571                 if (compiler_is_clang(args)) {
2572                         args_add(stripped_args, "-fcolor-diagnostics");
2573                         cc_log("Automatically enabling colors");
2574                 } else if (compiler_is_gcc(args)) {
2575                         /*
2576                          * GCC has it since 4.9, but that'd require detecting what GCC version is
2577                          * used for the actual compile. However it requires also GCC_COLORS to be
2578                          * set (and not empty), so use that for detecting if GCC would use
2579                          * colors.
2580                          */
2581                         if (getenv("GCC_COLORS") && getenv("GCC_COLORS")[0] != '\0') {
2582                                 args_add(stripped_args, "-fdiagnostics-color");
2583                                 cc_log("Automatically enabling colors");
2584                         }
2585                 }
2586         }
2587
2588         /*
2589          * Add flags for dependency generation only to the preprocessor command line.
2590          */
2591         if (generating_dependencies) {
2592                 if (!dependency_filename_specified) {
2593                         char *default_depfile_name;
2594                         char *base_name;
2595
2596                         base_name = remove_extension(output_obj);
2597                         default_depfile_name = format("%s.d", base_name);
2598                         free(base_name);
2599                         args_add(dep_args, "-MF");
2600                         args_add(dep_args, default_depfile_name);
2601                         output_dep = make_relative_path(x_strdup(default_depfile_name));
2602                 }
2603
2604                 if (!dependency_target_specified) {
2605                         args_add(dep_args, "-MQ");
2606                         args_add(dep_args, output_obj);
2607                 }
2608         }
2609         if (generating_coverage) {
2610                 char *default_covfile_name;
2611                 char *base_name;
2612
2613                 base_name = remove_extension(output_obj);
2614                 default_covfile_name = format("%s.gcno", base_name);
2615                 free(base_name);
2616                 output_cov = make_relative_path(x_strdup(default_covfile_name));
2617         }
2618
2619         *compiler_args = args_copy(stripped_args);
2620         if (conf->run_second_cpp) {
2621                 args_extend(*compiler_args, cpp_args);
2622         } else {
2623                 if (explicit_language) {
2624                         /*
2625                          * Workaround for a bug in Apple's patched distcc -- it doesn't properly
2626                          * reset the language specified with -x, so if -x is given, we have to
2627                          * specify the preprocessed language explicitly.
2628                          */
2629                         args_add(*compiler_args, "-x");
2630                         args_add(*compiler_args, p_language_for_language(explicit_language));
2631                 }
2632         }
2633
2634         if (found_c_opt) {
2635                 args_add(*compiler_args, "-c");
2636         }
2637
2638         /*
2639          * Only pass dependency arguments to the preprocesor since Intel's C++
2640          * compiler doesn't produce a correct .d file when compiling preprocessed
2641          * source.
2642          */
2643         args_extend(cpp_args, dep_args);
2644
2645         *preprocessor_args = args_copy(stripped_args);
2646         args_extend(*preprocessor_args, cpp_args);
2647
2648 out:
2649         args_free(expanded_args);
2650         args_free(stripped_args);
2651         args_free(dep_args);
2652         args_free(cpp_args);
2653         return result;
2654 }
2655
2656 static void
2657 create_initial_config_file(struct conf *conf, const char *path)
2658 {
2659         unsigned max_files;
2660         uint64_t max_size;
2661         char *stats_dir;
2662         FILE *f;
2663         struct stat st;
2664
2665         if (create_parent_dirs(path) != 0) {
2666                 return;
2667         }
2668
2669         stats_dir = format("%s/0", conf->cache_dir);
2670         if (stat(stats_dir, &st) == 0) {
2671                 stats_get_obsolete_limits(stats_dir, &max_files, &max_size);
2672                 /* STATS_MAXFILES and STATS_MAXSIZE was stored for each top directory. */
2673                 max_files *= 16;
2674                 max_size *= 16;
2675         } else {
2676                 max_files = 0;
2677                 max_size = conf->max_size;
2678         }
2679         free(stats_dir);
2680
2681         f = fopen(path, "w");
2682         if (!f) {
2683                 return;
2684         }
2685         if (max_files != 0) {
2686                 fprintf(f, "max_files = %u\n", max_files);
2687                 conf->max_files = max_files;
2688         }
2689         if (max_size != 0) {
2690                 char *size = format_parsable_size_with_suffix(max_size);
2691                 fprintf(f, "max_size = %s\n", size);
2692                 free(size);
2693                 conf->max_size = max_size;
2694         }
2695         fclose(f);
2696 }
2697
2698 /*
2699  * Read config file(s), populate variables, create configuration file in cache
2700  * directory if missing, etc.
2701  */
2702 static void
2703 initialize(void)
2704 {
2705         char *errmsg;
2706         char *p;
2707         struct stat st;
2708         bool should_create_initial_config = false;
2709
2710         conf_free(conf);
2711         conf = conf_create();
2712
2713         p = getenv("CCACHE_CONFIGPATH");
2714         if (p) {
2715                 primary_config_path = x_strdup(p);
2716         } else {
2717                 secondary_config_path = format("%s/ccache.conf", TO_STRING(SYSCONFDIR));
2718                 if (!conf_read(conf, secondary_config_path, &errmsg)) {
2719                         if (stat(secondary_config_path, &st) == 0) {
2720                                 fatal("%s", errmsg);
2721                         }
2722                         /* Missing config file in SYSCONFDIR is OK. */
2723                         free(errmsg);
2724                 }
2725
2726                 if (str_eq(conf->cache_dir, "")) {
2727                         fatal("configuration setting \"cache_dir\" must not be the empty string");
2728                 }
2729                 if ((p = getenv("CCACHE_DIR"))) {
2730                         free(conf->cache_dir);
2731                         conf->cache_dir = strdup(p);
2732                 }
2733                 if (str_eq(conf->cache_dir, "")) {
2734                         fatal("CCACHE_DIR must not be the empty string");
2735                 }
2736
2737                 primary_config_path = format("%s/ccache.conf", conf->cache_dir);
2738         }
2739
2740         if (!conf_read(conf, primary_config_path, &errmsg)) {
2741                 if (stat(primary_config_path, &st) == 0) {
2742                         fatal("%s", errmsg);
2743                 }
2744                 should_create_initial_config = true;
2745         }
2746
2747         if (!conf_update_from_environment(conf, &errmsg)) {
2748                 fatal("%s", errmsg);
2749         }
2750
2751         if (conf->disable) {
2752                 should_create_initial_config = false;
2753         }
2754
2755         if (should_create_initial_config) {
2756                 create_initial_config_file(conf, primary_config_path);
2757         }
2758
2759         exitfn_init();
2760         exitfn_add_nullary(stats_flush);
2761         exitfn_add_nullary(clean_up_pending_tmp_files);
2762
2763         cc_log("=== CCACHE %s STARTED =========================================",
2764                CCACHE_VERSION);
2765
2766         if (conf->umask != UINT_MAX) {
2767                 umask(conf->umask);
2768         }
2769 }
2770
2771 /* Reset the global state. Used by the test suite. */
2772 void
2773 cc_reset(void)
2774 {
2775         conf_free(conf); conf = NULL;
2776         free(primary_config_path); primary_config_path = NULL;
2777         free(secondary_config_path); secondary_config_path = NULL;
2778         free(current_working_dir); current_working_dir = NULL;
2779         free(profile_dir); profile_dir = NULL;
2780         free(included_pch_file); included_pch_file = NULL;
2781         args_free(orig_args); orig_args = NULL;
2782         free(input_file); input_file = NULL;
2783         free(output_obj); output_obj = NULL;
2784         free(output_dwo); output_dwo = NULL;
2785         free(output_dep); output_dep = NULL;
2786         free(output_cov); output_cov = NULL;
2787         free(output_dia); output_dia = NULL;
2788         free(cached_obj_hash); cached_obj_hash = NULL;
2789         free(cached_obj); cached_obj = NULL;
2790         free(cached_dwo); cached_dwo = NULL;
2791         free(cached_stderr); cached_stderr = NULL;
2792         free(cached_dep); cached_dep = NULL;
2793         free(cached_cov); cached_cov = NULL;
2794         free(cached_dia); cached_dia = NULL;
2795         free(manifest_path); manifest_path = NULL;
2796         time_of_compilation = 0;
2797         if (included_files) {
2798                 hashtable_destroy(included_files, 1); included_files = NULL;
2799         }
2800         generating_dependencies = false;
2801         generating_coverage = false;
2802         profile_arcs = false;
2803         free(profile_dir); profile_dir = NULL;
2804         i_tmpfile = NULL;
2805         direct_i_file = false;
2806         free(cpp_stderr); cpp_stderr = NULL;
2807         free(stats_file); stats_file = NULL;
2808         output_is_precompiled_header = false;
2809
2810         conf = conf_create();
2811         using_split_dwarf = false;
2812 }
2813
2814 /* Make a copy of stderr that will not be cached, so things like
2815  * distcc can send networking errors to it. */
2816 static void
2817 setup_uncached_err(void)
2818 {
2819         char *buf;
2820         int uncached_fd;
2821
2822         uncached_fd = dup(2);
2823         if (uncached_fd == -1) {
2824                 cc_log("dup(2) failed: %s", strerror(errno));
2825                 failed();
2826         }
2827
2828         /* leak a pointer to the environment */
2829         buf = format("UNCACHED_ERR_FD=%d", uncached_fd);
2830
2831         if (putenv(buf) == -1) {
2832                 cc_log("putenv failed: %s", strerror(errno));
2833                 failed();
2834         }
2835 }
2836
2837 static void
2838 configuration_logger(const char *descr, const char *origin, void *context)
2839 {
2840         (void)context;
2841         cc_bulklog("Config: (%s) %s", origin, descr);
2842 }
2843
2844 /* the main ccache driver function */
2845 static void
2846 ccache(int argc, char *argv[])
2847 {
2848         bool put_object_in_manifest = false;
2849         struct file_hash *object_hash;
2850         struct file_hash *object_hash_from_manifest = NULL;
2851         struct mdfour common_hash;
2852         struct mdfour direct_hash;
2853         struct mdfour cpp_hash;
2854
2855         /* Arguments (except -E) to send to the preprocessor. */
2856         struct args *preprocessor_args;
2857
2858         /* Arguments to send to the real compiler. */
2859         struct args *compiler_args;
2860
2861         orig_args = args_init(argc, argv);
2862
2863         initialize();
2864         find_compiler(argv);
2865
2866 #ifndef _WIN32
2867         signal(SIGHUP, signal_handler);
2868 #endif
2869         signal(SIGINT, signal_handler);
2870         signal(SIGTERM, signal_handler);
2871
2872         if (str_eq(conf->temporary_dir, "")) {
2873                 clean_up_internal_tempdir();
2874         }
2875
2876         if (!str_eq(conf->log_file, "")) {
2877                 conf_print_items(conf, configuration_logger, NULL);
2878         }
2879
2880         if (conf->disable) {
2881                 cc_log("ccache is disabled");
2882                 failed();
2883         }
2884
2885         setup_uncached_err();
2886
2887         cc_log_argv("Command line: ", argv);
2888         cc_log("Hostname: %s", get_hostname());
2889         cc_log("Working directory: %s", get_current_working_dir());
2890
2891         if (conf->unify) {
2892                 cc_log("Direct mode disabled because unify mode is enabled");
2893                 conf->direct_mode = false;
2894         }
2895
2896         if (!cc_process_args(orig_args, &preprocessor_args, &compiler_args)) {
2897                 failed();
2898         }
2899
2900         cc_log("Source file: %s", input_file);
2901         if (generating_dependencies) {
2902                 cc_log("Dependency file: %s", output_dep);
2903         }
2904         if (generating_coverage) {
2905                 cc_log("Coverage file: %s", output_cov);
2906         }
2907         if (output_dia) {
2908                 cc_log("Diagnostic file: %s", output_dia);
2909         }
2910
2911         if (using_split_dwarf) {
2912                 if (!generating_dependencies) {
2913                         assert(output_dwo);
2914                 }
2915         } else {
2916                 assert(!output_dwo);
2917         }
2918
2919         if (output_dwo) {
2920                 cc_log("Split dwarf file: %s", output_dwo);
2921         }
2922
2923         cc_log("Object file: %s", output_obj);
2924
2925         hash_start(&common_hash);
2926         calculate_common_hash(preprocessor_args, &common_hash);
2927
2928         /* try to find the hash using the manifest */
2929         direct_hash = common_hash;
2930         if (conf->direct_mode) {
2931                 cc_log("Trying direct lookup");
2932                 object_hash = calculate_object_hash(preprocessor_args, &direct_hash, 1);
2933                 if (object_hash) {
2934                         update_cached_result_globals(object_hash);
2935
2936                         /*
2937                          * If we can return from cache at this point then do
2938                          * so.
2939                          */
2940                         from_cache(FROMCACHE_DIRECT_MODE, 0);
2941
2942                         /*
2943                          * Wasn't able to return from cache at this point.
2944                          * However, the object was already found in manifest,
2945                          * so don't readd it later.
2946                          */
2947                         put_object_in_manifest = false;
2948
2949                         object_hash_from_manifest = object_hash;
2950                 } else {
2951                         /* Add object to manifest later. */
2952                         put_object_in_manifest = true;
2953                 }
2954         }
2955
2956         if (conf->read_only_direct) {
2957                 cc_log("Read-only direct mode; running real compiler");
2958                 failed();
2959         }
2960
2961         /*
2962          * Find the hash using the preprocessed output. Also updates
2963          * included_files.
2964          */
2965         cpp_hash = common_hash;
2966         object_hash = calculate_object_hash(preprocessor_args, &cpp_hash, 0);
2967         if (!object_hash) {
2968                 fatal("internal error: object hash from cpp returned NULL");
2969         }
2970         update_cached_result_globals(object_hash);
2971
2972         if (object_hash_from_manifest
2973             && !file_hashes_equal(object_hash_from_manifest, object_hash)) {
2974                 /*
2975                  * The hash from manifest differs from the hash of the
2976                  * preprocessor output. This could be because:
2977                  *
2978                  * - The preprocessor produces different output for the same
2979                  *   input (not likely).
2980                  * - There's a bug in ccache (maybe incorrect handling of
2981                  *   compiler arguments).
2982                  * - The user has used a different CCACHE_BASEDIR (most
2983                  *   likely).
2984                  *
2985                  * The best thing here would probably be to remove the hash
2986                  * entry from the manifest. For now, we use a simpler method:
2987                  * just remove the manifest file.
2988                  */
2989                 cc_log("Hash from manifest doesn't match preprocessor output");
2990                 cc_log("Likely reason: different CCACHE_BASEDIRs used");
2991                 cc_log("Removing manifest as a safety measure");
2992                 x_unlink(manifest_path);
2993
2994                 put_object_in_manifest = true;
2995         }
2996
2997         /* if we can return from cache at this point then do */
2998         from_cache(FROMCACHE_CPP_MODE, put_object_in_manifest);
2999
3000         if (conf->read_only) {
3001                 cc_log("Read-only mode; running real compiler");
3002                 failed();
3003         }
3004
3005         add_prefix(compiler_args);
3006
3007         /* run real compiler, sending output to cache */
3008         to_cache(compiler_args);
3009
3010         x_exit(0);
3011 }
3012
3013 static void
3014 configuration_printer(const char *descr, const char *origin, void *context)
3015 {
3016         assert(context);
3017         fprintf(context, "(%s) %s\n", origin, descr);
3018 }
3019
3020 /* the main program when not doing a compile */
3021 static int
3022 ccache_main_options(int argc, char *argv[])
3023 {
3024         int c;
3025         char *errmsg;
3026
3027         enum longopts {
3028                 DUMP_MANIFEST
3029         };
3030         static const struct option options[] = {
3031                 {"cleanup",       no_argument,       0, 'c'},
3032                 {"clear",         no_argument,       0, 'C'},
3033                 {"dump-manifest", required_argument, 0, DUMP_MANIFEST},
3034                 {"help",          no_argument,       0, 'h'},
3035                 {"max-files",     required_argument, 0, 'F'},
3036                 {"max-size",      required_argument, 0, 'M'},
3037                 {"set-config",    required_argument, 0, 'o'},
3038                 {"print-config",  no_argument,       0, 'p'},
3039                 {"show-stats",    no_argument,       0, 's'},
3040                 {"version",       no_argument,       0, 'V'},
3041                 {"zero-stats",    no_argument,       0, 'z'},
3042                 {0, 0, 0, 0}
3043         };
3044
3045         while ((c = getopt_long(argc, argv, "cChF:M:o:psVz", options, NULL)) != -1) {
3046                 switch (c) {
3047                 case DUMP_MANIFEST:
3048                         manifest_dump(optarg, stdout);
3049                         break;
3050
3051                 case 'c': /* --cleanup */
3052                         initialize();
3053                         cleanup_all(conf);
3054                         printf("Cleaned cache\n");
3055                         break;
3056
3057                 case 'C': /* --clear */
3058                         initialize();
3059                         wipe_all(conf);
3060                         printf("Cleared cache\n");
3061                         break;
3062
3063                 case 'h': /* --help */
3064                         fputs(USAGE_TEXT, stdout);
3065                         x_exit(0);
3066
3067                 case 'F': /* --max-files */
3068                 {
3069                         unsigned files;
3070                         initialize();
3071                         files = atoi(optarg);
3072                         if (conf_set_value_in_file(primary_config_path, "max_files", optarg,
3073                                                    &errmsg)) {
3074                                 if (files == 0) {
3075                                         printf("Unset cache file limit\n");
3076                                 } else {
3077                                         printf("Set cache file limit to %u\n", files);
3078                                 }
3079                         } else {
3080                                 fatal("could not set cache file limit: %s", errmsg);
3081                         }
3082                 }
3083                 break;
3084
3085                 case 'M': /* --max-size */
3086                 {
3087                         uint64_t size;
3088                         initialize();
3089                         if (!parse_size_with_suffix(optarg, &size)) {
3090                                 fatal("invalid size: %s", optarg);
3091                         }
3092                         if (conf_set_value_in_file(primary_config_path, "max_size", optarg,
3093                                                    &errmsg)) {
3094                                 if (size == 0) {
3095                                         printf("Unset cache size limit\n");
3096                                 } else {
3097                                         char *s = format_human_readable_size(size);
3098                                         printf("Set cache size limit to %s\n", s);
3099                                         free(s);
3100                                 }
3101                         } else {
3102                                 fatal("could not set cache size limit: %s", errmsg);
3103                         }
3104                 }
3105                 break;
3106
3107                 case 'o': /* --set-config */
3108                 {
3109                         char *errmsg, *key, *value, *p;
3110                         initialize();
3111                         p = strchr(optarg, '=');
3112                         if (!p) {
3113                                 fatal("missing equal sign in \"%s\"", optarg);
3114                         }
3115                         key = x_strndup(optarg, p - optarg);
3116                         value = p + 1;
3117                         if (!conf_set_value_in_file(primary_config_path, key, value, &errmsg)) {
3118                                 fatal("%s", errmsg);
3119                         }
3120                         free(key);
3121                 }
3122                 break;
3123
3124                 case 'p': /* --print-config */
3125                         initialize();
3126                         conf_print_items(conf, configuration_printer, stdout);
3127                         break;
3128
3129                 case 's': /* --show-stats */
3130                         initialize();
3131                         stats_summary(conf);
3132                         break;
3133
3134                 case 'V': /* --version */
3135                         fprintf(stdout, VERSION_TEXT, CCACHE_VERSION);
3136                         x_exit(0);
3137
3138                 case 'z': /* --zero-stats */
3139                         initialize();
3140                         stats_zero();
3141                         printf("Statistics cleared\n");
3142                         break;
3143
3144                 default:
3145                         fputs(USAGE_TEXT, stderr);
3146                         x_exit(1);
3147                 }
3148         }
3149
3150         return 0;
3151 }
3152
3153 int
3154 ccache_main(int argc, char *argv[])
3155 {
3156         /* check if we are being invoked as "ccache" */
3157         char *program_name = basename(argv[0]);
3158         if (same_executable_name(program_name, MYNAME)) {
3159                 if (argc < 2) {
3160                         fputs(USAGE_TEXT, stderr);
3161                         x_exit(1);
3162                 }
3163                 /* if the first argument isn't an option, then assume we are
3164                  * being passed a compiler name and options */
3165                 if (argv[1][0] == '-') {
3166                         return ccache_main_options(argc, argv);
3167                 }
3168         }
3169         free(program_name);
3170
3171         ccache(argc, argv);
3172         return 1;
3173 }