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 p->realsrcdir = strdup(path);
672 /* Unless the option to make object files was specified the
673 * the objects will be built in the source directory unless
674 * an object directory already exists.
676 if (!makeobj && !p->objdir && p->srcdir) {
680 snprintf(line, sizeof line, "%s/%s", objprefix, p->realsrcdir);
682 ((auto_obj = getenv("MK_AUTO_OBJ")) != NULL &&
683 strcmp(auto_obj, "yes") == 0)) {
684 if ((p->objdir = strdup(line)) == NULL)
687 p->objdir = p->realsrcdir;
691 * XXX look for a Makefile.{name} in local directory first.
692 * This lets us override the original Makefile.
694 snprintf(path, sizeof(path), "Makefile.%s", p->name);
695 if (is_nonempty_file(path)) {
696 snprintf(line, MAXLINELEN, "Using %s for %s", path, p->name);
700 snprintf(path, sizeof(path), "%s/Makefile", p->srcdir);
701 if (!p->objs && p->srcdir && is_nonempty_file(path))
702 fillin_program_objs(p, path);
704 if (!p->srcdir && !p->objdir && verbose)
706 "warning: could not find source directory",
707 infilename, p->name);
708 if (!p->objs && verbose)
709 warnx("%s: %s: warning: could not find any .o files",
710 infilename, p->name);
712 if ((!p->srcdir || !p->objdir) && !p->objs)
717 fillin_program_objs(prog_t *p, char *path)
724 char line[MAXLINELEN];
726 /* discover the objs from the srcdir Makefile */
729 * We reuse the same temporary file name for multiple objects. However,
730 * some libc implementations (such as glibc) return EINVAL if there
731 * are no XXXXX characters in the template. This happens after the
732 * first call to mkstemp since the argument is modified in-place.
733 * To avoid this error we use open() instead of mkstemp() after the
736 if (tempfname_initialized) {
737 if ((fd = open(tempfname, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1) {
738 err(EX_OSERR, "open(%s)", tempfname);
740 } else if ((fd = mkstemp(tempfname)) == -1) {
741 err(EX_OSERR, "mkstemp(%s)", tempfname);
743 tempfname_initialized = true;
744 if ((f = fdopen(fd, "w")) == NULL) {
745 warn("fdopen(%s)", tempfname);
753 * XXX include outhdrname (e.g. to contain Make variables)
755 if (outhdrname[0] != '\0')
756 fprintf(f, ".include \"%s\"\n", outhdrname);
757 fprintf(f, ".include \"%s\"\n", path);
758 fprintf(f, ".POSIX:\n");
760 fprintf(f, "BUILDOPTS+=");
761 output_strlst(f, buildopts);
763 fprintf(f, ".if defined(PROG)\n");
764 fprintf(f, "%s?=${PROG}.o\n", objvar);
765 fprintf(f, ".endif\n");
766 fprintf(f, "loop:\n\t@echo 'OBJS= '${%s}\n", objvar);
768 fprintf(f, "crunchgen_objs:\n"
769 "\t@cd %s && %s -f %s $(BUILDOPTS) $(%s_OPTS)",
770 p->srcdir, path_make, tempfname, p->ident);
771 for (s = p->buildopts; s != NULL; s = s->next)
772 fprintf(f, " %s", s->str);
773 fprintf(f, " loop\n");
777 snprintf(line, MAXLINELEN, "cd %s && %s -f %s -B crunchgen_objs",
778 p->srcdir, path_make, tempfname);
779 if ((f = popen(line, "r")) == NULL) {
780 warn("submake pipe");
785 while(fgets(line, MAXLINELEN, f)) {
786 if (strncmp(line, "OBJS= ", 6)) {
787 warnx("make error: %s", line);
793 while (isspace((unsigned char)*cp))
798 while (*cp && !isspace((unsigned char)*cp))
802 add_string(&p->objs, obj);
803 while (isspace((unsigned char)*cp))
808 if ((rc=pclose(f)) != 0) {
809 warnx("make error: make returned %d", rc);
817 remove_error_progs(void)
821 p1 = NULL; p2 = progs;
824 p1 = p2, p2 = p2->next;
826 /* delete it from linked list */
827 warnx("%s: %s: ignoring program because of errors",
828 infilename, p2->name);
839 gen_specials_cache(void)
843 char line[MAXLINELEN];
845 snprintf(line, MAXLINELEN, "generating %s", cachename);
848 if ((cachef = fopen(cachename, "w")) == NULL) {
849 warn("%s", cachename);
854 fprintf(cachef, "# %s - parm cache generated from %s by crunchgen "
856 cachename, infilename, CRUNCH_VERSION);
858 for (p = progs; p != NULL; p = p->next) {
859 fprintf(cachef, "\n");
861 fprintf(cachef, "special %s srcdir %s\n",
864 fprintf(cachef, "special %s objdir %s\n",
867 fprintf(cachef, "special %s objs", p->name);
868 output_strlst(cachef, p->objs);
871 fprintf(cachef, "special %s objpaths", p->name);
872 output_strlst(cachef, p->objpaths);
880 gen_output_makefile(void)
884 char line[MAXLINELEN];
886 snprintf(line, MAXLINELEN, "generating %s", outmkname);
889 if ((outmk = fopen(outmkname, "w")) == NULL) {
890 warn("%s", outmkname);
895 fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n",
896 outmkname, infilename, CRUNCH_VERSION);
898 if (outhdrname[0] != '\0')
899 fprintf(outmk, ".include \"%s\"\n", outhdrname);
901 top_makefile_rules(outmk);
902 for (p = progs; p != NULL; p = p->next)
903 prog_makefile_rules(outmk, p);
905 fprintf(outmk, "\n# ========\n");
911 gen_output_cfile(void)
917 char line[MAXLINELEN];
919 snprintf(line, MAXLINELEN, "generating %s", outcfname);
922 if((outcf = fopen(outcfname, "w")) == NULL) {
923 warn("%s", outcfname);
929 "/* %s - generated from %s by crunchgen %s */\n",
930 outcfname, infilename, CRUNCH_VERSION);
932 fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname);
933 for (cp = crunched_skel; *cp != NULL; cp++)
934 fprintf(outcf, "%s\n", *cp);
936 for (p = progs; p != NULL; p = p->next)
937 fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident);
939 fprintf(outcf, "\nstruct stub entry_points[] = {\n");
940 for (p = progs; p != NULL; p = p->next) {
941 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
943 for (s = p->links; s != NULL; s = s->next)
944 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
948 fprintf(outcf, "\t{ EXECNAME, crunched_main },\n");
949 fprintf(outcf, "\t{ NULL, NULL }\n};\n");
954 char *genident(char *str)
959 * generates a Makefile/C identifier from a program name,
960 * mapping '-' to '_' and ignoring all other non-identifier
961 * characters. This leads to programs named "foo.bar" and
962 * "foobar" to map to the same identifier.
965 if ((n = strdup(str)) == NULL)
967 for (d = s = n; *s != '\0'; s++) {
970 else if (*s == '_' || isalnum((unsigned char)*s))
978 char *dir_search(char *progname)
980 char path[MAXPATHLEN];
984 for (dir = srcdirs; dir != NULL; dir = dir->next) {
985 snprintf(path, MAXPATHLEN, "%s/%s", dir->str, progname);
989 if ((srcdir = strdup(path)) == NULL)
999 top_makefile_rules(FILE *outmk)
1003 fprintf(outmk, "LD?= ld\n");
1004 if ( subtract_strlst(&libs, &libs_so) )
1005 fprintf(outmk, "# NOTE: Some LIBS declarations below overridden by LIBS_SO\n");
1007 fprintf(outmk, "LIBS+=");
1008 output_strlst(outmk, libs);
1010 fprintf(outmk, "LIBS_SO+=");
1011 output_strlst(outmk, libs_so);
1014 fprintf(outmk, "MAKEOBJDIRPREFIX?=%s\n", objprefix);
1015 fprintf(outmk, "MAKEENV=env MAKEOBJDIRPREFIX=$(MAKEOBJDIRPREFIX)\n");
1016 fprintf(outmk, "CRUNCHMAKE=$(MAKEENV) $(MAKE)\n");
1018 fprintf(outmk, "CRUNCHMAKE=$(MAKE)\n");
1022 fprintf(outmk, "BUILDOPTS+=");
1023 output_strlst(outmk, buildopts);
1026 fprintf(outmk, "CRUNCHED_OBJS=");
1027 for (p = progs; p != NULL; p = p->next)
1028 fprintf(outmk, " %s.lo", p->name);
1029 fprintf(outmk, "\n");
1031 fprintf(outmk, "SUBMAKE_TARGETS=");
1032 for (p = progs; p != NULL; p = p->next)
1033 fprintf(outmk, " %s_make", p->ident);
1034 fprintf(outmk, "\nSUBCLEAN_TARGETS=");
1035 for (p = progs; p != NULL; p = p->next)
1036 fprintf(outmk, " %s_clean", p->ident);
1037 fprintf(outmk, "\n\n");
1039 fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n");
1040 fprintf(outmk, "exe: %s\n", execfname);
1041 fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS) $(SUBMAKE_TARGETS)\n", execfname, execfname);
1042 fprintf(outmk, ".if defined(LIBS_SO) && !empty(LIBS_SO)\n");
1043 fprintf(outmk, "\t$(CC) -o %s %s.o $(CRUNCHED_OBJS) \\\n",
1044 execfname, execfname);
1045 fprintf(outmk, "\t\t-Xlinker -Bstatic $(LIBS) \\\n");
1046 fprintf(outmk, "\t\t-Xlinker -Bdynamic $(LIBS_SO)\n");
1047 fprintf(outmk, ".else\n");
1048 fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n",
1049 execfname, execfname);
1050 fprintf(outmk, ".endif\n");
1051 fprintf(outmk, "realclean: clean subclean\n");
1052 fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", execfname);
1053 fprintf(outmk, "subclean: $(SUBCLEAN_TARGETS)\n");
1058 prog_makefile_rules(FILE *outmk, prog_t *p)
1062 fprintf(outmk, "\n# -------- %s\n\n", p->name);
1064 fprintf(outmk, "%s_OBJDIR=", p->ident);
1066 fprintf(outmk, "%s", p->objdir);
1068 fprintf(outmk, "$(MAKEOBJDIRPREFIX)/$(%s_REALSRCDIR)\n",
1070 fprintf(outmk, "\n");
1072 fprintf(outmk, "%s_OBJPATHS=", p->ident);
1074 output_strlst(outmk, p->objpaths);
1076 for (lst = p->objs; lst != NULL; lst = lst->next) {
1077 fprintf(outmk, " $(%s_OBJDIR)/%s", p->ident, lst->str);
1079 fprintf(outmk, "\n");
1081 fprintf(outmk, "$(%s_OBJPATHS): .NOMETA\n", p->ident);
1083 if (p->srcdir && p->objs) {
1084 fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir);
1085 fprintf(outmk, "%s_REALSRCDIR=%s\n", p->ident, p->realsrcdir);
1087 fprintf(outmk, "%s_OBJS=", p->ident);
1088 output_strlst(outmk, p->objs);
1089 if (p->buildopts != NULL) {
1090 fprintf(outmk, "%s_OPTS+=", p->ident);
1091 output_strlst(outmk, p->buildopts);
1094 fprintf(outmk, "$(%s_OBJPATHS): %s_make\n\n", p->ident, p->ident);
1096 fprintf(outmk, "%s_make:\n", p->ident);
1097 fprintf(outmk, "\t(cd $(%s_SRCDIR) && ", p->ident);
1099 fprintf(outmk, "$(CRUNCHMAKE) obj && ");
1100 fprintf(outmk, "\\\n");
1101 fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) depend &&",
1103 fprintf(outmk, "\\\n");
1104 fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) "
1106 p->ident, p->ident);
1107 fprintf(outmk, "\n");
1108 fprintf(outmk, "%s_clean:\n", p->ident);
1109 fprintf(outmk, "\t(cd $(%s_SRCDIR) && $(CRUNCHMAKE) $(BUILDOPTS) clean cleandepend)\n\n",
1112 fprintf(outmk, "%s_make:\n", p->ident);
1113 fprintf(outmk, "\t@echo \"** cannot make objs for %s\"\n\n",
1118 fprintf(outmk, "%s_LIBS=", p->ident);
1119 output_strlst(outmk, p->libs);
1122 fprintf(outmk, "%s_stub.c:\n", p->name);
1123 fprintf(outmk, "\techo \""
1124 "extern int main(int argc, char **argv, char **envp); "
1125 "int _crunched_%s_stub(int argc, char **argv, char **envp)"
1126 "{return main(argc,argv,envp);}\" >%s_stub.c\n",
1128 fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)",
1129 p->name, p->name, p->ident);
1131 fprintf(outmk, " $(%s_LIBS)", p->ident);
1133 fprintf(outmk, "\n");
1134 fprintf(outmk, "\t$(CC) -nostdlib -Wl,-dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)",
1135 p->name, p->name, p->ident);
1137 fprintf(outmk, " $(%s_LIBS)", p->ident);
1138 fprintf(outmk, "\n");
1139 fprintf(outmk, "\tcrunchide -k _crunched_%s_stub ", p->ident);
1140 for (lst = p->keeplist; lst != NULL; lst = lst->next)
1141 fprintf(outmk, "-k _%s ", lst->str);
1142 fprintf(outmk, "%s.lo\n", p->name);
1146 output_strlst(FILE *outf, strlst_t *lst)
1148 for (; lst != NULL; lst = lst->next)
1149 if ( strlen(lst->str) )
1150 fprintf(outf, " %s", lst->str);
1151 fprintf(outf, "\n");
1156 * ========================================================================
1157 * general library routines
1162 status(const char *str)
1164 static int lastlen = 0;
1171 spaces = lastlen - len;
1175 fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
1184 err(1, "%s: %d: out of memory, stopping", infilename, linenum);
1189 add_string(strlst_t **listp, char *str)
1193 /* add to end, but be smart about dups */
1195 for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
1196 if (!strcmp(p2->str, str))
1199 p2 = malloc(sizeof(strlst_t));
1202 p2->str = strdup(str);
1204 if (!p2 || !p2->str)
1214 subtract_strlst(strlst_t **lista, strlst_t **listb)
1216 int subtract_count = 0;
1218 for (p1 = *listb; p1 != NULL; p1 = p1->next)
1219 if ( in_list(lista, p1->str) ) {
1220 warnx("Will compile library `%s' dynamically", p1->str);
1221 strcat(p1->str, "");
1224 return subtract_count;
1228 in_list(strlst_t **listp, char *str)
1231 for (p1 = *listp; p1 != NULL; p1 = p1->next)
1232 if (!strcmp(p1->str, str))
1238 is_dir(const char *pathname)
1242 if (stat(pathname, &buf) == -1)
1245 return S_ISDIR(buf.st_mode);
1249 is_nonempty_file(const char *pathname)
1253 if (stat(pathname, &buf) == -1)
1256 return S_ISREG(buf.st_mode) && buf.st_size > 0;