blob: b9b4bbf4e8dd7cbb1234b2243557c42e75c04e67 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Sam Ravnborg264a2682009-10-18 00:49:24 +020019 * every file in the kernel includes autoconf.h.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 *
Sam Ravnborg264a2682009-10-18 00:49:24 +020021 * If the user re-runs make *config, autoconf.h will be
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * 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
Sam Ravnborg264a2682009-10-18 00:49:24 +020027 * the dependency on autoconf.h by a dependency on every config
Cao jin4e433fc2017-08-08 21:20:50 +080028 * option which is mentioned in any of the listed prerequisites.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 *
Jan Beulichc21b1e42007-03-29 10:27:14 +010030 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 *
36 * So if the user changes his CONFIG_HIS_DRIVER option, only the objects
Cao jin4e433fc2017-08-08 21:20:50 +080037 * which depend on "include/config/his/driver.h" will be rebuilt,
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * 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 * cmd_<target> = <cmdline>
74 *
75 * and then basically copies the .<target>.d file to stdout, in the
Sam Ravnborg264a2682009-10-18 00:49:24 +020076 * process filtering out the dependency on autoconf.h and adding
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * dependencies on include/config/my/option.h for every
Cao jin4e433fc2017-08-08 21:20:50 +080078 * CONFIG_MY_OPTION encountered in any of the prerequisites.
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
80 * It will also filter out all the dependencies on *.ver. We need
81 * to make sure that the generated version checksum are globally up
82 * to date before even starting the recursive build, so it's too late
83 * at this point anyway.
84 *
Alexey Dobriyandee81e92016-08-24 21:03:05 +030085 * We don't even try to really parse the header files, but
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * merely grep, i.e. if CONFIG_FOO is mentioned in a comment, it will
87 * be picked up as well. It's not a problem with respect to
88 * correctness, since that can only give too many dependencies, thus
89 * we cannot miss a rebuild. Since people tend to not mention totally
90 * unrelated CONFIG_ options all over the place, it's not an
91 * efficiency problem either.
92 *
93 * (Note: it'd be easy to port over the complete mkdep state machine,
94 * but I don't think the added complexity is worth it)
95 */
96/*
97 * Note 2: if somebody writes HELLO_CONFIG_BOOM in a file, it will depend onto
98 * CONFIG_BOOM. This could seem a bug (not too hard to fix), but please do not
99 * fix it! Some UserModeLinux files (look at arch/um/) call CONFIG_BOOM as
100 * UML_CONFIG_BOOM, to avoid conflicts with /usr/include/linux/autoconf.h,
101 * through arch/um/include/uml-config.h; this fixdep "bug" makes sure that
102 * those files will have correct dependencies.
103 */
104
105#include <sys/types.h>
106#include <sys/stat.h>
107#include <sys/mman.h>
108#include <unistd.h>
109#include <fcntl.h>
110#include <string.h>
111#include <stdlib.h>
112#include <stdio.h>
113#include <limits.h>
114#include <ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Nicolas Pitred8329e32016-02-12 15:00:50 -0500116int insert_extra_deps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117char *target;
118char *depfile;
119char *cmdline;
120
Trevor Keith4356f482009-09-18 12:49:23 -0700121static void usage(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Nicolas Pitred8329e32016-02-12 15:00:50 -0500123 fprintf(stderr, "Usage: fixdep [-e] <depfile> <target> <cmdline>\n");
124 fprintf(stderr, " -e insert extra dependencies given on stdin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 exit(1);
126}
127
Sam Ravnborg4d99f932005-12-25 23:21:14 +0100128/*
129 * Print out the commandline prefixed with cmd_<target filename> :=
Jan Beulich6176aa92006-01-30 10:04:27 +0100130 */
Trevor Keith4356f482009-09-18 12:49:23 -0700131static void print_cmdline(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Jan Beulich6176aa92006-01-30 10:04:27 +0100133 printf("cmd_%s := %s\n\n", target, cmdline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Nicolas Pitred8329e32016-02-12 15:00:50 -0500136/*
137 * Print out a dependency path from a symbol name
138 */
139static void print_config(const char *m, int slen)
140{
141 int c, i;
142
143 printf(" $(wildcard include/config/");
144 for (i = 0; i < slen; i++) {
145 c = m[i];
146 if (c == '_')
147 c = '/';
148 else
149 c = tolower(c);
150 putchar(c);
151 }
152 printf(".h) \\\n");
153}
154
155static void do_extra_deps(void)
156{
157 if (insert_extra_deps) {
158 char buf[80];
159 while(fgets(buf, sizeof(buf), stdin)) {
160 int len = strlen(buf);
161 if (len < 2 || buf[len-1] != '\n') {
162 fprintf(stderr, "fixdep: bad data on stdin\n");
163 exit(1);
164 }
165 print_config(buf, len-1);
166 }
167 }
168}
169
Eric Dumazet8af27e12010-11-09 16:29:27 +0100170struct item {
171 struct item *next;
172 unsigned int len;
173 unsigned int hash;
174 char name[0];
175};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Eric Dumazet8af27e12010-11-09 16:29:27 +0100177#define HASHSZ 256
178static struct item *hashtab[HASHSZ];
179
180static unsigned int strhash(const char *str, unsigned int sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Eric Dumazet8af27e12010-11-09 16:29:27 +0100182 /* fnv32 hash */
183 unsigned int i, hash = 2166136261U;
184
185 for (i = 0; i < sz; i++)
186 hash = (hash ^ str[i]) * 0x01000193;
187 return hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
191 * Lookup a value in the configuration string.
192 */
Eric Dumazet8af27e12010-11-09 16:29:27 +0100193static int is_defined_config(const char *name, int len, unsigned int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Eric Dumazet8af27e12010-11-09 16:29:27 +0100195 struct item *aux;
196
197 for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
198 if (aux->hash == hash && aux->len == len &&
199 memcmp(aux->name, name, len) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return 1;
201 }
202 return 0;
203}
204
205/*
206 * Add a new value to the configuration string.
207 */
Eric Dumazet8af27e12010-11-09 16:29:27 +0100208static void define_config(const char *name, int len, unsigned int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Eric Dumazet8af27e12010-11-09 16:29:27 +0100210 struct item *aux = malloc(sizeof(*aux) + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Eric Dumazet8af27e12010-11-09 16:29:27 +0100212 if (!aux) {
213 perror("fixdep:malloc");
214 exit(1);
215 }
216 memcpy(aux->name, name, len);
217 aux->len = len;
218 aux->hash = hash;
219 aux->next = hashtab[hash % HASHSZ];
220 hashtab[hash % HASHSZ] = aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * Record the use of a CONFIG_* word.
225 */
Eric Dumazet8af27e12010-11-09 16:29:27 +0100226static void use_config(const char *m, int slen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Eric Dumazet8af27e12010-11-09 16:29:27 +0100228 unsigned int hash = strhash(m, slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Eric Dumazet8af27e12010-11-09 16:29:27 +0100230 if (is_defined_config(m, slen, hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return;
232
Eric Dumazet8af27e12010-11-09 16:29:27 +0100233 define_config(m, slen, hash);
Nicolas Pitred8329e32016-02-12 15:00:50 -0500234 print_config(m, slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Alexey Dobriyandee81e92016-08-24 21:03:05 +0300237static void parse_config_file(const char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Alexey Dobriyandee81e92016-08-24 21:03:05 +0300239 const char *q, *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Alexey Dobriyandee81e92016-08-24 21:03:05 +0300241 while ((p = strstr(p, "CONFIG_"))) {
Masahiro Yamadad7211092015-07-24 14:18:45 +0900242 p += 7;
Alexey Dobriyandee81e92016-08-24 21:03:05 +0300243 q = p;
244 while (*q && (isalnum(*q) || *q == '_'))
245 q++;
246 if (memcmp(q - 7, "_MODULE", 7) == 0)
247 r = q - 7;
248 else
249 r = q;
250 if (r > p)
251 use_config(p, r - p);
252 p = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254}
255
Nicolas Pitred8329e32016-02-12 15:00:50 -0500256/* test if s ends in sub */
Nicolas Iooss4c835b52015-11-18 19:07:15 +0100257static int strrcmp(const char *s, const char *sub)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 int slen = strlen(s);
260 int sublen = strlen(sub);
261
262 if (sublen > slen)
263 return 1;
264
265 return memcmp(s + slen - sublen, sub, sublen);
266}
267
Eric Dumazet8af27e12010-11-09 16:29:27 +0100268static void do_config_file(const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 struct stat st;
271 int fd;
Alexey Dobriyandee81e92016-08-24 21:03:05 +0300272 char *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 fd = open(filename, O_RDONLY);
275 if (fd < 0) {
Ben Gamaria3ba8112010-12-22 13:30:14 -0500276 fprintf(stderr, "fixdep: error opening config file: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 perror(filename);
278 exit(2);
279 }
Tom Rini46fe94a2015-12-07 16:26:08 -0500280 if (fstat(fd, &st) < 0) {
281 fprintf(stderr, "fixdep: error fstat'ing config file: ");
282 perror(filename);
283 exit(2);
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (st.st_size == 0) {
286 close(fd);
287 return;
288 }
Alexey Dobriyandee81e92016-08-24 21:03:05 +0300289 map = malloc(st.st_size + 1);
290 if (!map) {
291 perror("fixdep: malloc");
Lukas Bulwahn7c2ec432018-01-08 11:04:01 +0100292 exit(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Alexey Dobriyandee81e92016-08-24 21:03:05 +0300294 if (read(fd, map, st.st_size) != st.st_size) {
295 perror("fixdep: read");
Lukas Bulwahn7c2ec432018-01-08 11:04:01 +0100296 exit(2);
Alexey Dobriyandee81e92016-08-24 21:03:05 +0300297 }
298 map[st.st_size] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 close(fd);
Alexey Dobriyandee81e92016-08-24 21:03:05 +0300300
301 parse_config_file(map);
302
303 free(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Michal Marek7840fea2011-03-11 22:34:47 +0100306/*
307 * Important: The below generated source_foo.o and deps_foo.o variable
308 * assignments are parsed not only by make, but also by the rather simple
309 * parser in scripts/mod/sumversion.c.
310 */
Trevor Keith4356f482009-09-18 12:49:23 -0700311static void parse_dep_file(void *map, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
J.A. Magallon48b9d032005-06-25 14:59:22 -0700313 char *m = map;
314 char *end = m + len;
315 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 char s[PATH_MAX];
Stephen Warren2ab8a992013-03-06 10:27:45 -0700317 int is_target;
318 int saw_any_target = 0;
319 int is_first_dep = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 while (m < end) {
Stephen Warren2ab8a992013-03-06 10:27:45 -0700322 /* Skip any "white space" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))
324 m++;
Stephen Warren2ab8a992013-03-06 10:27:45 -0700325 /* Find next "white space" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 p = m;
Stephen Warren2ab8a992013-03-06 10:27:45 -0700327 while (p < end && *p != ' ' && *p != '\\' && *p != '\n')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 p++;
Stephen Warren2ab8a992013-03-06 10:27:45 -0700329 /* Is the token we found a target name? */
330 is_target = (*(p-1) == ':');
331 /* Don't write any target names into the dependency file */
332 if (is_target) {
333 /* The /next/ file is the first dependency */
334 is_first_dep = 1;
335 } else {
336 /* Save this token/filename */
337 memcpy(s, m, p-m);
338 s[p - m] = 0;
339
340 /* Ignore certain dependencies */
341 if (strrcmp(s, "include/generated/autoconf.h") &&
Nicolas Pitrec1a95fd2016-01-22 13:41:57 -0500342 strrcmp(s, "include/generated/autoksyms.h") &&
Stephen Warren2ab8a992013-03-06 10:27:45 -0700343 strrcmp(s, "arch/um/include/uml-config.h") &&
344 strrcmp(s, "include/linux/kconfig.h") &&
345 strrcmp(s, ".ver")) {
346 /*
347 * Do not list the source file as dependency,
348 * so that kbuild is not confused if a .c file
349 * is rewritten into .S or vice versa. Storing
350 * it in source_* is needed for modpost to
351 * compute srcversions.
352 */
353 if (is_first_dep) {
354 /*
355 * If processing the concatenation of
356 * multiple dependency files, only
357 * process the first target name, which
358 * will be the original source name,
359 * and ignore any other target names,
360 * which will be intermediate temporary
361 * files.
362 */
363 if (!saw_any_target) {
364 saw_any_target = 1;
365 printf("source_%s := %s\n\n",
366 target, s);
367 printf("deps_%s := \\\n",
368 target);
369 }
370 is_first_dep = 0;
371 } else
372 printf(" %s \\\n", s);
373 do_config_file(s);
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Stephen Warren2ab8a992013-03-06 10:27:45 -0700376 /*
377 * Start searching for next token immediately after the first
378 * "whitespace" character that follows this token.
379 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 m = p + 1;
381 }
Stephen Warren2ab8a992013-03-06 10:27:45 -0700382
383 if (!saw_any_target) {
384 fprintf(stderr, "fixdep: parse error; no targets found\n");
385 exit(1);
386 }
387
Nicolas Pitred8329e32016-02-12 15:00:50 -0500388 do_extra_deps();
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 printf("\n%s: $(deps_%s)\n\n", target, target);
391 printf("$(deps_%s):\n", target);
392}
393
Trevor Keith4356f482009-09-18 12:49:23 -0700394static void print_deps(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 struct stat st;
397 int fd;
398 void *map;
399
400 fd = open(depfile, O_RDONLY);
401 if (fd < 0) {
Ben Gamaria3ba8112010-12-22 13:30:14 -0500402 fprintf(stderr, "fixdep: error opening depfile: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 perror(depfile);
404 exit(2);
405 }
Ben Gamaria3ba8112010-12-22 13:30:14 -0500406 if (fstat(fd, &st) < 0) {
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900407 fprintf(stderr, "fixdep: error fstat'ing depfile: ");
408 perror(depfile);
409 exit(2);
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (st.st_size == 0) {
412 fprintf(stderr,"fixdep: %s is empty\n",depfile);
413 close(fd);
414 return;
415 }
416 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
417 if ((long) map == -1) {
418 perror("fixdep: mmap");
419 close(fd);
420 return;
421 }
422
423 parse_dep_file(map, st.st_size);
424
425 munmap(map, st.st_size);
426
427 close(fd);
428}
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430int main(int argc, char *argv[])
431{
Nicolas Pitred8329e32016-02-12 15:00:50 -0500432 if (argc == 5 && !strcmp(argv[1], "-e")) {
433 insert_extra_deps = 1;
434 argv++;
435 } else if (argc != 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 usage();
437
438 depfile = argv[1];
439 target = argv[2];
440 cmdline = argv[3];
441
442 print_cmdline();
443 print_deps();
444
445 return 0;
446}