OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [scripts/] [mkdep.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1629 jcastillo
#include <stdio.h>
2
#include <stdlib.h>
3
 
4
#include <string.h>
5
#include <sys/types.h>
6
#include <sys/stat.h>
7
#include <unistd.h>
8
#include <sys/fcntl.h>
9
#include <sys/mman.h>
10
 
11
char *filename, *command, __depname[256] = "\n\t@touch ";
12
int needsconfig, hasconfig, hasdep;
13
int filelen;
14
 
15
#define depname (__depname+9)
16
 
17
struct path_struct {
18
        int len;
19
        char buffer[256-sizeof(int)];
20
} path_array[2] = {
21
/*      { 23, "/usr/src/linux/include/" },
22
*/
23
        { 35, "/home/simons/uClinux/linux/include/" },
24
        {  0, "" }
25
};
26
 
27
static void handle_include(int type, char *name, int len)
28
{
29
        int plen;
30
        struct path_struct *path = path_array+type;
31
 
32
        if (len == 14 && !memcmp(name, "linux/config.h", len))
33
                hasconfig = 1;
34
 
35
        plen = path->len;
36
        memcpy(path->buffer+plen, name, len);
37
        len += plen;
38
        path->buffer[len] = '\0';
39
        if (access(path->buffer, F_OK))
40
                return;
41
 
42
        if (!hasdep) {
43
                hasdep = 1;
44
                printf("%s:", depname);
45
        }
46
        printf(" \\\n   %s", path->buffer);
47
}
48
 
49
static void handle_config(void)
50
{
51
        needsconfig = 1;
52
        if (!hasconfig)
53
                fprintf(stderr,
54
                        "%s needs config but has not included config file\n",
55
                        filename);
56
}
57
 
58
#if defined(__alpha__) || defined(__i386__) || defined(__arm__)
59
#define LE_MACHINE
60
#endif
61
 
62
#ifdef LE_MACHINE
63
#define next_byte(x) (x >>= 8)
64
#define current ((unsigned char) __buf)
65
#else
66
#define next_byte(x) (x <<= 8)
67
#define current (__buf >> 8*(sizeof(unsigned long)-1))
68
#endif
69
 
70
#define GETNEXT { \
71
next_byte(__buf); \
72
if (!__nrbuf) { \
73
        __buf = *(unsigned long *) next; \
74
        __nrbuf = sizeof(unsigned long); \
75
        if (!__buf) \
76
                break; \
77
} if(!(--filelen))break;next++; __nrbuf--; }
78
 
79
#define CASE(c,label) if (current == c) goto label
80
#define NOTCASE(c,label) if (current != c) goto label
81
 
82
static void state_machine(register char *next)
83
{
84
        for(;;) {
85
 
86
        register unsigned long __buf = 0;
87
        register unsigned long __nrbuf = 0;
88
 
89
normal:
90
        GETNEXT
91
__normal:
92
        CASE('/',slash);
93
        CASE('"',string);
94
        CASE('\'',char_const);
95
        CASE('#',preproc);
96
        goto normal;
97
 
98
slash:
99
        GETNEXT
100
        CASE('*',comment);
101
        goto __normal;
102
 
103
string:
104
        GETNEXT
105
        CASE('"',normal);
106
        NOTCASE('\\',string);
107
        GETNEXT
108
        goto string;
109
 
110
char_const:
111
        GETNEXT
112
        CASE('\'',normal);
113
        NOTCASE('\\',char_const);
114
        GETNEXT
115
        goto char_const;
116
 
117
comment:
118
        GETNEXT
119
__comment:
120
        NOTCASE('*',comment);
121
        GETNEXT
122
        CASE('/',normal);
123
        goto __comment;
124
 
125
preproc:
126
        GETNEXT
127
        CASE('\n',normal);
128
        CASE(' ',preproc);
129
        CASE('\t',preproc);
130
        CASE('i',i_preproc);
131
        GETNEXT
132
 
133
skippreproc:
134
        CASE('\n',normal);
135
        CASE('\\',skippreprocslash);
136
        GETNEXT
137
        goto skippreproc;
138
 
139
skippreprocslash:
140
        GETNEXT;
141
        GETNEXT;
142
        goto skippreproc;
143
 
144
i_preproc:
145
        GETNEXT
146
        CASE('f',if_line);
147
        NOTCASE('n',skippreproc);
148
        GETNEXT
149
        NOTCASE('c',skippreproc);
150
        GETNEXT
151
        NOTCASE('l',skippreproc);
152
        GETNEXT
153
        NOTCASE('u',skippreproc);
154
        GETNEXT
155
        NOTCASE('d',skippreproc);
156
        GETNEXT
157
        NOTCASE('e',skippreproc);
158
 
159
/* "# include" found */
160
include_line:
161
        GETNEXT
162
        CASE('\n',normal);
163
        CASE('<', std_include_file);
164
        NOTCASE('"', include_line);
165
 
166
/* "local" include file */
167
{
168
        char *incname = next;
169
local_include_name:
170
        GETNEXT
171
        CASE('\n',normal);
172
        NOTCASE('"', local_include_name);
173
        handle_include(1, incname, next-incname-1);
174
        goto skippreproc;
175
}
176
 
177
/* <std> include file */
178
std_include_file:
179
{
180
        char *incname = next;
181
std_include_name:
182
        GETNEXT
183
        CASE('\n',normal);
184
        NOTCASE('>', std_include_name);
185
        handle_include(0, incname, next-incname-1);
186
        goto skippreproc;
187
}
188
 
189
if_line:
190
        if (needsconfig)
191
                goto skippreproc;
192
if_start:
193
        GETNEXT
194
        CASE('C', config);
195
        CASE('\n', normal);
196
        CASE('_', if_middle);
197
        if (current >= 'a' && current <= 'z')
198
                goto if_middle;
199
        if (current < 'A' || current > 'Z')
200
                goto if_start;
201
config:
202
        GETNEXT
203
        NOTCASE('O', __if_middle);
204
        GETNEXT
205
        NOTCASE('N', __if_middle);
206
        GETNEXT
207
        NOTCASE('F', __if_middle);
208
        GETNEXT
209
        NOTCASE('I', __if_middle);
210
        GETNEXT
211
        NOTCASE('G', __if_middle);
212
        GETNEXT
213
        NOTCASE('_', __if_middle);
214
        handle_config();
215
        goto skippreproc;
216
 
217
if_middle:
218
        GETNEXT
219
__if_middle:
220
        CASE('\n', normal);
221
        CASE('_', if_middle);
222
        if (current >= 'a' && current <= 'z')
223
                goto if_middle;
224
        if (current < 'A' || current > 'Z')
225
                goto if_start;
226
        goto if_middle;
227
        }
228
}
229
 
230
static void do_depend(void)
231
{
232
        char *map;
233
        int mapsize;
234
        int pagesizem1 = getpagesize()-1;
235
        int fd = open(filename, O_RDONLY);
236
        struct stat st;
237
 
238
        if (fd < 0) {
239
                perror("mkdep: open");
240
                return;
241
        }
242
        fstat(fd, &st);
243
/* SIMON */
244
        filelen = st.st_size;
245
        if (0 == (long)st.st_size) {
246
                perror("mkdep: file empty");
247
                close(fd);
248
                return;
249
        }
250
        mapsize = st.st_size + 2*sizeof(unsigned long);
251
        mapsize = (mapsize+pagesizem1) & ~pagesizem1;
252
        map = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0);
253
        if (-1 == (long)map) {
254
                perror("mkdep: mmap");
255
                close(fd);
256
                return;
257
        }
258
        close(fd);
259
        state_machine(map);
260
        munmap(map, mapsize);
261
        if (hasdep)
262
                puts(command);
263
}
264
 
265
int main(int argc, char **argv)
266
{
267
        int len;
268
        char * hpath;
269
 
270
        hpath = getenv("HPATH");
271
        if (!hpath)
272
                hpath = "/home/simons/uClinux/linux/include/";
273
        len = strlen(hpath);
274
        memcpy(path_array[0].buffer, hpath, len);
275
        if (len && hpath[len-1] != '/') {
276
                path_array[0].buffer[len] = '/';
277
                len++;
278
        }
279
        path_array[0].buffer[len] = '\0';
280
        path_array[0].len = len;
281
 
282
        while (--argc > 0) {
283
                int len;
284
                char *name = *++argv;
285
                filename = name;
286
                len = strlen(name);
287
                memcpy(depname, name, len+1);
288
                command = __depname;
289
                if (len > 2 && name[len-2] == '.') {
290
                        switch (name[len-1]) {
291
                                case 'c':
292
                                case 'S':
293
                                        depname[len-1] = 'o';
294
                                        command = "";
295
                        }
296
                }
297
                needsconfig = hasconfig = hasdep = 0;
298
                do_depend();
299
                if (hasconfig && !needsconfig)
300
                        fprintf(stderr, "%s doesn't need config\n", filename);
301
        }
302
        return 0;
303
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.