2 * ccache -- a fast C/C++ compiler cache
4 * Copyright (C) 2002-2007 Andrew Tridgell
5 * Copyright (C) 2009-2015 Joel Rosdahl
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 3 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 51
19 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #ifdef HAVE_GETOPT_LONG
27 #include "getopt_long.h"
29 #include "hashtable.h"
30 #include "hashtable_itr.h"
35 #define STRINGIFY(x) #x
36 #define TO_STRING(x) STRINGIFY(x)
38 static const char VERSION_TEXT[] =
39 MYNAME " version %s\n"
41 "Copyright (C) 2002-2007 Andrew Tridgell\n"
42 "Copyright (C) 2009-2015 Joel Rosdahl\n"
44 "This program is free software; you can redistribute it and/or modify it under\n"
45 "the terms of the GNU General Public License as published by the Free Software\n"
46 "Foundation; either version 3 of the License, or (at your option) any later\n"
49 static const char USAGE_TEXT[] =
51 " " MYNAME " [options]\n"
52 " " MYNAME " compiler [compiler options]\n"
53 " compiler [compiler options] (via symbolic link)\n"
56 " -c, --cleanup delete old files and recalculate size counters\n"
57 " (normally not needed as this is done automatically)\n"
58 " -C, --clear clear the cache completely (except configuration)\n"
59 " -F, --max-files=N set maximum number of files in cache to N (use 0 for\n"
61 " -M, --max-size=SIZE set maximum size of cache to SIZE (use 0 for no\n"
62 " limit); available suffixes: k, M, G, T (decimal) and\n"
63 " Ki, Mi, Gi, Ti (binary); default suffix: G\n"
64 " -o, --set-config=K=V set configuration key K to value V\n"
65 " -p, --print-config print current configuration options\n"
66 " -s, --show-stats show statistics summary\n"
67 " -z, --zero-stats zero statistics counters\n"
69 " -h, --help print this help text\n"
70 " -V, --version print version and copyright information\n"
72 "See also <http://ccache.samba.org>.\n";
74 /* Global configuration data. */
75 struct conf *conf = NULL;
77 /* Where to write configuration changes. */
78 char *primary_config_path = NULL;
80 /* Secondary, read-only configuration file (if any). */
81 char *secondary_config_path = NULL;
83 /* current working directory taken from $PWD, or getcwd() if $PWD is bad */
84 char *current_working_dir = NULL;
86 /* the original argument list */
87 static struct args *orig_args;
90 static char *input_file;
92 /* The output file being compiled to. */
93 static char *output_obj;
95 /* The path to the dependency file (implicit or specified with -MF). */
96 static char *output_dep;
98 /* The path to the coverage file (implicit when using -ftest-coverage). */
99 static char *output_cov;
101 /* Diagnostic generation information (clang). Contains pathname if not
103 static char *output_dia = NULL;
105 /* -gsplit-dwarf support: Split dwarf information (GCC 4.8 and
106 * up). Contains pathname if not NULL. */
107 static char *output_dwo = NULL;
110 * Name (represented as a struct file_hash) of the file containing the cached
113 static struct file_hash *cached_obj_hash;
116 * Full path to the file containing the cached object code
117 * (cachedir/a/b/cdef[...]-size.o).
119 static char *cached_obj;
122 * Full path to the file containing the standard error output
123 * (cachedir/a/b/cdef[...]-size.stderr).
125 static char *cached_stderr;
128 * Full path to the file containing the dependency information
129 * (cachedir/a/b/cdef[...]-size.d).
131 static char *cached_dep;
134 * Full path to the file containing the coverage information
135 * (cachedir/a/b/cdef[...]-size.gcno).
137 static char *cached_cov;
140 * Full path to the file containing the diagnostic information (for clang)
141 * (cachedir/a/b/cdef[...]-size.dia).
143 static char *cached_dia;
146 * -gsplit-dwarf support:
147 * Full path to the file containing the split dwarf (for GCC 4.8 and
149 * (cachedir/a/b/cdef[...]-size.dwo).
151 * contains NULL if -gsplit-dwarf is not given.
153 static char *cached_dwo;
156 * -gsplit-dwarf support:
157 * using_split_dwarf is true if "-gsplit-dwarf" is given to the
158 * compiler (GCC 4.8 and up).
160 bool using_split_dwarf = false;
163 * Full path to the file containing the manifest
164 * (cachedir/a/b/cdef[...]-size.manifest).
166 static char *manifest_path;
169 * Time of compilation. Used to see if include files have changed after
172 time_t time_of_compilation;
175 * Files included by the preprocessor and their hashes/sizes. Key: file path.
176 * Value: struct file_hash.
178 static struct hashtable *included_files;
180 /* is gcc being asked to output dependencies? */
181 static bool generating_dependencies;
183 /* is gcc being asked to output coverage? */
184 static bool generating_coverage;
186 /* is gcc being asked to output coverage data (.gcda) at runtime? */
187 static bool profile_arcs;
189 /* name of the custom profile directory (default: object dirname) */
190 static char *profile_dir;
192 /* the name of the temporary pre-processor file */
193 static char *i_tmpfile;
195 /* are we compiling a .i or .ii file directly? */
196 static bool direct_i_file;
198 /* the name of the cpp stderr file */
199 static char *cpp_stderr;
202 * Full path to the statistics file in the subdirectory where the cached result
203 * belongs (<cache_dir>/<x>/stats).
205 char *stats_file = NULL;
207 /* Whether the output is a precompiled header */
208 static bool output_is_precompiled_header = false;
210 /* Profile generation / usage information */
211 static char *profile_dir = NULL;
212 static bool profile_use = false;
213 static bool profile_generate = false;
216 * Whether we are using a precompiled header (either via -include, #include or
217 * clang's -include-pch or -include-pth).
219 static bool using_precompiled_header = false;
222 * The .gch/.pch/.pth file used for compilation.
224 static char *included_pch_file = NULL;
226 /* How long (in microseconds) to wait before breaking a stale lock. */
227 unsigned lock_staleness_limit = 2000000;
229 enum fromcache_call_mode {
230 FROMCACHE_DIRECT_MODE,
234 struct pending_tmp_file {
236 struct pending_tmp_file *next;
239 /* Temporary files to remove at program exit. */
240 static struct pending_tmp_file *pending_tmp_files = NULL;
243 * This is a string that identifies the current "version" of the hash sum
244 * computed by ccache. If, for any reason, we want to force the hash sum to be
245 * different for the same input in a new ccache version, we can just change
246 * this string. A typical example would be if the format of one of the files
247 * stored in the cache changes in a backwards-incompatible way.
249 static const char HASH_PREFIX[] = "3";
252 add_prefix(struct args *args)
255 char *tok, *saveptr = NULL;
259 if (str_eq(conf->prefix_command, "")) {
263 prefix = args_init(0, NULL);
264 e = x_strdup(conf->prefix_command);
265 for (tok = strtok_r(e, " ", &saveptr);
267 tok = strtok_r(NULL, " ", &saveptr)) {
270 p = find_executable(tok, MYNAME);
272 fatal("%s: %s", tok, strerror(errno));
280 cc_log("Using command-line prefix %s", conf->prefix_command);
281 for (i = prefix->argc; i != 0; i--) {
282 args_add_prefix(args, prefix->argv[i-1]);
287 /* Something went badly wrong - just execute the real compiler. */
293 args_strip(orig_args, "--ccache-");
294 add_prefix(orig_args);
296 cc_log("Failed; falling back to running the real compiler");
297 cc_log_argv("Executing ", orig_args->argv);
299 execv(orig_args->argv[0], orig_args->argv);
300 fatal("execv of %s failed: %s", orig_args->argv[0], strerror(errno));
306 static char *path = NULL;
308 return path; /* Memoize */
310 path = conf->temporary_dir;
311 if (str_eq(path, "")) {
312 path = format("%s/tmp", conf->cache_dir);
318 add_pending_tmp_file(const char *path)
320 struct pending_tmp_file *e = x_malloc(sizeof(*e));
321 e->path = x_strdup(path);
322 e->next = pending_tmp_files;
323 pending_tmp_files = e;
327 clean_up_pending_tmp_files(void)
329 struct pending_tmp_file *p = pending_tmp_files;
331 /* Can't call tmp_unlink here since its cc_log calls aren't signal safe. */
334 /* Leak p->path and p here because clean_up_pending_tmp_files needs to be
340 signal_handler(int signo)
343 clean_up_pending_tmp_files();
348 clean_up_internal_tempdir(void)
351 struct dirent *entry;
353 time_t now = time(NULL);
355 if (x_stat(conf->cache_dir, &st) != 0 || st.st_mtime + 3600 >= now) {
356 /* No cleanup needed. */
360 update_mtime(conf->cache_dir);
362 dir = opendir(temp_dir());
367 while ((entry = readdir(dir))) {
370 if (str_eq(entry->d_name, ".") || str_eq(entry->d_name, "..")) {
374 path = format("%s/%s", temp_dir(), entry->d_name);
375 if (x_lstat(path, &st) == 0 && st.st_mtime + 3600 < now) {
385 get_current_working_dir(void)
387 if (!current_working_dir) {
388 char *cwd = get_cwd();
390 current_working_dir = x_realpath(cwd);
393 if (!current_working_dir) {
394 cc_log("Unable to determine current working directory: %s",
399 return current_working_dir;
403 * Transform a name to a full path into the cache directory, creating needed
404 * sublevels if needed. Caller frees.
407 get_path_in_cache(const char *name, const char *suffix)
413 path = x_strdup(conf->cache_dir);
414 for (i = 0; i < conf->cache_dir_levels; ++i) {
415 char *p = format("%s/%c", path, name[i]);
420 result = format("%s/%s%s", path, name + conf->cache_dir_levels, suffix);
426 * This function hashes an include file and stores the path and hash in the
427 * global included_files variable. If the include file is a PCH, cpp_hash is
428 * also updated. Takes over ownership of path.
431 remember_include_file(char *path, struct mdfour *cpp_hash)
441 size_t path_len = strlen(path);
443 if (path_len >= 2 && (path[0] == '<' && path[path_len - 1] == '>')) {
444 /* Typically <built-in> or <command-line>. */
448 if (str_eq(path, input_file)) {
449 /* Don't remember the input file. */
453 if (hashtable_search(included_files, path)) {
454 /* Already known include file. */
459 /* stat fails on directories on win32 */
460 attributes = GetFileAttributes(path);
461 if (attributes != INVALID_FILE_ATTRIBUTES &&
462 attributes & FILE_ATTRIBUTE_DIRECTORY)
466 if (x_stat(path, &st) != 0) {
469 if (S_ISDIR(st.st_mode)) {
470 /* Ignore directory, typically $PWD. */
473 if (!S_ISREG(st.st_mode)) {
474 /* Device, pipe, socket or other strange creature. */
475 cc_log("Non-regular include file %s", path);
479 /* Let's hash the include file. */
480 if (!(conf->sloppiness & SLOPPY_INCLUDE_FILE_MTIME)
481 && st.st_mtime >= time_of_compilation) {
482 cc_log("Include file %s too new", path);
486 if (!(conf->sloppiness & SLOPPY_INCLUDE_FILE_CTIME)
487 && st.st_ctime >= time_of_compilation) {
488 cc_log("Include file %s ctime too new", path);
494 is_pch = is_precompiled_header(path);
496 struct file_hash pch_hash;
497 if (!hash_file(&fhash, path)) {
500 hash_result_as_bytes(&fhash, pch_hash.hash);
501 pch_hash.size = fhash.totalN;
502 hash_delimiter(cpp_hash, "pch_hash");
503 hash_buffer(cpp_hash, pch_hash.hash, sizeof(pch_hash.hash));
505 if (conf->direct_mode) {
508 if (!is_pch) { /* else: the file has already been hashed. */
511 if (st.st_size > 0) {
512 if (!read_file(path, st.st_size, &source, &size)) {
516 source = x_strdup("");
520 result = hash_source_code_string(conf, &fhash, source, size, path);
521 if (result & HASH_SOURCE_CODE_ERROR
522 || result & HASH_SOURCE_CODE_FOUND_TIME) {
527 h = x_malloc(sizeof(*h));
528 hash_result_as_bytes(&fhash, h->hash);
529 h->size = fhash.totalN;
530 hashtable_insert(included_files, path, h);
539 if (conf->direct_mode) {
540 cc_log("Disabling direct mode");
541 conf->direct_mode = false;
550 * Make a relative path from current working directory to path if path is under
551 * the base directory. Takes over ownership of path. Caller frees.
554 make_relative_path(char *path)
556 char *relpath, *canon_path, *path_suffix = NULL;
559 if (str_eq(conf->base_dir, "") || !str_startswith(path, conf->base_dir)) {
563 /* x_realpath only works for existing paths, so if path doesn't exist, try
564 * dirname(path) and assemble the path afterwards. We only bother to try
565 * canonicalizing one of these two paths since a compiler path argument
566 * typically only makes sense if path or dirname(path) exists. */
567 if (stat(path, &st) != 0) {
568 /* path doesn't exist. */
571 if (stat(dir, &st) != 0) {
572 /* And neither does its parent directory, so no action to take. */
576 path_suffix = basename(path);
578 path = dirname(path);
582 canon_path = x_realpath(path);
585 relpath = get_relative_path(get_current_working_dir(), canon_path);
588 path = format("%s/%s", relpath, path_suffix);
596 /* path doesn't exist, so leave it as it is. */
603 * This function reads and hashes a file. While doing this, it also does these
606 * - Makes include file paths for which the base directory is a prefix relative
607 * when computing the hash sum.
608 * - Stores the paths and hashes of included files in the global variable
612 process_preprocessed_file(struct mdfour *hash, const char *path)
618 if (!read_file(path, 0, &data, &size)) {
622 included_files = create_hashtable(1000, hash_from_string, strings_equal);
624 /* Bytes between p and q are pending to be hashed. */
628 /* There must be at least 7 characters (# 1 "x") left to potentially find an
629 * include file path. */
630 while (q < end - 7) {
632 * Check if we look at a line containing the file name of an included file.
633 * At least the following formats exist (where N is a positive integer):
639 * #pragma GCC pch_preprocess "file"
650 * Note that there may be other lines starting with '#' left after
651 * preprocessing as well, for instance "# pragma".
655 && ((q[1] == ' ' && q[2] >= '0' && q[2] <= '9')
656 /* GCC precompiled header: */
658 && str_startswith(&q[2], "ragma GCC pch_preprocess "))
660 || (q[1] == 'l' && q[2] == 'i' && q[3] == 'n' && q[4] == 'e'
662 && (q == data || q[-1] == '\n')) {
665 while (q < end && *q != '"' && *q != '\n') {
668 if (q < end && *q == '\n') {
669 /* A newline before the quotation mark -> no match. */
674 cc_log("Failed to parse included file path");
678 /* q points to the beginning of an include file path */
679 hash_buffer(hash, p, q - p);
681 while (q < end && *q != '"') {
684 /* p and q span the include file path */
685 path = x_strndup(p, q - p);
686 path = make_relative_path(path);
687 hash_string(hash, path);
688 remember_include_file(path, hash);
695 hash_buffer(hash, p, (end - p));
698 /* Explicitly check the .gch/.pch/.pth file, Clang does not include any
699 * mention of it in the preprocessed output. */
700 if (included_pch_file) {
701 char *path = x_strdup(included_pch_file);
702 path = make_relative_path(path);
703 hash_string(hash, path);
704 remember_include_file(path, hash);
710 /* Copy or link a file to the cache. */
712 put_file_in_cache(const char *source, const char *dest)
716 bool do_link = conf->hard_link && !conf->compression;
720 ret = link(source, dest);
722 ret = copy_file(source, dest, conf->compression);
725 cc_log("Failed to %s %s to %s: %s",
726 do_link ? "link" : "copy",
730 stats_update(STATS_ERROR);
733 cc_log("Stored in cache: %s -> %s", source, dest);
734 if (x_stat(dest, &st) != 0) {
735 stats_update(STATS_ERROR);
738 stats_update_size(file_size(&st), 1);
741 /* Copy or link a file from the cache. */
743 get_file_from_cache(const char *source, const char *dest)
746 bool do_link = conf->hard_link && !file_is_compressed(source);
750 ret = link(source, dest);
752 ret = copy_file(source, dest, 0);
756 if (errno == ENOENT) {
757 /* Someone removed the file just before we began copying? */
758 cc_log("Cache file %s just disappeared from cache", source);
759 stats_update(STATS_MISSING);
761 cc_log("Failed to %s %s to %s: %s",
762 do_link ? "link" : "copy",
766 stats_update(STATS_ERROR);
769 /* If there was trouble getting a file from the cached result, wipe the
770 * whole cached result for consistency. */
771 x_unlink(cached_stderr);
772 x_unlink(cached_obj);
773 x_unlink(cached_dep);
774 x_unlink(cached_dia);
779 cc_log("Created from cache: %s -> %s", source, dest);
782 /* Send cached stderr, if any, to stderr. */
784 send_cached_stderr(void)
786 int fd_stderr = open(cached_stderr, O_RDONLY | O_BINARY);
787 if (fd_stderr != -1) {
788 copy_fd(fd_stderr, 2);
793 /* Create or update the manifest file. */
794 void update_manifest_file(void)
797 size_t old_size = 0; /* in bytes */
799 if (!conf->direct_mode
802 || conf->read_only_direct) {
806 if (stat(manifest_path, &st) == 0) {
807 old_size = file_size(&st);
809 if (manifest_put(manifest_path, cached_obj_hash, included_files)) {
810 cc_log("Added object file hash to %s", manifest_path);
811 update_mtime(manifest_path);
812 if (x_stat(manifest_path, &st) == 0) {
813 stats_update_size(file_size(&st) - old_size, old_size == 0 ? 1 : 0);
816 cc_log("Failed to add object file hash to %s", manifest_path);
820 /* run the real compiler and put the result in cache */
822 to_cache(struct args *args)
824 char *tmp_stdout, *tmp_stderr, *tmp_aux, *tmp_cov;
825 char *tmp_dwo = NULL;
827 int status, tmp_stdout_fd, tmp_stderr_fd;
830 tmp_stdout = format("%s.tmp.stdout", cached_obj);
831 tmp_stdout_fd = create_tmp_fd(&tmp_stdout);
832 tmp_stderr = format("%s.tmp.stderr", cached_obj);
833 tmp_stderr_fd = create_tmp_fd(&tmp_stderr);
835 if (generating_coverage) {
836 /* gcc has some funny rule about max extension length */
837 if (strlen(get_extension(output_obj)) < 6) {
838 tmp_aux = remove_extension(output_obj);
840 tmp_aux = x_strdup(output_obj);
842 tmp_cov = format("%s.gcno", tmp_aux);
848 /* GCC (at least 4.8 and 4.9) forms the .dwo file name by removing everything
849 * after (and including) the last "." from the object file name and then
852 if (using_split_dwarf) {
853 char *base_name = remove_extension(output_obj);
854 tmp_dwo = format("%s.dwo", base_name);
858 args_add(args, "-o");
859 args_add(args, output_obj);
862 args_add(args, "--serialize-diagnostics");
863 args_add(args, output_dia);
866 /* Turn off DEPENDENCIES_OUTPUT when running cc1, because
867 * otherwise it will emit a line like
869 * tmp.stdout.vexed.732.o: /home/mbp/.ccache/tmp.stdout.vexed.732.i
871 x_unsetenv("DEPENDENCIES_OUTPUT");
873 if (conf->run_second_cpp) {
874 args_add(args, input_file);
876 args_add(args, i_tmpfile);
879 cc_log("Running real compiler");
880 status = execute(args->argv, tmp_stdout_fd, tmp_stderr_fd);
883 if (x_stat(tmp_stdout, &st) != 0) {
884 /* The stdout file was removed - cleanup in progress? Better bail out. */
885 stats_update(STATS_MISSING);
886 tmp_unlink(tmp_stdout);
887 tmp_unlink(tmp_stderr);
894 if (st.st_size != 0) {
895 cc_log("Compiler produced stdout");
896 stats_update(STATS_STDOUT);
897 tmp_unlink(tmp_stdout);
898 tmp_unlink(tmp_stderr);
905 tmp_unlink(tmp_stdout);
908 * Merge stderr from the preprocessor (if any) and stderr from the real
909 * compiler into tmp_stderr.
917 tmp_stderr2 = format("%s.2", tmp_stderr);
918 if (x_rename(tmp_stderr, tmp_stderr2)) {
919 cc_log("Failed to rename %s to %s: %s", tmp_stderr, tmp_stderr2,
923 fd_cpp_stderr = open(cpp_stderr, O_RDONLY | O_BINARY);
924 if (fd_cpp_stderr == -1) {
925 cc_log("Failed opening %s: %s", cpp_stderr, strerror(errno));
928 fd_real_stderr = open(tmp_stderr2, O_RDONLY | O_BINARY);
929 if (fd_real_stderr == -1) {
930 cc_log("Failed opening %s: %s", tmp_stderr2, strerror(errno));
933 fd_result = open(tmp_stderr, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
934 if (fd_result == -1) {
935 cc_log("Failed opening %s: %s", tmp_stderr, strerror(errno));
938 copy_fd(fd_cpp_stderr, fd_result);
939 copy_fd(fd_real_stderr, fd_result);
940 close(fd_cpp_stderr);
941 close(fd_real_stderr);
943 tmp_unlink(tmp_stderr2);
949 cc_log("Compiler gave exit status %d", status);
950 stats_update(STATS_STATUS);
952 fd = open(tmp_stderr, O_RDONLY | O_BINARY);
954 /* We can output stderr immediately instead of rerunning the compiler. */
957 tmp_unlink(tmp_stderr);
962 tmp_unlink(tmp_stderr);
971 if (stat(output_obj, &st) != 0) {
972 cc_log("Compiler didn't produce an object file");
973 stats_update(STATS_NOOUTPUT);
976 if (st.st_size == 0) {
977 cc_log("Compiler produced an empty object file");
978 stats_update(STATS_EMPTYOUTPUT);
982 if (using_split_dwarf) {
983 if (stat(tmp_dwo, &st) != 0) {
984 cc_log("Compiler didn't produce a split dwarf file");
985 stats_update(STATS_NOOUTPUT);
988 if (st.st_size == 0) {
989 cc_log("Compiler produced an empty split dwarf file");
990 stats_update(STATS_EMPTYOUTPUT);
995 if (x_stat(tmp_stderr, &st) != 0) {
996 stats_update(STATS_ERROR);
999 if (st.st_size > 0) {
1000 if (move_uncompressed_file(
1001 tmp_stderr, cached_stderr,
1002 conf->compression ? conf->compression_level : 0) != 0) {
1003 cc_log("Failed to move %s to %s: %s", tmp_stderr, cached_stderr,
1005 stats_update(STATS_ERROR);
1008 cc_log("Stored in cache: %s", cached_stderr);
1009 if (!conf->compression
1010 /* If the file was compressed, obtain the size again: */
1011 || (conf->compression && x_stat(cached_stderr, &st) == 0)) {
1012 stats_update_size(file_size(&st), 1);
1015 tmp_unlink(tmp_stderr);
1016 if (conf->recache) {
1017 /* If recaching, we need to remove any previous .stderr. */
1018 x_unlink(cached_stderr);
1022 if (generating_coverage) {
1023 /* gcc won't generate notes if there is no code */
1024 if (stat(tmp_cov, &st) != 0 && errno == ENOENT) {
1025 cc_log("Creating placeholder: %s", cached_cov);
1027 f = fopen(cached_cov, "wb");
1029 cc_log("Failed to create %s: %s", cached_cov, strerror(errno));
1030 stats_update(STATS_ERROR);
1034 stats_update_size(0, 1);
1036 put_file_in_cache(tmp_cov, cached_cov);
1041 if (x_stat(output_dia, &st) != 0) {
1042 stats_update(STATS_ERROR);
1045 if (st.st_size > 0) {
1046 put_file_in_cache(output_dia, cached_dia);
1050 put_file_in_cache(output_obj, cached_obj);
1052 if (using_split_dwarf) {
1055 put_file_in_cache(tmp_dwo, cached_dwo);
1058 if (generating_dependencies) {
1059 put_file_in_cache(output_dep, cached_dep);
1061 stats_update(STATS_TOCACHE);
1063 /* Make sure we have a CACHEDIR.TAG in the cache part of cache_dir. This can
1064 * be done almost anywhere, but we might as well do it near the end as we
1065 * save the stat call if we exit early.
1068 char *first_level_dir = dirname(stats_file);
1069 if (create_cachedirtag(first_level_dir) != 0) {
1070 cc_log("Failed to create %s/CACHEDIR.TAG (%s)\n",
1071 first_level_dir, strerror(errno));
1072 stats_update(STATS_ERROR);
1075 free(first_level_dir);
1077 /* Remove any CACHEDIR.TAG on the cache_dir level where it was located in
1078 * previous ccache versions. */
1079 if (getpid() % 1000 == 0) {
1080 char *path = format("%s/CACHEDIR.TAG", conf->cache_dir);
1086 /* Everything OK. */
1087 send_cached_stderr();
1088 update_manifest_file();
1097 * Find the object file name by running the compiler in preprocessor mode.
1098 * Returns the hash as a heap-allocated hex string.
1100 static struct file_hash *
1101 get_object_name_from_cpp(struct args *args, struct mdfour *hash)
1105 char *path_stdout, *path_stderr;
1106 int status, path_stderr_fd;
1107 struct file_hash *result;
1109 /* ~/hello.c -> tmp.hello.123.i
1110 * limit the basename to 10
1111 * characters in order to cope with filesystem with small
1112 * maximum filename length limits */
1113 input_base = basename(input_file);
1114 tmp = strchr(input_base, '.');
1118 if (strlen(input_base) > 10) {
1122 path_stderr = format("%s/tmp.cpp_stderr", temp_dir());
1123 path_stderr_fd = create_tmp_fd(&path_stderr);
1124 add_pending_tmp_file(path_stderr);
1126 time_of_compilation = time(NULL);
1128 if (direct_i_file) {
1129 /* We are compiling a .i or .ii file - that means we can skip the cpp stage
1130 * and directly form the correct i_tmpfile. */
1131 path_stdout = input_file;
1134 /* Run cpp on the input file to obtain the .i. */
1136 path_stdout = format("%s/%s.stdout", temp_dir(), input_base);
1137 path_stdout_fd = create_tmp_fd(&path_stdout);
1138 add_pending_tmp_file(path_stdout);
1140 args_add(args, "-E");
1141 args_add(args, input_file);
1142 cc_log("Running preprocessor");
1143 status = execute(args->argv, path_stdout_fd, path_stderr_fd);
1148 cc_log("Preprocessor gave exit status %d", status);
1149 stats_update(STATS_PREPROCESSOR);
1154 /* When we are doing the unifying tricks we need to include the input file
1155 * name in the hash to get the warnings right. */
1156 hash_delimiter(hash, "unifyfilename");
1157 hash_string(hash, input_file);
1159 hash_delimiter(hash, "unifycpp");
1160 if (unify_hash(hash, path_stdout) != 0) {
1161 stats_update(STATS_ERROR);
1162 cc_log("Failed to unify %s", path_stdout);
1166 hash_delimiter(hash, "cpp");
1167 if (!process_preprocessed_file(hash, path_stdout)) {
1168 stats_update(STATS_ERROR);
1173 hash_delimiter(hash, "cppstderr");
1174 if (!hash_file(hash, path_stderr)) {
1175 fatal("Failed to open %s: %s", path_stderr, strerror(errno));
1178 if (direct_i_file) {
1179 i_tmpfile = input_file;
1181 /* i_tmpfile needs the proper cpp_extension for the compiler to do its
1182 * thing correctly. */
1183 i_tmpfile = format("%s.%s", path_stdout, conf->cpp_extension);
1184 x_rename(path_stdout, i_tmpfile);
1185 add_pending_tmp_file(i_tmpfile);
1188 if (conf->run_second_cpp) {
1192 * If we are using the CPP trick, we need to remember this
1193 * stderr data and output it just before the main stderr from
1194 * the compiler pass.
1196 cpp_stderr = path_stderr;
1197 hash_delimiter(hash, "runsecondcpp");
1198 hash_string(hash, "false");
1201 result = x_malloc(sizeof(*result));
1202 hash_result_as_bytes(hash, result->hash);
1203 result->size = hash->totalN;
1208 update_cached_result_globals(struct file_hash *hash)
1211 object_name = format_hash_as_string(hash->hash, hash->size);
1212 cached_obj_hash = hash;
1213 cached_obj = get_path_in_cache(object_name, ".o");
1214 cached_stderr = get_path_in_cache(object_name, ".stderr");
1215 cached_dep = get_path_in_cache(object_name, ".d");
1216 cached_cov = get_path_in_cache(object_name, ".gcno");
1217 cached_dia = get_path_in_cache(object_name, ".dia");
1219 if (using_split_dwarf) {
1220 cached_dwo = get_path_in_cache(object_name, ".dwo");
1225 stats_file = format("%s/%c/stats", conf->cache_dir, object_name[0]);
1230 * Hash mtime or content of a file, or the output of a command, according to
1231 * the CCACHE_COMPILERCHECK setting.
1234 hash_compiler(struct mdfour *hash, struct stat *st, const char *path,
1237 if (str_eq(conf->compiler_check, "none")) {
1239 } else if (str_eq(conf->compiler_check, "mtime")) {
1240 hash_delimiter(hash, "cc_mtime");
1241 hash_int(hash, st->st_size);
1242 hash_int(hash, st->st_mtime);
1243 } else if (str_startswith(conf->compiler_check, "string:")) {
1244 hash_delimiter(hash, "cc_hash");
1245 hash_string(hash, conf->compiler_check + strlen("string:"));
1246 } else if (str_eq(conf->compiler_check, "content") || !allow_command) {
1247 hash_delimiter(hash, "cc_content");
1248 hash_file(hash, path);
1249 } else { /* command string */
1250 if (!hash_multicommand_output(
1251 hash, conf->compiler_check, orig_args->argv[0])) {
1252 fatal("Failure running compiler check command: %s", conf->compiler_check);
1258 * Note that these compiler checks are unreliable, so nothing should
1259 * hard-depend on them.
1263 compiler_is_clang(struct args *args)
1265 char *name = basename(args->argv[0]);
1266 bool is = strstr(name, "clang");
1272 compiler_is_gcc(struct args *args)
1274 char *name = basename(args->argv[0]);
1275 bool is = strstr(name, "gcc") || strstr(name, "g++");
1281 * Update a hash sum with information common for the direct and preprocessor
1285 calculate_common_hash(struct args *args, struct mdfour *hash)
1289 const char *full_path = args->argv[0];
1292 char full_path_win_ext[MAX_PATH + 1] = {0};
1295 hash_string(hash, HASH_PREFIX);
1298 * We have to hash the extension, as a .i file isn't treated the same
1299 * by the compiler as a .ii file.
1301 hash_delimiter(hash, "ext");
1302 hash_string(hash, conf->cpp_extension);
1305 ext = strrchr(args->argv[0], '.');
1306 add_exe_ext_if_no_to_fullpath(full_path_win_ext, MAX_PATH, ext,
1308 full_path = full_path_win_ext;
1311 if (x_stat(full_path, &st) != 0) {
1312 stats_update(STATS_COMPILER);
1317 * Hash information about the compiler.
1319 hash_compiler(hash, &st, args->argv[0], true);
1322 * Also hash the compiler name as some compilers use hard links and
1323 * behave differently depending on the real name.
1325 hash_delimiter(hash, "cc_name");
1326 p = basename(args->argv[0]);
1327 hash_string(hash, p);
1330 /* Possibly hash the current working directory. */
1331 if (conf->hash_dir) {
1332 char *cwd = gnu_getcwd();
1334 hash_delimiter(hash, "cwd");
1335 hash_string(hash, cwd);
1340 /* Possibly hash the coverage data file path. */
1341 if (generating_coverage && profile_arcs) {
1343 char *dir = dirname(output_obj);
1345 dir = x_strdup(profile_dir);
1347 char *real_dir = x_realpath(dir);
1352 char *base_name = basename(output_obj);
1353 p = remove_extension(base_name);
1355 gcda_path = format("%s/%s.gcda", dir, p);
1356 cc_log("Hashing coverage path %s", gcda_path);
1358 hash_delimiter(hash, "gcda");
1359 hash_string(hash, gcda_path);
1364 if (!str_eq(conf->extra_files_to_hash, "")) {
1365 char *path, *p, *q, *saveptr = NULL;
1366 p = x_strdup(conf->extra_files_to_hash);
1368 while ((path = strtok_r(q, PATH_DELIM, &saveptr))) {
1369 cc_log("Hashing extra file %s", path);
1370 hash_delimiter(hash, "extrafile");
1371 if (!hash_file(hash, path)) {
1372 stats_update(STATS_BADEXTRAFILE);
1380 /* Possibly hash GCC_COLORS (for color diagnostics). */
1381 if (compiler_is_gcc(args)) {
1382 const char *gcc_colors = getenv("GCC_COLORS");
1384 hash_delimiter(hash, "gcccolors");
1385 hash_string(hash, gcc_colors);
1391 * Update a hash sum with information specific to the direct and preprocessor
1392 * modes and calculate the object hash. Returns the object hash on success,
1393 * otherwise NULL. Caller frees.
1395 static struct file_hash *
1396 calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode)
1399 char *manifest_name;
1402 struct file_hash *object_hash = NULL;
1406 hash_delimiter(hash, "manifest version");
1407 hash_int(hash, MANIFEST_VERSION);
1410 /* first the arguments */
1411 for (i = 1; i < args->argc; i++) {
1412 /* -L doesn't affect compilation. */
1413 if (i < args->argc-1 && str_eq(args->argv[i], "-L")) {
1417 if (str_startswith(args->argv[i], "-L")) {
1421 /* -Wl,... doesn't affect compilation. */
1422 if (str_startswith(args->argv[i], "-Wl,")) {
1426 /* The -fdebug-prefix-map option may be used in combination with
1427 * CCACHE_BASEDIR to reuse results across different directories. Skip it
1429 if (str_startswith(args->argv[i], "-fdebug-prefix-map=")) {
1433 /* When using the preprocessor, some arguments don't contribute
1434 * to the hash. The theory is that these arguments will change
1435 * the output of -E if they are going to have any effect at
1436 * all. For precompiled headers this might not be the case. */
1437 if (!direct_mode && !output_is_precompiled_header
1438 && !using_precompiled_header) {
1439 if (compopt_affects_cpp(args->argv[i])) {
1443 if (compopt_short(compopt_affects_cpp, args->argv[i])) {
1448 /* If we're generating dependencies, we make sure to skip the
1449 * filename of the dependency file, since it doesn't impact the
1452 if (generating_dependencies) {
1453 if (str_startswith(args->argv[i], "-Wp,")) {
1454 if (str_startswith(args->argv[i], "-Wp,-MD,")
1455 && !strchr(args->argv[i] + 8, ',')) {
1456 hash_string_length(hash, args->argv[i], 8);
1458 } else if (str_startswith(args->argv[i], "-Wp,-MMD,")
1459 && !strchr(args->argv[i] + 9, ',')) {
1460 hash_string_length(hash, args->argv[i], 9);
1463 } else if (str_startswith(args->argv[i], "-MF")) {
1464 bool separate_argument = (strlen(args->argv[i]) == 3);
1466 /* In either case, hash the "-MF" part. */
1467 hash_string_length(hash, args->argv[i], 3);
1469 if (separate_argument) {
1470 /* Next argument is dependency name, so
1479 if (str_startswith(args->argv[i], "-specs=")) {
1480 p = args->argv[i] + 7;
1481 } else if (str_startswith(args->argv[i], "--specs=")) {
1482 p = args->argv[i] + 8;
1484 if (p && x_stat(p, &st) == 0) {
1485 /* If given an explicit specs file, then hash that file,
1486 * but don't include the path to it in the hash. */
1487 hash_delimiter(hash, "specs");
1488 hash_compiler(hash, &st, p, false);
1492 if (str_startswith(args->argv[i], "-fplugin=")
1493 && x_stat(args->argv[i] + 9, &st) == 0) {
1494 hash_delimiter(hash, "plugin");
1495 hash_compiler(hash, &st, args->argv[i] + 9, false);
1499 if (str_eq(args->argv[i], "-Xclang")
1500 && i + 3 < args->argc
1501 && str_eq(args->argv[i+1], "-load")
1502 && str_eq(args->argv[i+2], "-Xclang")
1503 && x_stat(args->argv[i+3], &st) == 0) {
1504 hash_delimiter(hash, "plugin");
1505 hash_compiler(hash, &st, args->argv[i+3], false);
1509 /* All other arguments are included in the hash. */
1510 hash_delimiter(hash, "arg");
1511 hash_string(hash, args->argv[i]);
1515 * For profile generation (-fprofile-arcs, -fprofile-generate):
1516 * - hash profile directory
1517 * - output to the real file first
1519 * For profile usage (-fprofile-use):
1520 * - hash profile data
1522 * -fbranch-probabilities and -fvpt usage is covered by
1523 * -fprofile-generate/-fprofile-use.
1525 * The profile directory can be specified as an argument to
1526 * -fprofile-generate=, -fprofile-use=, or -fprofile-dir=.
1530 * We need to output to the real object first here, otherwise runtime
1531 * artifacts will be produced in the wrong place.
1533 if (profile_generate) {
1535 profile_dir = get_cwd();
1537 cc_log("Adding profile directory %s to our hash", profile_dir);
1538 hash_delimiter(hash, "-fprofile-dir");
1539 hash_string(hash, profile_dir);
1543 /* Calculate gcda name */
1546 base_name = remove_extension(output_obj);
1548 profile_dir = get_cwd();
1550 gcda_name = format("%s/%s.gcda", profile_dir, base_name);
1551 cc_log("Adding profile data %s to our hash", gcda_name);
1552 /* Add the gcda to our hash */
1553 hash_delimiter(hash, "-fprofile-use");
1554 hash_file(hash, gcda_name);
1560 /* Hash environment variables that affect the preprocessor output. */
1562 const char *envvars[] = {
1565 "CPLUS_INCLUDE_PATH",
1566 "OBJC_INCLUDE_PATH",
1567 "OBJCPLUS_INCLUDE_PATH", /* clang */
1570 for (p = envvars; *p; ++p) {
1571 char *v = getenv(*p);
1573 hash_delimiter(hash, *p);
1574 hash_string(hash, v);
1578 if (!(conf->sloppiness & SLOPPY_FILE_MACRO)) {
1580 * The source code file or an include file may contain
1581 * __FILE__, so make sure that the hash is unique for
1584 hash_delimiter(hash, "inputfile");
1585 hash_string(hash, input_file);
1588 hash_delimiter(hash, "sourcecode");
1589 result = hash_source_code_file(conf, hash, input_file);
1590 if (result & HASH_SOURCE_CODE_ERROR) {
1593 if (result & HASH_SOURCE_CODE_FOUND_TIME) {
1594 cc_log("Disabling direct mode");
1595 conf->direct_mode = false;
1598 manifest_name = hash_result(hash);
1599 manifest_path = get_path_in_cache(manifest_name, ".manifest");
1600 free(manifest_name);
1601 cc_log("Looking for object file hash in %s", manifest_path);
1602 object_hash = manifest_get(conf, manifest_path);
1604 cc_log("Got object file hash from manifest");
1606 cc_log("Did not find object file hash in manifest");
1609 object_hash = get_object_name_from_cpp(args, hash);
1610 cc_log("Got object file hash from preprocessor");
1611 if (generating_dependencies) {
1612 cc_log("Preprocessor created %s", output_dep);
1620 * Try to return the compile result from cache. If we can return from cache
1621 * then this function exits with the correct status code, otherwise it returns.
1624 from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest)
1627 bool produce_dep_file = false;
1629 /* the user might be disabling cache hits */
1630 if (conf->recache) {
1634 if (stat(cached_obj, &st) != 0) {
1635 cc_log("Object file %s not in cache", cached_obj);
1639 /* Check if the diagnostic file is there. */
1640 if (output_dia && stat(cached_dia, &st) != 0) {
1641 cc_log("Diagnostic file %s not in cache", cached_dia);
1646 * Occasionally, e.g. on hard reset, our cache ends up as just filesystem
1647 * meta-data with no content. Catch an easy case of this.
1649 if (st.st_size == 0) {
1650 cc_log("Invalid (empty) object file %s in cache", cached_obj);
1651 x_unlink(cached_obj);
1655 if (using_split_dwarf && !generating_dependencies) {
1660 if (stat(cached_dwo, &st) != 0) {
1661 cc_log("Split dwarf file %s not in cache", cached_dwo);
1664 if (st.st_size == 0) {
1665 cc_log("Invalid (empty) dwo file %s in cache", cached_dwo);
1666 x_unlink(cached_dwo);
1667 x_unlink(cached_obj); /* to really invalidate */
1673 * (If mode != FROMCACHE_DIRECT_MODE, the dependency file is created by
1676 produce_dep_file = generating_dependencies && mode == FROMCACHE_DIRECT_MODE;
1678 /* If the dependency file should be in the cache, check that it is. */
1679 if (produce_dep_file && stat(cached_dep, &st) != 0) {
1680 cc_log("Dependency file %s missing in cache", cached_dep);
1685 * Copy object file from cache. Do so also for FissionDwarf file, cached_dwo,
1686 * when -gsplit-dwarf is specified.
1688 if (!str_eq(output_obj, "/dev/null")) {
1689 get_file_from_cache(cached_obj, output_obj);
1690 if (using_split_dwarf) {
1692 get_file_from_cache(cached_dwo, output_dwo);
1695 if (produce_dep_file) {
1696 get_file_from_cache(cached_dep, output_dep);
1698 if (generating_coverage && stat(cached_cov, &st) == 0 && st.st_size > 0) {
1699 /* gcc won't generate notes if there is no code */
1700 get_file_from_cache(cached_cov, output_cov);
1703 get_file_from_cache(cached_dia, output_dia);
1706 /* Update modification timestamps to save files from LRU cleanup.
1707 * Also gives files a sensible mtime when hard-linking. */
1708 update_mtime(cached_obj);
1709 update_mtime(cached_stderr);
1710 if (produce_dep_file) {
1711 update_mtime(cached_dep);
1713 if (generating_coverage) {
1714 update_mtime(cached_cov);
1717 update_mtime(cached_dia);
1720 update_mtime(cached_dwo);
1723 if (generating_dependencies && mode == FROMCACHE_CPP_MODE) {
1724 put_file_in_cache(output_dep, cached_dep);
1727 send_cached_stderr();
1729 if (put_object_in_manifest) {
1730 update_manifest_file();
1733 /* log the cache hit */
1735 case FROMCACHE_DIRECT_MODE:
1736 cc_log("Succeeded getting cached result");
1737 stats_update(STATS_CACHEHIT_DIR);
1740 case FROMCACHE_CPP_MODE:
1741 cc_log("Succeeded getting cached result");
1742 stats_update(STATS_CACHEHIT_CPP);
1746 /* and exit with the right status code */
1750 /* find the real compiler. We just search the PATH to find a executable of the
1751 * same name that isn't a link to ourselves */
1753 find_compiler(char **argv)
1758 base = basename(argv[0]);
1760 /* we might be being invoked like "ccache gcc -c foo.c" */
1761 if (same_executable_name(base, MYNAME)) {
1762 args_remove_first(orig_args);
1764 if (is_full_path(orig_args->argv[0])) {
1765 /* a full path was given */
1768 base = basename(orig_args->argv[0]);
1771 /* support user override of the compiler */
1772 if (!str_eq(conf->compiler, "")) {
1773 base = conf->compiler;
1776 compiler = find_executable(base, MYNAME);
1778 /* can't find the compiler! */
1780 stats_update(STATS_COMPILER);
1781 fatal("Could not find compiler \"%s\" in PATH", base);
1783 if (str_eq(compiler, argv[0])) {
1784 fatal("Recursive invocation (the name of the ccache binary must be \"%s\")",
1787 orig_args->argv[0] = compiler;
1791 is_precompiled_header(const char *path)
1793 return str_eq(get_extension(path), ".gch")
1794 || str_eq(get_extension(path), ".pch")
1795 || str_eq(get_extension(path), ".pth");
1799 color_output_possible(void)
1801 const char *term_env = getenv("TERM");
1802 return isatty(STDERR_FILENO) && term_env && strcasecmp(term_env, "DUMB") != 0;
1806 * Process the compiler options into options suitable for passing to the
1807 * preprocessor and the real compiler. The preprocessor options don't include
1808 * -E; this is added later. Returns true on success, otherwise false.
1811 cc_process_args(struct args *args, struct args **preprocessor_args,
1812 struct args **compiler_args)
1815 bool found_c_opt = false;
1816 bool found_S_opt = false;
1817 bool found_arch_opt = false;
1818 bool found_pch = false;
1819 bool found_fpch_preprocess = false;
1820 const char *explicit_language = NULL; /* As specified with -x. */
1821 const char *file_language; /* As deduced from file extension. */
1822 const char *actual_language; /* Language to actually use. */
1823 const char *input_charset = NULL;
1825 /* is the dependency makefile name overridden with -MF? */
1826 bool dependency_filename_specified = false;
1827 /* is the dependency makefile target name specified with -MT or -MQ? */
1828 bool dependency_target_specified = false;
1829 struct args *expanded_args, *stripped_args, *dep_args, *cpp_args;
1833 bool found_color_diagnostics = false;
1835 expanded_args = args_copy(args);
1836 stripped_args = args_init(0, NULL);
1837 dep_args = args_init(0, NULL);
1838 cpp_args = args_init(0, NULL);
1840 argc = expanded_args->argc;
1841 argv = expanded_args->argv;
1843 args_add(stripped_args, argv[0]);
1845 for (i = 1; i < argc; i++) {
1846 /* The user knows best: just swallow the next arg */
1847 if (str_eq(argv[i], "--ccache-skip")) {
1850 cc_log("--ccache-skip lacks an argument");
1854 args_add(stripped_args, argv[i]);
1858 /* Special case for -E. */
1859 if (str_eq(argv[i], "-E")) {
1860 stats_update(STATS_PREPROCESSING);
1865 /* Handle "@file" argument. */
1866 if (str_startswith(argv[i], "@") || str_startswith(argv[i], "-@")) {
1867 char *argpath = argv[i] + 1;
1868 struct args *file_args;
1870 if (argpath[-1] == '-') {
1873 file_args = args_init_from_gcc_atfile(argpath);
1875 cc_log("Couldn't read arg file %s", argpath);
1876 stats_update(STATS_ARGS);
1881 args_insert(expanded_args, i, file_args, true);
1882 argc = expanded_args->argc;
1883 argv = expanded_args->argv;
1888 /* These are always too hard. */
1889 if (compopt_too_hard(argv[i])
1890 || str_startswith(argv[i], "-fdump-")) {
1891 cc_log("Compiler option %s is unsupported", argv[i]);
1892 stats_update(STATS_UNSUPPORTED);
1897 /* These are too hard in direct mode. */
1898 if (conf->direct_mode) {
1899 if (compopt_too_hard_for_direct_mode(argv[i])) {
1900 cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
1901 conf->direct_mode = false;
1905 /* Multiple -arch options are too hard. */
1906 if (str_eq(argv[i], "-arch")) {
1907 if (found_arch_opt) {
1908 cc_log("More than one -arch compiler option is unsupported");
1909 stats_update(STATS_UNSUPPORTED);
1913 found_arch_opt = true;
1917 if (str_eq(argv[i], "-fpch-preprocess")
1918 || str_eq(argv[i], "-emit-pch")
1919 || str_eq(argv[i], "-emit-pth")) {
1920 found_fpch_preprocess = true;
1923 /* we must have -c */
1924 if (str_eq(argv[i], "-c")) {
1929 /* -S changes the default extension */
1930 if (str_eq(argv[i], "-S")) {
1931 args_add(stripped_args, argv[i]);
1937 * Special handling for -x: remember the last specified language before the
1938 * input file and strip all -x options from the arguments.
1940 if (str_eq(argv[i], "-x")) {
1942 cc_log("Missing argument to %s", argv[i]);
1943 stats_update(STATS_ARGS);
1948 explicit_language = argv[i+1];
1953 if (str_startswith(argv[i], "-x")) {
1955 explicit_language = &argv[i][2];
1960 /* we need to work out where the output was meant to go */
1961 if (str_eq(argv[i], "-o")) {
1963 cc_log("Missing argument to %s", argv[i]);
1964 stats_update(STATS_ARGS);
1968 output_obj = make_relative_path(x_strdup(argv[i+1]));
1973 /* alternate form of -o, with no space */
1974 if (str_startswith(argv[i], "-o")) {
1975 output_obj = make_relative_path(x_strdup(&argv[i][2]));
1979 if (str_eq(argv[i], "-gsplit-dwarf")) {
1980 cc_log("Enabling caching of dwarf files since -gsplit-dwarf is used");
1981 using_split_dwarf = true;
1982 args_add(stripped_args, argv[i]);
1986 /* Debugging is handled specially, so that we know if we can strip line
1988 if (str_startswith(argv[i], "-g")) {
1989 args_add(stripped_args, argv[i]);
1990 if (conf->unify && !str_eq(argv[i], "-g0")) {
1991 cc_log("%s used; disabling unify mode", argv[i]);
1992 conf->unify = false;
1994 if (str_eq(argv[i], "-g3")) {
1996 * Fix for bug 7190 ("commandline macros (-D)
1997 * have non-zero lineno when using -g3").
1999 cc_log("%s used; not compiling preprocessed code", argv[i]);
2000 conf->run_second_cpp = true;
2005 /* These options require special handling, because they
2006 * behave differently with gcc -E, when the output
2007 * file is not specified. */
2008 if (str_eq(argv[i], "-MD") || str_eq(argv[i], "-MMD")) {
2009 generating_dependencies = true;
2010 args_add(dep_args, argv[i]);
2013 if (str_startswith(argv[i], "-MF")) {
2015 bool separate_argument = (strlen(argv[i]) == 3);
2016 dependency_filename_specified = true;
2018 if (separate_argument) {
2020 if (i >= argc - 1) {
2021 cc_log("Missing argument to %s", argv[i]);
2022 stats_update(STATS_ARGS);
2032 output_dep = make_relative_path(x_strdup(arg));
2033 /* Keep the format of the args the same */
2034 if (separate_argument) {
2035 args_add(dep_args, "-MF");
2036 args_add(dep_args, output_dep);
2038 char *option = format("-MF%s", output_dep);
2039 args_add(dep_args, option);
2044 if (str_startswith(argv[i], "-MQ") || str_startswith(argv[i], "-MT")) {
2046 dependency_target_specified = true;
2047 if (strlen(argv[i]) == 3) {
2048 /* -MQ arg or -MT arg */
2049 if (i >= argc - 1) {
2050 cc_log("Missing argument to %s", argv[i]);
2051 stats_update(STATS_ARGS);
2055 args_add(dep_args, argv[i]);
2056 relpath = make_relative_path(x_strdup(argv[i + 1]));
2057 args_add(dep_args, relpath);
2063 arg_opt = x_strndup(argv[i], 3);
2064 relpath = make_relative_path(x_strdup(argv[i] + 3));
2065 option = format("%s%s", arg_opt, relpath);
2066 args_add(dep_args, option);
2073 if (str_eq(argv[i], "-fprofile-arcs")) {
2074 profile_arcs = true;
2075 args_add(stripped_args, argv[i]);
2078 if (str_eq(argv[i], "-ftest-coverage")) {
2079 generating_coverage = true;
2080 args_add(stripped_args, argv[i]);
2083 if (str_eq(argv[i], "--coverage")) { /* = -fprofile-arcs -ftest-coverage */
2084 profile_arcs = true;
2085 generating_coverage = true;
2086 args_add(stripped_args, argv[i]);
2089 if (str_startswith(argv[i], "-fprofile-dir=")) {
2090 profile_dir = x_strdup(argv[i] + 14);
2091 args_add(stripped_args, argv[i]);
2094 if (str_startswith(argv[i], "--sysroot=")) {
2095 char *relpath = make_relative_path(x_strdup(argv[i] + 10));
2096 char *option = format("--sysroot=%s", relpath);
2097 args_add(stripped_args, option);
2102 if (str_startswith(argv[i], "-Wp,")) {
2103 if (str_eq(argv[i], "-Wp,-P") || strstr(argv[i], ",-P,")) {
2104 /* -P removes preprocessor information in such a way that the object
2105 * file from compiling the preprocessed file will not be equal to the
2106 * object file produced when compiling without ccache. */
2107 cc_log("Too hard option -Wp,-P detected");
2108 stats_update(STATS_UNSUPPORTED);
2110 } else if (str_startswith(argv[i], "-Wp,-MD,")
2111 && !strchr(argv[i] + 8, ',')) {
2112 generating_dependencies = true;
2113 dependency_filename_specified = true;
2115 output_dep = make_relative_path(x_strdup(argv[i] + 8));
2116 args_add(dep_args, argv[i]);
2118 } else if (str_startswith(argv[i], "-Wp,-MMD,")
2119 && !strchr(argv[i] + 9, ',')) {
2120 generating_dependencies = true;
2121 dependency_filename_specified = true;
2123 output_dep = make_relative_path(x_strdup(argv[i] + 9));
2124 args_add(dep_args, argv[i]);
2126 } else if (conf->direct_mode) {
2128 * -Wp, can be used to pass too hard options to
2129 * the preprocessor. Hence, disable direct
2132 cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
2133 conf->direct_mode = false;
2136 if (str_eq(argv[i], "-MP")) {
2137 args_add(dep_args, argv[i]);
2141 /* Input charset needs to be handled specially. */
2142 if (str_startswith(argv[i], "-finput-charset=")) {
2143 input_charset = argv[i];
2147 if (str_eq(argv[i], "--serialize-diagnostics")) {
2148 if (i >= argc - 1) {
2149 cc_log("Missing argument to %s", argv[i]);
2150 stats_update(STATS_ARGS);
2154 output_dia = make_relative_path(x_strdup(argv[i+1]));
2159 if (str_startswith(argv[i], "-fprofile-")) {
2160 const char *arg_profile_dir = strchr(argv[i], '=');
2161 char *arg = x_strdup(argv[i]);
2162 bool supported_profile_option = false;
2164 if (arg_profile_dir) {
2165 char *option = x_strndup(argv[i], arg_profile_dir - argv[i]);
2168 /* Convert to absolute path. */
2169 dir = x_realpath(arg_profile_dir + 1);
2171 /* Directory doesn't exist. */
2172 dir = x_strdup(arg_profile_dir + 1);
2175 /* We can get a better hit rate by using the real path here. */
2177 arg = format("%s=%s", option, dir);
2178 cc_log("Rewriting %s to %s", argv[i], arg);
2183 if (str_startswith(argv[i], "-fprofile-generate")
2184 || str_eq(argv[i], "-fprofile-arcs")) {
2185 profile_generate = true;
2186 supported_profile_option = true;
2187 } else if (str_startswith(argv[i], "-fprofile-use")
2188 || str_eq(argv[i], "-fbranch-probabilities")) {
2190 supported_profile_option = true;
2191 } else if (str_eq(argv[i], "-fprofile-dir")) {
2192 supported_profile_option = true;
2195 if (supported_profile_option) {
2196 args_add(stripped_args, arg);
2200 * If the profile directory has already been set, give up... Hard to
2201 * know what the user means, and what the compiler will do.
2203 if (arg_profile_dir && profile_dir) {
2204 cc_log("Profile directory already set; giving up");
2207 } else if (arg_profile_dir) {
2208 cc_log("Setting profile directory to %s", profile_dir);
2209 profile_dir = x_strdup(arg_profile_dir);
2213 cc_log("Unknown profile option: %s", argv[i]);
2217 if (str_eq(argv[i], "-fcolor-diagnostics")
2218 || str_eq(argv[i], "-fno-color-diagnostics")
2219 || str_eq(argv[i], "-fdiagnostics-color")
2220 || str_eq(argv[i], "-fdiagnostics-color=always")
2221 || str_eq(argv[i], "-fno-diagnostics-color")
2222 || str_eq(argv[i], "-fdiagnostics-color=never")) {
2223 args_add(stripped_args, argv[i]);
2224 found_color_diagnostics = true;
2227 if (str_eq(argv[i], "-fdiagnostics-color=auto")) {
2228 if (color_output_possible()) {
2229 /* Output is redirected, so color output must be forced. */
2230 args_add(stripped_args, "-fdiagnostics-color=always");
2231 cc_log("Automatically forcing colors");
2233 args_add(stripped_args, argv[i]);
2235 found_color_diagnostics = true;
2240 * Options taking an argument that we may want to rewrite to relative paths
2241 * to get better hit rate. A secondary effect is that paths in the standard
2242 * error output produced by the compiler will be normalized.
2244 if (compopt_takes_path(argv[i])) {
2246 char *pch_file = NULL;
2248 cc_log("Missing argument to %s", argv[i]);
2249 stats_update(STATS_ARGS);
2254 relpath = make_relative_path(x_strdup(argv[i+1]));
2255 if (compopt_affects_cpp(argv[i])) {
2256 args_add(cpp_args, argv[i]);
2257 args_add(cpp_args, relpath);
2259 args_add(stripped_args, argv[i]);
2260 args_add(stripped_args, relpath);
2263 /* Try to be smart about detecting precompiled headers */
2264 if (str_eq(argv[i], "-include-pch")
2265 || str_eq(argv[i], "-include-pth")) {
2266 if (stat(argv[i+1], &st) == 0) {
2267 cc_log("Detected use of precompiled header: %s", argv[i+1]);
2269 pch_file = x_strdup(argv[i+1]);
2272 char *gchpath = format("%s.gch", argv[i+1]);
2273 if (stat(gchpath, &st) == 0) {
2274 cc_log("Detected use of precompiled header: %s", gchpath);
2276 pch_file = x_strdup(gchpath);
2278 char *pchpath = format("%s.pch", argv[i+1]);
2279 if (stat(pchpath, &st) == 0) {
2280 cc_log("Detected use of precompiled header: %s", pchpath);
2282 pch_file = x_strdup(pchpath);
2284 /* clang may use pretokenized headers */
2285 char *pthpath = format("%s.pth", argv[i+1]);
2286 if (stat(pthpath, &st) == 0) {
2287 cc_log("Detected use of pretokenized header: %s", pthpath);
2289 pch_file = x_strdup(pthpath);
2299 if (included_pch_file) {
2300 cc_log("Multiple precompiled headers used: %s and %s\n",
2301 included_pch_file, pch_file);
2302 stats_update(STATS_ARGS);
2306 included_pch_file = pch_file;
2314 /* Same as above but options with concatenated argument. */
2315 if (compopt_short(compopt_takes_path, argv[i])) {
2318 relpath = make_relative_path(x_strdup(argv[i] + 2));
2319 option = format("-%c%s", argv[i][1], relpath);
2321 if (compopt_short(compopt_affects_cpp, argv[i])) {
2322 args_add(cpp_args, option);
2324 args_add(stripped_args, option);
2332 /* options that take an argument */
2333 if (compopt_takes_arg(argv[i])) {
2335 cc_log("Missing argument to %s", argv[i]);
2336 stats_update(STATS_ARGS);
2341 if (compopt_affects_cpp(argv[i])) {
2342 args_add(cpp_args, argv[i]);
2343 args_add(cpp_args, argv[i+1]);
2345 args_add(stripped_args, argv[i]);
2346 args_add(stripped_args, argv[i+1]);
2354 if (argv[i][0] == '-') {
2355 if (compopt_affects_cpp(argv[i])
2356 || compopt_prefix_affects_cpp(argv[i])) {
2357 args_add(cpp_args, argv[i]);
2359 args_add(stripped_args, argv[i]);
2364 /* if an argument isn't a plain file then assume its
2365 * an option, not an input file. This allows us to
2366 * cope better with unusual compiler options */
2367 if (stat(argv[i], &st) != 0 || !S_ISREG(st.st_mode)) {
2368 cc_log("%s is not a regular file, not considering as input file",
2370 args_add(stripped_args, argv[i]);
2375 if (language_for_file(argv[i])) {
2376 cc_log("Multiple input files: %s and %s", input_file, argv[i]);
2377 stats_update(STATS_MULTIPLE);
2378 } else if (!found_c_opt) {
2379 cc_log("Called for link with %s", argv[i]);
2380 if (strstr(argv[i], "conftest.")) {
2381 stats_update(STATS_CONFTEST);
2383 stats_update(STATS_LINK);
2386 cc_log("Unsupported source extension: %s", argv[i]);
2387 stats_update(STATS_SOURCELANG);
2393 /* The source code file path gets put into the notes */
2394 if (generating_coverage) {
2395 input_file = x_strdup(argv[i]);
2399 /* Rewrite to relative to increase hit rate. */
2400 input_file = make_relative_path(x_strdup(argv[i]));
2404 /* Even if -gsplit-dwarf is given, the .dwo file is not generated when -S
2407 using_split_dwarf = false;
2408 cc_log("Disabling caching of dwarf files since -S is used");
2412 cc_log("No input file found");
2413 stats_update(STATS_NOINPUT);
2418 if (found_pch || found_fpch_preprocess) {
2419 using_precompiled_header = true;
2420 if (!(conf->sloppiness & SLOPPY_TIME_MACROS)) {
2421 cc_log("You have to specify \"time_macros\" sloppiness when using"
2422 " precompiled headers to get direct hits");
2423 cc_log("Disabling direct mode");
2424 stats_update(STATS_CANTUSEPCH);
2430 if (explicit_language && str_eq(explicit_language, "none")) {
2431 explicit_language = NULL;
2433 file_language = language_for_file(input_file);
2434 if (explicit_language) {
2435 if (!language_is_supported(explicit_language)) {
2436 cc_log("Unsupported language: %s", explicit_language);
2437 stats_update(STATS_SOURCELANG);
2441 actual_language = explicit_language;
2443 actual_language = file_language;
2446 output_is_precompiled_header =
2447 actual_language && strstr(actual_language, "-header");
2449 if (output_is_precompiled_header
2450 && !(conf->sloppiness & SLOPPY_PCH_DEFINES)) {
2451 cc_log("You have to specify \"pch_defines,time_macros\" sloppiness when"
2452 " creating precompiled headers");
2453 stats_update(STATS_CANTUSEPCH);
2459 if (output_is_precompiled_header) {
2460 args_add(stripped_args, "-c");
2462 cc_log("No -c option found");
2463 /* I find that having a separate statistic for autoconf tests is useful,
2464 * as they are the dominant form of "called for link" in many cases */
2465 if (strstr(input_file, "conftest.")) {
2466 stats_update(STATS_CONFTEST);
2468 stats_update(STATS_LINK);
2475 if (!actual_language) {
2476 cc_log("Unsupported source extension: %s", input_file);
2477 stats_update(STATS_SOURCELANG);
2482 direct_i_file = language_is_preprocessed(actual_language);
2484 if (output_is_precompiled_header) {
2485 /* It doesn't work to create the .gch from preprocessed source. */
2486 cc_log("Creating precompiled header; not compiling preprocessed code");
2487 conf->run_second_cpp = true;
2490 if (str_eq(conf->cpp_extension, "")) {
2491 const char *p_language = p_language_for_language(actual_language);
2492 free(conf->cpp_extension);
2493 conf->cpp_extension = x_strdup(extension_for_language(p_language) + 1);
2496 /* don't try to second guess the compilers heuristics for stdout handling */
2497 if (output_obj && str_eq(output_obj, "-")) {
2498 stats_update(STATS_OUTSTDOUT);
2499 cc_log("Output file is -");
2505 if (output_is_precompiled_header) {
2506 output_obj = format("%s.gch", input_file);
2509 output_obj = basename(input_file);
2510 p = strrchr(output_obj, '.');
2512 cc_log("Badly formed object filename");
2513 stats_update(STATS_ARGS);
2517 p[1] = found_S_opt ? 's' : 'o';
2522 if (using_split_dwarf) {
2524 p = strrchr(output_obj, '.');
2526 cc_log("Badly formed object filename");
2527 stats_update(STATS_ARGS);
2532 char *base_name = remove_extension(output_obj);
2533 output_dwo = format("%s.dwo", base_name);
2538 /* cope with -o /dev/null */
2539 if (!str_eq(output_obj, "/dev/null")
2540 && stat(output_obj, &st) == 0
2541 && !S_ISREG(st.st_mode)) {
2542 cc_log("Not a regular file: %s", output_obj);
2543 stats_update(STATS_DEVICE);
2549 * Some options shouldn't be passed to the real compiler when it compiles
2550 * preprocessed code:
2552 * -finput-charset=XXX (otherwise conversion happens twice)
2553 * -x XXX (otherwise the wrong language is selected)
2555 if (input_charset) {
2556 args_add(cpp_args, input_charset);
2559 args_add(cpp_args, "-fpch-preprocess");
2561 if (explicit_language) {
2562 args_add(cpp_args, "-x");
2563 args_add(cpp_args, explicit_language);
2567 * Since output is redirected, compilers will not color their output by
2568 * default, so force it explicitly if it would be otherwise done.
2570 if (!found_color_diagnostics && color_output_possible()) {
2571 if (compiler_is_clang(args)) {
2572 args_add(stripped_args, "-fcolor-diagnostics");
2573 cc_log("Automatically enabling colors");
2574 } else if (compiler_is_gcc(args)) {
2576 * GCC has it since 4.9, but that'd require detecting what GCC version is
2577 * used for the actual compile. However it requires also GCC_COLORS to be
2578 * set (and not empty), so use that for detecting if GCC would use
2581 if (getenv("GCC_COLORS") && getenv("GCC_COLORS")[0] != '\0') {
2582 args_add(stripped_args, "-fdiagnostics-color");
2583 cc_log("Automatically enabling colors");
2589 * Add flags for dependency generation only to the preprocessor command line.
2591 if (generating_dependencies) {
2592 if (!dependency_filename_specified) {
2593 char *default_depfile_name;
2596 base_name = remove_extension(output_obj);
2597 default_depfile_name = format("%s.d", base_name);
2599 args_add(dep_args, "-MF");
2600 args_add(dep_args, default_depfile_name);
2601 output_dep = make_relative_path(x_strdup(default_depfile_name));
2604 if (!dependency_target_specified) {
2605 args_add(dep_args, "-MQ");
2606 args_add(dep_args, output_obj);
2609 if (generating_coverage) {
2610 char *default_covfile_name;
2613 base_name = remove_extension(output_obj);
2614 default_covfile_name = format("%s.gcno", base_name);
2616 output_cov = make_relative_path(x_strdup(default_covfile_name));
2619 *compiler_args = args_copy(stripped_args);
2620 if (conf->run_second_cpp) {
2621 args_extend(*compiler_args, cpp_args);
2623 if (explicit_language) {
2625 * Workaround for a bug in Apple's patched distcc -- it doesn't properly
2626 * reset the language specified with -x, so if -x is given, we have to
2627 * specify the preprocessed language explicitly.
2629 args_add(*compiler_args, "-x");
2630 args_add(*compiler_args, p_language_for_language(explicit_language));
2635 args_add(*compiler_args, "-c");
2639 * Only pass dependency arguments to the preprocesor since Intel's C++
2640 * compiler doesn't produce a correct .d file when compiling preprocessed
2643 args_extend(cpp_args, dep_args);
2645 *preprocessor_args = args_copy(stripped_args);
2646 args_extend(*preprocessor_args, cpp_args);
2649 args_free(expanded_args);
2650 args_free(stripped_args);
2651 args_free(dep_args);
2652 args_free(cpp_args);
2657 create_initial_config_file(struct conf *conf, const char *path)
2665 if (create_parent_dirs(path) != 0) {
2669 stats_dir = format("%s/0", conf->cache_dir);
2670 if (stat(stats_dir, &st) == 0) {
2671 stats_get_obsolete_limits(stats_dir, &max_files, &max_size);
2672 /* STATS_MAXFILES and STATS_MAXSIZE was stored for each top directory. */
2677 max_size = conf->max_size;
2681 f = fopen(path, "w");
2685 if (max_files != 0) {
2686 fprintf(f, "max_files = %u\n", max_files);
2687 conf->max_files = max_files;
2689 if (max_size != 0) {
2690 char *size = format_parsable_size_with_suffix(max_size);
2691 fprintf(f, "max_size = %s\n", size);
2693 conf->max_size = max_size;
2699 * Read config file(s), populate variables, create configuration file in cache
2700 * directory if missing, etc.
2708 bool should_create_initial_config = false;
2711 conf = conf_create();
2713 p = getenv("CCACHE_CONFIGPATH");
2715 primary_config_path = x_strdup(p);
2717 secondary_config_path = format("%s/ccache.conf", TO_STRING(SYSCONFDIR));
2718 if (!conf_read(conf, secondary_config_path, &errmsg)) {
2719 if (stat(secondary_config_path, &st) == 0) {
2720 fatal("%s", errmsg);
2722 /* Missing config file in SYSCONFDIR is OK. */
2726 if (str_eq(conf->cache_dir, "")) {
2727 fatal("configuration setting \"cache_dir\" must not be the empty string");
2729 if ((p = getenv("CCACHE_DIR"))) {
2730 free(conf->cache_dir);
2731 conf->cache_dir = strdup(p);
2733 if (str_eq(conf->cache_dir, "")) {
2734 fatal("CCACHE_DIR must not be the empty string");
2737 primary_config_path = format("%s/ccache.conf", conf->cache_dir);
2740 if (!conf_read(conf, primary_config_path, &errmsg)) {
2741 if (stat(primary_config_path, &st) == 0) {
2742 fatal("%s", errmsg);
2744 should_create_initial_config = true;
2747 if (!conf_update_from_environment(conf, &errmsg)) {
2748 fatal("%s", errmsg);
2751 if (conf->disable) {
2752 should_create_initial_config = false;
2755 if (should_create_initial_config) {
2756 create_initial_config_file(conf, primary_config_path);
2760 exitfn_add_nullary(stats_flush);
2761 exitfn_add_nullary(clean_up_pending_tmp_files);
2763 cc_log("=== CCACHE %s STARTED =========================================",
2766 if (conf->umask != UINT_MAX) {
2771 /* Reset the global state. Used by the test suite. */
2775 conf_free(conf); conf = NULL;
2776 free(primary_config_path); primary_config_path = NULL;
2777 free(secondary_config_path); secondary_config_path = NULL;
2778 free(current_working_dir); current_working_dir = NULL;
2779 free(profile_dir); profile_dir = NULL;
2780 free(included_pch_file); included_pch_file = NULL;
2781 args_free(orig_args); orig_args = NULL;
2782 free(input_file); input_file = NULL;
2783 free(output_obj); output_obj = NULL;
2784 free(output_dwo); output_dwo = NULL;
2785 free(output_dep); output_dep = NULL;
2786 free(output_cov); output_cov = NULL;
2787 free(output_dia); output_dia = NULL;
2788 free(cached_obj_hash); cached_obj_hash = NULL;
2789 free(cached_obj); cached_obj = NULL;
2790 free(cached_dwo); cached_dwo = NULL;
2791 free(cached_stderr); cached_stderr = NULL;
2792 free(cached_dep); cached_dep = NULL;
2793 free(cached_cov); cached_cov = NULL;
2794 free(cached_dia); cached_dia = NULL;
2795 free(manifest_path); manifest_path = NULL;
2796 time_of_compilation = 0;
2797 if (included_files) {
2798 hashtable_destroy(included_files, 1); included_files = NULL;
2800 generating_dependencies = false;
2801 generating_coverage = false;
2802 profile_arcs = false;
2803 free(profile_dir); profile_dir = NULL;
2805 direct_i_file = false;
2806 free(cpp_stderr); cpp_stderr = NULL;
2807 free(stats_file); stats_file = NULL;
2808 output_is_precompiled_header = false;
2810 conf = conf_create();
2811 using_split_dwarf = false;
2814 /* Make a copy of stderr that will not be cached, so things like
2815 * distcc can send networking errors to it. */
2817 setup_uncached_err(void)
2822 uncached_fd = dup(2);
2823 if (uncached_fd == -1) {
2824 cc_log("dup(2) failed: %s", strerror(errno));
2828 /* leak a pointer to the environment */
2829 buf = format("UNCACHED_ERR_FD=%d", uncached_fd);
2831 if (putenv(buf) == -1) {
2832 cc_log("putenv failed: %s", strerror(errno));
2838 configuration_logger(const char *descr, const char *origin, void *context)
2841 cc_bulklog("Config: (%s) %s", origin, descr);
2844 /* the main ccache driver function */
2846 ccache(int argc, char *argv[])
2848 bool put_object_in_manifest = false;
2849 struct file_hash *object_hash;
2850 struct file_hash *object_hash_from_manifest = NULL;
2851 struct mdfour common_hash;
2852 struct mdfour direct_hash;
2853 struct mdfour cpp_hash;
2855 /* Arguments (except -E) to send to the preprocessor. */
2856 struct args *preprocessor_args;
2858 /* Arguments to send to the real compiler. */
2859 struct args *compiler_args;
2861 orig_args = args_init(argc, argv);
2864 find_compiler(argv);
2867 signal(SIGHUP, signal_handler);
2869 signal(SIGINT, signal_handler);
2870 signal(SIGTERM, signal_handler);
2872 if (str_eq(conf->temporary_dir, "")) {
2873 clean_up_internal_tempdir();
2876 if (!str_eq(conf->log_file, "")) {
2877 conf_print_items(conf, configuration_logger, NULL);
2880 if (conf->disable) {
2881 cc_log("ccache is disabled");
2885 setup_uncached_err();
2887 cc_log_argv("Command line: ", argv);
2888 cc_log("Hostname: %s", get_hostname());
2889 cc_log("Working directory: %s", get_current_working_dir());
2892 cc_log("Direct mode disabled because unify mode is enabled");
2893 conf->direct_mode = false;
2896 if (!cc_process_args(orig_args, &preprocessor_args, &compiler_args)) {
2900 cc_log("Source file: %s", input_file);
2901 if (generating_dependencies) {
2902 cc_log("Dependency file: %s", output_dep);
2904 if (generating_coverage) {
2905 cc_log("Coverage file: %s", output_cov);
2908 cc_log("Diagnostic file: %s", output_dia);
2911 if (using_split_dwarf) {
2912 if (!generating_dependencies) {
2916 assert(!output_dwo);
2920 cc_log("Split dwarf file: %s", output_dwo);
2923 cc_log("Object file: %s", output_obj);
2925 hash_start(&common_hash);
2926 calculate_common_hash(preprocessor_args, &common_hash);
2928 /* try to find the hash using the manifest */
2929 direct_hash = common_hash;
2930 if (conf->direct_mode) {
2931 cc_log("Trying direct lookup");
2932 object_hash = calculate_object_hash(preprocessor_args, &direct_hash, 1);
2934 update_cached_result_globals(object_hash);
2937 * If we can return from cache at this point then do
2940 from_cache(FROMCACHE_DIRECT_MODE, 0);
2943 * Wasn't able to return from cache at this point.
2944 * However, the object was already found in manifest,
2945 * so don't readd it later.
2947 put_object_in_manifest = false;
2949 object_hash_from_manifest = object_hash;
2951 /* Add object to manifest later. */
2952 put_object_in_manifest = true;
2956 if (conf->read_only_direct) {
2957 cc_log("Read-only direct mode; running real compiler");
2962 * Find the hash using the preprocessed output. Also updates
2965 cpp_hash = common_hash;
2966 object_hash = calculate_object_hash(preprocessor_args, &cpp_hash, 0);
2968 fatal("internal error: object hash from cpp returned NULL");
2970 update_cached_result_globals(object_hash);
2972 if (object_hash_from_manifest
2973 && !file_hashes_equal(object_hash_from_manifest, object_hash)) {
2975 * The hash from manifest differs from the hash of the
2976 * preprocessor output. This could be because:
2978 * - The preprocessor produces different output for the same
2979 * input (not likely).
2980 * - There's a bug in ccache (maybe incorrect handling of
2981 * compiler arguments).
2982 * - The user has used a different CCACHE_BASEDIR (most
2985 * The best thing here would probably be to remove the hash
2986 * entry from the manifest. For now, we use a simpler method:
2987 * just remove the manifest file.
2989 cc_log("Hash from manifest doesn't match preprocessor output");
2990 cc_log("Likely reason: different CCACHE_BASEDIRs used");
2991 cc_log("Removing manifest as a safety measure");
2992 x_unlink(manifest_path);
2994 put_object_in_manifest = true;
2997 /* if we can return from cache at this point then do */
2998 from_cache(FROMCACHE_CPP_MODE, put_object_in_manifest);
3000 if (conf->read_only) {
3001 cc_log("Read-only mode; running real compiler");
3005 add_prefix(compiler_args);
3007 /* run real compiler, sending output to cache */
3008 to_cache(compiler_args);
3014 configuration_printer(const char *descr, const char *origin, void *context)
3017 fprintf(context, "(%s) %s\n", origin, descr);
3020 /* the main program when not doing a compile */
3022 ccache_main_options(int argc, char *argv[])
3030 static const struct option options[] = {
3031 {"cleanup", no_argument, 0, 'c'},
3032 {"clear", no_argument, 0, 'C'},
3033 {"dump-manifest", required_argument, 0, DUMP_MANIFEST},
3034 {"help", no_argument, 0, 'h'},
3035 {"max-files", required_argument, 0, 'F'},
3036 {"max-size", required_argument, 0, 'M'},
3037 {"set-config", required_argument, 0, 'o'},
3038 {"print-config", no_argument, 0, 'p'},
3039 {"show-stats", no_argument, 0, 's'},
3040 {"version", no_argument, 0, 'V'},
3041 {"zero-stats", no_argument, 0, 'z'},
3045 while ((c = getopt_long(argc, argv, "cChF:M:o:psVz", options, NULL)) != -1) {
3048 manifest_dump(optarg, stdout);
3051 case 'c': /* --cleanup */
3054 printf("Cleaned cache\n");
3057 case 'C': /* --clear */
3060 printf("Cleared cache\n");
3063 case 'h': /* --help */
3064 fputs(USAGE_TEXT, stdout);
3067 case 'F': /* --max-files */
3071 files = atoi(optarg);
3072 if (conf_set_value_in_file(primary_config_path, "max_files", optarg,
3075 printf("Unset cache file limit\n");
3077 printf("Set cache file limit to %u\n", files);
3080 fatal("could not set cache file limit: %s", errmsg);
3085 case 'M': /* --max-size */
3089 if (!parse_size_with_suffix(optarg, &size)) {
3090 fatal("invalid size: %s", optarg);
3092 if (conf_set_value_in_file(primary_config_path, "max_size", optarg,
3095 printf("Unset cache size limit\n");
3097 char *s = format_human_readable_size(size);
3098 printf("Set cache size limit to %s\n", s);
3102 fatal("could not set cache size limit: %s", errmsg);
3107 case 'o': /* --set-config */
3109 char *errmsg, *key, *value, *p;
3111 p = strchr(optarg, '=');
3113 fatal("missing equal sign in \"%s\"", optarg);
3115 key = x_strndup(optarg, p - optarg);
3117 if (!conf_set_value_in_file(primary_config_path, key, value, &errmsg)) {
3118 fatal("%s", errmsg);
3124 case 'p': /* --print-config */
3126 conf_print_items(conf, configuration_printer, stdout);
3129 case 's': /* --show-stats */
3131 stats_summary(conf);
3134 case 'V': /* --version */
3135 fprintf(stdout, VERSION_TEXT, CCACHE_VERSION);
3138 case 'z': /* --zero-stats */
3141 printf("Statistics cleared\n");
3145 fputs(USAGE_TEXT, stderr);
3154 ccache_main(int argc, char *argv[])
3156 /* check if we are being invoked as "ccache" */
3157 char *program_name = basename(argv[0]);
3158 if (same_executable_name(program_name, MYNAME)) {
3160 fputs(USAGE_TEXT, stderr);
3163 /* if the first argument isn't an option, then assume we are
3164 * being passed a compiler name and options */
3165 if (argv[1][0] == '-') {
3166 return ccache_main_options(argc, argv);