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