xref: /openbmc/linux/scripts/basic/fixdep.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
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  <kai.germaschewski@gmx.de>
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:mec@shout.net>.
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 
usage(void)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
116faa91c47SMasahiro Yamada static struct item *config_hashtab[HASHSZ], *file_hashtab[HASHSZ];
1178af27e1dSEric Dumazet 
strhash(const char * str,unsigned int sz)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  */
add_to_hashtable(const char * name,int len,unsigned int hash,struct item * hashtab[])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  */
in_hashtable(const char * name,int len,struct item * hashtab[])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  */
use_config(const char * m,int slen)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 */
str_ends_with(const char * s,int slen,const char * 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 
parse_config_file(const char * p)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 
read_file(const char * filename)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 */
is_ignored_file(const char * s,int len)24787b95a81SMasahiro Yamada static int is_ignored_file(const char *s, int len)
24887b95a81SMasahiro Yamada {
249*5e9e95ccSMasahiro Yamada 	return str_ends_with(s, len, "include/generated/autoconf.h");
25087b95a81SMasahiro Yamada }
25187b95a81SMasahiro Yamada 
25293c656deSMasahiro Yamada /* Do not parse these files */
is_no_parse_file(const char * s,int len)25393c656deSMasahiro Yamada static int is_no_parse_file(const char *s, int len)
25493c656deSMasahiro Yamada {
25593c656deSMasahiro Yamada 	/* rustc may list binary files in dep-info */
25693c656deSMasahiro Yamada 	return str_ends_with(s, len, ".rlib") ||
25793c656deSMasahiro Yamada 	       str_ends_with(s, len, ".rmeta") ||
25893c656deSMasahiro Yamada 	       str_ends_with(s, len, ".so");
25993c656deSMasahiro Yamada }
26093c656deSMasahiro Yamada 
2617840fea2SMichal Marek /*
2627840fea2SMichal Marek  * Important: The below generated source_foo.o and deps_foo.o variable
2637840fea2SMichal Marek  * assignments are parsed not only by make, but also by the rather simple
2647840fea2SMichal Marek  * parser in scripts/mod/sumversion.c.
2657840fea2SMichal Marek  */
parse_dep_file(char * p,const char * target)266bc6df812SMasahiro Yamada static void parse_dep_file(char *p, const char *target)
2671da177e4SLinus Torvalds {
268bc6df812SMasahiro Yamada 	bool saw_any_target = false;
269bc6df812SMasahiro Yamada 	bool is_target = true;
270bc6df812SMasahiro Yamada 	bool is_source = false;
271bc6df812SMasahiro Yamada 	bool need_parse;
272bc6df812SMasahiro Yamada 	char *q, saved_c;
2731da177e4SLinus Torvalds 
274bc6df812SMasahiro Yamada 	while (*p) {
275bc6df812SMasahiro Yamada 		/* handle some special characters first. */
276bc6df812SMasahiro Yamada 		switch (*p) {
277bc6df812SMasahiro Yamada 		case '#':
278bc6df812SMasahiro Yamada 			/*
279bc6df812SMasahiro Yamada 			 * skip comments.
280bc6df812SMasahiro Yamada 			 * rustc may emit comments to dep-info.
281bc6df812SMasahiro Yamada 			 */
282bc6df812SMasahiro Yamada 			p++;
283bc6df812SMasahiro Yamada 			while (*p != '\0' && *p != '\n') {
284bc6df812SMasahiro Yamada 				/*
285bc6df812SMasahiro Yamada 				 * escaped newlines continue the comment across
286bc6df812SMasahiro Yamada 				 * multiple lines.
287bc6df812SMasahiro Yamada 				 */
288bc6df812SMasahiro Yamada 				if (*p == '\\')
289bc6df812SMasahiro Yamada 					p++;
290bc6df812SMasahiro Yamada 				p++;
291bc6df812SMasahiro Yamada 			}
292bc6df812SMasahiro Yamada 			continue;
293bc6df812SMasahiro Yamada 		case ' ':
294bc6df812SMasahiro Yamada 		case '\t':
295bc6df812SMasahiro Yamada 			/* skip whitespaces */
296bc6df812SMasahiro Yamada 			p++;
297bc6df812SMasahiro Yamada 			continue;
298bc6df812SMasahiro Yamada 		case '\\':
299bc6df812SMasahiro Yamada 			/*
300bc6df812SMasahiro Yamada 			 * backslash/newline combinations continue the
301bc6df812SMasahiro Yamada 			 * statement. Skip it just like a whitespace.
302bc6df812SMasahiro Yamada 			 */
303bc6df812SMasahiro Yamada 			if (*(p + 1) == '\n') {
304bc6df812SMasahiro Yamada 				p += 2;
305bc6df812SMasahiro Yamada 				continue;
306bc6df812SMasahiro Yamada 			}
307bc6df812SMasahiro Yamada 			break;
308bc6df812SMasahiro Yamada 		case '\n':
309bc6df812SMasahiro Yamada 			/*
310bc6df812SMasahiro Yamada 			 * Makefiles use a line-based syntax, where the newline
311bc6df812SMasahiro Yamada 			 * is the end of a statement. After seeing a newline,
312bc6df812SMasahiro Yamada 			 * we expect the next token is a target.
313bc6df812SMasahiro Yamada 			 */
314bc6df812SMasahiro Yamada 			p++;
315bc6df812SMasahiro Yamada 			is_target = true;
316bc6df812SMasahiro Yamada 			continue;
317bc6df812SMasahiro Yamada 		case ':':
318bc6df812SMasahiro Yamada 			/*
319bc6df812SMasahiro Yamada 			 * assume the first dependency after a colon as the
320bc6df812SMasahiro Yamada 			 * source file.
321bc6df812SMasahiro Yamada 			 */
322bc6df812SMasahiro Yamada 			p++;
323bc6df812SMasahiro Yamada 			is_target = false;
324bc6df812SMasahiro Yamada 			is_source = true;
325bc6df812SMasahiro Yamada 			continue;
326bc6df812SMasahiro Yamada 		}
32701b5cbe7SMasahiro Yamada 
328bc6df812SMasahiro Yamada 		/* find the end of the token */
329bc6df812SMasahiro Yamada 		q = p;
330bc6df812SMasahiro Yamada 		while (*q != ' ' && *q != '\t' && *q != '\n' && *q != '#' && *q != ':') {
331bc6df812SMasahiro Yamada 			if (*q == '\\') {
332bc6df812SMasahiro Yamada 				/*
333bc6df812SMasahiro Yamada 				 * backslash/newline combinations work like as
334bc6df812SMasahiro Yamada 				 * a whitespace, so this is the end of token.
335bc6df812SMasahiro Yamada 				 */
336bc6df812SMasahiro Yamada 				if (*(q + 1) == '\n')
33701b5cbe7SMasahiro Yamada 					break;
33801b5cbe7SMasahiro Yamada 
339bc6df812SMasahiro Yamada 				/* escaped special characters */
340bc6df812SMasahiro Yamada 				if (*(q + 1) == '#' || *(q + 1) == ':') {
341bc6df812SMasahiro Yamada 					memmove(p + 1, p, q - p);
3421da177e4SLinus Torvalds 					p++;
343bc6df812SMasahiro Yamada 				}
344bc6df812SMasahiro Yamada 
345bc6df812SMasahiro Yamada 				q++;
346bc6df812SMasahiro Yamada 			}
347bc6df812SMasahiro Yamada 
348bc6df812SMasahiro Yamada 			if (*q == '\0')
349bc6df812SMasahiro Yamada 				break;
350bc6df812SMasahiro Yamada 			q++;
351bc6df812SMasahiro Yamada 		}
352bc6df812SMasahiro Yamada 
353bc6df812SMasahiro Yamada 		/* Just discard the target */
3542ab8a996SStephen Warren 		if (is_target) {
355bc6df812SMasahiro Yamada 			p = q;
356bc6df812SMasahiro Yamada 			continue;
357bc6df812SMasahiro Yamada 		}
358bc6df812SMasahiro Yamada 
359bc6df812SMasahiro Yamada 		saved_c = *q;
360bc6df812SMasahiro Yamada 		*q = '\0';
361bc6df812SMasahiro Yamada 		need_parse = false;
3622ab8a996SStephen Warren 
363b7bd1821SMichal Marek 		/*
364bc6df812SMasahiro Yamada 		 * Do not list the source file as dependency, so that kbuild is
365bc6df812SMasahiro Yamada 		 * not confused if a .c file is rewritten into .S or vice versa.
366bc6df812SMasahiro Yamada 		 * Storing it in source_* is needed for modpost to compute
367bc6df812SMasahiro Yamada 		 * srcversions.
368b7bd1821SMichal Marek 		 */
369bc6df812SMasahiro Yamada 		if (is_source) {
3702ab8a996SStephen Warren 			/*
371bc6df812SMasahiro Yamada 			 * The DT build rule concatenates multiple dep files.
372bc6df812SMasahiro Yamada 			 * When processing them, only process the first source
373bc6df812SMasahiro Yamada 			 * name, which will be the original one, and ignore any
374bc6df812SMasahiro Yamada 			 * other source names, which will be intermediate
375bc6df812SMasahiro Yamada 			 * temporary files.
376faa91c47SMasahiro Yamada 			 *
377faa91c47SMasahiro Yamada 			 * rustc emits the same dependency list for each
378faa91c47SMasahiro Yamada 			 * emission type. It is enough to list the source name
379faa91c47SMasahiro Yamada 			 * just once.
3802ab8a996SStephen Warren 			 */
3812ab8a996SStephen Warren 			if (!saw_any_target) {
382bc6df812SMasahiro Yamada 				saw_any_target = true;
383bc6df812SMasahiro Yamada 				printf("source_%s := %s\n\n", target, p);
38469304379SMasahiro Yamada 				printf("deps_%s := \\\n", target);
385bc6df812SMasahiro Yamada 				need_parse = true;
3862ab8a996SStephen Warren 			}
387faa91c47SMasahiro Yamada 		} else if (!is_ignored_file(p, q - p) &&
388faa91c47SMasahiro Yamada 			   !in_hashtable(p, q - p, file_hashtab)) {
389bc6df812SMasahiro Yamada 			printf("  %s \\\n", p);
390bc6df812SMasahiro Yamada 			need_parse = true;
39187b95a81SMasahiro Yamada 		}
3924003fd80SMasahiro Yamada 
39393c656deSMasahiro Yamada 		if (need_parse && !is_no_parse_file(p, q - p)) {
394bc6df812SMasahiro Yamada 			void *buf;
395bc6df812SMasahiro Yamada 
396bc6df812SMasahiro Yamada 			buf = read_file(p);
3974003fd80SMasahiro Yamada 			parse_config_file(buf);
3984003fd80SMasahiro Yamada 			free(buf);
3991da177e4SLinus Torvalds 		}
40001b5cbe7SMasahiro Yamada 
401bc6df812SMasahiro Yamada 		is_source = false;
402bc6df812SMasahiro Yamada 		*q = saved_c;
403bc6df812SMasahiro Yamada 		p = q;
4041da177e4SLinus Torvalds 	}
4052ab8a996SStephen Warren 
4062ab8a996SStephen Warren 	if (!saw_any_target) {
4072ab8a996SStephen Warren 		fprintf(stderr, "fixdep: parse error; no targets found\n");
4082ab8a996SStephen Warren 		exit(1);
4092ab8a996SStephen Warren 	}
4102ab8a996SStephen Warren 
41169304379SMasahiro Yamada 	printf("\n%s: $(deps_%s)\n\n", target, target);
41269304379SMasahiro Yamada 	printf("$(deps_%s):\n", target);
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
main(int argc,char * argv[])4151da177e4SLinus Torvalds int main(int argc, char *argv[])
4161da177e4SLinus Torvalds {
4175d1ef76fSMasahiro Yamada 	const char *depfile, *target, *cmdline;
4184003fd80SMasahiro Yamada 	void *buf;
4194003fd80SMasahiro Yamada 
420bbda5ec6SMasahiro Yamada 	if (argc != 4)
4211da177e4SLinus Torvalds 		usage();
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	depfile = argv[1];
4241da177e4SLinus Torvalds 	target = argv[2];
4251da177e4SLinus Torvalds 	cmdline = argv[3];
4261da177e4SLinus Torvalds 
42792215e7aSMasahiro Yamada 	printf("savedcmd_%s := %s\n\n", target, cmdline);
4284003fd80SMasahiro Yamada 
4294003fd80SMasahiro Yamada 	buf = read_file(depfile);
430bbda5ec6SMasahiro Yamada 	parse_dep_file(buf, target);
4314003fd80SMasahiro Yamada 	free(buf);
4321da177e4SLinus Torvalds 
43369304379SMasahiro Yamada 	fflush(stdout);
43469304379SMasahiro Yamada 
43569304379SMasahiro Yamada 	/*
43669304379SMasahiro Yamada 	 * In the intended usage, the stdout is redirected to .*.cmd files.
43769304379SMasahiro Yamada 	 * Call ferror() to catch errors such as "No space left on device".
43869304379SMasahiro Yamada 	 */
43969304379SMasahiro Yamada 	if (ferror(stdout)) {
44069304379SMasahiro Yamada 		fprintf(stderr, "fixdep: not all data was written to the output\n");
44169304379SMasahiro Yamada 		exit(1);
44269304379SMasahiro Yamada 	}
44369304379SMasahiro Yamada 
4441da177e4SLinus Torvalds 	return 0;
4451da177e4SLinus Torvalds }
446