forked from dmidk/Icepack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakdep.c
executable file
·446 lines (353 loc) · 12.1 KB
/
makdep.c
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
** Print to stdout a dependency list for input file specified on the command
** line. A dependency is anything that is referenced by a "#include"' or
** f90 "use" statement. In addition to these dependencies, write a dependency
** rule of "file.d" for each "file.F" or "file.c". This is to accomodate the
** default "make" procedure for CCM.
**
** The name of the module being "use"d is assumed to be case sensitive even
** though the Fortran language is not. In addition, Fortran source files are
** assumed to end in .F. For example, the statement "use Xxx" will translate
** into a dependency of Xxx.o, and the file searched for will be Xxx.F.
**
** Only files which exist in at least one directory named in the current
** directory or one or more "-I" command line arguments will be considered.
**
** An ANSI C compiler is required to build this code.
*/
#include <stdio.h> /* printf, puts */
#include <stdlib.h> /* malloc, getopt */
#include <string.h> /* strcpy */
#include <unistd.h> /* access */
#include <ctype.h> /* isspace, isalnum, tolower */
#define MAXLEN 256
#define TRUE 1
#define FALSE 0
/*
** Linked list struct used for directories to search, and filenames already
** found.
*/
struct node {
char *name;
struct node *next;
};
/*
** lists of dependencies already found: prevents duplicates.
*/
static struct node *list = NULL; /* For #include */
static struct node *uselist = NULL; /* For use */
static struct node *suffix_list; /* List of Fortran suffixes to look for */
/*
** Function prototypes
*/
static void check (char *, struct node *, char *, int);
static int already_found (char *, struct node *);
int main (int argc, char **argv)
{
int lastdot; /* points to the last . in fname */
int c; /* return from getopt */
int recursive = FALSE; /* flag asks for recursive check:
** i.e. check the thing being #included for #includes */
FILE *fpFname;
char line[MAXLEN]; /* line read from input file */
char doto[MAXLEN]; /* name of .o file (from input file) */
char dotd[MAXLEN]; /* name of .o file (from input file) */
char fullpath[MAXLEN]; /* full pathname to potential dependency for .F files */
char depnam[MAXLEN]; /* dependency name (from #include or use) */
char srcfile[MAXLEN]; /* source file .F name from "use" */
char *lptr; /* points into line */
char *fname; /* input file name from command line */
char *fptr; /* pointer to copy into depnam */
char *relpath; /* input file name or path to it */
struct node *dirlist; /* list of directories to search */
struct node *dirptr; /* loop through dirlist */
struct node *newnode; /* malloc'd node */
struct node *last; /* last entry in #include list of found dependencies */
struct node *uselast; /* last entry in "use" list of found dependencies */
struct node *sptr; /* pointer into suffix_list */
/*
** Always put "." first in Filepath since gnumake will put "." first
** regardless of whether it is specified in VPATH
*/
dirlist = dirptr = (struct node *) malloc (sizeof (struct node));
dirptr->name = (char *) malloc (2);
strcpy (dirptr->name, ".");
dirptr->next = NULL;
/*
** Always look for .F and .F90 files. List can be augmented via "-s" cmd line arg(s).
*/
suffix_list = (struct node *) malloc (sizeof (struct node));
suffix_list-> name = (char *) malloc (3);
strcpy (suffix_list->name, ".F");
suffix_list->next = (struct node *) malloc (sizeof (struct node));
sptr = suffix_list->next;
sptr->name = (char *) malloc (5);
strcpy (sptr->name, ".F90");
sptr->next = NULL;
while ((c = getopt (argc, argv, "I:rs:f")) != -1) {
switch(c) {
case 'f': /* this arg is for backward compatibility */
break;
case 'I':
dirptr->next = (struct node *) malloc (sizeof (struct node));
dirptr = dirptr->next;
dirptr->name = (char *) malloc (strlen (optarg) + 1);
strcpy (dirptr->name, optarg);
dirptr->next = NULL;
break;
case 's':
sptr->next = (struct node *) malloc (sizeof (struct node));
sptr = sptr->next;
sptr->name = (char *) malloc (strlen (optarg) + 2);
strcpy (sptr->name, ".");
strcat (sptr->name, optarg);
sptr->next = NULL;
break;
case 'r':
recursive = TRUE;
break;
case '?': /* Unknown option */
fprintf (stderr, "%s: Unknown option encountered\n", argv[0]);
}
}
if (argc == optind+1) {
relpath = argv[optind];
} else {
fprintf (stderr, "Usage: %s [-Idir] [-r] [-s suffix] file\n", argv[0]);
exit (-1);
}
/*
** Retain only the filename of the input file for which dependencies are
** being generated.
*/
fname = relpath + strlen (relpath) - 1;
while (*fname != '/' && fname > relpath) fname--;
if (*fname == '/') fname++;
/*
** Define the .o file by changing tail to ".o"
*/
strcpy (doto, fname);
for (lastdot = strlen (fname) - 1; doto[lastdot] != '.' && lastdot > 0;
lastdot--);
if (lastdot == 0) {
fprintf (stderr, "Input file %s needs a head\n", fname);
exit (1);
}
doto[lastdot] = '\0';
strcpy (dotd, doto);
strcat (doto, ".o ");
strcat (dotd, ".d ");
/*
** write the blah.o blah.d: blah.F (or .c or whatever) dependency to stdout
*/
fputs (doto , stdout);
fputs (dotd , stdout);
fputs (": " , stdout);
fputs (fname , stdout);
fputs ("\n" , stdout);
if ((fpFname = fopen (relpath, "r")) == NULL) {
fprintf (stderr, "Can't open file %s\n", relpath);
exit (1);
}
while (fgets (line, MAXLEN, fpFname) != NULL) {
/*
** Check for dependencies of the cpp "include" variety. Allow for lines
** of the form "# include"
*/
if (line[0] == '#') {
for (lptr = line+1; isspace (*lptr); lptr++);
if (strncmp (lptr, "include ", 8) == 0) {
for (lptr += 8; *lptr != '<' && *lptr != '"' && *lptr != '\0'; lptr++);
if (*lptr == '\0')
break; /* Bad input line: ignore */
/*
** Fill in depnam with the dependency (i.e. the thing being
** #included. Syntax check is not perfect.
*/
for (fptr = depnam; *++lptr != '>' && *lptr != '"' && *lptr != '\0';
fptr++)
*fptr = *lptr;
if (*lptr == '\0')
break; /* Bad input line: ignore */
*fptr = '\0';
if ( ! already_found (depnam, list)) { /* Skip any duplicates */
/*
** Include only dependencies which are specified by -Ixxx on the
** command line. These directories are defined by the linked list
** pointed to by dirlist.
*/
for (dirptr = dirlist; dirptr != NULL; dirptr = dirptr->next) {
strcpy (fullpath, dirptr->name);
strcat (fullpath, "/");
strcat (fullpath, depnam);
/*
** If the file exists and is readable, add an entry to the "found"
** list, then write a dependency rule to stdout.
*/
if (access (fullpath, R_OK) == 0) {
newnode = malloc (sizeof (struct node));
newnode->name = malloc (strlen (depnam) + 1);
strcpy (newnode->name, depnam);
newnode->next = NULL;
if (list == NULL)
list = newnode;
else
last->next = newnode;
last = newnode;
fputs (doto , stdout);
fputs (": " , stdout);
fputs (depnam, stdout);
fputs ("\n", stdout);
/*
** Check for nested #include's if flag was set
*/
if (recursive) check (fullpath, dirlist, doto, 0);
break; /* Dependency found: process next line */
}
}
}
}
} else {
/*
** Check for dependencies of the f90 "use" variety. To strictly adhere
** to fortran std, should allow for spaces between chars of "use".
*/
for (lptr = line; isspace (*lptr); lptr++);
if (tolower ((int) lptr[0]) == 'u' &&
tolower ((int) lptr[1]) == 's' &&
tolower ((int) lptr[2]) == 'e') {
for (lptr += 3; isspace (*lptr); lptr++);
/*
** Fill in depnam with the dependency (i.e. the thing being "use"d.
** Strictly speaking, should disallow numeric starting character.
*/
for (fptr = depnam; isalnum (*lptr) || *lptr == '_'; (fptr++, lptr++))
*fptr = *lptr;
*fptr = '\0';
/*
** srcfile is the source file name from which the dependency is
** generated. Note case sensitivity of depnam.
*/
if ( ! already_found (depnam, uselist)) { /* Skip any duplicates */
/*
** Loop through suffix list
*/
for (sptr = suffix_list; sptr != NULL; sptr = sptr->next) {
strcpy (srcfile, depnam);
strcat (srcfile, sptr->name);
/*
** Include only dependencies which are specified by -Ixxx on the
** command line. These directories are defined by the linked list
** pointed to by dirlist.
*/
for (dirptr = dirlist; dirptr != NULL; dirptr = dirptr->next) {
strcpy (fullpath, dirptr->name);
strcat (fullpath, "/");
strcat (fullpath, srcfile);
/*
** If the file exists and is readable, add an entry to the "found"
** list, then write a dependency rule to stdout.
*/
if (access (fullpath, R_OK) == 0) {
newnode = malloc (sizeof (struct node));
newnode->name = malloc (strlen (srcfile) + 1);
strcpy (newnode->name, depnam);
newnode->next = NULL;
if (uselist == NULL)
uselist = newnode;
else
uselast->next = newnode;
uselast = newnode;
fputs (doto , stdout);
fputs (": " , stdout);
fputs (depnam, stdout);
fputs (".o" , stdout);
fputs ("\n" , stdout);
goto read_next_line; /* Dependency found: process next line */
} /* if (access (fullpath... */
} /* loop through linked list of directories from Filepath */
} /* loop through linked list of suffixes */
} /* if ( ! already_found (srcfile... */
} /* if (lptr points to "use " */
} /* else branch of if (line[0] == '#') */
read_next_line:
continue;
} /* Looping over lines in the file */
fclose (fpFname);
return (0);
}
void check (char *file, struct node *dirlist, char *doto, int recurse_level)
{
FILE *fpFile;
char line[MAXLEN], fullpath[MAXLEN];
char depnam[MAXLEN];
char *lptr, *fptr;
struct node *dirptr;
/*
** Don't bother checking beyond 3 levels of recursion
*/
if (recurse_level > 3) {
fprintf (stderr, "More than 3 levels of recursion detected: bailing out\n");
return;
}
if ((fpFile = fopen (file, "r")) == NULL) {
fprintf (stderr, "Can't open file %s\n", file);
exit (1);
}
while (fgets (line, MAXLEN, fpFile) != NULL) {
/*
** Check for dependencies of the cpp "include" variety. Allow for lines
** of the form "# include"
*/
if (line[0] == '#') {
for (lptr = line+1; isspace (*lptr); lptr++);
if (strncmp (lptr, "include ", 8) == 0) {
for (lptr += 8; *lptr != '<' && *lptr != '"' && *lptr != '\0'; lptr++);
if (*lptr == '\0')
break; /* Bad input line: ignore */
/*
** Fill in depnam with the dependency (i.e. the thing being
** #included. Syntax check is not perfect.
*/
for (fptr = depnam; *++lptr != '>' && *lptr != '"' && *lptr != '\0'; fptr++)
*fptr = *lptr;
if (*lptr == '\0')
break; /* Bad input line: ignore */
*fptr = '\0';
/*
** Don't include dependencies which are not in the Filepath
*/
for (dirptr = dirlist; dirptr != NULL; dirptr = dirptr->next) {
strcpy (fullpath, dirptr->name);
strcat (fullpath, "/");
strcat (fullpath, depnam);
/*
** If the file exists and is readable, add an entry to the "found"
** list, then write a dependency rule to stdout.
*/
if (access (fullpath, R_OK) == 0) {
fputs (doto , stdout);
fputs (": " , stdout);
fputs (depnam, stdout);
fputs ("\n", stdout);
/*
** Check for nested #include's
*/
check (fullpath, dirlist, doto, recurse_level+1);
break;
}
}
}
}
}
fclose (fpFile);
return;
}
int already_found (char *name, struct node *list)
{
struct node *ptr;
for (ptr = list; ptr != NULL; ptr = ptr->next) {
if (strcmp (ptr->name, name) == 0) return (1);
}
return (0);
}