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.
45 #include <sys/param.h>
55 #include <unordered_map>
59 #include "configvers.h"
61 typedef std::unordered_map<std::string, std::string> env_map;
63 static char *tail(char *);
64 static void do_clean(FILE *);
65 static void do_rules(FILE *);
66 static void do_xxfiles(char *, FILE *);
67 static void do_objs(FILE *);
68 static void do_before_depend(FILE *);
69 static void read_files(void);
70 static void sanitize_envline(char *result, const char *src);
71 static bool preprocess(char *line, char *result);
72 static void process_into_file(char *line, FILE *ofp);
73 static int process_into_map(char *line, env_map &emap);
74 static void dump_map(env_map &emap, FILE *ofp);
76 static void __printflike(1, 2)
77 errout(const char *fmt, ...)
82 vfprintf(stderr, fmt, ap);
88 * Lookup a file, by name.
90 static struct file_list *
95 STAILQ_FOREACH(fp, &ftab, f_next) {
96 if (eq(fp->f_fn, file))
103 * Make a new file list entry
105 static struct file_list *
108 struct file_list *fp;
110 fp = (struct file_list *) calloc(1, sizeof *fp);
112 err(EXIT_FAILURE, "calloc");
113 STAILQ_INSERT_TAIL(&ftab, fp, f_next);
118 * Open the correct Makefile and return it, or error out.
121 open_makefile_template(void)
126 snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
127 ifp = fopen(line, "r");
129 snprintf(line, sizeof(line), "Makefile.%s", machinename);
130 ifp = fopen(line, "r");
138 * Build the makefile from the skeleton
148 ifp = open_makefile_template();
149 ofp = fopen(path("Makefile.new"), "w");
151 err(1, "%s", path("Makefile.new"));
152 fprintf(ofp, "KERN_IDENT=%s\n", ident);
153 fprintf(ofp, "MACHINE=%s\n", machinename);
154 fprintf(ofp, "MACHINE_ARCH=%s\n", machinearch);
155 SLIST_FOREACH_SAFE(op, &mkopt, op_next, t) {
156 fprintf(ofp, "%s=%s", op->op_name, op->op_value);
157 while ((op = SLIST_NEXT(op, op_append)) != NULL)
158 fprintf(ofp, " %s", op->op_value);
162 fprintf(ofp, "DEBUG=-g\n");
164 fprintf(ofp,"S=%s\n", srcdir);
165 while (fgets(line, BUFSIZ, ifp) != NULL) {
167 fprintf(ofp, "%s", line);
170 if (eq(line, "%BEFORE_DEPEND\n"))
171 do_before_depend(ofp);
172 else if (eq(line, "%OBJS\n"))
174 else if (strncmp(line, "%FILES.", 7) == 0)
175 do_xxfiles(line, ofp);
176 else if (eq(line, "%RULES\n"))
178 else if (eq(line, "%CLEAN\n"))
180 else if (strncmp(line, "%VERSREQ=", 9) == 0)
181 line[0] = '\0'; /* handled elsewhere */
184 "Unknown %% construct in generic makefile: %s",
189 moveifchanged("Makefile.new", "Makefile");
193 sanitize_envline(char *result, const char *src)
199 /* If there is no '=' it's not a well-formed name=value line. */
200 if ((eq = strchr(src, '=')) == NULL) {
206 /* Copy chars before the '=', skipping any leading spaces/quotes. */
210 if (leading && (isspace(c) || c == '"'))
216 /* If it was all leading space, we don't have a well-formed line. */
222 /* Trim spaces/quotes immediately before the '=', then copy the '='. */
223 while (isspace(dst[-1]) || dst[-1] == '"')
227 /* Copy chars after the '=', skipping any leading whitespace. */
229 while ((c = *src++) != '\0') {
230 if (leading && (isspace(c) || c == '"'))
236 /* If it was all leading space, it's a valid 'var=' (nil value). */
242 /* Trim trailing whitespace and quotes. */
243 while (isspace(dst[-1]) || dst[-1] == '"')
250 * Returns true if the caller may use the string.
253 preprocess(char *line, char *result)
257 /* Strip any comments */
258 if ((s = strchr(line, '#')) != NULL)
260 sanitize_envline(result, line);
261 /* Return true if it's non-empty */
262 return (*result != '\0');
266 process_into_file(char *line, FILE *ofp)
270 if (preprocess(line, result))
271 fprintf(ofp, "\"%s\\0\"\n", result);
275 process_into_map(char *line, env_map &emap)
277 char result[BUFSIZ], *s;
279 if (preprocess(line, result)) {
280 s = strchr(result, '=');
284 emap[result] = s + 1;
291 dump_map(env_map &emap, FILE *ofp)
294 for (auto iter : emap) {
295 fprintf(ofp, "\"%s=%s\\0\"\n", iter.first.c_str(),
296 iter.second.c_str());
301 * Build hints.c from the skeleton
311 ofp = fopen(path("hints.c.new"), "w");
313 err(1, "%s", path("hints.c.new"));
314 fprintf(ofp, "#include <sys/types.h>\n");
315 fprintf(ofp, "#include <sys/systm.h>\n");
318 * Write out hintmode for older kernels. Remove when config(8) major
319 * version rolls over.
321 if (versreq <= CONFIGVERS_ENVMODE_REQ)
322 fprintf(ofp, "int hintmode = %d;\n",
323 !STAILQ_EMPTY(&hints) ? 1 : 0);
324 fprintf(ofp, "char static_hints[] = {\n");
325 STAILQ_FOREACH(hint, &hints, hint_next) {
326 ifp = fopen(hint->hint_name, "r");
328 err(1, "%s", hint->hint_name);
329 while (fgets(line, BUFSIZ, ifp) != NULL) {
330 if (process_into_map(line, emap) != 0)
331 errout("%s: malformed line: %s\n",
332 hint->hint_name, line);
337 fprintf(ofp, "\"\\0\"\n};\n");
339 moveifchanged("hints.c.new", "hints.c");
343 * Build env.c from the skeleton
351 struct envvar *envvar;
353 ofp = fopen(path("env.c.new"), "w");
355 err(1, "%s", path("env.c.new"));
356 fprintf(ofp, "#include <sys/types.h>\n");
357 fprintf(ofp, "#include <sys/systm.h>\n");
360 * Write out envmode for older kernels. Remove when config(8) major
361 * version rolls over.
363 if (versreq <= CONFIGVERS_ENVMODE_REQ)
364 fprintf(ofp, "int envmode = %d;\n",
365 !STAILQ_EMPTY(&envvars) ? 1 : 0);
366 fprintf(ofp, "char static_env[] = {\n");
367 STAILQ_FOREACH(envvar, &envvars, envvar_next) {
368 if (envvar->env_is_file) {
369 ifp = fopen(envvar->env_str, "r");
371 err(1, "%s", envvar->env_str);
372 while (fgets(line, BUFSIZ, ifp) != NULL) {
373 if (process_into_map(line, emap) != 0)
374 errout("%s: malformed line: %s\n",
375 envvar->env_str, line);
380 process_into_file(envvar->env_str, ofp);
382 fprintf(ofp, "\"\\0\"\n};\n");
384 moveifchanged("env.c.new", "env.c");
388 read_file(char *fname)
390 char ifname[MAXPATHLEN];
392 struct file_list *tp;
396 char *rfile, *compilewith, *depends, *clean, *warning;
397 const char *objprefix;
398 int compile, match, nreqs, std, filetype, negate,
399 imp_rule, no_ctfconvert, no_obj, before_depend, nowerror;
401 fp = fopen(fname, "r");
407 * filename [ standard | optional ]
408 * [ dev* [ | dev* ... ] | [ no-obj ]
409 * [ compile-with "compile rule" [no-implicit-rule] ]
410 * [ dependency "dependency-list"] [ before-depend ]
411 * [ clean "file-list"] [ warning "text warning" ]
412 * [ obj-prefix "file prefix"]
413 * [ nowerror ] [ local ]
424 while (!(wd = get_word(fp)).eof() && !wd.eol())
428 if (eq(wd, "include")) {
429 wd = get_quoted_word(fp);
430 if (wd.eof() || wd.eol())
431 errout("%s: missing include filename.\n", fname);
432 (void) snprintf(ifname, sizeof(ifname), "../../%s",
435 while (!(wd = get_word(fp)).eof() && !wd.eol())
444 errout("%s: No type for %s.\n", fname, rfile);
445 tp = fl_lookup(rfile);
462 if (eq(wd, "standard"))
464 else if (!eq(wd, "optional"))
465 errout("%s: \"%s\" %s must be optional or standard\n",
466 fname, wd->c_str(), rfile);
467 for (wd = get_word(fp); !wd.eol(); wd = get_word(fp)) {
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.eof() || wd.eol())
507 errout("%s: %s missing dependency string.\n",
512 if (eq(wd, "clean")) {
513 wd = get_quoted_word(fp);
514 if (wd.eof() || wd.eol())
515 errout("%s: %s missing clean file list.\n",
520 if (eq(wd, "compile-with")) {
521 wd = get_quoted_word(fp);
522 if (wd.eof() || wd.eol())
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.eof() || wd.eol())
531 errout("%s: %s missing warning text string.\n",
536 if (eq(wd, "obj-prefix")) {
537 wd = get_quoted_word(fp);
538 if (wd.eof() || wd.eol())
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")) {
558 errout("standard entry %s has optional inclusion specifier %s!\n",
560 STAILQ_FOREACH(dp, &dtab, d_next)
561 if (eq(dp->d_name, wd)) {
565 dp->d_done |= DEVDONE;
568 SLIST_FOREACH(op, &opt, op_next)
569 if (op->op_value == 0 &&
570 strcasecmp(op->op_name, wd) == 0) {
580 if (compile && tp == NULL) {
581 if (std == 0 && nreqs == 0)
582 errout("%s: what is %s optional on?\n",
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_CTFCONVERT;
596 tp->f_flags |= NO_OBJ | NO_CTFCONVERT;
598 tp->f_flags |= BEFORE_DEPEND;
600 tp->f_flags |= NOWERROR;
601 tp->f_compilewith = compilewith;
602 tp->f_depends = depends;
604 tp->f_warn = warning;
605 tp->f_objprefix = objprefix;
611 * Read in the information about files used in making the system.
612 * Store it in the ftab linked list.
617 char fname[MAXPATHLEN];
618 struct files_name *nl, *tnl;
620 (void) snprintf(fname, sizeof(fname), "../../conf/files");
622 (void) snprintf(fname, sizeof(fname),
623 "../../conf/files.%s", machinename);
625 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
626 read_file(nl->f_name);
627 tnl = STAILQ_NEXT(nl, f_next);
634 do_before_depend(FILE *fp)
636 struct file_list *tp;
639 fputs("BEFORE_DEPEND=", fp);
641 STAILQ_FOREACH(tp, &ftab, f_next)
642 if (tp->f_flags & BEFORE_DEPEND) {
643 len = strlen(tp->f_fn) + strlen(tp->f_srcprefix);
644 if (len + lpos > 72) {
648 if (tp->f_flags & NO_IMPLCT_RULE)
649 lpos += fprintf(fp, "%s ", tp->f_fn);
651 lpos += fprintf(fp, "%s%s ", tp->f_srcprefix,
661 struct file_list *tp;
665 fprintf(fp, "OBJS=");
667 STAILQ_FOREACH(tp, &ftab, f_next) {
668 if (tp->f_flags & NO_OBJ)
671 cp = sp + (len = strlen(sp)) - 1;
674 len += strlen(tp->f_objprefix);
675 if (len + lpos > 72) {
677 fprintf(fp, "\\\n\t");
679 fprintf(fp, "%s%s ", tp->f_objprefix, sp);
688 do_xxfiles(char *tag, FILE *fp)
690 struct file_list *tp;
694 if (tag[strlen(tag) - 1] == '\n')
695 tag[strlen(tag) - 1] = '\0';
702 fprintf(fp, "%sFILES=", SUFF);
705 STAILQ_FOREACH(tp, &ftab, f_next)
706 if (tp->f_type != NODEPEND) {
707 len = strlen(tp->f_fn);
708 if (tp->f_fn[len - slen - 1] != '.')
710 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
712 if (len + strlen(tp->f_srcprefix) + lpos > 72) {
716 lpos += fprintf(fp, "%s%s ", tp->f_srcprefix, tp->f_fn);
728 cp = strrchr(fn, '/');
735 * Create the makerules for each file
736 * which is part of the system.
742 struct file_list *ftp;
746 STAILQ_FOREACH(ftp, &ftab, f_next) {
748 fprintf(stderr, "WARNING: %s\n", ftp->f_warn);
749 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
751 if (ftp->f_flags & NO_IMPLCT_RULE) {
753 fprintf(f, "%s%s: %s\n",
754 ftp->f_objprefix, np, ftp->f_depends);
756 fprintf(f, "%s%s: \n", ftp->f_objprefix, np);
761 fprintf(f, "%s%so:\n\t-cp %s%so .\n\n",
762 ftp->f_objprefix, tail(np),
763 ftp->f_srcprefix, np);
766 if (ftp->f_depends) {
767 fprintf(f, "%s%so: %s%s%c %s\n",
768 ftp->f_objprefix, tail(np),
769 ftp->f_srcprefix, np, och,
773 fprintf(f, "%s%so: %s%s%c\n",
774 ftp->f_objprefix, tail(np),
775 ftp->f_srcprefix, np, och);
778 compilewith = ftp->f_compilewith;
779 if (compilewith == NULL) {
780 const char *ftype = NULL;
782 switch (ftp->f_type) {
788 "config: don't know rules for %s\n", np);
791 snprintf(cmd, sizeof(cmd),
794 ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
798 if (strlen(ftp->f_objprefix))
799 fprintf(f, "\t%s %s%s\n", compilewith,
800 ftp->f_srcprefix, np);
802 fprintf(f, "\t%s\n", compilewith);
804 if (!(ftp->f_flags & NO_CTFCONVERT))
805 fprintf(f, "\t${NORMAL_CTFCONVERT}\n\n");
814 struct file_list *tp;
819 STAILQ_FOREACH(tp, &ftab, f_next)
821 len = strlen(tp->f_clean);
822 if (len + lpos > 72) {
826 fprintf(fp, "%s ", tp->f_clean);
840 *str = toupper(*str);