ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / scripts / basic / fixdep.c
1 /*
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 <linux/config.h> which then again
20  * includes <linux/autoconf.h>
21  *
22  * If the user re-runs make *config, linux/autoconf.h will be
23  * regenerated.  make notices that and will rebuild every file which
24  * includes autoconf.h, i.e. basically all files. This is extremely
25  * annoying if the user just changed CONFIG_HIS_DRIVER from n to m.
26  *
27  * So we play the same trick that "mkdep" played before. We replace
28  * the dependency on linux/autoconf.h by a dependency on every config
29  * option which is mentioned in any of the listed prequisites.
30  *
31  * To be exact, split-include populates a tree in include/config/,
32  * e.g. include/config/his/driver.h, which contains the #define/#undef
33  * for the CONFIG_HIS_DRIVER option.
34  *
35  * So if the user changes his CONFIG_HIS_DRIVER option, only the objects
36  * which depend on "include/linux/config/his/driver.h" will be rebuilt,
37  * so most likely only his driver ;-)
38  *
39  * The idea above dates, by the way, back to Michael E Chastain, AFAIK.
40  *
41  * So to get dependencies right, there are two issues:
42  * o if any of the files the compiler read changed, we need to rebuild
43  * o if the command line given to the compile the file changed, we
44  *   better rebuild as well.
45  *
46  * The former is handled by using the -MD output, the later by saving
47  * the command line used to compile the old object and comparing it
48  * to the one we would now use.
49  *
50  * Again, also this idea is pretty old and has been discussed on
51  * kbuild-devel a long time ago. I don't have a sensibly working
52  * internet connection right now, so I rather don't mention names
53  * without double checking.
54  *
55  * This code here has been based partially based on mkdep.c, which
56  * says the following about its history:
57  *
58  *   Copyright abandoned, Michael Chastain, <mailto:mec@shout.net>.
59  *   This is a C version of syncdep.pl by Werner Almesberger.
60  *
61  *
62  * It is invoked as
63  *
64  *   fixdep <depfile> <target> <cmdline>
65  *
66  * and will read the dependency file <depfile>
67  *
68  * The transformed dependency snipped is written to stdout.
69  *
70  * It first generates a line
71  *
72  *   cmd_<target> = <cmdline>
73  *
74  * and then basically copies the .<target>.d file to stdout, in the
75  * process filtering out the dependency on linux/autoconf.h and adding
76  * dependencies on include/config/my/option.h for every
77  * CONFIG_MY_OPTION encountered in any of the prequisites.
78  *
79  * It will also filter out all the dependencies on *.ver. We need
80  * to make sure that the generated version checksum are globally up
81  * to date before even starting the recursive build, so it's too late
82  * at this point anyway.
83  *
84  * The algorithm to grep for "CONFIG_..." is bit unusual, but should
85  * be fast ;-) We don't even try to really parse the header files, but
86  * 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 #include <sys/types.h>
98 #include <sys/stat.h>
99 #include <sys/mman.h>
100 #include <unistd.h>
101 #include <fcntl.h>
102 #include <string.h>
103 #include <stdlib.h>
104 #include <stdio.h>
105 #include <limits.h>
106 #include <ctype.h>
107 #include <netinet/in.h>
108
109 #define INT_CONF ntohl(0x434f4e46)
110 #define INT_ONFI ntohl(0x4f4e4649)
111 #define INT_NFIG ntohl(0x4e464947)
112 #define INT_FIG_ ntohl(0x4649475f)
113
114 char *target;
115 char *depfile;
116 char *cmdline;
117
118 void usage(void)
119
120 {
121         fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
122         exit(1);
123 }
124
125 void print_cmdline(void)
126 {
127         printf("cmd_%s := %s\n\n", target, cmdline);
128 }
129
130 char * str_config  = NULL;
131 int    size_config = 0;
132 int    len_config  = 0;
133
134 /*
135  * Grow the configuration string to a desired length.
136  * Usually the first growth is plenty.
137  */
138 void grow_config(int len)
139 {
140         while (len_config + len > size_config) {
141                 if (size_config == 0)
142                         size_config = 2048;
143                 str_config = realloc(str_config, size_config *= 2);
144                 if (str_config == NULL)
145                         { perror("fixdep:malloc"); exit(1); }
146         }
147 }
148
149
150
151 /*
152  * Lookup a value in the configuration string.
153  */
154 int is_defined_config(const char * name, int len)
155 {
156         const char * pconfig;
157         const char * plast = str_config + len_config - len;
158         for ( pconfig = str_config + 1; pconfig < plast; pconfig++ ) {
159                 if (pconfig[ -1] == '\n'
160                 &&  pconfig[len] == '\n'
161                 &&  !memcmp(pconfig, name, len))
162                         return 1;
163         }
164         return 0;
165 }
166
167 /*
168  * Add a new value to the configuration string.
169  */
170 void define_config(const char * name, int len)
171 {
172         grow_config(len + 1);
173
174         memcpy(str_config+len_config, name, len);
175         len_config += len;
176         str_config[len_config++] = '\n';
177 }
178
179 /*
180  * Clear the set of configuration strings.
181  */
182 void clear_config(void)
183 {
184         len_config = 0;
185         define_config("", 0);
186 }
187
188 /*
189  * Record the use of a CONFIG_* word.
190  */
191 void use_config(char *m, int slen)
192 {
193         char s[PATH_MAX];
194         char *p;
195
196         if (is_defined_config(m, slen))
197             return;
198
199         define_config(m, slen);
200
201         memcpy(s, m, slen); s[slen] = 0;
202
203         for (p = s; p < s + slen; p++) {
204                 if (*p == '_')
205                         *p = '/';
206                 else
207                         *p = tolower((unsigned char)*p);
208         }
209         printf("    $(wildcard include/config/%s.h) \\\n", s);
210 }
211
212 void parse_config_file(char *map, size_t len)
213 {
214         int *end = (int *) (map + len);
215         /* start at +1, so that p can never be < map */
216         int *m   = (int *) map + 1;
217         char *p, *q;
218
219         for (; m < end; m++) {
220                 if (*m == INT_CONF) { p = (char *) m  ; goto conf; }
221                 if (*m == INT_ONFI) { p = (char *) m-1; goto conf; }
222                 if (*m == INT_NFIG) { p = (char *) m-2; goto conf; }
223                 if (*m == INT_FIG_) { p = (char *) m-3; goto conf; }
224                 continue;
225         conf:
226                 if (p > map + len - 7)
227                         continue;
228                 if (memcmp(p, "CONFIG_", 7))
229                         continue;
230                 for (q = p + 7; q < map + len; q++) {
231                         if (!(isalnum(*q) || *q == '_'))
232                                 goto found;
233                 }
234                 continue;
235
236         found:
237                 use_config(p+7, q-p-7);
238         }
239 }
240
241 /* test is s ends in sub */
242 int strrcmp(char *s, char *sub)
243 {
244         int slen = strlen(s);
245         int sublen = strlen(sub);
246
247         if (sublen > slen)
248                 return 1;
249
250         return memcmp(s + slen - sublen, sub, sublen);
251 }
252
253 void do_config_file(char *filename)
254 {
255         struct stat st;
256         int fd;
257         void *map;
258
259         fd = open(filename, O_RDONLY);
260         if (fd < 0) {
261                 fprintf(stderr, "fixdep: ");
262                 perror(filename);
263                 exit(2);
264         }
265         fstat(fd, &st);
266         if (st.st_size == 0) {
267                 close(fd);
268                 return;
269         }
270         map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
271         if ((long) map == -1) {
272                 perror("fixdep: mmap");
273                 close(fd);
274                 return;
275         }
276
277         parse_config_file(map, st.st_size);
278
279         munmap(map, st.st_size);
280
281         close(fd);
282 }
283
284 void parse_dep_file(void *map, size_t len)
285 {
286         char *m = map;
287         char *end = m + len;
288         char *p;
289         char s[PATH_MAX];
290
291         p = strchr(m, ':');
292         if (!p) {
293                 fprintf(stderr, "fixdep: parse error\n");
294                 exit(1);
295         }
296         memcpy(s, m, p-m); s[p-m] = 0;
297         printf("deps_%s := \\\n", target);
298         m = p+1;
299
300         clear_config();
301
302         while (m < end) {
303                 while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))
304                         m++;
305                 p = m;
306                 while (p < end && *p != ' ') p++;
307                 if (p == end) {
308                         do p--; while (!isalnum(*p));
309                         p++;
310                 }
311                 memcpy(s, m, p-m); s[p-m] = 0;
312                 if (strrcmp(s, "include/linux/autoconf.h") &&
313                     strrcmp(s, ".ver")) {
314                         printf("  %s \\\n", s);
315                         do_config_file(s);
316                 }
317                 m = p + 1;
318         }
319         printf("\n%s: $(deps_%s)\n\n", target, target);
320         printf("$(deps_%s):\n", target);
321 }
322
323 void print_deps(void)
324 {
325         struct stat st;
326         int fd;
327         void *map;
328
329         fd = open(depfile, O_RDONLY);
330         if (fd < 0) {
331                 fprintf(stderr, "fixdep: ");
332                 perror(depfile);
333                 exit(2);
334         }
335         fstat(fd, &st);
336         if (st.st_size == 0) {
337                 fprintf(stderr,"fixdep: %s is empty\n",depfile);
338                 close(fd);
339                 return;
340         }
341         map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
342         if ((long) map == -1) {
343                 perror("fixdep: mmap");
344                 close(fd);
345                 return;
346         }
347
348         parse_dep_file(map, st.st_size);
349
350         munmap(map, st.st_size);
351
352         close(fd);
353 }
354
355 void traps(void)
356 {
357         static char test[] __attribute__((aligned(sizeof(int)))) = "CONF";
358
359         if (*(int *)test != INT_CONF) {
360                 fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianess? %#x\n",
361                         *(int *)test);
362                 exit(2);
363         }
364 }
365
366 int main(int argc, char *argv[])
367 {
368         traps();
369
370         if (argc != 4)
371                 usage();
372
373         depfile = argv[1];
374         target = argv[2];
375         cmdline = argv[3];
376
377         print_cmdline();
378         print_deps();
379
380         return 0;
381 }