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 printf("%s: missing include filename.\n", fname);
347 (void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
349 while (((wd = get_word(fp)) != (char *)EOF) && wd)
356 printf("%s: No type for %s.\n",
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")) {
384 printf("%s: %s must be optional, mandatory or standard\n",
392 if (compile && tp == NULL)
398 printf("%s: syntax error describing %s\n",
407 if (eq(wd, "no-obj")) {
411 if (eq(wd, "no-implicit-rule")) {
412 if (compilewith == 0) {
413 printf("%s: alternate rule required when "
414 "\"no-implicit-rule\" is specified.\n",
420 if (eq(wd, "before-depend")) {
424 if (eq(wd, "dependency")) {
425 next_quoted_word(fp, wd);
427 printf("%s: %s missing compile command string.\n",
434 if (eq(wd, "clean")) {
435 next_quoted_word(fp, wd);
437 printf("%s: %s missing clean file list.\n",
444 if (eq(wd, "compile-with")) {
445 next_quoted_word(fp, wd);
447 printf("%s: %s missing compile command string.\n",
451 compilewith = ns(wd);
454 if (eq(wd, "warning")) {
455 next_quoted_word(fp, wd);
457 printf("%s: %s missing warning text string.\n",
465 if (eq(wd, "local")) {
469 if (eq(wd, "no-depend")) {
473 if (eq(wd, "profiling-routine")) {
474 filetype = PROFILING;
477 if (eq(wd, "nowerror")) {
481 STAILQ_FOREACH(dp, &dtab, d_next)
482 if (eq(dp->d_name, wd)) {
483 dp->d_done |= DEVDONE;
487 printf("%s: mandatory device \"%s\" not found\n",
492 printf("standard entry %s has a device keyword - %s!\n",
496 SLIST_FOREACH(op, &opt, op_next)
497 if (op->op_value == 0 && opteq(op->op_name, wd))
503 if (std == 0 && nreqs == 0) {
504 printf("%s: what is %s optional on?\n",
510 printf("%s: syntax error describing %s\n",
514 if (filetype == PROFILING && profiling == 0)
518 tp->f_type = filetype;
520 tp->f_flags |= NO_IMPLCT_RULE;
522 tp->f_flags |= NO_OBJ;
524 tp->f_flags |= BEFORE_DEPEND;
526 tp->f_flags |= NOWERROR;
527 tp->f_compilewith = compilewith;
528 tp->f_depends = depends;
530 tp->f_warn = warning;
535 * Read in the information about files used in making the system.
536 * Store it in the ftab linked list.
541 char fname[MAXPATHLEN];
542 struct files_name *nl, *tnl;
544 (void) snprintf(fname, sizeof(fname), "../../conf/files");
546 (void) snprintf(fname, sizeof(fname),
547 "../../conf/files.%s", machinename);
549 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
550 read_file(nl->f_name);
551 tnl = STAILQ_NEXT(nl, f_next);
558 opteq(const char *cp, const char *dp)
562 for (; ; cp++, dp++) {
564 c = isupper(*cp) ? tolower(*cp) : *cp;
565 d = isupper(*dp) ? tolower(*dp) : *dp;
575 do_before_depend(FILE *fp)
577 struct file_list *tp;
580 fputs("BEFORE_DEPEND=", fp);
582 STAILQ_FOREACH(tp, &ftab, f_next)
583 if (tp->f_flags & BEFORE_DEPEND) {
584 len = strlen(tp->f_fn);
585 if ((len = 3 + len) + lpos > 72) {
589 if (tp->f_flags & NO_IMPLCT_RULE)
590 fprintf(fp, "%s ", tp->f_fn);
592 fprintf(fp, "$S/%s ", tp->f_fn);
602 struct file_list *tp;
606 fprintf(fp, "OBJS=");
608 STAILQ_FOREACH(tp, &ftab, f_next) {
609 if (tp->f_flags & NO_OBJ)
612 cp = sp + (len = strlen(sp)) - 1;
615 if (len + lpos > 72) {
617 fprintf(fp, "\\\n\t");
619 fprintf(fp, "%s ", sp);
628 do_xxfiles(char *tag, FILE *fp)
630 struct file_list *tp;
634 if (tag[strlen(tag) - 1] == '\n')
635 tag[strlen(tag) - 1] = '\0';
642 fprintf(fp, "%sFILES=", SUFF);
644 STAILQ_FOREACH(tp, &ftab, f_next)
645 if (tp->f_type != NODEPEND) {
646 len = strlen(tp->f_fn);
647 if (tp->f_fn[len - slen - 1] != '.')
649 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
651 if ((len = 3 + len) + lpos > 72) {
655 if (tp->f_type != LOCAL)
656 fprintf(fp, "$S/%s ", tp->f_fn);
658 fprintf(fp, "%s ", tp->f_fn);
670 cp = rindex(fn, '/');
677 * Create the makerules for each file
678 * which is part of the system.
684 struct file_list *ftp;
687 STAILQ_FOREACH(ftp, &ftab, f_next) {
689 printf("WARNING: %s\n", ftp->f_warn);
690 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
692 if (ftp->f_flags & NO_IMPLCT_RULE) {
694 fprintf(f, "%s: %s\n", np, ftp->f_depends);
696 fprintf(f, "%s: \n", np);
701 fprintf(f, "%so:\n\t-cp $S/%so .\n\n",
705 if (ftp->f_depends) {
706 fprintf(f, "%sln: $S/%s%c %s\n", tail(np),
707 np, och, ftp->f_depends);
708 fprintf(f, "\t${NORMAL_LINT}\n\n");
709 fprintf(f, "%so: $S/%s%c %s\n", tail(np),
710 np, och, ftp->f_depends);
713 fprintf(f, "%sln: $S/%s%c\n", tail(np),
715 fprintf(f, "\t${NORMAL_LINT}\n\n");
716 fprintf(f, "%so: $S/%s%c\n", tail(np),
720 compilewith = ftp->f_compilewith;
721 if (compilewith == 0) {
722 const char *ftype = NULL;
723 static char cmd[128];
725 switch (ftp->f_type) {
738 printf("config: don't know rules for %s\n", np);
741 snprintf(cmd, sizeof(cmd), "${%s_%c%s}\n.if defined(NORMAL_CTFCONVERT) && !empty(NORMAL_CTFCONVERT)\n\t${NORMAL_CTFCONVERT}\n.endif", ftype,
743 ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
747 fprintf(f, "\t%s\n\n", compilewith);
754 struct file_list *tp;
759 STAILQ_FOREACH(tp, &ftab, f_next)
761 len = strlen(tp->f_clean);
762 if (len + lpos > 72) {
766 fprintf(fp, "%s ", tp->f_clean);
780 *str = toupper(*str);