Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * "Optimize" a list of dependencies as spit out by gcc -MD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * for the kernel build
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * ===========================================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author       Kai Germaschewski
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright    2002 by Kai Germaschewski  <kai.germaschewski@gmx.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * This software may be used and distributed according to the terms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * of the GNU General Public License, incorporated herein by reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * Introduction:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * gcc produces a very nice and correct list of dependencies which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * tells make when to remake a file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * To use this list as-is however has the drawback that virtually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * every file in the kernel includes autoconf.h.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * If the user re-runs make *config, autoconf.h will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * regenerated.  make notices that and will rebuild every file which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * includes autoconf.h, i.e. basically all files. This is extremely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * annoying if the user just changed CONFIG_HIS_DRIVER from n to m.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * So we play the same trick that "mkdep" played before. We replace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * the dependency on autoconf.h by a dependency on every config
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * option which is mentioned in any of the listed prerequisites.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * kconfig populates a tree in include/config/ with an empty file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * for each config symbol and when the configuration is updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * the files representing changed config options are touched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * which then let make pick up the changes and the files that use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * the config symbols are rebuilt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * So if the user changes his CONFIG_HIS_DRIVER option, only the objects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * which depend on "include/config/his/driver.h" will be rebuilt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * so most likely only his driver ;-)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * The idea above dates, by the way, back to Michael E Chastain, AFAIK.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * So to get dependencies right, there are two issues:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * o if any of the files the compiler read changed, we need to rebuild
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * o if the command line given to the compile the file changed, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  *   better rebuild as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * The former is handled by using the -MD output, the later by saving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * the command line used to compile the old object and comparing it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * to the one we would now use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * Again, also this idea is pretty old and has been discussed on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * kbuild-devel a long time ago. I don't have a sensibly working
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * internet connection right now, so I rather don't mention names
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * without double checking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * This code here has been based partially based on mkdep.c, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * says the following about its history:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  *   Copyright abandoned, Michael Chastain, <mailto:mec@shout.net>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  *   This is a C version of syncdep.pl by Werner Almesberger.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * It is invoked as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  *   fixdep <depfile> <target> <cmdline>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  * and will read the dependency file <depfile>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * The transformed dependency snipped is written to stdout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * It first generates a line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  *   cmd_<target> = <cmdline>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * and then basically copies the .<target>.d file to stdout, in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * process filtering out the dependency on autoconf.h and adding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * dependencies on include/config/my/option.h for every
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * CONFIG_MY_OPTION encountered in any of the prerequisites.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  * We don't even try to really parse the header files, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  * merely grep, i.e. if CONFIG_FOO is mentioned in a comment, it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  * be picked up as well. It's not a problem with respect to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * correctness, since that can only give too many dependencies, thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  * we cannot miss a rebuild. Since people tend to not mention totally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * unrelated CONFIG_ options all over the place, it's not an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * efficiency problem either.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * (Note: it'd be easy to port over the complete mkdep state machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  *  but I don't think the added complexity is worth it)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #include <ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static void usage(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)  * In the intended usage of this program, the stdout is redirected to .*.cmd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  * files. The return value of printf() and putchar() must be checked to catch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  * any error, e.g. "No space left on device".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static void xprintf(const char *format, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	va_list ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	va_start(ap, format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	ret = vprintf(format, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		perror("fixdep");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	va_end(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static void xputchar(int c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	ret = putchar(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (ret == EOF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		perror("fixdep");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  * Print out a dependency path from a symbol name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static void print_dep(const char *m, int slen, const char *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	int c, prev_c = '/', i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	xprintf("    $(wildcard %s/", dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	for (i = 0; i < slen; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		c = m[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		if (c == '_')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			c = '/';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			c = tolower(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		if (c != '/' || prev_c != '/')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			xputchar(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		prev_c = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	xprintf(".h) \\\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct item {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	struct item	*next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	unsigned int	len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	unsigned int	hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	char		name[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) #define HASHSZ 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static struct item *hashtab[HASHSZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static unsigned int strhash(const char *str, unsigned int sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	/* fnv32 hash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	unsigned int i, hash = 2166136261U;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	for (i = 0; i < sz; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		hash = (hash ^ str[i]) * 0x01000193;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	return hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  * Lookup a value in the configuration string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static int is_defined_config(const char *name, int len, unsigned int hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	struct item *aux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		if (aux->hash == hash && aux->len == len &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		    memcmp(aux->name, name, len) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  * Add a new value to the configuration string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static void define_config(const char *name, int len, unsigned int hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	struct item *aux = malloc(sizeof(*aux) + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (!aux) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		perror("fixdep:malloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	memcpy(aux->name, name, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	aux->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	aux->hash = hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	aux->next = hashtab[hash % HASHSZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	hashtab[hash % HASHSZ] = aux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)  * Record the use of a CONFIG_* word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static void use_config(const char *m, int slen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	unsigned int hash = strhash(m, slen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	if (is_defined_config(m, slen, hash))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	    return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	define_config(m, slen, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	print_dep(m, slen, "include/config");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /* test if s ends in sub */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static int str_ends_with(const char *s, int slen, const char *sub)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	int sublen = strlen(sub);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (sublen > slen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	return !memcmp(s + slen - sublen, sub, sublen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static void parse_config_file(const char *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	const char *q, *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	const char *start = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	while ((p = strstr(p, "CONFIG_"))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		if (p > start && (isalnum(p[-1]) || p[-1] == '_')) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			p += 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		p += 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		q = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		while (isalnum(*q) || *q == '_')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			q++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		if (str_ends_with(p, q - p, "_MODULE"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			r = q - 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			r = q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		if (r > p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			use_config(p, r - p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		p = q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static void *read_file(const char *filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	fd = open(filename, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		fprintf(stderr, "fixdep: error opening file: ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		perror(filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		exit(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (fstat(fd, &st) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		fprintf(stderr, "fixdep: error fstat'ing file: ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		perror(filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		exit(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	buf = malloc(st.st_size + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		perror("fixdep: malloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		exit(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (read(fd, buf, st.st_size) != st.st_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		perror("fixdep: read");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		exit(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	buf[st.st_size] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /* Ignore certain dependencies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static int is_ignored_file(const char *s, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	return str_ends_with(s, len, "include/generated/autoconf.h") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	       str_ends_with(s, len, "include/generated/autoksyms.h");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)  * Important: The below generated source_foo.o and deps_foo.o variable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)  * assignments are parsed not only by make, but also by the rather simple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)  * parser in scripts/mod/sumversion.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static void parse_dep_file(char *m, const char *target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	int is_last, is_target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	int saw_any_target = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	int is_first_dep = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		/* Skip any "white space" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		while (*m == ' ' || *m == '\\' || *m == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			m++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		if (!*m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		/* Find next "white space" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		p = m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		while (*p && *p != ' ' && *p != '\\' && *p != '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		is_last = (*p == '\0');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		/* Is the token we found a target name? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		is_target = (*(p-1) == ':');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		/* Don't write any target names into the dependency file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		if (is_target) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			/* The /next/ file is the first dependency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			is_first_dep = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		} else if (!is_ignored_file(m, p - m)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			*p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			 * Do not list the source file as dependency, so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			 * kbuild is not confused if a .c file is rewritten
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			 * into .S or vice versa. Storing it in source_* is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			 * needed for modpost to compute srcversions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			if (is_first_dep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 				 * If processing the concatenation of multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 				 * dependency files, only process the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 				 * target name, which will be the original
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 				 * source name, and ignore any other target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 				 * names, which will be intermediate temporary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 				 * files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 				if (!saw_any_target) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 					saw_any_target = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 					xprintf("source_%s := %s\n\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 						target, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 					xprintf("deps_%s := \\\n", target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 				is_first_dep = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 				xprintf("  %s \\\n", m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 			buf = read_file(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 			parse_config_file(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		if (is_last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		 * Start searching for next token immediately after the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		 * "whitespace" character that follows this token.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		m = p + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	if (!saw_any_target) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		fprintf(stderr, "fixdep: parse error; no targets found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	xprintf("\n%s: $(deps_%s)\n\n", target, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	xprintf("$(deps_%s):\n", target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	const char *depfile, *target, *cmdline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	if (argc != 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	depfile = argv[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	target = argv[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	cmdline = argv[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	xprintf("cmd_%s := %s\n\n", target, cmdline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	buf = read_file(depfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	parse_dep_file(buf, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	free(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }