]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Fix handling of the -MD and -MDD options
authorAndrea Bittau <a.bittau@cs.ucl.ac.uk>
Sun, 1 Nov 2009 18:39:58 +0000 (19:39 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 5 Jan 2010 17:53:00 +0000 (18:53 +0100)
From <http://lists.samba.org/archive/ccache/2007q2/000272.html>:

The -MD and -MDD options automatically determine where the dependency file
should land and what the target should look like based on the -o option.
However, ccache drops -o and things mess up. The original patch was posted by
Kaz Kylheku but I reworked it to make it work properly. Here is his post:

http://lists.samba.org/archive/ccache/2006q4/000249.html

ccache.c

index 172fcb131eca63547b4e259a8755f23f7780d8ee..b80a0de9e9063583ee27833412b8a3f872fd9ff8 100644 (file)
--- a/ccache.c
+++ b/ccache.c
@@ -651,6 +651,13 @@ static void process_args(int argc, char **argv)
        int found_S_opt = 0;
        struct stat st;
        char *e;
+       /* is gcc being asked to output dependencies? */
+       int generating_dependencies = 0;
+       /* is the dependency makefile name overridden with -MF? */
+       int dependency_filename_specified = 0;
+       /* is the dependency makefile target name specified with -MQ or -MF? */
+       int dependency_target_specified = 0;
+
 
        stripped_args = args_init(0, NULL);
 
@@ -728,6 +735,18 @@ static void process_args(int argc, char **argv)
                        continue;
                }
 
+               /* These options require special handling, because they
+                  behave differently with gcc -E, when the output
+                  file is not specified. */
+
+               if (strcmp(argv[i], "-MD") == 0 || strcmp(argv[i], "-MMD") == 0) {
+                       generating_dependencies = 1;
+               } else if (strcmp(argv[i], "-MF") == 0) {
+                       dependency_filename_specified = 1;
+               } else if (strcmp(argv[i], "-MQ") == 0 || strcmp(argv[i], "-MT") == 0) {
+                       dependency_target_specified = 1;
+               }
+
                /* options that take an argument */
                {
                        const char *opts[] = {"-I", "-include", "-imacros", "-iprefix",
@@ -840,6 +859,41 @@ static void process_args(int argc, char **argv)
                p[2] = 0;
        }
 
+       /* If dependencies are generated, configure the preprocessor */
+
+       if (generating_dependencies && output_file) {
+               if (!dependency_filename_specified) {
+                       char *default_depfile_name = x_strdup(output_file);
+                       char *p = strrchr(default_depfile_name, '.');
+
+                       if (p) {
+                               if (strlen(p) < 2) {
+                                       stats_update(STATS_ARGS);
+                                       failed();
+                                       return;
+                               }
+                               *p = 0;
+                       }
+                       else  {
+                               int len = p - default_depfile_name;
+                               
+                               p = x_malloc(len + 3);
+                               strncpy(default_depfile_name, p, len - 1);
+                               free(default_depfile_name);
+                               default_depfile_name = p;
+                       }
+
+                       strcat(default_depfile_name, ".d");
+                       args_add(stripped_args, "-MF");
+                       args_add(stripped_args, default_depfile_name);
+               }
+
+               if (!dependency_target_specified) {
+                       args_add(stripped_args, "-MT");
+                       args_add(stripped_args, output_file);
+               }
+       }
+
        /* cope with -o /dev/null */
        if (strcmp(output_file,"/dev/null") != 0 && stat(output_file, &st) == 0 && !S_ISREG(st.st_mode)) {
                cc_log("Not a regular file %s\n", output_file);