*: mass renaming of USE_XXXX to IF_XXXX
[oweals/busybox.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  * 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>
115 #include <arpa/inet.h>
116
117 /* bbox: not needed
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 */
123
124 char *target;
125 char *depfile;
126 char *cmdline;
127
128 void usage(void)
129
130 {
131         fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
132         exit(1);
133 }
134
135 /*
136  * Print out the commandline prefixed with cmd_<target filename> :=
137  */
138 void print_cmdline(void)
139 {
140         printf("cmd_%s := %s\n\n", target, cmdline);
141 }
142
143 char * str_config  = NULL;
144 int    size_config = 0;
145 int    len_config  = 0;
146
147 /*
148  * Grow the configuration string to a desired length.
149  * Usually the first growth is plenty.
150  */
151 void grow_config(int len)
152 {
153         while (len_config + len > size_config) {
154                 if (size_config == 0)
155                         size_config = 2048;
156                 str_config = realloc(str_config, size_config *= 2);
157                 if (str_config == NULL)
158                         { perror("fixdep:malloc"); exit(1); }
159         }
160 }
161
162
163
164 /*
165  * Lookup a value in the configuration string.
166  */
167 int is_defined_config(const char * name, int len)
168 {
169         const char * pconfig;
170         const char * plast = str_config + len_config - len;
171         for ( pconfig = str_config + 1; pconfig < plast; pconfig++ ) {
172                 if (pconfig[ -1] == '\n'
173                 &&  pconfig[len] == '\n'
174                 &&  !memcmp(pconfig, name, len))
175                         return 1;
176         }
177         return 0;
178 }
179
180 /*
181  * Add a new value to the configuration string.
182  */
183 void define_config(const char * name, int len)
184 {
185         grow_config(len + 1);
186
187         memcpy(str_config+len_config, name, len);
188         len_config += len;
189         str_config[len_config++] = '\n';
190 }
191
192 /*
193  * Clear the set of configuration strings.
194  */
195 void clear_config(void)
196 {
197         len_config = 0;
198         define_config("", 0);
199 }
200
201 /*
202  * Record the use of a CONFIG_* word.
203  */
204 void use_config(char *m, int slen)
205 {
206         char s[PATH_MAX];
207         char *p;
208
209         if (is_defined_config(m, slen))
210             return;
211
212         define_config(m, slen);
213
214         memcpy(s, m, slen); s[slen] = 0;
215
216         for (p = s; p < s + slen; p++) {
217                 if (*p == '_')
218                         *p = '/';
219                 else
220                         *p = tolower((int)*p);
221         }
222         printf("    $(wildcard include/config/%s.h) \\\n", s);
223 }
224
225 void parse_config_file(char *map, size_t len)
226 {
227         /* modified for bbox */
228         char *end_3 = map + len - 3; /* 3 == length of "IF_" */
229         char *end_7 = map + len - 7;
230         char *p = map;
231         char *q;
232         int off;
233
234         for (; p <= end_3; p++) {
235                 /* Find next identifier's beginning */
236                 if (!(isalnum(*p) || *p == '_'))
237                         continue;
238
239                 /* Check it */
240                 if (p < end_7 && p[6] == '_') {
241                         if (!memcmp(p, "CONFIG", 6)) goto conf7;
242                         if (!memcmp(p, "ENABLE", 6)) goto conf7;
243                         if (!memcmp(p, "IF_NOT", 6)) goto conf7;
244                 }
245                 /* we have at least 3 chars because of p <= end_3 */
246                 /*if (!memcmp(p, "IF_", 3)) goto conf3;*/
247                 if (p[0] == 'I' && p[1] == 'F' && p[2] == '_') goto conf3;
248
249                 /* This identifier is not interesting, skip it */
250                 while (p <= end_3 && (isalnum(*p) || *p == '_'))
251                         p++;
252                 continue;
253
254         conf3:  off = 3;
255         conf7:  off = 7;
256                 p += off;
257                 for (q = p; q < end_3+3; q++) {
258                         if (!(isalnum(*q) || *q == '_'))
259                                 break;
260                 }
261                 use_config(p, q-p);
262         }
263 }
264
265 /* test is s ends in sub */
266 int strrcmp(char *s, char *sub)
267 {
268         int slen = strlen(s);
269         int sublen = strlen(sub);
270
271         if (sublen > slen)
272                 return 1;
273
274         return memcmp(s + slen - sublen, sub, sublen);
275 }
276
277 void do_config_file(char *filename)
278 {
279         struct stat st;
280         int fd;
281         void *map;
282
283         fd = open(filename, O_RDONLY);
284         if (fd < 0) {
285                 fprintf(stderr, "fixdep: ");
286                 perror(filename);
287                 exit(2);
288         }
289         fstat(fd, &st);
290         if (st.st_size == 0) {
291                 close(fd);
292                 return;
293         }
294         map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
295         if ((long) map == -1) {
296                 perror("fixdep: mmap");
297                 close(fd);
298                 return;
299         }
300
301         parse_config_file(map, st.st_size);
302
303         munmap(map, st.st_size);
304
305         close(fd);
306 }
307
308 void parse_dep_file(void *map, size_t len)
309 {
310         char *m = map;
311         char *end = m + len;
312         char *p;
313         char s[PATH_MAX];
314
315         p = memchr(m, ':', len);
316         if (!p) {
317                 fprintf(stderr, "fixdep: parse error\n");
318                 exit(1);
319         }
320         memcpy(s, m, p-m); s[p-m] = 0;
321         printf("deps_%s := \\\n", target);
322         m = p+1;
323
324         clear_config();
325
326         while (m < end) {
327                 while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))
328                         m++;
329                 p = m;
330                 while (p < end && *p != ' ') p++;
331                 if (p == end) {
332                         do p--; while (!isalnum(*p));
333                         p++;
334                 }
335                 memcpy(s, m, p-m); s[p-m] = 0;
336                 if (strrcmp(s, "include/autoconf.h") &&
337                     strrcmp(s, "arch/um/include/uml-config.h") &&
338                     strrcmp(s, ".ver")) {
339                         printf("  %s \\\n", s);
340                         do_config_file(s);
341                 }
342                 m = p + 1;
343         }
344         printf("\n%s: $(deps_%s)\n\n", target, target);
345         printf("$(deps_%s):\n", target);
346 }
347
348 void print_deps(void)
349 {
350         struct stat st;
351         int fd;
352         void *map;
353
354         fd = open(depfile, O_RDONLY);
355         if (fd < 0) {
356                 fprintf(stderr, "fixdep: ");
357                 perror(depfile);
358                 exit(2);
359         }
360         fstat(fd, &st);
361         if (st.st_size == 0) {
362                 fprintf(stderr,"fixdep: %s is empty\n",depfile);
363                 close(fd);
364                 return;
365         }
366         map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
367         if ((long) map == -1) {
368                 perror("fixdep: mmap");
369                 close(fd);
370                 return;
371         }
372
373         parse_dep_file(map, st.st_size);
374
375         munmap(map, st.st_size);
376
377         close(fd);
378 }
379
380 void traps(void)
381 {
382 /* bbox: not needed
383         static char test[] __attribute__((aligned(sizeof(int)))) = "CONF";
384
385         if (*(int *)test != INT_CONF) {
386                 fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianess? %#x\n",
387                         *(int *)test);
388                 exit(2);
389         }
390 */
391 }
392
393 int main(int argc, char **argv)
394 {
395         traps();
396
397         if (argc != 4)
398                 usage();
399
400         depfile = argv[1];
401         target = argv[2];
402         cmdline = argv[3];
403
404         print_cmdline();
405         print_deps();
406
407         return 0;
408 }