2 * Copyright (c) 1993, 19801990
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)mkmakefile.c 8.1 (Berkeley) 6/6/93";
34 static const char rcsid[] =
39 * Build the makefile for the system, from
40 * the information in the files files and the
41 * additional files for the machine being compiled to.
48 #include <sys/param.h>
51 #include "configvers.h"
53 #define next_word(fp, wd) \
54 { char *word = get_word(fp); \
55 if (word == (char *)EOF) \
60 #define next_quoted_word(fp, wd) \
61 { char *word = get_quoted_word(fp); \
62 if (word == (char *)EOF) \
68 static char *tail(char *);
69 static void do_clean(FILE *);
70 static void do_rules(FILE *);
71 static void do_xxfiles(char *, FILE *);
72 static void do_objs(FILE *);
73 static void do_before_depend(FILE *);
74 static int opteq(const char *, const char *);
75 static void read_files(void);
78 * Lookup a file, by name.
80 static struct file_list *
85 STAILQ_FOREACH(fp, &ftab, f_next) {
86 if (eq(fp->f_fn, file))
93 * Make a new file list entry
95 static struct file_list *
100 fp = (struct file_list *) calloc(1, sizeof *fp);
102 err(EXIT_FAILURE, "calloc");
103 STAILQ_INSERT_TAIL(&ftab, fp, f_next);
108 * Open the correct Makefile and return it, or error out.
111 open_makefile_template(void)
116 snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
117 ifp = fopen(line, "r");
119 snprintf(line, sizeof(line), "Makefile.%s", machinename);
120 ifp = fopen(line, "r");
128 * Build the makefile from the skeleton
138 ifp = open_makefile_template();
139 ofp = fopen(path("Makefile.new"), "w");
141 err(1, "%s", path("Makefile.new"));
142 fprintf(ofp, "KERN_IDENT=%s\n", ident);
143 fprintf(ofp, "MACHINE=%s\n", machinename);
144 fprintf(ofp, "MACHINE_ARCH=%s\n", machinearch);
145 SLIST_FOREACH_SAFE(op, &mkopt, op_next, t) {
146 fprintf(ofp, "%s=%s", op->op_name, op->op_value);
147 while ((op = SLIST_NEXT(op, op_append)) != NULL)
148 fprintf(ofp, " %s", op->op_value);
152 fprintf(ofp, "DEBUG=-g\n");
154 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
156 fprintf(ofp,"S=%s\n", srcdir);
157 while (fgets(line, BUFSIZ, ifp) != 0) {
159 fprintf(ofp, "%s", line);
162 if (eq(line, "%BEFORE_DEPEND\n"))
163 do_before_depend(ofp);
164 else if (eq(line, "%OBJS\n"))
166 else if (strncmp(line, "%FILES.", 7) == 0)
167 do_xxfiles(line, ofp);
168 else if (eq(line, "%RULES\n"))
170 else if (eq(line, "%CLEAN\n"))
172 else if (strncmp(line, "%VERSREQ=", 9) == 0)
173 line[0] = '\0'; /* handled elsewhere */
176 "Unknown %% construct in generic makefile: %s",
181 moveifchanged(path("Makefile.new"), path("Makefile"));
185 * Build hints.c from the skeleton
195 ofp = fopen(path("hints.c.new"), "w");
197 err(1, "%s", path("hints.c.new"));
198 fprintf(ofp, "#include <sys/types.h>\n");
199 fprintf(ofp, "#include <sys/systm.h>\n");
201 fprintf(ofp, "int hintmode = %d;\n", hintmode);
202 fprintf(ofp, "char static_hints[] = {\n");
203 STAILQ_FOREACH(hint, &hints, hint_next) {
204 ifp = fopen(hint->hint_name, "r");
206 err(1, "%s", hint->hint_name);
207 while (fgets(line, BUFSIZ, ifp) != 0) {
208 /* zap trailing CR and/or LF */
209 while ((s = rindex(line, '\n')) != NULL)
211 while ((s = rindex(line, '\r')) != NULL)
213 /* remove # comments */
214 s = index(line, '#');
217 /* remove any whitespace and " characters */
220 if (*s == ' ' || *s == '\t' || *s == '"') {
234 fprintf(ofp, "\"%s\\0\"\n", line);
238 fprintf(ofp, "\"\\0\"\n};\n");
240 moveifchanged(path("hints.c.new"), path("hints.c"));
244 * Build env.c from the skeleton
254 ifp = fopen(env, "r");
260 ofp = fopen(path("env.c.new"), "w");
262 err(1, "%s", path("env.c.new"));
263 fprintf(ofp, "#include <sys/types.h>\n");
264 fprintf(ofp, "#include <sys/systm.h>\n");
266 fprintf(ofp, "int envmode = %d;\n", envmode);
267 fprintf(ofp, "char static_env[] = {\n");
269 while (fgets(line, BUFSIZ, ifp) != 0) {
270 /* zap trailing CR and/or LF */
271 while ((s = rindex(line, '\n')) != NULL)
273 while ((s = rindex(line, '\r')) != NULL)
275 /* remove # comments */
276 s = index(line, '#');
279 /* remove any whitespace and " characters */
282 if (*s == ' ' || *s == '\t' || *s == '"') {
296 fprintf(ofp, "\"%s\\0\"\n", line);
299 fprintf(ofp, "\"\\0\"\n};\n");
303 moveifchanged(path("env.c.new"), path("env.c"));
307 read_file(char *fname)
309 char ifname[MAXPATHLEN];
311 struct file_list *tp;
314 char *wd, *this, *compilewith, *depends, *clean, *warning;
315 const char *objprefix;
316 int compile, match, nreqs, std, filetype,
317 imp_rule, no_obj, before_depend, mandatory, nowerror;
319 fp = fopen(fname, "r");
325 * filename [ standard | mandatory | optional ]
326 * [ dev* [ | dev* ... ] | profiling-routine ] [ no-obj ]
327 * [ compile-with "compile rule" [no-implicit-rule] ]
328 * [ dependency "dependency-list"] [ before-depend ]
329 * [ clean "file-list"] [ warning "text warning" ]
330 * [ obj-prefix "file prefix"]
333 if (wd == (char *)EOF) {
341 while (((wd = get_word(fp)) != (char *)EOF) && wd)
345 if (eq(wd, "include")) {
346 next_quoted_word(fp, wd);
348 fprintf(stderr, "%s: missing include filename.\n",
352 (void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
354 while (((wd = get_word(fp)) != (char *)EOF) && wd)
361 fprintf(stderr, "%s: No type for %s.\n", fname, this);
364 tp = fl_lookup(this);
379 if (eq(wd, "standard")) {
382 * If an entry is marked "mandatory", config will abort if it's
383 * not called by a configuration line in the config file. Apart
384 * from this, the device is handled like one marked "optional".
386 } else if (eq(wd, "mandatory")) {
388 } else if (!eq(wd, "optional")) {
390 "%s: \"%s\" %s must be optional, mandatory or standard\n",
398 if (compile && tp == NULL)
404 fprintf(stderr, "%s: syntax error describing %s\n",
413 if (eq(wd, "no-obj")) {
417 if (eq(wd, "no-implicit-rule")) {
418 if (compilewith == 0) {
419 fprintf(stderr, "%s: alternate rule required when "
420 "\"no-implicit-rule\" is specified.\n",
426 if (eq(wd, "before-depend")) {
430 if (eq(wd, "dependency")) {
431 next_quoted_word(fp, wd);
434 "%s: %s missing dependency string.\n",
441 if (eq(wd, "clean")) {
442 next_quoted_word(fp, wd);
444 fprintf(stderr, "%s: %s missing clean file list.\n",
451 if (eq(wd, "compile-with")) {
452 next_quoted_word(fp, wd);
455 "%s: %s missing compile command string.\n",
459 compilewith = ns(wd);
462 if (eq(wd, "warning")) {
463 next_quoted_word(fp, wd);
466 "%s: %s missing warning text string.\n",
473 if (eq(wd, "obj-prefix")) {
474 next_quoted_word(fp, wd);
476 printf("%s: %s missing object prefix string.\n",
484 if (eq(wd, "local")) {
488 if (eq(wd, "no-depend")) {
492 if (eq(wd, "profiling-routine")) {
493 filetype = PROFILING;
496 if (eq(wd, "nowerror")) {
500 STAILQ_FOREACH(dp, &dtab, d_next)
501 if (eq(dp->d_name, wd)) {
502 dp->d_done |= DEVDONE;
506 fprintf(stderr, "%s: mandatory device \"%s\" not found\n",
512 "standard entry %s has a device keyword - %s!\n",
516 SLIST_FOREACH(op, &opt, op_next)
517 if (op->op_value == 0 && opteq(op->op_name, wd))
523 if (std == 0 && nreqs == 0) {
524 fprintf(stderr, "%s: what is %s optional on?\n",
530 fprintf(stderr, "%s: syntax error describing %s\n",
534 if (filetype == PROFILING && profiling == 0)
538 tp->f_type = filetype;
540 tp->f_flags |= NO_IMPLCT_RULE;
542 tp->f_flags |= NO_OBJ;
544 tp->f_flags |= BEFORE_DEPEND;
546 tp->f_flags |= NOWERROR;
547 tp->f_compilewith = compilewith;
548 tp->f_depends = depends;
550 tp->f_warn = warning;
551 tp->f_objprefix = objprefix;
556 * Read in the information about files used in making the system.
557 * Store it in the ftab linked list.
562 char fname[MAXPATHLEN];
563 struct files_name *nl, *tnl;
565 (void) snprintf(fname, sizeof(fname), "../../conf/files");
567 (void) snprintf(fname, sizeof(fname),
568 "../../conf/files.%s", machinename);
570 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
571 read_file(nl->f_name);
572 tnl = STAILQ_NEXT(nl, f_next);
579 opteq(const char *cp, const char *dp)
583 for (; ; cp++, dp++) {
585 c = isupper(*cp) ? tolower(*cp) : *cp;
586 d = isupper(*dp) ? tolower(*dp) : *dp;
596 do_before_depend(FILE *fp)
598 struct file_list *tp;
601 fputs("BEFORE_DEPEND=", fp);
603 STAILQ_FOREACH(tp, &ftab, f_next)
604 if (tp->f_flags & BEFORE_DEPEND) {
605 len = strlen(tp->f_fn);
606 if ((len = 3 + len) + lpos > 72) {
610 if (tp->f_flags & NO_IMPLCT_RULE)
611 fprintf(fp, "%s ", tp->f_fn);
613 fprintf(fp, "$S/%s ", tp->f_fn);
623 struct file_list *tp;
627 fprintf(fp, "OBJS=");
629 STAILQ_FOREACH(tp, &ftab, f_next) {
630 if (tp->f_flags & NO_OBJ)
633 cp = sp + (len = strlen(sp)) - 1;
636 len += strlen(tp->f_objprefix);
637 if (len + lpos > 72) {
639 fprintf(fp, "\\\n\t");
641 fprintf(fp, "%s%s ", tp->f_objprefix, sp);
650 do_xxfiles(char *tag, FILE *fp)
652 struct file_list *tp;
656 if (tag[strlen(tag) - 1] == '\n')
657 tag[strlen(tag) - 1] = '\0';
664 fprintf(fp, "%sFILES=", SUFF);
666 STAILQ_FOREACH(tp, &ftab, f_next)
667 if (tp->f_type != NODEPEND) {
668 len = strlen(tp->f_fn);
669 if (tp->f_fn[len - slen - 1] != '.')
671 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
673 if ((len = 3 + len) + lpos > 72) {
677 if (tp->f_type != LOCAL)
678 fprintf(fp, "$S/%s ", tp->f_fn);
680 fprintf(fp, "%s ", tp->f_fn);
692 cp = rindex(fn, '/');
699 * Create the makerules for each file
700 * which is part of the system.
706 struct file_list *ftp;
710 STAILQ_FOREACH(ftp, &ftab, f_next) {
712 fprintf(stderr, "WARNING: %s\n", ftp->f_warn);
713 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
715 if (ftp->f_flags & NO_IMPLCT_RULE) {
717 fprintf(f, "%s%s: %s\n",
718 ftp->f_objprefix, np, ftp->f_depends);
720 fprintf(f, "%s%s: \n", ftp->f_objprefix, np);
725 fprintf(f, "%s%so:\n\t-cp $S/%so .\n\n",
726 ftp->f_objprefix, tail(np), np);
729 if (ftp->f_depends) {
730 fprintf(f, "%s%sln: $S/%s%c %s\n",
731 ftp->f_objprefix, tail(np), np, och,
733 fprintf(f, "\t${NORMAL_LINT}\n\n");
734 fprintf(f, "%s%so: $S/%s%c %s\n",
735 ftp->f_objprefix, tail(np), np, och,
739 fprintf(f, "%s%sln: $S/%s%c\n",
740 ftp->f_objprefix, tail(np), np, och);
741 fprintf(f, "\t${NORMAL_LINT}\n\n");
742 fprintf(f, "%s%so: $S/%s%c\n",
743 ftp->f_objprefix, tail(np), np, och);
746 compilewith = ftp->f_compilewith;
747 if (compilewith == 0) {
748 const char *ftype = NULL;
750 switch (ftp->f_type) {
761 "config: don't know rules for %s\n", np);
764 snprintf(cmd, sizeof(cmd),
767 ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
771 if (strlen(ftp->f_objprefix))
772 fprintf(f, "\t%s $S/%s\n", compilewith, np);
774 fprintf(f, "\t%s\n", compilewith);
776 if (!(ftp->f_flags & NO_OBJ))
777 fprintf(f, "\t${NORMAL_CTFCONVERT}\n\n");
786 struct file_list *tp;
791 STAILQ_FOREACH(tp, &ftab, f_next)
793 len = strlen(tp->f_clean);
794 if (len + lpos > 72) {
798 fprintf(fp, "%s ", tp->f_clean);
812 *str = toupper(*str);