fixdep.c 11.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
L
Linus Torvalds 已提交
28 29
 * option which is mentioned in any of the listed prequisites.
 *
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 37 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 if the user changes his CONFIG_HIS_DRIVER option, only the objects
 * which depend on "include/linux/config/his/driver.h" will be rebuilt,
 * 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
 * dependencies on include/config/my/option.h for every
 * CONFIG_MY_OPTION encountered in any of the prequisites.
 *
 * 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.
 *
 * The algorithm to grep for "CONFIG_..." is bit unusual, but should
 * be fast ;-) We don't even try to really parse the header files, but
 * 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)
 */
/*
 * Note 2: if somebody writes HELLO_CONFIG_BOOM in a file, it will depend onto
 * CONFIG_BOOM. This could seem a bug (not too hard to fix), but please do not
 * fix it! Some UserModeLinux files (look at arch/um/) call CONFIG_BOOM as
 * UML_CONFIG_BOOM, to avoid conflicts with /usr/include/linux/autoconf.h,
 * through arch/um/include/uml-config.h; this fixdep "bug" makes sure that
 * those files will have correct dependencies.
 */

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

#define INT_CONF ntohl(0x434f4e46)
#define INT_ONFI ntohl(0x4f4e4649)
#define INT_NFIG ntohl(0x4e464947)
#define INT_FIG_ ntohl(0x4649475f)

123
int insert_extra_deps;
L
Linus Torvalds 已提交
124 125 126 127
char *target;
char *depfile;
char *cmdline;

T
Trevor Keith 已提交
128
static void usage(void)
L
Linus Torvalds 已提交
129
{
130 131
	fprintf(stderr, "Usage: fixdep [-e] <depfile> <target> <cmdline>\n");
	fprintf(stderr, " -e  insert extra dependencies given on stdin\n");
L
Linus Torvalds 已提交
132 133 134
	exit(1);
}

135 136
/*
 * Print out the commandline prefixed with cmd_<target filename> :=
137
 */
T
Trevor Keith 已提交
138
static void print_cmdline(void)
L
Linus Torvalds 已提交
139
{
140
	printf("cmd_%s := %s\n\n", target, cmdline);
L
Linus Torvalds 已提交
141 142
}

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
/*
 * Print out a dependency path from a symbol name
 */
static void print_config(const char *m, int slen)
{
	int c, i;

	printf("    $(wildcard include/config/");
	for (i = 0; i < slen; i++) {
		c = m[i];
		if (c == '_')
			c = '/';
		else
			c = tolower(c);
		putchar(c);
	}
	printf(".h) \\\n");
}

static void do_extra_deps(void)
{
	if (insert_extra_deps) {
		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);
			}
			print_config(buf, len-1);
		}
	}
}

177 178 179 180 181 182
struct item {
	struct item	*next;
	unsigned int	len;
	unsigned int	hash;
	char		name[0];
};
L
Linus Torvalds 已提交
183

184 185
#define HASHSZ 256
static struct item *hashtab[HASHSZ];
L
Linus Torvalds 已提交
186

187 188 189 190
static unsigned int strhash(const char *str, unsigned int sz)
{
	/* fnv32 hash */
	unsigned int i, hash = 2166136261U;
L
Linus Torvalds 已提交
191

192 193 194 195
	for (i = 0; i < sz; i++)
		hash = (hash ^ str[i]) * 0x01000193;
	return hash;
}
L
Linus Torvalds 已提交
196 197 198 199

/*
 * Lookup a value in the configuration string.
 */
200
static int is_defined_config(const char *name, int len, unsigned int hash)
L
Linus Torvalds 已提交
201
{
202 203 204 205 206
	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 已提交
207 208 209 210 211 212 213 214
			return 1;
	}
	return 0;
}

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

219 220 221 222 223 224 225 226 227
	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 已提交
228 229 230 231 232
}

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

237
	if (is_defined_config(m, slen, hash))
L
Linus Torvalds 已提交
238 239
	    return;

240
	define_config(m, slen, hash);
241
	print_config(m, slen);
L
Linus Torvalds 已提交
242 243
}

244
static void parse_config_file(const char *map, size_t len)
L
Linus Torvalds 已提交
245
{
246
	const int *end = (const int *) (map + len);
L
Linus Torvalds 已提交
247
	/* start at +1, so that p can never be < map */
248 249
	const int *m   = (const int *) map + 1;
	const char *p, *q;
L
Linus Torvalds 已提交
250 251

	for (; m < end; m++) {
J
J.A. Magallon 已提交
252 253 254 255
		if (*m == INT_CONF) { p = (char *) m  ; goto conf; }
		if (*m == INT_ONFI) { p = (char *) m-1; goto conf; }
		if (*m == INT_NFIG) { p = (char *) m-2; goto conf; }
		if (*m == INT_FIG_) { p = (char *) m-3; goto conf; }
L
Linus Torvalds 已提交
256 257 258 259 260 261
		continue;
	conf:
		if (p > map + len - 7)
			continue;
		if (memcmp(p, "CONFIG_", 7))
			continue;
262 263
		p += 7;
		for (q = p; q < map + len; q++) {
L
Linus Torvalds 已提交
264 265 266 267 268 269
			if (!(isalnum(*q) || *q == '_'))
				goto found;
		}
		continue;

	found:
270 271
		if (!memcmp(q - 7, "_MODULE", 7))
			q -= 7;
272
		if (q - p < 0)
273
			continue;
274
		use_config(p, q - p);
L
Linus Torvalds 已提交
275 276 277
	}
}

278
/* test if s ends in sub */
N
Nicolas Iooss 已提交
279
static int strrcmp(const char *s, const char *sub)
L
Linus Torvalds 已提交
280 281 282 283 284 285 286 287 288 289
{
	int slen = strlen(s);
	int sublen = strlen(sub);

	if (sublen > slen)
		return 1;

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

290
static void do_config_file(const char *filename)
L
Linus Torvalds 已提交
291 292 293 294 295 296 297
{
	struct stat st;
	int fd;
	void *map;

	fd = open(filename, O_RDONLY);
	if (fd < 0) {
298
		fprintf(stderr, "fixdep: error opening config file: ");
L
Linus Torvalds 已提交
299 300 301
		perror(filename);
		exit(2);
	}
302 303 304 305 306
	if (fstat(fd, &st) < 0) {
		fprintf(stderr, "fixdep: error fstat'ing config file: ");
		perror(filename);
		exit(2);
	}
L
Linus Torvalds 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
	if (st.st_size == 0) {
		close(fd);
		return;
	}
	map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
	if ((long) map == -1) {
		perror("fixdep: mmap");
		close(fd);
		return;
	}

	parse_config_file(map, st.st_size);

	munmap(map, st.st_size);

	close(fd);
}

325 326 327 328 329
/*
 * 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.
 */
T
Trevor Keith 已提交
330
static void parse_dep_file(void *map, size_t len)
L
Linus Torvalds 已提交
331
{
J
J.A. Magallon 已提交
332 333 334
	char *m = map;
	char *end = m + len;
	char *p;
L
Linus Torvalds 已提交
335
	char s[PATH_MAX];
336 337 338
	int is_target;
	int saw_any_target = 0;
	int is_first_dep = 0;
L
Linus Torvalds 已提交
339 340

	while (m < end) {
341
		/* Skip any "white space" */
L
Linus Torvalds 已提交
342 343
		while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))
			m++;
344
		/* Find next "white space" */
L
Linus Torvalds 已提交
345
		p = m;
346
		while (p < end && *p != ' ' && *p != '\\' && *p != '\n')
L
Linus Torvalds 已提交
347
			p++;
348 349 350 351 352 353 354 355 356 357 358 359 360
		/* 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;
		} else {
			/* Save this token/filename */
			memcpy(s, m, p-m);
			s[p - m] = 0;

			/* Ignore certain dependencies */
			if (strrcmp(s, "include/generated/autoconf.h") &&
361
			    strrcmp(s, "include/generated/autoksyms.h") &&
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
			    strrcmp(s, "arch/um/include/uml-config.h") &&
			    strrcmp(s, "include/linux/kconfig.h") &&
			    strrcmp(s, ".ver")) {
				/*
				 * 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) {
					/*
					 * 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.
					 */
					if (!saw_any_target) {
						saw_any_target = 1;
						printf("source_%s := %s\n\n",
							target, s);
						printf("deps_%s := \\\n",
							target);
					}
					is_first_dep = 0;
				} else
					printf("  %s \\\n", s);
				do_config_file(s);
			}
L
Linus Torvalds 已提交
394
		}
395 396 397 398
		/*
		 * Start searching for next token immediately after the first
		 * "whitespace" character that follows this token.
		 */
L
Linus Torvalds 已提交
399 400
		m = p + 1;
	}
401 402 403 404 405 406

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

407 408
	do_extra_deps();

L
Linus Torvalds 已提交
409 410 411 412
	printf("\n%s: $(deps_%s)\n\n", target, target);
	printf("$(deps_%s):\n", target);
}

T
Trevor Keith 已提交
413
static void print_deps(void)
L
Linus Torvalds 已提交
414 415 416 417 418 419 420
{
	struct stat st;
	int fd;
	void *map;

	fd = open(depfile, O_RDONLY);
	if (fd < 0) {
421
		fprintf(stderr, "fixdep: error opening depfile: ");
L
Linus Torvalds 已提交
422 423 424
		perror(depfile);
		exit(2);
	}
425
	if (fstat(fd, &st) < 0) {
426 427 428 429
		fprintf(stderr, "fixdep: error fstat'ing depfile: ");
		perror(depfile);
		exit(2);
	}
L
Linus Torvalds 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
	if (st.st_size == 0) {
		fprintf(stderr,"fixdep: %s is empty\n",depfile);
		close(fd);
		return;
	}
	map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
	if ((long) map == -1) {
		perror("fixdep: mmap");
		close(fd);
		return;
	}

	parse_dep_file(map, st.st_size);

	munmap(map, st.st_size);

	close(fd);
}

T
Trevor Keith 已提交
449
static void traps(void)
L
Linus Torvalds 已提交
450 451
{
	static char test[] __attribute__((aligned(sizeof(int)))) = "CONF";
A
Amerigo Wang 已提交
452
	int *p = (int *)test;
L
Linus Torvalds 已提交
453

A
Amerigo Wang 已提交
454
	if (*p != INT_CONF) {
455
		fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianness? %#x\n",
A
Amerigo Wang 已提交
456
			*p);
L
Linus Torvalds 已提交
457 458 459 460 461 462 463 464
		exit(2);
	}
}

int main(int argc, char *argv[])
{
	traps();

465 466 467 468
	if (argc == 5 && !strcmp(argv[1], "-e")) {
		insert_extra_deps = 1;
		argv++;
	} else if (argc != 4)
L
Linus Torvalds 已提交
469 470 471 472 473 474 475 476 477 478 479
		usage();

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

	print_cmdline();
	print_deps();

	return 0;
}