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 370e0345b7SAlexey Dobriyan * which depend on "include/config/HIS_DRIVER" 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 * 7392215e7aSMasahiro Yamada * savedcmd_<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 770e0345b7SAlexey Dobriyan * dependencies on include/config/MY_OPTION for every 784e433fc4SCao jin * CONFIG_MY_OPTION encountered in any of the prerequisites. 791da177e4SLinus Torvalds * 80dee81e98SAlexey Dobriyan * We don't even try to really parse the header files, but 811da177e4SLinus Torvalds * merely grep, i.e. if CONFIG_FOO is mentioned in a comment, it will 821da177e4SLinus Torvalds * be picked up as well. It's not a problem with respect to 831da177e4SLinus Torvalds * correctness, since that can only give too many dependencies, thus 841da177e4SLinus Torvalds * we cannot miss a rebuild. Since people tend to not mention totally 851da177e4SLinus Torvalds * unrelated CONFIG_ options all over the place, it's not an 861da177e4SLinus Torvalds * efficiency problem either. 871da177e4SLinus Torvalds * 881da177e4SLinus Torvalds * (Note: it'd be easy to port over the complete mkdep state machine, 891da177e4SLinus Torvalds * but I don't think the added complexity is worth it) 901da177e4SLinus Torvalds */ 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds #include <sys/types.h> 931da177e4SLinus Torvalds #include <sys/stat.h> 941da177e4SLinus Torvalds #include <unistd.h> 951da177e4SLinus Torvalds #include <fcntl.h> 961da177e4SLinus Torvalds #include <string.h> 97bc6df812SMasahiro Yamada #include <stdbool.h> 981da177e4SLinus Torvalds #include <stdlib.h> 991da177e4SLinus Torvalds #include <stdio.h> 1001da177e4SLinus Torvalds #include <ctype.h> 1011da177e4SLinus Torvalds 1024356f489STrevor Keith static void usage(void) 1031da177e4SLinus Torvalds { 104bbda5ec6SMasahiro Yamada fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n"); 1051da177e4SLinus Torvalds exit(1); 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds 1088af27e1dSEric Dumazet struct item { 1098af27e1dSEric Dumazet struct item *next; 1108af27e1dSEric Dumazet unsigned int len; 1118af27e1dSEric Dumazet unsigned int hash; 112859c8175SGustavo A. R. Silva char name[]; 1138af27e1dSEric Dumazet }; 1141da177e4SLinus Torvalds 1158af27e1dSEric Dumazet #define HASHSZ 256 116*faa91c47SMasahiro Yamada static struct item *config_hashtab[HASHSZ], *file_hashtab[HASHSZ]; 1178af27e1dSEric Dumazet 1188af27e1dSEric Dumazet static unsigned int strhash(const char *str, unsigned int sz) 1191da177e4SLinus Torvalds { 1208af27e1dSEric Dumazet /* fnv32 hash */ 1218af27e1dSEric Dumazet unsigned int i, hash = 2166136261U; 1221da177e4SLinus Torvalds 1238af27e1dSEric Dumazet for (i = 0; i < sz; i++) 1248af27e1dSEric Dumazet hash = (hash ^ str[i]) * 0x01000193; 1258af27e1dSEric Dumazet return hash; 1268af27e1dSEric Dumazet } 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds /* 1291da177e4SLinus Torvalds * Add a new value to the configuration string. 1301da177e4SLinus Torvalds */ 131871d6573SMasahiro Yamada static void add_to_hashtable(const char *name, int len, unsigned int hash, 132871d6573SMasahiro Yamada struct item *hashtab[]) 1331da177e4SLinus Torvalds { 1348af27e1dSEric Dumazet struct item *aux = malloc(sizeof(*aux) + len); 1351da177e4SLinus Torvalds 1368af27e1dSEric Dumazet if (!aux) { 1378af27e1dSEric Dumazet perror("fixdep:malloc"); 1388af27e1dSEric Dumazet exit(1); 1398af27e1dSEric Dumazet } 1408af27e1dSEric Dumazet memcpy(aux->name, name, len); 1418af27e1dSEric Dumazet aux->len = len; 1428af27e1dSEric Dumazet aux->hash = hash; 1438af27e1dSEric Dumazet aux->next = hashtab[hash % HASHSZ]; 1448af27e1dSEric Dumazet hashtab[hash % HASHSZ] = aux; 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds /* 148871d6573SMasahiro Yamada * Lookup a string in the hash table. If found, just return true. 149871d6573SMasahiro Yamada * If not, add it to the hashtable and return false. 150871d6573SMasahiro Yamada */ 151871d6573SMasahiro Yamada static bool in_hashtable(const char *name, int len, struct item *hashtab[]) 152871d6573SMasahiro Yamada { 153871d6573SMasahiro Yamada struct item *aux; 154871d6573SMasahiro Yamada unsigned int hash = strhash(name, len); 155871d6573SMasahiro Yamada 156871d6573SMasahiro Yamada for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) { 157871d6573SMasahiro Yamada if (aux->hash == hash && aux->len == len && 158871d6573SMasahiro Yamada memcmp(aux->name, name, len) == 0) 159871d6573SMasahiro Yamada return true; 160871d6573SMasahiro Yamada } 161871d6573SMasahiro Yamada 162871d6573SMasahiro Yamada add_to_hashtable(name, len, hash, hashtab); 163871d6573SMasahiro Yamada 164871d6573SMasahiro Yamada return false; 165871d6573SMasahiro Yamada } 166871d6573SMasahiro Yamada 167871d6573SMasahiro Yamada /* 1681da177e4SLinus Torvalds * Record the use of a CONFIG_* word. 1691da177e4SLinus Torvalds */ 1708af27e1dSEric Dumazet static void use_config(const char *m, int slen) 1711da177e4SLinus Torvalds { 172871d6573SMasahiro Yamada if (in_hashtable(m, slen, config_hashtab)) 1731da177e4SLinus Torvalds return; 1741da177e4SLinus Torvalds 1750e0345b7SAlexey Dobriyan /* Print out a dependency path from a symbol name. */ 17669304379SMasahiro Yamada printf(" $(wildcard include/config/%.*s) \\\n", slen, m); 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds 179d8329e35SNicolas Pitre /* test if s ends in sub */ 18087b95a81SMasahiro Yamada static int str_ends_with(const char *s, int slen, const char *sub) 1811da177e4SLinus Torvalds { 1821da177e4SLinus Torvalds int sublen = strlen(sub); 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds if (sublen > slen) 18587b95a81SMasahiro Yamada return 0; 1861da177e4SLinus Torvalds 18787b95a81SMasahiro Yamada return !memcmp(s + slen - sublen, sub, sublen); 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds 190ab9ce9feSMasahiro Yamada static void parse_config_file(const char *p) 191ab9ce9feSMasahiro Yamada { 192ab9ce9feSMasahiro Yamada const char *q, *r; 1935b8ad96dSRasmus Villemoes const char *start = p; 194ab9ce9feSMasahiro Yamada 195ab9ce9feSMasahiro Yamada while ((p = strstr(p, "CONFIG_"))) { 1965b8ad96dSRasmus Villemoes if (p > start && (isalnum(p[-1]) || p[-1] == '_')) { 1975b8ad96dSRasmus Villemoes p += 7; 1985b8ad96dSRasmus Villemoes continue; 1995b8ad96dSRasmus Villemoes } 200ab9ce9feSMasahiro Yamada p += 7; 201ab9ce9feSMasahiro Yamada q = p; 2023f9070a6SMasahiro Yamada while (isalnum(*q) || *q == '_') 203ab9ce9feSMasahiro Yamada q++; 204ab9ce9feSMasahiro Yamada if (str_ends_with(p, q - p, "_MODULE")) 205ab9ce9feSMasahiro Yamada r = q - 7; 206ab9ce9feSMasahiro Yamada else 207ab9ce9feSMasahiro Yamada r = q; 208ab9ce9feSMasahiro Yamada if (r > p) 209ab9ce9feSMasahiro Yamada use_config(p, r - p); 210ab9ce9feSMasahiro Yamada p = q; 211ab9ce9feSMasahiro Yamada } 212ab9ce9feSMasahiro Yamada } 213ab9ce9feSMasahiro Yamada 2144003fd80SMasahiro Yamada static void *read_file(const char *filename) 2151da177e4SLinus Torvalds { 2161da177e4SLinus Torvalds struct stat st; 2171da177e4SLinus Torvalds int fd; 2184003fd80SMasahiro Yamada char *buf; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds fd = open(filename, O_RDONLY); 2211da177e4SLinus Torvalds if (fd < 0) { 2224003fd80SMasahiro Yamada fprintf(stderr, "fixdep: error opening file: "); 2231da177e4SLinus Torvalds perror(filename); 2241da177e4SLinus Torvalds exit(2); 2251da177e4SLinus Torvalds } 22646fe94adSTom Rini if (fstat(fd, &st) < 0) { 2274003fd80SMasahiro Yamada fprintf(stderr, "fixdep: error fstat'ing file: "); 22846fe94adSTom Rini perror(filename); 22946fe94adSTom Rini exit(2); 23046fe94adSTom Rini } 2314003fd80SMasahiro Yamada buf = malloc(st.st_size + 1); 2324003fd80SMasahiro Yamada if (!buf) { 233dee81e98SAlexey Dobriyan perror("fixdep: malloc"); 2347c2ec43aSLukas Bulwahn exit(2); 2351da177e4SLinus Torvalds } 2364003fd80SMasahiro Yamada if (read(fd, buf, st.st_size) != st.st_size) { 237dee81e98SAlexey Dobriyan perror("fixdep: read"); 2387c2ec43aSLukas Bulwahn exit(2); 239dee81e98SAlexey Dobriyan } 2404003fd80SMasahiro Yamada buf[st.st_size] = '\0'; 241dee81e98SAlexey Dobriyan close(fd); 242dee81e98SAlexey Dobriyan 2434003fd80SMasahiro Yamada return buf; 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 24687b95a81SMasahiro Yamada /* Ignore certain dependencies */ 24787b95a81SMasahiro Yamada static int is_ignored_file(const char *s, int len) 24887b95a81SMasahiro Yamada { 24987b95a81SMasahiro Yamada return str_ends_with(s, len, "include/generated/autoconf.h") || 25087d660f0SMasahiro Yamada str_ends_with(s, len, "include/generated/autoksyms.h"); 25187b95a81SMasahiro Yamada } 25287b95a81SMasahiro Yamada 2537840fea2SMichal Marek /* 2547840fea2SMichal Marek * Important: The below generated source_foo.o and deps_foo.o variable 2557840fea2SMichal Marek * assignments are parsed not only by make, but also by the rather simple 2567840fea2SMichal Marek * parser in scripts/mod/sumversion.c. 2577840fea2SMichal Marek */ 258bc6df812SMasahiro Yamada static void parse_dep_file(char *p, const char *target) 2591da177e4SLinus Torvalds { 260bc6df812SMasahiro Yamada bool saw_any_target = false; 261bc6df812SMasahiro Yamada bool is_target = true; 262bc6df812SMasahiro Yamada bool is_source = false; 263bc6df812SMasahiro Yamada bool need_parse; 264bc6df812SMasahiro Yamada char *q, saved_c; 2651da177e4SLinus Torvalds 266bc6df812SMasahiro Yamada while (*p) { 267bc6df812SMasahiro Yamada /* handle some special characters first. */ 268bc6df812SMasahiro Yamada switch (*p) { 269bc6df812SMasahiro Yamada case '#': 270bc6df812SMasahiro Yamada /* 271bc6df812SMasahiro Yamada * skip comments. 272bc6df812SMasahiro Yamada * rustc may emit comments to dep-info. 273bc6df812SMasahiro Yamada */ 274bc6df812SMasahiro Yamada p++; 275bc6df812SMasahiro Yamada while (*p != '\0' && *p != '\n') { 276bc6df812SMasahiro Yamada /* 277bc6df812SMasahiro Yamada * escaped newlines continue the comment across 278bc6df812SMasahiro Yamada * multiple lines. 279bc6df812SMasahiro Yamada */ 280bc6df812SMasahiro Yamada if (*p == '\\') 281bc6df812SMasahiro Yamada p++; 282bc6df812SMasahiro Yamada p++; 283bc6df812SMasahiro Yamada } 284bc6df812SMasahiro Yamada continue; 285bc6df812SMasahiro Yamada case ' ': 286bc6df812SMasahiro Yamada case '\t': 287bc6df812SMasahiro Yamada /* skip whitespaces */ 288bc6df812SMasahiro Yamada p++; 289bc6df812SMasahiro Yamada continue; 290bc6df812SMasahiro Yamada case '\\': 291bc6df812SMasahiro Yamada /* 292bc6df812SMasahiro Yamada * backslash/newline combinations continue the 293bc6df812SMasahiro Yamada * statement. Skip it just like a whitespace. 294bc6df812SMasahiro Yamada */ 295bc6df812SMasahiro Yamada if (*(p + 1) == '\n') { 296bc6df812SMasahiro Yamada p += 2; 297bc6df812SMasahiro Yamada continue; 298bc6df812SMasahiro Yamada } 299bc6df812SMasahiro Yamada break; 300bc6df812SMasahiro Yamada case '\n': 301bc6df812SMasahiro Yamada /* 302bc6df812SMasahiro Yamada * Makefiles use a line-based syntax, where the newline 303bc6df812SMasahiro Yamada * is the end of a statement. After seeing a newline, 304bc6df812SMasahiro Yamada * we expect the next token is a target. 305bc6df812SMasahiro Yamada */ 306bc6df812SMasahiro Yamada p++; 307bc6df812SMasahiro Yamada is_target = true; 308bc6df812SMasahiro Yamada continue; 309bc6df812SMasahiro Yamada case ':': 310bc6df812SMasahiro Yamada /* 311bc6df812SMasahiro Yamada * assume the first dependency after a colon as the 312bc6df812SMasahiro Yamada * source file. 313bc6df812SMasahiro Yamada */ 314bc6df812SMasahiro Yamada p++; 315bc6df812SMasahiro Yamada is_target = false; 316bc6df812SMasahiro Yamada is_source = true; 317bc6df812SMasahiro Yamada continue; 318bc6df812SMasahiro Yamada } 31901b5cbe7SMasahiro Yamada 320bc6df812SMasahiro Yamada /* find the end of the token */ 321bc6df812SMasahiro Yamada q = p; 322bc6df812SMasahiro Yamada while (*q != ' ' && *q != '\t' && *q != '\n' && *q != '#' && *q != ':') { 323bc6df812SMasahiro Yamada if (*q == '\\') { 324bc6df812SMasahiro Yamada /* 325bc6df812SMasahiro Yamada * backslash/newline combinations work like as 326bc6df812SMasahiro Yamada * a whitespace, so this is the end of token. 327bc6df812SMasahiro Yamada */ 328bc6df812SMasahiro Yamada if (*(q + 1) == '\n') 32901b5cbe7SMasahiro Yamada break; 33001b5cbe7SMasahiro Yamada 331bc6df812SMasahiro Yamada /* escaped special characters */ 332bc6df812SMasahiro Yamada if (*(q + 1) == '#' || *(q + 1) == ':') { 333bc6df812SMasahiro Yamada memmove(p + 1, p, q - p); 3341da177e4SLinus Torvalds p++; 335bc6df812SMasahiro Yamada } 336bc6df812SMasahiro Yamada 337bc6df812SMasahiro Yamada q++; 338bc6df812SMasahiro Yamada } 339bc6df812SMasahiro Yamada 340bc6df812SMasahiro Yamada if (*q == '\0') 341bc6df812SMasahiro Yamada break; 342bc6df812SMasahiro Yamada q++; 343bc6df812SMasahiro Yamada } 344bc6df812SMasahiro Yamada 345bc6df812SMasahiro Yamada /* Just discard the target */ 3462ab8a996SStephen Warren if (is_target) { 347bc6df812SMasahiro Yamada p = q; 348bc6df812SMasahiro Yamada continue; 349bc6df812SMasahiro Yamada } 350bc6df812SMasahiro Yamada 351bc6df812SMasahiro Yamada saved_c = *q; 352bc6df812SMasahiro Yamada *q = '\0'; 353bc6df812SMasahiro Yamada need_parse = false; 3542ab8a996SStephen Warren 355b7bd1821SMichal Marek /* 356bc6df812SMasahiro Yamada * Do not list the source file as dependency, so that kbuild is 357bc6df812SMasahiro Yamada * not confused if a .c file is rewritten into .S or vice versa. 358bc6df812SMasahiro Yamada * Storing it in source_* is needed for modpost to compute 359bc6df812SMasahiro Yamada * srcversions. 360b7bd1821SMichal Marek */ 361bc6df812SMasahiro Yamada if (is_source) { 3622ab8a996SStephen Warren /* 363bc6df812SMasahiro Yamada * The DT build rule concatenates multiple dep files. 364bc6df812SMasahiro Yamada * When processing them, only process the first source 365bc6df812SMasahiro Yamada * name, which will be the original one, and ignore any 366bc6df812SMasahiro Yamada * other source names, which will be intermediate 367bc6df812SMasahiro Yamada * temporary files. 368*faa91c47SMasahiro Yamada * 369*faa91c47SMasahiro Yamada * rustc emits the same dependency list for each 370*faa91c47SMasahiro Yamada * emission type. It is enough to list the source name 371*faa91c47SMasahiro Yamada * just once. 3722ab8a996SStephen Warren */ 3732ab8a996SStephen Warren if (!saw_any_target) { 374bc6df812SMasahiro Yamada saw_any_target = true; 375bc6df812SMasahiro Yamada printf("source_%s := %s\n\n", target, p); 37669304379SMasahiro Yamada printf("deps_%s := \\\n", target); 377bc6df812SMasahiro Yamada need_parse = true; 3782ab8a996SStephen Warren } 379*faa91c47SMasahiro Yamada } else if (!is_ignored_file(p, q - p) && 380*faa91c47SMasahiro Yamada !in_hashtable(p, q - p, file_hashtab)) { 381bc6df812SMasahiro Yamada printf(" %s \\\n", p); 382bc6df812SMasahiro Yamada need_parse = true; 38387b95a81SMasahiro Yamada } 3844003fd80SMasahiro Yamada 385bc6df812SMasahiro Yamada if (need_parse) { 386bc6df812SMasahiro Yamada void *buf; 387bc6df812SMasahiro Yamada 388bc6df812SMasahiro Yamada buf = read_file(p); 3894003fd80SMasahiro Yamada parse_config_file(buf); 3904003fd80SMasahiro Yamada free(buf); 3911da177e4SLinus Torvalds } 39201b5cbe7SMasahiro Yamada 393bc6df812SMasahiro Yamada is_source = false; 394bc6df812SMasahiro Yamada *q = saved_c; 395bc6df812SMasahiro Yamada p = q; 3961da177e4SLinus Torvalds } 3972ab8a996SStephen Warren 3982ab8a996SStephen Warren if (!saw_any_target) { 3992ab8a996SStephen Warren fprintf(stderr, "fixdep: parse error; no targets found\n"); 4002ab8a996SStephen Warren exit(1); 4012ab8a996SStephen Warren } 4022ab8a996SStephen Warren 40369304379SMasahiro Yamada printf("\n%s: $(deps_%s)\n\n", target, target); 40469304379SMasahiro Yamada printf("$(deps_%s):\n", target); 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds int main(int argc, char *argv[]) 4081da177e4SLinus Torvalds { 4095d1ef76fSMasahiro Yamada const char *depfile, *target, *cmdline; 4104003fd80SMasahiro Yamada void *buf; 4114003fd80SMasahiro Yamada 412bbda5ec6SMasahiro Yamada if (argc != 4) 4131da177e4SLinus Torvalds usage(); 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds depfile = argv[1]; 4161da177e4SLinus Torvalds target = argv[2]; 4171da177e4SLinus Torvalds cmdline = argv[3]; 4181da177e4SLinus Torvalds 41992215e7aSMasahiro Yamada printf("savedcmd_%s := %s\n\n", target, cmdline); 4204003fd80SMasahiro Yamada 4214003fd80SMasahiro Yamada buf = read_file(depfile); 422bbda5ec6SMasahiro Yamada parse_dep_file(buf, target); 4234003fd80SMasahiro Yamada free(buf); 4241da177e4SLinus Torvalds 42569304379SMasahiro Yamada fflush(stdout); 42669304379SMasahiro Yamada 42769304379SMasahiro Yamada /* 42869304379SMasahiro Yamada * In the intended usage, the stdout is redirected to .*.cmd files. 42969304379SMasahiro Yamada * Call ferror() to catch errors such as "No space left on device". 43069304379SMasahiro Yamada */ 43169304379SMasahiro Yamada if (ferror(stdout)) { 43269304379SMasahiro Yamada fprintf(stderr, "fixdep: not all data was written to the output\n"); 43369304379SMasahiro Yamada exit(1); 43469304379SMasahiro Yamada } 43569304379SMasahiro Yamada 4361da177e4SLinus Torvalds return 0; 4371da177e4SLinus Torvalds } 438