xref: /linux-6.15/scripts/basic/fixdep.c (revision a46078d6)
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 
102*a46078d6SMasahiro Yamada #include <xalloc.h>
103*a46078d6SMasahiro Yamada 
usage(void)1044356f489STrevor Keith static void usage(void)
1051da177e4SLinus Torvalds {
106bbda5ec6SMasahiro Yamada 	fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
1071da177e4SLinus Torvalds 	exit(1);
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
1108af27e1dSEric Dumazet struct item {
1118af27e1dSEric Dumazet 	struct item	*next;
1128af27e1dSEric Dumazet 	unsigned int	len;
1138af27e1dSEric Dumazet 	unsigned int	hash;
114859c8175SGustavo A. R. Silva 	char		name[];
1158af27e1dSEric Dumazet };
1161da177e4SLinus Torvalds 
1178af27e1dSEric Dumazet #define HASHSZ 256
118faa91c47SMasahiro Yamada static struct item *config_hashtab[HASHSZ], *file_hashtab[HASHSZ];
1198af27e1dSEric Dumazet 
strhash(const char * str,unsigned int sz)1208af27e1dSEric Dumazet static unsigned int strhash(const char *str, unsigned int sz)
1211da177e4SLinus Torvalds {
1228af27e1dSEric Dumazet 	/* fnv32 hash */
1238af27e1dSEric Dumazet 	unsigned int i, hash = 2166136261U;
1241da177e4SLinus Torvalds 
1258af27e1dSEric Dumazet 	for (i = 0; i < sz; i++)
1268af27e1dSEric Dumazet 		hash = (hash ^ str[i]) * 0x01000193;
1278af27e1dSEric Dumazet 	return hash;
1288af27e1dSEric Dumazet }
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds /*
1311da177e4SLinus Torvalds  * Add a new value to the configuration string.
1321da177e4SLinus Torvalds  */
add_to_hashtable(const char * name,int len,unsigned int hash,struct item * hashtab[])133871d6573SMasahiro Yamada static void add_to_hashtable(const char *name, int len, unsigned int hash,
134871d6573SMasahiro Yamada 			     struct item *hashtab[])
1351da177e4SLinus Torvalds {
136*a46078d6SMasahiro Yamada 	struct item *aux;
1371da177e4SLinus Torvalds 
138*a46078d6SMasahiro Yamada 	aux = xmalloc(sizeof(*aux) + len);
1398af27e1dSEric Dumazet 	memcpy(aux->name, name, len);
1408af27e1dSEric Dumazet 	aux->len = len;
1418af27e1dSEric Dumazet 	aux->hash = hash;
1428af27e1dSEric Dumazet 	aux->next = hashtab[hash % HASHSZ];
1438af27e1dSEric Dumazet 	hashtab[hash % HASHSZ] = aux;
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds /*
147871d6573SMasahiro Yamada  * Lookup a string in the hash table. If found, just return true.
148871d6573SMasahiro Yamada  * If not, add it to the hashtable and return false.
149871d6573SMasahiro Yamada  */
in_hashtable(const char * name,int len,struct item * hashtab[])150871d6573SMasahiro Yamada static bool in_hashtable(const char *name, int len, struct item *hashtab[])
151871d6573SMasahiro Yamada {
152871d6573SMasahiro Yamada 	struct item *aux;
153871d6573SMasahiro Yamada 	unsigned int hash = strhash(name, len);
154871d6573SMasahiro Yamada 
155871d6573SMasahiro Yamada 	for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
156871d6573SMasahiro Yamada 		if (aux->hash == hash && aux->len == len &&
157871d6573SMasahiro Yamada 		    memcmp(aux->name, name, len) == 0)
158871d6573SMasahiro Yamada 			return true;
159871d6573SMasahiro Yamada 	}
160871d6573SMasahiro Yamada 
161871d6573SMasahiro Yamada 	add_to_hashtable(name, len, hash, hashtab);
162871d6573SMasahiro Yamada 
163871d6573SMasahiro Yamada 	return false;
164871d6573SMasahiro Yamada }
165871d6573SMasahiro Yamada 
166871d6573SMasahiro Yamada /*
1671da177e4SLinus Torvalds  * Record the use of a CONFIG_* word.
1681da177e4SLinus Torvalds  */
use_config(const char * m,int slen)1698af27e1dSEric Dumazet static void use_config(const char *m, int slen)
1701da177e4SLinus Torvalds {
171871d6573SMasahiro Yamada 	if (in_hashtable(m, slen, config_hashtab))
1721da177e4SLinus Torvalds 		return;
1731da177e4SLinus Torvalds 
1740e0345b7SAlexey Dobriyan 	/* Print out a dependency path from a symbol name. */
17569304379SMasahiro Yamada 	printf("    $(wildcard include/config/%.*s) \\\n", slen, m);
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
178d8329e35SNicolas Pitre /* test if s ends in sub */
str_ends_with(const char * s,int slen,const char * sub)17987b95a81SMasahiro Yamada static int str_ends_with(const char *s, int slen, const char *sub)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	int sublen = strlen(sub);
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	if (sublen > slen)
18487b95a81SMasahiro Yamada 		return 0;
1851da177e4SLinus Torvalds 
18687b95a81SMasahiro Yamada 	return !memcmp(s + slen - sublen, sub, sublen);
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
parse_config_file(const char * p)189ab9ce9feSMasahiro Yamada static void parse_config_file(const char *p)
190ab9ce9feSMasahiro Yamada {
191ab9ce9feSMasahiro Yamada 	const char *q, *r;
1925b8ad96dSRasmus Villemoes 	const char *start = p;
193ab9ce9feSMasahiro Yamada 
194ab9ce9feSMasahiro Yamada 	while ((p = strstr(p, "CONFIG_"))) {
1955b8ad96dSRasmus Villemoes 		if (p > start && (isalnum(p[-1]) || p[-1] == '_')) {
1965b8ad96dSRasmus Villemoes 			p += 7;
1975b8ad96dSRasmus Villemoes 			continue;
1985b8ad96dSRasmus Villemoes 		}
199ab9ce9feSMasahiro Yamada 		p += 7;
200ab9ce9feSMasahiro Yamada 		q = p;
2013f9070a6SMasahiro Yamada 		while (isalnum(*q) || *q == '_')
202ab9ce9feSMasahiro Yamada 			q++;
203ab9ce9feSMasahiro Yamada 		if (str_ends_with(p, q - p, "_MODULE"))
204ab9ce9feSMasahiro Yamada 			r = q - 7;
205ab9ce9feSMasahiro Yamada 		else
206ab9ce9feSMasahiro Yamada 			r = q;
207ab9ce9feSMasahiro Yamada 		if (r > p)
208ab9ce9feSMasahiro Yamada 			use_config(p, r - p);
209ab9ce9feSMasahiro Yamada 		p = q;
210ab9ce9feSMasahiro Yamada 	}
211ab9ce9feSMasahiro Yamada }
212ab9ce9feSMasahiro Yamada 
read_file(const char * filename)2134003fd80SMasahiro Yamada static void *read_file(const char *filename)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	struct stat st;
2161da177e4SLinus Torvalds 	int fd;
2174003fd80SMasahiro Yamada 	char *buf;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	fd = open(filename, O_RDONLY);
2201da177e4SLinus Torvalds 	if (fd < 0) {
2214003fd80SMasahiro Yamada 		fprintf(stderr, "fixdep: error opening file: ");
2221da177e4SLinus Torvalds 		perror(filename);
2231da177e4SLinus Torvalds 		exit(2);
2241da177e4SLinus Torvalds 	}
22546fe94adSTom Rini 	if (fstat(fd, &st) < 0) {
2264003fd80SMasahiro Yamada 		fprintf(stderr, "fixdep: error fstat'ing file: ");
22746fe94adSTom Rini 		perror(filename);
22846fe94adSTom Rini 		exit(2);
22946fe94adSTom Rini 	}
230*a46078d6SMasahiro Yamada 	buf = xmalloc(st.st_size + 1);
2314003fd80SMasahiro Yamada 	if (read(fd, buf, st.st_size) != st.st_size) {
232dee81e98SAlexey Dobriyan 		perror("fixdep: read");
2337c2ec43aSLukas Bulwahn 		exit(2);
234dee81e98SAlexey Dobriyan 	}
2354003fd80SMasahiro Yamada 	buf[st.st_size] = '\0';
236dee81e98SAlexey Dobriyan 	close(fd);
237dee81e98SAlexey Dobriyan 
2384003fd80SMasahiro Yamada 	return buf;
2391da177e4SLinus Torvalds }
2401da177e4SLinus Torvalds 
24187b95a81SMasahiro Yamada /* Ignore certain dependencies */
is_ignored_file(const char * s,int len)24287b95a81SMasahiro Yamada static int is_ignored_file(const char *s, int len)
24387b95a81SMasahiro Yamada {
2445e9e95ccSMasahiro Yamada 	return str_ends_with(s, len, "include/generated/autoconf.h");
24587b95a81SMasahiro Yamada }
24687b95a81SMasahiro Yamada 
24793c656deSMasahiro Yamada /* Do not parse these files */
is_no_parse_file(const char * s,int len)24893c656deSMasahiro Yamada static int is_no_parse_file(const char *s, int len)
24993c656deSMasahiro Yamada {
25093c656deSMasahiro Yamada 	/* rustc may list binary files in dep-info */
25193c656deSMasahiro Yamada 	return str_ends_with(s, len, ".rlib") ||
25293c656deSMasahiro Yamada 	       str_ends_with(s, len, ".rmeta") ||
25393c656deSMasahiro Yamada 	       str_ends_with(s, len, ".so");
25493c656deSMasahiro Yamada }
25593c656deSMasahiro Yamada 
2567840fea2SMichal Marek /*
2577840fea2SMichal Marek  * Important: The below generated source_foo.o and deps_foo.o variable
2587840fea2SMichal Marek  * assignments are parsed not only by make, but also by the rather simple
2597840fea2SMichal Marek  * parser in scripts/mod/sumversion.c.
2607840fea2SMichal Marek  */
parse_dep_file(char * p,const char * target)261bc6df812SMasahiro Yamada static void parse_dep_file(char *p, const char *target)
2621da177e4SLinus Torvalds {
263bc6df812SMasahiro Yamada 	bool saw_any_target = false;
264bc6df812SMasahiro Yamada 	bool is_target = true;
265bc6df812SMasahiro Yamada 	bool is_source = false;
266bc6df812SMasahiro Yamada 	bool need_parse;
267bc6df812SMasahiro Yamada 	char *q, saved_c;
2681da177e4SLinus Torvalds 
269bc6df812SMasahiro Yamada 	while (*p) {
270bc6df812SMasahiro Yamada 		/* handle some special characters first. */
271bc6df812SMasahiro Yamada 		switch (*p) {
272bc6df812SMasahiro Yamada 		case '#':
273bc6df812SMasahiro Yamada 			/*
274bc6df812SMasahiro Yamada 			 * skip comments.
275bc6df812SMasahiro Yamada 			 * rustc may emit comments to dep-info.
276bc6df812SMasahiro Yamada 			 */
277bc6df812SMasahiro Yamada 			p++;
278bc6df812SMasahiro Yamada 			while (*p != '\0' && *p != '\n') {
279bc6df812SMasahiro Yamada 				/*
280bc6df812SMasahiro Yamada 				 * escaped newlines continue the comment across
281bc6df812SMasahiro Yamada 				 * multiple lines.
282bc6df812SMasahiro Yamada 				 */
283bc6df812SMasahiro Yamada 				if (*p == '\\')
284bc6df812SMasahiro Yamada 					p++;
285bc6df812SMasahiro Yamada 				p++;
286bc6df812SMasahiro Yamada 			}
287bc6df812SMasahiro Yamada 			continue;
288bc6df812SMasahiro Yamada 		case ' ':
289bc6df812SMasahiro Yamada 		case '\t':
290bc6df812SMasahiro Yamada 			/* skip whitespaces */
291bc6df812SMasahiro Yamada 			p++;
292bc6df812SMasahiro Yamada 			continue;
293bc6df812SMasahiro Yamada 		case '\\':
294bc6df812SMasahiro Yamada 			/*
295bc6df812SMasahiro Yamada 			 * backslash/newline combinations continue the
296bc6df812SMasahiro Yamada 			 * statement. Skip it just like a whitespace.
297bc6df812SMasahiro Yamada 			 */
298bc6df812SMasahiro Yamada 			if (*(p + 1) == '\n') {
299bc6df812SMasahiro Yamada 				p += 2;
300bc6df812SMasahiro Yamada 				continue;
301bc6df812SMasahiro Yamada 			}
302bc6df812SMasahiro Yamada 			break;
303bc6df812SMasahiro Yamada 		case '\n':
304bc6df812SMasahiro Yamada 			/*
305bc6df812SMasahiro Yamada 			 * Makefiles use a line-based syntax, where the newline
306bc6df812SMasahiro Yamada 			 * is the end of a statement. After seeing a newline,
307bc6df812SMasahiro Yamada 			 * we expect the next token is a target.
308bc6df812SMasahiro Yamada 			 */
309bc6df812SMasahiro Yamada 			p++;
310bc6df812SMasahiro Yamada 			is_target = true;
311bc6df812SMasahiro Yamada 			continue;
312bc6df812SMasahiro Yamada 		case ':':
313bc6df812SMasahiro Yamada 			/*
314bc6df812SMasahiro Yamada 			 * assume the first dependency after a colon as the
315bc6df812SMasahiro Yamada 			 * source file.
316bc6df812SMasahiro Yamada 			 */
317bc6df812SMasahiro Yamada 			p++;
318bc6df812SMasahiro Yamada 			is_target = false;
319bc6df812SMasahiro Yamada 			is_source = true;
320bc6df812SMasahiro Yamada 			continue;
321bc6df812SMasahiro Yamada 		}
32201b5cbe7SMasahiro Yamada 
323bc6df812SMasahiro Yamada 		/* find the end of the token */
324bc6df812SMasahiro Yamada 		q = p;
325bc6df812SMasahiro Yamada 		while (*q != ' ' && *q != '\t' && *q != '\n' && *q != '#' && *q != ':') {
326bc6df812SMasahiro Yamada 			if (*q == '\\') {
327bc6df812SMasahiro Yamada 				/*
328bc6df812SMasahiro Yamada 				 * backslash/newline combinations work like as
329bc6df812SMasahiro Yamada 				 * a whitespace, so this is the end of token.
330bc6df812SMasahiro Yamada 				 */
331bc6df812SMasahiro Yamada 				if (*(q + 1) == '\n')
33201b5cbe7SMasahiro Yamada 					break;
33301b5cbe7SMasahiro Yamada 
334bc6df812SMasahiro Yamada 				/* escaped special characters */
335bc6df812SMasahiro Yamada 				if (*(q + 1) == '#' || *(q + 1) == ':') {
336bc6df812SMasahiro Yamada 					memmove(p + 1, p, q - p);
3371da177e4SLinus Torvalds 					p++;
338bc6df812SMasahiro Yamada 				}
339bc6df812SMasahiro Yamada 
340bc6df812SMasahiro Yamada 				q++;
341bc6df812SMasahiro Yamada 			}
342bc6df812SMasahiro Yamada 
343bc6df812SMasahiro Yamada 			if (*q == '\0')
344bc6df812SMasahiro Yamada 				break;
345bc6df812SMasahiro Yamada 			q++;
346bc6df812SMasahiro Yamada 		}
347bc6df812SMasahiro Yamada 
348bc6df812SMasahiro Yamada 		/* Just discard the target */
3492ab8a996SStephen Warren 		if (is_target) {
350bc6df812SMasahiro Yamada 			p = q;
351bc6df812SMasahiro Yamada 			continue;
352bc6df812SMasahiro Yamada 		}
353bc6df812SMasahiro Yamada 
354bc6df812SMasahiro Yamada 		saved_c = *q;
355bc6df812SMasahiro Yamada 		*q = '\0';
356bc6df812SMasahiro Yamada 		need_parse = false;
3572ab8a996SStephen Warren 
358b7bd1821SMichal Marek 		/*
359bc6df812SMasahiro Yamada 		 * Do not list the source file as dependency, so that kbuild is
360bc6df812SMasahiro Yamada 		 * not confused if a .c file is rewritten into .S or vice versa.
361bc6df812SMasahiro Yamada 		 * Storing it in source_* is needed for modpost to compute
362bc6df812SMasahiro Yamada 		 * srcversions.
363b7bd1821SMichal Marek 		 */
364bc6df812SMasahiro Yamada 		if (is_source) {
3652ab8a996SStephen Warren 			/*
366bc6df812SMasahiro Yamada 			 * The DT build rule concatenates multiple dep files.
367bc6df812SMasahiro Yamada 			 * When processing them, only process the first source
368bc6df812SMasahiro Yamada 			 * name, which will be the original one, and ignore any
369bc6df812SMasahiro Yamada 			 * other source names, which will be intermediate
370bc6df812SMasahiro Yamada 			 * temporary files.
371faa91c47SMasahiro Yamada 			 *
372faa91c47SMasahiro Yamada 			 * rustc emits the same dependency list for each
373faa91c47SMasahiro Yamada 			 * emission type. It is enough to list the source name
374faa91c47SMasahiro Yamada 			 * just once.
3752ab8a996SStephen Warren 			 */
3762ab8a996SStephen Warren 			if (!saw_any_target) {
377bc6df812SMasahiro Yamada 				saw_any_target = true;
378bc6df812SMasahiro Yamada 				printf("source_%s := %s\n\n", target, p);
37969304379SMasahiro Yamada 				printf("deps_%s := \\\n", target);
380bc6df812SMasahiro Yamada 				need_parse = true;
3812ab8a996SStephen Warren 			}
382faa91c47SMasahiro Yamada 		} else if (!is_ignored_file(p, q - p) &&
383faa91c47SMasahiro Yamada 			   !in_hashtable(p, q - p, file_hashtab)) {
384bc6df812SMasahiro Yamada 			printf("  %s \\\n", p);
385bc6df812SMasahiro Yamada 			need_parse = true;
38687b95a81SMasahiro Yamada 		}
3874003fd80SMasahiro Yamada 
38893c656deSMasahiro Yamada 		if (need_parse && !is_no_parse_file(p, q - p)) {
389bc6df812SMasahiro Yamada 			void *buf;
390bc6df812SMasahiro Yamada 
391bc6df812SMasahiro Yamada 			buf = read_file(p);
3924003fd80SMasahiro Yamada 			parse_config_file(buf);
3934003fd80SMasahiro Yamada 			free(buf);
3941da177e4SLinus Torvalds 		}
39501b5cbe7SMasahiro Yamada 
396bc6df812SMasahiro Yamada 		is_source = false;
397bc6df812SMasahiro Yamada 		*q = saved_c;
398bc6df812SMasahiro Yamada 		p = q;
3991da177e4SLinus Torvalds 	}
4002ab8a996SStephen Warren 
4012ab8a996SStephen Warren 	if (!saw_any_target) {
4022ab8a996SStephen Warren 		fprintf(stderr, "fixdep: parse error; no targets found\n");
4032ab8a996SStephen Warren 		exit(1);
4042ab8a996SStephen Warren 	}
4052ab8a996SStephen Warren 
40669304379SMasahiro Yamada 	printf("\n%s: $(deps_%s)\n\n", target, target);
40769304379SMasahiro Yamada 	printf("$(deps_%s):\n", target);
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
main(int argc,char * argv[])4101da177e4SLinus Torvalds int main(int argc, char *argv[])
4111da177e4SLinus Torvalds {
4125d1ef76fSMasahiro Yamada 	const char *depfile, *target, *cmdline;
4134003fd80SMasahiro Yamada 	void *buf;
4144003fd80SMasahiro Yamada 
415bbda5ec6SMasahiro Yamada 	if (argc != 4)
4161da177e4SLinus Torvalds 		usage();
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	depfile = argv[1];
4191da177e4SLinus Torvalds 	target = argv[2];
4201da177e4SLinus Torvalds 	cmdline = argv[3];
4211da177e4SLinus Torvalds 
42292215e7aSMasahiro Yamada 	printf("savedcmd_%s := %s\n\n", target, cmdline);
4234003fd80SMasahiro Yamada 
4244003fd80SMasahiro Yamada 	buf = read_file(depfile);
425bbda5ec6SMasahiro Yamada 	parse_dep_file(buf, target);
4264003fd80SMasahiro Yamada 	free(buf);
4271da177e4SLinus Torvalds 
42869304379SMasahiro Yamada 	fflush(stdout);
42969304379SMasahiro Yamada 
43069304379SMasahiro Yamada 	/*
43169304379SMasahiro Yamada 	 * In the intended usage, the stdout is redirected to .*.cmd files.
43269304379SMasahiro Yamada 	 * Call ferror() to catch errors such as "No space left on device".
43369304379SMasahiro Yamada 	 */
43469304379SMasahiro Yamada 	if (ferror(stdout)) {
43569304379SMasahiro Yamada 		fprintf(stderr, "fixdep: not all data was written to the output\n");
43669304379SMasahiro Yamada 		exit(1);
43769304379SMasahiro Yamada 	}
43869304379SMasahiro Yamada 
4391da177e4SLinus Torvalds 	return 0;
4401da177e4SLinus Torvalds }
441