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