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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)mkmakefile.c 8.1 (Berkeley) 6/6/93";
38 static const char rcsid[] =
43 * Build the makefile for the system, from
44 * the information in the files files and the
45 * additional files for the machine being compiled to.
52 #include <sys/param.h>
55 #include "configvers.h"
57 #define next_word(fp, wd) \
58 { char *word = get_word(fp); \
59 if (word == (char *)EOF) \
64 #define next_quoted_word(fp, wd) \
65 { char *word = get_quoted_word(fp); \
66 if (word == (char *)EOF) \
72 static struct file_list *fcur;
74 static char *tail(char *);
75 static void do_clean(FILE *);
76 static void do_rules(FILE *);
77 static void do_xxfiles(char *, FILE *);
78 static void do_objs(FILE *);
79 static void do_before_depend(FILE *);
80 static int opteq(const char *, const char *);
81 static void read_files(void);
84 * Lookup a file, by name.
86 static struct file_list *
91 for (fp = ftab ; fp != 0; fp = fp->f_next) {
92 if (eq(fp->f_fn, file))
99 * Make a new file list entry
101 static struct file_list *
104 struct file_list *fp;
106 fp = (struct file_list *) malloc(sizeof *fp);
107 bzero(fp, sizeof *fp);
117 * Build the makefile from the skeleton
129 snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
130 ifp = fopen(line, "r");
132 snprintf(line, sizeof(line), "Makefile.%s", machinename);
133 ifp = fopen(line, "r");
137 ofp = fopen(path("Makefile.new"), "w");
139 err(1, "%s", path("Makefile.new"));
140 fprintf(ofp, "KERN_IDENT=%s\n", raisestr(ident));
141 fprintf(ofp, "IDENT=");
143 fprintf(ofp, " -DGPROF");
146 printf("cpu type must be specified\n");
150 for (op = mkopt; op; op = op->op_next)
151 fprintf(ofp, "%s=%s\n", op->op_name, op->op_value);
153 fprintf(ofp, "DEBUG=-g\n");
155 fprintf(ofp, "PROF=-pg\n");
156 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
159 fprintf(ofp,"S=%s\n", srcdir);
160 while (fgets(line, BUFSIZ, ifp) != 0) {
162 fprintf(ofp, "%s", line);
165 if (eq(line, "%BEFORE_DEPEND\n"))
166 do_before_depend(ofp);
167 else if (eq(line, "%OBJS\n"))
169 else if (strncmp(line, "%FILES.", 7) == 0)
170 do_xxfiles(line, ofp);
171 else if (eq(line, "%RULES\n"))
173 else if (eq(line, "%CLEAN\n"))
175 else if (strncmp(line, "%VERSREQ=", sizeof("%VERSREQ=") - 1) == 0) {
176 versreq = atoi(line + sizeof("%VERSREQ=") - 1);
177 if (versreq != CONFIGVERS) {
178 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
179 fprintf(stderr, "config version = %d, ", CONFIGVERS);
180 fprintf(stderr, "version required = %d\n\n", versreq);
181 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
182 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
183 fprintf(stderr, "before trying this again.\n\n");
184 fprintf(stderr, "If running the new config fails check your config\n");
185 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
186 fprintf(stderr, "changes in config syntax, or option/device naming\n");
187 fprintf(stderr, "conventions\n\n");
192 "Unknown %% construct in generic makefile: %s",
197 moveifchanged(path("Makefile.new"), path("Makefile"));
199 /* XXX makefile() should make the Makefile, not hints.c. */
201 ifp = fopen(hints, "r");
207 ofp = fopen(path("hints.c.new"), "w");
209 err(1, "%s", path("hints.c.new"));
210 fprintf(ofp, "#include <sys/types.h>\n");
211 fprintf(ofp, "#include <sys/systm.h>\n");
213 fprintf(ofp, "int hintmode = %d;\n", hintmode);
214 fprintf(ofp, "char static_hints[] = {\n");
216 while (fgets(line, BUFSIZ, ifp) != 0) {
217 /* zap trailing CR and/or LF */
218 while ((s = rindex(line, '\n')) != NULL)
220 while ((s = rindex(line, '\r')) != NULL)
222 /* remove # comments */
223 s = index(line, '#');
226 /* remove any whitespace and " characters */
229 if (*s == ' ' || *s == '\t' || *s == '"') {
243 fprintf(ofp, "\"%s\\0\"\n", line);
246 fprintf(ofp, "\"\\0\"\n};\n");
250 moveifchanged(path("hints.c.new"), path("hints.c"));
252 /* XXX makefile() should make the Makefile, not env.c. */
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 in the information about files used in making the system.
308 * Store it in the ftab linked list.
314 struct file_list *tp, *pf;
317 char *wd, *this, *needs, *compilewith, *depends, *clean, *warning;
318 char fname[MAXPATHLEN];
319 int nreqs, first = 1, isdup, std, filetype,
320 imp_rule, no_obj, needcount, before_depend, mandatory;
324 printf("no ident line specified\n");
327 (void) snprintf(fname, sizeof(fname), "../../conf/files");
329 fp = fopen(fname, "r");
334 * filename [ standard | mandatory | optional | count ]
335 * [ dev* | profiling-routine ] [ no-obj ]
336 * [ compile-with "compile rule" [no-implicit-rule] ]
337 * [ dependency "dependency-list"] [ before-depend ]
338 * [ clean "file-list"] [ warning "text warning" ]
341 if (wd == (char *)EOF) {
345 (void) snprintf(fname, sizeof(fname),
346 "../../conf/files.%s", machinename);
347 fp = fopen(fname, "r");
350 (void) snprintf(fname, sizeof(fname),
351 "files.%s", machinename);
360 while (((wd = get_word(fp)) != (char *)EOF) && wd)
367 printf("%s: No type for %s.\n",
371 if ((pf = fl_lookup(this)) && (pf->f_type != INVISIBLE || pf->f_flags))
388 if (eq(wd, "standard")) {
391 * If an entry is marked "mandatory", config will abort if it's
392 * not called by a configuration line in the config file. Apart
393 * from this, the device is handled like one marked "optional".
395 } else if (eq(wd, "mandatory")) {
397 } else if (eq(wd, "count")) {
399 } else if (!eq(wd, "optional")) {
400 printf("%s: %s must be count, optional, mandatory or standard\n",
408 if (eq(wd, "no-obj")) {
412 if (eq(wd, "no-implicit-rule")) {
413 if (compilewith == 0) {
414 printf("%s: alternate rule required when "
415 "\"no-implicit-rule\" is specified.\n",
421 if (eq(wd, "before-depend")) {
425 if (eq(wd, "dependency")) {
426 next_quoted_word(fp, wd);
428 printf("%s: %s missing compile command string.\n",
435 if (eq(wd, "clean")) {
436 next_quoted_word(fp, wd);
438 printf("%s: %s missing clean file list.\n",
445 if (eq(wd, "compile-with")) {
446 next_quoted_word(fp, wd);
448 printf("%s: %s missing compile command string.\n",
452 compilewith = ns(wd);
455 if (eq(wd, "warning")) {
456 next_quoted_word(fp, wd);
458 printf("%s: %s missing warning text string.\n",
466 if (eq(wd, "local")) {
470 if (eq(wd, "no-depend")) {
474 if (eq(wd, "profiling-routine")) {
475 filetype = PROFILING;
478 if (needs == 0 && nreqs == 1)
482 for (dp = dtab; dp != 0; dp = dp->d_next)
483 if (eq(dp->d_name, wd)) {
484 if (std && dp->d_count <= 0)
489 printf("%s: mandatory device \"%s\" not found\n",
494 printf("standard entry %s has a device keyword - %s!\n",
498 for (op = opt; op != 0; op = op->op_next)
499 if (op->op_value == 0 && opteq(op->op_name, wd)) {
507 while ((wd = get_word(fp)) != 0)
512 tp->f_type = INVISIBLE;
514 tp->f_flags |= isdup;
516 tp->f_flags |= NEED_COUNT;
517 tp->f_compilewith = compilewith;
518 tp->f_depends = depends;
520 tp->f_warn = warning;
524 if (std == 0 && nreqs == 0) {
525 printf("%s: what is %s optional on?\n",
531 printf("%s: syntax error describing %s\n",
535 if (filetype == PROFILING && profiling == 0)
540 tp->f_type = filetype;
541 tp->f_flags &= ~ISDUP;
543 tp->f_flags |= NO_IMPLCT_RULE;
545 tp->f_flags |= NO_OBJ;
547 tp->f_flags |= BEFORE_DEPEND;
549 tp->f_flags |= NEED_COUNT;
551 tp->f_compilewith = compilewith;
552 tp->f_depends = depends;
554 tp->f_warn = warning;
555 if (pf && pf->f_type == INVISIBLE)
556 pf->f_flags |= ISDUP; /* mark as duplicate */
561 opteq(const char *cp, const char *dp)
565 for (; ; cp++, dp++) {
567 c = isupper(*cp) ? tolower(*cp) : *cp;
568 d = isupper(*dp) ? tolower(*dp) : *dp;
578 do_before_depend(FILE *fp)
580 struct file_list *tp;
583 fputs("BEFORE_DEPEND=", fp);
585 for (tp = ftab; tp; tp = tp->f_next)
586 if (tp->f_flags & BEFORE_DEPEND) {
587 len = strlen(tp->f_fn);
588 if ((len = 3 + len) + lpos > 72) {
592 if (tp->f_flags & NO_IMPLCT_RULE)
593 fprintf(fp, "%s ", tp->f_fn);
595 fprintf(fp, "$S/%s ", tp->f_fn);
605 struct file_list *tp;
609 fprintf(fp, "OBJS=");
611 for (tp = ftab; tp != 0; tp = tp->f_next) {
612 if (tp->f_type == INVISIBLE || tp->f_flags & NO_OBJ)
615 cp = sp + (len = strlen(sp)) - 1;
618 if (len + lpos > 72) {
620 fprintf(fp, "\\\n\t");
622 fprintf(fp, "%s ", sp);
631 do_xxfiles(char *tag, FILE *fp)
633 struct file_list *tp;
637 if (tag[strlen(tag) - 1] == '\n')
638 tag[strlen(tag) - 1] = '\0';
645 fprintf(fp, "%sFILES=", SUFF);
647 for (tp = ftab; tp; tp = tp->f_next)
648 if (tp->f_type != INVISIBLE && tp->f_type != NODEPEND) {
649 len = strlen(tp->f_fn);
650 if (tp->f_fn[len - slen - 1] != '.')
652 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
654 if ((len = 3 + len) + lpos > 72) {
658 if (tp->f_type != LOCAL)
659 fprintf(fp, "$S/%s ", tp->f_fn);
661 fprintf(fp, "%s ", tp->f_fn);
673 cp = rindex(fn, '/');
680 * Create the makerules for each file
681 * which is part of the system.
686 char *cp, *np, och, *tp;
687 struct file_list *ftp;
690 for (ftp = ftab; ftp != 0; ftp = ftp->f_next) {
691 if (ftp->f_type == INVISIBLE)
694 printf("WARNING: %s\n", ftp->f_warn);
695 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
697 if (ftp->f_flags & NO_IMPLCT_RULE) {
699 fprintf(f, "%s: %s\n", np, ftp->f_depends);
701 fprintf(f, "%s: \n", np);
706 fprintf(f, "%so:\n\t-cp $S/%so .\n\n",
711 fprintf(f, "%so: $S/%s%c %s\n", tail(np),
712 np, och, ftp->f_depends);
714 fprintf(f, "%so: $S/%s%c\n", tail(np),
718 compilewith = ftp->f_compilewith;
719 if (compilewith == 0) {
720 const char *ftype = NULL;
721 static char cmd[128];
723 switch (ftp->f_type) {
736 printf("config: don't know rules for %s\n", np);
739 snprintf(cmd, sizeof(cmd), "${%s_%c}", ftype,
744 fprintf(f, "\t%s\n\n", compilewith);
751 struct file_list *tp;
756 for (tp = ftab; tp; tp = tp->f_next)
758 len = strlen(tp->f_clean);
759 if (len + lpos > 72) {
763 fprintf(fp, "%s ", tp->f_clean);
777 *str = toupper(*str);