xref: /linux-6.15/scripts/basic/fixdep.c (revision bbda5ec6)
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 {
108*bbda5ec6SMasahiro Yamada 	fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
1091da177e4SLinus Torvalds 	exit(1);
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1124d99f93bSSam Ravnborg /*
113d8329e35SNicolas Pitre  * Print out a dependency path from a symbol name
114d8329e35SNicolas Pitre  */
115fbfa9be9SMasahiro Yamada static void print_dep(const char *m, int slen, const char *dir)
116d8329e35SNicolas Pitre {
117b3aa58d2SNicolas Pitre 	int c, prev_c = '/', i;
118d8329e35SNicolas Pitre 
119fbfa9be9SMasahiro Yamada 	printf("    $(wildcard %s/", dir);
120d8329e35SNicolas Pitre 	for (i = 0; i < slen; i++) {
121d8329e35SNicolas Pitre 		c = m[i];
122d8329e35SNicolas Pitre 		if (c == '_')
123d8329e35SNicolas Pitre 			c = '/';
124d8329e35SNicolas Pitre 		else
125d8329e35SNicolas Pitre 			c = tolower(c);
126b3aa58d2SNicolas Pitre 		if (c != '/' || prev_c != '/')
127d8329e35SNicolas Pitre 			putchar(c);
128b3aa58d2SNicolas Pitre 		prev_c = c;
129d8329e35SNicolas Pitre 	}
130d8329e35SNicolas Pitre 	printf(".h) \\\n");
131d8329e35SNicolas Pitre }
132d8329e35SNicolas Pitre 
1338af27e1dSEric Dumazet struct item {
1348af27e1dSEric Dumazet 	struct item	*next;
1358af27e1dSEric Dumazet 	unsigned int	len;
1368af27e1dSEric Dumazet 	unsigned int	hash;
1378af27e1dSEric Dumazet 	char		name[0];
1388af27e1dSEric Dumazet };
1391da177e4SLinus Torvalds 
1408af27e1dSEric Dumazet #define HASHSZ 256
1418af27e1dSEric Dumazet static struct item *hashtab[HASHSZ];
1428af27e1dSEric Dumazet 
1438af27e1dSEric Dumazet static unsigned int strhash(const char *str, unsigned int sz)
1441da177e4SLinus Torvalds {
1458af27e1dSEric Dumazet 	/* fnv32 hash */
1468af27e1dSEric Dumazet 	unsigned int i, hash = 2166136261U;
1471da177e4SLinus Torvalds 
1488af27e1dSEric Dumazet 	for (i = 0; i < sz; i++)
1498af27e1dSEric Dumazet 		hash = (hash ^ str[i]) * 0x01000193;
1508af27e1dSEric Dumazet 	return hash;
1518af27e1dSEric Dumazet }
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds /*
1541da177e4SLinus Torvalds  * Lookup a value in the configuration string.
1551da177e4SLinus Torvalds  */
1568af27e1dSEric Dumazet static int is_defined_config(const char *name, int len, unsigned int hash)
1571da177e4SLinus Torvalds {
1588af27e1dSEric Dumazet 	struct item *aux;
1598af27e1dSEric Dumazet 
1608af27e1dSEric Dumazet 	for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
1618af27e1dSEric Dumazet 		if (aux->hash == hash && aux->len == len &&
1628af27e1dSEric Dumazet 		    memcmp(aux->name, name, len) == 0)
1631da177e4SLinus Torvalds 			return 1;
1641da177e4SLinus Torvalds 	}
1651da177e4SLinus Torvalds 	return 0;
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds /*
1691da177e4SLinus Torvalds  * Add a new value to the configuration string.
1701da177e4SLinus Torvalds  */
1718af27e1dSEric Dumazet static void define_config(const char *name, int len, unsigned int hash)
1721da177e4SLinus Torvalds {
1738af27e1dSEric Dumazet 	struct item *aux = malloc(sizeof(*aux) + len);
1741da177e4SLinus Torvalds 
1758af27e1dSEric Dumazet 	if (!aux) {
1768af27e1dSEric Dumazet 		perror("fixdep:malloc");
1778af27e1dSEric Dumazet 		exit(1);
1788af27e1dSEric Dumazet 	}
1798af27e1dSEric Dumazet 	memcpy(aux->name, name, len);
1808af27e1dSEric Dumazet 	aux->len = len;
1818af27e1dSEric Dumazet 	aux->hash = hash;
1828af27e1dSEric Dumazet 	aux->next = hashtab[hash % HASHSZ];
1838af27e1dSEric Dumazet 	hashtab[hash % HASHSZ] = aux;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds /*
1871da177e4SLinus Torvalds  * Record the use of a CONFIG_* word.
1881da177e4SLinus Torvalds  */
1898af27e1dSEric Dumazet static void use_config(const char *m, int slen)
1901da177e4SLinus Torvalds {
1918af27e1dSEric Dumazet 	unsigned int hash = strhash(m, slen);
1921da177e4SLinus Torvalds 
1938af27e1dSEric Dumazet 	if (is_defined_config(m, slen, hash))
1941da177e4SLinus Torvalds 	    return;
1951da177e4SLinus Torvalds 
1968af27e1dSEric Dumazet 	define_config(m, slen, hash);
197fbfa9be9SMasahiro Yamada 	print_dep(m, slen, "include/config");
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
200d8329e35SNicolas Pitre /* test if s ends in sub */
20187b95a81SMasahiro Yamada static int str_ends_with(const char *s, int slen, const char *sub)
2021da177e4SLinus Torvalds {
2031da177e4SLinus Torvalds 	int sublen = strlen(sub);
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	if (sublen > slen)
20687b95a81SMasahiro Yamada 		return 0;
2071da177e4SLinus Torvalds 
20887b95a81SMasahiro Yamada 	return !memcmp(s + slen - sublen, sub, sublen);
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
211ab9ce9feSMasahiro Yamada static void parse_config_file(const char *p)
212ab9ce9feSMasahiro Yamada {
213ab9ce9feSMasahiro Yamada 	const char *q, *r;
2145b8ad96dSRasmus Villemoes 	const char *start = p;
215ab9ce9feSMasahiro Yamada 
216ab9ce9feSMasahiro Yamada 	while ((p = strstr(p, "CONFIG_"))) {
2175b8ad96dSRasmus Villemoes 		if (p > start && (isalnum(p[-1]) || p[-1] == '_')) {
2185b8ad96dSRasmus Villemoes 			p += 7;
2195b8ad96dSRasmus Villemoes 			continue;
2205b8ad96dSRasmus Villemoes 		}
221ab9ce9feSMasahiro Yamada 		p += 7;
222ab9ce9feSMasahiro Yamada 		q = p;
223ab9ce9feSMasahiro Yamada 		while (*q && (isalnum(*q) || *q == '_'))
224ab9ce9feSMasahiro Yamada 			q++;
225ab9ce9feSMasahiro Yamada 		if (str_ends_with(p, q - p, "_MODULE"))
226ab9ce9feSMasahiro Yamada 			r = q - 7;
227ab9ce9feSMasahiro Yamada 		else
228ab9ce9feSMasahiro Yamada 			r = q;
229ab9ce9feSMasahiro Yamada 		if (r > p)
230ab9ce9feSMasahiro Yamada 			use_config(p, r - p);
231ab9ce9feSMasahiro Yamada 		p = q;
232ab9ce9feSMasahiro Yamada 	}
233ab9ce9feSMasahiro Yamada }
234ab9ce9feSMasahiro Yamada 
2354003fd80SMasahiro Yamada static void *read_file(const char *filename)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	struct stat st;
2381da177e4SLinus Torvalds 	int fd;
2394003fd80SMasahiro Yamada 	char *buf;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	fd = open(filename, O_RDONLY);
2421da177e4SLinus Torvalds 	if (fd < 0) {
2434003fd80SMasahiro Yamada 		fprintf(stderr, "fixdep: error opening file: ");
2441da177e4SLinus Torvalds 		perror(filename);
2451da177e4SLinus Torvalds 		exit(2);
2461da177e4SLinus Torvalds 	}
24746fe94adSTom Rini 	if (fstat(fd, &st) < 0) {
2484003fd80SMasahiro Yamada 		fprintf(stderr, "fixdep: error fstat'ing file: ");
24946fe94adSTom Rini 		perror(filename);
25046fe94adSTom Rini 		exit(2);
25146fe94adSTom Rini 	}
2524003fd80SMasahiro Yamada 	buf = malloc(st.st_size + 1);
2534003fd80SMasahiro Yamada 	if (!buf) {
254dee81e98SAlexey Dobriyan 		perror("fixdep: malloc");
2557c2ec43aSLukas Bulwahn 		exit(2);
2561da177e4SLinus Torvalds 	}
2574003fd80SMasahiro Yamada 	if (read(fd, buf, st.st_size) != st.st_size) {
258dee81e98SAlexey Dobriyan 		perror("fixdep: read");
2597c2ec43aSLukas Bulwahn 		exit(2);
260dee81e98SAlexey Dobriyan 	}
2614003fd80SMasahiro Yamada 	buf[st.st_size] = '\0';
262dee81e98SAlexey Dobriyan 	close(fd);
263dee81e98SAlexey Dobriyan 
2644003fd80SMasahiro Yamada 	return buf;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
26787b95a81SMasahiro Yamada /* Ignore certain dependencies */
26887b95a81SMasahiro Yamada static int is_ignored_file(const char *s, int len)
26987b95a81SMasahiro Yamada {
27087b95a81SMasahiro Yamada 	return str_ends_with(s, len, "include/generated/autoconf.h") ||
27187b95a81SMasahiro Yamada 	       str_ends_with(s, len, "include/generated/autoksyms.h") ||
27287b95a81SMasahiro Yamada 	       str_ends_with(s, len, ".ver");
27387b95a81SMasahiro Yamada }
27487b95a81SMasahiro Yamada 
2757840fea2SMichal Marek /*
2767840fea2SMichal Marek  * Important: The below generated source_foo.o and deps_foo.o variable
2777840fea2SMichal Marek  * assignments are parsed not only by make, but also by the rather simple
2787840fea2SMichal Marek  * parser in scripts/mod/sumversion.c.
2797840fea2SMichal Marek  */
280*bbda5ec6SMasahiro Yamada static void parse_dep_file(char *m, const char *target)
2811da177e4SLinus Torvalds {
28248b9d03cSJ.A. Magallon 	char *p;
28301b5cbe7SMasahiro Yamada 	int is_last, is_target;
2842ab8a996SStephen Warren 	int saw_any_target = 0;
2852ab8a996SStephen Warren 	int is_first_dep = 0;
2864003fd80SMasahiro Yamada 	void *buf;
2871da177e4SLinus Torvalds 
28801b5cbe7SMasahiro Yamada 	while (1) {
2892ab8a996SStephen Warren 		/* Skip any "white space" */
29001b5cbe7SMasahiro Yamada 		while (*m == ' ' || *m == '\\' || *m == '\n')
2911da177e4SLinus Torvalds 			m++;
29201b5cbe7SMasahiro Yamada 
29301b5cbe7SMasahiro Yamada 		if (!*m)
29401b5cbe7SMasahiro Yamada 			break;
29501b5cbe7SMasahiro Yamada 
2962ab8a996SStephen Warren 		/* Find next "white space" */
2971da177e4SLinus Torvalds 		p = m;
29801b5cbe7SMasahiro Yamada 		while (*p && *p != ' ' && *p != '\\' && *p != '\n')
2991da177e4SLinus Torvalds 			p++;
30001b5cbe7SMasahiro Yamada 		is_last = (*p == '\0');
3012ab8a996SStephen Warren 		/* Is the token we found a target name? */
3022ab8a996SStephen Warren 		is_target = (*(p-1) == ':');
3032ab8a996SStephen Warren 		/* Don't write any target names into the dependency file */
3042ab8a996SStephen Warren 		if (is_target) {
3052ab8a996SStephen Warren 			/* The /next/ file is the first dependency */
3062ab8a996SStephen Warren 			is_first_dep = 1;
30787b95a81SMasahiro Yamada 		} else if (!is_ignored_file(m, p - m)) {
308ccfe7887SMasahiro Yamada 			*p = '\0';
3092ab8a996SStephen Warren 
310b7bd1821SMichal Marek 			/*
31187b95a81SMasahiro Yamada 			 * Do not list the source file as dependency, so that
31287b95a81SMasahiro Yamada 			 * kbuild is not confused if a .c file is rewritten
31387b95a81SMasahiro Yamada 			 * into .S or vice versa. Storing it in source_* is
31487b95a81SMasahiro Yamada 			 * needed for modpost to compute srcversions.
315b7bd1821SMichal Marek 			 */
3162ab8a996SStephen Warren 			if (is_first_dep) {
3172ab8a996SStephen Warren 				/*
31887b95a81SMasahiro Yamada 				 * If processing the concatenation of multiple
31987b95a81SMasahiro Yamada 				 * dependency files, only process the first
32087b95a81SMasahiro Yamada 				 * target name, which will be the original
32187b95a81SMasahiro Yamada 				 * source name, and ignore any other target
32287b95a81SMasahiro Yamada 				 * names, which will be intermediate temporary
3232ab8a996SStephen Warren 				 * files.
3242ab8a996SStephen Warren 				 */
3252ab8a996SStephen Warren 				if (!saw_any_target) {
3262ab8a996SStephen Warren 					saw_any_target = 1;
3272ab8a996SStephen Warren 					printf("source_%s := %s\n\n",
328ccfe7887SMasahiro Yamada 					       target, m);
32987b95a81SMasahiro Yamada 					printf("deps_%s := \\\n", target);
3302ab8a996SStephen Warren 				}
3312ab8a996SStephen Warren 				is_first_dep = 0;
33287b95a81SMasahiro Yamada 			} else {
333ccfe7887SMasahiro Yamada 				printf("  %s \\\n", m);
33487b95a81SMasahiro Yamada 			}
3354003fd80SMasahiro Yamada 
336ccfe7887SMasahiro Yamada 			buf = read_file(m);
3374003fd80SMasahiro Yamada 			parse_config_file(buf);
3384003fd80SMasahiro Yamada 			free(buf);
3391da177e4SLinus Torvalds 		}
34001b5cbe7SMasahiro Yamada 
34101b5cbe7SMasahiro Yamada 		if (is_last)
34201b5cbe7SMasahiro Yamada 			break;
34301b5cbe7SMasahiro Yamada 
3442ab8a996SStephen Warren 		/*
3452ab8a996SStephen Warren 		 * Start searching for next token immediately after the first
3462ab8a996SStephen Warren 		 * "whitespace" character that follows this token.
3472ab8a996SStephen Warren 		 */
3481da177e4SLinus Torvalds 		m = p + 1;
3491da177e4SLinus Torvalds 	}
3502ab8a996SStephen Warren 
3512ab8a996SStephen Warren 	if (!saw_any_target) {
3522ab8a996SStephen Warren 		fprintf(stderr, "fixdep: parse error; no targets found\n");
3532ab8a996SStephen Warren 		exit(1);
3542ab8a996SStephen Warren 	}
3552ab8a996SStephen Warren 
3561da177e4SLinus Torvalds 	printf("\n%s: $(deps_%s)\n\n", target, target);
3571da177e4SLinus Torvalds 	printf("$(deps_%s):\n", target);
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds int main(int argc, char *argv[])
3611da177e4SLinus Torvalds {
3625d1ef76fSMasahiro Yamada 	const char *depfile, *target, *cmdline;
3634003fd80SMasahiro Yamada 	void *buf;
3644003fd80SMasahiro Yamada 
365*bbda5ec6SMasahiro Yamada 	if (argc != 4)
3661da177e4SLinus Torvalds 		usage();
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	depfile = argv[1];
3691da177e4SLinus Torvalds 	target = argv[2];
3701da177e4SLinus Torvalds 	cmdline = argv[3];
3711da177e4SLinus Torvalds 
3725d1ef76fSMasahiro Yamada 	printf("cmd_%s := %s\n\n", target, cmdline);
3734003fd80SMasahiro Yamada 
3744003fd80SMasahiro Yamada 	buf = read_file(depfile);
375*bbda5ec6SMasahiro Yamada 	parse_dep_file(buf, target);
3764003fd80SMasahiro Yamada 	free(buf);
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	return 0;
3791da177e4SLinus Torvalds }
380