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.
54 #include <sys/param.h>
57 #include "configvers.h"
59 static char *tail(char *);
60 static void do_clean(FILE *);
61 static void do_rules(FILE *);
62 static void do_xxfiles(char *, FILE *);
63 static void do_objs(FILE *);
64 static void do_before_depend(FILE *);
65 static int opteq(const char *, const char *);
66 static void read_files(void);
67 static void sanitize_envline(char *result, const char *src);
68 static bool preprocess(char *line, char *result);
69 static void process_into_file(char *line, FILE *ofp);
70 static void process_into_nvlist(char *line, nvlist_t *nvl);
71 static void dump_nvlist(nvlist_t *nvl, FILE *ofp);
73 static void errout(const char *fmt, ...)
78 vfprintf(stderr, fmt, ap);
84 * Lookup a file, by name.
86 static struct file_list *
91 STAILQ_FOREACH(fp, &ftab, f_next) {
92 if (eq(fp->f_fn, file))
99 * Make a new file list entry
101 static struct file_list *
104 struct file_list *fp;
106 fp = (struct file_list *) calloc(1, sizeof *fp);
108 err(EXIT_FAILURE, "calloc");
109 STAILQ_INSERT_TAIL(&ftab, fp, f_next);
114 * Open the correct Makefile and return it, or error out.
117 open_makefile_template(void)
122 snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
123 ifp = fopen(line, "r");
125 snprintf(line, sizeof(line), "Makefile.%s", machinename);
126 ifp = fopen(line, "r");
134 * Build the makefile from the skeleton
144 ifp = open_makefile_template();
145 ofp = fopen(path("Makefile.new"), "w");
147 err(1, "%s", path("Makefile.new"));
148 fprintf(ofp, "KERN_IDENT=%s\n", ident);
149 fprintf(ofp, "MACHINE=%s\n", machinename);
150 fprintf(ofp, "MACHINE_ARCH=%s\n", machinearch);
151 SLIST_FOREACH_SAFE(op, &mkopt, op_next, t) {
152 fprintf(ofp, "%s=%s", op->op_name, op->op_value);
153 while ((op = SLIST_NEXT(op, op_append)) != NULL)
154 fprintf(ofp, " %s", op->op_value);
158 fprintf(ofp, "DEBUG=-g\n");
160 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
162 fprintf(ofp,"S=%s\n", srcdir);
163 while (fgets(line, BUFSIZ, ifp) != NULL) {
165 fprintf(ofp, "%s", line);
168 if (eq(line, "%BEFORE_DEPEND\n"))
169 do_before_depend(ofp);
170 else if (eq(line, "%OBJS\n"))
172 else if (strncmp(line, "%FILES.", 7) == 0)
173 do_xxfiles(line, ofp);
174 else if (eq(line, "%RULES\n"))
176 else if (eq(line, "%CLEAN\n"))
178 else if (strncmp(line, "%VERSREQ=", 9) == 0)
179 line[0] = '\0'; /* handled elsewhere */
182 "Unknown %% construct in generic makefile: %s",
187 moveifchanged(path("Makefile.new"), path("Makefile"));
191 sanitize_envline(char *result, const char *src)
197 /* If there is no '=' it's not a well-formed name=value line. */
198 if ((eq = strchr(src, '=')) == NULL) {
204 /* Copy chars before the '=', skipping any leading spaces/quotes. */
208 if (leading && (isspace(c) || c == '"'))
214 /* If it was all leading space, we don't have a well-formed line. */
220 /* Trim spaces/quotes immediately before the '=', then copy the '='. */
221 while (isspace(dst[-1]) || dst[-1] == '"')
225 /* Copy chars after the '=', skipping any leading whitespace. */
227 while ((c = *src++) != 0) {
228 if (leading && (isspace(c) || c == '"'))
234 /* If it was all leading space, it's a valid 'var=' (nil value). */
240 /* Trim trailing whitespace and quotes. */
241 while (isspace(dst[-1]) || dst[-1] == '"')
248 * Returns true if the caller may use the string.
251 preprocess(char *line, char *result)
255 /* Strip any comments */
256 if ((s = strchr(line, '#')) != NULL)
258 sanitize_envline(result, line);
259 /* Return true if it's non-empty */
260 return (*result != '\0');
264 process_into_file(char *line, FILE *ofp)
268 if (preprocess(line, result))
269 fprintf(ofp, "\"%s\\0\"\n", result);
273 process_into_nvlist(char *line, nvlist_t *nvl)
275 char result[BUFSIZ], *s;
277 if (preprocess(line, result)) {
278 s = strchr(result, '=');
280 if (nvlist_exists(nvl, result))
281 nvlist_free(nvl, result);
282 nvlist_add_string(nvl, result, s + 1);
287 dump_nvlist(nvlist_t *nvl, FILE *ofp)
295 while (!nvlist_empty(nvl)) {
297 name = nvlist_next(nvl, NULL, &cookie);
298 fprintf(ofp, "\"%s=%s\\0\"\n", name,
299 cnvlist_get_string(cookie));
301 cnvlist_free_string(cookie);
306 * Build hints.c from the skeleton
316 ofp = fopen(path("hints.c.new"), "w");
318 err(1, "%s", path("hints.c.new"));
319 fprintf(ofp, "#include <sys/types.h>\n");
320 fprintf(ofp, "#include <sys/systm.h>\n");
323 * Write out hintmode for older kernels. Remove when config(8) major
324 * version rolls over.
326 if (versreq <= CONFIGVERS_ENVMODE_REQ)
327 fprintf(ofp, "int hintmode = %d;\n",
328 !STAILQ_EMPTY(&hints) ? 1 : 0);
329 fprintf(ofp, "char static_hints[] = {\n");
330 nvl = nvlist_create(0);
331 STAILQ_FOREACH(hint, &hints, hint_next) {
332 ifp = fopen(hint->hint_name, "r");
334 err(1, "%s", hint->hint_name);
335 while (fgets(line, BUFSIZ, ifp) != NULL)
336 process_into_nvlist(line, nvl);
337 dump_nvlist(nvl, ofp);
341 fprintf(ofp, "\"\\0\"\n};\n");
343 moveifchanged(path("hints.c.new"), path("hints.c"));
347 * Build env.c from the skeleton
355 struct envvar *envvar;
357 ofp = fopen(path("env.c.new"), "w");
359 err(1, "%s", path("env.c.new"));
360 fprintf(ofp, "#include <sys/types.h>\n");
361 fprintf(ofp, "#include <sys/systm.h>\n");
364 * Write out envmode for older kernels. Remove when config(8) major
365 * version rolls over.
367 if (versreq <= CONFIGVERS_ENVMODE_REQ)
368 fprintf(ofp, "int envmode = %d;\n",
369 !STAILQ_EMPTY(&envvars) ? 1 : 0);
370 fprintf(ofp, "char static_env[] = {\n");
371 nvl = nvlist_create(0);
372 STAILQ_FOREACH(envvar, &envvars, envvar_next) {
373 if (envvar->env_is_file) {
374 ifp = fopen(envvar->env_str, "r");
376 err(1, "%s", envvar->env_str);
377 while (fgets(line, BUFSIZ, ifp) != NULL)
378 process_into_nvlist(line, nvl);
379 dump_nvlist(nvl, ofp);
382 process_into_file(envvar->env_str, ofp);
385 fprintf(ofp, "\"\\0\"\n};\n");
387 moveifchanged(path("env.c.new"), path("env.c"));
391 read_file(char *fname)
393 char ifname[MAXPATHLEN];
395 struct file_list *tp;
398 char *wd, *this, *compilewith, *depends, *clean, *warning;
399 const char *objprefix;
400 int compile, match, nreqs, std, filetype, not,
401 imp_rule, no_obj, before_depend, nowerror;
403 fp = fopen(fname, "r");
409 * filename [ standard | optional ]
410 * [ dev* [ | dev* ... ] | profiling-routine ] [ no-obj ]
411 * [ compile-with "compile rule" [no-implicit-rule] ]
412 * [ dependency "dependency-list"] [ before-depend ]
413 * [ clean "file-list"] [ warning "text warning" ]
414 * [ obj-prefix "file prefix"]
417 if (wd == (char *)EOF) {
425 while (((wd = get_word(fp)) != (char *)EOF) && wd)
429 if (eq(wd, "include")) {
430 wd = get_quoted_word(fp);
431 if (wd == (char *)EOF || wd == NULL)
432 errout("%s: missing include filename.\n", fname);
433 (void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
435 while (((wd = get_word(fp)) != (char *)EOF) && wd)
441 if (wd == (char *)EOF)
444 errout("%s: No type for %s.\n", fname, this);
445 tp = fl_lookup(this);
461 if (eq(wd, "standard"))
463 else if (!eq(wd, "optional"))
464 errout("%s: \"%s\" %s must be optional or standard\n",
466 for (wd = get_word(fp); wd; wd = get_word(fp)) {
467 if (wd == (char *)EOF)
475 errout("%s: syntax error describing %s\n",
482 if (eq(wd, "no-obj")) {
486 if (eq(wd, "no-implicit-rule")) {
487 if (compilewith == NULL)
488 errout("%s: alternate rule required when "
489 "\"no-implicit-rule\" is specified for"
495 if (eq(wd, "before-depend")) {
499 if (eq(wd, "dependency")) {
500 wd = get_quoted_word(fp);
501 if (wd == (char *)EOF || wd == NULL)
502 errout("%s: %s missing dependency string.\n",
507 if (eq(wd, "clean")) {
508 wd = get_quoted_word(fp);
509 if (wd == (char *)EOF || wd == NULL)
510 errout("%s: %s missing clean file list.\n",
515 if (eq(wd, "compile-with")) {
516 wd = get_quoted_word(fp);
517 if (wd == (char *)EOF || wd == NULL)
518 errout("%s: %s missing compile command string.\n",
520 compilewith = ns(wd);
523 if (eq(wd, "warning")) {
524 wd = get_quoted_word(fp);
525 if (wd == (char *)EOF || wd == NULL)
526 errout("%s: %s missing warning text string.\n",
531 if (eq(wd, "obj-prefix")) {
532 wd = get_quoted_word(fp);
533 if (wd == (char *)EOF || wd == NULL)
534 errout("%s: %s missing object prefix string.\n",
539 if (eq(wd, "nowerror")) {
543 if (eq(wd, "local")) {
547 if (eq(wd, "no-depend")) {
552 if (eq(wd, "profiling-routine")) {
553 filetype = PROFILING;
557 errout("standard entry %s has optional inclusion specifier %s!\n",
559 STAILQ_FOREACH(dp, &dtab, d_next)
560 if (eq(dp->d_name, wd)) {
564 dp->d_done |= DEVDONE;
567 SLIST_FOREACH(op, &opt, op_next)
568 if (op->op_value == 0 && opteq(op->op_name, wd)) {
578 if (compile && tp == NULL) {
579 if (std == 0 && nreqs == 0)
580 errout("%s: what is %s optional on?\n",
582 if (filetype == PROFILING && profiling == 0)
586 tp->f_type = filetype;
587 if (filetype == LOCAL)
588 tp->f_srcprefix = "";
590 tp->f_srcprefix = "$S/";
592 tp->f_flags |= NO_IMPLCT_RULE;
594 tp->f_flags |= NO_OBJ;
596 tp->f_flags |= BEFORE_DEPEND;
598 tp->f_flags |= NOWERROR;
599 tp->f_compilewith = compilewith;
600 tp->f_depends = depends;
602 tp->f_warn = warning;
603 tp->f_objprefix = objprefix;
609 * Read in the information about files used in making the system.
610 * Store it in the ftab linked list.
615 char fname[MAXPATHLEN];
616 struct files_name *nl, *tnl;
618 (void) snprintf(fname, sizeof(fname), "../../conf/files");
620 (void) snprintf(fname, sizeof(fname),
621 "../../conf/files.%s", machinename);
623 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
624 read_file(nl->f_name);
625 tnl = STAILQ_NEXT(nl, f_next);
632 opteq(const char *cp, const char *dp)
636 for (; ; cp++, dp++) {
638 c = isupper(*cp) ? tolower(*cp) : *cp;
639 d = isupper(*dp) ? tolower(*dp) : *dp;
649 do_before_depend(FILE *fp)
651 struct file_list *tp;
654 fputs("BEFORE_DEPEND=", fp);
656 STAILQ_FOREACH(tp, &ftab, f_next)
657 if (tp->f_flags & BEFORE_DEPEND) {
658 len = strlen(tp->f_fn);
659 if ((len = 3 + len) + lpos > 72) {
663 if (tp->f_flags & NO_IMPLCT_RULE)
664 fprintf(fp, "%s ", tp->f_fn);
666 fprintf(fp, "%s%s ", tp->f_srcprefix,
677 struct file_list *tp;
681 fprintf(fp, "OBJS=");
683 STAILQ_FOREACH(tp, &ftab, f_next) {
684 if (tp->f_flags & NO_OBJ)
687 cp = sp + (len = strlen(sp)) - 1;
690 len += strlen(tp->f_objprefix);
691 if (len + lpos > 72) {
693 fprintf(fp, "\\\n\t");
695 fprintf(fp, "%s%s ", tp->f_objprefix, sp);
704 do_xxfiles(char *tag, FILE *fp)
706 struct file_list *tp;
710 if (tag[strlen(tag) - 1] == '\n')
711 tag[strlen(tag) - 1] = '\0';
718 fprintf(fp, "%sFILES=", SUFF);
721 STAILQ_FOREACH(tp, &ftab, f_next)
722 if (tp->f_type != NODEPEND) {
723 len = strlen(tp->f_fn);
724 if (tp->f_fn[len - slen - 1] != '.')
726 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
728 if ((len = 3 + len) + lpos > 72) {
732 fprintf(fp, "%s%s ", tp->f_srcprefix, tp->f_fn);
745 cp = strrchr(fn, '/');
752 * Create the makerules for each file
753 * which is part of the system.
759 struct file_list *ftp;
763 STAILQ_FOREACH(ftp, &ftab, f_next) {
765 fprintf(stderr, "WARNING: %s\n", ftp->f_warn);
766 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
768 if (ftp->f_flags & NO_IMPLCT_RULE) {
770 fprintf(f, "%s%s: %s\n",
771 ftp->f_objprefix, np, ftp->f_depends);
773 fprintf(f, "%s%s: \n", ftp->f_objprefix, np);
778 fprintf(f, "%s%so:\n\t-cp %s%so .\n\n",
779 ftp->f_objprefix, tail(np),
780 ftp->f_srcprefix, np);
783 if (ftp->f_depends) {
784 fprintf(f, "%s%so: %s%s%c %s\n",
785 ftp->f_objprefix, tail(np),
786 ftp->f_srcprefix, np, och,
790 fprintf(f, "%s%so: %s%s%c\n",
791 ftp->f_objprefix, tail(np),
792 ftp->f_srcprefix, np, och);
795 compilewith = ftp->f_compilewith;
796 if (compilewith == NULL) {
797 const char *ftype = NULL;
799 switch (ftp->f_type) {
810 "config: don't know rules for %s\n", np);
813 snprintf(cmd, sizeof(cmd),
816 ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
820 if (strlen(ftp->f_objprefix))
821 fprintf(f, "\t%s %s%s\n", compilewith,
822 ftp->f_srcprefix, np);
824 fprintf(f, "\t%s\n", compilewith);
826 if (!(ftp->f_flags & NO_OBJ))
827 fprintf(f, "\t${NORMAL_CTFCONVERT}\n\n");
836 struct file_list *tp;
841 STAILQ_FOREACH(tp, &ftab, f_next)
843 len = strlen(tp->f_clean);
844 if (len + lpos > 72) {
848 fprintf(fp, "%s ", tp->f_clean);
862 *str = toupper(*str);