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.
51 #include <sys/param.h>
54 #include "configvers.h"
56 static char *tail(char *);
57 static void do_clean(FILE *);
58 static void do_rules(FILE *);
59 static void do_xxfiles(char *, FILE *);
60 static void do_objs(FILE *);
61 static void do_before_depend(FILE *);
62 static int opteq(const char *, const char *);
63 static void read_files(void);
65 static void errout(const char *fmt, ...)
70 vfprintf(stderr, fmt, ap);
76 * Lookup a file, by name.
78 static struct file_list *
83 STAILQ_FOREACH(fp, &ftab, f_next) {
84 if (eq(fp->f_fn, file))
91 * Make a new file list entry
93 static struct file_list *
98 fp = (struct file_list *) calloc(1, sizeof *fp);
100 err(EXIT_FAILURE, "calloc");
101 STAILQ_INSERT_TAIL(&ftab, fp, f_next);
106 * Open the correct Makefile and return it, or error out.
109 open_makefile_template(void)
114 snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
115 ifp = fopen(line, "r");
117 snprintf(line, sizeof(line), "Makefile.%s", machinename);
118 ifp = fopen(line, "r");
126 * Build the makefile from the skeleton
136 ifp = open_makefile_template();
137 ofp = fopen(path("Makefile.new"), "w");
139 err(1, "%s", path("Makefile.new"));
140 fprintf(ofp, "KERN_IDENT=%s\n", ident);
141 fprintf(ofp, "MACHINE=%s\n", machinename);
142 fprintf(ofp, "MACHINE_ARCH=%s\n", machinearch);
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) != NULL) {
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) != NULL) {
206 /* zap trailing CR and/or LF */
207 while ((s = strrchr(line, '\n')) != NULL)
209 while ((s = strrchr(line, '\r')) != NULL)
211 /* remove # comments */
212 s = strchr(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) != NULL) {
268 /* zap trailing CR and/or LF */
269 while ((s = strrchr(line, '\n')) != NULL)
271 while ((s = strrchr(line, '\r')) != NULL)
273 /* remove # comments */
274 s = strchr(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 const char *objprefix;
314 int compile, match, nreqs, std, filetype, not,
315 imp_rule, no_obj, before_depend, nowerror;
317 fp = fopen(fname, "r");
323 * filename [ standard | optional ]
324 * [ dev* [ | dev* ... ] | profiling-routine ] [ no-obj ]
325 * [ compile-with "compile rule" [no-implicit-rule] ]
326 * [ dependency "dependency-list"] [ before-depend ]
327 * [ clean "file-list"] [ warning "text warning" ]
328 * [ obj-prefix "file prefix"]
331 if (wd == (char *)EOF) {
339 while (((wd = get_word(fp)) != (char *)EOF) && wd)
343 if (eq(wd, "include")) {
344 wd = get_quoted_word(fp);
345 if (wd == (char *)EOF || wd == NULL)
346 errout("%s: missing include filename.\n", fname);
347 (void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
349 while (((wd = get_word(fp)) != (char *)EOF) && wd)
355 if (wd == (char *)EOF)
358 errout("%s: No type for %s.\n", fname, this);
359 tp = fl_lookup(this);
375 if (eq(wd, "standard"))
377 else if (!eq(wd, "optional"))
378 errout("%s: \"%s\" %s must be optional or standard\n",
380 for (wd = get_word(fp); wd; wd = get_word(fp)) {
381 if (wd == (char *)EOF)
389 errout("%s: syntax error describing %s\n",
396 if (eq(wd, "no-obj")) {
400 if (eq(wd, "no-implicit-rule")) {
401 if (compilewith == NULL)
402 errout("%s: alternate rule required when "
403 "\"no-implicit-rule\" is specified for"
409 if (eq(wd, "before-depend")) {
413 if (eq(wd, "dependency")) {
414 wd = get_quoted_word(fp);
415 if (wd == (char *)EOF || wd == NULL)
416 errout("%s: %s missing dependency string.\n",
421 if (eq(wd, "clean")) {
422 wd = get_quoted_word(fp);
423 if (wd == (char *)EOF || wd == NULL)
424 errout("%s: %s missing clean file list.\n",
429 if (eq(wd, "compile-with")) {
430 wd = get_quoted_word(fp);
431 if (wd == (char *)EOF || wd == NULL)
432 errout("%s: %s missing compile command string.\n",
434 compilewith = ns(wd);
437 if (eq(wd, "warning")) {
438 wd = get_quoted_word(fp);
439 if (wd == (char *)EOF || wd == NULL)
440 errout("%s: %s missing warning text string.\n",
445 if (eq(wd, "obj-prefix")) {
446 wd = get_quoted_word(fp);
447 if (wd == (char *)EOF || wd == NULL)
448 errout("%s: %s missing object prefix string.\n",
453 if (eq(wd, "nowerror")) {
457 if (eq(wd, "local")) {
461 if (eq(wd, "no-depend")) {
466 if (eq(wd, "profiling-routine")) {
467 filetype = PROFILING;
471 errout("standard entry %s has optional inclusion specifier %s!\n",
473 STAILQ_FOREACH(dp, &dtab, d_next)
474 if (eq(dp->d_name, wd)) {
478 dp->d_done |= DEVDONE;
481 SLIST_FOREACH(op, &opt, op_next)
482 if (op->op_value == 0 && opteq(op->op_name, wd)) {
492 if (compile && tp == NULL) {
493 if (std == 0 && nreqs == 0)
494 errout("%s: what is %s optional on?\n",
496 if (filetype == PROFILING && profiling == 0)
500 tp->f_type = filetype;
501 if (filetype == LOCAL)
502 tp->f_srcprefix = "";
504 tp->f_srcprefix = "$S/";
506 tp->f_flags |= NO_IMPLCT_RULE;
508 tp->f_flags |= NO_OBJ;
510 tp->f_flags |= BEFORE_DEPEND;
512 tp->f_flags |= NOWERROR;
513 tp->f_compilewith = compilewith;
514 tp->f_depends = depends;
516 tp->f_warn = warning;
517 tp->f_objprefix = objprefix;
523 * Read in the information about files used in making the system.
524 * Store it in the ftab linked list.
529 char fname[MAXPATHLEN];
530 struct files_name *nl, *tnl;
532 (void) snprintf(fname, sizeof(fname), "../../conf/files");
534 (void) snprintf(fname, sizeof(fname),
535 "../../conf/files.%s", machinename);
537 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
538 read_file(nl->f_name);
539 tnl = STAILQ_NEXT(nl, f_next);
546 opteq(const char *cp, const char *dp)
550 for (; ; cp++, dp++) {
552 c = isupper(*cp) ? tolower(*cp) : *cp;
553 d = isupper(*dp) ? tolower(*dp) : *dp;
563 do_before_depend(FILE *fp)
565 struct file_list *tp;
568 fputs("BEFORE_DEPEND=", fp);
570 STAILQ_FOREACH(tp, &ftab, f_next)
571 if (tp->f_flags & BEFORE_DEPEND) {
572 len = strlen(tp->f_fn);
573 if ((len = 3 + len) + lpos > 72) {
577 if (tp->f_flags & NO_IMPLCT_RULE)
578 fprintf(fp, "%s ", tp->f_fn);
580 fprintf(fp, "%s%s ", tp->f_srcprefix,
591 struct file_list *tp;
595 fprintf(fp, "OBJS=");
597 STAILQ_FOREACH(tp, &ftab, f_next) {
598 if (tp->f_flags & NO_OBJ)
601 cp = sp + (len = strlen(sp)) - 1;
604 len += strlen(tp->f_objprefix);
605 if (len + lpos > 72) {
607 fprintf(fp, "\\\n\t");
609 fprintf(fp, "%s%s ", tp->f_objprefix, sp);
618 do_xxfiles(char *tag, FILE *fp)
620 struct file_list *tp;
624 if (tag[strlen(tag) - 1] == '\n')
625 tag[strlen(tag) - 1] = '\0';
632 fprintf(fp, "%sFILES=", SUFF);
635 STAILQ_FOREACH(tp, &ftab, f_next)
636 if (tp->f_type != NODEPEND) {
637 len = strlen(tp->f_fn);
638 if (tp->f_fn[len - slen - 1] != '.')
640 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
642 if ((len = 3 + len) + lpos > 72) {
646 fprintf(fp, "%s%s ", tp->f_srcprefix, tp->f_fn);
659 cp = strrchr(fn, '/');
666 * Create the makerules for each file
667 * which is part of the system.
673 struct file_list *ftp;
677 STAILQ_FOREACH(ftp, &ftab, f_next) {
679 fprintf(stderr, "WARNING: %s\n", ftp->f_warn);
680 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
682 if (ftp->f_flags & NO_IMPLCT_RULE) {
684 fprintf(f, "%s%s: %s\n",
685 ftp->f_objprefix, np, ftp->f_depends);
687 fprintf(f, "%s%s: \n", ftp->f_objprefix, np);
692 fprintf(f, "%s%so:\n\t-cp %s%so .\n\n",
693 ftp->f_objprefix, tail(np),
694 ftp->f_srcprefix, np);
697 if (ftp->f_depends) {
698 fprintf(f, "%s%so: %s%s%c %s\n",
699 ftp->f_objprefix, tail(np),
700 ftp->f_srcprefix, np, och,
704 fprintf(f, "%s%so: %s%s%c\n",
705 ftp->f_objprefix, tail(np),
706 ftp->f_srcprefix, np, och);
709 compilewith = ftp->f_compilewith;
710 if (compilewith == NULL) {
711 const char *ftype = NULL;
713 switch (ftp->f_type) {
724 "config: don't know rules for %s\n", np);
727 snprintf(cmd, sizeof(cmd),
730 ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
734 if (strlen(ftp->f_objprefix))
735 fprintf(f, "\t%s %s%s\n", compilewith,
736 ftp->f_srcprefix, np);
738 fprintf(f, "\t%s\n", compilewith);
740 if (!(ftp->f_flags & NO_OBJ))
741 fprintf(f, "\t${NORMAL_CTFCONVERT}\n\n");
750 struct file_list *tp;
755 STAILQ_FOREACH(tp, &ftab, f_next)
757 len = strlen(tp->f_clean);
758 if (len + lpos > 72) {
762 fprintf(fp, "%s ", tp->f_clean);
776 *str = toupper(*str);