2 * Copyright (c) 1993, 19801990
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
29 * @(#)mkmakefile.c 8.1 (Berkeley) 6/6/93
30 * $FreeBSD: src/usr.sbin/config/mkmakefile.c,v 1.51.2.3 2001/01/23 00:09:32 peter Exp $
34 * Build the makefile for the system, from
35 * the information in the 'files' files and the
36 * additional files for the machine being compiled to.
43 #include <sys/param.h>
46 #include "configvers.h"
48 #define next_word(fp, wd) \
52 word = get_word(fp); \
53 if (word == (char *)EOF) \
58 #define next_quoted_word(fp, wd) \
62 word = get_quoted_word(fp); \
63 if (word == (char *)EOF) \
69 static struct file_list *fcur;
71 static char *tail(char *);
72 static void do_clean(FILE *);
73 static void do_rules(FILE *);
74 static void do_sfiles(FILE *);
75 static void do_mfiles(FILE *);
76 static void do_cfiles(FILE *);
77 static void do_objs(FILE *);
78 static void do_before_depend(FILE *);
79 static int opteq(char *, char *);
80 static void read_files(void);
83 * Lookup a file, by name.
85 static struct file_list *
90 for (fp = ftab; fp != NULL; fp = fp->f_next) {
91 if (strcmp(fp->f_fn, file) == 0)
98 * Lookup a file, by final component name.
100 static struct file_list *
101 fltail_lookup(char *file)
103 struct file_list *fp;
105 for (fp = ftab; fp != NULL; fp = fp->f_next) {
106 if (strcmp(tail(fp->f_fn), tail(file)) == 0)
113 * Make a new file list entry
115 static struct file_list *
118 struct file_list *fp;
120 fp = malloc(sizeof(*fp));
121 bzero(fp, sizeof(*fp));
131 * Build the makefile from the skeleton
142 snprintf(line, sizeof(line), "../platform/%s/conf/Makefile",
144 ifp = fopen(line, "r");
146 snprintf(line, sizeof(line), "Makefile.%s", platformname);
147 ifp = fopen(line, "r");
151 ofp = fopen(path("Makefile.new"), "w");
153 err(1, "%s", path("Makefile.new"));
154 fprintf(ofp, "KERN_IDENT=%s\n", raisestr(ident));
155 fprintf(ofp, "MACHINE_PLATFORM=%s\n", platformname);
156 fprintf(ofp, "MACHINE=%s\n", machinename);
157 fprintf(ofp, "MACHINE_ARCH=%s\n", machinearchname);
158 fprintf(ofp, ".if defined(.PARSEDIR)\n");
159 fprintf(ofp, ".export MACHINE_PLATFORM\n");
160 fprintf(ofp, ".export MACHINE\n");
161 fprintf(ofp, ".export MACHINE_ARCH\n");
162 fprintf(ofp, ".else\n");
163 fprintf(ofp, ".makeenv MACHINE_PLATFORM\n");
164 fprintf(ofp, ".makeenv MACHINE\n");
165 fprintf(ofp, ".makeenv MACHINE_ARCH\n");
166 fprintf(ofp, ".endif\n");
167 fprintf(ofp, "IDENT=");
169 /* Don't compile kernel profiling code for vkernel */
170 if (strncmp(platformname, "vkernel", 6) != 0)
171 fprintf(ofp, " -DGPROF");
175 printf("cpu type must be specified\n");
179 for (op = mkopt; op != NULL; op = op->op_next)
180 fprintf(ofp, "%s=%s\n", op->op_name, op->op_value);
182 fprintf(ofp, "DEBUG=-g\n");
184 fprintf(ofp, "PROF=-pg\n");
185 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
188 fprintf(ofp,"S=%s\n", srcdir);
189 while (fgets(line, BUFSIZ, ifp) != 0) {
191 fprintf(ofp, "%s", line);
194 if (strcmp(line, "%BEFORE_DEPEND\n") == 0)
195 do_before_depend(ofp);
196 else if (strcmp(line, "%OBJS\n") == 0)
198 else if (strcmp(line, "%MFILES\n") == 0)
200 else if (strcmp(line, "%CFILES\n") == 0)
202 else if (strcmp(line, "%SFILES\n") == 0)
204 else if (strcmp(line, "%RULES\n") == 0)
206 else if (strcmp(line, "%CLEAN\n") == 0)
208 else if (strncmp(line, "%VERSREQ=", sizeof("%VERSREQ=") - 1) == 0) {
209 versreq = atoi(line + sizeof("%VERSREQ=") - 1);
210 if (versreq != CONFIGVERS) {
211 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
212 fprintf(stderr, "config version = %d, ", CONFIGVERS);
213 fprintf(stderr, "version required = %d\n\n", versreq);
214 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
215 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
216 fprintf(stderr, "before trying this again.\n\n");
217 fprintf(stderr, "If running the new config fails check your config\n");
218 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
219 fprintf(stderr, "changes in config syntax, or option/device naming\n");
220 fprintf(stderr, "conventions\n\n");
225 "Unknown %% construct in generic makefile: %s",
230 moveifchanged(path("Makefile.new"), path("Makefile"));
234 * Read in the information about files used in making the system.
235 * Store it in the ftab linked list.
241 struct file_list *tp, *pf;
243 struct device *save_dp;
245 char *wd, *this, *needs, *special, *depends, *clean, *warning;
246 char fname[MAXPATHLEN];
248 int nreqs, first = 1, configdep, isdup, std, filetype,
249 imp_rule, no_obj, before_depend, nowerror, mandatory;
254 printf("no ident line specified\n");
257 snprintf(fname, sizeof(fname), "../conf/files");
259 fp = fopen(fname, "r");
264 * filename [ standard | mandatory | optional ] [ config-dependent ]
265 * [ dev* | profiling-routine ] [ no-obj ]
266 * [ compile-with "compile rule" [no-implicit-rule] ]
267 * [ dependency "dependency-list"] [ before-depend ]
268 * [ clean "file-list"] [ warning "text warning" ]
271 if (wd == (char *)EOF) {
275 snprintf(fname, sizeof(fname),
276 "../platform/%s/conf/files",
278 fp = fopen(fname, "r");
281 snprintf(fname, sizeof(fname),
282 "files.%s", platformname);
287 snprintf(fname, sizeof(fname),
288 "files.%s", raisestr(ident));
289 fp = fopen(fname, "r");
299 while (((wd = get_word(fp)) != (char *)EOF) && wd)
306 printf("%s: No type for %s.\n",
310 if ((pf = fl_lookup(this)) && (pf->f_type != INVISIBLE || pf->f_flags))
315 if (first == 3 && pf == NULL && (tp = fltail_lookup(this)) != NULL) {
316 if (tp->f_type != INVISIBLE || tp->f_flags)
317 printf("%s: Local file %s overrides %s.\n",
318 fname, this, tp->f_fn);
320 printf("%s: Local file %s could override %s"
321 " with a different kernel configuration.\n",
322 fname, this, tp->f_fn);
331 std = mandatory = nonoptional = 0;
337 if (strcmp(wd, "standard") == 0) {
339 } else if (strcmp(wd, "mandatory") == 0) {
341 * If an entry is marked "mandatory", config will abort if
342 * it's not called by a configuration line in the config
343 * file. Apart from this, the device is handled like one
347 } else if (strcmp(wd, "nonoptional") == 0) {
349 } else if (strcmp(wd, "optional") == 0) {
350 /* don't need to do anything */
352 printf("%s: %s must be optional, mandatory or standard\n",
354 printf("Alternatively, your version of config(8) may be out of sync with your\nkernel source.\n");
361 if (strcmp(wd, "config-dependent") == 0) {
365 if (strcmp(wd, "no-obj") == 0) {
369 if (strcmp(wd, "no-implicit-rule") == 0) {
370 if (special == NULL) {
371 printf("%s: alternate rule required when "
372 "\"no-implicit-rule\" is specified.\n",
378 if (strcmp(wd, "before-depend") == 0) {
382 if (strcmp(wd, "dependency") == 0) {
383 next_quoted_word(fp, wd);
385 printf("%s: %s missing compile command string.\n",
389 depends = strdup(wd);
392 if (strcmp(wd, "clean") == 0) {
393 next_quoted_word(fp, wd);
395 printf("%s: %s missing clean file list.\n",
402 if (strcmp(wd, "compile-with") == 0) {
403 next_quoted_word(fp, wd);
405 printf("%s: %s missing compile command string.\n",
409 special = strdup(wd);
412 if (strcmp(wd, "nowerror") == 0) {
416 if (strcmp(wd, "warning") == 0) {
417 next_quoted_word(fp, wd);
419 printf("%s: %s missing warning text string.\n",
423 warning = strdup(wd);
427 if (strcmp(wd, "local") == 0) {
431 if (strcmp(wd, "no-depend") == 0) {
435 if (strcmp(wd, "device-driver") == 0) {
436 printf("%s: `device-driver' flag obsolete.\n", fname);
439 if (strcmp(wd, "profiling-routine") == 0) {
440 filetype = PROFILING;
443 if (needs == NULL && nreqs == 1)
447 for (dp = dtab; dp != NULL; save_dp = dp, dp = dp->d_next)
448 if (strcmp(dp->d_name, wd) == 0) {
449 if (std && dp->d_type == PSEUDO_DEVICE &&
455 printf("%s: mandatory device \"%s\" not found\n",
460 dp = malloc(sizeof(*dp));
461 bzero(dp, sizeof(*dp));
463 dp->d_name = strdup(wd);
464 dp->d_type = PSEUDO_DEVICE;
466 save_dp->d_next = dp;
469 for (op = opt; op != NULL; op = op->op_next) {
470 if (op->op_value == 0 && opteq(op->op_name, wd)) {
479 printf("%s: the option \"%s\" MUST be specified\n",
484 while ((wd = get_word(fp)) != NULL)
489 tp->f_type = INVISIBLE;
492 tp->f_special = special;
493 tp->f_depends = depends;
495 tp->f_warn = warning;
499 if (std == 0 && nreqs == 0) {
500 printf("%s: what is %s optional on?\n",
506 printf("%s: syntax error describing %s\n",
510 if (filetype == PROFILING && profiling == 0)
515 tp->f_type = filetype;
518 tp->f_flags |= CONFIGDEP;
520 tp->f_flags |= NO_IMPLCT_RULE;
522 tp->f_flags |= NO_OBJ;
524 tp->f_flags |= BEFORE_DEPEND;
526 tp->f_flags |= NOWERROR;
528 tp->f_flags |= NO_IMPLCT_RULE;
530 tp->f_flags |= NO_OBJ;
532 tp->f_special = special;
533 tp->f_depends = depends;
535 tp->f_warn = warning;
536 if (pf && pf->f_type == INVISIBLE)
537 pf->f_flags = 1; /* mark as duplicate */
542 opteq(char *cp, char *dp)
546 for (;; cp++, dp++) {
548 c = isupper(*cp) ? tolower(*cp) : *cp;
549 d = isupper(*dp) ? tolower(*dp) : *dp;
559 do_before_depend(FILE *fp)
561 struct file_list *tp;
564 fputs("BEFORE_DEPEND=", fp);
566 for (tp = ftab; tp != NULL; tp = tp->f_next)
567 if (tp->f_flags & BEFORE_DEPEND) {
568 len = strlen(tp->f_fn);
569 if ((len = 3 + len) + lpos > 72) {
573 if (tp->f_flags & NO_IMPLCT_RULE)
574 fprintf(fp, "%s ", tp->f_fn);
576 fprintf(fp, "$S/%s ", tp->f_fn);
586 struct file_list *tp;
590 fprintf(fp, "OBJS=");
592 for (tp = ftab; tp != NULL; tp = tp->f_next) {
593 if (tp->f_type == INVISIBLE || tp->f_flags & NO_OBJ)
596 cp = sp + (len = strlen(sp)) - 1;
599 if (len + lpos > 72) {
601 fprintf(fp, "\\\n\t");
603 fprintf(fp, "%s ", sp);
614 struct file_list *tp;
617 fputs("CFILES=", fp);
619 for (tp = ftab; tp != NULL; tp = tp->f_next)
620 if (tp->f_type != INVISIBLE && tp->f_type != NODEPEND) {
621 len = strlen(tp->f_fn);
622 if (tp->f_fn[len - 1] != 'c')
624 if ((len = 3 + len) + lpos > 72) {
628 if (tp->f_type != LOCAL)
629 fprintf(fp, "$S/%s ", tp->f_fn);
631 fprintf(fp, "%s ", tp->f_fn);
642 struct file_list *tp;
645 fputs("MFILES=", fp);
647 for (tp = ftab; tp != NULL; tp = tp->f_next)
648 if (tp->f_type != INVISIBLE) {
649 len = strlen(tp->f_fn);
650 if (tp->f_fn[len - 1] != 'm' || tp->f_fn[len - 2] != '.')
652 if ((len = 3 + len) + lpos > 72) {
656 fprintf(fp, "$S/%s ", tp->f_fn);
666 struct file_list *tp;
669 fputs("SFILES=", fp);
671 for (tp = ftab; tp != NULL; tp = tp->f_next)
672 if (tp->f_type != INVISIBLE) {
673 len = strlen(tp->f_fn);
674 if (tp->f_fn[len - 1] != 'S' && tp->f_fn[len - 1] != 's')
676 if ((len = 3 + len) + lpos > 72) {
680 fprintf(fp, "$S/%s ", tp->f_fn);
693 cp = strrchr(fn, '/');
700 * Create the makerules for each file
701 * which is part of the system.
702 * Devices are processed with the special c2 option -i
703 * which avoids any problem areas with i/o addressing
704 * (e.g. for the VAX); assembler files are processed by as.
710 struct file_list *ftp;
713 for (ftp = ftab; ftp != NULL; ftp = ftp->f_next) {
714 if (ftp->f_type == INVISIBLE)
716 if (ftp->f_warn != NULL)
717 printf("WARNING: %s\n", ftp->f_warn);
718 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
720 if (ftp->f_flags & NO_IMPLCT_RULE) {
722 fprintf(f, "%s: %s\n", np, ftp->f_depends);
724 fprintf(f, "%s: \n", np);
729 fprintf(f, "%so:\n\t-cp $S/%so .\n\n",
734 fprintf(f, "%so: $S/%s%c %s\n", tail(np),
735 np, och, ftp->f_depends);
737 fprintf(f, "%so: $S/%s%c\n", tail(np),
740 special = ftp->f_special;
741 if (special == NULL) {
742 const char *ftype = NULL;
743 static char cmd[128];
745 switch (ftp->f_type) {
758 printf("config: don't know rules for %s\n", np);
761 snprintf(cmd, sizeof(cmd), "${%s_%c%s}%s",
763 ftp->f_flags & CONFIGDEP ? "_C" : "",
764 ftp->f_flags & NOWERROR ? "" : " ${WERROR}");
768 fprintf(f, "\t%s\n\n", special);
775 struct file_list *tp;
780 for (tp = ftab; tp != NULL; tp = tp->f_next)
782 len = strlen(tp->f_clean);
783 if (len + lpos > 72) {
787 fprintf(fp, "%s ", tp->f_clean);
801 *str = toupper(*str);