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