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>
51 #define CRUNCH_VERSION "0.2"
53 #define MAXLINELEN 16384
54 #define MAXFIELDS 2048
57 /* internal representation of conf file: */
59 /* simple lists of strings suffice for most parms */
61 typedef struct strlst {
66 /* progs have structure, each field can be set with "special" or calculated */
69 struct prog *next; /* link field */
70 char *name; /* program name */
71 char *ident; /* C identifier for the program name */
75 char *objvar; /* Makefile variable to replace OBJS */
76 strlst_t *objs, *objpaths;
88 strlst_t *buildopts = NULL;
89 strlst_t *srcdirs = NULL;
90 strlst_t *libs = NULL;
91 strlst_t *libs_so = NULL;
94 char confname[MAXPATHLEN], infilename[MAXPATHLEN];
95 char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN];
96 char tempfname[MAXPATHLEN], cachename[MAXPATHLEN], curfilename[MAXPATHLEN];
97 bool tempfname_initialized = false;
98 char outhdrname[MAXPATHLEN] ; /* user-supplied header for *.mk */
99 char *objprefix; /* where are the objects ? */
104 int verbose, readcache; /* options */
106 int makeobj = 0; /* add 'make obj' rules to the makefile */
110 /* general library routines */
112 void status(const char *str);
113 void out_of_memory(void);
114 void add_string(strlst_t **listp, char *str);
115 int is_dir(const char *pathname);
116 int is_nonempty_file(const char *pathname);
117 int subtract_strlst(strlst_t **lista, strlst_t **listb);
118 int in_list(strlst_t **listp, char *str);
120 /* helper routines for main() */
123 void parse_conf_file(void);
124 void gen_outputs(void);
126 extern char *crunched_skel[];
130 main(int argc, char **argv)
137 *outmkname = *outcfname = *execfname = '\0';
139 path_make = getenv("MAKE");
140 if (path_make == NULL || *path_make == '\0')
143 p = getenv("MAKEOBJDIRPREFIX");
144 if (p == NULL || *p == '\0')
145 objprefix = "/usr/obj"; /* default */
147 if ((objprefix = strdup(p)) == NULL)
150 while((optc = getopt(argc, argv, "lh:m:c:e:p:foq")) != -1) {
163 strlcpy(outmkname, optarg, sizeof(outmkname));
166 if ((objprefix = strdup(optarg)) == NULL)
171 strlcpy(outhdrname, optarg, sizeof(outhdrname));
174 strlcpy(outcfname, optarg, sizeof(outcfname));
177 strlcpy(execfname, optarg, sizeof(execfname));
201 strlcpy(infilename, argv[0], sizeof(infilename));
203 /* confname = `basename infilename .conf` */
205 if ((p=strrchr(infilename, '/')) != NULL)
206 strlcpy(confname, p + 1, sizeof(confname));
208 strlcpy(confname, infilename, sizeof(confname));
210 if ((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf"))
214 snprintf(outmkname, sizeof(outmkname), "%s.mk", confname);
216 snprintf(outcfname, sizeof(outcfname), "%s.c", confname);
218 snprintf(execfname, sizeof(execfname), "%s", confname);
220 snprintf(cachename, sizeof(cachename), "%s.cache", confname);
221 snprintf(tempfname, sizeof(tempfname), "%s/crunchgen_%sXXXXXX",
222 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP, confname);
223 tempfname_initialized = false;
238 fprintf(stderr, "%s%s\n\t%s%s\n", "usage: crunchgen [-foq] ",
239 "[-h <makefile-header-name>] [-m <makefile>]",
240 "[-p <obj-prefix>] [-c <c-file-name>] [-e <exec-file>] ",
247 * ========================================================================
248 * parse_conf_file subsystem
252 /* helper routines for parse_conf_file */
254 void parse_one_file(char *filename);
255 void parse_line(char *pline, int *fc, char **fv, int nf);
256 void add_srcdirs(int argc, char **argv);
257 void add_progs(int argc, char **argv);
258 void add_link(int argc, char **argv);
259 void add_libs(int argc, char **argv);
260 void add_libs_so(int argc, char **argv);
261 void add_buildopts(int argc, char **argv);
262 void add_special(int argc, char **argv);
264 prog_t *find_prog(char *str);
265 void add_prog(char *progname);
269 parse_conf_file(void)
271 if (!is_nonempty_file(infilename))
272 errx(1, "fatal: input file \"%s\" not found", infilename);
274 parse_one_file(infilename);
275 if (readcache && is_nonempty_file(cachename)) {
277 parse_one_file(cachename);
283 parse_one_file(char *filename)
285 char *fieldv[MAXFIELDS];
287 void (*f)(int c, char **v);
289 char line[MAXLINELEN];
291 snprintf(line, sizeof(line), "reading %s", filename);
293 strlcpy(curfilename, filename, sizeof(curfilename));
295 if ((cf = fopen(curfilename, "r")) == NULL) {
296 warn("%s", curfilename);
302 while (fgets(line, MAXLINELEN, cf) != NULL) {
304 parse_line(line, &fieldc, fieldv, MAXFIELDS);
309 if (!strcmp(fieldv[0], "srcdirs"))
311 else if(!strcmp(fieldv[0], "progs"))
313 else if(!strcmp(fieldv[0], "ln"))
315 else if(!strcmp(fieldv[0], "libs"))
317 else if(!strcmp(fieldv[0], "libs_so"))
319 else if(!strcmp(fieldv[0], "buildopts"))
321 else if(!strcmp(fieldv[0], "special"))
324 warnx("%s:%d: skipping unknown command `%s'",
325 curfilename, linenum, fieldv[0]);
331 warnx("%s:%d: %s %s",
332 curfilename, linenum, fieldv[0],
333 "command needs at least 1 argument, skipping");
342 warn("%s", curfilename);
350 parse_line(char *pline, int *fc, char **fv, int nf)
358 while (isspace((unsigned char)*p))
361 if (*p == '\0' || *p == '#')
367 while (*p && !isspace((unsigned char)*p) && *p != '#')
370 if (*p == '\0' || *p == '#')
377 *p = '\0'; /* needed for '#' case */
382 add_srcdirs(int argc, char **argv)
386 for (i = 1; i < argc; i++) {
388 add_string(&srcdirs, argv[i]);
390 warnx("%s:%d: `%s' is not a directory, skipping it",
391 curfilename, linenum, argv[i]);
399 add_progs(int argc, char **argv)
403 for (i = 1; i < argc; i++)
409 add_prog(char *progname)
413 /* add to end, but be smart about dups */
415 for (p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next)
416 if (!strcmp(p2->name, progname))
419 p2 = malloc(sizeof(prog_t));
421 memset(p2, 0, sizeof(prog_t));
422 p2->name = strdup(progname);
424 if (!p2 || !p2->name)
435 p2->realsrcdir = NULL;
442 p2->buildopts = NULL;
446 printf("%s\n",progname);
451 add_link(int argc, char **argv)
454 prog_t *p = find_prog(argv[1]);
457 warnx("%s:%d: no prog %s previously declared, skipping link",
458 curfilename, linenum, argv[1]);
463 for (i = 2; i < argc; i++) {
465 printf("%s\n",argv[i]);
467 add_string(&p->links, argv[i]);
473 add_libs(int argc, char **argv)
477 for(i = 1; i < argc; i++) {
478 add_string(&libs, argv[i]);
479 if ( in_list(&libs_so, argv[i]) )
481 "library `%s' specified as dynamic earlier",
482 curfilename, linenum, argv[i]);
488 add_libs_so(int argc, char **argv)
492 for(i = 1; i < argc; i++) {
493 add_string(&libs_so, argv[i]);
494 if ( in_list(&libs, argv[i]) )
496 "library `%s' specified as static earlier",
497 curfilename, linenum, argv[i]);
503 add_buildopts(int argc, char **argv)
507 for (i = 1; i < argc; i++)
508 add_string(&buildopts, argv[i]);
513 add_special(int argc, char **argv)
516 prog_t *p = find_prog(argv[1]);
522 warnx("%s:%d: no prog %s previously declared, skipping special",
523 curfilename, linenum, argv[1]);
528 if (!strcmp(argv[2], "ident")) {
531 if ((p->ident = strdup(argv[3])) == NULL)
533 } else if (!strcmp(argv[2], "srcdir")) {
536 if ((p->srcdir = strdup(argv[3])) == NULL)
538 } else if (!strcmp(argv[2], "objdir")) {
541 if((p->objdir = strdup(argv[3])) == NULL)
543 } else if (!strcmp(argv[2], "objs")) {
545 for (i = 3; i < argc; i++)
546 add_string(&p->objs, argv[i]);
547 } else if (!strcmp(argv[2], "objpaths")) {
549 for (i = 3; i < argc; i++)
550 add_string(&p->objpaths, argv[i]);
551 } else if (!strcmp(argv[2], "keep")) {
553 for(i = 3; i < argc; i++)
554 add_string(&p->keeplist, argv[i]);
555 } else if (!strcmp(argv[2], "objvar")) {
558 if ((p->objvar = strdup(argv[3])) == NULL)
560 } else if (!strcmp(argv[2], "buildopts")) {
562 for (i = 3; i < argc; i++)
563 add_string(&p->buildopts, argv[i]);
564 } else if (!strcmp(argv[2], "lib")) {
565 for (i = 3; i < argc; i++)
566 add_string(&p->libs, argv[i]);
568 warnx("%s:%d: bad parameter name `%s', skipping line",
569 curfilename, linenum, argv[2]);
575 warnx("%s:%d: too %s arguments, expected \"special %s %s <string>\"",
576 curfilename, linenum, argc < 4? "few" : "many", argv[1], argv[2]);
581 prog_t *find_prog(char *str)
585 for (p = progs; p != NULL; p = p->next)
586 if (!strcmp(p->name, str))
594 * ========================================================================
595 * gen_outputs subsystem
599 /* helper subroutines */
601 void remove_error_progs(void);
602 void fillin_program(prog_t *p);
603 void gen_specials_cache(void);
604 void gen_output_makefile(void);
605 void gen_output_cfile(void);
607 void fillin_program_objs(prog_t *p, char *path);
608 void top_makefile_rules(FILE *outmk);
609 void prog_makefile_rules(FILE *outmk, prog_t *p);
610 void output_strlst(FILE *outf, strlst_t *lst);
611 char *genident(char *str);
612 char *dir_search(char *progname);
620 for (p = progs; p != NULL; p = p->next)
623 remove_error_progs();
624 gen_specials_cache();
626 gen_output_makefile();
629 "Run \"%s -f %s\" to build crunched binary.\n",
630 path_make, outmkname);
634 * run the makefile for the program to find which objects are necessary
637 fillin_program(prog_t *p)
639 char path[MAXPATHLEN];
640 char line[MAXLINELEN];
643 snprintf(line, MAXLINELEN, "filling in parms for %s", p->name);
647 p->ident = genident(p->name);
649 /* look for the source directory if one wasn't specified by a special */
651 p->srcdir = dir_search(p->name);
654 /* Determine the actual srcdir (maybe symlinked). */
656 snprintf(line, MAXLINELEN, "cd %s && pwd -P", p->srcdir);
659 errx(1, "Can't execute: %s\n", line);
662 fgets(path, sizeof path, f);
664 errx(1, "Can't execute: %s\n", line);
667 errx(1, "Can't perform pwd on: %s\n", p->srcdir);
669 /* Chop off trailing newline. */
670 path[strlen(path) - 1] = '\0';
671 p->realsrcdir = strdup(path);
674 /* Unless the option to make object files was specified the
675 * the objects will be built in the source directory unless
676 * an object directory already exists.
678 if (!makeobj && !p->objdir && p->srcdir) {
682 snprintf(line, sizeof line, "%s/%s", objprefix, p->realsrcdir);
684 ((auto_obj = getenv("MK_AUTO_OBJ")) != NULL &&
685 strcmp(auto_obj, "yes") == 0)) {
686 if ((p->objdir = strdup(line)) == NULL)
689 p->objdir = p->realsrcdir;
693 * XXX look for a Makefile.{name} in local directory first.
694 * This lets us override the original Makefile.
696 snprintf(path, sizeof(path), "Makefile.%s", p->name);
697 if (is_nonempty_file(path)) {
698 snprintf(line, MAXLINELEN, "Using %s for %s", path, p->name);
702 snprintf(path, sizeof(path), "%s/Makefile", p->srcdir);
703 if (!p->objs && p->srcdir && is_nonempty_file(path))
704 fillin_program_objs(p, path);
706 if (!p->srcdir && !p->objdir && verbose)
708 "warning: could not find source directory",
709 infilename, p->name);
710 if (!p->objs && verbose)
711 warnx("%s: %s: warning: could not find any .o files",
712 infilename, p->name);
714 if ((!p->srcdir || !p->objdir) && !p->objs)
719 fillin_program_objs(prog_t *p, char *path)
726 char line[MAXLINELEN];
728 /* discover the objs from the srcdir Makefile */
731 * We reuse the same temporary file name for multiple objects. However,
732 * some libc implementations (such as glibc) return EINVAL if there
733 * are no XXXXX characters in the template. This happens after the
734 * first call to mkstemp since the argument is modified in-place.
735 * To avoid this error we use open() instead of mkstemp() after the
738 if (tempfname_initialized) {
739 if ((fd = open(tempfname, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1) {
740 err(EX_OSERR, "open(%s)", tempfname);
742 } else if ((fd = mkstemp(tempfname)) == -1) {
743 err(EX_OSERR, "mkstemp(%s)", tempfname);
745 tempfname_initialized = true;
746 if ((f = fdopen(fd, "w")) == NULL) {
747 warn("fdopen(%s)", tempfname);
755 * XXX include outhdrname (e.g. to contain Make variables)
757 if (outhdrname[0] != '\0')
758 fprintf(f, ".include \"%s\"\n", outhdrname);
759 fprintf(f, ".include \"%s\"\n", path);
760 fprintf(f, ".POSIX:\n");
762 fprintf(f, "BUILDOPTS+=");
763 output_strlst(f, buildopts);
765 fprintf(f, ".if defined(PROG)\n");
766 fprintf(f, "%s?=${PROG}.o\n", objvar);
767 fprintf(f, ".endif\n");
768 fprintf(f, "loop:\n\t@echo 'OBJS= '${%s}\n", objvar);
770 fprintf(f, "crunchgen_objs:\n"
771 "\t@cd %s && %s -f %s $(BUILDOPTS) $(%s_OPTS)",
772 p->srcdir, path_make, tempfname, p->ident);
773 for (s = p->buildopts; s != NULL; s = s->next)
774 fprintf(f, " %s", s->str);
775 fprintf(f, " loop\n");
779 snprintf(line, MAXLINELEN, "cd %s && %s -f %s -B crunchgen_objs",
780 p->srcdir, path_make, tempfname);
781 if ((f = popen(line, "r")) == NULL) {
782 warn("submake pipe");
787 while(fgets(line, MAXLINELEN, f)) {
788 if (strncmp(line, "OBJS= ", 6)) {
789 warnx("make error: %s", line);
795 while (isspace((unsigned char)*cp))
800 while (*cp && !isspace((unsigned char)*cp))
804 add_string(&p->objs, obj);
805 while (isspace((unsigned char)*cp))
810 if ((rc=pclose(f)) != 0) {
811 warnx("make error: make returned %d", rc);
819 remove_error_progs(void)
823 p1 = NULL; p2 = progs;
826 p1 = p2, p2 = p2->next;
828 /* delete it from linked list */
829 warnx("%s: %s: ignoring program because of errors",
830 infilename, p2->name);
841 gen_specials_cache(void)
845 char line[MAXLINELEN];
847 snprintf(line, MAXLINELEN, "generating %s", cachename);
850 if ((cachef = fopen(cachename, "w")) == NULL) {
851 warn("%s", cachename);
856 fprintf(cachef, "# %s - parm cache generated from %s by crunchgen "
858 cachename, infilename, CRUNCH_VERSION);
860 for (p = progs; p != NULL; p = p->next) {
861 fprintf(cachef, "\n");
863 fprintf(cachef, "special %s srcdir %s\n",
866 fprintf(cachef, "special %s objdir %s\n",
869 fprintf(cachef, "special %s objs", p->name);
870 output_strlst(cachef, p->objs);
873 fprintf(cachef, "special %s objpaths", p->name);
874 output_strlst(cachef, p->objpaths);
882 gen_output_makefile(void)
886 char line[MAXLINELEN];
888 snprintf(line, MAXLINELEN, "generating %s", outmkname);
891 if ((outmk = fopen(outmkname, "w")) == NULL) {
892 warn("%s", outmkname);
897 fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n",
898 outmkname, infilename, CRUNCH_VERSION);
900 if (outhdrname[0] != '\0')
901 fprintf(outmk, ".include \"%s\"\n", outhdrname);
903 top_makefile_rules(outmk);
904 for (p = progs; p != NULL; p = p->next)
905 prog_makefile_rules(outmk, p);
907 fprintf(outmk, "\n# ========\n");
913 gen_output_cfile(void)
919 char line[MAXLINELEN];
921 snprintf(line, MAXLINELEN, "generating %s", outcfname);
924 if((outcf = fopen(outcfname, "w")) == NULL) {
925 warn("%s", outcfname);
931 "/* %s - generated from %s by crunchgen %s */\n",
932 outcfname, infilename, CRUNCH_VERSION);
934 fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname);
935 for (cp = crunched_skel; *cp != NULL; cp++)
936 fprintf(outcf, "%s\n", *cp);
938 for (p = progs; p != NULL; p = p->next)
940 "extern crunched_stub_t _crunched_%s_stub;\n",
943 fprintf(outcf, "\nstruct stub entry_points[] = {\n");
944 for (p = progs; p != NULL; p = p->next) {
945 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
947 for (s = p->links; s != NULL; s = s->next)
948 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
952 fprintf(outcf, "\t{ EXECNAME, crunched_main },\n");
953 fprintf(outcf, "\t{ NULL, NULL }\n};\n");
958 char *genident(char *str)
963 * generates a Makefile/C identifier from a program name,
964 * mapping '-' to '_' and ignoring all other non-identifier
965 * characters. This leads to programs named "foo.bar" and
966 * "foobar" to map to the same identifier.
969 if ((n = strdup(str)) == NULL)
971 for (d = s = n; *s != '\0'; s++) {
974 else if (*s == '_' || isalnum((unsigned char)*s))
982 char *dir_search(char *progname)
984 char path[MAXPATHLEN];
988 for (dir = srcdirs; dir != NULL; dir = dir->next) {
989 snprintf(path, MAXPATHLEN, "%s/%s", dir->str, progname);
993 if ((srcdir = strdup(path)) == NULL)
1003 top_makefile_rules(FILE *outmk)
1007 fprintf(outmk, "LD?= ld\n");
1008 if ( subtract_strlst(&libs, &libs_so) )
1009 fprintf(outmk, "# NOTE: Some LIBS declarations below overridden by LIBS_SO\n");
1011 fprintf(outmk, "LIBS+=");
1012 output_strlst(outmk, libs);
1014 fprintf(outmk, "LIBS_SO+=");
1015 output_strlst(outmk, libs_so);
1018 fprintf(outmk, "MAKEOBJDIRPREFIX?=%s\n", objprefix);
1019 fprintf(outmk, "MAKEENV=env MAKEOBJDIRPREFIX=$(MAKEOBJDIRPREFIX)\n");
1020 fprintf(outmk, "CRUNCHMAKE=$(MAKEENV) $(MAKE)\n");
1022 fprintf(outmk, "CRUNCHMAKE=$(MAKE)\n");
1026 fprintf(outmk, "BUILDOPTS+=");
1027 output_strlst(outmk, buildopts);
1030 fprintf(outmk, "CRUNCHED_OBJS=");
1031 for (p = progs; p != NULL; p = p->next)
1032 fprintf(outmk, " %s.lo", p->name);
1033 fprintf(outmk, "\n");
1035 fprintf(outmk, "SUBMAKE_TARGETS=");
1036 for (p = progs; p != NULL; p = p->next)
1037 fprintf(outmk, " %s_make", p->ident);
1038 fprintf(outmk, "\nSUBCLEAN_TARGETS=");
1039 for (p = progs; p != NULL; p = p->next)
1040 fprintf(outmk, " %s_clean", p->ident);
1041 fprintf(outmk, "\n\n");
1043 fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n");
1044 fprintf(outmk, "exe: %s\n", execfname);
1045 fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS) $(SUBMAKE_TARGETS)\n", execfname, execfname);
1046 fprintf(outmk, ".if defined(LIBS_SO) && !empty(LIBS_SO)\n");
1047 fprintf(outmk, "\t$(CC) -o %s %s.o $(CRUNCHED_OBJS) \\\n",
1048 execfname, execfname);
1049 fprintf(outmk, "\t\t-Xlinker -Bstatic $(LIBS) \\\n");
1050 fprintf(outmk, "\t\t-Xlinker -Bdynamic $(LIBS_SO)\n");
1051 fprintf(outmk, ".else\n");
1052 fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n",
1053 execfname, execfname);
1054 fprintf(outmk, ".endif\n");
1055 fprintf(outmk, "realclean: clean subclean\n");
1056 fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", execfname);
1057 fprintf(outmk, "subclean: $(SUBCLEAN_TARGETS)\n");
1062 prog_makefile_rules(FILE *outmk, prog_t *p)
1066 fprintf(outmk, "\n# -------- %s\n\n", p->name);
1068 fprintf(outmk, "%s_OBJDIR=", p->ident);
1070 fprintf(outmk, "%s", p->objdir);
1072 fprintf(outmk, "$(MAKEOBJDIRPREFIX)/$(%s_REALSRCDIR)\n",
1074 fprintf(outmk, "\n");
1076 fprintf(outmk, "%s_OBJPATHS=", p->ident);
1078 output_strlst(outmk, p->objpaths);
1080 for (lst = p->objs; lst != NULL; lst = lst->next) {
1081 fprintf(outmk, " $(%s_OBJDIR)/%s", p->ident, lst->str);
1083 fprintf(outmk, "\n");
1085 fprintf(outmk, "$(%s_OBJPATHS): .NOMETA\n", p->ident);
1087 if (p->srcdir && p->objs) {
1088 fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir);
1089 fprintf(outmk, "%s_REALSRCDIR=%s\n", p->ident, p->realsrcdir);
1091 fprintf(outmk, "%s_OBJS=", p->ident);
1092 output_strlst(outmk, p->objs);
1093 if (p->buildopts != NULL) {
1094 fprintf(outmk, "%s_OPTS+=", p->ident);
1095 output_strlst(outmk, p->buildopts);
1098 fprintf(outmk, "$(%s_OBJPATHS): %s_make\n\n", p->ident, p->ident);
1100 fprintf(outmk, "%s_make:\n", p->ident);
1101 fprintf(outmk, "\t(cd $(%s_SRCDIR) && ", p->ident);
1103 fprintf(outmk, "$(CRUNCHMAKE) obj && ");
1104 fprintf(outmk, "\\\n");
1105 fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) depend &&",
1107 fprintf(outmk, "\\\n");
1108 fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) "
1110 p->ident, p->ident);
1111 fprintf(outmk, "\n");
1112 fprintf(outmk, "%s_clean:\n", p->ident);
1113 fprintf(outmk, "\t(cd $(%s_SRCDIR) && $(CRUNCHMAKE) $(BUILDOPTS) clean cleandepend)\n\n",
1116 fprintf(outmk, "%s_make:\n", p->ident);
1117 fprintf(outmk, "\t@echo \"** cannot make objs for %s\"\n\n",
1122 fprintf(outmk, "%s_LIBS=", p->ident);
1123 output_strlst(outmk, p->libs);
1126 fprintf(outmk, "%s_stub.c:\n", p->name);
1127 fprintf(outmk, "\techo \""
1128 "extern int main(int argc, char **argv, char **envp); "
1129 "int _crunched_%s_stub(int argc, char **argv, char **envp);"
1130 "int _crunched_%s_stub(int argc, char **argv, char **envp)"
1131 "{return main(argc,argv,envp);}\" >%s_stub.c\n",
1132 p->ident, p->ident, p->name);
1133 fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)",
1134 p->name, p->name, p->ident);
1136 fprintf(outmk, " $(%s_LIBS)", p->ident);
1138 fprintf(outmk, "\n");
1139 fprintf(outmk, "\t$(CC) -nostdlib -Wl,-dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)",
1140 p->name, p->name, p->ident);
1142 fprintf(outmk, " $(%s_LIBS)", p->ident);
1143 fprintf(outmk, "\n");
1144 fprintf(outmk, "\tcrunchide -k _crunched_%s_stub ", p->ident);
1145 for (lst = p->keeplist; lst != NULL; lst = lst->next)
1146 fprintf(outmk, "-k _%s ", lst->str);
1147 fprintf(outmk, "%s.lo\n", p->name);
1151 output_strlst(FILE *outf, strlst_t *lst)
1153 for (; lst != NULL; lst = lst->next)
1154 if ( strlen(lst->str) )
1155 fprintf(outf, " %s", lst->str);
1156 fprintf(outf, "\n");
1161 * ========================================================================
1162 * general library routines
1167 status(const char *str)
1169 static int lastlen = 0;
1176 spaces = lastlen - len;
1180 fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
1189 err(1, "%s: %d: out of memory, stopping", infilename, linenum);
1194 add_string(strlst_t **listp, char *str)
1198 /* add to end, but be smart about dups */
1200 for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
1201 if (!strcmp(p2->str, str))
1204 p2 = malloc(sizeof(strlst_t));
1207 p2->str = strdup(str);
1209 if (!p2 || !p2->str)
1219 subtract_strlst(strlst_t **lista, strlst_t **listb)
1221 int subtract_count = 0;
1223 for (p1 = *listb; p1 != NULL; p1 = p1->next)
1224 if ( in_list(lista, p1->str) ) {
1225 warnx("Will compile library `%s' dynamically", p1->str);
1226 strcat(p1->str, "");
1229 return subtract_count;
1233 in_list(strlst_t **listp, char *str)
1236 for (p1 = *listp; p1 != NULL; p1 = p1->next)
1237 if (!strcmp(p1->str, str))
1243 is_dir(const char *pathname)
1247 if (stat(pathname, &buf) == -1)
1250 return S_ISDIR(buf.st_mode);
1254 is_nonempty_file(const char *pathname)
1258 if (stat(pathname, &buf) == -1)
1261 return S_ISREG(buf.st_mode) && buf.st_size > 0;