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
29 * ========================================================================
32 * Generates a Makefile and main C file for a crunched executable,
33 * from specs given in a .conf file.
35 #include <sys/types.h>
37 #include <sys/param.h>
47 #define CRUNCH_VERSION "0.2"
49 #define MAXLINELEN 16384
50 #define MAXFIELDS 2048
53 /* internal representation of conf file: */
55 /* simple lists of strings suffice for most parms */
57 typedef struct strlst {
62 /* progs have structure, each field can be set with "special" or calculated */
65 struct prog *next; /* link field */
66 char *name; /* program name */
67 char *ident; /* C identifier for the program name */
71 char *objvar; /* Makefile variable to replace OBJS */
72 strlst_t *objs, *objpaths;
84 strlst_t *buildopts = NULL;
85 strlst_t *srcdirs = NULL;
86 strlst_t *libs = NULL;
87 strlst_t *libs_so = NULL;
90 char confname[MAXPATHLEN], infilename[MAXPATHLEN];
91 char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN];
92 char tempfname[MAXPATHLEN], cachename[MAXPATHLEN], curfilename[MAXPATHLEN];
93 char outhdrname[MAXPATHLEN] ; /* user-supplied header for *.mk */
94 char *objprefix; /* where are the objects ? */
98 int verbose, readcache; /* options */
100 int makeobj = 0; /* add 'make obj' rules to the makefile */
104 /* general library routines */
106 void status(char *str);
107 void out_of_memory(void);
108 void add_string(strlst_t **listp, char *str);
109 int is_dir(char *pathname);
110 int is_nonempty_file(char *pathname);
111 int subtract_strlst(strlst_t **lista, strlst_t **listb);
112 int in_list(strlst_t **listp, char *str);
114 /* helper routines for main() */
117 void parse_conf_file(void);
118 void gen_outputs(void);
121 int main(int argc, char **argv)
128 *outmkname = *outcfname = *execfname = '\0';
130 p = getenv("MAKEOBJDIRPREFIX");
131 if (p == NULL || *p == '\0')
132 objprefix = "/usr/obj"; /* default */
134 if ((objprefix = strdup(p)) == NULL)
137 while((optc = getopt(argc, argv, "lh:m:c:e:p:foq")) != -1) {
150 strlcpy(outmkname, optarg, sizeof(outmkname));
153 if ((objprefix = strdup(optarg)) == NULL)
158 strlcpy(outhdrname, optarg, sizeof(outhdrname));
161 strlcpy(outcfname, optarg, sizeof(outcfname));
164 strlcpy(execfname, optarg, sizeof(execfname));
188 strlcpy(infilename, argv[0], sizeof(infilename));
190 /* confname = `basename infilename .conf` */
192 if ((p=strrchr(infilename, '/')) != NULL)
193 strlcpy(confname, p + 1, sizeof(confname));
195 strlcpy(confname, infilename, sizeof(confname));
197 if ((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf"))
201 snprintf(outmkname, sizeof(outmkname), "%s.mk", confname);
203 snprintf(outcfname, sizeof(outcfname), "%s.c", confname);
205 snprintf(execfname, sizeof(execfname), "%s", confname);
207 snprintf(cachename, sizeof(cachename), "%s.cache", confname);
208 snprintf(tempfname, sizeof(tempfname), "%s/crunchgen_%sXXXXXX",
209 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP, confname);
223 fprintf(stderr, "%s%s\n\t%s%s\n", "usage: crunchgen [-foq] ",
224 "[-h <makefile-header-name>] [-m <makefile>]",
225 "[-p <obj-prefix>] [-c <c-file-name>] [-e <exec-file>] ",
232 * ========================================================================
233 * parse_conf_file subsystem
237 /* helper routines for parse_conf_file */
239 void parse_one_file(char *filename);
240 void parse_line(char *line, int *fc, char **fv, int nf);
241 void add_srcdirs(int argc, char **argv);
242 void add_progs(int argc, char **argv);
243 void add_link(int argc, char **argv);
244 void add_libs(int argc, char **argv);
245 void add_libs_so(int argc, char **argv);
246 void add_buildopts(int argc, char **argv);
247 void add_special(int argc, char **argv);
249 prog_t *find_prog(char *str);
250 void add_prog(char *progname);
253 void parse_conf_file(void)
255 if (!is_nonempty_file(infilename))
256 errx(1, "fatal: input file \"%s\" not found", infilename);
258 parse_one_file(infilename);
259 if (readcache && is_nonempty_file(cachename)) {
261 parse_one_file(cachename);
266 void parse_one_file(char *filename)
268 char *fieldv[MAXFIELDS];
270 void (*f)(int c, char **v);
272 char line[MAXLINELEN];
274 snprintf(line, sizeof(line), "reading %s", filename);
276 strlcpy(curfilename, filename, sizeof(curfilename));
278 if ((cf = fopen(curfilename, "r")) == NULL) {
279 warn("%s", curfilename);
285 while (fgets(line, MAXLINELEN, cf) != NULL) {
287 parse_line(line, &fieldc, fieldv, MAXFIELDS);
292 if (!strcmp(fieldv[0], "srcdirs"))
294 else if(!strcmp(fieldv[0], "progs"))
296 else if(!strcmp(fieldv[0], "ln"))
298 else if(!strcmp(fieldv[0], "libs"))
300 else if(!strcmp(fieldv[0], "libs_so"))
302 else if(!strcmp(fieldv[0], "buildopts"))
304 else if(!strcmp(fieldv[0], "special"))
307 warnx("%s:%d: skipping unknown command `%s'",
308 curfilename, linenum, fieldv[0]);
314 warnx("%s:%d: %s %s",
315 curfilename, linenum, fieldv[0],
316 "command needs at least 1 argument, skipping");
325 warn("%s", curfilename);
332 void parse_line(char *line, int *fc, char **fv, int nf)
343 if (*p == '\0' || *p == '#')
349 while (*p && !isspace(*p) && *p != '#')
352 if (*p == '\0' || *p == '#')
359 *p = '\0'; /* needed for '#' case */
363 void add_srcdirs(int argc, char **argv)
367 for (i = 1; i < argc; i++) {
369 add_string(&srcdirs, argv[i]);
371 warnx("%s:%d: `%s' is not a directory, skipping it",
372 curfilename, linenum, argv[i]);
379 void add_progs(int argc, char **argv)
383 for (i = 1; i < argc; i++)
388 void add_prog(char *progname)
392 /* add to end, but be smart about dups */
394 for (p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next)
395 if (!strcmp(p2->name, progname))
398 p2 = malloc(sizeof(prog_t));
400 memset(p2, 0, sizeof(prog_t));
401 p2->name = strdup(progname);
403 if (!p2 || !p2->name)
414 p2->realsrcdir = NULL;
421 p2->buildopts = NULL;
425 printf("%s\n",progname);
429 void add_link(int argc, char **argv)
432 prog_t *p = find_prog(argv[1]);
435 warnx("%s:%d: no prog %s previously declared, skipping link",
436 curfilename, linenum, argv[1]);
441 for (i = 2; i < argc; i++) {
443 printf("%s\n",argv[i]);
445 add_string(&p->links, argv[i]);
450 void add_libs(int argc, char **argv)
454 for(i = 1; i < argc; i++) {
455 add_string(&libs, argv[i]);
456 if ( in_list(&libs_so, argv[i]) )
458 "library `%s' specified as dynamic earlier",
459 curfilename, linenum, argv[i]);
464 void add_libs_so(int argc, char **argv)
468 for(i = 1; i < argc; i++) {
469 add_string(&libs_so, argv[i]);
470 if ( in_list(&libs, argv[i]) )
472 "library `%s' specified as static earlier",
473 curfilename, linenum, argv[i]);
478 void add_buildopts(int argc, char **argv)
482 for (i = 1; i < argc; i++)
483 add_string(&buildopts, argv[i]);
487 void add_special(int argc, char **argv)
490 prog_t *p = find_prog(argv[1]);
496 warnx("%s:%d: no prog %s previously declared, skipping special",
497 curfilename, linenum, argv[1]);
502 if (!strcmp(argv[2], "ident")) {
505 if ((p->ident = strdup(argv[3])) == NULL)
507 } else if (!strcmp(argv[2], "srcdir")) {
510 if ((p->srcdir = strdup(argv[3])) == NULL)
512 } else if (!strcmp(argv[2], "objdir")) {
515 if((p->objdir = strdup(argv[3])) == NULL)
517 } else if (!strcmp(argv[2], "objs")) {
519 for (i = 3; i < argc; i++)
520 add_string(&p->objs, argv[i]);
521 } else if (!strcmp(argv[2], "objpaths")) {
523 for (i = 3; i < argc; i++)
524 add_string(&p->objpaths, argv[i]);
525 } else if (!strcmp(argv[2], "keep")) {
527 for(i = 3; i < argc; i++)
528 add_string(&p->keeplist, argv[i]);
529 } else if (!strcmp(argv[2], "objvar")) {
532 if ((p->objvar = strdup(argv[3])) == NULL)
534 } else if (!strcmp(argv[2], "buildopts")) {
536 for (i = 3; i < argc; i++)
537 add_string(&p->buildopts, argv[i]);
538 } else if (!strcmp(argv[2], "lib")) {
539 for (i = 3; i < argc; i++)
540 add_string(&p->libs, argv[i]);
542 warnx("%s:%d: bad parameter name `%s', skipping line",
543 curfilename, linenum, argv[2]);
549 warnx("%s:%d: too %s arguments, expected \"special %s %s <string>\"",
550 curfilename, linenum, argc < 4? "few" : "many", argv[1], argv[2]);
555 prog_t *find_prog(char *str)
559 for (p = progs; p != NULL; p = p->next)
560 if (!strcmp(p->name, str))
568 * ========================================================================
569 * gen_outputs subsystem
573 /* helper subroutines */
575 void remove_error_progs(void);
576 void fillin_program(prog_t *p);
577 void gen_specials_cache(void);
578 void gen_output_makefile(void);
579 void gen_output_cfile(void);
581 void fillin_program_objs(prog_t *p, char *path);
582 void top_makefile_rules(FILE *outmk);
583 void prog_makefile_rules(FILE *outmk, prog_t *p);
584 void output_strlst(FILE *outf, strlst_t *lst);
585 char *genident(char *str);
586 char *dir_search(char *progname);
589 void gen_outputs(void)
593 for (p = progs; p != NULL; p = p->next)
596 remove_error_progs();
597 gen_specials_cache();
599 gen_output_makefile();
602 "Run \"make -f %s\" to build crunched binary.\n", outmkname);
606 * run the makefile for the program to find which objects are necessary
608 void fillin_program(prog_t *p)
610 char path[MAXPATHLEN];
611 char line[MAXLINELEN];
614 snprintf(line, MAXLINELEN, "filling in parms for %s", p->name);
618 p->ident = genident(p->name);
620 /* look for the source directory if one wasn't specified by a special */
622 p->srcdir = dir_search(p->name);
625 /* Determine the actual srcdir (maybe symlinked). */
627 snprintf(line, MAXLINELEN, "cd %s && echo -n `/bin/pwd`",
631 errx(1, "Can't execute: %s\n", line);
634 fgets(path, sizeof path, f);
636 errx(1, "Can't execute: %s\n", line);
639 errx(1, "Can't perform pwd on: %s\n", p->srcdir);
641 p->realsrcdir = strdup(path);
644 /* Unless the option to make object files was specified the
645 * the objects will be built in the source directory unless
646 * an object directory already exists.
648 if (!makeobj && !p->objdir && p->srcdir) {
649 snprintf(line, sizeof line, "%s/%s", objprefix, p->realsrcdir);
651 if ((p->objdir = strdup(line)) == NULL)
654 p->objdir = p->realsrcdir;
658 * XXX look for a Makefile.{name} in local directory first.
659 * This lets us override the original Makefile.
661 snprintf(path, sizeof(path), "Makefile.%s", p->name);
662 if (is_nonempty_file(path)) {
663 snprintf(line, MAXLINELEN, "Using %s for %s", path, p->name);
667 snprintf(path, sizeof(path), "%s/Makefile", p->srcdir);
668 if (!p->objs && p->srcdir && is_nonempty_file(path))
669 fillin_program_objs(p, path);
671 if (!p->srcdir && !p->objdir && verbose)
673 "warning: could not find source directory",
674 infilename, p->name);
675 if (!p->objs && verbose)
676 warnx("%s: %s: warning: could not find any .o files",
677 infilename, p->name);
679 if ((!p->srcdir || !p->objdir) && !p->objs)
683 void fillin_program_objs(prog_t *p, char *path)
690 char line[MAXLINELEN];
692 /* discover the objs from the srcdir Makefile */
694 if ((fd = mkstemp(tempfname)) == -1) {
698 if ((f = fdopen(fd, "w")) == NULL) {
699 warn("%s", tempfname);
707 * XXX include outhdrname (e.g. to contain Make variables)
709 if (outhdrname[0] != '\0')
710 fprintf(f, ".include \"%s\"\n", outhdrname);
711 fprintf(f, ".include \"%s\"\n", path);
712 fprintf(f, ".POSIX:\n");
714 fprintf(f, "BUILDOPTS+=");
715 output_strlst(f, buildopts);
717 fprintf(f, ".if defined(PROG)\n");
718 fprintf(f, "%s?=${PROG}.o\n", objvar);
719 fprintf(f, ".endif\n");
720 fprintf(f, "loop:\n\t@echo 'OBJS= '${%s}\n", objvar);
722 fprintf(f, "crunchgen_objs:\n"
723 "\t@cd %s && make -f %s $(BUILDOPTS) $(%s_OPTS)",
724 p->srcdir, tempfname, p->ident);
725 for (s = p->buildopts; s != NULL; s = s->next)
726 fprintf(f, " %s", s->str);
727 fprintf(f, " loop\n");
731 snprintf(line, MAXLINELEN, "cd %s && make -f %s -B crunchgen_objs",
732 p->srcdir, tempfname);
733 if ((f = popen(line, "r")) == NULL) {
734 warn("submake pipe");
739 while(fgets(line, MAXLINELEN, f)) {
740 if (strncmp(line, "OBJS= ", 6)) {
741 warnx("make error: %s", line);
752 while (*cp && !isspace(*cp))
756 add_string(&p->objs, obj);
762 if ((rc=pclose(f)) != 0) {
763 warnx("make error: make returned %d", rc);
770 void remove_error_progs(void)
774 p1 = NULL; p2 = progs;
777 p1 = p2, p2 = p2->next;
779 /* delete it from linked list */
780 warnx("%s: %s: ignoring program because of errors",
781 infilename, p2->name);
791 void gen_specials_cache(void)
795 char line[MAXLINELEN];
797 snprintf(line, MAXLINELEN, "generating %s", cachename);
800 if ((cachef = fopen(cachename, "w")) == NULL) {
801 warn("%s", cachename);
806 fprintf(cachef, "# %s - parm cache generated from %s by crunchgen "
808 cachename, infilename, CRUNCH_VERSION);
810 for (p = progs; p != NULL; p = p->next) {
811 fprintf(cachef, "\n");
813 fprintf(cachef, "special %s srcdir %s\n",
816 fprintf(cachef, "special %s objdir %s\n",
819 fprintf(cachef, "special %s objs", p->name);
820 output_strlst(cachef, p->objs);
823 fprintf(cachef, "special %s objpaths", p->name);
824 output_strlst(cachef, p->objpaths);
831 void gen_output_makefile(void)
835 char line[MAXLINELEN];
837 snprintf(line, MAXLINELEN, "generating %s", outmkname);
840 if ((outmk = fopen(outmkname, "w")) == NULL) {
841 warn("%s", outmkname);
846 fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n",
847 outmkname, infilename, CRUNCH_VERSION);
849 if (outhdrname[0] != '\0')
850 fprintf(outmk, ".include \"%s\"\n", outhdrname);
852 top_makefile_rules(outmk);
853 for (p = progs; p != NULL; p = p->next)
854 prog_makefile_rules(outmk, p);
856 fprintf(outmk, "\n# ========\n");
861 void gen_output_cfile(void)
863 extern char *crunched_skel[];
868 char line[MAXLINELEN];
870 snprintf(line, MAXLINELEN, "generating %s", outcfname);
873 if((outcf = fopen(outcfname, "w")) == NULL) {
874 warn("%s", outcfname);
880 "/* %s - generated from %s by crunchgen %s */\n",
881 outcfname, infilename, CRUNCH_VERSION);
883 fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname);
884 for (cp = crunched_skel; *cp != NULL; cp++)
885 fprintf(outcf, "%s\n", *cp);
887 for (p = progs; p != NULL; p = p->next)
888 fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident);
890 fprintf(outcf, "\nstruct stub entry_points[] = {\n");
891 for (p = progs; p != NULL; p = p->next) {
892 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
894 for (s = p->links; s != NULL; s = s->next)
895 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
899 fprintf(outcf, "\t{ EXECNAME, crunched_main },\n");
900 fprintf(outcf, "\t{ NULL, NULL }\n};\n");
905 char *genident(char *str)
910 * generates a Makefile/C identifier from a program name,
911 * mapping '-' to '_' and ignoring all other non-identifier
912 * characters. This leads to programs named "foo.bar" and
913 * "foobar" to map to the same identifier.
916 if ((n = strdup(str)) == NULL)
918 for (d = s = n; *s != '\0'; s++) {
921 else if (*s == '_' || isalnum(*s))
929 char *dir_search(char *progname)
931 char path[MAXPATHLEN];
935 for (dir = srcdirs; dir != NULL; dir = dir->next) {
936 snprintf(path, MAXPATHLEN, "%s/%s", dir->str, progname);
940 if ((srcdir = strdup(path)) == NULL)
949 void top_makefile_rules(FILE *outmk)
953 if ( subtract_strlst(&libs, &libs_so) )
954 fprintf(outmk, "# NOTE: Some LIBS declarations below overridden by LIBS_SO\n");
956 fprintf(outmk, "LIBS+=");
957 output_strlst(outmk, libs);
959 fprintf(outmk, "LIBS_SO+=");
960 output_strlst(outmk, libs_so);
963 fprintf(outmk, "MAKEOBJDIRPREFIX?=%s\n", objprefix);
964 fprintf(outmk, "MAKEENV=env MAKEOBJDIRPREFIX=$(MAKEOBJDIRPREFIX)\n");
965 fprintf(outmk, "CRUNCHMAKE=$(MAKEENV) $(MAKE)\n");
967 fprintf(outmk, "CRUNCHMAKE=$(MAKE)\n");
971 fprintf(outmk, "BUILDOPTS+=");
972 output_strlst(outmk, buildopts);
975 fprintf(outmk, "CRUNCHED_OBJS=");
976 for (p = progs; p != NULL; p = p->next)
977 fprintf(outmk, " %s.lo", p->name);
978 fprintf(outmk, "\n");
980 fprintf(outmk, "SUBMAKE_TARGETS=");
981 for (p = progs; p != NULL; p = p->next)
982 fprintf(outmk, " %s_make", p->ident);
983 fprintf(outmk, "\nSUBCLEAN_TARGETS=");
984 for (p = progs; p != NULL; p = p->next)
985 fprintf(outmk, " %s_clean", p->ident);
986 fprintf(outmk, "\n\n");
988 fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n");
989 fprintf(outmk, "exe: %s\n", execfname);
990 fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS) $(SUBMAKE_TARGETS)\n", execfname, execfname);
991 fprintf(outmk, ".if defined(LIBS_SO) && !empty(LIBS_SO)\n");
992 fprintf(outmk, "\t$(CC) -o %s %s.o $(CRUNCHED_OBJS) \\\n",
993 execfname, execfname);
994 fprintf(outmk, "\t\t-Xlinker -Bstatic $(LIBS) \\\n");
995 fprintf(outmk, "\t\t-Xlinker -Bdynamic $(LIBS_SO)\n");
996 fprintf(outmk, ".else\n");
997 fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n",
998 execfname, execfname);
999 fprintf(outmk, ".endif\n");
1000 fprintf(outmk, "\tstrip %s\n", execfname);
1001 fprintf(outmk, "realclean: clean subclean\n");
1002 fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", execfname);
1003 fprintf(outmk, "subclean: $(SUBCLEAN_TARGETS)\n");
1007 void prog_makefile_rules(FILE *outmk, prog_t *p)
1011 fprintf(outmk, "\n# -------- %s\n\n", p->name);
1013 fprintf(outmk, "%s_OBJDIR=", p->ident);
1015 fprintf(outmk, "%s", p->objdir);
1017 fprintf(outmk, "$(MAKEOBJDIRPREFIX)/$(%s_REALSRCDIR)\n",
1019 fprintf(outmk, "\n");
1021 fprintf(outmk, "%s_OBJPATHS=", p->ident);
1023 output_strlst(outmk, p->objpaths);
1025 for (lst = p->objs; lst != NULL; lst = lst->next) {
1026 fprintf(outmk, " $(%s_OBJDIR)/%s", p->ident, lst->str);
1028 fprintf(outmk, "\n");
1031 if (p->srcdir && p->objs) {
1032 fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir);
1033 fprintf(outmk, "%s_REALSRCDIR=%s\n", p->ident, p->realsrcdir);
1035 fprintf(outmk, "%s_OBJS=", p->ident);
1036 output_strlst(outmk, p->objs);
1037 if (p->buildopts != NULL) {
1038 fprintf(outmk, "%s_OPTS+=", p->ident);
1039 output_strlst(outmk, p->buildopts);
1042 fprintf(outmk, "$(%s_OBJPATHS): %s_make\n\n", p->ident, p->ident);
1044 fprintf(outmk, "%s_make:\n", p->ident);
1045 fprintf(outmk, "\t(cd $(%s_SRCDIR) && ", p->ident);
1047 fprintf(outmk, "$(CRUNCHMAKE) obj && ");
1048 fprintf(outmk, "\\\n");
1049 fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) depend &&",
1051 fprintf(outmk, "\\\n");
1052 fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) "
1054 p->ident, p->ident);
1055 fprintf(outmk, "\n");
1056 fprintf(outmk, "%s_clean:\n", p->ident);
1057 fprintf(outmk, "\t(cd $(%s_SRCDIR) && $(CRUNCHMAKE) $(BUILDOPTS) clean cleandepend)\n\n",
1060 fprintf(outmk, "%s_make:\n", p->ident);
1061 fprintf(outmk, "\t@echo \"** cannot make objs for %s\"\n\n",
1066 fprintf(outmk, "%s_LIBS=", p->ident);
1067 output_strlst(outmk, p->libs);
1070 fprintf(outmk, "%s_stub.c:\n", p->name);
1071 fprintf(outmk, "\techo \""
1072 "int _crunched_%s_stub(int argc, char **argv, char **envp)"
1073 "{return main(argc,argv,envp);}\" >%s_stub.c\n",
1075 fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)",
1076 p->name, p->name, p->ident);
1078 fprintf(outmk, " $(%s_LIBS)", p->ident);
1080 fprintf(outmk, "\n");
1081 fprintf(outmk, "\tld -dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)",
1082 p->name, p->name, p->ident);
1084 fprintf(outmk, " $(%s_LIBS)", p->ident);
1085 fprintf(outmk, "\n");
1086 fprintf(outmk, "\tcrunchide -k _crunched_%s_stub ", p->ident);
1087 for (lst = p->keeplist; lst != NULL; lst = lst->next)
1088 fprintf(outmk, "-k _%s ", lst->str);
1089 fprintf(outmk, "%s.lo\n", p->name);
1092 void output_strlst(FILE *outf, strlst_t *lst)
1094 for (; lst != NULL; lst = lst->next)
1095 if ( strlen(lst->str) )
1096 fprintf(outf, " %s", lst->str);
1097 fprintf(outf, "\n");
1102 * ========================================================================
1103 * general library routines
1107 void status(char *str)
1109 static int lastlen = 0;
1116 spaces = lastlen - len;
1120 fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
1126 void out_of_memory(void)
1128 err(1, "%s: %d: out of memory, stopping", infilename, linenum);
1132 void add_string(strlst_t **listp, char *str)
1136 /* add to end, but be smart about dups */
1138 for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
1139 if (!strcmp(p2->str, str))
1142 p2 = malloc(sizeof(strlst_t));
1145 p2->str = strdup(str);
1147 if (!p2 || !p2->str)
1156 int subtract_strlst(strlst_t **lista, strlst_t **listb)
1158 int subtract_count = 0;
1160 for (p1 = *listb; p1 != NULL; p1 = p1->next)
1161 if ( in_list(lista, p1->str) ) {
1162 warnx("Will compile library `%s' dynamically", p1->str);
1163 strcat(p1->str, "");
1166 return subtract_count;
1169 int in_list(strlst_t **listp, char *str)
1172 for (p1 = *listp; p1 != NULL; p1 = p1->next)
1173 if (!strcmp(p1->str, str))
1178 int is_dir(char *pathname)
1182 if (stat(pathname, &buf) == -1)
1185 return S_ISDIR(buf.st_mode);
1188 int is_nonempty_file(char *pathname)
1192 if (stat(pathname, &buf) == -1)
1195 return S_ISREG(buf.st_mode) && buf.st_size > 0;