added a lot of printk output to ease writing of emulator
[linux-2.4.21-pre4.git] / scripts / mkdep.c
1 /*
2  * Originally by Linus Torvalds.
3  * Smart CONFIG_* processing by Werner Almesberger, Michael Chastain.
4  *
5  * Usage: mkdep cflags -- file ...
6  * 
7  * Read source files and output makefile dependency lines for them.
8  * I make simple dependency lines for #include <*.h> and #include "*.h".
9  * I also find instances of CONFIG_FOO and generate dependencies
10  *    like include/config/foo.h.
11  *
12  * 1 August 1999, Michael Elizabeth Chastain, <mec@shout.net>
13  * - Keith Owens reported a bug in smart config processing.  There used
14  *   to be an optimization for "#define CONFIG_FOO ... #ifdef CONFIG_FOO",
15  *   so that the file would not depend on CONFIG_FOO because the file defines
16  *   this symbol itself.  But this optimization is bogus!  Consider this code:
17  *   "#if 0 \n #define CONFIG_FOO \n #endif ... #ifdef CONFIG_FOO".  Here
18  *   the definition is inactivated, but I still used it.  It turns out this
19  *   actually happens a few times in the kernel source.  The simple way to
20  *   fix this problem is to remove this particular optimization.
21  *
22  * 2.3.99-pre1, Andrew Morton <andrewm@uow.edu.au>
23  * - Changed so that 'filename.o' depends upon 'filename.[cS]'.  This is so that
24  *   missing source files are noticed, rather than silently ignored.
25  *
26  * 2.4.2-pre3, Keith Owens <kaos@ocs.com.au>
27  * - Accept cflags followed by '--' followed by filenames.  mkdep extracts -I
28  *   options from cflags and looks in the specified directories as well as the
29  *   defaults.   Only -I is supported, no attempt is made to handle -idirafter,
30  *   -isystem, -I- etc.
31  */
32
33 #include <ctype.h>
34 #include <fcntl.h>
35 #include <limits.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include <sys/fcntl.h>
42 #include <sys/mman.h>
43 #include <sys/stat.h>
44 #include <sys/types.h>
45
46
47
48 char __depname[512] = "\n\t@touch ";
49 #define depname (__depname+9)
50 int hasdep;
51
52 struct path_struct {
53         int len;
54         char *buffer;
55 };
56 struct path_struct *path_array;
57 int paths;
58
59
60 /* Current input file */
61 static const char *g_filename;
62
63 /*
64  * This records all the configuration options seen.
65  * In perl this would be a hash, but here it's a long string
66  * of values separated by newlines.  This is simple and
67  * extremely fast.
68  */
69 char * str_config  = NULL;
70 int    size_config = 0;
71 int    len_config  = 0;
72
73 static void
74 do_depname(void)
75 {
76         if (!hasdep) {
77                 hasdep = 1;
78                 printf("%s:", depname);
79                 if (g_filename)
80                         printf(" %s", g_filename);
81         }
82 }
83
84 /*
85  * Grow the configuration string to a desired length.
86  * Usually the first growth is plenty.
87  */
88 void grow_config(int len)
89 {
90         while (len_config + len > size_config) {
91                 if (size_config == 0)
92                         size_config = 2048;
93                 str_config = realloc(str_config, size_config *= 2);
94                 if (str_config == NULL)
95                         { perror("malloc config"); exit(1); }
96         }
97 }
98
99
100
101 /*
102  * Lookup a value in the configuration string.
103  */
104 int is_defined_config(const char * name, int len)
105 {
106         const char * pconfig;
107         const char * plast = str_config + len_config - len;
108         for ( pconfig = str_config + 1; pconfig < plast; pconfig++ ) {
109                 if (pconfig[ -1] == '\n'
110                 &&  pconfig[len] == '\n'
111                 &&  !memcmp(pconfig, name, len))
112                         return 1;
113         }
114         return 0;
115 }
116
117
118
119 /*
120  * Add a new value to the configuration string.
121  */
122 void define_config(const char * name, int len)
123 {
124         grow_config(len + 1);
125
126         memcpy(str_config+len_config, name, len);
127         len_config += len;
128         str_config[len_config++] = '\n';
129 }
130
131
132
133 /*
134  * Clear the set of configuration strings.
135  */
136 void clear_config(void)
137 {
138         len_config = 0;
139         define_config("", 0);
140 }
141
142
143
144 /*
145  * This records all the precious .h filenames.  No need for a hash,
146  * it's a long string of values enclosed in tab and newline.
147  */
148 char * str_precious  = NULL;
149 int    size_precious = 0;
150 int    len_precious  = 0;
151
152
153
154 /*
155  * Grow the precious string to a desired length.
156  * Usually the first growth is plenty.
157  */
158 void grow_precious(int len)
159 {
160         while (len_precious + len > size_precious) {
161                 if (size_precious == 0)
162                         size_precious = 2048;
163                 str_precious = realloc(str_precious, size_precious *= 2);
164                 if (str_precious == NULL)
165                         { perror("malloc"); exit(1); }
166         }
167 }
168
169
170
171 /*
172  * Add a new value to the precious string.
173  */
174 void define_precious(const char * filename)
175 {
176         int len = strlen(filename);
177         grow_precious(len + 4);
178         *(str_precious+len_precious++) = '\t';
179         memcpy(str_precious+len_precious, filename, len);
180         len_precious += len;
181         memcpy(str_precious+len_precious, " \\\n", 3);
182         len_precious += 3;
183 }
184
185
186
187 /*
188  * Handle an #include line.
189  */
190 void handle_include(int start, const char * name, int len)
191 {
192         struct path_struct *path;
193         int i;
194
195         if (len == 14 && !memcmp(name, "linux/config.h", len))
196                 return;
197
198         if (len >= 7 && !memcmp(name, "config/", 7))
199                 define_config(name+7, len-7-2);
200
201         for (i = start, path = path_array+start; i < paths; ++i, ++path) {
202                 memcpy(path->buffer+path->len, name, len);
203                 path->buffer[path->len+len] = '\0';
204                 if (access(path->buffer, F_OK) == 0) {
205                         do_depname();
206                         printf(" \\\n   %s", path->buffer);
207                         return;
208                 }
209         }
210
211 }
212
213
214
215 /*
216  * Add a path to the list of include paths.
217  */
218 void add_path(const char * name)
219 {
220         struct path_struct *path;
221         char resolved_path[PATH_MAX+1];
222         const char *name2;
223
224         //REX:
225         //fprintf(stderr, "mkdep.c: name is %s\n", name);
226         if (strcmp(name, ".")) {
227                 name2 = realpath(name, resolved_path);
228                 if (!name2) {
229                         fprintf(stderr, "realpath(%s) failed, %m\n", name);
230                         exit(1);
231                 }
232         }
233         else {
234                 name2 = "";
235         }
236
237         path_array = realloc(path_array, (++paths)*sizeof(*path_array));
238         if (!path_array) {
239                 fprintf(stderr, "cannot expand path_arry\n");
240                 exit(1);
241         }
242
243         path = path_array+paths-1;
244         path->len = strlen(name2);
245         path->buffer = malloc(path->len+1+256+1);
246         if (!path->buffer) {
247                 fprintf(stderr, "cannot allocate path buffer\n");
248                 exit(1);
249         }
250         strcpy(path->buffer, name2);
251         if (path->len && *(path->buffer+path->len-1) != '/') {
252                 *(path->buffer+path->len) = '/';
253                 *(path->buffer+(++(path->len))) = '\0';
254         }
255 }
256
257
258
259 /*
260  * Record the use of a CONFIG_* word.
261  */
262 void use_config(const char * name, int len)
263 {
264         char *pc;
265         int i;
266
267         pc = path_array[paths-1].buffer + path_array[paths-1].len;
268         memcpy(pc, "config/", 7);
269         pc += 7;
270
271         for (i = 0; i < len; i++) {
272             char c = name[i];
273             if (isupper((int)c)) c = tolower((int)c);
274             if (c == '_')   c = '/';
275             pc[i] = c;
276         }
277         pc[len] = '\0';
278
279         if (is_defined_config(pc, len))
280             return;
281
282         define_config(pc, len);
283
284         do_depname();
285         printf(" \\\n   $(wildcard %s.h)", path_array[paths-1].buffer);
286 }
287
288
289
290 /*
291  * Macros for stunningly fast map-based character access.
292  * __buf is a register which holds the current word of the input.
293  * Thus, there is one memory access per sizeof(unsigned long) characters.
294  */
295
296 #if defined(__alpha__) || defined(__i386__) || defined(__ia64__)  || defined(__x86_64__) || defined(__MIPSEL__) \
297     || defined(__arm__)
298 #define LE_MACHINE
299 #endif
300
301 #ifdef LE_MACHINE
302 #define next_byte(x) (x >>= 8)
303 #define current ((unsigned char) __buf)
304 #else
305 #define next_byte(x) (x <<= 8)
306 #define current (__buf >> 8*(sizeof(unsigned long)-1))
307 #endif
308
309 #define GETNEXT { \
310         next_byte(__buf); \
311         if ((unsigned long) next % sizeof(unsigned long) == 0) { \
312                 if (next >= end) \
313                         break; \
314                 __buf = * (unsigned long *) next; \
315         } \
316         next++; \
317 }
318
319 /*
320  * State machine macros.
321  */
322 #define CASE(c,label) if (current == c) goto label
323 #define NOTCASE(c,label) if (current != c) goto label
324
325 /*
326  * Yet another state machine speedup.
327  */
328 #define MAX2(a,b) ((a)>(b)?(a):(b))
329 #define MIN2(a,b) ((a)<(b)?(a):(b))
330 #define MAX5(a,b,c,d,e) (MAX2(a,MAX2(b,MAX2(c,MAX2(d,e)))))
331 #define MIN5(a,b,c,d,e) (MIN2(a,MIN2(b,MIN2(c,MIN2(d,e)))))
332
333
334
335 /*
336  * The state machine looks for (approximately) these Perl regular expressions:
337  *
338  *    m|\/\*.*?\*\/|
339  *    m|\/\/.*|
340  *    m|'.*?'|
341  *    m|".*?"|
342  *    m|#\s*include\s*"(.*?)"|
343  *    m|#\s*include\s*<(.*?>"|
344  *    m|#\s*(?define|undef)\s*CONFIG_(\w*)|
345  *    m|(?!\w)CONFIG_|
346  *
347  * About 98% of the CPU time is spent here, and most of that is in
348  * the 'start' paragraph.  Because the current characters are
349  * in a register, the start loop usually eats 4 or 8 characters
350  * per memory read.  The MAX5 and MIN5 tests dispose of most
351  * input characters with 1 or 2 comparisons.
352  */
353 void state_machine(const char * map, const char * end)
354 {
355         const char * next = map;
356         const char * map_dot;
357         unsigned long __buf = 0;
358
359         for (;;) {
360 start:
361         GETNEXT
362 __start:
363         if (current > MAX5('/','\'','"','#','C')) goto start;
364         if (current < MIN5('/','\'','"','#','C')) goto start;
365         CASE('/',  slash);
366         CASE('\'', squote);
367         CASE('"',  dquote);
368         CASE('#',  pound);
369         CASE('C',  cee);
370         goto start;
371
372 /* // */
373 slash_slash:
374         GETNEXT
375         CASE('\n', start);
376         NOTCASE('\\', slash_slash);
377         GETNEXT
378         goto slash_slash;
379
380 /* / */
381 slash:
382         GETNEXT
383         CASE('/',  slash_slash);
384         NOTCASE('*', __start);
385 slash_star_dot_star:
386         GETNEXT
387 __slash_star_dot_star:
388         NOTCASE('*', slash_star_dot_star);
389         GETNEXT
390         NOTCASE('/', __slash_star_dot_star);
391         goto start;
392
393 /* '.*?' */
394 squote:
395         GETNEXT
396         CASE('\'', start);
397         NOTCASE('\\', squote);
398         GETNEXT
399         goto squote;
400
401 /* ".*?" */
402 dquote:
403         GETNEXT
404         CASE('"', start);
405         NOTCASE('\\', dquote);
406         GETNEXT
407         goto dquote;
408
409 /* #\s* */
410 pound:
411         GETNEXT
412         CASE(' ',  pound);
413         CASE('\t', pound);
414         CASE('i',  pound_i);
415         CASE('d',  pound_d);
416         CASE('u',  pound_u);
417         goto __start;
418
419 /* #\s*i */
420 pound_i:
421         GETNEXT NOTCASE('n', __start);
422         GETNEXT NOTCASE('c', __start);
423         GETNEXT NOTCASE('l', __start);
424         GETNEXT NOTCASE('u', __start);
425         GETNEXT NOTCASE('d', __start);
426         GETNEXT NOTCASE('e', __start);
427         goto pound_include;
428
429 /* #\s*include\s* */
430 pound_include:
431         GETNEXT
432         CASE(' ',  pound_include);
433         CASE('\t', pound_include);
434         map_dot = next;
435         CASE('"',  pound_include_dquote);
436         CASE('<',  pound_include_langle);
437         goto __start;
438
439 /* #\s*include\s*"(.*)" */
440 pound_include_dquote:
441         GETNEXT
442         CASE('\n', start);
443         NOTCASE('"', pound_include_dquote);
444         handle_include(0, map_dot, next - map_dot - 1);
445         goto start;
446
447 /* #\s*include\s*<(.*)> */
448 pound_include_langle:
449         GETNEXT
450         CASE('\n', start);
451         NOTCASE('>', pound_include_langle);
452         handle_include(1, map_dot, next - map_dot - 1);
453         goto start;
454
455 /* #\s*d */
456 pound_d:
457         GETNEXT NOTCASE('e', __start);
458         GETNEXT NOTCASE('f', __start);
459         GETNEXT NOTCASE('i', __start);
460         GETNEXT NOTCASE('n', __start);
461         GETNEXT NOTCASE('e', __start);
462         goto pound_define_undef;
463
464 /* #\s*u */
465 pound_u:
466         GETNEXT NOTCASE('n', __start);
467         GETNEXT NOTCASE('d', __start);
468         GETNEXT NOTCASE('e', __start);
469         GETNEXT NOTCASE('f', __start);
470         goto pound_define_undef;
471
472 /*
473  * #\s*(define|undef)\s*CONFIG_(\w*)
474  *
475  * this does not define the word, because it could be inside another
476  * conditional (#if 0).  But I do parse the word so that this instance
477  * does not count as a use.  -- mec
478  */
479 pound_define_undef:
480         GETNEXT
481         CASE(' ',  pound_define_undef);
482         CASE('\t', pound_define_undef);
483
484                 NOTCASE('C', __start);
485         GETNEXT NOTCASE('O', __start);
486         GETNEXT NOTCASE('N', __start);
487         GETNEXT NOTCASE('F', __start);
488         GETNEXT NOTCASE('I', __start);
489         GETNEXT NOTCASE('G', __start);
490         GETNEXT NOTCASE('_', __start);
491
492         map_dot = next;
493 pound_define_undef_CONFIG_word:
494         GETNEXT
495         if (isalnum(current) || current == '_')
496                 goto pound_define_undef_CONFIG_word;
497         goto __start;
498
499 /* \<CONFIG_(\w*) */
500 cee:
501         if (next >= map+2 && (isalnum((int)next[-2]) || next[-2] == '_'))
502                 goto start;
503         GETNEXT NOTCASE('O', __start);
504         GETNEXT NOTCASE('N', __start);
505         GETNEXT NOTCASE('F', __start);
506         GETNEXT NOTCASE('I', __start);
507         GETNEXT NOTCASE('G', __start);
508         GETNEXT NOTCASE('_', __start);
509
510         map_dot = next;
511 cee_CONFIG_word:
512         GETNEXT
513         if (isalnum(current) || current == '_')
514                 goto cee_CONFIG_word;
515         use_config(map_dot, next - map_dot - 1);
516         goto __start;
517     }
518 }
519
520
521
522 /*
523  * Generate dependencies for one file.
524  */
525 void do_depend(const char * filename, const char * command)
526 {
527         int mapsize;
528         int pagesizem1 = getpagesize()-1;
529         int fd;
530         struct stat st;
531         char * map;
532
533         fd = open(filename, O_RDONLY);
534         if (fd < 0) {
535                 perror(filename);
536                 return;
537         }
538
539         fstat(fd, &st);
540         if (st.st_size == 0) {
541                 fprintf(stderr,"%s is empty\n",filename);
542                 close(fd);
543                 return;
544         }
545
546         mapsize = st.st_size;
547         mapsize = (mapsize+pagesizem1) & ~pagesizem1;
548         map = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0);
549         if ((long) map == -1) {
550                 perror("mkdep: mmap");
551                 close(fd);
552                 return;
553         }
554         if ((unsigned long) map % sizeof(unsigned long) != 0)
555         {
556                 fprintf(stderr, "do_depend: map not aligned\n");
557                 exit(1);
558         }
559
560         hasdep = 0;
561         clear_config();
562         state_machine(map, map+st.st_size);
563         if (hasdep) {
564                 puts(command);
565                 if (*command)
566                         define_precious(filename);
567         }
568
569         munmap(map, mapsize);
570         close(fd);
571 }
572
573
574
575 /*
576  * Generate dependencies for all files.
577  */
578 int main(int argc, char **argv)
579 {
580         int len;
581         const char *hpath;
582
583         hpath = getenv("HPATH");
584         if (!hpath) {
585                 fputs("mkdep: HPATH not set in environment.  "
586                       "Don't bypass the top level Makefile.\n", stderr);
587                 return 1;
588         }
589
590         add_path(".");          /* for #include "..." */
591
592         while (++argv, --argc > 0) {
593                 if (strncmp(*argv, "-I", 2) == 0) {
594                         if (*((*argv)+2)) {
595                                 add_path((*argv)+2);
596                         }
597                         else {
598                                 ++argv;
599                                 --argc;
600                                 add_path(*argv);
601                         }
602                 }
603                 else if (strcmp(*argv, "--") == 0) {
604                         break;
605                 }
606         }
607
608         add_path(hpath);        /* must be last entry, for config files */
609
610         while (--argc > 0) {
611                 const char * filename = *++argv;
612                 const char * command  = __depname;
613                 g_filename = 0;
614                 len = strlen(filename);
615                 memcpy(depname, filename, len+1);
616                 if (len > 2 && filename[len-2] == '.') {
617                         if (filename[len-1] == 'c' || filename[len-1] == 'S') {
618                             depname[len-1] = 'o';
619                             g_filename = filename;
620                             command = "";
621                         }
622                 }
623                 do_depend(filename, command);
624         }
625         if (len_precious) {
626                 *(str_precious+len_precious) = '\0';
627                 printf(".PRECIOUS:%s\n", str_precious);
628         }
629         return 0;
630 }