blob: 53424511f7e149808c44642a3a217f07539752a5 [file] [log] [blame]
Nicholas Flintham1e3d3112013-04-10 10:48:38 +01001/*
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#include <sys/types.h>
99#include <sys/stat.h>
100#include <sys/mman.h>
101#include <unistd.h>
102#include <fcntl.h>
103#include <string.h>
104#include <stdlib.h>
105#include <stdio.h>
106#include <limits.h>
107#include <ctype.h>
108#include <arpa/inet.h>
109
110#define INT_CONF ntohl(0x434f4e46)
111#define INT_ONFI ntohl(0x4f4e4649)
112#define INT_NFIG ntohl(0x4e464947)
113#define INT_FIG_ ntohl(0x4649475f)
114
115char *target;
116char *depfile;
117char *cmdline;
118
119static void usage(void)
120{
121 fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
122 exit(1);
123}
124
125static void print_cmdline(void)
126{
127 printf("cmd_%s := %s\n\n", target, cmdline);
128}
129
130struct item {
131 struct item *next;
132 unsigned int len;
133 unsigned int hash;
134 char name[0];
135};
136
137#define HASHSZ 256
138static struct item *hashtab[HASHSZ];
139
140static unsigned int strhash(const char *str, unsigned int sz)
141{
142
143 unsigned int i, hash = 2166136261U;
144
145 for (i = 0; i < sz; i++)
146 hash = (hash ^ str[i]) * 0x01000193;
147 return hash;
148}
149
150static int is_defined_config(const char *name, int len, unsigned int hash)
151{
152 struct item *aux;
153
154 for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
155 if (aux->hash == hash && aux->len == len &&
156 memcmp(aux->name, name, len) == 0)
157 return 1;
158 }
159 return 0;
160}
161
162static void define_config(const char *name, int len, unsigned int hash)
163{
164 struct item *aux = malloc(sizeof(*aux) + len);
165
166 if (!aux) {
167 perror("fixdep:malloc");
168 exit(1);
169 }
170 memcpy(aux->name, name, len);
171 aux->len = len;
172 aux->hash = hash;
173 aux->next = hashtab[hash % HASHSZ];
174 hashtab[hash % HASHSZ] = aux;
175}
176
177static void clear_config(void)
178{
179 struct item *aux, *next;
180 unsigned int i;
181
182 for (i = 0; i < HASHSZ; i++) {
183 for (aux = hashtab[i]; aux; aux = next) {
184 next = aux->next;
185 free(aux);
186 }
187 hashtab[i] = NULL;
188 }
189}
190
191static void use_config(const char *m, int slen)
192{
193 unsigned int hash = strhash(m, slen);
194 int c, i;
195
196 if (is_defined_config(m, slen, hash))
197 return;
198
199 define_config(m, slen, hash);
200
201 printf(" $(wildcard include/config/");
202 for (i = 0; i < slen; i++) {
203 c = m[i];
204 if (c == '_')
205 c = '/';
206 else
207 c = tolower(c);
208 putchar(c);
209 }
210 printf(".h) \\\n");
211}
212
213static void parse_config_file(const char *map, size_t len)
214{
215 const int *end = (const int *) (map + len);
216
217 const int *m = (const int *) map + 1;
218 const char *p, *q;
219
220 for (; m < end; m++) {
221 if (*m == INT_CONF) { p = (char *) m ; goto conf; }
222 if (*m == INT_ONFI) { p = (char *) m-1; goto conf; }
223 if (*m == INT_NFIG) { p = (char *) m-2; goto conf; }
224 if (*m == INT_FIG_) { p = (char *) m-3; goto conf; }
225 continue;
226 conf:
227 if (p > map + len - 7)
228 continue;
229 if (memcmp(p, "CONFIG_", 7))
230 continue;
231 for (q = p + 7; q < map + len; q++) {
232 if (!(isalnum(*q) || *q == '_'))
233 goto found;
234 }
235 continue;
236
237 found:
238 if (!memcmp(q - 7, "_MODULE", 7))
239 q -= 7;
240 if( (q-p-7) < 0 )
241 continue;
242 use_config(p+7, q-p-7);
243 }
244}
245
246static int strrcmp(char *s, char *sub)
247{
248 int slen = strlen(s);
249 int sublen = strlen(sub);
250
251 if (sublen > slen)
252 return 1;
253
254 return memcmp(s + slen - sublen, sub, sublen);
255}
256
257static void do_config_file(const char *filename)
258{
259 struct stat st;
260 int fd;
261 void *map;
262
263 fd = open(filename, O_RDONLY);
264 if (fd < 0) {
265 fprintf(stderr, "fixdep: error opening config file: ");
266 perror(filename);
267 exit(2);
268 }
269 fstat(fd, &st);
270 if (st.st_size == 0) {
271 close(fd);
272 return;
273 }
274 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
275 if ((long) map == -1) {
276 perror("fixdep: mmap");
277 close(fd);
278 return;
279 }
280
281 parse_config_file(map, st.st_size);
282
283 munmap(map, st.st_size);
284
285 close(fd);
286}
287
288static void parse_dep_file(void *map, size_t len)
289{
290 char *m = map;
291 char *end = m + len;
292 char *p;
293 char s[PATH_MAX];
294 int first;
295
296 p = strchr(m, ':');
297 if (!p) {
298 fprintf(stderr, "fixdep: parse error\n");
299 exit(1);
300 }
301 memcpy(s, m, p-m); s[p-m] = 0;
302 m = p+1;
303
304 clear_config();
305
306 first = 1;
307 while (m < end) {
308 while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))
309 m++;
310 p = m;
311 while (p < end && *p != ' ') p++;
312 if (p == end) {
313 do p--; while (!isalnum(*p));
314 p++;
315 }
316 memcpy(s, m, p-m); s[p-m] = 0;
317 if (strrcmp(s, "include/generated/autoconf.h") &&
318 strrcmp(s, "arch/um/include/uml-config.h") &&
319 strrcmp(s, "include/linux/kconfig.h") &&
320 strrcmp(s, ".ver")) {
321 /*
322 * Do not list the source file as dependency, so that
323 * kbuild is not confused if a .c file is rewritten
324 * into .S or vice versa. Storing it in source_* is
325 * needed for modpost to compute srcversions.
326 */
327 if (first) {
328 printf("source_%s := %s\n\n", target, s);
329 printf("deps_%s := \\\n", target);
330 } else
331 printf(" %s \\\n", s);
332 do_config_file(s);
333 }
334 first = 0;
335 m = p + 1;
336 }
337 printf("\n%s: $(deps_%s)\n\n", target, target);
338 printf("$(deps_%s):\n", target);
339}
340
341static void print_deps(void)
342{
343 struct stat st;
344 int fd;
345 void *map;
346
347 fd = open(depfile, O_RDONLY);
348 if (fd < 0) {
349 fprintf(stderr, "fixdep: error opening depfile: ");
350 perror(depfile);
351 exit(2);
352 }
353 if (fstat(fd, &st) < 0) {
354 fprintf(stderr, "fixdep: error fstat'ing depfile: ");
355 perror(depfile);
356 exit(2);
357 }
358 if (st.st_size == 0) {
359 fprintf(stderr,"fixdep: %s is empty\n",depfile);
360 close(fd);
361 return;
362 }
363 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
364 if ((long) map == -1) {
365 perror("fixdep: mmap");
366 close(fd);
367 return;
368 }
369
370 parse_dep_file(map, st.st_size);
371
372 munmap(map, st.st_size);
373
374 close(fd);
375}
376
377static void traps(void)
378{
379 static char test[] __attribute__((aligned(sizeof(int)))) = "CONF";
380 int *p = (int *)test;
381
382 if (*p != INT_CONF) {
383 fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianess? %#x\n",
384 *p);
385 exit(2);
386 }
387}
388
389int main(int argc, char *argv[])
390{
391 traps();
392
393 if (argc != 4)
394 usage();
395
396 depfile = argv[1];
397 target = argv[2];
398 cmdline = argv[3];
399
400 print_cmdline();
401 print_deps();
402
403 return 0;
404}