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 * Open the correct Makefile and return it, or error out.
111 open_makefile_template(void)
116 snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
117 ifp = fopen(line, "r");
119 snprintf(line, sizeof(line), "Makefile.%s", machinename);
120 ifp = fopen(line, "r");
128 * Build the makefile from the skeleton
138 ifp = open_makefile_template();
139 ofp = fopen(path("Makefile.new"), "w");
141 err(1, "%s", path("Makefile.new"));
142 fprintf(ofp, "KERN_IDENT=%s\n", ident);
143 SLIST_FOREACH_SAFE(op, &mkopt, op_next, t) {
144 fprintf(ofp, "%s=%s", op->op_name, op->op_value);
145 while ((op = SLIST_NEXT(op, op_append)) != NULL)
146 fprintf(ofp, " %s", op->op_value);
150 fprintf(ofp, "DEBUG=-g\n");
152 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
154 fprintf(ofp,"S=%s\n", srcdir);
155 while (fgets(line, BUFSIZ, ifp) != 0) {
157 fprintf(ofp, "%s", line);
160 if (eq(line, "%BEFORE_DEPEND\n"))
161 do_before_depend(ofp);
162 else if (eq(line, "%OBJS\n"))
164 else if (strncmp(line, "%FILES.", 7) == 0)
165 do_xxfiles(line, ofp);
166 else if (eq(line, "%RULES\n"))
168 else if (eq(line, "%CLEAN\n"))
170 else if (strncmp(line, "%VERSREQ=", 9) == 0)
171 line[0] = '\0'; /* handled elsewhere */
174 "Unknown %% construct in generic makefile: %s",
179 moveifchanged(path("Makefile.new"), path("Makefile"));
183 * Build hints.c from the skeleton
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");
201 STAILQ_FOREACH(hint, &hints, hint_next) {
202 ifp = fopen(hint->hint_name, "r");
204 err(1, "%s", hint->hint_name);
205 while (fgets(line, BUFSIZ, ifp) != 0) {
206 /* zap trailing CR and/or LF */
207 while ((s = rindex(line, '\n')) != NULL)
209 while ((s = rindex(line, '\r')) != NULL)
211 /* remove # comments */
212 s = index(line, '#');
215 /* remove any whitespace and " characters */
218 if (*s == ' ' || *s == '\t' || *s == '"') {
232 fprintf(ofp, "\"%s\\0\"\n", line);
236 fprintf(ofp, "\"\\0\"\n};\n");
238 moveifchanged(path("hints.c.new"), path("hints.c"));
242 * Build env.c from the skeleton
252 ifp = fopen(env, "r");
258 ofp = fopen(path("env.c.new"), "w");
260 err(1, "%s", path("env.c.new"));
261 fprintf(ofp, "#include <sys/types.h>\n");
262 fprintf(ofp, "#include <sys/systm.h>\n");
264 fprintf(ofp, "int envmode = %d;\n", envmode);
265 fprintf(ofp, "char static_env[] = {\n");
267 while (fgets(line, BUFSIZ, ifp) != 0) {
268 /* zap trailing CR and/or LF */
269 while ((s = rindex(line, '\n')) != NULL)
271 while ((s = rindex(line, '\r')) != NULL)
273 /* remove # comments */
274 s = index(line, '#');
277 /* remove any whitespace and " characters */
280 if (*s == ' ' || *s == '\t' || *s == '"') {
294 fprintf(ofp, "\"%s\\0\"\n", line);
297 fprintf(ofp, "\"\\0\"\n};\n");
301 moveifchanged(path("env.c.new"), path("env.c"));
305 read_file(char *fname)
307 char ifname[MAXPATHLEN];
309 struct file_list *tp;
312 char *wd, *this, *compilewith, *depends, *clean, *warning;
313 int compile, match, nreqs, std, filetype,
314 imp_rule, no_obj, before_depend, mandatory, nowerror;
316 fp = fopen(fname, "r");
322 * filename [ standard | mandatory | optional ]
323 * [ dev* [ | dev* ... ] | profiling-routine ] [ no-obj ]
324 * [ compile-with "compile rule" [no-implicit-rule] ]
325 * [ dependency "dependency-list"] [ before-depend ]
326 * [ clean "file-list"] [ warning "text warning" ]
329 if (wd == (char *)EOF) {
337 while (((wd = get_word(fp)) != (char *)EOF) && wd)
341 if (eq(wd, "include")) {
342 next_quoted_word(fp, wd);
344 fprintf(stderr, "%s: missing include filename.\n",
348 (void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
350 while (((wd = get_word(fp)) != (char *)EOF) && wd)
357 fprintf(stderr, "%s: No type for %s.\n", fname, this);
360 tp = fl_lookup(this);
374 if (eq(wd, "standard")) {
377 * If an entry is marked "mandatory", config will abort if it's
378 * not called by a configuration line in the config file. Apart
379 * from this, the device is handled like one marked "optional".
381 } else if (eq(wd, "mandatory")) {
383 } else if (!eq(wd, "optional")) {
385 "%s: %s must be optional, mandatory or standard\n",
393 if (compile && tp == NULL)
399 fprintf(stderr, "%s: syntax error describing %s\n",
408 if (eq(wd, "no-obj")) {
412 if (eq(wd, "no-implicit-rule")) {
413 if (compilewith == 0) {
414 fprintf(stderr, "%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);
429 "%s: %s missing compile command string.\n",
436 if (eq(wd, "clean")) {
437 next_quoted_word(fp, wd);
439 fprintf(stderr, "%s: %s missing clean file list.\n",
446 if (eq(wd, "compile-with")) {
447 next_quoted_word(fp, wd);
450 "%s: %s missing compile command string.\n",
454 compilewith = ns(wd);
457 if (eq(wd, "warning")) {
458 next_quoted_word(fp, wd);
461 "%s: %s missing warning text string.\n",
469 if (eq(wd, "local")) {
473 if (eq(wd, "no-depend")) {
477 if (eq(wd, "profiling-routine")) {
478 filetype = PROFILING;
481 if (eq(wd, "nowerror")) {
485 STAILQ_FOREACH(dp, &dtab, d_next)
486 if (eq(dp->d_name, wd)) {
487 dp->d_done |= DEVDONE;
491 fprintf(stderr, "%s: mandatory device \"%s\" not found\n",
497 "standard entry %s has a device keyword - %s!\n",
501 SLIST_FOREACH(op, &opt, op_next)
502 if (op->op_value == 0 && opteq(op->op_name, wd))
508 if (std == 0 && nreqs == 0) {
509 fprintf(stderr, "%s: what is %s optional on?\n",
515 fprintf(stderr, "%s: syntax error describing %s\n",
519 if (filetype == PROFILING && profiling == 0)
523 tp->f_type = filetype;
525 tp->f_flags |= NO_IMPLCT_RULE;
527 tp->f_flags |= NO_OBJ;
529 tp->f_flags |= BEFORE_DEPEND;
531 tp->f_flags |= NOWERROR;
532 tp->f_compilewith = compilewith;
533 tp->f_depends = depends;
535 tp->f_warn = warning;
540 * Read in the information about files used in making the system.
541 * Store it in the ftab linked list.
546 char fname[MAXPATHLEN];
547 struct files_name *nl, *tnl;
549 (void) snprintf(fname, sizeof(fname), "../../conf/files");
551 (void) snprintf(fname, sizeof(fname),
552 "../../conf/files.%s", machinename);
554 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
555 read_file(nl->f_name);
556 tnl = STAILQ_NEXT(nl, f_next);
563 opteq(const char *cp, const char *dp)
567 for (; ; cp++, dp++) {
569 c = isupper(*cp) ? tolower(*cp) : *cp;
570 d = isupper(*dp) ? tolower(*dp) : *dp;
580 do_before_depend(FILE *fp)
582 struct file_list *tp;
585 fputs("BEFORE_DEPEND=", fp);
587 STAILQ_FOREACH(tp, &ftab, f_next)
588 if (tp->f_flags & BEFORE_DEPEND) {
589 len = strlen(tp->f_fn);
590 if ((len = 3 + len) + lpos > 72) {
594 if (tp->f_flags & NO_IMPLCT_RULE)
595 fprintf(fp, "%s ", tp->f_fn);
597 fprintf(fp, "$S/%s ", tp->f_fn);
607 struct file_list *tp;
611 fprintf(fp, "OBJS=");
613 STAILQ_FOREACH(tp, &ftab, f_next) {
614 if (tp->f_flags & NO_OBJ)
617 cp = sp + (len = strlen(sp)) - 1;
620 if (len + lpos > 72) {
622 fprintf(fp, "\\\n\t");
624 fprintf(fp, "%s ", sp);
633 do_xxfiles(char *tag, FILE *fp)
635 struct file_list *tp;
639 if (tag[strlen(tag) - 1] == '\n')
640 tag[strlen(tag) - 1] = '\0';
647 fprintf(fp, "%sFILES=", SUFF);
649 STAILQ_FOREACH(tp, &ftab, f_next)
650 if (tp->f_type != NODEPEND) {
651 len = strlen(tp->f_fn);
652 if (tp->f_fn[len - slen - 1] != '.')
654 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
656 if ((len = 3 + len) + lpos > 72) {
660 if (tp->f_type != LOCAL)
661 fprintf(fp, "$S/%s ", tp->f_fn);
663 fprintf(fp, "%s ", tp->f_fn);
675 cp = rindex(fn, '/');
682 * Create the makerules for each file
683 * which is part of the system.
689 struct file_list *ftp;
693 STAILQ_FOREACH(ftp, &ftab, f_next) {
695 fprintf(stderr, "WARNING: %s\n", ftp->f_warn);
696 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
698 if (ftp->f_flags & NO_IMPLCT_RULE) {
700 fprintf(f, "%s: %s\n", np, ftp->f_depends);
702 fprintf(f, "%s: \n", np);
707 fprintf(f, "%so:\n\t-cp $S/%so .\n\n",
711 if (ftp->f_depends) {
712 fprintf(f, "%sln: $S/%s%c %s\n", tail(np),
713 np, och, ftp->f_depends);
714 fprintf(f, "\t${NORMAL_LINT}\n\n");
715 fprintf(f, "%so: $S/%s%c %s\n", tail(np),
716 np, och, ftp->f_depends);
719 fprintf(f, "%sln: $S/%s%c\n", tail(np),
721 fprintf(f, "\t${NORMAL_LINT}\n\n");
722 fprintf(f, "%so: $S/%s%c\n", tail(np),
726 compilewith = ftp->f_compilewith;
727 if (compilewith == 0) {
728 const char *ftype = NULL;
730 switch (ftp->f_type) {
741 "config: don't know rules for %s\n", np);
744 snprintf(cmd, sizeof(cmd),
745 "${%s_%c%s}\n.if defined(NORMAL_CTFCONVERT) && "
746 "!empty(NORMAL_CTFCONVERT)\n"
747 "\t${NORMAL_CTFCONVERT}\n.endif", ftype,
749 ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
753 fprintf(f, "\t%s\n\n", compilewith);
760 struct file_list *tp;
765 STAILQ_FOREACH(tp, &ftab, f_next)
767 len = strlen(tp->f_clean);
768 if (len + lpos > 72) {
772 fprintf(fp, "%s ", tp->f_clean);
786 *str = toupper(*str);