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);
101 STAILQ_INSERT_TAIL(&ftab, fp, f_next);
106 * Build the makefile from the skeleton
117 snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
118 ifp = fopen(line, "r");
120 snprintf(line, sizeof(line), "Makefile.%s", machinename);
121 ifp = fopen(line, "r");
126 ofp = fopen(path("Makefile.new"), "w");
128 err(1, "%s", path("Makefile.new"));
129 fprintf(ofp, "KERN_IDENT=%s\n", ident);
130 SLIST_FOREACH_SAFE(op, &mkopt, op_next, t) {
131 fprintf(ofp, "%s=%s", op->op_name, op->op_value);
132 while ((op = SLIST_NEXT(op, op_append)) != NULL)
133 fprintf(ofp, " %s", op->op_value);
137 fprintf(ofp, "DEBUG=-g\n");
139 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
141 fprintf(ofp,"S=%s\n", srcdir);
142 while (fgets(line, BUFSIZ, ifp) != 0) {
144 fprintf(ofp, "%s", line);
147 if (eq(line, "%BEFORE_DEPEND\n"))
148 do_before_depend(ofp);
149 else if (eq(line, "%OBJS\n"))
151 else if (strncmp(line, "%FILES.", 7) == 0)
152 do_xxfiles(line, ofp);
153 else if (eq(line, "%RULES\n"))
155 else if (eq(line, "%CLEAN\n"))
157 else if (strncmp(line, "%VERSREQ=", sizeof("%VERSREQ=") - 1) == 0) {
158 versreq = atoi(line + sizeof("%VERSREQ=") - 1);
159 if (MAJOR_VERS(versreq) != MAJOR_VERS(CONFIGVERS) ||
160 versreq > CONFIGVERS) {
161 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
162 fprintf(stderr, "config version = %d, ", CONFIGVERS);
163 fprintf(stderr, "version required = %d\n\n", versreq);
164 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
165 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
166 fprintf(stderr, "before trying this again.\n\n");
167 fprintf(stderr, "If running the new config fails check your config\n");
168 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
169 fprintf(stderr, "changes in config syntax, or option/device naming\n");
170 fprintf(stderr, "conventions\n\n");
175 "Unknown %% construct in generic makefile: %s",
180 moveifchanged(path("Makefile.new"), path("Makefile"));
184 * Build hints.c from the skeleton
194 ofp = fopen(path("hints.c.new"), "w");
196 err(1, "%s", path("hints.c.new"));
197 fprintf(ofp, "#include <sys/types.h>\n");
198 fprintf(ofp, "#include <sys/systm.h>\n");
200 fprintf(ofp, "int hintmode = %d;\n", hintmode);
201 fprintf(ofp, "char static_hints[] = {\n");
202 STAILQ_FOREACH(hint, &hints, hint_next) {
203 ifp = fopen(hint->hint_name, "r");
205 err(1, "%s", hint->hint_name);
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);
237 fprintf(ofp, "\"\\0\"\n};\n");
239 moveifchanged(path("hints.c.new"), path("hints.c"));
243 * Build env.c from the skeleton
253 ifp = fopen(env, "r");
259 ofp = fopen(path("env.c.new"), "w");
261 err(1, "%s", path("env.c.new"));
262 fprintf(ofp, "#include <sys/types.h>\n");
263 fprintf(ofp, "#include <sys/systm.h>\n");
265 fprintf(ofp, "int envmode = %d;\n", envmode);
266 fprintf(ofp, "char static_env[] = {\n");
268 while (fgets(line, BUFSIZ, ifp) != 0) {
269 /* zap trailing CR and/or LF */
270 while ((s = rindex(line, '\n')) != NULL)
272 while ((s = rindex(line, '\r')) != NULL)
274 /* remove # comments */
275 s = index(line, '#');
278 /* remove any whitespace and " characters */
281 if (*s == ' ' || *s == '\t' || *s == '"') {
295 fprintf(ofp, "\"%s\\0\"\n", line);
298 fprintf(ofp, "\"\\0\"\n};\n");
302 moveifchanged(path("env.c.new"), path("env.c"));
306 read_file(char *fname)
308 char ifname[MAXPATHLEN];
310 struct file_list *tp;
313 char *wd, *this, *compilewith, *depends, *clean, *warning;
314 int compile, match, nreqs, std, filetype,
315 imp_rule, no_obj, before_depend, mandatory, nowerror;
317 fp = fopen(fname, "r");
323 * filename [ standard | mandatory | optional ]
324 * [ dev* [ | dev* ... ] | profiling-routine ] [ no-obj ]
325 * [ compile-with "compile rule" [no-implicit-rule] ]
326 * [ dependency "dependency-list"] [ before-depend ]
327 * [ clean "file-list"] [ warning "text warning" ]
330 if (wd == (char *)EOF) {
338 while (((wd = get_word(fp)) != (char *)EOF) && wd)
342 if (eq(wd, "include")) {
343 next_quoted_word(fp, wd);
345 printf("%s: missing include filename.\n", fname);
348 (void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
350 while (((wd = get_word(fp)) != (char *)EOF) && wd)
357 printf("%s: No type for %s.\n",
361 tp = fl_lookup(this);
375 if (eq(wd, "standard")) {
378 * If an entry is marked "mandatory", config will abort if it's
379 * not called by a configuration line in the config file. Apart
380 * from this, the device is handled like one marked "optional".
382 } else if (eq(wd, "mandatory")) {
384 } else if (!eq(wd, "optional")) {
385 printf("%s: %s must be optional, mandatory or standard\n",
393 if (compile && tp == NULL)
399 printf("%s: syntax error describing %s\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 (eq(wd, "nowerror")) {
482 STAILQ_FOREACH(dp, &dtab, d_next)
483 if (eq(dp->d_name, wd)) {
484 dp->d_done |= DEVDONE;
488 printf("%s: mandatory device \"%s\" not found\n",
493 printf("standard entry %s has a device keyword - %s!\n",
497 SLIST_FOREACH(op, &opt, op_next)
498 if (op->op_value == 0 && opteq(op->op_name, wd))
504 if (std == 0 && nreqs == 0) {
505 printf("%s: what is %s optional on?\n",
511 printf("%s: syntax error describing %s\n",
515 if (filetype == PROFILING && profiling == 0)
519 tp->f_type = filetype;
521 tp->f_flags |= NO_IMPLCT_RULE;
523 tp->f_flags |= NO_OBJ;
525 tp->f_flags |= BEFORE_DEPEND;
527 tp->f_flags |= NOWERROR;
528 tp->f_compilewith = compilewith;
529 tp->f_depends = depends;
531 tp->f_warn = warning;
536 * Read in the information about files used in making the system.
537 * Store it in the ftab linked list.
542 char fname[MAXPATHLEN];
543 struct files_name *nl, *tnl;
545 (void) snprintf(fname, sizeof(fname), "../../conf/files");
547 (void) snprintf(fname, sizeof(fname),
548 "../../conf/files.%s", machinename);
550 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
551 read_file(nl->f_name);
552 tnl = STAILQ_NEXT(nl, f_next);
559 opteq(const char *cp, const char *dp)
563 for (; ; cp++, dp++) {
565 c = isupper(*cp) ? tolower(*cp) : *cp;
566 d = isupper(*dp) ? tolower(*dp) : *dp;
576 do_before_depend(FILE *fp)
578 struct file_list *tp;
581 fputs("BEFORE_DEPEND=", fp);
583 STAILQ_FOREACH(tp, &ftab, f_next)
584 if (tp->f_flags & BEFORE_DEPEND) {
585 len = strlen(tp->f_fn);
586 if ((len = 3 + len) + lpos > 72) {
590 if (tp->f_flags & NO_IMPLCT_RULE)
591 fprintf(fp, "%s ", tp->f_fn);
593 fprintf(fp, "$S/%s ", tp->f_fn);
603 struct file_list *tp;
607 fprintf(fp, "OBJS=");
609 STAILQ_FOREACH(tp, &ftab, f_next) {
610 if (tp->f_flags & NO_OBJ)
613 cp = sp + (len = strlen(sp)) - 1;
616 if (len + lpos > 72) {
618 fprintf(fp, "\\\n\t");
620 fprintf(fp, "%s ", sp);
629 do_xxfiles(char *tag, FILE *fp)
631 struct file_list *tp;
635 if (tag[strlen(tag) - 1] == '\n')
636 tag[strlen(tag) - 1] = '\0';
643 fprintf(fp, "%sFILES=", SUFF);
645 STAILQ_FOREACH(tp, &ftab, f_next)
646 if (tp->f_type != NODEPEND) {
647 len = strlen(tp->f_fn);
648 if (tp->f_fn[len - slen - 1] != '.')
650 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
652 if ((len = 3 + len) + lpos > 72) {
656 if (tp->f_type != LOCAL)
657 fprintf(fp, "$S/%s ", tp->f_fn);
659 fprintf(fp, "%s ", tp->f_fn);
671 cp = rindex(fn, '/');
678 * Create the makerules for each file
679 * which is part of the system.
685 struct file_list *ftp;
688 STAILQ_FOREACH(ftp, &ftab, f_next) {
690 printf("WARNING: %s\n", ftp->f_warn);
691 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
693 if (ftp->f_flags & NO_IMPLCT_RULE) {
695 fprintf(f, "%s: %s\n", np, ftp->f_depends);
697 fprintf(f, "%s: \n", np);
702 fprintf(f, "%so:\n\t-cp $S/%so .\n\n",
706 if (ftp->f_depends) {
707 fprintf(f, "%sln: $S/%s%c %s\n", tail(np),
708 np, och, ftp->f_depends);
709 fprintf(f, "\t${NORMAL_LINT}\n\n");
710 fprintf(f, "%so: $S/%s%c %s\n", tail(np),
711 np, och, ftp->f_depends);
714 fprintf(f, "%sln: $S/%s%c\n", tail(np),
716 fprintf(f, "\t${NORMAL_LINT}\n\n");
717 fprintf(f, "%so: $S/%s%c\n", tail(np),
721 compilewith = ftp->f_compilewith;
722 if (compilewith == 0) {
723 const char *ftype = NULL;
724 static char cmd[128];
726 switch (ftp->f_type) {
739 printf("config: don't know rules for %s\n", np);
742 snprintf(cmd, sizeof(cmd), "${%s_%c%s}\n"
743 ".if defined(NORMAL_CTFCONVERT) && !empty(NORMAL_CTFCONVERT)\n"
744 "\t${NORMAL_CTFCONVERT}\n.endif", ftype,
746 ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
750 fprintf(f, "\t%s\n\n", compilewith);
757 struct file_list *tp;
762 STAILQ_FOREACH(tp, &ftab, f_next)
764 len = strlen(tp->f_clean);
765 if (len + lpos > 72) {
769 fprintf(fp, "%s ", tp->f_clean);
783 *str = toupper(*str);