2 * Copyright (c) 1994 University of Maryland
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of U.M. not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. U.M. makes no representations about the
12 * suitability of this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
15 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
17 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
19 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Author: James da Silva, Systems Design and Analysis Group
23 * Computer Science Department
24 * University of Maryland at College Park
27 * ========================================================================
30 * Generates a Makefile and main C file for a crunched executable,
31 * from specs given in a .conf file.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
48 #define CRUNCH_VERSION "0.2"
50 #define MAXLINELEN 16384
51 #define MAXFIELDS 2048
54 /* internal representation of conf file: */
56 /* simple lists of strings suffice for most parms */
58 typedef struct strlst {
63 /* progs have structure, each field can be set with "special" or calculated */
66 struct prog *next; /* link field */
67 char *name; /* program name */
68 char *ident; /* C identifier for the program name */
72 char *objvar; /* Makefile variable to replace OBJS */
73 strlst_t *objs, *objpaths;
85 strlst_t *buildopts = NULL;
86 strlst_t *srcdirs = NULL;
87 strlst_t *libs = NULL;
88 strlst_t *libs_so = NULL;
91 char confname[MAXPATHLEN], infilename[MAXPATHLEN];
92 char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN];
93 char tempfname[MAXPATHLEN], cachename[MAXPATHLEN], curfilename[MAXPATHLEN];
94 char outhdrname[MAXPATHLEN] ; /* user-supplied header for *.mk */
95 char *objprefix; /* where are the objects ? */
100 int verbose, readcache; /* options */
102 int makeobj = 0; /* add 'make obj' rules to the makefile */
106 /* general library routines */
108 void status(const char *str);
109 void out_of_memory(void);
110 void add_string(strlst_t **listp, char *str);
111 int is_dir(const char *pathname);
112 int is_nonempty_file(const char *pathname);
113 int subtract_strlst(strlst_t **lista, strlst_t **listb);
114 int in_list(strlst_t **listp, char *str);
116 /* helper routines for main() */
119 void parse_conf_file(void);
120 void gen_outputs(void);
122 extern char *crunched_skel[];
126 main(int argc, char **argv)
133 *outmkname = *outcfname = *execfname = '\0';
135 path_make = getenv("MAKE");
136 if (path_make == NULL || *path_make == '\0')
139 p = getenv("MAKEOBJDIRPREFIX");
140 if (p == NULL || *p == '\0')
141 objprefix = "/usr/obj"; /* default */
143 if ((objprefix = strdup(p)) == NULL)
146 while((optc = getopt(argc, argv, "lh:m:c:e:p:foq")) != -1) {
159 strlcpy(outmkname, optarg, sizeof(outmkname));
162 if ((objprefix = strdup(optarg)) == NULL)
167 strlcpy(outhdrname, optarg, sizeof(outhdrname));
170 strlcpy(outcfname, optarg, sizeof(outcfname));
173 strlcpy(execfname, optarg, sizeof(execfname));
197 strlcpy(infilename, argv[0], sizeof(infilename));
199 /* confname = `basename infilename .conf` */
201 if ((p=strrchr(infilename, '/')) != NULL)
202 strlcpy(confname, p + 1, sizeof(confname));
204 strlcpy(confname, infilename, sizeof(confname));
206 if ((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf"))
210 snprintf(outmkname, sizeof(outmkname), "%s.mk", confname);
212 snprintf(outcfname, sizeof(outcfname), "%s.c", confname);
214 snprintf(execfname, sizeof(execfname), "%s", confname);
216 snprintf(cachename, sizeof(cachename), "%s.cache", confname);
217 snprintf(tempfname, sizeof(tempfname), "%s/crunchgen_%sXXXXXX",
218 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP, confname);
233 fprintf(stderr, "%s%s\n\t%s%s\n", "usage: crunchgen [-foq] ",
234 "[-h <makefile-header-name>] [-m <makefile>]",
235 "[-p <obj-prefix>] [-c <c-file-name>] [-e <exec-file>] ",
242 * ========================================================================
243 * parse_conf_file subsystem
247 /* helper routines for parse_conf_file */
249 void parse_one_file(char *filename);
250 void parse_line(char *pline, int *fc, char **fv, int nf);
251 void add_srcdirs(int argc, char **argv);
252 void add_progs(int argc, char **argv);
253 void add_link(int argc, char **argv);
254 void add_libs(int argc, char **argv);
255 void add_libs_so(int argc, char **argv);
256 void add_buildopts(int argc, char **argv);
257 void add_special(int argc, char **argv);
259 prog_t *find_prog(char *str);
260 void add_prog(char *progname);
264 parse_conf_file(void)
266 if (!is_nonempty_file(infilename))
267 errx(1, "fatal: input file \"%s\" not found", infilename);
269 parse_one_file(infilename);
270 if (readcache && is_nonempty_file(cachename)) {
272 parse_one_file(cachename);
278 parse_one_file(char *filename)
280 char *fieldv[MAXFIELDS];
282 void (*f)(int c, char **v);
284 char line[MAXLINELEN];
286 snprintf(line, sizeof(line), "reading %s", filename);
288 strlcpy(curfilename, filename, sizeof(curfilename));
290 if ((cf = fopen(curfilename, "r")) == NULL) {
291 warn("%s", curfilename);
297 while (fgets(line, MAXLINELEN, cf) != NULL) {
299 parse_line(line, &fieldc, fieldv, MAXFIELDS);
304 if (!strcmp(fieldv[0], "srcdirs"))
306 else if(!strcmp(fieldv[0], "progs"))
308 else if(!strcmp(fieldv[0], "ln"))
310 else if(!strcmp(fieldv[0], "libs"))
312 else if(!strcmp(fieldv[0], "libs_so"))
314 else if(!strcmp(fieldv[0], "buildopts"))
316 else if(!strcmp(fieldv[0], "special"))
319 warnx("%s:%d: skipping unknown command `%s'",
320 curfilename, linenum, fieldv[0]);
326 warnx("%s:%d: %s %s",
327 curfilename, linenum, fieldv[0],
328 "command needs at least 1 argument, skipping");
337 warn("%s", curfilename);
345 parse_line(char *pline, int *fc, char **fv, int nf)
353 while (isspace((unsigned char)*p))
356 if (*p == '\0' || *p == '#')
362 while (*p && !isspace((unsigned char)*p) && *p != '#')
365 if (*p == '\0' || *p == '#')
372 *p = '\0'; /* needed for '#' case */
377 add_srcdirs(int argc, char **argv)
381 for (i = 1; i < argc; i++) {
383 add_string(&srcdirs, argv[i]);
385 warnx("%s:%d: `%s' is not a directory, skipping it",
386 curfilename, linenum, argv[i]);
394 add_progs(int argc, char **argv)
398 for (i = 1; i < argc; i++)
404 add_prog(char *progname)
408 /* add to end, but be smart about dups */
410 for (p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next)
411 if (!strcmp(p2->name, progname))
414 p2 = malloc(sizeof(prog_t));
416 memset(p2, 0, sizeof(prog_t));
417 p2->name = strdup(progname);
419 if (!p2 || !p2->name)
430 p2->realsrcdir = NULL;
437 p2->buildopts = NULL;
441 printf("%s\n",progname);
446 add_link(int argc, char **argv)
449 prog_t *p = find_prog(argv[1]);
452 warnx("%s:%d: no prog %s previously declared, skipping link",
453 curfilename, linenum, argv[1]);
458 for (i = 2; i < argc; i++) {
460 printf("%s\n",argv[i]);
462 add_string(&p->links, argv[i]);
468 add_libs(int argc, char **argv)
472 for(i = 1; i < argc; i++) {
473 add_string(&libs, argv[i]);
474 if ( in_list(&libs_so, argv[i]) )
476 "library `%s' specified as dynamic earlier",
477 curfilename, linenum, argv[i]);
483 add_libs_so(int argc, char **argv)
487 for(i = 1; i < argc; i++) {
488 add_string(&libs_so, argv[i]);
489 if ( in_list(&libs, argv[i]) )
491 "library `%s' specified as static earlier",
492 curfilename, linenum, argv[i]);
498 add_buildopts(int argc, char **argv)
502 for (i = 1; i < argc; i++)
503 add_string(&buildopts, argv[i]);
508 add_special(int argc, char **argv)
511 prog_t *p = find_prog(argv[1]);
517 warnx("%s:%d: no prog %s previously declared, skipping special",
518 curfilename, linenum, argv[1]);
523 if (!strcmp(argv[2], "ident")) {
526 if ((p->ident = strdup(argv[3])) == NULL)
528 } else if (!strcmp(argv[2], "srcdir")) {
531 if ((p->srcdir = strdup(argv[3])) == NULL)
533 } else if (!strcmp(argv[2], "objdir")) {
536 if((p->objdir = strdup(argv[3])) == NULL)
538 } else if (!strcmp(argv[2], "objs")) {
540 for (i = 3; i < argc; i++)
541 add_string(&p->objs, argv[i]);
542 } else if (!strcmp(argv[2], "objpaths")) {
544 for (i = 3; i < argc; i++)
545 add_string(&p->objpaths, argv[i]);
546 } else if (!strcmp(argv[2], "keep")) {
548 for(i = 3; i < argc; i++)
549 add_string(&p->keeplist, argv[i]);
550 } else if (!strcmp(argv[2], "objvar")) {
553 if ((p->objvar = strdup(argv[3])) == NULL)
555 } else if (!strcmp(argv[2], "buildopts")) {
557 for (i = 3; i < argc; i++)
558 add_string(&p->buildopts, argv[i]);
559 } else if (!strcmp(argv[2], "lib")) {
560 for (i = 3; i < argc; i++)
561 add_string(&p->libs, argv[i]);
563 warnx("%s:%d: bad parameter name `%s', skipping line",
564 curfilename, linenum, argv[2]);
570 warnx("%s:%d: too %s arguments, expected \"special %s %s <string>\"",
571 curfilename, linenum, argc < 4? "few" : "many", argv[1], argv[2]);
576 prog_t *find_prog(char *str)
580 for (p = progs; p != NULL; p = p->next)
581 if (!strcmp(p->name, str))
589 * ========================================================================
590 * gen_outputs subsystem
594 /* helper subroutines */
596 void remove_error_progs(void);
597 void fillin_program(prog_t *p);
598 void gen_specials_cache(void);
599 void gen_output_makefile(void);
600 void gen_output_cfile(void);
602 void fillin_program_objs(prog_t *p, char *path);
603 void top_makefile_rules(FILE *outmk);
604 void prog_makefile_rules(FILE *outmk, prog_t *p);
605 void output_strlst(FILE *outf, strlst_t *lst);
606 char *genident(char *str);
607 char *dir_search(char *progname);
615 for (p = progs; p != NULL; p = p->next)
618 remove_error_progs();
619 gen_specials_cache();
621 gen_output_makefile();
624 "Run \"%s -f %s\" to build crunched binary.\n",
625 path_make, outmkname);
629 * run the makefile for the program to find which objects are necessary
632 fillin_program(prog_t *p)
634 char path[MAXPATHLEN];
635 char line[MAXLINELEN];
638 snprintf(line, MAXLINELEN, "filling in parms for %s", p->name);
642 p->ident = genident(p->name);
644 /* look for the source directory if one wasn't specified by a special */
646 p->srcdir = dir_search(p->name);
649 /* Determine the actual srcdir (maybe symlinked). */
651 snprintf(line, MAXLINELEN, "cd %s && echo -n `/bin/pwd`",
655 errx(1, "Can't execute: %s\n", line);
658 fgets(path, sizeof path, f);
660 errx(1, "Can't execute: %s\n", line);
663 errx(1, "Can't perform pwd on: %s\n", p->srcdir);
665 p->realsrcdir = strdup(path);
668 /* Unless the option to make object files was specified the
669 * the objects will be built in the source directory unless
670 * an object directory already exists.
672 if (!makeobj && !p->objdir && p->srcdir) {
673 snprintf(line, sizeof line, "%s/%s", objprefix, p->realsrcdir);
675 if ((p->objdir = strdup(line)) == NULL)
678 p->objdir = p->realsrcdir;
682 * XXX look for a Makefile.{name} in local directory first.
683 * This lets us override the original Makefile.
685 snprintf(path, sizeof(path), "Makefile.%s", p->name);
686 if (is_nonempty_file(path)) {
687 snprintf(line, MAXLINELEN, "Using %s for %s", path, p->name);
691 snprintf(path, sizeof(path), "%s/Makefile", p->srcdir);
692 if (!p->objs && p->srcdir && is_nonempty_file(path))
693 fillin_program_objs(p, path);
695 if (!p->srcdir && !p->objdir && verbose)
697 "warning: could not find source directory",
698 infilename, p->name);
699 if (!p->objs && verbose)
700 warnx("%s: %s: warning: could not find any .o files",
701 infilename, p->name);
703 if ((!p->srcdir || !p->objdir) && !p->objs)
708 fillin_program_objs(prog_t *p, char *path)
715 char line[MAXLINELEN];
717 /* discover the objs from the srcdir Makefile */
719 if ((fd = mkstemp(tempfname)) == -1) {
723 if ((f = fdopen(fd, "w")) == NULL) {
724 warn("%s", tempfname);
732 * XXX include outhdrname (e.g. to contain Make variables)
734 if (outhdrname[0] != '\0')
735 fprintf(f, ".include \"%s\"\n", outhdrname);
736 fprintf(f, ".include \"%s\"\n", path);
737 fprintf(f, ".POSIX:\n");
739 fprintf(f, "BUILDOPTS+=");
740 output_strlst(f, buildopts);
742 fprintf(f, ".if defined(PROG)\n");
743 fprintf(f, "%s?=${PROG}.o\n", objvar);
744 fprintf(f, ".endif\n");
745 fprintf(f, "loop:\n\t@echo 'OBJS= '${%s}\n", objvar);
747 fprintf(f, "crunchgen_objs:\n"
748 "\t@cd %s && %s -f %s $(BUILDOPTS) $(%s_OPTS)",
749 p->srcdir, path_make, tempfname, p->ident);
750 for (s = p->buildopts; s != NULL; s = s->next)
751 fprintf(f, " %s", s->str);
752 fprintf(f, " loop\n");
756 snprintf(line, MAXLINELEN, "cd %s && %s -f %s -B crunchgen_objs",
757 p->srcdir, path_make, tempfname);
758 if ((f = popen(line, "r")) == NULL) {
759 warn("submake pipe");
764 while(fgets(line, MAXLINELEN, f)) {
765 if (strncmp(line, "OBJS= ", 6)) {
766 warnx("make error: %s", line);
772 while (isspace((unsigned char)*cp))
777 while (*cp && !isspace((unsigned char)*cp))
781 add_string(&p->objs, obj);
782 while (isspace((unsigned char)*cp))
787 if ((rc=pclose(f)) != 0) {
788 warnx("make error: make returned %d", rc);
796 remove_error_progs(void)
800 p1 = NULL; p2 = progs;
803 p1 = p2, p2 = p2->next;
805 /* delete it from linked list */
806 warnx("%s: %s: ignoring program because of errors",
807 infilename, p2->name);
818 gen_specials_cache(void)
822 char line[MAXLINELEN];
824 snprintf(line, MAXLINELEN, "generating %s", cachename);
827 if ((cachef = fopen(cachename, "w")) == NULL) {
828 warn("%s", cachename);
833 fprintf(cachef, "# %s - parm cache generated from %s by crunchgen "
835 cachename, infilename, CRUNCH_VERSION);
837 for (p = progs; p != NULL; p = p->next) {
838 fprintf(cachef, "\n");
840 fprintf(cachef, "special %s srcdir %s\n",
843 fprintf(cachef, "special %s objdir %s\n",
846 fprintf(cachef, "special %s objs", p->name);
847 output_strlst(cachef, p->objs);
850 fprintf(cachef, "special %s objpaths", p->name);
851 output_strlst(cachef, p->objpaths);
859 gen_output_makefile(void)
863 char line[MAXLINELEN];
865 snprintf(line, MAXLINELEN, "generating %s", outmkname);
868 if ((outmk = fopen(outmkname, "w")) == NULL) {
869 warn("%s", outmkname);
874 fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n",
875 outmkname, infilename, CRUNCH_VERSION);
877 if (outhdrname[0] != '\0')
878 fprintf(outmk, ".include \"%s\"\n", outhdrname);
880 top_makefile_rules(outmk);
881 for (p = progs; p != NULL; p = p->next)
882 prog_makefile_rules(outmk, p);
884 fprintf(outmk, "\n# ========\n");
890 gen_output_cfile(void)
896 char line[MAXLINELEN];
898 snprintf(line, MAXLINELEN, "generating %s", outcfname);
901 if((outcf = fopen(outcfname, "w")) == NULL) {
902 warn("%s", outcfname);
908 "/* %s - generated from %s by crunchgen %s */\n",
909 outcfname, infilename, CRUNCH_VERSION);
911 fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname);
912 for (cp = crunched_skel; *cp != NULL; cp++)
913 fprintf(outcf, "%s\n", *cp);
915 for (p = progs; p != NULL; p = p->next)
916 fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident);
918 fprintf(outcf, "\nstruct stub entry_points[] = {\n");
919 for (p = progs; p != NULL; p = p->next) {
920 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
922 for (s = p->links; s != NULL; s = s->next)
923 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
927 fprintf(outcf, "\t{ EXECNAME, crunched_main },\n");
928 fprintf(outcf, "\t{ NULL, NULL }\n};\n");
933 char *genident(char *str)
938 * generates a Makefile/C identifier from a program name,
939 * mapping '-' to '_' and ignoring all other non-identifier
940 * characters. This leads to programs named "foo.bar" and
941 * "foobar" to map to the same identifier.
944 if ((n = strdup(str)) == NULL)
946 for (d = s = n; *s != '\0'; s++) {
949 else if (*s == '_' || isalnum((unsigned char)*s))
957 char *dir_search(char *progname)
959 char path[MAXPATHLEN];
963 for (dir = srcdirs; dir != NULL; dir = dir->next) {
964 snprintf(path, MAXPATHLEN, "%s/%s", dir->str, progname);
968 if ((srcdir = strdup(path)) == NULL)
978 top_makefile_rules(FILE *outmk)
982 if ( subtract_strlst(&libs, &libs_so) )
983 fprintf(outmk, "# NOTE: Some LIBS declarations below overridden by LIBS_SO\n");
985 fprintf(outmk, "LIBS+=");
986 output_strlst(outmk, libs);
988 fprintf(outmk, "LIBS_SO+=");
989 output_strlst(outmk, libs_so);
992 fprintf(outmk, "MAKEOBJDIRPREFIX?=%s\n", objprefix);
993 fprintf(outmk, "MAKEENV=env MAKEOBJDIRPREFIX=$(MAKEOBJDIRPREFIX)\n");
994 fprintf(outmk, "CRUNCHMAKE=$(MAKEENV) $(MAKE)\n");
996 fprintf(outmk, "CRUNCHMAKE=$(MAKE)\n");
1000 fprintf(outmk, "BUILDOPTS+=");
1001 output_strlst(outmk, buildopts);
1004 fprintf(outmk, "CRUNCHED_OBJS=");
1005 for (p = progs; p != NULL; p = p->next)
1006 fprintf(outmk, " %s.lo", p->name);
1007 fprintf(outmk, "\n");
1009 fprintf(outmk, "SUBMAKE_TARGETS=");
1010 for (p = progs; p != NULL; p = p->next)
1011 fprintf(outmk, " %s_make", p->ident);
1012 fprintf(outmk, "\nSUBCLEAN_TARGETS=");
1013 for (p = progs; p != NULL; p = p->next)
1014 fprintf(outmk, " %s_clean", p->ident);
1015 fprintf(outmk, "\n\n");
1017 fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n");
1018 fprintf(outmk, "exe: %s\n", execfname);
1019 fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS) $(SUBMAKE_TARGETS)\n", execfname, execfname);
1020 fprintf(outmk, ".if defined(LIBS_SO) && !empty(LIBS_SO)\n");
1021 fprintf(outmk, "\t$(CC) -o %s %s.o $(CRUNCHED_OBJS) \\\n",
1022 execfname, execfname);
1023 fprintf(outmk, "\t\t-Xlinker -Bstatic $(LIBS) \\\n");
1024 fprintf(outmk, "\t\t-Xlinker -Bdynamic $(LIBS_SO)\n");
1025 fprintf(outmk, ".else\n");
1026 fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n",
1027 execfname, execfname);
1028 fprintf(outmk, ".endif\n");
1029 fprintf(outmk, "\tstrip %s\n", execfname);
1030 fprintf(outmk, "realclean: clean subclean\n");
1031 fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", execfname);
1032 fprintf(outmk, "subclean: $(SUBCLEAN_TARGETS)\n");
1037 prog_makefile_rules(FILE *outmk, prog_t *p)
1041 fprintf(outmk, "\n# -------- %s\n\n", p->name);
1043 fprintf(outmk, "%s_OBJDIR=", p->ident);
1045 fprintf(outmk, "%s", p->objdir);
1047 fprintf(outmk, "$(MAKEOBJDIRPREFIX)/$(%s_REALSRCDIR)\n",
1049 fprintf(outmk, "\n");
1051 fprintf(outmk, "%s_OBJPATHS=", p->ident);
1053 output_strlst(outmk, p->objpaths);
1055 for (lst = p->objs; lst != NULL; lst = lst->next) {
1056 fprintf(outmk, " $(%s_OBJDIR)/%s", p->ident, lst->str);
1058 fprintf(outmk, "\n");
1061 if (p->srcdir && p->objs) {
1062 fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir);
1063 fprintf(outmk, "%s_REALSRCDIR=%s\n", p->ident, p->realsrcdir);
1065 fprintf(outmk, "%s_OBJS=", p->ident);
1066 output_strlst(outmk, p->objs);
1067 if (p->buildopts != NULL) {
1068 fprintf(outmk, "%s_OPTS+=", p->ident);
1069 output_strlst(outmk, p->buildopts);
1072 fprintf(outmk, "$(%s_OBJPATHS): %s_make\n\n", p->ident, p->ident);
1074 fprintf(outmk, "%s_make:\n", p->ident);
1075 fprintf(outmk, "\t(cd $(%s_SRCDIR) && ", p->ident);
1077 fprintf(outmk, "$(CRUNCHMAKE) obj && ");
1078 fprintf(outmk, "\\\n");
1079 fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) depend &&",
1081 fprintf(outmk, "\\\n");
1082 fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) "
1084 p->ident, p->ident);
1085 fprintf(outmk, "\n");
1086 fprintf(outmk, "%s_clean:\n", p->ident);
1087 fprintf(outmk, "\t(cd $(%s_SRCDIR) && $(CRUNCHMAKE) $(BUILDOPTS) clean cleandepend)\n\n",
1090 fprintf(outmk, "%s_make:\n", p->ident);
1091 fprintf(outmk, "\t@echo \"** cannot make objs for %s\"\n\n",
1096 fprintf(outmk, "%s_LIBS=", p->ident);
1097 output_strlst(outmk, p->libs);
1100 fprintf(outmk, "%s_stub.c:\n", p->name);
1101 fprintf(outmk, "\techo \""
1102 "int _crunched_%s_stub(int argc, char **argv, char **envp)"
1103 "{return main(argc,argv,envp);}\" >%s_stub.c\n",
1105 fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)",
1106 p->name, p->name, p->ident);
1108 fprintf(outmk, " $(%s_LIBS)", p->ident);
1110 fprintf(outmk, "\n");
1111 fprintf(outmk, "\tld -dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)",
1112 p->name, p->name, p->ident);
1114 fprintf(outmk, " $(%s_LIBS)", p->ident);
1115 fprintf(outmk, "\n");
1116 fprintf(outmk, "\tcrunchide -k _crunched_%s_stub ", p->ident);
1117 for (lst = p->keeplist; lst != NULL; lst = lst->next)
1118 fprintf(outmk, "-k _%s ", lst->str);
1119 fprintf(outmk, "%s.lo\n", p->name);
1123 output_strlst(FILE *outf, strlst_t *lst)
1125 for (; lst != NULL; lst = lst->next)
1126 if ( strlen(lst->str) )
1127 fprintf(outf, " %s", lst->str);
1128 fprintf(outf, "\n");
1133 * ========================================================================
1134 * general library routines
1139 status(const char *str)
1141 static int lastlen = 0;
1148 spaces = lastlen - len;
1152 fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
1161 err(1, "%s: %d: out of memory, stopping", infilename, linenum);
1166 add_string(strlst_t **listp, char *str)
1170 /* add to end, but be smart about dups */
1172 for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
1173 if (!strcmp(p2->str, str))
1176 p2 = malloc(sizeof(strlst_t));
1179 p2->str = strdup(str);
1181 if (!p2 || !p2->str)
1191 subtract_strlst(strlst_t **lista, strlst_t **listb)
1193 int subtract_count = 0;
1195 for (p1 = *listb; p1 != NULL; p1 = p1->next)
1196 if ( in_list(lista, p1->str) ) {
1197 warnx("Will compile library `%s' dynamically", p1->str);
1198 strcat(p1->str, "");
1201 return subtract_count;
1205 in_list(strlst_t **listp, char *str)
1208 for (p1 = *listp; p1 != NULL; p1 = p1->next)
1209 if (!strcmp(p1->str, str))
1215 is_dir(const char *pathname)
1219 if (stat(pathname, &buf) == -1)
1222 return S_ISDIR(buf.st_mode);
1226 is_nonempty_file(const char *pathname)
1230 if (stat(pathname, &buf) == -1)
1233 return S_ISREG(buf.st_mode) && buf.st_size > 0;