Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * "Optimize" a list of dependencies as spit out by gcc -MD
0003  * for the kernel build
0004  * ===========================================================================
0005  *
0006  * Author       Kai Germaschewski
0007  * Copyright    2002 by Kai Germaschewski  <kai.germaschewski@gmx.de>
0008  *
0009  * This software may be used and distributed according to the terms
0010  * of the GNU General Public License, incorporated herein by reference.
0011  *
0012  *
0013  * Introduction:
0014  *
0015  * gcc produces a very nice and correct list of dependencies which
0016  * tells make when to remake a file.
0017  *
0018  * To use this list as-is however has the drawback that virtually
0019  * every file in the kernel includes autoconf.h.
0020  *
0021  * If the user re-runs make *config, autoconf.h will be
0022  * regenerated.  make notices that and will rebuild every file which
0023  * includes autoconf.h, i.e. basically all files. This is extremely
0024  * annoying if the user just changed CONFIG_HIS_DRIVER from n to m.
0025  *
0026  * So we play the same trick that "mkdep" played before. We replace
0027  * the dependency on autoconf.h by a dependency on every config
0028  * option which is mentioned in any of the listed prerequisites.
0029  *
0030  * kconfig populates a tree in include/config/ with an empty file
0031  * for each config symbol and when the configuration is updated
0032  * the files representing changed config options are touched
0033  * which then let make pick up the changes and the files that use
0034  * the config symbols are rebuilt.
0035  *
0036  * So if the user changes his CONFIG_HIS_DRIVER option, only the objects
0037  * which depend on "include/config/HIS_DRIVER" will be rebuilt,
0038  * so most likely only his driver ;-)
0039  *
0040  * The idea above dates, by the way, back to Michael E Chastain, AFAIK.
0041  *
0042  * So to get dependencies right, there are two issues:
0043  * o if any of the files the compiler read changed, we need to rebuild
0044  * o if the command line given to the compile the file changed, we
0045  *   better rebuild as well.
0046  *
0047  * The former is handled by using the -MD output, the later by saving
0048  * the command line used to compile the old object and comparing it
0049  * to the one we would now use.
0050  *
0051  * Again, also this idea is pretty old and has been discussed on
0052  * kbuild-devel a long time ago. I don't have a sensibly working
0053  * internet connection right now, so I rather don't mention names
0054  * without double checking.
0055  *
0056  * This code here has been based partially based on mkdep.c, which
0057  * says the following about its history:
0058  *
0059  *   Copyright abandoned, Michael Chastain, <mailto:mec@shout.net>.
0060  *   This is a C version of syncdep.pl by Werner Almesberger.
0061  *
0062  *
0063  * It is invoked as
0064  *
0065  *   fixdep <depfile> <target> <cmdline>
0066  *
0067  * and will read the dependency file <depfile>
0068  *
0069  * The transformed dependency snipped is written to stdout.
0070  *
0071  * It first generates a line
0072  *
0073  *   cmd_<target> = <cmdline>
0074  *
0075  * and then basically copies the .<target>.d file to stdout, in the
0076  * process filtering out the dependency on autoconf.h and adding
0077  * dependencies on include/config/MY_OPTION for every
0078  * CONFIG_MY_OPTION encountered in any of the prerequisites.
0079  *
0080  * We don't even try to really parse the header files, but
0081  * merely grep, i.e. if CONFIG_FOO is mentioned in a comment, it will
0082  * be picked up as well. It's not a problem with respect to
0083  * correctness, since that can only give too many dependencies, thus
0084  * we cannot miss a rebuild. Since people tend to not mention totally
0085  * unrelated CONFIG_ options all over the place, it's not an
0086  * efficiency problem either.
0087  *
0088  * (Note: it'd be easy to port over the complete mkdep state machine,
0089  *  but I don't think the added complexity is worth it)
0090  */
0091 
0092 #include <sys/types.h>
0093 #include <sys/stat.h>
0094 #include <unistd.h>
0095 #include <fcntl.h>
0096 #include <string.h>
0097 #include <stdarg.h>
0098 #include <stdlib.h>
0099 #include <stdio.h>
0100 #include <ctype.h>
0101 
0102 static void usage(void)
0103 {
0104     fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
0105     exit(1);
0106 }
0107 
0108 struct item {
0109     struct item *next;
0110     unsigned int    len;
0111     unsigned int    hash;
0112     char        name[];
0113 };
0114 
0115 #define HASHSZ 256
0116 static struct item *hashtab[HASHSZ];
0117 
0118 static unsigned int strhash(const char *str, unsigned int sz)
0119 {
0120     /* fnv32 hash */
0121     unsigned int i, hash = 2166136261U;
0122 
0123     for (i = 0; i < sz; i++)
0124         hash = (hash ^ str[i]) * 0x01000193;
0125     return hash;
0126 }
0127 
0128 /*
0129  * Lookup a value in the configuration string.
0130  */
0131 static int is_defined_config(const char *name, int len, unsigned int hash)
0132 {
0133     struct item *aux;
0134 
0135     for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
0136         if (aux->hash == hash && aux->len == len &&
0137             memcmp(aux->name, name, len) == 0)
0138             return 1;
0139     }
0140     return 0;
0141 }
0142 
0143 /*
0144  * Add a new value to the configuration string.
0145  */
0146 static void define_config(const char *name, int len, unsigned int hash)
0147 {
0148     struct item *aux = malloc(sizeof(*aux) + len);
0149 
0150     if (!aux) {
0151         perror("fixdep:malloc");
0152         exit(1);
0153     }
0154     memcpy(aux->name, name, len);
0155     aux->len = len;
0156     aux->hash = hash;
0157     aux->next = hashtab[hash % HASHSZ];
0158     hashtab[hash % HASHSZ] = aux;
0159 }
0160 
0161 /*
0162  * Record the use of a CONFIG_* word.
0163  */
0164 static void use_config(const char *m, int slen)
0165 {
0166     unsigned int hash = strhash(m, slen);
0167 
0168     if (is_defined_config(m, slen, hash))
0169         return;
0170 
0171     define_config(m, slen, hash);
0172     /* Print out a dependency path from a symbol name. */
0173     printf("    $(wildcard include/config/%.*s) \\\n", slen, m);
0174 }
0175 
0176 /* test if s ends in sub */
0177 static int str_ends_with(const char *s, int slen, const char *sub)
0178 {
0179     int sublen = strlen(sub);
0180 
0181     if (sublen > slen)
0182         return 0;
0183 
0184     return !memcmp(s + slen - sublen, sub, sublen);
0185 }
0186 
0187 static void parse_config_file(const char *p)
0188 {
0189     const char *q, *r;
0190     const char *start = p;
0191 
0192     while ((p = strstr(p, "CONFIG_"))) {
0193         if (p > start && (isalnum(p[-1]) || p[-1] == '_')) {
0194             p += 7;
0195             continue;
0196         }
0197         p += 7;
0198         q = p;
0199         while (isalnum(*q) || *q == '_')
0200             q++;
0201         if (str_ends_with(p, q - p, "_MODULE"))
0202             r = q - 7;
0203         else
0204             r = q;
0205         if (r > p)
0206             use_config(p, r - p);
0207         p = q;
0208     }
0209 }
0210 
0211 static void *read_file(const char *filename)
0212 {
0213     struct stat st;
0214     int fd;
0215     char *buf;
0216 
0217     fd = open(filename, O_RDONLY);
0218     if (fd < 0) {
0219         fprintf(stderr, "fixdep: error opening file: ");
0220         perror(filename);
0221         exit(2);
0222     }
0223     if (fstat(fd, &st) < 0) {
0224         fprintf(stderr, "fixdep: error fstat'ing file: ");
0225         perror(filename);
0226         exit(2);
0227     }
0228     buf = malloc(st.st_size + 1);
0229     if (!buf) {
0230         perror("fixdep: malloc");
0231         exit(2);
0232     }
0233     if (read(fd, buf, st.st_size) != st.st_size) {
0234         perror("fixdep: read");
0235         exit(2);
0236     }
0237     buf[st.st_size] = '\0';
0238     close(fd);
0239 
0240     return buf;
0241 }
0242 
0243 /* Ignore certain dependencies */
0244 static int is_ignored_file(const char *s, int len)
0245 {
0246     return str_ends_with(s, len, "include/generated/autoconf.h") ||
0247            str_ends_with(s, len, "include/generated/autoksyms.h");
0248 }
0249 
0250 /*
0251  * Important: The below generated source_foo.o and deps_foo.o variable
0252  * assignments are parsed not only by make, but also by the rather simple
0253  * parser in scripts/mod/sumversion.c.
0254  */
0255 static void parse_dep_file(char *m, const char *target)
0256 {
0257     char *p;
0258     int is_last, is_target;
0259     int saw_any_target = 0;
0260     int is_first_dep = 0;
0261     void *buf;
0262 
0263     while (1) {
0264         /* Skip any "white space" */
0265         while (*m == ' ' || *m == '\\' || *m == '\n')
0266             m++;
0267 
0268         if (!*m)
0269             break;
0270 
0271         /* Find next "white space" */
0272         p = m;
0273         while (*p && *p != ' ' && *p != '\\' && *p != '\n')
0274             p++;
0275         is_last = (*p == '\0');
0276         /* Is the token we found a target name? */
0277         is_target = (*(p-1) == ':');
0278         /* Don't write any target names into the dependency file */
0279         if (is_target) {
0280             /* The /next/ file is the first dependency */
0281             is_first_dep = 1;
0282         } else if (!is_ignored_file(m, p - m)) {
0283             *p = '\0';
0284 
0285             /*
0286              * Do not list the source file as dependency, so that
0287              * kbuild is not confused if a .c file is rewritten
0288              * into .S or vice versa. Storing it in source_* is
0289              * needed for modpost to compute srcversions.
0290              */
0291             if (is_first_dep) {
0292                 /*
0293                  * If processing the concatenation of multiple
0294                  * dependency files, only process the first
0295                  * target name, which will be the original
0296                  * source name, and ignore any other target
0297                  * names, which will be intermediate temporary
0298                  * files.
0299                  */
0300                 if (!saw_any_target) {
0301                     saw_any_target = 1;
0302                     printf("source_%s := %s\n\n",
0303                            target, m);
0304                     printf("deps_%s := \\\n", target);
0305                 }
0306                 is_first_dep = 0;
0307             } else {
0308                 printf("  %s \\\n", m);
0309             }
0310 
0311             buf = read_file(m);
0312             parse_config_file(buf);
0313             free(buf);
0314         }
0315 
0316         if (is_last)
0317             break;
0318 
0319         /*
0320          * Start searching for next token immediately after the first
0321          * "whitespace" character that follows this token.
0322          */
0323         m = p + 1;
0324     }
0325 
0326     if (!saw_any_target) {
0327         fprintf(stderr, "fixdep: parse error; no targets found\n");
0328         exit(1);
0329     }
0330 
0331     printf("\n%s: $(deps_%s)\n\n", target, target);
0332     printf("$(deps_%s):\n", target);
0333 }
0334 
0335 int main(int argc, char *argv[])
0336 {
0337     const char *depfile, *target, *cmdline;
0338     void *buf;
0339 
0340     if (argc != 4)
0341         usage();
0342 
0343     depfile = argv[1];
0344     target = argv[2];
0345     cmdline = argv[3];
0346 
0347     printf("cmd_%s := %s\n\n", target, cmdline);
0348 
0349     buf = read_file(depfile);
0350     parse_dep_file(buf, target);
0351     free(buf);
0352 
0353     fflush(stdout);
0354 
0355     /*
0356      * In the intended usage, the stdout is redirected to .*.cmd files.
0357      * Call ferror() to catch errors such as "No space left on device".
0358      */
0359     if (ferror(stdout)) {
0360         fprintf(stderr, "fixdep: not all data was written to the output\n");
0361         exit(1);
0362     }
0363 
0364     return 0;
0365 }