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