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 *) malloc(sizeof *fp);
101 bzero(fp, sizeof *fp);
102 STAILQ_INSERT_TAIL(&ftab, fp, f_next);
107 * Build the makefile from the skeleton
119 snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
120 ifp = fopen(line, "r");
122 snprintf(line, sizeof(line), "Makefile.%s", machinename);
123 ifp = fopen(line, "r");
128 /* XXX this check seems to be misplaced. */
129 if (SLIST_EMPTY(&cputype)) {
130 printf("cpu type must be specified\n");
134 ofp = fopen(path("Makefile.new"), "w");
136 err(1, "%s", path("Makefile.new"));
137 fprintf(ofp, "KERN_IDENT=%s\n", ident);
138 SLIST_FOREACH(op, &mkopt, op_next)
139 fprintf(ofp, "%s=%s\n", op->op_name, op->op_value);
141 fprintf(ofp, "DEBUG=-g\n");
143 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
145 fprintf(ofp,"S=%s\n", srcdir);
146 while (fgets(line, BUFSIZ, ifp) != 0) {
148 fprintf(ofp, "%s", line);
151 if (eq(line, "%BEFORE_DEPEND\n"))
152 do_before_depend(ofp);
153 else if (eq(line, "%OBJS\n"))
155 else if (strncmp(line, "%FILES.", 7) == 0)
156 do_xxfiles(line, ofp);
157 else if (eq(line, "%RULES\n"))
159 else if (eq(line, "%CLEAN\n"))
161 else if (strncmp(line, "%VERSREQ=", sizeof("%VERSREQ=") - 1) == 0) {
162 versreq = atoi(line + sizeof("%VERSREQ=") - 1);
163 if (versreq != CONFIGVERS) {
164 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
165 fprintf(stderr, "config version = %d, ", CONFIGVERS);
166 fprintf(stderr, "version required = %d\n\n", versreq);
167 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
168 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
169 fprintf(stderr, "before trying this again.\n\n");
170 fprintf(stderr, "If running the new config fails check your config\n");
171 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
172 fprintf(stderr, "changes in config syntax, or option/device naming\n");
173 fprintf(stderr, "conventions\n\n");
178 "Unknown %% construct in generic makefile: %s",
183 moveifchanged(path("Makefile.new"), path("Makefile"));
185 /* XXX makefile() should make the Makefile, not hints.c. */
187 ifp = fopen(hints, "r");
193 ofp = fopen(path("hints.c.new"), "w");
195 err(1, "%s", path("hints.c.new"));
196 fprintf(ofp, "#include <sys/types.h>\n");
197 fprintf(ofp, "#include <sys/systm.h>\n");
199 fprintf(ofp, "int hintmode = %d;\n", hintmode);
200 fprintf(ofp, "char static_hints[] = {\n");
202 while (fgets(line, BUFSIZ, ifp) != 0) {
203 /* zap trailing CR and/or LF */
204 while ((s = rindex(line, '\n')) != NULL)
206 while ((s = rindex(line, '\r')) != NULL)
208 /* remove # comments */
209 s = index(line, '#');
212 /* remove any whitespace and " characters */
215 if (*s == ' ' || *s == '\t' || *s == '"') {
229 fprintf(ofp, "\"%s\\0\"\n", line);
232 fprintf(ofp, "\"\\0\"\n};\n");
236 moveifchanged(path("hints.c.new"), path("hints.c"));
238 /* XXX makefile() should make the Makefile, not env.c. */
240 ifp = fopen(env, "r");
246 ofp = fopen(path("env.c.new"), "w");
248 err(1, "%s", path("env.c.new"));
249 fprintf(ofp, "#include <sys/types.h>\n");
250 fprintf(ofp, "#include <sys/systm.h>\n");
252 fprintf(ofp, "int envmode = %d;\n", envmode);
253 fprintf(ofp, "char static_env[] = {\n");
255 while (fgets(line, BUFSIZ, ifp) != 0) {
256 /* zap trailing CR and/or LF */
257 while ((s = rindex(line, '\n')) != NULL)
259 while ((s = rindex(line, '\r')) != NULL)
261 /* remove # comments */
262 s = index(line, '#');
265 /* remove any whitespace and " characters */
268 if (*s == ' ' || *s == '\t' || *s == '"') {
282 fprintf(ofp, "\"%s\\0\"\n", line);
285 fprintf(ofp, "\"\\0\"\n};\n");
289 moveifchanged(path("env.c.new"), path("env.c"));
293 read_file(char *fname)
296 struct file_list *tp, *pf;
299 char *wd, *this, *needs, *compilewith, *depends, *clean, *warning;
300 int nreqs, isdup, std, filetype,
301 imp_rule, no_obj, before_depend, mandatory, nowerror;
303 fp = fopen(fname, "r");
308 * filename [ standard | mandatory | optional ]
309 * [ dev* | profiling-routine ] [ no-obj ]
310 * [ compile-with "compile rule" [no-implicit-rule] ]
311 * [ dependency "dependency-list"] [ before-depend ]
312 * [ clean "file-list"] [ warning "text warning" ]
315 if (wd == (char *)EOF) {
323 while (((wd = get_word(fp)) != (char *)EOF) && wd)
330 printf("%s: No type for %s.\n",
334 if ((pf = fl_lookup(this)) && (pf->f_type != INVISIBLE || pf->f_flags))
351 if (eq(wd, "standard")) {
354 * If an entry is marked "mandatory", config will abort if it's
355 * not called by a configuration line in the config file. Apart
356 * from this, the device is handled like one marked "optional".
358 } else if (eq(wd, "mandatory")) {
360 } else if (!eq(wd, "optional")) {
361 printf("%s: %s must be optional, mandatory or standard\n",
372 if (eq(wd, "no-obj")) {
376 if (eq(wd, "no-implicit-rule")) {
377 if (compilewith == 0) {
378 printf("%s: alternate rule required when "
379 "\"no-implicit-rule\" is specified.\n",
385 if (eq(wd, "before-depend")) {
389 if (eq(wd, "dependency")) {
390 next_quoted_word(fp, wd);
392 printf("%s: %s missing compile command string.\n",
399 if (eq(wd, "clean")) {
400 next_quoted_word(fp, wd);
402 printf("%s: %s missing clean file list.\n",
409 if (eq(wd, "compile-with")) {
410 next_quoted_word(fp, wd);
412 printf("%s: %s missing compile command string.\n",
416 compilewith = ns(wd);
419 if (eq(wd, "warning")) {
420 next_quoted_word(fp, wd);
422 printf("%s: %s missing warning text string.\n",
430 if (eq(wd, "local")) {
434 if (eq(wd, "no-depend")) {
438 if (eq(wd, "profiling-routine")) {
439 filetype = PROFILING;
442 if (eq(wd, "nowerror")) {
446 if (needs == 0 && nreqs == 1)
450 STAILQ_FOREACH(dp, &dtab, d_next)
451 if (eq(dp->d_name, wd))
454 printf("%s: mandatory device \"%s\" not found\n",
459 printf("standard entry %s has a device keyword - %s!\n",
463 SLIST_FOREACH(op, &opt, op_next)
464 if (op->op_value == 0 && opteq(op->op_name, wd)) {
472 while ((wd = get_word(fp)) != 0)
477 tp->f_type = INVISIBLE;
479 tp->f_flags |= isdup;
480 tp->f_compilewith = compilewith;
481 tp->f_depends = depends;
483 tp->f_warn = warning;
487 if (std == 0 && nreqs == 0) {
488 printf("%s: what is %s optional on?\n",
494 printf("%s: syntax error describing %s\n",
498 if (filetype == PROFILING && profiling == 0)
503 tp->f_type = filetype;
504 tp->f_flags &= ~ISDUP;
506 tp->f_flags |= NO_IMPLCT_RULE;
508 tp->f_flags |= NO_OBJ;
510 tp->f_flags |= BEFORE_DEPEND;
512 tp->f_flags |= NOWERROR;
514 tp->f_compilewith = compilewith;
515 tp->f_depends = depends;
517 tp->f_warn = warning;
518 if (pf && pf->f_type == INVISIBLE)
519 pf->f_flags |= ISDUP; /* mark as duplicate */
524 * Read in the information about files used in making the system.
525 * Store it in the ftab linked list.
530 char fname[MAXPATHLEN];
531 struct files_name *nl, *tnl;
534 printf("no ident line specified\n");
537 (void) snprintf(fname, sizeof(fname), "../../conf/files");
539 (void) snprintf(fname, sizeof(fname),
540 "../../conf/files.%s", machinename);
542 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
543 read_file(nl->f_name);
544 tnl = STAILQ_NEXT(nl, f_next);
551 opteq(const char *cp, const char *dp)
555 for (; ; cp++, dp++) {
557 c = isupper(*cp) ? tolower(*cp) : *cp;
558 d = isupper(*dp) ? tolower(*dp) : *dp;
568 do_before_depend(FILE *fp)
570 struct file_list *tp;
573 fputs("BEFORE_DEPEND=", fp);
575 STAILQ_FOREACH(tp, &ftab, f_next)
576 if (tp->f_flags & BEFORE_DEPEND) {
577 len = strlen(tp->f_fn);
578 if ((len = 3 + len) + lpos > 72) {
582 if (tp->f_flags & NO_IMPLCT_RULE)
583 fprintf(fp, "%s ", tp->f_fn);
585 fprintf(fp, "$S/%s ", tp->f_fn);
595 struct file_list *tp;
599 fprintf(fp, "OBJS=");
601 STAILQ_FOREACH(tp, &ftab, f_next) {
602 if (tp->f_type == INVISIBLE || tp->f_flags & NO_OBJ)
605 cp = sp + (len = strlen(sp)) - 1;
608 if (len + lpos > 72) {
610 fprintf(fp, "\\\n\t");
612 fprintf(fp, "%s ", sp);
621 do_xxfiles(char *tag, FILE *fp)
623 struct file_list *tp;
627 if (tag[strlen(tag) - 1] == '\n')
628 tag[strlen(tag) - 1] = '\0';
635 fprintf(fp, "%sFILES=", SUFF);
637 STAILQ_FOREACH(tp, &ftab, f_next)
638 if (tp->f_type != INVISIBLE && tp->f_type != NODEPEND) {
639 len = strlen(tp->f_fn);
640 if (tp->f_fn[len - slen - 1] != '.')
642 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
644 if ((len = 3 + len) + lpos > 72) {
648 if (tp->f_type != LOCAL)
649 fprintf(fp, "$S/%s ", tp->f_fn);
651 fprintf(fp, "%s ", tp->f_fn);
663 cp = rindex(fn, '/');
670 * Create the makerules for each file
671 * which is part of the system.
676 char *cp, *np, och, *tp;
677 struct file_list *ftp;
680 STAILQ_FOREACH(ftp, &ftab, f_next) {
681 if (ftp->f_type == INVISIBLE)
684 printf("WARNING: %s\n", ftp->f_warn);
685 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
687 if (ftp->f_flags & NO_IMPLCT_RULE) {
689 fprintf(f, "%s: %s\n", np, ftp->f_depends);
691 fprintf(f, "%s: \n", np);
696 fprintf(f, "%so:\n\t-cp $S/%so .\n\n",
700 if (ftp->f_depends) {
701 fprintf(f, "%sln: $S/%s%c %s\n", tail(np),
702 np, och, ftp->f_depends);
703 fprintf(f, "\t${NORMAL_LINT}\n\n");
704 fprintf(f, "%so: $S/%s%c %s\n", tail(np),
705 np, och, ftp->f_depends);
708 fprintf(f, "%sln: $S/%s%c\n", tail(np),
710 fprintf(f, "\t${NORMAL_LINT}\n\n");
711 fprintf(f, "%so: $S/%s%c\n", tail(np),
716 compilewith = ftp->f_compilewith;
717 if (compilewith == 0) {
718 const char *ftype = NULL;
719 static char cmd[128];
721 switch (ftp->f_type) {
734 printf("config: don't know rules for %s\n", np);
737 snprintf(cmd, sizeof(cmd), "${%s_%c%s}", ftype,
739 ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
743 fprintf(f, "\t%s\n\n", compilewith);
750 struct file_list *tp;
755 STAILQ_FOREACH(tp, &ftab, f_next)
757 len = strlen(tp->f_clean);
758 if (len + lpos > 72) {
762 fprintf(fp, "%s ", tp->f_clean);
776 *str = toupper(*str);