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