1 // ccache -- a fast C/C++ compiler cache
3 // Copyright (C) 2002-2007 Andrew Tridgell
4 // Copyright (C) 2009-2017 Joel Rosdahl
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)
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
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
22 #ifdef HAVE_GETOPT_LONG
25 #include "getopt_long.h"
27 #include "hashtable.h"
28 #include "hashtable_itr.h"
33 #define STRINGIFY(x) #x
34 #define TO_STRING(x) STRINGIFY(x)
36 static const char VERSION_TEXT[] =
37 MYNAME " version %s\n"
39 "Copyright (C) 2002-2007 Andrew Tridgell\n"
40 "Copyright (C) 2009-2017 Joel Rosdahl\n"
42 "This program is free software; you can redistribute it and/or modify it under\n"
43 "the terms of the GNU General Public License as published by the Free Software\n"
44 "Foundation; either version 3 of the License, or (at your option) any later\n"
47 static const char USAGE_TEXT[] =
49 " " MYNAME " [options]\n"
50 " " MYNAME " compiler [compiler options]\n"
51 " compiler [compiler options] (via symbolic link)\n"
54 " -c, --cleanup delete old files and recalculate size counters\n"
55 " (normally not needed as this is done automatically)\n"
56 " -C, --clear clear the cache completely (except configuration)\n"
57 " -F, --max-files=N set maximum number of files in cache to N (use 0 for\n"
59 " -M, --max-size=SIZE set maximum size of cache to SIZE (use 0 for no\n"
60 " limit); available suffixes: k, M, G, T (decimal) and\n"
61 " Ki, Mi, Gi, Ti (binary); default suffix: G\n"
62 " -o, --set-config=K=V set configuration key K to value V\n"
63 " -p, --print-config print current configuration options\n"
64 " -s, --show-stats show statistics summary\n"
65 " -z, --zero-stats zero statistics counters\n"
67 " -h, --help print this help text\n"
68 " -V, --version print version and copyright information\n"
70 "See also <https://ccache.samba.org>.\n";
72 // Global configuration data.
73 struct conf *conf = NULL;
75 // Where to write configuration changes.
76 char *primary_config_path = NULL;
78 // Secondary, read-only configuration file (if any).
79 char *secondary_config_path = NULL;
81 // Current working directory taken from $PWD, or getcwd() if $PWD is bad.
82 char *current_working_dir = NULL;
84 // The original argument list.
85 static struct args *orig_args;
88 static char *input_file;
90 // The output file being compiled to.
91 static char *output_obj;
93 // The path to the dependency file (implicit or specified with -MF).
94 static char *output_dep;
96 // The path to the coverage file (implicit when using -ftest-coverage).
97 static char *output_cov;
99 // Diagnostic generation information (clang). Contains pathname if not NULL.
100 static char *output_dia = NULL;
102 // Split dwarf information (GCC 4.8 andup). Contains pathname if not NULL.
103 static char *output_dwo = NULL;
105 // Array for storing -arch options.
106 #define MAX_ARCH_ARGS 10
107 static size_t arch_args_size = 0;
108 static char *arch_args[MAX_ARCH_ARGS] = {NULL};
110 // Name (represented as a struct file_hash) of the file containing the cached
112 static struct file_hash *cached_obj_hash;
114 // Full path to the file containing the cached object code
115 // (cachedir/a/b/cdef[...]-size.o).
116 static char *cached_obj;
118 // Full path to the file containing the standard error output
119 // (cachedir/a/b/cdef[...]-size.stderr).
120 static char *cached_stderr;
122 // Full path to the file containing the dependency information
123 // (cachedir/a/b/cdef[...]-size.d).
124 static char *cached_dep;
126 // Full path to the file containing the coverage information
127 // (cachedir/a/b/cdef[...]-size.gcno).
128 static char *cached_cov;
130 // Full path to the file containing the diagnostic information (for clang)
131 // (cachedir/a/b/cdef[...]-size.dia).
132 static char *cached_dia;
134 // Full path to the file containing the split dwarf (for GCC 4.8 and above)
135 // (cachedir/a/b/cdef[...]-size.dwo).
137 // Contains NULL if -gsplit-dwarf is not given.
138 static char *cached_dwo;
140 // using_split_dwarf is true if "-gsplit-dwarf" is given to the compiler (GCC
142 bool using_split_dwarf = false;
144 // Full path to the file containing the manifest
145 // (cachedir/a/b/cdef[...]-size.manifest).
146 static char *manifest_path;
148 // Time of compilation. Used to see if include files have changed after
150 time_t time_of_compilation;
152 // Files included by the preprocessor and their hashes/sizes. Key: file path.
153 // Value: struct file_hash.
154 static struct hashtable *included_files = NULL;
156 // Uses absolute path for some include files.
157 static bool has_absolute_include_headers = false;
159 // List of headers to ignore.
160 static char **ignore_headers;
162 // Size of headers to ignore list.
163 static size_t ignore_headers_len;
165 // Is the compiler being asked to output debug info?
166 static bool generating_debuginfo;
168 // Is the compiler being asked to output dependencies?
169 static bool generating_dependencies;
171 // Is the compiler being asked to output coverage?
172 static bool generating_coverage;
174 // Relocating debuginfo in the format old=new.
175 static char *debug_prefix_map = NULL;
177 // Is the compiler being asked to output coverage data (.gcda) at runtime?
178 static bool profile_arcs;
180 // Name of the custom profile directory (default: object dirname).
181 static char *profile_dir;
183 // The name of the temporary preprocessed file.
184 static char *i_tmpfile;
186 // Are we compiling a .i or .ii file directly?
187 static bool direct_i_file;
189 // The name of the cpp stderr file.
190 static char *cpp_stderr;
192 // Full path to the statistics file in the subdirectory where the cached result
193 // belongs (<cache_dir>/<x>/stats).
194 char *stats_file = NULL;
196 // Whether the output is a precompiled header.
197 static bool output_is_precompiled_header = false;
199 // Profile generation / usage information.
200 static char *profile_dir = NULL;
201 static bool profile_use = false;
202 static bool profile_generate = false;
204 // Whether we are using a precompiled header (either via -include, #include or
205 // clang's -include-pch or -include-pth).
206 static bool using_precompiled_header = false;
208 // The .gch/.pch/.pth file used for compilation.
209 static char *included_pch_file = NULL;
211 // How long (in microseconds) to wait before breaking a stale lock.
212 unsigned lock_staleness_limit = 2000000;
214 enum fromcache_call_mode {
215 FROMCACHE_DIRECT_MODE,
219 struct pending_tmp_file {
221 struct pending_tmp_file *next;
224 // Temporary files to remove at program exit.
225 static struct pending_tmp_file *pending_tmp_files = NULL;
228 static sigset_t fatal_signal_set;
230 // PID of currently executing compiler that we have started, if any. 0 means no
231 // ongoing compilation.
232 static pid_t compiler_pid = 0;
235 // This is a string that identifies the current "version" of the hash sum
236 // computed by ccache. If, for any reason, we want to force the hash sum to be
237 // different for the same input in a new ccache version, we can just change
238 // this string. A typical example would be if the format of one of the files
239 // stored in the cache changes in a backwards-incompatible way.
240 static const char HASH_PREFIX[] = "3";
243 add_prefix(struct args *args, char *prefix_command)
245 if (str_eq(prefix_command, "")) {
249 struct args *prefix = args_init(0, NULL);
250 char *e = x_strdup(prefix_command);
251 char *saveptr = NULL;
252 for (char *tok = strtok_r(e, " ", &saveptr);
254 tok = strtok_r(NULL, " ", &saveptr)) {
257 p = find_executable(tok, MYNAME);
259 fatal("%s: %s", tok, strerror(errno));
267 cc_log("Using command-line prefix %s", prefix_command);
268 for (int i = prefix->argc; i != 0; i--) {
269 args_add_prefix(args, prefix->argv[i-1]);
274 // Something went badly wrong - just execute the real compiler.
280 args_strip(orig_args, "--ccache-");
281 add_prefix(orig_args, conf->prefix_command);
283 cc_log("Failed; falling back to running the real compiler");
284 cc_log_argv("Executing ", orig_args->argv);
286 execv(orig_args->argv[0], orig_args->argv);
287 fatal("execv of %s failed: %s", orig_args->argv[0], strerror(errno));
293 static char *path = NULL;
295 return path; // Memoize
297 path = conf->temporary_dir;
298 if (str_eq(path, "")) {
299 path = format("%s/tmp", conf->cache_dir);
308 sigprocmask(SIG_BLOCK, &fatal_signal_set, NULL);
313 unblock_signals(void)
318 sigprocmask(SIG_SETMASK, &empty, NULL);
323 add_pending_tmp_file(const char *path)
326 struct pending_tmp_file *e = x_malloc(sizeof(*e));
327 e->path = x_strdup(path);
328 e->next = pending_tmp_files;
329 pending_tmp_files = e;
334 do_clean_up_pending_tmp_files(void)
336 struct pending_tmp_file *p = pending_tmp_files;
338 // Can't call tmp_unlink here since its cc_log calls aren't signal safe.
341 // Leak p->path and p here because clean_up_pending_tmp_files needs to be
347 clean_up_pending_tmp_files(void)
350 do_clean_up_pending_tmp_files();
356 signal_handler(int signum)
358 // Unregister handler for this signal so that we can send the signal to
359 // ourselves at the end of the handler.
360 signal(signum, SIG_DFL);
362 // If ccache was killed explicitly, then bring the compiler subprocess (if
363 // any) with us as well.
364 if (signum == SIGTERM
366 && waitpid(compiler_pid, NULL, WNOHANG) == 0) {
367 kill(compiler_pid, signum);
370 do_clean_up_pending_tmp_files();
372 if (compiler_pid != 0) {
373 // Wait for compiler subprocess to exit before we snuff it.
374 waitpid(compiler_pid, NULL, 0);
377 // Resend signal to ourselves to exit properly after returning from the
379 kill(getpid(), signum);
383 register_signal_handler(int signum)
385 struct sigaction act;
386 memset(&act, 0, sizeof(act));
387 act.sa_handler = signal_handler;
388 act.sa_mask = fatal_signal_set;
390 act.sa_flags = SA_RESTART;
392 sigaction(signum, &act, NULL);
396 set_up_signal_handlers(void)
398 sigemptyset(&fatal_signal_set);
399 sigaddset(&fatal_signal_set, SIGINT);
400 sigaddset(&fatal_signal_set, SIGTERM);
402 sigaddset(&fatal_signal_set, SIGHUP);
405 sigaddset(&fatal_signal_set, SIGQUIT);
408 register_signal_handler(SIGINT);
409 register_signal_handler(SIGTERM);
411 register_signal_handler(SIGHUP);
414 register_signal_handler(SIGQUIT);
420 clean_up_internal_tempdir(void)
422 time_t now = time(NULL);
424 if (x_stat(conf->cache_dir, &st) != 0 || st.st_mtime + 3600 >= now) {
425 // No cleanup needed.
429 update_mtime(conf->cache_dir);
431 DIR *dir = opendir(temp_dir());
436 struct dirent *entry;
437 while ((entry = readdir(dir))) {
438 if (str_eq(entry->d_name, ".") || str_eq(entry->d_name, "..")) {
442 char *path = format("%s/%s", temp_dir(), entry->d_name);
443 if (x_lstat(path, &st) == 0 && st.st_mtime + 3600 < now) {
453 get_current_working_dir(void)
455 if (!current_working_dir) {
456 char *cwd = get_cwd();
458 current_working_dir = x_realpath(cwd);
461 if (!current_working_dir) {
462 cc_log("Unable to determine current working directory: %s",
467 return current_working_dir;
470 // Transform a name to a full path into the cache directory, creating needed
471 // sublevels if needed. Caller frees.
473 get_path_in_cache(const char *name, const char *suffix)
475 char *path = x_strdup(conf->cache_dir);
476 for (unsigned i = 0; i < conf->cache_dir_levels; ++i) {
477 char *p = format("%s/%c", path, name[i]);
483 format("%s/%s%s", path, name + conf->cache_dir_levels, suffix);
488 // This function hashes an include file and stores the path and hash in the
489 // global included_files variable. If the include file is a PCH, cpp_hash is
490 // also updated. Takes over ownership of path.
492 remember_include_file(char *path, struct mdfour *cpp_hash, bool system)
494 size_t path_len = strlen(path);
495 if (path_len >= 2 && (path[0] == '<' && path[path_len - 1] == '>')) {
496 // Typically <built-in> or <command-line>.
500 if (str_eq(path, input_file)) {
501 // Don't remember the input file.
505 if (system && (conf->sloppiness & SLOPPY_NO_SYSTEM_HEADERS)) {
506 // Don't remember this system header.
510 if (hashtable_search(included_files, path)) {
511 // Already known include file.
516 // stat fails on directories on win32.
517 DWORD attributes = GetFileAttributes(path);
518 if (attributes != INVALID_FILE_ATTRIBUTES &&
519 attributes & FILE_ATTRIBUTE_DIRECTORY) {
525 if (x_stat(path, &st) != 0) {
528 if (S_ISDIR(st.st_mode)) {
529 // Ignore directory, typically $PWD.
532 if (!S_ISREG(st.st_mode)) {
533 // Device, pipe, socket or other strange creature.
534 cc_log("Non-regular include file %s", path);
538 // Canonicalize path for comparison; clang uses ./header.h.
539 char *canonical = path;
540 size_t canonical_len = path_len;
541 if (canonical[0] == '.' && canonical[1] == '/') {
546 for (size_t i = 0; i < ignore_headers_len; i++) {
547 char *ignore = ignore_headers[i];
548 size_t ignore_len = strlen(ignore);
549 if (ignore_len > canonical_len) {
552 if (strncmp(canonical, ignore, ignore_len) == 0
553 && (ignore[ignore_len-1] == DIR_DELIM_CH
554 || canonical[ignore_len] == DIR_DELIM_CH
555 || canonical[ignore_len] == '\0')) {
560 if (!(conf->sloppiness & SLOPPY_INCLUDE_FILE_MTIME)
561 && st.st_mtime >= time_of_compilation) {
562 cc_log("Include file %s too new", path);
566 if (!(conf->sloppiness & SLOPPY_INCLUDE_FILE_CTIME)
567 && st.st_ctime >= time_of_compilation) {
568 cc_log("Include file %s ctime too new", path);
572 // Let's hash the include file content.
576 bool is_pch = is_precompiled_header(path);
578 if (!hash_file(&fhash, path)) {
581 struct file_hash pch_hash;
582 hash_result_as_bytes(&fhash, pch_hash.hash);
583 pch_hash.size = fhash.totalN;
584 hash_delimiter(cpp_hash, "pch_hash");
585 hash_buffer(cpp_hash, pch_hash.hash, sizeof(pch_hash.hash));
588 if (conf->direct_mode) {
589 if (!is_pch) { // else: the file has already been hashed.
592 if (st.st_size > 0) {
593 if (!read_file(path, st.st_size, &source, &size)) {
597 source = x_strdup("");
601 int result = hash_source_code_string(conf, &fhash, source, size, path);
603 if (result & HASH_SOURCE_CODE_ERROR
604 || result & HASH_SOURCE_CODE_FOUND_TIME) {
609 struct file_hash *h = x_malloc(sizeof(*h));
610 hash_result_as_bytes(&fhash, h->hash);
611 h->size = fhash.totalN;
612 hashtable_insert(included_files, path, h);
620 if (conf->direct_mode) {
621 cc_log("Disabling direct mode");
622 conf->direct_mode = false;
629 // Make a relative path from current working directory to path if path is under
630 // the base directory. Takes over ownership of path. Caller frees.
632 make_relative_path(char *path)
634 if (str_eq(conf->base_dir, "") || !str_startswith(path, conf->base_dir)) {
639 if (path[0] == '/') {
640 path++; // Skip leading slash.
644 // x_realpath only works for existing paths, so if path doesn't exist, try
645 // dirname(path) and assemble the path afterwards. We only bother to try
646 // canonicalizing one of these two paths since a compiler path argument
647 // typically only makes sense if path or dirname(path) exists.
648 char *path_suffix = NULL;
650 if (stat(path, &st) != 0) {
651 // path doesn't exist.
652 char *dir = dirname(path);
653 if (stat(dir, &st) != 0) {
654 // And neither does its parent directory, so no action to take.
659 path_suffix = basename(path);
661 path = dirname(path);
665 char *canon_path = x_realpath(path);
668 char *relpath = get_relative_path(get_current_working_dir(), canon_path);
671 path = format("%s/%s", relpath, path_suffix);
679 // path doesn't exist, so leave it as it is.
685 // This function reads and hashes a file. While doing this, it also does these
688 // - Makes include file paths for which the base directory is a prefix relative
689 // when computing the hash sum.
690 // - Stores the paths and hashes of included files in the global variable
693 process_preprocessed_file(struct mdfour *hash, const char *path)
697 if (!read_file(path, 0, &data, &size)) {
701 ignore_headers = NULL;
702 ignore_headers_len = 0;
703 if (!str_eq(conf->ignore_headers_in_manifest, "")) {
704 char *header, *p, *q, *saveptr = NULL;
705 p = x_strdup(conf->ignore_headers_in_manifest);
707 while ((header = strtok_r(q, PATH_DELIM, &saveptr))) {
708 ignore_headers = x_realloc(ignore_headers,
709 (ignore_headers_len+1) * sizeof(char *));
710 ignore_headers[ignore_headers_len++] = x_strdup(header);
716 if (!included_files) {
717 included_files = create_hashtable(1000, hash_from_string, strings_equal);
720 // Bytes between p and q are pending to be hashed.
723 char *end = data + size;
725 // There must be at least 7 characters (# 1 "x") left to potentially find an
726 // include file path.
727 while (q < end - 7) {
728 // Check if we look at a line containing the file name of an included file.
729 // At least the following formats exist (where N is a positive integer):
735 // #pragma GCC pch_preprocess "file"
746 // Note that there may be other lines starting with '#' left after
747 // preprocessing as well, for instance "# pragma".
750 && ((q[1] == ' ' && q[2] >= '0' && q[2] <= '9')
751 // GCC precompiled header:
753 && str_startswith(&q[2], "ragma GCC pch_preprocess "))
755 || (q[1] == 'l' && q[2] == 'i' && q[3] == 'n' && q[4] == 'e'
757 && (q == data || q[-1] == '\n')) {
758 // Workarounds for preprocessor linemarker bugs in GCC version 6.
760 if (str_startswith(q, "# 31 \"<command-line>\"\n")) {
761 // Bogus extra line with #31, after the regular #1: Ignore the whole
762 // line, and continue parsing.
763 hash_buffer(hash, p, q - p);
764 while (q < end && *q != '\n') {
770 } else if (str_startswith(q, "# 32 \"<command-line>\" 2\n")) {
771 // Bogus wrong line with #32, instead of regular #1: Replace the line
772 // number with the usual one.
773 hash_buffer(hash, p, q - p);
782 while (q < end && *q != '"' && *q != '\n') {
785 if (q < end && *q == '\n') {
786 // A newline before the quotation mark -> no match.
791 cc_log("Failed to parse included file path");
795 // q points to the beginning of an include file path
796 hash_buffer(hash, p, q - p);
798 while (q < end && *q != '"') {
801 // Look for preprocessor flags, after the "filename".
804 while (r < end && *r != '\n') {
805 if (*r == '3') { // System header.
810 // p and q span the include file path.
811 char *inc_path = x_strndup(p, q - p);
812 if (!has_absolute_include_headers) {
813 has_absolute_include_headers = is_absolute_path(inc_path);
815 inc_path = make_relative_path(inc_path);
817 bool should_hash_inc_path = true;
818 if (!conf->hash_dir) {
819 char *cwd = gnu_getcwd();
820 if (str_startswith(inc_path, cwd) && str_endswith(inc_path, "//")) {
821 // When compiling with -g or similar, GCC adds the absolute path to
826 // If the user has opted out of including the CWD in the hash, don't
827 // hash it. See also how debug_prefix_map is handled.
828 should_hash_inc_path = false;
832 if (should_hash_inc_path) {
833 hash_string(hash, inc_path);
836 remember_include_file(inc_path, hash, system);
837 p = q; // Everything of interest between p and q has been hashed now.
838 } else if (q[0] == '.' && q[1] == 'i' && q[2] == 'n' && q[3] == 'c'
839 && q[4] == 'b' && q[5] == 'i' && q[6] == 'n') {
840 // An assembler .incbin statement (which could be part of inline
841 // assembly) refers to an external file. If the file changes, the hash
842 // should change as well, but finding out what file to hash is too hard
843 // for ccache, so just bail out.
844 cc_log("Found unsupported .incbin directive in source code");
845 stats_update(STATS_UNSUPPORTED_DIRECTIVE);
852 hash_buffer(hash, p, (end - p));
855 // Explicitly check the .gch/.pch/.pth file, Clang does not include any
856 // mention of it in the preprocessed output.
857 if (included_pch_file) {
858 char *path = x_strdup(included_pch_file);
859 path = make_relative_path(path);
860 hash_string(hash, path);
861 remember_include_file(path, hash, false);
867 // Replace absolute paths with relative paths in the provided dependency file.
869 use_relative_paths_in_depfile(const char *depfile)
871 if (str_eq(conf->base_dir, "")) {
872 cc_log("Base dir not set, skip using relative paths");
873 return; // nothing to do
875 if (!has_absolute_include_headers) {
876 cc_log("No absolute path for included files found, skip using relative"
878 return; // nothing to do
882 f = fopen(depfile, "r");
884 cc_log("Cannot open dependency file: %s (%s)", depfile, strerror(errno));
888 char *tmp_file = format("%s.tmp", depfile);
889 FILE *tmpf = create_tmp_file(&tmp_file, "w");
893 while (fgets(buf, sizeof(buf), f) && !ferror(tmpf)) {
895 char *token = strtok_r(buf, " \t", &saveptr);
898 if (is_absolute_path(token) && str_startswith(token, conf->base_dir)) {
899 relpath = make_relative_path(x_strdup(token));
904 if (token != buf) { // This is a dependency file.
907 fputs(relpath, tmpf);
908 if (relpath != token) {
911 token = strtok_r(NULL, " \t", &saveptr);
916 cc_log("Error reading dependency file: %s, skip relative path usage",
922 cc_log("Error writing temporary dependency file: %s, skip relative path"
932 if (x_rename(tmp_file, depfile) != 0) {
933 cc_log("Error renaming dependency file: %s -> %s (%s), skip relative"
934 " path usage", tmp_file, depfile, strerror(errno));
937 cc_log("Renamed dependency file: %s -> %s", tmp_file, depfile);
941 cc_log("Removing temporary dependency file: %s", tmp_file);
947 // Copy or link a file to the cache.
949 put_file_in_cache(const char *source, const char *dest)
951 assert(!conf->read_only);
952 assert(!conf->read_only_direct);
954 bool do_link = conf->hard_link && !conf->compression;
957 int ret = link(source, dest);
959 cc_log("Failed to link %s to %s: %s", source, dest, strerror(errno));
960 cc_log("Falling back to copying");
966 source, dest, conf->compression ? conf->compression_level : 0);
968 cc_log("Failed to copy %s to %s: %s", source, dest, strerror(errno));
969 stats_update(STATS_ERROR);
974 cc_log("Stored in cache: %s -> %s", source, dest);
977 if (x_stat(dest, &st) != 0) {
978 stats_update(STATS_ERROR);
981 stats_update_size(file_size(&st), 1);
984 // Copy or link a file from the cache.
986 get_file_from_cache(const char *source, const char *dest)
989 bool do_link = conf->hard_link && !file_is_compressed(source);
992 ret = link(source, dest);
994 ret = copy_file(source, dest, 0);
998 if (errno == ENOENT || errno == ESTALE) {
999 // Someone removed the file just before we began copying?
1000 cc_log("Cache file %s just disappeared from cache", source);
1001 stats_update(STATS_MISSING);
1003 cc_log("Failed to %s %s to %s: %s",
1004 do_link ? "link" : "copy",
1008 stats_update(STATS_ERROR);
1011 // If there was trouble getting a file from the cached result, wipe the
1012 // whole cached result for consistency.
1013 x_unlink(cached_stderr);
1014 x_unlink(cached_obj);
1015 x_unlink(cached_dep);
1016 x_unlink(cached_dia);
1021 cc_log("Created from cache: %s -> %s", source, dest);
1024 // Send cached stderr, if any, to stderr.
1026 send_cached_stderr(void)
1028 int fd_stderr = open(cached_stderr, O_RDONLY | O_BINARY);
1029 if (fd_stderr != -1) {
1030 copy_fd(fd_stderr, 2);
1035 // Create or update the manifest file.
1036 void update_manifest_file(void)
1038 if (!conf->direct_mode
1041 || conf->read_only_direct) {
1046 size_t old_size = 0; // in bytes
1047 if (stat(manifest_path, &st) == 0) {
1048 old_size = file_size(&st);
1050 if (manifest_put(manifest_path, cached_obj_hash, included_files)) {
1051 cc_log("Added object file hash to %s", manifest_path);
1052 update_mtime(manifest_path);
1053 if (x_stat(manifest_path, &st) == 0) {
1054 stats_update_size(file_size(&st) - old_size, old_size == 0 ? 1 : 0);
1057 cc_log("Failed to add object file hash to %s", manifest_path);
1061 // Run the real compiler and put the result in cache.
1063 to_cache(struct args *args)
1065 char *tmp_stdout = format("%s.tmp.stdout", cached_obj);
1066 int tmp_stdout_fd = create_tmp_fd(&tmp_stdout);
1067 char *tmp_stderr = format("%s.tmp.stderr", cached_obj);
1068 int tmp_stderr_fd = create_tmp_fd(&tmp_stderr);
1071 if (generating_coverage) {
1073 // GCC has some funny rule about max extension length.
1074 if (strlen(get_extension(output_obj)) < 6) {
1075 tmp_aux = remove_extension(output_obj);
1077 tmp_aux = x_strdup(output_obj);
1079 tmp_cov = format("%s.gcno", tmp_aux);
1085 // GCC (at least 4.8 and 4.9) forms the .dwo file name by removing everything
1086 // after (and including) the last "." from the object file name and then
1087 // appending ".dwo".
1088 char *tmp_dwo = NULL;
1089 if (using_split_dwarf) {
1090 char *base_name = remove_extension(output_obj);
1091 tmp_dwo = format("%s.dwo", base_name);
1095 args_add(args, "-o");
1096 args_add(args, output_obj);
1099 args_add(args, "--serialize-diagnostics");
1100 args_add(args, output_dia);
1103 // Turn off DEPENDENCIES_OUTPUT when running cc1, because otherwise it will
1104 // emit a line like this:
1106 // tmp.stdout.vexed.732.o: /home/mbp/.ccache/tmp.stdout.vexed.732.i
1107 x_unsetenv("DEPENDENCIES_OUTPUT");
1109 if (conf->run_second_cpp) {
1110 args_add(args, input_file);
1112 args_add(args, i_tmpfile);
1115 cc_log("Running real compiler");
1117 execute(args->argv, tmp_stdout_fd, tmp_stderr_fd, &compiler_pid);
1121 if (x_stat(tmp_stdout, &st) != 0) {
1122 // The stdout file was removed - cleanup in progress? Better bail out.
1123 stats_update(STATS_MISSING);
1124 tmp_unlink(tmp_stdout);
1125 tmp_unlink(tmp_stderr);
1127 tmp_unlink(tmp_cov);
1130 tmp_unlink(tmp_dwo);
1134 if (st.st_size != 0) {
1135 cc_log("Compiler produced stdout");
1136 stats_update(STATS_STDOUT);
1137 tmp_unlink(tmp_stdout);
1138 tmp_unlink(tmp_stderr);
1140 tmp_unlink(tmp_cov);
1143 tmp_unlink(tmp_dwo);
1147 tmp_unlink(tmp_stdout);
1149 // Merge stderr from the preprocessor (if any) and stderr from the real
1150 // compiler into tmp_stderr.
1152 char *tmp_stderr2 = format("%s.2", tmp_stderr);
1153 if (x_rename(tmp_stderr, tmp_stderr2)) {
1154 cc_log("Failed to rename %s to %s: %s", tmp_stderr, tmp_stderr2,
1159 int fd_cpp_stderr = open(cpp_stderr, O_RDONLY | O_BINARY);
1160 if (fd_cpp_stderr == -1) {
1161 cc_log("Failed opening %s: %s", cpp_stderr, strerror(errno));
1165 int fd_real_stderr = open(tmp_stderr2, O_RDONLY | O_BINARY);
1166 if (fd_real_stderr == -1) {
1167 cc_log("Failed opening %s: %s", tmp_stderr2, strerror(errno));
1172 open(tmp_stderr, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
1173 if (fd_result == -1) {
1174 cc_log("Failed opening %s: %s", tmp_stderr, strerror(errno));
1178 copy_fd(fd_cpp_stderr, fd_result);
1179 copy_fd(fd_real_stderr, fd_result);
1180 close(fd_cpp_stderr);
1181 close(fd_real_stderr);
1183 tmp_unlink(tmp_stderr2);
1188 cc_log("Compiler gave exit status %d", status);
1189 stats_update(STATS_STATUS);
1191 int fd = open(tmp_stderr, O_RDONLY | O_BINARY);
1193 // We can output stderr immediately instead of rerunning the compiler.
1196 tmp_unlink(tmp_stderr);
1201 tmp_unlink(tmp_stderr);
1203 tmp_unlink(tmp_cov);
1206 tmp_unlink(tmp_dwo);
1212 if (stat(output_obj, &st) != 0) {
1213 cc_log("Compiler didn't produce an object file");
1214 stats_update(STATS_NOOUTPUT);
1217 if (st.st_size == 0) {
1218 cc_log("Compiler produced an empty object file");
1219 stats_update(STATS_EMPTYOUTPUT);
1223 if (using_split_dwarf) {
1224 if (stat(tmp_dwo, &st) != 0) {
1225 cc_log("Compiler didn't produce a split dwarf file");
1226 stats_update(STATS_NOOUTPUT);
1229 if (st.st_size == 0) {
1230 cc_log("Compiler produced an empty split dwarf file");
1231 stats_update(STATS_EMPTYOUTPUT);
1236 if (x_stat(tmp_stderr, &st) != 0) {
1237 stats_update(STATS_ERROR);
1240 if (st.st_size > 0) {
1241 if (move_uncompressed_file(
1242 tmp_stderr, cached_stderr,
1243 conf->compression ? conf->compression_level : 0) != 0) {
1244 cc_log("Failed to move %s to %s: %s", tmp_stderr, cached_stderr,
1246 stats_update(STATS_ERROR);
1249 cc_log("Stored in cache: %s", cached_stderr);
1250 if (!conf->compression
1251 // If the file was compressed, obtain the size again:
1252 || x_stat(cached_stderr, &st) == 0) {
1253 stats_update_size(file_size(&st), 1);
1256 tmp_unlink(tmp_stderr);
1257 if (conf->recache) {
1258 // If recaching, we need to remove any previous .stderr.
1259 x_unlink(cached_stderr);
1263 if (generating_coverage) {
1264 // GCC won't generate notes if there is no code.
1265 if (stat(tmp_cov, &st) != 0 && errno == ENOENT) {
1266 FILE *f = fopen(cached_cov, "wb");
1267 cc_log("Creating placeholder: %s", cached_cov);
1269 cc_log("Failed to create %s: %s", cached_cov, strerror(errno));
1270 stats_update(STATS_ERROR);
1274 stats_update_size(0, 1);
1276 put_file_in_cache(tmp_cov, cached_cov);
1281 if (x_stat(output_dia, &st) != 0) {
1282 stats_update(STATS_ERROR);
1285 if (st.st_size > 0) {
1286 put_file_in_cache(output_dia, cached_dia);
1290 put_file_in_cache(output_obj, cached_obj);
1292 if (using_split_dwarf) {
1295 put_file_in_cache(tmp_dwo, cached_dwo);
1298 if (generating_dependencies) {
1299 use_relative_paths_in_depfile(output_dep);
1300 put_file_in_cache(output_dep, cached_dep);
1302 stats_update(STATS_TOCACHE);
1304 // Make sure we have a CACHEDIR.TAG in the cache part of cache_dir. This can
1305 // be done almost anywhere, but we might as well do it near the end as we
1306 // save the stat call if we exit early.
1308 char *first_level_dir = dirname(stats_file);
1309 if (create_cachedirtag(first_level_dir) != 0) {
1310 cc_log("Failed to create %s/CACHEDIR.TAG (%s)\n",
1311 first_level_dir, strerror(errno));
1312 stats_update(STATS_ERROR);
1315 free(first_level_dir);
1317 // Remove any CACHEDIR.TAG on the cache_dir level where it was located in
1318 // previous ccache versions.
1319 if (getpid() % 1000 == 0) {
1320 char *path = format("%s/CACHEDIR.TAG", conf->cache_dir);
1327 send_cached_stderr();
1328 update_manifest_file();
1336 // Find the object file name by running the compiler in preprocessor mode.
1337 // Returns the hash as a heap-allocated hex string.
1338 static struct file_hash *
1339 get_object_name_from_cpp(struct args *args, struct mdfour *hash)
1341 time_of_compilation = time(NULL);
1343 char *path_stderr = format("%s/tmp.cpp_stderr", temp_dir());
1344 int path_stderr_fd = create_tmp_fd(&path_stderr);
1345 add_pending_tmp_file(path_stderr);
1349 if (direct_i_file) {
1350 // We are compiling a .i or .ii file - that means we can skip the cpp stage
1351 // and directly form the correct i_tmpfile.
1352 path_stdout = input_file;
1355 // Run cpp on the input file to obtain the .i.
1357 // Limit the basename to 10 characters in order to cope with filesystem with
1358 // small maximum filename length limits.
1359 char *input_base = basename(input_file);
1360 char *tmp = strchr(input_base, '.');
1364 if (strlen(input_base) > 10) {
1368 path_stdout = format("%s/%s.stdout", temp_dir(), input_base);
1369 int path_stdout_fd = create_tmp_fd(&path_stdout);
1370 add_pending_tmp_file(path_stdout);
1373 args_add(args, "-E");
1374 if (conf->keep_comments_cpp) {
1375 args_add(args, "-C");
1378 args_add(args, input_file);
1379 add_prefix(args, conf->prefix_command_cpp);
1380 cc_log("Running preprocessor");
1381 status = execute(args->argv, path_stdout_fd, path_stderr_fd, &compiler_pid);
1382 args_pop(args, args_added);
1386 cc_log("Preprocessor gave exit status %d", status);
1387 stats_update(STATS_PREPROCESSOR);
1392 // When we are doing the unifying tricks we need to include the input file
1393 // name in the hash to get the warnings right.
1394 hash_delimiter(hash, "unifyfilename");
1395 hash_string(hash, input_file);
1397 hash_delimiter(hash, "unifycpp");
1398 if (unify_hash(hash, path_stdout) != 0) {
1399 stats_update(STATS_ERROR);
1400 cc_log("Failed to unify %s", path_stdout);
1404 hash_delimiter(hash, "cpp");
1405 if (!process_preprocessed_file(hash, path_stdout)) {
1406 stats_update(STATS_ERROR);
1411 hash_delimiter(hash, "cppstderr");
1412 if (!hash_file(hash, path_stderr)) {
1413 fatal("Failed to open %s: %s", path_stderr, strerror(errno));
1416 if (direct_i_file) {
1417 i_tmpfile = input_file;
1419 // i_tmpfile needs the proper cpp_extension for the compiler to do its
1421 i_tmpfile = format("%s.%s", path_stdout, conf->cpp_extension);
1422 x_rename(path_stdout, i_tmpfile);
1423 add_pending_tmp_file(i_tmpfile);
1426 if (conf->run_second_cpp) {
1429 // If we are using the CPP trick, we need to remember this stderr data and
1430 // output it just before the main stderr from the compiler pass.
1431 cpp_stderr = path_stderr;
1432 hash_delimiter(hash, "runsecondcpp");
1433 hash_string(hash, "false");
1436 struct file_hash *result = x_malloc(sizeof(*result));
1437 hash_result_as_bytes(hash, result->hash);
1438 result->size = hash->totalN;
1443 update_cached_result_globals(struct file_hash *hash)
1445 char *object_name = format_hash_as_string(hash->hash, hash->size);
1446 cached_obj_hash = hash;
1447 cached_obj = get_path_in_cache(object_name, ".o");
1448 cached_stderr = get_path_in_cache(object_name, ".stderr");
1449 cached_dep = get_path_in_cache(object_name, ".d");
1450 cached_cov = get_path_in_cache(object_name, ".gcno");
1451 cached_dia = get_path_in_cache(object_name, ".dia");
1453 if (using_split_dwarf) {
1454 cached_dwo = get_path_in_cache(object_name, ".dwo");
1459 stats_file = format("%s/%c/stats", conf->cache_dir, object_name[0]);
1463 // Hash mtime or content of a file, or the output of a command, according to
1464 // the CCACHE_COMPILERCHECK setting.
1466 hash_compiler(struct mdfour *hash, struct stat *st, const char *path,
1469 if (str_eq(conf->compiler_check, "none")) {
1471 } else if (str_eq(conf->compiler_check, "mtime")) {
1472 hash_delimiter(hash, "cc_mtime");
1473 hash_int(hash, st->st_size);
1474 hash_int(hash, st->st_mtime);
1475 } else if (str_startswith(conf->compiler_check, "string:")) {
1476 hash_delimiter(hash, "cc_hash");
1477 hash_string(hash, conf->compiler_check + strlen("string:"));
1478 } else if (str_eq(conf->compiler_check, "content") || !allow_command) {
1479 hash_delimiter(hash, "cc_content");
1480 hash_file(hash, path);
1481 } else { // command string
1482 if (!hash_multicommand_output(
1483 hash, conf->compiler_check, orig_args->argv[0])) {
1484 fatal("Failure running compiler check command: %s", conf->compiler_check);
1489 // Note that these compiler checks are unreliable, so nothing should
1490 // hard-depend on them.
1493 compiler_is_clang(struct args *args)
1495 char *name = basename(args->argv[0]);
1496 bool result = strstr(name, "clang") != NULL;
1502 compiler_is_gcc(struct args *args)
1504 char *name = basename(args->argv[0]);
1505 bool result = strstr(name, "gcc") || strstr(name, "g++");
1510 // Update a hash sum with information common for the direct and preprocessor
1513 calculate_common_hash(struct args *args, struct mdfour *hash)
1515 hash_string(hash, HASH_PREFIX);
1517 // We have to hash the extension, as a .i file isn't treated the same by the
1518 // compiler as a .ii file.
1519 hash_delimiter(hash, "ext");
1520 hash_string(hash, conf->cpp_extension);
1523 const char *ext = strrchr(args->argv[0], '.');
1524 char full_path_win_ext[MAX_PATH + 1] = {0};
1525 add_exe_ext_if_no_to_fullpath(full_path_win_ext, MAX_PATH, ext,
1527 const char *full_path = full_path_win_ext;
1529 const char *full_path = args->argv[0];
1533 if (x_stat(full_path, &st) != 0) {
1534 stats_update(STATS_COMPILER);
1538 // Hash information about the compiler.
1539 hash_compiler(hash, &st, args->argv[0], true);
1541 // Also hash the compiler name as some compilers use hard links and behave
1542 // differently depending on the real name.
1543 hash_delimiter(hash, "cc_name");
1544 char *p = basename(args->argv[0]);
1545 hash_string(hash, p);
1548 // Possibly hash the current working directory.
1549 if (generating_debuginfo && conf->hash_dir) {
1550 char *cwd = gnu_getcwd();
1551 if (debug_prefix_map) {
1552 char *map = debug_prefix_map;
1553 char *sep = strchr(map, '=');
1555 char *old = x_strndup(map, sep - map);
1556 char *new = x_strdup(sep + 1);
1557 cc_log("Relocating debuginfo cwd %s, from %s to %s", cwd, old, new);
1558 if (str_startswith(cwd, old)) {
1559 char *dir = format("%s%s", new, cwd + strlen(old));
1568 hash_delimiter(hash, "cwd");
1569 hash_string(hash, cwd);
1574 // Possibly hash the coverage data file path.
1575 if (generating_coverage && profile_arcs) {
1576 char *dir = dirname(output_obj);
1578 dir = x_strdup(profile_dir);
1580 char *real_dir = x_realpath(dir);
1585 char *base_name = basename(output_obj);
1586 p = remove_extension(base_name);
1588 char *gcda_path = format("%s/%s.gcda", dir, p);
1589 cc_log("Hashing coverage path %s", gcda_path);
1591 hash_delimiter(hash, "gcda");
1592 hash_string(hash, gcda_path);
1597 if (!str_eq(conf->extra_files_to_hash, "")) {
1598 char *p = x_strdup(conf->extra_files_to_hash);
1601 char *saveptr = NULL;
1602 while ((path = strtok_r(q, PATH_DELIM, &saveptr))) {
1603 cc_log("Hashing extra file %s", path);
1604 hash_delimiter(hash, "extrafile");
1605 if (!hash_file(hash, path)) {
1606 stats_update(STATS_BADEXTRAFILE);
1614 // Possibly hash GCC_COLORS (for color diagnostics).
1615 if (compiler_is_gcc(args)) {
1616 const char *gcc_colors = getenv("GCC_COLORS");
1618 hash_delimiter(hash, "gcccolors");
1619 hash_string(hash, gcc_colors);
1624 // Update a hash sum with information specific to the direct and preprocessor
1625 // modes and calculate the object hash. Returns the object hash on success,
1626 // otherwise NULL. Caller frees.
1627 static struct file_hash *
1628 calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode)
1631 hash_delimiter(hash, "manifest version");
1632 hash_int(hash, MANIFEST_VERSION);
1635 // First the arguments.
1636 for (int i = 1; i < args->argc; i++) {
1637 // -L doesn't affect compilation.
1638 if (i < args->argc-1 && str_eq(args->argv[i], "-L")) {
1642 if (str_startswith(args->argv[i], "-L")) {
1646 // -Wl,... doesn't affect compilation.
1647 if (str_startswith(args->argv[i], "-Wl,")) {
1651 // The -fdebug-prefix-map option may be used in combination with
1652 // CCACHE_BASEDIR to reuse results across different directories. Skip it
1654 if (str_startswith(args->argv[i], "-fdebug-prefix-map=")) {
1658 // When using the preprocessor, some arguments don't contribute to the
1659 // hash. The theory is that these arguments will change the output of -E if
1660 // they are going to have any effect at all. For precompiled headers this
1661 // might not be the case.
1662 if (!direct_mode && !output_is_precompiled_header
1663 && !using_precompiled_header) {
1664 if (compopt_affects_cpp(args->argv[i])) {
1668 if (compopt_short(compopt_affects_cpp, args->argv[i])) {
1673 // If we're generating dependencies, we make sure to skip the filename of
1674 // the dependency file, since it doesn't impact the output.
1675 if (generating_dependencies) {
1676 if (str_startswith(args->argv[i], "-Wp,")) {
1677 if (str_startswith(args->argv[i], "-Wp,-MD,")
1678 && !strchr(args->argv[i] + 8, ',')) {
1679 hash_string_length(hash, args->argv[i], 8);
1681 } else if (str_startswith(args->argv[i], "-Wp,-MMD,")
1682 && !strchr(args->argv[i] + 9, ',')) {
1683 hash_string_length(hash, args->argv[i], 9);
1686 } else if (str_startswith(args->argv[i], "-MF")) {
1687 // In either case, hash the "-MF" part.
1688 hash_string_length(hash, args->argv[i], 3);
1690 bool separate_argument = (strlen(args->argv[i]) == 3);
1691 if (separate_argument) {
1692 // Next argument is dependency name, so skip it.
1700 if (str_startswith(args->argv[i], "-specs=")) {
1701 p = args->argv[i] + 7;
1702 } else if (str_startswith(args->argv[i], "--specs=")) {
1703 p = args->argv[i] + 8;
1707 if (p && x_stat(p, &st) == 0) {
1708 // If given an explicit specs file, then hash that file, but don't
1709 // include the path to it in the hash.
1710 hash_delimiter(hash, "specs");
1711 hash_compiler(hash, &st, p, false);
1715 if (str_startswith(args->argv[i], "-fplugin=")
1716 && x_stat(args->argv[i] + 9, &st) == 0) {
1717 hash_delimiter(hash, "plugin");
1718 hash_compiler(hash, &st, args->argv[i] + 9, false);
1722 if (str_eq(args->argv[i], "-Xclang")
1723 && i + 3 < args->argc
1724 && str_eq(args->argv[i+1], "-load")
1725 && str_eq(args->argv[i+2], "-Xclang")
1726 && x_stat(args->argv[i+3], &st) == 0) {
1727 hash_delimiter(hash, "plugin");
1728 hash_compiler(hash, &st, args->argv[i+3], false);
1733 // All other arguments are included in the hash.
1734 hash_delimiter(hash, "arg");
1735 hash_string(hash, args->argv[i]);
1736 if (i + 1 < args->argc && compopt_takes_arg(args->argv[i])) {
1738 hash_delimiter(hash, "arg");
1739 hash_string(hash, args->argv[i]);
1743 // For profile generation (-fprofile-arcs, -fprofile-generate):
1744 // - hash profile directory
1746 // For profile usage (-fprofile-use):
1747 // - hash profile data
1749 // -fbranch-probabilities and -fvpt usage is covered by
1750 // -fprofile-generate/-fprofile-use.
1752 // The profile directory can be specified as an argument to
1753 // -fprofile-generate=, -fprofile-use= or -fprofile-dir=.
1754 if (profile_generate) {
1756 profile_dir = get_cwd();
1758 cc_log("Adding profile directory %s to our hash", profile_dir);
1759 hash_delimiter(hash, "-fprofile-dir");
1760 hash_string(hash, profile_dir);
1764 // Calculate gcda name.
1766 profile_dir = get_cwd();
1768 char *base_name = remove_extension(output_obj);
1769 char *gcda_name = format("%s/%s.gcda", profile_dir, base_name);
1770 cc_log("Adding profile data %s to our hash", gcda_name);
1771 // Add the gcda to our hash.
1772 hash_delimiter(hash, "-fprofile-use");
1773 hash_file(hash, gcda_name);
1778 // Adding -arch to hash since cpp output is affected.
1779 for (size_t i = 0; i < arch_args_size; ++i) {
1780 hash_delimiter(hash, "-arch");
1781 hash_string(hash, arch_args[i]);
1784 struct file_hash *object_hash = NULL;
1786 // Hash environment variables that affect the preprocessor output.
1787 const char *envvars[] = {
1790 "CPLUS_INCLUDE_PATH",
1791 "OBJC_INCLUDE_PATH",
1792 "OBJCPLUS_INCLUDE_PATH", // clang
1795 for (const char **p = envvars; *p; ++p) {
1796 char *v = getenv(*p);
1798 hash_delimiter(hash, *p);
1799 hash_string(hash, v);
1803 if (!(conf->sloppiness & SLOPPY_FILE_MACRO)) {
1804 // The source code file or an include file may contain __FILE__, so make
1805 // sure that the hash is unique for the file name.
1806 hash_delimiter(hash, "inputfile");
1807 hash_string(hash, input_file);
1810 hash_delimiter(hash, "sourcecode");
1811 int result = hash_source_code_file(conf, hash, input_file);
1812 if (result & HASH_SOURCE_CODE_ERROR) {
1815 if (result & HASH_SOURCE_CODE_FOUND_TIME) {
1816 cc_log("Disabling direct mode");
1817 conf->direct_mode = false;
1820 char *manifest_name = hash_result(hash);
1821 manifest_path = get_path_in_cache(manifest_name, ".manifest");
1822 free(manifest_name);
1823 cc_log("Looking for object file hash in %s", manifest_path);
1824 object_hash = manifest_get(conf, manifest_path);
1826 cc_log("Got object file hash from manifest");
1828 cc_log("Did not find object file hash in manifest");
1831 if (arch_args_size == 0) {
1832 object_hash = get_object_name_from_cpp(args, hash);
1833 cc_log("Got object file hash from preprocessor");
1835 args_add(args, "-arch");
1836 for (size_t i = 0; i < arch_args_size; ++i) {
1837 args_add(args, arch_args[i]);
1838 object_hash = get_object_name_from_cpp(args, hash);
1839 cc_log("Got object file hash from preprocessor with -arch %s",
1841 if (i != arch_args_size - 1) {
1848 if (generating_dependencies) {
1849 cc_log("Preprocessor created %s", output_dep);
1856 // Try to return the compile result from cache. If we can return from cache
1857 // then this function exits with the correct status code, otherwise it returns.
1859 from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest)
1861 // The user might be disabling cache hits.
1862 if (conf->recache) {
1867 if (stat(cached_obj, &st) != 0) {
1868 cc_log("Object file %s not in cache", cached_obj);
1872 // Check if the diagnostic file is there.
1873 if (output_dia && stat(cached_dia, &st) != 0) {
1874 cc_log("Diagnostic file %s not in cache", cached_dia);
1878 // Occasionally, e.g. on hard reset, our cache ends up as just filesystem
1879 // meta-data with no content. Catch an easy case of this.
1880 if (st.st_size == 0) {
1881 cc_log("Invalid (empty) object file %s in cache", cached_obj);
1882 x_unlink(cached_obj);
1886 if (using_split_dwarf && !generating_dependencies) {
1891 if (stat(cached_dwo, &st) != 0) {
1892 cc_log("Split dwarf file %s not in cache", cached_dwo);
1895 if (st.st_size == 0) {
1896 cc_log("Invalid (empty) dwo file %s in cache", cached_dwo);
1897 x_unlink(cached_dwo);
1898 x_unlink(cached_obj); // To really invalidate.
1903 // (If mode != FROMCACHE_DIRECT_MODE, the dependency file is created by gcc.)
1904 bool produce_dep_file =
1905 generating_dependencies && mode == FROMCACHE_DIRECT_MODE;
1907 // If the dependency file should be in the cache, check that it is.
1908 if (produce_dep_file && stat(cached_dep, &st) != 0) {
1909 cc_log("Dependency file %s missing in cache", cached_dep);
1913 // Copy object file from cache. Do so also for FissionDwarf file, cached_dwo,
1914 // when -gsplit-dwarf is specified.
1915 if (!str_eq(output_obj, "/dev/null")) {
1916 get_file_from_cache(cached_obj, output_obj);
1917 if (using_split_dwarf) {
1919 get_file_from_cache(cached_dwo, output_dwo);
1922 if (produce_dep_file) {
1923 get_file_from_cache(cached_dep, output_dep);
1925 if (generating_coverage && stat(cached_cov, &st) == 0 && st.st_size > 0) {
1926 // The compiler won't generate notes if there is no code
1927 get_file_from_cache(cached_cov, output_cov);
1930 get_file_from_cache(cached_dia, output_dia);
1933 // Update modification timestamps to save files from LRU cleanup. Also gives
1934 // files a sensible mtime when hard-linking.
1935 update_mtime(cached_obj);
1936 update_mtime(cached_stderr);
1937 if (produce_dep_file) {
1938 update_mtime(cached_dep);
1940 if (generating_coverage) {
1941 update_mtime(cached_cov);
1944 update_mtime(cached_dia);
1947 update_mtime(cached_dwo);
1950 if (generating_dependencies && mode == FROMCACHE_CPP_MODE
1951 && !conf->read_only && !conf->read_only_direct) {
1952 put_file_in_cache(output_dep, cached_dep);
1955 send_cached_stderr();
1957 if (put_object_in_manifest) {
1958 update_manifest_file();
1961 // Log the cache hit.
1963 case FROMCACHE_DIRECT_MODE:
1964 cc_log("Succeeded getting cached result");
1965 stats_update(STATS_CACHEHIT_DIR);
1968 case FROMCACHE_CPP_MODE:
1969 cc_log("Succeeded getting cached result");
1970 stats_update(STATS_CACHEHIT_CPP);
1974 // And exit with the right status code.
1978 // Find the real compiler. We just search the PATH to find an executable of the
1979 // same name that isn't a link to ourselves.
1981 find_compiler(char **argv)
1983 // We might be being invoked like "ccache gcc -c foo.c".
1984 char *base = basename(argv[0]);
1985 if (same_executable_name(base, MYNAME)) {
1986 args_remove_first(orig_args);
1988 if (is_full_path(orig_args->argv[0])) {
1989 // A full path was given.
1992 base = basename(orig_args->argv[0]);
1995 // Support user override of the compiler.
1996 if (!str_eq(conf->compiler, "")) {
1997 base = conf->compiler;
2000 char *compiler = find_executable(base, MYNAME);
2002 stats_update(STATS_COMPILER);
2003 fatal("Could not find compiler \"%s\" in PATH", base);
2005 if (str_eq(compiler, argv[0])) {
2006 fatal("Recursive invocation (the name of the ccache binary must be \"%s\")",
2009 orig_args->argv[0] = compiler;
2013 is_precompiled_header(const char *path)
2015 return str_eq(get_extension(path), ".gch")
2016 || str_eq(get_extension(path), ".pch")
2017 || str_eq(get_extension(path), ".pth");
2021 color_output_possible(void)
2023 const char *term_env = getenv("TERM");
2024 return isatty(STDERR_FILENO) && term_env && strcasecmp(term_env, "DUMB") != 0;
2028 detect_pch(const char *option, const char *arg, bool *found_pch)
2032 // Try to be smart about detecting precompiled headers.
2033 char *pch_file = NULL;
2034 if (str_eq(option, "-include-pch") || str_eq(option, "-include-pth")) {
2035 if (stat(arg, &st) == 0) {
2036 cc_log("Detected use of precompiled header: %s", arg);
2037 pch_file = x_strdup(arg);
2040 char *gchpath = format("%s.gch", arg);
2041 if (stat(gchpath, &st) == 0) {
2042 cc_log("Detected use of precompiled header: %s", gchpath);
2043 pch_file = x_strdup(gchpath);
2045 char *pchpath = format("%s.pch", arg);
2046 if (stat(pchpath, &st) == 0) {
2047 cc_log("Detected use of precompiled header: %s", pchpath);
2048 pch_file = x_strdup(pchpath);
2050 // clang may use pretokenized headers.
2051 char *pthpath = format("%s.pth", arg);
2052 if (stat(pthpath, &st) == 0) {
2053 cc_log("Detected use of pretokenized header: %s", pthpath);
2054 pch_file = x_strdup(pthpath);
2064 if (included_pch_file) {
2065 cc_log("Multiple precompiled headers used: %s and %s\n",
2066 included_pch_file, pch_file);
2067 stats_update(STATS_ARGS);
2070 included_pch_file = pch_file;
2076 // Process the compiler options into options suitable for passing to the
2077 // preprocessor and the real compiler. The preprocessor options don't include
2078 // -E; this is added later. Returns true on success, otherwise false.
2080 cc_process_args(struct args *args, struct args **preprocessor_args,
2081 struct args **compiler_args)
2083 bool found_c_opt = false;
2084 bool found_S_opt = false;
2085 bool found_pch = false;
2086 bool found_fpch_preprocess = false;
2087 const char *explicit_language = NULL; // As specified with -x.
2088 const char *file_language; // As deduced from file extension.
2089 const char *actual_language; // Language to actually use.
2090 const char *input_charset = NULL;
2091 // Is the dependency makefile name overridden with -MF?
2092 bool dependency_filename_specified = false;
2093 // Is the dependency makefile target name specified with -MT or -MQ?
2094 bool dependency_target_specified = false;
2095 // expanded_args is a copy of the original arguments given to the compiler
2096 // but with arguments from @file and similar constructs expanded. It's only
2097 // used as a temporary data structure to loop over.
2098 struct args *expanded_args = args_copy(args);
2099 // stripped_args essentially contains all original arguments except those
2100 // that only should be passed to the preprocessor (if run_second_cpp is
2101 // false) and except dependency options (like -MD and friends).
2102 struct args *stripped_args = args_init(0, NULL);
2103 // cpp_args contains arguments that were not added to stripped_args, i.e.
2104 // those that should only be passed to the preprocessor if run_second_cpp is
2105 // false. If run_second_cpp is true, they will be passed to the compiler as
2107 struct args *cpp_args = args_init(0, NULL);
2108 // dep_args contains dependency options like -MD. They only passed to the
2109 // preprocessor, never to the compiler.
2110 struct args *dep_args = args_init(0, NULL);
2112 bool found_color_diagnostics = false;
2113 int debug_level = 0;
2114 const char *debug_argument = NULL;
2116 int argc = expanded_args->argc;
2117 char **argv = expanded_args->argv;
2118 args_add(stripped_args, argv[0]);
2121 for (int i = 1; i < argc; i++) {
2122 // The user knows best: just swallow the next arg.
2123 if (str_eq(argv[i], "--ccache-skip")) {
2126 cc_log("--ccache-skip lacks an argument");
2130 args_add(stripped_args, argv[i]);
2134 // Special case for -E.
2135 if (str_eq(argv[i], "-E")) {
2136 stats_update(STATS_PREPROCESSING);
2141 // Handle "@file" argument.
2142 if (str_startswith(argv[i], "@") || str_startswith(argv[i], "-@")) {
2143 char *argpath = argv[i] + 1;
2145 if (argpath[-1] == '-') {
2148 struct args *file_args = args_init_from_gcc_atfile(argpath);
2150 cc_log("Couldn't read arg file %s", argpath);
2151 stats_update(STATS_ARGS);
2156 args_insert(expanded_args, i, file_args, true);
2157 argc = expanded_args->argc;
2158 argv = expanded_args->argv;
2163 // Handle cuda "-optf" and "--options-file" argument.
2164 if (str_eq(argv[i], "-optf") || str_eq(argv[i], "--options-file")) {
2166 cc_log("Expected argument after -optf/--options-file");
2167 stats_update(STATS_ARGS);
2173 // Argument is a comma-separated list of files.
2174 char *str_start = argv[i];
2175 char *str_end = strchr(str_start, ',');
2179 str_end = str_start + strlen(str_start);
2184 struct args *file_args = args_init_from_gcc_atfile(str_start);
2186 cc_log("Couldn't read cuda options file %s", str_start);
2187 stats_update(STATS_ARGS);
2192 int new_index = file_args->argc + index;
2193 args_insert(expanded_args, index, file_args, false);
2195 str_start = str_end;
2196 str_end = strchr(str_start, ',');
2199 argc = expanded_args->argc;
2200 argv = expanded_args->argv;
2204 // These are always too hard.
2205 if (compopt_too_hard(argv[i]) || str_startswith(argv[i], "-fdump-")) {
2206 cc_log("Compiler option %s is unsupported", argv[i]);
2207 stats_update(STATS_UNSUPPORTED_OPTION);
2212 // These are too hard in direct mode.
2213 if (conf->direct_mode && compopt_too_hard_for_direct_mode(argv[i])) {
2214 cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
2215 conf->direct_mode = false;
2218 // -Xarch_* options are too hard.
2219 if (str_startswith(argv[i], "-Xarch_")) {
2220 cc_log("Unsupported compiler option :%s", argv[i]);
2221 stats_update(STATS_UNSUPPORTED_OPTION);
2226 // Handle -arch options.
2227 if (str_eq(argv[i], "-arch")) {
2228 if (arch_args_size == MAX_ARCH_ARGS - 1) {
2229 cc_log("Too many -arch compiler options; ccache supports at most %d",
2231 stats_update(STATS_UNSUPPORTED_OPTION);
2237 arch_args[arch_args_size] = x_strdup(argv[i]); // It will leak.
2239 if (arch_args_size == 2) {
2240 conf->run_second_cpp = true;
2245 if (str_eq(argv[i], "-fpch-preprocess")
2246 || str_eq(argv[i], "-emit-pch")
2247 || str_eq(argv[i], "-emit-pth")) {
2248 found_fpch_preprocess = true;
2252 if (str_eq(argv[i], "-c")) {
2257 // -S changes the default extension.
2258 if (str_eq(argv[i], "-S")) {
2259 args_add(stripped_args, argv[i]);
2264 // Special handling for -x: remember the last specified language before the
2265 // input file and strip all -x options from the arguments.
2266 if (str_eq(argv[i], "-x")) {
2268 cc_log("Missing argument to %s", argv[i]);
2269 stats_update(STATS_ARGS);
2274 explicit_language = argv[i+1];
2279 if (str_startswith(argv[i], "-x")) {
2281 explicit_language = &argv[i][2];
2286 // We need to work out where the output was meant to go.
2287 if (str_eq(argv[i], "-o")) {
2289 cc_log("Missing argument to %s", argv[i]);
2290 stats_update(STATS_ARGS);
2294 output_obj = make_relative_path(x_strdup(argv[i+1]));
2299 // Alternate form of -o with no space.
2300 if (str_startswith(argv[i], "-o")) {
2301 output_obj = make_relative_path(x_strdup(&argv[i][2]));
2305 if (str_eq(argv[i], "-gsplit-dwarf")) {
2306 cc_log("Enabling caching of dwarf files since -gsplit-dwarf is used");
2307 using_split_dwarf = true;
2308 args_add(stripped_args, argv[i]);
2311 if (str_startswith(argv[i], "-fdebug-prefix-map=")) {
2312 debug_prefix_map = x_strdup(argv[i] + 19);
2313 args_add(stripped_args, argv[i]);
2317 // Debugging is handled specially, so that we know if we can strip line
2319 if (str_startswith(argv[i], "-g")) {
2320 const char *pLevel = argv[i] + 2;
2321 if (str_startswith(argv[i], "-ggdb")) {
2322 pLevel = argv[i] + 5;
2323 } else if (str_startswith(argv[i], "-gstabs")) {
2324 pLevel = argv[i] + 7;
2325 } else if (str_startswith(argv[i], "-gcoff")) {
2326 pLevel = argv[i] + 6;
2327 } else if (str_startswith(argv[i], "-gxcoff")) {
2328 pLevel = argv[i] + 7;
2329 } else if (str_startswith(argv[i], "-gvms")) {
2330 pLevel = argv[i] + 5;
2333 // Deduce level from argument, default is 2.
2334 int foundlevel = -1;
2335 if (pLevel[0] == '\0') {
2337 } else if (pLevel[0] >= '0' && pLevel[0] <= '9') {
2338 foundlevel = atoi(pLevel);
2341 if (foundlevel >= 0) {
2342 debug_level = foundlevel;
2343 debug_argument = argv[i];
2348 // These options require special handling, because they behave differently
2349 // with gcc -E, when the output file is not specified.
2350 if (str_eq(argv[i], "-MD") || str_eq(argv[i], "-MMD")) {
2351 generating_dependencies = true;
2352 args_add(dep_args, argv[i]);
2355 if (str_startswith(argv[i], "-MF")) {
2356 dependency_filename_specified = true;
2360 bool separate_argument = (strlen(argv[i]) == 3);
2361 if (separate_argument) {
2363 if (i >= argc - 1) {
2364 cc_log("Missing argument to %s", argv[i]);
2365 stats_update(STATS_ARGS);
2375 output_dep = make_relative_path(x_strdup(arg));
2376 // Keep the format of the args the same.
2377 if (separate_argument) {
2378 args_add(dep_args, "-MF");
2379 args_add(dep_args, output_dep);
2381 char *option = format("-MF%s", output_dep);
2382 args_add(dep_args, option);
2387 if (str_startswith(argv[i], "-MQ") || str_startswith(argv[i], "-MT")) {
2388 dependency_target_specified = true;
2391 if (strlen(argv[i]) == 3) {
2392 // -MQ arg or -MT arg
2393 if (i >= argc - 1) {
2394 cc_log("Missing argument to %s", argv[i]);
2395 stats_update(STATS_ARGS);
2399 args_add(dep_args, argv[i]);
2400 relpath = make_relative_path(x_strdup(argv[i + 1]));
2401 args_add(dep_args, relpath);
2405 char *arg_opt = x_strndup(argv[i], 3);
2406 relpath = make_relative_path(x_strdup(argv[i] + 3));
2407 char *option = format("%s%s", arg_opt, relpath);
2408 args_add(dep_args, option);
2415 if (str_eq(argv[i], "-fprofile-arcs")) {
2416 profile_arcs = true;
2417 args_add(stripped_args, argv[i]);
2420 if (str_eq(argv[i], "-ftest-coverage")) {
2421 generating_coverage = true;
2422 args_add(stripped_args, argv[i]);
2425 if (str_eq(argv[i], "--coverage") // = -fprofile-arcs -ftest-coverage
2426 || str_eq(argv[i], "-coverage")) { // Undocumented but still works.
2427 profile_arcs = true;
2428 generating_coverage = true;
2429 args_add(stripped_args, argv[i]);
2432 if (str_startswith(argv[i], "-fprofile-dir=")) {
2433 profile_dir = x_strdup(argv[i] + 14);
2434 args_add(stripped_args, argv[i]);
2437 if (str_startswith(argv[i], "--sysroot=")) {
2438 char *relpath = make_relative_path(x_strdup(argv[i] + 10));
2439 char *option = format("--sysroot=%s", relpath);
2440 args_add(stripped_args, option);
2445 if (str_startswith(argv[i], "-Wp,")) {
2446 if (str_eq(argv[i], "-Wp,-P")
2447 || strstr(argv[i], ",-P,")
2448 || str_endswith(argv[i], ",-P")) {
2449 // -P removes preprocessor information in such a way that the object
2450 // file from compiling the preprocessed file will not be equal to the
2451 // object file produced when compiling without ccache.
2452 cc_log("Too hard option -Wp,-P detected");
2453 stats_update(STATS_UNSUPPORTED_OPTION);
2455 } else if (str_startswith(argv[i], "-Wp,-MD,")
2456 && !strchr(argv[i] + 8, ',')) {
2457 generating_dependencies = true;
2458 dependency_filename_specified = true;
2460 output_dep = make_relative_path(x_strdup(argv[i] + 8));
2461 args_add(dep_args, argv[i]);
2463 } else if (str_startswith(argv[i], "-Wp,-MMD,")
2464 && !strchr(argv[i] + 9, ',')) {
2465 generating_dependencies = true;
2466 dependency_filename_specified = true;
2468 output_dep = make_relative_path(x_strdup(argv[i] + 9));
2469 args_add(dep_args, argv[i]);
2471 } else if (str_startswith(argv[i], "-Wp,-D")
2472 && !strchr(argv[i] + 6, ',')) {
2473 // Treat it like -D.
2474 args_add(cpp_args, argv[i] + 4);
2476 } else if (str_eq(argv[i], "-Wp,-MP")
2477 || (strlen(argv[i]) > 8
2478 && str_startswith(argv[i], "-Wp,-M")
2479 && argv[i][7] == ','
2480 && (argv[i][6] == 'F'
2481 || argv[i][6] == 'Q'
2482 || argv[i][6] == 'T')
2483 && !strchr(argv[i] + 8, ','))) {
2484 // TODO: Make argument to MF/MQ/MT relative.
2485 args_add(dep_args, argv[i]);
2487 } else if (conf->direct_mode) {
2488 // -Wp, can be used to pass too hard options to the preprocessor.
2489 // Hence, disable direct mode.
2490 cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
2491 conf->direct_mode = false;
2494 // Any other -Wp,* arguments are only relevant for the preprocessor.
2495 args_add(cpp_args, argv[i]);
2498 if (str_eq(argv[i], "-MP")) {
2499 args_add(dep_args, argv[i]);
2503 // Input charset needs to be handled specially.
2504 if (str_startswith(argv[i], "-finput-charset=")) {
2505 input_charset = argv[i];
2509 if (str_eq(argv[i], "--serialize-diagnostics")) {
2510 if (i >= argc - 1) {
2511 cc_log("Missing argument to %s", argv[i]);
2512 stats_update(STATS_ARGS);
2516 output_dia = make_relative_path(x_strdup(argv[i+1]));
2521 if (str_startswith(argv[i], "-fprofile-")) {
2522 char *arg = x_strdup(argv[i]);
2523 const char *arg_profile_dir = strchr(argv[i], '=');
2524 if (arg_profile_dir) {
2525 // Convert to absolute path.
2526 char *dir = x_realpath(arg_profile_dir + 1);
2528 // Directory doesn't exist.
2529 dir = x_strdup(arg_profile_dir + 1);
2532 // We can get a better hit rate by using the real path here.
2534 char *option = x_strndup(argv[i], arg_profile_dir - argv[i]);
2535 arg = format("%s=%s", option, dir);
2536 cc_log("Rewriting %s to %s", argv[i], arg);
2541 bool supported_profile_option = false;
2542 if (str_startswith(argv[i], "-fprofile-generate")
2543 || str_eq(argv[i], "-fprofile-arcs")) {
2544 profile_generate = true;
2545 supported_profile_option = true;
2546 } else if (str_startswith(argv[i], "-fprofile-use")
2547 || str_eq(argv[i], "-fbranch-probabilities")) {
2549 supported_profile_option = true;
2550 } else if (str_eq(argv[i], "-fprofile-dir")) {
2551 supported_profile_option = true;
2554 if (supported_profile_option) {
2555 args_add(stripped_args, arg);
2558 // If the profile directory has already been set, give up... Hard to
2559 // know what the user means, and what the compiler will do.
2560 if (arg_profile_dir && profile_dir) {
2561 cc_log("Profile directory already set; giving up");
2564 } else if (arg_profile_dir) {
2565 cc_log("Setting profile directory to %s", profile_dir);
2566 profile_dir = x_strdup(arg_profile_dir);
2570 cc_log("Unknown profile option: %s", argv[i]);
2574 if (str_eq(argv[i], "-fcolor-diagnostics")
2575 || str_eq(argv[i], "-fno-color-diagnostics")
2576 || str_eq(argv[i], "-fdiagnostics-color")
2577 || str_eq(argv[i], "-fdiagnostics-color=always")
2578 || str_eq(argv[i], "-fno-diagnostics-color")
2579 || str_eq(argv[i], "-fdiagnostics-color=never")) {
2580 args_add(stripped_args, argv[i]);
2581 found_color_diagnostics = true;
2584 if (str_eq(argv[i], "-fdiagnostics-color=auto")) {
2585 if (color_output_possible()) {
2586 // Output is redirected, so color output must be forced.
2587 args_add(stripped_args, "-fdiagnostics-color=always");
2588 cc_log("Automatically forcing colors");
2590 args_add(stripped_args, argv[i]);
2592 found_color_diagnostics = true;
2596 // Options taking an argument that we may want to rewrite to relative paths
2597 // to get better hit rate. A secondary effect is that paths in the standard
2598 // error output produced by the compiler will be normalized.
2599 if (compopt_takes_path(argv[i])) {
2601 cc_log("Missing argument to %s", argv[i]);
2602 stats_update(STATS_ARGS);
2607 if (!detect_pch(argv[i], argv[i+1], &found_pch)) {
2612 char *relpath = make_relative_path(x_strdup(argv[i+1]));
2613 if (compopt_affects_cpp(argv[i])) {
2614 args_add(cpp_args, argv[i]);
2615 args_add(cpp_args, relpath);
2617 args_add(stripped_args, argv[i]);
2618 args_add(stripped_args, relpath);
2626 // Same as above but options with concatenated argument beginning with a
2628 if (argv[i][0] == '-') {
2629 char *slash_pos = strchr(argv[i], '/');
2631 char *option = x_strndup(argv[i], slash_pos - argv[i]);
2632 if (compopt_takes_concat_arg(option) && compopt_takes_path(option)) {
2633 char *relpath = make_relative_path(x_strdup(slash_pos));
2634 char *new_option = format("%s%s", option, relpath);
2635 if (compopt_affects_cpp(option)) {
2636 args_add(cpp_args, new_option);
2638 args_add(stripped_args, new_option);
2650 // Options that take an argument.
2651 if (compopt_takes_arg(argv[i])) {
2653 cc_log("Missing argument to %s", argv[i]);
2654 stats_update(STATS_ARGS);
2659 if (compopt_affects_cpp(argv[i])) {
2660 args_add(cpp_args, argv[i]);
2661 args_add(cpp_args, argv[i+1]);
2663 args_add(stripped_args, argv[i]);
2664 args_add(stripped_args, argv[i+1]);
2672 if (argv[i][0] == '-') {
2673 if (compopt_affects_cpp(argv[i])
2674 || compopt_prefix_affects_cpp(argv[i])) {
2675 args_add(cpp_args, argv[i]);
2677 args_add(stripped_args, argv[i]);
2682 // If an argument isn't a plain file then assume its an option, not an
2683 // input file. This allows us to cope better with unusual compiler options.
2685 if (stat(argv[i], &st) != 0 || !S_ISREG(st.st_mode)) {
2686 cc_log("%s is not a regular file, not considering as input file",
2688 args_add(stripped_args, argv[i]);
2693 if (language_for_file(argv[i])) {
2694 cc_log("Multiple input files: %s and %s", input_file, argv[i]);
2695 stats_update(STATS_MULTIPLE);
2696 } else if (!found_c_opt) {
2697 cc_log("Called for link with %s", argv[i]);
2698 if (strstr(argv[i], "conftest.")) {
2699 stats_update(STATS_CONFTEST);
2701 stats_update(STATS_LINK);
2704 cc_log("Unsupported source extension: %s", argv[i]);
2705 stats_update(STATS_SOURCELANG);
2711 // The source code file path gets put into the notes.
2712 if (generating_coverage) {
2713 input_file = x_strdup(argv[i]);
2717 if (is_symlink(argv[i])) {
2718 // Don't rewrite source file path if it's a symlink since
2719 // make_relative_path resolves symlinks using realpath(3) and this leads
2720 // to potentially choosing incorrect relative header files. See the
2721 // "symlink to source file" test.
2722 input_file = x_strdup(argv[i]);
2724 // Rewrite to relative to increase hit rate.
2725 input_file = make_relative_path(x_strdup(argv[i]));
2729 if (debug_level > 0) {
2730 generating_debuginfo = true;
2731 args_add(stripped_args, debug_argument);
2733 cc_log("%s used; disabling unify mode", debug_argument);
2734 conf->unify = false;
2736 if (debug_level >= 3 && !conf->run_second_cpp) {
2737 cc_log("%s used; not compiling preprocessed code", debug_argument);
2738 conf->run_second_cpp = true;
2743 // Even if -gsplit-dwarf is given, the .dwo file is not generated when -S
2745 using_split_dwarf = false;
2746 cc_log("Disabling caching of dwarf files since -S is used");
2750 cc_log("No input file found");
2751 stats_update(STATS_NOINPUT);
2756 if (found_pch || found_fpch_preprocess) {
2757 using_precompiled_header = true;
2758 if (!(conf->sloppiness & SLOPPY_TIME_MACROS)) {
2759 cc_log("You have to specify \"time_macros\" sloppiness when using"
2760 " precompiled headers to get direct hits");
2761 cc_log("Disabling direct mode");
2762 stats_update(STATS_CANTUSEPCH);
2768 if (explicit_language && str_eq(explicit_language, "none")) {
2769 explicit_language = NULL;
2771 file_language = language_for_file(input_file);
2772 if (explicit_language) {
2773 if (!language_is_supported(explicit_language)) {
2774 cc_log("Unsupported language: %s", explicit_language);
2775 stats_update(STATS_SOURCELANG);
2779 actual_language = explicit_language;
2781 actual_language = file_language;
2784 output_is_precompiled_header =
2785 actual_language && strstr(actual_language, "-header");
2787 if (output_is_precompiled_header
2788 && !(conf->sloppiness & SLOPPY_PCH_DEFINES)) {
2789 cc_log("You have to specify \"pch_defines,time_macros\" sloppiness when"
2790 " creating precompiled headers");
2791 stats_update(STATS_CANTUSEPCH);
2796 if (!found_c_opt && !found_S_opt) {
2797 if (output_is_precompiled_header) {
2798 args_add(stripped_args, "-c");
2800 cc_log("No -c option found");
2801 // I find that having a separate statistic for autoconf tests is useful,
2802 // as they are the dominant form of "called for link" in many cases.
2803 if (strstr(input_file, "conftest.")) {
2804 stats_update(STATS_CONFTEST);
2806 stats_update(STATS_LINK);
2813 if (!actual_language) {
2814 cc_log("Unsupported source extension: %s", input_file);
2815 stats_update(STATS_SOURCELANG);
2820 direct_i_file = language_is_preprocessed(actual_language);
2822 if (output_is_precompiled_header && !conf->run_second_cpp) {
2823 // It doesn't work to create the .gch from preprocessed source.
2824 cc_log("Creating precompiled header; not compiling preprocessed code");
2825 conf->run_second_cpp = true;
2828 if (str_eq(conf->cpp_extension, "")) {
2829 const char *p_language = p_language_for_language(actual_language);
2830 free(conf->cpp_extension);
2831 conf->cpp_extension = x_strdup(extension_for_language(p_language) + 1);
2834 // Don't try to second guess the compilers heuristics for stdout handling.
2835 if (output_obj && str_eq(output_obj, "-")) {
2836 stats_update(STATS_OUTSTDOUT);
2837 cc_log("Output file is -");
2843 if (output_is_precompiled_header) {
2844 output_obj = format("%s.gch", input_file);
2846 output_obj = basename(input_file);
2847 char *p = strrchr(output_obj, '.');
2849 cc_log("Badly formed object filename");
2850 stats_update(STATS_ARGS);
2854 p[1] = found_S_opt ? 's' : 'o';
2859 if (using_split_dwarf) {
2860 char *p = strrchr(output_obj, '.');
2862 cc_log("Badly formed object filename");
2863 stats_update(STATS_ARGS);
2868 char *base_name = remove_extension(output_obj);
2869 output_dwo = format("%s.dwo", base_name);
2873 // Cope with -o /dev/null.
2875 if (!str_eq(output_obj, "/dev/null")
2876 && stat(output_obj, &st) == 0
2877 && !S_ISREG(st.st_mode)) {
2878 cc_log("Not a regular file: %s", output_obj);
2879 stats_update(STATS_DEVICE);
2884 // Some options shouldn't be passed to the real compiler when it compiles
2885 // preprocessed code:
2887 // -finput-charset=XXX (otherwise conversion happens twice)
2888 // -x XXX (otherwise the wrong language is selected)
2889 if (input_charset) {
2890 args_add(cpp_args, input_charset);
2893 args_add(cpp_args, "-fpch-preprocess");
2895 if (explicit_language) {
2896 args_add(cpp_args, "-x");
2897 args_add(cpp_args, explicit_language);
2900 // Since output is redirected, compilers will not color their output by
2901 // default, so force it explicitly if it would be otherwise done.
2902 if (!found_color_diagnostics && color_output_possible()) {
2903 if (compiler_is_clang(args)) {
2904 if (!str_eq(actual_language, "assembler")) {
2905 args_add(stripped_args, "-fcolor-diagnostics");
2906 cc_log("Automatically enabling colors");
2908 } else if (compiler_is_gcc(args)) {
2909 // GCC has it since 4.9, but that'd require detecting what GCC version is
2910 // used for the actual compile. However it requires also GCC_COLORS to be
2911 // set (and not empty), so use that for detecting if GCC would use
2913 if (getenv("GCC_COLORS") && getenv("GCC_COLORS")[0] != '\0') {
2914 args_add(stripped_args, "-fdiagnostics-color");
2915 cc_log("Automatically enabling colors");
2920 // Add flags for dependency generation only to the preprocessor command line.
2921 if (generating_dependencies) {
2922 if (!dependency_filename_specified) {
2923 char *base_name = remove_extension(output_obj);
2924 char *default_depfile_name = format("%s.d", base_name);
2926 args_add(dep_args, "-MF");
2927 args_add(dep_args, default_depfile_name);
2928 output_dep = make_relative_path(x_strdup(default_depfile_name));
2931 if (!dependency_target_specified) {
2932 args_add(dep_args, "-MQ");
2933 args_add(dep_args, output_obj);
2936 if (generating_coverage) {
2937 char *base_name = remove_extension(output_obj);
2938 char *default_covfile_name = format("%s.gcno", base_name);
2940 output_cov = make_relative_path(x_strdup(default_covfile_name));
2943 *compiler_args = args_copy(stripped_args);
2944 if (conf->run_second_cpp) {
2945 args_extend(*compiler_args, cpp_args);
2946 } else if (explicit_language) {
2947 // Workaround for a bug in Apple's patched distcc -- it doesn't properly
2948 // reset the language specified with -x, so if -x is given, we have to
2949 // specify the preprocessed language explicitly.
2950 args_add(*compiler_args, "-x");
2951 args_add(*compiler_args, p_language_for_language(explicit_language));
2955 args_add(*compiler_args, "-c");
2958 for (size_t i = 0; i < arch_args_size; ++i) {
2959 args_add(*compiler_args, "-arch");
2960 args_add(*compiler_args, arch_args[i]);
2963 // Only pass dependency arguments to the preprocesor since Intel's C++
2964 // compiler doesn't produce a correct .d file when compiling preprocessed
2966 args_extend(cpp_args, dep_args);
2968 *preprocessor_args = args_copy(stripped_args);
2969 args_extend(*preprocessor_args, cpp_args);
2972 args_free(expanded_args);
2973 args_free(stripped_args);
2974 args_free(dep_args);
2975 args_free(cpp_args);
2980 create_initial_config_file(struct conf *conf, const char *path)
2982 if (create_parent_dirs(path) != 0) {
2988 char *stats_dir = format("%s/0", conf->cache_dir);
2990 if (stat(stats_dir, &st) == 0) {
2991 stats_get_obsolete_limits(stats_dir, &max_files, &max_size);
2992 // STATS_MAXFILES and STATS_MAXSIZE was stored for each top directory.
2997 max_size = conf->max_size;
3001 FILE *f = fopen(path, "w");
3005 if (max_files != 0) {
3006 fprintf(f, "max_files = %u\n", max_files);
3007 conf->max_files = max_files;
3009 if (max_size != 0) {
3010 char *size = format_parsable_size_with_suffix(max_size);
3011 fprintf(f, "max_size = %s\n", size);
3013 conf->max_size = max_size;
3018 // Read config file(s), populate variables, create configuration file in cache
3019 // directory if missing, etc.
3024 conf = conf_create();
3028 char *p = getenv("CCACHE_CONFIGPATH");
3030 primary_config_path = x_strdup(p);
3032 secondary_config_path = format("%s/ccache.conf", TO_STRING(SYSCONFDIR));
3033 if (!conf_read(conf, secondary_config_path, &errmsg)) {
3034 if (stat(secondary_config_path, &st) == 0) {
3035 fatal("%s", errmsg);
3037 // Missing config file in SYSCONFDIR is OK.
3041 if (str_eq(conf->cache_dir, "")) {
3042 fatal("configuration setting \"cache_dir\" must not be the empty string");
3044 if ((p = getenv("CCACHE_DIR"))) {
3045 free(conf->cache_dir);
3046 conf->cache_dir = strdup(p);
3048 if (str_eq(conf->cache_dir, "")) {
3049 fatal("CCACHE_DIR must not be the empty string");
3052 primary_config_path = format("%s/ccache.conf", conf->cache_dir);
3055 bool should_create_initial_config = false;
3056 if (!conf_read(conf, primary_config_path, &errmsg)) {
3057 if (stat(primary_config_path, &st) == 0) {
3058 fatal("%s", errmsg);
3060 should_create_initial_config = true;
3063 if (!conf_update_from_environment(conf, &errmsg)) {
3064 fatal("%s", errmsg);
3067 if (conf->disable) {
3068 should_create_initial_config = false;
3071 if (should_create_initial_config) {
3072 create_initial_config_file(conf, primary_config_path);
3076 exitfn_add_nullary(stats_flush);
3077 exitfn_add_nullary(clean_up_pending_tmp_files);
3079 cc_log("=== CCACHE %s STARTED =========================================",
3082 if (conf->umask != UINT_MAX) {
3087 // Reset the global state. Used by the test suite.
3091 conf_free(conf); conf = NULL;
3092 free(primary_config_path); primary_config_path = NULL;
3093 free(secondary_config_path); secondary_config_path = NULL;
3094 free(current_working_dir); current_working_dir = NULL;
3095 free(debug_prefix_map); debug_prefix_map = NULL;
3096 free(profile_dir); profile_dir = NULL;
3097 free(included_pch_file); included_pch_file = NULL;
3098 args_free(orig_args); orig_args = NULL;
3099 free(input_file); input_file = NULL;
3100 free(output_obj); output_obj = NULL;
3101 free(output_dwo); output_dwo = NULL;
3102 free(output_dep); output_dep = NULL;
3103 free(output_cov); output_cov = NULL;
3104 free(output_dia); output_dia = NULL;
3105 free(cached_obj_hash); cached_obj_hash = NULL;
3106 free(cached_obj); cached_obj = NULL;
3107 free(cached_dwo); cached_dwo = NULL;
3108 free(cached_stderr); cached_stderr = NULL;
3109 free(cached_dep); cached_dep = NULL;
3110 free(cached_cov); cached_cov = NULL;
3111 free(cached_dia); cached_dia = NULL;
3112 free(manifest_path); manifest_path = NULL;
3113 time_of_compilation = 0;
3114 for (size_t i = 0; i < ignore_headers_len; i++) {
3115 free(ignore_headers[i]);
3116 ignore_headers[i] = NULL;
3118 free(ignore_headers); ignore_headers = NULL;
3119 ignore_headers_len = 0;
3120 if (included_files) {
3121 hashtable_destroy(included_files, 1); included_files = NULL;
3123 has_absolute_include_headers = false;
3124 generating_debuginfo = false;
3125 generating_dependencies = false;
3126 generating_coverage = false;
3127 profile_arcs = false;
3128 free(profile_dir); profile_dir = NULL;
3130 direct_i_file = false;
3131 free(cpp_stderr); cpp_stderr = NULL;
3132 free(stats_file); stats_file = NULL;
3133 output_is_precompiled_header = false;
3135 conf = conf_create();
3136 using_split_dwarf = false;
3139 // Make a copy of stderr that will not be cached, so things like distcc can
3140 // send networking errors to it.
3142 setup_uncached_err(void)
3144 int uncached_fd = dup(2);
3145 if (uncached_fd == -1) {
3146 cc_log("dup(2) failed: %s", strerror(errno));
3150 // Leak a pointer to the environment.
3151 char *buf = format("UNCACHED_ERR_FD=%d", uncached_fd);
3152 if (putenv(buf) == -1) {
3153 cc_log("putenv failed: %s", strerror(errno));
3159 configuration_logger(const char *descr, const char *origin, void *context)
3162 cc_bulklog("Config: (%s) %s", origin, descr);
3165 // The main ccache driver function.
3167 ccache(int argc, char *argv[])
3170 set_up_signal_handlers();
3173 orig_args = args_init(argc, argv);
3176 find_compiler(argv);
3178 if (str_eq(conf->temporary_dir, "")) {
3179 clean_up_internal_tempdir();
3182 if (!str_eq(conf->log_file, "")) {
3183 conf_print_items(conf, configuration_logger, NULL);
3186 if (conf->disable) {
3187 cc_log("ccache is disabled");
3191 setup_uncached_err();
3193 cc_log_argv("Command line: ", argv);
3194 cc_log("Hostname: %s", get_hostname());
3195 cc_log("Working directory: %s", get_current_working_dir());
3198 cc_log("Direct mode disabled because unify mode is enabled");
3199 conf->direct_mode = false;
3202 conf->limit_multiple = MIN(MAX(conf->limit_multiple, 0.0), 1.0);
3204 // Arguments (except -E) to send to the preprocessor.
3205 struct args *preprocessor_args;
3206 // Arguments to send to the real compiler.
3207 struct args *compiler_args;
3208 if (!cc_process_args(orig_args, &preprocessor_args, &compiler_args)) {
3212 cc_log("Source file: %s", input_file);
3213 if (generating_dependencies) {
3214 cc_log("Dependency file: %s", output_dep);
3216 if (generating_coverage) {
3217 cc_log("Coverage file: %s", output_cov);
3220 cc_log("Diagnostic file: %s", output_dia);
3223 if (using_split_dwarf) {
3224 if (!generating_dependencies) {
3228 assert(!output_dwo);
3232 cc_log("Split dwarf file: %s", output_dwo);
3235 cc_log("Object file: %s", output_obj);
3237 struct mdfour common_hash;
3238 hash_start(&common_hash);
3239 calculate_common_hash(preprocessor_args, &common_hash);
3241 // Try to find the hash using the manifest.
3242 struct mdfour direct_hash = common_hash;
3243 bool put_object_in_manifest = false;
3244 struct file_hash *object_hash = NULL;
3245 struct file_hash *object_hash_from_manifest = NULL;
3246 if (conf->direct_mode) {
3247 cc_log("Trying direct lookup");
3248 object_hash = calculate_object_hash(preprocessor_args, &direct_hash, 1);
3250 update_cached_result_globals(object_hash);
3252 // If we can return from cache at this point then do so.
3253 from_cache(FROMCACHE_DIRECT_MODE, 0);
3255 // Wasn't able to return from cache at this point. However, the object
3256 // was already found in manifest, so don't readd it later.
3257 put_object_in_manifest = false;
3259 object_hash_from_manifest = object_hash;
3261 // Add object to manifest later.
3262 put_object_in_manifest = true;
3266 if (conf->read_only_direct) {
3267 cc_log("Read-only direct mode; running real compiler");
3271 // Find the hash using the preprocessed output. Also updates included_files.
3272 struct mdfour cpp_hash = common_hash;
3273 object_hash = calculate_object_hash(preprocessor_args, &cpp_hash, 0);
3275 fatal("internal error: object hash from cpp returned NULL");
3277 update_cached_result_globals(object_hash);
3279 if (object_hash_from_manifest
3280 && !file_hashes_equal(object_hash_from_manifest, object_hash)) {
3281 // The hash from manifest differs from the hash of the preprocessor output.
3282 // This could be because:
3284 // - The preprocessor produces different output for the same input (not
3286 // - There's a bug in ccache (maybe incorrect handling of compiler
3288 // - The user has used a different CCACHE_BASEDIR (most likely).
3290 // The best thing here would probably be to remove the hash entry from the
3291 // manifest. For now, we use a simpler method: just remove the manifest
3293 cc_log("Hash from manifest doesn't match preprocessor output");
3294 cc_log("Likely reason: different CCACHE_BASEDIRs used");
3295 cc_log("Removing manifest as a safety measure");
3296 x_unlink(manifest_path);
3298 put_object_in_manifest = true;
3301 // If we can return from cache at this point then do.
3302 from_cache(FROMCACHE_CPP_MODE, put_object_in_manifest);
3304 if (conf->read_only) {
3305 cc_log("Read-only mode; running real compiler");
3309 add_prefix(compiler_args, conf->prefix_command);
3311 // Run real compiler, sending output to cache.
3312 to_cache(compiler_args);
3318 configuration_printer(const char *descr, const char *origin, void *context)
3321 fprintf(context, "(%s) %s\n", origin, descr);
3324 // The main program when not doing a compile.
3326 ccache_main_options(int argc, char *argv[])
3331 static const struct option options[] = {
3332 {"cleanup", no_argument, 0, 'c'},
3333 {"clear", no_argument, 0, 'C'},
3334 {"dump-manifest", required_argument, 0, DUMP_MANIFEST},
3335 {"help", no_argument, 0, 'h'},
3336 {"max-files", required_argument, 0, 'F'},
3337 {"max-size", required_argument, 0, 'M'},
3338 {"set-config", required_argument, 0, 'o'},
3339 {"print-config", no_argument, 0, 'p'},
3340 {"show-stats", no_argument, 0, 's'},
3341 {"version", no_argument, 0, 'V'},
3342 {"zero-stats", no_argument, 0, 'z'},
3347 while ((c = getopt_long(argc, argv, "cChF:M:o:psVz", options, NULL)) != -1) {
3350 manifest_dump(optarg, stdout);
3353 case 'c': // --cleanup
3356 printf("Cleaned cache\n");
3359 case 'C': // --clear
3362 printf("Cleared cache\n");
3366 fputs(USAGE_TEXT, stdout);
3369 case 'F': // --max-files
3373 if (conf_set_value_in_file(primary_config_path, "max_files", optarg,
3375 unsigned files = atoi(optarg);
3377 printf("Unset cache file limit\n");
3379 printf("Set cache file limit to %u\n", files);
3382 fatal("could not set cache file limit: %s", errmsg);
3387 case 'M': // --max-size
3391 if (!parse_size_with_suffix(optarg, &size)) {
3392 fatal("invalid size: %s", optarg);
3395 if (conf_set_value_in_file(primary_config_path, "max_size", optarg,
3398 printf("Unset cache size limit\n");
3400 char *s = format_human_readable_size(size);
3401 printf("Set cache size limit to %s\n", s);
3405 fatal("could not set cache size limit: %s", errmsg);
3410 case 'o': // --set-config
3413 char *p = strchr(optarg, '=');
3415 fatal("missing equal sign in \"%s\"", optarg);
3417 char *key = x_strndup(optarg, p - optarg);
3418 char *value = p + 1;
3420 if (!conf_set_value_in_file(primary_config_path, key, value, &errmsg)) {
3421 fatal("%s", errmsg);
3427 case 'p': // --print-config
3429 conf_print_items(conf, configuration_printer, stdout);
3432 case 's': // --show-stats
3434 stats_summary(conf);
3437 case 'V': // --version
3438 fprintf(stdout, VERSION_TEXT, CCACHE_VERSION);
3441 case 'z': // --zero-stats
3444 printf("Statistics cleared\n");
3448 fputs(USAGE_TEXT, stderr);
3457 ccache_main(int argc, char *argv[])
3459 // Check if we are being invoked as "ccache".
3460 char *program_name = basename(argv[0]);
3461 if (same_executable_name(program_name, MYNAME)) {
3463 fputs(USAGE_TEXT, stderr);
3466 // If the first argument isn't an option, then assume we are being passed a
3467 // compiler name and options.
3468 if (argv[1][0] == '-') {
3469 return ccache_main_options(argc, argv);