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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)mkmakefile.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.sbin/config/mkmakefile.c,v 1.51.2.3 2001/01/23 00:09:32 peter Exp $
35 * $DragonFly: src/usr.sbin/config/mkmakefile.c,v 1.3 2003/11/03 19:31:36 eirikn Exp $
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.
48 #include <sys/param.h>
51 #include "configvers.h"
53 #define next_word(fp, wd) \
54 { register char *word = get_word(fp); \
55 if (word == (char *)EOF) \
60 #define next_quoted_word(fp, wd) \
61 { register char *word = get_quoted_word(fp); \
62 if (word == (char *)EOF) \
68 static struct file_list *fcur;
70 static char *tail(char *);
71 static void do_clean(FILE *);
72 static void do_rules(FILE *);
73 static void do_sfiles(FILE *);
74 static void do_mfiles(FILE *);
75 static void do_cfiles(FILE *);
76 static void do_objs(FILE *);
77 static void do_before_depend(FILE *);
78 static int opteq(char *, char *);
79 static void read_files(void);
82 * Lookup a file, by name.
84 static struct file_list *
88 register struct file_list *fp;
90 for (fp = ftab ; fp != 0; fp = fp->f_next) {
91 if (eq(fp->f_fn, file))
98 * Lookup a file, by final component name.
100 static struct file_list *
104 register struct file_list *fp;
106 for (fp = ftab ; fp != 0; fp = fp->f_next) {
107 if (eq(tail(fp->f_fn), tail(file)))
114 * Make a new file list entry
116 static struct file_list *
119 register struct file_list *fp;
121 fp = (struct file_list *) malloc(sizeof *fp);
122 bzero(fp, sizeof *fp);
132 * Build the makefile from the skeleton
143 snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
144 ifp = fopen(line, "r");
146 snprintf(line, sizeof(line), "Makefile.%s", machinename);
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, "IDENT=");
157 fprintf(ofp, " -DGPROF");
160 printf("cpu type must be specified\n");
164 for (op = mkopt; op; op = op->op_next)
165 fprintf(ofp, "%s=%s\n", op->op_name, op->op_value);
167 fprintf(ofp, "DEBUG=-g\n");
169 fprintf(ofp, "PROF=-pg\n");
170 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
173 fprintf(ofp,"S=%s\n", srcdir);
174 while (fgets(line, BUFSIZ, ifp) != 0) {
176 fprintf(ofp, "%s", line);
179 if (eq(line, "%BEFORE_DEPEND\n"))
180 do_before_depend(ofp);
181 else if (eq(line, "%OBJS\n"))
183 else if (eq(line, "%MFILES\n"))
185 else if (eq(line, "%CFILES\n"))
187 else if (eq(line, "%SFILES\n"))
189 else if (eq(line, "%RULES\n"))
191 else if (eq(line, "%CLEAN\n"))
193 else if (strncmp(line, "%VERSREQ=", sizeof("%VERSREQ=") - 1) == 0) {
194 versreq = atoi(line + sizeof("%VERSREQ=") - 1);
195 if (versreq != CONFIGVERS) {
196 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
197 fprintf(stderr, "config version = %d, ", CONFIGVERS);
198 fprintf(stderr, "version required = %d\n\n", versreq);
199 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
200 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
201 fprintf(stderr, "before trying this again.\n\n");
202 fprintf(stderr, "If running the new config fails check your config\n");
203 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
204 fprintf(stderr, "changes in config syntax, or option/device naming\n");
205 fprintf(stderr, "conventions\n\n");
210 "Unknown %% construct in generic makefile: %s",
215 moveifchanged(path("Makefile.new"), path("Makefile"));
217 printf("Don't forget to do a ``make depend''\n");
221 * Read in the information about files used in making the system.
222 * Store it in the ftab linked list.
228 register struct file_list *tp, *pf;
229 register struct device *dp;
230 struct device *save_dp;
231 register struct opt *op;
232 char *wd, *this, *needs, *special, *depends, *clean, *warn;
233 char fname[MAXPATHLEN];
234 int nreqs, first = 1, configdep, isdup, std, filetype,
235 imp_rule, no_obj, before_depend, mandatory;
240 printf("no ident line specified\n");
243 (void) snprintf(fname, sizeof(fname), "../../conf/files");
245 fp = fopen(fname, "r");
250 * filename [ standard | mandatory | optional ] [ config-dependent ]
251 * [ dev* | profiling-routine ] [ no-obj ]
252 * [ compile-with "compile rule" [no-implicit-rule] ]
253 * [ dependency "dependency-list"] [ before-depend ]
254 * [ clean "file-list"] [ warning "text warning" ]
257 if (wd == (char *)EOF) {
261 (void) snprintf(fname, sizeof(fname),
262 "../../conf/files.%s", machinename);
263 fp = fopen(fname, "r");
266 (void) snprintf(fname, sizeof(fname),
267 "files.%s", machinename);
272 (void) snprintf(fname, sizeof(fname),
273 "files.%s", raisestr(ident));
274 fp = fopen(fname, "r");
284 while (((wd = get_word(fp)) != (char *)EOF) && wd)
291 printf("%s: No type for %s.\n",
295 if ((pf = fl_lookup(this)) && (pf->f_type != INVISIBLE || pf->f_flags))
300 if (first == 3 && pf == 0 && (tp = fltail_lookup(this)) != 0) {
301 if (tp->f_type != INVISIBLE || tp->f_flags)
302 printf("%s: Local file %s overrides %s.\n",
303 fname, this, tp->f_fn);
305 printf("%s: Local file %s could override %s"
306 " with a different kernel configuration.\n",
307 fname, this, tp->f_fn);
321 if (eq(wd, "standard"))
324 * If an entry is marked "mandatory", config will abort if it's
325 * not called by a configuration line in the config file. Apart
326 * from this, the device is handled like one marked "optional".
328 else if (eq(wd, "mandatory"))
330 else if (!eq(wd, "optional")) {
331 printf("%s: %s must be optional, mandatory or standard\n",
333 printf("Your version of config(8) is out of sync with your kernel source.\n");
340 if (eq(wd, "config-dependent")) {
344 if (eq(wd, "no-obj")) {
348 if (eq(wd, "no-implicit-rule")) {
350 printf("%s: alternate rule required when "
351 "\"no-implicit-rule\" is specified.\n",
357 if (eq(wd, "before-depend")) {
361 if (eq(wd, "dependency")) {
362 next_quoted_word(fp, wd);
364 printf("%s: %s missing compile command string.\n",
371 if (eq(wd, "clean")) {
372 next_quoted_word(fp, wd);
374 printf("%s: %s missing clean file list.\n",
381 if (eq(wd, "compile-with")) {
382 next_quoted_word(fp, wd);
384 printf("%s: %s missing compile command string.\n",
391 if (eq(wd, "warning")) {
392 next_quoted_word(fp, wd);
394 printf("%s: %s missing warning text string.\n",
402 if (eq(wd, "local")) {
406 if (eq(wd, "no-depend")) {
410 if (eq(wd, "device-driver")) {
411 printf("%s: `device-driver' flag obsolete.\n", fname);
414 if (eq(wd, "profiling-routine")) {
415 filetype = PROFILING;
418 if (needs == 0 && nreqs == 1)
422 for (dp = dtab; dp != 0; save_dp = dp, dp = dp->d_next)
423 if (eq(dp->d_name, wd)) {
424 if (std && dp->d_type == PSEUDO_DEVICE &&
430 printf("%s: mandatory device \"%s\" not found\n",
435 dp = (struct device *) malloc(sizeof *dp);
436 bzero(dp, sizeof *dp);
439 dp->d_type = PSEUDO_DEVICE;
441 save_dp->d_next = dp;
444 for (op = opt; op != 0; op = op->op_next)
445 if (op->op_value == 0 && opteq(op->op_name, wd)) {
453 while ((wd = get_word(fp)) != 0)
458 tp->f_type = INVISIBLE;
461 tp->f_special = special;
462 tp->f_depends = depends;
468 if (std == 0 && nreqs == 0) {
469 printf("%s: what is %s optional on?\n",
475 printf("%s: syntax error describing %s\n",
479 if (filetype == PROFILING && profiling == 0)
484 tp->f_type = filetype;
487 tp->f_flags |= CONFIGDEP;
489 tp->f_flags |= NO_IMPLCT_RULE;
491 tp->f_flags |= NO_OBJ;
493 tp->f_flags |= BEFORE_DEPEND;
495 tp->f_flags |= NO_IMPLCT_RULE;
497 tp->f_flags |= NO_OBJ;
499 tp->f_special = special;
500 tp->f_depends = depends;
503 if (pf && pf->f_type == INVISIBLE)
504 pf->f_flags = 1; /* mark as duplicate */
514 for (; ; cp++, dp++) {
516 c = isupper(*cp) ? tolower(*cp) : *cp;
517 d = isupper(*dp) ? tolower(*dp) : *dp;
530 register struct file_list *tp;
531 register int lpos, len;
533 fputs("BEFORE_DEPEND=", fp);
535 for (tp = ftab; tp; tp = tp->f_next)
536 if (tp->f_flags & BEFORE_DEPEND) {
537 len = strlen(tp->f_fn);
538 if ((len = 3 + len) + lpos > 72) {
542 if (tp->f_flags & NO_IMPLCT_RULE)
543 fprintf(fp, "%s ", tp->f_fn);
545 fprintf(fp, "$S/%s ", tp->f_fn);
556 register struct file_list *tp;
557 register int lpos, len;
558 register char *cp, och, *sp;
560 fprintf(fp, "OBJS=");
562 for (tp = ftab; tp != 0; tp = tp->f_next) {
563 if (tp->f_type == INVISIBLE || tp->f_flags & NO_OBJ)
566 cp = sp + (len = strlen(sp)) - 1;
569 if (len + lpos > 72) {
571 fprintf(fp, "\\\n\t");
573 fprintf(fp, "%s ", sp);
585 register struct file_list *tp;
586 register int lpos, len;
588 fputs("CFILES=", fp);
590 for (tp = ftab; tp; tp = tp->f_next)
591 if (tp->f_type != INVISIBLE && tp->f_type != NODEPEND) {
592 len = strlen(tp->f_fn);
593 if (tp->f_fn[len - 1] != 'c')
595 if ((len = 3 + len) + lpos > 72) {
599 if (tp->f_type != LOCAL)
600 fprintf(fp, "$S/%s ", tp->f_fn);
602 fprintf(fp, "%s ", tp->f_fn);
614 register struct file_list *tp;
615 register int lpos, len;
617 fputs("MFILES=", fp);
619 for (tp = ftab; tp; tp = tp->f_next)
620 if (tp->f_type != INVISIBLE) {
621 len = strlen(tp->f_fn);
622 if (tp->f_fn[len - 1] != 'm' || tp->f_fn[len - 2] != '.')
624 if ((len = 3 + len) + lpos > 72) {
628 fprintf(fp, "$S/%s ", tp->f_fn);
639 register struct file_list *tp;
640 register int lpos, len;
642 fputs("SFILES=", fp);
644 for (tp = ftab; tp; tp = tp->f_next)
645 if (tp->f_type != INVISIBLE) {
646 len = strlen(tp->f_fn);
647 if (tp->f_fn[len - 1] != 'S' && tp->f_fn[len - 1] != 's')
649 if ((len = 3 + len) + lpos > 72) {
653 fprintf(fp, "$S/%s ", tp->f_fn);
667 cp = rindex(fn, '/');
674 * Create the makerules for each file
675 * which is part of the system.
676 * Devices are processed with the special c2 option -i
677 * which avoids any problem areas with i/o addressing
678 * (e.g. for the VAX); assembler files are processed by as.
684 register char *cp, *np, och, *tp;
685 register struct file_list *ftp;
688 for (ftp = ftab; ftp != 0; ftp = ftp->f_next) {
689 if (ftp->f_type == INVISIBLE)
692 printf("WARNING: %s\n", ftp->f_warn);
693 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
695 if (ftp->f_flags & NO_IMPLCT_RULE) {
697 fprintf(f, "%s: %s\n", np, ftp->f_depends);
699 fprintf(f, "%s: \n", np);
704 fprintf(f, "%so:\n\t-cp $S/%so .\n\n",
709 fprintf(f, "%so: $S/%s%c %s\n", tail(np),
710 np, och, ftp->f_depends);
712 fprintf(f, "%so: $S/%s%c\n", tail(np),
716 special = ftp->f_special;
719 static char cmd[128];
721 switch (ftp->f_type) {
734 printf("config: don't know rules for %s\n", np);
737 (void)snprintf(cmd, sizeof(cmd), "${%s_%c%s}", ftype, toupper(och),
738 ftp->f_flags & CONFIGDEP? "_C" : "");
742 fprintf(f, "\t%s\n\n", special);
750 register struct file_list *tp;
751 register int lpos, len;
755 for (tp = ftab; tp; tp = tp->f_next)
757 len = strlen(tp->f_clean);
758 if (len + lpos > 72) {
762 fprintf(fp, "%s ", tp->f_clean);
773 register char *cp = str;
777 *str = toupper(*str);