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, ...)
67 vfprintf(stderr, fmt, ap);
73 * Lookup a file, by name.
75 static struct file_list *
80 STAILQ_FOREACH(fp, &ftab, f_next) {
81 if (eq(fp->f_fn, file))
88 * Make a new file list entry
90 static struct file_list *
95 fp = (struct file_list *) calloc(1, sizeof *fp);
97 err(EXIT_FAILURE, "calloc");
98 STAILQ_INSERT_TAIL(&ftab, fp, f_next);
103 * Open the correct Makefile and return it, or error out.
106 open_makefile_template(void)
111 snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
112 ifp = fopen(line, "r");
114 snprintf(line, sizeof(line), "Makefile.%s", machinename);
115 ifp = fopen(line, "r");
123 * Build the makefile from the skeleton
133 ifp = open_makefile_template();
134 ofp = fopen(path("Makefile.new"), "w");
136 err(1, "%s", path("Makefile.new"));
137 fprintf(ofp, "KERN_IDENT=%s\n", ident);
138 fprintf(ofp, "MACHINE=%s\n", machinename);
139 fprintf(ofp, "MACHINE_ARCH=%s\n", machinearch);
140 SLIST_FOREACH_SAFE(op, &mkopt, op_next, t) {
141 fprintf(ofp, "%s=%s", op->op_name, op->op_value);
142 while ((op = SLIST_NEXT(op, op_append)) != NULL)
143 fprintf(ofp, " %s", op->op_value);
147 fprintf(ofp, "DEBUG=-g\n");
149 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
151 fprintf(ofp,"S=%s\n", srcdir);
152 while (fgets(line, BUFSIZ, ifp) != NULL) {
154 fprintf(ofp, "%s", line);
157 if (eq(line, "%BEFORE_DEPEND\n"))
158 do_before_depend(ofp);
159 else if (eq(line, "%OBJS\n"))
161 else if (strncmp(line, "%FILES.", 7) == 0)
162 do_xxfiles(line, ofp);
163 else if (eq(line, "%RULES\n"))
165 else if (eq(line, "%CLEAN\n"))
167 else if (strncmp(line, "%VERSREQ=", 9) == 0)
168 line[0] = '\0'; /* handled elsewhere */
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) != NULL) {
203 /* zap trailing CR and/or LF */
204 while ((s = strrchr(line, '\n')) != NULL)
206 while ((s = strrchr(line, '\r')) != NULL)
208 /* remove # comments */
209 s = strchr(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) != NULL) {
265 /* zap trailing CR and/or LF */
266 while ((s = strrchr(line, '\n')) != NULL)
268 while ((s = strrchr(line, '\r')) != NULL)
270 /* remove # comments */
271 s = strchr(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 const char *objprefix;
311 int compile, match, nreqs, std, filetype,
312 imp_rule, no_obj, before_depend, nowerror;
314 fp = fopen(fname, "r");
320 * filename [ standard | optional ]
321 * [ dev* [ | dev* ... ] | profiling-routine ] [ no-obj ]
322 * [ compile-with "compile rule" [no-implicit-rule] ]
323 * [ dependency "dependency-list"] [ before-depend ]
324 * [ clean "file-list"] [ warning "text warning" ]
325 * [ obj-prefix "file prefix"]
328 if (wd == (char *)EOF) {
336 while (((wd = get_word(fp)) != (char *)EOF) && wd)
340 if (eq(wd, "include")) {
341 wd = get_quoted_word(fp);
342 if (wd == (char *)EOF || wd == 0)
343 errout("%s: missing include filename.\n", fname);
344 (void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
346 while (((wd = get_word(fp)) != (char *)EOF) && wd)
352 if (wd == (char *)EOF)
355 errout("%s: No type for %s.\n", fname, this);
356 tp = fl_lookup(this);
371 if (eq(wd, "standard"))
373 else if (!eq(wd, "optional"))
374 errout("%s: \"%s\" %s must be optional or standard\n",
378 if (wd == (char *)EOF)
382 if (compile && tp == NULL) {
383 if (std == 0 && nreqs == 0)
384 errout("%s: what is %s optional on?\n",
386 if (filetype == PROFILING && profiling == 0)
390 tp->f_type = filetype;
392 tp->f_flags |= NO_IMPLCT_RULE;
394 tp->f_flags |= NO_OBJ;
396 tp->f_flags |= BEFORE_DEPEND;
398 tp->f_flags |= NOWERROR;
399 tp->f_compilewith = compilewith;
400 tp->f_depends = depends;
402 tp->f_warn = warning;
403 tp->f_objprefix = objprefix;
409 errout("%s: syntax error describing %s\n",
416 if (eq(wd, "no-obj")) {
420 if (eq(wd, "no-implicit-rule")) {
421 if (compilewith == 0)
422 errout("%s: alternate rule required when "
423 "\"no-implicit-rule\" is specified for %s.\n",
428 if (eq(wd, "before-depend")) {
432 if (eq(wd, "dependency")) {
433 wd = get_quoted_word(fp);
434 if (wd == (char *)EOF || wd == 0)
435 errout("%s: %s missing dependency string.\n",
440 if (eq(wd, "clean")) {
441 wd = get_quoted_word(fp);
442 if (wd == (char *)EOF || wd == 0)
443 errout("%s: %s missing clean file list.\n",
448 if (eq(wd, "compile-with")) {
449 wd = get_quoted_word(fp);
450 if (wd == (char *)EOF || wd == 0)
451 errout("%s: %s missing compile command string.\n",
453 compilewith = ns(wd);
456 if (eq(wd, "warning")) {
457 wd = get_quoted_word(fp);
458 if (wd == (char *)EOF || wd == 0)
459 errout("%s: %s missing warning text string.\n",
464 if (eq(wd, "obj-prefix")) {
465 wd = get_quoted_word(fp);
466 if (wd == (char *)EOF || wd == 0)
467 errout("%s: %s missing object prefix string.\n",
472 if (eq(wd, "local")) {
476 if (eq(wd, "no-depend")) {
480 if (eq(wd, "nowerror")) {
485 /* Hack to allow "optional profiling-routine" to work */
486 if (eq(wd, "profiling-routine")) {
487 filetype = PROFILING;
491 errout("standard entry %s has optional inclusion specifier %s!\n",
493 STAILQ_FOREACH(dp, &dtab, d_next)
494 if (eq(dp->d_name, wd)) {
495 dp->d_done |= DEVDONE;
498 SLIST_FOREACH(op, &opt, op_next)
499 if (op->op_value == 0 && opteq(op->op_name, wd))
506 * Read in the information about files used in making the system.
507 * Store it in the ftab linked list.
512 char fname[MAXPATHLEN];
513 struct files_name *nl, *tnl;
515 (void) snprintf(fname, sizeof(fname), "../../conf/files");
517 (void) snprintf(fname, sizeof(fname),
518 "../../conf/files.%s", machinename);
520 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
521 read_file(nl->f_name);
522 tnl = STAILQ_NEXT(nl, f_next);
529 opteq(const char *cp, const char *dp)
533 for (; ; cp++, dp++) {
535 c = isupper(*cp) ? tolower(*cp) : *cp;
536 d = isupper(*dp) ? tolower(*dp) : *dp;
546 do_before_depend(FILE *fp)
548 struct file_list *tp;
551 fputs("BEFORE_DEPEND=", fp);
553 STAILQ_FOREACH(tp, &ftab, f_next)
554 if (tp->f_flags & BEFORE_DEPEND) {
555 len = strlen(tp->f_fn);
556 if ((len = 3 + len) + lpos > 72) {
560 if (tp->f_flags & NO_IMPLCT_RULE)
561 fprintf(fp, "%s ", tp->f_fn);
563 fprintf(fp, "$S/%s ", tp->f_fn);
573 struct file_list *tp;
577 fprintf(fp, "OBJS=");
579 STAILQ_FOREACH(tp, &ftab, f_next) {
580 if (tp->f_flags & NO_OBJ)
583 cp = sp + (len = strlen(sp)) - 1;
586 len += strlen(tp->f_objprefix);
587 if (len + lpos > 72) {
589 fprintf(fp, "\\\n\t");
591 fprintf(fp, "%s%s ", tp->f_objprefix, sp);
600 do_xxfiles(char *tag, FILE *fp)
602 struct file_list *tp;
606 if (tag[strlen(tag) - 1] == '\n')
607 tag[strlen(tag) - 1] = '\0';
614 fprintf(fp, "%sFILES=", SUFF);
616 STAILQ_FOREACH(tp, &ftab, f_next)
617 if (tp->f_type != NODEPEND) {
618 len = strlen(tp->f_fn);
619 if (tp->f_fn[len - slen - 1] != '.')
621 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
623 if ((len = 3 + len) + lpos > 72) {
627 if (tp->f_type != LOCAL)
628 fprintf(fp, "$S/%s ", tp->f_fn);
630 fprintf(fp, "%s ", tp->f_fn);
642 cp = strrchr(fn, '/');
649 * Create the makerules for each file
650 * which is part of the system.
656 struct file_list *ftp;
660 STAILQ_FOREACH(ftp, &ftab, f_next) {
662 fprintf(stderr, "WARNING: %s\n", ftp->f_warn);
663 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
665 if (ftp->f_flags & NO_IMPLCT_RULE) {
667 fprintf(f, "%s%s: %s\n",
668 ftp->f_objprefix, np, ftp->f_depends);
670 fprintf(f, "%s%s: \n", ftp->f_objprefix, np);
675 fprintf(f, "%s%so:\n\t-cp $S/%so .\n\n",
676 ftp->f_objprefix, tail(np), np);
679 if (ftp->f_depends) {
680 fprintf(f, "%s%sln: $S/%s%c %s\n",
681 ftp->f_objprefix, tail(np), np, och,
683 fprintf(f, "\t${NORMAL_LINT}\n\n");
684 fprintf(f, "%s%so: $S/%s%c %s\n",
685 ftp->f_objprefix, tail(np), np, och,
689 fprintf(f, "%s%sln: $S/%s%c\n",
690 ftp->f_objprefix, tail(np), np, och);
691 fprintf(f, "\t${NORMAL_LINT}\n\n");
692 fprintf(f, "%s%so: $S/%s%c\n",
693 ftp->f_objprefix, tail(np), np, och);
696 compilewith = ftp->f_compilewith;
697 if (compilewith == 0) {
698 const char *ftype = NULL;
700 switch (ftp->f_type) {
711 "config: don't know rules for %s\n", np);
714 snprintf(cmd, sizeof(cmd),
717 ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
721 if (strlen(ftp->f_objprefix))
722 fprintf(f, "\t%s $S/%s\n", compilewith, np);
724 fprintf(f, "\t%s\n", compilewith);
726 if (!(ftp->f_flags & NO_OBJ))
727 fprintf(f, "\t${NORMAL_CTFCONVERT}\n\n");
736 struct file_list *tp;
741 STAILQ_FOREACH(tp, &ftab, f_next)
743 len = strlen(tp->f_clean);
744 if (len + lpos > 72) {
748 fprintf(fp, "%s ", tp->f_clean);
762 *str = toupper(*str);