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