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#include <sys/types.h>
98#include <sys/stat.h>
99#include <unistd.h>
100#include <fcntl.h>
101#include <string.h>
102#include <stdlib.h>
103#include <stdio.h>
104#include <ctype.h>
105
106static void usage(void)
107{
108 fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
109 exit(1);
110}
111
112
113
114
115static void print_dep(const char *m, int slen, const char *dir)
116{
117 int c, prev_c = '/', i;
118
119 printf(" $(wildcard %s/", dir);
120 for (i = 0; i < slen; i++) {
121 c = m[i];
122 if (c == '_')
123 c = '/';
124 else
125 c = tolower(c);
126 if (c != '/' || prev_c != '/')
127 putchar(c);
128 prev_c = c;
129 }
130 printf(".h) \\\n");
131}
132
133struct item {
134 struct item *next;
135 unsigned int len;
136 unsigned int hash;
137 char name[0];
138};
139
140#define HASHSZ 256
141static struct item *hashtab[HASHSZ];
142
143static unsigned int strhash(const char *str, unsigned int sz)
144{
145
146 unsigned int i, hash = 2166136261U;
147
148 for (i = 0; i < sz; i++)
149 hash = (hash ^ str[i]) * 0x01000193;
150 return hash;
151}
152
153
154
155
156static int is_defined_config(const char *name, int len, unsigned int hash)
157{
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)
163 return 1;
164 }
165 return 0;
166}
167
168
169
170
171static void define_config(const char *name, int len, unsigned int hash)
172{
173 struct item *aux = malloc(sizeof(*aux) + len);
174
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;
184}
185
186
187
188
189static void use_config(const char *m, int slen)
190{
191 unsigned int hash = strhash(m, slen);
192
193 if (is_defined_config(m, slen, hash))
194 return;
195
196 define_config(m, slen, hash);
197 print_dep(m, slen, "include/config");
198}
199
200
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
211static void parse_config_file(const char *p)
212{
213 const char *q, *r;
214 const char *start = p;
215
216 while ((p = strstr(p, "CONFIG_"))) {
217 if (p > start && (isalnum(p[-1]) || p[-1] == '_')) {
218 p += 7;
219 continue;
220 }
221 p += 7;
222 q = p;
223 while (*q && (isalnum(*q) || *q == '_'))
224 q++;
225 if (str_ends_with(p, q - p, "_MODULE"))
226 r = q - 7;
227 else
228 r = q;
229 if (r > p)
230 use_config(p, r - p);
231 p = q;
232 }
233}
234
235static void *read_file(const char *filename)
236{
237 struct stat st;
238 int fd;
239 char *buf;
240
241 fd = open(filename, O_RDONLY);
242 if (fd < 0) {
243 fprintf(stderr, "fixdep: error opening file: ");
244 perror(filename);
245 exit(2);
246 }
247 if (fstat(fd, &st) < 0) {
248 fprintf(stderr, "fixdep: error fstat'ing file: ");
249 perror(filename);
250 exit(2);
251 }
252 buf = malloc(st.st_size + 1);
253 if (!buf) {
254 perror("fixdep: malloc");
255 exit(2);
256 }
257 if (read(fd, buf, st.st_size) != st.st_size) {
258 perror("fixdep: read");
259 exit(2);
260 }
261 buf[st.st_size] = '\0';
262 close(fd);
263
264 return buf;
265}
266
267
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") ||
272 str_ends_with(s, len, ".ver");
273}
274
275
276
277
278
279
280static void parse_dep_file(char *m, const char *target)
281{
282 char *p;
283 int is_last, is_target;
284 int saw_any_target = 0;
285 int is_first_dep = 0;
286 void *buf;
287
288 while (1) {
289
290 while (*m == ' ' || *m == '\\' || *m == '\n')
291 m++;
292
293 if (!*m)
294 break;
295
296
297 p = m;
298 while (*p && *p != ' ' && *p != '\\' && *p != '\n')
299 p++;
300 is_last = (*p == '\0');
301
302 is_target = (*(p-1) == ':');
303
304 if (is_target) {
305
306 is_first_dep = 1;
307 } else if (!is_ignored_file(m, p - m)) {
308 *p = '\0';
309
310
311
312
313
314
315
316 if (is_first_dep) {
317
318
319
320
321
322
323
324
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);
334 }
335
336 buf = read_file(m);
337 parse_config_file(buf);
338 free(buf);
339 }
340
341 if (is_last)
342 break;
343
344
345
346
347
348 m = p + 1;
349 }
350
351 if (!saw_any_target) {
352 fprintf(stderr, "fixdep: parse error; no targets found\n");
353 exit(1);
354 }
355
356 printf("\n%s: $(deps_%s)\n\n", target, target);
357 printf("$(deps_%s):\n", target);
358}
359
360int main(int argc, char *argv[])
361{
362 const char *depfile, *target, *cmdline;
363 void *buf;
364
365 if (argc != 4)
366 usage();
367
368 depfile = argv[1];
369 target = argv[2];
370 cmdline = argv[3];
371
372 printf("cmd_%s := %s\n\n", target, cmdline);
373
374 buf = read_file(depfile);
375 parse_dep_file(buf, target);
376 free(buf);
377
378 return 0;
379}
380