1 // ccache -- a fast C/C++ compiler cache
3 // Copyright (C) 2002-2007 Andrew Tridgell
4 // Copyright (C) 2009-2019 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"
28 #include "hashtable.h"
29 #include "hashtable_itr.h"
35 #define STRINGIFY(x) #x
36 #define TO_STRING(x) STRINGIFY(x)
38 // Global variables used by other compilation units.
39 extern struct conf *conf;
40 extern char *primary_config_path;
41 extern char *secondary_config_path;
42 extern char *current_working_dir;
43 extern char *stats_file;
44 extern unsigned lock_staleness_limit;
46 static const char VERSION_TEXT[] =
47 MYNAME " version %s\n"
49 "Copyright (C) 2002-2007 Andrew Tridgell\n"
50 "Copyright (C) 2009-2019 Joel Rosdahl\n"
52 "This program is free software; you can redistribute it and/or modify it under\n"
53 "the terms of the GNU General Public License as published by the Free Software\n"
54 "Foundation; either version 3 of the License, or (at your option) any later\n"
57 static const char USAGE_TEXT[] =
59 " " MYNAME " [options]\n"
60 " " MYNAME " compiler [compiler options]\n"
61 " compiler [compiler options] (via symbolic link)\n"
64 " -c, --cleanup delete old files and recalculate size counters\n"
65 " (normally not needed as this is done automatically)\n"
66 " -C, --clear clear the cache completely (except configuration)\n"
67 " -F, --max-files=N set maximum number of files in cache to N (use 0 for\n"
69 " -k, --get-config=K get the value of the configuration key K\n"
70 " -M, --max-size=SIZE set maximum size of cache to SIZE (use 0 for no\n"
71 " limit); available suffixes: k, M, G, T (decimal) and\n"
72 " Ki, Mi, Gi, Ti (binary); default suffix: G\n"
73 " -o, --set-config=K=V set configuration key K to value V\n"
74 " -p, --print-config print current configuration options\n"
75 " -s, --show-stats show statistics summary\n"
76 " -z, --zero-stats zero statistics counters\n"
78 " -h, --help print this help text\n"
79 " -V, --version print version and copyright information\n"
81 "See also <https://ccache.samba.org>.\n";
83 // Global configuration data.
84 struct conf *conf = NULL;
86 // Where to write configuration changes.
87 char *primary_config_path = NULL;
89 // Secondary, read-only configuration file (if any).
90 char *secondary_config_path = NULL;
92 // Current working directory taken from $PWD, or getcwd() if $PWD is bad.
93 char *current_working_dir = NULL;
95 // The original argument list.
96 static struct args *orig_args;
99 static char *input_file;
101 // The output file being compiled to.
102 static char *output_obj;
104 // The path to the dependency file (implicit or specified with -MF).
105 static char *output_dep;
107 // The path to the coverage file (implicit when using -ftest-coverage).
108 static char *output_cov;
110 // The path to the stack usage (implicit when using -fstack-usage).
111 static char *output_su;
113 // Diagnostic generation information (clang). Contains pathname if not NULL.
114 static char *output_dia;
116 // Split dwarf information (GCC 4.8 and up). Contains pathname if not NULL.
117 static char *output_dwo;
119 // Language to use for the compilation target (see language.c).
120 static const char *actual_language;
122 // Array for storing -arch options.
123 #define MAX_ARCH_ARGS 10
124 static size_t arch_args_size = 0;
125 static char *arch_args[MAX_ARCH_ARGS] = {NULL};
127 // Name (represented as a struct file_hash) of the file containing the cached
129 static struct file_hash *cached_obj_hash;
131 // Full path to the file containing the cached object code
132 // (cachedir/a/b/cdef[...]-size.o).
133 static char *cached_obj;
135 // Full path to the file containing the standard error output
136 // (cachedir/a/b/cdef[...]-size.stderr).
137 static char *cached_stderr;
139 // Full path to the file containing the dependency information
140 // (cachedir/a/b/cdef[...]-size.d).
141 static char *cached_dep;
143 // Full path to the file containing the coverage information
144 // (cachedir/a/b/cdef[...]-size.gcno).
145 static char *cached_cov;
147 // Full path to the file containing the stack usage
148 // (cachedir/a/b/cdef[...]-size.su).
149 static char *cached_su;
151 // Full path to the file containing the diagnostic information (for clang)
152 // (cachedir/a/b/cdef[...]-size.dia).
153 static char *cached_dia;
155 // Full path to the file containing the split dwarf (for GCC 4.8 and above)
156 // (cachedir/a/b/cdef[...]-size.dwo).
158 // Contains NULL if -gsplit-dwarf is not given.
159 static char *cached_dwo;
161 // Full path to the file containing the manifest
162 // (cachedir/a/b/cdef[...]-size.manifest).
163 static char *manifest_path;
165 // Time of compilation. Used to see if include files have changed after
167 time_t time_of_compilation;
169 // Files included by the preprocessor and their hashes/sizes. Key: file path.
170 // Value: struct file_hash.
171 static struct hashtable *included_files = NULL;
173 // Uses absolute path for some include files.
174 static bool has_absolute_include_headers = false;
176 // List of headers to ignore.
177 static char **ignore_headers;
179 // Size of headers to ignore list.
180 static size_t ignore_headers_len;
182 // Is the compiler being asked to output debug info?
183 static bool generating_debuginfo;
185 // Is the compiler being asked to output dependencies?
186 static bool generating_dependencies;
188 // Is the compiler being asked to output coverage?
189 static bool generating_coverage;
191 // Is the compiler being asked to output stack usage?
192 static bool generating_stackusage;
194 // Us the compiler being asked to generate diagnostics
195 // (--serialize-diagnostics)?
196 static bool generating_diagnostics;
198 // Is the compiler being asked to separate dwarf debug info into a separate
199 // file (-gsplit-dwarf)"?
200 static bool using_split_dwarf;
202 // Relocating debuginfo in the format old=new.
203 static char **debug_prefix_maps = NULL;
205 // Size of debug_prefix_maps list.
206 static size_t debug_prefix_maps_len = 0;
208 // Is the compiler being asked to output coverage data (.gcda) at runtime?
209 static bool profile_arcs;
211 // Name of the custom profile directory (default: object dirname).
212 static char *profile_dir;
214 // The name of the temporary preprocessed file.
215 static char *i_tmpfile;
217 // Are we compiling a .i or .ii file directly?
218 static bool direct_i_file;
220 // The name of the cpp stderr file.
221 static char *cpp_stderr;
223 // Full path to the statistics file in the subdirectory where the cached result
224 // belongs (<cache_dir>/<x>/stats).
225 char *stats_file = NULL;
227 // Whether the output is a precompiled header.
228 bool output_is_precompiled_header = false;
230 // Compiler guessing is currently only based on the compiler name, so nothing
231 // should hard-depend on it if possible.
232 enum guessed_compiler guessed_compiler = GUESSED_UNKNOWN;
234 // Profile generation / usage information.
235 static char *profile_dir = NULL;
236 static bool profile_use = false;
237 static bool profile_generate = false;
239 // Sanitize blacklist
240 static char *sanitize_blacklist = NULL;
242 // Whether we are using a precompiled header (either via -include, #include or
243 // clang's -include-pch or -include-pth).
244 static bool using_precompiled_header = false;
246 // The .gch/.pch/.pth file used for compilation.
247 static char *included_pch_file = NULL;
249 // How long (in microseconds) to wait before breaking a stale lock.
250 unsigned lock_staleness_limit = 2000000;
252 enum fromcache_call_mode {
253 FROMCACHE_DIRECT_MODE,
257 struct pending_tmp_file {
259 struct pending_tmp_file *next;
262 // Temporary files to remove at program exit.
263 static struct pending_tmp_file *pending_tmp_files = NULL;
266 static sigset_t fatal_signal_set;
268 // PID of currently executing compiler that we have started, if any. 0 means no
269 // ongoing compilation.
270 static pid_t compiler_pid = 0;
273 // This is a string that identifies the current "version" of the hash sum
274 // computed by ccache. If, for any reason, we want to force the hash sum to be
275 // different for the same input in a new ccache version, we can just change
276 // this string. A typical example would be if the format of one of the files
277 // stored in the cache changes in a backwards-incompatible way.
278 static const char HASH_PREFIX[] = "3";
281 add_prefix(struct args *args, char *prefix_command)
283 if (str_eq(prefix_command, "")) {
287 struct args *prefix = args_init(0, NULL);
288 char *e = x_strdup(prefix_command);
289 char *saveptr = NULL;
290 for (char *tok = strtok_r(e, " ", &saveptr);
292 tok = strtok_r(NULL, " ", &saveptr)) {
295 p = find_executable(tok, MYNAME);
297 fatal("%s: %s", tok, strerror(errno));
305 cc_log("Using command-line prefix %s", prefix_command);
306 for (int i = prefix->argc; i != 0; i--) {
307 args_add_prefix(args, prefix->argv[i-1]);
313 static void failed(void) ATTR_NORETURN;
315 // Something went badly wrong - just execute the real compiler.
321 args_strip(orig_args, "--ccache-");
322 add_prefix(orig_args, conf->prefix_command);
324 cc_log("Failed; falling back to running the real compiler");
325 cc_log_argv("Executing ", orig_args->argv);
327 execv(orig_args->argv[0], orig_args->argv);
328 fatal("execv of %s failed: %s", orig_args->argv[0], strerror(errno));
334 static char *path = NULL;
336 return path; // Memoize
338 path = conf->temporary_dir;
339 if (str_eq(path, "")) {
340 path = format("%s/tmp", conf->cache_dir);
349 sigprocmask(SIG_BLOCK, &fatal_signal_set, NULL);
354 unblock_signals(void)
359 sigprocmask(SIG_SETMASK, &empty, NULL);
364 add_pending_tmp_file(const char *path)
367 struct pending_tmp_file *e = x_malloc(sizeof(*e));
368 e->path = x_strdup(path);
369 e->next = pending_tmp_files;
370 pending_tmp_files = e;
375 do_clean_up_pending_tmp_files(void)
377 struct pending_tmp_file *p = pending_tmp_files;
379 // Can't call tmp_unlink here since its cc_log calls aren't signal safe.
382 // Leak p->path and p here because clean_up_pending_tmp_files needs to be
388 clean_up_pending_tmp_files(void)
391 do_clean_up_pending_tmp_files();
397 signal_handler(int signum)
399 // Unregister handler for this signal so that we can send the signal to
400 // ourselves at the end of the handler.
401 signal(signum, SIG_DFL);
403 // If ccache was killed explicitly, then bring the compiler subprocess (if
404 // any) with us as well.
405 if (signum == SIGTERM
407 && waitpid(compiler_pid, NULL, WNOHANG) == 0) {
408 kill(compiler_pid, signum);
411 do_clean_up_pending_tmp_files();
413 if (compiler_pid != 0) {
414 // Wait for compiler subprocess to exit before we snuff it.
415 waitpid(compiler_pid, NULL, 0);
418 // Resend signal to ourselves to exit properly after returning from the
420 kill(getpid(), signum);
424 register_signal_handler(int signum)
426 struct sigaction act;
427 memset(&act, 0, sizeof(act));
428 act.sa_handler = signal_handler;
429 act.sa_mask = fatal_signal_set;
431 act.sa_flags = SA_RESTART;
433 sigaction(signum, &act, NULL);
437 set_up_signal_handlers(void)
439 sigemptyset(&fatal_signal_set);
440 sigaddset(&fatal_signal_set, SIGINT);
441 sigaddset(&fatal_signal_set, SIGTERM);
443 sigaddset(&fatal_signal_set, SIGHUP);
446 sigaddset(&fatal_signal_set, SIGQUIT);
449 register_signal_handler(SIGINT);
450 register_signal_handler(SIGTERM);
452 register_signal_handler(SIGHUP);
455 register_signal_handler(SIGQUIT);
461 clean_up_internal_tempdir(void)
463 time_t now = time(NULL);
465 if (x_stat(conf->cache_dir, &st) != 0 || st.st_mtime + 3600 >= now) {
466 // No cleanup needed.
470 update_mtime(conf->cache_dir);
472 DIR *dir = opendir(temp_dir());
477 struct dirent *entry;
478 while ((entry = readdir(dir))) {
479 if (str_eq(entry->d_name, ".") || str_eq(entry->d_name, "..")) {
483 char *path = format("%s/%s", temp_dir(), entry->d_name);
484 if (x_lstat(path, &st) == 0 && st.st_mtime + 3600 < now) {
494 fclose_exitfn(void *context)
496 fclose((FILE *)context);
500 dump_log_buffer_exitfn(void *context)
506 char *path = format("%s.ccache-log", (const char *)context);
507 cc_dump_log_buffer(path);
512 init_hash_debug(struct hash *hash, const char *obj_path, char type,
513 const char *section_name, FILE *debug_text_file)
519 char *path = format("%s.ccache-input-%c", obj_path, type);
520 FILE *debug_binary_file = fopen(path, "wb");
521 hash_enable_debug(hash, section_name, debug_binary_file, debug_text_file);
523 exitfn_add(fclose_exitfn, debug_binary_file);
526 static enum guessed_compiler
527 guess_compiler(const char *path)
529 char *name = basename(path);
530 enum guessed_compiler result = GUESSED_UNKNOWN;
531 if (strstr(name, "clang")) {
532 result = GUESSED_CLANG;
533 } else if (strstr(name, "gcc") || strstr(name, "g++")) {
534 result = GUESSED_GCC;
535 } else if (strstr(name, "nvcc")) {
536 result = GUESSED_NVCC;
537 } else if (str_eq(name, "pump") || str_eq(name, "distcc-pump")) {
538 result = GUESSED_PUMP;
545 get_current_working_dir(void)
547 if (!current_working_dir) {
548 char *cwd = get_cwd();
550 current_working_dir = x_realpath(cwd);
553 if (!current_working_dir) {
554 cc_log("Unable to determine current working directory: %s",
559 return current_working_dir;
562 // Transform a name to a full path into the cache directory, creating needed
563 // sublevels if needed. Caller frees.
565 get_path_in_cache(const char *name, const char *suffix)
567 char *path = x_strdup(conf->cache_dir);
568 for (unsigned i = 0; i < conf->cache_dir_levels; ++i) {
569 char *p = format("%s/%c", path, name[i]);
575 format("%s/%s%s", path, name + conf->cache_dir_levels, suffix);
580 // This function hashes an include file and stores the path and hash in the
581 // global included_files variable. If the include file is a PCH, cpp_hash is
582 // also updated. Takes over ownership of path.
584 remember_include_file(char *path, struct hash *cpp_hash, bool system)
586 struct hash *fhash = NULL;
588 size_t path_len = strlen(path);
589 if (path_len >= 2 && (path[0] == '<' && path[path_len - 1] == '>')) {
590 // Typically <built-in> or <command-line>.
594 if (str_eq(path, input_file)) {
595 // Don't remember the input file.
599 if (system && (conf->sloppiness & SLOPPY_NO_SYSTEM_HEADERS)) {
600 // Don't remember this system header.
604 if (hashtable_search(included_files, path)) {
605 // Already known include file.
610 // stat fails on directories on win32.
611 DWORD attributes = GetFileAttributes(path);
612 if (attributes != INVALID_FILE_ATTRIBUTES &&
613 attributes & FILE_ATTRIBUTE_DIRECTORY) {
619 if (x_stat(path, &st) != 0) {
622 if (S_ISDIR(st.st_mode)) {
623 // Ignore directory, typically $PWD.
626 if (!S_ISREG(st.st_mode)) {
627 // Device, pipe, socket or other strange creature.
628 cc_log("Non-regular include file %s", path);
632 // Canonicalize path for comparison; clang uses ./header.h.
633 char *canonical = path;
634 size_t canonical_len = path_len;
635 if (canonical[0] == '.' && canonical[1] == '/') {
640 for (size_t i = 0; i < ignore_headers_len; i++) {
641 char *ignore = ignore_headers[i];
642 size_t ignore_len = strlen(ignore);
643 if (ignore_len > canonical_len) {
646 if (strncmp(canonical, ignore, ignore_len) == 0
647 && (ignore[ignore_len-1] == DIR_DELIM_CH
648 || canonical[ignore_len] == DIR_DELIM_CH
649 || canonical[ignore_len] == '\0')) {
654 // The comparison using >= is intentional, due to a possible race between
655 // starting compilation and writing the include file. See also the notes
656 // under "Performance" in doc/MANUAL.adoc.
657 if (!(conf->sloppiness & SLOPPY_INCLUDE_FILE_MTIME)
658 && st.st_mtime >= time_of_compilation) {
659 cc_log("Include file %s too new", path);
663 // The same >= logic as above applies to the change time of the file.
664 if (!(conf->sloppiness & SLOPPY_INCLUDE_FILE_CTIME)
665 && st.st_ctime >= time_of_compilation) {
666 cc_log("Include file %s ctime too new", path);
670 // Let's hash the include file content.
673 bool is_pch = is_precompiled_header(path);
675 bool using_pch_sum = false;
676 if (conf->pch_external_checksum) {
677 // hash pch.sum instead of pch when it exists
678 // to prevent hashing a very large .pch file every time
679 char *pch_sum_path = format("%s.sum", path);
680 if (x_stat(pch_sum_path, &st) == 0) {
681 char *old_path = path;
683 pch_sum_path = old_path;
684 using_pch_sum = true;
685 cc_log("Using pch.sum file %s", path);
690 if (!hash_file(fhash, path)) {
693 hash_delimiter(cpp_hash, using_pch_sum ? "pch_sum_hash" : "pch_hash");
694 char *pch_hash_result = hash_result(fhash);
695 hash_string(cpp_hash, pch_hash_result);
696 free(pch_hash_result);
699 if (conf->direct_mode) {
700 if (!is_pch) { // else: the file has already been hashed.
703 if (st.st_size > 0) {
704 if (!read_file(path, st.st_size, &source, &size)) {
708 source = x_strdup("");
712 int result = hash_source_code_string(conf, fhash, source, size, path);
714 if (result & HASH_SOURCE_CODE_ERROR
715 || result & HASH_SOURCE_CODE_FOUND_TIME) {
720 struct file_hash *h = x_malloc(sizeof(*h));
721 hash_result_as_bytes(fhash, h->hash);
722 h->size = hash_input_size(fhash);
723 hashtable_insert(included_files, path, h);
724 path = NULL; // Ownership transferred to included_files.
730 if (conf->direct_mode) {
731 cc_log("Disabling direct mode");
732 conf->direct_mode = false;
740 // Make a relative path from current working directory to path if path is under
741 // the base directory. Takes over ownership of path. Caller frees.
743 make_relative_path(char *path)
745 if (str_eq(conf->base_dir, "") || !str_startswith(path, conf->base_dir)) {
750 if (path[0] == '/') {
751 path++; // Skip leading slash.
755 // x_realpath only works for existing paths, so if path doesn't exist, try
756 // dirname(path) and assemble the path afterwards. We only bother to try
757 // canonicalizing one of these two paths since a compiler path argument
758 // typically only makes sense if path or dirname(path) exists.
759 char *path_suffix = NULL;
761 if (stat(path, &st) != 0) {
762 // path doesn't exist.
763 char *dir = dirname(path);
764 if (stat(dir, &st) != 0) {
765 // And neither does its parent directory, so no action to take.
770 path_suffix = basename(path);
772 path = dirname(path);
776 char *canon_path = x_realpath(path);
779 char *relpath = get_relative_path(get_current_working_dir(), canon_path);
782 path = format("%s/%s", relpath, path_suffix);
790 // path doesn't exist, so leave it as it is.
796 // This function reads and hashes a file. While doing this, it also does these
799 // - Makes include file paths for which the base directory is a prefix relative
800 // when computing the hash sum.
801 // - Stores the paths and hashes of included files in the global variable
804 process_preprocessed_file(struct hash *hash, const char *path, bool pump)
808 if (!read_file(path, 0, &data, &size)) {
812 ignore_headers = NULL;
813 ignore_headers_len = 0;
814 if (!str_eq(conf->ignore_headers_in_manifest, "")) {
815 char *header, *p, *q, *saveptr = NULL;
816 p = x_strdup(conf->ignore_headers_in_manifest);
818 while ((header = strtok_r(q, PATH_DELIM, &saveptr))) {
819 ignore_headers = x_realloc(ignore_headers,
820 (ignore_headers_len+1) * sizeof(char *));
821 ignore_headers[ignore_headers_len++] = x_strdup(header);
827 if (!included_files) {
828 included_files = create_hashtable(1000, hash_from_string, strings_equal);
831 char *cwd = gnu_getcwd();
833 // Bytes between p and q are pending to be hashed.
836 char *end = data + size;
838 // There must be at least 7 characters (# 1 "x") left to potentially find an
839 // include file path.
840 while (q < end - 7) {
841 // Check if we look at a line containing the file name of an included file.
842 // At least the following formats exist (where N is a positive integer):
848 // #pragma GCC pch_preprocess "file"
859 // Note that there may be other lines starting with '#' left after
860 // preprocessing as well, for instance "# pragma".
863 && ((q[1] == ' ' && q[2] >= '0' && q[2] <= '9')
864 // GCC precompiled header:
866 && str_startswith(&q[2], "ragma GCC pch_preprocess "))
868 || (q[1] == 'l' && q[2] == 'i' && q[3] == 'n' && q[4] == 'e'
870 && (q == data || q[-1] == '\n')) {
871 // Workarounds for preprocessor linemarker bugs in GCC version 6.
873 if (str_startswith(q, "# 31 \"<command-line>\"\n")) {
874 // Bogus extra line with #31, after the regular #1: Ignore the whole
875 // line, and continue parsing.
876 hash_string_buffer(hash, p, q - p);
877 while (q < end && *q != '\n') {
883 } else if (str_startswith(q, "# 32 \"<command-line>\" 2\n")) {
884 // Bogus wrong line with #32, instead of regular #1: Replace the line
885 // number with the usual one.
886 hash_string_buffer(hash, p, q - p);
895 while (q < end && *q != '"' && *q != '\n') {
898 if (q < end && *q == '\n') {
899 // A newline before the quotation mark -> no match.
904 cc_log("Failed to parse included file path");
909 // q points to the beginning of an include file path
910 hash_string_buffer(hash, p, q - p);
912 while (q < end && *q != '"') {
915 // Look for preprocessor flags, after the "filename".
918 while (r < end && *r != '\n') {
919 if (*r == '3') { // System header.
924 // p and q span the include file path.
925 char *inc_path = x_strndup(p, q - p);
926 if (!has_absolute_include_headers) {
927 has_absolute_include_headers = is_absolute_path(inc_path);
929 inc_path = make_relative_path(inc_path);
931 bool should_hash_inc_path = true;
932 if (!conf->hash_dir) {
933 if (str_startswith(inc_path, cwd) && str_endswith(inc_path, "//")) {
934 // When compiling with -g or similar, GCC adds the absolute path to
939 // If the user has opted out of including the CWD in the hash, don't
940 // hash it. See also how debug_prefix_map is handled.
941 should_hash_inc_path = false;
944 if (should_hash_inc_path) {
945 hash_string_buffer(hash, inc_path, strlen(inc_path));
948 remember_include_file(inc_path, hash, system);
949 p = q; // Everything of interest between p and q has been hashed now.
950 } else if (q[0] == '.' && q[1] == 'i' && q[2] == 'n' && q[3] == 'c'
951 && q[4] == 'b' && q[5] == 'i' && q[6] == 'n') {
952 // An assembler .inc bin (without the space) statement, which could be
953 // part of inline assembly, refers to an external file. If the file
954 // changes, the hash should change as well, but finding out what file to
955 // hash is too hard for ccache, so just bail out.
956 cc_log("Found unsupported .inc" "bin directive in source code");
957 stats_update(STATS_UNSUPPORTED_DIRECTIVE);
959 } else if (pump && strncmp(q, "_________", 9) == 0) {
960 // Unfortunately the distcc-pump wrapper outputs standard output lines:
961 // __________Using distcc-pump from /usr/bin
962 // __________Using # distcc servers in pump mode
963 // __________Shutting down distcc-pump include server
964 while (q < end && *q != '\n') {
977 hash_string_buffer(hash, p, (end - p));
981 // Explicitly check the .gch/.pch/.pth file, Clang does not include any
982 // mention of it in the preprocessed output.
983 if (included_pch_file) {
984 char *pch_path = x_strdup(included_pch_file);
985 pch_path = make_relative_path(pch_path);
986 hash_string(hash, pch_path);
987 remember_include_file(pch_path, hash, false);
993 // Replace absolute paths with relative paths in the provided dependency file.
995 use_relative_paths_in_depfile(const char *depfile)
997 if (str_eq(conf->base_dir, "")) {
998 cc_log("Base dir not set, skip using relative paths");
999 return; // nothing to do
1001 if (!has_absolute_include_headers) {
1002 cc_log("No absolute path for included files found, skip using relative"
1004 return; // nothing to do
1008 f = fopen(depfile, "r");
1010 cc_log("Cannot open dependency file: %s (%s)", depfile, strerror(errno));
1014 char *tmp_file = format("%s.tmp", depfile);
1015 FILE *tmpf = create_tmp_file(&tmp_file, "w");
1017 bool result = false;
1019 while (fgets(buf, sizeof(buf), f) && !ferror(tmpf)) {
1021 char *token = strtok_r(buf, " \t", &saveptr);
1024 if (is_absolute_path(token) && str_startswith(token, conf->base_dir)) {
1025 relpath = make_relative_path(x_strdup(token));
1030 if (token != buf) { // This is a dependency file.
1033 fputs(relpath, tmpf);
1034 if (relpath != token) {
1037 token = strtok_r(NULL, " \t", &saveptr);
1042 cc_log("Error reading dependency file: %s, skip relative path usage",
1048 cc_log("Error writing temporary dependency file: %s, skip relative path"
1049 " usage", tmp_file);
1058 if (x_rename(tmp_file, depfile) != 0) {
1059 cc_log("Error renaming dependency file: %s -> %s (%s), skip relative"
1060 " path usage", tmp_file, depfile, strerror(errno));
1063 cc_log("Renamed dependency file: %s -> %s", tmp_file, depfile);
1067 cc_log("Removing temporary dependency file: %s", tmp_file);
1073 // Helper method for copy_file_to_cache and move_file_to_cache_same_fs.
1075 do_copy_or_move_file_to_cache(const char *source, const char *dest, bool copy)
1077 assert(!conf->read_only);
1078 assert(!conf->read_only_direct);
1080 struct stat orig_dest_st;
1081 bool orig_dest_existed = stat(dest, &orig_dest_st) == 0;
1082 int compression_level = conf->compression ? conf->compression_level : 0;
1083 bool do_move = !copy && !conf->compression;
1084 bool do_link = copy && conf->hard_link && !conf->compression;
1087 move_uncompressed_file(source, dest, compression_level);
1091 int ret = link(source, dest);
1093 cc_log("Failed to link %s to %s: %s", source, dest, strerror(errno));
1094 cc_log("Falling back to copying");
1099 int ret = copy_file(source, dest, compression_level);
1101 cc_log("Failed to copy %s to %s: %s", source, dest, strerror(errno));
1102 stats_update(STATS_ERROR);
1108 if (!copy && conf->compression) {
1109 // We fell back to copying since dest should be compressed, so clean up.
1113 cc_log("Stored in cache: %s -> %s (%s)",
1116 do_move ? "moved" : (do_link ? "linked" : "copied"));
1119 if (x_stat(dest, &st) != 0) {
1120 stats_update(STATS_ERROR);
1124 file_size(&st) - (orig_dest_existed ? file_size(&orig_dest_st) : 0),
1125 orig_dest_existed ? 0 : 1);
1128 // Copy a file into the cache.
1130 // dest must be a path in the cache (see get_path_in_cache). source does not
1131 // have to be on the same file system as dest.
1133 // An attempt will be made to hard link source to dest if conf->hard_link is
1134 // true and conf->compression is false, otherwise copy. dest will be compressed
1135 // if conf->compression is true.
1137 copy_file_to_cache(const char *source, const char *dest)
1139 do_copy_or_move_file_to_cache(source, dest, true);
1142 // Move a file into the cache.
1144 // dest must be a path in the cache (see get_path_in_cache). source must be on
1145 // the same file system as dest. dest will be compressed if conf->compression
1148 move_file_to_cache_same_fs(const char *source, const char *dest)
1150 do_copy_or_move_file_to_cache(source, dest, false);
1153 // Copy or link a file from the cache.
1155 get_file_from_cache(const char *source, const char *dest)
1158 bool do_link = conf->hard_link && !file_is_compressed(source);
1161 ret = link(source, dest);
1163 ret = copy_file(source, dest, 0);
1167 if (errno == ENOENT || errno == ESTALE) {
1168 cc_log("File missing in cache: %s", source);
1169 stats_update(STATS_MISSING);
1171 cc_log("Failed to %s %s to %s: %s",
1172 do_link ? "link" : "copy",
1176 stats_update(STATS_ERROR);
1179 // If there was trouble getting a file from the cached result, wipe the
1180 // whole cached result for consistency.
1181 x_unlink(cached_stderr);
1182 x_unlink(cached_obj);
1183 x_unlink(cached_dep);
1184 x_unlink(cached_cov);
1185 x_unlink(cached_su);
1186 x_unlink(cached_dia);
1187 x_unlink(cached_dwo);
1192 cc_log("Created from cache: %s -> %s", source, dest);
1195 // Send cached stderr, if any, to stderr.
1197 send_cached_stderr(void)
1199 int fd_stderr = open(cached_stderr, O_RDONLY | O_BINARY);
1200 if (fd_stderr != -1) {
1201 copy_fd(fd_stderr, 2);
1206 // Create or update the manifest file.
1208 update_manifest_file(void)
1210 if (!conf->direct_mode
1213 || conf->read_only_direct) {
1218 size_t old_size = 0; // in bytes
1219 if (stat(manifest_path, &st) == 0) {
1220 old_size = file_size(&st);
1222 if (manifest_put(manifest_path, cached_obj_hash, included_files)) {
1223 cc_log("Added object file hash to %s", manifest_path);
1224 update_mtime(manifest_path);
1225 if (x_stat(manifest_path, &st) == 0) {
1226 stats_update_size(file_size(&st) - old_size, old_size == 0 ? 1 : 0);
1229 cc_log("Failed to add object file hash to %s", manifest_path);
1233 // Run the real compiler and put the result in cache.
1235 to_cache(struct args *args)
1237 char *tmp_stdout = format("%s.tmp.stdout", cached_obj);
1238 int tmp_stdout_fd = create_tmp_fd(&tmp_stdout);
1239 char *tmp_stderr = format("%s.tmp.stderr", cached_obj);
1240 int tmp_stderr_fd = create_tmp_fd(&tmp_stderr);
1242 args_add(args, "-o");
1243 args_add(args, output_obj);
1245 if (generating_diagnostics) {
1246 args_add(args, "--serialize-diagnostics");
1247 args_add(args, output_dia);
1250 // Turn off DEPENDENCIES_OUTPUT when running cc1, because otherwise it will
1251 // emit a line like this:
1253 // tmp.stdout.vexed.732.o: /home/mbp/.ccache/tmp.stdout.vexed.732.i
1254 x_unsetenv("DEPENDENCIES_OUTPUT");
1256 if (conf->run_second_cpp) {
1257 args_add(args, input_file);
1259 args_add(args, i_tmpfile);
1262 cc_log("Running real compiler");
1264 execute(args->argv, tmp_stdout_fd, tmp_stderr_fd, &compiler_pid);
1268 if (x_stat(tmp_stdout, &st) != 0) {
1269 // The stdout file was removed - cleanup in progress? Better bail out.
1270 stats_update(STATS_MISSING);
1271 tmp_unlink(tmp_stdout);
1272 tmp_unlink(tmp_stderr);
1276 // distcc-pump outputs lines like this:
1277 // __________Using # distcc servers in pump mode
1278 if (st.st_size != 0 && guessed_compiler != GUESSED_PUMP) {
1279 cc_log("Compiler produced stdout");
1280 stats_update(STATS_STDOUT);
1281 tmp_unlink(tmp_stdout);
1282 tmp_unlink(tmp_stderr);
1285 tmp_unlink(tmp_stdout);
1287 // Merge stderr from the preprocessor (if any) and stderr from the real
1288 // compiler into tmp_stderr.
1290 char *tmp_stderr2 = format("%s.2", tmp_stderr);
1291 if (x_rename(tmp_stderr, tmp_stderr2)) {
1292 cc_log("Failed to rename %s to %s: %s", tmp_stderr, tmp_stderr2,
1297 int fd_cpp_stderr = open(cpp_stderr, O_RDONLY | O_BINARY);
1298 if (fd_cpp_stderr == -1) {
1299 cc_log("Failed opening %s: %s", cpp_stderr, strerror(errno));
1303 int fd_real_stderr = open(tmp_stderr2, O_RDONLY | O_BINARY);
1304 if (fd_real_stderr == -1) {
1305 cc_log("Failed opening %s: %s", tmp_stderr2, strerror(errno));
1310 open(tmp_stderr, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
1311 if (fd_result == -1) {
1312 cc_log("Failed opening %s: %s", tmp_stderr, strerror(errno));
1316 copy_fd(fd_cpp_stderr, fd_result);
1317 copy_fd(fd_real_stderr, fd_result);
1318 close(fd_cpp_stderr);
1319 close(fd_real_stderr);
1321 tmp_unlink(tmp_stderr2);
1326 cc_log("Compiler gave exit status %d", status);
1327 stats_update(STATS_STATUS);
1329 int fd = open(tmp_stderr, O_RDONLY | O_BINARY);
1331 // We can output stderr immediately instead of rerunning the compiler.
1334 tmp_unlink(tmp_stderr);
1339 tmp_unlink(tmp_stderr);
1343 if (stat(output_obj, &st) != 0) {
1344 cc_log("Compiler didn't produce an object file");
1345 stats_update(STATS_NOOUTPUT);
1348 if (st.st_size == 0) {
1349 cc_log("Compiler produced an empty object file");
1350 stats_update(STATS_EMPTYOUTPUT);
1354 if (x_stat(tmp_stderr, &st) != 0) {
1355 stats_update(STATS_ERROR);
1358 if (st.st_size > 0) {
1359 move_file_to_cache_same_fs(tmp_stderr, cached_stderr);
1361 tmp_unlink(tmp_stderr);
1362 if (conf->recache) {
1363 // If recaching, we need to remove any previous .stderr.
1364 x_unlink(cached_stderr);
1368 copy_file_to_cache(output_obj, cached_obj);
1369 if (generating_dependencies) {
1370 use_relative_paths_in_depfile(output_dep);
1371 copy_file_to_cache(output_dep, cached_dep);
1373 if (generating_coverage) {
1374 copy_file_to_cache(output_cov, cached_cov);
1376 if (generating_stackusage) {
1377 copy_file_to_cache(output_su, cached_su);
1379 if (generating_diagnostics) {
1380 copy_file_to_cache(output_dia, cached_dia);
1382 if (using_split_dwarf) {
1383 copy_file_to_cache(output_dwo, cached_dwo);
1386 stats_update(STATS_TOCACHE);
1388 // Make sure we have a CACHEDIR.TAG in the cache part of cache_dir. This can
1389 // be done almost anywhere, but we might as well do it near the end as we
1390 // save the stat call if we exit early.
1392 char *first_level_dir = dirname(stats_file);
1393 if (create_cachedirtag(first_level_dir) != 0) {
1394 cc_log("Failed to create %s/CACHEDIR.TAG (%s)\n",
1395 first_level_dir, strerror(errno));
1396 stats_update(STATS_ERROR);
1399 free(first_level_dir);
1401 // Remove any CACHEDIR.TAG on the cache_dir level where it was located in
1402 // previous ccache versions.
1403 if (getpid() % 1000 == 0) {
1404 char *path = format("%s/CACHEDIR.TAG", conf->cache_dir);
1411 send_cached_stderr();
1412 update_manifest_file();
1418 // Find the object file name by running the compiler in preprocessor mode.
1419 // Returns the hash as a heap-allocated hex string.
1420 static struct file_hash *
1421 get_object_name_from_cpp(struct args *args, struct hash *hash)
1423 time_of_compilation = time(NULL);
1425 char *path_stderr = NULL;
1428 if (direct_i_file) {
1429 // We are compiling a .i or .ii file - that means we can skip the cpp stage
1430 // and directly form the correct i_tmpfile.
1431 path_stdout = input_file;
1434 // Run cpp on the input file to obtain the .i.
1436 // Limit the basename to 10 characters in order to cope with filesystem with
1437 // small maximum filename length limits.
1438 char *input_base = basename(input_file);
1439 char *tmp = strchr(input_base, '.');
1443 if (strlen(input_base) > 10) {
1447 path_stdout = format("%s/%s.stdout", temp_dir(), input_base);
1448 int path_stdout_fd = create_tmp_fd(&path_stdout);
1449 add_pending_tmp_file(path_stdout);
1451 path_stderr = format("%s/tmp.cpp_stderr", temp_dir());
1452 int path_stderr_fd = create_tmp_fd(&path_stderr);
1453 add_pending_tmp_file(path_stderr);
1456 args_add(args, "-E");
1457 if (conf->keep_comments_cpp) {
1458 args_add(args, "-C");
1461 args_add(args, input_file);
1462 add_prefix(args, conf->prefix_command_cpp);
1463 cc_log("Running preprocessor");
1464 status = execute(args->argv, path_stdout_fd, path_stderr_fd, &compiler_pid);
1465 args_pop(args, args_added);
1469 cc_log("Preprocessor gave exit status %d", status);
1470 stats_update(STATS_PREPROCESSOR);
1475 // When we are doing the unifying tricks we need to include the input file
1476 // name in the hash to get the warnings right.
1477 hash_delimiter(hash, "unifyfilename");
1478 hash_string(hash, input_file);
1480 hash_delimiter(hash, "unifycpp");
1482 bool debug_unify = getenv("CCACHE_DEBUG_UNIFY");
1483 if (unify_hash(hash, path_stdout, debug_unify) != 0) {
1484 stats_update(STATS_ERROR);
1485 cc_log("Failed to unify %s", path_stdout);
1489 hash_delimiter(hash, "cpp");
1490 if (!process_preprocessed_file(hash, path_stdout,
1491 guessed_compiler == GUESSED_PUMP)) {
1492 stats_update(STATS_ERROR);
1497 hash_delimiter(hash, "cppstderr");
1498 if (!direct_i_file && !hash_file(hash, path_stderr)) {
1499 fatal("Failed to open %s: %s", path_stderr, strerror(errno));
1502 if (direct_i_file) {
1503 i_tmpfile = input_file;
1505 // i_tmpfile needs the proper cpp_extension for the compiler to do its
1507 i_tmpfile = format("%s.%s", path_stdout, conf->cpp_extension);
1508 x_rename(path_stdout, i_tmpfile);
1509 add_pending_tmp_file(i_tmpfile);
1512 if (conf->run_second_cpp) {
1515 // If we are using the CPP trick, we need to remember this stderr data and
1516 // output it just before the main stderr from the compiler pass.
1517 cpp_stderr = path_stderr;
1518 hash_delimiter(hash, "runsecondcpp");
1519 hash_string(hash, "false");
1522 struct file_hash *result = x_malloc(sizeof(*result));
1523 hash_result_as_bytes(hash, result->hash);
1524 result->size = hash_input_size(hash);
1529 update_cached_result_globals(struct file_hash *hash)
1531 char *object_name = format_hash_as_string(hash->hash, hash->size);
1532 cached_obj_hash = hash;
1533 cached_obj = get_path_in_cache(object_name, ".o");
1534 cached_stderr = get_path_in_cache(object_name, ".stderr");
1535 cached_dep = get_path_in_cache(object_name, ".d");
1536 cached_cov = get_path_in_cache(object_name, ".gcno");
1537 cached_su = get_path_in_cache(object_name, ".su");
1538 cached_dia = get_path_in_cache(object_name, ".dia");
1539 cached_dwo = get_path_in_cache(object_name, ".dwo");
1541 stats_file = format("%s/%c/stats", conf->cache_dir, object_name[0]);
1545 // Hash mtime or content of a file, or the output of a command, according to
1546 // the CCACHE_COMPILERCHECK setting.
1548 hash_compiler(struct hash *hash, struct stat *st, const char *path,
1551 if (str_eq(conf->compiler_check, "none")) {
1553 } else if (str_eq(conf->compiler_check, "mtime")) {
1554 hash_delimiter(hash, "cc_mtime");
1555 hash_int(hash, st->st_size);
1556 hash_int(hash, st->st_mtime);
1557 } else if (str_startswith(conf->compiler_check, "string:")) {
1558 hash_delimiter(hash, "cc_hash");
1559 hash_string(hash, conf->compiler_check + strlen("string:"));
1560 } else if (str_eq(conf->compiler_check, "content") || !allow_command) {
1561 hash_delimiter(hash, "cc_content");
1562 hash_file(hash, path);
1563 } else { // command string
1564 bool ok = hash_multicommand_output(
1565 hash, conf->compiler_check, orig_args->argv[0]);
1567 fatal("Failure running compiler check command: %s", conf->compiler_check);
1572 // Hash the host compiler(s) invoked by nvcc.
1574 // If ccbin_st and ccbin are set, they refer to a directory or compiler set
1575 // with -ccbin/--compiler-bindir. If they are NULL, the compilers are looked up
1578 hash_nvcc_host_compiler(struct hash *hash, struct stat *ccbin_st,
1581 // From <http://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html>:
1583 // "[...] Specify the directory in which the compiler executable resides.
1584 // The host compiler executable name can be also specified to ensure that
1585 // the correct host compiler is selected."
1589 // "On all platforms, the default host compiler executable (gcc and g++ on
1590 // Linux, clang and clang++ on Mac OS X, and cl.exe on Windows) found in
1591 // the current execution search path will be used".
1593 if (!ccbin || S_ISDIR(ccbin_st->st_mode)) {
1594 #if defined(__APPLE__)
1595 const char *compilers[] = {"clang", "clang++"};
1596 #elif defined(_WIN32)
1597 const char *compilers[] = {"cl.exe"};
1599 const char *compilers[] = {"gcc", "g++"};
1601 for (size_t i = 0; i < ARRAY_SIZE(compilers); i++) {
1603 char *path = format("%s/%s", ccbin, compilers[i]);
1605 if (stat(path, &st) == 0) {
1606 hash_compiler(hash, &st, path, false);
1610 char *path = find_executable(compilers[i], MYNAME);
1614 hash_compiler(hash, &st, ccbin, false);
1620 hash_compiler(hash, ccbin_st, ccbin, false);
1624 // Update a hash sum with information common for the direct and preprocessor
1627 calculate_common_hash(struct args *args, struct hash *hash)
1629 hash_string(hash, HASH_PREFIX);
1631 // We have to hash the extension, as a .i file isn't treated the same by the
1632 // compiler as a .ii file.
1633 hash_delimiter(hash, "ext");
1634 hash_string(hash, conf->cpp_extension);
1637 const char *ext = strrchr(args->argv[0], '.');
1638 char full_path_win_ext[MAX_PATH + 1] = {0};
1639 add_exe_ext_if_no_to_fullpath(full_path_win_ext, MAX_PATH, ext,
1641 const char *full_path = full_path_win_ext;
1643 const char *full_path = args->argv[0];
1647 if (x_stat(full_path, &st) != 0) {
1648 stats_update(STATS_COMPILER);
1652 // Hash information about the compiler.
1653 hash_compiler(hash, &st, args->argv[0], true);
1655 // Also hash the compiler name as some compilers use hard links and behave
1656 // differently depending on the real name.
1657 hash_delimiter(hash, "cc_name");
1658 char *base = basename(args->argv[0]);
1659 hash_string(hash, base);
1662 // Possibly hash the current working directory.
1663 if (generating_debuginfo && conf->hash_dir) {
1664 char *cwd = gnu_getcwd();
1665 for (size_t i = 0; i < debug_prefix_maps_len; i++) {
1666 char *map = debug_prefix_maps[i];
1667 char *sep = strchr(map, '=');
1669 char *old = x_strndup(map, sep - map);
1670 char *new = x_strdup(sep + 1);
1671 cc_log("Relocating debuginfo CWD %s from %s to %s", cwd, old, new);
1672 if (str_startswith(cwd, old)) {
1673 char *dir = format("%s%s", new, cwd + strlen(old));
1682 cc_log("Hashing CWD %s", cwd);
1683 hash_delimiter(hash, "cwd");
1684 hash_string(hash, cwd);
1689 // Possibly hash the coverage data file path.
1690 if (generating_coverage && profile_arcs) {
1691 char *dir = dirname(output_obj);
1693 dir = x_strdup(profile_dir);
1695 char *real_dir = x_realpath(dir);
1700 char *base_name = basename(output_obj);
1701 char *p = remove_extension(base_name);
1703 char *gcda_path = format("%s/%s.gcda", dir, p);
1704 cc_log("Hashing coverage path %s", gcda_path);
1706 hash_delimiter(hash, "gcda");
1707 hash_string(hash, gcda_path);
1712 // Possibly hash the sanitize blacklist file path.
1713 if (sanitize_blacklist) {
1714 cc_log("Hashing sanitize blacklist %s", sanitize_blacklist);
1715 hash_delimiter(hash, "sanitizeblacklist");
1716 if (!hash_file(hash, sanitize_blacklist)) {
1717 stats_update(STATS_BADEXTRAFILE);
1722 if (!str_eq(conf->extra_files_to_hash, "")) {
1723 char *p = x_strdup(conf->extra_files_to_hash);
1726 char *saveptr = NULL;
1727 while ((path = strtok_r(q, PATH_DELIM, &saveptr))) {
1728 cc_log("Hashing extra file %s", path);
1729 hash_delimiter(hash, "extrafile");
1730 if (!hash_file(hash, path)) {
1731 stats_update(STATS_BADEXTRAFILE);
1739 // Possibly hash GCC_COLORS (for color diagnostics).
1740 if (guessed_compiler == GUESSED_GCC) {
1741 const char *gcc_colors = getenv("GCC_COLORS");
1743 hash_delimiter(hash, "gcccolors");
1744 hash_string(hash, gcc_colors);
1749 // Update a hash sum with information specific to the direct and preprocessor
1750 // modes and calculate the object hash. Returns the object hash on success,
1751 // otherwise NULL. Caller frees.
1752 static struct file_hash *
1753 calculate_object_hash(struct args *args, struct hash *hash, int direct_mode)
1755 bool found_ccbin = false;
1758 hash_delimiter(hash, "manifest version");
1759 hash_int(hash, MANIFEST_VERSION);
1762 // clang will emit warnings for unused linker flags, so we shouldn't skip
1765 guessed_compiler == GUESSED_CLANG || guessed_compiler == GUESSED_UNKNOWN;
1767 // First the arguments.
1768 for (int i = 1; i < args->argc; i++) {
1769 // -L doesn't affect compilation (except for clang).
1770 if (i < args->argc-1 && str_eq(args->argv[i], "-L") && !is_clang) {
1774 if (str_startswith(args->argv[i], "-L") && !is_clang) {
1778 // -Wl,... doesn't affect compilation (except for clang).
1779 if (str_startswith(args->argv[i], "-Wl,") && !is_clang) {
1783 // The -fdebug-prefix-map option may be used in combination with
1784 // CCACHE_BASEDIR to reuse results across different directories. Skip it
1786 if (str_startswith(args->argv[i], "-fdebug-prefix-map=")) {
1790 // When using the preprocessor, some arguments don't contribute to the
1791 // hash. The theory is that these arguments will change the output of -E if
1792 // they are going to have any effect at all. For precompiled headers this
1793 // might not be the case.
1794 if (!direct_mode && !output_is_precompiled_header
1795 && !using_precompiled_header) {
1796 if (compopt_affects_cpp(args->argv[i])) {
1797 if (compopt_takes_arg(args->argv[i])) {
1802 if (compopt_short(compopt_affects_cpp, args->argv[i])) {
1807 // If we're generating dependencies, we make sure to skip the filename of
1808 // the dependency file, since it doesn't impact the output.
1809 if (generating_dependencies) {
1810 if (str_startswith(args->argv[i], "-Wp,")) {
1811 if (str_startswith(args->argv[i], "-Wp,-MD,")
1812 && !strchr(args->argv[i] + 8, ',')) {
1813 hash_string_buffer(hash, args->argv[i], 8);
1815 } else if (str_startswith(args->argv[i], "-Wp,-MMD,")
1816 && !strchr(args->argv[i] + 9, ',')) {
1817 hash_string_buffer(hash, args->argv[i], 9);
1820 } else if (str_startswith(args->argv[i], "-MF")) {
1821 // In either case, hash the "-MF" part.
1822 hash_delimiter(hash, "arg");
1823 hash_string_buffer(hash, args->argv[i], 3);
1825 bool separate_argument = (strlen(args->argv[i]) == 3);
1826 if (separate_argument) {
1827 // Next argument is dependency name, so skip it.
1835 if (str_startswith(args->argv[i], "-specs=")) {
1836 p = args->argv[i] + 7;
1837 } else if (str_startswith(args->argv[i], "--specs=")) {
1838 p = args->argv[i] + 8;
1842 if (p && x_stat(p, &st) == 0) {
1843 // If given an explicit specs file, then hash that file, but don't
1844 // include the path to it in the hash.
1845 hash_delimiter(hash, "specs");
1846 hash_compiler(hash, &st, p, false);
1850 if (str_startswith(args->argv[i], "-fplugin=")
1851 && x_stat(args->argv[i] + 9, &st) == 0) {
1852 hash_delimiter(hash, "plugin");
1853 hash_compiler(hash, &st, args->argv[i] + 9, false);
1857 if (str_eq(args->argv[i], "-Xclang")
1858 && i + 3 < args->argc
1859 && str_eq(args->argv[i+1], "-load")
1860 && str_eq(args->argv[i+2], "-Xclang")
1861 && x_stat(args->argv[i+3], &st) == 0) {
1862 hash_delimiter(hash, "plugin");
1863 hash_compiler(hash, &st, args->argv[i+3], false);
1868 if ((str_eq(args->argv[i], "-ccbin")
1869 || str_eq(args->argv[i], "--compiler-bindir"))
1870 && i + 1 < args->argc
1871 && x_stat(args->argv[i+1], &st) == 0) {
1873 hash_delimiter(hash, "ccbin");
1874 hash_nvcc_host_compiler(hash, &st, args->argv[i+1]);
1879 // All other arguments are included in the hash.
1880 hash_delimiter(hash, "arg");
1881 hash_string(hash, args->argv[i]);
1882 if (i + 1 < args->argc && compopt_takes_arg(args->argv[i])) {
1884 hash_delimiter(hash, "arg");
1885 hash_string(hash, args->argv[i]);
1889 if (!found_ccbin && str_eq(actual_language, "cuda")) {
1890 hash_nvcc_host_compiler(hash, NULL, NULL);
1893 // For profile generation (-fprofile-arcs, -fprofile-generate):
1894 // - hash profile directory
1896 // For profile usage (-fprofile-use):
1897 // - hash profile data
1899 // -fbranch-probabilities and -fvpt usage is covered by
1900 // -fprofile-generate/-fprofile-use.
1902 // The profile directory can be specified as an argument to
1903 // -fprofile-generate=, -fprofile-use= or -fprofile-dir=.
1904 if (profile_generate) {
1906 profile_dir = get_cwd();
1908 cc_log("Adding profile directory %s to our hash", profile_dir);
1909 hash_delimiter(hash, "-fprofile-dir");
1910 hash_string(hash, profile_dir);
1914 // Calculate gcda name.
1916 profile_dir = get_cwd();
1918 char *base_name = remove_extension(output_obj);
1919 char *gcda_name = format("%s/%s.gcda", profile_dir, base_name);
1920 cc_log("Adding profile data %s to our hash", gcda_name);
1921 // Add the gcda to our hash.
1922 hash_delimiter(hash, "-fprofile-use");
1923 hash_file(hash, gcda_name);
1928 // Adding -arch to hash since cpp output is affected.
1929 for (size_t i = 0; i < arch_args_size; ++i) {
1930 hash_delimiter(hash, "-arch");
1931 hash_string(hash, arch_args[i]);
1934 struct file_hash *object_hash = NULL;
1936 // Hash environment variables that affect the preprocessor output.
1937 const char *envvars[] = {
1940 "CPLUS_INCLUDE_PATH",
1941 "OBJC_INCLUDE_PATH",
1942 "OBJCPLUS_INCLUDE_PATH", // clang
1945 for (const char **p = envvars; *p; ++p) {
1946 char *v = getenv(*p);
1948 hash_delimiter(hash, *p);
1949 hash_string(hash, v);
1953 if (!(conf->sloppiness & SLOPPY_FILE_MACRO)) {
1954 // The source code file or an include file may contain __FILE__, so make
1955 // sure that the hash is unique for the file name.
1956 hash_delimiter(hash, "inputfile");
1957 hash_string(hash, input_file);
1960 hash_delimiter(hash, "sourcecode");
1961 int result = hash_source_code_file(conf, hash, input_file);
1962 if (result & HASH_SOURCE_CODE_ERROR) {
1965 if (result & HASH_SOURCE_CODE_FOUND_TIME) {
1966 cc_log("Disabling direct mode");
1967 conf->direct_mode = false;
1970 char *manifest_name = hash_result(hash);
1971 manifest_path = get_path_in_cache(manifest_name, ".manifest");
1972 free(manifest_name);
1973 cc_log("Looking for object file hash in %s", manifest_path);
1974 object_hash = manifest_get(conf, manifest_path);
1976 cc_log("Got object file hash from manifest");
1978 cc_log("Did not find object file hash in manifest");
1981 if (arch_args_size == 0) {
1982 object_hash = get_object_name_from_cpp(args, hash);
1983 cc_log("Got object file hash from preprocessor");
1985 args_add(args, "-arch");
1986 for (size_t i = 0; i < arch_args_size; ++i) {
1987 args_add(args, arch_args[i]);
1988 object_hash = get_object_name_from_cpp(args, hash);
1989 cc_log("Got object file hash from preprocessor with -arch %s",
1991 if (i != arch_args_size - 1) {
1999 if (generating_dependencies) {
2000 cc_log("Preprocessor created %s", output_dep);
2007 // Try to return the compile result from cache. If we can return from cache
2008 // then this function exits with the correct status code, otherwise it returns.
2010 from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest)
2012 // The user might be disabling cache hits.
2013 if (conf->recache) {
2017 // If we're using Clang, we can't trust a precompiled header object based on
2018 // running the preprocessor since clang will produce a fatal error when the
2019 // precompiled header is used and one of the included files has an updated
2022 // file 'foo.h' has been modified since the precompiled header 'foo.pch'
2024 if ((guessed_compiler == GUESSED_CLANG || guessed_compiler == GUESSED_UNKNOWN)
2025 && output_is_precompiled_header
2026 && mode == FROMCACHE_CPP_MODE) {
2027 cc_log("Not considering cached precompiled header in preprocessor mode");
2031 // Occasionally, e.g. on hard reset, our cache ends up as just filesystem
2032 // meta-data with no content. Catch an easy case of this.
2034 if (stat(cached_obj, &st) != 0) {
2035 cc_log("Object file %s not in cache", cached_obj);
2038 if (st.st_size == 0) {
2039 cc_log("Invalid (empty) object file %s in cache", cached_obj);
2040 x_unlink(cached_obj);
2044 // (If mode != FROMCACHE_DIRECT_MODE, the dependency file is created by gcc.)
2045 bool produce_dep_file =
2046 generating_dependencies && mode == FROMCACHE_DIRECT_MODE;
2048 // Get result from cache.
2049 if (!str_eq(output_obj, "/dev/null")) {
2050 get_file_from_cache(cached_obj, output_obj);
2051 if (using_split_dwarf) {
2052 get_file_from_cache(cached_dwo, output_dwo);
2055 if (produce_dep_file) {
2056 get_file_from_cache(cached_dep, output_dep);
2058 if (generating_coverage) {
2059 get_file_from_cache(cached_cov, output_cov);
2061 if (generating_stackusage) {
2062 get_file_from_cache(cached_su, output_su);
2064 if (generating_diagnostics) {
2065 get_file_from_cache(cached_dia, output_dia);
2068 // Update modification timestamps to save files from LRU cleanup. Also gives
2069 // files a sensible mtime when hard-linking.
2070 update_mtime(cached_obj);
2071 update_mtime(cached_stderr);
2072 if (produce_dep_file) {
2073 update_mtime(cached_dep);
2075 if (generating_coverage) {
2076 update_mtime(cached_cov);
2078 if (generating_stackusage) {
2079 update_mtime(cached_su);
2081 if (generating_diagnostics) {
2082 update_mtime(cached_dia);
2085 update_mtime(cached_dwo);
2088 send_cached_stderr();
2090 if (put_object_in_manifest) {
2091 update_manifest_file();
2094 // Log the cache hit.
2096 case FROMCACHE_DIRECT_MODE:
2097 cc_log("Succeeded getting cached result");
2098 stats_update(STATS_CACHEHIT_DIR);
2101 case FROMCACHE_CPP_MODE:
2102 cc_log("Succeeded getting cached result");
2103 stats_update(STATS_CACHEHIT_CPP);
2107 // And exit with the right status code.
2111 // Find the real compiler. We just search the PATH to find an executable of the
2112 // same name that isn't a link to ourselves.
2114 find_compiler(char **argv)
2116 // We might be being invoked like "ccache gcc -c foo.c".
2117 char *base = basename(argv[0]);
2118 if (same_executable_name(base, MYNAME)) {
2119 args_remove_first(orig_args);
2121 if (is_full_path(orig_args->argv[0])) {
2122 // A full path was given.
2125 base = basename(orig_args->argv[0]);
2128 // Support user override of the compiler.
2129 if (!str_eq(conf->compiler, "")) {
2130 base = conf->compiler;
2133 char *compiler = find_executable(base, MYNAME);
2135 stats_update(STATS_COMPILER);
2136 fatal("Could not find compiler \"%s\" in PATH", base);
2138 if (str_eq(compiler, argv[0])) {
2139 fatal("Recursive invocation (the name of the ccache binary must be \"%s\")",
2142 orig_args->argv[0] = compiler;
2146 is_precompiled_header(const char *path)
2148 const char *ext = get_extension(path);
2149 char *dir = dirname(path);
2150 const char *dir_ext = get_extension(dir);
2153 || str_eq(ext, ".pch")
2154 || str_eq(ext, ".pth")
2155 || str_eq(dir_ext, ".gch"); // See "Precompiled Headers" in GCC docs.
2161 color_output_possible(void)
2163 const char *term_env = getenv("TERM");
2164 return isatty(STDERR_FILENO) && term_env && strcasecmp(term_env, "DUMB") != 0;
2168 detect_pch(const char *option, const char *arg, bool *found_pch)
2172 // Try to be smart about detecting precompiled headers.
2173 char *pch_file = NULL;
2174 if (str_eq(option, "-include-pch") || str_eq(option, "-include-pth")) {
2175 if (stat(arg, &st) == 0) {
2176 cc_log("Detected use of precompiled header: %s", arg);
2177 pch_file = x_strdup(arg);
2180 char *gchpath = format("%s.gch", arg);
2181 if (stat(gchpath, &st) == 0) {
2182 cc_log("Detected use of precompiled header: %s", gchpath);
2183 pch_file = x_strdup(gchpath);
2185 char *pchpath = format("%s.pch", arg);
2186 if (stat(pchpath, &st) == 0) {
2187 cc_log("Detected use of precompiled header: %s", pchpath);
2188 pch_file = x_strdup(pchpath);
2190 // clang may use pretokenized headers.
2191 char *pthpath = format("%s.pth", arg);
2192 if (stat(pthpath, &st) == 0) {
2193 cc_log("Detected use of pretokenized header: %s", pthpath);
2194 pch_file = x_strdup(pthpath);
2204 if (included_pch_file) {
2205 cc_log("Multiple precompiled headers used: %s and %s\n",
2206 included_pch_file, pch_file);
2207 stats_update(STATS_ARGS);
2210 included_pch_file = pch_file;
2216 // Process the compiler options into options suitable for passing to the
2217 // preprocessor and the real compiler. The preprocessor options don't include
2218 // -E; this is added later. Returns true on success, otherwise false.
2220 cc_process_args(struct args *args, struct args **preprocessor_args,
2221 struct args **compiler_args)
2223 bool found_c_opt = false;
2224 bool found_S_opt = false;
2225 bool found_pch = false;
2226 bool found_fpch_preprocess = false;
2227 const char *explicit_language = NULL; // As specified with -x.
2228 const char *file_language; // As deduced from file extension.
2229 const char *input_charset = NULL;
2230 // Is the dependency makefile name overridden with -MF?
2231 bool dependency_filename_specified = false;
2232 // Is the dependency makefile target name specified with -MT or -MQ?
2233 bool dependency_target_specified = false;
2234 // expanded_args is a copy of the original arguments given to the compiler
2235 // but with arguments from @file and similar constructs expanded. It's only
2236 // used as a temporary data structure to loop over.
2237 struct args *expanded_args = args_copy(args);
2238 // stripped_args essentially contains all original arguments except those
2239 // that only should be passed to the preprocessor (if run_second_cpp is
2240 // false) and except dependency options (like -MD and friends).
2241 struct args *stripped_args = args_init(0, NULL);
2242 // cpp_args contains arguments that were not added to stripped_args, i.e.
2243 // those that should only be passed to the preprocessor if run_second_cpp is
2244 // false. If run_second_cpp is true, they will be passed to the compiler as
2246 struct args *cpp_args = args_init(0, NULL);
2247 // dep_args contains dependency options like -MD. They only passed to the
2248 // preprocessor, never to the compiler.
2249 struct args *dep_args = args_init(0, NULL);
2251 bool found_color_diagnostics = false;
2253 bool found_directives_only = false;
2254 bool found_rewrite_includes = false;
2256 int argc = expanded_args->argc;
2257 char **argv = expanded_args->argv;
2258 args_add(stripped_args, argv[0]);
2261 for (int i = 1; i < argc; i++) {
2262 // The user knows best: just swallow the next arg.
2263 if (str_eq(argv[i], "--ccache-skip")) {
2266 cc_log("--ccache-skip lacks an argument");
2270 args_add(stripped_args, argv[i]);
2274 // Special case for -E.
2275 if (str_eq(argv[i], "-E")) {
2276 stats_update(STATS_PREPROCESSING);
2281 // Handle "@file" argument.
2282 if (str_startswith(argv[i], "@") || str_startswith(argv[i], "-@")) {
2283 char *argpath = argv[i] + 1;
2285 if (argpath[-1] == '-') {
2288 struct args *file_args = args_init_from_gcc_atfile(argpath);
2290 cc_log("Couldn't read arg file %s", argpath);
2291 stats_update(STATS_ARGS);
2296 args_insert(expanded_args, i, file_args, true);
2297 argc = expanded_args->argc;
2298 argv = expanded_args->argv;
2303 // Handle cuda "-optf" and "--options-file" argument.
2304 if (guessed_compiler == GUESSED_NVCC
2305 && (str_eq(argv[i], "-optf") || str_eq(argv[i], "--options-file"))) {
2306 if (i == argc - 1) {
2307 cc_log("Expected argument after %s", argv[i]);
2308 stats_update(STATS_ARGS);
2314 // Argument is a comma-separated list of files.
2315 char *str_start = argv[i];
2316 char *str_end = strchr(str_start, ',');
2320 str_end = str_start + strlen(str_start);
2325 struct args *file_args = args_init_from_gcc_atfile(str_start);
2327 cc_log("Couldn't read cuda options file %s", str_start);
2328 stats_update(STATS_ARGS);
2333 int new_index = file_args->argc + index;
2334 args_insert(expanded_args, index, file_args, false);
2336 str_start = str_end;
2337 str_end = strchr(str_start, ',');
2340 argc = expanded_args->argc;
2341 argv = expanded_args->argv;
2345 // These are always too hard.
2346 if (compopt_too_hard(argv[i]) || str_startswith(argv[i], "-fdump-")) {
2347 cc_log("Compiler option %s is unsupported", argv[i]);
2348 stats_update(STATS_UNSUPPORTED_OPTION);
2353 // These are too hard in direct mode.
2354 if (conf->direct_mode && compopt_too_hard_for_direct_mode(argv[i])) {
2355 cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
2356 conf->direct_mode = false;
2359 // -Xarch_* options are too hard.
2360 if (str_startswith(argv[i], "-Xarch_")) {
2361 cc_log("Unsupported compiler option :%s", argv[i]);
2362 stats_update(STATS_UNSUPPORTED_OPTION);
2367 // Handle -arch options.
2368 if (str_eq(argv[i], "-arch")) {
2369 if (arch_args_size == MAX_ARCH_ARGS - 1) {
2370 cc_log("Too many -arch compiler options; ccache supports at most %d",
2372 stats_update(STATS_UNSUPPORTED_OPTION);
2378 arch_args[arch_args_size] = x_strdup(argv[i]); // It will leak.
2380 if (arch_args_size == 2) {
2381 conf->run_second_cpp = true;
2386 if (str_eq(argv[i], "-fpch-preprocess")
2387 || str_eq(argv[i], "-emit-pch")
2388 || str_eq(argv[i], "-emit-pth")) {
2389 found_fpch_preprocess = true;
2393 if (str_eq(argv[i], "-c")) {
2398 // -S changes the default extension.
2399 if (str_eq(argv[i], "-S")) {
2400 args_add(stripped_args, argv[i]);
2405 // Special handling for -x: remember the last specified language before the
2406 // input file and strip all -x options from the arguments.
2407 if (str_eq(argv[i], "-x")) {
2408 if (i == argc - 1) {
2409 cc_log("Missing argument to %s", argv[i]);
2410 stats_update(STATS_ARGS);
2415 explicit_language = argv[i+1];
2420 if (str_startswith(argv[i], "-x")) {
2422 explicit_language = &argv[i][2];
2427 // We need to work out where the output was meant to go.
2428 if (str_eq(argv[i], "-o")) {
2429 if (i == argc - 1) {
2430 cc_log("Missing argument to %s", argv[i]);
2431 stats_update(STATS_ARGS);
2435 output_obj = make_relative_path(x_strdup(argv[i+1]));
2440 // Alternate form of -o with no space. Nvcc does not support this.
2441 if (str_startswith(argv[i], "-o") && guessed_compiler != GUESSED_NVCC) {
2442 output_obj = make_relative_path(x_strdup(&argv[i][2]));
2446 if (str_eq(argv[i], "-gsplit-dwarf")) {
2447 cc_log("Enabling caching of dwarf files since -gsplit-dwarf is used");
2448 using_split_dwarf = true;
2449 args_add(stripped_args, argv[i]);
2452 if (str_startswith(argv[i], "-fdebug-prefix-map=")) {
2453 debug_prefix_maps = x_realloc(
2455 (debug_prefix_maps_len + 1) * sizeof(char *));
2456 debug_prefix_maps[debug_prefix_maps_len++] = x_strdup(argv[i] + 19);
2457 args_add(stripped_args, argv[i]);
2461 // Debugging is handled specially, so that we know if we can strip line
2463 if (str_startswith(argv[i], "-g")) {
2464 generating_debuginfo = true;
2465 args_add(stripped_args, argv[i]);
2466 if (conf->unify && !str_eq(argv[i], "-g0")) {
2467 cc_log("%s used; disabling unify mode", argv[i]);
2468 conf->unify = false;
2470 if (str_eq(argv[i], "-g3")) {
2471 cc_log("%s used; not compiling preprocessed code", argv[i]);
2472 conf->run_second_cpp = true;
2477 // These options require special handling, because they behave differently
2478 // with gcc -E, when the output file is not specified.
2479 if (str_eq(argv[i], "-MD") || str_eq(argv[i], "-MMD")) {
2480 generating_dependencies = true;
2481 args_add(dep_args, argv[i]);
2484 if (str_startswith(argv[i], "-MF")) {
2485 dependency_filename_specified = true;
2489 bool separate_argument = (strlen(argv[i]) == 3);
2490 if (separate_argument) {
2492 if (i == argc - 1) {
2493 cc_log("Missing argument to %s", argv[i]);
2494 stats_update(STATS_ARGS);
2504 output_dep = make_relative_path(x_strdup(arg));
2505 // Keep the format of the args the same.
2506 if (separate_argument) {
2507 args_add(dep_args, "-MF");
2508 args_add(dep_args, output_dep);
2510 char *option = format("-MF%s", output_dep);
2511 args_add(dep_args, option);
2516 if (str_startswith(argv[i], "-MQ") || str_startswith(argv[i], "-MT")) {
2517 dependency_target_specified = true;
2520 if (strlen(argv[i]) == 3) {
2521 // -MQ arg or -MT arg
2522 if (i == argc - 1) {
2523 cc_log("Missing argument to %s", argv[i]);
2524 stats_update(STATS_ARGS);
2528 args_add(dep_args, argv[i]);
2529 relpath = make_relative_path(x_strdup(argv[i + 1]));
2530 args_add(dep_args, relpath);
2534 char *arg_opt = x_strndup(argv[i], 3);
2535 relpath = make_relative_path(x_strdup(argv[i] + 3));
2536 char *option = format("%s%s", arg_opt, relpath);
2537 args_add(dep_args, option);
2544 if (str_eq(argv[i], "-fprofile-arcs")) {
2545 profile_arcs = true;
2546 args_add(stripped_args, argv[i]);
2549 if (str_eq(argv[i], "-ftest-coverage")) {
2550 generating_coverage = true;
2551 args_add(stripped_args, argv[i]);
2554 if (str_eq(argv[i], "-fstack-usage")) {
2555 generating_stackusage = true;
2556 args_add(stripped_args, argv[i]);
2559 if (str_eq(argv[i], "--coverage") // = -fprofile-arcs -ftest-coverage
2560 || str_eq(argv[i], "-coverage")) { // Undocumented but still works.
2561 profile_arcs = true;
2562 generating_coverage = true;
2563 args_add(stripped_args, argv[i]);
2566 if (str_startswith(argv[i], "-fprofile-dir=")) {
2567 profile_dir = x_strdup(argv[i] + 14);
2568 args_add(stripped_args, argv[i]);
2571 if (str_startswith(argv[i], "-fsanitize-blacklist=")) {
2572 sanitize_blacklist = x_strdup(argv[i] + 21);
2573 args_add(stripped_args, argv[i]);
2576 if (str_startswith(argv[i], "--sysroot=")) {
2577 char *relpath = make_relative_path(x_strdup(argv[i] + 10));
2578 char *option = format("--sysroot=%s", relpath);
2579 args_add(stripped_args, option);
2584 // Alternate form of specifying sysroot without =
2585 if (str_eq(argv[i], "--sysroot")) {
2587 cc_log("Missing argument to %s", argv[i]);
2588 stats_update(STATS_ARGS);
2592 args_add(stripped_args, argv[i]);
2593 char *relpath = make_relative_path(x_strdup(argv[i+1]));
2594 args_add(stripped_args, relpath);
2599 // Alternate form of specifying target without =
2600 if (str_eq(argv[i], "-target")) {
2602 cc_log("Missing argument to %s", argv[i]);
2603 stats_update(STATS_ARGS);
2607 args_add(stripped_args, argv[i]);
2608 args_add(stripped_args, argv[i+1]);
2612 if (str_startswith(argv[i], "-Wp,")) {
2613 if (str_eq(argv[i], "-Wp,-P")
2614 || strstr(argv[i], ",-P,")
2615 || str_endswith(argv[i], ",-P")) {
2616 // -P removes preprocessor information in such a way that the object
2617 // file from compiling the preprocessed file will not be equal to the
2618 // object file produced when compiling without ccache.
2619 cc_log("Too hard option -Wp,-P detected");
2620 stats_update(STATS_UNSUPPORTED_OPTION);
2622 } else if (str_startswith(argv[i], "-Wp,-MD,")
2623 && !strchr(argv[i] + 8, ',')) {
2624 generating_dependencies = true;
2625 dependency_filename_specified = true;
2627 output_dep = make_relative_path(x_strdup(argv[i] + 8));
2628 args_add(dep_args, argv[i]);
2630 } else if (str_startswith(argv[i], "-Wp,-MMD,")
2631 && !strchr(argv[i] + 9, ',')) {
2632 generating_dependencies = true;
2633 dependency_filename_specified = true;
2635 output_dep = make_relative_path(x_strdup(argv[i] + 9));
2636 args_add(dep_args, argv[i]);
2638 } else if (str_startswith(argv[i], "-Wp,-D")
2639 && !strchr(argv[i] + 6, ',')) {
2640 // Treat it like -D.
2641 args_add(cpp_args, argv[i] + 4);
2643 } else if (str_eq(argv[i], "-Wp,-MP")
2644 || (strlen(argv[i]) > 8
2645 && str_startswith(argv[i], "-Wp,-M")
2646 && argv[i][7] == ','
2647 && (argv[i][6] == 'F'
2648 || argv[i][6] == 'Q'
2649 || argv[i][6] == 'T')
2650 && !strchr(argv[i] + 8, ','))) {
2651 // TODO: Make argument to MF/MQ/MT relative.
2652 args_add(dep_args, argv[i]);
2654 } else if (conf->direct_mode) {
2655 // -Wp, can be used to pass too hard options to the preprocessor.
2656 // Hence, disable direct mode.
2657 cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
2658 conf->direct_mode = false;
2661 // Any other -Wp,* arguments are only relevant for the preprocessor.
2662 args_add(cpp_args, argv[i]);
2665 if (str_eq(argv[i], "-MP")) {
2666 args_add(dep_args, argv[i]);
2670 // Input charset needs to be handled specially.
2671 if (str_startswith(argv[i], "-finput-charset=")) {
2672 input_charset = argv[i];
2676 if (str_eq(argv[i], "--serialize-diagnostics")) {
2677 if (i == argc - 1) {
2678 cc_log("Missing argument to %s", argv[i]);
2679 stats_update(STATS_ARGS);
2683 generating_diagnostics = true;
2684 output_dia = make_relative_path(x_strdup(argv[i+1]));
2689 if (str_startswith(argv[i], "-fprofile-")) {
2690 char *arg = x_strdup(argv[i]);
2691 const char *arg_profile_dir = strchr(argv[i], '=');
2692 if (arg_profile_dir) {
2693 // Convert to absolute path.
2694 char *dir = x_realpath(arg_profile_dir + 1);
2696 // Directory doesn't exist.
2697 dir = x_strdup(arg_profile_dir + 1);
2700 // We can get a better hit rate by using the real path here.
2702 char *option = x_strndup(argv[i], arg_profile_dir - argv[i]);
2703 arg = format("%s=%s", option, dir);
2704 cc_log("Rewriting %s to %s", argv[i], arg);
2709 bool supported_profile_option = false;
2710 if (str_startswith(argv[i], "-fprofile-generate")
2711 || str_eq(argv[i], "-fprofile-arcs")) {
2712 profile_generate = true;
2713 supported_profile_option = true;
2714 } else if (str_startswith(argv[i], "-fprofile-use")
2715 || str_eq(argv[i], "-fbranch-probabilities")) {
2717 supported_profile_option = true;
2718 } else if (str_eq(argv[i], "-fprofile-dir")) {
2719 supported_profile_option = true;
2722 if (supported_profile_option) {
2723 args_add(stripped_args, arg);
2726 // If the profile directory has already been set, give up... Hard to
2727 // know what the user means, and what the compiler will do.
2728 if (arg_profile_dir && profile_dir) {
2729 cc_log("Profile directory already set; giving up");
2732 } else if (arg_profile_dir) {
2733 cc_log("Setting profile directory to %s", profile_dir);
2734 profile_dir = x_strdup(arg_profile_dir);
2738 cc_log("Unknown profile option: %s", argv[i]);
2742 if (str_eq(argv[i], "-fcolor-diagnostics")
2743 || str_eq(argv[i], "-fno-color-diagnostics")
2744 || str_eq(argv[i], "-fdiagnostics-color")
2745 || str_eq(argv[i], "-fdiagnostics-color=always")
2746 || str_eq(argv[i], "-fno-diagnostics-color")
2747 || str_eq(argv[i], "-fdiagnostics-color=never")) {
2748 args_add(stripped_args, argv[i]);
2749 found_color_diagnostics = true;
2752 if (str_eq(argv[i], "-fdiagnostics-color=auto")) {
2753 if (color_output_possible()) {
2754 // Output is redirected, so color output must be forced.
2755 args_add(stripped_args, "-fdiagnostics-color=always");
2756 cc_log("Automatically forcing colors");
2758 args_add(stripped_args, argv[i]);
2760 found_color_diagnostics = true;
2765 if (str_eq(argv[i], "-fdirectives-only")) {
2766 found_directives_only = true;
2770 if (str_eq(argv[i], "-frewrite-includes")) {
2771 found_rewrite_includes = true;
2775 // Options taking an argument that we may want to rewrite to relative paths
2776 // to get better hit rate. A secondary effect is that paths in the standard
2777 // error output produced by the compiler will be normalized.
2778 if (compopt_takes_path(argv[i])) {
2779 if (i == argc - 1) {
2780 cc_log("Missing argument to %s", argv[i]);
2781 stats_update(STATS_ARGS);
2786 if (!detect_pch(argv[i], argv[i+1], &found_pch)) {
2791 char *relpath = make_relative_path(x_strdup(argv[i+1]));
2792 if (compopt_affects_cpp(argv[i])) {
2793 args_add(cpp_args, argv[i]);
2794 args_add(cpp_args, relpath);
2796 args_add(stripped_args, argv[i]);
2797 args_add(stripped_args, relpath);
2805 // Same as above but options with concatenated argument beginning with a
2807 if (argv[i][0] == '-') {
2808 char *slash_pos = strchr(argv[i], '/');
2810 char *option = x_strndup(argv[i], slash_pos - argv[i]);
2811 if (compopt_takes_concat_arg(option) && compopt_takes_path(option)) {
2812 char *relpath = make_relative_path(x_strdup(slash_pos));
2813 char *new_option = format("%s%s", option, relpath);
2814 if (compopt_affects_cpp(option)) {
2815 args_add(cpp_args, new_option);
2817 args_add(stripped_args, new_option);
2829 // Options that take an argument.
2830 if (compopt_takes_arg(argv[i])) {
2831 if (i == argc - 1) {
2832 cc_log("Missing argument to %s", argv[i]);
2833 stats_update(STATS_ARGS);
2838 if (compopt_affects_cpp(argv[i])) {
2839 args_add(cpp_args, argv[i]);
2840 args_add(cpp_args, argv[i+1]);
2842 args_add(stripped_args, argv[i]);
2843 args_add(stripped_args, argv[i+1]);
2851 if (argv[i][0] == '-') {
2852 if (compopt_affects_cpp(argv[i])
2853 || compopt_prefix_affects_cpp(argv[i])) {
2854 args_add(cpp_args, argv[i]);
2856 args_add(stripped_args, argv[i]);
2861 // If an argument isn't a plain file then assume its an option, not an
2862 // input file. This allows us to cope better with unusual compiler options.
2864 if (stat(argv[i], &st) != 0 || !S_ISREG(st.st_mode)) {
2865 cc_log("%s is not a regular file, not considering as input file",
2867 args_add(stripped_args, argv[i]);
2872 if (language_for_file(argv[i])) {
2873 cc_log("Multiple input files: %s and %s", input_file, argv[i]);
2874 stats_update(STATS_MULTIPLE);
2875 } else if (!found_c_opt) {
2876 cc_log("Called for link with %s", argv[i]);
2877 if (strstr(argv[i], "conftest.")) {
2878 stats_update(STATS_CONFTEST);
2880 stats_update(STATS_LINK);
2883 cc_log("Unsupported source extension: %s", argv[i]);
2884 stats_update(STATS_SOURCELANG);
2890 // The source code file path gets put into the notes.
2891 if (generating_coverage) {
2892 input_file = x_strdup(argv[i]);
2896 if (is_symlink(argv[i])) {
2897 // Don't rewrite source file path if it's a symlink since
2898 // make_relative_path resolves symlinks using realpath(3) and this leads
2899 // to potentially choosing incorrect relative header files. See the
2900 // "symlink to source file" test.
2901 input_file = x_strdup(argv[i]);
2903 // Rewrite to relative to increase hit rate.
2904 input_file = make_relative_path(x_strdup(argv[i]));
2909 // Even if -gsplit-dwarf is given, the .dwo file is not generated when -S
2911 using_split_dwarf = false;
2912 cc_log("Disabling caching of dwarf files since -S is used");
2916 cc_log("No input file found");
2917 stats_update(STATS_NOINPUT);
2922 if (found_pch || found_fpch_preprocess) {
2923 using_precompiled_header = true;
2924 if (!(conf->sloppiness & SLOPPY_TIME_MACROS)) {
2925 cc_log("You have to specify \"time_macros\" sloppiness when using"
2926 " precompiled headers to get direct hits");
2927 cc_log("Disabling direct mode");
2928 stats_update(STATS_CANTUSEPCH);
2934 if (explicit_language && str_eq(explicit_language, "none")) {
2935 explicit_language = NULL;
2937 file_language = language_for_file(input_file);
2938 if (explicit_language) {
2939 if (!language_is_supported(explicit_language)) {
2940 cc_log("Unsupported language: %s", explicit_language);
2941 stats_update(STATS_SOURCELANG);
2945 actual_language = x_strdup(explicit_language);
2947 actual_language = file_language;
2950 output_is_precompiled_header =
2951 actual_language && strstr(actual_language, "-header");
2953 if (output_is_precompiled_header
2954 && !(conf->sloppiness & SLOPPY_PCH_DEFINES)) {
2955 cc_log("You have to specify \"pch_defines,time_macros\" sloppiness when"
2956 " creating precompiled headers");
2957 stats_update(STATS_CANTUSEPCH);
2962 if (!found_c_opt && !found_S_opt) {
2963 if (output_is_precompiled_header) {
2964 args_add(stripped_args, "-c");
2966 cc_log("No -c option found");
2967 // I find that having a separate statistic for autoconf tests is useful,
2968 // as they are the dominant form of "called for link" in many cases.
2969 if (strstr(input_file, "conftest.")) {
2970 stats_update(STATS_CONFTEST);
2972 stats_update(STATS_LINK);
2979 if (!actual_language) {
2980 cc_log("Unsupported source extension: %s", input_file);
2981 stats_update(STATS_SOURCELANG);
2986 if (!conf->run_second_cpp && str_eq(actual_language, "cuda")) {
2987 cc_log("Using CUDA compiler; not compiling preprocessed code");
2988 conf->run_second_cpp = true;
2991 direct_i_file = language_is_preprocessed(actual_language);
2993 if (output_is_precompiled_header && !conf->run_second_cpp) {
2994 // It doesn't work to create the .gch from preprocessed source.
2995 cc_log("Creating precompiled header; not compiling preprocessed code");
2996 conf->run_second_cpp = true;
2999 if (str_eq(conf->cpp_extension, "")) {
3000 const char *p_language = p_language_for_language(actual_language);
3001 free(conf->cpp_extension);
3002 conf->cpp_extension = x_strdup(extension_for_language(p_language) + 1);
3005 // Don't try to second guess the compilers heuristics for stdout handling.
3006 if (output_obj && str_eq(output_obj, "-")) {
3007 stats_update(STATS_OUTSTDOUT);
3008 cc_log("Output file is -");
3014 if (output_is_precompiled_header) {
3015 output_obj = format("%s.gch", input_file);
3017 output_obj = basename(input_file);
3018 char *p = strrchr(output_obj, '.');
3020 cc_log("Badly formed object filename");
3021 stats_update(STATS_ARGS);
3025 p[1] = found_S_opt ? 's' : 'o';
3030 if (using_split_dwarf) {
3031 char *p = strrchr(output_obj, '.');
3033 cc_log("Badly formed object filename");
3034 stats_update(STATS_ARGS);
3039 char *base_name = remove_extension(output_obj);
3040 output_dwo = format("%s.dwo", base_name);
3044 // Cope with -o /dev/null.
3046 if (!str_eq(output_obj, "/dev/null")
3047 && stat(output_obj, &st) == 0
3048 && !S_ISREG(st.st_mode)) {
3049 cc_log("Not a regular file: %s", output_obj);
3050 stats_update(STATS_DEVICE);
3055 // Some options shouldn't be passed to the real compiler when it compiles
3056 // preprocessed code:
3058 // -finput-charset=XXX (otherwise conversion happens twice)
3059 // -x XXX (otherwise the wrong language is selected)
3060 if (input_charset) {
3061 args_add(cpp_args, input_charset);
3064 args_add(cpp_args, "-fpch-preprocess");
3066 if (explicit_language) {
3067 args_add(cpp_args, "-x");
3068 args_add(cpp_args, explicit_language);
3071 // Since output is redirected, compilers will not color their output by
3072 // default, so force it explicitly if it would be otherwise done.
3073 if (!found_color_diagnostics && color_output_possible()) {
3074 if (guessed_compiler == GUESSED_CLANG) {
3075 if (!str_eq(actual_language, "assembler")) {
3076 args_add(stripped_args, "-fcolor-diagnostics");
3077 cc_log("Automatically enabling colors");
3079 } else if (guessed_compiler == GUESSED_GCC) {
3080 // GCC has it since 4.9, but that'd require detecting what GCC version is
3081 // used for the actual compile. However it requires also GCC_COLORS to be
3082 // set (and not empty), so use that for detecting if GCC would use
3084 if (getenv("GCC_COLORS") && getenv("GCC_COLORS")[0] != '\0') {
3085 args_add(stripped_args, "-fdiagnostics-color");
3086 cc_log("Automatically enabling colors");
3091 // Add flags for dependency generation only to the preprocessor command line.
3092 if (generating_dependencies) {
3093 if (!dependency_filename_specified) {
3094 char *base_name = remove_extension(output_obj);
3095 char *default_depfile_name = format("%s.d", base_name);
3097 args_add(dep_args, "-MF");
3098 args_add(dep_args, default_depfile_name);
3099 output_dep = make_relative_path(x_strdup(default_depfile_name));
3102 if (!dependency_target_specified
3103 && !str_eq(get_extension(output_dep), ".o")) {
3104 args_add(dep_args, "-MQ");
3105 args_add(dep_args, output_obj);
3108 if (generating_coverage) {
3109 char *base_name = remove_extension(output_obj);
3110 char *default_covfile_name = format("%s.gcno", base_name);
3112 output_cov = make_relative_path(default_covfile_name);
3114 if (generating_stackusage) {
3115 char *base_name = remove_extension(output_obj);
3116 char *default_sufile_name = format("%s.su", base_name);
3118 output_su = make_relative_path(default_sufile_name);
3121 *compiler_args = args_copy(stripped_args);
3122 if (conf->run_second_cpp) {
3123 args_extend(*compiler_args, cpp_args);
3124 } else if (found_directives_only || found_rewrite_includes) {
3125 // Need to pass the macros and any other preprocessor directives again.
3126 args_extend(*compiler_args, cpp_args);
3127 if (found_directives_only) {
3128 args_add(cpp_args, "-fdirectives-only");
3129 // The preprocessed source code still needs some more preprocessing.
3130 args_add(*compiler_args, "-fpreprocessed");
3131 args_add(*compiler_args, "-fdirectives-only");
3133 if (found_rewrite_includes) {
3134 args_add(cpp_args, "-frewrite-includes");
3135 // The preprocessed source code still needs some more preprocessing.
3136 args_add(*compiler_args, "-x");
3137 args_add(*compiler_args, actual_language);
3139 } else if (explicit_language) {
3140 // Workaround for a bug in Apple's patched distcc -- it doesn't properly
3141 // reset the language specified with -x, so if -x is given, we have to
3142 // specify the preprocessed language explicitly.
3143 args_add(*compiler_args, "-x");
3144 args_add(*compiler_args, p_language_for_language(explicit_language));
3148 args_add(*compiler_args, "-c");
3151 for (size_t i = 0; i < arch_args_size; ++i) {
3152 args_add(*compiler_args, "-arch");
3153 args_add(*compiler_args, arch_args[i]);
3156 // Only pass dependency arguments to the preprocesor since Intel's C++
3157 // compiler doesn't produce a correct .d file when compiling preprocessed
3159 args_extend(cpp_args, dep_args);
3161 *preprocessor_args = args_copy(stripped_args);
3162 args_extend(*preprocessor_args, cpp_args);
3165 args_free(expanded_args);
3166 args_free(stripped_args);
3167 args_free(dep_args);
3168 args_free(cpp_args);
3173 create_initial_config_file(const char *path)
3175 if (create_parent_dirs(path) != 0) {
3181 char *stats_dir = format("%s/0", conf->cache_dir);
3183 if (stat(stats_dir, &st) == 0) {
3184 stats_get_obsolete_limits(stats_dir, &max_files, &max_size);
3185 // STATS_MAXFILES and STATS_MAXSIZE was stored for each top directory.
3190 max_size = conf->max_size;
3194 FILE *f = fopen(path, "w");
3198 if (max_files != 0) {
3199 fprintf(f, "max_files = %u\n", max_files);
3200 conf->max_files = max_files;
3202 if (max_size != 0) {
3203 char *size = format_parsable_size_with_suffix(max_size);
3204 fprintf(f, "max_size = %s\n", size);
3206 conf->max_size = max_size;
3211 // Read config file(s), populate variables, create configuration file in cache
3212 // directory if missing, etc.
3217 conf = conf_create();
3220 char *p = getenv("CCACHE_CONFIGPATH");
3222 primary_config_path = x_strdup(p);
3224 secondary_config_path = format("%s/ccache.conf", TO_STRING(SYSCONFDIR));
3225 if (!conf_read(conf, secondary_config_path, &errmsg)) {
3227 // We could read the file but it contained errors.
3228 fatal("%s", errmsg);
3230 // A missing config file in SYSCONFDIR is OK.
3234 if (str_eq(conf->cache_dir, "")) {
3235 fatal("configuration setting \"cache_dir\" must not be the empty string");
3237 if ((p = getenv("CCACHE_DIR"))) {
3238 free(conf->cache_dir);
3239 conf->cache_dir = strdup(p);
3241 if (str_eq(conf->cache_dir, "")) {
3242 fatal("CCACHE_DIR must not be the empty string");
3245 primary_config_path = format("%s/ccache.conf", conf->cache_dir);
3248 bool should_create_initial_config = false;
3249 if (!conf_read(conf, primary_config_path, &errmsg)) {
3251 // We could read the file but it contained errors.
3252 fatal("%s", errmsg);
3254 if (!conf->disable) {
3255 should_create_initial_config = true;
3259 if (!conf_update_from_environment(conf, &errmsg)) {
3260 fatal("%s", errmsg);
3263 if (should_create_initial_config) {
3264 create_initial_config_file(primary_config_path);
3268 exitfn_add_nullary(stats_flush);
3269 exitfn_add_nullary(clean_up_pending_tmp_files);
3271 cc_log("=== CCACHE %s STARTED =========================================",
3274 if (conf->umask != UINT_MAX) {
3279 // Reset the global state. Used by the test suite.
3283 conf_free(conf); conf = NULL;
3284 free(primary_config_path); primary_config_path = NULL;
3285 free(secondary_config_path); secondary_config_path = NULL;
3286 free(current_working_dir); current_working_dir = NULL;
3287 for (size_t i = 0; i < debug_prefix_maps_len; i++) {
3288 free(debug_prefix_maps[i]);
3289 debug_prefix_maps[i] = NULL;
3291 free(debug_prefix_maps); debug_prefix_maps = NULL;
3292 debug_prefix_maps_len = 0;
3293 free(profile_dir); profile_dir = NULL;
3294 free(sanitize_blacklist); sanitize_blacklist = NULL;
3295 free(included_pch_file); included_pch_file = NULL;
3296 args_free(orig_args); orig_args = NULL;
3297 free(input_file); input_file = NULL;
3298 free(output_obj); output_obj = NULL;
3299 free(output_dep); output_dep = NULL;
3300 free(output_cov); output_cov = NULL;
3301 free(output_su); output_su = NULL;
3302 free(output_dia); output_dia = NULL;
3303 free(output_dwo); output_dwo = NULL;
3304 free(cached_obj_hash); cached_obj_hash = NULL;
3305 free(cached_stderr); cached_stderr = NULL;
3306 free(cached_obj); cached_obj = NULL;
3307 free(cached_dep); cached_dep = NULL;
3308 free(cached_cov); cached_cov = NULL;
3309 free(cached_su); cached_su = NULL;
3310 free(cached_dia); cached_dia = NULL;
3311 free(cached_dwo); cached_dwo = NULL;
3312 free(manifest_path); manifest_path = NULL;
3313 time_of_compilation = 0;
3314 for (size_t i = 0; i < ignore_headers_len; i++) {
3315 free(ignore_headers[i]);
3316 ignore_headers[i] = NULL;
3318 free(ignore_headers); ignore_headers = NULL;
3319 ignore_headers_len = 0;
3320 if (included_files) {
3321 hashtable_destroy(included_files, 1); included_files = NULL;
3323 has_absolute_include_headers = false;
3324 generating_debuginfo = false;
3325 generating_dependencies = false;
3326 generating_coverage = false;
3327 generating_stackusage = false;
3328 profile_arcs = false;
3329 free(profile_dir); profile_dir = NULL;
3331 direct_i_file = false;
3332 free(cpp_stderr); cpp_stderr = NULL;
3333 free(stats_file); stats_file = NULL;
3334 output_is_precompiled_header = false;
3336 conf = conf_create();
3337 using_split_dwarf = false;
3340 // Make a copy of stderr that will not be cached, so things like distcc can
3341 // send networking errors to it.
3343 set_up_uncached_err(void)
3345 int uncached_fd = dup(2); // The file descriptor is intentionally leaked.
3346 if (uncached_fd == -1) {
3347 cc_log("dup(2) failed: %s", strerror(errno));
3351 // Leak a pointer to the environment.
3352 char *buf = format("UNCACHED_ERR_FD=%d", uncached_fd);
3353 if (putenv(buf) == -1) {
3354 cc_log("putenv failed: %s", strerror(errno));
3360 configuration_logger(const char *descr, const char *origin, void *context)
3363 cc_bulklog("Config: (%s) %s", origin, descr);
3366 static void ccache(int argc, char *argv[]) ATTR_NORETURN;
3368 // The main ccache driver function.
3370 ccache(int argc, char *argv[])
3373 set_up_signal_handlers();
3376 orig_args = args_init(argc, argv);
3379 find_compiler(argv);
3381 if (str_eq(conf->temporary_dir, "")) {
3382 clean_up_internal_tempdir();
3385 if (!str_eq(conf->log_file, "") || conf->debug) {
3386 conf_print_items(conf, configuration_logger, NULL);
3389 if (conf->disable) {
3390 cc_log("ccache is disabled");
3394 set_up_uncached_err();
3396 cc_log_argv("Command line: ", argv);
3397 cc_log("Hostname: %s", get_hostname());
3398 cc_log("Working directory: %s", get_current_working_dir());
3400 conf->limit_multiple = MIN(MAX(conf->limit_multiple, 0.0), 1.0);
3402 guessed_compiler = guess_compiler(orig_args->argv[0]);
3404 // Arguments (except -E) to send to the preprocessor.
3405 struct args *preprocessor_args;
3406 // Arguments to send to the real compiler.
3407 struct args *compiler_args;
3408 if (!cc_process_args(orig_args, &preprocessor_args, &compiler_args)) {
3412 cc_log("Source file: %s", input_file);
3413 if (generating_dependencies) {
3414 cc_log("Dependency file: %s", output_dep);
3416 if (generating_coverage) {
3417 cc_log("Coverage file: %s", output_cov);
3419 if (generating_stackusage) {
3420 cc_log("Stack usage file: %s", output_su);
3422 if (generating_diagnostics) {
3423 cc_log("Diagnostics file: %s", output_dia);
3426 cc_log("Split dwarf file: %s", output_dwo);
3429 cc_log("Object file: %s", output_obj);
3431 // Need to dump log buffer as the last exit function to not lose any logs.
3432 exitfn_add_last(dump_log_buffer_exitfn, output_obj);
3434 FILE *debug_text_file = NULL;
3436 char *path = format("%s.ccache-input-text", output_obj);
3437 debug_text_file = fopen(path, "w");
3439 exitfn_add(fclose_exitfn, debug_text_file);
3442 struct hash *common_hash = hash_init();
3443 init_hash_debug(common_hash, output_obj, 'c', "COMMON", debug_text_file);
3445 calculate_common_hash(preprocessor_args, common_hash);
3447 // Try to find the hash using the manifest.
3448 struct hash *direct_hash = hash_copy(common_hash);
3450 direct_hash, output_obj, 'd', "DIRECT MODE", debug_text_file);
3452 bool put_object_in_manifest = false;
3453 struct file_hash *object_hash = NULL;
3454 struct file_hash *object_hash_from_manifest = NULL;
3455 if (conf->direct_mode) {
3456 cc_log("Trying direct lookup");
3457 object_hash = calculate_object_hash(preprocessor_args, direct_hash, 1);
3459 update_cached_result_globals(object_hash);
3461 // If we can return from cache at this point then do so.
3462 from_cache(FROMCACHE_DIRECT_MODE, 0);
3464 // Wasn't able to return from cache at this point. However, the object
3465 // was already found in manifest, so don't readd it later.
3466 put_object_in_manifest = false;
3468 object_hash_from_manifest = object_hash;
3470 // Add object to manifest later.
3471 put_object_in_manifest = true;
3475 if (conf->read_only_direct) {
3476 cc_log("Read-only direct mode; running real compiler");
3480 // Find the hash using the preprocessed output. Also updates included_files.
3481 struct hash *cpp_hash = hash_copy(common_hash);
3483 cpp_hash, output_obj, 'p', "PREPROCESSOR MODE", debug_text_file);
3485 object_hash = calculate_object_hash(preprocessor_args, cpp_hash, 0);
3487 fatal("internal error: object hash from cpp returned NULL");
3489 update_cached_result_globals(object_hash);
3491 if (object_hash_from_manifest
3492 && !file_hashes_equal(object_hash_from_manifest, object_hash)) {
3493 // The hash from manifest differs from the hash of the preprocessor output.
3494 // This could be because:
3496 // - The preprocessor produces different output for the same input (not
3498 // - There's a bug in ccache (maybe incorrect handling of compiler
3500 // - The user has used a different CCACHE_BASEDIR (most likely).
3502 // The best thing here would probably be to remove the hash entry from the
3503 // manifest. For now, we use a simpler method: just remove the manifest
3505 cc_log("Hash from manifest doesn't match preprocessor output");
3506 cc_log("Likely reason: different CCACHE_BASEDIRs used");
3507 cc_log("Removing manifest as a safety measure");
3508 x_unlink(manifest_path);
3510 put_object_in_manifest = true;
3513 // If we can return from cache at this point then do.
3514 from_cache(FROMCACHE_CPP_MODE, put_object_in_manifest);
3516 if (conf->read_only) {
3517 cc_log("Read-only mode; running real compiler");
3521 add_prefix(compiler_args, conf->prefix_command);
3523 // Run real compiler, sending output to cache.
3524 to_cache(compiler_args);
3530 configuration_printer(const char *descr, const char *origin, void *context)
3533 fprintf(context, "(%s) %s\n", origin, descr);
3536 // The main program when not doing a compile.
3538 ccache_main_options(int argc, char *argv[])
3544 static const struct option options[] = {
3545 {"cleanup", no_argument, 0, 'c'},
3546 {"clear", no_argument, 0, 'C'},
3547 {"dump-manifest", required_argument, 0, DUMP_MANIFEST},
3548 {"get-config", required_argument, 0, 'k'},
3549 {"hash-file", required_argument, 0, HASH_FILE},
3550 {"help", no_argument, 0, 'h'},
3551 {"max-files", required_argument, 0, 'F'},
3552 {"max-size", required_argument, 0, 'M'},
3553 {"print-config", no_argument, 0, 'p'},
3554 {"set-config", required_argument, 0, 'o'},
3555 {"show-stats", no_argument, 0, 's'},
3556 {"version", no_argument, 0, 'V'},
3557 {"zero-stats", no_argument, 0, 'z'},
3562 while ((c = getopt_long(argc, argv, "cCk:hF:M:po:sVz", options, NULL))
3566 manifest_dump(optarg, stdout);
3572 struct hash *hash = hash_init();
3573 if (str_eq(optarg, "-")) {
3574 hash_fd(hash, STDIN_FILENO);
3576 hash_file(hash, optarg);
3578 char *result = hash_result(hash);
3585 case 'c': // --cleanup
3588 printf("Cleaned cache\n");
3591 case 'C': // --clear
3594 printf("Cleared cache\n");
3598 fputs(USAGE_TEXT, stdout);
3601 case 'k': // --get-config
3605 if (!conf_print_value(conf, optarg, stdout, &errmsg)) {
3606 fatal("%s", errmsg);
3611 case 'F': // --max-files
3615 if (conf_set_value_in_file(primary_config_path, "max_files", optarg,
3617 unsigned files = atoi(optarg);
3619 printf("Unset cache file limit\n");
3621 printf("Set cache file limit to %u\n", files);
3624 fatal("could not set cache file limit: %s", errmsg);
3629 case 'M': // --max-size
3633 if (!parse_size_with_suffix(optarg, &size)) {
3634 fatal("invalid size: %s", optarg);
3637 if (conf_set_value_in_file(primary_config_path, "max_size", optarg,
3640 printf("Unset cache size limit\n");
3642 char *s = format_human_readable_size(size);
3643 printf("Set cache size limit to %s\n", s);
3647 fatal("could not set cache size limit: %s", errmsg);
3652 case 'o': // --set-config
3655 char *p = strchr(optarg, '=');
3657 fatal("missing equal sign in \"%s\"", optarg);
3659 char *key = x_strndup(optarg, p - optarg);
3660 char *value = p + 1;
3662 if (!conf_set_value_in_file(primary_config_path, key, value, &errmsg)) {
3663 fatal("%s", errmsg);
3669 case 'p': // --print-config
3671 conf_print_items(conf, configuration_printer, stdout);
3674 case 's': // --show-stats
3679 case 'V': // --version
3680 fprintf(stdout, VERSION_TEXT, CCACHE_VERSION);
3683 case 'z': // --zero-stats
3686 printf("Statistics zeroed\n");
3690 fputs(USAGE_TEXT, stderr);
3698 int ccache_main(int argc, char *argv[]);
3701 ccache_main(int argc, char *argv[])
3703 // Check if we are being invoked as "ccache".
3704 char *program_name = basename(argv[0]);
3705 if (same_executable_name(program_name, MYNAME)) {
3707 fputs(USAGE_TEXT, stderr);
3710 // If the first argument isn't an option, then assume we are being passed a
3711 // compiler name and options.
3712 if (argv[1][0] == '-') {
3713 return ccache_main_options(argc, argv);