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) {
676 snprintf(line, sizeof line, "%s/%s", objprefix, p->realsrcdir);
678 ((auto_obj = getenv("MK_AUTO_OBJ")) != NULL &&
679 strcmp(auto_obj, "yes") == 0)) {
680 if ((p->objdir = strdup(line)) == NULL)
683 p->objdir = p->realsrcdir;
687 * XXX look for a Makefile.{name} in local directory first.
688 * This lets us override the original Makefile.
690 snprintf(path, sizeof(path), "Makefile.%s", p->name);
691 if (is_nonempty_file(path)) {
692 snprintf(line, MAXLINELEN, "Using %s for %s", path, p->name);
696 snprintf(path, sizeof(path), "%s/Makefile", p->srcdir);
697 if (!p->objs && p->srcdir && is_nonempty_file(path))
698 fillin_program_objs(p, path);
700 if (!p->srcdir && !p->objdir && verbose)
702 "warning: could not find source directory",
703 infilename, p->name);
704 if (!p->objs && verbose)
705 warnx("%s: %s: warning: could not find any .o files",
706 infilename, p->name);
708 if ((!p->srcdir || !p->objdir) && !p->objs)
713 fillin_program_objs(prog_t *p, char *path)
720 char line[MAXLINELEN];
722 /* discover the objs from the srcdir Makefile */
724 if ((fd = mkstemp(tempfname)) == -1) {
728 if ((f = fdopen(fd, "w")) == NULL) {
729 warn("%s", tempfname);
737 * XXX include outhdrname (e.g. to contain Make variables)
739 if (outhdrname[0] != '\0')
740 fprintf(f, ".include \"%s\"\n", outhdrname);
741 fprintf(f, ".include \"%s\"\n", path);
742 fprintf(f, ".POSIX:\n");
744 fprintf(f, "BUILDOPTS+=");
745 output_strlst(f, buildopts);
747 fprintf(f, ".if defined(PROG)\n");
748 fprintf(f, "%s?=${PROG}.o\n", objvar);
749 fprintf(f, ".endif\n");
750 fprintf(f, "loop:\n\t@echo 'OBJS= '${%s}\n", objvar);
752 fprintf(f, "crunchgen_objs:\n"
753 "\t@cd %s && %s -f %s $(BUILDOPTS) $(%s_OPTS)",
754 p->srcdir, path_make, tempfname, p->ident);
755 for (s = p->buildopts; s != NULL; s = s->next)
756 fprintf(f, " %s", s->str);
757 fprintf(f, " loop\n");
761 snprintf(line, MAXLINELEN, "cd %s && %s -f %s -B crunchgen_objs",
762 p->srcdir, path_make, tempfname);
763 if ((f = popen(line, "r")) == NULL) {
764 warn("submake pipe");
769 while(fgets(line, MAXLINELEN, f)) {
770 if (strncmp(line, "OBJS= ", 6)) {
771 warnx("make error: %s", line);
777 while (isspace((unsigned char)*cp))
782 while (*cp && !isspace((unsigned char)*cp))
786 add_string(&p->objs, obj);
787 while (isspace((unsigned char)*cp))
792 if ((rc=pclose(f)) != 0) {
793 warnx("make error: make returned %d", rc);
801 remove_error_progs(void)
805 p1 = NULL; p2 = progs;
808 p1 = p2, p2 = p2->next;
810 /* delete it from linked list */
811 warnx("%s: %s: ignoring program because of errors",
812 infilename, p2->name);
823 gen_specials_cache(void)
827 char line[MAXLINELEN];
829 snprintf(line, MAXLINELEN, "generating %s", cachename);
832 if ((cachef = fopen(cachename, "w")) == NULL) {
833 warn("%s", cachename);
838 fprintf(cachef, "# %s - parm cache generated from %s by crunchgen "
840 cachename, infilename, CRUNCH_VERSION);
842 for (p = progs; p != NULL; p = p->next) {
843 fprintf(cachef, "\n");
845 fprintf(cachef, "special %s srcdir %s\n",
848 fprintf(cachef, "special %s objdir %s\n",
851 fprintf(cachef, "special %s objs", p->name);
852 output_strlst(cachef, p->objs);
855 fprintf(cachef, "special %s objpaths", p->name);
856 output_strlst(cachef, p->objpaths);
864 gen_output_makefile(void)
868 char line[MAXLINELEN];
870 snprintf(line, MAXLINELEN, "generating %s", outmkname);
873 if ((outmk = fopen(outmkname, "w")) == NULL) {
874 warn("%s", outmkname);
879 fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n",
880 outmkname, infilename, CRUNCH_VERSION);
882 if (outhdrname[0] != '\0')
883 fprintf(outmk, ".include \"%s\"\n", outhdrname);
885 top_makefile_rules(outmk);
886 for (p = progs; p != NULL; p = p->next)
887 prog_makefile_rules(outmk, p);
889 fprintf(outmk, "\n# ========\n");
895 gen_output_cfile(void)
901 char line[MAXLINELEN];
903 snprintf(line, MAXLINELEN, "generating %s", outcfname);
906 if((outcf = fopen(outcfname, "w")) == NULL) {
907 warn("%s", outcfname);
913 "/* %s - generated from %s by crunchgen %s */\n",
914 outcfname, infilename, CRUNCH_VERSION);
916 fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname);
917 for (cp = crunched_skel; *cp != NULL; cp++)
918 fprintf(outcf, "%s\n", *cp);
920 for (p = progs; p != NULL; p = p->next)
921 fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident);
923 fprintf(outcf, "\nstruct stub entry_points[] = {\n");
924 for (p = progs; p != NULL; p = p->next) {
925 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
927 for (s = p->links; s != NULL; s = s->next)
928 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
932 fprintf(outcf, "\t{ EXECNAME, crunched_main },\n");
933 fprintf(outcf, "\t{ NULL, NULL }\n};\n");
938 char *genident(char *str)
943 * generates a Makefile/C identifier from a program name,
944 * mapping '-' to '_' and ignoring all other non-identifier
945 * characters. This leads to programs named "foo.bar" and
946 * "foobar" to map to the same identifier.
949 if ((n = strdup(str)) == NULL)
951 for (d = s = n; *s != '\0'; s++) {
954 else if (*s == '_' || isalnum((unsigned char)*s))
962 char *dir_search(char *progname)
964 char path[MAXPATHLEN];
968 for (dir = srcdirs; dir != NULL; dir = dir->next) {
969 snprintf(path, MAXPATHLEN, "%s/%s", dir->str, progname);
973 if ((srcdir = strdup(path)) == NULL)
983 top_makefile_rules(FILE *outmk)
987 fprintf(outmk, "LD?= ld\n");
988 if ( subtract_strlst(&libs, &libs_so) )
989 fprintf(outmk, "# NOTE: Some LIBS declarations below overridden by LIBS_SO\n");
991 fprintf(outmk, "LIBS+=");
992 output_strlst(outmk, libs);
994 fprintf(outmk, "LIBS_SO+=");
995 output_strlst(outmk, libs_so);
998 fprintf(outmk, "MAKEOBJDIRPREFIX?=%s\n", objprefix);
999 fprintf(outmk, "MAKEENV=env MAKEOBJDIRPREFIX=$(MAKEOBJDIRPREFIX)\n");
1000 fprintf(outmk, "CRUNCHMAKE=$(MAKEENV) $(MAKE)\n");
1002 fprintf(outmk, "CRUNCHMAKE=$(MAKE)\n");
1006 fprintf(outmk, "BUILDOPTS+=");
1007 output_strlst(outmk, buildopts);
1010 fprintf(outmk, "CRUNCHED_OBJS=");
1011 for (p = progs; p != NULL; p = p->next)
1012 fprintf(outmk, " %s.lo", p->name);
1013 fprintf(outmk, "\n");
1015 fprintf(outmk, "SUBMAKE_TARGETS=");
1016 for (p = progs; p != NULL; p = p->next)
1017 fprintf(outmk, " %s_make", p->ident);
1018 fprintf(outmk, "\nSUBCLEAN_TARGETS=");
1019 for (p = progs; p != NULL; p = p->next)
1020 fprintf(outmk, " %s_clean", p->ident);
1021 fprintf(outmk, "\n\n");
1023 fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n");
1024 fprintf(outmk, "exe: %s\n", execfname);
1025 fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS) $(SUBMAKE_TARGETS)\n", execfname, execfname);
1026 fprintf(outmk, ".if defined(LIBS_SO) && !empty(LIBS_SO)\n");
1027 fprintf(outmk, "\t$(CC) -o %s %s.o $(CRUNCHED_OBJS) \\\n",
1028 execfname, execfname);
1029 fprintf(outmk, "\t\t-Xlinker -Bstatic $(LIBS) \\\n");
1030 fprintf(outmk, "\t\t-Xlinker -Bdynamic $(LIBS_SO)\n");
1031 fprintf(outmk, ".else\n");
1032 fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n",
1033 execfname, execfname);
1034 fprintf(outmk, ".endif\n");
1035 fprintf(outmk, "realclean: clean subclean\n");
1036 fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", execfname);
1037 fprintf(outmk, "subclean: $(SUBCLEAN_TARGETS)\n");
1042 prog_makefile_rules(FILE *outmk, prog_t *p)
1046 fprintf(outmk, "\n# -------- %s\n\n", p->name);
1048 fprintf(outmk, "%s_OBJDIR=", p->ident);
1050 fprintf(outmk, "%s", p->objdir);
1052 fprintf(outmk, "$(MAKEOBJDIRPREFIX)/$(%s_REALSRCDIR)\n",
1054 fprintf(outmk, "\n");
1056 fprintf(outmk, "%s_OBJPATHS=", p->ident);
1058 output_strlst(outmk, p->objpaths);
1060 for (lst = p->objs; lst != NULL; lst = lst->next) {
1061 fprintf(outmk, " $(%s_OBJDIR)/%s", p->ident, lst->str);
1063 fprintf(outmk, "\n");
1065 fprintf(outmk, "$(%s_OBJPATHS): .NOMETA\n", p->ident);
1067 if (p->srcdir && p->objs) {
1068 fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir);
1069 fprintf(outmk, "%s_REALSRCDIR=%s\n", p->ident, p->realsrcdir);
1071 fprintf(outmk, "%s_OBJS=", p->ident);
1072 output_strlst(outmk, p->objs);
1073 if (p->buildopts != NULL) {
1074 fprintf(outmk, "%s_OPTS+=", p->ident);
1075 output_strlst(outmk, p->buildopts);
1078 fprintf(outmk, "$(%s_OBJPATHS): %s_make\n\n", p->ident, p->ident);
1080 fprintf(outmk, "%s_make:\n", p->ident);
1081 fprintf(outmk, "\t(cd $(%s_SRCDIR) && ", p->ident);
1083 fprintf(outmk, "$(CRUNCHMAKE) obj && ");
1084 fprintf(outmk, "\\\n");
1085 fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) depend &&",
1087 fprintf(outmk, "\\\n");
1088 fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) "
1090 p->ident, p->ident);
1091 fprintf(outmk, "\n");
1092 fprintf(outmk, "%s_clean:\n", p->ident);
1093 fprintf(outmk, "\t(cd $(%s_SRCDIR) && $(CRUNCHMAKE) $(BUILDOPTS) clean cleandepend)\n\n",
1096 fprintf(outmk, "%s_make:\n", p->ident);
1097 fprintf(outmk, "\t@echo \"** cannot make objs for %s\"\n\n",
1102 fprintf(outmk, "%s_LIBS=", p->ident);
1103 output_strlst(outmk, p->libs);
1106 fprintf(outmk, "%s_stub.c:\n", p->name);
1107 fprintf(outmk, "\techo \""
1108 "extern int main(int argc, char **argv, char **envp); "
1109 "int _crunched_%s_stub(int argc, char **argv, char **envp)"
1110 "{return main(argc,argv,envp);}\" >%s_stub.c\n",
1112 fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)",
1113 p->name, p->name, p->ident);
1115 fprintf(outmk, " $(%s_LIBS)", p->ident);
1117 fprintf(outmk, "\n");
1118 fprintf(outmk, "\t$(CC) -nostdlib -Wl,-dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)",
1119 p->name, p->name, p->ident);
1121 fprintf(outmk, " $(%s_LIBS)", p->ident);
1122 fprintf(outmk, "\n");
1123 fprintf(outmk, "\tcrunchide -k _crunched_%s_stub ", p->ident);
1124 for (lst = p->keeplist; lst != NULL; lst = lst->next)
1125 fprintf(outmk, "-k _%s ", lst->str);
1126 fprintf(outmk, "%s.lo\n", p->name);
1130 output_strlst(FILE *outf, strlst_t *lst)
1132 for (; lst != NULL; lst = lst->next)
1133 if ( strlen(lst->str) )
1134 fprintf(outf, " %s", lst->str);
1135 fprintf(outf, "\n");
1140 * ========================================================================
1141 * general library routines
1146 status(const char *str)
1148 static int lastlen = 0;
1155 spaces = lastlen - len;
1159 fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
1168 err(1, "%s: %d: out of memory, stopping", infilename, linenum);
1173 add_string(strlst_t **listp, char *str)
1177 /* add to end, but be smart about dups */
1179 for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
1180 if (!strcmp(p2->str, str))
1183 p2 = malloc(sizeof(strlst_t));
1186 p2->str = strdup(str);
1188 if (!p2 || !p2->str)
1198 subtract_strlst(strlst_t **lista, strlst_t **listb)
1200 int subtract_count = 0;
1202 for (p1 = *listb; p1 != NULL; p1 = p1->next)
1203 if ( in_list(lista, p1->str) ) {
1204 warnx("Will compile library `%s' dynamically", p1->str);
1205 strcat(p1->str, "");
1208 return subtract_count;
1212 in_list(strlst_t **listp, char *str)
1215 for (p1 = *listp; p1 != NULL; p1 = p1->next)
1216 if (!strcmp(p1->str, str))
1222 is_dir(const char *pathname)
1226 if (stat(pathname, &buf) == -1)
1229 return S_ISDIR(buf.st_mode);
1233 is_nonempty_file(const char *pathname)
1237 if (stat(pathname, &buf) == -1)
1240 return S_ISREG(buf.st_mode) && buf.st_size > 0;