2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)mkmakefile.c 8.1 (Berkeley) 6/6/93";
36 static const char rcsid[] =
41 * Build the makefile for the system, from
42 * the information in the files files and the
43 * additional files for the machine being compiled to.
52 #include <sys/param.h>
55 #include "configvers.h"
57 static char *tail(char *);
58 static void do_clean(FILE *);
59 static void do_rules(FILE *);
60 static void do_xxfiles(char *, FILE *);
61 static void do_objs(FILE *);
62 static void do_before_depend(FILE *);
63 static int opteq(const char *, const char *);
64 static void read_files(void);
66 static void errout(const char *fmt, ...)
71 vfprintf(stderr, fmt, ap);
77 * Lookup a file, by name.
79 static struct file_list *
84 STAILQ_FOREACH(fp, &ftab, f_next) {
85 if (eq(fp->f_fn, file))
92 * Make a new file list entry
94 static struct file_list *
99 fp = (struct file_list *) calloc(1, sizeof *fp);
101 err(EXIT_FAILURE, "calloc");
102 STAILQ_INSERT_TAIL(&ftab, fp, f_next);
107 * Open the correct Makefile and return it, or error out.
110 open_makefile_template(void)
115 snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
116 ifp = fopen(line, "r");
118 snprintf(line, sizeof(line), "Makefile.%s", machinename);
119 ifp = fopen(line, "r");
127 * Build the makefile from the skeleton
137 ifp = open_makefile_template();
138 ofp = fopen(path("Makefile.new"), "w");
140 err(1, "%s", path("Makefile.new"));
141 fprintf(ofp, "KERN_IDENT=%s\n", ident);
142 fprintf(ofp, "MACHINE=%s\n", machinename);
143 fprintf(ofp, "MACHINE_ARCH=%s\n", machinearch);
144 SLIST_FOREACH_SAFE(op, &mkopt, op_next, t) {
145 fprintf(ofp, "%s=%s", op->op_name, op->op_value);
146 while ((op = SLIST_NEXT(op, op_append)) != NULL)
147 fprintf(ofp, " %s", op->op_value);
151 fprintf(ofp, "DEBUG=-g\n");
153 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
155 fprintf(ofp,"S=%s\n", srcdir);
156 while (fgets(line, BUFSIZ, ifp) != NULL) {
158 fprintf(ofp, "%s", line);
161 if (eq(line, "%BEFORE_DEPEND\n"))
162 do_before_depend(ofp);
163 else if (eq(line, "%OBJS\n"))
165 else if (strncmp(line, "%FILES.", 7) == 0)
166 do_xxfiles(line, ofp);
167 else if (eq(line, "%RULES\n"))
169 else if (eq(line, "%CLEAN\n"))
171 else if (strncmp(line, "%VERSREQ=", 9) == 0)
172 line[0] = '\0'; /* handled elsewhere */
175 "Unknown %% construct in generic makefile: %s",
180 moveifchanged(path("Makefile.new"), path("Makefile"));
184 * Build hints.c from the skeleton
194 ofp = fopen(path("hints.c.new"), "w");
196 err(1, "%s", path("hints.c.new"));
197 fprintf(ofp, "#include <sys/types.h>\n");
198 fprintf(ofp, "#include <sys/systm.h>\n");
200 fprintf(ofp, "int hintmode = %d;\n", hintmode);
201 fprintf(ofp, "char static_hints[] = {\n");
202 STAILQ_FOREACH(hint, &hints, hint_next) {
203 ifp = fopen(hint->hint_name, "r");
205 err(1, "%s", hint->hint_name);
206 while (fgets(line, BUFSIZ, ifp) != NULL) {
207 /* zap trailing CR and/or LF */
208 while ((s = strrchr(line, '\n')) != NULL)
210 while ((s = strrchr(line, '\r')) != NULL)
212 /* remove # comments */
213 s = strchr(line, '#');
216 /* remove any whitespace and " characters */
219 if (*s == ' ' || *s == '\t' || *s == '"') {
233 fprintf(ofp, "\"%s\\0\"\n", line);
237 fprintf(ofp, "\"\\0\"\n};\n");
239 moveifchanged(path("hints.c.new"), path("hints.c"));
243 sanitize_envline(char *result, const char *src)
249 /* If there is no '=' it's not a well-formed name=value line. */
250 if ((eq = strchr(src, '=')) == NULL) {
256 /* Copy chars before the '=', skipping any leading spaces/quotes. */
260 if (leading && (isspace(c) || c == '"'))
266 /* If it was all leading space, we don't have a well-formed line. */
272 /* Trim spaces/quotes immediately before the '=', then copy the '='. */
273 while (isspace(dst[-1]) || dst[-1] == '"')
277 /* Copy chars after the '=', skipping any leading whitespace. */
279 while ((c = *src++) != 0) {
280 if (leading && (isspace(c) || c == '"'))
286 /* If it was all leading space, it's a valid 'var=' (nil value). */
292 /* Trim trailing whitespace and quotes. */
293 while (isspace(dst[-1]) || dst[-1] == '"')
300 * Build env.c from the skeleton
306 char line[BUFSIZ], result[BUFSIZ], *linep;
307 struct envvar *envvar;
309 ofp = fopen(path("env.c.new"), "w");
311 err(1, "%s", path("env.c.new"));
312 fprintf(ofp, "#include <sys/types.h>\n");
313 fprintf(ofp, "#include <sys/systm.h>\n");
315 fprintf(ofp, "int envmode = %d;\n", envmode);
316 fprintf(ofp, "char static_env[] = {\n");
317 STAILQ_FOREACH(envvar, &envvars, envvar_next) {
318 if (envvar->env_is_file) {
319 ifp = fopen(envvar->env_str, "r");
321 err(1, "%s", envvar->env_str);
322 while (fgets(line, BUFSIZ, ifp) != NULL) {
323 sanitize_envline(result, line);
327 fprintf(ofp, "\"%s\\0\"\n", result);
331 linep = envvar->env_str;
332 sanitize_envline(result, linep);
335 fprintf(ofp, "\"%s\\0\"\n", result);
338 fprintf(ofp, "\"\\0\"\n};\n");
342 moveifchanged(path("env.c.new"), path("env.c"));
346 read_file(char *fname)
348 char ifname[MAXPATHLEN];
350 struct file_list *tp;
353 char *wd, *this, *compilewith, *depends, *clean, *warning;
354 const char *objprefix;
355 int compile, match, nreqs, std, filetype, not,
356 imp_rule, no_obj, before_depend, nowerror;
358 fp = fopen(fname, "r");
364 * filename [ standard | optional ]
365 * [ dev* [ | dev* ... ] | profiling-routine ] [ no-obj ]
366 * [ compile-with "compile rule" [no-implicit-rule] ]
367 * [ dependency "dependency-list"] [ before-depend ]
368 * [ clean "file-list"] [ warning "text warning" ]
369 * [ obj-prefix "file prefix"]
372 if (wd == (char *)EOF) {
380 while (((wd = get_word(fp)) != (char *)EOF) && wd)
384 if (eq(wd, "include")) {
385 wd = get_quoted_word(fp);
386 if (wd == (char *)EOF || wd == NULL)
387 errout("%s: missing include filename.\n", fname);
388 (void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
390 while (((wd = get_word(fp)) != (char *)EOF) && wd)
396 if (wd == (char *)EOF)
399 errout("%s: No type for %s.\n", fname, this);
400 tp = fl_lookup(this);
416 if (eq(wd, "standard"))
418 else if (!eq(wd, "optional"))
419 errout("%s: \"%s\" %s must be optional or standard\n",
421 for (wd = get_word(fp); wd; wd = get_word(fp)) {
422 if (wd == (char *)EOF)
430 errout("%s: syntax error describing %s\n",
437 if (eq(wd, "no-obj")) {
441 if (eq(wd, "no-implicit-rule")) {
442 if (compilewith == NULL)
443 errout("%s: alternate rule required when "
444 "\"no-implicit-rule\" is specified for"
450 if (eq(wd, "before-depend")) {
454 if (eq(wd, "dependency")) {
455 wd = get_quoted_word(fp);
456 if (wd == (char *)EOF || wd == NULL)
457 errout("%s: %s missing dependency string.\n",
462 if (eq(wd, "clean")) {
463 wd = get_quoted_word(fp);
464 if (wd == (char *)EOF || wd == NULL)
465 errout("%s: %s missing clean file list.\n",
470 if (eq(wd, "compile-with")) {
471 wd = get_quoted_word(fp);
472 if (wd == (char *)EOF || wd == NULL)
473 errout("%s: %s missing compile command string.\n",
475 compilewith = ns(wd);
478 if (eq(wd, "warning")) {
479 wd = get_quoted_word(fp);
480 if (wd == (char *)EOF || wd == NULL)
481 errout("%s: %s missing warning text string.\n",
486 if (eq(wd, "obj-prefix")) {
487 wd = get_quoted_word(fp);
488 if (wd == (char *)EOF || wd == NULL)
489 errout("%s: %s missing object prefix string.\n",
494 if (eq(wd, "nowerror")) {
498 if (eq(wd, "local")) {
502 if (eq(wd, "no-depend")) {
507 if (eq(wd, "profiling-routine")) {
508 filetype = PROFILING;
512 errout("standard entry %s has optional inclusion specifier %s!\n",
514 STAILQ_FOREACH(dp, &dtab, d_next)
515 if (eq(dp->d_name, wd)) {
519 dp->d_done |= DEVDONE;
522 SLIST_FOREACH(op, &opt, op_next)
523 if (op->op_value == 0 && opteq(op->op_name, wd)) {
533 if (compile && tp == NULL) {
534 if (std == 0 && nreqs == 0)
535 errout("%s: what is %s optional on?\n",
537 if (filetype == PROFILING && profiling == 0)
541 tp->f_type = filetype;
542 if (filetype == LOCAL)
543 tp->f_srcprefix = "";
545 tp->f_srcprefix = "$S/";
547 tp->f_flags |= NO_IMPLCT_RULE;
549 tp->f_flags |= NO_OBJ;
551 tp->f_flags |= BEFORE_DEPEND;
553 tp->f_flags |= NOWERROR;
554 tp->f_compilewith = compilewith;
555 tp->f_depends = depends;
557 tp->f_warn = warning;
558 tp->f_objprefix = objprefix;
564 * Read in the information about files used in making the system.
565 * Store it in the ftab linked list.
570 char fname[MAXPATHLEN];
571 struct files_name *nl, *tnl;
573 (void) snprintf(fname, sizeof(fname), "../../conf/files");
575 (void) snprintf(fname, sizeof(fname),
576 "../../conf/files.%s", machinename);
578 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
579 read_file(nl->f_name);
580 tnl = STAILQ_NEXT(nl, f_next);
587 opteq(const char *cp, const char *dp)
591 for (; ; cp++, dp++) {
593 c = isupper(*cp) ? tolower(*cp) : *cp;
594 d = isupper(*dp) ? tolower(*dp) : *dp;
604 do_before_depend(FILE *fp)
606 struct file_list *tp;
609 fputs("BEFORE_DEPEND=", fp);
611 STAILQ_FOREACH(tp, &ftab, f_next)
612 if (tp->f_flags & BEFORE_DEPEND) {
613 len = strlen(tp->f_fn);
614 if ((len = 3 + len) + lpos > 72) {
618 if (tp->f_flags & NO_IMPLCT_RULE)
619 fprintf(fp, "%s ", tp->f_fn);
621 fprintf(fp, "%s%s ", tp->f_srcprefix,
632 struct file_list *tp;
636 fprintf(fp, "OBJS=");
638 STAILQ_FOREACH(tp, &ftab, f_next) {
639 if (tp->f_flags & NO_OBJ)
642 cp = sp + (len = strlen(sp)) - 1;
645 len += strlen(tp->f_objprefix);
646 if (len + lpos > 72) {
648 fprintf(fp, "\\\n\t");
650 fprintf(fp, "%s%s ", tp->f_objprefix, sp);
659 do_xxfiles(char *tag, FILE *fp)
661 struct file_list *tp;
665 if (tag[strlen(tag) - 1] == '\n')
666 tag[strlen(tag) - 1] = '\0';
673 fprintf(fp, "%sFILES=", SUFF);
676 STAILQ_FOREACH(tp, &ftab, f_next)
677 if (tp->f_type != NODEPEND) {
678 len = strlen(tp->f_fn);
679 if (tp->f_fn[len - slen - 1] != '.')
681 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
683 if ((len = 3 + len) + lpos > 72) {
687 fprintf(fp, "%s%s ", tp->f_srcprefix, tp->f_fn);
700 cp = strrchr(fn, '/');
707 * Create the makerules for each file
708 * which is part of the system.
714 struct file_list *ftp;
718 STAILQ_FOREACH(ftp, &ftab, f_next) {
720 fprintf(stderr, "WARNING: %s\n", ftp->f_warn);
721 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
723 if (ftp->f_flags & NO_IMPLCT_RULE) {
725 fprintf(f, "%s%s: %s\n",
726 ftp->f_objprefix, np, ftp->f_depends);
728 fprintf(f, "%s%s: \n", ftp->f_objprefix, np);
733 fprintf(f, "%s%so:\n\t-cp %s%so .\n\n",
734 ftp->f_objprefix, tail(np),
735 ftp->f_srcprefix, np);
738 if (ftp->f_depends) {
739 fprintf(f, "%s%so: %s%s%c %s\n",
740 ftp->f_objprefix, tail(np),
741 ftp->f_srcprefix, np, och,
745 fprintf(f, "%s%so: %s%s%c\n",
746 ftp->f_objprefix, tail(np),
747 ftp->f_srcprefix, np, och);
750 compilewith = ftp->f_compilewith;
751 if (compilewith == NULL) {
752 const char *ftype = NULL;
754 switch (ftp->f_type) {
765 "config: don't know rules for %s\n", np);
768 snprintf(cmd, sizeof(cmd),
771 ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
775 if (strlen(ftp->f_objprefix))
776 fprintf(f, "\t%s %s%s\n", compilewith,
777 ftp->f_srcprefix, np);
779 fprintf(f, "\t%s\n", compilewith);
781 if (!(ftp->f_flags & NO_OBJ))
782 fprintf(f, "\t${NORMAL_CTFCONVERT}\n\n");
791 struct file_list *tp;
796 STAILQ_FOREACH(tp, &ftab, f_next)
798 len = strlen(tp->f_clean);
799 if (len + lpos > 72) {
803 fprintf(fp, "%s ", tp->f_clean);
817 *str = toupper(*str);