2 * ccache -- a fast C/C++ compiler cache
4 * Copyright (C) 2002-2007 Andrew Tridgell
5 * Copyright (C) 2009-2011 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 static const char VERSION_TEXT[] =
36 MYNAME " version %s\n"
38 "Copyright (C) 2002-2007 Andrew Tridgell\n"
39 "Copyright (C) 2009-2011 Joel Rosdahl\n"
41 "This program is free software; you can redistribute it and/or modify it under\n"
42 "the terms of the GNU General Public License as published by the Free Software\n"
43 "Foundation; either version 3 of the License, or (at your option) any later\n"
46 static const char USAGE_TEXT[] =
48 " " MYNAME " [options]\n"
49 " " MYNAME " compiler [compiler options]\n"
50 " compiler [compiler options] (via symbolic link)\n"
53 " -c, --cleanup delete old files and recalculate size counters\n"
54 " (normally not needed as this is done automatically)\n"
55 " -C, --clear clear the cache completely\n"
56 " -F, --max-files=N set maximum number of files in cache to N (use 0 for\n"
58 " -M, --max-size=SIZE set maximum size of cache to SIZE (use 0 for no\n"
59 " limit; available suffixes: G, M and K; default\n"
61 " -s, --show-stats show statistics summary\n"
62 " -z, --zero-stats zero statistics counters\n"
64 " -h, --help print this help text\n"
65 " -V, --version print version and copyright information\n"
67 "See also <http://ccache.samba.org>.\n";
69 /* current working directory taken from $PWD, or getcwd() if $PWD is bad */
70 char *current_working_dir = NULL;
72 /* the base cache directory */
73 char *cache_dir = NULL;
75 /* the directory for temporary files */
76 static char *temp_dir;
78 /* the debug logfile name, if set */
79 char *cache_logfile = NULL;
81 /* base directory (from CCACHE_BASEDIR) */
82 char *base_dir = NULL;
84 /* the original argument list */
85 static struct args *orig_args;
88 static char *input_file;
90 /* The output file being compiled to. */
91 static char *output_obj;
93 /* The path to the dependency file (implicit or specified with -MF). */
94 static char *output_dep;
97 * Name (represented as a struct file_hash) of the file containing the cached
100 static struct file_hash *cached_obj_hash;
103 * Full path to the file containing the cached object code
104 * (cachedir/a/b/cdef[...]-size.o).
106 static char *cached_obj;
109 * Full path to the file containing the standard error output
110 * (cachedir/a/b/cdef[...]-size.stderr).
112 static char *cached_stderr;
115 * Full path to the file containing the dependency information
116 * (cachedir/a/b/cdef[...]-size.d).
118 static char *cached_dep;
121 * Full path to the file containing the manifest
122 * (cachedir/a/b/cdef[...]-size.manifest).
124 static char *manifest_path;
127 * Time of compilation. Used to see if include files have changed after
130 static time_t time_of_compilation;
132 /* Bitmask of SLOPPY_*. */
133 unsigned sloppiness = 0;
136 * Files included by the preprocessor and their hashes/sizes. Key: file path.
137 * Value: struct file_hash.
139 static struct hashtable *included_files;
141 /* is gcc being asked to output dependencies? */
142 static bool generating_dependencies;
144 /* the extension of the file (without dot) after pre-processing */
145 static const char *i_extension;
147 /* the name of the temporary pre-processor file */
148 static char *i_tmpfile;
150 /* are we compiling a .i or .ii file directly? */
151 static bool direct_i_file;
153 /* the name of the cpp stderr file */
154 static char *cpp_stderr;
157 * Full path to the statistics file in the subdirectory where the cached result
158 * belongs (CCACHE_DIR/X/stats).
160 char *stats_file = NULL;
162 /* can we safely use the unification hashing backend? */
163 static bool enable_unify;
165 /* should we use the direct mode? */
166 static bool enable_direct = true;
169 * Whether to enable compression of files stored in the cache. (Manifest files
170 * are always compressed.)
172 static bool enable_compression = false;
174 /* number of levels (1 <= nlevels <= 8) */
175 static int nlevels = 2;
178 * Whether we should use the optimization of passing the already existing
179 * preprocessed source code to the compiler.
181 static bool compile_preprocessed_source_code;
183 /* Whether the output is a precompiled header */
184 static bool output_is_precompiled_header = false;
187 * Whether we are using a precompiled header (either via -include or #include).
189 static bool using_precompiled_header = false;
191 /* How long (in microseconds) to wait before breaking a stale lock. */
192 unsigned lock_staleness_limit = 2000000;
194 enum fromcache_call_mode {
195 FROMCACHE_DIRECT_MODE,
197 FROMCACHE_COMPILED_MODE
201 * This is a string that identifies the current "version" of the hash sum
202 * computed by ccache. If, for any reason, we want to force the hash sum to be
203 * different for the same input in a new ccache version, we can just change
204 * this string. A typical example would be if the format of one of the files
205 * stored in the cache changes in a backwards-incompatible way.
207 static const char HASH_PREFIX[] = "3";
209 /* Something went badly wrong - just execute the real compiler. */
215 /* strip any local args */
216 args_strip(orig_args, "--ccache-");
218 if ((e = getenv("CCACHE_PREFIX"))) {
219 char *p = find_executable(e, MYNAME);
221 fatal("%s: %s", e, strerror(errno));
223 args_add_prefix(orig_args, p);
226 cc_log("Failed; falling back to running the real compiler");
227 cc_log_argv("Executing ", orig_args->argv);
229 execv(orig_args->argv[0], orig_args->argv);
230 fatal("%s: execv returned (%s)", orig_args->argv[0], strerror(errno));
236 /* delete intermediate pre-processor file if needed */
238 if (!direct_i_file) {
239 tmp_unlink(i_tmpfile);
245 /* delete the cpp stderr file if necessary */
247 tmp_unlink(cpp_stderr);
254 * Transform a name to a full path into the cache directory, creating needed
255 * sublevels if needed. Caller frees.
258 get_path_in_cache(const char *name, const char *suffix)
264 path = x_strdup(cache_dir);
265 for (i = 0; i < nlevels; ++i) {
266 char *p = format("%s/%c", path, name[i]);
269 if (create_dir(path) != 0) {
270 cc_log("Failed to create %s: %s", path, strerror(errno));
274 result = format("%s/%s%s", path, name + nlevels, suffix);
280 * This function hashes an include file and stores the path and hash in the
281 * global included_files variable. If the include file is a PCH, cpp_hash is
282 * also updated. Takes over ownership of path.
285 remember_include_file(char *path, size_t path_len, struct mdfour *cpp_hash)
294 if (path_len >= 2 && (path[0] == '<' && path[path_len - 1] == '>')) {
295 /* Typically <built-in> or <command-line>. */
299 if (str_eq(path, input_file)) {
300 /* Don't remember the input file. */
304 if (hashtable_search(included_files, path)) {
305 /* Already known include file. */
309 if (stat(path, &st) != 0) {
310 cc_log("Failed to stat include file %s: %s", path, strerror(errno));
313 if (S_ISDIR(st.st_mode)) {
314 /* Ignore directory, typically $PWD. */
317 if (!S_ISREG(st.st_mode)) {
318 /* Device, pipe, socket or other strange creature. */
319 cc_log("Non-regular include file %s", path);
323 /* Let's hash the include file. */
324 if (!(sloppiness & SLOPPY_INCLUDE_FILE_MTIME)
325 && st.st_mtime >= time_of_compilation) {
326 cc_log("Include file %s too new", path);
332 is_pch = is_precompiled_header(path);
334 struct file_hash pch_hash;
335 if (!hash_file(&fhash, path)) {
338 hash_result_as_bytes(&fhash, pch_hash.hash);
339 pch_hash.size = fhash.totalN;
340 hash_delimiter(cpp_hash, "pch_hash");
341 hash_buffer(cpp_hash, pch_hash.hash, sizeof(pch_hash.hash));
346 if (!is_pch) { /* else: the file has already been hashed. */
347 if (st.st_size > 0) {
348 if (!read_file(path, st.st_size, &source, &size)) {
352 source = x_strdup("");
356 result = hash_source_code_string(&fhash, source, size, path);
357 if (result & HASH_SOURCE_CODE_ERROR
358 || result & HASH_SOURCE_CODE_FOUND_TIME) {
363 h = x_malloc(sizeof(*h));
364 hash_result_as_bytes(&fhash, h->hash);
365 h->size = fhash.totalN;
366 hashtable_insert(included_files, path, h);
375 cc_log("Disabling direct mode");
376 enable_direct = false;
384 * Make a relative path from CCACHE_BASEDIR to path. Takes over ownership of
385 * path. Caller frees.
388 make_relative_path(char *path)
392 if (!base_dir || !str_startswith(path, base_dir)) {
396 relpath = get_relative_path(current_working_dir, path);
402 * This function reads and hashes a file. While doing this, it also does these
405 * - Makes include file paths whose prefix is CCACHE_BASEDIR relative when
406 * computing the hash sum.
407 * - Stores the paths and hashes of included files in the global variable
411 process_preprocessed_file(struct mdfour *hash, const char *path)
417 if (!read_file(path, 0, &data, &size)) {
421 included_files = create_hashtable(1000, hash_from_string, strings_equal);
423 /* Bytes between p and q are pending to be hashed. */
427 while (q < end - 7) { /* There must be at least 7 characters (# 1 "x") left
428 to potentially find an include file path. */
430 * Check if we look at a line containing the file name of an included file.
431 * At least the following formats exist (where N is a positive integer):
437 * #pragma GCC pch_preprocess "file"
448 * Note that there may be other lines starting with '#' left after
449 * preprocessing as well, for instance "# pragma".
453 && ((q[1] == ' ' && q[2] >= '0' && q[2] <= '9')
454 /* GCC precompiled header: */
456 && str_startswith(&q[2], "ragma GCC pch_preprocess "))
458 || (q[1] == 'l' && q[2] == 'i' && q[3] == 'n' && q[4] == 'e'
460 && (q == data || q[-1] == '\n')) {
463 while (q < end && *q != '"' && *q != '\n') {
466 if (q < end && *q == '\n') {
467 /* A newline before the quotation mark -> no match. */
472 cc_log("Failed to parse included file path");
476 /* q points to the beginning of an include file path */
477 hash_buffer(hash, p, q - p);
479 while (q < end && *q != '"') {
482 /* p and q span the include file path */
483 path = x_strndup(p, q - p);
484 path = make_relative_path(path);
485 hash_string(hash, path);
486 remember_include_file(path, q - p, hash);
493 hash_buffer(hash, p, (end - p));
498 /* run the real compiler and put the result in cache */
500 to_cache(struct args *args)
502 char *tmp_stdout, *tmp_stderr, *tmp_obj;
505 size_t added_bytes = 0;
506 unsigned added_files = 0;
508 tmp_stdout = format("%s.tmp.stdout.%s", cached_obj, tmp_string());
509 tmp_stderr = format("%s.tmp.stderr.%s", cached_obj, tmp_string());
510 tmp_obj = format("%s.tmp.%s", cached_obj, tmp_string());
512 args_add(args, "-o");
513 args_add(args, tmp_obj);
515 /* Turn off DEPENDENCIES_OUTPUT when running cc1, because
516 * otherwise it will emit a line like
518 * tmp.stdout.vexed.732.o: /home/mbp/.ccache/tmp.stdout.vexed.732.i
520 * unsetenv() is on BSD and Linux but not portable. */
521 putenv("DEPENDENCIES_OUTPUT");
523 if (compile_preprocessed_source_code) {
524 args_add(args, i_tmpfile);
526 args_add(args, input_file);
529 cc_log("Running real compiler");
530 status = execute(args->argv, tmp_stdout, tmp_stderr);
533 if (stat(tmp_stdout, &st) != 0 || st.st_size != 0) {
534 cc_log("Compiler produced stdout");
535 stats_update(STATS_STDOUT);
536 tmp_unlink(tmp_stdout);
537 tmp_unlink(tmp_stderr);
541 tmp_unlink(tmp_stdout);
544 * Merge stderr from the preprocessor (if any) and stderr from the real
545 * compiler into tmp_stderr.
553 tmp_stderr2 = format("%s.tmp.stderr2.%s", cached_obj, tmp_string());
554 if (x_rename(tmp_stderr, tmp_stderr2)) {
555 cc_log("Failed to rename %s to %s: %s", tmp_stderr, tmp_stderr2,
559 fd_cpp_stderr = open(cpp_stderr, O_RDONLY | O_BINARY);
560 if (fd_cpp_stderr == -1) {
561 cc_log("Failed opening %s: %s", cpp_stderr, strerror(errno));
564 fd_real_stderr = open(tmp_stderr2, O_RDONLY | O_BINARY);
565 if (fd_real_stderr == -1) {
566 cc_log("Failed opening %s: %s", tmp_stderr2, strerror(errno));
569 fd_result = open(tmp_stderr, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
570 if (fd_result == -1) {
571 cc_log("Failed opening %s: %s", tmp_stderr, strerror(errno));
574 copy_fd(fd_cpp_stderr, fd_result);
575 copy_fd(fd_real_stderr, fd_result);
576 close(fd_cpp_stderr);
577 close(fd_real_stderr);
579 tmp_unlink(tmp_stderr2);
585 cc_log("Compiler gave exit status %d", status);
586 stats_update(STATS_STATUS);
588 fd = open(tmp_stderr, O_RDONLY | O_BINARY);
590 if (str_eq(output_obj, "/dev/null")
591 || (access(tmp_obj, R_OK) == 0
592 && move_file(tmp_obj, output_obj, 0) == 0)
593 || errno == ENOENT) {
594 /* we can use a quick method of getting the failed output */
597 tmp_unlink(tmp_stderr);
602 tmp_unlink(tmp_stderr);
607 if (stat(tmp_obj, &st) != 0) {
608 cc_log("Compiler didn't produce an object file");
609 stats_update(STATS_NOOUTPUT);
612 if (st.st_size == 0) {
613 cc_log("Compiler produced an empty object file");
614 stats_update(STATS_EMPTYOUTPUT);
618 if (stat(tmp_stderr, &st) != 0) {
619 cc_log("Failed to stat %s: %s", tmp_stderr, strerror(errno));
620 stats_update(STATS_ERROR);
623 if (st.st_size > 0) {
624 if (move_uncompressed_file(tmp_stderr, cached_stderr,
625 enable_compression) != 0) {
626 cc_log("Failed to move %s to %s: %s", tmp_stderr, cached_stderr,
628 stats_update(STATS_ERROR);
631 cc_log("Stored in cache: %s", cached_stderr);
632 if (enable_compression) {
633 stat(cached_stderr, &st);
635 added_bytes += file_size(&st);
638 tmp_unlink(tmp_stderr);
640 if (move_uncompressed_file(tmp_obj, cached_obj, enable_compression) != 0) {
641 cc_log("Failed to move %s to %s: %s", tmp_obj, cached_obj, strerror(errno));
642 stats_update(STATS_ERROR);
645 cc_log("Stored in cache: %s", cached_obj);
646 stat(cached_obj, &st);
647 added_bytes += file_size(&st);
652 * Do an extra stat on the potentially compressed object file for the
655 if (stat(cached_obj, &st) != 0) {
656 cc_log("Failed to stat %s: %s", cached_obj, strerror(errno));
657 stats_update(STATS_ERROR);
661 stats_update_size(STATS_TOCACHE, added_bytes / 1024, added_files);
669 * Find the object file name by running the compiler in preprocessor mode.
670 * Returns the hash as a heap-allocated hex string.
672 static struct file_hash *
673 get_object_name_from_cpp(struct args *args, struct mdfour *hash)
677 char *path_stdout, *path_stderr;
679 struct file_hash *result;
681 /* ~/hello.c -> tmp.hello.123.i
682 limit the basename to 10
683 characters in order to cope with filesystem with small
684 maximum filename length limits */
685 input_base = basename(input_file);
686 tmp = strchr(input_base, '.');
690 if (strlen(input_base) > 10) {
695 path_stdout = format("%s/%s.tmp.%s.%s",
696 temp_dir, input_base, tmp_string(), i_extension);
697 path_stderr = format("%s/tmp.cpp_stderr.%s", temp_dir, tmp_string());
699 time_of_compilation = time(NULL);
701 if (!direct_i_file) {
702 /* run cpp on the input file to obtain the .i */
703 args_add(args, "-E");
704 args_add(args, input_file);
705 status = execute(args->argv, path_stdout, path_stderr);
708 /* we are compiling a .i or .ii file - that means we
709 can skip the cpp stage and directly form the
711 path_stdout = input_file;
712 if (create_empty_file(path_stderr) != 0) {
713 cc_log("Failed to create %s: %s", path_stderr, strerror(errno));
714 stats_update(STATS_ERROR);
721 if (!direct_i_file) {
722 tmp_unlink(path_stdout);
724 tmp_unlink(path_stderr);
725 cc_log("Preprocessor gave exit status %d", status);
726 stats_update(STATS_PREPROCESSOR);
732 * When we are doing the unifying tricks we need to include the
733 * input file name in the hash to get the warnings right.
735 hash_delimiter(hash, "unifyfilename");
736 hash_string(hash, input_file);
738 hash_delimiter(hash, "unifycpp");
739 if (unify_hash(hash, path_stdout) != 0) {
740 stats_update(STATS_ERROR);
741 tmp_unlink(path_stderr);
742 cc_log("Failed to unify %s", path_stdout);
746 hash_delimiter(hash, "cpp");
747 if (!process_preprocessed_file(hash, path_stdout)) {
748 stats_update(STATS_ERROR);
749 tmp_unlink(path_stderr);
754 hash_delimiter(hash, "cppstderr");
755 if (!hash_file(hash, path_stderr)) {
756 fatal("Failed to open %s: %s", path_stderr, strerror(errno));
759 i_tmpfile = path_stdout;
761 if (compile_preprocessed_source_code) {
763 * If we are using the CPP trick, we need to remember this
764 * stderr data and output it just before the main stderr from
767 cpp_stderr = path_stderr;
769 tmp_unlink(path_stderr);
773 result = x_malloc(sizeof(*result));
774 hash_result_as_bytes(hash, result->hash);
775 result->size = hash->totalN;
780 update_cached_result_globals(struct file_hash *hash)
784 object_name = format_hash_as_string(hash->hash, hash->size);
785 cached_obj_hash = hash;
786 cached_obj = get_path_in_cache(object_name, ".o");
787 cached_stderr = get_path_in_cache(object_name, ".stderr");
788 cached_dep = get_path_in_cache(object_name, ".d");
789 stats_file = format("%s/%c/stats", cache_dir, object_name[0]);
794 * Update a hash sum with information common for the direct and preprocessor
798 calculate_common_hash(struct args *args, struct mdfour *hash)
801 const char *compilercheck;
804 hash_string(hash, HASH_PREFIX);
807 * We have to hash the extension, as a .i file isn't treated the same
808 * by the compiler as a .ii file.
810 hash_delimiter(hash, "ext");
811 hash_string(hash, i_extension);
813 if (stat(args->argv[0], &st) != 0) {
814 cc_log("Couldn't stat compiler %s: %s", args->argv[0], strerror(errno));
815 stats_update(STATS_COMPILER);
820 * Hash information about the compiler.
822 compilercheck = getenv("CCACHE_COMPILERCHECK");
823 if (!compilercheck) {
824 compilercheck = "mtime";
826 if (str_eq(compilercheck, "none")) {
828 } else if (str_eq(compilercheck, "content")) {
829 hash_delimiter(hash, "cc_content");
830 hash_file(hash, args->argv[0]);
831 } else if (str_eq(compilercheck, "mtime")) {
832 hash_delimiter(hash, "cc_mtime");
833 hash_int(hash, st.st_size);
834 hash_int(hash, st.st_mtime);
835 } else { /* command string */
836 if (!hash_multicommand_output(hash, compilercheck, orig_args->argv[0])) {
837 fatal("Failure running compiler check command: %s", compilercheck);
842 * Also hash the compiler name as some compilers use hard links and
843 * behave differently depending on the real name.
845 hash_delimiter(hash, "cc_name");
846 hash_string(hash, basename(args->argv[0]));
848 /* Possibly hash the current working directory. */
849 if (getenv("CCACHE_HASHDIR")) {
850 char *cwd = gnu_getcwd();
852 hash_delimiter(hash, "cwd");
853 hash_string(hash, cwd);
858 p = getenv("CCACHE_EXTRAFILES");
860 char *path, *q, *saveptr = NULL;
863 while ((path = strtok_r(q, PATH_DELIM, &saveptr))) {
864 cc_log("Hashing extra file %s", path);
865 hash_delimiter(hash, "extrafile");
866 if (!hash_file(hash, path)) {
867 stats_update(STATS_BADEXTRAFILE);
877 * Update a hash sum with information specific to the direct and preprocessor
878 * modes and calculate the object hash. Returns the object hash on success,
879 * otherwise NULL. Caller frees.
881 static struct file_hash *
882 calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode)
888 struct file_hash *object_hash = NULL;
890 /* first the arguments */
891 for (i = 1; i < args->argc; i++) {
892 /* -L doesn't affect compilation. */
893 if (i < args->argc-1 && str_eq(args->argv[i], "-L")) {
897 if (str_startswith(args->argv[i], "-L")) {
901 /* When using the preprocessor, some arguments don't contribute
902 to the hash. The theory is that these arguments will change
903 the output of -E if they are going to have any effect at
904 all. For precompiled headers this might not be the case. */
905 if (!direct_mode && !output_is_precompiled_header
906 && !using_precompiled_header) {
907 if (compopt_affects_cpp(args->argv[i])) {
911 if (compopt_short(compopt_affects_cpp, args->argv[i])) {
916 if (str_startswith(args->argv[i], "--specs=") &&
917 stat(args->argv[i] + 8, &st) == 0) {
918 /* If given a explicit specs file, then hash that file,
919 but don't include the path to it in the hash. */
920 hash_delimiter(hash, "specs");
921 if (!hash_file(hash, args->argv[i] + 8)) {
927 /* All other arguments are included in the hash. */
928 hash_delimiter(hash, "arg");
929 hash_string(hash, args->argv[i]);
933 if (!(sloppiness & SLOPPY_FILE_MACRO)) {
935 * The source code file or an include file may contain
936 * __FILE__, so make sure that the hash is unique for
939 hash_delimiter(hash, "inputfile");
940 hash_string(hash, input_file);
943 hash_delimiter(hash, "sourcecode");
944 result = hash_source_code_file(hash, input_file);
945 if (result & HASH_SOURCE_CODE_ERROR) {
948 if (result & HASH_SOURCE_CODE_FOUND_TIME) {
949 cc_log("Disabling direct mode");
950 enable_direct = false;
953 manifest_name = hash_result(hash);
954 manifest_path = get_path_in_cache(manifest_name, ".manifest");
956 cc_log("Looking for object file hash in %s", manifest_path);
957 object_hash = manifest_get(manifest_path);
959 cc_log("Got object file hash from manifest");
961 cc_log("Did not find object file hash in manifest");
964 object_hash = get_object_name_from_cpp(args, hash);
965 cc_log("Got object file hash from preprocessor");
966 if (generating_dependencies) {
967 cc_log("Preprocessor created %s", output_dep);
975 * Try to return the compile result from cache. If we can return from cache
976 * then this function exits with the correct status code, otherwise it returns.
979 from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest)
984 bool produce_dep_file;
986 /* the user might be disabling cache hits */
987 if (mode != FROMCACHE_COMPILED_MODE && getenv("CCACHE_RECACHE")) {
991 /* Check if the object file is there. */
992 if (stat(cached_obj, &st) != 0) {
993 cc_log("Object file %s not in cache", cached_obj);
998 * (If mode != FROMCACHE_DIRECT_MODE, the dependency file is created by
1001 produce_dep_file = generating_dependencies && mode == FROMCACHE_DIRECT_MODE;
1003 /* If the dependency file should be in the cache, check that it is. */
1004 if (produce_dep_file && stat(cached_dep, &st) != 0) {
1005 cc_log("Dependency file %s missing in cache", cached_dep);
1009 if (str_eq(output_obj, "/dev/null")) {
1012 x_unlink(output_obj);
1013 /* only make a hardlink if the cache file is uncompressed */
1014 if (getenv("CCACHE_HARDLINK") && !file_is_compressed(cached_obj)) {
1015 ret = link(cached_obj, output_obj);
1017 ret = copy_file(cached_obj, output_obj, 0);
1022 if (errno == ENOENT) {
1023 /* Someone removed the file just before we began copying? */
1024 cc_log("Object file %s just disappeared from cache", cached_obj);
1025 stats_update(STATS_MISSING);
1027 cc_log("Failed to copy/link %s to %s: %s",
1028 cached_obj, output_obj, strerror(errno));
1029 stats_update(STATS_ERROR);
1032 x_unlink(output_obj);
1033 x_unlink(cached_stderr);
1034 x_unlink(cached_obj);
1035 x_unlink(cached_dep);
1038 cc_log("Created %s from %s", output_obj, cached_obj);
1041 if (produce_dep_file) {
1042 x_unlink(output_dep);
1043 /* only make a hardlink if the cache file is uncompressed */
1044 if (getenv("CCACHE_HARDLINK") && !file_is_compressed(cached_dep)) {
1045 ret = link(cached_dep, output_dep);
1047 ret = copy_file(cached_dep, output_dep, 0);
1050 if (errno == ENOENT) {
1052 * Someone removed the file just before we
1055 cc_log("Dependency file %s just disappeared from cache", output_obj);
1056 stats_update(STATS_MISSING);
1058 cc_log("Failed to copy/link %s to %s: %s",
1059 cached_dep, output_dep, strerror(errno));
1060 stats_update(STATS_ERROR);
1063 x_unlink(output_obj);
1064 x_unlink(output_dep);
1065 x_unlink(cached_stderr);
1066 x_unlink(cached_obj);
1067 x_unlink(cached_dep);
1070 cc_log("Created %s from %s", output_dep, cached_dep);
1074 /* Update modification timestamps to save files from LRU cleanup.
1075 Also gives files a sensible mtime when hard-linking. */
1076 update_mtime(cached_obj);
1077 update_mtime(cached_stderr);
1078 if (produce_dep_file) {
1079 update_mtime(cached_dep);
1082 if (generating_dependencies && mode != FROMCACHE_DIRECT_MODE) {
1083 /* Store the dependency file in the cache. */
1084 ret = copy_file(output_dep, cached_dep, enable_compression);
1086 cc_log("Failed to copy %s to %s: %s", output_dep, cached_dep,
1088 /* Continue despite the error. */
1090 cc_log("Stored in cache: %s", cached_dep);
1091 stat(cached_dep, &st);
1092 stats_update_size(STATS_NONE, file_size(&st) / 1024, 1);
1096 /* Send the stderr, if any. */
1097 fd_stderr = open(cached_stderr, O_RDONLY | O_BINARY);
1098 if (fd_stderr != -1) {
1099 copy_fd(fd_stderr, 2);
1103 /* Create or update the manifest file. */
1105 && put_object_in_manifest
1107 && !getenv("CCACHE_READONLY")) {
1109 size_t old_size = 0; /* in bytes */
1110 if (stat(manifest_path, &st) == 0) {
1111 old_size = file_size(&st);
1113 if (manifest_put(manifest_path, cached_obj_hash, included_files)) {
1114 cc_log("Added object file hash to %s", manifest_path);
1115 update_mtime(manifest_path);
1116 stat(manifest_path, &st);
1117 stats_update_size(STATS_NONE,
1118 (file_size(&st) - old_size) / 1024,
1119 old_size == 0 ? 1 : 0);
1121 cc_log("Failed to add object file hash to %s", manifest_path);
1125 /* log the cache hit */
1127 case FROMCACHE_DIRECT_MODE:
1128 cc_log("Succeded getting cached result");
1129 stats_update(STATS_CACHEHIT_DIR);
1132 case FROMCACHE_CPP_MODE:
1133 cc_log("Succeded getting cached result");
1134 stats_update(STATS_CACHEHIT_CPP);
1137 case FROMCACHE_COMPILED_MODE:
1138 /* Stats already updated in to_cache(). */
1142 /* and exit with the right status code */
1146 /* find the real compiler. We just search the PATH to find a executable of the
1147 same name that isn't a link to ourselves */
1149 find_compiler(int argc, char **argv)
1155 orig_args = args_init(argc, argv);
1157 base = basename(argv[0]);
1159 /* we might be being invoked like "ccache gcc -c foo.c" */
1160 if (same_executable_name(base, MYNAME)) {
1161 args_remove_first(orig_args);
1163 if (is_full_path(argv[1])) {
1164 /* a full path was given */
1167 base = basename(argv[1]);
1170 /* support user override of the compiler */
1171 if ((path = getenv("CCACHE_CC"))) {
1172 base = x_strdup(path);
1175 compiler = find_executable(base, MYNAME);
1177 /* can't find the compiler! */
1179 stats_update(STATS_COMPILER);
1180 fatal("Could not find compiler \"%s\" in PATH", base);
1182 if (str_eq(compiler, argv[0])) {
1183 fatal("Recursive invocation (the name of the ccache binary must be \"%s\")",
1186 orig_args->argv[0] = compiler;
1190 is_precompiled_header(const char *path)
1192 return str_eq(get_extension(path), ".gch");
1196 * Process the compiler options into options suitable for passing to the
1197 * preprocessor and the real compiler. The preprocessor options don't include
1198 * -E; this is added later. Returns true on success, otherwise false.
1201 cc_process_args(struct args *orig_args, struct args **preprocessor_args,
1202 struct args **compiler_args)
1205 bool found_c_opt = false;
1206 bool found_S_opt = false;
1207 bool found_arch_opt = false;
1208 bool found_pch = false;
1209 bool found_fpch_preprocess = false;
1210 const char *explicit_language = NULL; /* As specified with -x. */
1211 const char *file_language; /* As deduced from file extension. */
1212 const char *actual_language; /* Language to actually use. */
1213 const char *input_charset = NULL;
1215 /* is the dependency makefile name overridden with -MF? */
1216 bool dependency_filename_specified = false;
1217 /* is the dependency makefile target name specified with -MT or -MQ? */
1218 bool dependency_target_specified = false;
1219 struct args *stripped_args = NULL, *dep_args = NULL;
1220 int argc = orig_args->argc;
1221 char **argv = orig_args->argv;
1224 stripped_args = args_init(0, NULL);
1225 dep_args = args_init(0, NULL);
1227 args_add(stripped_args, argv[0]);
1229 for (i = 1; i < argc; i++) {
1230 /* The user knows best: just swallow the next arg */
1231 if (str_eq(argv[i], "--ccache-skip")) {
1234 cc_log("--ccache-skip lacks an argument");
1238 args_add(stripped_args, argv[i]);
1242 /* Special case for -E. */
1243 if (str_eq(argv[i], "-E")) {
1244 stats_update(STATS_PREPROCESSING);
1249 /* These are always too hard. */
1250 if (compopt_too_hard(argv[i])
1251 || str_startswith(argv[i], "@")
1252 || str_startswith(argv[i], "-fdump-")) {
1253 cc_log("Compiler option %s is unsupported", argv[i]);
1254 stats_update(STATS_UNSUPPORTED);
1259 /* These are too hard in direct mode. */
1260 if (enable_direct) {
1261 if (compopt_too_hard_for_direct_mode(argv[i])) {
1262 cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
1263 enable_direct = false;
1267 /* Multiple -arch options are too hard. */
1268 if (str_eq(argv[i], "-arch")) {
1269 if (found_arch_opt) {
1270 cc_log("More than one -arch compiler option is unsupported");
1271 stats_update(STATS_UNSUPPORTED);
1275 found_arch_opt = true;
1279 if (str_eq(argv[i], "-fpch-preprocess")) {
1280 found_fpch_preprocess = true;
1283 /* we must have -c */
1284 if (str_eq(argv[i], "-c")) {
1285 args_add(stripped_args, argv[i]);
1290 /* -S changes the default extension */
1291 if (str_eq(argv[i], "-S")) {
1292 args_add(stripped_args, argv[i]);
1298 * Special handling for -x: remember the last specified language before the
1299 * input file and strip all -x options from the arguments.
1301 if (str_eq(argv[i], "-x")) {
1303 cc_log("Missing argument to %s", argv[i]);
1304 stats_update(STATS_ARGS);
1309 explicit_language = argv[i+1];
1314 if (str_startswith(argv[i], "-x")) {
1316 explicit_language = &argv[i][2];
1321 /* we need to work out where the output was meant to go */
1322 if (str_eq(argv[i], "-o")) {
1324 cc_log("Missing argument to %s", argv[i]);
1325 stats_update(STATS_ARGS);
1329 output_obj = argv[i+1];
1334 /* alternate form of -o, with no space */
1335 if (str_startswith(argv[i], "-o")) {
1336 output_obj = &argv[i][2];
1340 /* debugging is handled specially, so that we know if we
1341 can strip line number info
1343 if (str_startswith(argv[i], "-g")) {
1344 args_add(stripped_args, argv[i]);
1345 if (enable_unify && !str_eq(argv[i], "-g0")) {
1346 cc_log("%s used; disabling unify mode", argv[i]);
1347 enable_unify = false;
1349 if (str_eq(argv[i], "-g3")) {
1351 * Fix for bug 7190 ("commandline macros (-D)
1352 * have non-zero lineno when using -g3").
1354 cc_log("%s used; not compiling preprocessed code", argv[i]);
1355 compile_preprocessed_source_code = false;
1360 /* These options require special handling, because they
1361 behave differently with gcc -E, when the output
1362 file is not specified. */
1363 if (str_eq(argv[i], "-MD") || str_eq(argv[i], "-MMD")) {
1364 generating_dependencies = true;
1365 args_add(dep_args, argv[i]);
1368 if (str_startswith(argv[i], "-MF")) {
1370 dependency_filename_specified = true;
1372 args_add(dep_args, argv[i]);
1373 if (strlen(argv[i]) == 3) {
1375 if (i >= argc - 1) {
1376 cc_log("Missing argument to %s", argv[i]);
1377 stats_update(STATS_ARGS);
1382 args_add(dep_args, argv[i + 1]);
1388 output_dep = make_relative_path(x_strdup(arg));
1391 if (str_startswith(argv[i], "-MQ") || str_startswith(argv[i], "-MT")) {
1392 args_add(dep_args, argv[i]);
1393 if (strlen(argv[i]) == 3) {
1394 /* -MQ arg or -MT arg */
1395 if (i >= argc - 1) {
1396 cc_log("Missing argument to %s", argv[i]);
1397 stats_update(STATS_ARGS);
1401 args_add(dep_args, argv[i + 1]);
1404 * Yes, that's right. It's strange, but apparently, GCC behaves
1405 * differently for -MT arg and -MTarg (and similar for -MQ): in the
1406 * latter case, but not in the former, an implicit dependency for the
1407 * object file is added to the dependency file.
1409 dependency_target_specified = true;
1413 if (str_startswith(argv[i], "--sysroot=")) {
1414 char *relpath = make_relative_path(x_strdup(argv[i] + 10));
1415 char *option = format("--sysroot=%s", relpath);
1416 args_add(stripped_args, option);
1421 if (str_startswith(argv[i], "-Wp,")) {
1422 if (str_startswith(argv[i], "-Wp,-MD,") && !strchr(argv[i] + 8, ',')) {
1423 generating_dependencies = true;
1424 dependency_filename_specified = true;
1426 output_dep = make_relative_path(x_strdup(argv[i] + 8));
1427 args_add(dep_args, argv[i]);
1429 } else if (str_startswith(argv[i], "-Wp,-MMD,")
1430 && !strchr(argv[i] + 9, ',')) {
1431 generating_dependencies = true;
1432 dependency_filename_specified = true;
1434 output_dep = make_relative_path(x_strdup(argv[i] + 9));
1435 args_add(dep_args, argv[i]);
1437 } else if (enable_direct) {
1439 * -Wp, can be used to pass too hard options to
1440 * the preprocessor. Hence, disable direct
1443 cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
1444 enable_direct = false;
1447 if (str_eq(argv[i], "-MP")) {
1448 args_add(dep_args, argv[i]);
1452 /* Input charset needs to be handled specially. */
1453 if (str_startswith(argv[i], "-finput-charset=")) {
1454 input_charset = argv[i];
1459 * Options taking an argument that that we may want to rewrite
1460 * to relative paths to get better hit rate. A secondary effect
1461 * is that paths in the standard error output produced by the
1462 * compiler will be normalized.
1464 if (compopt_takes_path(argv[i])) {
1468 cc_log("Missing argument to %s", argv[i]);
1469 stats_update(STATS_ARGS);
1474 args_add(stripped_args, argv[i]);
1475 relpath = make_relative_path(x_strdup(argv[i+1]));
1476 args_add(stripped_args, relpath);
1478 /* Try to be smart about detecting precompiled headers */
1479 pchpath = format("%s.gch", argv[i+1]);
1480 if (stat(pchpath, &st) == 0) {
1481 cc_log("Detected use of precompiled header: %s", pchpath);
1491 /* Same as above but options with concatenated argument. */
1492 if (compopt_short(compopt_takes_path, argv[i])) {
1495 relpath = make_relative_path(x_strdup(argv[i] + 2));
1496 option = format("-%c%s", argv[i][1], relpath);
1497 args_add(stripped_args, option);
1503 /* options that take an argument */
1504 if (compopt_takes_arg(argv[i])) {
1506 cc_log("Missing argument to %s", argv[i]);
1507 stats_update(STATS_ARGS);
1511 args_add(stripped_args, argv[i]);
1512 args_add(stripped_args, argv[i+1]);
1518 if (argv[i][0] == '-') {
1519 args_add(stripped_args, argv[i]);
1523 /* if an argument isn't a plain file then assume its
1524 an option, not an input file. This allows us to
1525 cope better with unusual compiler options */
1526 if (stat(argv[i], &st) != 0 || !S_ISREG(st.st_mode)) {
1527 cc_log("%s is not a regular file, not considering as input file",
1529 args_add(stripped_args, argv[i]);
1534 if (language_for_file(argv[i])) {
1535 cc_log("Multiple input files: %s and %s", input_file, argv[i]);
1536 stats_update(STATS_MULTIPLE);
1537 } else if (!found_c_opt) {
1538 cc_log("Called for link with %s", argv[i]);
1539 if (strstr(argv[i], "conftest.")) {
1540 stats_update(STATS_CONFTEST);
1542 stats_update(STATS_LINK);
1545 cc_log("Unsupported source extension: %s", argv[i]);
1546 stats_update(STATS_SOURCELANG);
1552 /* Rewrite to relative to increase hit rate. */
1553 input_file = make_relative_path(x_strdup(argv[i]));
1557 cc_log("No input file found");
1558 stats_update(STATS_NOINPUT);
1563 if (found_pch || found_fpch_preprocess) {
1564 using_precompiled_header = true;
1565 if (!(sloppiness & SLOPPY_TIME_MACROS)) {
1566 cc_log("You have to specify \"time_macros\" sloppiness when using"
1567 " precompiled headers to get direct hits");
1568 cc_log("Disabling direct mode");
1569 stats_update(STATS_CANTUSEPCH);
1575 if (explicit_language && str_eq(explicit_language, "none")) {
1576 explicit_language = NULL;
1578 file_language = language_for_file(input_file);
1579 if (explicit_language) {
1580 if (!language_is_supported(explicit_language)) {
1581 cc_log("Unsupported language: %s", explicit_language);
1582 stats_update(STATS_SOURCELANG);
1586 actual_language = explicit_language;
1588 actual_language = file_language;
1591 output_is_precompiled_header =
1592 actual_language && strstr(actual_language, "-header") != NULL;
1594 if (!found_c_opt && !output_is_precompiled_header) {
1595 cc_log("No -c option found");
1596 /* I find that having a separate statistic for autoconf tests is useful,
1597 as they are the dominant form of "called for link" in many cases */
1598 if (strstr(input_file, "conftest.")) {
1599 stats_update(STATS_CONFTEST);
1601 stats_update(STATS_LINK);
1607 if (!actual_language) {
1608 cc_log("Unsupported source extension: %s", input_file);
1609 stats_update(STATS_SOURCELANG);
1614 direct_i_file = language_is_preprocessed(actual_language);
1616 if (output_is_precompiled_header) {
1617 /* It doesn't work to create the .gch from preprocessed source. */
1618 cc_log("Creating precompiled header; not compiling preprocessed code");
1619 compile_preprocessed_source_code = false;
1622 i_extension = getenv("CCACHE_EXTENSION");
1624 const char *p_language = p_language_for_language(actual_language);
1625 i_extension = extension_for_language(p_language) + 1;
1628 /* don't try to second guess the compilers heuristics for stdout handling */
1629 if (output_obj && str_eq(output_obj, "-")) {
1630 stats_update(STATS_OUTSTDOUT);
1631 cc_log("Output file is -");
1637 if (output_is_precompiled_header) {
1638 output_obj = format("%s.gch", input_file);
1641 output_obj = x_strdup(input_file);
1642 if ((p = strrchr(output_obj, '/'))) {
1645 p = strrchr(output_obj, '.');
1647 cc_log("Badly formed object filename");
1648 stats_update(STATS_ARGS);
1652 p[1] = found_S_opt ? 's' : 'o';
1657 /* cope with -o /dev/null */
1658 if (!str_eq(output_obj,"/dev/null")
1659 && stat(output_obj, &st) == 0
1660 && !S_ISREG(st.st_mode)) {
1661 cc_log("Not a regular file: %s", output_obj);
1662 stats_update(STATS_DEVICE);
1668 * Some options shouldn't be passed to the real compiler when it compiles
1669 * preprocessed code:
1671 * -finput-charset=XXX (otherwise conversion happens twice)
1672 * -x XXX (otherwise the wrong language is selected)
1674 *preprocessor_args = args_copy(stripped_args);
1675 if (input_charset) {
1676 args_add(*preprocessor_args, input_charset);
1679 args_add(*preprocessor_args, "-fpch-preprocess");
1681 if (explicit_language) {
1682 args_add(*preprocessor_args, "-x");
1683 args_add(*preprocessor_args, explicit_language);
1687 * Add flags for dependency generation only to the preprocessor command line.
1689 if (generating_dependencies) {
1690 if (!dependency_filename_specified) {
1691 char *default_depfile_name;
1694 base_name = remove_extension(output_obj);
1695 default_depfile_name = format("%s.d", base_name);
1697 args_add(dep_args, "-MF");
1698 args_add(dep_args, default_depfile_name);
1699 output_dep = make_relative_path(x_strdup(default_depfile_name));
1702 if (!dependency_target_specified) {
1703 args_add(dep_args, "-MQ");
1704 args_add(dep_args, output_obj);
1708 if (compile_preprocessed_source_code) {
1709 *compiler_args = args_copy(stripped_args);
1710 if (explicit_language) {
1712 * Workaround for a bug in Apple's patched distcc -- it doesn't properly
1713 * reset the language specified with -x, so if -x is given, we have to
1714 * specify the preprocessed language explicitly.
1716 args_add(*compiler_args, "-x");
1717 args_add(*compiler_args, p_language_for_language(explicit_language));
1720 *compiler_args = args_copy(*preprocessor_args);
1724 * Only pass dependency arguments to the preprocesor since Intel's C++
1725 * compiler doesn't produce a correct .d file when compiling preprocessed
1728 args_extend(*preprocessor_args, dep_args);
1731 args_free(stripped_args);
1732 args_free(dep_args);
1736 /* Reset the global state. Used by the test suite. */
1740 free(current_working_dir); current_working_dir = NULL;
1741 free(cache_dir); cache_dir = NULL;
1742 cache_logfile = NULL;
1744 args_free(orig_args); orig_args = NULL;
1745 free(input_file); input_file = NULL;
1747 free(output_dep); output_dep = NULL;
1748 free(cached_obj_hash); cached_obj_hash = NULL;
1749 free(cached_obj); cached_obj = NULL;
1750 free(cached_stderr); cached_stderr = NULL;
1751 free(cached_dep); cached_dep = NULL;
1752 free(manifest_path); manifest_path = NULL;
1753 time_of_compilation = 0;
1755 if (included_files) {
1756 hashtable_destroy(included_files, 1); included_files = NULL;
1758 generating_dependencies = false;
1761 direct_i_file = false;
1762 free(cpp_stderr); cpp_stderr = NULL;
1763 free(stats_file); stats_file = NULL;
1764 enable_unify = false;
1765 enable_direct = true;
1766 enable_compression = false;
1768 compile_preprocessed_source_code = false;
1769 output_is_precompiled_header = false;
1773 parse_sloppiness(char *p)
1775 unsigned result = 0;
1776 char *word, *q, *saveptr = NULL;
1783 while ((word = strtok_r(q, ", ", &saveptr))) {
1784 if (str_eq(word, "file_macro")) {
1785 cc_log("Being sloppy about __FILE__");
1786 result |= SLOPPY_FILE_MACRO;
1788 if (str_eq(word, "include_file_mtime")) {
1789 cc_log("Being sloppy about include file mtime");
1790 result |= SLOPPY_INCLUDE_FILE_MTIME;
1792 if (str_eq(word, "time_macros")) {
1793 cc_log("Being sloppy about __DATE__ and __TIME__");
1794 result |= SLOPPY_TIME_MACROS;
1802 /* the main ccache driver function */
1804 ccache(int argc, char *argv[])
1806 bool put_object_in_manifest = false;
1807 struct file_hash *object_hash;
1808 struct file_hash *object_hash_from_manifest = NULL;
1810 struct mdfour common_hash;
1811 struct mdfour direct_hash;
1812 struct mdfour cpp_hash;
1814 /* Arguments (except -E) to send to the preprocessor. */
1815 struct args *preprocessor_args;
1817 /* Arguments to send to the real compiler. */
1818 struct args *compiler_args;
1820 find_compiler(argc, argv);
1822 if (getenv("CCACHE_DISABLE")) {
1823 cc_log("ccache is disabled");
1827 sloppiness = parse_sloppiness(getenv("CCACHE_SLOPPINESS"));
1829 cc_log_argv("Command line: ", argv);
1830 cc_log("Hostname: %s", get_hostname());
1831 cc_log("Working directory: %s", current_working_dir);
1834 cc_log("Base directory: %s", base_dir);
1837 if (getenv("CCACHE_UNIFY")) {
1838 cc_log("Unify mode disabled");
1839 enable_unify = true;
1842 if (getenv("CCACHE_NODIRECT") || enable_unify) {
1843 cc_log("Direct mode disabled");
1844 enable_direct = false;
1847 if (getenv("CCACHE_COMPRESS")) {
1848 cc_log("Compression enabled");
1849 enable_compression = true;
1852 if ((env = getenv("CCACHE_NLEVELS"))) {
1853 nlevels = atoi(env);
1854 if (nlevels < 1) nlevels = 1;
1855 if (nlevels > 8) nlevels = 8;
1858 if (!cc_process_args(orig_args, &preprocessor_args, &compiler_args)) {
1862 cc_log("Source file: %s", input_file);
1863 if (generating_dependencies) {
1864 cc_log("Dependency file: %s", output_dep);
1866 cc_log("Object file: %s", output_obj);
1868 hash_start(&common_hash);
1869 calculate_common_hash(preprocessor_args, &common_hash);
1871 /* try to find the hash using the manifest */
1872 direct_hash = common_hash;
1873 if (enable_direct) {
1874 cc_log("Trying direct lookup");
1875 object_hash = calculate_object_hash(preprocessor_args, &direct_hash, 1);
1877 update_cached_result_globals(object_hash);
1880 * If we can return from cache at this point then do
1883 from_cache(FROMCACHE_DIRECT_MODE, 0);
1886 * Wasn't able to return from cache at this point.
1887 * However, the object was already found in manifest,
1888 * so don't readd it later.
1890 put_object_in_manifest = false;
1892 object_hash_from_manifest = object_hash;
1894 /* Add object to manifest later. */
1895 put_object_in_manifest = true;
1900 * Find the hash using the preprocessed output. Also updates
1903 cpp_hash = common_hash;
1904 cc_log("Running preprocessor");
1905 object_hash = calculate_object_hash(preprocessor_args, &cpp_hash, 0);
1907 fatal("internal error: object hash from cpp returned NULL");
1909 update_cached_result_globals(object_hash);
1911 if (object_hash_from_manifest
1912 && !file_hashes_equal(object_hash_from_manifest, object_hash)) {
1914 * The hash from manifest differs from the hash of the
1915 * preprocessor output. This could be because:
1917 * - The preprocessor produces different output for the same
1918 * input (not likely).
1919 * - There's a bug in ccache (maybe incorrect handling of
1920 * compiler arguments).
1921 * - The user has used a different CCACHE_BASEDIR (most
1924 * The best thing here would probably be to remove the hash
1925 * entry from the manifest. For now, we use a simpler method:
1926 * just remove the manifest file.
1928 cc_log("Hash from manifest doesn't match preprocessor output");
1929 cc_log("Likely reason: different CCACHE_BASEDIRs used");
1930 cc_log("Removing manifest as a safety measure");
1931 x_unlink(manifest_path);
1933 put_object_in_manifest = true;
1936 /* if we can return from cache at this point then do */
1937 from_cache(FROMCACHE_CPP_MODE, put_object_in_manifest);
1939 if (getenv("CCACHE_READONLY")) {
1940 cc_log("Read-only mode; running real compiler");
1944 env = getenv("CCACHE_PREFIX");
1946 char *p = find_executable(env, MYNAME);
1948 fatal("%s: %s", env, strerror(errno));
1950 cc_log("Using command-line prefix %s", env);
1951 args_add_prefix(compiler_args, p);
1954 /* run real compiler, sending output to cache */
1955 to_cache(compiler_args);
1957 /* return from cache */
1958 from_cache(FROMCACHE_COMPILED_MODE, put_object_in_manifest);
1961 cc_log("Secondary from_cache failed");
1962 stats_update(STATS_ERROR);
1967 check_cache_dir(void)
1970 fatal("Unable to determine cache directory");
1974 /* the main program when not doing a compile */
1976 ccache_main_options(int argc, char *argv[])
1981 static const struct option options[] = {
1982 {"show-stats", no_argument, 0, 's'},
1983 {"zero-stats", no_argument, 0, 'z'},
1984 {"cleanup", no_argument, 0, 'c'},
1985 {"clear", no_argument, 0, 'C'},
1986 {"max-files", required_argument, 0, 'F'},
1987 {"max-size", required_argument, 0, 'M'},
1988 {"help", no_argument, 0, 'h'},
1989 {"version", no_argument, 0, 'V'},
1993 while ((c = getopt_long(argc, argv, "hszcCF:M:V", options, NULL)) != -1) {
1996 fprintf(stdout, VERSION_TEXT, CCACHE_VERSION);
2000 fputs(USAGE_TEXT, stdout);
2010 cleanup_all(cache_dir);
2011 printf("Cleaned cache\n");
2016 wipe_all(cache_dir);
2017 printf("Cleared cache\n");
2023 printf("Statistics cleared\n");
2029 if (stats_set_limits(v, -1) == 0) {
2031 printf("Unset cache file limit\n");
2033 printf("Set cache file limit to %u\n", (unsigned)v);
2036 printf("Could not set cache file limit.\n");
2043 v = value_units(optarg);
2044 if (stats_set_limits(-1, v) == 0) {
2046 printf("Unset cache size limit\n");
2048 char *s = format_size(v);
2049 printf("Set cache size limit to %s\n", s);
2053 printf("Could not set cache size limit.\n");
2059 fputs(USAGE_TEXT, stderr);
2068 /* Make a copy of stderr that will not be cached, so things like
2069 distcc can send networking errors to it. */
2071 setup_uncached_err(void)
2076 uncached_fd = dup(2);
2077 if (uncached_fd == -1) {
2078 cc_log("dup(2) failed: %s", strerror(errno));
2082 /* leak a pointer to the environment */
2083 buf = format("UNCACHED_ERR_FD=%d", uncached_fd);
2085 if (putenv(buf) == -1) {
2086 cc_log("putenv failed: %s", strerror(errno));
2092 ccache_main(int argc, char *argv[])
2098 exitfn_add_nullary(stats_flush);
2099 exitfn_add_nullary(clean_up_tmp_files);
2101 /* check for logging early so cc_log messages start working ASAP */
2102 cache_logfile = getenv("CCACHE_LOGFILE");
2103 cc_log("=== CCACHE STARTED =========================================");
2105 /* the user might have set CCACHE_UMASK */
2106 p = getenv("CCACHE_UMASK");
2110 mask = strtol(p, NULL, 8);
2116 current_working_dir = get_cwd();
2117 if (!current_working_dir) {
2118 cc_log("Could not determine current working directory");
2121 cache_dir = getenv("CCACHE_DIR");
2123 cache_dir = x_strdup(cache_dir);
2125 const char *home_directory = get_home_directory();
2126 if (home_directory) {
2127 cache_dir = format("%s/.ccache", home_directory);
2131 /* check if we are being invoked as "ccache" */
2132 program_name = basename(argv[0]);
2133 if (same_executable_name(program_name, MYNAME)) {
2135 fputs(USAGE_TEXT, stderr);
2138 /* if the first argument isn't an option, then assume we are
2139 being passed a compiler name and options */
2140 if (argv[1][0] == '-') {
2141 return ccache_main_options(argc, argv);
2148 temp_dir = getenv("CCACHE_TEMPDIR");
2150 temp_dir = format("%s/tmp", cache_dir);
2153 base_dir = getenv("CCACHE_BASEDIR");
2154 if (base_dir && base_dir[0] != '/') {
2155 cc_log("Ignoring non-absolute base directory %s", base_dir);
2159 compile_preprocessed_source_code = !getenv("CCACHE_CPP2");
2161 setup_uncached_err();
2163 /* make sure the cache dir exists */
2164 if (create_dir(cache_dir) != 0) {
2166 "ccache: failed to create %s (%s)\n",
2167 cache_dir, strerror(errno));
2171 /* make sure the temp dir exists */
2172 if (create_dir(temp_dir) != 0) {
2174 "ccache: failed to create %s (%s)\n",
2175 temp_dir, strerror(errno));
2179 if (!getenv("CCACHE_READONLY")) {
2180 if (create_cachedirtag(cache_dir) != 0) {
2182 "ccache: failed to create %s/CACHEDIR.TAG (%s)\n",
2183 cache_dir, strerror(errno));