crunchgen.c revision 1.26 1 /* $NetBSD: crunchgen.c,v 1.26 2001/10/21 23:06:59 jmc Exp $ */
2 /*
3 * Copyright (c) 1994 University of Maryland
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
15 *
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 *
23 * Author: James da Silva, Systems Design and Analysis Group
24 * Computer Science Department
25 * University of Maryland at College Park
26 */
27 /*
28 * ========================================================================
29 * crunchgen.c
30 *
31 * Generates a Makefile and main C file for a crunched executable,
32 * from specs given in a .conf file.
33 */
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: crunchgen.c,v 1.26 2001/10/21 23:06:59 jmc Exp $");
37 #endif
38
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <stdio.h>
42 #include <ctype.h>
43 #include <string.h>
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/param.h>
48 #include <sys/utsname.h>
49
50 #define CRUNCH_VERSION "0.2"
51
52 #define MAXLINELEN 16384
53 #define MAXFIELDS 2048
54
55
56 /* internal representation of conf file: */
57
58 /* simple lists of strings suffice for most parms */
59
60 typedef struct strlst {
61 struct strlst *next;
62 char *str;
63 } strlst_t;
64
65 /* progs have structure, each field can be set with "special" or calculated */
66
67 typedef struct prog {
68 struct prog *next;
69 char *name, *ident;
70 char *srcdir, *objdir;
71 strlst_t *objs, *objpaths;
72 strlst_t *links;
73 int goterror;
74 } prog_t;
75
76
77 /* global state */
78
79 strlst_t *srcdirs = NULL;
80 strlst_t *libs = NULL;
81 prog_t *progs = NULL;
82
83 char line[MAXLINELEN];
84
85 char confname[MAXPATHLEN], infilename[MAXPATHLEN];
86 char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN];
87 char tempfname[MAXPATHLEN], cachename[MAXPATHLEN], curfilename[MAXPATHLEN];
88 char topdir[MAXPATHLEN];
89 char libdir[MAXPATHLEN] = "/usr/lib";
90 char dbg[MAXPATHLEN] = "-Os";
91 int linenum = -1;
92 int goterror = 0;
93
94 char *pname = "crunchgen";
95
96 int verbose, readcache, useobjs; /* options */
97 int reading_cache;
98 char *machine;
99 char *makeobjdirprefix;
100 char *makebin;
101 char *makeflags;
102
103 /* general library routines */
104
105 void status(char *str);
106 void out_of_memory(void);
107 void add_string(strlst_t **listp, char *str);
108 int is_dir(char *pathname);
109 int is_nonempty_file(char *pathname);
110
111 /* helper routines for main() */
112
113 void usage(void);
114 void parse_conf_file(void);
115 void gen_outputs(void);
116
117 extern char *crunched_skel[];
118
119 int main(int argc, char **argv)
120 {
121 char *p;
122 int optc;
123
124 if ((makebin = getenv("MAKE")) == NULL)
125 makebin = strdup("make");
126
127 if ((makeflags = getenv("MAKEFLAGS")) == NULL)
128 makeflags = strdup("");
129
130 if ((machine = getenv("MACHINE")) == NULL) {
131 struct utsname utsname;
132
133 if (uname(&utsname) == -1) {
134 perror("uname");
135 exit(1);
136 }
137 machine = utsname.machine;
138 }
139 makeobjdirprefix = getenv("MAKEOBJDIRPREFIX");
140 verbose = 1;
141 readcache = 1;
142 useobjs = 0;
143 *outmkname = *outcfname = *execfname = '\0';
144
145 if(argc > 0) pname = argv[0];
146
147 while((optc = getopt(argc, argv, "m:c:d:e:foqD:L:")) != -1) {
148 switch(optc) {
149 case 'f': readcache = 0; break;
150 case 'q': verbose = 0; break;
151 case 'o': useobjs = 1; break;
152
153 case 'm': strcpy(outmkname, optarg); break;
154 case 'c': strcpy(outcfname, optarg); break;
155 case 'e': strcpy(execfname, optarg); break;
156 case 'd': strcpy(dbg, optarg); break;
157
158 case 'D': strcpy(topdir, optarg); break;
159 case 'L': strcpy(libdir, optarg); break;
160
161 case '?':
162 default: usage();
163 }
164 }
165
166 argc -= optind;
167 argv += optind;
168
169 if(argc != 1) usage();
170
171 /*
172 * generate filenames
173 */
174
175 strcpy(infilename, argv[0]);
176
177 /* confname = `basename infilename .conf` */
178
179 if((p=strrchr(infilename, '/')) != NULL) strcpy(confname, p+1);
180 else strcpy(confname, infilename);
181 if((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf")) *p = '\0';
182
183 if (!*outmkname)
184 (void)snprintf(outmkname, sizeof(outmkname), "%s.mk", confname);
185 if (!*outcfname)
186 (void)snprintf(outcfname, sizeof(outcfname), "%s.c", confname);
187 if (!*execfname)
188 (void)snprintf(execfname, sizeof(execfname), "%s", confname);
189
190 (void)snprintf(cachename, sizeof(cachename), "%s.cache", confname);
191 (void)snprintf(tempfname, sizeof(tempfname), "/tmp/%sXXXXXX", confname);
192
193 parse_conf_file();
194 gen_outputs();
195
196 exit(goterror);
197 }
198
199
200 void usage(void)
201 {
202 fprintf(stderr,
203 "%s [-fq] [-m <makefile>] [-c <c file>] [-e <exec file>] <conffile>\n",
204 pname);
205 exit(1);
206 }
207
208
209 /*
210 * ========================================================================
211 * parse_conf_file subsystem
212 *
213 */
214
215 /* helper routines for parse_conf_file */
216
217 void parse_one_file(char *filename);
218 void parse_line(char *line, int *fc, char **fv, int nf);
219 void add_srcdirs(int argc, char **argv);
220 void add_progs(int argc, char **argv);
221 void add_link(int argc, char **argv);
222 void add_libs(int argc, char **argv);
223 void add_special(int argc, char **argv);
224
225 prog_t *find_prog(char *str);
226 void add_prog(char *progname);
227
228
229 void parse_conf_file(void)
230 {
231 if(!is_nonempty_file(infilename)) {
232 fprintf(stderr, "%s: fatal: input file \"%s\" not found.\n",
233 pname, infilename);
234 exit(1);
235 }
236 parse_one_file(infilename);
237 if(readcache && is_nonempty_file(cachename)) {
238 reading_cache = 1;
239 parse_one_file(cachename);
240 }
241 }
242
243
244 void parse_one_file(char *filename)
245 {
246 char *fieldv[MAXFIELDS];
247 int fieldc;
248 void (*f)(int c, char **v);
249 FILE *cf;
250
251 (void)snprintf(line, sizeof(line), "reading %s", filename);
252 status(line);
253 strcpy(curfilename, filename);
254
255 if((cf = fopen(curfilename, "r")) == NULL) {
256 perror(curfilename);
257 goterror = 1;
258 return;
259 }
260
261 linenum = 0;
262 while(fgets(line, MAXLINELEN, cf) != NULL) {
263 linenum++;
264 parse_line(line, &fieldc, fieldv, MAXFIELDS);
265 if(fieldc < 1) continue;
266 if(!strcmp(fieldv[0], "srcdirs")) f = add_srcdirs;
267 else if(!strcmp(fieldv[0], "progs")) f = add_progs;
268 else if(!strcmp(fieldv[0], "ln")) f = add_link;
269 else if(!strcmp(fieldv[0], "libs")) f = add_libs;
270 else if(!strcmp(fieldv[0], "special")) f = add_special;
271 else {
272 fprintf(stderr, "%s:%d: skipping unknown command `%s'.\n",
273 curfilename, linenum, fieldv[0]);
274 goterror = 1;
275 continue;
276 }
277 if(fieldc < 2) {
278 fprintf(stderr,
279 "%s:%d: %s command needs at least 1 argument, skipping.\n",
280 curfilename, linenum, fieldv[0]);
281 goterror = 1;
282 continue;
283 }
284 f(fieldc, fieldv);
285 }
286
287 if(ferror(cf)) {
288 perror(curfilename);
289 goterror = 1;
290 }
291 fclose(cf);
292 }
293
294
295 void parse_line(char *line, int *fc, char **fv, int nf)
296 {
297 char *p;
298
299 p = line;
300 *fc = 0;
301 while(1) {
302 while(isspace(*p)) p++;
303 if(*p == '\0' || *p == '#') break;
304
305 if(*fc < nf) fv[(*fc)++] = p;
306 while(*p && !isspace(*p) && *p != '#') p++;
307 if(*p == '\0' || *p == '#') break;
308 *p++ = '\0';
309 }
310 if(*p) *p = '\0'; /* needed for '#' case */
311 }
312
313
314 void add_srcdirs(int argc, char **argv)
315 {
316 int i;
317 char tmppath[MAXPATHLEN];
318
319 for(i=1;i<argc;i++) {
320 if (argv[i][0] == '/' || topdir[0] == '\0')
321 strcpy(tmppath, argv[i]);
322 else {
323 strcpy(tmppath, topdir);
324 strcat(tmppath, "/");
325 strcat(tmppath, argv[i]);
326 }
327 if(is_dir(tmppath))
328 add_string(&srcdirs, tmppath);
329 else {
330 fprintf(stderr, "%s:%d: `%s' is not a directory, skipping it.\n",
331 curfilename, linenum, tmppath);
332 goterror = 1;
333 }
334 }
335 }
336
337
338 void add_progs(int argc, char **argv)
339 {
340 int i;
341
342 for(i=1;i<argc;i++)
343 add_prog(argv[i]);
344 }
345
346
347 void add_prog(char *progname)
348 {
349 prog_t *p1, *p2;
350
351 /* add to end, but be smart about dups */
352
353 for(p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next)
354 if(!strcmp(p2->name, progname)) return;
355
356 p2 = malloc(sizeof(prog_t));
357 if(p2) p2->name = strdup(progname);
358 if(!p2 || !p2->name)
359 out_of_memory();
360
361 p2->next = NULL;
362 if(p1 == NULL) progs = p2;
363 else p1->next = p2;
364
365 p2->ident = p2->srcdir = p2->objdir = NULL;
366 p2->objs = p2->objpaths = p2->links = NULL;
367 p2->goterror = 0;
368 }
369
370
371 void add_link(int argc, char **argv)
372 {
373 int i;
374 prog_t *p = find_prog(argv[1]);
375
376 if(p == NULL) {
377 fprintf(stderr,
378 "%s:%d: no prog %s previously declared, skipping link.\n",
379 curfilename, linenum, argv[1]);
380 goterror = 1;
381 return;
382 }
383 for(i=2;i<argc;i++)
384 add_string(&p->links, argv[i]);
385 }
386
387
388 void add_libs(int argc, char **argv)
389 {
390 int i;
391
392 for(i=1;i<argc;i++)
393 add_string(&libs, argv[i]);
394 }
395
396
397 void add_special(int argc, char **argv)
398 {
399 int i;
400 prog_t *p = find_prog(argv[1]);
401
402 if(p == NULL) {
403 if(reading_cache) return;
404 fprintf(stderr,
405 "%s:%d: no prog %s previously declared, skipping special.\n",
406 curfilename, linenum, argv[1]);
407 goterror = 1;
408 return;
409 }
410
411 if(!strcmp(argv[2], "ident")) {
412 if(argc != 4) goto argcount;
413 if((p->ident = strdup(argv[3])) == NULL)
414 out_of_memory();
415 }
416 else if(!strcmp(argv[2], "srcdir")) {
417 if(argc != 4) goto argcount;
418 if (argv[3][0] == '/' || topdir[0] == '\0') {
419 if((p->srcdir = strdup(argv[3])) == NULL)
420 out_of_memory();
421 } else {
422 char tmppath[MAXPATHLEN];
423 strcpy(tmppath, topdir);
424 strcat(tmppath, "/");
425 strcat(tmppath, argv[3]);
426 if((p->srcdir = strdup(tmppath)) == NULL)
427 out_of_memory();
428 }
429 }
430 else if(!strcmp(argv[2], "objdir")) {
431 if(argc != 4) goto argcount;
432 if((p->objdir = strdup(argv[3])) == NULL)
433 out_of_memory();
434 }
435 else if(!strcmp(argv[2], "objs")) {
436 p->objs = NULL;
437 for(i=3;i<argc;i++)
438 add_string(&p->objs, argv[i]);
439 }
440 else if(!strcmp(argv[2], "objpaths")) {
441 p->objpaths = NULL;
442 for(i=3;i<argc;i++)
443 add_string(&p->objpaths, argv[i]);
444 }
445 else {
446 fprintf(stderr, "%s:%d: bad parameter name `%s', skipping line.\n",
447 curfilename, linenum, argv[2]);
448 goterror = 1;
449 }
450 return;
451
452
453 argcount:
454 fprintf(stderr,
455 "%s:%d: too %s arguments, expected \"special %s %s <string>\".\n",
456 curfilename, linenum, argc < 4? "few" : "many", argv[1], argv[2]);
457 goterror = 1;
458 }
459
460
461 prog_t *find_prog(char *str)
462 {
463 prog_t *p;
464
465 for(p = progs; p != NULL; p = p->next)
466 if(!strcmp(p->name, str)) return p;
467
468 return NULL;
469 }
470
471
472 /*
473 * ========================================================================
474 * gen_outputs subsystem
475 *
476 */
477
478 /* helper subroutines */
479
480 void remove_error_progs(void);
481 void fillin_program(prog_t *p);
482 void gen_specials_cache(void);
483 void gen_output_makefile(void);
484 void gen_output_cfile(void);
485
486 void fillin_program_objs(prog_t *p, char *path);
487 void top_makefile_rules(FILE *outmk);
488 void prog_makefile_rules(FILE *outmk, prog_t *p);
489 void output_strlst(FILE *outf, strlst_t *lst);
490 char *genident(char *str);
491 char *dir_search(char *progname);
492
493
494 void gen_outputs(void)
495 {
496 prog_t *p;
497
498 for(p = progs; p != NULL; p = p->next)
499 fillin_program(p);
500
501 remove_error_progs();
502 gen_specials_cache();
503 gen_output_cfile();
504 gen_output_makefile();
505 status("");
506 fprintf(stderr,
507 "Run \"make -f %s objs exe\" to build crunched binary.\n",
508 outmkname);
509 }
510
511
512 void fillin_program(prog_t *p)
513 {
514 char path[MAXPATHLEN];
515 char *srcparent;
516 strlst_t *s;
517
518 (void)snprintf(line, sizeof(line), "filling in parms for %s", p->name);
519 status(line);
520
521 if(!p->ident)
522 p->ident = genident(p->name);
523 if(!p->srcdir) {
524 srcparent = dir_search(p->name);
525 if(srcparent) {
526 (void)snprintf(path, sizeof(path), "%s/%s", srcparent, p->name);
527 if(is_dir(path))
528 p->srcdir = strdup(path);
529 }
530 }
531 if(!p->objdir && p->srcdir && useobjs) {
532 if (makeobjdirprefix) {
533 (void)snprintf(path, sizeof(path), "%s/%s", makeobjdirprefix, p->srcdir);
534 if (is_dir(path))
535 p->objdir = strdup(path);
536 }
537 if (!p->objdir) {
538 (void)snprintf(path, sizeof(path), "%s/obj.%s", p->srcdir, machine);
539 if (is_dir(path))
540 p->objdir = strdup(path);
541 }
542 if (!p->objdir) {
543 (void)snprintf(path, sizeof(path), "%s/obj", p->srcdir);
544 if(is_dir(path))
545 p->objdir = strdup(path);
546 }
547 if (!p->objdir) {
548 p->objdir = p->srcdir;
549 }
550 }
551
552 if(p->srcdir)
553 (void)snprintf(path, sizeof(path), "%s/Makefile", p->srcdir);
554 if(!p->objs && p->srcdir && is_nonempty_file(path))
555 fillin_program_objs(p, p->srcdir);
556
557 if(!p->objpaths && p->objs) {
558 if (p->objdir && useobjs) {
559 for(s = p->objs; s != NULL; s = s->next) {
560 (void)snprintf(line, sizeof(line), "%s/%s", p->objdir, s->str);
561 add_string(&p->objpaths, line);
562 }
563 } else {
564 for(s = p->objs; s != NULL; s = s->next) {
565 (void)snprintf(line, sizeof(line), "%s/%s", p->ident, s->str);
566 add_string(&p->objpaths, line);
567 }
568 }
569 }
570
571 if(!p->srcdir && verbose)
572 fprintf(stderr, "%s: %s: warning: could not find source directory.\n",
573 infilename, p->name);
574 if(!p->objs && verbose)
575 fprintf(stderr, "%s: %s: warning: could not find any .o files.\n",
576 infilename, p->name);
577
578 if(!p->objpaths) {
579 fprintf(stderr,
580 "%s: %s: error: no objpaths specified or calculated.\n",
581 infilename, p->name);
582 p->goterror = goterror = 1;
583 }
584 }
585
586 void fillin_program_objs(prog_t *p, char *dirpath)
587 {
588 char *obj, *cp;
589 int rc;
590 int fd;
591 FILE *f;
592
593 /* discover the objs from the srcdir Makefile */
594
595 if((fd = mkstemp(tempfname)) < 0) {
596 perror(tempfname);
597 exit(1);
598 }
599
600 if((f = fdopen(fd, "w")) == NULL) {
601 perror(tempfname);
602 goterror = 1;
603 return;
604 }
605
606 fprintf(f, ".include \"${.CURDIR}/Makefile\"\n");
607 fprintf(f, ".if defined(PROG)\n");
608 fprintf(f, "OBJS?= ${PROG}.o\n");
609 fprintf(f, ".endif\n");
610 fprintf(f, "crunchgen_objs:\n\t@echo 'OBJS= '${OBJS}\n");
611 fclose(f);
612
613 (void)snprintf(line, sizeof(line),
614 "cd %s && %s -f %s %s crunchgen_objs 2>&1", dirpath, makebin,
615 tempfname, makeflags);
616 if((f = popen(line, "r+")) == NULL) {
617 perror("submake pipe");
618 goterror = 1;
619 unlink(tempfname);
620 return;
621 }
622
623 while(fgets(line, MAXLINELEN, f)) {
624 if(strncmp(line, "OBJS= ", 6)) {
625 if (strcmp(line,
626 "sh: warning: running as root with dot in PATH\n") == 0)
627 continue;
628 fprintf(stderr, "make error: %s", line);
629 goterror = 1;
630 continue;
631 }
632 cp = line + 6;
633 while(isspace(*cp)) cp++;
634 while(*cp) {
635 obj = cp;
636 while(*cp && !isspace(*cp)) cp++;
637 if(*cp) *cp++ = '\0';
638 add_string(&p->objs, obj);
639 while(isspace(*cp)) cp++;
640 }
641 }
642 if((rc=pclose(f)) != 0) {
643 fprintf(stderr, "make error: make returned %d\n", rc);
644 goterror = 1;
645 }
646 unlink(tempfname);
647 }
648
649 void remove_error_progs(void)
650 {
651 prog_t *p1, *p2;
652
653 p1 = NULL; p2 = progs;
654 while(p2 != NULL) {
655 if(!p2->goterror)
656 p1 = p2, p2 = p2->next;
657 else {
658 /* delete it from linked list */
659 fprintf(stderr, "%s: %s: ignoring program because of errors.\n",
660 infilename, p2->name);
661 if(p1) p1->next = p2->next;
662 else progs = p2->next;
663 p2 = p2->next;
664 }
665 }
666 }
667
668 void gen_specials_cache(void)
669 {
670 FILE *cachef;
671 prog_t *p;
672
673 (void)snprintf(line, sizeof(line), "generating %s", cachename);
674 status(line);
675
676 if((cachef = fopen(cachename, "w")) == NULL) {
677 perror(cachename);
678 goterror = 1;
679 return;
680 }
681
682 fprintf(cachef, "# %s - parm cache generated from %s by crunchgen %s\n\n",
683 cachename, infilename, CRUNCH_VERSION);
684
685 for(p = progs; p != NULL; p = p->next) {
686 fprintf(cachef, "\n");
687 if(p->srcdir)
688 fprintf(cachef, "special %s srcdir %s\n", p->name, p->srcdir);
689 if(p->objdir && useobjs)
690 fprintf(cachef, "special %s objdir %s\n", p->name, p->objdir);
691 if(p->objs) {
692 fprintf(cachef, "special %s objs", p->name);
693 output_strlst(cachef, p->objs);
694 }
695 fprintf(cachef, "special %s objpaths", p->name);
696 output_strlst(cachef, p->objpaths);
697 }
698 fclose(cachef);
699 }
700
701
702 void gen_output_makefile(void)
703 {
704 prog_t *p;
705 FILE *outmk;
706
707 (void)snprintf(line, sizeof(line), "generating %s", outmkname);
708 status(line);
709
710 if((outmk = fopen(outmkname, "w")) == NULL) {
711 perror(outmkname);
712 goterror = 1;
713 return;
714 }
715
716 fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n",
717 outmkname, infilename, CRUNCH_VERSION);
718
719 top_makefile_rules(outmk);
720
721 for(p = progs; p != NULL; p = p->next)
722 prog_makefile_rules(outmk, p);
723
724 fprintf(outmk, "\n.include <bsd.prog.mk>\n");
725 fprintf(outmk, "\n# ========\n");
726 fclose(outmk);
727 }
728
729
730 void gen_output_cfile(void)
731 {
732 char **cp;
733 FILE *outcf;
734 prog_t *p;
735 strlst_t *s;
736
737 (void)snprintf(line, sizeof(line), "generating %s", outcfname);
738 status(line);
739
740 if((outcf = fopen(outcfname, "w")) == NULL) {
741 perror(outcfname);
742 goterror = 1;
743 return;
744 }
745
746 fprintf(outcf,
747 "/* %s - generated from %s by crunchgen %s */\n",
748 outcfname, infilename, CRUNCH_VERSION);
749
750 fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname);
751 for(cp = crunched_skel; *cp != NULL; cp++)
752 fprintf(outcf, "%s\n", *cp);
753
754 for(p = progs; p != NULL; p = p->next)
755 fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident);
756
757 fprintf(outcf, "\nstruct stub entry_points[] = {\n");
758 for(p = progs; p != NULL; p = p->next) {
759 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
760 p->name, p->ident);
761 for(s = p->links; s != NULL; s = s->next)
762 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
763 s->str, p->ident);
764 }
765
766 fprintf(outcf, "\t{ EXECNAME, crunched_main },\n");
767 fprintf(outcf, "\t{ NULL, NULL }\n};\n");
768 fclose(outcf);
769 }
770
771
772 char *genident(char *str)
773 {
774 char *n,*s,*d;
775
776 /*
777 * generates a Makefile/C identifier from a program name, mapping '-' to
778 * '_' and ignoring all other non-identifier characters. This leads to
779 * programs named "foo.bar" and "foobar" to map to the same identifier.
780 */
781
782 if((n = strdup(str)) == NULL)
783 return NULL;
784 for(d = s = n; *s != '\0'; s++) {
785 if(*s == '-') *d++ = '_';
786 else if(*s == '_' || isalnum(*s)) *d++ = *s;
787 }
788 *d = '\0';
789 return n;
790 }
791
792
793 char *dir_search(char *progname)
794 {
795 char path[MAXPATHLEN];
796 strlst_t *dir;
797
798 for(dir=srcdirs; dir != NULL; dir=dir->next) {
799 (void)snprintf(path, sizeof(path), "%s/%s", dir->str, progname);
800 if(is_dir(path)) return dir->str;
801 }
802 return NULL;
803 }
804
805
806 void top_makefile_rules(FILE *outmk)
807 {
808 prog_t *p;
809
810 fprintf(outmk, "DBG=%s\n", dbg);
811 fprintf(outmk, "STRIP?=strip\n");
812 fprintf(outmk, "MAKE?=make\n");
813 #ifdef NEW_TOOLCHAIN
814 fprintf(outmk, "OBJCOPY?=objcopy\n");
815 #else
816 fprintf(outmk, "CRUNCHIDE?=crunchide\n");
817 #endif
818
819 fprintf(outmk, "CRUNCHED_OBJS=");
820 for(p = progs; p != NULL; p = p->next)
821 fprintf(outmk, " %s.cro", p->name);
822 fprintf(outmk, "\n");
823 fprintf(outmk, "DPADD+= ${CRUNCHED_OBJS}\n");
824 fprintf(outmk, "LDADD+= ${CRUNCHED_OBJS} ");
825 output_strlst(outmk, libs);
826 fprintf(outmk, "CRUNCHEDOBJSDIRS=${CRUNCHED_OBJS:R}\n\n");
827
828 fprintf(outmk, "SUBMAKE_TARGETS=");
829 for(p = progs; p != NULL; p = p->next)
830 fprintf(outmk, " %s_make", p->ident);
831 fprintf(outmk, "\n\n");
832
833 fprintf(outmk, "PROG=%s\n", execfname);
834 fprintf(outmk, "MKMAN=no\n\n");
835
836 fprintf(outmk, "all: ${PROG}\n\t${STRIP} ${PROG}\n");
837 fprintf(outmk, "objs: $(SUBMAKE_TARGETS)\n");
838 fprintf(outmk, "exe: %s\n", execfname);
839 fprintf(outmk, "clean:\n\trm -rf %s *.cro *.o *_stub.c ${CRUNCHEDOBJSDIRS}\n",
840 execfname);
841 }
842
843
844 void prog_makefile_rules(FILE *outmk, prog_t *p)
845 {
846 fprintf(outmk, "\n# -------- %s\n\n", p->name);
847
848 fprintf(outmk, "%s_OBJPATHS=", p->ident);
849 output_strlst(outmk, p->objpaths);
850
851 if(p->srcdir && p->objs && !useobjs) {
852 fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir);
853 fprintf(outmk, "%s_OBJS=", p->ident);
854 output_strlst(outmk, p->objs);
855 fprintf(outmk, "%s_make: ${%s_OBJPATHS}\n", p->ident, p->ident);
856 fprintf(outmk, "${%s_OBJPATHS}: \n", p->ident, p->ident);
857 fprintf(outmk, "\tif [ \\! -d %s ]; then mkdir %s; fi; cd %s; \\\n",
858 p->ident, p->ident, p->ident);
859 fprintf(outmk, "\tprintf \".PATH: ${%s_SRCDIR}\\n.CURDIR:= ${%s_SRCDIR}\\n"
860 ".include \\\"\\$${.CURDIR}/Makefile\\\"\\n\" \\\n", p->ident, p->ident);
861 fprintf(outmk, "\t| ${MAKE} DBG=\"${DBG}\" -f- depend ${%s_OBJS}\n\n",
862 p->ident);
863 }
864 else
865 fprintf(outmk, "%s_make:\n\t@echo \"** Using existing objs for %s\"\n\n",
866 p->ident, p->name);
867
868
869 fprintf(outmk, "%s_stub.c:\n", p->name);
870 fprintf(outmk, "\techo \""
871 "int _crunched_%s_stub(int argc, char **argv, char **envp)"
872 "{return main(argc,argv,envp);}\" >%s_stub.c\n",
873 p->ident, p->name);
874 if (useobjs)
875 fprintf(outmk, "%s.cro: %s_stub.o\n",
876 p->name, p->name, p->ident);
877 else
878 fprintf(outmk, "%s.cro: %s_stub.o ${%s_OBJPATHS}\n",
879 p->name, p->name, p->ident);
880 fprintf(outmk, "\t${LD} -dc -r -o %s.cro %s_stub.o $(%s_OBJPATHS)\n",
881 p->name, p->name, p->ident);
882 #ifdef NEW_TOOLCHAIN
883 fprintf(outmk, "\t${OBJCOPY} -S -K _crunched_%s_stub %s.cro\n",
884 p->ident, p->name);
885 #else
886 fprintf(outmk, "\t${CRUNCHIDE} -k _crunched_%s_stub %s.cro\n",
887 p->ident, p->name);
888 #endif
889 }
890
891 void output_strlst(FILE *outf, strlst_t *lst)
892 {
893 for(; lst != NULL; lst = lst->next)
894 fprintf(outf, " %s", lst->str);
895 fprintf(outf, "\n");
896 }
897
898
899 /*
900 * ========================================================================
901 * general library routines
902 *
903 */
904
905 void status(char *str)
906 {
907 static int lastlen = 0;
908 int len, spaces;
909
910 if(!verbose) return;
911
912 len = strlen(str);
913 spaces = lastlen - len;
914 if(spaces < 1) spaces = 1;
915
916 fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
917 fflush(stderr);
918 lastlen = len;
919 }
920
921
922 void out_of_memory(void)
923 {
924 fprintf(stderr, "%s: %d: out of memory, stopping.\n", infilename, linenum);
925 exit(1);
926 }
927
928
929 void add_string(strlst_t **listp, char *str)
930 {
931 strlst_t *p1, *p2;
932
933 /* add to end, but be smart about dups */
934
935 for(p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
936 if(!strcmp(p2->str, str)) return;
937
938 p2 = malloc(sizeof(strlst_t));
939 if(p2) p2->str = strdup(str);
940 if(!p2 || !p2->str)
941 out_of_memory();
942
943 p2->next = NULL;
944 if(p1 == NULL) *listp = p2;
945 else p1->next = p2;
946 }
947
948
949 int is_dir(char *pathname)
950 {
951 struct stat buf;
952
953 if(stat(pathname, &buf) == -1)
954 return 0;
955 return S_ISDIR(buf.st_mode);
956 }
957
958 int is_nonempty_file(char *pathname)
959 {
960 struct stat buf;
961
962 if(stat(pathname, &buf) == -1)
963 return 0;
964
965 return S_ISREG(buf.st_mode) && buf.st_size > 0;
966 }
967