blob: 20fed03beabad5c5b915e51763e338c7ab8833f4 [file] [log] [blame]
Masahiro Yamada47b65f72014-02-04 17:24:27 +09001/*
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 prequisites.
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/linux/config/his/driver.h" 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 * cmd_<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.h for every
78 * CONFIG_MY_OPTION encountered in any of the prequisites.
79 *
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 *
85 * The algorithm to grep for "CONFIG_..." is bit unusual, but should
86 * be fast ;-) We don't even try to really parse the header files, but
87 * merely grep, i.e. if CONFIG_FOO is mentioned in a comment, it will
88 * be picked up as well. It's not a problem with respect to
89 * correctness, since that can only give too many dependencies, thus
90 * we cannot miss a rebuild. Since people tend to not mention totally
91 * unrelated CONFIG_ options all over the place, it's not an
92 * efficiency problem either.
93 *
94 * (Note: it'd be easy to port over the complete mkdep state machine,
95 * but I don't think the added complexity is worth it)
96 */
97/*
98 * Note 2: if somebody writes HELLO_CONFIG_BOOM in a file, it will depend onto
99 * CONFIG_BOOM. This could seem a bug (not too hard to fix), but please do not
100 * fix it! Some UserModeLinux files (look at arch/um/) call CONFIG_BOOM as
101 * UML_CONFIG_BOOM, to avoid conflicts with /usr/include/linux/autoconf.h,
102 * through arch/um/include/uml-config.h; this fixdep "bug" makes sure that
103 * those files will have correct dependencies.
104 */
105
106#include <sys/types.h>
107#include <sys/stat.h>
108#include <sys/mman.h>
109#include <unistd.h>
110#include <fcntl.h>
111#include <string.h>
112#include <stdlib.h>
113#include <stdio.h>
114#include <limits.h>
115#include <ctype.h>
116#include <arpa/inet.h>
117
118#define INT_CONF ntohl(0x434f4e46)
119#define INT_ONFI ntohl(0x4f4e4649)
120#define INT_NFIG ntohl(0x4e464947)
121#define INT_FIG_ ntohl(0x4649475f)
122
123char *target;
124char *depfile;
125char *cmdline;
Masahiro Yamada88c1ccf2015-08-12 07:31:43 +0900126int is_spl_build = 0; /* hack for U-boot */
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900127
128static void usage(void)
129{
130 fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
131 exit(1);
132}
133
134/*
135 * Print out the commandline prefixed with cmd_<target filename> :=
136 */
137static void print_cmdline(void)
138{
139 printf("cmd_%s := %s\n\n", target, cmdline);
140}
141
142struct item {
143 struct item *next;
144 unsigned int len;
145 unsigned int hash;
146 char name[0];
147};
148
149#define HASHSZ 256
150static struct item *hashtab[HASHSZ];
151
152static unsigned int strhash(const char *str, unsigned int sz)
153{
154 /* fnv32 hash */
155 unsigned int i, hash = 2166136261U;
156
157 for (i = 0; i < sz; i++)
158 hash = (hash ^ str[i]) * 0x01000193;
159 return hash;
160}
161
162/*
163 * Lookup a value in the configuration string.
164 */
165static int is_defined_config(const char *name, int len, unsigned int hash)
166{
167 struct item *aux;
168
169 for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
170 if (aux->hash == hash && aux->len == len &&
171 memcmp(aux->name, name, len) == 0)
172 return 1;
173 }
174 return 0;
175}
176
177/*
178 * Add a new value to the configuration string.
179 */
180static void define_config(const char *name, int len, unsigned int hash)
181{
182 struct item *aux = malloc(sizeof(*aux) + len);
183
184 if (!aux) {
185 perror("fixdep:malloc");
186 exit(1);
187 }
188 memcpy(aux->name, name, len);
189 aux->len = len;
190 aux->hash = hash;
191 aux->next = hashtab[hash % HASHSZ];
192 hashtab[hash % HASHSZ] = aux;
193}
194
195/*
196 * Clear the set of configuration strings.
197 */
198static void clear_config(void)
199{
200 struct item *aux, *next;
201 unsigned int i;
202
203 for (i = 0; i < HASHSZ; i++) {
204 for (aux = hashtab[i]; aux; aux = next) {
205 next = aux->next;
206 free(aux);
207 }
208 hashtab[i] = NULL;
209 }
210}
211
212/*
213 * Record the use of a CONFIG_* word.
214 */
215static void use_config(const char *m, int slen)
216{
217 unsigned int hash = strhash(m, slen);
218 int c, i;
219
220 if (is_defined_config(m, slen, hash))
221 return;
222
223 define_config(m, slen, hash);
224
Masahiro Yamadaa06476e2015-02-28 00:37:57 +0900225 printf(" $(wildcard include/config/");
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900226 for (i = 0; i < slen; i++) {
227 c = m[i];
228 if (c == '_')
229 c = '/';
230 else
231 c = tolower(c);
232 putchar(c);
233 }
234 printf(".h) \\\n");
235}
236
237static void parse_config_file(const char *map, size_t len)
238{
239 const int *end = (const int *) (map + len);
240 /* start at +1, so that p can never be < map */
241 const int *m = (const int *) map + 1;
242 const char *p, *q;
Masahiro Yamada88c1ccf2015-08-12 07:31:43 +0900243 char tmp_buf[256] = "SPL_"; /* hack for U-Boot */
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900244
245 for (; m < end; m++) {
246 if (*m == INT_CONF) { p = (char *) m ; goto conf; }
247 if (*m == INT_ONFI) { p = (char *) m-1; goto conf; }
248 if (*m == INT_NFIG) { p = (char *) m-2; goto conf; }
249 if (*m == INT_FIG_) { p = (char *) m-3; goto conf; }
250 continue;
251 conf:
252 if (p > map + len - 7)
253 continue;
254 if (memcmp(p, "CONFIG_", 7))
255 continue;
Masahiro Yamada15e79352015-08-12 07:31:41 +0900256 p += 7;
257 for (q = p; q < map + len; q++) {
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900258 if (!(isalnum(*q) || *q == '_'))
259 goto found;
260 }
261 continue;
262
263 found:
264 if (!memcmp(q - 7, "_MODULE", 7))
265 q -= 7;
Masahiro Yamada15e79352015-08-12 07:31:41 +0900266 if (q - p < 0)
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900267 continue;
Masahiro Yamada88c1ccf2015-08-12 07:31:43 +0900268
269 /* U-Boot also handles CONFIG_IS_{ENABLED/BUILTIN/MODULE} */
270 if ((q - p == 10 && !memcmp(p, "IS_ENABLED(", 11)) ||
271 (q - p == 10 && !memcmp(p, "IS_BUILTIN(", 11)) ||
272 (q - p == 9 && !memcmp(p, "IS_MODULE(", 10))) {
273 p = q + 1;
274 for (q = p; q < map + len; q++)
275 if (*q == ')')
276 goto found2;
277 continue;
278
279 found2:
280 if (is_spl_build) {
281 memcpy(tmp_buf + 4, p, q - p);
282 q = tmp_buf + 4 + (q - p);
283 p = tmp_buf;
284 }
285 }
286 /* end U-Boot hack */
287
Masahiro Yamada15e79352015-08-12 07:31:41 +0900288 use_config(p, q - p);
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900289 }
290}
291
292/* test is s ends in sub */
293static int strrcmp(char *s, char *sub)
294{
295 int slen = strlen(s);
296 int sublen = strlen(sub);
297
298 if (sublen > slen)
299 return 1;
300
301 return memcmp(s + slen - sublen, sub, sublen);
302}
303
304static void do_config_file(const char *filename)
305{
306 struct stat st;
307 int fd;
308 void *map;
309
310 fd = open(filename, O_RDONLY);
311 if (fd < 0) {
312 fprintf(stderr, "fixdep: error opening config file: ");
313 perror(filename);
314 exit(2);
315 }
316 fstat(fd, &st);
317 if (st.st_size == 0) {
318 close(fd);
319 return;
320 }
321 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
322 if ((long) map == -1) {
323 perror("fixdep: mmap");
324 close(fd);
325 return;
326 }
327
328 parse_config_file(map, st.st_size);
329
330 munmap(map, st.st_size);
331
332 close(fd);
333}
334
335/*
336 * Important: The below generated source_foo.o and deps_foo.o variable
337 * assignments are parsed not only by make, but also by the rather simple
338 * parser in scripts/mod/sumversion.c.
339 */
340static void parse_dep_file(void *map, size_t len)
341{
342 char *m = map;
343 char *end = m + len;
344 char *p;
345 char s[PATH_MAX];
346 int is_target;
347 int saw_any_target = 0;
348 int is_first_dep = 0;
349
350 clear_config();
351
352 while (m < end) {
353 /* Skip any "white space" */
354 while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))
355 m++;
356 /* Find next "white space" */
357 p = m;
358 while (p < end && *p != ' ' && *p != '\\' && *p != '\n')
359 p++;
360 /* Is the token we found a target name? */
361 is_target = (*(p-1) == ':');
362 /* Don't write any target names into the dependency file */
363 if (is_target) {
364 /* The /next/ file is the first dependency */
365 is_first_dep = 1;
366 } else {
367 /* Save this token/filename */
368 memcpy(s, m, p-m);
369 s[p - m] = 0;
370
371 /* Ignore certain dependencies */
372 if (strrcmp(s, "include/generated/autoconf.h") &&
373 strrcmp(s, "arch/um/include/uml-config.h") &&
374 strrcmp(s, "include/linux/kconfig.h") &&
375 strrcmp(s, ".ver")) {
376 /*
377 * Do not list the source file as dependency,
378 * so that kbuild is not confused if a .c file
379 * is rewritten into .S or vice versa. Storing
380 * it in source_* is needed for modpost to
381 * compute srcversions.
382 */
383 if (is_first_dep) {
384 /*
385 * If processing the concatenation of
386 * multiple dependency files, only
387 * process the first target name, which
388 * will be the original source name,
389 * and ignore any other target names,
390 * which will be intermediate temporary
391 * files.
392 */
393 if (!saw_any_target) {
394 saw_any_target = 1;
395 printf("source_%s := %s\n\n",
396 target, s);
397 printf("deps_%s := \\\n",
398 target);
399 }
400 is_first_dep = 0;
401 } else
402 printf(" %s \\\n", s);
403 do_config_file(s);
404 }
405 }
406 /*
407 * Start searching for next token immediately after the first
408 * "whitespace" character that follows this token.
409 */
410 m = p + 1;
411 }
412
413 if (!saw_any_target) {
414 fprintf(stderr, "fixdep: parse error; no targets found\n");
415 exit(1);
416 }
417
418 printf("\n%s: $(deps_%s)\n\n", target, target);
419 printf("$(deps_%s):\n", target);
420}
421
422static void print_deps(void)
423{
424 struct stat st;
425 int fd;
426 void *map;
427
428 fd = open(depfile, O_RDONLY);
429 if (fd < 0) {
430 fprintf(stderr, "fixdep: error opening depfile: ");
431 perror(depfile);
432 exit(2);
433 }
434 if (fstat(fd, &st) < 0) {
Masahiro Yamada33eb5e72014-06-16 18:56:38 +0900435 fprintf(stderr, "fixdep: error fstat'ing depfile: ");
436 perror(depfile);
437 exit(2);
438 }
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900439 if (st.st_size == 0) {
440 fprintf(stderr,"fixdep: %s is empty\n",depfile);
441 close(fd);
442 return;
443 }
444 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
445 if ((long) map == -1) {
446 perror("fixdep: mmap");
447 close(fd);
448 return;
449 }
450
451 parse_dep_file(map, st.st_size);
452
453 munmap(map, st.st_size);
454
455 close(fd);
456}
457
458static void traps(void)
459{
460 static char test[] __attribute__((aligned(sizeof(int)))) = "CONF";
461 int *p = (int *)test;
462
463 if (*p != INT_CONF) {
464 fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianness? %#x\n",
465 *p);
466 exit(2);
467 }
468}
469
470int main(int argc, char *argv[])
471{
472 traps();
473
474 if (argc != 4)
475 usage();
476
477 depfile = argv[1];
478 target = argv[2];
479 cmdline = argv[3];
480
Masahiro Yamada88c1ccf2015-08-12 07:31:43 +0900481 /* hack for U-boot */
482 if (!strncmp(target, "spl/", 4) || !strncmp(target, "tpl/", 4))
483 is_spl_build = 1;
484
Masahiro Yamada47b65f72014-02-04 17:24:27 +0900485 print_cmdline();
486 print_deps();
487
488 return 0;
489}