xref: /linux-6.15/scripts/basic/fixdep.c (revision 4e433fc4)
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
28*4e433fc4SCao 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
37*4e433fc4SCao 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
78*4e433fc4SCao 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  * Note 2: if somebody writes HELLO_CONFIG_BOOM in a file, it will depend onto
981da177e4SLinus Torvalds  * CONFIG_BOOM. This could seem a bug (not too hard to fix), but please do not
991da177e4SLinus Torvalds  * fix it! Some UserModeLinux files (look at arch/um/) call CONFIG_BOOM as
1001da177e4SLinus Torvalds  * UML_CONFIG_BOOM, to avoid conflicts with /usr/include/linux/autoconf.h,
1011da177e4SLinus Torvalds  * through arch/um/include/uml-config.h; this fixdep "bug" makes sure that
1021da177e4SLinus Torvalds  * those files will have correct dependencies.
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds #include <sys/types.h>
1061da177e4SLinus Torvalds #include <sys/stat.h>
1071da177e4SLinus Torvalds #include <sys/mman.h>
1081da177e4SLinus Torvalds #include <unistd.h>
1091da177e4SLinus Torvalds #include <fcntl.h>
1101da177e4SLinus Torvalds #include <string.h>
1111da177e4SLinus Torvalds #include <stdlib.h>
1121da177e4SLinus Torvalds #include <stdio.h>
1131da177e4SLinus Torvalds #include <limits.h>
1141da177e4SLinus Torvalds #include <ctype.h>
1151da177e4SLinus Torvalds #include <arpa/inet.h>
1161da177e4SLinus Torvalds 
117d8329e35SNicolas Pitre int insert_extra_deps;
1181da177e4SLinus Torvalds char *target;
1191da177e4SLinus Torvalds char *depfile;
1201da177e4SLinus Torvalds char *cmdline;
1211da177e4SLinus Torvalds 
1224356f489STrevor Keith static void usage(void)
1231da177e4SLinus Torvalds {
124d8329e35SNicolas Pitre 	fprintf(stderr, "Usage: fixdep [-e] <depfile> <target> <cmdline>\n");
125d8329e35SNicolas Pitre 	fprintf(stderr, " -e  insert extra dependencies given on stdin\n");
1261da177e4SLinus Torvalds 	exit(1);
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
1294d99f93bSSam Ravnborg /*
1304d99f93bSSam Ravnborg  * Print out the commandline prefixed with cmd_<target filename> :=
1316176aa9aSJan Beulich  */
1324356f489STrevor Keith static void print_cmdline(void)
1331da177e4SLinus Torvalds {
1346176aa9aSJan Beulich 	printf("cmd_%s := %s\n\n", target, cmdline);
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
137d8329e35SNicolas Pitre /*
138d8329e35SNicolas Pitre  * Print out a dependency path from a symbol name
139d8329e35SNicolas Pitre  */
140d8329e35SNicolas Pitre static void print_config(const char *m, int slen)
141d8329e35SNicolas Pitre {
142d8329e35SNicolas Pitre 	int c, i;
143d8329e35SNicolas Pitre 
144d8329e35SNicolas Pitre 	printf("    $(wildcard include/config/");
145d8329e35SNicolas Pitre 	for (i = 0; i < slen; i++) {
146d8329e35SNicolas Pitre 		c = m[i];
147d8329e35SNicolas Pitre 		if (c == '_')
148d8329e35SNicolas Pitre 			c = '/';
149d8329e35SNicolas Pitre 		else
150d8329e35SNicolas Pitre 			c = tolower(c);
151d8329e35SNicolas Pitre 		putchar(c);
152d8329e35SNicolas Pitre 	}
153d8329e35SNicolas Pitre 	printf(".h) \\\n");
154d8329e35SNicolas Pitre }
155d8329e35SNicolas Pitre 
156d8329e35SNicolas Pitre static void do_extra_deps(void)
157d8329e35SNicolas Pitre {
158d8329e35SNicolas Pitre 	if (insert_extra_deps) {
159d8329e35SNicolas Pitre 		char buf[80];
160d8329e35SNicolas Pitre 		while(fgets(buf, sizeof(buf), stdin)) {
161d8329e35SNicolas Pitre 			int len = strlen(buf);
162d8329e35SNicolas Pitre 			if (len < 2 || buf[len-1] != '\n') {
163d8329e35SNicolas Pitre 				fprintf(stderr, "fixdep: bad data on stdin\n");
164d8329e35SNicolas Pitre 				exit(1);
165d8329e35SNicolas Pitre 			}
166d8329e35SNicolas Pitre 			print_config(buf, len-1);
167d8329e35SNicolas Pitre 		}
168d8329e35SNicolas Pitre 	}
169d8329e35SNicolas Pitre }
170d8329e35SNicolas Pitre 
1718af27e1dSEric Dumazet struct item {
1728af27e1dSEric Dumazet 	struct item	*next;
1738af27e1dSEric Dumazet 	unsigned int	len;
1748af27e1dSEric Dumazet 	unsigned int	hash;
1758af27e1dSEric Dumazet 	char		name[0];
1768af27e1dSEric Dumazet };
1771da177e4SLinus Torvalds 
1788af27e1dSEric Dumazet #define HASHSZ 256
1798af27e1dSEric Dumazet static struct item *hashtab[HASHSZ];
1808af27e1dSEric Dumazet 
1818af27e1dSEric Dumazet static unsigned int strhash(const char *str, unsigned int sz)
1821da177e4SLinus Torvalds {
1838af27e1dSEric Dumazet 	/* fnv32 hash */
1848af27e1dSEric Dumazet 	unsigned int i, hash = 2166136261U;
1851da177e4SLinus Torvalds 
1868af27e1dSEric Dumazet 	for (i = 0; i < sz; i++)
1878af27e1dSEric Dumazet 		hash = (hash ^ str[i]) * 0x01000193;
1888af27e1dSEric Dumazet 	return hash;
1898af27e1dSEric Dumazet }
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds /*
1921da177e4SLinus Torvalds  * Lookup a value in the configuration string.
1931da177e4SLinus Torvalds  */
1948af27e1dSEric Dumazet static int is_defined_config(const char *name, int len, unsigned int hash)
1951da177e4SLinus Torvalds {
1968af27e1dSEric Dumazet 	struct item *aux;
1978af27e1dSEric Dumazet 
1988af27e1dSEric Dumazet 	for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
1998af27e1dSEric Dumazet 		if (aux->hash == hash && aux->len == len &&
2008af27e1dSEric Dumazet 		    memcmp(aux->name, name, len) == 0)
2011da177e4SLinus Torvalds 			return 1;
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds 	return 0;
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds /*
2071da177e4SLinus Torvalds  * Add a new value to the configuration string.
2081da177e4SLinus Torvalds  */
2098af27e1dSEric Dumazet static void define_config(const char *name, int len, unsigned int hash)
2101da177e4SLinus Torvalds {
2118af27e1dSEric Dumazet 	struct item *aux = malloc(sizeof(*aux) + len);
2121da177e4SLinus Torvalds 
2138af27e1dSEric Dumazet 	if (!aux) {
2148af27e1dSEric Dumazet 		perror("fixdep:malloc");
2158af27e1dSEric Dumazet 		exit(1);
2168af27e1dSEric Dumazet 	}
2178af27e1dSEric Dumazet 	memcpy(aux->name, name, len);
2188af27e1dSEric Dumazet 	aux->len = len;
2198af27e1dSEric Dumazet 	aux->hash = hash;
2208af27e1dSEric Dumazet 	aux->next = hashtab[hash % HASHSZ];
2218af27e1dSEric Dumazet 	hashtab[hash % HASHSZ] = aux;
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds /*
2251da177e4SLinus Torvalds  * Record the use of a CONFIG_* word.
2261da177e4SLinus Torvalds  */
2278af27e1dSEric Dumazet static void use_config(const char *m, int slen)
2281da177e4SLinus Torvalds {
2298af27e1dSEric Dumazet 	unsigned int hash = strhash(m, slen);
2301da177e4SLinus Torvalds 
2318af27e1dSEric Dumazet 	if (is_defined_config(m, slen, hash))
2321da177e4SLinus Torvalds 	    return;
2331da177e4SLinus Torvalds 
2348af27e1dSEric Dumazet 	define_config(m, slen, hash);
235d8329e35SNicolas Pitre 	print_config(m, slen);
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
238dee81e98SAlexey Dobriyan static void parse_config_file(const char *p)
2391da177e4SLinus Torvalds {
240dee81e98SAlexey Dobriyan 	const char *q, *r;
2411da177e4SLinus Torvalds 
242dee81e98SAlexey Dobriyan 	while ((p = strstr(p, "CONFIG_"))) {
243d7211096SMasahiro Yamada 		p += 7;
244dee81e98SAlexey Dobriyan 		q = p;
245dee81e98SAlexey Dobriyan 		while (*q && (isalnum(*q) || *q == '_'))
246dee81e98SAlexey Dobriyan 			q++;
247dee81e98SAlexey Dobriyan 		if (memcmp(q - 7, "_MODULE", 7) == 0)
248dee81e98SAlexey Dobriyan 			r = q - 7;
249dee81e98SAlexey Dobriyan 		else
250dee81e98SAlexey Dobriyan 			r = q;
251dee81e98SAlexey Dobriyan 		if (r > p)
252dee81e98SAlexey Dobriyan 			use_config(p, r - p);
253dee81e98SAlexey Dobriyan 		p = q;
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
257d8329e35SNicolas Pitre /* test if s ends in sub */
2584c835b57SNicolas Iooss static int strrcmp(const char *s, const char *sub)
2591da177e4SLinus Torvalds {
2601da177e4SLinus Torvalds 	int slen = strlen(s);
2611da177e4SLinus Torvalds 	int sublen = strlen(sub);
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	if (sublen > slen)
2641da177e4SLinus Torvalds 		return 1;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	return memcmp(s + slen - sublen, sub, sublen);
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
2698af27e1dSEric Dumazet static void do_config_file(const char *filename)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	struct stat st;
2721da177e4SLinus Torvalds 	int fd;
273dee81e98SAlexey Dobriyan 	char *map;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	fd = open(filename, O_RDONLY);
2761da177e4SLinus Torvalds 	if (fd < 0) {
277a3ba8113SBen Gamari 		fprintf(stderr, "fixdep: error opening config file: ");
2781da177e4SLinus Torvalds 		perror(filename);
2791da177e4SLinus Torvalds 		exit(2);
2801da177e4SLinus Torvalds 	}
28146fe94adSTom Rini 	if (fstat(fd, &st) < 0) {
28246fe94adSTom Rini 		fprintf(stderr, "fixdep: error fstat'ing config file: ");
28346fe94adSTom Rini 		perror(filename);
28446fe94adSTom Rini 		exit(2);
28546fe94adSTom Rini 	}
2861da177e4SLinus Torvalds 	if (st.st_size == 0) {
2871da177e4SLinus Torvalds 		close(fd);
2881da177e4SLinus Torvalds 		return;
2891da177e4SLinus Torvalds 	}
290dee81e98SAlexey Dobriyan 	map = malloc(st.st_size + 1);
291dee81e98SAlexey Dobriyan 	if (!map) {
292dee81e98SAlexey Dobriyan 		perror("fixdep: malloc");
2931da177e4SLinus Torvalds 		close(fd);
2941da177e4SLinus Torvalds 		return;
2951da177e4SLinus Torvalds 	}
296dee81e98SAlexey Dobriyan 	if (read(fd, map, st.st_size) != st.st_size) {
297dee81e98SAlexey Dobriyan 		perror("fixdep: read");
2981da177e4SLinus Torvalds 		close(fd);
299dee81e98SAlexey Dobriyan 		return;
300dee81e98SAlexey Dobriyan 	}
301dee81e98SAlexey Dobriyan 	map[st.st_size] = '\0';
302dee81e98SAlexey Dobriyan 	close(fd);
303dee81e98SAlexey Dobriyan 
304dee81e98SAlexey Dobriyan 	parse_config_file(map);
305dee81e98SAlexey Dobriyan 
306dee81e98SAlexey Dobriyan 	free(map);
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3097840fea2SMichal Marek /*
3107840fea2SMichal Marek  * Important: The below generated source_foo.o and deps_foo.o variable
3117840fea2SMichal Marek  * assignments are parsed not only by make, but also by the rather simple
3127840fea2SMichal Marek  * parser in scripts/mod/sumversion.c.
3137840fea2SMichal Marek  */
3144356f489STrevor Keith static void parse_dep_file(void *map, size_t len)
3151da177e4SLinus Torvalds {
31648b9d03cSJ.A. Magallon 	char *m = map;
31748b9d03cSJ.A. Magallon 	char *end = m + len;
31848b9d03cSJ.A. Magallon 	char *p;
3191da177e4SLinus Torvalds 	char s[PATH_MAX];
3202ab8a996SStephen Warren 	int is_target;
3212ab8a996SStephen Warren 	int saw_any_target = 0;
3222ab8a996SStephen Warren 	int is_first_dep = 0;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	while (m < end) {
3252ab8a996SStephen Warren 		/* Skip any "white space" */
3261da177e4SLinus Torvalds 		while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))
3271da177e4SLinus Torvalds 			m++;
3282ab8a996SStephen Warren 		/* Find next "white space" */
3291da177e4SLinus Torvalds 		p = m;
3302ab8a996SStephen Warren 		while (p < end && *p != ' ' && *p != '\\' && *p != '\n')
3311da177e4SLinus Torvalds 			p++;
3322ab8a996SStephen Warren 		/* Is the token we found a target name? */
3332ab8a996SStephen Warren 		is_target = (*(p-1) == ':');
3342ab8a996SStephen Warren 		/* Don't write any target names into the dependency file */
3352ab8a996SStephen Warren 		if (is_target) {
3362ab8a996SStephen Warren 			/* The /next/ file is the first dependency */
3372ab8a996SStephen Warren 			is_first_dep = 1;
3382ab8a996SStephen Warren 		} else {
3392ab8a996SStephen Warren 			/* Save this token/filename */
3402ab8a996SStephen Warren 			memcpy(s, m, p-m);
3412ab8a996SStephen Warren 			s[p - m] = 0;
3422ab8a996SStephen Warren 
3432ab8a996SStephen Warren 			/* Ignore certain dependencies */
344264a2683SSam Ravnborg 			if (strrcmp(s, "include/generated/autoconf.h") &&
345c1a95fdaSNicolas Pitre 			    strrcmp(s, "include/generated/autoksyms.h") &&
3461da177e4SLinus Torvalds 			    strrcmp(s, "arch/um/include/uml-config.h") &&
3476a5be57fSPeter Foley 			    strrcmp(s, "include/linux/kconfig.h") &&
3481da177e4SLinus Torvalds 			    strrcmp(s, ".ver")) {
349b7bd1821SMichal Marek 				/*
3502ab8a996SStephen Warren 				 * Do not list the source file as dependency,
3512ab8a996SStephen Warren 				 * so that kbuild is not confused if a .c file
3522ab8a996SStephen Warren 				 * is rewritten into .S or vice versa. Storing
3532ab8a996SStephen Warren 				 * it in source_* is needed for modpost to
3542ab8a996SStephen Warren 				 * compute srcversions.
355b7bd1821SMichal Marek 				 */
3562ab8a996SStephen Warren 				if (is_first_dep) {
3572ab8a996SStephen Warren 					/*
3582ab8a996SStephen Warren 					 * If processing the concatenation of
3592ab8a996SStephen Warren 					 * multiple dependency files, only
3602ab8a996SStephen Warren 					 * process the first target name, which
3612ab8a996SStephen Warren 					 * will be the original source name,
3622ab8a996SStephen Warren 					 * and ignore any other target names,
3632ab8a996SStephen Warren 					 * which will be intermediate temporary
3642ab8a996SStephen Warren 					 * files.
3652ab8a996SStephen Warren 					 */
3662ab8a996SStephen Warren 					if (!saw_any_target) {
3672ab8a996SStephen Warren 						saw_any_target = 1;
3682ab8a996SStephen Warren 						printf("source_%s := %s\n\n",
3692ab8a996SStephen Warren 							target, s);
3702ab8a996SStephen Warren 						printf("deps_%s := \\\n",
3712ab8a996SStephen Warren 							target);
3722ab8a996SStephen Warren 					}
3732ab8a996SStephen Warren 					is_first_dep = 0;
3747840fea2SMichal Marek 				} else
3751da177e4SLinus Torvalds 					printf("  %s \\\n", s);
3761da177e4SLinus Torvalds 				do_config_file(s);
3771da177e4SLinus Torvalds 			}
3782ab8a996SStephen Warren 		}
3792ab8a996SStephen Warren 		/*
3802ab8a996SStephen Warren 		 * Start searching for next token immediately after the first
3812ab8a996SStephen Warren 		 * "whitespace" character that follows this token.
3822ab8a996SStephen Warren 		 */
3831da177e4SLinus Torvalds 		m = p + 1;
3841da177e4SLinus Torvalds 	}
3852ab8a996SStephen Warren 
3862ab8a996SStephen Warren 	if (!saw_any_target) {
3872ab8a996SStephen Warren 		fprintf(stderr, "fixdep: parse error; no targets found\n");
3882ab8a996SStephen Warren 		exit(1);
3892ab8a996SStephen Warren 	}
3902ab8a996SStephen Warren 
391d8329e35SNicolas Pitre 	do_extra_deps();
392d8329e35SNicolas Pitre 
3931da177e4SLinus Torvalds 	printf("\n%s: $(deps_%s)\n\n", target, target);
3941da177e4SLinus Torvalds 	printf("$(deps_%s):\n", target);
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
3974356f489STrevor Keith static void print_deps(void)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	struct stat st;
4001da177e4SLinus Torvalds 	int fd;
4011da177e4SLinus Torvalds 	void *map;
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	fd = open(depfile, O_RDONLY);
4041da177e4SLinus Torvalds 	if (fd < 0) {
405a3ba8113SBen Gamari 		fprintf(stderr, "fixdep: error opening depfile: ");
4061da177e4SLinus Torvalds 		perror(depfile);
4071da177e4SLinus Torvalds 		exit(2);
4081da177e4SLinus Torvalds 	}
409a3ba8113SBen Gamari 	if (fstat(fd, &st) < 0) {
410a3ba8113SBen Gamari 		fprintf(stderr, "fixdep: error fstat'ing depfile: ");
411a3ba8113SBen Gamari 		perror(depfile);
412a3ba8113SBen Gamari 		exit(2);
413a3ba8113SBen Gamari 	}
4141da177e4SLinus Torvalds 	if (st.st_size == 0) {
4151da177e4SLinus Torvalds 		fprintf(stderr,"fixdep: %s is empty\n",depfile);
4161da177e4SLinus Torvalds 		close(fd);
4171da177e4SLinus Torvalds 		return;
4181da177e4SLinus Torvalds 	}
4191da177e4SLinus Torvalds 	map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
4201da177e4SLinus Torvalds 	if ((long) map == -1) {
4211da177e4SLinus Torvalds 		perror("fixdep: mmap");
4221da177e4SLinus Torvalds 		close(fd);
4231da177e4SLinus Torvalds 		return;
4241da177e4SLinus Torvalds 	}
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	parse_dep_file(map, st.st_size);
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	munmap(map, st.st_size);
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	close(fd);
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds int main(int argc, char *argv[])
4341da177e4SLinus Torvalds {
435d8329e35SNicolas Pitre 	if (argc == 5 && !strcmp(argv[1], "-e")) {
436d8329e35SNicolas Pitre 		insert_extra_deps = 1;
437d8329e35SNicolas Pitre 		argv++;
438d8329e35SNicolas Pitre 	} else if (argc != 4)
4391da177e4SLinus Torvalds 		usage();
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	depfile = argv[1];
4421da177e4SLinus Torvalds 	target = argv[2];
4431da177e4SLinus Torvalds 	cmdline = argv[3];
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	print_cmdline();
4461da177e4SLinus Torvalds 	print_deps();
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	return 0;
4491da177e4SLinus Torvalds }
450