fixdep.c 9.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * "Optimize" a list of dependencies as spit out by gcc -MD
 * for the kernel build
 * ===========================================================================
 *
 * Author       Kai Germaschewski
 * Copyright    2002 by Kai Germaschewski  <kai.germaschewski@gmx.de>
 *
 * This software may be used and distributed according to the terms
 * of the GNU General Public License, incorporated herein by reference.
 *
 *
 * Introduction:
 *
 * gcc produces a very nice and correct list of dependencies which
 * tells make when to remake a file.
 *
 * To use this list as-is however has the drawback that virtually
19
 * every file in the kernel includes autoconf.h.
L
Linus Torvalds 已提交
20
 *
21
 * If the user re-runs make *config, autoconf.h will be
L
Linus Torvalds 已提交
22 23 24 25 26
 * regenerated.  make notices that and will rebuild every file which
 * includes autoconf.h, i.e. basically all files. This is extremely
 * annoying if the user just changed CONFIG_HIS_DRIVER from n to m.
 *
 * 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.
L
Linus Torvalds 已提交
29
 *
30 31 32 33 34
 * kconfig populates a tree in include/config/ with an empty file
 * for each config symbol and when the configuration is updated
 * the files representing changed config options are touched
 * which then let make pick up the changes and the files that use
 * the config symbols are rebuilt.
L
Linus Torvalds 已提交
35 36
 *
 * So if the user changes his CONFIG_HIS_DRIVER option, only the objects
37
 * which depend on "include/config/his/driver.h" will be rebuilt,
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
 * so most likely only his driver ;-)
 *
 * The idea above dates, by the way, back to Michael E Chastain, AFAIK.
 *
 * So to get dependencies right, there are two issues:
 * o if any of the files the compiler read changed, we need to rebuild
 * o if the command line given to the compile the file changed, we
 *   better rebuild as well.
 *
 * The former is handled by using the -MD output, the later by saving
 * the command line used to compile the old object and comparing it
 * to the one we would now use.
 *
 * Again, also this idea is pretty old and has been discussed on
 * kbuild-devel a long time ago. I don't have a sensibly working
 * internet connection right now, so I rather don't mention names
 * without double checking.
 *
 * This code here has been based partially based on mkdep.c, which
 * says the following about its history:
 *
 *   Copyright abandoned, Michael Chastain, <mailto:mec@shout.net>.
 *   This is a C version of syncdep.pl by Werner Almesberger.
 *
 *
 * It is invoked as
 *
 *   fixdep <depfile> <target> <cmdline>
 *
 * and will read the dependency file <depfile>
 *
 * The transformed dependency snipped is written to stdout.
 *
 * It first generates a line
 *
 *   cmd_<target> = <cmdline>
 *
 * and then basically copies the .<target>.d file to stdout, in the
76
 * process filtering out the dependency on autoconf.h and adding
L
Linus Torvalds 已提交
77
 * dependencies on include/config/my/option.h for every
78
 * CONFIG_MY_OPTION encountered in any of the prerequisites.
L
Linus Torvalds 已提交
79 80 81 82 83 84
 *
 * It will also filter out all the dependencies on *.ver. We need
 * to make sure that the generated version checksum are globally up
 * to date before even starting the recursive build, so it's too late
 * at this point anyway.
 *
A
Alexey Dobriyan 已提交
85
 * We don't even try to really parse the header files, but
L
Linus Torvalds 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
 * merely grep, i.e. if CONFIG_FOO is mentioned in a comment, it will
 * be picked up as well. It's not a problem with respect to
 * correctness, since that can only give too many dependencies, thus
 * we cannot miss a rebuild. Since people tend to not mention totally
 * unrelated CONFIG_ options all over the place, it's not an
 * efficiency problem either.
 *
 * (Note: it'd be easy to port over the complete mkdep state machine,
 *  but I don't think the added complexity is worth it)
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

T
Trevor Keith 已提交
106
static void usage(void)
L
Linus Torvalds 已提交
107
{
108 109
	fprintf(stderr, "Usage: fixdep [-e] <depfile> <target> <cmdline>\n");
	fprintf(stderr, " -e  insert extra dependencies given on stdin\n");
L
Linus Torvalds 已提交
110 111 112
	exit(1);
}

113 114 115
/*
 * Print out a dependency path from a symbol name
 */
116
static void print_dep(const char *m, int slen, const char *dir)
117
{
118
	int c, prev_c = '/', i;
119

120
	printf("    $(wildcard %s/", dir);
121 122 123 124 125 126
	for (i = 0; i < slen; i++) {
		c = m[i];
		if (c == '_')
			c = '/';
		else
			c = tolower(c);
127 128 129
		if (c != '/' || prev_c != '/')
			putchar(c);
		prev_c = c;
130 131 132 133 134 135
	}
	printf(".h) \\\n");
}

static void do_extra_deps(void)
{
136 137 138 139 140 141 142 143
	char buf[80];

	while (fgets(buf, sizeof(buf), stdin)) {
		int len = strlen(buf);

		if (len < 2 || buf[len - 1] != '\n') {
			fprintf(stderr, "fixdep: bad data on stdin\n");
			exit(1);
144
		}
145
		print_dep(buf, len - 1, "include/ksym");
146 147 148
	}
}

149 150 151 152 153 154
struct item {
	struct item	*next;
	unsigned int	len;
	unsigned int	hash;
	char		name[0];
};
L
Linus Torvalds 已提交
155

156 157
#define HASHSZ 256
static struct item *hashtab[HASHSZ];
L
Linus Torvalds 已提交
158

159 160 161 162
static unsigned int strhash(const char *str, unsigned int sz)
{
	/* fnv32 hash */
	unsigned int i, hash = 2166136261U;
L
Linus Torvalds 已提交
163

164 165 166 167
	for (i = 0; i < sz; i++)
		hash = (hash ^ str[i]) * 0x01000193;
	return hash;
}
L
Linus Torvalds 已提交
168 169 170 171

/*
 * Lookup a value in the configuration string.
 */
172
static int is_defined_config(const char *name, int len, unsigned int hash)
L
Linus Torvalds 已提交
173
{
174 175 176 177 178
	struct item *aux;

	for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
		if (aux->hash == hash && aux->len == len &&
		    memcmp(aux->name, name, len) == 0)
L
Linus Torvalds 已提交
179 180 181 182 183 184 185 186
			return 1;
	}
	return 0;
}

/*
 * Add a new value to the configuration string.
 */
187
static void define_config(const char *name, int len, unsigned int hash)
L
Linus Torvalds 已提交
188
{
189
	struct item *aux = malloc(sizeof(*aux) + len);
L
Linus Torvalds 已提交
190

191 192 193 194 195 196 197 198 199
	if (!aux) {
		perror("fixdep:malloc");
		exit(1);
	}
	memcpy(aux->name, name, len);
	aux->len = len;
	aux->hash = hash;
	aux->next = hashtab[hash % HASHSZ];
	hashtab[hash % HASHSZ] = aux;
L
Linus Torvalds 已提交
200 201 202 203 204
}

/*
 * Record the use of a CONFIG_* word.
 */
205
static void use_config(const char *m, int slen)
L
Linus Torvalds 已提交
206
{
207
	unsigned int hash = strhash(m, slen);
L
Linus Torvalds 已提交
208

209
	if (is_defined_config(m, slen, hash))
L
Linus Torvalds 已提交
210 211
	    return;

212
	define_config(m, slen, hash);
213
	print_dep(m, slen, "include/config");
L
Linus Torvalds 已提交
214 215
}

216 217 218 219 220 221 222 223 224 225 226
/* test if s ends in sub */
static int str_ends_with(const char *s, int slen, const char *sub)
{
	int sublen = strlen(sub);

	if (sublen > slen)
		return 0;

	return !memcmp(s + slen - sublen, sub, sublen);
}

A
Alexey Dobriyan 已提交
227
static void parse_config_file(const char *p)
L
Linus Torvalds 已提交
228
{
A
Alexey Dobriyan 已提交
229
	const char *q, *r;
230
	const char *start = p;
A
Alexey Dobriyan 已提交
231 232

	while ((p = strstr(p, "CONFIG_"))) {
233 234 235 236
		if (p > start && (isalnum(p[-1]) || p[-1] == '_')) {
			p += 7;
			continue;
		}
237
		p += 7;
A
Alexey Dobriyan 已提交
238 239 240
		q = p;
		while (*q && (isalnum(*q) || *q == '_'))
			q++;
241
		if (str_ends_with(p, q - p, "_MODULE"))
A
Alexey Dobriyan 已提交
242 243 244 245 246 247
			r = q - 7;
		else
			r = q;
		if (r > p)
			use_config(p, r - p);
		p = q;
L
Linus Torvalds 已提交
248 249 250
	}
}

251
static void *read_file(const char *filename)
L
Linus Torvalds 已提交
252 253 254
{
	struct stat st;
	int fd;
255
	char *buf;
L
Linus Torvalds 已提交
256 257 258

	fd = open(filename, O_RDONLY);
	if (fd < 0) {
259
		fprintf(stderr, "fixdep: error opening file: ");
L
Linus Torvalds 已提交
260 261 262
		perror(filename);
		exit(2);
	}
263
	if (fstat(fd, &st) < 0) {
264
		fprintf(stderr, "fixdep: error fstat'ing file: ");
265 266 267
		perror(filename);
		exit(2);
	}
268 269
	buf = malloc(st.st_size + 1);
	if (!buf) {
A
Alexey Dobriyan 已提交
270
		perror("fixdep: malloc");
271
		exit(2);
L
Linus Torvalds 已提交
272
	}
273
	if (read(fd, buf, st.st_size) != st.st_size) {
A
Alexey Dobriyan 已提交
274
		perror("fixdep: read");
275
		exit(2);
A
Alexey Dobriyan 已提交
276
	}
277
	buf[st.st_size] = '\0';
A
Alexey Dobriyan 已提交
278
	close(fd);
L
Linus Torvalds 已提交
279

280
	return buf;
L
Linus Torvalds 已提交
281 282
}

283 284 285 286 287 288 289 290
/* Ignore certain dependencies */
static int is_ignored_file(const char *s, int len)
{
	return str_ends_with(s, len, "include/generated/autoconf.h") ||
	       str_ends_with(s, len, "include/generated/autoksyms.h") ||
	       str_ends_with(s, len, ".ver");
}

291 292 293 294 295
/*
 * Important: The below generated source_foo.o and deps_foo.o variable
 * assignments are parsed not only by make, but also by the rather simple
 * parser in scripts/mod/sumversion.c.
 */
296
static void parse_dep_file(char *m, const char *target, int insert_extra_deps)
L
Linus Torvalds 已提交
297
{
J
J.A. Magallon 已提交
298
	char *p;
299
	int is_last, is_target;
300 301
	int saw_any_target = 0;
	int is_first_dep = 0;
302
	void *buf;
L
Linus Torvalds 已提交
303

304
	while (1) {
305
		/* Skip any "white space" */
306
		while (*m == ' ' || *m == '\\' || *m == '\n')
L
Linus Torvalds 已提交
307
			m++;
308 309 310 311

		if (!*m)
			break;

312
		/* Find next "white space" */
L
Linus Torvalds 已提交
313
		p = m;
314
		while (*p && *p != ' ' && *p != '\\' && *p != '\n')
L
Linus Torvalds 已提交
315
			p++;
316
		is_last = (*p == '\0');
317 318 319 320 321 322
		/* Is the token we found a target name? */
		is_target = (*(p-1) == ':');
		/* Don't write any target names into the dependency file */
		if (is_target) {
			/* The /next/ file is the first dependency */
			is_first_dep = 1;
323
		} else if (!is_ignored_file(m, p - m)) {
324
			*p = '\0';
325

326 327 328 329 330 331 332
			/*
			 * Do not list the source file as dependency, so that
			 * kbuild is not confused if a .c file is rewritten
			 * into .S or vice versa. Storing it in source_* is
			 * needed for modpost to compute srcversions.
			 */
			if (is_first_dep) {
333
				/*
334 335 336 337 338 339
				 * If processing the concatenation of multiple
				 * dependency files, only process the first
				 * target name, which will be the original
				 * source name, and ignore any other target
				 * names, which will be intermediate temporary
				 * files.
340
				 */
341 342 343 344 345 346 347 348 349
				if (!saw_any_target) {
					saw_any_target = 1;
					printf("source_%s := %s\n\n",
					       target, m);
					printf("deps_%s := \\\n", target);
				}
				is_first_dep = 0;
			} else {
				printf("  %s \\\n", m);
350
			}
351 352 353 354

			buf = read_file(m);
			parse_config_file(buf);
			free(buf);
L
Linus Torvalds 已提交
355
		}
356 357 358 359

		if (is_last)
			break;

360 361 362 363
		/*
		 * Start searching for next token immediately after the first
		 * "whitespace" character that follows this token.
		 */
L
Linus Torvalds 已提交
364 365
		m = p + 1;
	}
366 367 368 369 370 371

	if (!saw_any_target) {
		fprintf(stderr, "fixdep: parse error; no targets found\n");
		exit(1);
	}

372 373
	if (insert_extra_deps)
		do_extra_deps();
374

L
Linus Torvalds 已提交
375 376 377 378 379 380
	printf("\n%s: $(deps_%s)\n\n", target, target);
	printf("$(deps_%s):\n", target);
}

int main(int argc, char *argv[])
{
381 382
	const char *depfile, *target, *cmdline;
	int insert_extra_deps = 0;
383 384
	void *buf;

385 386 387 388
	if (argc == 5 && !strcmp(argv[1], "-e")) {
		insert_extra_deps = 1;
		argv++;
	} else if (argc != 4)
L
Linus Torvalds 已提交
389 390 391 392 393 394
		usage();

	depfile = argv[1];
	target = argv[2];
	cmdline = argv[3];

395
	printf("cmd_%s := %s\n\n", target, cmdline);
396 397

	buf = read_file(depfile);
398
	parse_dep_file(buf, target, insert_extra_deps);
399
	free(buf);
L
Linus Torvalds 已提交
400 401 402

	return 0;
}