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