40 #define BUFSIZE 1000000
54 char **targv=0,**ptr,**
phelp;
58 actualdir=opendir(directory);
60 if(!actualdir)
return targv;
63 for(entry=readdir(actualdir);entry!=NULL;entry=readdir(actualdir))
66 if(strcmp(entry->d_name,
".")==0 ||
67 strcmp(entry->d_name,
"..")==0)
continue;
69 if(buffer) free(buffer);
70 buffer=(
char*) malloc((strlen(directory)+
71 strlen(entry->d_name)+2)*
sizeof(char));
72 strcpy(buffer,directory);
73 strncat(buffer,
"/",1);
74 strcat(buffer,entry->d_name);
75 s=stat(buffer,&status);
78 if(S_ISDIR(status.st_mode))
86 targv=(
char**) malloc((*argc+targc)*
sizeof(
char*));
87 memcpy(targv,phelp,*argc*
sizeof(
char*));
88 memcpy(&targv[*argc],ptr,targc*
sizeof(
char*));
94 else if(S_ISREG(status.st_mode))
97 len=strlen(entry->d_name);
98 if(entry->d_name[len-2]==
'.' && entry->d_name[len-1]==
'd')
101 targv=(
char**) malloc((*argc+1)*
sizeof(
char*));
102 memcpy(targv,phelp,*argc*
sizeof(
char*));
103 targv[*argc]=strdup(buffer);
113 " No status - perhaps file %s does not exist.\n",
119 if(buffer) free(buffer);
126 int main (
int argc,
char** argv) {
129 char *ptr,*
p,**
pp,**pp1,**pp2,*directory=0,*libpath=0;
132 int i,optl=0,optm=0,swapping,
c,rargc;
135 #if defined ( _WIN32 ) || defined ( __CYGWIN__ ) || defined ( __CYGWIN32__ )
136 char *ntg4tmp=0,*ntg4tmp1=0;
146 struct libmap_ *next;
149 struct libmap_ *libmap=0,*libmapPtr=0,*libmapPtr1=0,*libmapPtr2=0,
150 *prevPtr1,*prevPtr2,*tmpp,*userLibmapPtr;
152 while((c=getopt(argc,argv,
"ld: m:"))!=EOF)
192 " ERROR: If you specify a directory don't also specify files\n");
197 if(optl)fprintf(stderr,
" Reading library name map file...\n");
202 if(feof(stdin))
break;
203 if (strlen(buffer) >=
BUFSIZE-1)
206 " Internal ERROR: BUFSIZE too small to read library name map file\n");
210 if ( buffer[strlen(buffer)-1] ==
'\n')
211 { buffer[strlen(buffer)-1]=
'\0'; }
213 ptr=strtok(buffer,
":\n");
216 for(libmapPtr1=libmap;libmapPtr1;libmapPtr1=libmapPtr1->next)
218 if(strcmp(libmapPtr1->lib,ptr)==0)
220 fprintf(stderr,
" ERROR: Duplicate library name: %s\n",ptr);
222 " Perhaps a duplicate subdirectory with"
223 " a GNUmakefile with the same library name.\n"
231 libmapPtr->next=(
struct libmap_*) malloc(
sizeof(
struct libmap_));
232 libmapPtr=libmapPtr->next;
236 libmap=(
struct libmap_*) malloc(
sizeof(
struct libmap_));
240 libmapPtr->lib=strdup(ptr);
242 libmapPtr->uses=(
char**)calloc(
NLIBMAX,
sizeof(
char*));
250 ptr=strtok(NULL,
" \n");
255 ptr=strtok(NULL,
" \n");
264 if (strlen(buffer) >=
BUFSIZE-1)
267 " Internal ERROR: BUFSIZE too small to read directory name\n");
271 if ( buffer[strlen(buffer)-1] ==
'\n')
272 { buffer[strlen(buffer)-1]=
'\0'; }
274 ptr=strtok(buffer,
"/");
277 fprintf(stderr,
" ERROR: \"/\" before \"source\" expected.\n");
280 while(ptr&&strcmp (ptr,
"source"))ptr=strtok(NULL,
"/");
281 ptr=strtok(NULL,
"/");
284 fprintf(stderr,
" ERROR: \"source\" expected.\n");
287 libmapPtr->trigger=(
char*)malloc(
TRIGSIZE);
290 fprintf(stderr,
" ERROR: String overflow for: %s\n", ptr);
293 strcpy(libmapPtr->trigger,ptr);
294 ptr=strtok(NULL,
"/");
295 while(ptr&&strcmp(ptr,
"GNUmakefile"))
297 strncat(libmapPtr->trigger,
"/",1);
298 strcat(libmapPtr->trigger,ptr);
299 ptr=strtok(NULL,
"/");
305 " ERROR: \"source/<unique-sub-path>/GNUmakefile\" expected.\n");
311 if(optl)fprintf(stderr,
" Reading dependency files...\n");
313 #if defined ( _WIN32 ) || defined ( __CYGWIN__ ) || defined ( __CYGWIN32__ )
314 ntg4tmp=getenv(
"G4TMP");
317 fprintf(stderr,
" ERROR: Cannot find environment variable G4TMP\n");
320 ntg4tmp1=strdup(ntg4tmp);
325 fp=fopen(rargv[i],
"r");
328 #if defined ( _WIN32 ) || defined ( __CYGWIN__ ) || defined ( __CYGWIN32__ )
329 ptr=strchr(ntg4tmp1,
':');
331 while ( ptr=strchr(buffer,
'\\') ) *ptr=
'/';
333 while (ntg4tmp1!=NULL && (ptr=strstr(buffer,ntg4tmp1))!=NULL )
335 for(nti=0;nti<strlen(ntg4tmp1);nti++) ptr[nti]=
' ';
340 ptr=strtok(buffer,
":");
343 for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
345 if(strlen(libmapPtr->lib)>256)
347 fprintf(stderr,
" ERROR: String overflow for: %s\n", libmapPtr->lib);
350 strcpy(workbuf,libmapPtr->lib);
352 strncat(workbuf,
"/",1);
353 if(strstr(ptr,workbuf))
break;
357 userLibmapPtr=libmapPtr;
367 bufferPtr=strtok(NULL,
"\n");
370 fprintf(stderr,
" WARNING: It seems there is nothing after \':\' in dependency file %s.\n", rargv[i]);
375 for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
378 if(strlen(libmapPtr->trigger)>256)
380 fprintf(stderr,
" ERROR: String overflow for: %s\n", libmapPtr->trigger);
383 strcpy(workbuf,libmapPtr->trigger);
384 strncat(workbuf,
"/include",8);
385 ptr=strstr(bufferPtr,workbuf);
386 if(ptr && (userLibmapPtr != libmapPtr))
391 for(pp=userLibmapPtr->uses;*pp;pp++)
393 if(strcmp(*pp,libmapPtr->lib)==0)
break;
395 if(!*pp)*pp=libmapPtr->lib;
404 if(strlen(libmapPtr->lib)>256)
406 fprintf(stderr,
" ERROR: String overflow for: %s\n", libmapPtr->lib);
409 strcpy(workbuf,libmapPtr->lib);
410 strncat(workbuf,
"/",1);
411 ptr=strstr(bufferPtr,workbuf);
412 if(ptr && (userLibmapPtr != libmapPtr))
417 for(pp=userLibmapPtr->uses;*pp;pp++)
419 if(strcmp(*pp,libmapPtr->lib)==0)
break;
421 if(!*pp)*pp=libmapPtr->lib;
428 #if defined ( _WIN32 ) || defined ( __CYGWIN__ ) || defined ( __CYGWIN32__ )
429 while ( ptr=strchr(buffer,
'\\') ) *ptr=
'/';
431 while (ntg4tmp1 && (ptr=strstr(buffer,ntg4tmp1)) )
433 for(nti=0;nti<strlen(ntg4tmp1);nti++) ptr[nti]=
' ';
443 #if defined ( _WIN32 ) || defined ( __CYGWIN__ ) || defined ( __CYGWIN32__ )
450 fprintf(stderr,
" Checking for circular dependencies...\n");
451 for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
453 for(pp=libmapPtr->uses;*pp;pp++)
455 for(libmapPtr1=libmap;libmapPtr1!=libmapPtr;
456 libmapPtr1=libmapPtr1->next)
458 if(strcmp(libmapPtr1->lib,*pp)==0)
460 for(pp1=libmapPtr1->uses;*pp1;pp1++)
462 if(strcmp(*pp1,libmapPtr->lib)==0)
break;
468 " WARNING: %s and %s use each other.\n",
500 fprintf(stderr,
" Reordering according to dependencies...\n");
505 for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
507 for(pp=libmapPtr->uses;*pp;pp++)
510 for(libmapPtr1=libmap;libmapPtr1!=libmapPtr;
511 libmapPtr1=libmapPtr1->next)
513 if(strcmp(libmapPtr1->lib,*pp)==0)
516 for(pp1=libmapPtr1->uses;*pp1;pp1++)
518 if(strcmp(*pp1,libmapPtr->lib)==0)
break;
526 prevPtr1->next=libmapPtr;
535 tmpp=libmapPtr->next;
536 if(libmapPtr1->next==libmapPtr)
538 libmapPtr->next=libmapPtr1;
542 libmapPtr->next=libmapPtr1->next;
546 if(libmapPtr1->next!=libmapPtr)
548 prevPtr2->next=libmapPtr1;
551 libmapPtr1->next=tmpp;
564 fprintf(stderr,
" Writing new library map file...\n");
565 for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
567 printf(
"%s:",libmapPtr->lib);
568 for(pp=libmapPtr->uses;*pp;pp++)
573 printf(
"source/%s/GNUmakefile\n",libmapPtr->trigger);
579 int libname_usable_size=24;
580 if ( ! libname ) libname=malloc(libname_usable_size+16);
583 for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
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);
593 sprintf(libname,
"%s/lib%s.a", libpath, libmapPtr->lib);
594 if (access(libname,R_OK))
596 sprintf(libname,
"%s/lib%s.so", libpath, libmapPtr->lib);
597 if (!access(libname,R_OK))
599 printf(
"-l%s ",libmapPtr->lib);
603 sprintf(libname,
"%s/lib%s.dylib", libpath, libmapPtr->lib);
604 if (!access(libname,R_OK))
606 printf(
"-l%s ",libmapPtr->lib);
612 printf(
"-l%s ",libmapPtr->lib);
614 libmapPtr=libmapPtr->next;
620 for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
624 for(pp=libmapPtr->uses;*pp;pp++)
626 for(libmapPtr1=libmap;libmapPtr1;libmapPtr1=libmapPtr1->next)
628 if(strcmp(libmapPtr1->lib,*pp)==0)
638 for(libmapPtr=libmap;libmapPtr;libmapPtr=libmapPtr->next)
642 printf(
"-l%s ",libmapPtr->lib);