1 | /* |
---|
2 | |
---|
3 | */ |
---|
4 | |
---|
5 | |
---|
6 | #include <stdio.h> |
---|
7 | #include <string.h> |
---|
8 | #include <stdlib.h> |
---|
9 | #ifdef __WATCOMC__ |
---|
10 | #include <sys\types.h> |
---|
11 | #include <direct.h> |
---|
12 | #define make_dir(dir) mkdir(dir) |
---|
13 | #else |
---|
14 | #include <sys/stat.h> |
---|
15 | #define make_dir(dir) mkdir(dir,511) |
---|
16 | #endif |
---|
17 | |
---|
18 | |
---|
19 | int line_on,no_include; |
---|
20 | enum { LINUX, DOS, AIX, SUN, SGI }; |
---|
21 | |
---|
22 | char *plat_names[] = {"Linux (SVGA & X11)", |
---|
23 | "Watcom for MS-DOS", |
---|
24 | "IBM AIX for RS6000's", |
---|
25 | "Sun OS", |
---|
26 | "Silicon Graphics"}; |
---|
27 | |
---|
28 | char *plat_name[] = {"LINUX","DOS","AIX","SUN","SGI"}; |
---|
29 | |
---|
30 | |
---|
31 | |
---|
32 | char z[5000]; |
---|
33 | |
---|
34 | struct var_node |
---|
35 | { |
---|
36 | struct var_node *next; |
---|
37 | char *name; |
---|
38 | char *subst; |
---|
39 | } *first_var; |
---|
40 | |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | |
---|
45 | char *basename="noname"; |
---|
46 | char *imlib_objs=""; |
---|
47 | char *add_libs=""; |
---|
48 | char *ofiles=""; |
---|
49 | char *imlib_dir="../imlib"; |
---|
50 | char *plat_stuff=NULL; |
---|
51 | |
---|
52 | void create_dir(char *dir) |
---|
53 | { |
---|
54 | char *d,ch; |
---|
55 | d=dir; |
---|
56 | while (*d) |
---|
57 | { |
---|
58 | if (*d=='\\' || *d=='/') |
---|
59 | { |
---|
60 | ch=*d; |
---|
61 | *d=0; |
---|
62 | make_dir(dir); |
---|
63 | *d=ch; |
---|
64 | } |
---|
65 | d++; |
---|
66 | } |
---|
67 | make_dir(dir); |
---|
68 | } |
---|
69 | |
---|
70 | void oextend(char *dir, char *word, char *new_dir, int platform, int optimize); |
---|
71 | |
---|
72 | void store_var(char *name, char *subst) |
---|
73 | { |
---|
74 | struct var_node *p; |
---|
75 | for (p=first_var;p && strcmp(p->name,name);p=p->next); |
---|
76 | if (p) |
---|
77 | free(p->subst); |
---|
78 | else |
---|
79 | { |
---|
80 | p=(struct var_node *)malloc(sizeof(struct var_node)); |
---|
81 | p->name=(char *)strcpy((char *)malloc(strlen(name)+1),name); |
---|
82 | p->next=first_var; |
---|
83 | first_var=p; |
---|
84 | } |
---|
85 | p->subst=(char *)strcpy((char *)malloc(strlen(subst)+1),subst); |
---|
86 | |
---|
87 | } |
---|
88 | |
---|
89 | void expand_line(char *line, char *buffer); |
---|
90 | |
---|
91 | int get_var(char *name, char *buffer) |
---|
92 | { |
---|
93 | char *cp,*cp2; |
---|
94 | struct var_node *p; |
---|
95 | char tmp[100]; |
---|
96 | for (p=first_var;p && strcmp(p->name,name);p=p->next); |
---|
97 | if (p) |
---|
98 | { |
---|
99 | expand_line(p->subst,buffer); |
---|
100 | return 1; |
---|
101 | } |
---|
102 | else |
---|
103 | { |
---|
104 | buffer[0]=0; |
---|
105 | return 0; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | int detect_platform() |
---|
110 | { |
---|
111 | #ifdef __linux__ |
---|
112 | return LINUX; |
---|
113 | #endif |
---|
114 | |
---|
115 | #ifdef __WATCOMC__ |
---|
116 | return DOS; |
---|
117 | #endif |
---|
118 | |
---|
119 | #ifdef _AIX |
---|
120 | return AIX; |
---|
121 | #endif |
---|
122 | |
---|
123 | #ifdef sun |
---|
124 | return SUN; |
---|
125 | #endif |
---|
126 | |
---|
127 | #ifdef SUN3 |
---|
128 | return SUN; |
---|
129 | #endif |
---|
130 | |
---|
131 | #ifdef SUN4 |
---|
132 | return SUN; |
---|
133 | #endif |
---|
134 | |
---|
135 | printf("Cannot detect platform\n"); |
---|
136 | exit(1); |
---|
137 | |
---|
138 | |
---|
139 | return 0; |
---|
140 | } |
---|
141 | |
---|
142 | int get_word(char **st, char *buffer) |
---|
143 | { |
---|
144 | char *bp; |
---|
145 | while (*(*st)==' ') (*st)++; |
---|
146 | if (*(*st)==0 || *(*st)=='\n' || *(*st)=='\r' || *(*st)=='=') |
---|
147 | return 0; |
---|
148 | |
---|
149 | for (bp=buffer;*(*st) && *(*st)!=' ' && *(*st)!='\n' && *(*st)!='\r' && *(*st)!='=';(*st)++,bp++) |
---|
150 | *bp=*(*st); |
---|
151 | *(bp)=0; |
---|
152 | return 1; |
---|
153 | } |
---|
154 | |
---|
155 | void get_equal(char **st) |
---|
156 | { |
---|
157 | while (*(*st)==' ') (*st)++; |
---|
158 | if (*(*st)!='=') |
---|
159 | { |
---|
160 | printf("expecting '=' on line %d\n",line_on); |
---|
161 | exit(0); |
---|
162 | } |
---|
163 | (*st)++; |
---|
164 | } |
---|
165 | |
---|
166 | void process_file(char *name); |
---|
167 | |
---|
168 | void expand_line(char *line, char *buffer) |
---|
169 | { |
---|
170 | char tmp[100],*cp2; |
---|
171 | for (;*line;) |
---|
172 | { |
---|
173 | if (*line=='$') |
---|
174 | { |
---|
175 | line++; |
---|
176 | if (*line=='$') |
---|
177 | *(buffer++)=*(line++); |
---|
178 | else if (*line!='(') |
---|
179 | { |
---|
180 | printf("Expecting ( after $\n"); |
---|
181 | exit(0); |
---|
182 | } else |
---|
183 | { |
---|
184 | line++; |
---|
185 | for (cp2=tmp;*line!=')' && *line;line++,cp2++) *cp2=*line; line++; |
---|
186 | *cp2=0; |
---|
187 | get_var(tmp,buffer); |
---|
188 | while (*buffer) buffer++; |
---|
189 | } |
---|
190 | } else *(buffer++)=*(line++); |
---|
191 | } |
---|
192 | *buffer=0; |
---|
193 | } |
---|
194 | |
---|
195 | void process_line(char *st, FILE *fp) |
---|
196 | { |
---|
197 | char word[100],*wp,*ws,skip,rd; |
---|
198 | expand_line(st,z); |
---|
199 | st=z; |
---|
200 | |
---|
201 | if (get_word(&st,word)) |
---|
202 | { |
---|
203 | if (!strcmp(word,"INCLUDE")) |
---|
204 | { |
---|
205 | if (!get_word(&st,word)) |
---|
206 | { |
---|
207 | fprintf(stderr,"expecting filename after INCLUDE on line %d\n",line_on); |
---|
208 | exit(1); |
---|
209 | } |
---|
210 | process_file(word); |
---|
211 | } else if (!strcmp(word,"SECTION")) |
---|
212 | { |
---|
213 | skip=0; rd=0; |
---|
214 | wp=st; |
---|
215 | do |
---|
216 | { |
---|
217 | while (*wp==' ') wp++; |
---|
218 | ws=wp; |
---|
219 | if (*wp==0) skip=1; |
---|
220 | else |
---|
221 | { |
---|
222 | while (*wp!=' ' && *wp) wp++; |
---|
223 | *wp=0; |
---|
224 | if (!strcmp(ws,plat_name[detect_platform()])) |
---|
225 | rd=1; |
---|
226 | } |
---|
227 | } while (!skip && !rd); |
---|
228 | do |
---|
229 | { |
---|
230 | fgets(word,100,fp); |
---|
231 | while (word[strlen(word)-1]=='\n' || word[strlen(word)-1]=='\r') |
---|
232 | word[strlen(word)-1]=0; |
---|
233 | strcat(word,"\n"); |
---|
234 | if (rd) |
---|
235 | { |
---|
236 | if (strcmp(word,"END\n")) |
---|
237 | { |
---|
238 | if (plat_stuff) |
---|
239 | { |
---|
240 | plat_stuff=realloc(plat_stuff,strlen(plat_stuff)+1+strlen(word)); |
---|
241 | strcat(plat_stuff,word); |
---|
242 | } |
---|
243 | else |
---|
244 | { |
---|
245 | plat_stuff=malloc(strlen(plat_stuff)+1+strlen(word)); |
---|
246 | strcpy(plat_stuff,word); |
---|
247 | } |
---|
248 | } |
---|
249 | } |
---|
250 | } while (strcmp(word,"END\n")); |
---|
251 | |
---|
252 | |
---|
253 | } else |
---|
254 | { |
---|
255 | get_equal(&st); |
---|
256 | store_var(word,st); |
---|
257 | } |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | void dos_path(char *path) |
---|
263 | { |
---|
264 | if (detect_platform()==DOS) |
---|
265 | { |
---|
266 | for (;*path;path++) |
---|
267 | if (*path=='/') *path='\\'; |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | char *include_path(int platform) |
---|
272 | { |
---|
273 | char tmp[200]; |
---|
274 | if (platform==DOS) |
---|
275 | { |
---|
276 | z[0]=0; |
---|
277 | /* dos_path(imlib_dir,tmp); |
---|
278 | sprintf(z,"-ic:\\watcom\\h;%s\\include;-i. ",tmp); */ |
---|
279 | } |
---|
280 | else |
---|
281 | sprintf(z,"-I%s/include",imlib_dir); |
---|
282 | return z; |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | void debug_flags(FILE *fp, int platform) |
---|
287 | { |
---|
288 | switch (platform) |
---|
289 | { |
---|
290 | case LINUX : |
---|
291 | case SUN : |
---|
292 | case AIX : |
---|
293 | case SGI : |
---|
294 | { |
---|
295 | fprintf(fp,"-g"); |
---|
296 | } break; |
---|
297 | case DOS : |
---|
298 | { |
---|
299 | fprintf(fp,"/zq /d2"); |
---|
300 | } break; |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | char *compiler(int platform) |
---|
305 | { |
---|
306 | switch(platform) |
---|
307 | { |
---|
308 | case DOS : |
---|
309 | { return "wpp386"; } break; |
---|
310 | default : |
---|
311 | { return "g++"; } break; |
---|
312 | } |
---|
313 | } |
---|
314 | |
---|
315 | int fetch_word(char **ch, char *buffer) |
---|
316 | { |
---|
317 | while (*(*ch)==' ' || *(*ch)=='\t') (*ch)++; |
---|
318 | if (*(*ch)==0) return 0; |
---|
319 | while (*(*ch) && *(*ch)!=' ' && *(*ch)!='\t') |
---|
320 | { |
---|
321 | *(buffer++)=*(*ch); |
---|
322 | (*ch)++; |
---|
323 | } |
---|
324 | *buffer=0; |
---|
325 | return 1; |
---|
326 | } |
---|
327 | |
---|
328 | void list_o_files(char *name, FILE *fp, char *fl, char *dir, char *ext, int platform, int optimize) |
---|
329 | { |
---|
330 | char fname[200],ofname[200],*ch,sl; |
---|
331 | if (platform==DOS) sl='\\'; else sl='/'; |
---|
332 | if (optimize) |
---|
333 | fprintf(fp,"%s_O = ",name); |
---|
334 | else |
---|
335 | fprintf(fp,"%s = ",name); |
---|
336 | |
---|
337 | while (*fl==' ' || *fl=='\t') fl++; |
---|
338 | while (*fl) |
---|
339 | { |
---|
340 | if (dir && dir[0]) |
---|
341 | sprintf(fname,"%s%c",dir,sl); |
---|
342 | else fname[0]=0; |
---|
343 | ch=fname+strlen(fname); |
---|
344 | |
---|
345 | |
---|
346 | while (*fl && *fl!=' ' && *fl!='\t') |
---|
347 | *(ch++)=*(fl++); |
---|
348 | *ch=0; |
---|
349 | oextend(NULL,fname,ofname,platform,optimize); |
---|
350 | fprintf(fp,"%s",ofname); |
---|
351 | |
---|
352 | while (*fl==' ' || *fl=='\t') fl++; |
---|
353 | if (*fl) |
---|
354 | { |
---|
355 | if (platform==DOS) |
---|
356 | fprintf(fp," &\n\t"); |
---|
357 | else |
---|
358 | fprintf(fp," \\\n\t"); |
---|
359 | } |
---|
360 | else fprintf(fp,"\n\n"); |
---|
361 | } |
---|
362 | } |
---|
363 | |
---|
364 | char *object_extension(int platform) |
---|
365 | { |
---|
366 | if (platform==DOS) |
---|
367 | return ".obj"; |
---|
368 | else return ".o"; |
---|
369 | } |
---|
370 | |
---|
371 | void list_wlink_files(FILE *fp, char *var, char *dir, int optimize) |
---|
372 | { |
---|
373 | char tmp[2000],name[100],*prep,*last_slash,*np; |
---|
374 | get_var(var,tmp); |
---|
375 | |
---|
376 | if (!dir) dir="."; |
---|
377 | dos_path(dir); |
---|
378 | var=tmp; |
---|
379 | while (*var) |
---|
380 | { |
---|
381 | while (*var==' ' || *var=='\t') var++; |
---|
382 | if (*var) |
---|
383 | { |
---|
384 | for (prep=name,last_slash=NULL;*var!=' ' && *var!='\t' && *var;prep++,var++) |
---|
385 | { |
---|
386 | *prep=*var; |
---|
387 | if (*var=='/') |
---|
388 | { |
---|
389 | last_slash=prep; |
---|
390 | *prep='\\'; |
---|
391 | } |
---|
392 | } |
---|
393 | *prep=0; |
---|
394 | if (last_slash) |
---|
395 | { |
---|
396 | *last_slash=0; |
---|
397 | fprintf(fp,"file %s\\%s\\DOS\\",dir,name); |
---|
398 | np=last_slash+1; |
---|
399 | } else |
---|
400 | { |
---|
401 | fprintf(fp,"file %s\\DOS\\",dir); |
---|
402 | np=name; |
---|
403 | } |
---|
404 | if (optimize) |
---|
405 | fprintf(fp,"opt\\"); |
---|
406 | else fprintf(fp,"debug\\"); |
---|
407 | fprintf(fp,"%s.obj\n",np); |
---|
408 | } |
---|
409 | } |
---|
410 | } |
---|
411 | |
---|
412 | void make_program(FILE *fp, char *name, char *plat_base, int platform, int optimize) |
---|
413 | { |
---|
414 | FILE *lfp; |
---|
415 | char tmp[2000],tmp2[200],de[5],*oe,*oe2; |
---|
416 | sprintf(tmp2,"%s_FILES",plat_base); |
---|
417 | get_var(tmp2,tmp); |
---|
418 | list_o_files(tmp2,fp,tmp,imlib_dir,object_extension(platform),platform,optimize); |
---|
419 | if (platform==DOS) |
---|
420 | strcpy(de,".exe"); |
---|
421 | else |
---|
422 | de[0]=0; |
---|
423 | |
---|
424 | if (optimize) { oe="o"; oe2="_O";} else { oe=""; oe2=""; } |
---|
425 | |
---|
426 | fprintf(fp,"%s%s%s : $(%s_FILES%s) $(IMLIB_OBJS%s) $(PROG_OBJS%s)\n",name,oe,de,plat_base,oe2,oe2,oe2); |
---|
427 | |
---|
428 | if (platform!=DOS) |
---|
429 | { |
---|
430 | if (optimize) |
---|
431 | fprintf(fp,"\t$(CC) -o %s%s $(%s_FILES_O) $(IMLIB_OBJS_O) $(PROG_OBJS_O)", |
---|
432 | name,oe,plat_base); |
---|
433 | else |
---|
434 | fprintf(fp,"\t$(CC) -o %s%s $(%s_FILES) $(IMLIB_OBJS) $(PROG_OBJS)", |
---|
435 | name,oe,plat_base); |
---|
436 | |
---|
437 | sprintf(tmp,"%s_LIBS",plat_base); |
---|
438 | if (get_var(tmp,tmp)) |
---|
439 | fprintf(fp," %s",tmp); |
---|
440 | fprintf(fp,"\n"); |
---|
441 | } else |
---|
442 | { |
---|
443 | if (optimize) |
---|
444 | sprintf(tmp,"%so.lnk",name); |
---|
445 | else |
---|
446 | sprintf(tmp,"%s.lnk",name); |
---|
447 | |
---|
448 | fprintf(fp,"\twlink @%s\n",tmp); |
---|
449 | lfp=fopen(tmp,"wb"); |
---|
450 | if (!lfp) |
---|
451 | { |
---|
452 | printf("Unable to open linker file %s\n",tmp); |
---|
453 | exit(0); |
---|
454 | } |
---|
455 | if (!optimize) |
---|
456 | fprintf(lfp,"debug all\n"); |
---|
457 | fprintf(lfp,"system dos4g\n" |
---|
458 | "option caseexact\n"); |
---|
459 | if (optimize) |
---|
460 | fprintf(lfp,"name %so%s\n",name,de); |
---|
461 | else |
---|
462 | fprintf(lfp,"name %s%s\n",name,de); |
---|
463 | |
---|
464 | if (get_var("STACK_SIZE",tmp2)) |
---|
465 | fprintf(lfp,"option stack=%s\n",tmp2); |
---|
466 | else fprintf(lfp,"option stack=8k\n"); |
---|
467 | |
---|
468 | |
---|
469 | |
---|
470 | sprintf(tmp,"%s_FILES",plat_base); |
---|
471 | list_wlink_files(lfp,tmp,imlib_dir,optimize); |
---|
472 | sprintf(tmp,"IMLIB_OBJS"); |
---|
473 | list_wlink_files(lfp,tmp,imlib_dir,optimize); |
---|
474 | sprintf(tmp,"O_FILES"); |
---|
475 | list_wlink_files(lfp,tmp,NULL,optimize); |
---|
476 | fclose(lfp); |
---|
477 | } |
---|
478 | |
---|
479 | fprintf(fp,"\n"); |
---|
480 | } |
---|
481 | |
---|
482 | |
---|
483 | struct dep_file |
---|
484 | { |
---|
485 | struct dep_file *next; |
---|
486 | char *name; |
---|
487 | } ; |
---|
488 | |
---|
489 | |
---|
490 | struct dep_file *make_depend_list(struct dep_file *first, char *name, int platform) |
---|
491 | { |
---|
492 | FILE *fp; |
---|
493 | struct dep_file *p; |
---|
494 | char tmp[200],*ch,*ch2; |
---|
495 | strcpy(tmp,name); |
---|
496 | fp=fopen(tmp,"rb"); |
---|
497 | if (!fp) |
---|
498 | { |
---|
499 | if (platform==DOS) |
---|
500 | sprintf(tmp,"%s\\include\\%s",imlib_dir,name); |
---|
501 | else |
---|
502 | sprintf(tmp,"%s/include/%s",imlib_dir,name); |
---|
503 | fp=fopen(tmp,"rb"); |
---|
504 | } |
---|
505 | |
---|
506 | if (fp) |
---|
507 | { |
---|
508 | for (p=first;p;p=p->next) |
---|
509 | { |
---|
510 | if (!strcmp(p->name,tmp)) /* make sure we have not already processed this file */ |
---|
511 | { |
---|
512 | fclose(fp); |
---|
513 | return first; |
---|
514 | } |
---|
515 | } |
---|
516 | p=(struct dep_file *)malloc(sizeof(struct dep_file)); |
---|
517 | p->next=first; |
---|
518 | p->name=(char *)strcpy((char *)malloc(strlen(tmp)+1),tmp); |
---|
519 | first=p; |
---|
520 | |
---|
521 | if (!no_include) |
---|
522 | { |
---|
523 | while (!feof(fp)) |
---|
524 | { |
---|
525 | fgets(tmp,200,fp); |
---|
526 | if (!feof(fp)) |
---|
527 | { |
---|
528 | if (memcmp(tmp,"#include",8)==0) |
---|
529 | { |
---|
530 | for (ch=tmp+8;*ch==' ' || *ch=='\t';ch++); |
---|
531 | if (*ch=='"') |
---|
532 | { |
---|
533 | ch++; |
---|
534 | for (ch2=tmp;*ch!='"';ch++,ch2++) |
---|
535 | { *ch2=*ch; } |
---|
536 | *ch2=0; |
---|
537 | first=make_depend_list(first,tmp,platform); |
---|
538 | } |
---|
539 | } |
---|
540 | } |
---|
541 | } |
---|
542 | } |
---|
543 | fclose(fp); |
---|
544 | } |
---|
545 | return first; |
---|
546 | } |
---|
547 | |
---|
548 | void list_c_depends(FILE *fp, char *file, int platform, int optimize) |
---|
549 | { |
---|
550 | char nn[200],nn2[200],*ndir; |
---|
551 | struct dep_file *first,*p; |
---|
552 | first=make_depend_list(NULL,file,platform); |
---|
553 | fprintf(stderr,"checking dependancies for : %s \r",file); |
---|
554 | strcpy(nn,file); |
---|
555 | nn[strlen(nn)-2]=0; |
---|
556 | oextend(NULL,nn,nn2,platform,optimize); |
---|
557 | if (nn2[0]=='.' && (nn2[1]=='\\' || nn2[1]=='/')) |
---|
558 | ndir=nn2+2; |
---|
559 | else ndir=nn2; |
---|
560 | while (first) |
---|
561 | { |
---|
562 | if (strcmp(first->name,file)) |
---|
563 | fprintf(fp,"%s : %s\n",ndir,first->name); |
---|
564 | p=first; |
---|
565 | first=first->next; |
---|
566 | free(p); |
---|
567 | } |
---|
568 | } |
---|
569 | |
---|
570 | void oextend(char *dir, char *word, char *new_dir, int platform, int optimize) |
---|
571 | { |
---|
572 | char slash,*sl,*last_slash,ext[200],small_word[100],mk[200]; |
---|
573 | if (platform==DOS) slash='\\'; else slash='/'; |
---|
574 | new_dir[0]=0; |
---|
575 | if (dir && strcmp(dir,".") && strcmp(dir,".\\") && strcmp(dir,"./")) |
---|
576 | sprintf(new_dir,"%s%c%s",dir,slash,word); |
---|
577 | else strcpy(new_dir,word); |
---|
578 | |
---|
579 | for (sl=new_dir,last_slash=NULL;*sl;sl++) |
---|
580 | if (*sl=='\\' || *sl=='/') |
---|
581 | last_slash=sl; |
---|
582 | |
---|
583 | if (last_slash) |
---|
584 | { |
---|
585 | strcpy(small_word,last_slash+1); |
---|
586 | *(last_slash+1)=0; |
---|
587 | } |
---|
588 | else |
---|
589 | { |
---|
590 | strcpy(small_word,new_dir); |
---|
591 | new_dir[0]=0; |
---|
592 | } |
---|
593 | |
---|
594 | if (dir && strcmp(dir,".") && strcmp(dir,".\\") && strcmp(dir,"./")) |
---|
595 | sprintf(mk,"%s%c%s",dir,slash,word); |
---|
596 | else |
---|
597 | strcpy(mk,word); |
---|
598 | |
---|
599 | |
---|
600 | sprintf(mk,"%s%c%s",dir,slash,word); |
---|
601 | |
---|
602 | for (sl=mk;*sl;sl++); |
---|
603 | while (*sl!='/' && *sl!='\\') sl--; |
---|
604 | sl++; |
---|
605 | if (optimize) |
---|
606 | sprintf(sl,"%s/%s",plat_name[platform],"opt"); |
---|
607 | else |
---|
608 | sprintf(sl,"%s/%s",plat_name[platform],"debug"); |
---|
609 | create_dir(mk); |
---|
610 | |
---|
611 | |
---|
612 | if (optimize) |
---|
613 | sprintf(ext,"%s/%s/%s%s",plat_name[platform],"opt",small_word,object_extension(platform)); |
---|
614 | else |
---|
615 | sprintf(ext,"%s/%s/%s%s",plat_name[platform],"debug",small_word,object_extension(platform)); |
---|
616 | |
---|
617 | strcat(new_dir,ext); |
---|
618 | if (platform==DOS) |
---|
619 | { |
---|
620 | for (sl=new_dir;*sl;sl++) |
---|
621 | if (*sl=='/') *sl='\\'; |
---|
622 | } |
---|
623 | } |
---|
624 | |
---|
625 | void list_o_depends(FILE *fp, char *var, char *dir, int platform, int optimize) |
---|
626 | { |
---|
627 | char *ch,word[100],tmp[200],o_name[200],sl,*tail; |
---|
628 | if (platform==DOS) sl='\\'; else sl='/'; |
---|
629 | get_var(var,z); |
---|
630 | ch=z; |
---|
631 | if (!dir) dir="."; |
---|
632 | dos_path(dir); |
---|
633 | if (optimize) tail="_O"; else tail="_D"; |
---|
634 | |
---|
635 | while (fetch_word(&ch,word)) |
---|
636 | { |
---|
637 | oextend(dir,word,o_name,platform,optimize); |
---|
638 | if (!get_var(o_name,z)) |
---|
639 | { |
---|
640 | dos_path(word); |
---|
641 | if (platform!=DOS) |
---|
642 | { |
---|
643 | if (!strcmp(dir,".")) |
---|
644 | fprintf(fp,"%s : %s.c\n",o_name,word); |
---|
645 | else |
---|
646 | fprintf(fp,"%s : %s%c%s.c\n",o_name,dir,sl,word); |
---|
647 | } |
---|
648 | else |
---|
649 | fprintf(fp,"%s : .%c%s%c%s.c\n",o_name,sl,dir,sl,word); |
---|
650 | |
---|
651 | if (platform==DOS) |
---|
652 | fprintf(fp,"\twpp386 %s%c%s.c $(CFLAGS%s) -fo=%s\n",dir,sl,word,tail,o_name); |
---|
653 | else |
---|
654 | fprintf(fp,"\t$(CC) %s%c%s.c $(CFLAGS%s) -c -o %s\n",dir,sl,word,tail,o_name); |
---|
655 | |
---|
656 | |
---|
657 | store_var(o_name,"O"); |
---|
658 | if (dir) |
---|
659 | sprintf(tmp,"%s%c%s.c",dir,sl,word); |
---|
660 | else |
---|
661 | sprintf(tmp,"%s.c",word); |
---|
662 | list_c_depends(fp,tmp,platform,optimize); |
---|
663 | } |
---|
664 | } |
---|
665 | |
---|
666 | } |
---|
667 | |
---|
668 | |
---|
669 | void write_flags(FILE *fp,int platform, int optimize) |
---|
670 | { |
---|
671 | char tmp[1000]; |
---|
672 | char nm[100]; |
---|
673 | if (optimize) |
---|
674 | fprintf(fp,"CFLAGS_O=%s",include_path(platform)); |
---|
675 | else |
---|
676 | fprintf(fp,"CFLAGS_D=%s",include_path(platform)); |
---|
677 | |
---|
678 | if (optimize) |
---|
679 | sprintf(nm,"%s_OPTIMIZE",plat_name[platform]); |
---|
680 | else |
---|
681 | sprintf(nm,"%s_DEBUG",plat_name[platform]); |
---|
682 | |
---|
683 | get_var(nm,tmp); |
---|
684 | fprintf(fp," %s ",tmp); |
---|
685 | |
---|
686 | |
---|
687 | if (platform!=DOS && platform!=LINUX) |
---|
688 | fprintf(fp,"-DBIG_ENDIANS "); |
---|
689 | if (platform==SUN) |
---|
690 | fprintf(fp,"-I/lusr/X11R5/include "); |
---|
691 | if (get_var("CFLAGS",z)) |
---|
692 | fprintf(fp,"%s ",z); |
---|
693 | |
---|
694 | sprintf(tmp,"%s_FLAGS",plat_name[platform]); |
---|
695 | if (get_var(tmp,z)) |
---|
696 | fprintf(fp,"%s ",z); |
---|
697 | |
---|
698 | |
---|
699 | fprintf(fp,"\n"); |
---|
700 | } |
---|
701 | |
---|
702 | void make_makefile(int platform) |
---|
703 | { |
---|
704 | char tmp[100],*s,de[5]; |
---|
705 | FILE *fp; |
---|
706 | if (platform==DOS) |
---|
707 | strcpy(de,".exe"); |
---|
708 | else de[0]=0; |
---|
709 | |
---|
710 | if (!get_var("MAKEFILE_NAME",tmp)) |
---|
711 | { |
---|
712 | if (platform==DOS) |
---|
713 | strcpy(tmp,"makefile.wat"); |
---|
714 | else |
---|
715 | strcpy(tmp,"Makefile"); |
---|
716 | } |
---|
717 | |
---|
718 | fp=fopen(tmp,"w"); |
---|
719 | |
---|
720 | if (!fp) |
---|
721 | { |
---|
722 | printf("Unable to open %s for writing\n",tmp); |
---|
723 | exit(0); |
---|
724 | } |
---|
725 | |
---|
726 | fprintf(fp,"CC=%s\n",compiler(platform)); |
---|
727 | |
---|
728 | write_flags(fp,platform,0); |
---|
729 | write_flags(fp,platform,1); |
---|
730 | |
---|
731 | |
---|
732 | |
---|
733 | list_o_files("IMLIB_OBJS",fp,imlib_objs,imlib_dir,object_extension(platform),platform,0); |
---|
734 | list_o_files("IMLIB_OBJS",fp,imlib_objs,imlib_dir,object_extension(platform),platform,1); |
---|
735 | list_o_files("PROG_OBJS",fp,ofiles,NULL,object_extension(platform),platform,0); |
---|
736 | list_o_files("PROG_OBJS",fp,ofiles,NULL,object_extension(platform),platform,1); |
---|
737 | |
---|
738 | |
---|
739 | if (platform==LINUX) /* for linux make two versions of program, X11 & SVGA */ |
---|
740 | { |
---|
741 | sprintf(tmp,"%sx",basename); |
---|
742 | fprintf(fp,"all : %s %s\n\n",basename,tmp); |
---|
743 | |
---|
744 | make_program(fp,basename,"LINUX_SVGA",platform,0); |
---|
745 | make_program(fp,tmp,"LINUX_X",platform,0); |
---|
746 | |
---|
747 | fprintf(fp,"opt : %so %so\n\n",basename,tmp); |
---|
748 | |
---|
749 | make_program(fp,basename,"LINUX_SVGA",platform,1); |
---|
750 | make_program(fp,tmp,"LINUX_X",platform,1); |
---|
751 | } |
---|
752 | else |
---|
753 | { |
---|
754 | fprintf(fp,"all : %s%s\n\n",basename,de); |
---|
755 | make_program(fp,basename,plat_name[platform],platform,0); |
---|
756 | |
---|
757 | fprintf(fp,"opt : %so%s\n\n",basename,de); |
---|
758 | make_program(fp,basename,plat_name[platform],platform,1); |
---|
759 | } |
---|
760 | sprintf(tmp,"%s_FILES",plat_name[platform]); |
---|
761 | |
---|
762 | list_o_depends(fp,tmp,imlib_dir,platform,0); |
---|
763 | list_o_depends(fp,tmp,imlib_dir,platform,1); |
---|
764 | list_o_depends(fp,"IMLIB_OBJS",imlib_dir,platform,0); |
---|
765 | list_o_depends(fp,"IMLIB_OBJS",imlib_dir,platform,1); |
---|
766 | list_o_depends(fp,"O_FILES",NULL,platform,0); |
---|
767 | list_o_depends(fp,"O_FILES",NULL,platform,1); |
---|
768 | if (platform!=DOS) |
---|
769 | { |
---|
770 | fprintf(fp,"clean :\n\t" |
---|
771 | "rm -f $(%s_FILES) $(IMLIB_OBJS) $(O_FILES)\n", |
---|
772 | plat_name[platform]); |
---|
773 | fprintf(fp,"cleano :\n\t" |
---|
774 | "rm -f $(%s_FILES_O) $(IMLIB_OBJS_O) $(O_FILES_O)\n", |
---|
775 | plat_name[platform]); |
---|
776 | } |
---|
777 | if (plat_stuff) |
---|
778 | fprintf(fp,"%s",plat_stuff); /* add any platform specific additions */ |
---|
779 | |
---|
780 | fclose(fp); |
---|
781 | } |
---|
782 | |
---|
783 | |
---|
784 | void process_file(char *name) |
---|
785 | { |
---|
786 | char line[500],tmp[3000],*chp; |
---|
787 | int ol; |
---|
788 | int get_next_line,i; |
---|
789 | FILE *fp; |
---|
790 | ol=line_on; |
---|
791 | fp=fopen(name,"rb"); |
---|
792 | if (!fp) |
---|
793 | { |
---|
794 | printf("Unable to open makefile template (%s)\n",name); |
---|
795 | exit(1); |
---|
796 | } |
---|
797 | |
---|
798 | line_on=1; |
---|
799 | while (!feof(fp)) |
---|
800 | { |
---|
801 | tmp[0]=0; |
---|
802 | do |
---|
803 | { |
---|
804 | get_next_line=0; |
---|
805 | fgets(line,1000,fp); |
---|
806 | |
---|
807 | for (chp=line;*chp && *chp!=';';chp++); |
---|
808 | *chp=0; |
---|
809 | |
---|
810 | while (line[strlen(line)-1]=='\n' || line[strlen(line)-1]=='\r' || line[strlen(line)-1]==' ') |
---|
811 | line[strlen(line)-1]=0; |
---|
812 | |
---|
813 | if (line[strlen(line)-1]=='\\') |
---|
814 | { |
---|
815 | line[strlen(line)-1]=0; |
---|
816 | get_next_line=1; |
---|
817 | } |
---|
818 | strcat(tmp,line); |
---|
819 | |
---|
820 | } while (get_next_line && !feof(fp)); |
---|
821 | |
---|
822 | |
---|
823 | if (!feof(fp)) |
---|
824 | process_line(tmp,fp); |
---|
825 | line_on++; |
---|
826 | } |
---|
827 | |
---|
828 | fclose(fp); |
---|
829 | line_on=ol; |
---|
830 | } |
---|
831 | |
---|
832 | char *template_file="maker.tmp"; |
---|
833 | main(int argc, char **argv) |
---|
834 | { |
---|
835 | int pf,i; |
---|
836 | char tmp[1000]; |
---|
837 | first_var=NULL; |
---|
838 | no_include=0; |
---|
839 | |
---|
840 | for (i=1;i<argc;i++) |
---|
841 | { |
---|
842 | if (!strcmp(argv[i],"-f")) |
---|
843 | { |
---|
844 | i++; |
---|
845 | template_file=argv[i]; |
---|
846 | } |
---|
847 | if (!strcmp(argv[i],"-noi")) |
---|
848 | no_include=1; |
---|
849 | } |
---|
850 | |
---|
851 | process_file(template_file); |
---|
852 | pf=detect_platform(); |
---|
853 | printf("Genering Makefile for %s\n",plat_names[pf]); |
---|
854 | |
---|
855 | if (get_var("O_FILES",tmp)) |
---|
856 | ofiles=(char *)strcpy((char *)malloc(strlen(tmp)+1),tmp); |
---|
857 | if (get_var("BASE_NAME",tmp)) |
---|
858 | basename=(char *)strcpy((char *)malloc(strlen(tmp)+1),tmp); |
---|
859 | if (get_var("IMLIB_DIR",tmp)) |
---|
860 | imlib_dir=(char *)strcpy((char *)malloc(strlen(tmp)+1),tmp); |
---|
861 | if (get_var("IMLIB_OBJS",tmp)) |
---|
862 | imlib_objs=(char *)strcpy((char *)malloc(strlen(tmp)+1),tmp); |
---|
863 | |
---|
864 | |
---|
865 | make_makefile(pf); |
---|
866 | printf("done \n"); |
---|
867 | } |
---|
868 | |
---|
869 | |
---|