11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * "Optimize" a list of dependencies as spit out by gcc -MD 31da177e4SLinus Torvalds * for the kernel build 41da177e4SLinus Torvalds * =========================================================================== 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Author Kai Germaschewski 71da177e4SLinus Torvalds * Copyright 2002 by Kai Germaschewski <[email protected]> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * This software may be used and distributed according to the terms 101da177e4SLinus Torvalds * of the GNU General Public License, incorporated herein by reference. 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * Introduction: 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * gcc produces a very nice and correct list of dependencies which 161da177e4SLinus Torvalds * tells make when to remake a file. 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * To use this list as-is however has the drawback that virtually 19264a2683SSam Ravnborg * every file in the kernel includes autoconf.h. 201da177e4SLinus Torvalds * 21264a2683SSam Ravnborg * If the user re-runs make *config, autoconf.h will be 221da177e4SLinus Torvalds * regenerated. make notices that and will rebuild every file which 231da177e4SLinus Torvalds * includes autoconf.h, i.e. basically all files. This is extremely 241da177e4SLinus Torvalds * annoying if the user just changed CONFIG_HIS_DRIVER from n to m. 251da177e4SLinus Torvalds * 261da177e4SLinus Torvalds * So we play the same trick that "mkdep" played before. We replace 27264a2683SSam Ravnborg * the dependency on autoconf.h by a dependency on every config 284e433fc4SCao jin * option which is mentioned in any of the listed prerequisites. 291da177e4SLinus Torvalds * 30c21b1e4dSJan Beulich * kconfig populates a tree in include/config/ with an empty file 31c21b1e4dSJan Beulich * for each config symbol and when the configuration is updated 32c21b1e4dSJan Beulich * the files representing changed config options are touched 33c21b1e4dSJan Beulich * which then let make pick up the changes and the files that use 34c21b1e4dSJan Beulich * the config symbols are rebuilt. 351da177e4SLinus Torvalds * 361da177e4SLinus Torvalds * So if the user changes his CONFIG_HIS_DRIVER option, only the objects 374e433fc4SCao jin * which depend on "include/config/his/driver.h" will be rebuilt, 381da177e4SLinus Torvalds * so most likely only his driver ;-) 391da177e4SLinus Torvalds * 401da177e4SLinus Torvalds * The idea above dates, by the way, back to Michael E Chastain, AFAIK. 411da177e4SLinus Torvalds * 421da177e4SLinus Torvalds * So to get dependencies right, there are two issues: 431da177e4SLinus Torvalds * o if any of the files the compiler read changed, we need to rebuild 441da177e4SLinus Torvalds * o if the command line given to the compile the file changed, we 451da177e4SLinus Torvalds * better rebuild as well. 461da177e4SLinus Torvalds * 471da177e4SLinus Torvalds * The former is handled by using the -MD output, the later by saving 481da177e4SLinus Torvalds * the command line used to compile the old object and comparing it 491da177e4SLinus Torvalds * to the one we would now use. 501da177e4SLinus Torvalds * 511da177e4SLinus Torvalds * Again, also this idea is pretty old and has been discussed on 521da177e4SLinus Torvalds * kbuild-devel a long time ago. I don't have a sensibly working 531da177e4SLinus Torvalds * internet connection right now, so I rather don't mention names 541da177e4SLinus Torvalds * without double checking. 551da177e4SLinus Torvalds * 561da177e4SLinus Torvalds * This code here has been based partially based on mkdep.c, which 571da177e4SLinus Torvalds * says the following about its history: 581da177e4SLinus Torvalds * 591da177e4SLinus Torvalds * Copyright abandoned, Michael Chastain, <mailto:[email protected]>. 601da177e4SLinus Torvalds * This is a C version of syncdep.pl by Werner Almesberger. 611da177e4SLinus Torvalds * 621da177e4SLinus Torvalds * 631da177e4SLinus Torvalds * It is invoked as 641da177e4SLinus Torvalds * 651da177e4SLinus Torvalds * fixdep <depfile> <target> <cmdline> 661da177e4SLinus Torvalds * 671da177e4SLinus Torvalds * and will read the dependency file <depfile> 681da177e4SLinus Torvalds * 691da177e4SLinus Torvalds * The transformed dependency snipped is written to stdout. 701da177e4SLinus Torvalds * 711da177e4SLinus Torvalds * It first generates a line 721da177e4SLinus Torvalds * 731da177e4SLinus Torvalds * cmd_<target> = <cmdline> 741da177e4SLinus Torvalds * 751da177e4SLinus Torvalds * and then basically copies the .<target>.d file to stdout, in the 76264a2683SSam Ravnborg * process filtering out the dependency on autoconf.h and adding 771da177e4SLinus Torvalds * dependencies on include/config/my/option.h for every 784e433fc4SCao jin * CONFIG_MY_OPTION encountered in any of the prerequisites. 791da177e4SLinus Torvalds * 801da177e4SLinus Torvalds * It will also filter out all the dependencies on *.ver. We need 811da177e4SLinus Torvalds * to make sure that the generated version checksum are globally up 821da177e4SLinus Torvalds * to date before even starting the recursive build, so it's too late 831da177e4SLinus Torvalds * at this point anyway. 841da177e4SLinus Torvalds * 85dee81e98SAlexey Dobriyan * We don't even try to really parse the header files, but 861da177e4SLinus Torvalds * merely grep, i.e. if CONFIG_FOO is mentioned in a comment, it will 871da177e4SLinus Torvalds * be picked up as well. It's not a problem with respect to 881da177e4SLinus Torvalds * correctness, since that can only give too many dependencies, thus 891da177e4SLinus Torvalds * we cannot miss a rebuild. Since people tend to not mention totally 901da177e4SLinus Torvalds * unrelated CONFIG_ options all over the place, it's not an 911da177e4SLinus Torvalds * efficiency problem either. 921da177e4SLinus Torvalds * 931da177e4SLinus Torvalds * (Note: it'd be easy to port over the complete mkdep state machine, 941da177e4SLinus Torvalds * but I don't think the added complexity is worth it) 951da177e4SLinus Torvalds */ 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds #include <sys/types.h> 981da177e4SLinus Torvalds #include <sys/stat.h> 991da177e4SLinus Torvalds #include <unistd.h> 1001da177e4SLinus Torvalds #include <fcntl.h> 1011da177e4SLinus Torvalds #include <string.h> 1021da177e4SLinus Torvalds #include <stdlib.h> 1031da177e4SLinus Torvalds #include <stdio.h> 1041da177e4SLinus Torvalds #include <ctype.h> 1051da177e4SLinus Torvalds 1064356f489STrevor Keith static void usage(void) 1071da177e4SLinus Torvalds { 108d8329e35SNicolas Pitre fprintf(stderr, "Usage: fixdep [-e] <depfile> <target> <cmdline>\n"); 109d8329e35SNicolas Pitre fprintf(stderr, " -e insert extra dependencies given on stdin\n"); 1101da177e4SLinus Torvalds exit(1); 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 1134d99f93bSSam Ravnborg /* 114d8329e35SNicolas Pitre * Print out a dependency path from a symbol name 115d8329e35SNicolas Pitre */ 116*fbfa9be9SMasahiro Yamada static void print_dep(const char *m, int slen, const char *dir) 117d8329e35SNicolas Pitre { 118d8329e35SNicolas Pitre int c, i; 119d8329e35SNicolas Pitre 120*fbfa9be9SMasahiro Yamada printf(" $(wildcard %s/", dir); 121d8329e35SNicolas Pitre for (i = 0; i < slen; i++) { 122d8329e35SNicolas Pitre c = m[i]; 123d8329e35SNicolas Pitre if (c == '_') 124d8329e35SNicolas Pitre c = '/'; 125d8329e35SNicolas Pitre else 126d8329e35SNicolas Pitre c = tolower(c); 127d8329e35SNicolas Pitre putchar(c); 128d8329e35SNicolas Pitre } 129d8329e35SNicolas Pitre printf(".h) \\\n"); 130d8329e35SNicolas Pitre } 131d8329e35SNicolas Pitre 132d8329e35SNicolas Pitre static void do_extra_deps(void) 133d8329e35SNicolas Pitre { 134d8329e35SNicolas Pitre char buf[80]; 1355d1ef76fSMasahiro Yamada 136d8329e35SNicolas Pitre while (fgets(buf, sizeof(buf), stdin)) { 137d8329e35SNicolas Pitre int len = strlen(buf); 1385d1ef76fSMasahiro Yamada 139d8329e35SNicolas Pitre if (len < 2 || buf[len - 1] != '\n') { 140d8329e35SNicolas Pitre fprintf(stderr, "fixdep: bad data on stdin\n"); 141d8329e35SNicolas Pitre exit(1); 142d8329e35SNicolas Pitre } 143*fbfa9be9SMasahiro Yamada print_dep(buf, len - 1, "include/ksym"); 144d8329e35SNicolas Pitre } 145d8329e35SNicolas Pitre } 146d8329e35SNicolas Pitre 1478af27e1dSEric Dumazet struct item { 1488af27e1dSEric Dumazet struct item *next; 1498af27e1dSEric Dumazet unsigned int len; 1508af27e1dSEric Dumazet unsigned int hash; 1518af27e1dSEric Dumazet char name[0]; 1528af27e1dSEric Dumazet }; 1531da177e4SLinus Torvalds 1548af27e1dSEric Dumazet #define HASHSZ 256 1558af27e1dSEric Dumazet static struct item *hashtab[HASHSZ]; 1568af27e1dSEric Dumazet 1578af27e1dSEric Dumazet static unsigned int strhash(const char *str, unsigned int sz) 1581da177e4SLinus Torvalds { 1598af27e1dSEric Dumazet /* fnv32 hash */ 1608af27e1dSEric Dumazet unsigned int i, hash = 2166136261U; 1611da177e4SLinus Torvalds 1628af27e1dSEric Dumazet for (i = 0; i < sz; i++) 1638af27e1dSEric Dumazet hash = (hash ^ str[i]) * 0x01000193; 1648af27e1dSEric Dumazet return hash; 1658af27e1dSEric Dumazet } 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds /* 1681da177e4SLinus Torvalds * Lookup a value in the configuration string. 1691da177e4SLinus Torvalds */ 1708af27e1dSEric Dumazet static int is_defined_config(const char *name, int len, unsigned int hash) 1711da177e4SLinus Torvalds { 1728af27e1dSEric Dumazet struct item *aux; 1738af27e1dSEric Dumazet 1748af27e1dSEric Dumazet for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) { 1758af27e1dSEric Dumazet if (aux->hash == hash && aux->len == len && 1768af27e1dSEric Dumazet memcmp(aux->name, name, len) == 0) 1771da177e4SLinus Torvalds return 1; 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds return 0; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds /* 1831da177e4SLinus Torvalds * Add a new value to the configuration string. 1841da177e4SLinus Torvalds */ 1858af27e1dSEric Dumazet static void define_config(const char *name, int len, unsigned int hash) 1861da177e4SLinus Torvalds { 1878af27e1dSEric Dumazet struct item *aux = malloc(sizeof(*aux) + len); 1881da177e4SLinus Torvalds 1898af27e1dSEric Dumazet if (!aux) { 1908af27e1dSEric Dumazet perror("fixdep:malloc"); 1918af27e1dSEric Dumazet exit(1); 1928af27e1dSEric Dumazet } 1938af27e1dSEric Dumazet memcpy(aux->name, name, len); 1948af27e1dSEric Dumazet aux->len = len; 1958af27e1dSEric Dumazet aux->hash = hash; 1968af27e1dSEric Dumazet aux->next = hashtab[hash % HASHSZ]; 1978af27e1dSEric Dumazet hashtab[hash % HASHSZ] = aux; 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds /* 2011da177e4SLinus Torvalds * Record the use of a CONFIG_* word. 2021da177e4SLinus Torvalds */ 2038af27e1dSEric Dumazet static void use_config(const char *m, int slen) 2041da177e4SLinus Torvalds { 2058af27e1dSEric Dumazet unsigned int hash = strhash(m, slen); 2061da177e4SLinus Torvalds 2078af27e1dSEric Dumazet if (is_defined_config(m, slen, hash)) 2081da177e4SLinus Torvalds return; 2091da177e4SLinus Torvalds 2108af27e1dSEric Dumazet define_config(m, slen, hash); 211*fbfa9be9SMasahiro Yamada print_dep(m, slen, "include/config"); 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 214d8329e35SNicolas Pitre /* test if s ends in sub */ 21587b95a81SMasahiro Yamada static int str_ends_with(const char *s, int slen, const char *sub) 2161da177e4SLinus Torvalds { 2171da177e4SLinus Torvalds int sublen = strlen(sub); 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds if (sublen > slen) 22087b95a81SMasahiro Yamada return 0; 2211da177e4SLinus Torvalds 22287b95a81SMasahiro Yamada return !memcmp(s + slen - sublen, sub, sublen); 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds 225ab9ce9feSMasahiro Yamada static void parse_config_file(const char *p) 226ab9ce9feSMasahiro Yamada { 227ab9ce9feSMasahiro Yamada const char *q, *r; 2285b8ad96dSRasmus Villemoes const char *start = p; 229ab9ce9feSMasahiro Yamada 230ab9ce9feSMasahiro Yamada while ((p = strstr(p, "CONFIG_"))) { 2315b8ad96dSRasmus Villemoes if (p > start && (isalnum(p[-1]) || p[-1] == '_')) { 2325b8ad96dSRasmus Villemoes p += 7; 2335b8ad96dSRasmus Villemoes continue; 2345b8ad96dSRasmus Villemoes } 235ab9ce9feSMasahiro Yamada p += 7; 236ab9ce9feSMasahiro Yamada q = p; 237ab9ce9feSMasahiro Yamada while (*q && (isalnum(*q) || *q == '_')) 238ab9ce9feSMasahiro Yamada q++; 239ab9ce9feSMasahiro Yamada if (str_ends_with(p, q - p, "_MODULE")) 240ab9ce9feSMasahiro Yamada r = q - 7; 241ab9ce9feSMasahiro Yamada else 242ab9ce9feSMasahiro Yamada r = q; 243ab9ce9feSMasahiro Yamada if (r > p) 244ab9ce9feSMasahiro Yamada use_config(p, r - p); 245ab9ce9feSMasahiro Yamada p = q; 246ab9ce9feSMasahiro Yamada } 247ab9ce9feSMasahiro Yamada } 248ab9ce9feSMasahiro Yamada 2494003fd80SMasahiro Yamada static void *read_file(const char *filename) 2501da177e4SLinus Torvalds { 2511da177e4SLinus Torvalds struct stat st; 2521da177e4SLinus Torvalds int fd; 2534003fd80SMasahiro Yamada char *buf; 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds fd = open(filename, O_RDONLY); 2561da177e4SLinus Torvalds if (fd < 0) { 2574003fd80SMasahiro Yamada fprintf(stderr, "fixdep: error opening file: "); 2581da177e4SLinus Torvalds perror(filename); 2591da177e4SLinus Torvalds exit(2); 2601da177e4SLinus Torvalds } 26146fe94adSTom Rini if (fstat(fd, &st) < 0) { 2624003fd80SMasahiro Yamada fprintf(stderr, "fixdep: error fstat'ing file: "); 26346fe94adSTom Rini perror(filename); 26446fe94adSTom Rini exit(2); 26546fe94adSTom Rini } 2664003fd80SMasahiro Yamada buf = malloc(st.st_size + 1); 2674003fd80SMasahiro Yamada if (!buf) { 268dee81e98SAlexey Dobriyan perror("fixdep: malloc"); 2697c2ec43aSLukas Bulwahn exit(2); 2701da177e4SLinus Torvalds } 2714003fd80SMasahiro Yamada if (read(fd, buf, st.st_size) != st.st_size) { 272dee81e98SAlexey Dobriyan perror("fixdep: read"); 2737c2ec43aSLukas Bulwahn exit(2); 274dee81e98SAlexey Dobriyan } 2754003fd80SMasahiro Yamada buf[st.st_size] = '\0'; 276dee81e98SAlexey Dobriyan close(fd); 277dee81e98SAlexey Dobriyan 2784003fd80SMasahiro Yamada return buf; 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds 28187b95a81SMasahiro Yamada /* Ignore certain dependencies */ 28287b95a81SMasahiro Yamada static int is_ignored_file(const char *s, int len) 28387b95a81SMasahiro Yamada { 28487b95a81SMasahiro Yamada return str_ends_with(s, len, "include/generated/autoconf.h") || 28587b95a81SMasahiro Yamada str_ends_with(s, len, "include/generated/autoksyms.h") || 28687b95a81SMasahiro Yamada str_ends_with(s, len, ".ver"); 28787b95a81SMasahiro Yamada } 28887b95a81SMasahiro Yamada 2897840fea2SMichal Marek /* 2907840fea2SMichal Marek * Important: The below generated source_foo.o and deps_foo.o variable 2917840fea2SMichal Marek * assignments are parsed not only by make, but also by the rather simple 2927840fea2SMichal Marek * parser in scripts/mod/sumversion.c. 2937840fea2SMichal Marek */ 2945d1ef76fSMasahiro Yamada static void parse_dep_file(char *m, const char *target, int insert_extra_deps) 2951da177e4SLinus Torvalds { 29648b9d03cSJ.A. Magallon char *p; 29701b5cbe7SMasahiro Yamada int is_last, is_target; 2982ab8a996SStephen Warren int saw_any_target = 0; 2992ab8a996SStephen Warren int is_first_dep = 0; 3004003fd80SMasahiro Yamada void *buf; 3011da177e4SLinus Torvalds 30201b5cbe7SMasahiro Yamada while (1) { 3032ab8a996SStephen Warren /* Skip any "white space" */ 30401b5cbe7SMasahiro Yamada while (*m == ' ' || *m == '\\' || *m == '\n') 3051da177e4SLinus Torvalds m++; 30601b5cbe7SMasahiro Yamada 30701b5cbe7SMasahiro Yamada if (!*m) 30801b5cbe7SMasahiro Yamada break; 30901b5cbe7SMasahiro Yamada 3102ab8a996SStephen Warren /* Find next "white space" */ 3111da177e4SLinus Torvalds p = m; 31201b5cbe7SMasahiro Yamada while (*p && *p != ' ' && *p != '\\' && *p != '\n') 3131da177e4SLinus Torvalds p++; 31401b5cbe7SMasahiro Yamada is_last = (*p == '\0'); 3152ab8a996SStephen Warren /* Is the token we found a target name? */ 3162ab8a996SStephen Warren is_target = (*(p-1) == ':'); 3172ab8a996SStephen Warren /* Don't write any target names into the dependency file */ 3182ab8a996SStephen Warren if (is_target) { 3192ab8a996SStephen Warren /* The /next/ file is the first dependency */ 3202ab8a996SStephen Warren is_first_dep = 1; 32187b95a81SMasahiro Yamada } else if (!is_ignored_file(m, p - m)) { 322ccfe7887SMasahiro Yamada *p = '\0'; 3232ab8a996SStephen Warren 324b7bd1821SMichal Marek /* 32587b95a81SMasahiro Yamada * Do not list the source file as dependency, so that 32687b95a81SMasahiro Yamada * kbuild is not confused if a .c file is rewritten 32787b95a81SMasahiro Yamada * into .S or vice versa. Storing it in source_* is 32887b95a81SMasahiro Yamada * needed for modpost to compute srcversions. 329b7bd1821SMichal Marek */ 3302ab8a996SStephen Warren if (is_first_dep) { 3312ab8a996SStephen Warren /* 33287b95a81SMasahiro Yamada * If processing the concatenation of multiple 33387b95a81SMasahiro Yamada * dependency files, only process the first 33487b95a81SMasahiro Yamada * target name, which will be the original 33587b95a81SMasahiro Yamada * source name, and ignore any other target 33687b95a81SMasahiro Yamada * names, which will be intermediate temporary 3372ab8a996SStephen Warren * files. 3382ab8a996SStephen Warren */ 3392ab8a996SStephen Warren if (!saw_any_target) { 3402ab8a996SStephen Warren saw_any_target = 1; 3412ab8a996SStephen Warren printf("source_%s := %s\n\n", 342ccfe7887SMasahiro Yamada target, m); 34387b95a81SMasahiro Yamada printf("deps_%s := \\\n", target); 3442ab8a996SStephen Warren } 3452ab8a996SStephen Warren is_first_dep = 0; 34687b95a81SMasahiro Yamada } else { 347ccfe7887SMasahiro Yamada printf(" %s \\\n", m); 34887b95a81SMasahiro Yamada } 3494003fd80SMasahiro Yamada 350ccfe7887SMasahiro Yamada buf = read_file(m); 3514003fd80SMasahiro Yamada parse_config_file(buf); 3524003fd80SMasahiro Yamada free(buf); 3531da177e4SLinus Torvalds } 35401b5cbe7SMasahiro Yamada 35501b5cbe7SMasahiro Yamada if (is_last) 35601b5cbe7SMasahiro Yamada break; 35701b5cbe7SMasahiro Yamada 3582ab8a996SStephen Warren /* 3592ab8a996SStephen Warren * Start searching for next token immediately after the first 3602ab8a996SStephen Warren * "whitespace" character that follows this token. 3612ab8a996SStephen Warren */ 3621da177e4SLinus Torvalds m = p + 1; 3631da177e4SLinus Torvalds } 3642ab8a996SStephen Warren 3652ab8a996SStephen Warren if (!saw_any_target) { 3662ab8a996SStephen Warren fprintf(stderr, "fixdep: parse error; no targets found\n"); 3672ab8a996SStephen Warren exit(1); 3682ab8a996SStephen Warren } 3692ab8a996SStephen Warren 3705d1ef76fSMasahiro Yamada if (insert_extra_deps) 371d8329e35SNicolas Pitre do_extra_deps(); 372d8329e35SNicolas Pitre 3731da177e4SLinus Torvalds printf("\n%s: $(deps_%s)\n\n", target, target); 3741da177e4SLinus Torvalds printf("$(deps_%s):\n", target); 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds int main(int argc, char *argv[]) 3781da177e4SLinus Torvalds { 3795d1ef76fSMasahiro Yamada const char *depfile, *target, *cmdline; 3805d1ef76fSMasahiro Yamada int insert_extra_deps = 0; 3814003fd80SMasahiro Yamada void *buf; 3824003fd80SMasahiro Yamada 383d8329e35SNicolas Pitre if (argc == 5 && !strcmp(argv[1], "-e")) { 384d8329e35SNicolas Pitre insert_extra_deps = 1; 385d8329e35SNicolas Pitre argv++; 386d8329e35SNicolas Pitre } else if (argc != 4) 3871da177e4SLinus Torvalds usage(); 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds depfile = argv[1]; 3901da177e4SLinus Torvalds target = argv[2]; 3911da177e4SLinus Torvalds cmdline = argv[3]; 3921da177e4SLinus Torvalds 3935d1ef76fSMasahiro Yamada printf("cmd_%s := %s\n\n", target, cmdline); 3944003fd80SMasahiro Yamada 3954003fd80SMasahiro Yamada buf = read_file(depfile); 3965d1ef76fSMasahiro Yamada parse_dep_file(buf, target, insert_extra_deps); 3974003fd80SMasahiro Yamada free(buf); 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds return 0; 4001da177e4SLinus Torvalds } 401