2 * Copyright (c) 1980, 1990, 1993
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.
49 #include <sys/param.h>
52 #include "configvers.h"
54 static char *tail(char *);
55 static void do_clean(FILE *);
56 static void do_rules(FILE *);
57 static void do_xxfiles(char *, FILE *);
58 static void do_objs(FILE *);
59 static void do_before_depend(FILE *);
60 static int opteq(const char *, const char *);
61 static void read_files(void);
63 static void errout(const char *fmt, ...)
68 vfprintf(stderr, fmt, ap);
74 * Lookup a file, by name.
76 static struct file_list *
81 STAILQ_FOREACH(fp, &ftab, f_next) {
82 if (eq(fp->f_fn, file))
89 * Make a new file list entry
91 static struct file_list *
96 fp = (struct file_list *) calloc(1, sizeof *fp);
98 err(EXIT_FAILURE, "calloc");
99 STAILQ_INSERT_TAIL(&ftab, fp, f_next);
104 * Open the correct Makefile and return it, or error out.
107 open_makefile_template(void)
112 snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
113 ifp = fopen(line, "r");
115 snprintf(line, sizeof(line), "Makefile.%s", machinename);
116 ifp = fopen(line, "r");
124 * Build the makefile from the skeleton
134 ifp = open_makefile_template();
135 ofp = fopen(path("Makefile.new"), "w");
137 err(1, "%s", path("Makefile.new"));
138 fprintf(ofp, "KERN_IDENT=%s\n", ident);
139 fprintf(ofp, "MACHINE=%s\n", machinename);
140 fprintf(ofp, "MACHINE_ARCH=%s\n", machinearch);
141 SLIST_FOREACH_SAFE(op, &mkopt, op_next, t) {
142 fprintf(ofp, "%s=%s", op->op_name, op->op_value);
143 while ((op = SLIST_NEXT(op, op_append)) != NULL)
144 fprintf(ofp, " %s", op->op_value);
148 fprintf(ofp, "DEBUG=-g\n");
150 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
152 fprintf(ofp,"S=%s\n", srcdir);
153 while (fgets(line, BUFSIZ, ifp) != NULL) {
155 fprintf(ofp, "%s", line);
158 if (eq(line, "%BEFORE_DEPEND\n"))
159 do_before_depend(ofp);
160 else if (eq(line, "%OBJS\n"))
162 else if (strncmp(line, "%FILES.", 7) == 0)
163 do_xxfiles(line, ofp);
164 else if (eq(line, "%RULES\n"))
166 else if (eq(line, "%CLEAN\n"))
168 else if (strncmp(line, "%VERSREQ=", 9) == 0)
169 line[0] = '\0'; /* handled elsewhere */
172 "Unknown %% construct in generic makefile: %s",
177 moveifchanged(path("Makefile.new"), path("Makefile"));
181 * Build hints.c from the skeleton
191 ofp = fopen(path("hints.c.new"), "w");
193 err(1, "%s", path("hints.c.new"));
194 fprintf(ofp, "#include <sys/types.h>\n");
195 fprintf(ofp, "#include <sys/systm.h>\n");
197 fprintf(ofp, "int hintmode = %d;\n", hintmode);
198 fprintf(ofp, "char static_hints[] = {\n");
199 STAILQ_FOREACH(hint, &hints, hint_next) {
200 ifp = fopen(hint->hint_name, "r");
202 err(1, "%s", hint->hint_name);
203 while (fgets(line, BUFSIZ, ifp) != NULL) {
204 /* zap trailing CR and/or LF */
205 while ((s = strrchr(line, '\n')) != NULL)
207 while ((s = strrchr(line, '\r')) != NULL)
209 /* remove # comments */
210 s = strchr(line, '#');
213 /* remove any whitespace and " characters */
216 if (*s == ' ' || *s == '\t' || *s == '"') {
230 fprintf(ofp, "\"%s\\0\"\n", line);
234 fprintf(ofp, "\"\\0\"\n};\n");
236 moveifchanged(path("hints.c.new"), path("hints.c"));
240 * Build env.c from the skeleton
250 ifp = fopen(env, "r");
256 ofp = fopen(path("env.c.new"), "w");
258 err(1, "%s", path("env.c.new"));
259 fprintf(ofp, "#include <sys/types.h>\n");
260 fprintf(ofp, "#include <sys/systm.h>\n");
262 fprintf(ofp, "int envmode = %d;\n", envmode);
263 fprintf(ofp, "char static_env[] = {\n");
265 while (fgets(line, BUFSIZ, ifp) != NULL) {
266 /* zap trailing CR and/or LF */
267 while ((s = strrchr(line, '\n')) != NULL)
269 while ((s = strrchr(line, '\r')) != NULL)
271 /* remove # comments */
272 s = strchr(line, '#');
275 /* remove any whitespace and " characters */
278 if (*s == ' ' || *s == '\t' || *s == '"') {
292 fprintf(ofp, "\"%s\\0\"\n", line);
295 fprintf(ofp, "\"\\0\"\n};\n");
299 moveifchanged(path("env.c.new"), path("env.c"));
303 read_file(char *fname)
305 char ifname[MAXPATHLEN];
307 struct file_list *tp;
310 char *wd, *this, *compilewith, *depends, *clean, *warning;
311 const char *objprefix;
312 int compile, match, nreqs, std, filetype, not,
313 imp_rule, no_obj, before_depend, nowerror;
315 fp = fopen(fname, "r");
321 * filename [ standard | optional ]
322 * [ dev* [ | dev* ... ] | profiling-routine ] [ no-obj ]
323 * [ compile-with "compile rule" [no-implicit-rule] ]
324 * [ dependency "dependency-list"] [ before-depend ]
325 * [ clean "file-list"] [ warning "text warning" ]
326 * [ obj-prefix "file prefix"]
329 if (wd == (char *)EOF) {
337 while (((wd = get_word(fp)) != (char *)EOF) && wd)
341 if (eq(wd, "include")) {
342 wd = get_quoted_word(fp);
343 if (wd == (char *)EOF || wd == 0)
344 errout("%s: missing include filename.\n", fname);
345 (void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
347 while (((wd = get_word(fp)) != (char *)EOF) && wd)
353 if (wd == (char *)EOF)
356 errout("%s: No type for %s.\n", fname, this);
357 tp = fl_lookup(this);
373 if (eq(wd, "standard"))
375 else if (!eq(wd, "optional"))
376 errout("%s: \"%s\" %s must be optional or standard\n",
378 for (wd = get_word(fp); wd; wd = get_word(fp)) {
379 if (wd == (char *)EOF)
387 errout("%s: syntax error describing %s\n",
394 if (eq(wd, "no-obj")) {
398 if (eq(wd, "no-implicit-rule")) {
399 if (compilewith == 0)
400 errout("%s: alternate rule required when "
401 "\"no-implicit-rule\" is specified for"
407 if (eq(wd, "before-depend")) {
411 if (eq(wd, "dependency")) {
412 wd = get_quoted_word(fp);
413 if (wd == (char *)EOF || wd == 0)
414 errout("%s: %s missing dependency string.\n",
419 if (eq(wd, "clean")) {
420 wd = get_quoted_word(fp);
421 if (wd == (char *)EOF || wd == 0)
422 errout("%s: %s missing clean file list.\n",
427 if (eq(wd, "compile-with")) {
428 wd = get_quoted_word(fp);
429 if (wd == (char *)EOF || wd == 0)
430 errout("%s: %s missing compile command string.\n",
432 compilewith = ns(wd);
435 if (eq(wd, "warning")) {
436 wd = get_quoted_word(fp);
437 if (wd == (char *)EOF || wd == 0)
438 errout("%s: %s missing warning text string.\n",
443 if (eq(wd, "obj-prefix")) {
444 wd = get_quoted_word(fp);
445 if (wd == (char *)EOF || wd == 0)
446 errout("%s: %s missing object prefix string.\n",
451 if (eq(wd, "nowerror")) {
455 if (eq(wd, "local")) {
459 if (eq(wd, "no-depend")) {
464 if (eq(wd, "profiling-routine")) {
465 filetype = PROFILING;
469 errout("standard entry %s has optional inclusion specifier %s!\n",
471 STAILQ_FOREACH(dp, &dtab, d_next)
472 if (eq(dp->d_name, wd)) {
476 dp->d_done |= DEVDONE;
479 SLIST_FOREACH(op, &opt, op_next)
480 if (op->op_value == 0 && opteq(op->op_name, wd)) {
490 if (compile && tp == NULL) {
491 if (std == 0 && nreqs == 0)
492 errout("%s: what is %s optional on?\n",
494 if (filetype == PROFILING && profiling == 0)
498 tp->f_type = filetype;
500 tp->f_flags |= NO_IMPLCT_RULE;
502 tp->f_flags |= NO_OBJ;
504 tp->f_flags |= BEFORE_DEPEND;
506 tp->f_flags |= NOWERROR;
507 tp->f_compilewith = compilewith;
508 tp->f_depends = depends;
510 tp->f_warn = warning;
511 tp->f_objprefix = objprefix;
517 * Read in the information about files used in making the system.
518 * Store it in the ftab linked list.
523 char fname[MAXPATHLEN];
524 struct files_name *nl, *tnl;
526 (void) snprintf(fname, sizeof(fname), "../../conf/files");
528 (void) snprintf(fname, sizeof(fname),
529 "../../conf/files.%s", machinename);
531 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
532 read_file(nl->f_name);
533 tnl = STAILQ_NEXT(nl, f_next);
540 opteq(const char *cp, const char *dp)
544 for (; ; cp++, dp++) {
546 c = isupper(*cp) ? tolower(*cp) : *cp;
547 d = isupper(*dp) ? tolower(*dp) : *dp;
557 do_before_depend(FILE *fp)
559 struct file_list *tp;
562 fputs("BEFORE_DEPEND=", fp);
564 STAILQ_FOREACH(tp, &ftab, f_next)
565 if (tp->f_flags & BEFORE_DEPEND) {
566 len = strlen(tp->f_fn);
567 if ((len = 3 + len) + lpos > 72) {
571 if (tp->f_flags & NO_IMPLCT_RULE)
572 fprintf(fp, "%s ", tp->f_fn);
574 fprintf(fp, "$S/%s ", tp->f_fn);
584 struct file_list *tp;
588 fprintf(fp, "OBJS=");
590 STAILQ_FOREACH(tp, &ftab, f_next) {
591 if (tp->f_flags & NO_OBJ)
594 cp = sp + (len = strlen(sp)) - 1;
597 len += strlen(tp->f_objprefix);
598 if (len + lpos > 72) {
600 fprintf(fp, "\\\n\t");
602 fprintf(fp, "%s%s ", tp->f_objprefix, sp);
611 do_xxfiles(char *tag, FILE *fp)
613 struct file_list *tp;
617 if (tag[strlen(tag) - 1] == '\n')
618 tag[strlen(tag) - 1] = '\0';
625 fprintf(fp, "%sFILES=", SUFF);
627 STAILQ_FOREACH(tp, &ftab, f_next)
628 if (tp->f_type != NODEPEND) {
629 len = strlen(tp->f_fn);
630 if (tp->f_fn[len - slen - 1] != '.')
632 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
634 if ((len = 3 + len) + lpos > 72) {
638 if (tp->f_type != LOCAL)
639 fprintf(fp, "$S/%s ", tp->f_fn);
641 fprintf(fp, "%s ", tp->f_fn);
653 cp = strrchr(fn, '/');
660 * Create the makerules for each file
661 * which is part of the system.
667 struct file_list *ftp;
671 STAILQ_FOREACH(ftp, &ftab, f_next) {
673 fprintf(stderr, "WARNING: %s\n", ftp->f_warn);
674 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
676 if (ftp->f_flags & NO_IMPLCT_RULE) {
678 fprintf(f, "%s%s: %s\n",
679 ftp->f_objprefix, np, ftp->f_depends);
681 fprintf(f, "%s%s: \n", ftp->f_objprefix, np);
686 fprintf(f, "%s%so:\n\t-cp $S/%so .\n\n",
687 ftp->f_objprefix, tail(np), np);
690 if (ftp->f_depends) {
691 fprintf(f, "%s%sln: $S/%s%c %s\n",
692 ftp->f_objprefix, tail(np), np, och,
694 fprintf(f, "\t${NORMAL_LINT}\n\n");
695 fprintf(f, "%s%so: $S/%s%c %s\n",
696 ftp->f_objprefix, tail(np), np, och,
700 fprintf(f, "%s%sln: $S/%s%c\n",
701 ftp->f_objprefix, tail(np), np, och);
702 fprintf(f, "\t${NORMAL_LINT}\n\n");
703 fprintf(f, "%s%so: $S/%s%c\n",
704 ftp->f_objprefix, tail(np), np, och);
707 compilewith = ftp->f_compilewith;
708 if (compilewith == 0) {
709 const char *ftype = NULL;
711 switch (ftp->f_type) {
722 "config: don't know rules for %s\n", np);
725 snprintf(cmd, sizeof(cmd),
728 ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
732 if (strlen(ftp->f_objprefix))
733 fprintf(f, "\t%s $S/%s\n", compilewith, np);
735 fprintf(f, "\t%s\n", compilewith);
737 if (!(ftp->f_flags & NO_OBJ))
738 fprintf(f, "\t${NORMAL_CTFCONVERT}\n\n");
747 struct file_list *tp;
752 STAILQ_FOREACH(tp, &ftab, f_next)
754 len = strlen(tp->f_clean);
755 if (len + lpos > 72) {
759 fprintf(fp, "%s ", tp->f_clean);
773 *str = toupper(*str);