You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

778 lines
16 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /**[txh]********************************************************************
  2. Copyright (c) 2003 by Salvador Eduardo Tropea.
  3. Covered by the GPL license.
  4. Description:
  5. This program generates a .imk file containing the dependencies for a
  6. project.
  7. ***************************************************************************/
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <sys/stat.h>
  12. #include <limits.h>
  13. #include <unistd.h>
  14. #include <time.h>
  15. #include <ctype.h>
  16. #define ONE_DEP_BY_LINE 1
  17. const int maxLine=1024;
  18. unsigned maxCol=78;
  19. struct node;
  20. static char *projectBase;
  21. static int projectBaseL;
  22. static int IncludeCounter=0;
  23. struct stMak
  24. {
  25. node *base, *last;
  26. char *objDir;
  27. char *mainTarget;
  28. char *baseDir;
  29. };
  30. struct node
  31. {
  32. char *name;
  33. node *deps;
  34. node *ldep;
  35. node *next;
  36. stMak *subprj;
  37. };
  38. struct stIncDir
  39. {
  40. char *var;
  41. char *dir;
  42. int ldir;
  43. };
  44. stIncDir incDirs[]=
  45. {
  46. {"INCLUDE_DIR", "../include", 0},
  47. {"CLASSES_DIR", "../classes", 0},
  48. {"EXTRA_SRC_DIR", "..", 0},
  49. {0,0}
  50. };
  51. static
  52. void AddFileName(const char *name, stMak &mk)
  53. {
  54. //printf("`%s'\n",name);
  55. if (mk.base)
  56. {
  57. mk.last->next=new node;
  58. mk.last=mk.last->next;
  59. }
  60. else
  61. mk.base=mk.last=new node;
  62. mk.last->name=strdup(name);
  63. mk.last->next=mk.last->deps=mk.last->ldep=NULL;
  64. mk.last->subprj=NULL;
  65. }
  66. static
  67. int PrConvertExt(FILE *d, const char *file)
  68. {
  69. char *s=strrchr(file,'.');
  70. if (!s)
  71. return fprintf(d,"%s",file);
  72. int l=s-file;
  73. fwrite(file,l,1,d);
  74. if (strcmp(s,".o")==0)
  75. l+=fprintf(d,"$(ExOBJ)");
  76. else if (strcmp(s,".exe")==0)
  77. l+=fprintf(d,"$(ExEXE)");
  78. else if (strcmp(s,".a")==0)
  79. l+=fprintf(d,"$(ExLIB)");
  80. else if (strcmp(s,".env")==0)
  81. l+=fprintf(d,".env");
  82. else if (strcmp(s,".imk")==0)
  83. l+=fprintf(d,".imk");
  84. else if (strcmp(s,".h")==0)
  85. l+=fprintf(d,".h");
  86. else if (strcmp(s,".c")==0)
  87. l+=fprintf(d,".c");
  88. else if (strcmp(s,".cc")==0)
  89. l+=fprintf(d,".cc");
  90. else if (strcmp(s,".cpp")==0)
  91. l+=fprintf(d,".cpp");
  92. else
  93. {
  94. fprintf(stderr,"Unknown extension: '%s'\n",s);
  95. exit(15);
  96. }
  97. return l;
  98. }
  99. static
  100. void ExtractSources(FILE *f, stMak &mk)
  101. {
  102. char buffer[maxLine];
  103. char found=0;
  104. do
  105. {
  106. if (fgets(buffer,maxLine,f))
  107. if (strncmp(buffer,"PROJECT_ITEMS",13)==0)
  108. {
  109. found=1;
  110. break;
  111. }
  112. }
  113. while (!feof(f));
  114. if (!found)
  115. {
  116. fprintf(stderr,"Can't find project items\n");
  117. exit(1);
  118. }
  119. char *s=buffer+14;
  120. do
  121. {
  122. char *fName=strtok(s," \t\\\n");
  123. while (fName)
  124. {
  125. if (*fName)
  126. AddFileName(fName,mk);
  127. fName=strtok(NULL," \t\\\n");
  128. }
  129. fgets(buffer,maxLine,f);
  130. s=buffer;
  131. }
  132. while (!feof(f) && buffer[0]=='\t');
  133. }
  134. static
  135. void AddDependency(const char *name, node *p)
  136. {
  137. if (p->deps)
  138. {
  139. p->ldep->next=new node;
  140. p->ldep=p->ldep->next;
  141. }
  142. else
  143. p->deps=p->ldep=new node;
  144. p->ldep->name=strdup(name);
  145. p->ldep->next=p->ldep->deps=p->ldep->ldep=NULL;
  146. p->ldep->subprj=NULL;
  147. }
  148. static
  149. void ExtractDeps(FILE *f, node *p)
  150. {
  151. char buffer[maxLine];
  152. char bName[maxLine];
  153. char found=0;
  154. int depNum;
  155. do
  156. {
  157. if (fgets(buffer,maxLine,f) &&
  158. sscanf(buffer,"DEPS_%d=%s ",&depNum,bName)==2 &&
  159. depNum)
  160. {
  161. if (strcmp(bName,p->name))
  162. {
  163. fprintf(stderr,"Error: unsorted deps? (%d,%s looking for %s)\n",depNum,bName,
  164. p->name);
  165. exit(2);
  166. }
  167. found=1;
  168. break;
  169. }
  170. }
  171. while (!feof(f));
  172. if (!found)
  173. {
  174. fprintf(stderr,"Failed to find %s dependencies\n",p->name);
  175. exit(3);
  176. }
  177. char *s=strchr(buffer,'=')+1+strlen(p->name);
  178. do
  179. {
  180. char *fName=strtok(s," \t\\\n");
  181. while (fName)
  182. {
  183. if (*fName)
  184. AddDependency(fName,p);
  185. fName=strtok(NULL," \t\\\n");
  186. }
  187. fgets(buffer,maxLine,f);
  188. s=buffer;
  189. }
  190. while (!feof(f) && buffer[0]=='\t');
  191. }
  192. static
  193. int PrintDep(FILE *d, int l, const char *s)
  194. {
  195. if (ONE_DEP_BY_LINE)
  196. {
  197. fputs("\\\n\t",d);
  198. l=8+PrConvertExt(d,s)+1;
  199. fputc(' ',d);
  200. }
  201. else
  202. {
  203. if (l+strlen(s)+2>maxCol)
  204. {
  205. fputs("\\\n\t",d);
  206. l=8;
  207. }
  208. l+=PrConvertExt(d,s)+1;
  209. fputc(' ',d);
  210. }
  211. return l;
  212. }
  213. static
  214. int PrintDepDir(FILE *d, int l, const char *s,int lenDir, const char *dir)
  215. {
  216. if (ONE_DEP_BY_LINE)
  217. {
  218. fputs("\\\n\t",d);
  219. l=8+fprintf(d,"%s/",dir)+PrConvertExt(d,s)+1;
  220. fputc(' ',d);
  221. }
  222. else
  223. {
  224. if (l+strlen(s)+3+lenDir>maxCol)
  225. {
  226. fputs("\\\n\t",d);
  227. l=8;
  228. }
  229. l+=fprintf(d,"%s/",dir)+PrConvertExt(d,s)+1;
  230. fputc(' ',d);
  231. }
  232. return l;
  233. }
  234. static
  235. int AddFixedDeps(FILE *d, int l)
  236. {
  237. //l=PrintDep(d,l,"rhide.env");
  238. //l=PrintDep(d,l,"common.imk");
  239. l=PrintDep(d,l,"$(MAKEFILE_DEPS)");
  240. return l;
  241. }
  242. static
  243. void GenerateDepFor(node *p, FILE *d, stMak &mk)
  244. {
  245. char *baseName=strdup(p->name);
  246. char *s=strrchr(baseName,'.');
  247. char *ext=s+1;
  248. *s=0;
  249. // Already compiled item?
  250. if (strcmp(ext,"o")==0 || strcmp(ext,"a")==0)
  251. return;
  252. int l=fprintf(d,"%s/%s$(ExOBJ):: %s ",mk.objDir,baseName,p->name);
  253. node *c=p->deps;
  254. while (c)
  255. {
  256. s=c->name;
  257. char *toStat=NULL;
  258. struct stat st;
  259. //fprintf(stderr,"Dependencia: %s\n",s);
  260. if (s[0]=='/' || s[0]=='\\' || (isalpha(s[0]) && s[1]==':'))
  261. {// Absolute path
  262. //fprintf(stderr,"Chequeando con la base: %s (%d)\n",projectBase,projectBaseL);
  263. if (strncmp(s,projectBase,projectBaseL)==0)
  264. {
  265. char *sub=s+projectBaseL;
  266. toStat=(char *)malloc(strlen(sub)+3+1);
  267. sprintf(toStat,"../%s",sub);
  268. //fprintf(stderr,"Transformada a relativa: %s\n",toStat);
  269. }
  270. }
  271. if (!toStat)
  272. {
  273. if (mk.baseDir)
  274. {
  275. toStat=new char[strlen(mk.baseDir)+strlen(s)+1];
  276. strcpy(toStat,mk.baseDir);
  277. strcat(toStat,s);
  278. //fprintf(stderr,"Agregada base relativa: %s\n",toStat);
  279. }
  280. else
  281. toStat=strdup(s);
  282. }
  283. int foundOnVPath=0;
  284. if (stat(toStat,&st))
  285. {
  286. //fprintf(stderr,"Buscando %s\n",toStat);
  287. // RHIDE 1.5 CVS filters the VPATH part, now I added it to common.imk.
  288. int i;
  289. char buf[PATH_MAX];
  290. for (i=0; !foundOnVPath && incDirs[i].var; i++)
  291. {
  292. strcpy(buf,incDirs[i].dir);
  293. strcat(buf,"/");
  294. strcat(buf,toStat);
  295. if (stat(buf,&st)==0)
  296. foundOnVPath=1;
  297. }
  298. if (!foundOnVPath)
  299. {// Try without the base directory.
  300. for (i=0; !foundOnVPath && incDirs[i].var; i++)
  301. {
  302. strcpy(buf,incDirs[i].dir);
  303. strcat(buf,"/");
  304. strcat(buf,s);
  305. if (stat(buf,&st)==0)
  306. foundOnVPath=1;
  307. }
  308. if (!foundOnVPath)
  309. {
  310. fprintf(stderr,"Can't stat %s dependency\n",toStat);
  311. exit(12);
  312. }
  313. }
  314. }
  315. free(toStat);
  316. if (!foundOnVPath)
  317. {
  318. int i;
  319. if (strchr(s,'/'))
  320. {
  321. for (i=0; incDirs[i].var; i++)
  322. if (strncmp(s,incDirs[i].dir,incDirs[i].ldir)==0)
  323. {
  324. s=(char *)malloc(3+strlen(incDirs[i].var)+1+strlen(s+incDirs[i].ldir));
  325. sprintf(s,"$(%s)%s",incDirs[i].var,c->name+incDirs[i].ldir);
  326. break;
  327. }
  328. if (s==c->name)
  329. {
  330. // This is some bug in RHIDE 1.5 CVS: some paths are emitted as absolute,
  331. // maybe is because the program is loading an old project.
  332. if (strncmp(s,projectBase,projectBaseL)==0)
  333. {
  334. char *sub=s+projectBaseL;
  335. char *toTest=(char *)malloc(strlen(sub)+3+1);
  336. sprintf(toTest,"../%s",sub);
  337. for (i=0; incDirs[i].var; i++)
  338. if (strncmp(toTest,incDirs[i].dir,incDirs[i].ldir)==0)
  339. {
  340. s=(char *)malloc(3+strlen(incDirs[i].var)+1+strlen(toTest+incDirs[i].ldir));
  341. sprintf(s,"$(%s)%s",incDirs[i].var,toTest+incDirs[i].ldir);
  342. //fprintf(stderr,"%s Da: %s\n",c->name,s);
  343. break;
  344. }
  345. free(toTest);
  346. }
  347. if (s==c->name)
  348. {
  349. fprintf(stderr,"Unknown include dir: %s\n",c->name);
  350. exit(4);
  351. }
  352. }
  353. }
  354. }
  355. l=PrintDep(d,l,s);
  356. if (s!=c->name)
  357. free(s);
  358. c=c->next;
  359. }
  360. l=AddFixedDeps(d,l);
  361. fputc('\n',d);
  362. if (strcmp(ext,"c")==0)
  363. fputs("\t$(RHIDE_COMPILE_C)",d);
  364. else if (strcmp(ext,"cc")==0)
  365. fputs("\t$(RHIDE_COMPILE_CC)",d);
  366. else if (strcmp(ext,"cpp")==0)
  367. fputs("\t$(RHIDE_COMPILE_CC)",d);
  368. else if (strcmp(ext,"s")==0)
  369. fputs("\t$(RHIDE_COMPILE_ASM_FORCE)",d);
  370. else
  371. {
  372. fprintf(stderr,"\nUnknown extension `%s'\n",ext);
  373. exit(5);
  374. }
  375. fputs("\n\n",d);
  376. free(baseName);
  377. }
  378. static
  379. int ExtractVar(FILE *f, const char *var, char *&dest, char ret)
  380. {
  381. char buffer[maxLine];
  382. int l=strlen(var);
  383. do
  384. {
  385. if (fgets(buffer,maxLine,f))
  386. {
  387. if (strncmp(buffer,var,l)==0)
  388. {
  389. char *s=strtok(buffer+l+1,"\n");
  390. dest=s ? strdup(s) : strdup("");
  391. return 1;
  392. }
  393. else
  394. {
  395. if (strncmp(buffer,"include",7)==0)
  396. {
  397. char *s=strtok(buffer+8,"\n");
  398. FILE *inc=fopen(s,"rt");
  399. if (!inc)
  400. {
  401. fprintf(stderr,"Can't open include %s\n",s);
  402. exit(13);
  403. }
  404. int retVal=ExtractVar(inc,var,dest,1);
  405. fclose(inc);
  406. if (retVal)
  407. return 1;
  408. }
  409. }
  410. }
  411. }
  412. while (!feof(f));
  413. if (ret)
  414. return 0;
  415. fprintf(stderr,"Unable to find %s var\n",var);
  416. exit(7);
  417. }
  418. static
  419. void ExtractObjDir(FILE *f, stMak &mk)
  420. {
  421. mk.objDir=".";
  422. ExtractVar(f,"vpath_obj",mk.objDir,1);
  423. }
  424. /*static
  425. void ExtractTVDir(FILE *f)
  426. {
  427. if (incDirs[0].dir)
  428. return;
  429. ExtractVar(f,"TVISION_INC",incDirs[0].dir,0);
  430. }*/
  431. static
  432. void ExtractTarget(FILE *f, stMak &mk)
  433. {
  434. ExtractVar(f,"MAIN_TARGET",mk.mainTarget,0);
  435. }
  436. static
  437. int CollectTargets(FILE *d, const char *name, stMak &mk, int l);
  438. static
  439. int ListTargetItems(FILE *d, int l, stMak &mk)
  440. {
  441. node *p=mk.base;
  442. int lenObjDir=strlen(mk.objDir);
  443. while (p)
  444. {
  445. if (strstr(p->name,".gpr")==NULL)
  446. {
  447. char *s=strdup(p->name);
  448. char *ext=strrchr(s,'.');
  449. if (strcmp(ext,".o")==0 || strcmp(ext,".a")==0)
  450. {
  451. l=PrintDep(d,l,s);
  452. }
  453. else
  454. {
  455. strcpy(ext,".o");
  456. l=PrintDepDir(d,l,s,lenObjDir,mk.objDir);
  457. }
  458. free(s);
  459. }
  460. else
  461. {
  462. l=CollectTargets(d,p->name,*p->subprj,l);
  463. }
  464. p=p->next;
  465. }
  466. return l;
  467. }
  468. static
  469. int CollectTargets(FILE *d, const char *name, stMak &mk, int l)
  470. {
  471. if (mk.mainTarget && *mk.mainTarget)
  472. {// Simple, it have a lib
  473. l=PrintDep(d,l,mk.mainTarget);
  474. }
  475. else
  476. {// More complex
  477. l=ListTargetItems(d,l,mk);
  478. }
  479. return l;
  480. }
  481. static
  482. void GenerateTarget(FILE *d, stMak &mk)
  483. {
  484. if (!*mk.mainTarget)
  485. return;
  486. int l=PrConvertExt(d,mk.mainTarget)+3;
  487. fputs(":: ",d);
  488. l=ListTargetItems(d,l,mk);
  489. l=AddFixedDeps(d,l);
  490. char *ext=strrchr(mk.mainTarget,'.');
  491. if (ext) ext++; else ext="exe";
  492. fputc('\n',d);
  493. if (strcmp(ext,"exe")==0)
  494. fputs("\t$(RHIDE_COMPILE_LINK)",d);
  495. else if (strcmp(ext,"a")==0)
  496. fputs("\t$(RHIDE_COMPILE_ARCHIVE)",d);
  497. else
  498. {
  499. fprintf(stderr,"\nUnknown extension `%s'\n",ext);
  500. exit(8);
  501. }
  502. fputs("\n\n",d);
  503. }
  504. static
  505. void GenerateAll(FILE *f, stMak &mk)
  506. {
  507. GenerateTarget(f,mk);
  508. node *p=mk.base;
  509. while (p)
  510. {
  511. if (p->subprj)
  512. {
  513. if (0)
  514. GenerateAll(f,*p->subprj);
  515. else
  516. {
  517. char *s=strdup(p->name);
  518. // This is some bug in RHIDE 1.5 CVS: some paths are emitted as absolute,
  519. // maybe is because the program is loading an old project.
  520. if (strncmp(s,projectBase,projectBaseL)==0)
  521. {
  522. char *sub=s+projectBaseL;
  523. char *toTest=(char *)malloc(strlen(sub)+3+1);
  524. sprintf(toTest,"../%s",sub);
  525. free(s);
  526. s=toTest;
  527. }
  528. char *ext=strrchr(s,'.');
  529. *ext=0;
  530. fprintf(f,"INCLUDE%02d=%s$(ExIMK)\n\n",IncludeCounter++,s);
  531. free(s);
  532. }
  533. }
  534. else
  535. GenerateDepFor(p,f,mk);
  536. p=p->next;
  537. }
  538. }
  539. static
  540. int ListTargetOItems(FILE *d, int l, stMak &mk)
  541. {
  542. node *p=mk.base;
  543. int lenObjDir=strlen(mk.objDir);
  544. while (p)
  545. {
  546. if (strstr(p->name,".gpr")==NULL)
  547. {
  548. char *ext=strrchr(p->name,'.');
  549. if (strcmp(ext,".a"))
  550. {
  551. char *s=strdup(p->name);
  552. if (strcmp(ext,".o"))
  553. strcpy(strrchr(s,'.'),".o");
  554. l=PrintDepDir(d,l,s,lenObjDir,mk.objDir);
  555. free(s);
  556. }
  557. }
  558. else
  559. {
  560. if (!p->subprj->mainTarget || !*p->subprj->mainTarget)
  561. l=ListTargetOItems(d,l,*p->subprj);
  562. }
  563. p=p->next;
  564. }
  565. return l;
  566. }
  567. static
  568. void CollectOTargets(FILE *d, const char *name, stMak &mk)
  569. {
  570. }
  571. static
  572. void GenerateObjs(FILE *f, stMak &mk)
  573. {
  574. int l=fprintf(f,"OBJFILES=");
  575. ListTargetOItems(f,l,mk);
  576. fputs("\n\n",f);
  577. }
  578. static
  579. int ListTargetAItems(FILE *d, int l, stMak &mk)
  580. {
  581. node *p=mk.base;
  582. if (mk.mainTarget && *mk.mainTarget)
  583. {
  584. l=PrintDep(d,l,mk.mainTarget);
  585. }
  586. else
  587. {
  588. while (p)
  589. {
  590. if (p->subprj)
  591. ListTargetAItems(d,l,*p->subprj);
  592. else
  593. {
  594. char *ext=strrchr(p->name,'.');
  595. if (strcmp(ext,".a")==0)
  596. l=PrintDep(d,l,p->name);
  597. }
  598. p=p->next;
  599. }
  600. }
  601. return l;
  602. }
  603. static
  604. void GenerateLibs(FILE *f, stMak &mk)
  605. {
  606. int l=fprintf(f,"LIBRARIES=");
  607. node *p=mk.base;
  608. while (p)
  609. {
  610. if (p->subprj)
  611. l=ListTargetAItems(f,l,*p->subprj);
  612. else
  613. {
  614. char *ext=strrchr(p->name,'.');
  615. if (strcmp(ext,".a")==0)
  616. l=PrintDep(f,l,p->name);
  617. }
  618. p=p->next;
  619. }
  620. fputs("\n\n",f);
  621. }
  622. static
  623. void ExtractBaseDir(const char *mak, stMak &mk)
  624. {
  625. char *s=strrchr(mak,'/');
  626. if (s)
  627. {
  628. int l=s-mak+1;
  629. mk.baseDir=new char[l+1];
  630. memcpy(mk.baseDir,mak,l);
  631. mk.baseDir[l]=0;
  632. }
  633. }
  634. static
  635. void ProcessMakefile(const char *mak, stMak &mk, int level)
  636. {
  637. //printf("\n\n\nParsing %s makefile\n\n\n",mak);
  638. ExtractBaseDir(mak,mk);
  639. FILE *f;
  640. f=fopen(mak,"rt");
  641. if (!f)
  642. {
  643. fprintf(stderr,"Can't open %s\n",mak);
  644. exit(8);
  645. }
  646. ExtractObjDir(f,mk);
  647. fseek(f,0,SEEK_SET);
  648. long pos=ftell(f);
  649. ExtractTarget(f,mk);
  650. ExtractSources(f,mk);
  651. // RHIDE 1.4.7 have a different order than 1.4.9 and 1.5
  652. fseek(f,pos,SEEK_SET);
  653. //ExtractTVDir(f);
  654. int i;
  655. if (!incDirs[0].ldir)
  656. for (i=0; incDirs[i].var; i++)
  657. incDirs[i].ldir=strlen(incDirs[i].dir);
  658. node *p;
  659. //printf("Should dig:\n");
  660. p=mk.base;
  661. while (p)
  662. {
  663. char *s=strstr(p->name,".gpr");
  664. if (s)
  665. {
  666. //printf("%s\n",p->name);
  667. p->subprj=new stMak;
  668. memset(p->subprj,0,sizeof(stMak));
  669. char *subMak=strdup(p->name);
  670. s=strstr(subMak,".gpr");
  671. strcpy(s,".mak");
  672. ProcessMakefile(subMak,*p->subprj,level+1);
  673. free(subMak);
  674. }
  675. p=p->next;
  676. }
  677. p=mk.base;
  678. while (p)
  679. {
  680. char *ext=strrchr(p->name,'.');
  681. if (strcmp(ext,".gpr") && strcmp(ext,".a") && strcmp(ext,".o"))
  682. ExtractDeps(f,p);
  683. p=p->next;
  684. }
  685. // Generation
  686. if (level)
  687. return;
  688. char timeBuf[32];
  689. time_t now;
  690. time(&now);
  691. struct tm *brkT=localtime(&now);
  692. strftime(timeBuf,32,"%Y-%m-%d %H:%M",brkT);
  693. fprintf(stdout,"#!/usr/bin/make\n# Automatically generated from RHIDE projects, don't edit\n# %s\n#\n\n",timeBuf);
  694. GenerateAll(stdout,mk);
  695. if (mk.mainTarget && *mk.mainTarget)
  696. {
  697. char *ext=strrchr(mk.mainTarget,'.');
  698. if (!ext) ext=".exe";
  699. if (strcmp(ext,".a")==0 || strcmp(ext,".exe")==0)
  700. {
  701. GenerateObjs(stdout,mk);
  702. GenerateLibs(stdout,mk);
  703. }
  704. }
  705. }
  706. static
  707. void SetUpCurDir()
  708. {
  709. char buf[PATH_MAX];
  710. getcwd(buf,PATH_MAX);
  711. char *mk=strstr(buf,"/makes");
  712. if (!mk)
  713. {
  714. fprintf(stderr,"We aren't in make directory\n");
  715. exit(30);
  716. }
  717. mk[1]=0;
  718. projectBase=strdup(buf);
  719. projectBaseL=strlen(projectBase);
  720. }
  721. int main(int argc, char *argv[])
  722. {
  723. if (argc!=2)
  724. {
  725. printf("Usage: %s file.mak\n",argv[0]);
  726. return 1;
  727. }
  728. stMak mak;
  729. memset(&mak,0,sizeof(mak));
  730. SetUpCurDir();
  731. ProcessMakefile(argv[1],mak,0);
  732. //ExtractSources();
  733. return 0;
  734. }