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