2 * Copyright (c) 1980, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)mkmakefile.c 8.1 (Berkeley) 6/6/93";
34 static const char rcsid[] =
39 * Build the makefile for the system, from
40 * the information in the files files and the
41 * 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);
65 static void sanitize_envline(char *result, const char *src);
66 static bool preprocess(char *line, char *result);
67 static void process_into_file(char *line, FILE *ofp);
68 static void process_into_nvlist(char *line, nvlist_t *nvl);
69 static void dump_nvlist(nvlist_t *nvl, FILE *ofp);
71 static void errout(const char *fmt, ...)
76 vfprintf(stderr, fmt, ap);
82 * Lookup a file, by name.
84 static struct file_list *
89 STAILQ_FOREACH(fp, &ftab, f_next) {
90 if (eq(fp->f_fn, file))
97 * Make a new file list entry
99 static struct file_list *
102 struct file_list *fp;
104 fp = (struct file_list *) calloc(1, sizeof *fp);
106 err(EXIT_FAILURE, "calloc");
107 STAILQ_INSERT_TAIL(&ftab, fp, f_next);
112 * Open the correct Makefile and return it, or error out.
115 open_makefile_template(void)
120 snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
121 ifp = fopen(line, "r");
123 snprintf(line, sizeof(line), "Makefile.%s", machinename);
124 ifp = fopen(line, "r");
132 * Build the makefile from the skeleton
142 ifp = open_makefile_template();
143 ofp = fopen(path("Makefile.new"), "w");
145 err(1, "%s", path("Makefile.new"));
146 fprintf(ofp, "KERN_IDENT=%s\n", ident);
147 fprintf(ofp, "MACHINE=%s\n", machinename);
148 fprintf(ofp, "MACHINE_ARCH=%s\n", machinearch);
149 SLIST_FOREACH_SAFE(op, &mkopt, op_next, t) {
150 fprintf(ofp, "%s=%s", op->op_name, op->op_value);
151 while ((op = SLIST_NEXT(op, op_append)) != NULL)
152 fprintf(ofp, " %s", op->op_value);
156 fprintf(ofp, "DEBUG=-g\n");
158 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
160 fprintf(ofp,"S=%s\n", srcdir);
161 while (fgets(line, BUFSIZ, ifp) != NULL) {
163 fprintf(ofp, "%s", line);
166 if (eq(line, "%BEFORE_DEPEND\n"))
167 do_before_depend(ofp);
168 else if (eq(line, "%OBJS\n"))
170 else if (strncmp(line, "%FILES.", 7) == 0)
171 do_xxfiles(line, ofp);
172 else if (eq(line, "%RULES\n"))
174 else if (eq(line, "%CLEAN\n"))
176 else if (strncmp(line, "%VERSREQ=", 9) == 0)
177 line[0] = '\0'; /* handled elsewhere */
180 "Unknown %% construct in generic makefile: %s",
185 moveifchanged(path("Makefile.new"), path("Makefile"));
189 sanitize_envline(char *result, const char *src)
195 /* If there is no '=' it's not a well-formed name=value line. */
196 if ((eq = strchr(src, '=')) == NULL) {
202 /* Copy chars before the '=', skipping any leading spaces/quotes. */
206 if (leading && (isspace(c) || c == '"'))
212 /* If it was all leading space, we don't have a well-formed line. */
218 /* Trim spaces/quotes immediately before the '=', then copy the '='. */
219 while (isspace(dst[-1]) || dst[-1] == '"')
223 /* Copy chars after the '=', skipping any leading whitespace. */
225 while ((c = *src++) != 0) {
226 if (leading && (isspace(c) || c == '"'))
232 /* If it was all leading space, it's a valid 'var=' (nil value). */
238 /* Trim trailing whitespace and quotes. */
239 while (isspace(dst[-1]) || dst[-1] == '"')
246 * Returns true if the caller may use the string.
249 preprocess(char *line, char *result)
253 /* Strip any comments */
254 if ((s = strchr(line, '#')) != NULL)
256 sanitize_envline(result, line);
257 /* Return true if it's non-empty */
258 return (*result != '\0');
262 process_into_file(char *line, FILE *ofp)
266 if (preprocess(line, result))
267 fprintf(ofp, "\"%s\\0\"\n", result);
271 process_into_nvlist(char *line, nvlist_t *nvl)
273 char result[BUFSIZ], *s;
275 if (preprocess(line, result)) {
276 s = strchr(result, '=');
278 if (nvlist_exists(nvl, result))
279 nvlist_free(nvl, result);
280 nvlist_add_string(nvl, result, s + 1);
285 dump_nvlist(nvlist_t *nvl, FILE *ofp)
293 while (!nvlist_empty(nvl)) {
295 name = nvlist_next(nvl, NULL, &cookie);
296 fprintf(ofp, "\"%s=%s\\0\"\n", name,
297 cnvlist_get_string(cookie));
299 cnvlist_free_string(cookie);
304 * Build hints.c from the skeleton
314 ofp = fopen(path("hints.c.new"), "w");
316 err(1, "%s", path("hints.c.new"));
317 fprintf(ofp, "#include <sys/types.h>\n");
318 fprintf(ofp, "#include <sys/systm.h>\n");
321 * Write out hintmode for older kernels. Remove when config(8) major
322 * version rolls over.
324 if (versreq <= CONFIGVERS_ENVMODE_REQ)
325 fprintf(ofp, "int hintmode = %d;\n",
326 !STAILQ_EMPTY(&hints) ? 1 : 0);
327 fprintf(ofp, "char static_hints[] = {\n");
328 nvl = nvlist_create(0);
329 STAILQ_FOREACH(hint, &hints, hint_next) {
330 ifp = fopen(hint->hint_name, "r");
332 err(1, "%s", hint->hint_name);
333 while (fgets(line, BUFSIZ, ifp) != NULL)
334 process_into_nvlist(line, nvl);
335 dump_nvlist(nvl, ofp);
339 fprintf(ofp, "\"\\0\"\n};\n");
341 moveifchanged(path("hints.c.new"), path("hints.c"));
345 * Build env.c from the skeleton
353 struct envvar *envvar;
355 ofp = fopen(path("env.c.new"), "w");
357 err(1, "%s", path("env.c.new"));
358 fprintf(ofp, "#include <sys/types.h>\n");
359 fprintf(ofp, "#include <sys/systm.h>\n");
362 * Write out envmode for older kernels. Remove when config(8) major
363 * version rolls over.
365 if (versreq <= CONFIGVERS_ENVMODE_REQ)
366 fprintf(ofp, "int envmode = %d;\n",
367 !STAILQ_EMPTY(&envvars) ? 1 : 0);
368 fprintf(ofp, "char static_env[] = {\n");
369 nvl = nvlist_create(0);
370 STAILQ_FOREACH(envvar, &envvars, envvar_next) {
371 if (envvar->env_is_file) {
372 ifp = fopen(envvar->env_str, "r");
374 err(1, "%s", envvar->env_str);
375 while (fgets(line, BUFSIZ, ifp) != NULL)
376 process_into_nvlist(line, nvl);
377 dump_nvlist(nvl, ofp);
380 process_into_file(envvar->env_str, ofp);
383 fprintf(ofp, "\"\\0\"\n};\n");
385 moveifchanged(path("env.c.new"), path("env.c"));
389 read_file(char *fname)
391 char ifname[MAXPATHLEN];
393 struct file_list *tp;
396 char *wd, *this, *compilewith, *depends, *clean, *warning;
397 const char *objprefix;
398 int compile, match, nreqs, std, filetype, not,
399 imp_rule, no_obj, before_depend, nowerror;
401 fp = fopen(fname, "r");
407 * filename [ standard | optional ]
408 * [ dev* [ | dev* ... ] | profiling-routine ] [ 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"]
415 if (wd == (char *)EOF) {
423 while (((wd = get_word(fp)) != (char *)EOF) && wd)
427 if (eq(wd, "include")) {
428 wd = get_quoted_word(fp);
429 if (wd == (char *)EOF || wd == NULL)
430 errout("%s: missing include filename.\n", fname);
431 (void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
433 while (((wd = get_word(fp)) != (char *)EOF) && wd)
439 if (wd == (char *)EOF)
442 errout("%s: No type for %s.\n", fname, this);
443 tp = fl_lookup(this);
459 if (eq(wd, "standard"))
461 else if (!eq(wd, "optional"))
462 errout("%s: \"%s\" %s must be optional or standard\n",
464 for (wd = get_word(fp); wd; wd = get_word(fp)) {
465 if (wd == (char *)EOF)
473 errout("%s: syntax error describing %s\n",
480 if (eq(wd, "no-obj")) {
484 if (eq(wd, "no-implicit-rule")) {
485 if (compilewith == NULL)
486 errout("%s: alternate rule required when "
487 "\"no-implicit-rule\" is specified for"
493 if (eq(wd, "before-depend")) {
497 if (eq(wd, "dependency")) {
498 wd = get_quoted_word(fp);
499 if (wd == (char *)EOF || wd == NULL)
500 errout("%s: %s missing dependency string.\n",
505 if (eq(wd, "clean")) {
506 wd = get_quoted_word(fp);
507 if (wd == (char *)EOF || wd == NULL)
508 errout("%s: %s missing clean file list.\n",
513 if (eq(wd, "compile-with")) {
514 wd = get_quoted_word(fp);
515 if (wd == (char *)EOF || wd == NULL)
516 errout("%s: %s missing compile command string.\n",
518 compilewith = ns(wd);
521 if (eq(wd, "warning")) {
522 wd = get_quoted_word(fp);
523 if (wd == (char *)EOF || wd == NULL)
524 errout("%s: %s missing warning text string.\n",
529 if (eq(wd, "obj-prefix")) {
530 wd = get_quoted_word(fp);
531 if (wd == (char *)EOF || wd == NULL)
532 errout("%s: %s missing object prefix string.\n",
537 if (eq(wd, "nowerror")) {
541 if (eq(wd, "local")) {
545 if (eq(wd, "no-depend")) {
550 if (eq(wd, "profiling-routine")) {
551 filetype = PROFILING;
555 errout("standard entry %s has optional inclusion specifier %s!\n",
557 STAILQ_FOREACH(dp, &dtab, d_next)
558 if (eq(dp->d_name, wd)) {
562 dp->d_done |= DEVDONE;
565 SLIST_FOREACH(op, &opt, op_next)
566 if (op->op_value == 0 && opteq(op->op_name, wd)) {
576 if (compile && tp == NULL) {
577 if (std == 0 && nreqs == 0)
578 errout("%s: what is %s optional on?\n",
580 if (filetype == PROFILING && profiling == 0)
584 tp->f_type = filetype;
585 if (filetype == LOCAL)
586 tp->f_srcprefix = "";
588 tp->f_srcprefix = "$S/";
590 tp->f_flags |= NO_IMPLCT_RULE;
592 tp->f_flags |= NO_OBJ;
594 tp->f_flags |= BEFORE_DEPEND;
596 tp->f_flags |= NOWERROR;
597 tp->f_compilewith = compilewith;
598 tp->f_depends = depends;
600 tp->f_warn = warning;
601 tp->f_objprefix = objprefix;
607 * Read in the information about files used in making the system.
608 * Store it in the ftab linked list.
613 char fname[MAXPATHLEN];
614 struct files_name *nl, *tnl;
616 (void) snprintf(fname, sizeof(fname), "../../conf/files");
618 (void) snprintf(fname, sizeof(fname),
619 "../../conf/files.%s", machinename);
621 for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
622 read_file(nl->f_name);
623 tnl = STAILQ_NEXT(nl, f_next);
630 opteq(const char *cp, const char *dp)
634 for (; ; cp++, dp++) {
636 c = isupper(*cp) ? tolower(*cp) : *cp;
637 d = isupper(*dp) ? tolower(*dp) : *dp;
647 do_before_depend(FILE *fp)
649 struct file_list *tp;
652 fputs("BEFORE_DEPEND=", fp);
654 STAILQ_FOREACH(tp, &ftab, f_next)
655 if (tp->f_flags & BEFORE_DEPEND) {
656 len = strlen(tp->f_fn);
657 if ((len = 3 + len) + lpos > 72) {
661 if (tp->f_flags & NO_IMPLCT_RULE)
662 fprintf(fp, "%s ", tp->f_fn);
664 fprintf(fp, "%s%s ", tp->f_srcprefix,
675 struct file_list *tp;
679 fprintf(fp, "OBJS=");
681 STAILQ_FOREACH(tp, &ftab, f_next) {
682 if (tp->f_flags & NO_OBJ)
685 cp = sp + (len = strlen(sp)) - 1;
688 len += strlen(tp->f_objprefix);
689 if (len + lpos > 72) {
691 fprintf(fp, "\\\n\t");
693 fprintf(fp, "%s%s ", tp->f_objprefix, sp);
702 do_xxfiles(char *tag, FILE *fp)
704 struct file_list *tp;
708 if (tag[strlen(tag) - 1] == '\n')
709 tag[strlen(tag) - 1] = '\0';
716 fprintf(fp, "%sFILES=", SUFF);
719 STAILQ_FOREACH(tp, &ftab, f_next)
720 if (tp->f_type != NODEPEND) {
721 len = strlen(tp->f_fn);
722 if (tp->f_fn[len - slen - 1] != '.')
724 if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
726 if ((len = 3 + len) + lpos > 72) {
730 fprintf(fp, "%s%s ", tp->f_srcprefix, tp->f_fn);
743 cp = strrchr(fn, '/');
750 * Create the makerules for each file
751 * which is part of the system.
757 struct file_list *ftp;
761 STAILQ_FOREACH(ftp, &ftab, f_next) {
763 fprintf(stderr, "WARNING: %s\n", ftp->f_warn);
764 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
766 if (ftp->f_flags & NO_IMPLCT_RULE) {
768 fprintf(f, "%s%s: %s\n",
769 ftp->f_objprefix, np, ftp->f_depends);
771 fprintf(f, "%s%s: \n", ftp->f_objprefix, np);
776 fprintf(f, "%s%so:\n\t-cp %s%so .\n\n",
777 ftp->f_objprefix, tail(np),
778 ftp->f_srcprefix, np);
781 if (ftp->f_depends) {
782 fprintf(f, "%s%sln: %s%s%c %s\n",
783 ftp->f_objprefix, tail(np),
784 ftp->f_srcprefix, np, och,
786 fprintf(f, "\t${NORMAL_LINT}\n\n");
787 fprintf(f, "%s%so: %s%s%c %s\n",
788 ftp->f_objprefix, tail(np),
789 ftp->f_srcprefix, np, och,
793 fprintf(f, "%s%sln: %s%s%c\n",
794 ftp->f_objprefix, tail(np),
795 ftp->f_srcprefix, np, och);
796 fprintf(f, "\t${NORMAL_LINT}\n\n");
797 fprintf(f, "%s%so: %s%s%c\n",
798 ftp->f_objprefix, tail(np),
799 ftp->f_srcprefix, np, och);
802 compilewith = ftp->f_compilewith;
803 if (compilewith == NULL) {
804 const char *ftype = NULL;
806 switch (ftp->f_type) {
817 "config: don't know rules for %s\n", np);
820 snprintf(cmd, sizeof(cmd),
823 ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
827 if (strlen(ftp->f_objprefix))
828 fprintf(f, "\t%s %s%s\n", compilewith,
829 ftp->f_srcprefix, np);
831 fprintf(f, "\t%s\n", compilewith);
833 if (!(ftp->f_flags & NO_OBJ))
834 fprintf(f, "\t${NORMAL_CTFCONVERT}\n\n");
843 struct file_list *tp;
848 STAILQ_FOREACH(tp, &ftab, f_next)
850 len = strlen(tp->f_clean);
851 if (len + lpos > 72) {
855 fprintf(fp, "%s ", tp->f_clean);
869 *str = toupper(*str);