]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - scripts/basic/fixdep.c
xfrm interface: fix list corruption for x-netns
[mirror_ubuntu-eoan-kernel.git] / scripts / basic / fixdep.c
CommitLineData
1da177e4
LT
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
264a2683 19 * every file in the kernel includes autoconf.h.
1da177e4 20 *
264a2683 21 * If the user re-runs make *config, autoconf.h will be
1da177e4
LT
22 * regenerated. make notices that and will rebuild every file which
23 * includes autoconf.h, i.e. basically all files. This is extremely
24 * annoying if the user just changed CONFIG_HIS_DRIVER from n to m.
25 *
26 * So we play the same trick that "mkdep" played before. We replace
264a2683 27 * the dependency on autoconf.h by a dependency on every config
4e433fc4 28 * option which is mentioned in any of the listed prerequisites.
1da177e4 29 *
c21b1e4d
JB
30 * kconfig populates a tree in include/config/ with an empty file
31 * for each config symbol and when the configuration is updated
32 * the files representing changed config options are touched
33 * which then let make pick up the changes and the files that use
34 * the config symbols are rebuilt.
1da177e4
LT
35 *
36 * So if the user changes his CONFIG_HIS_DRIVER option, only the objects
4e433fc4 37 * which depend on "include/config/his/driver.h" will be rebuilt,
1da177e4
LT
38 * so most likely only his driver ;-)
39 *
40 * The idea above dates, by the way, back to Michael E Chastain, AFAIK.
41 *
42 * So to get dependencies right, there are two issues:
43 * o if any of the files the compiler read changed, we need to rebuild
44 * o if the command line given to the compile the file changed, we
45 * better rebuild as well.
46 *
47 * The former is handled by using the -MD output, the later by saving
48 * the command line used to compile the old object and comparing it
49 * to the one we would now use.
50 *
51 * Again, also this idea is pretty old and has been discussed on
52 * kbuild-devel a long time ago. I don't have a sensibly working
53 * internet connection right now, so I rather don't mention names
54 * without double checking.
55 *
56 * This code here has been based partially based on mkdep.c, which
57 * says the following about its history:
58 *
59 * Copyright abandoned, Michael Chastain, <mailto:mec@shout.net>.
60 * This is a C version of syncdep.pl by Werner Almesberger.
61 *
62 *
63 * It is invoked as
64 *
65 * fixdep <depfile> <target> <cmdline>
66 *
67 * and will read the dependency file <depfile>
68 *
69 * The transformed dependency snipped is written to stdout.
70 *
71 * It first generates a line
72 *
73 * cmd_<target> = <cmdline>
74 *
75 * and then basically copies the .<target>.d file to stdout, in the
264a2683 76 * process filtering out the dependency on autoconf.h and adding
1da177e4 77 * dependencies on include/config/my/option.h for every
4e433fc4 78 * CONFIG_MY_OPTION encountered in any of the prerequisites.
1da177e4
LT
79 *
80 * It will also filter out all the dependencies on *.ver. We need
81 * to make sure that the generated version checksum are globally up
82 * to date before even starting the recursive build, so it's too late
83 * at this point anyway.
84 *
dee81e98 85 * We don't even try to really parse the header files, but
1da177e4
LT
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 */
1da177e4
LT
96
97#include <sys/types.h>
98#include <sys/stat.h>
1da177e4
LT
99#include <unistd.h>
100#include <fcntl.h>
101#include <string.h>
102#include <stdlib.h>
103#include <stdio.h>
1da177e4 104#include <ctype.h>
1da177e4 105
4356f489 106static void usage(void)
1da177e4 107{
bbda5ec6 108 fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
1da177e4
LT
109 exit(1);
110}
111
d8329e35
NP
112/*
113 * Print out a dependency path from a symbol name
114 */
fbfa9be9 115static void print_dep(const char *m, int slen, const char *dir)
d8329e35 116{
b3aa58d2 117 int c, prev_c = '/', i;
d8329e35 118
fbfa9be9 119 printf(" $(wildcard %s/", dir);
d8329e35
NP
120 for (i = 0; i < slen; i++) {
121 c = m[i];
122 if (c == '_')
123 c = '/';
124 else
125 c = tolower(c);
b3aa58d2
NP
126 if (c != '/' || prev_c != '/')
127 putchar(c);
128 prev_c = c;
d8329e35
NP
129 }
130 printf(".h) \\\n");
131}
132
8af27e1d
ED
133struct item {
134 struct item *next;
135 unsigned int len;
136 unsigned int hash;
137 char name[0];
138};
1da177e4 139
8af27e1d
ED
140#define HASHSZ 256
141static struct item *hashtab[HASHSZ];
1da177e4 142
8af27e1d
ED
143static unsigned int strhash(const char *str, unsigned int sz)
144{
145 /* fnv32 hash */
146 unsigned int i, hash = 2166136261U;
1da177e4 147
8af27e1d
ED
148 for (i = 0; i < sz; i++)
149 hash = (hash ^ str[i]) * 0x01000193;
150 return hash;
151}
1da177e4
LT
152
153/*
154 * Lookup a value in the configuration string.
155 */
8af27e1d 156static int is_defined_config(const char *name, int len, unsigned int hash)
1da177e4 157{
8af27e1d
ED
158 struct item *aux;
159
160 for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
161 if (aux->hash == hash && aux->len == len &&
162 memcmp(aux->name, name, len) == 0)
1da177e4
LT
163 return 1;
164 }
165 return 0;
166}
167
168/*
169 * Add a new value to the configuration string.
170 */
8af27e1d 171static void define_config(const char *name, int len, unsigned int hash)
1da177e4 172{
8af27e1d 173 struct item *aux = malloc(sizeof(*aux) + len);
1da177e4 174
8af27e1d
ED
175 if (!aux) {
176 perror("fixdep:malloc");
177 exit(1);
178 }
179 memcpy(aux->name, name, len);
180 aux->len = len;
181 aux->hash = hash;
182 aux->next = hashtab[hash % HASHSZ];
183 hashtab[hash % HASHSZ] = aux;
1da177e4
LT
184}
185
1da177e4
LT
186/*
187 * Record the use of a CONFIG_* word.
188 */
8af27e1d 189static void use_config(const char *m, int slen)
1da177e4 190{
8af27e1d 191 unsigned int hash = strhash(m, slen);
1da177e4 192
8af27e1d 193 if (is_defined_config(m, slen, hash))
1da177e4
LT
194 return;
195
8af27e1d 196 define_config(m, slen, hash);
fbfa9be9 197 print_dep(m, slen, "include/config");
1da177e4
LT
198}
199
ab9ce9fe
MY
200/* test if s ends in sub */
201static int str_ends_with(const char *s, int slen, const char *sub)
202{
203 int sublen = strlen(sub);
204
205 if (sublen > slen)
206 return 0;
207
208 return !memcmp(s + slen - sublen, sub, sublen);
209}
210
dee81e98 211static void parse_config_file(const char *p)
1da177e4 212{
dee81e98 213 const char *q, *r;
5b8ad96d 214 const char *start = p;
dee81e98
AD
215
216 while ((p = strstr(p, "CONFIG_"))) {
5b8ad96d
RV
217 if (p > start && (isalnum(p[-1]) || p[-1] == '_')) {
218 p += 7;
219 continue;
220 }
d7211096 221 p += 7;
dee81e98
AD
222 q = p;
223 while (*q && (isalnum(*q) || *q == '_'))
224 q++;
ab9ce9fe 225 if (str_ends_with(p, q - p, "_MODULE"))
dee81e98
AD
226 r = q - 7;
227 else
228 r = q;
229 if (r > p)
230 use_config(p, r - p);
231 p = q;
1da177e4
LT
232 }
233}
234
4003fd80 235static void *read_file(const char *filename)
1da177e4
LT
236{
237 struct stat st;
238 int fd;
4003fd80 239 char *buf;
1da177e4
LT
240
241 fd = open(filename, O_RDONLY);
242 if (fd < 0) {
4003fd80 243 fprintf(stderr, "fixdep: error opening file: ");
1da177e4
LT
244 perror(filename);
245 exit(2);
246 }
46fe94ad 247 if (fstat(fd, &st) < 0) {
4003fd80 248 fprintf(stderr, "fixdep: error fstat'ing file: ");
46fe94ad
TR
249 perror(filename);
250 exit(2);
251 }
4003fd80
MY
252 buf = malloc(st.st_size + 1);
253 if (!buf) {
dee81e98 254 perror("fixdep: malloc");
7c2ec43a 255 exit(2);
1da177e4 256 }
4003fd80 257 if (read(fd, buf, st.st_size) != st.st_size) {
dee81e98 258 perror("fixdep: read");
7c2ec43a 259 exit(2);
dee81e98 260 }
4003fd80 261 buf[st.st_size] = '\0';
dee81e98 262 close(fd);
1da177e4 263
4003fd80 264 return buf;
1da177e4
LT
265}
266
87b95a81
MY
267/* Ignore certain dependencies */
268static int is_ignored_file(const char *s, int len)
269{
270 return str_ends_with(s, len, "include/generated/autoconf.h") ||
271 str_ends_with(s, len, "include/generated/autoksyms.h") ||
87b95a81
MY
272 str_ends_with(s, len, ".ver");
273}
274
7840fea2
MM
275/*
276 * Important: The below generated source_foo.o and deps_foo.o variable
277 * assignments are parsed not only by make, but also by the rather simple
278 * parser in scripts/mod/sumversion.c.
279 */
bbda5ec6 280static void parse_dep_file(char *m, const char *target)
1da177e4 281{
48b9d03c 282 char *p;
01b5cbe7 283 int is_last, is_target;
2ab8a996
SW
284 int saw_any_target = 0;
285 int is_first_dep = 0;
4003fd80 286 void *buf;
1da177e4 287
01b5cbe7 288 while (1) {
2ab8a996 289 /* Skip any "white space" */
01b5cbe7 290 while (*m == ' ' || *m == '\\' || *m == '\n')
1da177e4 291 m++;
01b5cbe7
MY
292
293 if (!*m)
294 break;
295
2ab8a996 296 /* Find next "white space" */
1da177e4 297 p = m;
01b5cbe7 298 while (*p && *p != ' ' && *p != '\\' && *p != '\n')
1da177e4 299 p++;
01b5cbe7 300 is_last = (*p == '\0');
2ab8a996
SW
301 /* Is the token we found a target name? */
302 is_target = (*(p-1) == ':');
303 /* Don't write any target names into the dependency file */
304 if (is_target) {
305 /* The /next/ file is the first dependency */
306 is_first_dep = 1;
87b95a81 307 } else if (!is_ignored_file(m, p - m)) {
ccfe7887 308 *p = '\0';
2ab8a996 309
87b95a81
MY
310 /*
311 * Do not list the source file as dependency, so that
312 * kbuild is not confused if a .c file is rewritten
313 * into .S or vice versa. Storing it in source_* is
314 * needed for modpost to compute srcversions.
315 */
316 if (is_first_dep) {
2ab8a996 317 /*
87b95a81
MY
318 * If processing the concatenation of multiple
319 * dependency files, only process the first
320 * target name, which will be the original
321 * source name, and ignore any other target
322 * names, which will be intermediate temporary
323 * files.
2ab8a996 324 */
87b95a81
MY
325 if (!saw_any_target) {
326 saw_any_target = 1;
327 printf("source_%s := %s\n\n",
328 target, m);
329 printf("deps_%s := \\\n", target);
330 }
331 is_first_dep = 0;
332 } else {
333 printf(" %s \\\n", m);
2ab8a996 334 }
87b95a81
MY
335
336 buf = read_file(m);
337 parse_config_file(buf);
338 free(buf);
1da177e4 339 }
01b5cbe7
MY
340
341 if (is_last)
342 break;
343
2ab8a996
SW
344 /*
345 * Start searching for next token immediately after the first
346 * "whitespace" character that follows this token.
347 */
1da177e4
LT
348 m = p + 1;
349 }
2ab8a996
SW
350
351 if (!saw_any_target) {
352 fprintf(stderr, "fixdep: parse error; no targets found\n");
353 exit(1);
354 }
355
1da177e4
LT
356 printf("\n%s: $(deps_%s)\n\n", target, target);
357 printf("$(deps_%s):\n", target);
358}
359
1da177e4
LT
360int main(int argc, char *argv[])
361{
5d1ef76f 362 const char *depfile, *target, *cmdline;
4003fd80
MY
363 void *buf;
364
bbda5ec6 365 if (argc != 4)
1da177e4
LT
366 usage();
367
368 depfile = argv[1];
369 target = argv[2];
370 cmdline = argv[3];
371
5d1ef76f 372 printf("cmd_%s := %s\n\n", target, cmdline);
4003fd80
MY
373
374 buf = read_file(depfile);
bbda5ec6 375 parse_dep_file(buf, target);
4003fd80 376 free(buf);
1da177e4
LT
377
378 return 0;
379}