-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathliblist.c
649 lines (598 loc) · 16 KB
/
liblist.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*
Given a "libname.map" file on standard input and a list or directory
of .d dependency files liblist produces:
a) without -l option, a library list ordered according to the libname.map,
giving a warning if conflicting dependencies are found in the .d files.
b) with -l option, another libname.map, ordered according firstly to the
original libname.map file, then reordered according to the dependencies
found in the .d files. This option is used for compiling the file
libname.map from all the dependencies.
c) with -m <lpath> option, the whole existing libraries list ordered
according to the libname.map, where libraries are placed in <lpath>.
The .d files are specified in the argument(s).
The libname.map is on standard input.
Usage:
liblist *.d < libname.map
liblist -d <ddir> < libname.map
liblist -l *.d < libname.map
liblist -ld <ddir> < libname.map
liblist -l -d <ddir> < libname.map
liblist -m <lpath> < libname.map
where:
<ddir> is a directory name of a directory which is recursively
searched for dependency files
<lpath> is the path where libraries are located
Frank Behner, John Allison 13th February 1999.
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#define BUFSIZE 1000000
#define TRIGSIZE 1000
#define NLIBMAX 200
extern char *optarg;
extern int optind, opterr, optopt;
char** parsedir(char *directory,int *argc)
{
DIR *actualdir;
FILE *actualfile;
struct dirent *entry;
char *buffer=0;
struct stat status;
char **targv=0,**ptr,**phelp;
int len,targc,s;
/*Open the actual directory*/
actualdir=opendir(directory);
if(!actualdir) return targv;
/*Loop over all entries */
for(entry=readdir(actualdir);entry!=NULL;entry=readdir(actualdir))
{
/* Throw away . and .. */
if(strcmp(entry->d_name,".")==0 ||
strcmp(entry->d_name,"..")==0) continue;
/* Obtain the status information of that entry */
if(buffer) free(buffer);
buffer=(char*) malloc((strlen(directory)+
strlen(entry->d_name)+2)*sizeof(char));
strcpy(buffer,directory);
strcat(buffer,"/");
strcat(buffer,entry->d_name);
s=stat(buffer,&status);
if(s==0)
{
if(S_ISDIR(status.st_mode))
{
/* a directory, so we are going recursive*/
targc=0;
ptr=parsedir(buffer,&targc);
if(targc)
{
phelp=targv;
targv=(char**) malloc((*argc+targc)*sizeof(char*));
memcpy(targv,phelp,*argc*sizeof(char*));
memcpy(&targv[*argc],ptr,targc*sizeof(char*));
*argc+=targc;
free(phelp);
free(ptr);
}
}
else if(S_ISREG(status.st_mode))
{
/* a regular file is it .d? */
len=strlen(entry->d_name);
if(entry->d_name[len-2]=='.' && entry->d_name[len-1]=='d')
{
phelp=targv;
targv=(char**) malloc((*argc+1)*sizeof(char*));
memcpy(targv,phelp,*argc*sizeof(char*));
targv[*argc]=strdup(buffer);
(*argc)++;
free(phelp);
}
}
}
else
{
fprintf
(stderr,
" No status - perhaps file %s does not exist.\n",
directory);
exit(1);
}
}
if(buffer) free(buffer);
closedir(actualdir);
return targv;
}
int main (int argc, char** argv) {
char static buffer[BUFSIZE],*bufferPtr,workbuf[256];
char *ptr,*p,**pp,**pp1,**pp2,*directory=0,*libpath=0;
char **rargv;
char *libname=0;
int i,optl=0,optm=0,swapping,c,rargc;
FILE *fp;
#if defined ( _WIN32 ) || defined ( __CYGWIN__ ) || defined ( __CYGWIN32__ )
char *ntg4tmp=0,*ntg4tmp1=0;
int nti;
#endif
struct libmap_
{
char *lib; /* Library name, e.g., G4run. */
char *trigger; /* Source directory, e.g., source/run/. */
int used; /* True if used by these dependency files. */
char **uses; /* List of library names which this library uses. */
struct libmap_ *next;
};
struct libmap_ *libmap=0,*libmapPtr=0,*libmapPtr1=0,*libmapPtr2=0,
*prevPtr1,*prevPtr2,*tmpp,*userLibmapPtr;
while((c=getopt(argc,argv,"ld: m:"))!=EOF)
{
switch(c)
{
case 'l':
optl=1;
break;
case 'd':
directory=strdup(optarg);
break;
case 'm':
optm=1;
libpath=strdup(optarg);
break;
}
}
/*Adjust parameters after parsing options */
if(optind<argc)
{
rargv=&argv[optind];
rargc=argc-optind;
}
else
{
rargv=0;
rargc=0;
}
if(directory)
{
if(rargc==0)
{
rargv=parsedir(directory,&rargc);
}
else
{
fprintf
(stderr,
" ERROR: If you specify a directory don't also specify files\n");
exit(1);
}
}
if(optl)fprintf(stderr," Reading library name map file...\n");
while (!feof(stdin))
{
/* Get library name... */
fgets(buffer,BUFSIZE,stdin);
if(feof(stdin)) break;
if (strlen(buffer) >= BUFSIZE-1)
{
fprintf(stderr,
" Internal ERROR: BUFSIZE too small to read library name map file\n");
exit(1);
}
/* discarded trailing \n, as gets() was doing */
if ( buffer[strlen(buffer)-1] == '\n')
{ buffer[strlen(buffer)-1]='\0'; }
ptr=strtok(buffer,":\n");
/* Check for duplicate names... */
for(libmapPtr1=libmap;libmapPtr1;libmapPtr1=libmapPtr1->next)
{
if(strcmp(libmapPtr1->lib,ptr)==0)
{
fprintf(stderr," ERROR: Duplicate library name: %s\n",ptr);
fprintf(stderr,
" Perhaps a duplicate subdirectory with"
" a GNUmakefile with the same library name.\n"
);
exit(1);
}
}
if(libmap)
{
libmapPtr->next=(struct libmap_*) malloc(sizeof(struct libmap_));
libmapPtr=libmapPtr->next;
}
else /* First time through anchor libmapPtr to libmap. */
{
libmap=(struct libmap_*) malloc(sizeof(struct libmap_));
libmapPtr=libmap;
}
libmapPtr->next=0;
libmapPtr->lib=strdup(ptr);
libmapPtr->used=0;
libmapPtr->uses=(char**)calloc(NLIBMAX,sizeof(char*));
/* If option -l not specified, fill uses list... */
if(!optl && !optm)
{
pp=libmapPtr->uses;
if(ptr)
{
ptr=strtok(NULL," \n");
while (ptr)
{
*pp=strdup(ptr);
pp++;
ptr=strtok(NULL," \n");
}
}
}
if(!optm)
{
/* Get directory name... */
fgets(buffer,BUFSIZE,stdin);
if (strlen(buffer) >= BUFSIZE-1)
{
fprintf(stderr,
" Internal ERROR: BUFSIZE too small to read directory name\n");
exit(1);
}
/* discarded trailing \n, as gets() was doing */
if ( buffer[strlen(buffer)-1] == '\n')
{ buffer[strlen(buffer)-1]='\0'; }
ptr=strtok(buffer,"/");
if(!ptr)
{
fprintf(stderr," ERROR: \"/\" before \"source\" expected.\n");
exit(1);
}
while(ptr&&strcmp (ptr,"source"))ptr=strtok(NULL,"/");
ptr=strtok(NULL,"/");
if(!ptr)
{
fprintf(stderr," ERROR: \"source\" expected.\n");
exit(1);
}
libmapPtr->trigger=(char*)malloc(TRIGSIZE);
if(strlen(ptr)>TRIGSIZE)
{
fprintf(stderr," ERROR: String overflow for: %s\n", ptr);
exit(1);
}
strcpy(libmapPtr->trigger,ptr);
ptr=strtok(NULL,"/");
while(ptr&&strcmp(ptr,"GNUmakefile"))
{
strcat(libmapPtr->trigger,"/");
strcat(libmapPtr->trigger,ptr);
ptr=strtok(NULL,"/");
}
if(!ptr)
{
fprintf
(stderr,
" ERROR: \"source/<unique-sub-path>/GNUmakefile\" expected.\n");
exit(1);
}
}
}
if(optl)fprintf(stderr," Reading dependency files...\n");
#if defined ( _WIN32 ) || defined ( __CYGWIN__ ) || defined ( __CYGWIN32__ )
ntg4tmp=getenv("G4TMP");
if ( ! ntg4tmp )
{
fprintf(stderr," ERROR: Cannot find environment variable G4TMP\n");
exit(1);
}
ntg4tmp1=strdup(ntg4tmp);
#endif
for(i=0;i<rargc;i++)
{
fp=fopen(rargv[i],"r");
fgets(buffer,BUFSIZE,fp);
#if defined ( _WIN32 ) || defined ( __CYGWIN__ ) || defined ( __CYGWIN32__ )
ptr=strchr(ntg4tmp1,':');
while ( ptr=strchr(buffer,'\\') ) *ptr='/';
while (ntg4tmp1!=NULL && (ptr=strstr(buffer,ntg4tmp1))!=NULL )
{
for(nti=0;nti<strlen(ntg4tmp1);nti++) ptr[nti]=' ';
}
#endif
/* Clip target out of dependency file... */
ptr=strtok(buffer,":");
/* Look for a "user" library... */
for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
{
if(strlen(libmapPtr->lib)>256)
{
fprintf(stderr," ERROR: String overflow for: %s\n", libmapPtr->lib);
exit(1);
}
strcpy(workbuf,libmapPtr->lib);
/* Add trailing "/" to distinguish track/ and tracking/, etc. */
strcat(workbuf,"/");
if(strstr(ptr,workbuf)) break;
}
if(libmapPtr)
{
userLibmapPtr=libmapPtr;
}
else
{
userLibmapPtr=0;
}
if(!optm)
{
/* Look for a "used" library and add it to the "user" uses list... */
bufferPtr=strtok(NULL,"\n"); /* Start *after* ":". */
if (!bufferPtr)
{
fprintf(stderr," WARNING: It seems there is nothing after \':\' in dependency file %s.\n", rargv[i]);
}
else {
do
{
for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
{
/* Look for trigger string. */
if(strlen(libmapPtr->trigger)>256)
{
fprintf(stderr," ERROR: String overflow for: %s\n", libmapPtr->trigger);
exit(1);
}
strcpy(workbuf,libmapPtr->trigger);
strcat(workbuf,"/include");
ptr=strstr(bufferPtr,workbuf);
if(ptr && (userLibmapPtr != libmapPtr))
{
libmapPtr->used=1;
if(userLibmapPtr)
{
for(pp=userLibmapPtr->uses;*pp;pp++)
{
if(strcmp(*pp,libmapPtr->lib)==0)break;
}
if(!*pp)*pp=libmapPtr->lib;
}
}
/* Also look for library name in case header files are
placed in temporary directories under a subdirectory
with the same name as the library name. This can
happen with Objectivity which makes header files
from .ddl files and places them in a temporary
directory. */
if(strlen(libmapPtr->lib)>256)
{
fprintf(stderr," ERROR: String overflow for: %s\n", libmapPtr->lib);
exit(1);
}
strcpy(workbuf,libmapPtr->lib);
strcat(workbuf,"/");
ptr=strstr(bufferPtr,workbuf);
if(ptr && (userLibmapPtr != libmapPtr))
{
libmapPtr->used=1;
if(userLibmapPtr)
{
for(pp=userLibmapPtr->uses;*pp;pp++)
{
if(strcmp(*pp,libmapPtr->lib)==0)break;
}
if(!*pp)*pp=libmapPtr->lib;
}
}
}
fgets(buffer,BUFSIZE,fp);
bufferPtr=buffer;
#if defined ( _WIN32 ) || defined ( __CYGWIN__ ) || defined ( __CYGWIN32__ )
while ( ptr=strchr(buffer,'\\') ) *ptr='/';
while (ntg4tmp1 && (ptr=strstr(buffer,ntg4tmp1)) )
{
for(nti=0;nti<strlen(ntg4tmp1);nti++) ptr[nti]=' ';
}
#endif
} while(!feof(fp));
fclose(fp);
}
}
}
#if defined ( _WIN32 ) || defined ( __CYGWIN__ ) || defined ( __CYGWIN32__ )
free(ntg4tmp1);
#endif
if(optl) /* This option is used for compiling the file libname.map
from all the dependencies. */
{
fprintf(stderr," Checking for circular dependencies...\n");
for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
{
for(pp=libmapPtr->uses;*pp;pp++)
{
for(libmapPtr1=libmap;libmapPtr1!=libmapPtr;
libmapPtr1=libmapPtr1->next)
{
if(strcmp(libmapPtr1->lib,*pp)==0)
{
for(pp1=libmapPtr1->uses;*pp1;pp1++)
{
if(strcmp(*pp1,libmapPtr->lib)==0)break;
}
if(*pp1)
{
fprintf
(stderr,
" WARNING: %s and %s use each other.\n",
libmapPtr->lib,
libmapPtr1->lib);
}
}
else
{
/* Not right yet...
for(pp1=libmapPtr1->uses;*pp1;pp1++)
{
for(libmapPtr0=libmap;libmapPtr0!=libmapPtr1;
libmapPtr0=libmapPtr0->next)
{
if(libmapPtr0==*pp)
{
fprintf
(stderr,
" WARNING: triangular dependecy:\n"
" %s uses %s uses %s uses %s.\n",
libmapPtr->lib,
libmapPtr1->lib,
libmapPtr0->lib,
libmapPtr->lib);
}
}
}
*/
}
}
}
}
fprintf(stderr," Reordering according to dependencies...\n");
do
{
swapping=0;
prevPtr2=0;
for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
{
for(pp=libmapPtr->uses;*pp;pp++)
{
prevPtr1=0;
for(libmapPtr1=libmap;libmapPtr1!=libmapPtr;
libmapPtr1=libmapPtr1->next)
{
if(strcmp(libmapPtr1->lib,*pp)==0)
{
/* Check that 1st doesn't use 2nd... */
for(pp1=libmapPtr1->uses;*pp1;pp1++)
{
if(strcmp(*pp1,libmapPtr->lib)==0)break;
}
if(!*pp1) /* If not... */
{
swapping=1;
/* Make previous of 1st now point to 2nd... */
if(prevPtr1)
{
prevPtr1->next=libmapPtr;
}
else
{
libmap=libmapPtr;
}
/* Make 2nd now point to what 1st did, unless
it's adjacent, in which case make it point
to 1st itself... */
tmpp=libmapPtr->next;
if(libmapPtr1->next==libmapPtr)
{
libmapPtr->next=libmapPtr1;
}
else
{
libmapPtr->next=libmapPtr1->next;
}
/* Make previous of 2nd point to 1st, unless
it's adjacent, in which case leave it... */
if(libmapPtr1->next!=libmapPtr)
{
prevPtr2->next=libmapPtr1;
}
/* Make 1st now point to what 2nd did... */
libmapPtr1->next=tmpp;
break;
}
}
prevPtr1=libmapPtr1;
}
if(swapping)break;
}
prevPtr2=libmapPtr;
if(swapping)break;
}
}while(swapping);
fprintf(stderr," Writing new library map file...\n");
for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
{
printf("%s:",libmapPtr->lib);
for(pp=libmapPtr->uses;*pp;pp++)
{
printf(" %s",*pp);
}
printf("\n");
printf("source/%s/GNUmakefile\n",libmapPtr->trigger);
}
}
else if (optm)
{
/* create tmp. string libname */
int libname_usable_size=24;
if ( ! libname ) libname=malloc(libname_usable_size+16);
/* Write out full library list... */
for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
{
if ( strlen(libpath)+strlen(libmapPtr->lib) > libname_usable_size ) {
libname_usable_size=(strlen(libpath)+strlen(libmapPtr->lib))*2;
free(libname);
libname=malloc(libname_usable_size+16);
}
/* Check existance of libraries and print out only installed ones */
snprintf(libname, libname_usable_size+16, "%s/lib%s.a", libpath, libmapPtr->lib);
if (access(libname,R_OK))
{
snprintf(libname, libname_usable_size+16, "%s/lib%s.so", libpath, libmapPtr->lib);
if (!access(libname,R_OK))
{
printf("-l%s ",libmapPtr->lib);
}
else /* case MacOS .dylib */
{
snprintf(libname, libname_usable_size+16, "%s/lib%s.dylib", libpath, libmapPtr->lib);
if (!access(libname,R_OK))
{
printf("-l%s ",libmapPtr->lib);
}
}
}
else
{
printf("-l%s ",libmapPtr->lib);
}
libmapPtr=libmapPtr->next;
}
}
else
{
/* Add dependent libraries... */
for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
{
if(libmapPtr->used)
{
for(pp=libmapPtr->uses;*pp;pp++)
{
for(libmapPtr1=libmap;libmapPtr1;libmapPtr1=libmapPtr1->next)
{
if(strcmp(libmapPtr1->lib,*pp)==0)
{
libmapPtr1->used=1;
}
}
}
}
}
/* Write out library list... */
for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
{
if(libmapPtr->used)
{
printf("-l%s ",libmapPtr->lib);
}
}
}
exit(0);
}