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