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");
340 moveifchanged(path("env.c.new"), path("env.c"));
344 read_file(char *fname)
346 char ifname[MAXPATHLEN];
348 struct file_list *tp;
351 char *wd, *this, *compilewith, *depends, *clean, *warning;
352 const char *objprefix;
353 int compile, match, nreqs, std, filetype, not,
354 imp_rule, no_obj, before_depend, nowerror;
356 fp = fopen(fname, "r");
362 * filename [ standard | optional ]
363 * [ dev* [ | dev* ... ] | profiling-routine ] [ no-obj ]
364 * [ compile-with "compile rule" [no-implicit-rule] ]
365 * [ dependency "dependency-list"] [ before-depend ]
366 * [ clean "file-list"] [ warning "text warning" ]
367 * [ obj-prefix "file prefix"]
370 if (wd == (char *)EOF) {
378 while (((wd = get_word(fp)) != (char *)EOF) && wd)
382 if (eq(wd, "include")) {
383 wd = get_quoted_word(fp);
384 if (wd == (char *)EOF || wd == NULL)
385 errout("%s: missing include filename.\n", fname);
386 (void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
388 while (((wd = get_word(fp)) != (char *)EOF) && wd)
394 if (wd == (char *)EOF)
397 errout("%s: No type for %s.\n", fname, this);
398 tp = fl_lookup(this);
414 if (eq(wd, "standard"))
416 else if (!eq(wd, "optional"))
417 errout("%s: \"%s\" %s must be optional or standard\n",
419 for (wd = get_word(fp); wd; wd = get_word(fp)) {
420 if (wd == (char *)EOF)
428 errout("%s: syntax error describing %s\n",
435 if (eq(wd, "no-obj")) {
439 if (eq(wd, "no-implicit-rule")) {
440 if (compilewith == NULL)
441 errout("%s: alternate rule required when "
442 "\"no-implicit-rule\" is specified for"
448 if (eq(wd, "before-depend")) {
452 if (eq(wd, "dependency")) {
453 wd = get_quoted_word(fp);
454 if (wd == (char *)EOF || wd == NULL)
455 errout("%s: %s missing dependency string.\n",
460 if (eq(wd, "clean")) {
461 wd = get_quoted_word(fp);
462 if (wd == (char *)EOF || wd == NULL)
463 errout("%s: %s missing clean file list.\n",
468 if (eq(wd, "compile-with")) {
469 wd = get_quoted_word(fp);
470 if (wd == (char *)EOF || wd == NULL)
471 errout("%s: %s missing compile command string.\n",
473 compilewith = ns(wd);
476 if (eq(wd, "warning")) {
477 wd = get_quoted_word(fp);
478 if (wd == (char *)EOF || wd == NULL)
479 errout("%s: %s missing warning text string.\n",
484 if (eq(wd, "obj-prefix")) {
485 wd = get_quoted_word(fp);
486 if (wd == (char *)EOF || wd == NULL)
487 errout("%s: %s missing object prefix string.\n",
492 if (eq(wd, "nowerror")) {
496 if (eq(wd, "local")) {
500 if (eq(wd, "no-depend")) {
505 if (eq(wd, "profiling-routine")) {
506 filetype = PROFILING;
510 errout("standard entry %s has optional inclusion specifier %s!\n",
512 STAILQ_FOREACH(dp, &dtab, d_next)
513 if (eq(dp->d_name, wd)) {
517 dp->d_done |= DEVDONE;
520 SLIST_FOREACH(op, &opt, op_next)
521 if (op->op_value == 0 && opteq(op->op_name, wd)) {
531 if (compile && tp == NULL) {
532 if (std == 0 && nreqs == 0)
533 errout("%s: what is %s optional on?\n",
535 if (filetype == PROFILING && profiling == 0)
539 tp->f_type = filetype;
540 if (filetype == LOCAL)
541 tp->f_srcprefix = "";
543 tp->f_srcprefix = "$S/";
545 tp->f_flags |= NO_IMPLCT_RULE;
547 tp->f_flags |= NO_OBJ;
549 tp->f_flags |= BEFORE_DEPEND;
551 tp->f_flags |= NOWERROR;
552 tp->f_compilewith = compilewith;
553 tp->f_depends = depends;
555 tp->f_warn = warning;
556 tp->f_objprefix = objprefix;
562 * Read in the information about files used in making the system.
563 * Store it in the ftab linked list.
568 char fname[MAXPATHLEN];
569 struct files_name *nl, *tnl;
571 (void) snprintf(fname, sizeof(fname), "../../conf/files");
573 (void) snprintf(fname, sizeof(fname),
574 "../../conf/files.%s", machinename);
576 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
577 read_file(nl->f_name);
578 tnl = STAILQ_NEXT(nl, f_next);
585 opteq(const char *cp, const char *dp)
589 for (; ; cp++, dp++) {
591 c = isupper(*cp) ? tolower(*cp) : *cp;
592 d = isupper(*dp) ? tolower(*dp) : *dp;
602 do_before_depend(FILE *fp)
604 struct file_list *tp;
607 fputs("BEFORE_DEPEND=", fp);
609 STAILQ_FOREACH(tp, &ftab, f_next)
610 if (tp->f_flags & BEFORE_DEPEND) {
611 len = strlen(tp->f_fn);
612 if ((len = 3 + len) + lpos > 72) {
616 if (tp->f_flags & NO_IMPLCT_RULE)
617 fprintf(fp, "%s ", tp->f_fn);
619 fprintf(fp, "%s%s ", tp->f_srcprefix,
630 struct file_list *tp;
634 fprintf(fp, "OBJS=");
636 STAILQ_FOREACH(tp, &ftab, f_next) {
637 if (tp->f_flags & NO_OBJ)
640 cp = sp + (len = strlen(sp)) - 1;
643 len += strlen(tp->f_objprefix);
644 if (len + lpos > 72) {
646 fprintf(fp, "\\\n\t");
648 fprintf(fp, "%s%s ", tp->f_objprefix, sp);
657 do_xxfiles(char *tag, FILE *fp)
659 struct file_list *tp;
663 if (tag[strlen(tag) - 1] == '\n')
664 tag[strlen(tag) - 1] = '\0';
671 fprintf(fp, "%sFILES=", SUFF);
674 STAILQ_FOREACH(tp, &ftab, f_next)
675 if (tp->f_type != NODEPEND) {
676 len = strlen(tp->f_fn);
677 if (tp->f_fn[len - slen - 1] != '.')
679 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
681 if ((len = 3 + len) + lpos > 72) {
685 fprintf(fp, "%s%s ", tp->f_srcprefix, tp->f_fn);
698 cp = strrchr(fn, '/');
705 * Create the makerules for each file
706 * which is part of the system.
712 struct file_list *ftp;
716 STAILQ_FOREACH(ftp, &ftab, f_next) {
718 fprintf(stderr, "WARNING: %s\n", ftp->f_warn);
719 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
721 if (ftp->f_flags & NO_IMPLCT_RULE) {
723 fprintf(f, "%s%s: %s\n",
724 ftp->f_objprefix, np, ftp->f_depends);
726 fprintf(f, "%s%s: \n", ftp->f_objprefix, np);
731 fprintf(f, "%s%so:\n\t-cp %s%so .\n\n",
732 ftp->f_objprefix, tail(np),
733 ftp->f_srcprefix, np);
736 if (ftp->f_depends) {
737 fprintf(f, "%s%so: %s%s%c %s\n",
738 ftp->f_objprefix, tail(np),
739 ftp->f_srcprefix, np, och,
743 fprintf(f, "%s%so: %s%s%c\n",
744 ftp->f_objprefix, tail(np),
745 ftp->f_srcprefix, np, och);
748 compilewith = ftp->f_compilewith;
749 if (compilewith == NULL) {
750 const char *ftype = NULL;
752 switch (ftp->f_type) {
763 "config: don't know rules for %s\n", np);
766 snprintf(cmd, sizeof(cmd),
769 ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
773 if (strlen(ftp->f_objprefix))
774 fprintf(f, "\t%s %s%s\n", compilewith,
775 ftp->f_srcprefix, np);
777 fprintf(f, "\t%s\n", compilewith);
779 if (!(ftp->f_flags & NO_OBJ))
780 fprintf(f, "\t${NORMAL_CTFCONVERT}\n\n");
789 struct file_list *tp;
794 STAILQ_FOREACH(tp, &ftab, f_next)
796 len = strlen(tp->f_clean);
797 if (len + lpos > 72) {
801 fprintf(fp, "%s ", tp->f_clean);
815 *str = toupper(*str);