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 void read_files(void);
66 static void sanitize_envline(char *result, const char *src);
67 static bool preprocess(char *line, char *result);
68 static void process_into_file(char *line, FILE *ofp);
69 static void process_into_nvlist(char *line, nvlist_t *nvl);
70 static void dump_nvlist(nvlist_t *nvl, FILE *ofp);
72 static void errout(const char *fmt, ...)
77 vfprintf(stderr, fmt, ap);
83 * Lookup a file, by name.
85 static struct file_list *
90 STAILQ_FOREACH(fp, &ftab, f_next) {
91 if (eq(fp->f_fn, file))
98 * Make a new file list entry
100 static struct file_list *
103 struct file_list *fp;
105 fp = (struct file_list *) calloc(1, sizeof *fp);
107 err(EXIT_FAILURE, "calloc");
108 STAILQ_INSERT_TAIL(&ftab, fp, f_next);
113 * Open the correct Makefile and return it, or error out.
116 open_makefile_template(void)
121 snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
122 ifp = fopen(line, "r");
124 snprintf(line, sizeof(line), "Makefile.%s", machinename);
125 ifp = fopen(line, "r");
133 * Build the makefile from the skeleton
143 ifp = open_makefile_template();
144 ofp = fopen(path("Makefile.new"), "w");
146 err(1, "%s", path("Makefile.new"));
147 fprintf(ofp, "KERN_IDENT=%s\n", ident);
148 fprintf(ofp, "MACHINE=%s\n", machinename);
149 fprintf(ofp, "MACHINE_ARCH=%s\n", machinearch);
150 SLIST_FOREACH_SAFE(op, &mkopt, op_next, t) {
151 fprintf(ofp, "%s=%s", op->op_name, op->op_value);
152 while ((op = SLIST_NEXT(op, op_append)) != NULL)
153 fprintf(ofp, " %s", op->op_value);
157 fprintf(ofp, "DEBUG=-g\n");
159 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
161 fprintf(ofp,"S=%s\n", srcdir);
162 while (fgets(line, BUFSIZ, ifp) != NULL) {
164 fprintf(ofp, "%s", line);
167 if (eq(line, "%BEFORE_DEPEND\n"))
168 do_before_depend(ofp);
169 else if (eq(line, "%OBJS\n"))
171 else if (strncmp(line, "%FILES.", 7) == 0)
172 do_xxfiles(line, ofp);
173 else if (eq(line, "%RULES\n"))
175 else if (eq(line, "%CLEAN\n"))
177 else if (strncmp(line, "%VERSREQ=", 9) == 0)
178 line[0] = '\0'; /* handled elsewhere */
181 "Unknown %% construct in generic makefile: %s",
186 moveifchanged(path("Makefile.new"), path("Makefile"));
190 sanitize_envline(char *result, const char *src)
196 /* If there is no '=' it's not a well-formed name=value line. */
197 if ((eq = strchr(src, '=')) == NULL) {
203 /* Copy chars before the '=', skipping any leading spaces/quotes. */
207 if (leading && (isspace(c) || c == '"'))
213 /* If it was all leading space, we don't have a well-formed line. */
219 /* Trim spaces/quotes immediately before the '=', then copy the '='. */
220 while (isspace(dst[-1]) || dst[-1] == '"')
224 /* Copy chars after the '=', skipping any leading whitespace. */
226 while ((c = *src++) != 0) {
227 if (leading && (isspace(c) || c == '"'))
233 /* If it was all leading space, it's a valid 'var=' (nil value). */
239 /* Trim trailing whitespace and quotes. */
240 while (isspace(dst[-1]) || dst[-1] == '"')
247 * Returns true if the caller may use the string.
250 preprocess(char *line, char *result)
254 /* Strip any comments */
255 if ((s = strchr(line, '#')) != NULL)
257 sanitize_envline(result, line);
258 /* Return true if it's non-empty */
259 return (*result != '\0');
263 process_into_file(char *line, FILE *ofp)
267 if (preprocess(line, result))
268 fprintf(ofp, "\"%s\\0\"\n", result);
272 process_into_nvlist(char *line, nvlist_t *nvl)
274 char result[BUFSIZ], *s;
276 if (preprocess(line, result)) {
277 s = strchr(result, '=');
279 if (nvlist_exists(nvl, result))
280 nvlist_free(nvl, result);
281 nvlist_add_string(nvl, result, s + 1);
286 dump_nvlist(nvlist_t *nvl, FILE *ofp)
294 while (!nvlist_empty(nvl)) {
296 name = nvlist_next(nvl, NULL, &cookie);
297 fprintf(ofp, "\"%s=%s\\0\"\n", name,
298 cnvlist_get_string(cookie));
300 cnvlist_free_string(cookie);
305 * Build hints.c from the skeleton
315 ofp = fopen(path("hints.c.new"), "w");
317 err(1, "%s", path("hints.c.new"));
318 fprintf(ofp, "#include <sys/types.h>\n");
319 fprintf(ofp, "#include <sys/systm.h>\n");
322 * Write out hintmode for older kernels. Remove when config(8) major
323 * version rolls over.
325 if (versreq <= CONFIGVERS_ENVMODE_REQ)
326 fprintf(ofp, "int hintmode = %d;\n",
327 !STAILQ_EMPTY(&hints) ? 1 : 0);
328 fprintf(ofp, "char static_hints[] = {\n");
329 nvl = nvlist_create(0);
330 STAILQ_FOREACH(hint, &hints, hint_next) {
331 ifp = fopen(hint->hint_name, "r");
333 err(1, "%s", hint->hint_name);
334 while (fgets(line, BUFSIZ, ifp) != NULL)
335 process_into_nvlist(line, nvl);
336 dump_nvlist(nvl, ofp);
340 fprintf(ofp, "\"\\0\"\n};\n");
342 moveifchanged(path("hints.c.new"), path("hints.c"));
346 * Build env.c from the skeleton
354 struct envvar *envvar;
356 ofp = fopen(path("env.c.new"), "w");
358 err(1, "%s", path("env.c.new"));
359 fprintf(ofp, "#include <sys/types.h>\n");
360 fprintf(ofp, "#include <sys/systm.h>\n");
363 * Write out envmode for older kernels. Remove when config(8) major
364 * version rolls over.
366 if (versreq <= CONFIGVERS_ENVMODE_REQ)
367 fprintf(ofp, "int envmode = %d;\n",
368 !STAILQ_EMPTY(&envvars) ? 1 : 0);
369 fprintf(ofp, "char static_env[] = {\n");
370 nvl = nvlist_create(0);
371 STAILQ_FOREACH(envvar, &envvars, envvar_next) {
372 if (envvar->env_is_file) {
373 ifp = fopen(envvar->env_str, "r");
375 err(1, "%s", envvar->env_str);
376 while (fgets(line, BUFSIZ, ifp) != NULL)
377 process_into_nvlist(line, nvl);
378 dump_nvlist(nvl, ofp);
381 process_into_file(envvar->env_str, ofp);
384 fprintf(ofp, "\"\\0\"\n};\n");
386 moveifchanged(path("env.c.new"), path("env.c"));
390 read_file(char *fname)
392 char ifname[MAXPATHLEN];
394 struct file_list *tp;
397 char *wd, *this, *compilewith, *depends, *clean, *warning;
398 const char *objprefix;
399 int compile, match, nreqs, std, filetype, not,
400 imp_rule, no_ctfconvert, no_obj, before_depend, nowerror;
402 fp = fopen(fname, "r");
408 * filename [ standard | optional ]
409 * [ dev* [ | dev* ... ] | profiling-routine ] [ no-obj ]
410 * [ compile-with "compile rule" [no-implicit-rule] ]
411 * [ dependency "dependency-list"] [ before-depend ]
412 * [ clean "file-list"] [ warning "text warning" ]
413 * [ obj-prefix "file prefix"]
414 * [ nowerror ] [ local ]
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);
462 if (eq(wd, "standard"))
464 else if (!eq(wd, "optional"))
465 errout("%s: \"%s\" %s must be optional or standard\n",
467 for (wd = get_word(fp); wd; wd = get_word(fp)) {
468 if (wd == (char *)EOF)
476 errout("%s: syntax error describing %s\n",
483 if (eq(wd, "no-ctfconvert")) {
487 if (eq(wd, "no-obj")) {
491 if (eq(wd, "no-implicit-rule")) {
492 if (compilewith == NULL)
493 errout("%s: alternate rule required when "
494 "\"no-implicit-rule\" is specified for"
500 if (eq(wd, "before-depend")) {
504 if (eq(wd, "dependency")) {
505 wd = get_quoted_word(fp);
506 if (wd == (char *)EOF || wd == NULL)
507 errout("%s: %s missing dependency string.\n",
512 if (eq(wd, "clean")) {
513 wd = get_quoted_word(fp);
514 if (wd == (char *)EOF || wd == NULL)
515 errout("%s: %s missing clean file list.\n",
520 if (eq(wd, "compile-with")) {
521 wd = get_quoted_word(fp);
522 if (wd == (char *)EOF || wd == NULL)
523 errout("%s: %s missing compile command string.\n",
525 compilewith = ns(wd);
528 if (eq(wd, "warning")) {
529 wd = get_quoted_word(fp);
530 if (wd == (char *)EOF || wd == NULL)
531 errout("%s: %s missing warning text string.\n",
536 if (eq(wd, "obj-prefix")) {
537 wd = get_quoted_word(fp);
538 if (wd == (char *)EOF || wd == NULL)
539 errout("%s: %s missing object prefix string.\n",
544 if (eq(wd, "nowerror")) {
548 if (eq(wd, "local")) {
552 if (eq(wd, "no-depend")) {
557 if (eq(wd, "profiling-routine")) {
558 filetype = PROFILING;
562 errout("standard entry %s has optional inclusion specifier %s!\n",
564 STAILQ_FOREACH(dp, &dtab, d_next)
565 if (eq(dp->d_name, wd)) {
569 dp->d_done |= DEVDONE;
572 SLIST_FOREACH(op, &opt, op_next)
573 if (op->op_value == 0 &&
574 strcasecmp(op->op_name, wd) == 0) {
584 if (compile && tp == NULL) {
585 if (std == 0 && nreqs == 0)
586 errout("%s: what is %s optional on?\n",
588 if (filetype == PROFILING && profiling == 0)
592 tp->f_type = filetype;
593 if (filetype == LOCAL)
594 tp->f_srcprefix = "";
596 tp->f_srcprefix = "$S/";
598 tp->f_flags |= NO_IMPLCT_RULE;
600 tp->f_flags |= NO_CTFCONVERT;
602 tp->f_flags |= NO_OBJ | NO_CTFCONVERT;
604 tp->f_flags |= BEFORE_DEPEND;
606 tp->f_flags |= NOWERROR;
607 tp->f_compilewith = compilewith;
608 tp->f_depends = depends;
610 tp->f_warn = warning;
611 tp->f_objprefix = objprefix;
617 * Read in the information about files used in making the system.
618 * Store it in the ftab linked list.
623 char fname[MAXPATHLEN];
624 struct files_name *nl, *tnl;
626 (void) snprintf(fname, sizeof(fname), "../../conf/files");
628 (void) snprintf(fname, sizeof(fname),
629 "../../conf/files.%s", machinename);
631 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
632 read_file(nl->f_name);
633 tnl = STAILQ_NEXT(nl, f_next);
640 do_before_depend(FILE *fp)
642 struct file_list *tp;
645 fputs("BEFORE_DEPEND=", fp);
647 STAILQ_FOREACH(tp, &ftab, f_next)
648 if (tp->f_flags & BEFORE_DEPEND) {
649 len = strlen(tp->f_fn) + strlen(tp->f_srcprefix);
650 if (len + lpos > 72) {
654 if (tp->f_flags & NO_IMPLCT_RULE)
655 lpos += fprintf(fp, "%s ", tp->f_fn);
657 lpos += fprintf(fp, "%s%s ", tp->f_srcprefix,
667 struct file_list *tp;
671 fprintf(fp, "OBJS=");
673 STAILQ_FOREACH(tp, &ftab, f_next) {
674 if (tp->f_flags & NO_OBJ)
677 cp = sp + (len = strlen(sp)) - 1;
680 len += strlen(tp->f_objprefix);
681 if (len + lpos > 72) {
683 fprintf(fp, "\\\n\t");
685 fprintf(fp, "%s%s ", tp->f_objprefix, sp);
694 do_xxfiles(char *tag, FILE *fp)
696 struct file_list *tp;
700 if (tag[strlen(tag) - 1] == '\n')
701 tag[strlen(tag) - 1] = '\0';
708 fprintf(fp, "%sFILES=", SUFF);
711 STAILQ_FOREACH(tp, &ftab, f_next)
712 if (tp->f_type != NODEPEND) {
713 len = strlen(tp->f_fn);
714 if (tp->f_fn[len - slen - 1] != '.')
716 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
718 if (len + strlen(tp->f_srcprefix) + lpos > 72) {
722 lpos += fprintf(fp, "%s%s ", tp->f_srcprefix, tp->f_fn);
734 cp = strrchr(fn, '/');
741 * Create the makerules for each file
742 * which is part of the system.
748 struct file_list *ftp;
752 STAILQ_FOREACH(ftp, &ftab, f_next) {
754 fprintf(stderr, "WARNING: %s\n", ftp->f_warn);
755 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
757 if (ftp->f_flags & NO_IMPLCT_RULE) {
759 fprintf(f, "%s%s: %s\n",
760 ftp->f_objprefix, np, ftp->f_depends);
762 fprintf(f, "%s%s: \n", ftp->f_objprefix, np);
767 fprintf(f, "%s%so:\n\t-cp %s%so .\n\n",
768 ftp->f_objprefix, tail(np),
769 ftp->f_srcprefix, np);
772 if (ftp->f_depends) {
773 fprintf(f, "%s%so: %s%s%c %s\n",
774 ftp->f_objprefix, tail(np),
775 ftp->f_srcprefix, np, och,
779 fprintf(f, "%s%so: %s%s%c\n",
780 ftp->f_objprefix, tail(np),
781 ftp->f_srcprefix, np, och);
784 compilewith = ftp->f_compilewith;
785 if (compilewith == NULL) {
786 const char *ftype = NULL;
788 switch (ftp->f_type) {
799 "config: don't know rules for %s\n", np);
802 snprintf(cmd, sizeof(cmd),
805 ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
809 if (strlen(ftp->f_objprefix))
810 fprintf(f, "\t%s %s%s\n", compilewith,
811 ftp->f_srcprefix, np);
813 fprintf(f, "\t%s\n", compilewith);
815 if (!(ftp->f_flags & NO_CTFCONVERT))
816 fprintf(f, "\t${NORMAL_CTFCONVERT}\n\n");
825 struct file_list *tp;
830 STAILQ_FOREACH(tp, &ftab, f_next)
832 len = strlen(tp->f_clean);
833 if (len + lpos > 72) {
837 fprintf(fp, "%s ", tp->f_clean);
851 *str = toupper(*str);