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);
102 err(EXIT_FAILURE, "calloc");
103 STAILQ_INSERT_TAIL(&ftab, fp, f_next);
108 * 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 ofp = fopen(path("Makefile.new"), "w");
130 err(1, "%s", path("Makefile.new"));
131 fprintf(ofp, "KERN_IDENT=%s\n", ident);
132 SLIST_FOREACH_SAFE(op, &mkopt, op_next, t) {
133 fprintf(ofp, "%s=%s", op->op_name, op->op_value);
134 while ((op = SLIST_NEXT(op, op_append)) != NULL)
135 fprintf(ofp, " %s", op->op_value);
139 fprintf(ofp, "DEBUG=-g\n");
141 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
143 fprintf(ofp,"S=%s\n", srcdir);
144 while (fgets(line, BUFSIZ, ifp) != 0) {
146 fprintf(ofp, "%s", line);
149 if (eq(line, "%BEFORE_DEPEND\n"))
150 do_before_depend(ofp);
151 else if (eq(line, "%OBJS\n"))
153 else if (strncmp(line, "%FILES.", 7) == 0)
154 do_xxfiles(line, ofp);
155 else if (eq(line, "%RULES\n"))
157 else if (eq(line, "%CLEAN\n"))
159 else if (strncmp(line, "%VERSREQ=", sizeof("%VERSREQ=") - 1) == 0) {
160 versreq = atoi(line + sizeof("%VERSREQ=") - 1);
161 if (MAJOR_VERS(versreq) != MAJOR_VERS(CONFIGVERS) ||
162 versreq > CONFIGVERS) {
163 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
164 fprintf(stderr, "config version = %d, ", CONFIGVERS);
165 fprintf(stderr, "version required = %d\n\n", versreq);
166 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
167 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
168 fprintf(stderr, "before trying this again.\n\n");
169 fprintf(stderr, "If running the new config fails check your config\n");
170 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
171 fprintf(stderr, "changes in config syntax, or option/device naming\n");
172 fprintf(stderr, "conventions\n\n");
177 "Unknown %% construct in generic makefile: %s",
182 moveifchanged(path("Makefile.new"), path("Makefile"));
186 * Build hints.c from the skeleton
196 ofp = fopen(path("hints.c.new"), "w");
198 err(1, "%s", path("hints.c.new"));
199 fprintf(ofp, "#include <sys/types.h>\n");
200 fprintf(ofp, "#include <sys/systm.h>\n");
202 fprintf(ofp, "int hintmode = %d;\n", hintmode);
203 fprintf(ofp, "char static_hints[] = {\n");
204 STAILQ_FOREACH(hint, &hints, hint_next) {
205 ifp = fopen(hint->hint_name, "r");
207 err(1, "%s", hint->hint_name);
208 while (fgets(line, BUFSIZ, ifp) != 0) {
209 /* zap trailing CR and/or LF */
210 while ((s = rindex(line, '\n')) != NULL)
212 while ((s = rindex(line, '\r')) != NULL)
214 /* remove # comments */
215 s = index(line, '#');
218 /* remove any whitespace and " characters */
221 if (*s == ' ' || *s == '\t' || *s == '"') {
235 fprintf(ofp, "\"%s\\0\"\n", line);
239 fprintf(ofp, "\"\\0\"\n};\n");
241 moveifchanged(path("hints.c.new"), path("hints.c"));
245 * Build env.c from the skeleton
255 ifp = fopen(env, "r");
261 ofp = fopen(path("env.c.new"), "w");
263 err(1, "%s", path("env.c.new"));
264 fprintf(ofp, "#include <sys/types.h>\n");
265 fprintf(ofp, "#include <sys/systm.h>\n");
267 fprintf(ofp, "int envmode = %d;\n", envmode);
268 fprintf(ofp, "char static_env[] = {\n");
270 while (fgets(line, BUFSIZ, ifp) != 0) {
271 /* zap trailing CR and/or LF */
272 while ((s = rindex(line, '\n')) != NULL)
274 while ((s = rindex(line, '\r')) != NULL)
276 /* remove # comments */
277 s = index(line, '#');
280 /* remove any whitespace and " characters */
283 if (*s == ' ' || *s == '\t' || *s == '"') {
297 fprintf(ofp, "\"%s\\0\"\n", line);
300 fprintf(ofp, "\"\\0\"\n};\n");
304 moveifchanged(path("env.c.new"), path("env.c"));
308 read_file(char *fname)
310 char ifname[MAXPATHLEN];
312 struct file_list *tp;
315 char *wd, *this, *compilewith, *depends, *clean, *warning;
316 int compile, match, nreqs, std, filetype,
317 imp_rule, no_obj, before_depend, mandatory, nowerror;
319 fp = fopen(fname, "r");
325 * filename [ standard | mandatory | optional ]
326 * [ dev* [ | dev* ... ] | profiling-routine ] [ no-obj ]
327 * [ compile-with "compile rule" [no-implicit-rule] ]
328 * [ dependency "dependency-list"] [ before-depend ]
329 * [ clean "file-list"] [ warning "text warning" ]
332 if (wd == (char *)EOF) {
340 while (((wd = get_word(fp)) != (char *)EOF) && wd)
344 if (eq(wd, "include")) {
345 next_quoted_word(fp, wd);
347 printf("%s: missing include filename.\n", fname);
350 (void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
352 while (((wd = get_word(fp)) != (char *)EOF) && wd)
359 printf("%s: No type for %s.\n",
363 tp = fl_lookup(this);
377 if (eq(wd, "standard")) {
380 * If an entry is marked "mandatory", config will abort if it's
381 * not called by a configuration line in the config file. Apart
382 * from this, the device is handled like one marked "optional".
384 } else if (eq(wd, "mandatory")) {
386 } else if (!eq(wd, "optional")) {
387 printf("%s: %s must be optional, mandatory or standard\n",
395 if (compile && tp == NULL)
401 printf("%s: syntax error describing %s\n",
410 if (eq(wd, "no-obj")) {
414 if (eq(wd, "no-implicit-rule")) {
415 if (compilewith == 0) {
416 printf("%s: alternate rule required when "
417 "\"no-implicit-rule\" is specified.\n",
423 if (eq(wd, "before-depend")) {
427 if (eq(wd, "dependency")) {
428 next_quoted_word(fp, wd);
430 printf("%s: %s missing compile command string.\n",
437 if (eq(wd, "clean")) {
438 next_quoted_word(fp, wd);
440 printf("%s: %s missing clean file list.\n",
447 if (eq(wd, "compile-with")) {
448 next_quoted_word(fp, wd);
450 printf("%s: %s missing compile command string.\n",
454 compilewith = ns(wd);
457 if (eq(wd, "warning")) {
458 next_quoted_word(fp, wd);
460 printf("%s: %s missing warning text string.\n",
468 if (eq(wd, "local")) {
472 if (eq(wd, "no-depend")) {
476 if (eq(wd, "profiling-routine")) {
477 filetype = PROFILING;
480 if (eq(wd, "nowerror")) {
484 STAILQ_FOREACH(dp, &dtab, d_next)
485 if (eq(dp->d_name, wd)) {
486 dp->d_done |= DEVDONE;
490 printf("%s: mandatory device \"%s\" not found\n",
495 printf("standard entry %s has a device keyword - %s!\n",
499 SLIST_FOREACH(op, &opt, op_next)
500 if (op->op_value == 0 && opteq(op->op_name, wd))
506 if (std == 0 && nreqs == 0) {
507 printf("%s: what is %s optional on?\n",
513 printf("%s: syntax error describing %s\n",
517 if (filetype == PROFILING && profiling == 0)
521 tp->f_type = filetype;
523 tp->f_flags |= NO_IMPLCT_RULE;
525 tp->f_flags |= NO_OBJ;
527 tp->f_flags |= BEFORE_DEPEND;
529 tp->f_flags |= NOWERROR;
530 tp->f_compilewith = compilewith;
531 tp->f_depends = depends;
533 tp->f_warn = warning;
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;
547 (void) snprintf(fname, sizeof(fname), "../../conf/files");
549 (void) snprintf(fname, sizeof(fname),
550 "../../conf/files.%s", machinename);
552 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
553 read_file(nl->f_name);
554 tnl = STAILQ_NEXT(nl, f_next);
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 STAILQ_FOREACH(tp, &ftab, 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 STAILQ_FOREACH(tp, &ftab, f_next) {
612 if (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 STAILQ_FOREACH(tp, &ftab, f_next)
648 if (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.
687 struct file_list *ftp;
690 STAILQ_FOREACH(ftp, &ftab, f_next) {
692 printf("WARNING: %s\n", ftp->f_warn);
693 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
695 if (ftp->f_flags & NO_IMPLCT_RULE) {
697 fprintf(f, "%s: %s\n", np, ftp->f_depends);
699 fprintf(f, "%s: \n", np);
704 fprintf(f, "%so:\n\t-cp $S/%so .\n\n",
708 if (ftp->f_depends) {
709 fprintf(f, "%sln: $S/%s%c %s\n", tail(np),
710 np, och, ftp->f_depends);
711 fprintf(f, "\t${NORMAL_LINT}\n\n");
712 fprintf(f, "%so: $S/%s%c %s\n", tail(np),
713 np, och, ftp->f_depends);
716 fprintf(f, "%sln: $S/%s%c\n", tail(np),
718 fprintf(f, "\t${NORMAL_LINT}\n\n");
719 fprintf(f, "%so: $S/%s%c\n", tail(np),
723 compilewith = ftp->f_compilewith;
724 if (compilewith == 0) {
725 const char *ftype = NULL;
726 static char cmd[128];
728 switch (ftp->f_type) {
741 printf("config: don't know rules for %s\n", np);
744 snprintf(cmd, sizeof(cmd), "${%s_%c%s}\n.if defined(NORMAL_CTFCONVERT) && !empty(NORMAL_CTFCONVERT)\n\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);