126 {
127
129 char *ptr,*p,**
pp,**pp1,**pp2,*directory=0,*libpath=0;
130 char **rargv;
131 char *libname=0;
132 int i,optl=0,optm=0,swapping,c,rargc;
133 FILE *fp;
134
135#if defined ( _WIN32 ) || defined ( __CYGWIN__ ) || defined ( __CYGWIN32__ )
136 char *ntg4tmp=0,*ntg4tmp1=0;
137 int nti;
138#endif
139
140 struct libmap_
141 {
142 char *lib;
143 char *trigger;
144 int used;
145 char **uses;
146 struct libmap_ *next;
147 };
148
149 struct libmap_ *libmap=0,*libmapPtr=0,*libmapPtr1=0,*libmapPtr2=0,
150 *prevPtr1,*prevPtr2,*tmpp,*userLibmapPtr;
151
152 while((c=getopt(argc,argv,"ld: m:"))!=EOF)
153 {
154 switch(c)
155 {
156 case 'l':
157 optl=1;
158 break;
159 case 'd':
161 break;
162 case 'm':
163 optm=1;
165 break;
166 }
167 }
168
169
170
172 {
175 }
176 else
177 {
178 rargv=0;
179 rargc=0;
180 }
181
182 if(directory)
183 {
184 if(rargc==0)
185 {
187 }
188 else
189 {
190 fprintf
191 (stderr,
192 " ERROR: If you specify a directory don't also specify files\n");
194 }
195 }
196
197 if(optl)fprintf(stderr," Reading library name map file...\n");
198 while (!feof(stdin))
199 {
200
202 if(feof(stdin)) break;
204 {
205 fprintf(stderr,
206 " Internal ERROR: BUFSIZE too small to read library name map file\n");
208 }
209
212
214
215
216 for(libmapPtr1=libmap;libmapPtr1;libmapPtr1=libmapPtr1->next)
217 {
218 if(strcmp(libmapPtr1->lib,ptr)==0)
219 {
220 fprintf(stderr," ERROR: Duplicate library name: %s\n",ptr);
221 fprintf(stderr,
222 " Perhaps a duplicate subdirectory with"
223 " a GNUmakefile with the same library name.\n"
224 );
226 }
227 }
228
229 if(libmap)
230 {
231 libmapPtr->next=(struct libmap_*) malloc(sizeof(struct libmap_));
232 libmapPtr=libmapPtr->next;
233 }
234 else
235 {
236 libmap=(struct libmap_*) malloc(sizeof(struct libmap_));
237 libmapPtr=libmap;
238 }
239 libmapPtr->next=0;
240 libmapPtr->lib=strdup(ptr);
241 libmapPtr->used=0;
242 libmapPtr->uses=(
char**)calloc(
NLIBMAX,
sizeof(
char*));
243
244
245 if(!optl && !optm)
246 {
248 if(ptr)
249 {
250 ptr=strtok(NULL," \n");
251 while (ptr)
252 {
255 ptr=strtok(NULL," \n");
256 }
257 }
258 }
259
260 if(!optm)
261 {
262
265 {
266 fprintf(stderr,
267 " Internal ERROR: BUFSIZE too small to read directory name\n");
269 }
270
273
275 if(!ptr)
276 {
277 fprintf(stderr," ERROR: \"/\" before \"source\" expected.\n");
279 }
280 while(ptr&&strcmp (ptr,"source"))ptr=strtok(NULL,"/");
281 ptr=strtok(NULL,"/");
282 if(!ptr)
283 {
284 fprintf(stderr," ERROR: \"source\" expected.\n");
286 }
287 libmapPtr->trigger=(
char*)malloc(
TRIGSIZE);
289 {
290 fprintf(stderr," ERROR: String overflow for: %s\n", ptr);
292 }
293 strcpy(libmapPtr->trigger,ptr);
294 ptr=strtok(NULL,"/");
295 while(ptr&&strcmp(ptr,"GNUmakefile"))
296 {
297 strncat(libmapPtr->trigger,"/",1);
298 strcat(libmapPtr->trigger,ptr);
299 ptr=strtok(NULL,"/");
300 }
301 if(!ptr)
302 {
303 fprintf
304 (stderr,
305 " ERROR: \"source/<unique-sub-path>/GNUmakefile\" expected.\n");
307 }
308 }
309 }
310
311 if(optl)fprintf(stderr," Reading dependency files...\n");
312
313#if defined ( _WIN32 ) || defined ( __CYGWIN__ ) || defined ( __CYGWIN32__ )
314 ntg4tmp=getenv("G4TMP");
315 if ( ! ntg4tmp )
316 {
317 fprintf(stderr," ERROR: Cannot find environment variable G4TMP\n");
319 }
320 ntg4tmp1=strdup(ntg4tmp);
321#endif
322
323 for(i=0;i<rargc;i++)
324 {
325 fp=fopen(rargv[i],"r");
327
328#if defined ( _WIN32 ) || defined ( __CYGWIN__ ) || defined ( __CYGWIN32__ )
329 ptr=strchr(ntg4tmp1,':');
330
331 while ( ptr=strchr(
buffer,
'\\') ) *ptr=
'/';
332
333 while (ntg4tmp1!=NULL && (ptr=strstr(
buffer,ntg4tmp1))!=NULL )
334 {
335 for(nti=0;nti<strlen(ntg4tmp1);nti++) ptr[nti]=' ';
336 }
337#endif
338
339
341
342
343 for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
344 {
345 if(strlen(libmapPtr->lib)>256)
346 {
347 fprintf(stderr," ERROR: String overflow for: %s\n", libmapPtr->lib);
349 }
350 strcpy(workbuf,libmapPtr->lib);
351
352 strncat(workbuf,"/",1);
353 if(strstr(ptr,workbuf)) break;
354 }
355 if(libmapPtr)
356 {
357 userLibmapPtr=libmapPtr;
358 }
359 else
360 {
361 userLibmapPtr=0;
362 }
363
364 if(!optm)
365 {
366
369 {
370 fprintf(stderr," WARNING: It seems there is nothing after \':\' in dependency file %s.\n", rargv[i]);
371 }
372 else {
373 do
374 {
375 for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
376 {
377
378 if(strlen(libmapPtr->trigger)>256)
379 {
380 fprintf(stderr," ERROR: String overflow for: %s\n", libmapPtr->trigger);
382 }
383 strcpy(workbuf,libmapPtr->trigger);
384 strncat(workbuf,"/include",8);
386 if(ptr && (userLibmapPtr != libmapPtr))
387 {
388 libmapPtr->used=1;
389 if(userLibmapPtr)
390 {
391 for(
pp=userLibmapPtr->uses;*
pp;
pp++)
392 {
393 if(strcmp(*
pp,libmapPtr->lib)==0)
break;
394 }
395 if(!*
pp)*
pp=libmapPtr->lib;
396 }
397 }
398
399
400
401
402
403
404 if(strlen(libmapPtr->lib)>256)
405 {
406 fprintf(stderr," ERROR: String overflow for: %s\n", libmapPtr->lib);
408 }
409 strcpy(workbuf,libmapPtr->lib);
410 strncat(workbuf,"/",1);
412 if(ptr && (userLibmapPtr != libmapPtr))
413 {
414 libmapPtr->used=1;
415 if(userLibmapPtr)
416 {
417 for(
pp=userLibmapPtr->uses;*
pp;
pp++)
418 {
419 if(strcmp(*
pp,libmapPtr->lib)==0)
break;
420 }
421 if(!*
pp)*
pp=libmapPtr->lib;
422 }
423 }
424 }
427
428#if defined ( _WIN32 ) || defined ( __CYGWIN__ ) || defined ( __CYGWIN32__ )
429 while ( ptr=strchr(
buffer,
'\\') ) *ptr=
'/';
430
431 while (ntg4tmp1 && (ptr=strstr(
buffer,ntg4tmp1)) )
432 {
433 for(nti=0;nti<strlen(ntg4tmp1);nti++) ptr[nti]=' ';
434 }
435#endif
436
437 } while(!feof(fp));
438 fclose(fp);
439 }
440 }
441 }
442
443#if defined ( _WIN32 ) || defined ( __CYGWIN__ ) || defined ( __CYGWIN32__ )
445#endif
446
447 if(optl)
448
449 {
450 fprintf(stderr," Checking for circular dependencies...\n");
451 for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
452 {
453 for(
pp=libmapPtr->uses;*
pp;
pp++)
454 {
455 for(libmapPtr1=libmap;libmapPtr1!=libmapPtr;
456 libmapPtr1=libmapPtr1->next)
457 {
458 if(strcmp(libmapPtr1->lib,*
pp)==0)
459 {
460 for(pp1=libmapPtr1->uses;*pp1;pp1++)
461 {
462 if(strcmp(*pp1,libmapPtr->lib)==0)break;
463 }
464 if(*pp1)
465 {
466 fprintf
467 (stderr,
468 " WARNING: %s and %s use each other.\n",
469 libmapPtr->lib,
470 libmapPtr1->lib);
471 }
472 }
473 else
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 fprintf(stderr," Reordering according to dependencies...\n");
501 do
502 {
503 swapping=0;
504 prevPtr2=0;
505 for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
506 {
507 for(
pp=libmapPtr->uses;*
pp;
pp++)
508 {
509 prevPtr1=0;
510 for(libmapPtr1=libmap;libmapPtr1!=libmapPtr;
511 libmapPtr1=libmapPtr1->next)
512 {
513 if(strcmp(libmapPtr1->lib,*
pp)==0)
514 {
515
516 for(pp1=libmapPtr1->uses;*pp1;pp1++)
517 {
518 if(strcmp(*pp1,libmapPtr->lib)==0)break;
519 }
520 if(!*pp1)
521 {
522 swapping=1;
523
524 if(prevPtr1)
525 {
526 prevPtr1->next=libmapPtr;
527 }
528 else
529 {
530 libmap=libmapPtr;
531 }
532
533
534
535 tmpp=libmapPtr->next;
536 if(libmapPtr1->next==libmapPtr)
537 {
538 libmapPtr->next=libmapPtr1;
539 }
540 else
541 {
542 libmapPtr->next=libmapPtr1->next;
543 }
544
545
546 if(libmapPtr1->next!=libmapPtr)
547 {
548 prevPtr2->next=libmapPtr1;
549 }
550
551 libmapPtr1->next=tmpp;
552 break;
553 }
554 }
555 prevPtr1=libmapPtr1;
556 }
557 if(swapping)break;
558 }
559 prevPtr2=libmapPtr;
560 if(swapping)break;
561 }
562 }while(swapping);
563
564 fprintf(stderr," Writing new library map file...\n");
565 for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
566 {
567 printf("%s:",libmapPtr->lib);
568 for(
pp=libmapPtr->uses;*
pp;
pp++)
569 {
571 }
572 printf("\n");
573 printf("source/%s/GNUmakefile\n",libmapPtr->trigger);
574 }
575 }
576 else if (optm)
577 {
578
579 int libname_usable_size=24;
580 if ( ! libname ) libname=malloc(libname_usable_size+16);
581
582
583 for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
584 {
585 if ( strlen(libpath)+strlen(libmapPtr->lib) > libname_usable_size ) {
586 libname_usable_size=(strlen(libpath)+strlen(libmapPtr->lib))*2;
588 libname=malloc(libname_usable_size+16);
589 }
590
591
592
593 sprintf(libname, "%s/lib%s.a", libpath, libmapPtr->lib);
594 if (access(libname,R_OK))
595 {
596 sprintf(libname, "%s/lib%s.so", libpath, libmapPtr->lib);
597 if (!access(libname,R_OK))
598 {
599 printf("-l%s ",libmapPtr->lib);
600 }
601 else
602 {
603 sprintf(libname, "%s/lib%s.dylib", libpath, libmapPtr->lib);
604 if (!access(libname,R_OK))
605 {
606 printf("-l%s ",libmapPtr->lib);
607 }
608 }
609 }
610 else
611 {
612 printf("-l%s ",libmapPtr->lib);
613 }
614 libmapPtr=libmapPtr->next;
615 }
616 }
617 else
618 {
619
620 for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
621 {
622 if(libmapPtr->used)
623 {
624 for(
pp=libmapPtr->uses;*
pp;
pp++)
625 {
626 for(libmapPtr1=libmap;libmapPtr1;libmapPtr1=libmapPtr1->next)
627 {
628 if(strcmp(libmapPtr1->lib,*
pp)==0)
629 {
630 libmapPtr1->used=1;
631 }
632 }
633 }
634 }
635 }
636
637
638 for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
639 {
640 if(libmapPtr->used)
641 {
642 printf("-l%s ",libmapPtr->lib);
643 }
644 }
645 }
646
648
649}
char ** parsedir(char *directory, int *argc)