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(op, &mkopt, op_next)
131 fprintf(ofp, "%s=%s\n", op->op_name, op->op_value);
133 fprintf(ofp, "DEBUG=-g\n");
135 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
137 fprintf(ofp,"S=%s\n", srcdir);
138 while (fgets(line, BUFSIZ, ifp) != 0) {
140 fprintf(ofp, "%s", line);
143 if (eq(line, "%BEFORE_DEPEND\n"))
144 do_before_depend(ofp);
145 else if (eq(line, "%OBJS\n"))
147 else if (strncmp(line, "%FILES.", 7) == 0)
148 do_xxfiles(line, ofp);
149 else if (eq(line, "%RULES\n"))
151 else if (eq(line, "%CLEAN\n"))
153 else if (strncmp(line, "%VERSREQ=", sizeof("%VERSREQ=") - 1) == 0) {
154 versreq = atoi(line + sizeof("%VERSREQ=") - 1);
155 if (MAJOR_VERS(versreq) != MAJOR_VERS(CONFIGVERS) ||
156 versreq > CONFIGVERS) {
157 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
158 fprintf(stderr, "config version = %d, ", CONFIGVERS);
159 fprintf(stderr, "version required = %d\n\n", versreq);
160 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
161 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
162 fprintf(stderr, "before trying this again.\n\n");
163 fprintf(stderr, "If running the new config fails check your config\n");
164 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
165 fprintf(stderr, "changes in config syntax, or option/device naming\n");
166 fprintf(stderr, "conventions\n\n");
171 "Unknown %% construct in generic makefile: %s",
176 moveifchanged(path("Makefile.new"), path("Makefile"));
180 * Build hints.c from the skeleton
190 ofp = fopen(path("hints.c.new"), "w");
192 err(1, "%s", path("hints.c.new"));
193 fprintf(ofp, "#include <sys/types.h>\n");
194 fprintf(ofp, "#include <sys/systm.h>\n");
196 fprintf(ofp, "int hintmode = %d;\n", hintmode);
197 fprintf(ofp, "char static_hints[] = {\n");
198 STAILQ_FOREACH(hint, &hints, hint_next) {
199 ifp = fopen(hint->hint_name, "r");
201 err(1, "%s", hint->hint_name);
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);
233 fprintf(ofp, "\"\\0\"\n};\n");
235 moveifchanged(path("hints.c.new"), path("hints.c"));
239 * Build env.c from the skeleton
249 ifp = fopen(env, "r");
255 ofp = fopen(path("env.c.new"), "w");
257 err(1, "%s", path("env.c.new"));
258 fprintf(ofp, "#include <sys/types.h>\n");
259 fprintf(ofp, "#include <sys/systm.h>\n");
261 fprintf(ofp, "int envmode = %d;\n", envmode);
262 fprintf(ofp, "char static_env[] = {\n");
264 while (fgets(line, BUFSIZ, ifp) != 0) {
265 /* zap trailing CR and/or LF */
266 while ((s = rindex(line, '\n')) != NULL)
268 while ((s = rindex(line, '\r')) != NULL)
270 /* remove # comments */
271 s = index(line, '#');
274 /* remove any whitespace and " characters */
277 if (*s == ' ' || *s == '\t' || *s == '"') {
291 fprintf(ofp, "\"%s\\0\"\n", line);
294 fprintf(ofp, "\"\\0\"\n};\n");
298 moveifchanged(path("env.c.new"), path("env.c"));
302 read_file(char *fname)
304 char ifname[MAXPATHLEN];
306 struct file_list *tp;
309 char *wd, *this, *compilewith, *depends, *clean, *warning;
310 int compile, match, nreqs, std, filetype,
311 imp_rule, no_obj, before_depend, mandatory, nowerror;
313 fp = fopen(fname, "r");
319 * filename [ standard | mandatory | optional ]
320 * [ dev* [ | dev* ... ] | profiling-routine ] [ no-obj ]
321 * [ compile-with "compile rule" [no-implicit-rule] ]
322 * [ dependency "dependency-list"] [ before-depend ]
323 * [ clean "file-list"] [ warning "text warning" ]
326 if (wd == (char *)EOF) {
334 while (((wd = get_word(fp)) != (char *)EOF) && wd)
338 if (eq(wd, "include")) {
339 next_quoted_word(fp, wd);
341 printf("%s: missing include filename.\n", fname);
344 (void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
346 while (((wd = get_word(fp)) != (char *)EOF) && wd)
353 printf("%s: No type for %s.\n",
357 tp = fl_lookup(this);
371 if (eq(wd, "standard")) {
374 * If an entry is marked "mandatory", config will abort if it's
375 * not called by a configuration line in the config file. Apart
376 * from this, the device is handled like one marked "optional".
378 } else if (eq(wd, "mandatory")) {
380 } else if (!eq(wd, "optional")) {
381 printf("%s: %s must be optional, mandatory or standard\n",
389 if (compile && tp == NULL)
395 printf("%s: syntax error describing %s\n",
404 if (eq(wd, "no-obj")) {
408 if (eq(wd, "no-implicit-rule")) {
409 if (compilewith == 0) {
410 printf("%s: alternate rule required when "
411 "\"no-implicit-rule\" is specified.\n",
417 if (eq(wd, "before-depend")) {
421 if (eq(wd, "dependency")) {
422 next_quoted_word(fp, wd);
424 printf("%s: %s missing compile command string.\n",
431 if (eq(wd, "clean")) {
432 next_quoted_word(fp, wd);
434 printf("%s: %s missing clean file list.\n",
441 if (eq(wd, "compile-with")) {
442 next_quoted_word(fp, wd);
444 printf("%s: %s missing compile command string.\n",
448 compilewith = ns(wd);
451 if (eq(wd, "warning")) {
452 next_quoted_word(fp, wd);
454 printf("%s: %s missing warning text string.\n",
462 if (eq(wd, "local")) {
466 if (eq(wd, "no-depend")) {
470 if (eq(wd, "profiling-routine")) {
471 filetype = PROFILING;
474 if (eq(wd, "nowerror")) {
478 STAILQ_FOREACH(dp, &dtab, d_next)
479 if (eq(dp->d_name, wd)) {
480 dp->d_done |= DEVDONE;
484 printf("%s: mandatory device \"%s\" not found\n",
489 printf("standard entry %s has a device keyword - %s!\n",
493 SLIST_FOREACH(op, &opt, op_next)
494 if (op->op_value == 0 && opteq(op->op_name, wd))
500 if (std == 0 && nreqs == 0) {
501 printf("%s: what is %s optional on?\n",
507 printf("%s: syntax error describing %s\n",
511 if (filetype == PROFILING && profiling == 0)
515 tp->f_type = filetype;
517 tp->f_flags |= NO_IMPLCT_RULE;
519 tp->f_flags |= NO_OBJ;
521 tp->f_flags |= BEFORE_DEPEND;
523 tp->f_flags |= NOWERROR;
524 tp->f_compilewith = compilewith;
525 tp->f_depends = depends;
527 tp->f_warn = warning;
532 * Read in the information about files used in making the system.
533 * Store it in the ftab linked list.
538 char fname[MAXPATHLEN];
539 struct files_name *nl, *tnl;
541 (void) snprintf(fname, sizeof(fname), "../../conf/files");
543 (void) snprintf(fname, sizeof(fname),
544 "../../conf/files.%s", machinename);
546 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
547 read_file(nl->f_name);
548 tnl = STAILQ_NEXT(nl, f_next);
555 opteq(const char *cp, const char *dp)
559 for (; ; cp++, dp++) {
561 c = isupper(*cp) ? tolower(*cp) : *cp;
562 d = isupper(*dp) ? tolower(*dp) : *dp;
572 do_before_depend(FILE *fp)
574 struct file_list *tp;
577 fputs("BEFORE_DEPEND=", fp);
579 STAILQ_FOREACH(tp, &ftab, f_next)
580 if (tp->f_flags & BEFORE_DEPEND) {
581 len = strlen(tp->f_fn);
582 if ((len = 3 + len) + lpos > 72) {
586 if (tp->f_flags & NO_IMPLCT_RULE)
587 fprintf(fp, "%s ", tp->f_fn);
589 fprintf(fp, "$S/%s ", tp->f_fn);
599 struct file_list *tp;
603 fprintf(fp, "OBJS=");
605 STAILQ_FOREACH(tp, &ftab, f_next) {
606 if (tp->f_flags & NO_OBJ)
609 cp = sp + (len = strlen(sp)) - 1;
612 if (len + lpos > 72) {
614 fprintf(fp, "\\\n\t");
616 fprintf(fp, "%s ", sp);
625 do_xxfiles(char *tag, FILE *fp)
627 struct file_list *tp;
631 if (tag[strlen(tag) - 1] == '\n')
632 tag[strlen(tag) - 1] = '\0';
639 fprintf(fp, "%sFILES=", SUFF);
641 STAILQ_FOREACH(tp, &ftab, f_next)
642 if (tp->f_type != NODEPEND) {
643 len = strlen(tp->f_fn);
644 if (tp->f_fn[len - slen - 1] != '.')
646 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
648 if ((len = 3 + len) + lpos > 72) {
652 if (tp->f_type != LOCAL)
653 fprintf(fp, "$S/%s ", tp->f_fn);
655 fprintf(fp, "%s ", tp->f_fn);
667 cp = rindex(fn, '/');
674 * Create the makerules for each file
675 * which is part of the system.
681 struct file_list *ftp;
684 STAILQ_FOREACH(ftp, &ftab, f_next) {
686 printf("WARNING: %s\n", ftp->f_warn);
687 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
689 if (ftp->f_flags & NO_IMPLCT_RULE) {
691 fprintf(f, "%s: %s\n", np, ftp->f_depends);
693 fprintf(f, "%s: \n", np);
698 fprintf(f, "%so:\n\t-cp $S/%so .\n\n",
702 if (ftp->f_depends) {
703 fprintf(f, "%sln: $S/%s%c %s\n", tail(np),
704 np, och, ftp->f_depends);
705 fprintf(f, "\t${NORMAL_LINT}\n\n");
706 fprintf(f, "%so: $S/%s%c %s\n", tail(np),
707 np, och, ftp->f_depends);
710 fprintf(f, "%sln: $S/%s%c\n", tail(np),
712 fprintf(f, "\t${NORMAL_LINT}\n\n");
713 fprintf(f, "%so: $S/%s%c\n", tail(np),
717 compilewith = ftp->f_compilewith;
718 if (compilewith == 0) {
719 const char *ftype = NULL;
720 static char cmd[128];
722 switch (ftp->f_type) {
735 printf("config: don't know rules for %s\n", np);
738 snprintf(cmd, sizeof(cmd), "${%s_%c%s}", ftype,
740 ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
744 fprintf(f, "\t%s\n\n", compilewith);
751 struct file_list *tp;
756 STAILQ_FOREACH(tp, &ftab, f_next)
758 len = strlen(tp->f_clean);
759 if (len + lpos > 72) {
763 fprintf(fp, "%s ", tp->f_clean);
777 *str = toupper(*str);