diff options
author | svn2git <svn2git@FreeBSD.org> | 1994-07-01 00:00:00 -0800 |
---|---|---|
committer | svn2git <svn2git@FreeBSD.org> | 1994-07-01 00:00:00 -0800 |
commit | 5e0e9b99dc3fc0ecd49d929db0d57c784b66f481 (patch) | |
tree | e779b5a6edddbb949b7990751b12d6f25304ba86 /contrib/crunch/crunchgen | |
parent | a16f65c7d117419bd266c28a1901ef129a337569 (diff) | |
download | src-releng/1.tar.gz src-releng/1.zip |
Release FreeBSD 1.1.5.1release/1.1.5.1_cvsreleng/1
This commit was manufactured to restore the state of the 1.1.5.1-RELEASE image.
Releases prior to 5.3-RELEASE are omitting the secure/ and crypto/ subdirs.
Diffstat (limited to 'contrib/crunch/crunchgen')
-rw-r--r-- | contrib/crunch/crunchgen/Makefile | 9 | ||||
-rw-r--r-- | contrib/crunch/crunchgen/crunched_main.c | 102 | ||||
-rw-r--r-- | contrib/crunch/crunchgen/crunchgen.1 | 266 | ||||
-rw-r--r-- | contrib/crunch/crunchgen/crunchgen.c | 856 | ||||
-rwxr-xr-x | contrib/crunch/crunchgen/mkskel.sh | 15 |
5 files changed, 1248 insertions, 0 deletions
diff --git a/contrib/crunch/crunchgen/Makefile b/contrib/crunch/crunchgen/Makefile new file mode 100644 index 000000000000..71acb214dca0 --- /dev/null +++ b/contrib/crunch/crunchgen/Makefile @@ -0,0 +1,9 @@ + +PROG=crunchgen +SRCS=crunchgen.c crunched_skel.c +CFLAGS+=-g -Wall + +crunched_skel.c: crunched_main.c + ${.CURDIR}/mkskel.sh ${.CURDIR}/crunched_main.c >crunched_skel.c + +.include <bsd.prog.mk> diff --git a/contrib/crunch/crunchgen/crunched_main.c b/contrib/crunch/crunchgen/crunched_main.c new file mode 100644 index 000000000000..a07317aa5a6b --- /dev/null +++ b/contrib/crunch/crunchgen/crunched_main.c @@ -0,0 +1,102 @@ +/* + * Copyright (c) 1994 University of Maryland + * All Rights Reserved. + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of U.M. not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. U.M. makes no representations about the + * suitability of this software for any purpose. It is provided "as is" + * without express or implied warranty. + * + * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M. + * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Author: James da Silva, Systems Design and Analysis Group + * Computer Science Department + * University of Maryland at College Park + */ +/* + * crunched_main.c - main program for crunched binaries, it branches to a + * particular subprogram based on the value of argv[0]. Also included + * is a little program invoked when the crunched binary is called via + * its EXECNAME. This one prints out the list of compiled-in binaries, + * or calls one of them based on argv[1]. This allows the testing of + * the crunched binary without creating all the links. + */ +#include <stdio.h> +#include <string.h> + +struct stub { + char *name; + int (*f)(); +}; + +extern struct stub entry_points[]; + +int main(int argc, char **argv) +{ + char *slash, *basename; + struct stub *ep; + + if(argv[0] == NULL || *argv[0] == '\0') + crunched_usage(); + + slash = strrchr(argv[0], '/'); + basename = slash? slash+1 : argv[0]; + + for(ep=entry_points; ep->name != NULL; ep++) + if(!strcmp(basename, ep->name)) break; + + if(ep->name) + return ep->f(argc, argv); + else { + fprintf(stderr, "%s: %s not compiled in\n", EXECNAME, basename); + crunched_usage(); + } +} + + +int crunched_main(int argc, char **argv) +{ + struct stub *ep; + int columns, len; + + if(argc <= 1) + crunched_usage(); + + return main(--argc, ++argv); +} + + +int crunched_usage() +{ + int columns, len; + struct stub *ep; + + fprintf(stderr, "Usage: %s <prog> <args> ..., where <prog> is one of:\n", + EXECNAME); + columns = 0; + for(ep=entry_points; ep->name != NULL; ep++) { + len = strlen(ep->name) + 1; + if(columns+len < 80) + columns += len; + else { + fprintf(stderr, "\n"); + columns = len; + } + fprintf(stderr, " %s", ep->name); + } + fprintf(stderr, "\n"); + exit(1); +} + +/* end of crunched_main.c */ + diff --git a/contrib/crunch/crunchgen/crunchgen.1 b/contrib/crunch/crunchgen/crunchgen.1 new file mode 100644 index 000000000000..8c97d66cf1de --- /dev/null +++ b/contrib/crunch/crunchgen/crunchgen.1 @@ -0,0 +1,266 @@ +.\" +.\" Copyright (c) 1994 University of Maryland +.\" All Rights Reserved. +.\" +.\" Permission to use, copy, modify, distribute, and sell this software and its +.\" documentation for any purpose is hereby granted without fee, provided that +.\" the above copyright notice appear in all copies and that both that +.\" copyright notice and this permission notice appear in supporting +.\" documentation, and that the name of U.M. not be used in advertising or +.\" publicity pertaining to distribution of the software without specific, +.\" written prior permission. U.M. makes no representations about the +.\" suitability of this software for any purpose. It is provided "as is" +.\" without express or implied warranty. +.\" +.\" U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M. +.\" BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR +.\" IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +.\" +.\" Author: James da Silva, Systems Design and Analysis Group +.\" Computer Science Department +.\" University of Maryland at College Park +.\" +.Dd June 14, 1994 +.Dt CRUNCHGEN 1 +.Os BSD 4 +.Sh NAME +.Nm \&crunchgen +.Nd generates build environment for a crunched binary +.Sh SYNOPSIS +.Nm \&crunchgen +.Op Fl fq +.Op Fl m Ar makefile-name +.Op Fl c Ar c-file-name +.Op Fl e Ar exec-file-name +.Op Ar conf-file +.Sh DESCRIPTION + +A crunched binary is a program made up of many other programs linked +together into a single executable. The crunched binary main() +function determines which component program to run by the contents of +argv[0]. The main reason to crunch programs together is for fitting +as many programs as possible onto an installation or system recovery +floppy. + +.Pp +.Nm Crunchgen +reads in the specifications in +.Ar conf-file +for a crunched binary, and generates a Makefile and accompanying +top-level C source file that when built create the crunched executable +file from the component programs. For each component program, +.Nm crunchgen +can optionally attempt to determine the object (.o) files that make up +the program from its source directory Makefile. This information is +cached between runs. +.Nm Crunchgen +uses the companion program +.Nm crunchide +to eliminate link-time conflicts between the component programs by +hiding all unnecessary symbols. + +.Pp +After +.Nm crunchgen +is run, the crunched binary can be built by running ``make -f +<conf-name>.mk''. The component programs' object files must already +be built. A ``objs'' target, included in the output makefile, will +run make in each component program's source dir to build the object +files for the user. This is not done automatically since in release +engineering circumstances it is generally not desireable to be +modifying objects in other directories. + +.Pp +The options are as follows: +.Bl -tag -width indent +.It Fl c Ar c-file-name +Set output C file name to +.Ar c-file-name . +The default name is ``<conf-name>.c''. +.It Fl e Ar exec-file-name +Set crunched binary executable file name to +.Ar exec-file-name . +The default name is ``<conf-name>''. +.It Fl f +Flush cache. Forces the recalculation of cached parameters. +.It Fl m Ar makefile-name +Set output Makefile name to +.Ar makefile-name . +The default name is ``<conf-name>.mk''. +.It Fl q +Quiet operation. Status messages are suppressed. +.El +.Sh CRUNCHGEN CONFIGURATION FILE COMMANDS + +.Nm Crunchgen +reads specifications from the +.Ar conf-file +that describe the components of the crunched binary. In its simplest +use, the component program names are merely listed along with the +top-level source directories in which their sources can be found. +.Nm Crunchgen +then calculates (via the source makefiles) and caches the +list of object files and their locations. For more specialized +situations, the user can specify by hand all the parameters that +.Nm crunchgen +needs. +.Pp +The +.Ar conf-file +commands are as follows: +.Bl -tag -width indent +.It Nm srcdirs Ar dirname ... +A list of source trees in which the source directories of the +component programs can be found. These dirs are searched using the +BSD ``<source-dir>/<progname>/'' convention. Multiple +.Nm srcdirs +lines can be specified. The directories are searched in the order +they are given. +.It Nm progs Ar progname ... +A list of programs that make up the crunched binary. Multiple +.Nm progs +lines can be specified. +.It Nm libs Ar libspec ... +A list of library specifications to be included in the crunched binary link. +Multiple +.Nm libs +lines can be specified. +.It Nm ln Ar progname linkname +Causes the crunched binary to invoke +.Ar progname +whenever +.Ar linkname +appears in argv[0]. This allows programs that change their behavior when +run under different names to operate correctly. +.El + +To handle specialized situations, such as when the source is not +available or not built via a conventional Makefile, the following +.Nm special +commands can be used to set +.Nm crunchgen +parameters for a component program. +.Bl -tag -width indent +.It Nm special Ar progname Nm srcdir Ar pathname +Set the source directory for +.Ar progname . +This is normally calculated by searching the specified +.Nm srcdirs +for a directory named +.Ar progname . +.It Nm special Ar progname Nm objdir Ar pathname +Set the obj directory for +.Ar progname . +This is normally calculated by looking for a directory named +.Dq Pa obj +under the +.Ar srcdir , +and if that is not found, the +.Ar srcdir +itself becomes the +.Ar objdir . +.It Nm special Ar progname Nm objs Ar object-file-name ... +Set the list of object files for program +.Ar progname . +This is normally calculated by constructing a temporary makefile that includes +.Dq Nm srcdir / Pa Makefile +and outputs the value of $(OBJS). +.It Nm special Ar progname Nm objpaths Ar full-pathname-to-object-file ... +Sets the pathnames of the object files for program +.Ar progname . +This is normally calculated by prepending the +.Nm objdir +pathname to each file in the +.Nm objs +list. +.El + +.Pp +Only the +.Nm objpaths +parameter is actually needed by +.Nm crunchgen , +but it is calculated from +.Nm objdir +and +.Nm objs , +which are in turn calculated from +.Nm srcdir , +so is sometimes convenient to specify the earlier parameters and let +.Nm crunchgen +calculate forward from there if it can. + +.Pp +The makefile produced by +.Nm crunchgen +contains an optional +.Ar objs +target that will build the object files for each component program by +running make inside that program's source directory. For this to work the +.Nm srcdir +and +.Nm objs +parameters must also be valid. If they are not valid for a particular program, that +program is skipped in the +.Ar objs +target. +.Sh EXAMPLE +Here is an example +.Nm crunchgen +input conf file, named +.Dq Pa kcopy.conf : +.Pp +.nf + srcdirs /usr/src/bin /usr/src/sbin + + progs test cp echo sh fsck halt init mount umount myinstall + ln test [ # test can be invoked via [ + ln sh -sh # init invokes the shell with "-sh" in argv[0] + + special myprog objpaths /homes/leroy/src/myinstall.o # no sources + + libs -lutil -lcrypt +.fi +.Pp +This conf file specifies a small crunched binary consisting of some +basic system utilities plus a homegrown install program ``myinstall'', +for which no source directory is specified, but its object file is +specified directly with the +.Nm special +line. +.Pp +The crunched binary ``kcopy'' can be built as follows: +.Pp +.nf + % crunchgen -m Makefile kcopy.conf # gen Makefile and kcopy.c + % make objs # build the component progams' .o files + % make # build the crunched binary kcopy + % kcopy sh # test that this invokes a sh shell + $ # it works! +.fi +.Pp +At this point the binary ``kcopy'' can be copied onto an install floppy +and hard-linked to the names of the component programs. +.Sh SEE ALSO +.Xr crunchide 1 +.Sh CAVEATS +While +.Nm crunch +takes care to eliminate link conflicts between the component programs +of a crunched binary, conflicts are still possible between the +libraries that are linked in. Some shuffling in the order of +libraries may be required, and in some rare cases two libraries may +have an unresolveable conflict and thus cannot be crunched together. +.Pp +Some versions of the BSD build environment do not by default build the +intermediate object file for single-source file programs. The ``make +objs'' target must then be used to get those object files built, or +some other arrangements made. +.Sh AUTHOR +.Nm Crunch +was written by James da Silva <jds@cs.umd.edu>. +.sp 0 +Copyright (c) 1994 University of Maryland. All Rights Reserved. diff --git a/contrib/crunch/crunchgen/crunchgen.c b/contrib/crunch/crunchgen/crunchgen.c new file mode 100644 index 000000000000..6e9af1880dd9 --- /dev/null +++ b/contrib/crunch/crunchgen/crunchgen.c @@ -0,0 +1,856 @@ +/* + * Copyright (c) 1994 University of Maryland + * All Rights Reserved. + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of U.M. not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. U.M. makes no representations about the + * suitability of this software for any purpose. It is provided "as is" + * without express or implied warranty. + * + * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M. + * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Author: James da Silva, Systems Design and Analysis Group + * Computer Science Department + * University of Maryland at College Park + */ +/* + * ======================================================================== + * crunchgen.c + * + * Generates a Makefile and main C file for a crunched executable, + * from specs given in a .conf file. + */ +#include <stdlib.h> +#include <unistd.h> +#include <stdio.h> +#include <ctype.h> +#include <string.h> + +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/param.h> + +#define CRUNCH_VERSION "0.2" + +#define MAXLINELEN 16384 +#define MAXFIELDS 2048 + + +/* internal representation of conf file: */ + +/* simple lists of strings suffice for most parms */ + +typedef struct strlst { + struct strlst *next; + char *str; +} strlst_t; + +/* progs have structure, each field can be set with "special" or calculated */ + +typedef struct prog { + struct prog *next; + char *name, *ident; + char *srcdir, *objdir; + strlst_t *objs, *objpaths; + strlst_t *links; + int goterror; +} prog_t; + + +/* global state */ + +strlst_t *srcdirs = NULL; +strlst_t *libs = NULL; +prog_t *progs = NULL; + +char line[MAXLINELEN]; + +char confname[MAXPATHLEN], infilename[MAXPATHLEN]; +char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN]; +char tempfname[MAXPATHLEN], cachename[MAXPATHLEN], curfilename[MAXPATHLEN]; +int linenum = -1; +int goterror = 0; + +char *pname = "crunchgen"; + +int verbose, readcache; /* options */ +int reading_cache; + +/* general library routines */ + +void status(char *str); +void out_of_memory(void); +void add_string(strlst_t **listp, char *str); +int is_dir(char *pathname); +int is_nonempty_file(char *pathname); + +/* helper routines for main() */ + +void usage(void); +void parse_conf_file(void); +void gen_outputs(void); + + +int main(int argc, char **argv) +{ + char *p; + int optc; + extern int optind; + extern char *optarg; + + verbose = 1; + readcache = 1; + *outmkname = *outcfname = *execfname = '\0'; + + if(argc > 0) pname = argv[0]; + + while((optc = getopt(argc, argv, "m:c:e:fq")) != -1) { + switch(optc) { + case 'f': readcache = 0; break; + case 'q': verbose = 0; break; + + case 'm': strcpy(outmkname, optarg); break; + case 'c': strcpy(outcfname, optarg); break; + case 'e': strcpy(execfname, optarg); break; + + case '?': + default: usage(); + } + } + + argc -= optind; + argv += optind; + + if(argc != 1) usage(); + + /* + * generate filenames + */ + + strcpy(infilename, argv[0]); + + /* confname = `basename infilename .conf` */ + + if((p=strrchr(infilename, '/')) != NULL) strcpy(confname, p+1); + else strcpy(confname, infilename); + if((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf")) *p = '\0'; + + if(!*outmkname) sprintf(outmkname, "%s.mk", confname); + if(!*outcfname) sprintf(outcfname, "%s.c", confname); + if(!*execfname) sprintf(execfname, "%s", confname); + + sprintf(cachename, "%s.cache", confname); + sprintf(tempfname, ".tmp_%sXXXXXX", confname); + if(mktemp(tempfname) == NULL) { + perror(tempfname); + exit(1); + } + + parse_conf_file(); + gen_outputs(); + + exit(goterror); +} + + +void usage(void) +{ + fprintf(stderr, + "%s [-fq] [-m <makefile>] [-c <c file>] [-e <exec file>] <conffile>\n", + pname); + exit(1); +} + + +/* + * ======================================================================== + * parse_conf_file subsystem + * + */ + +/* helper routines for parse_conf_file */ + +void parse_one_file(char *filename); +void parse_line(char *line, int *fc, char **fv, int nf); +void add_srcdirs(int argc, char **argv); +void add_progs(int argc, char **argv); +void add_link(int argc, char **argv); +void add_libs(int argc, char **argv); +void add_special(int argc, char **argv); + +prog_t *find_prog(char *str); +void add_prog(char *progname); + + +void parse_conf_file(void) +{ + if(!is_nonempty_file(infilename)) { + fprintf(stderr, "%s: fatal: input file \"%s\" not found.\n", + pname, infilename); + exit(1); + } + parse_one_file(infilename); + if(readcache && is_nonempty_file(cachename)) { + reading_cache = 1; + parse_one_file(cachename); + } +} + + +void parse_one_file(char *filename) +{ + char *fieldv[MAXFIELDS]; + int fieldc; + void (*f)(int c, char **v); + FILE *cf; + + sprintf(line, "reading %s", filename); + status(line); + strcpy(curfilename, filename); + + if((cf = fopen(curfilename, "r")) == NULL) { + perror(curfilename); + goterror = 1; + return; + } + + linenum = 0; + while(fgets(line, MAXLINELEN, cf) != NULL) { + linenum++; + parse_line(line, &fieldc, fieldv, MAXFIELDS); + if(fieldc < 1) continue; + if(!strcmp(fieldv[0], "srcdirs")) f = add_srcdirs; + else if(!strcmp(fieldv[0], "progs")) f = add_progs; + else if(!strcmp(fieldv[0], "ln")) f = add_link; + else if(!strcmp(fieldv[0], "libs")) f = add_libs; + else if(!strcmp(fieldv[0], "special")) f = add_special; + else { + fprintf(stderr, "%s:%d: skipping unknown command `%s'.\n", + curfilename, linenum, fieldv[0]); + goterror = 1; + continue; + } + if(fieldc < 2) { + fprintf(stderr, + "%s:%d: %s command needs at least 1 argument, skipping.\n", + curfilename, linenum, fieldv[0]); + goterror = 1; + continue; + } + f(fieldc, fieldv); + } + + if(ferror(cf)) { + perror(curfilename); + goterror = 1; + } + fclose(cf); +} + + +void parse_line(char *line, int *fc, char **fv, int nf) +{ + char *p; + + p = line; + *fc = 0; + while(1) { + while(isspace(*p)) p++; + if(*p == '\0' || *p == '#') break; + + if(*fc < nf) fv[(*fc)++] = p; + while(*p && !isspace(*p) && *p != '#') p++; + if(*p == '\0' || *p == '#') break; + *p++ = '\0'; + } + if(*p) *p = '\0'; /* needed for '#' case */ +} + + +void add_srcdirs(int argc, char **argv) +{ + int i; + + for(i=1;i<argc;i++) { + if(is_dir(argv[i])) + add_string(&srcdirs, argv[i]); + else { + fprintf(stderr, "%s:%d: `%s' is not a directory, skipping it.\n", + curfilename, linenum, argv[i]); + goterror = 1; + } + } +} + + +void add_progs(int argc, char **argv) +{ + int i; + + for(i=1;i<argc;i++) + add_prog(argv[i]); +} + + +void add_prog(char *progname) +{ + prog_t *p1, *p2; + + /* add to end, but be smart about dups */ + + for(p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next) + if(!strcmp(p2->name, progname)) return; + + p2 = malloc(sizeof(prog_t)); + if(p2) p2->name = strdup(progname); + if(!p2 || !p2->name) + out_of_memory(); + + p2->next = NULL; + if(p1 == NULL) progs = p2; + else p1->next = p2; + + p2->ident = p2->srcdir = p2->objdir = NULL; + p2->links = p2->objs = NULL; + p2->goterror = 0; +} + + +void add_link(int argc, char **argv) +{ + int i; + prog_t *p = find_prog(argv[1]); + + if(p == NULL) { + fprintf(stderr, + "%s:%d: no prog %s previously declared, skipping link.\n", + curfilename, linenum, argv[1]); + goterror = 1; + return; + } + for(i=2;i<argc;i++) + add_string(&p->links, argv[i]); +} + + +void add_libs(int argc, char **argv) +{ + int i; + + for(i=1;i<argc;i++) + add_string(&libs, argv[i]); +} + + +void add_special(int argc, char **argv) +{ + int i; + prog_t *p = find_prog(argv[1]); + + if(p == NULL) { + if(reading_cache) return; + fprintf(stderr, + "%s:%d: no prog %s previously declared, skipping special.\n", + curfilename, linenum, argv[1]); + goterror = 1; + return; + } + + if(!strcmp(argv[2], "ident")) { + if(argc != 4) goto argcount; + if((p->ident = strdup(argv[3])) == NULL) + out_of_memory(); + } + else if(!strcmp(argv[2], "srcdir")) { + if(argc != 4) goto argcount; + if((p->srcdir = strdup(argv[3])) == NULL) + out_of_memory(); + } + else if(!strcmp(argv[2], "objdir")) { + if(argc != 4) goto argcount; + if((p->objdir = strdup(argv[3])) == NULL) + out_of_memory(); + } + else if(!strcmp(argv[2], "objs")) { + p->objs = NULL; + for(i=3;i<argc;i++) + add_string(&p->objs, argv[i]); + } + else if(!strcmp(argv[2], "objpaths")) { + p->objpaths = NULL; + for(i=3;i<argc;i++) + add_string(&p->objpaths, argv[i]); + } + else { + fprintf(stderr, "%s:%d: bad parameter name `%s', skipping line.\n", + curfilename, linenum, argv[2]); + goterror = 1; + } + return; + + + argcount: + fprintf(stderr, + "%s:%d: too %s arguments, expected \"special %s %s <string>\".\n", + curfilename, linenum, argc < 4? "few" : "many", argv[1], argv[2]); + goterror = 1; +} + + +prog_t *find_prog(char *str) +{ + prog_t *p; + + for(p = progs; p != NULL; p = p->next) + if(!strcmp(p->name, str)) return p; + + return NULL; +} + + +/* + * ======================================================================== + * gen_outputs subsystem + * + */ + +/* helper subroutines */ + +void remove_error_progs(void); +void fillin_program(prog_t *p); +void gen_specials_cache(void); +void gen_output_makefile(void); +void gen_output_cfile(void); + +void fillin_program_objs(prog_t *p, char *path); +void top_makefile_rules(FILE *outmk); +void prog_makefile_rules(FILE *outmk, prog_t *p); +void output_strlst(FILE *outf, strlst_t *lst); +char *genident(char *str); +char *dir_search(char *progname); + + +void gen_outputs(void) +{ + prog_t *p; + + for(p = progs; p != NULL; p = p->next) + fillin_program(p); + + remove_error_progs(); + gen_specials_cache(); + gen_output_cfile(); + gen_output_makefile(); + status(""); + fprintf(stderr, + "Run \"make -f %s objs exe\" to build crunched binary.\n", + outmkname); +} + + +void fillin_program(prog_t *p) +{ + char path[MAXPATHLEN]; + char *srcparent; + strlst_t *s; + + sprintf(line, "filling in parms for %s", p->name); + status(line); + + if(!p->ident) + p->ident = genident(p->name); + if(!p->srcdir) { + srcparent = dir_search(p->name); + if(srcparent) + sprintf(path, "%s/%s", srcparent, p->name); + if(is_dir(path)) + p->srcdir = strdup(path); + } + if(!p->objdir && p->srcdir) { + sprintf(path, "%s/obj", p->srcdir); + if(is_dir(path)) + p->objdir = strdup(path); + else + p->objdir = p->srcdir; + } + + if(p->srcdir) sprintf(path, "%s/Makefile", p->srcdir); + if(!p->objs && p->srcdir && is_nonempty_file(path)) + fillin_program_objs(p, path); + + if(!p->objpaths && p->objdir && p->objs) + for(s = p->objs; s != NULL; s = s->next) { + sprintf(line, "%s/%s", p->objdir, s->str); + add_string(&p->objpaths, line); + } + + if(!p->srcdir && verbose) + fprintf(stderr, "%s: %s: warning: could not find source directory.\n", + infilename, p->name); + if(!p->objs && verbose) + fprintf(stderr, "%s: %s: warning: could not find any .o files.\n", + infilename, p->name); + + if(!p->objpaths) { + fprintf(stderr, + "%s: %s: error: no objpaths specified or calculated.\n", + infilename, p->name); + p->goterror = goterror = 1; + } +} + +void fillin_program_objs(prog_t *p, char *path) +{ + char *obj, *cp; + int rc; + FILE *f; + + /* discover the objs from the srcdir Makefile */ + + if((f = fopen(tempfname, "w")) == NULL) { + perror(tempfname); + goterror = 1; + return; + } + + fprintf(f, ".include \"%s\"\n", path); + fprintf(f, ".if defined(PROG) && !defined(OBJS)\n"); + fprintf(f, "OBJS=${PROG}.o\n"); + fprintf(f, ".endif\n"); + fprintf(f, "crunchgen_objs:\n\t@echo 'OBJS= '${OBJS}\n"); + fclose(f); + + sprintf(line, "make -f %s crunchgen_objs 2>&1", tempfname); + if((f = popen(line, "r")) == NULL) { + perror("submake pipe"); + goterror = 1; + return; + } + + while(fgets(line, MAXLINELEN, f)) { + if(strncmp(line, "OBJS= ", 6)) { + fprintf(stderr, "make error: %s", line); + goterror = 1; + continue; + } + cp = line + 6; + while(isspace(*cp)) cp++; + while(*cp) { + obj = cp; + while(*cp && !isspace(*cp)) cp++; + if(*cp) *cp++ = '\0'; + add_string(&p->objs, obj); + while(isspace(*cp)) cp++; + } + } + if((rc=pclose(f)) != 0) { + fprintf(stderr, "make error: make returned %d\n", rc); + goterror = 1; + } + unlink(tempfname); +} + +void remove_error_progs(void) +{ + prog_t *p1, *p2; + + p1 = NULL; p2 = progs; + while(p2 != NULL) { + if(!p2->goterror) + p1 = p2, p2 = p2->next; + else { + /* delete it from linked list */ + fprintf(stderr, "%s: %s: ignoring program because of errors.\n", + infilename, p2->name); + if(p1) p1->next = p2->next; + else progs = p2->next; + p2 = p2->next; + } + } +} + +void gen_specials_cache(void) +{ + FILE *cachef; + prog_t *p; + + sprintf(line, "generating %s", cachename); + status(line); + + if((cachef = fopen(cachename, "w")) == NULL) { + perror(cachename); + goterror = 1; + return; + } + + fprintf(cachef, "# %s - parm cache generated from %s by crunchgen %s\n\n", + cachename, infilename, CRUNCH_VERSION); + + for(p = progs; p != NULL; p = p->next) { + fprintf(cachef, "\n"); + if(p->srcdir) + fprintf(cachef, "special %s srcdir %s\n", p->name, p->srcdir); + if(p->objdir) + fprintf(cachef, "special %s objdir %s\n", p->name, p->objdir); + if(p->objs) { + fprintf(cachef, "special %s objs", p->name); + output_strlst(cachef, p->objs); + } + fprintf(cachef, "special %s objpaths", p->name); + output_strlst(cachef, p->objpaths); + } + fclose(cachef); +} + + +void gen_output_makefile(void) +{ + prog_t *p; + FILE *outmk; + + sprintf(line, "generating %s", outmkname); + status(line); + + if((outmk = fopen(outmkname, "w")) == NULL) { + perror(outmkname); + goterror = 1; + return; + } + + fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n", + outmkname, infilename, CRUNCH_VERSION); + + top_makefile_rules(outmk); + + for(p = progs; p != NULL; p = p->next) + prog_makefile_rules(outmk, p); + + fprintf(outmk, "\n# ========\n"); + fclose(outmk); +} + + +void gen_output_cfile(void) +{ + extern char *crunched_skel[]; + char **cp; + FILE *outcf; + prog_t *p; + strlst_t *s; + + sprintf(line, "generating %s", outcfname); + status(line); + + if((outcf = fopen(outcfname, "w")) == NULL) { + perror(outcfname); + goterror = 1; + return; + } + + fprintf(outcf, + "/* %s - generated from %s by crunchgen %s */\n", + outcfname, infilename, CRUNCH_VERSION); + + fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname); + for(cp = crunched_skel; *cp != NULL; cp++) + fprintf(outcf, "%s\n", *cp); + + for(p = progs; p != NULL; p = p->next) + fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident); + + fprintf(outcf, "\nstruct stub entry_points[] = {\n"); + for(p = progs; p != NULL; p = p->next) { + fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n", + p->name, p->ident); + for(s = p->links; s != NULL; s = s->next) + fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n", + s->str, p->ident); + } + + fprintf(outcf, "\t{ EXECNAME, crunched_main },\n"); + fprintf(outcf, "\t{ NULL, NULL }\n};\n"); + fclose(outcf); +} + + +char *genident(char *str) +{ + char *n,*s,*d; + + /* + * generates a Makefile/C identifier from a program name, mapping '-' to + * '_' and ignoring all other non-identifier characters. This leads to + * programs named "foo.bar" and "foobar" to map to the same identifier. + */ + + if((n = strdup(str)) == NULL) + return NULL; + for(d = s = n; *s != '\0'; s++) { + if(*s == '-') *d++ = '_'; + else if(*s == '_' || isalnum(*s)) *d++ = *s; + } + *d = '\0'; + return n; +} + + +char *dir_search(char *progname) +{ + char path[MAXPATHLEN]; + strlst_t *dir; + + for(dir=srcdirs; dir != NULL; dir=dir->next) { + sprintf(path, "%s/%s", dir->str, progname); + if(is_dir(path)) return dir->str; + } + return NULL; +} + + +void top_makefile_rules(FILE *outmk) +{ + prog_t *p; + + fprintf(outmk, "LIBS="); + output_strlst(outmk, libs); + + fprintf(outmk, "CRUNCHED_OBJS="); + for(p = progs; p != NULL; p = p->next) + fprintf(outmk, " %s.lo", p->name); + fprintf(outmk, "\n"); + + fprintf(outmk, "SUBMAKE_TARGETS="); + for(p = progs; p != NULL; p = p->next) + fprintf(outmk, " %s_make", p->ident); + fprintf(outmk, "\n\n"); + + fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS)\n", + execfname, execfname); + fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n", + execfname, execfname); + fprintf(outmk, "\tstrip %s\n", execfname); + fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n"); + fprintf(outmk, "exe: %s\n", execfname); + fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", + execfname); +} + + +void prog_makefile_rules(FILE *outmk, prog_t *p) +{ + fprintf(outmk, "\n# -------- %s\n\n", p->name); + + if(p->srcdir && p->objs) { + fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir); + fprintf(outmk, "%s_OBJS=", p->ident); + output_strlst(outmk, p->objs); + fprintf(outmk, "%s_make:\n", p->ident); + fprintf(outmk, "\t(cd $(%s_SRCDIR); make $(%s_OBJS))\n\n", + p->ident, p->ident); + } + else + fprintf(outmk, "%s_make:\n\t@echo \"** cannot make objs for %s\"\n\n", + p->ident, p->name); + + fprintf(outmk, "%s_OBJPATHS=", p->ident); + output_strlst(outmk, p->objpaths); + + fprintf(outmk, "%s_stub.c:\n", p->name); + fprintf(outmk, "\techo \"" + "int _crunched_%s_stub(int argc, char **argv, char **envp)" + "{return main(argc,argv,envp);}\" >%s_stub.c\n", + p->ident, p->name); + fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)\n", + p->name, p->name, p->ident); + fprintf(outmk, "\tld -dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)\n", + p->name, p->name, p->ident); + fprintf(outmk, "\tcrunchide -k __crunched_%s_stub %s.lo\n", + p->ident, p->name); +} + +void output_strlst(FILE *outf, strlst_t *lst) +{ + for(; lst != NULL; lst = lst->next) + fprintf(outf, " %s", lst->str); + fprintf(outf, "\n"); +} + + +/* + * ======================================================================== + * general library routines + * + */ + +void status(char *str) +{ + static int lastlen = 0; + int len, spaces; + + if(!verbose) return; + + len = strlen(str); + spaces = lastlen - len; + if(spaces < 1) spaces = 1; + + fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " "); + fflush(stderr); + lastlen = len; +} + + +void out_of_memory(void) +{ + fprintf(stderr, "%s: %d: out of memory, stopping.\n", infilename, linenum); + exit(1); +} + + +void add_string(strlst_t **listp, char *str) +{ + strlst_t *p1, *p2; + + /* add to end, but be smart about dups */ + + for(p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next) + if(!strcmp(p2->str, str)) return; + + p2 = malloc(sizeof(strlst_t)); + if(p2) p2->str = strdup(str); + if(!p2 || !p2->str) + out_of_memory(); + + p2->next = NULL; + if(p1 == NULL) *listp = p2; + else p1->next = p2; +} + + +int is_dir(char *pathname) +{ + struct stat buf; + + if(stat(pathname, &buf) == -1) + return 0; + return S_ISDIR(buf.st_mode); +} + +int is_nonempty_file(char *pathname) +{ + struct stat buf; + + if(stat(pathname, &buf) == -1) + return 0; + + return S_ISREG(buf.st_mode) && buf.st_size > 0; +} diff --git a/contrib/crunch/crunchgen/mkskel.sh b/contrib/crunch/crunchgen/mkskel.sh new file mode 100755 index 000000000000..fd53d78bbbac --- /dev/null +++ b/contrib/crunch/crunchgen/mkskel.sh @@ -0,0 +1,15 @@ +#! /bin/sh +# idea and sed lines taken straight from flex + +cat <<!EOF +/* File created via mkskel.sh */ + +char *crunched_skel[] = { +!EOF + +sed 's/\\/&&/g' $* | sed 's/"/\\"/g' | sed 's/.*/ "&",/' + +cat <<!EOF + 0 +}; +!EOF |