[AGPGART] Suspend/Resume support for ATI GART
[linux-2.6/mini2440.git] / scripts / basic / fixdep.c
blob679124b11e12645b088877fbee8d3d6e901cb24e
1 /*
2 * "Optimize" a list of dependencies as spit out by gcc -MD
3 * for the kernel build
4 * ===========================================================================
6 * Author Kai Germaschewski
7 * Copyright 2002 by Kai Germaschewski <kai.germaschewski@gmx.de>
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
13 * Introduction:
15 * gcc produces a very nice and correct list of dependencies which
16 * tells make when to remake a file.
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>
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.
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.
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.
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 ;-)
39 * The idea above dates, by the way, back to Michael E Chastain, AFAIK.
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.
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.
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.
55 * This code here has been based partially based on mkdep.c, which
56 * says the following about its history:
58 * Copyright abandoned, Michael Chastain, <mailto:mec@shout.net>.
59 * This is a C version of syncdep.pl by Werner Almesberger.
62 * It is invoked as
64 * fixdep <depfile> <target> <cmdline>
66 * and will read the dependency file <depfile>
68 * The transformed dependency snipped is written to stdout.
70 * It first generates a line
72 * cmd_<target> = <cmdline>
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.
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.
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.
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)
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.
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>
117 #define INT_CONF ntohl(0x434f4e46)
118 #define INT_ONFI ntohl(0x4f4e4649)
119 #define INT_NFIG ntohl(0x4e464947)
120 #define INT_FIG_ ntohl(0x4649475f)
122 char *target;
123 char *depfile;
124 char *cmdline;
126 void usage(void)
129 fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
130 exit(1);
134 * Print out the commandline prefixed with cmd_<target filename> :=
135 * If commandline contains '#' escape with '\' so make to not see
136 * the '#' as a start-of-comment symbol
138 void print_cmdline(void)
140 char *p = cmdline;
142 printf("cmd_%s := ", target);
143 for (; *p; p++) {
144 if (*p == '#')
145 printf("\\");
146 printf("%c", *p);
148 printf("\n\n");
151 char * str_config = NULL;
152 int size_config = 0;
153 int len_config = 0;
156 * Grow the configuration string to a desired length.
157 * Usually the first growth is plenty.
159 void grow_config(int len)
161 while (len_config + len > size_config) {
162 if (size_config == 0)
163 size_config = 2048;
164 str_config = realloc(str_config, size_config *= 2);
165 if (str_config == NULL)
166 { perror("fixdep:malloc"); exit(1); }
173 * Lookup a value in the configuration string.
175 int is_defined_config(const char * name, int len)
177 const char * pconfig;
178 const char * plast = str_config + len_config - len;
179 for ( pconfig = str_config + 1; pconfig < plast; pconfig++ ) {
180 if (pconfig[ -1] == '\n'
181 && pconfig[len] == '\n'
182 && !memcmp(pconfig, name, len))
183 return 1;
185 return 0;
189 * Add a new value to the configuration string.
191 void define_config(const char * name, int len)
193 grow_config(len + 1);
195 memcpy(str_config+len_config, name, len);
196 len_config += len;
197 str_config[len_config++] = '\n';
201 * Clear the set of configuration strings.
203 void clear_config(void)
205 len_config = 0;
206 define_config("", 0);
210 * Record the use of a CONFIG_* word.
212 void use_config(char *m, int slen)
214 char s[PATH_MAX];
215 char *p;
217 if (is_defined_config(m, slen))
218 return;
220 define_config(m, slen);
222 memcpy(s, m, slen); s[slen] = 0;
224 for (p = s; p < s + slen; p++) {
225 if (*p == '_')
226 *p = '/';
227 else
228 *p = tolower((int)*p);
230 printf(" $(wildcard include/config/%s.h) \\\n", s);
233 void parse_config_file(char *map, size_t len)
235 int *end = (int *) (map + len);
236 /* start at +1, so that p can never be < map */
237 int *m = (int *) map + 1;
238 char *p, *q;
240 for (; m < end; m++) {
241 if (*m == INT_CONF) { p = (char *) m ; goto conf; }
242 if (*m == INT_ONFI) { p = (char *) m-1; goto conf; }
243 if (*m == INT_NFIG) { p = (char *) m-2; goto conf; }
244 if (*m == INT_FIG_) { p = (char *) m-3; goto conf; }
245 continue;
246 conf:
247 if (p > map + len - 7)
248 continue;
249 if (memcmp(p, "CONFIG_", 7))
250 continue;
251 for (q = p + 7; q < map + len; q++) {
252 if (!(isalnum(*q) || *q == '_'))
253 goto found;
255 continue;
257 found:
258 use_config(p+7, q-p-7);
262 /* test is s ends in sub */
263 int strrcmp(char *s, char *sub)
265 int slen = strlen(s);
266 int sublen = strlen(sub);
268 if (sublen > slen)
269 return 1;
271 return memcmp(s + slen - sublen, sub, sublen);
274 void do_config_file(char *filename)
276 struct stat st;
277 int fd;
278 void *map;
280 fd = open(filename, O_RDONLY);
281 if (fd < 0) {
282 fprintf(stderr, "fixdep: ");
283 perror(filename);
284 exit(2);
286 fstat(fd, &st);
287 if (st.st_size == 0) {
288 close(fd);
289 return;
291 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
292 if ((long) map == -1) {
293 perror("fixdep: mmap");
294 close(fd);
295 return;
298 parse_config_file(map, st.st_size);
300 munmap(map, st.st_size);
302 close(fd);
305 void parse_dep_file(void *map, size_t len)
307 char *m = map;
308 char *end = m + len;
309 char *p;
310 char s[PATH_MAX];
312 p = strchr(m, ':');
313 if (!p) {
314 fprintf(stderr, "fixdep: parse error\n");
315 exit(1);
317 memcpy(s, m, p-m); s[p-m] = 0;
318 printf("deps_%s := \\\n", target);
319 m = p+1;
321 clear_config();
323 while (m < end) {
324 while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))
325 m++;
326 p = m;
327 while (p < end && *p != ' ') p++;
328 if (p == end) {
329 do p--; while (!isalnum(*p));
330 p++;
332 memcpy(s, m, p-m); s[p-m] = 0;
333 if (strrcmp(s, "include/linux/autoconf.h") &&
334 strrcmp(s, "arch/um/include/uml-config.h") &&
335 strrcmp(s, ".ver")) {
336 printf(" %s \\\n", s);
337 do_config_file(s);
339 m = p + 1;
341 printf("\n%s: $(deps_%s)\n\n", target, target);
342 printf("$(deps_%s):\n", target);
345 void print_deps(void)
347 struct stat st;
348 int fd;
349 void *map;
351 fd = open(depfile, O_RDONLY);
352 if (fd < 0) {
353 fprintf(stderr, "fixdep: ");
354 perror(depfile);
355 exit(2);
357 fstat(fd, &st);
358 if (st.st_size == 0) {
359 fprintf(stderr,"fixdep: %s is empty\n",depfile);
360 close(fd);
361 return;
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;
370 parse_dep_file(map, st.st_size);
372 munmap(map, st.st_size);
374 close(fd);
377 void traps(void)
379 static char test[] __attribute__((aligned(sizeof(int)))) = "CONF";
381 if (*(int *)test != INT_CONF) {
382 fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianess? %#x\n",
383 *(int *)test);
384 exit(2);
388 int main(int argc, char *argv[])
390 traps();
392 if (argc != 4)
393 usage();
395 depfile = argv[1];
396 target = argv[2];
397 cmdline = argv[3];
399 print_cmdline();
400 print_deps();
402 return 0;