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 char *tail(char *);
73 static void do_clean(FILE *);
74 static void do_rules(FILE *);
75 static void do_xxfiles(char *, FILE *);
76 static void do_objs(FILE *);
77 static void do_before_depend(FILE *);
78 static int opteq(const char *, const char *);
79 static void read_files(void);
82 * Lookup a file, by name.
84 static struct file_list *
89 STAILQ_FOREACH(fp, &ftab, f_next) {
90 if (eq(fp->f_fn, file))
97 * Make a new file list entry
99 static struct file_list *
102 struct file_list *fp;
104 fp = (struct file_list *) malloc(sizeof *fp);
105 bzero(fp, sizeof *fp);
106 STAILQ_INSERT_TAIL(&ftab, fp, f_next);
111 * Build the makefile from the skeleton
123 snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
124 ifp = fopen(line, "r");
126 snprintf(line, sizeof(line), "Makefile.%s", machinename);
127 ifp = fopen(line, "r");
132 /* XXX this check seems to be misplaced. */
133 if (SLIST_EMPTY(&cputype)) {
134 printf("cpu type must be specified\n");
138 ofp = fopen(path("Makefile.new"), "w");
140 err(1, "%s", path("Makefile.new"));
141 fprintf(ofp, "KERN_IDENT=%s\n", ident);
142 SLIST_FOREACH(op, &mkopt, op_next)
143 fprintf(ofp, "%s=%s\n", op->op_name, op->op_value);
145 fprintf(ofp, "DEBUG=-g\n");
147 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
149 fprintf(ofp,"S=%s\n", srcdir);
150 while (fgets(line, BUFSIZ, ifp) != 0) {
152 fprintf(ofp, "%s", line);
155 if (eq(line, "%BEFORE_DEPEND\n"))
156 do_before_depend(ofp);
157 else if (eq(line, "%OBJS\n"))
159 else if (strncmp(line, "%FILES.", 7) == 0)
160 do_xxfiles(line, ofp);
161 else if (eq(line, "%RULES\n"))
163 else if (eq(line, "%CLEAN\n"))
165 else if (strncmp(line, "%VERSREQ=", sizeof("%VERSREQ=") - 1) == 0) {
166 versreq = atoi(line + sizeof("%VERSREQ=") - 1);
167 if (versreq != CONFIGVERS) {
168 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
169 fprintf(stderr, "config version = %d, ", CONFIGVERS);
170 fprintf(stderr, "version required = %d\n\n", versreq);
171 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
172 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
173 fprintf(stderr, "before trying this again.\n\n");
174 fprintf(stderr, "If running the new config fails check your config\n");
175 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
176 fprintf(stderr, "changes in config syntax, or option/device naming\n");
177 fprintf(stderr, "conventions\n\n");
182 "Unknown %% construct in generic makefile: %s",
187 moveifchanged(path("Makefile.new"), path("Makefile"));
189 /* XXX makefile() should make the Makefile, not hints.c. */
191 ifp = fopen(hints, "r");
197 ofp = fopen(path("hints.c.new"), "w");
199 err(1, "%s", path("hints.c.new"));
200 fprintf(ofp, "#include <sys/types.h>\n");
201 fprintf(ofp, "#include <sys/systm.h>\n");
203 fprintf(ofp, "int hintmode = %d;\n", hintmode);
204 fprintf(ofp, "char static_hints[] = {\n");
206 while (fgets(line, BUFSIZ, ifp) != 0) {
207 /* zap trailing CR and/or LF */
208 while ((s = rindex(line, '\n')) != NULL)
210 while ((s = rindex(line, '\r')) != NULL)
212 /* remove # comments */
213 s = index(line, '#');
216 /* remove any whitespace and " characters */
219 if (*s == ' ' || *s == '\t' || *s == '"') {
233 fprintf(ofp, "\"%s\\0\"\n", line);
236 fprintf(ofp, "\"\\0\"\n};\n");
240 moveifchanged(path("hints.c.new"), path("hints.c"));
242 /* XXX makefile() should make the Makefile, not env.c. */
244 ifp = fopen(env, "r");
250 ofp = fopen(path("env.c.new"), "w");
252 err(1, "%s", path("env.c.new"));
253 fprintf(ofp, "#include <sys/types.h>\n");
254 fprintf(ofp, "#include <sys/systm.h>\n");
256 fprintf(ofp, "int envmode = %d;\n", envmode);
257 fprintf(ofp, "char static_env[] = {\n");
259 while (fgets(line, BUFSIZ, ifp) != 0) {
260 /* zap trailing CR and/or LF */
261 while ((s = rindex(line, '\n')) != NULL)
263 while ((s = rindex(line, '\r')) != NULL)
265 /* remove # comments */
266 s = index(line, '#');
269 /* remove any whitespace and " characters */
272 if (*s == ' ' || *s == '\t' || *s == '"') {
286 fprintf(ofp, "\"%s\\0\"\n", line);
289 fprintf(ofp, "\"\\0\"\n};\n");
293 moveifchanged(path("env.c.new"), path("env.c"));
297 read_file(char *fname)
300 struct file_list *tp, *pf;
303 char *wd, *this, *needs, *compilewith, *depends, *clean, *warning;
304 int nreqs, isdup, std, filetype,
305 imp_rule, no_obj, needcount, before_depend, mandatory, nowerror;
307 fp = fopen(fname, "r");
312 * filename [ standard | mandatory | optional | count ]
313 * [ dev* | profiling-routine ] [ no-obj ]
314 * [ compile-with "compile rule" [no-implicit-rule] ]
315 * [ dependency "dependency-list"] [ before-depend ]
316 * [ clean "file-list"] [ warning "text warning" ]
319 if (wd == (char *)EOF) {
327 while (((wd = get_word(fp)) != (char *)EOF) && wd)
334 printf("%s: No type for %s.\n",
338 if ((pf = fl_lookup(this)) && (pf->f_type != INVISIBLE || pf->f_flags))
356 if (eq(wd, "standard")) {
359 * If an entry is marked "mandatory", config will abort if it's
360 * not called by a configuration line in the config file. Apart
361 * from this, the device is handled like one marked "optional".
363 } else if (eq(wd, "mandatory")) {
365 } else if (eq(wd, "count")) {
367 } else if (!eq(wd, "optional")) {
368 printf("%s: %s must be count, optional, mandatory or standard\n",
379 if (eq(wd, "no-obj")) {
383 if (eq(wd, "no-implicit-rule")) {
384 if (compilewith == 0) {
385 printf("%s: alternate rule required when "
386 "\"no-implicit-rule\" is specified.\n",
392 if (eq(wd, "before-depend")) {
396 if (eq(wd, "dependency")) {
397 next_quoted_word(fp, wd);
399 printf("%s: %s missing compile command string.\n",
406 if (eq(wd, "clean")) {
407 next_quoted_word(fp, wd);
409 printf("%s: %s missing clean file list.\n",
416 if (eq(wd, "compile-with")) {
417 next_quoted_word(fp, wd);
419 printf("%s: %s missing compile command string.\n",
423 compilewith = ns(wd);
426 if (eq(wd, "warning")) {
427 next_quoted_word(fp, wd);
429 printf("%s: %s missing warning text string.\n",
437 if (eq(wd, "local")) {
441 if (eq(wd, "no-depend")) {
445 if (eq(wd, "profiling-routine")) {
446 filetype = PROFILING;
449 if (eq(wd, "nowerror")) {
453 if (needs == 0 && nreqs == 1)
457 STAILQ_FOREACH(dp, &dtab, d_next)
458 if (eq(dp->d_name, wd)) {
459 if (std && dp->d_count <= 0)
464 printf("%s: mandatory device \"%s\" not found\n",
469 printf("standard entry %s has a device keyword - %s!\n",
473 SLIST_FOREACH(op, &opt, op_next)
474 if (op->op_value == 0 && opteq(op->op_name, wd)) {
482 while ((wd = get_word(fp)) != 0)
487 tp->f_type = INVISIBLE;
489 tp->f_flags |= isdup;
491 tp->f_flags |= NEED_COUNT;
492 tp->f_compilewith = compilewith;
493 tp->f_depends = depends;
495 tp->f_warn = warning;
499 if (std == 0 && nreqs == 0) {
500 printf("%s: what is %s optional on?\n",
506 printf("%s: syntax error describing %s\n",
510 if (filetype == PROFILING && profiling == 0)
515 tp->f_type = filetype;
516 tp->f_flags &= ~ISDUP;
518 tp->f_flags |= NO_IMPLCT_RULE;
520 tp->f_flags |= NO_OBJ;
522 tp->f_flags |= BEFORE_DEPEND;
524 tp->f_flags |= NEED_COUNT;
526 tp->f_flags |= NOWERROR;
528 tp->f_compilewith = compilewith;
529 tp->f_depends = depends;
531 tp->f_warn = warning;
532 if (pf && pf->f_type == INVISIBLE)
533 pf->f_flags |= ISDUP; /* mark as duplicate */
538 * Read in the information about files used in making the system.
539 * Store it in the ftab linked list.
544 char fname[MAXPATHLEN];
545 struct files_name *nl, *tnl;
548 printf("no ident line specified\n");
551 (void) snprintf(fname, sizeof(fname), "../../conf/files");
553 (void) snprintf(fname, sizeof(fname),
554 "../../conf/files.%s", machinename);
556 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
557 read_file(nl->f_name);
558 tnl = STAILQ_NEXT(nl, f_next);
565 opteq(const char *cp, const char *dp)
569 for (; ; cp++, dp++) {
571 c = isupper(*cp) ? tolower(*cp) : *cp;
572 d = isupper(*dp) ? tolower(*dp) : *dp;
582 do_before_depend(FILE *fp)
584 struct file_list *tp;
587 fputs("BEFORE_DEPEND=", fp);
589 STAILQ_FOREACH(tp, &ftab, f_next)
590 if (tp->f_flags & BEFORE_DEPEND) {
591 len = strlen(tp->f_fn);
592 if ((len = 3 + len) + lpos > 72) {
596 if (tp->f_flags & NO_IMPLCT_RULE)
597 fprintf(fp, "%s ", tp->f_fn);
599 fprintf(fp, "$S/%s ", tp->f_fn);
609 struct file_list *tp;
613 fprintf(fp, "OBJS=");
615 STAILQ_FOREACH(tp, &ftab, f_next) {
616 if (tp->f_type == INVISIBLE || tp->f_flags & NO_OBJ)
619 cp = sp + (len = strlen(sp)) - 1;
622 if (len + lpos > 72) {
624 fprintf(fp, "\\\n\t");
626 fprintf(fp, "%s ", sp);
635 do_xxfiles(char *tag, FILE *fp)
637 struct file_list *tp;
641 if (tag[strlen(tag) - 1] == '\n')
642 tag[strlen(tag) - 1] = '\0';
649 fprintf(fp, "%sFILES=", SUFF);
651 STAILQ_FOREACH(tp, &ftab, f_next)
652 if (tp->f_type != INVISIBLE && tp->f_type != NODEPEND) {
653 len = strlen(tp->f_fn);
654 if (tp->f_fn[len - slen - 1] != '.')
656 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
658 if ((len = 3 + len) + lpos > 72) {
662 if (tp->f_type != LOCAL)
663 fprintf(fp, "$S/%s ", tp->f_fn);
665 fprintf(fp, "%s ", tp->f_fn);
677 cp = rindex(fn, '/');
684 * Create the makerules for each file
685 * which is part of the system.
690 char *cp, *np, och, *tp;
691 struct file_list *ftp;
694 STAILQ_FOREACH(ftp, &ftab, f_next) {
695 if (ftp->f_type == INVISIBLE)
698 printf("WARNING: %s\n", ftp->f_warn);
699 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
701 if (ftp->f_flags & NO_IMPLCT_RULE) {
703 fprintf(f, "%s: %s\n", np, ftp->f_depends);
705 fprintf(f, "%s: \n", np);
710 fprintf(f, "%so:\n\t-cp $S/%so .\n\n",
714 if (ftp->f_depends) {
715 fprintf(f, "%sln: $S/%s%c %s\n", tail(np),
716 np, och, ftp->f_depends);
717 fprintf(f, "\t${NORMAL_LINT}\n\n");
718 fprintf(f, "%so: $S/%s%c %s\n", tail(np),
719 np, och, ftp->f_depends);
722 fprintf(f, "%sln: $S/%s%c\n", tail(np),
724 fprintf(f, "\t${NORMAL_LINT}\n\n");
725 fprintf(f, "%so: $S/%s%c\n", tail(np),
730 compilewith = ftp->f_compilewith;
731 if (compilewith == 0) {
732 const char *ftype = NULL;
733 static char cmd[128];
735 switch (ftp->f_type) {
748 printf("config: don't know rules for %s\n", np);
751 snprintf(cmd, sizeof(cmd), "${%s_%c%s}", ftype,
753 ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
757 fprintf(f, "\t%s\n\n", compilewith);
764 struct file_list *tp;
769 STAILQ_FOREACH(tp, &ftab, f_next)
771 len = strlen(tp->f_clean);
772 if (len + lpos > 72) {
776 fprintf(fp, "%s ", tp->f_clean);
790 *str = toupper(*str);