1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107#include <sys/types.h>
108#include <sys/stat.h>
109#include <sys/mman.h>
110#include <unistd.h>
111#include <fcntl.h>
112#include <string.h>
113#include <stdlib.h>
114#include <stdio.h>
115#include <limits.h>
116#include <ctype.h>
117#include <arpa/inet.h>
118
119#define INT_CONF ntohl(0x434f4e46)
120#define INT_ONFI ntohl(0x4f4e4649)
121#define INT_NFIG ntohl(0x4e464947)
122#define INT_FIG_ ntohl(0x4649475f)
123
124char *target;
125char *depfile;
126char *cmdline;
127
128void usage(void)
129
130{
131 fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
132 exit(1);
133}
134
135
136
137
138void print_cmdline(void)
139{
140 printf("cmd_%s := %s\n\n", target, cmdline);
141}
142
143char * str_config = NULL;
144int size_config = 0;
145int len_config = 0;
146
147
148
149
150
151void 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
166
167int 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
182
183void 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
194
195void clear_config(void)
196{
197 len_config = 0;
198 define_config("", 0);
199}
200
201
202
203
204void 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
225void parse_config_file(char *map, size_t len)
226{
227 int *end = (int *) (map + len);
228
229 int *m = (int *) map + 1;
230 char *p, *q;
231
232 for (; m < end; m++) {
233 if (*m == INT_CONF) { p = (char *) m ; goto conf; }
234 if (*m == INT_ONFI) { p = (char *) m-1; goto conf; }
235 if (*m == INT_NFIG) { p = (char *) m-2; goto conf; }
236 if (*m == INT_FIG_) { p = (char *) m-3; goto conf; }
237 continue;
238 conf:
239 if (p > map + len - 7)
240 continue;
241 if (memcmp(p, "CONFIG_", 7))
242 continue;
243 for (q = p + 7; q < map + len; q++) {
244 if (!(isalnum(*q) || *q == '_'))
245 goto found;
246 }
247 continue;
248
249 found:
250 if (!memcmp(q - 7, "_MODULE", 7))
251 q -= 7;
252 if( (q-p-7) < 0 )
253 continue;
254 use_config(p+7, q-p-7);
255 }
256}
257
258
259int strrcmp(char *s, char *sub)
260{
261 int slen = strlen(s);
262 int sublen = strlen(sub);
263
264 if (sublen > slen)
265 return 1;
266
267 return memcmp(s + slen - sublen, sub, sublen);
268}
269
270void do_config_file(char *filename)
271{
272 struct stat st;
273 int fd;
274 void *map;
275
276 fd = open(filename, O_RDONLY);
277 if (fd < 0) {
278 fprintf(stderr, "fixdep: ");
279 perror(filename);
280 exit(2);
281 }
282 fstat(fd, &st);
283 if (st.st_size == 0) {
284 close(fd);
285 return;
286 }
287 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
288 if ((long) map == -1) {
289 perror("fixdep: mmap");
290 close(fd);
291 return;
292 }
293
294 parse_config_file(map, st.st_size);
295
296 munmap(map, st.st_size);
297
298 close(fd);
299}
300
301void parse_dep_file(void *map, size_t len)
302{
303 char *m = map;
304 char *end = m + len;
305 char *p;
306 char s[PATH_MAX];
307
308 p = strchr(m, ':');
309 if (!p) {
310 fprintf(stderr, "fixdep: parse error\n");
311 exit(1);
312 }
313 memcpy(s, m, p-m); s[p-m] = 0;
314 printf("deps_%s := \\\n", target);
315 m = p+1;
316
317 clear_config();
318
319 while (m < end) {
320 while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))
321 m++;
322 p = m;
323 while (p < end && *p != ' ') p++;
324 if (p == end) {
325 do p--; while (!isalnum(*p));
326 p++;
327 }
328 memcpy(s, m, p-m); s[p-m] = 0;
329 if (strrcmp(s, "include/linux/autoconf.h") &&
330 strrcmp(s, "arch/um/include/uml-config.h") &&
331 strrcmp(s, ".ver")) {
332 printf(" %s \\\n", s);
333 do_config_file(s);
334 }
335 m = p + 1;
336 }
337 printf("\n%s: $(deps_%s)\n\n", target, target);
338 printf("$(deps_%s):\n", target);
339}
340
341void print_deps(void)
342{
343 struct stat st;
344 int fd;
345 void *map;
346
347 fd = open(depfile, O_RDONLY);
348 if (fd < 0) {
349 fprintf(stderr, "fixdep: ");
350 perror(depfile);
351 exit(2);
352 }
353 fstat(fd, &st);
354 if (st.st_size == 0) {
355 fprintf(stderr,"fixdep: %s is empty\n",depfile);
356 close(fd);
357 return;
358 }
359 map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
360 if ((long) map == -1) {
361 perror("fixdep: mmap");
362 close(fd);
363 return;
364 }
365
366 parse_dep_file(map, st.st_size);
367
368 munmap(map, st.st_size);
369
370 close(fd);
371}
372
373void traps(void)
374{
375 static char test[] __attribute__((aligned(sizeof(int)))) = "CONF";
376
377 if (*(int *)test != INT_CONF) {
378 fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianess? %#x\n",
379 *(int *)test);
380 exit(2);
381 }
382}
383
384int main(int argc, char *argv[])
385{
386 traps();
387
388 if (argc != 4)
389 usage();
390
391 depfile = argv[1];
392 target = argv[2];
393 cmdline = argv[3];
394
395 print_cmdline();
396 print_deps();
397
398 return 0;
399}
400